|
1 | 1 | import Base from "./base"; |
| 2 | +import BasePattern from "./basepattern"; |
2 | 3 | import registry from "./registry"; |
3 | 4 |
|
4 | 5 | describe("pat-registry: The registry for patterns", function () { |
@@ -150,4 +151,185 @@ describe("pat-registry: The registry for patterns", function () { |
150 | 151 | expect(tree.textContent).toBe("initialized"); |
151 | 152 | }); |
152 | 153 |
|
| 154 | + describe("orderPatterns", function () { |
| 155 | + it("Orders patterns by their order property with lower values first", function () { |
| 156 | + // Create test patterns with different order values |
| 157 | + class Pattern1 extends BasePattern { |
| 158 | + static name = "pattern1"; |
| 159 | + static order = 500; |
| 160 | + } |
| 161 | + |
| 162 | + class Pattern2 extends BasePattern { |
| 163 | + static name = "pattern2"; |
| 164 | + static order = 100; |
| 165 | + } |
| 166 | + |
| 167 | + class Pattern3 extends BasePattern { |
| 168 | + static name = "pattern3"; |
| 169 | + static order = 300; |
| 170 | + } |
| 171 | + |
| 172 | + // Register patterns |
| 173 | + registry.register(Pattern1); |
| 174 | + registry.register(Pattern2); |
| 175 | + registry.register(Pattern3); |
| 176 | + |
| 177 | + const pattern_names = ["pattern1", "pattern2", "pattern3"]; |
| 178 | + const ordered_patterns = registry.orderPatterns(pattern_names); |
| 179 | + |
| 180 | + // Should be ordered by order property: pattern2 (100), pattern3 (300), pattern1 (500) |
| 181 | + expect(ordered_patterns).toEqual(["pattern2", "pattern3", "pattern1"]); |
| 182 | + }); |
| 183 | + |
| 184 | + it("Uses default order of 1000 for patterns without explicit order", function () { |
| 185 | + class PatternWithOrder extends BasePattern { |
| 186 | + static name = "pattern-with-order"; |
| 187 | + static order = 200; |
| 188 | + } |
| 189 | + |
| 190 | + class PatternWithoutOrder extends BasePattern { |
| 191 | + static name = "pattern-without-order"; |
| 192 | + // No order property, should use default 1000 |
| 193 | + } |
| 194 | + |
| 195 | + registry.register(PatternWithOrder); |
| 196 | + registry.register(PatternWithoutOrder); |
| 197 | + |
| 198 | + const pattern_names = ["pattern-without-order", "pattern-with-order"]; |
| 199 | + const ordered_patterns = registry.orderPatterns(pattern_names); |
| 200 | + |
| 201 | + // pattern-with-order (200) should come before pattern-without-order (1000) |
| 202 | + expect(ordered_patterns).toEqual(["pattern-with-order", "pattern-without-order"]); |
| 203 | + }); |
| 204 | + |
| 205 | + it("Handles patterns with same order value consistently", function () { |
| 206 | + class Pattern1 extends BasePattern { |
| 207 | + static name = "pattern1"; |
| 208 | + static order = 500; |
| 209 | + } |
| 210 | + |
| 211 | + class Pattern2 extends BasePattern { |
| 212 | + static name = "pattern2"; |
| 213 | + static order = 500; |
| 214 | + } |
| 215 | + |
| 216 | + registry.register(Pattern1); |
| 217 | + registry.register(Pattern2); |
| 218 | + |
| 219 | + const pattern_names = ["pattern2", "pattern1"]; |
| 220 | + const ordered_patterns = registry.orderPatterns(pattern_names); |
| 221 | + |
| 222 | + // Both have same order, should maintain stable sort |
| 223 | + expect(ordered_patterns).toEqual(["pattern2", "pattern1"]); |
| 224 | + }); |
| 225 | + |
| 226 | + it("Ignores non-existent patterns during ordering", function () { |
| 227 | + class ExistingPattern extends BasePattern { |
| 228 | + static name = "existing"; |
| 229 | + static order = 300; |
| 230 | + } |
| 231 | + |
| 232 | + registry.register(ExistingPattern); |
| 233 | + |
| 234 | + const pattern_names = ["non-existent", "existing", "another-non-existent"]; |
| 235 | + const ordered_patterns = registry.orderPatterns(pattern_names); |
| 236 | + |
| 237 | + // Only existing pattern should be returned |
| 238 | + expect(ordered_patterns).toEqual(["existing"]); |
| 239 | + }); |
| 240 | + |
| 241 | + it("Returns empty array when no valid patterns are provided", function () { |
| 242 | + const pattern_names = ["non-existent1", "non-existent2"]; |
| 243 | + const ordered_patterns = registry.orderPatterns(pattern_names); |
| 244 | + |
| 245 | + expect(ordered_patterns).toEqual([]); |
| 246 | + }); |
| 247 | + |
| 248 | + it("Does not modify the original patterns array", function () { |
| 249 | + class Pattern1 extends BasePattern { |
| 250 | + static name = "pattern1"; |
| 251 | + static order = 500; |
| 252 | + } |
| 253 | + |
| 254 | + class Pattern2 extends BasePattern { |
| 255 | + static name = "pattern2"; |
| 256 | + static order = 100; |
| 257 | + } |
| 258 | + |
| 259 | + registry.register(Pattern1); |
| 260 | + registry.register(Pattern2); |
| 261 | + |
| 262 | + const pattern_names = ["pattern1", "pattern2"]; |
| 263 | + const original_order = [...pattern_names]; |
| 264 | + |
| 265 | + const ordered_patterns = registry.orderPatterns(pattern_names); |
| 266 | + |
| 267 | + // Original array should be unchanged |
| 268 | + expect(pattern_names).toEqual(original_order); |
| 269 | + // But result should be sorted |
| 270 | + expect(ordered_patterns).toEqual(["pattern2", "pattern1"]); |
| 271 | + }); |
| 272 | + |
| 273 | + it("Validates expected order values for special patterns", function () { |
| 274 | + // Test the specific order values mentioned in the commit |
| 275 | + class ValidationPattern extends BasePattern { |
| 276 | + static name = "validation"; |
| 277 | + static order = 100; |
| 278 | + } |
| 279 | + |
| 280 | + class CloneCodePattern extends BasePattern { |
| 281 | + static name = "clone-code"; |
| 282 | + static order = 200; |
| 283 | + } |
| 284 | + |
| 285 | + class RegularPattern extends BasePattern { |
| 286 | + static name = "regular"; |
| 287 | + static order = 1000; |
| 288 | + } |
| 289 | + |
| 290 | + registry.register(ValidationPattern); |
| 291 | + registry.register(CloneCodePattern); |
| 292 | + registry.register(RegularPattern); |
| 293 | + |
| 294 | + const pattern_names = ["regular", "clone-code", "validation"]; |
| 295 | + const ordered_patterns = registry.orderPatterns(pattern_names); |
| 296 | + |
| 297 | + // Should be ordered: validation (100), clone-code (200), regular (1000) |
| 298 | + expect(ordered_patterns).toEqual(["validation", "clone-code", "regular"]); |
| 299 | + }); |
| 300 | + |
| 301 | + it("Works with mixed order values including edge cases", function () { |
| 302 | + class EarliestPattern extends BasePattern { |
| 303 | + static name = "earliest"; |
| 304 | + static order = 1; |
| 305 | + } |
| 306 | + |
| 307 | + class LatestPattern extends BasePattern { |
| 308 | + static name = "latest"; |
| 309 | + static order = 9999; |
| 310 | + } |
| 311 | + |
| 312 | + class NegativeOrderPattern extends BasePattern { |
| 313 | + static name = "negative"; |
| 314 | + static order = -50; |
| 315 | + } |
| 316 | + |
| 317 | + class DefaultPattern extends BasePattern { |
| 318 | + static name = "default"; |
| 319 | + // Uses default order 1000 |
| 320 | + } |
| 321 | + |
| 322 | + registry.register(EarliestPattern); |
| 323 | + registry.register(LatestPattern); |
| 324 | + registry.register(NegativeOrderPattern); |
| 325 | + registry.register(DefaultPattern); |
| 326 | + |
| 327 | + const pattern_names = ["latest", "default", "earliest", "negative"]; |
| 328 | + const ordered_patterns = registry.orderPatterns(pattern_names); |
| 329 | + |
| 330 | + // Should be ordered by order value: negative (-50), earliest (1), default (1000), latest (9999) |
| 331 | + expect(ordered_patterns).toEqual(["negative", "earliest", "default", "latest"]); |
| 332 | + }); |
| 333 | + }); |
| 334 | + |
153 | 335 | }); |
0 commit comments