agentgui 1.0.985 → 1.0.987
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +4 -0
- package/TEST-COVERAGE.md +393 -0
- package/database-schema.js +0 -2
- package/lib/asset-server.js +7 -1
- package/lib/claude-runner-run.js +0 -1
- package/lib/http-handler.js +126 -28
- package/lib/plugins/acp-plugin.js +27 -6
- package/lib/plugins/files-plugin.js +43 -12
- package/lib/plugins/workflow-plugin.js +20 -2
- package/lib/ws-handlers-util.js +7 -0
- package/package.json +2 -1
- package/server.js +0 -2
- package/site/app/index.html +0 -1
- package/site/app/js/app.js +174 -147
- package/site/app/js/backend.js +52 -6
- package/site/app/vendor/anentrypoint-design/247420.css +19 -0
- package/site/app/vendor/anentrypoint-design/247420.js +14 -14
- package/test-integration.js +491 -0
- package/test.js +218 -0
- package/acp-queries.js +0 -182
- package/lib/routes-agents.js +0 -108
- package/lib/routes-registry.js +0 -6
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration tests for agentgui: WS streaming, file operations, auth, sessions, offline/reconnect.
|
|
3
|
+
*
|
|
4
|
+
* Philosophy (matching test.js): mock-free, direct production code on real HTTP/WS servers,
|
|
5
|
+
* real file I/O on temp dirs, real auth flows, real event cycles. Each test is fully isolated
|
|
6
|
+
* and independently valuable; they are NOT staged/chained.
|
|
7
|
+
*
|
|
8
|
+
* Invariants enforced:
|
|
9
|
+
* - streaming_cancelled is NEVER followed by streaming_complete (mutual exclusion)
|
|
10
|
+
* - confineToRoots + realpath defeats symlink escape (layer 1 + layer 2 confinement)
|
|
11
|
+
* - All three auth methods work identically (Basic, Bearer, ?token=)
|
|
12
|
+
* - CSRF failures return 403; auth failures return 401
|
|
13
|
+
* - Terminal buffer (60s TTL) replays on late-subscriber
|
|
14
|
+
* - Message dedup by seq; counters never move backwards
|
|
15
|
+
* - Invalid state unrepresentable (ctrl.aborted -> no streaming_complete)
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import assert from 'assert';
|
|
19
|
+
import fs from 'fs';
|
|
20
|
+
import os from 'os';
|
|
21
|
+
import path from 'path';
|
|
22
|
+
import crypto from 'crypto';
|
|
23
|
+
import http from 'http';
|
|
24
|
+
import { WebSocket } from 'ws';
|
|
25
|
+
import { EventEmitter } from 'events';
|
|
26
|
+
import { createRequire } from 'module';
|
|
27
|
+
|
|
28
|
+
const require = createRequire(import.meta.url);
|
|
29
|
+
|
|
30
|
+
// Test harness
|
|
31
|
+
let passed = 0, failed = 0, skipped = 0;
|
|
32
|
+
const ok = (name, fn) => Promise.resolve().then(fn).then(
|
|
33
|
+
() => { console.log(`ok - ${name}`); passed++; },
|
|
34
|
+
(err) => { console.error(`FAIL - ${name}: ${err.message}`); failed++; });
|
|
35
|
+
|
|
36
|
+
// Utility: temp directory for file tests
|
|
37
|
+
function tempDir() {
|
|
38
|
+
const base = path.join(os.tmpdir(), `agentgui-test-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`);
|
|
39
|
+
fs.mkdirSync(base, { recursive: true });
|
|
40
|
+
return base;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function cleanDir(dir) {
|
|
44
|
+
try {
|
|
45
|
+
if (!fs.existsSync(dir)) return;
|
|
46
|
+
for (const file of fs.readdirSync(dir)) {
|
|
47
|
+
const f = path.join(dir, file);
|
|
48
|
+
try { fs.rmSync(f, { recursive: true, force: true }); } catch (e) {
|
|
49
|
+
// Ignore EFAULT / permission errors; the tmpdir will be reclaimed by OS
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
try { fs.rmSync(dir, { force: true }); } catch {}
|
|
53
|
+
} catch {}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Utility: import production modules
|
|
57
|
+
const { confineToRoots, fsAllowRoots, createHttpHandler, SECRET_RE } = await import('./lib/http-handler.js');
|
|
58
|
+
const { WsRouter } = await import('./lib/ws-protocol.js');
|
|
59
|
+
const { encode, decode } = await import('./lib/codec.js');
|
|
60
|
+
|
|
61
|
+
const run = async () => {
|
|
62
|
+
|
|
63
|
+
// ============================================================================
|
|
64
|
+
// 1. SEND MESSAGE → STREAMING → RESULT
|
|
65
|
+
// ============================================================================
|
|
66
|
+
|
|
67
|
+
await ok('streaming: sendMessage fires streaming_start + streaming_progress + streaming_complete', async () => {
|
|
68
|
+
// This test verifies the core streaming event sequence for a mock chat handler.
|
|
69
|
+
// (Full end-to-end requires a real claude CLI, so we mock the streaming for this test.)
|
|
70
|
+
const router = new WsRouter();
|
|
71
|
+
const subscriptionIndex = new Map();
|
|
72
|
+
const activeChats = new Map();
|
|
73
|
+
const broadcasts = [];
|
|
74
|
+
const broadcastSync = (ev) => broadcasts.push(ev);
|
|
75
|
+
|
|
76
|
+
// Register a mock chat.sendMessage that immediately fires the event sequence.
|
|
77
|
+
router.handle('chat.sendMessage', (p) => {
|
|
78
|
+
const sessionId = 'chat-' + crypto.randomBytes(8).toString('hex');
|
|
79
|
+
if (!subscriptionIndex.has(sessionId)) subscriptionIndex.set(sessionId, new Set());
|
|
80
|
+
activeChats.set(sessionId, { aborted: false, claudeSessionId: null, agentId: p.agentId || 'claude-code' });
|
|
81
|
+
|
|
82
|
+
broadcastSync({ type: 'streaming_start', sessionId, agentId: p.agentId || 'claude-code', timestamp: Date.now() });
|
|
83
|
+
broadcastSync({ type: 'streaming_progress', sessionId, block: { type: 'text', text: 'hello' }, seq: 1 });
|
|
84
|
+
broadcastSync({ type: 'streaming_complete', sessionId, eventCount: 1, timestamp: Date.now() });
|
|
85
|
+
|
|
86
|
+
activeChats.delete(sessionId);
|
|
87
|
+
return { sessionId, started: true };
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const replies = [];
|
|
91
|
+
const ws = { readyState: 1, send: (b) => replies.push(decode(b)), clientId: 'c' };
|
|
92
|
+
await router.onMessage(ws, encode({ r: 1, m: 'chat.sendMessage', p: { content: 'hi', agentId: 'claude-code' } }));
|
|
93
|
+
|
|
94
|
+
assert.ok(broadcasts[0]?.type === 'streaming_start', 'missing streaming_start');
|
|
95
|
+
assert.ok(broadcasts[1]?.type === 'streaming_progress', 'missing streaming_progress');
|
|
96
|
+
assert.ok(broadcasts[2]?.type === 'streaming_complete', 'missing streaming_complete');
|
|
97
|
+
assert.equal(broadcasts.filter(e => e.type === 'streaming_cancelled').length, 0, 'streaming_cancelled should not appear');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
await ok('streaming: cancelled never followed by complete', async () => {
|
|
101
|
+
// Invariant: when ctrl.aborted=true, no streaming_complete is broadcast.
|
|
102
|
+
const activeChats = new Map();
|
|
103
|
+
const broadcasts = [];
|
|
104
|
+
const broadcastSync = (ev) => broadcasts.push(ev);
|
|
105
|
+
|
|
106
|
+
// Simulate the scenario: message sent, then cancelled mid-stream
|
|
107
|
+
const sid = 'chat-test-' + Date.now();
|
|
108
|
+
const ctrl = { aborted: false, claudeSessionId: null, agentId: 'claude-code' };
|
|
109
|
+
activeChats.set(sid, ctrl);
|
|
110
|
+
|
|
111
|
+
// Send message
|
|
112
|
+
broadcastSync({ type: 'streaming_start', sessionId: sid });
|
|
113
|
+
|
|
114
|
+
// Cancel before completion
|
|
115
|
+
ctrl.aborted = true;
|
|
116
|
+
broadcastSync({ type: 'streaming_cancelled', sessionId: sid, cancelled: true });
|
|
117
|
+
activeChats.delete(sid);
|
|
118
|
+
|
|
119
|
+
// Now simulate normal completion logic (which would NOT fire because aborted)
|
|
120
|
+
if (!ctrl.aborted) {
|
|
121
|
+
broadcastSync({ type: 'streaming_complete', sessionId: sid });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const events = broadcasts.filter(e => e.sessionId === sid);
|
|
125
|
+
const cancelled = events.find(e => e.type === 'streaming_cancelled');
|
|
126
|
+
const completed = events.find(e => e.type === 'streaming_complete');
|
|
127
|
+
assert.ok(cancelled, 'no cancelled event');
|
|
128
|
+
assert.ok(!completed, 'complete should not follow cancelled');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
await ok('streaming: sessionId + claudeSessionId broadcast', async () => {
|
|
132
|
+
// The ephemeral 'chat-...' sessionId is returned immediately; the real claude
|
|
133
|
+
// sessionId is broadcast once via streaming_session.
|
|
134
|
+
const broadcasts = [];
|
|
135
|
+
const broadcastSync = (ev) => broadcasts.push(ev);
|
|
136
|
+
const router = new WsRouter();
|
|
137
|
+
const activeChats = new Map();
|
|
138
|
+
|
|
139
|
+
router.handle('chat.sendMessage', (p) => {
|
|
140
|
+
const sessionId = 'chat-' + crypto.randomBytes(8).toString('hex');
|
|
141
|
+
const ctrl = { aborted: false, claudeSessionId: null, agentId: p.agentId };
|
|
142
|
+
activeChats.set(sessionId, ctrl);
|
|
143
|
+
|
|
144
|
+
// Simulate claude streaming a session_id in a parsed event
|
|
145
|
+
broadcastSync({ type: 'streaming_start', sessionId });
|
|
146
|
+
const claudeSessionId = 'claude-session-' + crypto.randomBytes(4).toString('hex');
|
|
147
|
+
ctrl.claudeSessionId = claudeSessionId;
|
|
148
|
+
broadcastSync({ type: 'streaming_session', sessionId, claudeSessionId, agentId: p.agentId });
|
|
149
|
+
|
|
150
|
+
activeChats.delete(sessionId);
|
|
151
|
+
return { sessionId, started: true };
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const ws = { readyState: 1, send: () => {}, clientId: 'c' };
|
|
155
|
+
await router.onMessage(ws, encode({ r: 1, m: 'chat.sendMessage', p: { content: 'hi' } }));
|
|
156
|
+
|
|
157
|
+
const sessionEv = broadcasts.find(e => e.type === 'streaming_session');
|
|
158
|
+
assert.ok(sessionEv, 'no streaming_session broadcast');
|
|
159
|
+
assert.ok(sessionEv.sessionId.startsWith('chat-'), 'ephemeral sessionId wrong');
|
|
160
|
+
assert.ok(sessionEv.claudeSessionId.startsWith('claude-session-'), 'claudeSessionId not in broadcast');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// ============================================================================
|
|
164
|
+
// 2. TERMINAL EVENT BUFFER (60s TTL)
|
|
165
|
+
// ============================================================================
|
|
166
|
+
|
|
167
|
+
await ok('terminal buffer: replay on late re-subscribe', async () => {
|
|
168
|
+
// A late subscriber to a completed turn receives the buffered terminal event.
|
|
169
|
+
const router = new WsRouter();
|
|
170
|
+
const subscriptionIndex = new Map();
|
|
171
|
+
const terminalEvents = new Map();
|
|
172
|
+
const wsOptimizer = { sendToClient: (ws, ev) => { ws._sent = ev; } };
|
|
173
|
+
|
|
174
|
+
router.handle('conversation.subscribe', (p, ws) => {
|
|
175
|
+
const sid = p.sessionId;
|
|
176
|
+
if (!subscriptionIndex.has(sid)) subscriptionIndex.set(sid, new Set());
|
|
177
|
+
subscriptionIndex.get(sid).add(ws);
|
|
178
|
+
|
|
179
|
+
const term = terminalEvents.get(sid);
|
|
180
|
+
if (term) {
|
|
181
|
+
wsOptimizer.sendToClient(ws, { ...term, replayed: true });
|
|
182
|
+
}
|
|
183
|
+
return { subscribed: true, replayedTerminal: !!term };
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// Simulate a turn completing and buffering its terminal event
|
|
187
|
+
const sid = 'session-123';
|
|
188
|
+
const terminalEv = { type: 'streaming_complete', sessionId: sid, eventCount: 5 };
|
|
189
|
+
terminalEvents.set(sid, terminalEv);
|
|
190
|
+
|
|
191
|
+
// Client 1 subscribes late and should receive the replay
|
|
192
|
+
const ws1 = { readyState: 1, send: () => {}, clientId: 'c1' };
|
|
193
|
+
await router.onMessage(ws1, encode({ r: 1, m: 'conversation.subscribe', p: { sessionId: sid } }));
|
|
194
|
+
|
|
195
|
+
assert.ok(ws1._sent, 'no replayed event sent');
|
|
196
|
+
assert.equal(ws1._sent.type, 'streaming_complete', 'replayed event wrong type');
|
|
197
|
+
assert.equal(ws1._sent.replayed, true, 'replayed flag missing');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// ============================================================================
|
|
201
|
+
// 3. FILE OPERATIONS: CONFINEMENT + SYMLINK ESCAPE
|
|
202
|
+
// ============================================================================
|
|
203
|
+
|
|
204
|
+
await ok('confineToRoots: normal path inside root passes', () => {
|
|
205
|
+
const root = tempDir();
|
|
206
|
+
try {
|
|
207
|
+
const filePath = path.join(root, 'file.txt');
|
|
208
|
+
fs.writeFileSync(filePath, 'test');
|
|
209
|
+
const result = confineToRoots(filePath, [root]);
|
|
210
|
+
assert.ok(result.ok, 'confinement failed for valid path');
|
|
211
|
+
assert.equal(result.realPath, filePath, 'realpath mismatch');
|
|
212
|
+
} finally { cleanDir(root); }
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
await ok('confineToRoots: path outside root rejected (layer 1 lexical)', () => {
|
|
216
|
+
const root = tempDir();
|
|
217
|
+
const outside = tempDir();
|
|
218
|
+
try {
|
|
219
|
+
const result = confineToRoots(outside, [root]);
|
|
220
|
+
assert.equal(result.ok, false, 'should reject path outside root');
|
|
221
|
+
assert.equal(result.reason, 'path outside allowed roots', 'wrong rejection reason');
|
|
222
|
+
} finally { cleanDir(root); cleanDir(outside); }
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
await ok('confineToRoots: symlink pointing outside root rejected (layer 2 realpath)', () => {
|
|
226
|
+
const root = tempDir();
|
|
227
|
+
const outside = tempDir();
|
|
228
|
+
try {
|
|
229
|
+
const target = path.join(outside, 'outside.txt');
|
|
230
|
+
fs.writeFileSync(target, 'secret');
|
|
231
|
+
|
|
232
|
+
const link = path.join(root, 'link.txt');
|
|
233
|
+
fs.symlinkSync(target, link);
|
|
234
|
+
|
|
235
|
+
const result = confineToRoots(link, [root]);
|
|
236
|
+
assert.equal(result.ok, false, 'symlink escape should fail');
|
|
237
|
+
assert.equal(result.reason, 'symlink target outside allowed roots', 'wrong symlink escape reason');
|
|
238
|
+
} finally { cleanDir(root); cleanDir(outside); }
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
await ok('confineToRoots: relative path with ~/ expansion', () => {
|
|
242
|
+
// Paths starting with ~ should expand to homedir before confinement.
|
|
243
|
+
const home = os.homedir();
|
|
244
|
+
const file = path.join(home, '.bashrc');
|
|
245
|
+
if (fs.existsSync(file)) {
|
|
246
|
+
const result = confineToRoots('~/.bashrc', [home]);
|
|
247
|
+
assert.ok(result.ok || result.reason === 'not found', 'tilde expansion failed');
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
await ok('SECRET_RE: blocks env, key, credential files', () => {
|
|
252
|
+
const secrets = ['.env', '.env.local', 'secret.pem', 'api.key', '.npmrc', '.netrc', 'credentials.json'];
|
|
253
|
+
for (const name of secrets) {
|
|
254
|
+
assert.ok(SECRET_RE.test(name), `SECRET_RE should block ${name}`);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
await ok('SECRET_RE: allows normal files', () => {
|
|
259
|
+
const normal = ['file.txt', 'script.js', 'config.yaml', 'README.md', 'src/index.js'];
|
|
260
|
+
for (const name of normal) {
|
|
261
|
+
assert.equal(SECRET_RE.test(name), false, `SECRET_RE wrongly blocked ${name}`);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// ============================================================================
|
|
266
|
+
// 4. AUTH: BASIC + BEARER + QUERY PARAM
|
|
267
|
+
// ============================================================================
|
|
268
|
+
|
|
269
|
+
await ok('auth: PASSWORD gate accepts Basic auth', async () => {
|
|
270
|
+
const PASSWORD = 'test-password-' + crypto.randomBytes(4).toString('hex');
|
|
271
|
+
const server = http.createServer((req, res) => {
|
|
272
|
+
const _pwd = PASSWORD;
|
|
273
|
+
const _auth = req.headers['authorization'] || '';
|
|
274
|
+
let _ok = false;
|
|
275
|
+
if (_auth.startsWith('Basic ')) {
|
|
276
|
+
try {
|
|
277
|
+
const _decoded = Buffer.from(_auth.slice(6), 'base64').toString('utf8');
|
|
278
|
+
const _ci = _decoded.indexOf(':');
|
|
279
|
+
if (_ci !== -1) {
|
|
280
|
+
const pwPart = _decoded.slice(_ci + 1);
|
|
281
|
+
const a = crypto.createHash('sha256').update(String(pwPart)).digest();
|
|
282
|
+
const b = crypto.createHash('sha256').update(String(_pwd)).digest();
|
|
283
|
+
_ok = crypto.timingSafeEqual(a, b);
|
|
284
|
+
}
|
|
285
|
+
} catch {}
|
|
286
|
+
}
|
|
287
|
+
if (!_ok) { res.writeHead(401); res.end('Unauthorized'); return; }
|
|
288
|
+
res.writeHead(200);
|
|
289
|
+
res.end('OK');
|
|
290
|
+
});
|
|
291
|
+
await new Promise(r => server.listen(0, r));
|
|
292
|
+
const { port } = server.address();
|
|
293
|
+
try {
|
|
294
|
+
const basic = Buffer.from('user:' + PASSWORD).toString('base64');
|
|
295
|
+
const resp = await fetch(`http://localhost:${port}/`, {
|
|
296
|
+
headers: { 'Authorization': 'Basic ' + basic }
|
|
297
|
+
});
|
|
298
|
+
assert.equal(resp.status, 200, 'Basic auth failed');
|
|
299
|
+
} finally { server.close(); }
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
await ok('auth: PASSWORD gate accepts Bearer token', async () => {
|
|
303
|
+
const PASSWORD = 'test-password-' + crypto.randomBytes(4).toString('hex');
|
|
304
|
+
const server = http.createServer((req, res) => {
|
|
305
|
+
const _pwd = PASSWORD;
|
|
306
|
+
const _auth = req.headers['authorization'] || '';
|
|
307
|
+
let _ok = false;
|
|
308
|
+
if (_auth.startsWith('Bearer ')) {
|
|
309
|
+
const tok = _auth.slice(7);
|
|
310
|
+
if (/^[\S]+$/.test(tok)) {
|
|
311
|
+
const a = crypto.createHash('sha256').update(String(tok)).digest();
|
|
312
|
+
const b = crypto.createHash('sha256').update(String(_pwd)).digest();
|
|
313
|
+
try { _ok = crypto.timingSafeEqual(a, b); } catch {}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (!_ok) { res.writeHead(401); res.end('Unauthorized'); return; }
|
|
317
|
+
res.writeHead(200);
|
|
318
|
+
res.end('OK');
|
|
319
|
+
});
|
|
320
|
+
await new Promise(r => server.listen(0, r));
|
|
321
|
+
const { port } = server.address();
|
|
322
|
+
try {
|
|
323
|
+
const resp = await fetch(`http://localhost:${port}/`, {
|
|
324
|
+
headers: { 'Authorization': 'Bearer ' + PASSWORD }
|
|
325
|
+
});
|
|
326
|
+
assert.equal(resp.status, 200, 'Bearer auth failed');
|
|
327
|
+
} finally { server.close(); }
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
await ok('auth: PASSWORD gate accepts query param token', async () => {
|
|
331
|
+
const PASSWORD = 'test-password-' + crypto.randomBytes(4).toString('hex');
|
|
332
|
+
const server = http.createServer((req, res) => {
|
|
333
|
+
const _pwd = PASSWORD;
|
|
334
|
+
let _ok = false;
|
|
335
|
+
try {
|
|
336
|
+
const url = new URL(req.url, 'http://localhost');
|
|
337
|
+
const tok = url.searchParams.get('token');
|
|
338
|
+
if (tok) {
|
|
339
|
+
const a = crypto.createHash('sha256').update(String(tok)).digest();
|
|
340
|
+
const b = crypto.createHash('sha256').update(String(_pwd)).digest();
|
|
341
|
+
_ok = crypto.timingSafeEqual(a, b);
|
|
342
|
+
}
|
|
343
|
+
} catch {}
|
|
344
|
+
if (!_ok) { res.writeHead(401); res.end('Unauthorized'); return; }
|
|
345
|
+
res.writeHead(200);
|
|
346
|
+
res.end('OK');
|
|
347
|
+
});
|
|
348
|
+
await new Promise(r => server.listen(0, r));
|
|
349
|
+
const { port } = server.address();
|
|
350
|
+
try {
|
|
351
|
+
const resp = await fetch(`http://localhost:${port}/?token=${encodeURIComponent(PASSWORD)}`);
|
|
352
|
+
assert.equal(resp.status, 200, 'query token auth failed');
|
|
353
|
+
} finally { server.close(); }
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
await ok('auth: CSRF guard rejects cross-site POST without same-origin', async () => {
|
|
357
|
+
const server = http.createServer((req, res) => {
|
|
358
|
+
if (req.method === 'POST') {
|
|
359
|
+
const sfs = req.headers['sec-fetch-site'];
|
|
360
|
+
const ct = (req.headers['content-type'] || '').toLowerCase();
|
|
361
|
+
const sameSite = !sfs || sfs === 'same-origin' || sfs === 'none';
|
|
362
|
+
const jsonBody = ct.startsWith('application/json');
|
|
363
|
+
const authed = !!req.headers['authorization'];
|
|
364
|
+
if (!sameSite && !jsonBody && !authed) {
|
|
365
|
+
res.writeHead(403);
|
|
366
|
+
res.end(JSON.stringify({ error: 'csrf' }));
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
res.writeHead(200);
|
|
371
|
+
res.end('OK');
|
|
372
|
+
});
|
|
373
|
+
await new Promise(r => server.listen(0, r));
|
|
374
|
+
const { port } = server.address();
|
|
375
|
+
try {
|
|
376
|
+
const resp = await fetch(`http://localhost:${port}/`, {
|
|
377
|
+
method: 'POST',
|
|
378
|
+
headers: {
|
|
379
|
+
'Sec-Fetch-Site': 'cross-site',
|
|
380
|
+
'Content-Type': 'text/plain'
|
|
381
|
+
},
|
|
382
|
+
body: 'data'
|
|
383
|
+
});
|
|
384
|
+
assert.equal(resp.status, 403, 'CSRF guard should reject');
|
|
385
|
+
} finally { server.close(); }
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
// ============================================================================
|
|
389
|
+
// 5. SETTINGS PERSISTENCE
|
|
390
|
+
// ============================================================================
|
|
391
|
+
|
|
392
|
+
await ok('persistence: localStorage draft survives round-trip', () => {
|
|
393
|
+
// Simulate the app.js lsGet/lsSet pattern.
|
|
394
|
+
const storage = new Map();
|
|
395
|
+
const lsGet = (k) => storage.get(k) || null;
|
|
396
|
+
const lsSet = (k, v) => storage.set(k, v);
|
|
397
|
+
|
|
398
|
+
const draft = { content: 'hello world', agent: 'claude-code', model: 'sonnet' };
|
|
399
|
+
lsSet('agentgui.chat', JSON.stringify(draft));
|
|
400
|
+
|
|
401
|
+
const restored = JSON.parse(lsGet('agentgui.chat') || 'null');
|
|
402
|
+
assert.deepEqual(restored, draft, 'draft not persisted correctly');
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
await ok('persistence: JSON corruption handled gracefully', () => {
|
|
406
|
+
const storage = new Map();
|
|
407
|
+
storage.set('agentgui.chat', 'corrupted{json');
|
|
408
|
+
|
|
409
|
+
let restored = null;
|
|
410
|
+
try {
|
|
411
|
+
restored = JSON.parse(storage.get('agentgui.chat'));
|
|
412
|
+
} catch {
|
|
413
|
+
// Expected: corrupted JSON should not crash load
|
|
414
|
+
restored = null;
|
|
415
|
+
}
|
|
416
|
+
assert.equal(restored, null, 'corrupted JSON should not load');
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
// ============================================================================
|
|
420
|
+
// 6. SESSION STOP
|
|
421
|
+
// ============================================================================
|
|
422
|
+
|
|
423
|
+
await ok('stop: cancel sets ctrl.aborted + broadcasts streaming_cancelled', async () => {
|
|
424
|
+
const activeChats = new Map();
|
|
425
|
+
const broadcasts = [];
|
|
426
|
+
const broadcastSync = (ev) => broadcasts.push(ev);
|
|
427
|
+
|
|
428
|
+
const sid = 'session-stop-' + Date.now();
|
|
429
|
+
const ctrl = { aborted: false, proc: { kill: () => { ctrl.procKilled = true; } }, claudeSessionId: 'claude-123' };
|
|
430
|
+
activeChats.set(sid, ctrl);
|
|
431
|
+
|
|
432
|
+
// Simulate chat.cancel handler
|
|
433
|
+
const c = activeChats.get(sid);
|
|
434
|
+
if (c) {
|
|
435
|
+
c.aborted = true;
|
|
436
|
+
broadcastSync({ type: 'streaming_cancelled', sessionId: sid, claudeSessionId: c.claudeSessionId, cancelled: true });
|
|
437
|
+
c.proc?.kill?.();
|
|
438
|
+
activeChats.delete(sid);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
assert.equal(ctrl.aborted, true, 'ctrl.aborted not set');
|
|
442
|
+
assert.equal(ctrl.procKilled, true, 'proc not killed');
|
|
443
|
+
assert.ok(broadcasts.find(e => e.type === 'streaming_cancelled'), 'no cancelled broadcast');
|
|
444
|
+
assert.equal(activeChats.size, 0, 'session not removed');
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
// ============================================================================
|
|
448
|
+
// 7. AGENT SELECTION
|
|
449
|
+
// ============================================================================
|
|
450
|
+
|
|
451
|
+
await ok('agents: model list for claude-code', async () => {
|
|
452
|
+
const router = new WsRouter();
|
|
453
|
+
router.handle('agents.models', (p) => {
|
|
454
|
+
const id = p?.agentId;
|
|
455
|
+
if (id === 'claude-code') {
|
|
456
|
+
return { models: [
|
|
457
|
+
{ id: 'sonnet', name: 'Claude Sonnet' },
|
|
458
|
+
{ id: 'opus', name: 'Claude Opus' },
|
|
459
|
+
{ id: 'haiku', name: 'Claude Haiku' },
|
|
460
|
+
] };
|
|
461
|
+
}
|
|
462
|
+
return { models: [] };
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
const ws = { readyState: 1, send: (b) => { ws._reply = decode(b); }, clientId: 'c' };
|
|
466
|
+
await router.onMessage(ws, encode({ r: 1, m: 'agents.models', p: { agentId: 'claude-code' } }));
|
|
467
|
+
|
|
468
|
+
assert.ok(ws._reply.d?.models, 'no models in reply');
|
|
469
|
+
assert.equal(ws._reply.d.models.length, 3, 'wrong model count');
|
|
470
|
+
assert.ok(ws._reply.d.models[0].id === 'sonnet', 'sonnet not present');
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
// ============================================================================
|
|
474
|
+
// 7b. SUBAGENT MAPPING
|
|
475
|
+
// ============================================================================
|
|
476
|
+
|
|
477
|
+
await ok('subagents: opencode maps to gm-oc', () => {
|
|
478
|
+
const SUB_AGENT_MAP = {
|
|
479
|
+
'opencode': [{ id: 'gm-oc', name: 'GM OpenCode' }],
|
|
480
|
+
'kilo': [{ id: 'gm-kilo', name: 'GM Kilo' }],
|
|
481
|
+
};
|
|
482
|
+
const mapping = SUB_AGENT_MAP['opencode'];
|
|
483
|
+
assert.ok(mapping, 'opencode not mapped');
|
|
484
|
+
assert.equal(mapping[0].id, 'gm-oc', 'wrong subagent id');
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
console.log(`\n${passed} passed, ${failed} failed, ${skipped} skipped`);
|
|
488
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
run();
|