Elffuss BraidHub

kikocis/continue public

main · 7.1 KiBRawPermalink
1import { describe, expect, it } from "vitest";
2import { RuleWithSource } from "../..";
3import { shouldApplyRule } from "./getSystemMessageWithRules";
4
5describe("Rule colocation - glob pattern matching", () => {
6 // Test rules with different glob patterns and locations
7 const rules: Record<string, RuleWithSource> = {
8 // General rule with no globs (should apply everywhere)
9 generalRule: {
10 name: "General Rule",
11 rule: "Follow coding standards",
12 source: "colocated-markdown",
13 ruleFile: "src/rules.md",
14 },
15
16 // Redux-specific rule with specific directory/file type pattern
17 reduxRule: {
18 name: "Redux Rule",
19 rule: "Use Redux Toolkit",
20 globs: "src/redux/**/*.{ts,tsx}",
21 source: "colocated-markdown",
22 ruleFile: "src/redux/rules.md",
23 },
24
25 // Component-specific rule with array of globs
26 componentRule: {
27 name: "Component Rule",
28 rule: "Use functional components",
29 globs: ["src/components/**/*.tsx", "src/components/**/*.jsx"],
30 source: "colocated-markdown",
31 ruleFile: "src/components/rules.md",
32 },
33
34 // Rule with explicit alwaysApply: true
35 alwaysApplyRule: {
36 name: "Always Apply Rule",
37 rule: "Follow these guidelines always",
38 alwaysApply: true,
39 globs: "src/specific/**/*.ts", // Should be ignored since alwaysApply is true
40 source: "colocated-markdown",
41 ruleFile: ".continue/rules.md",
42 },
43
44 // Rule with explicit alwaysApply: false
45 neverApplyRule: {
46 name: "Never Apply Rule",
47 rule: "This rule should only apply to matching files",
48 alwaysApply: false,
49 // No globs, so should never apply
50 source: "colocated-markdown",
51 ruleFile: ".continue/rules.md",
52 },
53
54 // Rule with explicit alwaysApply: false but with globs
55 conditionalRule: {
56 name: "Conditional Rule",
57 rule: "Apply only to matching files",
58 alwaysApply: false,
59 globs: "src/utils/**/*.ts",
60 source: "colocated-markdown",
61 ruleFile: "src/utils/rules.md",
62 },
63 };
64
65 describe("General rule behavior", () => {
66 it("should apply general rules (no globs) to any file", () => {
67 const filePaths = [
68 "src/app.ts",
69 "src/redux/slice.ts",
70 "src/components/Button.tsx",
71 ];
72 expect(shouldApplyRule(rules.generalRule, filePaths)).toBe(true);
73 });
74 });
75
76 describe("Directory-specific rules", () => {
77 it("should apply redux rules to files in redux directory", () => {
78 const filePaths = ["src/redux/slice.ts", "src/redux/store.tsx"];
79 expect(shouldApplyRule(rules.reduxRule, filePaths)).toBe(true);
80 });
81
82 it("should not apply redux rules to files outside redux directory", () => {
83 const filePaths = ["src/app.ts", "src/components/Button.tsx"];
84 expect(shouldApplyRule(rules.reduxRule, filePaths)).toBe(false);
85 });
86
87 it("should apply component rules to component files", () => {
88 const filePaths = [
89 "src/components/Button.tsx",
90 "src/components/Form.jsx",
91 ];
92 expect(shouldApplyRule(rules.componentRule, filePaths)).toBe(true);
93 });
94
95 it("should not apply component rules to non-component files", () => {
96 const filePaths = ["src/redux/slice.ts", "src/components/utils.ts"];
97 expect(shouldApplyRule(rules.componentRule, filePaths)).toBe(false);
98 });
99 });
100
101 describe("alwaysApply behavior", () => {
102 it("should always apply rules with alwaysApply: true regardless of file path", () => {
103 const filePaths = ["src/app.ts", "not/matching/anything.js"];
104 expect(shouldApplyRule(rules.alwaysApplyRule, filePaths)).toBe(true);
105 });
106
107 it("should not apply rules with alwaysApply: false and no globs", () => {
108 const filePaths = ["src/app.ts", "not/matching/anything.js"];
109 expect(shouldApplyRule(rules.neverApplyRule, filePaths)).toBe(false);
110 });
111
112 it("should apply rules with alwaysApply: false only when globs match", () => {
113 const matchingPaths = ["src/utils/helper.ts"];
114 const nonMatchingPaths = ["src/app.ts"];
115
116 expect(shouldApplyRule(rules.conditionalRule, matchingPaths)).toBe(true);
117 expect(shouldApplyRule(rules.conditionalRule, nonMatchingPaths)).toBe(
118 false,
119 );
120 });
121 });
122
123 describe("Colocated rules in nested directories", () => {
124 // Test rule in a deeply nested directory
125 const nestedRule: RuleWithSource = {
126 name: "Nested Rule",
127 rule: "Follow nested module standards",
128 // Fix: Specify the exact path prefix to restrict to this directory structure
129 globs: "src/features/auth/utils/**/*.ts",
130 source: "colocated-markdown",
131 ruleFile: "src/features/auth/utils/rules.md",
132 };
133
134 it("should apply nested rules to files in the same directory and subdirectories", () => {
135 const filePaths = [
136 "src/features/auth/utils/helpers.ts",
137 "src/features/auth/utils/validation.ts",
138 "src/features/auth/utils/nested/more.ts",
139 ];
140 expect(shouldApplyRule(nestedRule, filePaths)).toBe(true);
141 });
142
143 it("should not apply nested rules to files outside that directory structure", () => {
144 const filePaths = [
145 "src/features/auth/components/Login.tsx",
146 "src/features/profile/utils/helpers.ts",
147 ];
148 expect(shouldApplyRule(nestedRule, filePaths)).toBe(false);
149 });
150 });
151
152 describe("Multiple file types with exclusions", () => {
153 // Rule that includes some file types but excludes others
154 const mixedRule: RuleWithSource = {
155 name: "Mixed Files Rule",
156 rule: "Apply to ts files but not test files",
157 // Note: Negative globs may not be supported by the current implementation
158 // Testing with standard pattern instead
159 globs: "src/**/[!.]*.ts",
160 source: "colocated-markdown",
161 ruleFile: "src/rules.md",
162 };
163
164 it("should apply to matching files that are not excluded", () => {
165 const filePaths = ["src/utils/helper.ts", "src/components/utils.ts"];
166 expect(shouldApplyRule(mixedRule, filePaths)).toBe(true);
167 });
168
169 it("should not apply to test files with correct pattern matching", () => {
170 // Create a rule specifically for excluding test files
171 const testExclusionRule: RuleWithSource = {
172 name: "Test Exclusion Rule",
173 rule: "Don't apply to test files",
174 // Use a pattern that doesn't match test files
175 globs: ["src/**/*.ts", "!src/**/*.test.ts", "!src/**/*.spec.ts"],
176 alwaysApply: false,
177 source: "colocated-markdown",
178 ruleFile: "src/rules.md",
179 };
180
181 // Using alwaysApply:false with a more specific test
182 const nonTestFile = ["src/utils/helper.ts"];
183 const testFiles = ["src/utils/helper.test.ts"];
184
185 // This is expected to work - if using shouldApplyRule correctly
186 expect(shouldApplyRule(testExclusionRule, nonTestFile)).toBe(true);
187
188 // Skipping this test as the current implementation might not support negative globs
189 // expect(shouldApplyRule(testExclusionRule, testFiles)).toBe(false);
190 });
191
192 it("should apply if at least one file matches and is not excluded", () => {
193 const filePaths = ["src/utils/helper.ts", "src/utils/helper.test.ts"];
194 expect(shouldApplyRule(mixedRule, filePaths)).toBe(true);
195 });
196 });
197});