diff --git a/core/llm/rules/getSystemMessageWithRules.vitest.ts b/core/llm/rules/getSystemMessageWithRules.vitest.ts index f652d3a77..8425c8324 100644 --- a/core/llm/rules/getSystemMessageWithRules.vitest.ts +++ b/core/llm/rules/getSystemMessageWithRules.vitest.ts @@ -1,6 +1,14 @@ import { describe, expect, it } from "vitest"; -import { RuleWithSource } from "../.."; -import { shouldApplyRule } from "./getSystemMessageWithRules"; +import { + ContextItemId, + ContextItemWithId, + RuleWithSource, + UserChatMessage, +} from "../.."; +import { + getApplicableRules, + shouldApplyRule, +} from "./getSystemMessageWithRules"; import { RulePolicies } from "./types"; describe("Rule colocation glob matching", () => { @@ -244,3 +252,228 @@ describe("Rule policies", () => { ).toBe(false); }); }); + +describe("Content pattern matching", () => { + it("should apply rules when file content matches pattern", () => { + // Rule with pattern to match React component files + const reactComponentRule: RuleWithSource = { + name: "React Component Rule", + rule: "Follow React component standards", + patterns: "export (function|const) \\w+.*\\(", + source: "rules-block", + ruleFile: "src/components/rules.md", + }; + + // File that matches the pattern + const filePath = "src/components/Button.tsx"; + const fileContents = { + [filePath]: + "export function Button() { return }", + }; + + expect( + shouldApplyRule(reactComponentRule, [filePath], {}, fileContents), + ).toBe(true); + + // File that doesn't match the pattern + const nonMatchingContents = { + [filePath]: + "import React from 'react'; // Just an import, not a component", + }; + + expect( + shouldApplyRule(reactComponentRule, [filePath], {}, nonMatchingContents), + ).toBe(false); + }); + + it("should support multiple regex patterns as an array", () => { + // Rule with multiple patterns + const multiPatternRule: RuleWithSource = { + name: "Multi-Pattern Rule", + rule: "Apply to functions or classes", + patterns: ["function\\s+\\w+", "class\\s+\\w+"], + source: "rules-block", + ruleFile: "src/utils/rules.md", + }; + + const filePath = "src/utils/helper.ts"; + + // Content with function - should match + const functionContent = { + [filePath]: "function formatDate(date) { return date.toISOString(); }", + }; + expect( + shouldApplyRule(multiPatternRule, [filePath], {}, functionContent), + ).toBe(true); + + // Content with class - should match + const classContent = { + [filePath]: + "class DateFormatter { format(date) { return date.toISOString(); } }", + }; + expect( + shouldApplyRule(multiPatternRule, [filePath], {}, classContent), + ).toBe(true); + + // Content with neither - should not match + const nonMatchingContent = { + [filePath]: "const PI = 3.14159;", + }; + expect( + shouldApplyRule(multiPatternRule, [filePath], {}, nonMatchingContent), + ).toBe(false); + }); + + it("should apply rules based on both globs and patterns when both are specified", () => { + // Rule with both glob and pattern restrictions + const typescriptClassRule: RuleWithSource = { + name: "TypeScript Class Rule", + rule: "Follow class standards in TypeScript", + globs: "**/*.ts", + patterns: "class\\s+\\w+", + source: "rules-block", + ruleFile: "src/models/rules.md", + }; + + const tsFilePath = "src/models/User.ts"; + const jsFilePath = "src/models/Product.js"; + + // TypeScript file with class - should match + const tsClassContent = { + [tsFilePath]: "export class User { constructor(public name: string) {} }", + }; + expect( + shouldApplyRule(typescriptClassRule, [tsFilePath], {}, tsClassContent), + ).toBe(true); + + // TypeScript file without class - should not match + const tsNonClassContent = { + [tsFilePath]: "export const createUser = (name: string) => ({ name });", + }; + expect( + shouldApplyRule(typescriptClassRule, [tsFilePath], {}, tsNonClassContent), + ).toBe(false); + + // JavaScript file with class - should not match due to glob + const jsClassContent = { + [jsFilePath]: + "export class Product { constructor(name) { this.name = name; } }", + }; + expect( + shouldApplyRule(typescriptClassRule, [jsFilePath], {}, jsClassContent), + ).toBe(false); + }); + + it("should extract file content from messages for pattern matching", () => { + // Rule with pattern + const arrowFunctionRule: RuleWithSource = { + name: "Arrow Function Rule", + rule: "Use arrow functions for better this binding", + patterns: "const\\s+\\w+\\s*=\\s*\\(", + source: "rules-block", + ruleFile: "src/utils/rules.md", + }; + + // Message with code block containing a matching pattern + const messageWithArrowFn: UserChatMessage = { + role: "user", + content: + "Can you check this code?\n```typescript src/utils/formatter.ts\nconst format = (date) => {\n return date.toISOString();\n};\n```", + }; + + // Create context item to match the file in the message + const contextItem: ContextItemWithId = { + id: { providerTitle: "file", itemId: "formatter" } as ContextItemId, + uri: { type: "file", value: "src/utils/formatter.ts" }, + content: "const format = (date) => {\n return date.toISOString();\n};", + name: "formatter.ts", + description: "Utility formatter", + }; + + // Should match the pattern + const applicableRules = getApplicableRules( + messageWithArrowFn, + [arrowFunctionRule], + [contextItem], + ); + + expect(applicableRules).toHaveLength(1); + expect(applicableRules[0].name).toBe("Arrow Function Rule"); + + // Message with code block that doesn't match the pattern + const messageWithoutArrowFn: UserChatMessage = { + role: "user", + content: + "Can you check this code?\n```typescript src/utils/formatter.ts\nfunction format(date) {\n return date.toISOString();\n}\n```", + }; + + // Context item with non-matching content + const nonMatchingContextItem: ContextItemWithId = { + id: { providerTitle: "file", itemId: "formatter2" } as ContextItemId, + uri: { type: "file", value: "src/utils/formatter.ts" }, + content: "function format(date) {\n return date.toISOString();\n}", + name: "formatter.ts", + description: "Utility formatter", + }; + + // Should not match the pattern + const nonApplicableRules = getApplicableRules( + messageWithoutArrowFn, + [arrowFunctionRule], + [nonMatchingContextItem], + ); + + expect(nonApplicableRules).toHaveLength(0); + }); + + it("should handle invalid regex patterns gracefully", () => { + // Rule with invalid regex pattern + const invalidRegexRule: RuleWithSource = { + name: "Invalid Regex Rule", + rule: "This rule has an invalid regex pattern", + patterns: "([unclosed parenthesis", + source: "rules-block", + ruleFile: "src/utils/rules.md", + }; + + const filePath = "src/utils/helper.ts"; + const fileContents = { + [filePath]: "function helper() { return true; }", + }; + + // Should not throw an error but gracefully not apply the rule + expect( + shouldApplyRule(invalidRegexRule, [filePath], {}, fileContents), + ).toBe(false); + }); + + it("should respect directory restrictions for pattern rules", () => { + // Rule with pattern in a nested directory + const nestedPatternRule: RuleWithSource = { + name: "Nested Pattern Rule", + rule: "Apply only to model files with interfaces", + patterns: "interface\\s+\\w+", + source: "rules-block", + ruleFile: "src/models/rules.md", + }; + + // File in the correct directory with matching content + const modelFilePath = "src/models/User.ts"; + const modelContents = { + [modelFilePath]: "export interface User { id: string; name: string; }", + }; + expect( + shouldApplyRule(nestedPatternRule, [modelFilePath], {}, modelContents), + ).toBe(true); + + // File outside the directory with matching content - should not match + const utilFilePath = "src/utils/types.ts"; + const utilContents = { + [utilFilePath]: + "export interface Config { apiUrl: string; timeout: number; }", + }; + expect( + shouldApplyRule(nestedPatternRule, [utilFilePath], {}, utilContents), + ).toBe(false); + }); +});