@masstensor/diagnose 0.7.0 → 0.8.0

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.
@@ -98,3 +98,45 @@ test('session idle and lifetime clocks start after pairing, not while the code i
98
98
  try { await joiner.open(); assert.equal(await host.ended, 'Idle timeout'); }
99
99
  finally { joiner.close(); host.close(); await relay.close(); await rm(root, { recursive: true, force: true }); }
100
100
  });
101
+
102
+ test('relay renews the locked room on encrypted traffic past six hours and expires a silent room', { timeout: 10000 }, async t => {
103
+ let now = Date.now();
104
+ t.mock.method(Date, 'now', () => now);
105
+ const relay = createRelay({ port: 0 }), url = 'ws://127.0.0.1:' + await relay.listen(), code = createInvitation();
106
+ const host = new Peer({ role: 'host', code, relay: url, accept: async () => true });
107
+ let joiner: Peer | undefined;
108
+ try {
109
+ await host.open();
110
+ joiner = new Peer({ role: 'join', code, relay: url });
111
+ await joiner.open();
112
+ await host.active;
113
+ for (let round = 0; round < 3; round++) {
114
+ now += 5 * 60 * 60_000;
115
+ const received = once(host, 'message');
116
+ await joiner.send({ type: 'task.cancel', id: '26155a8b-c35a-436e-948a-9797732e6259' });
117
+ assert.equal((await received)[0].type, 'task.cancel');
118
+ }
119
+ now += 6 * 60 * 60_000;
120
+ await joiner.send({ type: 'ping' });
121
+ assert.equal(await host.ended, 'Relay session expired');
122
+ assert.equal(await joiner.ended, 'Relay session expired');
123
+ } finally { joiner?.close(); host.close(); await relay.close(); }
124
+ });
125
+
126
+ test('normal heartbeat is not diagnostic activity but an explicit result poll is', { timeout: 5000 }, async () => {
127
+ const relay = createRelay({ port: 0 }), url = 'ws://127.0.0.1:' + await relay.listen(), code = createInvitation();
128
+ const host = new Peer({ role: 'host', code, relay: url, accept: async () => true });
129
+ await host.open();
130
+ const joiner = new Peer({ role: 'join', code, relay: url });
131
+ let activity = 0;
132
+ host.on('activity', () => { activity++; });
133
+ try {
134
+ await joiner.open();
135
+ await joiner.send({ type: 'ping' });
136
+ assert.equal(activity, 0);
137
+ const touched = once(host, 'activity');
138
+ await joiner.send({ type: 'ping', activity: true });
139
+ await touched;
140
+ assert.equal(activity, 1);
141
+ } finally { joiner.close(); host.close(); await relay.close(); }
142
+ });
@@ -0,0 +1,79 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { spawn } from 'node:child_process';
4
+ import { createHash } from 'node:crypto';
5
+ import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
6
+ import { join, resolve } from 'node:path';
7
+ import { tmpdir } from 'node:os';
8
+ import { createRelay } from '../dist/relay.js';
9
+ import { createInvitation } from '../dist/crypto.js';
10
+ import { Peer } from '../dist/peer.js';
11
+ import { ProbeEngine } from '../dist/engine.js';
12
+ import { Audit } from '../dist/util.js';
13
+
14
+ function helper(action: string, input: Record<string, unknown>): Promise<Record<string, any>> {
15
+ const script = process.env.DIAGNOSE_TEST_SKILL ?? resolve('skills/connect/scripts/run.mjs');
16
+ const child = spawn(process.execPath, [script, action], { stdio: 'pipe' });
17
+ let out = '', err = '';
18
+ child.stdout.setEncoding('utf8').on('data', text => { out += text; });
19
+ child.stderr.setEncoding('utf8').on('data', text => { err += text; });
20
+ child.stdin.end(JSON.stringify(input));
21
+ return new Promise((resolve, reject) => {
22
+ child.on('error', reject);
23
+ child.on('close', status => {
24
+ if (status !== 0) { reject(new Error(err)); return; }
25
+ try { resolve(JSON.parse(out)); }
26
+ catch (error) { reject(error); }
27
+ });
28
+ });
29
+ }
30
+
31
+ test('native Skill helper pairs once, reuses the connection for a repair and closes it', { timeout: 30000 }, async () => {
32
+ const scratch = await mkdtemp(join(tmpdir(), 'diagnose-skill-'));
33
+ const config = join(scratch, 'config.txt');
34
+ await writeFile(config, 'offline\n');
35
+ const relay = createRelay({ port: 0 });
36
+ const url = 'ws://127.0.0.1:' + await relay.listen();
37
+ const code = createInvitation();
38
+ let pairs = 0;
39
+ const approvals: Array<string> = [];
40
+ const host = new Peer({ role: 'host', code, relay: url, accept: async () => { pairs++; return true; } });
41
+ new ProbeEngine(host, async request => { approvals.push(request.stage); return true; }, new Audit(join(scratch, 'audit')));
42
+ let session: string | undefined;
43
+ try {
44
+ await host.open();
45
+ const opened = await helper('open', { code, label: 'Skill test', relay: url });
46
+ assert.equal(opened.connected, true);
47
+ assert.equal(opened.idleSeconds, 6 * 60 * 60);
48
+ session = opened.session;
49
+ assert.ok(session);
50
+ const call = (tool: string, args: object) => helper('call', { session, tool, arguments: args });
51
+ const run = async (operation: string, args: object) => {
52
+ const { id } = await call('diagnose_start', { operation, arguments: args, reason: 'Test an isolated demo configuration' });
53
+ return call('diagnose_wait', { id, timeoutMs: 5000 });
54
+ };
55
+ const forged = session.replace(/.$/, session.endsWith('0') ? '1' : '0');
56
+ await assert.rejects(helper('close', { session: forged }), /Invalid skill session/);
57
+ await assert.rejects(call('diagnose_start', { operation: 'net.forward', arguments: {} }), /Unsupported operation/);
58
+ const inspection = await run('system.inspect', {});
59
+ assert.equal(inspection.result.data.platform, process.platform);
60
+ assert.ok(Array.isArray(inspection.result.data.serialPorts));
61
+ assert.equal((await run('file.read', { path: config })).outputs.file, 'offline\n');
62
+ const expectedSha256 = createHash('sha256').update('offline\n').digest('hex');
63
+ assert.equal((await run('file.write', { path: config, content: 'online\n', expectedSha256 })).result.status, 'ok');
64
+ assert.equal((await run('file.read', { path: config })).outputs.file, 'online\n');
65
+ assert.equal(await readFile(config, 'utf8'), 'online\n');
66
+ assert.equal(pairs, 1);
67
+ assert.deepEqual(approvals, ['execute', 'send', 'execute', 'send', 'execute', 'send', 'execute', 'send']);
68
+ const handle = session;
69
+ assert.equal((await helper('close', { session })).disconnected, true);
70
+ session = undefined;
71
+ assert.equal(await host.ended, 'Diagnostician ended session');
72
+ await assert.rejects(helper('call', { session: handle, tool: 'diagnose_wait', arguments: {} }), /session unavailable/i);
73
+ } finally {
74
+ if (session) await helper('close', { session }).catch(() => {});
75
+ host.close();
76
+ await relay.close();
77
+ await rm(scratch, { recursive: true, force: true });
78
+ }
79
+ });