Elffuss BraidHub

kikocis/continue public

fc89067dc2f1 · 5.8 KiBRawPermalink
1import { beforeEach, describe, expect, it, vi } from "vitest";
2
3import { BaseCommandOptions } from "./commands/BaseCommandOptions.js";
4import { ConfigEnhancer } from "./configEnhancer.js";
5
6// Mock processRule to simulate hub loading
7vi.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
21describe("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({ name: "GPT-5", provider: "openai", model: "gpt-5" });
154 expect(config.models?.[1]).toEqual({ name: "Claude-3", provider: "anthropic", model: "claude-3" });
155 expect(config.models?.[2]).toEqual({ name: "GPT-4", provider: "openai", model: "gpt-4" });
156 expect(config.models?.[3]).toEqual({ name: "Claude-2", provider: "anthropic", model: "claude-2" });
157 });
158
159 it("should prepend MCPs from --mcp flag", async () => {
160 // Mock loadPackagesFromHub to return test MCPs
161 const { loadPackagesFromHub } = await import("./hubLoader.js");
162 (loadPackagesFromHub as any).mockResolvedValueOnce([
163 { name: "New-MCP", command: "new-mcp" },
164 ]);
165
166 // Set up existing MCPs in config
167 mockConfig.mcpServers = [
168 { name: "Existing-MCP", command: "existing-mcp" },
169 ];
170
171 const options: BaseCommandOptions = {
172 mcp: ["test/new-mcp"],
173 };
174
175 const config = await enhancer.enhanceConfig(mockConfig, options);
176
177 // MCPs from --mcp flag should be prepended (at the start)
178 expect(config.mcpServers).toHaveLength(2);
179 expect(config.mcpServers?.[0]).toEqual({ name: "New-MCP", command: "new-mcp" });
180 expect(config.mcpServers?.[1]).toEqual({ name: "Existing-MCP", command: "existing-mcp" });
181 });
182});