-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.test.ts
More file actions
258 lines (222 loc) · 9.33 KB
/
Copy pathconfig.test.ts
File metadata and controls
258 lines (222 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { ConfigManager } from "../src/core/config.js";
import { AgenticBlackboard } from "../src/core/blackboard.js";
describe("ConfigManager", () => {
let tmpDir: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "config-test-"));
});
afterEach(() => {
vi.restoreAllMocks();
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it("should load mandates from .universal-refiner.json", () => {
const config = {
mandates: ["Always use tabs", "Write JSDoc for all functions"]
};
fs.writeFileSync(path.join(tmpDir, ".universal-refiner.json"), JSON.stringify(config));
const loaded = ConfigManager.loadConfig(tmpDir);
expect(loaded.mandates).toContain("Always use tabs");
expect(loaded.mandates).toHaveLength(2);
});
it("should return empty object if config missing", () => {
const loaded = ConfigManager.loadConfig(tmpDir);
expect(loaded).toEqual({});
});
it("falls back to legacy .gemini-refiner.json with a deprecation warning", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
fs.writeFileSync(path.join(tmpDir, ".gemini-refiner.json"), JSON.stringify({
mandates: ["legacy mandate"],
semantic: { models: ["legacy-model"] },
}));
expect(ConfigManager.loadConfig(tmpDir).mandates).toEqual(["legacy mandate"]);
expect(ConfigManager.getSemanticConfig(tmpDir).models).toEqual(["legacy-model"]);
expect(warn).toHaveBeenCalledWith(expect.stringContaining(".gemini-refiner.json is deprecated"));
});
it("prefers .universal-refiner.json over legacy config when both exist", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
fs.writeFileSync(path.join(tmpDir, ".gemini-refiner.json"), JSON.stringify({ mandates: ["legacy"] }));
fs.writeFileSync(path.join(tmpDir, ".universal-refiner.json"), JSON.stringify({ mandates: ["current"] }));
expect(ConfigManager.loadConfig(tmpDir).mandates).toEqual(["current"]);
expect(warn).not.toHaveBeenCalled();
});
it("mergeConfig writes the new filename while preserving loaded legacy config", () => {
vi.spyOn(console, "warn").mockImplementation(() => undefined);
fs.writeFileSync(path.join(tmpDir, ".gemini-refiner.json"), JSON.stringify({
mandates: ["legacy"],
}));
ConfigManager.mergeConfig(tmpDir, { ignoredPaths: ["dist"] });
expect(JSON.parse(fs.readFileSync(path.join(tmpDir, ".universal-refiner.json"), "utf8"))).toEqual({
mandates: ["legacy"],
ignoredPaths: ["dist"],
});
});
it("should use quality-first local semantic defaults", () => {
const config = ConfigManager.getSemanticConfig(tmpDir);
expect(config.baseUrl).toBe("http://localhost:9000/v1");
expect(config.models).toEqual(["gemma3:12b", "gemma3:1b"]);
expect(config.allowNonLoopback).toBe(false);
});
it("should merge semantic overrides with safe defaults", () => {
fs.writeFileSync(path.join(tmpDir, ".universal-refiner.json"), JSON.stringify({
semantic: { models: ["gemma3:1b"], timeoutMs: 5000 }
}));
const config = ConfigManager.getSemanticConfig(tmpDir);
expect(config.models).toEqual(["gemma3:1b"]);
expect(config.timeoutMs).toBe(5000);
expect(config.allowNonLoopback).toBe(false);
});
it("should reject malformed semantic overrides", () => {
fs.writeFileSync(path.join(tmpDir, ".universal-refiner.json"), JSON.stringify({
semantic: { models: [42], timeoutMs: -1, temperature: 99, baseUrl: "" }
}));
const config = ConfigManager.getSemanticConfig(tmpDir);
expect(config.models).toEqual(["gemma3:12b", "gemma3:1b"]);
expect(config.timeoutMs).toBe(120000);
expect(config.temperature).toBe(0.2);
expect(config.baseUrl).toBe("http://localhost:9000/v1");
});
it("returns an empty config and reports invalid JSON", () => {
fs.writeFileSync(path.join(tmpDir, ".universal-refiner.json"), "{");
const error = vi.spyOn(console, "error").mockImplementation(() => undefined);
expect(ConfigManager.loadConfig(tmpDir)).toEqual({});
expect(error).toHaveBeenCalled();
});
it("accepts all bounded semantic overrides", () => {
fs.writeFileSync(path.join(tmpDir, ".universal-refiner.json"), JSON.stringify({
semantic: {
localEnabled: false,
mcpSamplingEnabled: false,
baseUrl: " http://127.0.0.1:1234/v1 ",
models: [" primary ", " fallback "],
timeoutMs: 1,
temperature: 2,
allowNonLoopback: true,
},
}));
expect(ConfigManager.getSemanticConfig(tmpDir)).toEqual({
localEnabled: false,
mcpSamplingEnabled: false,
baseUrl: "http://127.0.0.1:1234/v1",
models: ["primary", "fallback"],
timeoutMs: 1,
temperature: 2,
allowNonLoopback: true,
});
});
it("rejects every invalid semantic override boundary", () => {
vi.spyOn(ConfigManager, "loadConfig").mockReturnValue({
semantic: {
localEnabled: "yes" as unknown as boolean,
mcpSamplingEnabled: 1 as unknown as boolean,
baseUrl: 42 as unknown as string,
models: [],
timeoutMs: Number.POSITIVE_INFINITY,
temperature: -1,
allowNonLoopback: null as unknown as boolean,
},
});
expect(ConfigManager.getSemanticConfig(tmpDir)).toEqual({
localEnabled: true,
mcpSamplingEnabled: true,
baseUrl: "http://localhost:9000/v1",
models: ["gemma3:12b", "gemma3:1b"],
timeoutMs: 120000,
temperature: 0.2,
allowNonLoopback: false,
});
});
it.each([
[[""], "blank model"],
[["valid", " "], "one blank model"],
["not-an-array", "non-array models"],
])("rejects %s", (models) => {
vi.spyOn(ConfigManager, "loadConfig").mockReturnValue({
semantic: { models: models as unknown as string[] },
});
expect(ConfigManager.getSemanticConfig(tmpDir).models).toEqual(["gemma3:12b", "gemma3:1b"]);
});
it.each([
[{ timeoutMs: "fast" as unknown as number }, "non-number timeout"],
[{ timeoutMs: 0 }, "zero timeout"],
[{ temperature: Number.NaN }, "non-finite temperature"],
[{ temperature: -0.1 }, "negative temperature"],
])("rejects %s", (semantic) => {
vi.spyOn(ConfigManager, "loadConfig").mockReturnValue({ semantic });
const config = ConfigManager.getSemanticConfig(tmpDir);
expect(config.timeoutMs).toBe(120000);
expect(config.temperature).toBe(0.2);
});
it("uses default-path overloads without requiring a config file", () => {
const previousCwd = process.cwd();
try {
process.chdir(tmpDir);
expect(ConfigManager.loadConfig()).toEqual({});
expect(ConfigManager.getSemanticConfig()).toMatchObject({
localEnabled: true,
mcpSamplingEnabled: true,
});
} finally {
process.chdir(previousCwd);
}
});
it("derives supported predictive mandates and ignores unsupported recurring keywords", () => {
vi.spyOn(AgenticBlackboard, "getLogs").mockReturnValue([
{ timestamp: "", message: "test security doc performance error refactor" },
{ timestamp: "", message: "TEST SECURITY DOC PERFORMANCE ERROR REFACTOR" },
{ timestamp: "", message: "test security doc performance error refactor" },
{ timestamp: "", message: "unrelated" },
]);
expect(ConfigManager.getPredictiveMandates()).toEqual([
"Predictive: You've asked for tests in 30% of recent prompts. Ensure comprehensive testing.",
"Predictive: Security is a recurring theme. Apply OWASP principles strictly.",
"Predictive: Frequent documentation requests detected. Ensure JSDoc/README updates.",
]);
});
it("only considers the ten most recent logs and requires three keyword matches", () => {
vi.spyOn(AgenticBlackboard, "getLogs").mockReturnValue([
{ timestamp: "", message: "test" },
{ timestamp: "", message: "test" },
...Array.from({ length: 8 }, () => ({ timestamp: "", message: "unrelated" })),
{ timestamp: "", message: "test security doc" },
{ timestamp: "", message: "security doc" },
{ timestamp: "", message: "security doc" },
]);
expect(ConfigManager.getPredictiveMandates()).toEqual([]);
});
it("getObsidianConfig returns null if not specified", () => {
expect(ConfigManager.getObsidianConfig(tmpDir)).toBeNull();
});
it("getObsidianConfig returns configured values", () => {
fs.writeFileSync(path.join(tmpDir, ".universal-refiner.json"), JSON.stringify({
obsidian: {
vaultPath: "C:/My Vault",
syncLessons: true
}
}));
expect(ConfigManager.getObsidianConfig(tmpDir)).toEqual({
vaultPath: "C:/My Vault",
syncLessons: true
});
});
it("getAtlassianConfig returns null if not specified", () => {
expect(ConfigManager.getAtlassianConfig(tmpDir)).toBeNull();
});
it("getAtlassianConfig returns configured values", () => {
fs.writeFileSync(path.join(tmpDir, ".universal-refiner.json"), JSON.stringify({
atlassian: {
baseUrl: "https://example.atlassian.net",
email: "test@example.com",
apiToken: "token"
}
}));
expect(ConfigManager.getAtlassianConfig(tmpDir)).toEqual({
baseUrl: "https://example.atlassian.net",
email: "test@example.com",
apiToken: "token"
});
});
});