/** * `DayAssignmentsRepository` (Plan 3c Task 2) β€” the DY1/DY3/AS1/AS3 * assignment-with-place projection contract (built here, Task 3 consumes it * unchanged for AS1/AS3). */ import { DayAccommodations } from '../../../../src/db/entities/DayAccommodations.entity'; import { DayAssignments } from '../../../../src/db/entities/DayAssignments.entity'; import { Places } from '../../../../src/db/entities/Places.entity'; import type { DayAssignmentsRepository } from '../../../../src/db/repositories/DayAssignments.repository'; import { withRequestContext } from '../../../../src/nest/database/request-context'; import { UnitOfWork } from '../../../../src/nest/database/unit-of-work'; import { createSnapshotTestDb } from '../../../helpers/db-mock'; import { createCategory, createDay, createDayAccommodation, createDayAssignment, createPlace, createTrip, createUser, } from '../../../helpers/factories'; import type { DayAssignmentRow } from '../../../helpers/factories/itinerary'; import { findRow, findRows, insertRow, updateRows } from '../../../helpers/factories/rows'; import { resetTestDb } from '../../../helpers/test-db'; import { createTestOrm, type TestOrm } from '../../../helpers/test-orm'; import { createTour } from '../../../helpers/tours-repos'; import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; const testDb = createSnapshotTestDb(); let t: TestOrm; let assignments: DayAssignmentsRepository; let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); assignments = t.repo(DayAssignments); uow = new UnitOfWork(t.em); }); beforeEach(() => { resetTestDb(testDb); t.clear(); }); afterAll(async () => { await t.close(); testDb.close(); }); /** The named columns of the assignment, or undefined once it is gone: what a `SELECT FROM day_assignments` read back. */ async function assignmentCols( id: number, ...cols: K[] ): Promise | undefined> { const row = await findRow(t, DayAssignments, { id }); if (!row) return undefined; return Object.fromEntries(cols.map((c) => [c, row[c]])) as Pick; } /** The assignment's order_index. */ async function orderOf(id: number) { return (await findRow(t, DayAssignments, { id }))!.order_index; } /** The DY1/DY3/AS1/AS3 statement, run raw β€” the parity oracle every assertion below is checked against. */ function legacyProjectionRow(id: number): unknown { // test-sql-allow: the legacy DY1/DY3/AS1/AS3 statement is the parity oracle the repository is compared against. return testDb .prepare( ` SELECT da.*, p.id as place_id, p.name as place_name, p.description as place_description, p.lat, p.lng, p.address, p.category_id, p.price, p.currency as place_currency, COALESCE(da.assignment_time, p.place_time) as place_time, COALESCE(da.assignment_end_time, p.end_time) as end_time, p.duration_minutes, p.notes as place_notes, p.image_url, p.transport_mode, p.google_place_id, p.google_ftid, p.osm_id, p.amap_poi_id, p.website, p.phone, p.stop_type, p.fill_percent, t.place_id AS tour_place_id, CASE WHEN t.place_id IS NOT NULL THEN p.route_geometry END AS tour_route_geometry, c.name as category_name, c.color as category_color, c.icon as category_icon FROM day_assignments da JOIN places p ON da.place_id = p.id LEFT JOIN tours t ON t.place_id = p.id LEFT JOIN categories c ON p.category_id = c.id WHERE da.id = ? `, ) .get(id); } async function withQueryCount(fn: () => Promise): Promise<{ value: T; queries: number }> { const connection = t.orm.em.getConnection(); const spy = vi.spyOn(connection, 'execute'); try { const value = await fn(); return { value, queries: spy.mock.calls.length }; } finally { spy.mockRestore(); } } describe('DayAssignmentsRepository β€” the DY1/DY3/AS1/AS3 projection', () => { it('ASSIGNPLACEREPO-001: listForDay returns [] for a day with no assignments', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); expect(await assignments.listForDay(day.id)).toEqual([]); }); it('ASSIGNPLACEREPO-002: listForDay is byte-identical to the legacy statement, column for column', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const category = createCategory(testDb, { name: 'Museum', color: '#111111', icon: 'πŸ›οΈ' }); const place = createPlace(testDb, trip.id, { name: 'Louvre', category_id: category.id }); const assignment = createDayAssignment(testDb, day.id, place.id); const rows = await assignments.listForDay(day.id); expect(rows).toHaveLength(1); expect(rows[0]).toStrictEqual(legacyProjectionRow(assignment.id)); // Named spot-checks so a future field rename fails loudly here, not just via toStrictEqual. expect(rows[0]).toMatchObject({ id: assignment.id, day_id: day.id, place_id: place.id, place_name: 'Louvre', category_id: category.id, category_name: 'Museum', category_color: '#111111', category_icon: 'πŸ›οΈ', }); }); it('ASSIGNPLACEREPO-003: findWithPlaceAndCategory(id) is the same row, and null for a missing id', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id, { name: 'Eiffel Tower' }); const assignment = createDayAssignment(testDb, day.id, place.id); expect(await assignments.findWithPlaceAndCategory(assignment.id)).toStrictEqual(legacyProjectionRow(assignment.id)); expect(await assignments.findWithPlaceAndCategory(999999)).toBeNull(); }); it('ASSIGNPLACEREPO-009: the Tour facet columns are set for a Tour and null for a plain place, even one with a route', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const tourPlace = createPlace(testDb, trip.id, { name: 'Ridge walk' }); const trackPlace = createPlace(testDb, trip.id, { name: 'Imported track' }); await updateRows(t, Places, { id: tourPlace.id }, { route_geometry: '[[47,11],[47.1,11.1]]' }); await updateRows(t, Places, { id: trackPlace.id }, { route_geometry: '[[1,2],[3,4]]' }); createTour(testDb, tourPlace.id); const tour = createDayAssignment(testDb, day.id, tourPlace.id, { order_index: 0 }); const track = createDayAssignment(testDb, day.id, trackPlace.id, { order_index: 1 }); const one = await assignments.findWithPlaceAndCategory(tour.id); expect(one).toStrictEqual(legacyProjectionRow(tour.id)); expect(one).toMatchObject({ tour_place_id: tourPlace.id, tour_route_geometry: '[[47,11],[47.1,11.1]]' }); expect(await assignments.findWithPlaceAndCategory(track.id)).toMatchObject({ tour_place_id: null, tour_route_geometry: null, }); const facets = (rows: { tour_place_id: number | null; tour_route_geometry: string | null }[]) => rows.map((r) => [r.tour_place_id, r.tour_route_geometry]); const expected = [ [tourPlace.id, '[[47,11],[47.1,11.1]]'], [null, null], ]; expect(facets(await assignments.listForDay(day.id))).toEqual(expected); expect(facets(await assignments.listWithPlaceAndCategory([day.id]))).toEqual(expected); }); it("ASSIGNPLACEREPO-004: place_time/end_time COALESCE β€” the assignment's own time wins, the place's time is the fallback", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); await updateRows(t, Places, { id: place.id }, { place_time: '09:00', end_time: '10:00' }); const withOwnTime = createDayAssignment(testDb, day.id, place.id); await updateRows( t, DayAssignments, { id: withOwnTime.id }, { assignment_time: '14:00', assignment_end_time: '15:00' }, ); const withoutOwnTime = createDayAssignment(testDb, day.id, place.id); const rows = await assignments.listForDay(day.id); const own = rows.find((r) => r.id === withOwnTime.id)!; const fallback = rows.find((r) => r.id === withoutOwnTime.id)!; expect(own.place_time).toBe('14:00'); expect(own.end_time).toBe('15:00'); expect(fallback.place_time).toBe('09:00'); expect(fallback.end_time).toBe('10:00'); }); it('ASSIGNPLACEREPO-005: category is null for an uncategorised place (LEFT JOIN, not INNER)', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); await updateRows(t, Places, { id: place.id }, { category: null }); const assignment = createDayAssignment(testDb, day.id, place.id); const [row] = await assignments.listForDay(day.id); expect(row.category_id).toBeNull(); expect(row.category_name).toBeNull(); expect(row.category_color).toBeNull(); expect(row.category_icon).toBeNull(); expect(row.id).toBe(assignment.id); }); it('ASSIGNPLACEREPO-006: ORDER BY order_index ASC, created_at ASC', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const placeA = createPlace(testDb, trip.id, { name: 'Second' }); const placeB = createPlace(testDb, trip.id, { name: 'First' }); createDayAssignment(testDb, day.id, placeA.id, { order_index: 2 }); createDayAssignment(testDb, day.id, placeB.id, { order_index: 1 }); const rows = await assignments.listForDay(day.id); expect(rows.map((r) => r.place_name)).toEqual(['First', 'Second']); }); it('ASSIGNPLACEREPO-007: listWithPlaceAndCategory spans several days, in the same order; an empty array short-circuits with 0 queries', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const dayA = createDay(testDb, trip.id, { day_number: 1 }); const dayB = createDay(testDb, trip.id, { day_number: 2 }); const placeA = createPlace(testDb, trip.id, { name: 'A' }); const placeB = createPlace(testDb, trip.id, { name: 'B' }); const a = createDayAssignment(testDb, dayA.id, placeA.id); const b = createDayAssignment(testDb, dayB.id, placeB.id); const rows = await assignments.listWithPlaceAndCategory([dayA.id, dayB.id]); expect(rows.map((r) => r.id).sort()).toEqual([a.id, b.id].sort()); const { value, queries } = await withQueryCount(() => assignments.listWithPlaceAndCategory([])); expect(value).toEqual([]); expect(queries).toBe(0); }); // D-shape: every method here is a `qb().execute(..., false)` projection, which // never hydrates an entity into the identity map by construction β€” proven // anyway, the "not required, proven regardless" shape (AssignmentParticipants // precedent). // // Task 2 review (task-2-review.md, L1): the setup read now passes `{ // disableIdentityMap: false }` so it genuinely caches a managed // `DayAssignments` entity first (the base's own default previously made // this a no-op read, same class of vacuousness as `DAYREPO-018`) β€” even // so, `listForDay` is `qb().execute('all', false)`, never `find`/ // `findOne`, so it cannot return that cached entity regardless; this test // proves the projection's OWN freshness, not the identity-map guard. it('ASSIGNPLACEREPO-008 (fresh after a raw UPDATE, not D-shape): a place rename after an unrelated identity-map read is visible in the FIRST wider projection', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id, { name: 'Old Name' }); const assignment = createDayAssignment(testDb, day.id, place.id); await t.repo(DayAssignments).find({}, { disableIdentityMap: false }); // populate the identity map with the managed assignment entity // test-sql-allow: the out-of-band write this case is about has to bypass every EntityManager, the one under test included. testDb.prepare('UPDATE places SET name = ? WHERE id = ?').run('Renamed', place.id); const [row] = await assignments.listForDay(day.id); expect(row.place_name).toBe('Renamed'); expect(row.id).toBe(assignment.id); }); }); describe("DayAssignmentsRepository β€” AC20/AC21 (the stop a booking owns, carried to another day by night-seat.ts's reseatOwnStopWith)", () => { it('ASSIGNREPO-036 (AC20, maxOrderIndexExcluding): the highest order_index on the day without the excluded row β€” null for a day holding nothing else, a stored 0 not confused with "no rows"', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const fromDay = createDay(testDb, trip.id, { day_number: 1 }); const toDay = createDay(testDb, trip.id, { day_number: 2 }); const stopPlace = createPlace(testDb, trip.id, { name: 'Hotel stop' }); const otherPlace = createPlace(testDb, trip.id, { name: 'Existing stop' }); const stop = createDayAssignment(testDb, fromDay.id, stopPlace.id, { order_index: 0 }); expect(await assignments.maxOrderIndexExcluding(toDay.id, stop.id)).toBeNull(); const seated = createDayAssignment(testDb, toDay.id, otherPlace.id, { order_index: 0 }); expect(await assignments.maxOrderIndexExcluding(toDay.id, stop.id)).toBe(0); // The excluded row is the only one on the day: nothing is left to measure against. expect(await assignments.maxOrderIndexExcluding(toDay.id, seated.id)).toBeNull(); createDayAssignment(testDb, toDay.id, createPlace(testDb, trip.id).id, { order_index: 5 }); expect(await assignments.maxOrderIndexExcluding(toDay.id, stop.id)).toBe(5); expect(await assignments.maxOrderIndexExcluding(fromDay.id, stop.id)).toBeNull(); }); it('ASSIGNREPO-037 (AC21, relocate): writes day_id, place_id and order_index onto exactly the given row, the other stops of both days untouched', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const fromDay = createDay(testDb, trip.id, { day_number: 1 }); const toDay = createDay(testDb, trip.id, { day_number: 2 }); const stopPlace = createPlace(testDb, trip.id, { name: 'Hotel stop' }); const otherPlace = createPlace(testDb, trip.id, { name: 'Existing stop' }); const stop = createDayAssignment(testDb, fromDay.id, stopPlace.id, { order_index: 0 }); const neighbour = createDayAssignment(testDb, fromDay.id, otherPlace.id, { order_index: 1 }); const existing = createDayAssignment(testDb, toDay.id, otherPlace.id, { order_index: 5 }); await assignments.relocate(stop.id, toDay.id, stopPlace.id, 6); const read = (id: number) => assignmentCols(id, 'day_id', 'place_id', 'order_index'); expect(await read(stop.id)).toEqual({ day_id: toDay.id, place_id: stopPlace.id, order_index: 6 }); expect(await read(neighbour.id)).toEqual({ day_id: fromDay.id, place_id: otherPlace.id, order_index: 1 }); expect(await read(existing.id)).toEqual({ day_id: toDay.id, place_id: otherPlace.id, order_index: 5 }); }); }); // --------------------------------------------------------------------------- // Plan 3c Task 3 (`AssignmentsService`) β€” appended after Task 2's own // methods/tests above, per this task's file-ownership rule. // --------------------------------------------------------------------------- describe('DayAssignmentsRepository β€” AS4/AS9/AS12 existence + trip-scoped read', () => { it('ASSIGNREPO-001 (AS9, existsInDay): true only for the matching assignment/day/trip triple', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const otherDay = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); expect(await assignments.existsInDay(a.id, day.id, trip.id)).toBe(true); expect(await assignments.existsInDay(a.id, otherDay.id, trip.id)).toBe(false); expect(await assignments.existsInDay(a.id, day.id, trip.id + 1)).toBe(false); }); it("ASSIGNREPO-002 (AS9): raw-bind on day_id/trip_id β€” a string binds unconverted, same as a real number; `id` itself is `number`-only now (Task 3 review H1, absorbed in Task 4: the method's only caller `toRowId`s `id` before calling, so the type narrowed to match)", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); expect(await assignments.existsInDay(a.id, String(day.id), String(trip.id))).toBe(true); }); it('ASSIGNREPO-003 (AS12, findInTrip): the raw `da.*` row, scoped to the trip via a two-hop join; undefined cross-trip', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id, { order_index: 3 }); await updateRows(t, DayAssignments, { id: a.id }, { assignment_time: '09:00', notes: 'a note' }); const row = await assignments.findInTrip(a.id, trip.id); // test-sql-allow: the legacy AS12 statement is the parity oracle the repository is compared against. const legacy = testDb .prepare(`SELECT da.* FROM day_assignments da JOIN days d ON da.day_id = d.id WHERE da.id = ? AND d.trip_id = ?`) .get(a.id, trip.id); expect(row).toStrictEqual(legacy); expect(row).toMatchObject({ id: a.id, day_id: day.id, order_index: 3, assignment_time: '09:00', notes: 'a note' }); expect(await assignments.findInTrip(a.id, trip.id + 1)).toBeUndefined(); }); }); describe('DayAssignmentsRepository β€” AS6/AS7/AS8 (createAssignment)', () => { it('ASSIGNREPO-004 (AS6, maxOrderIndex): null for a day with no assignments, otherwise the highest order_index β€” a stored 0 is not confused with "no rows"', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); expect(await assignments.maxOrderIndex(day.id)).toBeNull(); createDayAssignment(testDb, day.id, place.id, { order_index: 0 }); expect(await assignments.maxOrderIndex(day.id)).toBe(0); createDayAssignment(testDb, day.id, place.id, { order_index: 5 }); expect(await assignments.maxOrderIndex(day.id)).toBe(5); }); it('ASSIGNREPO-005 (AS7, shiftOrderFrom): shifts order_index for rows at/after the threshold, on that day only', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const otherDay = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const before = createDayAssignment(testDb, day.id, place.id, { order_index: 0 }); const at = createDayAssignment(testDb, day.id, place.id, { order_index: 1 }); const after = createDayAssignment(testDb, day.id, place.id, { order_index: 2 }); const foreign = createDayAssignment(testDb, otherDay.id, place.id, { order_index: 1 }); await withRequestContext(t.orm, async () => { await assignments.shiftOrderFrom(day.id, 1); }); expect(await orderOf(before.id)).toBe(0); expect(await orderOf(at.id)).toBe(2); expect(await orderOf(after.id)).toBe(3); expect(await orderOf(foreign.id)).toBe(1); }); it('ASSIGNREPO-006 (AS8, insertAssignment): writes every column and returns the inserted id, notes/accommodation_id nullable', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const id = await withRequestContext(t.orm, () => assignments.insertAssignment({ day_id: day.id, place_id: place.id, order_index: 2, notes: null, accommodation_id: null, }), ); expect(typeof id).toBe('number'); const row = await assignmentCols(id, 'day_id', 'place_id', 'order_index', 'notes', 'accommodation_id'); expect(row).toEqual({ day_id: day.id, place_id: place.id, order_index: 2, notes: null, accommodation_id: null }); const id2 = await withRequestContext(t.orm, () => assignments.insertAssignment({ day_id: day.id, place_id: place.id, order_index: 0, notes: 'skip the line', accommodation_id: 42, }), ); const row2 = await assignmentCols(id2, 'notes', 'accommodation_id'); expect(row2).toEqual({ notes: 'skip the line', accommodation_id: 42 }); }); }); describe('DayAssignmentsRepository β€” AS10/AS11/AS13/AS14/AS19 (delete / order / move)', () => { it('ASSIGNREPO-007 (AS10, deleteById): removes the row', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); await withRequestContext(t.orm, () => assignments.deleteById(a.id)); expect(await findRow(t, DayAssignments, { id: a.id })).toBeNull(); }); it('ASSIGNREPO-008 (AS11, setOrderIndex day-scoped): writes only the row matching BOTH id and day_id', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const otherDay = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id, { order_index: 0 }); const foreign = createDayAssignment(testDb, otherDay.id, place.id, { order_index: 0 }); await withRequestContext(t.orm, () => assignments.setOrderIndex(a.id, day.id, 7)); // A day_id mismatch (this id does not sit on otherDay) writes nothing. await withRequestContext(t.orm, () => assignments.setOrderIndex(foreign.id, day.id, 9)); expect(await orderOf(a.id)).toBe(7); expect(await orderOf(foreign.id)).toBe(0); }); it('ASSIGNREPO-009 (AS19, setOrderIndex unscoped): day_id undefined writes by id alone', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id, { order_index: 0 }); await withRequestContext(t.orm, () => assignments.setOrderIndex(a.id, undefined, 3)); expect(await orderOf(a.id)).toBe(3); }); it('ASSIGNREPO-009b (setOrderIndexes): one statement leaves what the per-row writes left, day scope included', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const otherDay = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id, { order_index: 0 }); const b = createDayAssignment(testDb, day.id, place.id, { order_index: 1 }); const c = createDayAssignment(testDb, day.id, place.id, { order_index: 2 }); const foreign = createDayAssignment(testDb, otherDay.id, place.id, { order_index: 5 }); // b twice: the later index wins, as two writes in a row would leave it; foreign is on another day. await withRequestContext(t.orm, () => assignments.setOrderIndexes( [ { id: c.id, order_index: 0 }, { id: b.id, order_index: 9 }, { id: a.id, order_index: 2 }, { id: b.id, order_index: 1 }, { id: foreign.id, order_index: 0 }, ], day.id, ), ); expect([await orderOf(a.id), await orderOf(b.id), await orderOf(c.id), await orderOf(foreign.id)]).toEqual([ 2, 1, 0, 5, ]); // Unscoped: by id alone, and only the listed rows. await withRequestContext(t.orm, () => assignments.setOrderIndexes([{ id: foreign.id, order_index: 1 }])); expect([await orderOf(foreign.id), await orderOf(a.id)]).toEqual([1, 2]); // Nothing to write is no statement at all. await withRequestContext(t.orm, () => assignments.setOrderIndexes([], day.id)); expect(await orderOf(c.id)).toBe(0); }); it("ASSIGNREPO-010 (AS13, getDayId): the assignment's day_id, undefined for a missing id", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); expect(await assignments.getDayId(a.id)).toBe(day.id); expect(await assignments.getDayId(999999)).toBeUndefined(); }); it('ASSIGNREPO-042 (getPlaceId): the place_id of the assignment, undefined for a missing id', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); expect(await assignments.getPlaceId(a.id)).toBe(place.id); expect(await assignments.getPlaceId(999999)).toBeUndefined(); }); it('ASSIGNREPO-011 (AS14, moveToDay): writes day_id and order_index together', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const target = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id, { order_index: 0 }); await withRequestContext(t.orm, () => assignments.moveToDay(a.id, target.id, 4)); const row = await assignmentCols(a.id, 'day_id', 'order_index'); expect(row).toEqual({ day_id: target.id, order_index: 4 }); }); // Task 9 fix wave (B-M5, the OAUTHTOKREPO-012/REANCHORDAY-003 rollback // shape): `moveToDay` writes inside a `uow.transactional` body that then // throws β€” the write must roll back with it. MUTATION-PROVED (see the // report): removing the `throw` makes the post-rollback assertion below // fail (the row WOULD have moved). it('ASSIGNREPO-031: inside a uow.transactional that then ROLLS BACK, moveToDay is left un-moved', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const target = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id, { order_index: 0 }); let caught: unknown; try { await withRequestContext(t.orm, async () => { await uow.transactional(async () => { await assignments.moveToDay(a.id, target.id, 4); throw new Error('force rollback'); }); }); } catch (e) { caught = e; } expect((caught as Error).message).toBe('force rollback'); const row = await assignmentCols(a.id, 'day_id', 'order_index'); expect(row).toEqual({ day_id: day.id, order_index: 0 }); }); }); describe('DayAssignmentsRepository.effectiveStart (AS16, Kysely)', () => { it("ASSIGNREPO-012: byte-identical to the legacy three-way COALESCE, own time wins over the place's", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); await updateRows(t, Places, { id: place.id }, { place_time: '08:00' }); const a = createDayAssignment(testDb, day.id, place.id); await updateRows(t, DayAssignments, { id: a.id }, { assignment_time: '14:00' }); const row = await withRequestContext(t.orm, () => assignments.effectiveStart(a.id)); // test-sql-allow: the legacy AS16 three-way COALESCE is the parity oracle the repository is compared against. const legacy = testDb .prepare( ` SELECT da.day_id, COALESCE(da.assignment_time, p.place_time, acc.check_in) AS start FROM day_assignments da JOIN places p ON da.place_id = p.id LEFT JOIN day_accommodations acc ON acc.id = da.accommodation_id WHERE da.id = ? `, ) .get(a.id); expect(row).toEqual(legacy); expect(row).toEqual({ day_id: day.id, start: '14:00' }); }); it("ASSIGNREPO-013: falls back to the place's own time when the assignment has none", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); await updateRows(t, Places, { id: place.id }, { place_time: '08:00' }); const a = createDayAssignment(testDb, day.id, place.id); expect(await withRequestContext(t.orm, () => assignments.effectiveStart(a.id))).toEqual({ day_id: day.id, start: '08:00', }); }); it("ASSIGNREPO-014: a booked night falls back further, to the accommodation's check_in (a LEFT JOIN through the plain accommodation_id column, no ORM relation)", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const hotel = createPlace(testDb, trip.id); const night = createDayAssignment(testDb, day.id, hotel.id); const stayId = await insertRow(t, DayAccommodations, { trip: trip.id, place: hotel.id, startDay: day.id, endDay: day.id, check_in: '17:00', }); await updateRows(t, DayAssignments, { id: night.id }, { accommodation_id: stayId }); expect(await withRequestContext(t.orm, () => assignments.effectiveStart(night.id))).toEqual({ day_id: day.id, start: '17:00', }); }); it('ASSIGNREPO-015: null start when nothing sets a time anywhere in the chain', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); expect(await withRequestContext(t.orm, () => assignments.effectiveStart(a.id))).toEqual({ day_id: day.id, start: null, }); }); it('ASSIGNREPO-016: undefined for a missing id', async () => { expect(await withRequestContext(t.orm, () => assignments.effectiveStart(999999))).toBeUndefined(); }); }); describe('DayAssignmentsRepository.listForTimeSort (AS18, Kysely)', () => { it('ASSIGNREPO-017: byte-identical to the legacy statement, including the computed `located` boolean (0/1) and the three-key ORDER BY', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const located = createPlace(testDb, trip.id, { name: 'Has coords' }); const unlocated = createPlace(testDb, trip.id, { name: 'No coords' }); await updateRows(t, Places, { id: unlocated.id }, { lat: null, lng: null }); const a1 = createDayAssignment(testDb, day.id, located.id, { order_index: 1 }); const a2 = createDayAssignment(testDb, day.id, unlocated.id, { order_index: 0 }); await updateRows(t, DayAssignments, { id: a1.id }, { assignment_time: '09:00' }); const rows = await withRequestContext(t.orm, () => assignments.listForTimeSort(day.id)); // test-sql-allow: the legacy AS18 statement is the parity oracle the repository is compared against. const legacy = testDb .prepare( ` SELECT da.id, da.order_index, COALESCE(da.assignment_time, p.place_time, acc.check_in) as effective_time, (p.lat IS NOT NULL AND p.lng IS NOT NULL) as located FROM day_assignments da JOIN places p ON da.place_id = p.id LEFT JOIN day_accommodations acc ON acc.id = da.accommodation_id WHERE da.day_id = ? ORDER BY da.order_index ASC, da.created_at ASC, da.id ASC `, ) .all(day.id); expect(rows).toEqual(legacy); expect(rows.map((r) => r.id)).toEqual([a2.id, a1.id]); expect(rows.find((r) => r.id === a1.id)?.located).toBe(1); expect(rows.find((r) => r.id === a2.id)?.located).toBe(0); }); it('ASSIGNREPO-018: [] for a day with no assignments', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); expect(await withRequestContext(t.orm, () => assignments.listForTimeSort(day.id))).toEqual([]); }); }); describe('DayAssignmentsRepository.listSeatRows (AC7, Kysely)', () => { it("ASSIGNREPO-038: byte-identical to the legacy statement β€” a booked night at its check_in, a stop at its own time, a stop at its place's, `located` 0/1 and the three-key ORDER BY", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const hotel = createPlace(testDb, trip.id, { name: 'Hotel' }); const museum = createPlace(testDb, trip.id, { name: 'Museum' }); const cafe = createPlace(testDb, trip.id, { name: 'Cafe' }); const unlocated = createPlace(testDb, trip.id, { name: 'No coords' }); await updateRows(t, Places, { id: hotel.id }, { place_time: '08:00' }); await updateRows(t, Places, { id: cafe.id }, { place_time: '11:00' }); await updateRows(t, Places, { id: unlocated.id }, { lat: null, lng: null }); const night = createDayAssignment(testDb, day.id, hotel.id, { order_index: 3 }); const ownTime = createDayAssignment(testDb, day.id, museum.id, { order_index: 1 }); const placeTime = createDayAssignment(testDb, day.id, cafe.id, { order_index: 0 }); const noCoords = createDayAssignment(testDb, day.id, unlocated.id, { order_index: 2 }); const stay = createDayAccommodation(testDb, trip.id, hotel.id, day.id, day.id, { check_in: '17:00' }); await updateRows(t, DayAssignments, { id: night.id }, { accommodation_id: stay.id }); await updateRows(t, DayAssignments, { id: ownTime.id }, { assignment_time: '09:00' }); const rows = await withRequestContext(t.orm, () => assignments.listSeatRows(day.id)); // test-sql-allow: the legacy AC7 statement is the parity oracle the repository is compared against. const legacy = testDb .prepare( ` SELECT da.id, da.order_index, CASE WHEN other.id IS NULL THEN COALESCE(da.assignment_time, p.place_time) ELSE other.check_in END AS at, other.id AS night_id, (p.lat IS NOT NULL AND p.lng IS NOT NULL) AS located FROM day_assignments da JOIN places p ON p.id = da.place_id LEFT JOIN day_accommodations other ON other.id = da.accommodation_id WHERE da.day_id = ? ORDER BY da.order_index ASC, da.created_at ASC, da.id ASC `, ) .all(day.id); expect(rows).toEqual(legacy); expect(rows.map((r) => r.id)).toEqual([placeTime.id, ownTime.id, noCoords.id, night.id]); expect(rows.find((r) => r.id === night.id)).toMatchObject({ at: '17:00', night_id: stay.id, located: 1 }); expect(rows.find((r) => r.id === ownTime.id)).toMatchObject({ at: '09:00', night_id: null, located: 1 }); expect(rows.find((r) => r.id === placeTime.id)).toMatchObject({ at: '11:00', night_id: null }); expect(rows.find((r) => r.id === noCoords.id)?.located).toBe(0); }); }); describe('DayAssignmentsRepository β€” AS17/AS24/AS25/AS26/AS27/AS32/AS33/AS34 (single-column writes)', () => { it('ASSIGNREPO-019 (AS17, setTimes): writes both columns, null-clearable', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); await withRequestContext(t.orm, () => assignments.setTimes(a.id, '09:00', '10:30')); expect(await assignmentCols(a.id, 'assignment_time', 'assignment_end_time')).toEqual({ assignment_time: '09:00', assignment_end_time: '10:30', }); await withRequestContext(t.orm, () => assignments.setTimes(a.id, null, null)); expect(await assignmentCols(a.id, 'assignment_time', 'assignment_end_time')).toEqual({ assignment_time: null, assignment_end_time: null, }); }); // Task 9 fix wave (B-M5, the OAUTHTOKREPO-012/REANCHORDAY-003 rollback // shape): `setTimes` writes inside a `uow.transactional` body that then // throws β€” the write must roll back with it. it('ASSIGNREPO-032: inside a uow.transactional that then ROLLS BACK, setTimes is left unwritten', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); let caught: unknown; try { await withRequestContext(t.orm, async () => { await uow.transactional(async () => { await assignments.setTimes(a.id, '09:00', '10:30'); throw new Error('force rollback'); }); }); } catch (e) { caught = e; } expect((caught as Error).message).toBe('force rollback'); expect(await assignmentCols(a.id, 'assignment_time', 'assignment_end_time')).toEqual({ assignment_time: null, assignment_end_time: null, }); }); it('ASSIGNREPO-020 (AS24, setEndDay): writes the 0/1 flag', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); await withRequestContext(t.orm, () => assignments.setEndDay(a.id, 1)); expect((await findRow(t, DayAssignments, { id: a.id }))!.end_day).toBe(1); }); it('ASSIGNREPO-021 (AS25, setNotes): null-clearable', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); await withRequestContext(t.orm, () => assignments.setNotes(a.id, 'a note')); expect((await findRow(t, DayAssignments, { id: a.id }))!.notes).toBe('a note'); await withRequestContext(t.orm, () => assignments.setNotes(a.id, null)); expect((await findRow(t, DayAssignments, { id: a.id }))!.notes).toBeNull(); }); it('ASSIGNREPO-022 (AS26/AS27, setLegMode/setIncomingLegMode): each writes its own column', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); await withRequestContext(t.orm, () => assignments.setLegMode(a.id, 'cycling')); await withRequestContext(t.orm, () => assignments.setIncomingLegMode(a.id, 'walking')); expect(await assignmentCols(a.id, 'leg_transport_mode', 'incoming_leg_transport_mode')).toEqual({ leg_transport_mode: 'cycling', incoming_leg_transport_mode: 'walking', }); await withRequestContext(t.orm, () => assignments.setLegMode(a.id, null)); expect((await findRow(t, DayAssignments, { id: a.id }))!.leg_transport_mode).toBeNull(); }); it("ASSIGNREPO-039 (AS32, listIdsToClear): exactly the day's ids, [] for an empty day", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const otherDay = createDay(testDb, trip.id); const emptyDay = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a1 = createDayAssignment(testDb, day.id, place.id); const a2 = createDayAssignment(testDb, day.id, place.id); createDayAssignment(testDb, otherDay.id, place.id); expect((await withRequestContext(t.orm, () => assignments.listIdsToClear(day.id))).sort((x, y) => x - y)).toEqual([ a1.id, a2.id, ]); expect(await withRequestContext(t.orm, () => assignments.listIdsToClear(emptyDay.id))).toEqual([]); }); it("ASSIGNREPO-040 (AS33, deleteForDay): removes only that day's rows", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const otherDay = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); createDayAssignment(testDb, day.id, place.id); createDayAssignment(testDb, day.id, place.id); const kept = createDayAssignment(testDb, otherDay.id, place.id); await withRequestContext(t.orm, () => assignments.deleteForDay(day.id)); expect(await findRows(t, DayAssignments, { day: day.id })).toEqual([]); expect((await findRows(t, DayAssignments, { day: otherDay.id }, { id: 'asc' })).map((r) => ({ id: r.id }))).toEqual( [{ id: kept.id }], ); }); it('ASSIGNREPO-041 (AS34, setRouteExcluded): writes 1 and then 0 on exactly the given row', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); const other = createDayAssignment(testDb, day.id, place.id); const routeExcluded = async (id: number) => (await findRow(t, DayAssignments, { id }))!.route_excluded; await withRequestContext(t.orm, () => assignments.setRouteExcluded(a.id, 1)); expect(await routeExcluded(a.id)).toBe(1); expect(await routeExcluded(other.id)).toBe(0); await withRequestContext(t.orm, () => assignments.setRouteExcluded(a.id, 0)); expect(await routeExcluded(a.id)).toBe(0); expect(await routeExcluded(other.id)).toBe(0); }); }); // D-shape: a `qb().execute(..., false)` projection or a Kysely read, neither // of which hydrates an entity into the identity map by construction (the // ASSIGNPLACEREPO-008 precedent above) β€” proven anyway for the new reads. // Task 2 review (task-2-review.md, L1, program note): the setup read passes // `{ disableIdentityMap: false }` so it genuinely caches the managed // assignment entity first (a bare `find({})` would be a no-op read under // the base's own default, the same vacuousness `DAYREPO-018` had). it('ASSIGNREPO-023 (fresh after a raw UPDATE, not D-shape): a notes write after an unrelated identity-map read is visible in findInTrip', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const a = createDayAssignment(testDb, day.id, place.id); await t.repo(DayAssignments).find({}, { disableIdentityMap: false }); // populate the identity map with the managed assignment entity await withRequestContext(t.orm, () => assignments.setNotes(a.id, 'fresh note')); expect(await assignments.findInTrip(a.id, trip.id)).toMatchObject({ notes: 'fresh note' }); }); // --------------------------------------------------------------------------- // Plan 3c Task 4 (`PlacesService.exportGpx`) β€” the PL30 itinerary read. // --------------------------------------------------------------------------- describe('DayAssignmentsRepository.listItineraryForGpx (PL30)', () => { it('ASSIGNREPO-024: joins day + place, only stops with coordinates, ordered by day_number then order_index', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day1 = createDay(testDb, trip.id, { day_number: 1, date: '2026-01-01', title: 'Day One' }); const day2 = createDay(testDb, trip.id, { day_number: 2, date: '2026-01-02' }); const geo = createPlace(testDb, trip.id, { name: 'Has Coords', lat: 1, lng: 2 }); const noGeo = await insertRow(t, Places, { trip: trip.id, name: 'No Coords', lat: null, lng: null }); const secondStop = createPlace(testDb, trip.id, { name: 'Second Stop', lat: 3, lng: 4 }); // day2's stop first (order_index 0), day1's stop second β€” proves the // ORDER BY is day_number then order_index, not insertion/id order. createDayAssignment(testDb, day2.id, secondStop.id, { order_index: 0 }); createDayAssignment(testDb, day1.id, geo.id, { order_index: 0 }); createDayAssignment(testDb, day1.id, noGeo, { order_index: 1 }); const rows = await assignments.listItineraryForGpx(trip.id); expect(rows).toEqual([ { day_number: 1, date: '2026-01-01', title: 'Day One', name: 'Has Coords', lat: 1, lng: 2 }, { day_number: 2, date: '2026-01-02', title: null, name: 'Second Stop', lat: 3, lng: 4 }, ]); }); it('ASSIGNREPO-025: a trip with no coordinate-bearing stops returns []', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const noGeo = await insertRow(t, Places, { trip: trip.id, name: 'No Coords', lat: null, lng: null }); createDayAssignment(testDb, day.id, noGeo); expect(await assignments.listItineraryForGpx(trip.id)).toEqual([]); }); it("ASSIGNREPO-026: another trip's stops never leak in", async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const other = createTrip(testDb, user.id); const day = createDay(testDb, other.id); const place = createPlace(testDb, other.id, { lat: 5, lng: 6 }); createDayAssignment(testDb, day.id, place.id); expect(await assignments.listItineraryForGpx(trip.id)).toEqual([]); }); }); // ── Plan 3c Task 8 (`TripsService.copy`, TP48/TP49/TP57) β€” additive ───────── describe('DayAssignmentsRepository.listAllForTrip (TP48)', () => { // Task 9 fix wave (B-M4, rule 19): widened from an ids-only assertion to // full-row parity on a fully seeded fixture β€” every nullable column // non-null in one row, null in the other, plus a unicode string and a // '007'-style digit string β€” `toEqual()`, so a renamed/dropped column or a changed row // order would fail this, not just the id list. it('ASSIGNREPO-027: every assignment of the trip, no ORDER BY guarantee, scoped by the dayβ†’trip join β€” toEqual(legacy) on a fully seeded fixture', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const other = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id, { name: 'Stop A' }); const bare = createPlace(testDb, trip.id, { name: 'Stop B' }); const accommodation = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id); // Row A: every nullable column non-null, a unicode string and a // '007'-style digit string among the values. const a1 = await insertRow(t, DayAssignments, { day: day.id, place: place.id, order_index: 5, notes: 'Notes β€” ζ—₯本 β˜•οΈ 007', reservation_status: 'confirmed', reservation_notes: 'resv notes 007', reservation_datetime: '2026-01-01T10:00:00Z', created_at: '2026-01-01 09:00:00', assignment_time: '10:00', assignment_end_time: '11:00', leg_transport_mode: 'walking', incoming_leg_transport_mode: 'driving', end_day: 1, accommodation_id: accommodation.id, }); // Row B: every nullable column NULL (end_day is NOT NULL, given 0). const a2 = await insertRow(t, DayAssignments, { day: day.id, place: bare.id, order_index: null, notes: null, reservation_status: null, reservation_notes: null, reservation_datetime: null, created_at: null, assignment_time: null, assignment_end_time: null, leg_transport_mode: null, incoming_leg_transport_mode: null, end_day: 0, accommodation_id: null, }); const otherDay = createDay(testDb, other.id); const otherPlace = createPlace(testDb, other.id); createDayAssignment(testDb, otherDay.id, otherPlace.id); const rows = await assignments.listAllForTrip(trip.id); // test-sql-allow: the legacy TP48 statement is the parity oracle the repository is compared against. const legacy = testDb .prepare('SELECT da.* FROM day_assignments da JOIN days d ON d.id = da.day_id WHERE d.trip_id = ?') .all(trip.id); expect(rows).toEqual(legacy); expect(rows.map((r) => r.id)).toEqual([a1, a2]); }); it('ASSIGNREPO-028: an empty trip returns []', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); expect(await assignments.listAllForTrip(trip.id)).toEqual([]); }); }); describe('DayAssignmentsRepository.insertAssignmentCopy (TP49)', () => { it('ASSIGNREPO-029: writes the 10-column copy set verbatim, no accommodation_id (stamped separately, TP57)', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const newId = await assignments.insertAssignmentCopy({ day_id: day.id, place_id: place.id, order_index: 2, notes: 'n', reservation_status: 'booked', reservation_notes: 'rn', reservation_datetime: '2026-01-01T10:00', assignment_time: '10:00', assignment_end_time: '11:00', end_day: 0, }); const row = await assignmentCols( newId, 'day_id', 'place_id', 'order_index', 'notes', 'reservation_status', 'reservation_notes', 'reservation_datetime', 'assignment_time', 'assignment_end_time', 'end_day', 'accommodation_id', ); expect(row).toEqual({ day_id: day.id, place_id: place.id, order_index: 2, notes: 'n', reservation_status: 'booked', reservation_notes: 'rn', reservation_datetime: '2026-01-01T10:00', assignment_time: '10:00', assignment_end_time: '11:00', end_day: 0, accommodation_id: null, }); }); }); describe('DayAssignmentsRepository.setAccommodation (TP57)', () => { it('ASSIGNREPO-030: stamps accommodation_id onto exactly the given assignment', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id); const assignment = createDayAssignment(testDb, day.id, place.id); const other = createDayAssignment(testDb, day.id, place.id); const accom = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id); await assignments.setAccommodation(assignment.id, accom.id); expect((await findRow(t, DayAssignments, { id: assignment.id }))!.accommodation_id).toBe(accom.id); expect((await findRow(t, DayAssignments, { id: other.id }))!.accommodation_id).toBeNull(); }); }); // ───────────────────────────────────────────────────────────────────────────── // Plan 4 Task 8b-2, item 1 (3d Task 7 review's "DayAssignments.listRoadtripVisits" // carry): RPL2 (`roadtrip-plan.service.ts::context`'s `visits` read) had no // repository-level toEqual() test β€” a correlated scalar subquery // inside a LEFT JOIN ON clause, exercised here with every nullable column // both null (no stay, no leg transport, coalesce falls back to the place) // and set (a matching stay + its checkout day, explicit assignment overrides). // ───────────────────────────────────────────────────────────────────────────── const LEGACY_LIST_ROADTRIP_VISITS = ` SELECT a.id, a.day_id, a.order_index, a.place_id, p.name, p.lat, p.lng, COALESCE(a.assignment_time, p.place_time) AS time, COALESCE(a.assignment_end_time, p.end_time) AS end_time, p.duration_minutes, a.end_day, a.leg_transport_mode, a.incoming_leg_transport_mode, p.stop_type, p.fill_percent, a.accommodation_id, stay.id AS stay_id, stay.check_in, stay.check_out, checkout.day_number AS checkout_day FROM day_assignments a JOIN days d ON d.id = a.day_id JOIN places p ON p.id = a.place_id LEFT JOIN day_accommodations stay ON stay.id = (SELECT id FROM day_accommodations WHERE place_id = p.id AND start_day_id = d.id ORDER BY id LIMIT 1) LEFT JOIN days checkout ON checkout.id = stay.end_day_id WHERE d.trip_id = ? ORDER BY d.day_number, a.order_index, a.created_at `; describe('DayAssignmentsRepository.listRoadtripVisits (RPL2, roadtrip-plan.service.ts::context)', () => { it('ASSIGNREPO-034: matches the legacy statement β€” no stay, coalesce falls back to the place, leg transport NULL', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id); const place = createPlace(testDb, trip.id, { name: 'Museum' }); await updateRows( t, Places, { id: place.id }, { place_time: '09:00', end_time: '11:00', duration_minutes: 90, stop_type: null, fill_percent: null }, ); createDayAssignment(testDb, day.id, place.id); // test-sql-allow: the legacy RPL2 statement is the parity oracle the repository is compared against. const legacy = testDb.prepare(LEGACY_LIST_ROADTRIP_VISITS).all(trip.id); const typed = await assignments.listRoadtripVisits(trip.id); expect(typed).toEqual(legacy); expect(typed[0]).toMatchObject({ time: '09:00', end_time: '11:00', stay_id: null, check_in: null, check_out: null, checkout_day: null, leg_transport_mode: null, }); }); it('ASSIGNREPO-035: matches the legacy statement β€” a matching stay populates stay/checkout columns, explicit overrides win over coalesce', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day = createDay(testDb, trip.id, { day_number: 1 }); const checkoutDay = createDay(testDb, trip.id, { day_number: 2 }); const place = createPlace(testDb, trip.id, { name: 'Grand Hotel' }); await updateRows( t, Places, { id: place.id }, { place_time: '08:00', end_time: '10:00', duration_minutes: 45, stop_type: 'lodging', fill_percent: 80 }, ); const stay = createDayAccommodation(testDb, trip.id, place.id, day.id, checkoutDay.id, { check_in: '15:00', check_out: '11:00', }); const assignment = createDayAssignment(testDb, day.id, place.id); await updateRows( t, DayAssignments, { id: assignment.id }, { assignment_time: '14:00', assignment_end_time: '14:30', leg_transport_mode: 'driving', incoming_leg_transport_mode: 'walking', end_day: 1, }, ); // test-sql-allow: the legacy RPL2 statement is the parity oracle the repository is compared against. const legacy = testDb.prepare(LEGACY_LIST_ROADTRIP_VISITS).all(trip.id); const typed = await assignments.listRoadtripVisits(trip.id); expect(typed).toEqual(legacy); expect(typed[0]).toMatchObject({ time: '14:00', end_time: '14:30', stay_id: stay.id, check_in: '15:00', check_out: '11:00', checkout_day: 2, leg_transport_mode: 'driving', incoming_leg_transport_mode: 'walking', end_day: 1, stop_type: 'lodging', fill_percent: 80, duration_minutes: 45, }); }); it('ASSIGNREPO-033: empty array for a trip with no assignments', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); expect(await assignments.listRoadtripVisits(trip.id)).toEqual([]); }); }); /** * Plan 4 Task 8b-4a (3h L4) β€” full-key `toEqual()` parity for * `DayAssignmentsRepository.listPublicForShare` (SH9), flagged by the 3h * ledger as having no repository-level parity test. Same WHERE/ORDER BY * shape as {@link legacyProjectionRow}'s join above (`listForDay`'s single- * id form widened to `day_id IN (...)`), but a narrower SELECT list: the * six columns `SharePublicAssignmentRow` omits (`google_place_id`, * `google_ftid`, `osm_id`, `amap_poi_id`, `stop_type`, `fill_percent`) must * never appear on the row at all β€” a public share must not leak them. */ function legacyPublicForShare(dayIds: number[]): unknown { if (dayIds.length === 0) return []; const placeholders = dayIds.map(() => '?').join(','); // test-sql-allow: the legacy SH9 statement is the parity oracle the repository is compared against. return testDb .prepare( ` SELECT da.*, p.id as place_id, p.name as place_name, p.description as place_description, p.lat, p.lng, p.address, p.category_id, p.price, p.currency as place_currency, COALESCE(da.assignment_time, p.place_time) as place_time, COALESCE(da.assignment_end_time, p.end_time) as end_time, p.duration_minutes, p.notes as place_notes, p.image_url, p.transport_mode, p.website, p.phone, c.name as category_name, c.color as category_color, c.icon as category_icon FROM day_assignments da JOIN places p ON da.place_id = p.id LEFT JOIN categories c ON p.category_id = c.id WHERE da.day_id IN (${placeholders}) ORDER BY da.order_index ASC, da.created_at ASC `, ) .all(...dayIds); } describe('DayAssignmentsRepository.listPublicForShare (SH9, share.service.ts)', () => { it('matches the legacy statement across multiple days, every nullable column both null and set, owner-only columns withheld', async () => { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const day1 = createDay(testDb, trip.id, { day_number: 1 }); const day2 = createDay(testDb, trip.id, { day_number: 2 }); const excludedDay = createDay(testDb, trip.id, { day_number: 3 }); const category = createCategory(testDb, { name: 'Museum', color: '#111111', icon: 'πŸ›οΈ' }); const placeFull = createPlace(testDb, trip.id, { name: 'Louvre', lat: 48.86, lng: 2.34, category_id: category.id, description: 'Art museum', }); await updateRows( t, Places, { id: placeFull.id }, { address: 'Rue de Rivoli', price: 17.5, currency: 'EUR', place_time: '09:00', end_time: '11:00', duration_minutes: 120, notes: 'bring ID', image_url: 'https://img/louvre.jpg', transport_mode: 'walking', website: 'https://louvre.fr', phone: '+33140205050', google_place_id: 'ChIJ123', google_ftid: 'ftid1', osm_id: 'osm1', amap_poi_id: 'amap1', stop_type: 'lodging', fill_percent: 80, }, ); const assignmentOnFullPlace = createDayAssignment(testDb, day1.id, placeFull.id, { order_index: 2 }); await updateRows( t, DayAssignments, { id: assignmentOnFullPlace.id }, { assignment_time: '14:00', assignment_end_time: '14:30', reservation_status: 'confirmed', reservation_notes: 'window seat', reservation_datetime: '2026-09-01T09:00:00.000Z', leg_transport_mode: 'driving', incoming_leg_transport_mode: 'walking', accommodation_id: null, }, ); const placeBare = createPlace(testDb, trip.id, { name: 'Unnamed spot' }); await updateRows( t, Places, { id: placeBare.id }, { category: null, lat: null, lng: null, address: null, price: null, currency: null, place_time: null, end_time: null, duration_minutes: null, notes: null, image_url: null, transport_mode: null, website: null, phone: null, description: null, }, ); const assignmentOnBarePlace = createDayAssignment(testDb, day2.id, placeBare.id, { order_index: 1 }); // A second assignment on day1, ordered before both of the above by order_index. const placeSecond = createPlace(testDb, trip.id, { name: 'Second stop' }); const earlierOrderAssignment = createDayAssignment(testDb, day1.id, placeSecond.id, { order_index: 0 }); // An assignment on a day NOT in the requested set β€” must never leak in. const excludedPlace = createPlace(testDb, trip.id, { name: 'Excluded' }); createDayAssignment(testDb, excludedDay.id, excludedPlace.id); const dayIds = [day1.id, day2.id]; const legacy = legacyPublicForShare(dayIds); const typed = await assignments.listPublicForShare(dayIds); expect(typed).toEqual(legacy); expect(typed.map((r) => r.id)).toEqual([ earlierOrderAssignment.id, assignmentOnBarePlace.id, assignmentOnFullPlace.id, ]); const fullRow = typed.find((r) => r.id === assignmentOnFullPlace.id)!; expect(fullRow).toMatchObject({ category_name: 'Museum', category_color: '#111111', category_icon: 'πŸ›οΈ', place_time: '14:00', end_time: '14:30', }); expect(fullRow).not.toHaveProperty('google_place_id'); expect(fullRow).not.toHaveProperty('google_ftid'); expect(fullRow).not.toHaveProperty('osm_id'); expect(fullRow).not.toHaveProperty('amap_poi_id'); expect(fullRow).not.toHaveProperty('stop_type'); expect(fullRow).not.toHaveProperty('fill_percent'); const bareRow = typed.find((r) => r.id === assignmentOnBarePlace.id)!; expect(bareRow).toMatchObject({ category_id: null, category_name: null, category_color: null, category_icon: null, place_time: null, end_time: null, }); }); it('[] for an empty day_ids array (no query issued)', async () => { expect(await assignments.listPublicForShare([])).toEqual([]); }); }); describe('DayAssignmentsRepository tour stages (MDT-02)', () => { /** A tour on a trip with three days, the first holding nothing yet. */ function tourOnTrip() { const { user } = createUser(testDb); const trip = createTrip(testDb, user.id); const days = [createDay(testDb, trip.id), createDay(testDb, trip.id), createDay(testDb, trip.id)]; const place = createPlace(testDb, trip.id); createTour(testDb, place.id); return { trip, days, place }; } const stageOf = async (id: number) => (await findRow(t, DayAssignments, { id }))?.tour_stage_key; it('ASSIGNREPO-043: writes the stage a day holds, and none for the whole tour', async () => { const { days, place } = tourOnTrip(); const base = { place_id: place.id, order_index: 0, notes: null, accommodation_id: null }; const stage = await assignments.insertAssignment({ ...base, day_id: days[0].id, tour_stage_key: 'stage-one' }); const whole = await assignments.insertAssignment({ ...base, day_id: days[1].id }); expect(await stageOf(stage)).toBe('stage-one'); expect(await stageOf(whole)).toBeNull(); }); it('ASSIGNREPO-047: a copied assignment keeps the stage it held', async () => { const { days, place } = tourOnTrip(); const copy = { place_id: place.id, order_index: 0, notes: null, reservation_status: null, reservation_notes: null, reservation_datetime: null, assignment_time: null, assignment_end_time: null, end_day: 0, }; const stage = await assignments.insertAssignmentCopy({ ...copy, day_id: days[0].id, tour_stage_key: 'stage-one' }); const whole = await assignments.insertAssignmentCopy({ ...copy, day_id: days[1].id }); expect(await stageOf(stage)).toBe('stage-one'); expect(await stageOf(whole)).toBeNull(); }); });