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
@@ -415,3 +415,27 @@ describe('Assignment participants', () => {
expect(update.body.assignment.place.end_time).toBe('16:00');
});
});
// ─────────────────────────────────────────────────────────────────────────────
// H1 (task-4-review.md, the trip-id half fixed here) — `dayExists`/
// `placeExists` gate on `toRowId(tripId)` now, not `Number(tripId)`: a
// hex-spelled trip id whose `Number()` value is a real, accessible trip must
// answer the legacy "Day not found", not reach that trip's real day/place
// (rule 21).
// ─────────────────────────────────────────────────────────────────────────────
describe('H1 — trip id parsed once at the gate (rule 21)', () => {
it('POST create-assignment by the hex-spelled trip id 404s "Day not found" (legacy: 404, not 201)', async () => {
const { user } = createUser(testDb);
const { trip, day, place } = setupAssignmentFixtures(user.id);
const hexTripId = '0x' + trip.id.toString(16);
const res = await request(app)
.post(`/api/trips/${hexTripId}/days/${day.id}/assignments`)
.set('Cookie', authCookie(user.id))
.send({ place_id: place.id });
expect(res.status).toBe(404);
expect(res.body).toEqual({ error: 'Day not found' });
expect(testDb.prepare('SELECT COUNT(*) AS n FROM day_assignments WHERE day_id = ?').get(day.id)).toEqual({ n: 0 });
});
});
+175
View File
@@ -35,6 +35,7 @@ import { createUser, createAdmin, createTrip, createPlace, addTripMember } from
import { authCookie } from '../helpers/auth';
import { PlacesService } from '../../src/nest/places/places.service';
import { invalidatePermissionsCache } from '../../src/nest/permissions/permissions-cache';
import { broadcast } from '../../src/websocket';
let nestApp: INestApplication;
let app: Application;
@@ -1049,3 +1050,177 @@ describe('Custom place image upload', () => {
expect(res.status).toBe(400);
});
});
// ─────────────────────────────────────────────────────────────────────────────
// H1 (task-4-review.md) — the trip id is parsed ONCE, at the gate, and that
// value is what every later call uses (rule 21). A hex-spelled trip id whose
// `Number()` value is a REAL, accessible trip must answer the same not-found
// every place/day/assignment id already does — not reach the real trip
// through a `Number(tripId)` gate while a raw-bound write behind it (or vice
// versa) misses.
// ─────────────────────────────────────────────────────────────────────────────
describe('H1 — trip id parsed once at the gate (rule 21)', () => {
it('GET by the trip\'s hex-spelled id 404s — it does not read the real trip\'s place', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Spot' });
const hexTripId = '0x' + trip.id.toString(16);
const res = await request(app)
.get(`/api/trips/${hexTripId}/places/${place.id}`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(404);
expect(res.body).toEqual({ error: 'Place not found' });
});
it('PUT with tags by the hex-spelled trip id 404s and leaves the tags untouched (H1 live: they used to be wiped)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const tagResult = testDb.prepare('INSERT INTO tags (name, user_id) VALUES (?, ?)').run('Original', user.id);
const tagId = tagResult.lastInsertRowid as number;
const createRes = await request(app)
.post(`/api/trips/${trip.id}/places`)
.set('Cookie', authCookie(user.id))
.send({ name: 'Taggable', tags: [tagId] });
expect(createRes.status).toBe(201);
const placeId = createRes.body.place.id;
const hexTripId = '0x' + trip.id.toString(16);
const res = await request(app)
.put(`/api/trips/${hexTripId}/places/${placeId}`)
.set('Cookie', authCookie(user.id))
.send({ name: 'Renamed', tags: [] });
expect(res.status).toBe(404);
expect(res.body).toEqual({ error: 'Place not found' });
const after = await request(app)
.get(`/api/trips/${trip.id}/places/${placeId}`)
.set('Cookie', authCookie(user.id));
expect(after.body.place.name).toBe('Taggable');
expect((after.body.place.tags as { id: number }[]).some((t) => t.id === tagId)).toBe(true);
});
it('PUT :id/rating by the hex-spelled trip id 404s', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Rated' });
const hexTripId = '0x' + trip.id.toString(16);
const res = await request(app)
.put(`/api/trips/${hexTripId}/places/${place.id}/rating`)
.set('Cookie', authCookie(user.id))
.send({ rating: 4 });
expect(res.status).toBe(404);
expect(res.body).toEqual({ error: 'Place not found' });
expect(testDb.prepare('SELECT COUNT(*) AS n FROM place_ratings WHERE place_id = ?').get(place.id)).toEqual({ n: 0 });
});
it('POST create by the hex-spelled trip id mirrors the legacy 500 (base 94c6efbbc: FK violation on the raw-bound trip id) — every OTHER route 404s, this is the one documented exception', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const hexTripId = '0x' + trip.id.toString(16);
const res = await request(app)
.post(`/api/trips/${hexTripId}/places`)
.set('Cookie', authCookie(user.id))
.send({ name: 'Should not land' });
expect(res.status).toBe(500);
expect(res.body).toEqual({ error: 'Internal server error' });
expect(testDb.prepare('SELECT COUNT(*) AS n FROM places WHERE trip_id = ?').get(trip.id)).toEqual({ n: 0 });
});
it('DELETE :id by the hex-spelled trip id 404s, deletes nothing and broadcasts nothing, even with a linked expense (#1298)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Louvre' });
testDb.prepare("INSERT INTO budget_items (trip_id, name, total_price, place_id) VALUES (?, 'Tickets', 34, ?)").run(trip.id, place.id);
const hexTripId = '0x' + trip.id.toString(16);
vi.mocked(broadcast).mockClear();
const res = await request(app)
.delete(`/api/trips/${hexTripId}/places/${place.id}`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(404);
expect(res.body).toEqual({ error: 'Place not found' });
expect(testDb.prepare('SELECT id FROM places WHERE id = ?').get(place.id)).toBeTruthy();
expect(testDb.prepare('SELECT id FROM budget_items WHERE place_id = ?').get(place.id)).toBeTruthy();
expect(broadcast).not.toHaveBeenCalled();
});
it('POST bulk-delete by the hex-spelled trip id deletes nothing (scopedIds already binds the trip id raw, so this was never divergent — kept as a regression alongside the single-delete case)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Louvre' });
testDb.prepare("INSERT INTO budget_items (trip_id, name, total_price, place_id) VALUES (?, 'Tickets', 34, ?)").run(trip.id, place.id);
const hexTripId = '0x' + trip.id.toString(16);
vi.mocked(broadcast).mockClear();
const res = await request(app)
.post(`/api/trips/${hexTripId}/places/bulk-delete`)
.set('Cookie', authCookie(user.id))
.send({ ids: [place.id] });
expect(res.status).toBe(200);
expect(res.body).toEqual({ deleted: [], count: 0 });
expect(testDb.prepare('SELECT id FROM places WHERE id = ?').get(place.id)).toBeTruthy();
expect(broadcast).not.toHaveBeenCalled();
});
});
// ─────────────────────────────────────────────────────────────────────────────
// M1 / program rule 22 (task-4-review.md) — a NUL byte in a user string no
// longer 500s: the platform override restores the legacy raw-bind's
// byte-for-byte round-trip (base 94c6efbbc: 200/201; before this fix, every
// ORM-converted statement 500'd on NUL).
// ─────────────────────────────────────────────────────────────────────────────
describe('M1 — NUL-safe value quoting on the SQLite platform (rule 22)', () => {
it('GET ?search=%00 is 200, not 500 (status parity — SQLite\'s LIKE pattern matcher itself iterates its RHS as NUL-terminated, an independent SQLite limitation this platform fix does not touch, so a NUL search matches everything on BOTH trees, not nothing)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
createPlace(testDb, trip.id, { name: 'Spot' });
// A raw, pre-encoded `%00` in the URL — the review's exact literal
// request shape (`GET /api/trips/:id/places?search=%00`). `.query({...})`
// goes through superagent's own `qs` encoder, which is not guaranteed to
// round-trip a NUL character the same way; the literal query string is
// what Express/the route actually receives in production.
const res = await request(app)
.get(`/api/trips/${trip.id}/places?search=%00`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
});
it('POST create with a NUL in the name is 201 and the name round-trips byte-for-byte', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const name = 'nul\u0000name';
const res = await request(app)
.post(`/api/trips/${trip.id}/places`)
.set('Cookie', authCookie(user.id))
.send({ name });
expect(res.status).toBe(201);
expect(res.body.place.name).toBe(name);
const stored = testDb.prepare('SELECT name FROM places WHERE id = ?').get(res.body.place.id) as { name: string };
expect(stored.name).toBe(name);
});
it('fuzzes every 0x01-0x1F control character in a created place name: none 500 and every one round-trips', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
// 0x00 is covered by its own dedicated test above; this sweeps the rest
// of the control-character range the review's fuzz asked for.
for (let code = 0x01; code <= 0x1f; code++) {
const name = `ctrl${String.fromCharCode(code)}char`;
const res = await request(app)
.post(`/api/trips/${trip.id}/places`)
.set('Cookie', authCookie(user.id))
.send({ name });
expect(res.status).toBe(201);
expect(res.body.place.name).toBe(name);
}
});
});
@@ -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);
+352 -7
View File
@@ -81,6 +81,22 @@ import { makeStorageFixture } from '../../helpers/storage-fixture';
import { createTestUnitOfWork, createTestAppSettingsRepo, createTestUsersRepo, createTestTagsRepo, createTestPlaceRatingsRepo, createTestAssignmentParticipantsRepo, createTestPlacesRepo, createTestTripMembersRepo, createTestDayAssignmentsRepo, createTestCategoriesRepo, sharedTestOrm } from '../../helpers/test-uow';
import type { AppSettingsRepository } from '../../../src/db/repositories/AppSettings.repository';
import type { UsersRepository } from '../../../src/db/repositories/Users.repository';
import { isUpdateConflict, type UpdateConflict } from '../../../src/nest/common/conflictResult';
/**
* Narrows `PlacesService.update`'s `PlaceWithTags | UpdateConflict | null`
* union to the successful-write case, for tests that only ever exercise the
* happy path and previously reached for `as any` to read a field off the
* result (L4/L1 — no new `any`).
*/
function asUpdatedPlace(
result: Awaited<ReturnType<PlacesService['update']>>,
): Exclude<Awaited<ReturnType<PlacesService['update']>>, null | UpdateConflict> {
if (result === null || isUpdateConflict(result)) {
throw new Error('expected a successful update, got null or a conflict');
}
return result;
}
// The default `maps` MapsService below never has its key-resolving methods
// exercised by this file's own cases (a real caller passes its own `maps`
@@ -412,6 +428,96 @@ describe('update', () => {
expect(created.route_geometry).toBe('[[48.0,2.0],[49.0,3.0]]');
expect(created.route_color).toBe('#7c3aed');
});
// ── PL11's three COALESCE columns (Task 4 review M2) ─────────────────────────
//
// `name`/`currency`/`transport_mode` bind `x || null` and fold with
// `?? existingPlace.x` — a falsy input (undefined, null OR '') keeps the old
// value, exactly like SQL's `COALESCE(?, col)` with a null bind. Only a
// truthy string writes. This is the opposite polarity of route_color/
// stop_type/fill_percent just above (an explicit null clears those). The
// review's mutation table: M3 (`name: name ?? existing`, so `''` would
// write instead of keeping) and M4/M4b (`transport_mode`/`currency` treated
// as `!== undefined ? x : existing`, so `null` would clear instead of
// keeping) all SURVIVED every test in this file before these three.
it('PLACE-SVC-013c (PL11, M3) — an empty-string name keeps the old name, it does not clear it', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Original' });
const updated = asUpdatedPlace(await svc.update(String(trip.id), String(place.id), { name: '' }));
expect(updated.name).toBe('Original');
});
it('PLACE-SVC-013d (PL11, M3) — an explicit null name keeps the old name', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Original' });
const updated = asUpdatedPlace(await svc.update(String(trip.id), String(place.id), { name: null as unknown as string }));
expect(updated.name).toBe('Original');
});
it('PLACE-SVC-013e (PL11) — a truthy name still overwrites (the COALESCE bind is not a one-way lock)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Original' });
const updated = asUpdatedPlace(await svc.update(String(trip.id), String(place.id), { name: 'Renamed' }));
expect(updated.name).toBe('Renamed');
});
it('PLACE-SVC-013f (PL11, M4b) — an explicit null currency keeps the old currency, it does not clear it', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Priced' });
await svc.update(String(trip.id), String(place.id), { currency: 'EUR' });
const updated = asUpdatedPlace(await svc.update(String(trip.id), String(place.id), { currency: null }));
expect(updated.currency).toBe('EUR');
});
it('PLACE-SVC-013g (PL11, M4b) — an empty-string currency keeps the old currency', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Priced' });
await svc.update(String(trip.id), String(place.id), { currency: 'EUR' });
const updated = asUpdatedPlace(await svc.update(String(trip.id), String(place.id), { currency: '' }));
expect(updated.currency).toBe('EUR');
});
it('PLACE-SVC-013h (PL11) — a truthy currency still overwrites', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Priced' });
await svc.update(String(trip.id), String(place.id), { currency: 'EUR' });
const updated = asUpdatedPlace(await svc.update(String(trip.id), String(place.id), { currency: 'USD' }));
expect(updated.currency).toBe('USD');
});
it('PLACE-SVC-013i (PL11, M4) — an explicit null transport_mode keeps the old mode, it does not clear it', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Moving' });
await svc.update(String(trip.id), String(place.id), { transport_mode: 'driving' });
const updated = asUpdatedPlace(await svc.update(String(trip.id), String(place.id), { transport_mode: null }));
expect(updated.transport_mode).toBe('driving');
});
it('PLACE-SVC-013j (PL11, M4) — an empty-string transport_mode keeps the old mode', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Moving' });
await svc.update(String(trip.id), String(place.id), { transport_mode: 'driving' });
const updated = asUpdatedPlace(await svc.update(String(trip.id), String(place.id), { transport_mode: '' }));
expect(updated.transport_mode).toBe('driving');
});
it('PLACE-SVC-013k (PL11) — a truthy transport_mode still overwrites', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Moving' });
await svc.update(String(trip.id), String(place.id), { transport_mode: 'driving' });
const updated = asUpdatedPlace(await svc.update(String(trip.id), String(place.id), { transport_mode: 'cycling' }));
expect(updated.transport_mode).toBe('cycling');
});
});
// ── updateMany ────────────────────────────────────────────────────────────────
@@ -660,6 +766,50 @@ describe('importGpx', () => {
expect(result.places[1].name).toBe('London');
});
// Task 5 review M2: the four importers' hand-written 25-field `insertPlace`
// partials had no test at the service level — only repository-level tests
// that already seed `duration_minutes: 60`/`transport_mode: 'walking'` in
// their own fixture, so they test `insertPlace` in general, not what THIS
// service actually passes it. Mutants that survived every suite until this
// test: GPX `transport_mode → 'driving'`, `duration_minutes: 30`.
it('PLACE-SVC-021b (M2) — the full stored row matches the legacy 7-column GPX shape, every other column null', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const gpx = Buffer.from(`<?xml version="1.0"?><gpx version="1.1">
<wpt lat="48.8566" lon="2.3522"><name>Paris</name></wpt>
</gpx>`);
const result = await svc.importGpx(String(trip.id), gpx) as { places: { id: number }[] };
const row = testDb.prepare('SELECT * FROM places WHERE id = ?').get(result.places[0].id) as Record<string, unknown>;
expect(row).toMatchObject({
trip_id: trip.id, name: 'Paris', description: null, lat: 48.8566, lng: 2.3522,
address: null, category_id: null, price: null, currency: null, place_time: null, end_time: null,
duration_minutes: 60, notes: null, image_url: null, google_place_id: null, google_ftid: null,
osm_id: null, amap_poi_id: null, website: null, phone: null, transport_mode: 'walking',
route_geometry: null, route_color: null, stop_type: null, fill_percent: null,
});
});
it('PLACE-SVC-021c (L2) — an import is atomic: a failure partway through the loop leaves nothing stored', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const placesRepo = await createTestPlacesRepo(dbs.connection);
const realInsertPlace = placesRepo.insertPlace.bind(placesRepo);
let calls = 0;
const insertSpy = vi.spyOn(placesRepo, 'insertPlace').mockImplementation(async (input) => {
calls++;
if (calls === 2) throw new Error('boom');
return realInsertPlace(input);
});
const gpx = Buffer.from(`<?xml version="1.0"?><gpx version="1.1">
<wpt lat="48.8566" lon="2.3522"><name>Paris</name></wpt>
<wpt lat="51.5074" lon="-0.1278"><name>London</name></wpt>
</gpx>`);
await expect(svc.importGpx(String(trip.id), gpx)).rejects.toThrow('boom');
const count = testDb.prepare('SELECT COUNT(*) AS n FROM places WHERE trip_id = ?').get(trip.id) as { n: number };
expect(count.n).toBe(0);
insertSpy.mockRestore();
});
it('PLACE-SVC-022 — imports <rte> as a single polyline-place with routeGeometry', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -821,6 +971,53 @@ describe('importGoogleList', () => {
expect(result.places[1].name).toBe('London');
});
// Task 5 review M2/L2. Mutants that survived every suite until these
// tests: Google `notes → null`, `duration_minutes: 30`, an insert loop
// outside `uow.transactional`.
it('PLACE-SVC-028f (M2) — the full stored row matches the legacy 7-column Google shape, notes included, every other column null', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const listPayload = [
[null, null, null, null, 'My Test List', null, null, null, [
[null, [null, null, null, null, null, [null, null, 51.5074, -0.1278]], 'London', 'Great city'],
]],
];
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, text: async () => 'prefix\n' + JSON.stringify(listPayload) }));
const result = await svc.importGoogleList(String(trip.id), 'https://www.google.com/maps/placelists/list/ABC123DEF456') as { places: { id: number }[] };
const row = testDb.prepare('SELECT * FROM places WHERE id = ?').get(result.places[0].id) as Record<string, unknown>;
expect(row).toMatchObject({
trip_id: trip.id, name: 'London', description: null, lat: 51.5074, lng: -0.1278,
address: null, category_id: null, price: null, currency: null, place_time: null, end_time: null,
duration_minutes: 60, notes: 'Great city', image_url: null, google_place_id: null,
osm_id: null, amap_poi_id: null, website: null, phone: null, transport_mode: 'walking',
route_geometry: null, route_color: null, stop_type: null, fill_percent: null,
});
});
it('PLACE-SVC-028g (L2) — a failure partway through the Google-list loop leaves nothing stored', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const listPayload = [
[null, null, null, null, 'My Test List', null, null, null, [
[null, [null, null, null, null, null, [null, null, 48.8566, 2.3522]], 'Paris', null],
[null, [null, null, null, null, null, [null, null, 51.5074, -0.1278]], 'London', 'Great city'],
]],
];
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, text: async () => 'prefix\n' + JSON.stringify(listPayload) }));
const placesRepo = await createTestPlacesRepo(dbs.connection);
const realInsertPlace = placesRepo.insertPlace.bind(placesRepo);
let calls = 0;
const insertSpy = vi.spyOn(placesRepo, 'insertPlace').mockImplementation(async (input) => {
calls++;
if (calls === 2) throw new Error('boom');
return realInsertPlace(input);
});
await expect(svc.importGoogleList(String(trip.id), 'https://www.google.com/maps/placelists/list/ABC123DEF456')).rejects.toThrow('boom');
const count = testDb.prepare('SELECT COUNT(*) AS n FROM places WHERE trip_id = ?').get(trip.id) as { n: number };
expect(count.n).toBe(0);
insertSpy.mockRestore();
});
it('PLACE-SVC-028b — stores a Google Maps ftid separately from google_place_id', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -997,9 +1194,12 @@ describe('searchImage', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id);
const result = await svc.searchImage(String(trip.id), `${place.id} `, user.id) as any;
expect(result.error).toBeDefined();
expect(result.status).toBe(404);
const result: Awaited<ReturnType<PlacesService['searchImage']>> = await svc.searchImage(String(trip.id), `${place.id} `, user.id);
expect('error' in result).toBe(true);
if ('error' in result) {
expect(result.error).toBeDefined();
expect(result.status).toBe(404);
}
});
it('PLACE-SVC-031 — searches Unsplash without a stored API key', async () => {
@@ -1116,6 +1316,51 @@ describe('importKmlPlaces deduplication', () => {
});
});
// Task 5 review M2/L2 — the same full-stored-row/atomicity gap for KML.
// Mutants that survived every suite until these tests: KML `route_geometry
// → null`, `duration_minutes: 30`, the GPX loop's `uow.transactional`
// removal class repeated here for KML's own loop.
describe('importKmlPlaces — full stored row and atomicity (M2/L2)', () => {
it('PLACE-SVC-036b (M2) — a LineString placemark stores route_geometry, duration_minutes: 60 and transport_mode: walking', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const kml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"><Document>
<Placemark><name>Ridge Path</name><LineString><coordinates>2.0,48.0,0 2.1,48.1,0</coordinates></LineString></Placemark>
</Document></kml>`);
const result = await svc.importKmlPlaces(String(trip.id), kml);
expect(result.count).toBe(1);
const row = testDb.prepare('SELECT * FROM places WHERE trip_id = ? AND name = ?').get(trip.id, 'Ridge Path') as Record<string, unknown>;
expect(row.route_geometry).toBeTruthy();
expect(JSON.parse(row.route_geometry as string)).toHaveLength(2);
expect(row.duration_minutes).toBe(60);
expect(row.transport_mode).toBe('walking');
expect(row.category_id).toBeNull();
});
it('PLACE-SVC-036c (L2) — a failure partway through the KML loop leaves nothing stored', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const placesRepo = await createTestPlacesRepo(dbs.connection);
const realInsertPlace = placesRepo.insertPlace.bind(placesRepo);
let calls = 0;
const insertSpy = vi.spyOn(placesRepo, 'insertPlace').mockImplementation(async (input) => {
calls++;
if (calls === 2) throw new Error('boom');
return realInsertPlace(input);
});
const kml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"><Document>
<Placemark><name>A</name><Point><coordinates>2.0,48.0,0</coordinates></Point></Placemark>
<Placemark><name>B</name><Point><coordinates>2.1,48.1,0</coordinates></Point></Placemark>
</Document></kml>`);
await expect(svc.importKmlPlaces(String(trip.id), kml)).rejects.toThrow('boom');
const count = testDb.prepare('SELECT COUNT(*) AS n FROM places WHERE trip_id = ?').get(trip.id) as { n: number };
expect(count.n).toBe(0);
insertSpy.mockRestore();
});
});
// ── Custom place image reclaim (#1136) ──────────────────────────────────────────
describe('custom place image reclaim', () => {
@@ -1228,6 +1473,12 @@ describe('custom place image reclaim', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Silent Vote' }) as { id: number };
// Task 4 review M2: a freshly-created row's `updated_at` can share the
// same one-second `datetime('now')` bucket as a bump this test means to
// catch, so `before === after` proved nothing either way (M1 — `upsertRating`
// also bumping `places.updated_at` — survived this test as written). Seed a
// visibly stale timestamp first so any bump is unmistakably a different value.
testDb.prepare("UPDATE places SET updated_at = datetime('now', '-1 hour') WHERE id = ?").run(place.id);
const before = (testDb.prepare('SELECT updated_at FROM places WHERE id = ?').get(place.id) as { updated_at: string | null }).updated_at;
await svc.rate(String(trip.id), String(place.id), user.id, 5);
@@ -1371,6 +1622,43 @@ describe('PlacesService — automatic track colours (#776)', () => {
expect(tracksOf(trip.id)).toEqual([]);
});
// Task 5 review M1: `PLACEREPO-039` proves the driver's single-connection
// serialisation for `t.em.transactional`, not that `colorizeImportedTracks`
// actually wraps its read+write in one transaction — removing
// `this.uow.transactional` from the service survived every suite until
// this test. Numbered 018 (not the review's suggested 008) to avoid
// colliding with the pre-existing PLACES-SVC-008 in the
// `findMatchingPlaceId` describe block below.
it('PLACES-SVC-018 (M1) — two concurrent colourings of the same trip never share a colour', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const ids = [1, 2].map((n) => (testDb.prepare(
"INSERT INTO places (trip_id, name, lat, lng, route_geometry) VALUES (?, ?, 1, 1, '[[1,1],[2,2]]') RETURNING id",
).get(trip.id, 'cc' + n) as { id: number }).id);
await Promise.all(ids.map((id) => (svc as unknown as { colorizeImportedTracks: (tripId: string, result: { places: { id: number; route_geometry: string; route_color: string | null }[] }) => Promise<void> }).colorizeImportedTracks(String(trip.id), {
places: [{ id, route_geometry: '[[1,1],[2,2]]', route_color: null }],
})));
expect(new Set(tracksOf(trip.id).map(t => t.route_color)).size).toBe(2);
});
// Task 5 review L1: the palette-wrap arm (`TRACK_COLORS[(i - free.length) %
// len]`) had no test — a mutation collapsing every wrapped index to
// `TRACK_COLORS[0]` survived every suite until this test.
it('PLACES-SVC-019 (L1) — more tracks than free colours wrap from the palette start', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const places = Array.from({ length: TRACK_COLORS.length + 2 }, (_, i) => ({
id: (testDb.prepare(
"INSERT INTO places (trip_id, name, lat, lng, route_geometry) VALUES (?, ?, 1, 1, '[[1,1],[2,2]]') RETURNING id",
).get(trip.id, 'w' + i) as { id: number }).id,
route_geometry: '[[1,1],[2,2]]',
route_color: null as string | null,
}));
await (svc as unknown as { colorizeImportedTracks: (tripId: string, result: { places: typeof places }) => Promise<void> }).colorizeImportedTracks(String(trip.id), { places });
const stored = tracksOf(trip.id).map(t => t.route_color);
expect(stored).toEqual([...TRACK_COLORS, TRACK_COLORS[0], TRACK_COLORS[1]]);
});
});
// ── Import enrichment (#886) ──────────────────────────────────────────────────
@@ -1424,7 +1712,7 @@ describe('enrichImportedPlaces', () => {
it('PLACE-SVC-060b (PL45) — broadcasts place:updated with socketId: undefined, no exclusion (the importer\'s own client gets the late update)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Bar', lat: 48.85, lng: 2.35 }) as any;
const place = createPlace(testDb, trip.id, { name: 'Bar', lat: 48.85, lng: 2.35 });
const broadcastSpy = vi.spyOn(RealtimeService.prototype, 'broadcast');
const svcWithMaps = await enrichSvc({
@@ -1439,7 +1727,10 @@ describe('enrichImportedPlaces', () => {
const [tripArg, event, payload, socketId] = broadcastSpy.mock.calls[0];
expect(tripArg).toBe(String(trip.id));
expect(event).toBe('place:updated');
expect((payload as { place: { id: number } }).place.id).toBe(place.id);
// Task 5 review L7: the payload carries the just-written value, not a
// stale pre-enrichment snapshot (the qb read bypasses the identity map).
expect((payload as { place: { id: number; google_place_id: string | null } }).place.id).toBe(place.id);
expect((payload as { place: { google_place_id: string | null } }).place.google_place_id).toBe('ChIJ1');
expect(socketId).toBeUndefined();
broadcastSpy.mockRestore();
});
@@ -1714,6 +2005,57 @@ describe('importNaverList provider payload', () => {
expect(result.places).toHaveLength(1);
expect(result.places[0].name).toBe('Gyeongbokgung');
});
// Task 5 review M2/L2. Mutants that survived every suite until these
// tests: `duration_minutes: 30` (all four importers), an insert loop
// outside `uow.transactional`.
it('PLACE-SVC-077b (M2) — the full stored row matches the legacy 7-column Naver shape, address+notes included, every other column null', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: true,
text: async () => JSON.stringify({
folder: { name: 'Seoul', bookmarkCount: 1 },
bookmarkList: [{ name: 'Gyeongbokgung', px: 126.977, py: 37.5796, memo: 'A palace', address: 'Sejongno' }],
}),
}));
const result = await svc.importNaverList(String(trip.id), FOLDER_URL) as { places: { id: number }[] };
const row = testDb.prepare('SELECT * FROM places WHERE id = ?').get(result.places[0].id) as Record<string, unknown>;
expect(row).toMatchObject({
trip_id: trip.id, name: 'Gyeongbokgung', description: null, lat: 37.5796, lng: 126.977,
address: 'Sejongno', category_id: null, price: null, currency: null, place_time: null, end_time: null,
duration_minutes: 60, notes: 'A palace', image_url: null, google_place_id: null, google_ftid: null,
osm_id: null, amap_poi_id: null, website: null, phone: null, transport_mode: 'walking',
route_geometry: null, route_color: null, stop_type: null, fill_percent: null,
});
});
it('PLACE-SVC-077c (L2) — a failure partway through the Naver-list loop leaves nothing stored', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: true,
text: async () => JSON.stringify({
folder: { name: 'Seoul', bookmarkCount: 2 },
bookmarkList: [
{ name: 'Gyeongbokgung', px: 126.977, py: 37.5796, memo: null, address: 'Sejongno' },
{ name: 'Namsan Tower', px: 126.988, py: 37.5512, memo: null, address: 'Yongsan' },
],
}),
}));
const placesRepo = await createTestPlacesRepo(dbs.connection);
const realInsertPlace = placesRepo.insertPlace.bind(placesRepo);
let calls = 0;
const insertSpy = vi.spyOn(placesRepo, 'insertPlace').mockImplementation(async (input) => {
calls++;
if (calls === 2) throw new Error('boom');
return realInsertPlace(input);
});
await expect(svc.importNaverList(String(trip.id), FOLDER_URL)).rejects.toThrow('boom');
const count = testDb.prepare('SELECT COUNT(*) AS n FROM places WHERE trip_id = ?').get(trip.id) as { n: number };
expect(count.n).toBe(0);
insertSpy.mockRestore();
});
});
// ── Free address backfill for list imports (#1954) ───────────────────────────
@@ -1745,7 +2087,7 @@ describe('backfillMissingAddresses', () => {
it('PLACE-SVC-078b (PL47) — broadcasts place:updated with socketId: undefined, no exclusion', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Bar', lat: 48.85, lng: 2.35 }) as any;
const place = createPlace(testDb, trip.id, { name: 'Bar', lat: 48.85, lng: 2.35 });
const broadcastSpy = vi.spyOn(RealtimeService.prototype, 'broadcast');
const reverseGeocode = vi.fn(async () => ({ name: null, address: '1 Rue de Rivoli, Paris' }));
@@ -1757,7 +2099,10 @@ describe('backfillMissingAddresses', () => {
const [tripArg, event, payload, socketId] = broadcastSpy.mock.calls[0];
expect(tripArg).toBe(String(trip.id));
expect(event).toBe('place:updated');
expect((payload as { place: { id: number } }).place.id).toBe(place.id);
// Task 5 review L7: the payload carries the just-filled address, not a
// stale pre-backfill snapshot (the qb read bypasses the identity map).
expect((payload as { place: { id: number; address: string | null } }).place.id).toBe(place.id);
expect((payload as { place: { address: string | null } }).place.address).toBe('1 Rue de Rivoli, Paris');
expect(socketId).toBeUndefined();
broadcastSpy.mockRestore();
});