Files
TREK/server/tests/unit/nest/common/row-id.test.ts
T
Maurice 7f6f2eb97c fix(db): read every request row id through toRowId, at the trip guards and the repository lookups
toRowId moves to src/db/row-id.ts so repositories can use it (src/db sits below src/nest), together with a new withRowId helper; src/nest/common/row-id.ts re-exports both. Its docstring is rewritten to state the rule plainly. TripsRepository read its number | string trip ids raw: SQLite matched '0x1' against nothing, Postgres 14 and 15 refused the cast with a 500, and 16 and later read it as trip 1. Its access reads, DaysRepository.existsInTrip and the packing template lookups now parse the id first, so an id toRowId refuses is no row on every engine. TripAccessGuard, TripOwnerGuard and JourneyAssistantGuard used Number(), which read '0x1' and '1e0' as row 1; they now use toRowId.
2026-11-18 13:20:05 +01:00

93 lines
4.1 KiB
TypeScript

import { toRowId, uriRowId, withRowId } from '../../../../src/nest/common/row-id';
import { describe, it, expect } from 'vitest';
describe('toRowId', () => {
it('ROWID-001: rejects a non-numeric string', () => {
expect(toRowId('abc')).toBeNull();
});
it('ROWID-002: rejects a hex-shaped string — Number() would accept it, SQLite affinity never would (verified against better-sqlite3)', () => {
expect(toRowId('0x10')).toBeNull();
});
it("ROWID-003: rejects an exponent-shaped string (\"1e3\") — a DELIBERATE, ACCEPTED deviation: SQLite's own numeric-affinity comparison DOES match this against integer 1000 (verified against better-sqlite3, not assumed — the row-id.ts docstring previously claimed the opposite), but no id this guard ever sees originates that way (every id is a canonical decimal string from our own client), so this fails closed to the route's legacy not-found result instead of widening the guard to match SQLite's affinity exactly", () => {
expect(toRowId('1e3')).toBeNull();
});
it('ROWID-004: rejects a negative sign', () => {
expect(toRowId('-1')).toBeNull();
});
it('ROWID-005: rejects a leading space (" 1") — same deliberate deviation as ROWID-003: SQLite\'s affinity conversion ignores leading whitespace and matches integer 1 (verified against better-sqlite3)', () => {
expect(toRowId(' 1')).toBeNull();
});
it('ROWID-005B: rejects a trailing space ("1 ") — same deliberate deviation: SQLite\'s affinity conversion ignores trailing whitespace too and matches integer 1 (verified against better-sqlite3)', () => {
expect(toRowId('1 ')).toBeNull();
});
it('ROWID-006: rejects a decimal point ("1.0") — same deliberate deviation: SQLite\'s affinity conversion parses this as REAL 1.0, which compares equal to INTEGER 1 (verified against better-sqlite3)', () => {
expect(toRowId('1.0')).toBeNull();
});
it('ROWID-006B: rejects a leading plus sign ("+1") — same deliberate deviation: SQLite\'s affinity conversion accepts a leading sign and matches integer 1 (verified against better-sqlite3)', () => {
expect(toRowId('+1')).toBeNull();
});
it('ROWID-007: rejects an empty string', () => {
expect(toRowId('')).toBeNull();
});
it('ROWID-008: rejects undefined', () => {
expect(toRowId(undefined)).toBeNull();
});
it('ROWID-009: accepts a plain digit string', () => {
expect(toRowId('42')).toBe(42);
});
it('ROWID-009B: accepts a leading-zero digit string ("007") — SQLite\'s own affinity conversion resolves this to integer 7 too (verified against better-sqlite3), and the guard\'s regex (/^\\d+$/) must keep matching it: never tighten it to /^(0|[1-9]\\d*)$/', () => {
expect(toRowId('007')).toBe(7);
});
it('ROWID-010: accepts an already-numeric safe integer', () => {
expect(toRowId(42)).toBe(42);
});
it('ROWID-011: rejects a number one past MAX_SAFE_INTEGER (2**53)', () => {
expect(toRowId(2 ** 53)).toBeNull();
});
it("ROWID-012: rejects a digits-only STRING one past MAX_SAFE_INTEGER (coverage: the string branch's own safe-integer check)", () => {
expect(toRowId('9007199254740993')).toBeNull();
});
});
describe('withRowId', () => {
it('ROWID-013: reads with the parsed id', async () => {
expect(await withRowId('42', async (id) => id + 1)).toBe(43);
});
it('ROWID-014: answers undefined without reading for a spelling toRowId refuses', async () => {
let read = false;
const spellings = ['0x2a', '42e0', '42.0', '+42', '4_2', ' 42', 'abc', '99999999999999999999'];
const answers = await Promise.all(spellings.map((spelling) => withRowId(spelling, async () => ((read = true), 1))));
expect(answers).toEqual(spellings.map(() => undefined));
expect(read).toBe(false);
});
});
describe('uriRowId', () => {
it('ROWID-015: reads the first value of an MCP URI variable as a positive id', () => {
expect(uriRowId('7')).toBe(7);
expect(uriRowId(['8', '9'])).toBe(8);
});
it('ROWID-016: refuses zero and every spelling toRowId refuses, Number() read them as ids', () => {
for (const spelling of ['0', '0x7', '7e0', '7.0', '+7', '1e20', 'not-a-number']) {
expect(uriRowId(spelling)).toBeNull();
}
});
});