From 3b1d30b5878f6ca9693d62e3f2078a2631b50ed0 Mon Sep 17 00:00:00 2001 From: Maurice Date: Thu, 8 Oct 2026 17:03:40 +0200 Subject: [PATCH] test(server): seed the places integration and trip members repository specs through the factories Converts the raw prepare() fixtures and reads in the places integration suite and the trip members repository spec to the ORM factories and readers. Tags, categories and linked expenses are seeded through makeTag, makeCategory and makeBudgetItem; the member rows the roster cases backdate or point at an inviter are written through insertRow and updateRows. The legacy UNION and join statements the roster reads are held to stay raw behind allow markers. --- server/tests/integration/places.test.ts | 98 +++++++++++-------- .../TripMembers.repository.test.ts | 81 ++++++++------- 2 files changed, 103 insertions(+), 76 deletions(-) diff --git a/server/tests/integration/places.test.ts b/server/tests/integration/places.test.ts index 1e9bf5a22..1d92b8fbc 100644 --- a/server/tests/integration/places.test.ts +++ b/server/tests/integration/places.test.ts @@ -29,16 +29,28 @@ vi.mock('../../src/config', () => ({ vi.mock('../../src/websocket', () => ({ broadcast: vi.fn(), broadcastToUser: vi.fn() })); import { db as testDb } from '../../src/db/database'; +import { MikroORM } from '@mikro-orm/core'; import { buildApp } from '../../src/bootstrap'; import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import type { FactoryOrm } from '../helpers/factories/context'; import { createUser, createAdmin, createTrip, createPlace, addTripMember } from '../helpers/factories'; import { authCookie } from '../helpers/auth'; +import { countRows, findRow, findRows, updateRows } from '../helpers/factories/rows'; +import { makeCategory, makeTag } from '../helpers/factories/places'; +import { makeBudgetItem } from '../helpers/factories/budget'; +import { setAppSetting } from '../helpers/factories/settings'; +import { Addons } from '../../src/db/entities/Addons.entity'; +import { BudgetItems } from '../../src/db/entities/BudgetItems.entity'; +import { Categories } from '../../src/db/entities/Categories.entity'; +import { PlaceRatings } from '../../src/db/entities/PlaceRatings.entity'; +import { Places } from '../../src/db/entities/Places.entity'; 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; +let orm: FactoryOrm; // Since the place DI fold the two outbound-I/O paths are stubbed as spies on the // container's PlacesService singleton (permissions precedent) instead of a path // mock of the deleted services/placeService. Bare spies keep the real @@ -54,6 +66,7 @@ const KMZ_FIXTURE = path.join(__dirname, '../fixtures/test.kmz'); beforeAll(async () => { nestApp = await buildApp(); app = nestApp.getHttpAdapter().getInstance(); + orm = nestApp.get(MikroORM); }); beforeEach(async () => { @@ -127,7 +140,8 @@ describe('Create place', () => { it('PLACE-016 — create place with category assigns it correctly', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); - const cat = testDb.prepare('SELECT id FROM categories LIMIT 1').get() as { id: number }; + const cat = await findRow(orm, Categories, {}); + if (!cat) throw new Error('the snapshot seeds no category'); const res = await request(app) .post(`/api/trips/${trip.id}/places`) @@ -185,7 +199,7 @@ describe('List places', () => { it('PLACE-017 — GET /api/trips/:tripId/places?category=X filters by category id', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); - const cats = testDb.prepare('SELECT id, name FROM categories LIMIT 2').all() as { id: number; name: string }[]; + const cats = (await findRows(orm, Categories)).slice(0, 2); expect(cats.length).toBeGreaterThanOrEqual(2); createPlace(testDb, trip.id, { name: 'Hotel Alpha', category_id: cats[0].id }); @@ -306,7 +320,7 @@ describe('Tags', () => { it('PLACE-013 — GET /api/tags returns user tags', async () => { const { user } = createUser(testDb); // Create a tag in DB - testDb.prepare('INSERT INTO tags (name, user_id) VALUES (?, ?)').run('Must-see', user.id); + await makeTag(orm, user.id, { name: 'Must-see' }); const res = await request(app) .get('/api/tags') @@ -321,8 +335,8 @@ describe('Tags', () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); // Pre-create a tag - const tagResult = testDb.prepare('INSERT INTO tags (name, user_id) VALUES (?, ?)').run('Romantic', user.id); - const tagId = tagResult.lastInsertRowid as number; + const tagResult = await makeTag(orm, user.id, { name: 'Romantic' }); + const tagId = tagResult.id; // The places API accepts `tags` as an array of tag IDs const res = await request(app) @@ -340,8 +354,8 @@ describe('Tags', () => { it('PLACE-012 — DELETE /api/tags/:id removes tag', async () => { const { user } = createUser(testDb); - const tagResult = testDb.prepare('INSERT INTO tags (name, user_id) VALUES (?, ?)').run('OldTag', user.id); - const tagId = tagResult.lastInsertRowid as number; + const tagResult = await makeTag(orm, user.id, { name: 'OldTag' }); + const tagId = tagResult.id; const res = await request(app) .delete(`/api/tags/${tagId}`) @@ -362,10 +376,10 @@ describe('Update place tags', () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); - const tag1Result = testDb.prepare('INSERT INTO tags (name, user_id) VALUES (?, ?)').run('OldTag', user.id); - const tag2Result = testDb.prepare('INSERT INTO tags (name, user_id) VALUES (?, ?)').run('NewTag', user.id); - const tag1Id = tag1Result.lastInsertRowid as number; - const tag2Id = tag2Result.lastInsertRowid as number; + const tag1Result = await makeTag(orm, user.id, { name: 'OldTag' }); + const tag2Result = await makeTag(orm, user.id, { name: 'NewTag' }); + const tag1Id = tag1Result.id; + const tag2Id = tag2Result.id; // Create place with tag1 const createRes = await request(app) @@ -390,8 +404,8 @@ describe('Update place tags', () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); - const tagResult = testDb.prepare('INSERT INTO tags (name, user_id) VALUES (?, ?)').run('RemovableTag', user.id); - const tagId = tagResult.lastInsertRowid as number; + const tagResult = await makeTag(orm, user.id, { name: 'RemovableTag' }); + const tagId = tagResult.id; const createRes = await request(app) .post(`/api/trips/${trip.id}/places`) @@ -462,8 +476,8 @@ describe('Search places', () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); - const tagResult = testDb.prepare('INSERT INTO tags (name, user_id) VALUES (?, ?)').run('Scenic', user.id); - const tagId = tagResult.lastInsertRowid as number; + const tagResult = await makeTag(orm, user.id, { name: 'Scenic' }); + const tagId = tagResult.id; // Create place with the tag and one without const createRes = await request(app) @@ -517,7 +531,7 @@ describe('Naver list import', () => { const trip = createTrip(testDb, user.id); const folderId = 'a04c3f7a8dd24d42a8eb52d710a700cc'; - testDb.prepare("UPDATE addons SET enabled = 1 WHERE id = 'naver_list_import'").run(); + await updateRows(orm, Addons, { id: 'naver_list_import' }, { enabled: true }); const fetchMock = vi.fn() .mockResolvedValueOnce({ @@ -569,7 +583,7 @@ describe('Naver list import', () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); - testDb.prepare("UPDATE addons SET enabled = 1 WHERE id = 'naver_list_import'").run(); + await updateRows(orm, Addons, { id: 'naver_list_import' }, { enabled: true }); const res = await request(app) .post(`/api/trips/${trip.id}/places/import/naver-list`) @@ -585,7 +599,7 @@ describe('Naver list import', () => { const trip = createTrip(testDb, user.id); const folderId = 'abc123'; - testDb.prepare("UPDATE addons SET enabled = 1 WHERE id = 'naver_list_import'").run(); + await updateRows(orm, Addons, { id: 'naver_list_import' }, { enabled: true }); const fetchMock = vi.fn() .mockResolvedValueOnce({ ok: false }); @@ -606,7 +620,7 @@ describe('Naver list import', () => { const trip = createTrip(testDb, user.id); const folderId = 'abc123'; - testDb.prepare("UPDATE addons SET enabled = 1 WHERE id = 'naver_list_import'").run(); + await updateRows(orm, Addons, { id: 'naver_list_import' }, { enabled: true }); const fetchMock = vi.fn().mockResolvedValueOnce({ ok: true, @@ -629,7 +643,7 @@ describe('Naver list import', () => { const trip = createTrip(testDb, user.id); const folderId = 'abc123'; - testDb.prepare("UPDATE addons SET enabled = 1 WHERE id = 'naver_list_import'").run(); + await updateRows(orm, Addons, { id: 'naver_list_import' }, { enabled: true }); const fetchMock = vi.fn().mockResolvedValueOnce({ ok: true, @@ -658,7 +672,7 @@ describe('Naver list import', () => { const trip = createTrip(testDb, user.id); const folderId = 'abc123'; - testDb.prepare("UPDATE addons SET enabled = 1 WHERE id = 'naver_list_import'").run(); + await updateRows(orm, Addons, { id: 'naver_list_import' }, { enabled: true }); const fetchMock = vi.fn().mockResolvedValueOnce({ ok: true, @@ -719,8 +733,7 @@ describe('KML/KMZ Import', () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); - testDb.prepare('INSERT INTO categories (name, color, icon, user_id) VALUES (?, ?, ?, ?)') - .run('Museums', '#3b82f6', 'Landmark', user.id); + await makeCategory(orm, { name: 'Museums', color: '#3b82f6', icon: 'Landmark', user: user.id }); const res = await request(app) .post(`/api/trips/${trip.id}/places/import/map`) @@ -745,8 +758,7 @@ describe('KML/KMZ Import', () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); - testDb.prepare('INSERT INTO categories (name, color, icon, user_id) VALUES (?, ?, ?, ?)') - .run('Parks', '#22c55e', 'Trees', user.id); + await makeCategory(orm, { name: 'Parks', color: '#22c55e', icon: 'Trees', user: user.id }); const res = await request(app) .post(`/api/trips/${trip.id}/places/import/map`) @@ -983,7 +995,7 @@ describe('Delete place — permission edge cases', () => { const place = createPlace(testDb, trip.id, { name: 'Restricted Place' }); // Restrict place edits to trip owner only - testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_place_edit', 'trip_owner')").run(); + await setAppSetting(orm, 'perm_place_edit', 'trip_owner'); await invalidatePermissionsCache(); const res = await request(app) @@ -1077,8 +1089,8 @@ describe('H1 — trip id parsed once at the gate (rule 21)', () => { it('PUT with tags by the hex-spelled trip id 404s "Trip not found" (Task 9 fix wave: `verifyTripAccess` now gates BEFORE the place-id read, so the string changed from "Place not found") 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 tagResult = await makeTag(orm, user.id, { name: 'Original' }); + const tagId = tagResult.id; const createRes = await request(app) .post(`/api/trips/${trip.id}/places`) .set('Cookie', authCookie(user.id)) @@ -1113,7 +1125,7 @@ describe('H1 — trip id parsed once at the gate (rule 21)', () => { .send({ rating: 4 }); expect(res.status).toBe(404); expect(res.body).toEqual({ error: 'Trip not found' }); - expect(testDb.prepare('SELECT COUNT(*) AS n FROM place_ratings WHERE place_id = ?').get(place.id)).toEqual({ n: 0 }); + expect(await countRows(orm, PlaceRatings, { place: place.id })).toBe(0); }); it('POST create by the hex-spelled trip id now 404s "Trip not found" (Task 9 fix wave, M1: `verifyTripAccess` gates with `toRowId` before `create()` is ever reached — the base 94c6efbbc 500 this test used to mirror was ruled a defect, not the contract to preserve, once the gate itself refuses the id)', async () => { @@ -1127,14 +1139,14 @@ describe('H1 — trip id parsed once at the gate (rule 21)', () => { .send({ name: 'Should not land' }); expect(res.status).toBe(404); expect(res.body).toEqual({ error: 'Trip not found' }); - expect(testDb.prepare('SELECT COUNT(*) AS n FROM places WHERE trip_id = ?').get(trip.id)).toEqual({ n: 0 }); + expect(await countRows(orm, Places, { trip: trip.id })).toBe(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); + await makeBudgetItem(orm, trip.id, { name: 'Tickets', total_price: 34, place: place.id }); const hexTripId = '0x' + trip.id.toString(16); vi.mocked(broadcast).mockClear(); @@ -1143,8 +1155,8 @@ describe('H1 — trip id parsed once at the gate (rule 21)', () => { .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(await findRow(orm, Places, { id: place.id })).toBeTruthy(); + expect(await findRow(orm, BudgetItems, { place: place.id })).toBeTruthy(); expect(broadcast).not.toHaveBeenCalled(); }); @@ -1152,7 +1164,7 @@ describe('H1 — trip id parsed once at the gate (rule 21)', () => { 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); + await makeBudgetItem(orm, trip.id, { name: 'Tickets', total_price: 34, place: place.id }); const hexTripId = '0x' + trip.id.toString(16); vi.mocked(broadcast).mockClear(); @@ -1162,7 +1174,7 @@ describe('H1 — trip id parsed once at the gate (rule 21)', () => { .send({ ids: [place.id] }); expect(res.status).toBe(404); expect(res.body).toEqual({ error: 'Trip not found' }); - expect(testDb.prepare('SELECT id FROM places WHERE id = ?').get(place.id)).toBeTruthy(); + expect(await findRow(orm, Places, { id: place.id })).toBeTruthy(); expect(broadcast).not.toHaveBeenCalled(); }); }); @@ -1206,7 +1218,7 @@ describe('A-H1 / A-M1 / B-H1 — verifyTripAccess parses once with toRowId (Task .send({ name: 'Should not land' }); expect(res.status).toBe(404); expect(res.body).toEqual({ error: 'Trip not found' }); - expect(testDb.prepare('SELECT COUNT(*) AS n FROM places WHERE trip_id = ?').get(trip.id)).toEqual({ n: 0 }); + expect(await countRows(orm, Places, { trip: trip.id })).toBe(0); }); }); @@ -1223,8 +1235,8 @@ describe('A-H1 / A-M1 / B-H1 — verifyTripAccess parses once with toRowId (Task .send({ name: 'Renamed' }); expect(res.status).toBe(404); expect(res.body).toEqual({ error: 'Trip not found' }); - const row = testDb.prepare('SELECT name FROM places WHERE id = ?').get(place.id) as { name: string }; - expect(row.name).toBe('Untouched'); + const row = await findRow(orm, Places, { id: place.id }); + expect(row?.name).toBe('Untouched'); }); }); @@ -1242,7 +1254,7 @@ describe('A-H1 / A-M1 / B-H1 — verifyTripAccess parses once with toRowId (Task .send({ ids: [place.id] }); expect(res.status).toBe(404); expect(res.body).toEqual({ error: 'Trip not found' }); - expect(testDb.prepare('SELECT id FROM places WHERE id = ?').get(place.id)).toBeTruthy(); + expect(await findRow(orm, Places, { id: place.id })).toBeTruthy(); expect(broadcast).not.toHaveBeenCalled(); }); }); @@ -1276,7 +1288,7 @@ describe('A-H1 / A-M1 / B-H1 — verifyTripAccess parses once with toRowId (Task .send({ rating: 4 }); expect(res.status).toBe(404); expect(res.body).toEqual({ error: 'Trip not found' }); - expect(testDb.prepare('SELECT COUNT(*) AS n FROM place_ratings WHERE place_id = ?').get(place.id)).toEqual({ n: 0 }); + expect(await countRows(orm, PlaceRatings, { place: place.id })).toBe(0); }); }); @@ -1306,7 +1318,7 @@ describe('A-H1 / A-M1 / B-H1 — verifyTripAccess parses once with toRowId (Task .attach('file', GPX_FIXTURE); expect(res.status).toBe(404); expect(res.body).toEqual({ error: 'Trip not found' }); - expect(testDb.prepare('SELECT COUNT(*) AS n FROM places WHERE trip_id = ?').get(trip.id)).toEqual({ n: 0 }); + expect(await countRows(orm, Places, { trip: trip.id })).toBe(0); }); }); }); @@ -1347,8 +1359,8 @@ describe('M1 — NUL-safe value quoting on the SQLite platform (rule 22)', () => 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); + const stored = await findRow(orm, Places, { id: res.body.place.id }); + 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 () => { diff --git a/server/tests/unit/db/repositories/TripMembers.repository.test.ts b/server/tests/unit/db/repositories/TripMembers.repository.test.ts index 7e985e314..e3b02f247 100644 --- a/server/tests/unit/db/repositories/TripMembers.repository.test.ts +++ b/server/tests/unit/db/repositories/TripMembers.repository.test.ts @@ -12,6 +12,8 @@ import { createTestOrm, type TestOrm } from '../../../helpers/test-orm'; import { addTripMember, createTrip, createUser } from '../../../helpers/factories'; import { TripMembers } from '../../../../src/db/entities/TripMembers.entity'; import type { TripMembersRepository } from '../../../../src/db/repositories/TripMembers.repository'; +import { Users } from '../../../../src/db/entities/Users.entity'; +import { countRows, findRow, findRows, insertRow, updateRows } from '../../../helpers/factories/rows'; const testDb = createSnapshotTestDb(); let t: TestOrm; @@ -24,8 +26,19 @@ beforeAll(async () => { beforeEach(() => { resetTestDb(testDb); t.clear(); }); afterAll(async () => { await t.close(); testDb.close(); }); +/** The membership row of the user on the trip, or null. */ +function memberRow(tripId: number, userId: number) { + return findRow(t, TripMembers, { trip: tripId, user: userId }); +} + +/** The three columns a membership is: trip, user and who invited them. */ +function membership(row: { trip_id?: number; user_id?: number; invited_by?: number | null } | null) { + return row ? { trip_id: row.trip_id, user_id: row.user_id, invited_by: row.invited_by } : row; +} + const legacyRoster = (tripId: number): Set => { const rows = testDb + // test-sql-allow: the raw statement is the legacy oracle this parity test holds the repository to. .prepare('SELECT user_id FROM trip_members WHERE trip_id = ? UNION SELECT user_id FROM trips WHERE id = ?') .all(tripId, tripId) as { user_id: number }[]; return new Set(rows.map((r) => r.user_id)); @@ -92,9 +105,9 @@ describe('TripMembersRepository — listUserIdsByTrip / exists / addMember (Plan const { user: second } = createUser(testDb); const trip = createTrip(testDb, owner.id); addTripMember(testDb, trip.id, first.id); - testDb.prepare('UPDATE trip_members SET added_at = ? WHERE trip_id = ? AND user_id = ?').run('2026-01-01 00:00:00', trip.id, first.id); + await updateRows(t, TripMembers, { trip: trip.id, user: first.id }, { added_at: '2026-01-01 00:00:00' }); addTripMember(testDb, trip.id, second.id); - testDb.prepare('UPDATE trip_members SET added_at = ? WHERE trip_id = ? AND user_id = ?').run('2026-02-01 00:00:00', trip.id, second.id); + await updateRows(t, TripMembers, { trip: trip.id, user: second.id }, { added_at: '2026-02-01 00:00:00' }); expect(await tripMembers.listUserIdsByTrip(trip.id)).toEqual([first.id, second.id]); }); @@ -125,13 +138,13 @@ describe('TripMembersRepository — listUserIdsByTrip / exists / addMember (Plan const trip = createTrip(testDb, owner.id); await tripMembers.addMember(trip.id, joiner.id, inviter.id); - const row = testDb.prepare('SELECT trip_id, user_id, invited_by FROM trip_members WHERE trip_id = ? AND user_id = ?').get(trip.id, joiner.id); + const row = membership(await memberRow(trip.id, joiner.id)); expect(row).toEqual({ trip_id: trip.id, user_id: joiner.id, invited_by: inviter.id }); const { user: joiner2 } = createUser(testDb); await tripMembers.addMember(trip.id, joiner2.id, null); - const row2 = testDb.prepare('SELECT invited_by FROM trip_members WHERE trip_id = ? AND user_id = ?').get(trip.id, joiner2.id); - expect(row2).toEqual({ invited_by: null }); + const row2 = await memberRow(trip.id, joiner2.id); + expect({ invited_by: row2?.invited_by }).toEqual({ invited_by: null }); }); }); @@ -142,6 +155,7 @@ describe('TripMembersRepository — listUserIdsByTrip / exists / addMember (Plan /** TM2's statement, run raw on the same rows — the parity oracle every assertion below is checked against. */ function legacyListWithUserAndInviter(tripId: number, ownerId: number): unknown { + // test-sql-allow: the raw statement is the legacy oracle this parity test holds the repository to. return testDb.prepare(` SELECT u.id, COALESCE(u.display_name, u.username) AS username, u.email, u.avatar, u.is_guest, CASE WHEN u.id = ? THEN 'owner' ELSE 'member' END as role, @@ -161,27 +175,27 @@ describe('TripMembersRepository.listWithUserAndInviter (TM2, Plan 3c Task 6)', ( const trip = createTrip(testDb, owner.id); const { user: inviterWithName } = createUser(testDb, { username: 'inviter-1' }); - testDb.prepare('UPDATE users SET display_name = ? WHERE id = ?').run('Inviter One', inviterWithName.id); + await updateRows(t, Users, { id: inviterWithName.id }, { display_name: 'Inviter One' }); const { user: inviterBare } = createUser(testDb, { username: 'inviter-2' }); const { user: memberWithDisplayName } = createUser(testDb, { username: 'member-1-handle' }); - testDb.prepare('UPDATE users SET display_name = ?, avatar = ? WHERE id = ?').run('Member One', 'm1.png', memberWithDisplayName.id); - testDb.prepare('INSERT INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, ?)').run(trip.id, memberWithDisplayName.id, inviterWithName.id); + await updateRows(t, Users, { id: memberWithDisplayName.id }, { display_name: 'Member One', avatar: 'm1.png' }); + await insertRow(t, TripMembers, { trip: trip.id, user: memberWithDisplayName.id, invitedByRef: inviterWithName.id }); const { user: memberBare } = createUser(testDb, { username: 'member-2-handle' }); - testDb.prepare('INSERT INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, ?)').run(trip.id, memberBare.id, inviterBare.id); + await insertRow(t, TripMembers, { trip: trip.id, user: memberBare.id, invitedByRef: inviterBare.id }); const { user: memberNoInviter } = createUser(testDb, { username: 'member-3-handle' }); - testDb.prepare('INSERT INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, NULL)').run(trip.id, memberNoInviter.id); + await insertRow(t, TripMembers, { trip: trip.id, user: memberNoInviter.id, invitedByRef: null }); const { user: guest } = createUser(testDb, { username: 'guest-handle' }); - testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(guest.id); - testDb.prepare('INSERT INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, ?)').run(trip.id, guest.id, owner.id); + await updateRows(t, Users, { id: guest.id }, { is_guest: 1 }); + await insertRow(t, TripMembers, { trip: trip.id, user: guest.id, invitedByRef: owner.id }); - testDb.prepare('UPDATE trip_members SET added_at = ? WHERE trip_id = ? AND user_id = ?').run('2026-01-01 00:00:00', trip.id, memberWithDisplayName.id); - testDb.prepare('UPDATE trip_members SET added_at = ? WHERE trip_id = ? AND user_id = ?').run('2026-01-02 00:00:00', trip.id, memberBare.id); - testDb.prepare('UPDATE trip_members SET added_at = ? WHERE trip_id = ? AND user_id = ?').run('2026-01-03 00:00:00', trip.id, memberNoInviter.id); - testDb.prepare('UPDATE trip_members SET added_at = ? WHERE trip_id = ? AND user_id = ?').run('2026-01-04 00:00:00', trip.id, guest.id); + await updateRows(t, TripMembers, { trip: trip.id, user: memberWithDisplayName.id }, { added_at: '2026-01-01 00:00:00' }); + await updateRows(t, TripMembers, { trip: trip.id, user: memberBare.id }, { added_at: '2026-01-02 00:00:00' }); + await updateRows(t, TripMembers, { trip: trip.id, user: memberNoInviter.id }, { added_at: '2026-01-03 00:00:00' }); + await updateRows(t, TripMembers, { trip: trip.id, user: guest.id }, { added_at: '2026-01-04 00:00:00' }); const rows = await tripMembers.listWithUserAndInviter(trip.id, owner.id); expect(rows).toEqual(legacyListWithUserAndInviter(trip.id, owner.id)); @@ -196,7 +210,7 @@ describe('TripMembersRepository.listWithUserAndInviter (TM2, Plan 3c Task 6)', ( it('TMEMREPO-012: the trip owner\'s own row (if ever a member) reports role "owner"', async () => { const { user: owner } = createUser(testDb); const trip = createTrip(testDb, owner.id); - testDb.prepare('INSERT INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, NULL)').run(trip.id, owner.id); + await insertRow(t, TripMembers, { trip: trip.id, user: owner.id, invitedByRef: null }); const rows = await tripMembers.listWithUserAndInviter(trip.id, owner.id); expect(rows).toEqual([expect.objectContaining({ id: owner.id, role: 'owner' })]); }); @@ -228,7 +242,7 @@ describe('TripMembersRepository.isGuestOfTrip (TM18, security-sensitive, Plan 3c it('TMEMREPO-015: true only for a guest who is actually a member of THIS trip', async () => { const { user: owner } = createUser(testDb); const { user: guest } = createUser(testDb, { username: 'guest-scoped' }); - testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(guest.id); + await updateRows(t, Users, { id: guest.id }, { is_guest: 1 }); const trip = createTrip(testDb, owner.id); addTripMember(testDb, trip.id, guest.id); @@ -247,7 +261,7 @@ describe('TripMembersRepository.isGuestOfTrip (TM18, security-sensitive, Plan 3c const { user: ownerA } = createUser(testDb); const { user: ownerB } = createUser(testDb); const { user: guest } = createUser(testDb, { username: 'guest-of-a' }); - testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(guest.id); + await updateRows(t, Users, { id: guest.id }, { is_guest: 1 }); const tripA = createTrip(testDb, ownerA.id); const tripB = createTrip(testDb, ownerB.id); addTripMember(testDb, tripA.id, guest.id); @@ -270,7 +284,7 @@ describe('TripMembersRepository.addIgnoringConflict / remove (TM8/TM14/TM15, sec const { user: newOwner } = createUser(testDb); const trip = createTrip(testDb, owner.id); await tripMembers.addIgnoringConflict(trip.id, former.id, newOwner.id); - const row = testDb.prepare('SELECT trip_id, user_id, invited_by FROM trip_members WHERE trip_id = ? AND user_id = ?').get(trip.id, former.id); + const row = membership(await memberRow(trip.id, former.id)); expect(row).toEqual({ trip_id: trip.id, user_id: former.id, invited_by: newOwner.id }); }); @@ -282,9 +296,9 @@ describe('TripMembersRepository.addIgnoringConflict / remove (TM8/TM14/TM15, sec // The existing row's invited_by (NULL, from addTripMember) must survive — // an upsert-merge would overwrite it, which is NOT what `INSERT OR IGNORE` does. await expect(tripMembers.addIgnoringConflict(trip.id, member.id, owner.id)).resolves.toBeUndefined(); - const row = testDb.prepare('SELECT invited_by FROM trip_members WHERE trip_id = ? AND user_id = ?').get(trip.id, member.id); - expect(row).toEqual({ invited_by: null }); - expect((testDb.prepare('SELECT COUNT(*) AS n FROM trip_members WHERE trip_id = ? AND user_id = ?').get(trip.id, member.id) as { n: number }).n).toBe(1); + const row = await memberRow(trip.id, member.id); + expect({ invited_by: row?.invited_by }).toEqual({ invited_by: null }); + expect(await countRows(t, TripMembers, { trip: trip.id, user: member.id })).toBe(1); }); it('TMEMREPO-021: remove deletes exactly the (trip, user) row, leaving other members untouched', async () => { @@ -296,8 +310,8 @@ describe('TripMembersRepository.addIgnoringConflict / remove (TM8/TM14/TM15, sec addTripMember(testDb, trip.id, memberB.id); await tripMembers.remove(trip.id, memberA.id); - expect(testDb.prepare('SELECT id FROM trip_members WHERE trip_id = ? AND user_id = ?').get(trip.id, memberA.id)).toBeUndefined(); - expect(testDb.prepare('SELECT id FROM trip_members WHERE trip_id = ? AND user_id = ?').get(trip.id, memberB.id)).toBeDefined(); + expect(await memberRow(trip.id, memberA.id)).toBeNull(); + expect(await memberRow(trip.id, memberB.id)).not.toBeNull(); }); it('TMEMREPO-022: remove on a missing row is a silent no-op', async () => { @@ -330,13 +344,13 @@ describe('NaN user_id — rule 15 (a non-numeric route id parses to NaN, must no addTripMember(testDb, trip.id, member.id); await expect(tripMembers.remove(trip.id, NaN)).resolves.toBeUndefined(); // The real member row survives — NaN must not accidentally match anything. - expect(testDb.prepare('SELECT id FROM trip_members WHERE trip_id = ? AND user_id = ?').get(trip.id, member.id)).toBeDefined(); + expect(await memberRow(trip.id, member.id)).not.toBeNull(); }); it('TMEMREPO-025: isGuestOfTrip(tripId, NaN) resolves false, matching the legacy 404 Guest not found', async () => { const { user: owner } = createUser(testDb); const { user: guest } = createUser(testDb); - testDb.prepare('UPDATE users SET is_guest = 1 WHERE id = ?').run(guest.id); + await updateRows(t, Users, { id: guest.id }, { is_guest: 1 }); const trip = createTrip(testDb, owner.id); addTripMember(testDb, trip.id, guest.id); await expect(tripMembers.isGuestOfTrip(trip.id, NaN)).resolves.toBe(false); @@ -349,12 +363,13 @@ describe('TripMembersRepository.listRawUsernameAndDisplayName (RP3)', () => { it('TMEMREPO-026: byte-identical to the legacy statement — raw username AND display_name, NOT TM2\'s COALESCE', async () => { const { user: owner } = createUser(testDb); const { user: named } = createUser(testDb, { username: 'bare-name' }); - testDb.prepare('UPDATE users SET display_name = ?, avatar = ? WHERE id = ?').run('Displayed', 'a.png', named.id); + await updateRows(t, Users, { id: named.id }, { display_name: 'Displayed', avatar: 'a.png' }); const { user: bare } = createUser(testDb, { username: 'no-display' }); const trip = createTrip(testDb, owner.id); addTripMember(testDb, trip.id, named.id); addTripMember(testDb, trip.id, bare.id); + // test-sql-allow: the raw statement is the legacy oracle this parity test holds the repository to. const legacy = testDb.prepare( 'SELECT u.id, u.username, u.display_name, u.avatar FROM trip_members tm JOIN users u ON u.id = tm.user_id WHERE tm.trip_id = ?', ).all(trip.id); @@ -388,15 +403,15 @@ describe('TripMembersRepository.clearInvitedBy (Plan 4 Task 1, UC4)', () => { const { user: other } = createUser(testDb, { username: 'other' }); const tripA = createTrip(testDb, owner.id); const tripB = createTrip(testDb, owner.id); - testDb.prepare('INSERT INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, ?)').run(tripA.id, other.id, departing.id); - testDb.prepare('INSERT INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, ?)').run(tripB.id, owner.id, departing.id); + await insertRow(t, TripMembers, { trip: tripA.id, user: other.id, invitedByRef: departing.id }); + await insertRow(t, TripMembers, { trip: tripB.id, user: owner.id, invitedByRef: departing.id }); // Control row: invited by someone else entirely — must survive untouched. const { user: controlMember } = createUser(testDb, { username: 'control-member' }); - testDb.prepare('INSERT INTO trip_members (trip_id, user_id, invited_by) VALUES (?, ?, ?)').run(tripA.id, controlMember.id, owner.id); + await insertRow(t, TripMembers, { trip: tripA.id, user: controlMember.id, invitedByRef: owner.id }); await tripMembers.clearInvitedBy(departing.id); - const rows = testDb.prepare('SELECT trip_id, user_id, invited_by FROM trip_members ORDER BY trip_id, user_id').all(); + const rows = (await findRows(t, TripMembers, {}, { trip: 'asc', user: 'asc' })).map(membership); expect(rows).toEqual([ { trip_id: tripA.id, user_id: other.id, invited_by: null }, { trip_id: tripA.id, user_id: controlMember.id, invited_by: owner.id }, @@ -411,6 +426,6 @@ describe('TripMembersRepository.clearInvitedBy (Plan 4 Task 1, UC4)', () => { addTripMember(testDb, trip.id, member.id); await expect(tripMembers.clearInvitedBy(999999)).resolves.toBeUndefined(); - expect((testDb.prepare('SELECT invited_by FROM trip_members WHERE trip_id = ? AND user_id = ?').get(trip.id, member.id) as { invited_by: number | null }).invited_by).toBeNull(); + expect((await memberRow(trip.id, member.id))?.invited_by).toBeNull(); }); });