| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { RuleWithSource, UserChatMessage } from "../.."; |
| 3 | import { getApplicableRules } from "./getSystemMessageWithRules"; |
| 4 | |
| 5 | describe("Implicit global rules application", () => { |
| 6 | // Create a rule with no alwaysApply and no globs - should behave like a global rule |
| 7 | const implicitGlobalRule: RuleWithSource = { |
| 8 | name: "Implicit Global Rule", |
| 9 | rule: "This rule should be applied to all messages (implicit global)", |
| 10 | source: "rules-block", |
| 11 | ruleFile: ".continue/global-rule.md", |
| 12 | // No alwaysApply specified |
| 13 | // No globs specified |
| 14 | }; |
| 15 | |
| 16 | // Create a rule with explicit alwaysApply: true for comparison |
| 17 | const explicitGlobalRule: RuleWithSource = { |
| 18 | name: "Explicit Global Rule", |
| 19 | rule: "This rule should always be applied (explicit global)", |
| 20 | alwaysApply: true, |
| 21 | source: "rules-block", |
| 22 | ruleFile: ".continue/explicit-global.md", |
| 23 | }; |
| 24 | |
| 25 | // Create a colocated rule in a nested directory |
| 26 | const nestedDirRule: RuleWithSource = { |
| 27 | name: "Nested Directory Rule", |
| 28 | rule: "This rule applies to files in the nested directory", |
| 29 | source: "rules-block", |
| 30 | ruleFile: "nested-folder/rules.md", |
| 31 | }; |
| 32 | |
| 33 | it("should apply rules with no alwaysApply and no globs to all messages, even with no file references", () => { |
| 34 | // Message with no code blocks or file references |
| 35 | const simpleMessage: UserChatMessage = { |
| 36 | role: "user", |
| 37 | content: "Can you help me understand how this works?", |
| 38 | }; |
| 39 | |
| 40 | // Apply rules with no context items |
| 41 | const applicableRules = getApplicableRules( |
| 42 | simpleMessage, |
| 43 | [implicitGlobalRule, explicitGlobalRule, nestedDirRule], |
| 44 | [], |
| 45 | ); |
| 46 | |
| 47 | // Both global rules should be included regardless of context |
| 48 | expect(applicableRules).toHaveLength(2); |
| 49 | expect(applicableRules.map((r) => r.name)).toContain( |
| 50 | "Implicit Global Rule", |
| 51 | ); |
| 52 | expect(applicableRules.map((r) => r.name)).toContain( |
| 53 | "Explicit Global Rule", |
| 54 | ); |
| 55 | expect(applicableRules.map((r) => r.name)).not.toContain( |
| 56 | "Nested Directory Rule", |
| 57 | ); |
| 58 | }); |
| 59 | |
| 60 | it("should treat root-level rules with no globs as global rules", () => { |
| 61 | // Root rule with no globs |
| 62 | const rootNoGlobsRule: RuleWithSource = { |
| 63 | name: "Root No Globs Rule", |
| 64 | rule: "This is a root-level rule with no globs", |
| 65 | source: "rules-block", |
| 66 | ruleFile: ".continue/rules.md", |
| 67 | // No alwaysApply, no globs |
| 68 | }; |
| 69 | |
| 70 | // Test with no file context |
| 71 | const applicableRules = getApplicableRules( |
| 72 | undefined, |
| 73 | [rootNoGlobsRule], |
| 74 | [], |
| 75 | ); |
| 76 | |
| 77 | // Should include the rule even with no context |
| 78 | expect(applicableRules).toHaveLength(1); |
| 79 | expect(applicableRules[0].name).toBe("Root No Globs Rule"); |
| 80 | }); |
| 81 | |
| 82 | it("should apply implicit global rules alongside file-specific rules", () => { |
| 83 | // File-specific message |
| 84 | const messageWithFile: UserChatMessage = { |
| 85 | role: "user", |
| 86 | content: |
| 87 | "Can you help with this file?\n```ts nested-folder/example.ts\nexport const example = () => {...}\n```", |
| 88 | }; |
| 89 | |
| 90 | // Apply all rule types together |
| 91 | const applicableRules = getApplicableRules( |
| 92 | messageWithFile, |
| 93 | [implicitGlobalRule, explicitGlobalRule, nestedDirRule], |
| 94 | [], |
| 95 | ); |
| 96 | |
| 97 | // Should include all rules |
| 98 | expect(applicableRules).toHaveLength(3); |
| 99 | expect(applicableRules.map((r) => r.name)).toContain( |
| 100 | "Implicit Global Rule", |
| 101 | ); |
| 102 | expect(applicableRules.map((r) => r.name)).toContain( |
| 103 | "Explicit Global Rule", |
| 104 | ); |
| 105 | expect(applicableRules.map((r) => r.name)).toContain( |
| 106 | "Nested Directory Rule", |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | it("should apply implicit global rules for first message without code", () => { |
| 111 | // Simple first message |
| 112 | const firstMessage: UserChatMessage = { |
| 113 | role: "user", |
| 114 | content: "Hello, can you help me?", |
| 115 | }; |
| 116 | |
| 117 | // Assistant rules that should always apply |
| 118 | const assistantRule: RuleWithSource = { |
| 119 | name: "Assistant Guidelines", |
| 120 | rule: "SOLID Design Principles - Coding Assistant Guidelines", |
| 121 | source: "rules-block", |
| 122 | ruleFile: ".continue/rules.md", |
| 123 | // No alwaysApply, no globs - should still apply |
| 124 | }; |
| 125 | |
| 126 | const applicableRules = getApplicableRules( |
| 127 | firstMessage, |
| 128 | [assistantRule], |
| 129 | [], |
| 130 | ); |
| 131 | |
| 132 | // Should include the assistant rule even though there's no code |
| 133 | expect(applicableRules).toHaveLength(1); |
| 134 | expect(applicableRules[0].name).toBe("Assistant Guidelines"); |
| 135 | }); |
| 136 | }); |