-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.test.ts
More file actions
182 lines (156 loc) · 6.47 KB
/
Copy pathconfig.test.ts
File metadata and controls
182 lines (156 loc) · 6.47 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
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 .gemini-refiner.json", () => {
const config = {
mandates: ["Always use tabs", "Write JSDoc for all functions"]
};
fs.writeFileSync(path.join(tmpDir, ".gemini-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("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, ".gemini-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, ".gemini-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, ".gemini-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, ".gemini-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", () => {
expect(ConfigManager.loadConfig()).toEqual({});
expect(ConfigManager.getSemanticConfig()).toMatchObject({
localEnabled: true,
mcpSamplingEnabled: true,
});
});
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([]);
});
});