| 1 | import { AssistantUnrolled } from "@continuedev/config-yaml"; |
| 2 | import { describe, expect, it, vi } from "vitest"; |
| 3 | |
| 4 | import { BaseCommandOptions } from "../commands/BaseCommandOptions.js"; |
| 5 | import { ConfigEnhancer } from "../configEnhancer.js"; |
| 6 | |
| 7 | // Mock the processRule function to avoid network calls |
| 8 | vi.mock("../hubLoader.js", () => ({ |
| 9 | processRule: vi.fn((rule: string) => { |
| 10 | // Simulate hub slug loading - return content for hub slugs |
| 11 | if (rule.includes("/") && !rule.startsWith(".") && !rule.startsWith("/")) { |
| 12 | return Promise.resolve(`Content for ${rule}`); |
| 13 | } |
| 14 | // Return as-is for direct content |
| 15 | return Promise.resolve(rule); |
| 16 | }), |
| 17 | loadPackagesFromHub: vi.fn(() => Promise.resolve([])), |
| 18 | mcpProcessor: {}, |
| 19 | modelProcessor: {}, |
| 20 | })); |
| 21 | |
| 22 | describe("Rule duplication integration test", () => { |
| 23 | it("should not duplicate rules when using --rule flag", async () => { |
| 24 | const enhancer = new ConfigEnhancer(); |
| 25 | |
| 26 | // Initial config without any rules |
| 27 | const initialConfig: AssistantUnrolled = { |
| 28 | name: "Test Assistant", |
| 29 | rules: [], |
| 30 | } as any; |
| 31 | |
| 32 | // Simulate command-line options with --rule flag |
| 33 | const options: BaseCommandOptions = { |
| 34 | rule: ["nate/spanish"], |
| 35 | }; |
| 36 | |
| 37 | // Enhance config with command-line rules |
| 38 | const enhancedConfig = await enhancer.enhanceConfig(initialConfig, options); |
| 39 | |
| 40 | // Verify the rule was added exactly once as a RuleObject |
| 41 | expect(enhancedConfig.rules).toHaveLength(1); |
| 42 | expect(enhancedConfig.rules).toEqual([ |
| 43 | { name: "nate/spanish", rule: "Content for nate/spanish" }, |
| 44 | ]); |
| 45 | }); |
| 46 | |
| 47 | it("should merge command-line rules with existing config rules", async () => { |
| 48 | const enhancer = new ConfigEnhancer(); |
| 49 | |
| 50 | // Initial config with existing rules |
| 51 | const initialConfig: AssistantUnrolled = { |
| 52 | name: "Test Assistant", |
| 53 | rules: ["existing-rule"], |
| 54 | } as any; |
| 55 | |
| 56 | // Simulate command-line options with --rule flag |
| 57 | const options: BaseCommandOptions = { |
| 58 | rule: ["nate/spanish", "direct-rule"], |
| 59 | }; |
| 60 | |
| 61 | // Enhance config with command-line rules |
| 62 | const enhancedConfig = await enhancer.enhanceConfig(initialConfig, options); |
| 63 | |
| 64 | // Verify all rules are present without duplication |
| 65 | expect(enhancedConfig.rules).toHaveLength(3); |
| 66 | expect(enhancedConfig.rules).toEqual([ |
| 67 | "existing-rule", |
| 68 | { name: "nate/spanish", rule: "Content for nate/spanish" }, |
| 69 | "direct-rule", |
| 70 | ]); |
| 71 | }); |
| 72 | |
| 73 | it("should handle rule content with frontmatter", async () => { |
| 74 | const enhancer = new ConfigEnhancer(); |
| 75 | |
| 76 | // Mock processRule to return content with frontmatter |
| 77 | const { processRule } = await import("../hubLoader.js"); |
| 78 | (processRule as any).mockImplementation((rule: string) => { |
| 79 | if (rule === "nate/spanish") { |
| 80 | return Promise.resolve(`--- |
| 81 | alwaysApply: true |
| 82 | --- |
| 83 | |
| 84 | Always respond in Spanish.`); |
| 85 | } |
| 86 | return Promise.resolve(rule); |
| 87 | }); |
| 88 | |
| 89 | const initialConfig: AssistantUnrolled = { |
| 90 | name: "Test Assistant", |
| 91 | rules: [], |
| 92 | } as any; |
| 93 | |
| 94 | const options: BaseCommandOptions = { |
| 95 | rule: ["nate/spanish"], |
| 96 | }; |
| 97 | |
| 98 | const enhancedConfig = await enhancer.enhanceConfig(initialConfig, options); |
| 99 | |
| 100 | // Hub rules should be stored as RuleObject with name and content |
| 101 | expect(enhancedConfig.rules).toHaveLength(1); |
| 102 | const ruleObj = enhancedConfig.rules?.[0] as any; |
| 103 | expect(ruleObj.name).toBe("nate/spanish"); |
| 104 | expect(ruleObj.rule).toContain("Always respond in Spanish."); |
| 105 | expect(ruleObj.rule).toContain("alwaysApply: true"); |
| 106 | }); |
| 107 | }); |