forked from nexu-io/open-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-config-update-install-mode.test.ts
More file actions
277 lines (237 loc) · 10.8 KB
/
Copy pathapp-config-update-install-mode.test.ts
File metadata and controls
277 lines (237 loc) · 10.8 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
// Tests for updateInstallMode pref — part of #4467 (PR1).
//
// Spec: apps/daemon/src/app-config.ts gains:
// - `updateInstallMode?: 'automatic' | 'manual'` on AppConfigPrefs
// - key added to ALLOWED_KEYS
// - applyConfigValue validates the enum (accept 'automatic'/'manual'; reject anything else)
// - absent/unset treated as 'automatic' (no migration needed)
//
// These tests are RED until the implementation lands.
//
// Additional tests added for #4467 (PR2) — CLI unset regression:
// - Daemon-level null-clear guard (non-regression; likely green already)
// - CLI `od config unset updateInstallMode` must send { updateInstallMode: null }
// in the PUT body so the merge-style writeAppConfig actually clears the value.
// The current code (delete next[key]) omits the key, leaving 'manual' stored.
import http from 'node:http';
import { spawn } from 'node:child_process';
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path, { dirname, resolve as pathResolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { readAppConfig, writeAppConfig } from '../src/app-config.js';
// ---------------------------------------------------------------------------
// Helpers shared by the CLI stub tests
// ---------------------------------------------------------------------------
const __filename = fileURLToPath(import.meta.url);
const __dirnameLocal = dirname(__filename);
const REPO_ROOT = pathResolve(__dirnameLocal, '../../..');
const CLI_SRC = pathResolve(__dirnameLocal, '../src/cli.ts');
const TSX_CLI = pathResolve(REPO_ROOT, 'node_modules/tsx/dist/cli.mjs');
interface CapturedRequest {
method: string;
url: string;
body: string;
}
interface StubServer {
baseUrl: string;
requests: CapturedRequest[];
/** Replace the response for the next matching call. */
setNextGetResponse: (body: unknown) => void;
close: () => Promise<void>;
}
/** Minimal HTTP stub that records every request body. The GET /api/app-config
* endpoint returns the most-recently set mock response (default: empty config).
* All other methods return 200 with an empty config. */
async function startConfigStubServer(): Promise<StubServer> {
const requests: CapturedRequest[] = [];
let nextGetBody: unknown = { config: {} };
const server = http.createServer((req, res) => {
let raw = '';
req.on('data', (chunk: Buffer) => {
raw += chunk.toString();
});
req.on('end', () => {
requests.push({ method: req.method ?? '', url: req.url ?? '', body: raw });
res.statusCode = 200;
res.setHeader('content-type', 'application/json');
if (req.method === 'GET') {
res.end(JSON.stringify(nextGetBody));
} else {
// PUT — echo back the written config so the CLI can log it.
let written: unknown = {};
try { written = JSON.parse(raw); } catch { /* ignore */ }
res.end(JSON.stringify({ config: written }));
}
});
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
const addr = server.address();
if (!addr || typeof addr === 'string') throw new Error('stub server has no address');
return {
baseUrl: `http://127.0.0.1:${(addr as { port: number }).port}`,
requests,
setNextGetResponse: (body) => { nextGetBody = body; },
close: () =>
new Promise<void>((resolve, reject) => {
server.close((err) => (err ? reject(err) : resolve()));
}),
};
}
function runCli(
args: string[],
daemonUrl: string,
): Promise<{ stdout: string; stderr: string; code: number | null }> {
return new Promise((resolve) => {
const env: NodeJS.ProcessEnv = { ...process.env, OD_DAEMON_URL: daemonUrl };
delete env.NODE_OPTIONS;
const child = spawn(process.execPath, [TSX_CLI, CLI_SRC, ...args], {
cwd: pathResolve(__dirnameLocal, '..'),
env,
stdio: ['pipe', 'pipe', 'pipe'],
timeout: 20_000,
});
let stdout = '';
let stderr = '';
child.stdout.on('data', (c: Buffer) => { stdout += c.toString(); });
child.stderr.on('data', (c: Buffer) => { stderr += c.toString(); });
child.on('close', (code) => resolve({ stdout, stderr, code }));
child.stdin.end();
});
}
describe('app-config updateInstallMode pref', () => {
let dataDir: string;
beforeEach(async () => {
dataDir = await mkdtemp(path.join(tmpdir(), 'od-update-install-mode-'));
});
afterEach(async () => {
await rm(dataDir, { recursive: true, force: true });
});
it("persists 'manual' and reads it back", async () => {
await writeAppConfig(dataDir, { updateInstallMode: 'manual' });
const cfg = await readAppConfig(dataDir);
expect(cfg.updateInstallMode).toBe('manual');
});
it("persists 'automatic' and reads it back", async () => {
await writeAppConfig(dataDir, { updateInstallMode: 'automatic' });
const cfg = await readAppConfig(dataDir);
expect(cfg.updateInstallMode).toBe('automatic');
});
it('rejects invalid enum values and drops them', async () => {
// 'bogus' is not in the allowed enum; writeAppConfig must drop it.
await writeAppConfig(dataDir, { updateInstallMode: 'bogus' as any });
const cfg = await readAppConfig(dataDir);
expect(cfg.updateInstallMode).toBeUndefined();
});
it('treats absent field as automatic (no stored value, no crash)', async () => {
// Fresh config with no updateInstallMode — should be absent (undefined),
// which callers treat as 'automatic'.
const cfg = await readAppConfig(dataDir);
expect(cfg.updateInstallMode).toBeUndefined();
});
it("updateInstallMode is included in ALLOWED_KEYS (round-trip without unknown-key filter dropping it)", async () => {
// ALLOWED_KEYS gate: only keys in the set survive writeAppConfig.
// If updateInstallMode is missing from ALLOWED_KEYS it is silently
// dropped — this test catches that regression.
await writeAppConfig(dataDir, { updateInstallMode: 'manual', agentId: 'claude' });
const cfg = await readAppConfig(dataDir);
expect(cfg.updateInstallMode).toBe('manual');
expect(cfg.agentId).toBe('claude');
});
it("clears updateInstallMode when null is sent", async () => {
await writeAppConfig(dataDir, { updateInstallMode: 'manual' });
await writeAppConfig(dataDir, { updateInstallMode: null as any });
const cfg = await readAppConfig(dataDir);
expect(cfg.updateInstallMode).toBeUndefined();
});
});
// ---------------------------------------------------------------------------
// Daemon-level null-clear contract (non-regression guard for the fix)
//
// writeAppConfig(dir, { updateInstallMode: null }) must clear a previously-
// stored 'manual' value. applyConfigValue already handles null → delete, so
// this should be GREEN on both the current code and after the fix. It exists
// to document and protect the daemon-side contract the CLI fix relies on.
// ---------------------------------------------------------------------------
describe('app-config updateInstallMode null-clear (daemon-level guard)', () => {
let dataDir: string;
beforeEach(async () => {
dataDir = await mkdtemp(path.join(tmpdir(), 'od-uim-null-guard-'));
});
afterEach(async () => {
await rm(dataDir, { recursive: true, force: true });
});
it('clears a previously stored manual value when null is written via writeAppConfig', async () => {
// Set 'manual' first.
await writeAppConfig(dataDir, { updateInstallMode: 'manual' });
const before = await readAppConfig(dataDir);
expect(before.updateInstallMode).toBe('manual');
// Send null — applyConfigValue must delete the key.
await writeAppConfig(dataDir, { updateInstallMode: null as any });
const after = await readAppConfig(dataDir);
expect(after.updateInstallMode).toBeUndefined();
});
});
// ---------------------------------------------------------------------------
// CLI `od config unset updateInstallMode` regression test (RED)
//
// The bug: the current unset path does `delete next[key]` then sends the
// object WITHOUT the key in the PUT body. writeAppConfig's doWrite only
// iterates keys that are present in the partial — an absent key is a no-op,
// so 'manual' survives the unset.
//
// The fix contract: unset must send { updateInstallMode: null } so doWrite
// calls applyConfigValue(…, null) which deletes the stored value.
//
// We intercept at the HTTP layer (stub server) because:
// 1. No live daemon required — fast, no port conflicts.
// 2. The bug lives entirely in what body the CLI sends; the stub captures it.
// 3. This is the same approach used by cli-files-write.test.ts.
// ---------------------------------------------------------------------------
describe('od config unset updateInstallMode — CLI sends null in PUT body (regression)', () => {
let stub: StubServer;
beforeAll(async () => {
stub = await startConfigStubServer();
});
afterAll(async () => {
await stub.close();
});
beforeEach(() => {
stub.requests.length = 0;
});
it('PUT body includes { updateInstallMode: null } so the daemon can clear it', async () => {
// Seed the stub so the CLI's GET /api/app-config returns { updateInstallMode: 'manual' }.
stub.setNextGetResponse({ config: { updateInstallMode: 'manual' } });
const result = await runCli(
['config', 'unset', 'updateInstallMode', '--daemon-url', stub.baseUrl],
stub.baseUrl,
);
// The CLI must exit cleanly.
expect(result.code).toBe(0);
// Find the PUT request.
const putRequest = stub.requests.find((r) => r.method === 'PUT');
expect(putRequest).toBeDefined();
const putBody = JSON.parse(putRequest!.body) as Record<string, unknown>;
// The key MUST be present in the PUT body with value null.
// With the current buggy code this assertion fails because the key is
// absent entirely (delete next[key] removes it before JSON.stringify).
expect(Object.prototype.hasOwnProperty.call(putBody, 'updateInstallMode')).toBe(true);
expect(putBody['updateInstallMode']).toBeNull();
});
it('PUT body does not contain a non-null updateInstallMode after unset', async () => {
// Belt-and-braces: even if the key is present it must not be 'manual'.
stub.setNextGetResponse({ config: { updateInstallMode: 'manual' } });
const result = await runCli(
['config', 'unset', 'updateInstallMode', '--daemon-url', stub.baseUrl],
stub.baseUrl,
);
expect(result.code).toBe(0);
const putRequest = stub.requests.find((r) => r.method === 'PUT');
expect(putRequest).toBeDefined();
const putBody = JSON.parse(putRequest!.body) as Record<string, unknown>;
// The value must not be 'manual' — either null (correct) or absent (buggy).
// Combined with the previous test, both must pass for a complete fix.
expect(putBody['updateInstallMode']).not.toBe('manual');
});
});