mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
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.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { toRowId } from '../../../../src/nest/common/row-id';
|
||||
import { toRowId, uriRowId, withRowId } from '../../../../src/nest/common/row-id';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
@@ -63,3 +63,30 @@ describe('toRowId', () => {
|
||||
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();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user