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 }); const names: string[] = []; // every rpc-req name received, in order 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') { names.push(f.name); 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`, names, 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(); }); /** The `ws` client, reporting a send buffer over the 1 MiB mark while `stalled`. */ let stalled = false; const realBuffered = Object.getOwnPropertyDescriptor(WS.prototype, 'bufferedAmount')!.get!; class Backed extends WS { constructor(url: string) { super(url); Object.defineProperty(this, 'bufferedAmount', { get: () => (stalled ? 2_000_000 : realBuffered.call(this)) }); } } const BackedCtor = Backed as unknown as SocketCtor; it('holds an rpc-req while the link is backed up, and sends it once it drains (#41)', async () => { const t = startTeapot(); const link = await connectLink({ url: t.url, secret: 'good', Ctor: BackedCtor }); stalled = true; let settled = false; const call = link.rpc('scope', 'auth', env).finally(() => (settled = true)); await new Promise((resolve) => setTimeout(resolve, 60)); expect(settled).toBe(false); stalled = false; expect(await call).toEqual({ got: 'auth' }); link.close(); t.close(); }); it('times out an rpc still waiting for room, and never sends it (#41)', async () => { const t = startTeapot(); const link = await connectLink({ url: t.url, secret: 'good', timeoutMs: 100, Ctor: BackedCtor }); stalled = true; await expect(link.rpc('scope', 'auth', env)).rejects.toMatchObject({ status: 504 }); stalled = false; expect(await link.rpc('scope', 'next', env)).toEqual({ got: 'next' }); expect(t.names).toEqual(['next']); // the timed-out call was dropped, not sent late link.close(); t.close(); }); });