fix(server): trip ids parsed once at the gate for places and assignments; NUL-safe value quoting on the SQLite platform; places parity tests completed

This commit is contained in:
jubnl
2026-09-23 10:09:38 +02:00
parent 18a4784ebd
commit 4cb8432ade
10 changed files with 958 additions and 53 deletions
@@ -0,0 +1,88 @@
import Database from 'better-sqlite3';
import { describe, expect, it } from 'vitest';
import { NulSafeSqlitePlatform } from '../../../src/db/nul-safe-sqlite-platform';
/**
* Plan 3c Task 4 review M1 / program rule 22: MikroORM 7 inlines every bound
* value into the SQL text via `platform.quoteValue`/`escape` (see
* `NulSafeSqlitePlatform`'s own docstring for the full mechanism). These
* tests prove the override round-trips a NUL byte at every position SQLite
* itself will accept, by handing the platform's own generated SQL literal to
* a REAL `better-sqlite3` connection and reading the value back — not a
* string-shape assertion, an actual SQLite round-trip.
*/
describe('NulSafeSqlitePlatform', () => {
const platform = new NulSafeSqlitePlatform();
const db = new Database(':memory:');
function roundTrip(value: string): string {
const literal = platform.escape(value);
const row = db.prepare(`SELECT ${literal} as v`).get() as { v: string };
return row.v;
}
it('round-trips a string with no NUL unchanged (baseline: unaffected by the override)', () => {
expect(roundTrip('plain string')).toBe('plain string');
});
it('round-trips a NUL in the middle of a string', () => {
const value = 'nul\u0000name';
expect(roundTrip(value)).toBe(value);
});
it('round-trips a NUL at the very start of a string', () => {
const value = '\u0000leading';
expect(roundTrip(value)).toBe(value);
});
it('round-trips a NUL at the very end of a string', () => {
const value = 'trailing\u0000';
expect(roundTrip(value)).toBe(value);
});
it('round-trips a string made ENTIRELY of NUL bytes', () => {
expect(roundTrip('\u0000')).toBe('\u0000');
expect(roundTrip('\u0000\u0000\u0000')).toBe('\u0000\u0000\u0000');
});
it('round-trips a NUL next to a single quote (the string-literal escape character)', () => {
const value = "o'brien\u0000's café";
expect(roundTrip(value)).toBe(value);
});
it('round-trips a NUL next to a backslash (not special in a SQLite string literal)', () => {
const value = 'back\\slash\u0000end\\';
expect(roundTrip(value)).toBe(value);
});
it('round-trips multiple NULs mixed with quotes and backslashes', () => {
const value = "a'\u0000b\\\u0000c''\u0000\\\\d";
expect(roundTrip(value)).toBe(value);
});
it('fuzzes every 0x00-0x1F control character: none corrupt the round-trip and none throw', () => {
for (let code = 0x00; code <= 0x1f; code++) {
const ch = String.fromCharCode(code);
const value = `before${ch}after`;
expect(() => roundTrip(value)).not.toThrow();
expect(roundTrip(value)).toBe(value);
}
});
it('leaves non-string values to the base platform (numbers, null, dates, buffers untouched)', () => {
expect(platform.escape(42)).toBe('42');
expect(platform.escape(null)).toBe('null');
expect(platform.escape(true)).toBe('true');
});
it('formatQuery (the real call path AbstractSqlConnection.execute uses: formatQuery -> quoteValue -> escape) inlines a NUL-safe literal for a bound `?`, matched via a real table', () => {
db.exec('CREATE TABLE probe (name TEXT)');
const value = 'search\u0000term';
const insertSql = platform.formatQuery('insert into probe (name) values (?)', [value]);
db.exec(insertSql);
const selectSql = platform.formatQuery('select name from probe where name = ?', [value]);
const row = db.prepare(selectSql).get() as { name: string } | undefined;
expect(row?.name).toBe(value);
});
});
@@ -393,6 +393,17 @@ describe('PlacesRepository.findDuplicateByExternalId / findDuplicateByName / fin
expect(hit?.id).toBe(first.id);
});
// Task 4 review L2: only osm_id/google_ftid were exercised — a mutation
// dropping `amap_poi_id` from the `$or` survived every suite until this.
it('PLACEREPO-022b (PL25, L2): matches on amap_poi_id alone (no osm_id/google_ftid/google_place_id set)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Amap-only' });
testDb.prepare('UPDATE places SET amap_poi_id = ? WHERE id = ?').run('B0FFH00X01', place.id);
const hit = await places.findDuplicateByExternalId(String(trip.id), 'B0FFH00X01');
expect(hit?.id).toBe(place.id);
});
it('PLACEREPO-023 (PL26, rule 18 exception): lower(trim(name)) matches ASCII-folded names; a non-ASCII cased letter does NOT match — the documented legacy divergence, pinned not fixed', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -422,6 +433,19 @@ describe('PlacesRepository.findDuplicateByExternalId / findDuplicateByName / fin
const miss = await places.findDuplicateByCoords(String(trip.id), 48.9, 2.5, 0.0001);
expect(miss).toBeUndefined();
});
// Task 4 review L2: PLACEREPO-024's miss case differed on both lat AND
// lng, so `lng` alone was never proven — a mutation widening the lng
// tolerance (or dropping the lng check entirely) survived every suite
// until this.
it('PLACEREPO-024b (PL27, L2): a candidate within lat tolerance but OUTSIDE lng tolerance is a miss', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
createPlace(testDb, trip.id, { lat: 48.8566, lng: 2.3522 });
// lat matches exactly; lng is 0.01 off — 100x the tolerance below.
const miss = await places.findDuplicateByCoords(String(trip.id), 48.8566, 2.3622, 0.0001);
expect(miss).toBeUndefined();
});
});
describe('PlacesRepository.listForGpx (PL29) and existsByImageUrl (PI1)', () => {
@@ -455,6 +479,41 @@ describe('PlacesRepository.listForGpx (PL29) and existsByImageUrl (PI1)', () =>
});
});
/**
* PL24 (Task 5 review L6 ruling): `listDedupInputs` carries only the
* legacy projection — `PlacesService.buildDedupSet`'s JS folding (name
* lowercase+trim, coordinates only for unnamed rows, provider ids for every
* row) is untested here on purpose; it belongs to, and is already covered
* by, the service-level dedup tests.
*/
describe('PlacesRepository.listDedupInputs (PL24)', () => {
it('PLACEREPO-048: projects exactly the seven dedup columns, scoped to the trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const other = createTrip(testDb, user.id);
testDb.prepare(
`INSERT INTO places (trip_id, name, lat, lng, google_place_id, google_ftid, osm_id, amap_poi_id)
VALUES (?, 'Louvre', 48.86, 2.34, 'gp1', 'ft1', 'osm1', 'amap1')`,
).run(trip.id);
testDb.prepare(
`INSERT INTO places (trip_id, name, lat, lng) VALUES (?, '', 1.1, 2.2)`,
).run(trip.id);
createPlace(testDb, other.id, { name: 'Not this trip' });
const rows = await places.listDedupInputs(String(trip.id));
expect(rows).toEqual([
{ name: 'Louvre', lat: 48.86, lng: 2.34, google_place_id: 'gp1', google_ftid: 'ft1', osm_id: 'osm1', amap_poi_id: 'amap1' },
{ name: '', lat: 1.1, lng: 2.2, google_place_id: null, google_ftid: null, osm_id: null, amap_poi_id: null },
]);
});
it('PLACEREPO-049: an empty trip returns an empty array', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(await places.listDedupInputs(String(trip.id))).toEqual([]);
});
});
describe('PlacesRepository.listForTrip (PL3) — filter fragments', () => {
it('PLACEREPO-027: no filters — every trip place, DISTINCT, newest first', async () => {
const { user } = createUser(testDb);