| 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 | |
| 10 | describe("Rule application with alwaysApply", () => { |
| 11 | // Create an always-apply rule |
| 12 | const alwaysApplyRule: RuleWithSource = { |
| 13 | name: "Always Apply Rule", |
| 14 | rule: "This rule should always be applied", |
| 15 | alwaysApply: true, |
| 16 | source: "rules-block", |
| 17 | ruleFile: ".continue/always-apply.md", |
| 18 | }; |
| 19 | |
| 20 | // Create a colocated rule in a nested directory |
| 21 | const nestedDirRule: RuleWithSource = { |
| 22 | name: "Nested Directory Rule", |
| 23 | rule: "This rule applies to files in the nested directory", |
| 24 | source: "rules-block", |
| 25 | ruleFile: "nested-folder/rules.md", |
| 26 | }; |
| 27 | |
| 28 | it("should apply alwaysApply rules even with no file references", () => { |
| 29 | // Message with no code blocks or file references |
| 30 | const simpleMessage: UserChatMessage = { |
| 31 | role: "user", |
| 32 | content: "Can you help me understand how this works?", |
| 33 | }; |
| 34 | |
| 35 | // Apply rules with no context items |
| 36 | const applicableRules = getApplicableRules( |
| 37 | simpleMessage, |
| 38 | [alwaysApplyRule, nestedDirRule], |
| 39 | [], |
| 40 | ); |
| 41 | |
| 42 | // The always apply rule should be included regardless of context |
| 43 | expect(applicableRules).toHaveLength(1); |
| 44 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 45 | expect(applicableRules.map((r) => r.name)).not.toContain( |
| 46 | "Nested Directory Rule", |
| 47 | ); |
| 48 | }); |
| 49 | |
| 50 | it("should apply nested directory rules to files in that directory", () => { |
| 51 | // Context with a file in the nested directory |
| 52 | const nestedFileContext: ContextItemWithId = { |
| 53 | id: { providerTitle: "file", itemId: "context1" } as ContextItemId, |
| 54 | uri: { type: "file", value: "nested-folder/example.ts" }, |
| 55 | content: "export const example = () => {...}", |
| 56 | name: "example.ts", |
| 57 | description: "Example file", |
| 58 | }; |
| 59 | |
| 60 | // Apply rules with file context in nested directory |
| 61 | const applicableRules = getApplicableRules( |
| 62 | undefined, // No message needed |
| 63 | [alwaysApplyRule, nestedDirRule], |
| 64 | [nestedFileContext], |
| 65 | ); |
| 66 | |
| 67 | // Both rules should apply |
| 68 | expect(applicableRules).toHaveLength(2); |
| 69 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 70 | expect(applicableRules.map((r) => r.name)).toContain( |
| 71 | "Nested Directory Rule", |
| 72 | ); |
| 73 | }); |
| 74 | |
| 75 | it("should NOT apply nested directory rules to files outside that directory", () => { |
| 76 | // Context with a file outside the nested directory |
| 77 | const outsideFileContext: ContextItemWithId = { |
| 78 | id: { providerTitle: "file", itemId: "context2" } as ContextItemId, |
| 79 | uri: { type: "file", value: "src/utils/helper.ts" }, |
| 80 | content: "export const helper = () => {...}", |
| 81 | name: "helper.ts", |
| 82 | description: "Helper utility", |
| 83 | }; |
| 84 | |
| 85 | // Apply rules with file context outside nested directory |
| 86 | const applicableRules = getApplicableRules( |
| 87 | undefined, // No message needed |
| 88 | [alwaysApplyRule, nestedDirRule], |
| 89 | [outsideFileContext], |
| 90 | ); |
| 91 | |
| 92 | // Only the always apply rule should be included |
| 93 | expect(applicableRules).toHaveLength(1); |
| 94 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 95 | expect(applicableRules.map((r) => r.name)).not.toContain( |
| 96 | "Nested Directory Rule", |
| 97 | ); |
| 98 | }); |
| 99 | |
| 100 | it("should correctly apply rules when a file is mentioned in a message", () => { |
| 101 | // Message with a file reference |
| 102 | const messageWithFile: UserChatMessage = { |
| 103 | role: "user", |
| 104 | content: |
| 105 | "Can you help with this file?\n```ts nested-folder/example.ts\nexport const example = () => {...}\n```", |
| 106 | }; |
| 107 | |
| 108 | // Apply rules with a message containing a file reference |
| 109 | const applicableRules = getApplicableRules( |
| 110 | messageWithFile, |
| 111 | [alwaysApplyRule, nestedDirRule], |
| 112 | [], |
| 113 | ); |
| 114 | |
| 115 | // Both rules should apply |
| 116 | expect(applicableRules).toHaveLength(2); |
| 117 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 118 | expect(applicableRules.map((r) => r.name)).toContain( |
| 119 | "Nested Directory Rule", |
| 120 | ); |
| 121 | }); |
| 122 | |
| 123 | it("should always apply rules with alwaysApply regardless of message or context", () => { |
| 124 | // Test with no message or context |
| 125 | let applicableRules = getApplicableRules(undefined, [alwaysApplyRule], []); |
| 126 | expect(applicableRules).toHaveLength(1); |
| 127 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 128 | |
| 129 | // Test with message but no file references and no context |
| 130 | const simpleMessage: UserChatMessage = { |
| 131 | role: "user", |
| 132 | content: "Hello, can you help me?", |
| 133 | }; |
| 134 | |
| 135 | applicableRules = getApplicableRules(simpleMessage, [alwaysApplyRule], []); |
| 136 | expect(applicableRules).toHaveLength(1); |
| 137 | expect(applicableRules.map((r) => r.name)).toContain("Always Apply Rule"); |
| 138 | }); |
| 139 | }); |