mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
cc0fe6ce2b
ROUTEID walks every route the app registers. - ROUTEID-001 sends 'abc' and the non-canonical spellings of a seeded row's id to each path param named like an id, and requires no 5xx and the answer 'abc' gets. It then sends the spellings of an id no row has and records what the database drivers are handed (tests/helpers/sql-capture.ts, below MikroORM and Kysely). It fails when a spelling, or the number Number() makes of it, reaches SQL, so a route over a table the suite seeds nothing in is held too. Stale NOT_ROW_IDS entries fail, and the walk runs on one listening keep-alive server. - ROUTEID-002 walks every @trek/shared request, body and query schema and every route's body and query DTO, and fails on a field named like a row id that accepts such a spelling. String ids are listed with their reason; open fields name the handler test that holds them. Three more suites cover the other entry points: - MCPROWID does the same for every MCP resource template. - RPCROWID refuses a plugin RPC id param read past rowIdParam. - BODYID, JRNID and QUERYID cover the open accommodation update body, the collab reply, the journey fields a handler parses itself and the notifications limit and offset. All suites that boot the app run in the SQLite suite and in the Postgres lane on 14 and 18. server/CLAUDE.md states the rule and the four gates.
117 lines
4.2 KiB
TypeScript
117 lines
4.2 KiB
TypeScript
/**
|
|
* Records what the database drivers are handed while a recording runs: each
|
|
* statement's SQL text and its bound values, on SQLite (better-sqlite3) and on
|
|
* Postgres (pg), below MikroORM and Kysely, so every statement the app sends
|
|
* is seen whichever API built it.
|
|
*
|
|
* For suites that must prove a value never reached SQL (ROUTEID, in
|
|
* route-row-ids.e2e.test.ts): a route that parsed '0x3ade68b1' with Number()
|
|
* binds 987654321 even when no row has that id, which no answer would show.
|
|
*/
|
|
import Database from 'better-sqlite3';
|
|
import { createRequire } from 'node:module';
|
|
import path from 'node:path';
|
|
|
|
interface SentStatement {
|
|
sql: string;
|
|
values: unknown[];
|
|
}
|
|
|
|
type Method = (this: unknown, ...args: unknown[]) => unknown;
|
|
type Patchable = Record<string, Method>;
|
|
|
|
let recording: SentStatement[] | null = null;
|
|
const restores: Array<() => void> = [];
|
|
|
|
function flatten(args: unknown[]): unknown[] {
|
|
return args.flatMap((arg) => {
|
|
if (Array.isArray(arg)) return arg as unknown[];
|
|
if (arg !== null && typeof arg === 'object') return Object.values(arg);
|
|
return [arg];
|
|
});
|
|
}
|
|
|
|
function patch(target: Patchable, name: string, record: (self: unknown, args: unknown[]) => void): void {
|
|
const original = target[name];
|
|
if (typeof original !== 'function') return;
|
|
target[name] = function (this: unknown, ...args: unknown[]) {
|
|
if (recording) record(this, args);
|
|
return original.apply(this, args);
|
|
};
|
|
restores.push(() => {
|
|
target[name] = original;
|
|
});
|
|
}
|
|
|
|
/** better-sqlite3's Statement prototype: every statement of every handle runs through its methods. */
|
|
function patchSqlite(): void {
|
|
const scratch = new Database(':memory:');
|
|
const prepare = (Database.prototype as unknown as Patchable)['prepare'];
|
|
const statementProto = Object.getPrototypeOf(prepare.call(scratch, 'SELECT 1')) as Patchable;
|
|
scratch.close();
|
|
for (const method of ['run', 'get', 'all', 'iterate']) {
|
|
patch(statementProto, method, (self, args) => {
|
|
recording?.push({ sql: String((self as { source?: unknown }).source ?? ''), values: flatten(args) });
|
|
});
|
|
}
|
|
}
|
|
|
|
/** pg's Client.prototype.query, which the pool's clients run every statement through. */
|
|
function patchPostgres(): void {
|
|
// pg arrives with @mikro-orm/postgresql rather than as a dependency of the server's own.
|
|
const fromServer = createRequire(path.join(process.cwd(), 'package.json'));
|
|
const fromDriver = createRequire(fromServer.resolve('@mikro-orm/postgresql'));
|
|
const pg = fromDriver('pg') as { Client: { prototype: Patchable } };
|
|
patch(pg.Client.prototype, 'query', (_self, args) => {
|
|
const [config, values] = args;
|
|
if (typeof config === 'string') {
|
|
recording?.push({ sql: config, values: Array.isArray(values) ? values : [] });
|
|
} else if (config !== null && typeof config === 'object') {
|
|
const query = config as { text?: unknown; values?: unknown };
|
|
recording?.push({
|
|
sql: String(query.text ?? ''),
|
|
values: Array.isArray(query.values) ? query.values : Array.isArray(values) ? values : [],
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
/** Wraps both drivers once per test file; `uninstallSqlCapture` puts them back. */
|
|
export function installSqlCapture(): void {
|
|
if (restores.length > 0) return;
|
|
patchSqlite();
|
|
patchPostgres();
|
|
}
|
|
|
|
export function uninstallSqlCapture(): void {
|
|
for (const restore of restores.splice(0).reverse()) restore();
|
|
recording = null;
|
|
}
|
|
|
|
/** The statements the drivers were handed while `run` ran. */
|
|
export async function recordSql(run: () => Promise<unknown>): Promise<SentStatement[]> {
|
|
const sent: SentStatement[] = [];
|
|
recording = sent;
|
|
try {
|
|
await run();
|
|
} finally {
|
|
recording = null;
|
|
}
|
|
return sent;
|
|
}
|
|
|
|
/**
|
|
* The first statement that carried `spelling` to the database, as a bound value
|
|
* or quoted in its text, or the number `id` that Number() or parseInt() makes
|
|
* of it; undefined when none did.
|
|
*/
|
|
export function carriedTo(sent: SentStatement[], spelling: string, id: number): string | undefined {
|
|
const bare = new RegExp(`(^|[^0-9])${id}([^0-9]|$)`);
|
|
return sent.find(
|
|
({ sql, values }) =>
|
|
values.some((v) => v === spelling || v === id || v === String(id)) ||
|
|
sql.includes(`'${spelling}'`) ||
|
|
bare.test(sql),
|
|
)?.sql;
|
|
}
|