| 1 | import { RuleWithSource, UserChatMessage } from "../.."; |
| 2 | import { getSystemMessageWithRules } from "./getSystemMessageWithRules"; |
| 3 | |
| 4 | describe("getSystemMessageWithRules", () => { |
| 5 | const baseSystemMessage = "Base system message"; |
| 6 | const tsRule: RuleWithSource = { |
| 7 | name: "TypeScript Rule", |
| 8 | rule: "Follow TypeScript best practices", |
| 9 | globs: "**/*.ts?(x)", |
| 10 | source: "rules-block", |
| 11 | }; |
| 12 | |
| 13 | // Rule without pattern (always active) |
| 14 | const generalRule: RuleWithSource = { |
| 15 | name: "General Rule", |
| 16 | rule: "Always write clear comments", |
| 17 | source: "rules-block", |
| 18 | }; |
| 19 | |
| 20 | // Rule with a different pattern |
| 21 | const pythonRule: RuleWithSource = { |
| 22 | name: "Python Rule", |
| 23 | rule: "Follow PEP 8 guidelines", |
| 24 | globs: "**/*.py", |
| 25 | source: "rules-block", |
| 26 | }; |
| 27 | |
| 28 | it("should not include pattern-matched rules when no file paths are mentioned", () => { |
| 29 | const result = getSystemMessageWithRules({ |
| 30 | baseSystemMessage, |
| 31 | userMessage: undefined, |
| 32 | rules: [tsRule], |
| 33 | }); |
| 34 | |
| 35 | expect(result).toBe(baseSystemMessage); |
| 36 | }); |
| 37 | |
| 38 | it("should include general rules (without matches) regardless of file paths", () => { |
| 39 | const result = getSystemMessageWithRules({ |
| 40 | baseSystemMessage, |
| 41 | userMessage: undefined, |
| 42 | rules: [generalRule], |
| 43 | }); |
| 44 | |
| 45 | expect(result).toBe(`${baseSystemMessage}\n\n${generalRule.rule}`); |
| 46 | }); |
| 47 | |
| 48 | it("should include only matching rules based on file paths", () => { |
| 49 | const userMessage: UserChatMessage = { |
| 50 | role: "user", |
| 51 | content: |
| 52 | "```tsx Component.tsx\nexport const Component = () => <div>Hello</div>;\n```", |
| 53 | }; |
| 54 | |
| 55 | const result = getSystemMessageWithRules({ |
| 56 | baseSystemMessage, |
| 57 | userMessage, |
| 58 | rules: [tsRule, pythonRule, generalRule], |
| 59 | }); |
| 60 | |
| 61 | // Should include TS rule and general rule, but not Python rule |
| 62 | const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${generalRule.rule}`; |
| 63 | expect(result).toBe(expected); |
| 64 | }); |
| 65 | |
| 66 | it("should include multiple matching rules", () => { |
| 67 | const userMessage: UserChatMessage = { |
| 68 | role: "user", |
| 69 | content: |
| 70 | "```tsx Component.tsx\nexport const Component = () => <div>Hello</div>;\n```\n```python test.py\nprint('hello')\n```", |
| 71 | }; |
| 72 | |
| 73 | const result = getSystemMessageWithRules({ |
| 74 | baseSystemMessage, |
| 75 | userMessage, |
| 76 | rules: [tsRule, pythonRule, generalRule], |
| 77 | }); |
| 78 | |
| 79 | // Should include all rules |
| 80 | const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${pythonRule.rule}\n\n${generalRule.rule}`; |
| 81 | expect(result).toBe(expected); |
| 82 | }); |
| 83 | |
| 84 | it("should include only general rules when no paths match pattern-specific rules", () => { |
| 85 | const userMessage: UserChatMessage = { |
| 86 | role: "user", |
| 87 | content: "```ruby test.rb\nputs 'hello'\n```", |
| 88 | }; |
| 89 | |
| 90 | const result = getSystemMessageWithRules({ |
| 91 | baseSystemMessage, |
| 92 | userMessage, |
| 93 | rules: [tsRule, pythonRule, generalRule], |
| 94 | }); |
| 95 | |
| 96 | // Should only include the general rule |
| 97 | const expected = `${baseSystemMessage}\n\n${generalRule.rule}`; |
| 98 | expect(result).toBe(expected); |
| 99 | }); |
| 100 | |
| 101 | it("should NOT match with comma-separated glob patterns", () => { |
| 102 | const userMessage: UserChatMessage = { |
| 103 | role: "user", |
| 104 | content: |
| 105 | "```ts src/main.ts\nconsole.log('hello');\n```\n```ts tests/example.test.ts\ntest('should work', () => {});\n```", |
| 106 | }; |
| 107 | |
| 108 | // Rule with comma-separated glob patterns |
| 109 | const commaRule: RuleWithSource = { |
| 110 | name: "TypeScript Standards", |
| 111 | rule: "Use TypeScript best practices for comma-separated globs", |
| 112 | globs: "src/**/*.ts, tests/**/*.ts", |
| 113 | source: "rules-block", |
| 114 | }; |
| 115 | |
| 116 | const result = getSystemMessageWithRules({ |
| 117 | baseSystemMessage, |
| 118 | userMessage, |
| 119 | rules: [commaRule], |
| 120 | }); |
| 121 | |
| 122 | // With the current implementation, the comma-separated pattern is treated as a single string, |
| 123 | // so it won't match any of the file paths (minimatch doesn't support comma-separated patterns) |
| 124 | expect(result).toBe(baseSystemMessage); |
| 125 | }); |
| 126 | |
| 127 | it("should match with an array of glob patterns", () => { |
| 128 | const userMessage: UserChatMessage = { |
| 129 | role: "user", |
| 130 | content: |
| 131 | "```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```", |
| 132 | }; |
| 133 | |
| 134 | // Rule with an array of glob patterns |
| 135 | const arrayGlobRule: RuleWithSource = { |
| 136 | name: "TypeScript Standards", |
| 137 | rule: "Use TypeScript best practices for array globs", |
| 138 | globs: ["src/**/*.ts", "tests/**/*.ts"], |
| 139 | source: "rules-block", |
| 140 | }; |
| 141 | |
| 142 | const result = getSystemMessageWithRules({ |
| 143 | baseSystemMessage, |
| 144 | userMessage, |
| 145 | rules: [arrayGlobRule], |
| 146 | }); |
| 147 | |
| 148 | // With our new implementation, the array of patterns should match both src/main.ts and tests/example.test.ts |
| 149 | expect(result).toBe(`${baseSystemMessage}\n\n${arrayGlobRule.rule}`); |
| 150 | }); |
| 151 | |
| 152 | it("should match only patterns in the array", () => { |
| 153 | const userMessage: UserChatMessage = { |
| 154 | role: "user", |
| 155 | content: |
| 156 | "```ts src/main.ts\nconsole.log('hello');\n```\n```ts config/settings.ts\nconst config = {};\n```", |
| 157 | }; |
| 158 | |
| 159 | // Rule with an array of glob patterns |
| 160 | const arrayGlobRule: RuleWithSource = { |
| 161 | name: "TypeScript Standards", |
| 162 | rule: "Use TypeScript best practices for array globs", |
| 163 | globs: ["src/**/*.ts", "tests/**/*.ts"], |
| 164 | source: "rules-block", |
| 165 | }; |
| 166 | |
| 167 | const result = getSystemMessageWithRules({ |
| 168 | baseSystemMessage, |
| 169 | userMessage, |
| 170 | rules: [arrayGlobRule], |
| 171 | }); |
| 172 | |
| 173 | // Should match src/main.ts but not config/settings.ts |
| 174 | expect(result).toBe(`${baseSystemMessage}\n\n${arrayGlobRule.rule}`); |
| 175 | }); |
| 176 | |
| 177 | it("should not match any when no patterns in the array match", () => { |
| 178 | const userMessage: UserChatMessage = { |
| 179 | role: "user", |
| 180 | content: |
| 181 | "```ts config/settings.ts\nconst config = {};\n```\n```ruby test.rb\nputs 'hello'\n```", |
| 182 | }; |
| 183 | |
| 184 | // Rule with an array of glob patterns |
| 185 | const arrayGlobRule: RuleWithSource = { |
| 186 | name: "TypeScript Standards", |
| 187 | rule: "Use TypeScript best practices for array globs", |
| 188 | globs: ["src/**/*.ts", "tests/**/*.ts"], |
| 189 | source: "rules-block", |
| 190 | }; |
| 191 | |
| 192 | const result = getSystemMessageWithRules({ |
| 193 | baseSystemMessage, |
| 194 | userMessage, |
| 195 | rules: [arrayGlobRule], |
| 196 | }); |
| 197 | |
| 198 | // Should not match any file paths |
| 199 | expect(result).toBe(baseSystemMessage); |
| 200 | }); |
| 201 | }); |