Files
core/test/router-parity/ws.test.ts
T
chrnx c30cf7e457 test(router): freeze the differential, application, WS and boot-error corpus
The parity corpus from ROUTER-CORPUS §2, with every expected output captured
from ed47836 rather than written by hand.

Matcher: 24 hand-picked scenarios (precedence, constraint ties, exact versus
catch-all, equal-rank and same-pattern ties with reversed twins, implicit and
explicit HEAD, streaming GETs, canonical Allow, encoding, case, Unicode and
normalization) plus 40 scenarios from a fixed-seed generator
(router-parity-gen-v1, seed 10908). The generated file stores the seed, the
rules and every generated input; the test regenerates and requires a match,
so changing the generator cannot move the baseline. Each scenario runs through
the oracle and the live router and both must equal the capture, compared by
declaration ID, decoded params, implicitHead, ordered Allow, or the
normalization error.

Application level: status, Allow, headers and body through app.fetch and a
Node socket, including HEAD body suppression, 400/404/405 and OPTIONS.

WebSocket, separate from HTTP: first-registered selection, an HTTP route at the
same path not taking the upgrade, and the reserved mesh control path, through
app.upgrade and the Node listener. This freezes one adapter disagreement as
observed: a user @Ws('/__mesh__/control') on an app without mesh is refused by
app.upgrade (1008) and served by the Node listener.

Boot-time rejections: pattern compilation, local ambiguity and mesh splicing
(duplicate exports, same-shape exports, local-over-remote precedence with its
warning, and a remote static outranking a local parameter route).

Refs #109

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

184 lines
6.1 KiB
TypeScript

