| 1 | /* eslint-disable max-lines-per-function */ |
| 2 | import { ContextItemWithId, RuleWithSource, UserChatMessage } from "../.."; |
| 3 | import { |
| 4 | getSystemMessageWithRules, |
| 5 | shouldApplyRule, |
| 6 | } from "./getSystemMessageWithRules"; |
| 7 | |
| 8 | describe("getSystemMessageWithRules", () => { |
| 9 | const baseSystemMessage = "Base system message"; |
| 10 | const tsRule: RuleWithSource = { |
| 11 | name: "TypeScript Rule", |
| 12 | rule: "Follow TypeScript best practices", |
| 13 | globs: "**/*.ts?(x)", |
| 14 | source: "rules-block", |
| 15 | }; |
| 16 | |
| 17 | // Rule without pattern (always active) |
| 18 | const generalRule: RuleWithSource = { |
| 19 | name: "General Rule", |
| 20 | rule: "Always write clear comments", |
| 21 | source: "rules-block", |
| 22 | }; |
| 23 | |
| 24 | // Rule with a different pattern |
| 25 | const pythonRule: RuleWithSource = { |
| 26 | name: "Python Rule", |
| 27 | rule: "Follow PEP 8 guidelines", |
| 28 | globs: "**/*.py", |
| 29 | source: "rules-block", |
| 30 | }; |
| 31 | |
| 32 | // JavaScript rule |
| 33 | const jsRule: RuleWithSource = { |
| 34 | name: "JavaScript Rule", |
| 35 | rule: "Follow JavaScript best practices", |
| 36 | globs: "**/*.js", |
| 37 | source: "rules-block", |
| 38 | }; |
| 39 | |
| 40 | // Empty context items |
| 41 | const emptyContextItems: ContextItemWithId[] = []; |
| 42 | |
| 43 | // Context items with file paths |
| 44 | const tsContextItem: ContextItemWithId = { |
| 45 | content: "TypeScript file content", |
| 46 | name: "Component.tsx", |
| 47 | description: "A TypeScript component", |
| 48 | id: { providerTitle: "file", itemId: "src/Component.tsx" }, |
| 49 | uri: { type: "file", value: "src/Component.tsx" }, |
| 50 | }; |
| 51 | |
| 52 | const pyContextItem: ContextItemWithId = { |
| 53 | content: "Python file content", |
| 54 | name: "utils.py", |
| 55 | description: "A Python utility file", |
| 56 | id: { providerTitle: "file", itemId: "utils.py" }, |
| 57 | uri: { type: "file", value: "utils.py" }, |
| 58 | }; |
| 59 | |
| 60 | const jsContextItem: ContextItemWithId = { |
| 61 | content: "JavaScript file content", |
| 62 | name: "utils.js", |
| 63 | description: "A JavaScript utility file", |
| 64 | id: { providerTitle: "file", itemId: "src/utils.js" }, |
| 65 | uri: { type: "file", value: "src/utils.js" }, |
| 66 | }; |
| 67 | |
| 68 | it("should not include pattern-matched rules when no file paths are mentioned", () => { |
| 69 | const result = getSystemMessageWithRules({ |
| 70 | baseSystemMessage, |
| 71 | userMessage: undefined, |
| 72 | rules: [tsRule], |
| 73 | contextItems: emptyContextItems, |
| 74 | }); |
| 75 | |
| 76 | expect(result).toBe(baseSystemMessage); |
| 77 | }); |
| 78 | |
| 79 | it("should include general rules (without matches) regardless of file paths", () => { |
| 80 | const result = getSystemMessageWithRules({ |
| 81 | baseSystemMessage, |
| 82 | userMessage: undefined, |
| 83 | rules: [generalRule], |
| 84 | contextItems: emptyContextItems, |
| 85 | }); |
| 86 | |
| 87 | expect(result).toBe(`${baseSystemMessage}\n\n${generalRule.rule}`); |
| 88 | }); |
| 89 | |
| 90 | it("should include only matching rules based on file paths in message", () => { |
| 91 | const userMessage: UserChatMessage = { |
| 92 | role: "user", |
| 93 | content: |
| 94 | "```tsx Component.tsx\nexport const Component = () => <div>Hello</div>;\n```", |
| 95 | }; |
| 96 | |
| 97 | const result = getSystemMessageWithRules({ |
| 98 | baseSystemMessage, |
| 99 | userMessage, |
| 100 | rules: [tsRule, pythonRule, generalRule], |
| 101 | contextItems: emptyContextItems, |
| 102 | }); |
| 103 | |
| 104 | // Should include TS rule and general rule, but not Python rule |
| 105 | const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${generalRule.rule}`; |
| 106 | expect(result).toBe(expected); |
| 107 | }); |
| 108 | |
| 109 | it("should include matching rules based on file paths in context items", () => { |
| 110 | const result = getSystemMessageWithRules({ |
| 111 | baseSystemMessage, |
| 112 | userMessage: undefined, |
| 113 | rules: [tsRule, pythonRule, generalRule], |
| 114 | contextItems: [tsContextItem], |
| 115 | }); |
| 116 | |
| 117 | // Should include TS rule and general rule, but not Python rule |
| 118 | const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${generalRule.rule}`; |
| 119 | expect(result).toBe(expected); |
| 120 | }); |
| 121 | |
| 122 | it("should include matching rules from both message and context items", () => { |
| 123 | const userMessage: UserChatMessage = { |
| 124 | role: "user", |
| 125 | content: "```python test.py\nprint('hello')\n```", |
| 126 | }; |
| 127 | |
| 128 | const result = getSystemMessageWithRules({ |
| 129 | baseSystemMessage, |
| 130 | userMessage, |
| 131 | rules: [tsRule, pythonRule, generalRule], |
| 132 | contextItems: [tsContextItem], |
| 133 | }); |
| 134 | |
| 135 | // Should include TS rule, Python rule, and general rule |
| 136 | const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${pythonRule.rule}\n\n${generalRule.rule}`; |
| 137 | expect(result).toBe(expected); |
| 138 | }); |
| 139 | |
| 140 | it("should include multiple matching rules from message", () => { |
| 141 | const userMessage: UserChatMessage = { |
| 142 | role: "user", |
| 143 | content: |
| 144 | "```tsx Component.tsx\nexport const Component = () => <div>Hello</div>;\n```\n```python test.py\nprint('hello')\n```", |
| 145 | }; |
| 146 | |
| 147 | const result = getSystemMessageWithRules({ |
| 148 | baseSystemMessage, |
| 149 | userMessage, |
| 150 | rules: [tsRule, pythonRule, generalRule], |
| 151 | contextItems: emptyContextItems, |
| 152 | }); |
| 153 | |
| 154 | // Should include all rules |
| 155 | const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${pythonRule.rule}\n\n${generalRule.rule}`; |
| 156 | expect(result).toBe(expected); |
| 157 | }); |
| 158 | |
| 159 | it("should include multiple matching rules from context items", () => { |
| 160 | const result = getSystemMessageWithRules({ |
| 161 | baseSystemMessage, |
| 162 | userMessage: undefined, |
| 163 | rules: [tsRule, pythonRule, generalRule], |
| 164 | contextItems: [tsContextItem, pyContextItem], |
| 165 | }); |
| 166 | |
| 167 | // Should include all rules |
| 168 | const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${pythonRule.rule}\n\n${generalRule.rule}`; |
| 169 | expect(result).toBe(expected); |
| 170 | }); |
| 171 | |
| 172 | it("should include only general rules when no paths match pattern-specific rules", () => { |
| 173 | const userMessage: UserChatMessage = { |
| 174 | role: "user", |
| 175 | content: "```ruby test.rb\nputs 'hello'\n```", |
| 176 | }; |
| 177 | |
| 178 | const result = getSystemMessageWithRules({ |
| 179 | baseSystemMessage, |
| 180 | userMessage, |
| 181 | rules: [tsRule, pythonRule, generalRule], |
| 182 | contextItems: emptyContextItems, |
| 183 | }); |
| 184 | |
| 185 | // Should only include the general rule |
| 186 | const expected = `${baseSystemMessage}\n\n${generalRule.rule}`; |
| 187 | expect(result).toBe(expected); |
| 188 | }); |
| 189 | |
| 190 | it("should NOT match with comma-separated glob patterns", () => { |
| 191 | const userMessage: UserChatMessage = { |
| 192 | role: "user", |
| 193 | content: |
| 194 | "```ts src/main.ts\nconsole.log('hello');\n```\n```ts tests/example.test.ts\ntest('should work', () => {});\n```", |
| 195 | }; |
| 196 | |
| 197 | // Rule with comma-separated glob patterns |
| 198 | const commaRule: RuleWithSource = { |
| 199 | name: "TypeScript Standards", |
| 200 | rule: "Use TypeScript best practices for comma-separated globs", |
| 201 | globs: "src/**/*.ts, tests/**/*.ts", |
| 202 | source: "rules-block", |
| 203 | }; |
| 204 | |
| 205 | const result = getSystemMessageWithRules({ |
| 206 | baseSystemMessage, |
| 207 | userMessage, |
| 208 | rules: [commaRule], |
| 209 | contextItems: emptyContextItems, |
| 210 | }); |
| 211 | |
| 212 | // With the current implementation, the comma-separated pattern is treated as a single string, |
| 213 | // so it won't match any of the file paths (minimatch doesn't support comma-separated patterns) |
| 214 | expect(result).toBe(baseSystemMessage); |
| 215 | }); |
| 216 | |
| 217 | it("should match with an array of glob patterns", () => { |
| 218 | const userMessage: UserChatMessage = { |
| 219 | role: "user", |
| 220 | content: |
| 221 | "```ts src/main.ts\nconsole.log('hello');\n```\n```ts tests/example.test.ts\ntest('should work', () => {});\n```\n```ts config/settings.ts\nconst config = {};\n```", |
| 222 | }; |
| 223 | |
| 224 | // Rule with an array of glob patterns |
| 225 | const arrayGlobRule: RuleWithSource = { |
| 226 | name: "TypeScript Standards", |
| 227 | rule: "Use TypeScript best practices for array globs", |
| 228 | globs: ["src/**/*.ts", "tests/**/*.ts"], |
| 229 | source: "rules-block", |
| 230 | }; |
| 231 | |
| 232 | const result = getSystemMessageWithRules({ |
| 233 | baseSystemMessage, |
| 234 | userMessage, |
| 235 | rules: [arrayGlobRule], |
| 236 | contextItems: emptyContextItems, |
| 237 | }); |
| 238 | |
| 239 | // With our new implementation, the array of patterns should match both src/main.ts and tests/example.test.ts |
| 240 | expect(result).toBe(`${baseSystemMessage}\n\n${arrayGlobRule.rule}`); |
| 241 | }); |
| 242 | |
| 243 | it("should match only patterns in the array", () => { |
| 244 | const userMessage: UserChatMessage = { |
| 245 | role: "user", |
| 246 | content: |
| 247 | "```ts src/main.ts\nconsole.log('hello');\n```\n```ts config/settings.ts\nconst config = {};\n```", |
| 248 | }; |
| 249 | |
| 250 | // Rule with an array of glob patterns |
| 251 | const arrayGlobRule: RuleWithSource = { |
| 252 | name: "TypeScript Standards", |
| 253 | rule: "Use TypeScript best practices for array globs", |
| 254 | globs: ["src/**/*.ts", "tests/**/*.ts"], |
| 255 | source: "rules-block", |
| 256 | }; |
| 257 | |
| 258 | const result = getSystemMessageWithRules({ |
| 259 | baseSystemMessage, |
| 260 | userMessage, |
| 261 | rules: [arrayGlobRule], |
| 262 | contextItems: emptyContextItems, |
| 263 | }); |
| 264 | |
| 265 | // Should match src/main.ts but not config/settings.ts |
| 266 | expect(result).toBe(`${baseSystemMessage}\n\n${arrayGlobRule.rule}`); |
| 267 | }); |
| 268 | |
| 269 | it("should not match any when no patterns in the array match", () => { |
| 270 | const userMessage: UserChatMessage = { |
| 271 | role: "user", |
| 272 | content: |
| 273 | "```ts config/settings.ts\nconst config = {};\n```\n```ruby test.rb\nputs 'hello'\n```", |
| 274 | }; |
| 275 | |
| 276 | // Rule with an array of glob patterns |
| 277 | const arrayGlobRule: RuleWithSource = { |
| 278 | name: "TypeScript Standards", |
| 279 | rule: "Use TypeScript best practices for array globs", |
| 280 | globs: ["src/**/*.ts", "tests/**/*.ts"], |
| 281 | source: "rules-block", |
| 282 | }; |
| 283 | |
| 284 | const result = getSystemMessageWithRules({ |
| 285 | baseSystemMessage, |
| 286 | userMessage, |
| 287 | rules: [arrayGlobRule], |
| 288 | contextItems: emptyContextItems, |
| 289 | }); |
| 290 | |
| 291 | // Should not match any file paths |
| 292 | expect(result).toBe(baseSystemMessage); |
| 293 | }); |
| 294 | |
| 295 | // New test for code block with filename only (no language) |
| 296 | it("should handle code blocks with filename only (no language identifier)", () => { |
| 297 | const userMessage: UserChatMessage = { |
| 298 | role: "user", |
| 299 | content: "```test.js\nclass Calculator { /* code */ }\n```", |
| 300 | }; |
| 301 | |
| 302 | const result = getSystemMessageWithRules({ |
| 303 | baseSystemMessage, |
| 304 | userMessage, |
| 305 | rules: [jsRule, tsRule, generalRule], |
| 306 | contextItems: emptyContextItems, |
| 307 | }); |
| 308 | |
| 309 | // Should include JS rule and general rule |
| 310 | const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${generalRule.rule}`; |
| 311 | expect(result).toBe(expected); |
| 312 | }); |
| 313 | |
| 314 | // New test for code blocks with line ranges |
| 315 | it("should handle code blocks with line ranges", () => { |
| 316 | const userMessage: UserChatMessage = { |
| 317 | role: "user", |
| 318 | content: "```js test.js (19-25)\ndivide(number) { /* code */ }\n```", |
| 319 | }; |
| 320 | |
| 321 | const result = getSystemMessageWithRules({ |
| 322 | baseSystemMessage, |
| 323 | userMessage, |
| 324 | rules: [jsRule, tsRule, generalRule], |
| 325 | contextItems: emptyContextItems, |
| 326 | }); |
| 327 | |
| 328 | // Should include JS rule and general rule |
| 329 | const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${generalRule.rule}`; |
| 330 | expect(result).toBe(expected); |
| 331 | }); |
| 332 | |
| 333 | // New test for mixed code block formats |
| 334 | it("should handle mixed code block formats in the same message", () => { |
| 335 | const userMessage: UserChatMessage = { |
| 336 | role: "user", |
| 337 | content: |
| 338 | "```test.js\nclass Calculator { /* code */ }\n```\n" + |
| 339 | "```js utils.js (19-25)\ndivide(number) { /* code */ }\n```\n" + |
| 340 | "```ts config/settings.ts\nconst config = {};\n```", |
| 341 | }; |
| 342 | |
| 343 | const result = getSystemMessageWithRules({ |
| 344 | baseSystemMessage, |
| 345 | userMessage, |
| 346 | rules: [jsRule, tsRule, generalRule], |
| 347 | contextItems: emptyContextItems, |
| 348 | }); |
| 349 | |
| 350 | // Should include JS rule, TS rule, and general rule |
| 351 | const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${tsRule.rule}\n\n${generalRule.rule}`; |
| 352 | expect(result).toBe(expected); |
| 353 | }); |
| 354 | |
| 355 | // Test for context items when there's no message |
| 356 | it("should apply rules based on context items only when no message is present", () => { |
| 357 | const result = getSystemMessageWithRules({ |
| 358 | baseSystemMessage, |
| 359 | userMessage: undefined, |
| 360 | rules: [jsRule, tsRule, generalRule], |
| 361 | contextItems: [jsContextItem], |
| 362 | }); |
| 363 | |
| 364 | // Should include JS rule and general rule |
| 365 | const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${generalRule.rule}`; |
| 366 | expect(result).toBe(expected); |
| 367 | }); |
| 368 | |
| 369 | // Test for non-file context items |
| 370 | it("should ignore non-file context items", () => { |
| 371 | const nonFileContextItem: ContextItemWithId = { |
| 372 | content: "Some search result", |
| 373 | name: "Search result", |
| 374 | description: "A search result", |
| 375 | id: { providerTitle: "search", itemId: "search-result" }, |
| 376 | // No uri with type "file" |
| 377 | }; |
| 378 | |
| 379 | const result = getSystemMessageWithRules({ |
| 380 | baseSystemMessage, |
| 381 | userMessage: undefined, |
| 382 | rules: [jsRule, tsRule, generalRule], |
| 383 | contextItems: [nonFileContextItem], |
| 384 | }); |
| 385 | |
| 386 | // Should only include general rule |
| 387 | const expected = `${baseSystemMessage}\n\n${generalRule.rule}`; |
| 388 | expect(result).toBe(expected); |
| 389 | }); |
| 390 | |
| 391 | // Test combining context items with message |
| 392 | it("should match rules from both message code blocks and context items", () => { |
| 393 | const userMessage: UserChatMessage = { |
| 394 | role: "user", |
| 395 | content: "```ts src/main.ts\nconsole.log('hello');\n```", |
| 396 | }; |
| 397 | |
| 398 | const result = getSystemMessageWithRules({ |
| 399 | baseSystemMessage, |
| 400 | userMessage, |
| 401 | rules: [jsRule, tsRule, pythonRule, generalRule], |
| 402 | contextItems: [jsContextItem, pyContextItem], |
| 403 | }); |
| 404 | |
| 405 | // Should include JS, TS, Python rules and general rule |
| 406 | const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${tsRule.rule}\n\n${pythonRule.rule}\n\n${generalRule.rule}`; |
| 407 | expect(result).toBe(expected); |
| 408 | }); |
| 409 | |
| 410 | // Tests for alwaysApply property |
| 411 | describe("alwaysApply property", () => { |
| 412 | const alwaysApplyTrueRule: RuleWithSource = { |
| 413 | name: "Always Apply True Rule", |
| 414 | rule: "This rule should always be applied", |
| 415 | globs: "**/*.nonexistent", |
| 416 | alwaysApply: true, |
| 417 | source: "rules-block", |
| 418 | }; |
| 419 | |
| 420 | const alwaysApplyFalseRule: RuleWithSource = { |
| 421 | name: "Always Apply False Rule", |
| 422 | rule: "This rule should never be applied", |
| 423 | alwaysApply: false, |
| 424 | source: "rules-block", |
| 425 | }; |
| 426 | |
| 427 | const alwaysApplyFalseWithMatchingGlobs: RuleWithSource = { |
| 428 | name: "Always Apply False with Matching Globs", |
| 429 | rule: "This rule should never be applied even with matching globs", |
| 430 | globs: "**/*.ts?(x)", |
| 431 | alwaysApply: false, |
| 432 | source: "rules-block", |
| 433 | }; |
| 434 | |
| 435 | it("should always include rules with alwaysApply: true regardless of globs or file paths", () => { |
| 436 | const userMessage: UserChatMessage = { |
| 437 | role: "user", |
| 438 | content: "```js main.js\nconsole.log('hello');\n```", |
| 439 | }; |
| 440 | |
| 441 | const result = getSystemMessageWithRules({ |
| 442 | baseSystemMessage, |
| 443 | userMessage, |
| 444 | rules: [alwaysApplyTrueRule, tsRule], |
| 445 | contextItems: emptyContextItems, |
| 446 | }); |
| 447 | |
| 448 | // Should include the alwaysApply:true rule even though globs don't match |
| 449 | const expected = `${baseSystemMessage}\n\n${alwaysApplyTrueRule.rule}`; |
| 450 | expect(result).toBe(expected); |
| 451 | }); |
| 452 | |
| 453 | it("should include rules with alwaysApply: false when globs match", () => { |
| 454 | const userMessage: UserChatMessage = { |
| 455 | role: "user", |
| 456 | content: |
| 457 | "```tsx Component.tsx\nexport const Component = () => <div>Hello</div>;\n```", |
| 458 | }; |
| 459 | |
| 460 | const result = getSystemMessageWithRules({ |
| 461 | baseSystemMessage, |
| 462 | userMessage, |
| 463 | rules: [alwaysApplyFalseWithMatchingGlobs, tsRule, generalRule], |
| 464 | contextItems: emptyContextItems, |
| 465 | }); |
| 466 | |
| 467 | // Should include alwaysApply:false rule because globs match |
| 468 | const expected = `${baseSystemMessage}\n\n${alwaysApplyFalseWithMatchingGlobs.rule}\n\n${tsRule.rule}\n\n${generalRule.rule}`; |
| 469 | expect(result).toBe(expected); |
| 470 | }); |
| 471 | |
| 472 | it("should include rules with alwaysApply: false when globs match", () => { |
| 473 | const userMessage: UserChatMessage = { |
| 474 | role: "user", |
| 475 | content: |
| 476 | "```ts Component.tsx\nexport const Component = () => <div>Hello</div>;\n```", |
| 477 | }; |
| 478 | |
| 479 | const result = getSystemMessageWithRules({ |
| 480 | baseSystemMessage, |
| 481 | userMessage, |
| 482 | rules: [alwaysApplyFalseWithMatchingGlobs, tsRule, generalRule], |
| 483 | contextItems: emptyContextItems, |
| 484 | }); |
| 485 | |
| 486 | // Should include alwaysApply:false rule because globs match |
| 487 | const expected = `${baseSystemMessage}\n\n${alwaysApplyFalseWithMatchingGlobs.rule}\n\n${tsRule.rule}\n\n${generalRule.rule}`; |
| 488 | expect(result).toBe(expected); |
| 489 | }); |
| 490 | |
| 491 | it("should NOT include rules with alwaysApply: false when globs don't match", () => { |
| 492 | const userMessage: UserChatMessage = { |
| 493 | role: "user", |
| 494 | content: "```py script.py\nprint('hello')\n```", |
| 495 | }; |
| 496 | |
| 497 | const result = getSystemMessageWithRules({ |
| 498 | baseSystemMessage, |
| 499 | userMessage, |
| 500 | rules: [alwaysApplyFalseWithMatchingGlobs, generalRule], |
| 501 | contextItems: emptyContextItems, |
| 502 | }); |
| 503 | |
| 504 | // Should only include general rule (alwaysApply:false rule doesn't match .py files) |
| 505 | const expected = `${baseSystemMessage}\n\n${generalRule.rule}`; |
| 506 | expect(result).toBe(expected); |
| 507 | }); |
| 508 | |
| 509 | it("should NOT include rules with alwaysApply: false when no globs are specified", () => { |
| 510 | const userMessage: UserChatMessage = { |
| 511 | role: "user", |
| 512 | content: "```js main.js\nconsole.log('hello');\n```", |
| 513 | }; |
| 514 | |
| 515 | const alwaysApplyFalseNoGlobs: RuleWithSource = { |
| 516 | name: "Always Apply False No Globs", |
| 517 | rule: "This rule has alwaysApply false and no globs", |
| 518 | alwaysApply: false, |
| 519 | source: "rules-block", |
| 520 | }; |
| 521 | |
| 522 | const result = getSystemMessageWithRules({ |
| 523 | baseSystemMessage, |
| 524 | userMessage, |
| 525 | rules: [alwaysApplyFalseNoGlobs, jsRule, generalRule], |
| 526 | contextItems: emptyContextItems, |
| 527 | }); |
| 528 | |
| 529 | // Should NOT include alwaysApply:false rule when no globs specified |
| 530 | const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${generalRule.rule}`; |
| 531 | expect(result).toBe(expected); |
| 532 | }); |
| 533 | |
| 534 | it("should include rules with alwaysApply: true even when no files are present", () => { |
| 535 | const result = getSystemMessageWithRules({ |
| 536 | baseSystemMessage, |
| 537 | userMessage: undefined, |
| 538 | rules: [alwaysApplyTrueRule, tsRule, pythonRule], |
| 539 | contextItems: emptyContextItems, |
| 540 | }); |
| 541 | |
| 542 | // Should only include the alwaysApply:true rule |
| 543 | const expected = `${baseSystemMessage}\n\n${alwaysApplyTrueRule.rule}`; |
| 544 | expect(result).toBe(expected); |
| 545 | }); |
| 546 | |
| 547 | it("should handle mixed alwaysApply values correctly", () => { |
| 548 | const userMessage: UserChatMessage = { |
| 549 | role: "user", |
| 550 | content: |
| 551 | "```ts Component.tsx\nexport const Component = () => <div>Hello</div>;\n```", |
| 552 | }; |
| 553 | |
| 554 | const result = getSystemMessageWithRules({ |
| 555 | baseSystemMessage, |
| 556 | userMessage, |
| 557 | rules: [ |
| 558 | alwaysApplyTrueRule, |
| 559 | alwaysApplyFalseRule, |
| 560 | alwaysApplyFalseWithMatchingGlobs, |
| 561 | tsRule, |
| 562 | generalRule, |
| 563 | ], |
| 564 | contextItems: emptyContextItems, |
| 565 | }); |
| 566 | |
| 567 | // Should include: |
| 568 | // - alwaysApplyTrueRule (always applies) |
| 569 | // - alwaysApplyFalseWithMatchingGlobs (has globs that match .tsx) |
| 570 | // - tsRule (globs match .tsx) |
| 571 | // - generalRule (no globs, so applies to all) |
| 572 | // Should NOT include: |
| 573 | // - alwaysApplyFalseRule (alwaysApply: false and no globs) |
| 574 | const expected = `${baseSystemMessage}\n\n${alwaysApplyTrueRule.rule}\n\n${alwaysApplyFalseWithMatchingGlobs.rule}\n\n${tsRule.rule}\n\n${generalRule.rule}`; |
| 575 | expect(result).toBe(expected); |
| 576 | }); |
| 577 | |
| 578 | it("should use glob matching when alwaysApply is false", () => { |
| 579 | // This tests that rules with alwaysApply: false follow glob matching |
| 580 | const ruleWithAlwaysApplyFalse: RuleWithSource = { |
| 581 | name: "Rule With Always Apply False", |
| 582 | rule: "This rule follows glob matching behavior", |
| 583 | globs: "**/*.ts?(x)", |
| 584 | alwaysApply: false, |
| 585 | source: "rules-block", |
| 586 | }; |
| 587 | |
| 588 | const userMessage: UserChatMessage = { |
| 589 | role: "user", |
| 590 | content: |
| 591 | "```ts Component.tsx\nexport const Component = () => <div>Hello</div>;\n```", |
| 592 | }; |
| 593 | |
| 594 | const result = getSystemMessageWithRules({ |
| 595 | baseSystemMessage, |
| 596 | userMessage, |
| 597 | rules: [ruleWithAlwaysApplyFalse], |
| 598 | contextItems: emptyContextItems, |
| 599 | }); |
| 600 | |
| 601 | // Should include the rule because it matches the file path |
| 602 | const expected = `${baseSystemMessage}\n\n${ruleWithAlwaysApplyFalse.rule}`; |
| 603 | expect(result).toBe(expected); |
| 604 | }); |
| 605 | |
| 606 | it("should include rules with globs when context file paths match (alwaysApply false)", () => { |
| 607 | const ruleWithGlobsOnly: RuleWithSource = { |
| 608 | name: "TypeScript Only Rule", |
| 609 | rule: "This rule should apply to TypeScript files only", |
| 610 | globs: "**/*.ts", |
| 611 | alwaysApply: false, |
| 612 | source: "rules-block", |
| 613 | }; |
| 614 | |
| 615 | const tsContextItem: ContextItemWithId = { |
| 616 | content: "TypeScript file content", |
| 617 | name: "utils.ts", |
| 618 | description: "A TypeScript utility file", |
| 619 | id: { providerTitle: "file", itemId: "src/utils.ts" }, |
| 620 | uri: { type: "file", value: "src/utils.ts" }, |
| 621 | }; |
| 622 | |
| 623 | const result = getSystemMessageWithRules({ |
| 624 | baseSystemMessage, |
| 625 | userMessage: undefined, // No message, only context |
| 626 | rules: [ruleWithGlobsOnly, pythonRule], // Include a non-matching rule |
| 627 | contextItems: [tsContextItem], |
| 628 | }); |
| 629 | |
| 630 | // Should include the TypeScript rule but not the Python rule |
| 631 | const expected = `${baseSystemMessage}\n\n${ruleWithGlobsOnly.rule}`; |
| 632 | expect(result).toBe(expected); |
| 633 | }); |
| 634 | |
| 635 | it("should NOT include rules with globs when context file paths don't match (alwaysApply false)", () => { |
| 636 | const ruleWithGlobsOnly: RuleWithSource = { |
| 637 | name: "TypeScript Only Rule", |
| 638 | rule: "This rule should apply to TypeScript files only", |
| 639 | globs: "**/*.ts", |
| 640 | alwaysApply: false, |
| 641 | source: "rules-block", |
| 642 | }; |
| 643 | |
| 644 | const pyContextItem: ContextItemWithId = { |
| 645 | content: "Python file content", |
| 646 | name: "utils.py", |
| 647 | description: "A Python utility file", |
| 648 | id: { providerTitle: "file", itemId: "src/utils.py" }, |
| 649 | uri: { type: "file", value: "src/utils.py" }, |
| 650 | }; |
| 651 | |
| 652 | const result = getSystemMessageWithRules({ |
| 653 | baseSystemMessage, |
| 654 | userMessage: undefined, // No message, only context |
| 655 | rules: [ruleWithGlobsOnly], |
| 656 | contextItems: [pyContextItem], // Python file doesn't match *.ts pattern |
| 657 | }); |
| 658 | |
| 659 | // Should NOT include the rule because context doesn't match the glob |
| 660 | expect(result).toBe(baseSystemMessage); |
| 661 | }); |
| 662 | }); |
| 663 | }); |
| 664 | |
| 665 | describe("shouldApplyRule", () => { |
| 666 | const ruleWithGlobs: RuleWithSource = { |
| 667 | name: "Rule with Globs", |
| 668 | rule: "Apply to TypeScript files", |
| 669 | globs: "**/*.ts?(x)", |
| 670 | alwaysApply: false, |
| 671 | source: "rules-block", |
| 672 | }; |
| 673 | |
| 674 | const ruleWithoutGlobs: RuleWithSource = { |
| 675 | name: "Rule without Globs", |
| 676 | rule: "Apply to all files", |
| 677 | alwaysApply: true, |
| 678 | source: "rules-block", |
| 679 | }; |
| 680 | |
| 681 | const ruleAlwaysApplyTrue: RuleWithSource = { |
| 682 | name: "Always Apply True", |
| 683 | rule: "Always apply this rule", |
| 684 | globs: "**/*.nonexistent", |
| 685 | alwaysApply: true, |
| 686 | source: "rules-block", |
| 687 | }; |
| 688 | |
| 689 | const ruleAlwaysApplyFalse: RuleWithSource = { |
| 690 | name: "Always Apply False", |
| 691 | rule: "Never apply this rule", |
| 692 | globs: "**/*.ts?(x)", |
| 693 | alwaysApply: false, |
| 694 | source: "rules-block", |
| 695 | }; |
| 696 | |
| 697 | const ruleAlwaysApplyFalseNoGlobs: RuleWithSource = { |
| 698 | name: "Always Apply False No Globs", |
| 699 | rule: "Never apply this rule", |
| 700 | alwaysApply: false, |
| 701 | source: "rules-block", |
| 702 | }; |
| 703 | |
| 704 | describe("alwaysApply behavior", () => { |
| 705 | it("should return true when alwaysApply is true, regardless of file paths", () => { |
| 706 | expect(shouldApplyRule(ruleAlwaysApplyTrue, [])).toBe(true); |
| 707 | expect(shouldApplyRule(ruleAlwaysApplyTrue, ["src/main.js"])).toBe(true); |
| 708 | expect(shouldApplyRule(ruleAlwaysApplyTrue, ["Component.tsx"])).toBe( |
| 709 | true, |
| 710 | ); |
| 711 | }); |
| 712 | |
| 713 | it("should use glob matching when alwaysApply is false", () => { |
| 714 | // Should apply when globs match |
| 715 | expect(shouldApplyRule(ruleAlwaysApplyFalse, ["src/main.ts"])).toBe(true); |
| 716 | expect(shouldApplyRule(ruleAlwaysApplyFalse, ["Component.tsx"])).toBe( |
| 717 | true, |
| 718 | ); |
| 719 | |
| 720 | // Should not apply when globs don't match |
| 721 | expect(shouldApplyRule(ruleAlwaysApplyFalse, ["script.py"])).toBe(false); |
| 722 | expect(shouldApplyRule(ruleAlwaysApplyFalse, [])).toBe(false); |
| 723 | }); |
| 724 | |
| 725 | it("should return false when alwaysApply is false and no globs specified", () => { |
| 726 | expect(shouldApplyRule(ruleAlwaysApplyFalseNoGlobs, [])).toBe(false); |
| 727 | expect( |
| 728 | shouldApplyRule(ruleAlwaysApplyFalseNoGlobs, ["any-file.js"]), |
| 729 | ).toBe(false); |
| 730 | }); |
| 731 | }); |
| 732 | |
| 733 | describe("default behavior (alwaysApply undefined)", () => { |
| 734 | it("should return true for rules without globs regardless of file paths", () => { |
| 735 | expect(shouldApplyRule(ruleWithoutGlobs, [])).toBe(true); |
| 736 | expect(shouldApplyRule(ruleWithoutGlobs, ["src/main.js"])).toBe(true); |
| 737 | expect( |
| 738 | shouldApplyRule(ruleWithoutGlobs, ["Component.tsx", "utils.py"]), |
| 739 | ).toBe(true); |
| 740 | }); |
| 741 | |
| 742 | it("should return false for rules with globs when no file paths are provided", () => { |
| 743 | expect(shouldApplyRule(ruleWithGlobs, [])).toBe(false); |
| 744 | }); |
| 745 | |
| 746 | it("should return true for rules with globs when matching file paths are provided", () => { |
| 747 | expect(shouldApplyRule(ruleWithGlobs, ["Component.tsx"])).toBe(true); |
| 748 | expect(shouldApplyRule(ruleWithGlobs, ["src/main.ts"])).toBe(true); |
| 749 | expect( |
| 750 | shouldApplyRule(ruleWithGlobs, ["utils.js", "Component.tsx"]), |
| 751 | ).toBe(true); |
| 752 | }); |
| 753 | |
| 754 | it("should return false for rules with globs when no matching file paths are provided", () => { |
| 755 | expect(shouldApplyRule(ruleWithGlobs, ["utils.py"])).toBe(false); |
| 756 | expect(shouldApplyRule(ruleWithGlobs, ["main.js", "script.rb"])).toBe( |
| 757 | false, |
| 758 | ); |
| 759 | }); |
| 760 | }); |
| 761 | |
| 762 | describe("glob pattern matching", () => { |
| 763 | const ruleWithArrayGlobs: RuleWithSource = { |
| 764 | name: "Rule with Array Globs", |
| 765 | rule: "Apply to specific patterns", |
| 766 | globs: ["src/**/*.ts", "tests/**/*.test.js"], |
| 767 | source: "rules-block", |
| 768 | }; |
| 769 | |
| 770 | const ruleWithSpecificPattern: RuleWithSource = { |
| 771 | name: "Rule with Specific Pattern", |
| 772 | rule: "Apply to Python files", |
| 773 | globs: "**/*.py", |
| 774 | source: "rules-block", |
| 775 | }; |
| 776 | |
| 777 | it("should handle array of glob patterns", () => { |
| 778 | expect(shouldApplyRule(ruleWithArrayGlobs, ["src/main.ts"])).toBe(true); |
| 779 | expect(shouldApplyRule(ruleWithArrayGlobs, ["tests/unit.test.js"])).toBe( |
| 780 | true, |
| 781 | ); |
| 782 | expect( |
| 783 | shouldApplyRule(ruleWithArrayGlobs, ["config/settings.json"]), |
| 784 | ).toBe(false); |
| 785 | }); |
| 786 | |
| 787 | it("should handle string glob patterns", () => { |
| 788 | expect(shouldApplyRule(ruleWithSpecificPattern, ["utils.py"])).toBe(true); |
| 789 | expect( |
| 790 | shouldApplyRule(ruleWithSpecificPattern, ["src/models/user.py"]), |
| 791 | ).toBe(true); |
| 792 | expect(shouldApplyRule(ruleWithSpecificPattern, ["utils.js"])).toBe( |
| 793 | false, |
| 794 | ); |
| 795 | }); |
| 796 | |
| 797 | it("should return true if any file path matches when multiple paths provided", () => { |
| 798 | expect( |
| 799 | shouldApplyRule(ruleWithSpecificPattern, [ |
| 800 | "utils.js", |
| 801 | "models.py", |
| 802 | "config.json", |
| 803 | ]), |
| 804 | ).toBe(true); |
| 805 | expect( |
| 806 | shouldApplyRule(ruleWithGlobs, [ |
| 807 | "utils.py", |
| 808 | "Component.tsx", |
| 809 | "script.rb", |
| 810 | ]), |
| 811 | ).toBe(true); |
| 812 | }); |
| 813 | }); |
| 814 | |
| 815 | describe("edge cases", () => { |
| 816 | it("should handle empty globs array", () => { |
| 817 | const ruleWithEmptyGlobs: RuleWithSource = { |
| 818 | name: "Rule with Empty Globs", |
| 819 | rule: "Test rule", |
| 820 | globs: [], |
| 821 | source: "rules-block", |
| 822 | }; |
| 823 | |
| 824 | // Empty array should be treated as "no globs" (truthy check fails) |
| 825 | expect(shouldApplyRule(ruleWithEmptyGlobs, ["any-file.js"])).toBe(false); |
| 826 | }); |
| 827 | |
| 828 | it("should handle undefined globs", () => { |
| 829 | const ruleUndefinedGlobs: RuleWithSource = { |
| 830 | name: "Rule with Undefined Globs", |
| 831 | rule: "Test rule", |
| 832 | globs: undefined, |
| 833 | source: "rules-block", |
| 834 | }; |
| 835 | |
| 836 | expect(shouldApplyRule(ruleUndefinedGlobs, ["any-file.js"])).toBe(true); |
| 837 | expect(shouldApplyRule(ruleUndefinedGlobs, [])).toBe(true); |
| 838 | }); |
| 839 | }); |
| 840 | }); |