-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.js
More file actions
198 lines (176 loc) · 7.8 KB
/
test.js
File metadata and controls
198 lines (176 loc) · 7.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
import assert from 'assert';
import Database from 'better-sqlite3';
import { initSchema } from './database-schema.js';
import { migrateConversationColumns } from './database-migrations.js';
import { migrateACPSchema } from './database-migrations-acp.js';
import { createQueries } from './lib/db-queries.js';
import { encode, decode } from './lib/codec.js';
import { WsRouter } from './lib/ws-protocol.js';
import * as toolInstallMachine from './lib/tool-install-machine.js';
import * as execMachine from './lib/execution-machine.js';
import * as acpServerMachine from './lib/acp-server-machine.js';
let passed = 0, failed = 0;
const section = (name, fn) => {
try { fn(); console.log(`ok — ${name}`); passed++; }
catch (err) { console.error(`FAIL — ${name}: ${err.message}`); failed++; }
};
function inMemDb() {
const db = new Database(':memory:');
db.pragma('foreign_keys = ON');
const origLog = console.log, origWarn = console.warn;
console.log = () => {}; console.warn = () => {};
try {
initSchema(db);
migrateConversationColumns(db);
migrateACPSchema(db);
} finally { console.log = origLog; console.warn = origWarn; }
const prep = (sql) => db.prepare(sql);
const gid = (p) => `${p}-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
return { db, prep, gid };
}
section('codec: roundtrip primitives', () => {
const obj = { a: 1, b: 'str', c: [1, 2, 3], d: { nested: true } };
assert.deepEqual(decode(encode(obj)), obj);
});
section('codec: binary payload', () => {
const buf = Buffer.from([1, 2, 3, 4]);
const round = decode(encode({ bin: buf }));
assert.deepEqual(Array.from(round.bin), [1, 2, 3, 4]);
});
section('db: init schema creates conversations table', () => {
const { db } = inMemDb();
const t = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='conversations'").get();
assert.ok(t, 'conversations table exists');
});
section('db-queries: createConversation round-trip', () => {
const { db, prep, gid } = inMemDb();
const q = createQueries(db, prep, gid);
const c = q.createConversation('claude-code', 'Test', '/tmp', 'sonnet', null);
assert.equal(c.title, 'Test');
const fetched = q.getConversation(c.id);
assert.equal(fetched.title, 'Test');
assert.equal(fetched.status, 'active');
});
section('db-queries: archive + restore', () => {
const { db, prep, gid } = inMemDb();
const q = createQueries(db, prep, gid);
const c = q.createConversation('claude-code', 'A');
q.archiveConversation(c.id);
assert.equal(q.getConversation(c.id).status, 'archived');
q.restoreConversation(c.id);
assert.equal(q.getConversation(c.id).status, 'active');
});
section('db-queries: streaming flag', () => {
const { db, prep, gid } = inMemDb();
const q = createQueries(db, prep, gid);
const c = q.createConversation('claude-code', 'S');
q.setIsStreaming(c.id, true);
assert.equal(q.getIsStreaming(c.id), true);
q.setIsStreaming(c.id, false);
assert.equal(q.getIsStreaming(c.id), false);
});
section('acp-queries: createThread + getThread + patchThread', () => {
const { db, prep, gid } = inMemDb();
const q = createQueries(db, prep, gid);
const t = q.createThread({ foo: 'bar' });
assert.ok(t.thread_id);
const got = q.getThread(t.thread_id);
assert.deepEqual(got.metadata, { foo: 'bar' });
const patched = q.patchThread(t.thread_id, { metadata: { foo: 'baz' }, status: 'active' });
assert.equal(patched.status, 'active');
assert.deepEqual(q.getThread(t.thread_id).metadata, { foo: 'baz' });
});
section('acp-queries: searchThreads parameterized', () => {
const { db, prep, gid } = inMemDb();
const q = createQueries(db, prep, gid);
q.createThread({ kind: 'a' });
q.createThread({ kind: 'b' });
const r = q.searchThreads({});
assert.equal(r.total, 2);
assert.equal(r.threads.length, 2);
});
section('WsRouter: dispatch registered method', async () => {
const router = new WsRouter();
router.handle('ping', async (p) => ({ pong: p.n }));
const replies = [];
const ws = { readyState: 1, send: (buf) => replies.push(decode(buf)), clientId: 'c1' };
await router.onMessage(ws, encode({ r: 1, m: 'ping', p: { n: 7 } }));
assert.deepEqual(replies[0], { r: 1, d: { pong: 7 } });
});
section('WsRouter: unknown method replies 404', async () => {
const router = new WsRouter();
const replies = [];
const ws = { readyState: 1, send: (buf) => replies.push(decode(buf)), clientId: 'c' };
await router.onMessage(ws, encode({ r: 2, m: 'nope', p: {} }));
assert.equal(replies[0].r, 2);
assert.equal(replies[0].e.c, 404);
});
section('WsRouter: handler exception becomes error reply', async () => {
const router = new WsRouter();
router.handle('boom', async () => { throw Object.assign(new Error('kaboom'), { code: 422 }); });
const replies = [];
const ws = { readyState: 1, send: (buf) => replies.push(decode(buf)), clientId: 'c' };
await router.onMessage(ws, encode({ r: 3, m: 'boom', p: {} }));
assert.equal(replies[0].e.c, 422);
assert.equal(replies[0].e.m, 'kaboom');
});
section('WsRouter: legacy handler called for non-RPC messages', async () => {
const router = new WsRouter();
let seen = null;
router.onLegacy((msg) => { seen = msg; });
const ws = { readyState: 1, send: () => {}, clientId: 'c' };
await router.onMessage(ws, encode({ type: 'subscribe', id: 'x' }));
assert.equal(seen.type, 'subscribe');
});
section('tool-install-machine: actors map exposed', () => {
const actors = toolInstallMachine.getMachineActors();
assert.ok(actors instanceof Map);
});
section('execution-machine: snapshot returns null for unknown id', () => {
assert.equal(execMachine.snapshot('nonexistent-conv-id'), null);
});
section('acp-server-machine: lifecycle transitions', () => {
const actor = acpServerMachine.getOrCreate('test-tool');
assert.equal(actor.getSnapshot().value, 'stopped');
acpServerMachine.send('test-tool', { type: 'START', pid: 123 });
assert.equal(acpServerMachine.get('test-tool').getSnapshot().value, 'starting');
acpServerMachine.send('test-tool', { type: 'HEALTHY', providerInfo: { ok: true } });
assert.equal(acpServerMachine.isHealthy('test-tool'), true);
acpServerMachine.stopAll();
});
section('acp-server-machine: getMachineActors returns Map', () => {
const actors = acpServerMachine.getMachineActors();
assert.ok(actors instanceof Map);
});
section('workflow-plugin: deps only list active plugins', async () => {
const wp = await import('./lib/plugins/workflow-plugin.js');
assert.deepEqual(wp.default.dependencies, ['database']);
assert.equal(typeof wp.default.init, 'function');
});
section('agent-registry: hermes registered as stdio ACP', async () => {
const { registry } = await import('./lib/claude-runner-agents.js');
assert.ok(registry.has('hermes'));
const h = registry.get('hermes');
assert.equal(h.name, 'Hermes Agent');
assert.equal(h.command, 'hermes');
assert.equal(h.protocol, 'acp');
assert.deepEqual(h.buildArgs(), ['acp']);
assert.ok(h.supportedFeatures.includes('acp-protocol'));
});
section('delete-all: soft-deletes conv rows + wipes related data', () => {
const { db, prep, gid } = inMemDb();
const q = createQueries(db, prep, gid);
const c1 = q.createConversation('claude-code', 'A');
q.createConversation('claude-code', 'B');
q.createSession(c1.id);
q.createMessage(c1.id, 'user', 'hello');
const origLog = console.log; console.log = () => {};
try { q.deleteAllConversations(); } finally { console.log = origLog; }
const rows = db.prepare('SELECT status, count(*) as c FROM conversations GROUP BY status').all();
assert.deepEqual(rows, [{ status: 'deleted', c: 2 }]);
assert.equal(db.prepare('SELECT count(*) as c FROM messages').get().c, 0);
assert.equal(db.prepare('SELECT count(*) as c FROM sessions').get().c, 0);
assert.equal(q.getConversationsList().length, 0);
});
console.log(`\n${passed} passed, ${failed} failed`);
process.exit(failed === 0 ? 0 : 1);