Files
core/test/mesh/link.test.ts
T
chrnx bd478ce09c build!: require Node 22
Node 18 and Node 20 are end-of-life and receive no security patches, so
supporting them before RC.1 would mean promising a runtime nobody can
patch. Node 22 is the oldest line still maintained.

engines.node moves to >=22, @types/node to ^22, the tsup target to
node22, and CI's build job from Node 18 to 22, so every workflow now
runs Node 22 only. The docs, the bug report example and the comments
that stated 18 as the floor follow.

The node:crypto fallback in the HTTP core existed only for Node 18,
which lacked the global crypto; it goes with its regression test, which
said as much. The ws fallback for the WebSocket client stays: Node 22
has the global, but it can still be switched off with a flag.

Moving off Node 18 and 20 also unblocks vitest 4, which the remaining
dev audit advisories need.

BREAKING CHANGE: Node 18 and Node 20 are no longer supported. The
minimum is Node 22.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-10 12:10:52 -05:00

139 lines
5.6 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import WS, { WebSocketServer } from 'ws';
import { connectLink } from '../../src/mesh/link';
import { loadWebSocketCtor, type SocketCtor } from '../../src/http/ws-adapter';
import { encode, decode, MESH_PROTOCOL_VERSION } from '../../src/mesh/protocol';
const V = MESH_PROTOCOL_VERSION;
const env = { method: 'GET', params: {}, query: {}, body: undefined, headers: {} };
// teapot that validates secret 'good', returns a manifest, answers scope rpc (boom -> error)
function startTeapot(secret = 'good') {
const wss = new WebSocketServer({ port: 0 });
wss.on('connection', (ws) => {
let authed = false;
ws.on('message', (d) => {
const f = decode(d.toString());
if (!authed) {
if (f.type === 'hello' && f.secret === secret) { authed = true; ws.send(encode({ type: 'manifest', v: V, steps: ['auth'], routes: [] })); }
else ws.close(1008);
return;
}
if (f.type === 'rpc-req') {
if (f.name === 'boom') ws.send(encode({ type: 'rpc-res', id: f.id, ok: false, error: { message: 'denied', status: 403 } }));
else ws.send(encode({ type: 'rpc-res', id: f.id, ok: true, result: { got: f.name } }));
}
});
});
const port = (wss.address() as any).port;
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, close: () => wss.close() };
}
// teapot that completes the handshake but NEVER answers rpc-req (for timeout)
function startSilentTeapot(secret = 'good') {
const wss = new WebSocketServer({ port: 0 });
wss.on('connection', (ws) => {
ws.on('message', (d) => {
const f = decode(d.toString());
if (f.type === 'hello' && f.secret === secret) ws.send(encode({ type: 'manifest', v: V, steps: [], routes: [] }));
// ignore rpc-req entirely
});
});
const port = (wss.address() as any).port;
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, close: () => wss.close() };
}
// teapot running a future protocol: handshakes, but answers with a skewed manifest
function startSkewedTeapot() {
const wss = new WebSocketServer({ port: 0 });
wss.on('connection', (ws) => {
ws.on('message', () => ws.send(encode({ type: 'manifest', v: V + 1, steps: [], routes: [] } as any)));
});
const port = (wss.address() as any).port;
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, close: () => wss.close() };
}
/**
* Both client implementations, exercised on whatever Node runs the suite. `ws` is always in the
* list: it is the fallback for a Node run with the global WebSocket switched off, and every
* supported Node has the global, so it would otherwise never be covered and rot unnoticed.
*/
const platform = (globalThis as { WebSocket?: SocketCtor }).WebSocket;
const CTORS: [string, SocketCtor][] = [['the ws package (fallback path)', WS as unknown as SocketCtor]];
if (platform) CTORS.push(['the platform global WebSocket', platform]);
describe('WebSocket client implementations', () => {
it.each(CTORS)('handshakes and serves RPC over %s', async (_label, Ctor) => {
const t = startTeapot();
const link = await connectLink({ url: t.url, secret: 'good', Ctor });
expect(link.manifest.steps).toEqual(['auth']);
expect(await link.rpc('scope', 'auth', env)).toEqual({ got: 'auth' });
link.close();
t.close();
});
it('prefers the platform global when it exists, else falls back to the ws package', () => {
expect(loadWebSocketCtor()).toBe(platform ?? (WS as unknown as SocketCtor));
});
});
describe('connectLink', () => {
it('sends its protocol version in the hello', async () => {
const seen: any[] = [];
const wss = new WebSocketServer({ port: 0 });
wss.on('connection', (ws) =>
ws.on('message', (d) => {
seen.push(decode(d.toString()));
ws.send(encode({ type: 'manifest', v: V, steps: [], routes: [] }));
}),
);
const url = `ws://127.0.0.1:${(wss.address() as any).port}/__mesh__/control`;
const link = await connectLink({ url, secret: 'good' });
expect(seen[0]).toMatchObject({ type: 'hello', v: V });
link.close();
wss.close();
});
it('refuses a teapot on a different protocol version', async () => {
const t = startSkewedTeapot();
await expect(connectLink({ url: t.url, secret: 'good', timeoutMs: 500 })).rejects.toThrow(/version/i);
t.close();
});
it('handshakes and exposes the manifest', async () => {
const t = startTeapot();
const link = await connectLink({ url: t.url, secret: 'good' });
expect(link.manifest.steps).toEqual(['auth']);
link.close(); t.close();
});
it('rpc resolves a result by id', async () => {
const t = startTeapot();
const link = await connectLink({ url: t.url, secret: 'good' });
expect(await link.rpc('scope', 'auth', env)).toEqual({ got: 'auth' });
link.close(); t.close();
});
it('rpc rejects with an HttpError carrying the remote status', async () => {
const t = startTeapot();
const link = await connectLink({ url: t.url, secret: 'good' });
await expect(link.rpc('scope', 'boom', env)).rejects.toMatchObject({ status: 403, message: 'denied' });
link.close(); t.close();
});
it('rejects connect on bad secret', async () => {
const t = startTeapot();
await expect(connectLink({ url: t.url, secret: 'wrong', timeoutMs: 500 })).rejects.toThrow();
t.close();
});
it('times out an rpc when the teapot never answers', async () => {
const t = startSilentTeapot();
const link = await connectLink({ url: t.url, secret: 'good', timeoutMs: 150 });
await expect(link.rpc('scope', 'whatever', env)).rejects.toThrow(/timeout/i);
link.close(); t.close();
});
});