| 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | import { BaseCommandOptions } from "./commands/BaseCommandOptions.js"; |
| 4 | import { ConfigEnhancer } from "./configEnhancer.js"; |
| 5 | |
| 6 | // Mock processRule to simulate hub loading |
| 7 | vi.mock("./hubLoader.js", () => ({ |
| 8 | processRule: vi.fn((rule: string) => { |
| 9 | // Simulate hub slug loading |
| 10 | if (rule.includes("/") && !rule.startsWith(".") && !rule.startsWith("/")) { |
| 11 | return Promise.resolve(`Content for ${rule}`); |
| 12 | } |
| 13 | // Return as-is for direct content |
| 14 | return Promise.resolve(rule); |
| 15 | }), |
| 16 | loadPackagesFromHub: vi.fn(() => Promise.resolve([])), |
| 17 | mcpProcessor: {}, |
| 18 | modelProcessor: {}, |
| 19 | })); |
| 20 | |
| 21 | describe("ConfigEnhancer", () => { |
| 22 | let enhancer: ConfigEnhancer; |
| 23 | let mockConfig: any; |
| 24 | |
| 25 | beforeEach(() => { |
| 26 | enhancer = new ConfigEnhancer(); |
| 27 | mockConfig = { |
| 28 | name: "test-config", |
| 29 | version: "1.0.0", |
| 30 | models: [], |
| 31 | mcpServers: [], |
| 32 | } as any; |
| 33 | vi.clearAllMocks(); |
| 34 | }); |
| 35 | |
| 36 | it("should return unchanged config when no enhancements provided", async () => { |
| 37 | const config = await enhancer.enhanceConfig(mockConfig, {}); |
| 38 | |
| 39 | expect(config).toEqual(mockConfig); |
| 40 | }); |
| 41 | |
| 42 | it("should apply rules enhancement", async () => { |
| 43 | const options: BaseCommandOptions = { |
| 44 | rule: ["test-rule"], |
| 45 | }; |
| 46 | |
| 47 | const config = await enhancer.enhanceConfig(mockConfig, options); |
| 48 | |
| 49 | expect(config.rules).toEqual(["test-rule"]); |
| 50 | }); |
| 51 | |
| 52 | it("should apply multiple enhancements", async () => { |
| 53 | const options: BaseCommandOptions = { |
| 54 | rule: ["rule1", "rule2"], |
| 55 | mcp: [], |
| 56 | }; |
| 57 | |
| 58 | const config = await enhancer.enhanceConfig(mockConfig, options); |
| 59 | |
| 60 | expect(config.rules).toEqual(["rule1", "rule2"]); |
| 61 | }); |
| 62 | |
| 63 | it("should preserve existing rules when adding new ones", async () => { |
| 64 | mockConfig.rules = ["existing rule"]; |
| 65 | const options: BaseCommandOptions = { |
| 66 | rule: ["new rule"], |
| 67 | }; |
| 68 | |
| 69 | const config = await enhancer.enhanceConfig(mockConfig, options); |
| 70 | |
| 71 | expect(config.rules).toEqual(["existing rule", "new rule"]); |
| 72 | }); |
| 73 | |
| 74 | it("should not mutate original config", async () => { |
| 75 | const originalConfig = { ...mockConfig }; |
| 76 | const options: BaseCommandOptions = { |
| 77 | rule: ["test-rule"], |
| 78 | }; |
| 79 | |
| 80 | await enhancer.enhanceConfig(mockConfig, options); |
| 81 | |
| 82 | expect(mockConfig).toEqual(originalConfig); |
| 83 | }); |
| 84 | |
| 85 | it("should preserve hub slug as rule name", async () => { |
| 86 | const options: BaseCommandOptions = { |
| 87 | rule: ["nate/spanish"], |
| 88 | }; |
| 89 | |
| 90 | const config = await enhancer.enhanceConfig(mockConfig, options); |
| 91 | |
| 92 | expect(config.rules).toHaveLength(1); |
| 93 | expect(config.rules?.[0]).toEqual({ |
| 94 | name: "nate/spanish", |
| 95 | rule: "Content for nate/spanish", |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | it("should handle mix of hub slugs and direct content", async () => { |
| 100 | const options: BaseCommandOptions = { |
| 101 | rule: ["nate/spanish", "Always be helpful", "org/another-rule"], |
| 102 | }; |
| 103 | |
| 104 | const config = await enhancer.enhanceConfig(mockConfig, options); |
| 105 | |
| 106 | expect(config.rules).toHaveLength(3); |
| 107 | expect(config.rules?.[0]).toEqual({ |
| 108 | name: "nate/spanish", |
| 109 | rule: "Content for nate/spanish", |
| 110 | }); |
| 111 | expect(config.rules?.[1]).toBe("Always be helpful"); |
| 112 | expect(config.rules?.[2]).toEqual({ |
| 113 | name: "org/another-rule", |
| 114 | rule: "Content for org/another-rule", |
| 115 | }); |
| 116 | }); |
| 117 | |
| 118 | it("should treat file paths as plain strings", async () => { |
| 119 | const options: BaseCommandOptions = { |
| 120 | rule: ["./rules/my-rule.md", "/absolute/path/rule.txt"], |
| 121 | }; |
| 122 | |
| 123 | const config = await enhancer.enhanceConfig(mockConfig, options); |
| 124 | |
| 125 | expect(config.rules).toHaveLength(2); |
| 126 | // File paths should be stored as plain strings |
| 127 | expect(config.rules?.[0]).toBe("./rules/my-rule.md"); |
| 128 | expect(config.rules?.[1]).toBe("/absolute/path/rule.txt"); |
| 129 | }); |
| 130 | |
| 131 | it("should prepend models from --model flag to make them default", async () => { |
| 132 | // Mock loadPackagesFromHub to return test models |
| 133 | const { loadPackagesFromHub } = await import("./hubLoader.js"); |
| 134 | (loadPackagesFromHub as any).mockResolvedValueOnce([ |
| 135 | { name: "GPT-5", provider: "openai", model: "gpt-5" }, |
| 136 | { name: "Claude-3", provider: "anthropic", model: "claude-3" }, |
| 137 | ]); |
| 138 | |
| 139 | // Set up existing models in config |
| 140 | mockConfig.models = [ |
| 141 | { name: "GPT-4", provider: "openai", model: "gpt-4" }, |
| 142 | { name: "Claude-2", provider: "anthropic", model: "claude-2" }, |
| 143 | ]; |
| 144 | |
| 145 | const options: BaseCommandOptions = { |
| 146 | model: ["openai/gpt-5", "anthropic/claude-3"], |
| 147 | }; |
| 148 | |
| 149 | const config = await enhancer.enhanceConfig(mockConfig, options); |
| 150 | |
| 151 | // Models from --model flag should be prepended (at the start) |
| 152 | expect(config.models).toHaveLength(4); |
| 153 | expect(config.models?.[0]).toEqual({ |
| 154 | name: "GPT-5", |
| 155 | provider: "openai", |
| 156 | model: "gpt-5", |
| 157 | }); |
| 158 | expect(config.models?.[1]).toEqual({ |
| 159 | name: "Claude-3", |
| 160 | provider: "anthropic", |
| 161 | model: "claude-3", |
| 162 | }); |
| 163 | expect(config.models?.[2]).toEqual({ |
| 164 | name: "GPT-4", |
| 165 | provider: "openai", |
| 166 | model: "gpt-4", |
| 167 | }); |
| 168 | expect(config.models?.[3]).toEqual({ |
| 169 | name: "Claude-2", |
| 170 | provider: "anthropic", |
| 171 | model: "claude-2", |
| 172 | }); |
| 173 | }); |
| 174 | |
| 175 | it("should prepend MCPs from --mcp flag", async () => { |
| 176 | // Mock loadPackagesFromHub to return test MCPs |
| 177 | const { loadPackagesFromHub } = await import("./hubLoader.js"); |
| 178 | (loadPackagesFromHub as any).mockResolvedValueOnce([ |
| 179 | { name: "New-MCP", command: "new-mcp" }, |
| 180 | ]); |
| 181 | |
| 182 | // Set up existing MCPs in config |
| 183 | mockConfig.mcpServers = [{ name: "Existing-MCP", command: "existing-mcp" }]; |
| 184 | |
| 185 | const options: BaseCommandOptions = { |
| 186 | mcp: ["test/new-mcp"], |
| 187 | }; |
| 188 | |
| 189 | const config = await enhancer.enhanceConfig(mockConfig, options); |
| 190 | |
| 191 | // MCPs from --mcp flag should be prepended (at the start) |
| 192 | expect(config.mcpServers).toHaveLength(2); |
| 193 | expect(config.mcpServers?.[0]).toEqual({ |
| 194 | name: "New-MCP", |
| 195 | command: "new-mcp", |
| 196 | }); |
| 197 | expect(config.mcpServers?.[1]).toEqual({ |
| 198 | name: "Existing-MCP", |
| 199 | command: "existing-mcp", |
| 200 | }); |
| 201 | }); |
| 202 | }); |