/**
* WebSocket upgrade parity, kept apart from HTTP on purpose: WS selection is first-registered-wins
* with no specificity ranking, an HTTP route at the same path never takes the upgrade, and the mesh
* control path is checked before WS route matching. Captured through both upgrade entry points —
* `app.upgrade` (Deno/Bun/edge) and the Node `server.on('upgrade')` listener — because they are
* separate code paths and do not agree on everything (see the reserved-path rows in the fixture).
*/
import 'reflect-metadata';
import type { AddressInfo } from 'net';
import WebSocket from 'ws';
import { describe, it, beforeAll, afterAll } from 'vitest';
import { createApp, Route, Get, Post, Ws, Stream, Module, param } from '../../src/index';
import { channel } from '../../src/channel';
import type { App } from '../../src/app/types';
import type { WsRequest, WsSocket } from '../../src/http/ws-core';
import { frozen, expectFrozenEqual } from './frozen';
const announce = (id: string, params: Record<string, string>) =>
(async function* () {
yield { id, params };
})();
@Route('/')
class ParityWs {
// Registered before the static twin: WS does not rank, so this one answers /live/lobby.
@Ws('/live/:room') liveRoom(@param() p: Record<string, string>) {
return announce('liveRoom', p);
}
@Ws('/live/lobby') liveLobby(@param() p: Record<string, string>) {
return announce('liveLobby', p);
}
// Same path as the WS routes, different method, so the boot accepts it; it must not take upgrades.
@Post('/live/lobby') httpLobby() {
return { id: 'httpLobby' };
}
@Get('/plain') plain() {
return { id: 'plain' };
}
// `negotiate` routes are registered for both HTTP and WS.
@Stream('/feed') feed() {
return announce('feed', {});
}
// A user route on the reserved mesh path, on an app with no mesh: the two adapters disagree here.
@Ws('/__mesh__/control') reserved(@param() p: Record<string, string>) {
return announce('reserved', p);
}
}
@Module({ mountpoint: '/', controllers: [ParityWs] })
class ParityWsModule {}
// The `ws` client runs its URL through the URL parser, so on the Node rows dot segments are already
// resolved before the server sees them; `app.upgrade` receives each path exactly as written here.
const UPGRADE_PATHS = [
'/live/lobby',
'/live/kitchen',
'/live/lobby/',
'/live/x/../lobby',
'/live/a%20b',
'/live/lobby?x=1',
'/live/%E0%A4%A',
'/live//lobby',
'/plain',
'/nope',
'/feed',
'/__mesh__/control',
];
const HTTP_REQUESTS: Array<[string, string]> = [
['GET', '/live/lobby'],
['POST', '/live/lobby'],
['OPTIONS', '/live/lobby'],
['GET', '/live/kitchen'],
['GET', '/feed'],
];
interface UpgradeOutcome {
path: string;
sent: unknown[];
closes: Array<{ code?: number; reason?: string }>;
}
async function viaAppUpgrade(app: App, path: string): Promise<UpgradeOutcome> {
const inbound = channel<unknown>();
const ac = new AbortController();
const sent: unknown[] = [];
const closes: Array<{ code?: number; reason?: string }> = [];
let open = true;
const socket: WsSocket = {
inbound,
abort: ac.signal,
get isOpen() {
return open;
},
send: (data) => sent.push(JSON.parse(data)),
close: (code, reason) => {
open = false;
closes.push({ code, reason });
ac.abort();
},
terminate: () => {
open = false;
ac.abort();
},
};
const request: WsRequest = { url: path, headers: {}, protocol: 'http', ip: '127.0.0.1' };
await app.upgrade(request, socket);
return { path, sent, closes };
}
interface NodeUpgradeOutcome {
path: string;
status?: number;
sent: unknown[];
close?: { code: number; reason: string };
}
function viaNodeUpgrade(port: number, path: string): Promise<NodeUpgradeOutcome> {
return new Promise((resolve) => {
const sent: unknown[] = [];
const client = new WebSocket(`ws://127.0.0.1:${port}${path}`);
client.on('unexpected-response', (_req, res) => {
resolve({ path, status: res.statusCode, sent });
res.resume();
client.terminate();
});
client.on('message', (data) => sent.push(JSON.parse(String(data))));
client.on('close', (code, reason) => resolve({ path, status: 101, sent, close: { code, reason: String(reason) } }));
client.on('error', () => {
/* reported through unexpected-response or close */
});
});
}
describe('router parity: WebSocket upgrade outcomes', () => {
let app: App;
let nodeApp: App;
let port: number;
beforeAll(async () => {
app = createApp({ modules: [ParityWsModule] });
nodeApp = createApp({ modules: [ParityWsModule] });
const server = await nodeApp.listen(0);
port = (server.address() as AddressInfo).port;
});
afterAll(async () => {
await nodeApp.close();
});
it('app.upgrade outcomes equal the frozen capture', async () => {
const run = async () => {
const out: UpgradeOutcome[] = [];
for (const path of UPGRADE_PATHS) out.push(await viaAppUpgrade(app, path));
return out;
};
const expected = await frozen('ws-app-upgrade', run);
expectFrozenEqual(await run(), expected, 'app.upgrade');
});
it('Node upgrade listener outcomes equal the frozen capture', async () => {
const run = async () => {
const out: NodeUpgradeOutcome[] = [];
for (const path of UPGRADE_PATHS) out.push(await viaNodeUpgrade(port, path));
return out;
};
const expected = await frozen('ws-node-upgrade', run);
expectFrozenEqual(await run(), expected, 'node upgrade');
});
it('plain HTTP requests at the WS paths equal the frozen capture', async () => {
const run = async () => {
const out: Array<{ method: string; path: string; status: number; allow: string | null; body: string }> = [];
for (const [method, path] of HTTP_REQUESTS) {
const res = await app.fetch(new Request(`http://parity.test${path}`, { method }));
out.push({ method, path, status: res.status, allow: res.headers.get('allow'), body: await res.text() });
}
return out;
};
const expected = await frozen('ws-http-same-path', run);
expectFrozenEqual(await run(), expected, 'http at ws paths');
});
});