| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | ContextItemId, |
| 4 | ContextItemWithId, |
| 5 | RuleWithSource, |
| 6 | UserChatMessage, |
| 7 | } from "../.."; |
| 8 | import { getApplicableRules } from "./getSystemMessageWithRules"; |
| 9 | import { RulePolicies } from "./types"; |
| 10 | |
| 11 | describe("Rule application with alwaysApply", () => { |
| 12 | // Create an always-apply rule |
| 13 | const alwaysApplyRule: RuleWithSource = { |
| 14 | name: "Always Apply Rule", |
| 15 | rule: "This rule should always be applied", |
| 16 | alwaysApply: true, |
| 17 | source: "rules-block", |
| 18 | ruleFile: ".continue/always-apply.md", |
| 19 | }; |
| 20 | |
| 21 | // Create a colocated rule in a nested directory |
| 22 | const nestedDirRule: RuleWithSource = { |
| 23 | name: "Nested Directory Rule", |
| 24 | rule: "This rule applies to files in the nested directory", |
| 25 | source: "rules-block", |
| 26 | ruleFile: "nested-folder/rules.md", |
| 27 | }; |
| 28 | |
| 29 | it("should apply alwaysApply rules even with no file references", () => { |
| 30 | // Message with no code blocks or file references |
| 31 | const simpleMessage: UserChatMessage = { |
| 32 | role: "user", |
| 33 | content: "Can you help me understand how this works?", |
| 34 | }; |
| 35 | |
| 36 | // Apply rules with no context items |
| 37 | const applicableRules = getApplicableRules( |
| 38 | simpleMessage, |
| 39 | [alwaysApplyRule, nestedDirRule], |
| 40 | [], |
| 41 | ); |
| 42 | |
| 43 | // The always apply rule should be included regardless of context |
| 44 | expect(applicableRules).toHaveLength(1); |
| 45 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 46 | expect(applicableRules.map((r) => r.name)).not.toContain( |
| 47 | "Nested Directory Rule", |
| 48 | ); |
| 49 | }); |
| 50 | |
| 51 | it("should apply nested directory rules to files in that directory", () => { |
| 52 | // Context with a file in the nested directory |
| 53 | const nestedFileContext: ContextItemWithId = { |
| 54 | id: { providerTitle: "file", itemId: "context1" } as ContextItemId, |
| 55 | uri: { type: "file", value: "nested-folder/example.ts" }, |
| 56 | content: "export const example = () => {...}", |
| 57 | name: "example.ts", |
| 58 | description: "Example file", |
| 59 | }; |
| 60 | |
| 61 | // Apply rules with file context in nested directory |
| 62 | const applicableRules = getApplicableRules( |
| 63 | undefined, // No message needed |
| 64 | [alwaysApplyRule, nestedDirRule], |
| 65 | [nestedFileContext], |
| 66 | ); |
| 67 | |
| 68 | // Both rules should apply |
| 69 | expect(applicableRules).toHaveLength(2); |
| 70 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 71 | expect(applicableRules.map((r) => r.name)).toContain( |
| 72 | "Nested Directory Rule", |
| 73 | ); |
| 74 | }); |
| 75 | |
| 76 | it("should NOT apply nested directory rules to files outside that directory", () => { |
| 77 | // Context with a file outside the nested directory |
| 78 | const outsideFileContext: ContextItemWithId = { |
| 79 | id: { providerTitle: "file", itemId: "context2" } as ContextItemId, |
| 80 | uri: { type: "file", value: "src/utils/helper.ts" }, |
| 81 | content: "export const helper = () => {...}", |
| 82 | name: "helper.ts", |
| 83 | description: "Helper utility", |
| 84 | }; |
| 85 | |
| 86 | // Apply rules with file context outside nested directory |
| 87 | const applicableRules = getApplicableRules( |
| 88 | undefined, // No message needed |
| 89 | [alwaysApplyRule, nestedDirRule], |
| 90 | [outsideFileContext], |
| 91 | ); |
| 92 | |
| 93 | // Only the always apply rule should be included |
| 94 | expect(applicableRules).toHaveLength(1); |
| 95 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 96 | expect(applicableRules.map((r) => r.name)).not.toContain( |
| 97 | "Nested Directory Rule", |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | it("should correctly apply rules when a file is mentioned in a message", () => { |
| 102 | // Message with a file reference |
| 103 | const messageWithFile: UserChatMessage = { |
| 104 | role: "user", |
| 105 | content: |
| 106 | "Can you help with this file?\n```ts nested-folder/example.ts\nexport const example = () => {...}\n```", |
| 107 | }; |
| 108 | |
| 109 | // Apply rules with a message containing a file reference |
| 110 | const applicableRules = getApplicableRules( |
| 111 | messageWithFile, |
| 112 | [alwaysApplyRule, nestedDirRule], |
| 113 | [], |
| 114 | ); |
| 115 | |
| 116 | // Both rules should apply |
| 117 | expect(applicableRules).toHaveLength(2); |
| 118 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 119 | expect(applicableRules.map((r) => r.name)).toContain( |
| 120 | "Nested Directory Rule", |
| 121 | ); |
| 122 | }); |
| 123 | |
| 124 | it("should always apply rules with alwaysApply regardless of message or context", () => { |
| 125 | // Test with no message or context |
| 126 | let applicableRules = getApplicableRules(undefined, [alwaysApplyRule], []); |
| 127 | expect(applicableRules).toHaveLength(1); |
| 128 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 129 | |
| 130 | // Test with message but no file references and no context |
| 131 | const simpleMessage: UserChatMessage = { |
| 132 | role: "user", |
| 133 | content: "Hello, can you help me?", |
| 134 | }; |
| 135 | |
| 136 | applicableRules = getApplicableRules(simpleMessage, [alwaysApplyRule], []); |
| 137 | expect(applicableRules).toHaveLength(1); |
| 138 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 139 | }); |
| 140 | |
| 141 | it("should respect 'off' rulePolicies over alwaysApply when there are no file paths", () => { |
| 142 | // Rule with alwaysApply: true |
| 143 | const globalRule: RuleWithSource = { |
| 144 | name: "Global Rule", |
| 145 | rule: "This rule has alwaysApply: true but is blocked by rulePolicies", |
| 146 | alwaysApply: true, |
| 147 | source: "rules-block", |
| 148 | ruleFile: "src/some/path.md", |
| 149 | }; |
| 150 | |
| 151 | // Message with no code blocks or file references |
| 152 | const simpleMessage: UserChatMessage = { |
| 153 | role: "user", |
| 154 | content: "Can you help me with something?", |
| 155 | }; |
| 156 | |
| 157 | // Create a rule policy that blocks the rule |
| 158 | const rulePolicies: RulePolicies = { |
| 159 | "Global Rule": "off", |
| 160 | }; |
| 161 | |
| 162 | // The rule should NOT be applied due to the "off" policy, |
| 163 | // even though it has alwaysApply: true and there are no file paths |
| 164 | const applicableRules = getApplicableRules( |
| 165 | simpleMessage, |
| 166 | [globalRule], |
| 167 | [], |
| 168 | rulePolicies, |
| 169 | ); |
| 170 | |
| 171 | expect(applicableRules).toHaveLength(0); |
| 172 | expect(applicableRules.map((r) => r.name)).not.toContain("Global Rule"); |
| 173 | }); |
| 174 | }); |