diff --git a/core/llm/rules/nestedDirectoryRules.vitest.ts b/core/llm/rules/nestedDirectoryRules.vitest.ts index 90ec1b5bc..496ab8c32 100644 --- a/core/llm/rules/nestedDirectoryRules.vitest.ts +++ b/core/llm/rules/nestedDirectoryRules.vitest.ts @@ -91,37 +91,78 @@ describe("Nested directory rules application", () => { ); }); - it("should apply file extension rules across all directories", () => { - // Add a rule targeting specific file extensions (not directory-specific) - const extensionRule: RuleWithSource = { - name: "Extension Rule", - rule: "Follow best practices for this file type", - globs: "**/*.py", // Matches any .py file in any directory + it("should apply glob patterns relative to rules.md location", () => { + // A rule in src/ that targets *.py files + const srcPythonRule: RuleWithSource = { + name: "Src Python Rule", + rule: "Follow Python best practices for src directory", + globs: "**/*.py", // This should only match .py files in src/ and subdirectories source: "rules-block", ruleFile: "src/rules.md", }; - // Context with a file in the nested directory - const nestedFileContext: ContextItemWithId = { - id: "nested1", - uri: { - type: "file", - value: "manual-testing-sandbox/nested-folder/hellonested.py", - }, - content: 'print("Hello nested")', + // A rule in utils/ that targets *.py files + const utilsPythonRule: RuleWithSource = { + name: "Utils Python Rule", + rule: "Follow Python best practices for utils directory", + globs: "**/*.py", // This should only match .py files in utils/ and subdirectories + source: "rules-block", + ruleFile: "utils/rules.md", + }; + + // A file in src/ + const srcPythonFileContext: ContextItemWithId = { + id: "python1", + uri: { type: "file", value: "src/pythonfile.py" }, + content: 'print("Hello")', retrievedAt: new Date().toISOString(), }; - // Apply all rules with the nested file context - const applicableRules = getApplicableRules( + // A file in utils/ + const utilsPythonFileContext: ContextItemWithId = { + id: "python2", + uri: { type: "file", value: "utils/pythonfile.py" }, + content: 'print("Hello")', + retrievedAt: new Date().toISOString(), + }; + + // A file in manual-testing-sandbox/ + const manualTestingPythonFileContext: ContextItemWithId = { + id: "python3", + uri: { type: "file", value: "manual-testing-sandbox/pythonfile.py" }, + content: 'print("Hello")', + retrievedAt: new Date().toISOString(), + }; + + // Apply src rule to src file - should match + let applicableRules = getApplicableRules( undefined, - [nestedFolderRule, globalRule, extensionRule], - [nestedFileContext], + [srcPythonRule, utilsPythonRule], + [srcPythonFileContext], + ); + expect(applicableRules.map((r) => r.name)).toContain("Src Python Rule"); + expect(applicableRules.map((r) => r.name)).not.toContain( + "Utils Python Rule", ); - // All rules should apply (global, nested directory, and extension rule) - expect(applicableRules.map((r) => r.name)).toContain("Global Rule"); - expect(applicableRules.map((r) => r.name)).toContain("Nested Folder Rule"); - expect(applicableRules.map((r) => r.name)).toContain("Extension Rule"); + // Apply utils rule to utils file - should match + applicableRules = getApplicableRules( + undefined, + [srcPythonRule, utilsPythonRule], + [utilsPythonFileContext], + ); + expect(applicableRules.map((r) => r.name)).not.toContain("Src Python Rule"); + expect(applicableRules.map((r) => r.name)).toContain("Utils Python Rule"); + + // Apply both rules to manual-testing-sandbox file - should not match either + applicableRules = getApplicableRules( + undefined, + [srcPythonRule, utilsPythonRule], + [manualTestingPythonFileContext], + ); + expect(applicableRules.map((r) => r.name)).not.toContain("Src Python Rule"); + expect(applicableRules.map((r) => r.name)).not.toContain( + "Utils Python Rule", + ); }); });