The matcher comparison does not depend on the runtime, so it should run on every
runtime core supports. Only the Node suite ran it before.
The comparison and the app.fetch fixtures move into differential.ts and
app-fixture.ts, which use neither vitest nor a file API. Each runtime passes in
a loader for the frozen JSON. The vitest files use the same modules, plus a
Node check that the neutral runners report no mismatch, so the two paths cannot
drift.
- Deno and Bun: test/{deno,bun}/router-parity.test.ts run all 1158 frozen
matcher cases (hand-picked, owner, generated v1 and v2, WS) through the oracle
and the live router, plus the 55 app.fetch cases.
- workerd: test/edge/parity-worker.ts bundles the frozen JSON and runs the
same checks; test/edge/run.ts adds the phase after its existing assertions.
The Node-socket, WS-upgrade, boot and mesh captures stay Node-only: they need
http.request, the ws client, or a mesh peer on a port.
Refs #109
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
87 lines
3.0 KiB
TypeScript
87 lines
3.0 KiB
TypeScript
/**
|
|
* Application-level router parity: what a client sees through `app.fetch` and through a real Node
|
|
* socket — status, `Allow`, response headers, and HEAD body suppression. Matcher equality alone
|
|
* cannot prove these: the 404/405/OPTIONS decisions, the 400 for a bad path and the HEAD body rule
|
|
* live in `src/http/core.ts`, around the matcher.
|
|
*/
|
|
import http from 'http';
|
|
import type { AddressInfo } from 'net';
|
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
import { createApp } from '../../src/index';
|
|
import { readFileSync } from 'fs';
|
|
import { join } from 'path';
|
|
import {
|
|
ParityHttpModule,
|
|
REQUESTS,
|
|
runAppDifferential,
|
|
stableHeaders,
|
|
viaFetch,
|
|
type Observed,
|
|
type Req,
|
|
} from './app-fixture';
|
|
import type { App } from '../../src/app/types';
|
|
import { frozen, expectFrozenEqual } from './frozen';
|
|
|
|
function viaNode(port: number, [method, raw]: Req): Promise<Observed> {
|
|
// `http.request` sends the path bytes as given, so the server sees the raw target.
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request({ host: '127.0.0.1', port, method, path: raw, agent: false }, (res) => {
|
|
let body = '';
|
|
res.setEncoding('utf8');
|
|
res.on('data', (chunk) => (body += chunk));
|
|
res.on('end', () => {
|
|
const headers = Object.entries(res.headers).map(([k, v]): [string, string] => [
|
|
k,
|
|
Array.isArray(v) ? v.join(', ') : String(v),
|
|
]);
|
|
resolve({ method, raw, status: res.statusCode ?? 0, headers: stableHeaders(headers), body });
|
|
});
|
|
});
|
|
req.on('error', reject);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
describe('router parity: application-level HTTP outcomes', () => {
|
|
let fetchApp: App;
|
|
let nodeApp: App;
|
|
let port: number;
|
|
|
|
beforeAll(async () => {
|
|
fetchApp = createApp({ modules: [ParityHttpModule] });
|
|
nodeApp = createApp({ modules: [ParityHttpModule] });
|
|
const server = await nodeApp.listen(0);
|
|
port = (server.address() as AddressInfo).port;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await nodeApp.close();
|
|
});
|
|
|
|
it('app.fetch outcomes equal the frozen capture', async () => {
|
|
const run = async () => {
|
|
const out: Observed[] = [];
|
|
for (const req of REQUESTS) out.push(await viaFetch(fetchApp, req));
|
|
return out;
|
|
};
|
|
const expected = await frozen('app-fetch', run);
|
|
expectFrozenEqual(await run(), expected, 'app.fetch');
|
|
});
|
|
|
|
it('Node listener outcomes equal the frozen capture', async () => {
|
|
const run = async () => {
|
|
const out: Observed[] = [];
|
|
for (const req of REQUESTS) out.push(await viaNode(port, req));
|
|
return out;
|
|
};
|
|
const expected = await frozen('app-node', run);
|
|
expectFrozenEqual(await run(), expected, 'node listener');
|
|
});
|
|
|
|
it('the runtime-neutral app differential (the one Deno, Bun and workerd run) reports no mismatch', async () => {
|
|
const dir = join(__dirname, 'frozen');
|
|
const report = await runAppDifferential((name) => JSON.parse(readFileSync(join(dir, `${name}.json`), 'utf8')));
|
|
expect(report.mismatches).toEqual([]);
|
|
});
|
|
});
|