-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest-phase5.ts
More file actions
326 lines (258 loc) · 11.8 KB
/
test-phase5.ts
File metadata and controls
326 lines (258 loc) · 11.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
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
/**
* Network-AI Phase 5 — Named Multi-Blackboard API
*
* Tests for `orchestrator.getBlackboard(name)` and related methods.
*
* Run with:
* npx ts-node test-phase5.ts
*
* No API key required. Zero external dependencies.
*/
import { tmpdir } from 'os';
import { join } from 'path';
import { existsSync, rmSync } from 'fs';
import { SwarmOrchestrator, NamedBlackboardOptions } from './index';
// ============================================================================
// TEST HELPERS
// ============================================================================
let passCount = 0;
let failCount = 0;
const c = {
reset : '\x1b[0m',
bold : '\x1b[1m',
green : '\x1b[32m',
red : '\x1b[31m',
yellow: '\x1b[33m',
cyan : '\x1b[36m',
dim : '\x1b[2m',
};
function pass(test: string): void {
passCount++;
console.log(` ${c.green}[PASS]${c.reset} ${test}`);
}
function fail(test: string, err?: string): void {
failCount++;
console.log(` ${c.red}[FAIL]${c.reset} ${test}${err ? ` — ${err}` : ''}`);
}
function assert(cond: boolean, name: string, err?: string): void {
if (cond) pass(name);
else fail(name, err);
}
async function assertThrows(fn: () => unknown, name: string): Promise<void> {
try {
await fn();
fail(name, 'Expected an error but none was thrown');
} catch {
pass(name);
}
}
function header(title: string): void {
console.log(`\n${c.bold}${c.cyan}${'─'.repeat(64)}${c.reset}`);
console.log(`${c.bold} ${title}${c.reset}`);
console.log(`${c.cyan}${'─'.repeat(64)}${c.reset}`);
}
// Unique tmp dir per run so tests are fully isolated
const TMP = join(tmpdir(), `network-ai-phase5-${Date.now()}`);
function makeOrchestrator(): SwarmOrchestrator {
return new SwarmOrchestrator(TMP);
}
// ============================================================================
// SECTION 1 — getBlackboard() basics
// ============================================================================
async function testGetBlackboardBasics(): Promise<void> {
header('SECTION 1 — getBlackboard() basics');
const orch = makeOrchestrator();
// Returns a board instance
const board = orch.getBlackboard('alpha');
assert(board !== null && board !== undefined, 'getBlackboard returns a board instance');
// Calling it again returns the SAME instance (idempotent)
const board2 = orch.getBlackboard('alpha');
assert(board === board2, 'getBlackboard is idempotent — same instance returned');
// Two different names → two different instances
const beta = orch.getBlackboard('beta');
assert(board !== beta, 'Different names produce different instances');
// Board directory is created on disk
const boardPath = join(TMP, 'boards', 'alpha');
assert(existsSync(boardPath), 'Board directory created on disk');
}
// ============================================================================
// SECTION 2 — Isolation: boards do NOT share data
// ============================================================================
async function testBoardIsolation(): Promise<void> {
header('SECTION 2 — Board isolation');
const orch = makeOrchestrator();
const alpha = orch.getBlackboard('alpha');
const beta = orch.getBlackboard('beta');
// Register agents and write to alpha
alpha.registerAgent('agentA', 'tok-a', ['*']);
alpha.write('shared-key', { board: 'alpha' }, 'agentA', undefined, 'tok-a');
// beta should not see alpha's data
const betaEntry = beta.read('shared-key');
assert(betaEntry === null, 'Beta board cannot read data written to alpha board');
// Write same key to beta with different value
beta.registerAgent('agentB', 'tok-b', ['*']);
beta.write('shared-key', { board: 'beta' }, 'agentB', undefined, 'tok-b');
const alphaEntry = alpha.read('shared-key');
const betaEntry2 = beta.read('shared-key');
assert(
(alphaEntry?.value as any)?.board === 'alpha',
'Alpha board retains its own data after beta write',
);
assert(
(betaEntry2?.value as any)?.board === 'beta',
'Beta board has its own independent value for the same key',
);
}
// ============================================================================
// SECTION 3 — listBlackboards() and hasBlackboard()
// ============================================================================
async function testListAndHas(): Promise<void> {
header('SECTION 3 — listBlackboards() and hasBlackboard()');
const orch = makeOrchestrator();
assert(orch.listBlackboards().length === 0, 'listBlackboards() starts empty');
assert(!orch.hasBlackboard('alpha'), 'hasBlackboard() returns false before creation');
orch.getBlackboard('alpha');
orch.getBlackboard('beta');
orch.getBlackboard('gamma');
const names = orch.listBlackboards();
assert(names.length === 3, 'listBlackboards() returns all 3 boards');
assert(names.includes('alpha'), 'listBlackboards() includes alpha');
assert(names.includes('beta'), 'listBlackboards() includes beta');
assert(names.includes('gamma'), 'listBlackboards() includes gamma');
assert(orch.hasBlackboard('alpha'), 'hasBlackboard() returns true for existing board');
assert(!orch.hasBlackboard('delta'), 'hasBlackboard() returns false for non-existent board');
}
// ============================================================================
// SECTION 4 — destroyBlackboard()
// ============================================================================
async function testDestroyBlackboard(): Promise<void> {
header('SECTION 4 — destroyBlackboard()');
const orch = makeOrchestrator();
orch.getBlackboard('alpha');
const removed = orch.destroyBlackboard('alpha');
assert(removed === true, 'destroyBlackboard() returns true when board existed');
assert(!orch.hasBlackboard('alpha'), 'Board is no longer in registry after destroy');
const removedAgain = orch.destroyBlackboard('alpha');
assert(removedAgain === false, 'destroyBlackboard() returns false for already-removed board');
// On-disk data still exists after destroy (data is NOT deleted)
const boardPath = join(TMP, 'boards', 'alpha');
// Note: destroy is called AFTER the board was created, so path should still exist
assert(existsSync(boardPath), 'On-disk data is NOT deleted by destroyBlackboard()');
// Re-attach to same persistent board
const reattached = orch.getBlackboard('alpha');
assert(reattached !== null, 'Can re-attach to same board after destroy (persistent data)');
}
// ============================================================================
// SECTION 5 — NamedBlackboardOptions
// ============================================================================
async function testNamedBlackboardOptions(): Promise<void> {
header('SECTION 5 — NamedBlackboardOptions');
const orch = makeOrchestrator();
const opts: NamedBlackboardOptions = {
allowedNamespaces: ['result:', 'analysis:'],
};
const board = orch.getBlackboard('restricted', opts);
board.registerAgent('analyst', 'tok-analyst', ['result:', 'analysis:']);
// Write within allowed namespace
board.write('result:summary', { ok: true }, 'analyst', undefined, 'tok-analyst');
const entry = board.read('result:summary');
assert(entry !== null, 'Write to allowed namespace succeeds');
// Options are accepted without throwing
pass('NamedBlackboardOptions accepted without error');
}
// ============================================================================
// SECTION 6 — Input validation
// ============================================================================
async function testInputValidation(): Promise<void> {
header('SECTION 6 — Input validation');
const orch = makeOrchestrator();
await assertThrows(() => orch.getBlackboard(''), 'Empty name throws');
await assertThrows(() => orch.getBlackboard(' '), 'Whitespace-only name throws');
await assertThrows(() => orch.getBlackboard('bad name'), 'Name with space throws');
await assertThrows(() => orch.getBlackboard('bad/name'), 'Name with slash throws');
await assertThrows(() => orch.getBlackboard('bad:name'), 'Name with colon throws');
// Valid names
const validNames = ['alpha', 'proj-1', 'my_board', 'Board123', 'a'];
for (const name of validNames) {
try {
orch.getBlackboard(name);
pass(`Valid name accepted: "${name}"`);
} catch (e) {
fail(`Valid name rejected: "${name}"`, String(e));
}
}
}
// ============================================================================
// SECTION 7 — Named board does not affect default orchestrator blackboard
// ============================================================================
async function testDefaultBlackboardUnaffected(): Promise<void> {
header('SECTION 7 — Default blackboard is unaffected');
const orch = makeOrchestrator();
// Write to default blackboard via execute
const result = await orch.execute(
'update_blackboard',
{ key: 'default:check', value: { source: 'default' } },
{ agentId: 'orchestrator' },
);
assert(result.success === true, 'Write to default blackboard succeeds');
// Create a named board with the same key
const named = orch.getBlackboard('side-board');
named.registerAgent('agentX', 'tok-x', ['*']);
named.write('default:check', { source: 'named' }, 'agentX', undefined, 'tok-x');
// The named board read should return the named board value
const namedEntry = named.read('default:check');
assert(
(namedEntry?.value as any)?.source === 'named',
'Named board has its own value for the key',
);
// Default board is separate (we can't read it directly in tests, but no cross-contamination)
pass('Default blackboard unaffected by named board writes (separate LockedBlackboard instances)');
}
// ============================================================================
// MAIN
// ============================================================================
async function main(): Promise<void> {
console.log(`\n${c.bold}${'='.repeat(64)}${c.reset}`);
console.log(`${c.bold} Network-AI Phase 5 — Named Multi-Blackboard API${c.reset}`);
console.log(`${c.bold}${'='.repeat(64)}${c.reset}`);
try {
await testGetBlackboardBasics();
await testBoardIsolation();
await testListAndHas();
await testDestroyBlackboard();
await testNamedBlackboardOptions();
await testInputValidation();
await testDefaultBlackboardUnaffected();
console.log(`\n${c.bold}${'─'.repeat(64)}${c.reset}`);
console.log(`${c.bold} RESULTS${c.reset}`);
console.log(`${c.bold}${'─'.repeat(64)}${c.reset}`);
console.log(`\n ${c.green}Passed: ${passCount}${c.reset}`);
if (failCount > 0) {
console.log(` ${c.red}Failed: ${failCount}${c.reset}`);
} else {
console.log(` ${c.dim}Failed: 0${c.reset}`);
}
console.log('');
if (failCount === 0) {
console.log(` ${c.green}${c.bold}All tests passed!${c.reset}`);
console.log(`\n ${c.cyan}Verified:${c.reset}`);
console.log(` ${c.cyan}[PASS] getBlackboard() — creates isolated boards on disk${c.reset}`);
console.log(` ${c.cyan}[PASS] Board isolation — boards do not share data${c.reset}`);
console.log(` ${c.cyan}[PASS] listBlackboards() / hasBlackboard() — registry tracking${c.reset}`);
console.log(` ${c.cyan}[PASS] destroyBlackboard() — in-memory removal, data preserved${c.reset}`);
console.log(` ${c.cyan}[PASS] NamedBlackboardOptions — namespace restrictions${c.reset}`);
console.log(` ${c.cyan}[PASS] Input validation — invalid names rejected${c.reset}`);
console.log(` ${c.cyan}[PASS] Default blackboard unaffected${c.reset}`);
} else {
console.log(` ${c.yellow}${failCount} test(s) failed. Review output above.${c.reset}`);
}
console.log('\n');
// Cleanup tmp directory
try { rmSync(TMP, { recursive: true, force: true }); } catch { /* ignore */ }
process.exit(failCount > 0 ? 1 : 0);
} catch (err) {
console.error('\n Unexpected test runner error:', err);
process.exit(1);
}
}
main();