-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconfig.test.ts
More file actions
333 lines (277 loc) · 9.7 KB
/
Copy pathconfig.test.ts
File metadata and controls
333 lines (277 loc) · 9.7 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { afterEach, describe, expect, test } from "bun:test";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import {
DEFAULT_CONFIG,
getSessionStartPersistedConfig,
isLoopbackEndpoint,
loadConfigWithSource,
needsOnboarding,
saveConfig,
type VoiceConfig,
} from "../extensions/voice/config";
const tempDirs: string[] = [];
function makeTempDir(): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-voice-config-test-"));
tempDirs.push(dir);
return dir;
}
function writeSettings(baseDir: string, relativePath: string, voice: unknown) {
const fullPath = path.join(baseDir, relativePath);
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, JSON.stringify({ voice }, null, 2));
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
describe("loadConfigWithSource", () => {
test("returns defaults with incomplete onboarding when no settings exist", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
const result = loadConfigWithSource(cwd, { agentDir });
expect(result.source).toBe("default");
expect(result.config.enabled).toBe(true);
expect(result.config.onboarding.completed).toBe(false);
expect(result.config.scope).toBe("global");
});
test("migrates legacy global config and marks onboarding complete when backend and model were explicit", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
// Legacy config had backend + model fields — migration still recognizes them
writeSettings(agentDir, "settings.json", {
enabled: true,
language: "en",
backend: "deepgram",
model: "nova-3",
});
const result = loadConfigWithSource(cwd, { agentDir });
expect(result.source).toBe("global");
expect(result.config.scope).toBe("global");
expect(result.config.onboarding.completed).toBe(true);
expect(result.config.onboarding.schemaVersion).toBe(result.config.version);
});
test("keeps onboarding incomplete for partial legacy config", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
writeSettings(agentDir, "settings.json", {
enabled: true,
});
const result = loadConfigWithSource(cwd, { agentDir });
expect(result.source).toBe("global");
expect(result.config.onboarding.completed).toBe(false);
});
test("loads Deepgram keyterms from settings", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
writeSettings(agentDir, "settings.json", {
enabled: true,
deepgramKeyterms: ["Raycast", "", " ", "VS Code"],
});
const result = loadConfigWithSource(cwd, { agentDir });
expect(result.config.deepgramKeyterms).toEqual(["Raycast", "VS Code"]);
});
test("prefers project config over global config and preserves project scope", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
writeSettings(agentDir, "settings.json", {
enabled: true,
language: "en",
backend: "deepgram",
model: "nova-3",
});
writeSettings(cwd, ".pi/settings.json", {
version: 2,
enabled: true,
language: "en",
scope: "project",
onboarding: {
completed: true,
schemaVersion: 2,
},
});
const result = loadConfigWithSource(cwd, { agentDir });
expect(result.source).toBe("project");
expect(result.config.scope).toBe("project");
});
});
describe("needsOnboarding", () => {
test("suppresses the startup prompt for a recent remind-me-later marker", () => {
const config: VoiceConfig = {
...DEFAULT_CONFIG,
onboarding: {
...DEFAULT_CONFIG.onboarding,
skippedAt: new Date().toISOString(),
},
};
expect(needsOnboarding(config, "default")).toBe(false);
});
test("requires onboarding again once the remind-me-later window expires", () => {
const config: VoiceConfig = {
...DEFAULT_CONFIG,
onboarding: {
...DEFAULT_CONFIG.onboarding,
skippedAt: new Date(Date.now() - 1000 * 60 * 60 * 25).toISOString(),
},
};
expect(needsOnboarding(config, "default")).toBe(true);
});
});
describe("saveConfig", () => {
test("does not persist an env-only Deepgram key during session-start saves", () => {
const persisted = getSessionStartPersistedConfig({
config: {
...DEFAULT_CONFIG,
scope: "global",
onboarding: {
completed: true,
schemaVersion: DEFAULT_CONFIG.version,
},
},
envDeepgramApiKey: "env-secret-123",
});
expect(persisted.deepgramApiKey).toBeUndefined();
});
test("preserves an explicitly stored Deepgram key during session-start saves", () => {
const persisted = getSessionStartPersistedConfig({
config: {
...DEFAULT_CONFIG,
scope: "global",
deepgramApiKey: "saved-secret-123",
onboarding: {
completed: true,
schemaVersion: DEFAULT_CONFIG.version,
},
},
envDeepgramApiKey: "env-secret-123",
});
expect(persisted.deepgramApiKey).toBe("saved-secret-123");
});
test("writes global settings when scope is global", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
const config: VoiceConfig = {
...DEFAULT_CONFIG,
scope: "global",
onboarding: {
completed: true,
schemaVersion: DEFAULT_CONFIG.version,
},
};
const savedPath = saveConfig(config, "global", cwd, { agentDir });
const saved = JSON.parse(fs.readFileSync(savedPath, "utf8"));
expect(savedPath).toBe(path.join(agentDir, "settings.json"));
expect(saved.voice.scope).toBe("global");
});
test("writes project settings when scope is project", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
const config: VoiceConfig = {
...DEFAULT_CONFIG,
scope: "project",
onboarding: {
completed: true,
schemaVersion: DEFAULT_CONFIG.version,
},
};
const savedPath = saveConfig(config, "project", cwd, { agentDir });
const saved = JSON.parse(fs.readFileSync(savedPath, "utf8"));
expect(savedPath).toBe(path.join(cwd, ".pi", "settings.json"));
expect(saved.voice.scope).toBe("project");
});
test("strips deepgramApiKey from project-scoped saves", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
const config: VoiceConfig = {
...DEFAULT_CONFIG,
scope: "project",
deepgramApiKey: "secret-key-abc123",
onboarding: { completed: true, schemaVersion: DEFAULT_CONFIG.version },
};
const savedPath = saveConfig(config, "project", cwd, { agentDir });
const saved = JSON.parse(fs.readFileSync(savedPath, "utf8"));
expect(saved.voice.deepgramApiKey).toBeUndefined();
});
test("preserves deepgramApiKey in global-scoped saves", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
const config: VoiceConfig = {
...DEFAULT_CONFIG,
scope: "global",
deepgramApiKey: "secret-key-abc123",
onboarding: { completed: true, schemaVersion: DEFAULT_CONFIG.version },
};
const savedPath = saveConfig(config, "global", cwd, { agentDir });
const saved = JSON.parse(fs.readFileSync(savedPath, "utf8"));
expect(saved.voice.deepgramApiKey).toBe("secret-key-abc123");
});
test("strips non-loopback localEndpoint from project-scoped saves", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
const config: VoiceConfig = {
...DEFAULT_CONFIG,
scope: "project",
localEndpoint: "http://evil.com:8080",
onboarding: { completed: true, schemaVersion: DEFAULT_CONFIG.version },
};
const savedPath = saveConfig(config, "project", cwd, { agentDir });
const saved = JSON.parse(fs.readFileSync(savedPath, "utf8"));
expect(saved.voice.localEndpoint).toBeUndefined();
});
test("preserves loopback localEndpoint in project-scoped saves", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
const config: VoiceConfig = {
...DEFAULT_CONFIG,
scope: "project",
localEndpoint: "http://localhost:8080",
onboarding: { completed: true, schemaVersion: DEFAULT_CONFIG.version },
};
const savedPath = saveConfig(config, "project", cwd, { agentDir });
const saved = JSON.parse(fs.readFileSync(savedPath, "utf8"));
expect(saved.voice.localEndpoint).toBe("http://localhost:8080");
});
test("atomic write leaves no .tmp files after save", () => {
const cwd = makeTempDir();
const agentDir = path.join(cwd, "agent-home");
const config: VoiceConfig = {
...DEFAULT_CONFIG,
onboarding: { completed: true, schemaVersion: DEFAULT_CONFIG.version },
};
const savedPath = saveConfig(config, "global", cwd, { agentDir });
const dir = path.dirname(savedPath);
const tmpFiles = fs.readdirSync(dir).filter(f => f.endsWith(".tmp"));
expect(tmpFiles).toHaveLength(0);
expect(JSON.parse(fs.readFileSync(savedPath, "utf8"))).toBeDefined();
});
});
describe("isLoopbackEndpoint", () => {
test("accepts localhost", () => {
expect(isLoopbackEndpoint("http://localhost:8080")).toBe(true);
});
test("accepts 127.0.0.1", () => {
expect(isLoopbackEndpoint("http://127.0.0.1:9090")).toBe(true);
});
test("accepts ::1", () => {
expect(isLoopbackEndpoint("http://[::1]:8080")).toBe(true);
});
test("accepts https localhost", () => {
expect(isLoopbackEndpoint("https://localhost:8443")).toBe(true);
});
test("rejects remote hosts", () => {
expect(isLoopbackEndpoint("http://evil.com:8080")).toBe(false);
});
test("rejects private IPs", () => {
expect(isLoopbackEndpoint("http://192.168.1.1:8080")).toBe(false);
});
test("rejects non-http protocols", () => {
expect(isLoopbackEndpoint("ftp://localhost:21")).toBe(false);
expect(isLoopbackEndpoint("file://localhost/etc/passwd")).toBe(false);
});
test("rejects invalid URLs", () => {
expect(isLoopbackEndpoint("not-a-url")).toBe(false);
expect(isLoopbackEndpoint("")).toBe(false);
});
});