| 1 | import { RuleWithSource } from "../.."; |
| 2 | import { getRequestRuleDescription } from "./requestRule"; |
| 3 | |
| 4 | describe("getRequestRuleDescription", () => { |
| 5 | it("should return no rules message when no agent-requested rules exist", () => { |
| 6 | const rules: RuleWithSource[] = [ |
| 7 | { |
| 8 | name: "Always Apply Rule", |
| 9 | description: "This rule always applies", |
| 10 | source: "default-chat", |
| 11 | rule: "Always use semicolons", |
| 12 | alwaysApply: true, |
| 13 | ruleFile: "/path/to/rule1.md", |
| 14 | }, |
| 15 | { |
| 16 | name: "Rule with Globs", |
| 17 | description: "This rule has globs", |
| 18 | source: "rules-block", |
| 19 | rule: "Use TypeScript", |
| 20 | globs: "**/*.ts", |
| 21 | ruleFile: "/path/to/rule2.md", |
| 22 | }, |
| 23 | ]; |
| 24 | |
| 25 | const result = getRequestRuleDescription(rules); |
| 26 | |
| 27 | expect(result).toBe( |
| 28 | "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + |
| 29 | "No rules available.", |
| 30 | ); |
| 31 | }); |
| 32 | |
| 33 | it("should return formatted rules when agent-requested rules exist", () => { |
| 34 | const rules: RuleWithSource[] = [ |
| 35 | { |
| 36 | name: "Agent Rule 1", |
| 37 | description: "First agent-requested rule", |
| 38 | source: "rules-block", |
| 39 | rule: "Use consistent formatting", |
| 40 | alwaysApply: false, |
| 41 | ruleFile: "/path/to/agent-rule1.md", |
| 42 | }, |
| 43 | { |
| 44 | name: "Agent Rule 2", |
| 45 | description: "Second agent-requested rule", |
| 46 | source: "default-agent", |
| 47 | rule: "Follow naming conventions", |
| 48 | alwaysApply: false, |
| 49 | ruleFile: "/path/to/agent-rule2.md", |
| 50 | }, |
| 51 | // These should be filtered out |
| 52 | { |
| 53 | name: "Always Apply Rule", |
| 54 | description: "This rule always applies", |
| 55 | source: "default-chat", |
| 56 | rule: "Always use semicolons", |
| 57 | alwaysApply: true, |
| 58 | ruleFile: "/path/to/rule1.md", |
| 59 | }, |
| 60 | { |
| 61 | name: "Rule with Globs", |
| 62 | description: "This rule has globs", |
| 63 | source: "rules-block", |
| 64 | rule: "Use TypeScript", |
| 65 | globs: "**/*.ts", |
| 66 | ruleFile: "/path/to/rule2.md", |
| 67 | }, |
| 68 | ]; |
| 69 | |
| 70 | const result = getRequestRuleDescription(rules); |
| 71 | |
| 72 | const expected = |
| 73 | "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + |
| 74 | "Agent Rule 1: First agent-requested rule\n" + |
| 75 | "Agent Rule 2: Second agent-requested rule"; |
| 76 | |
| 77 | expect(result).toBe(expected); |
| 78 | }); |
| 79 | |
| 80 | it("should handle rules with missing description", () => { |
| 81 | const rules: RuleWithSource[] = [ |
| 82 | { |
| 83 | name: "Rule Without Description", |
| 84 | source: "rules-block", |
| 85 | rule: "Some rule content", |
| 86 | alwaysApply: false, |
| 87 | ruleFile: "/path/to/no-desc-rule.md", |
| 88 | }, |
| 89 | ]; |
| 90 | |
| 91 | const result = getRequestRuleDescription(rules); |
| 92 | |
| 93 | const expected = |
| 94 | "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + |
| 95 | "Rule Without Description: undefined"; |
| 96 | |
| 97 | expect(result).toBe(expected); |
| 98 | }); |
| 99 | |
| 100 | it("should handle rules with missing name", () => { |
| 101 | const rules: RuleWithSource[] = [ |
| 102 | { |
| 103 | description: "Rule without name", |
| 104 | source: "rules-block", |
| 105 | rule: "Some rule content", |
| 106 | alwaysApply: false, |
| 107 | ruleFile: "/path/to/no-name-rule.md", |
| 108 | }, |
| 109 | ]; |
| 110 | |
| 111 | const result = getRequestRuleDescription(rules); |
| 112 | |
| 113 | const expected = |
| 114 | "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + |
| 115 | "undefined: Rule without name"; |
| 116 | |
| 117 | expect(result).toBe(expected); |
| 118 | }); |
| 119 | |
| 120 | it("should handle rules with missing ruleFile", () => { |
| 121 | const rules: RuleWithSource[] = [ |
| 122 | { |
| 123 | name: "Rule Without File", |
| 124 | description: "Rule without file path", |
| 125 | source: "rules-block", |
| 126 | rule: "Some rule content", |
| 127 | alwaysApply: false, |
| 128 | }, |
| 129 | ]; |
| 130 | |
| 131 | const result = getRequestRuleDescription(rules); |
| 132 | |
| 133 | const expected = |
| 134 | "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + |
| 135 | "Rule Without File: Rule without file path"; |
| 136 | |
| 137 | expect(result).toBe(expected); |
| 138 | }); |
| 139 | |
| 140 | it("should filter out rules with alwaysApply undefined (truthy)", () => { |
| 141 | const rules: RuleWithSource[] = [ |
| 142 | { |
| 143 | name: "Rule with undefined alwaysApply", |
| 144 | description: "This should be filtered out", |
| 145 | source: "rules-block", |
| 146 | rule: "Some rule", |
| 147 | ruleFile: "/path/to/rule.md", |
| 148 | // alwaysApply is undefined, which is truthy in the filter condition |
| 149 | }, |
| 150 | { |
| 151 | name: "Valid Agent Rule", |
| 152 | description: "This should be included", |
| 153 | source: "rules-block", |
| 154 | rule: "Some rule", |
| 155 | alwaysApply: false, |
| 156 | ruleFile: "/path/to/valid-rule.md", |
| 157 | }, |
| 158 | ]; |
| 159 | |
| 160 | const result = getRequestRuleDescription(rules); |
| 161 | |
| 162 | const expected = |
| 163 | "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + |
| 164 | "Valid Agent Rule: This should be included"; |
| 165 | |
| 166 | expect(result).toBe(expected); |
| 167 | }); |
| 168 | |
| 169 | it("should filter out rules with any globs defined", () => { |
| 170 | const rules: RuleWithSource[] = [ |
| 171 | { |
| 172 | name: "Rule with string globs", |
| 173 | description: "This should be filtered out", |
| 174 | source: "rules-block", |
| 175 | rule: "Some rule", |
| 176 | alwaysApply: false, |
| 177 | globs: "**/*.ts", |
| 178 | ruleFile: "/path/to/rule1.md", |
| 179 | }, |
| 180 | { |
| 181 | name: "Rule with array globs", |
| 182 | description: "This should also be filtered out", |
| 183 | source: "rules-block", |
| 184 | rule: "Some rule", |
| 185 | alwaysApply: false, |
| 186 | globs: ["**/*.ts", "**/*.js"], |
| 187 | ruleFile: "/path/to/rule2.md", |
| 188 | }, |
| 189 | { |
| 190 | name: "Valid Agent Rule", |
| 191 | description: "This should be included", |
| 192 | source: "rules-block", |
| 193 | rule: "Some rule", |
| 194 | alwaysApply: false, |
| 195 | ruleFile: "/path/to/valid-rule.md", |
| 196 | }, |
| 197 | ]; |
| 198 | |
| 199 | const result = getRequestRuleDescription(rules); |
| 200 | |
| 201 | const expected = |
| 202 | "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + |
| 203 | "Valid Agent Rule: This should be included"; |
| 204 | |
| 205 | expect(result).toBe(expected); |
| 206 | }); |
| 207 | |
| 208 | it("should handle empty rules array", () => { |
| 209 | const rules: RuleWithSource[] = []; |
| 210 | |
| 211 | const result = getRequestRuleDescription(rules); |
| 212 | |
| 213 | expect(result).toBe( |
| 214 | "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + |
| 215 | "No rules available.", |
| 216 | ); |
| 217 | }); |
| 218 | |
| 219 | it("should handle single agent-requested rule", () => { |
| 220 | const rules: RuleWithSource[] = [ |
| 221 | { |
| 222 | name: "Single Rule", |
| 223 | description: "The only agent-requested rule", |
| 224 | source: "rules-block", |
| 225 | rule: "Follow this guideline", |
| 226 | alwaysApply: false, |
| 227 | ruleFile: "/path/to/single-rule.md", |
| 228 | }, |
| 229 | ]; |
| 230 | |
| 231 | const result = getRequestRuleDescription(rules); |
| 232 | |
| 233 | const expected = |
| 234 | "Use this tool to retrieve additional 'rules' that contain more context/instructions based on their descriptions. Available rules:\n" + |
| 235 | "Single Rule: The only agent-requested rule"; |
| 236 | |
| 237 | expect(result).toBe(expected); |
| 238 | }); |
| 239 | }); |