-
Notifications
You must be signed in to change notification settings - Fork 819
Expand file tree
/
Copy pathextensions.test.js
More file actions
288 lines (245 loc) · 8.67 KB
/
Copy pathextensions.test.js
File metadata and controls
288 lines (245 loc) · 8.67 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
'use strict';
const { describe, it, before, after } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { MailboxStore } = require('../src/proxy/mailbox/store');
const { SkillUpdater } = require('../src/proxy/extensions/skillUpdater');
const { DmHandler } = require('../src/proxy/extensions/dmHandler');
const { SessionHandler } = require('../src/proxy/extensions/sessionHandler');
function tmpDataDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'extensions-test-'));
}
describe('SkillUpdater', () => {
let store, dataDir, skillDir;
before(() => {
dataDir = tmpDataDir();
skillDir = tmpDataDir();
store = new MailboxStore(dataDir);
});
after(() => {
store.close();
try { fs.rmSync(dataDir, { recursive: true }); } catch {}
try { fs.rmSync(skillDir, { recursive: true }); } catch {}
});
it('updates skill.md from inbound message', () => {
const skillPath = path.join(skillDir, 'SKILL.md');
const updater = new SkillUpdater({
store,
skillPath,
logger: { log: () => {}, warn: () => {}, error: () => {} },
});
const result = updater.processSkillUpdate({
payload: { content: '# Updated Skill\nNew content here.', version: '1.1.0' },
});
assert.equal(result, true);
assert.equal(fs.readFileSync(skillPath, 'utf8'), '# Updated Skill\nNew content here.');
assert.equal(store.getState('skill_version'), '1.1.0');
});
it('creates backup before overwriting', () => {
const skillPath = path.join(skillDir, 'SKILL2.md');
fs.writeFileSync(skillPath, 'original content', 'utf8');
const updater = new SkillUpdater({
store,
skillPath,
logger: { log: () => {}, warn: () => {}, error: () => {} },
});
updater.processSkillUpdate({
payload: { content: 'updated content', version: '2.0' },
});
assert.equal(fs.readFileSync(skillPath, 'utf8'), 'updated content');
assert.equal(fs.readFileSync(skillPath + '.bak', 'utf8'), 'original content');
});
it('returns false without skill path', () => {
const updater = new SkillUpdater({
store,
logger: { log: () => {}, warn: () => {}, error: () => {} },
});
assert.equal(updater.processSkillUpdate({ payload: { content: 'x' } }), false);
});
it('returns false without content', () => {
const updater = new SkillUpdater({
store,
skillPath: path.join(skillDir, 'noop.md'),
logger: { log: () => {}, warn: () => {}, error: () => {} },
});
assert.equal(updater.processSkillUpdate({ payload: {} }), false);
});
it('pollAndApply processes pending skill_update messages', () => {
const dir2 = tmpDataDir();
const s2 = new MailboxStore(dir2);
const sp = path.join(skillDir, 'polled.md');
s2.writeInbound({
type: 'skill_update',
payload: { content: '# Polled skill', version: '3.0' },
});
const updater = new SkillUpdater({
store: s2,
skillPath: sp,
logger: { log: () => {}, warn: () => {}, error: () => {} },
});
const applied = updater.pollAndApply();
assert.equal(applied, 1);
assert.equal(fs.readFileSync(sp, 'utf8'), '# Polled skill');
s2.close();
try { fs.rmSync(dir2, { recursive: true }); } catch {}
});
});
describe('DmHandler', () => {
let store, handler, dataDir;
before(() => {
dataDir = tmpDataDir();
store = new MailboxStore(dataDir);
handler = new DmHandler({ store });
});
after(() => {
store.close();
try { fs.rmSync(dataDir, { recursive: true }); } catch {}
});
it('sends a DM and creates outbound message', () => {
const result = handler.send({
recipientNodeId: 'node_abc',
content: 'Hello there',
});
assert.ok(result.message_id);
assert.equal(result.status, 'pending');
const msg = store.getById(result.message_id);
assert.equal(msg.type, 'dm');
assert.equal(msg.direction, 'outbound');
assert.equal(msg.payload.recipient_node_id, 'node_abc');
assert.equal(msg.payload.content, 'Hello there');
});
it('throws on missing recipientNodeId', () => {
assert.throws(() => handler.send({ content: 'x' }), /recipientNodeId/);
});
it('throws on missing content', () => {
assert.throws(() => handler.send({ recipientNodeId: 'n' }), /content/);
});
it('polls inbound DMs', () => {
store.writeInbound({ type: 'dm', payload: { content: 'incoming dm' } });
const msgs = handler.poll();
assert.ok(msgs.length >= 1);
assert.equal(msgs[0].type, 'dm');
});
it('acks DM messages', () => {
const id = store.writeInbound({ type: 'dm', payload: { content: 'to ack' } });
const count = handler.ack(id);
assert.equal(count, 1);
const msg = store.getById(id);
assert.equal(msg.status, 'delivered');
});
it('lists DM history', () => {
const msgs = handler.list();
assert.ok(Array.isArray(msgs));
});
});
describe('SessionHandler', () => {
let store, handler, dataDir;
before(() => {
dataDir = tmpDataDir();
store = new MailboxStore(dataDir);
handler = new SessionHandler({ store });
});
after(() => {
store.close();
try { fs.rmSync(dataDir, { recursive: true }); } catch {}
});
it('creates a session and stores outbound message', () => {
const result = handler.createSession({
title: 'Test Session',
description: 'A test collaboration session',
inviteNodeIds: ['node_a', 'node_b'],
});
assert.ok(result.message_id);
assert.equal(result.status, 'pending');
const msg = store.getById(result.message_id);
assert.equal(msg.type, 'session_create');
assert.equal(msg.direction, 'outbound');
assert.equal(msg.payload.title, 'Test Session');
assert.deepEqual(msg.payload.invite_node_ids, ['node_a', 'node_b']);
});
it('throws on missing title', () => {
assert.throws(() => handler.createSession({}), /title/);
});
it('joins a session', () => {
const result = handler.joinSession({ sessionId: 'sess_123' });
assert.ok(result.message_id);
const msg = store.getById(result.message_id);
assert.equal(msg.type, 'session_join');
assert.equal(msg.payload.session_id, 'sess_123');
});
it('throws on join without sessionId', () => {
assert.throws(() => handler.joinSession({}), /sessionId/);
});
it('leaves a session', () => {
const result = handler.leaveSession({ sessionId: 'sess_456' });
assert.ok(result.message_id);
const msg = store.getById(result.message_id);
assert.equal(msg.type, 'session_leave');
});
it('sends a message to a session', () => {
const result = handler.sendMessage({
sessionId: 'sess_789',
toNodeId: 'node_c',
msgType: 'context_update',
payload: { key: 'value' },
});
assert.ok(result.message_id);
const msg = store.getById(result.message_id);
assert.equal(msg.type, 'session_message');
assert.equal(msg.payload.session_id, 'sess_789');
assert.equal(msg.payload.to_node_id, 'node_c');
});
it('throws on send message with oversized payload', () => {
const bigPayload = { data: 'x'.repeat(17000) };
assert.throws(() => handler.sendMessage({
sessionId: 'sess_big',
payload: bigPayload,
}), /too large/);
});
it('delegates a subtask', () => {
const result = handler.delegateSubtask({
sessionId: 'sess_del',
toNodeId: 'node_worker',
title: 'Implement feature X',
role: 'builder',
});
assert.ok(result.message_id);
const msg = store.getById(result.message_id);
assert.equal(msg.type, 'session_delegate');
assert.equal(msg.payload.role, 'builder');
assert.equal(msg.payload.title, 'Implement feature X');
});
it('normalizes invalid role to builder', () => {
const result = handler.delegateSubtask({
sessionId: 'sess_role',
title: 'Fix bug',
role: 'invalid_role',
});
const msg = store.getById(result.message_id);
assert.equal(msg.payload.role, 'builder');
});
it('submits a result', () => {
const result = handler.submitResult({
sessionId: 'sess_sub',
taskId: 'task_1',
resultAssetId: 'asset_1',
summary: 'Completed the implementation',
});
assert.ok(result.message_id);
const msg = store.getById(result.message_id);
assert.equal(msg.type, 'session_submit');
assert.equal(msg.payload.task_id, 'task_1');
});
it('polls session invites', () => {
store.writeInbound({ type: 'collaboration_invite', payload: { session_id: 's1' } });
const msgs = handler.pollInvites();
assert.ok(msgs.length >= 1);
});
it('lists active sessions', () => {
const sessions = handler.listActiveSessions();
assert.ok(Array.isArray(sessions));
assert.ok(sessions.length > 0);
});
});