Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

58.2 KB
Raw Blame
/**
 * Plan 3d Task 4 — the proof for CL1/CL2/CL4/CL7 (calendar) and RS20
 * (`listUpcoming`): full-key `toEqual(<legacy run raw>)` parity between
 * `ReservationsRepository`'s new methods and the exact legacy SQL they
 * replace (the legacy text is this file's own oracle — the production
 * copies were deleted the same commit these methods were added, per R3
 * "one source"; see `_shared/reservation-visibility.test.ts`'s identical
 * reasoning for keeping its own oracle text local).
 */
import { DayAccommodations } from '../../../../src/db/entities/DayAccommodations.entity';
import { DayAssignments } from '../../../../src/db/entities/DayAssignments.entity';
import { Days } from '../../../../src/db/entities/Days.entity';
import { ReservationDayPositions } from '../../../../src/db/entities/ReservationDayPositions.entity';
import { ReservationEndpoints } from '../../../../src/db/entities/ReservationEndpoints.entity';
import { Reservations } from '../../../../src/db/entities/Reservations.entity';
import { Trips } from '../../../../src/db/entities/Trips.entity';
import type { DayNotesRepository } from '../../../../src/db/repositories/DayNotes.repository';
import type { DaysRepository } from '../../../../src/db/repositories/Days.repository';
import type { ReservationsRepository } from '../../../../src/db/repositories/Reservations.repository';
import type { TripsRepository } from '../../../../src/db/repositories/Trips.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import {
  createDay,
  createDayAccommodation,
  createDayAssignment,
  createPlace,
  createReservation,
  createTrip,
  createUser,
  addTripMember,
} from '../../../helpers/factories';
import { findRow, findRows, insertRow, updateRows } from '../../../helpers/factories/rows';
import { resetTestDb } from '../../../helpers/test-db';
import {
  createTestReservationsRepo,
  createTestTripsRepo,
  createTestDaysRepo,
  createTestDayNotesRepo,
  sharedTestOrm,
} from '../../../helpers/test-uow';
import type { EntityData } from '@mikro-orm/core';

import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';

const testDb = createSnapshotTestDb();
let reservationsRepo: ReservationsRepository;
let tripsRepo: TripsRepository;
let daysRepo: DaysRepository;
let dayNotesRepo: DayNotesRepository;

beforeAll(async () => {
  reservationsRepo = await createTestReservationsRepo(testDb);
  tripsRepo = await createTestTripsRepo(testDb);
  daysRepo = await createTestDaysRepo(testDb);
  dayNotesRepo = await createTestDayNotesRepo(testDb);
});
beforeEach(() => resetTestDb(testDb));
afterAll(() => testDb.close());

const orm = () => sharedTestOrm(testDb);

async function setReservation(id: number, data: EntityData<Reservations>): Promise<void> {
  await updateRows(await orm(), Reservations, { id }, data);
}

async function setTrip(id: number, data: EntityData<Trips>): Promise<void> {
  await updateRows(await orm(), Trips, { id }, data);
}

/** The stored booking; fails the case when it is gone. */
async function reservationRow(id: number) {
  const row = await findRow(await orm(), Reservations, { id });
  if (!row) throw new Error(`no reservation ${id}`);
  return row;
}

/** The trip's day ids in day order. */
async function dayIds(tripId: number): Promise<{ id: number }[]> {
  return (await findRows(await orm(), Days, { trip: tripId }, { day_number: 'asc' })).map((d) => ({ id: d.id }));
}

// The RV1 fragment CL2/CL7's legacy statements interpolated inline — kept
// here, not imported, for the same "no production copy left to import from"
// reason `_shared/reservation-visibility.test.ts` gives.
const publicReservationSql = (alias = 'r'): string => `COALESCE(${alias}.ingest_state, 'live') <> 'staged'`;
const publicStaySql = (alias = 'a'): string => `(
      NOT EXISTS (SELECT 1 FROM reservations vr WHERE CAST(vr.accommodation_id AS INTEGER) = ${alias}.id)
      OR EXISTS (SELECT 1 FROM reservations vr WHERE CAST(vr.accommodation_id AS INTEGER) = ${alias}.id
                   AND ${publicReservationSql('vr')})
    )`;

describe('ReservationsRepository — CL1 (findRaw, existing method reused by calendar)', () => {
  it('CAL-CL1-001: matches SELECT * FROM trips WHERE id = ?', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { title: 'Alps' });

    const typed = await tripsRepo.findRaw(trip.id);
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb.prepare('SELECT * FROM trips WHERE id = ?').get(trip.id);

    expect(typed).toEqual(legacy);
  });
});

describe('ReservationsRepository — CL2 (listForCalendar)', () => {
  it('CAL-CL2-001: matches the legacy joined statement, live bookings only, staged excluded', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-06-01', end_date: '2026-06-05' });
    const place = createPlace(testDb, trip.id);
    const live = createReservation(testDb, trip.id, { title: 'Live' });
    await setReservation(live.id, { place: place.id });
    const staged = createReservation(testDb, trip.id, { title: 'Staged' });
    await setReservation(staged.id, { ingest_state: 'staged' });

    const typed = await reservationsRepo.listForCalendar(trip.id);
    const legacy = testDb
      // test-sql-allow: the legacy statement is the oracle the repository read is held to.
      .prepare(
        `SELECT r.*, pl.lat AS place_lat, pl.lng AS place_lng,
                sd.date AS stay_start_date, ed.date AS stay_end_date,
                a.check_in AS stay_check_in, a.check_out AS stay_check_out,
                (SELECT MIN(r2.id) FROM reservations r2
                  WHERE r2.accommodation_id = a.id) AS stay_first_reservation_id,
                rd.date AS day_date, red.date AS end_day_date
         FROM reservations r
         LEFT JOIN places pl ON r.place_id = pl.id
         LEFT JOIN day_accommodations a ON r.accommodation_id = a.id
         LEFT JOIN days sd ON a.start_day_id = sd.id
         LEFT JOIN days ed ON a.end_day_id = ed.id
         LEFT JOIN days rd ON r.day_id = rd.id
         LEFT JOIN days red ON r.end_day_id = red.id
         WHERE r.trip_id = ? AND ${publicReservationSql('r')}`,
      )
      .all(trip.id);

    expect(typed.map((r) => r.id)).toEqual([live.id]);
    expect(typed).toEqual(legacy);
  });

  it('CAL-CL2-002: a "14.0"-shaped accommodation_id join still resolves the stay dates', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-06-01', end_date: '2026-06-05' });
    const place = createPlace(testDb, trip.id);
    const days = await dayIds(trip.id);
    const stay = createDayAccommodation(testDb, trip.id, place.id, days[0].id, days[1].id, {
      check_in: '15:00',
      check_out: '11:00',
    });
    const booking = createReservation(testDb, trip.id, { title: 'Hotel', type: 'hotel' });
    await setReservation(booking.id, { accommodation_id: `${stay.id}.0` });

    const typed = await reservationsRepo.listForCalendar(trip.id);
    const legacy = testDb
      // test-sql-allow: the legacy statement is the oracle the repository read is held to.
      .prepare(
        `SELECT r.*, pl.lat AS place_lat, pl.lng AS place_lng,
                sd.date AS stay_start_date, ed.date AS stay_end_date,
                a.check_in AS stay_check_in, a.check_out AS stay_check_out,
                (SELECT MIN(r2.id) FROM reservations r2
                  WHERE r2.accommodation_id = a.id) AS stay_first_reservation_id,
                rd.date AS day_date, red.date AS end_day_date
         FROM reservations r
         LEFT JOIN places pl ON r.place_id = pl.id
         LEFT JOIN day_accommodations a ON r.accommodation_id = a.id
         LEFT JOIN days sd ON a.start_day_id = sd.id
         LEFT JOIN days ed ON a.end_day_id = ed.id
         LEFT JOIN days rd ON r.day_id = rd.id
         LEFT JOIN days red ON r.end_day_id = red.id
         WHERE r.trip_id = ? AND ${publicReservationSql('r')}`,
      )
      .all(trip.id);

    expect(typed[0]?.stay_check_in).toBe('15:00');
    expect(typed).toEqual(legacy);
  });

  // Inventory §18.2's documented, unfixed hole: `stay_first_reservation_id`'s
  // correlated MIN(r2.id) is NOT filtered by RV1, so a staged sibling booking
  // with a LOWER id than the live one still wins the slot. Parity keeps it —
  // pinned by name so nobody "fixes" it silently in a later change.
  it('CAL-CL2-HOLE-001: a staged sibling with a lower id wins stay_first_reservation_id over the live booking (documented, unfixed)', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-06-01', end_date: '2026-06-05' });
    const place = createPlace(testDb, trip.id);
    const days = await dayIds(trip.id);
    const stay = createDayAccommodation(testDb, trip.id, place.id, days[0].id, days[1].id);

    const stagedFirst = createReservation(testDb, trip.id, { title: 'Staged, lower id', type: 'hotel' });
    await setReservation(stagedFirst.id, { accommodation_id: String(stay.id), ingest_state: 'staged' });
    const liveSecond = createReservation(testDb, trip.id, { title: 'Live, higher id', type: 'hotel' });
    await setReservation(liveSecond.id, { accommodation_id: String(stay.id) });

    const typed = await reservationsRepo.listForCalendar(trip.id);
    const liveRow = typed.find((r) => r.id === liveSecond.id);

    // The visible (live) row reports the STAGED sibling's id as the "first
    // reservation" for its own stay — that is the hole, reproduced exactly.
    expect(liveRow?.stay_first_reservation_id).toBe(stagedFirst.id);
    expect(liveRow?.stay_first_reservation_id).not.toBe(liveSecond.id);
  });
});

describe('ReservationsRepository — CL4 (listCalendarStopsForDays)', () => {
  it('CAL-CL4-001: matches the legacy per-day statement, excluding a booked-night stop', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-06-01', end_date: '2026-06-03' });
    const day = createDay(testDb, trip.id, { date: '2026-06-01' });
    const place = createPlace(testDb, trip.id, { name: 'Museum' });
    await insertRow(await orm(), DayAssignments, { day: day.id, place: place.id, order_index: 0 });
    // A booked-night stop: linked to an accommodation, must be excluded.
    const hotelPlace = createPlace(testDb, trip.id, { name: 'Hotel' });
    const accId = await insertRow(await orm(), DayAccommodations, {
      trip: trip.id,
      place: hotelPlace.id,
      startDay: day.id,
      endDay: day.id,
    });
    await insertRow(await orm(), DayAssignments, {
      day: day.id,
      place: hotelPlace.id,
      order_index: 1,
      accommodation_id: accId,
    });

    const typed = await reservationsRepo.listCalendarStopsForDays([day.id]);
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
        SELECT da.*, p.name as place_name, p.address as place_address,
          p.lat as place_lat, p.lng as place_lng, p.duration_minutes as place_duration_minutes,
          COALESCE(da.assignment_time, p.place_time) as effective_time,
          COALESCE(da.assignment_end_time, p.end_time) as effective_end_time
        FROM day_assignments da
        JOIN places p ON da.place_id = p.id
        WHERE da.day_id = ?
          AND da.accommodation_id IS NULL
        ORDER BY da.order_index ASC, da.created_at ASC
      `,
      )
      .all(day.id);

    expect(typed.map((s) => s.place_name)).toEqual(['Museum']);
    expect(typed).toEqual(legacy);
  });
});

describe('ReservationsRepository — CL7 (listPublicStaysForCalendar)', () => {
  it('CAL-CL7-001: matches the legacy statement — a live-linked stay is included, a staged-only one is not', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-06-01', end_date: '2026-06-06' });
    const place = createPlace(testDb, trip.id);
    const days = await dayIds(trip.id);

    const liveLinked = createDayAccommodation(testDb, trip.id, place.id, days[0].id, days[1].id, {
      check_in: '15:00',
      check_out: '11:00',
    });
    const liveBooking = createReservation(testDb, trip.id, { title: 'Live', type: 'hotel' });
    await setReservation(liveBooking.id, { accommodation_id: String(liveLinked.id) });

    const stagedOnly = createDayAccommodation(testDb, trip.id, place.id, days[2].id, days[3].id);
    const stagedBooking = createReservation(testDb, trip.id, { title: 'Staged', type: 'hotel' });
    await setReservation(stagedBooking.id, { accommodation_id: String(stagedOnly.id), ingest_state: 'staged' });

    const typed = await reservationsRepo.listPublicStaysForCalendar(trip.id);
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT a.id, a.check_in, a.check_in_end, a.check_out,
             sd.date AS start_date, ed.date AS end_date,
             p.name AS place_name, p.address AS place_address, p.lat AS place_lat, p.lng AS place_lng,
             (SELECT r.title FROM reservations r
               WHERE r.accommodation_id = a.id AND ${publicReservationSql('r')}
               ORDER BY r.id ASC LIMIT 1) AS reservation_title
      FROM day_accommodations a
      LEFT JOIN days sd ON a.start_day_id = sd.id
      LEFT JOIN days ed ON a.end_day_id = ed.id
      LEFT JOIN places p ON a.place_id = p.id
      WHERE a.trip_id = ? AND ${publicStaySql('a')}
      ORDER BY a.id ASC
    `,
      )
      .all(trip.id);

    expect(typed.map((s) => s.id)).toEqual([liveLinked.id]);
    expect(typed).toEqual(legacy);
  });
});

// ── RS20 (listUpcomingForUser) ───────────────────────────────────────────

/** The exact legacy statement RS20's `ReservationsService.listUpcoming` ran, kept here as this test's own oracle (§ file header). */
const LEGACY_LIST_UPCOMING = `
    WITH visible_trips AS (
      SELECT t.id, t.title, t.cover_image
      FROM trips t
      LEFT JOIN trip_members tm ON tm.trip_id = t.id AND tm.user_id = ?
      WHERE (t.user_id = ? OR tm.user_id IS NOT NULL) AND t.is_archived = 0
    ),
    entries AS (
      SELECT r.id, r.trip_id, r.title, r.type, r.status, r.location,
             r.reservation_time, r.confirmation_number,
             tr.title as trip_title, tr.cover_image as trip_cover,
             d.date as day_date, p.name as place_name, p.image_url as place_image,
             CASE WHEN r.reservation_time GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*' THEN substr(r.reservation_time, 1, 10) ELSE d.date END as at_date,
             CASE WHEN r.reservation_time GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*' THEN substr(r.reservation_time, 12) ELSE r.reservation_time END as at_time
      FROM reservations r
      JOIN visible_trips tr ON tr.id = r.trip_id
      LEFT JOIN days d ON r.day_id = d.id
      LEFT JOIN places p ON r.place_id = p.id
      WHERE r.status != 'cancelled'
        AND COALESCE(r.type, '') != 'hotel'
        AND COALESCE(r.type, '') != 'transit'

      UNION ALL

      SELECT a.id, a.trip_id,
             COALESCE(p.name, (SELECT res.title FROM reservations res
                                WHERE CAST(res.accommodation_id AS INTEGER) = a.id
                                  AND res.status != 'cancelled'
                                ORDER BY res.id LIMIT 1), tr.title) as title,
             'checkin' as type, 'confirmed' as status, NULL as location,
             CASE WHEN a.check_in IS NOT NULL THEN d.date || 'T' || a.check_in END as reservation_time,
             a.confirmation as confirmation_number,
             tr.title as trip_title, tr.cover_image as trip_cover,
             d.date as day_date, p.name as place_name, p.image_url as place_image,
             d.date as at_date, a.check_in as at_time
      FROM day_accommodations a
      JOIN visible_trips tr ON tr.id = a.trip_id
      JOIN days d ON d.id = a.start_day_id
      LEFT JOIN places p ON p.id = a.place_id

      UNION ALL

      SELECT a.id, a.trip_id,
             COALESCE(p.name, (SELECT res.title FROM reservations res
                                WHERE CAST(res.accommodation_id AS INTEGER) = a.id
                                  AND res.status != 'cancelled'
                                ORDER BY res.id LIMIT 1), tr.title) as title,
             'checkout' as type, 'confirmed' as status, NULL as location,
             CASE WHEN a.check_out IS NOT NULL THEN d.date || 'T' || a.check_out END as reservation_time,
             a.confirmation as confirmation_number,
             tr.title as trip_title, tr.cover_image as trip_cover,
             d.date as day_date, p.name as place_name, p.image_url as place_image,
             d.date as at_date, a.check_out as at_time
      FROM day_accommodations a
      JOIN visible_trips tr ON tr.id = a.trip_id
      JOIN days d ON d.id = a.end_day_id
      LEFT JOIN places p ON p.id = a.place_id
    )
    SELECT id, trip_id, title, type, status, location, reservation_time,
           confirmation_number, trip_title, trip_cover, day_date, place_name, place_image
    FROM entries
    WHERE at_date IS NOT NULL
      AND (at_date > ? OR (at_date = ? AND COALESCE(at_time, '23:59') >= ?))
    ORDER BY at_date ASC, COALESCE(at_time, '00:00') ASC, id ASC
    LIMIT ?
  `;

function legacyListUpcoming(userId: number, today: string, nowHHMM: string, limit: number) {
  // test-sql-allow: the legacy statement is the oracle the repository read is held to.
  return testDb.prepare(LEGACY_LIST_UPCOMING).all(userId, userId, today, today, nowHHMM, limit);
}

describe('ReservationsRepository — RS20 (listUpcomingForUser)', () => {
  /**
   * One fully seeded world — owner + member + archived + hidden trips, a
   * mixed-type/mixed-shape reservation set, a NAMED stay and a NAMELESS one
   * whose only booking's `accommodation_id` is written in the `"<id>.0"`
   * TEXT shape (§18.1) — run against 5 `(today, now, limit)` settings,
   * including one where two bookings tie on `(at_date, at_time)` so the
   * `id` tiebreak is load-bearing, and one where `limit` cuts the result
   * strictly inside that tie.
   */
  it('RS20-001..005: matches the legacy CTE statement across 5 (today, now, limit) settings, incl. the cross-table id tiebreak and a limit straddling a tie', async () => {
    const { user: owner } = createUser(testDb);
    const { user: stranger } = createUser(testDb);

    const tripOwned = createTrip(testDb, owner.id, { start_date: '2999-01-01', end_date: '2999-01-10' });
    const tripMember = createTrip(testDb, stranger.id, { start_date: '2999-01-01', end_date: '2999-01-05' });
    addTripMember(testDb, tripMember.id, owner.id);
    const tripHidden = createTrip(testDb, stranger.id, { start_date: '2999-01-01', end_date: '2999-01-05' });
    const tripArchived = createTrip(testDb, owner.id, { start_date: '2999-01-01', end_date: '2999-01-05' });
    await setTrip(tripArchived.id, { is_archived: 1 });

    // Two bookings on the SAME date+time — the (at_date, at_time) tie the
    // `id ASC` tiebreak has to resolve, in insertion (id) order.
    const tie1 = createReservation(testDb, tripOwned.id, { title: 'Tie 1' });
    await setReservation(tie1.id, { reservation_time: '2999-01-02T09:00:00' });
    const tie2 = createReservation(testDb, tripOwned.id, { title: 'Tie 2' });
    await setReservation(tie2.id, { reservation_time: '2999-01-02T09:00:00' });

    // Excluded regardless of settings: cancelled, and a hotel-typed booking
    // dated through its own reservation_time.
    const cancelled = createReservation(testDb, tripOwned.id, { title: 'Cancelled' });
    await setReservation(cancelled.id, { reservation_time: '2999-01-03T10:00:00', status: 'cancelled' });
    const hotelDated = createReservation(testDb, tripOwned.id, { title: 'Hotel dated', type: 'hotel' });
    await setReservation(hotelDated.id, { reservation_time: '2999-01-03T10:00:00' });
    // A planned public-transit journey is routing, not a booking; a booked train is one.
    const transit = createReservation(testDb, tripOwned.id, { title: 'Metro A to B', type: 'transit' });
    await setReservation(transit.id, { reservation_time: '2999-01-03T11:00:00', status: 'confirmed' });
    const train = createReservation(testDb, tripOwned.id, { title: 'Booked train', type: 'train' });
    await setReservation(train.id, { reservation_time: '2999-01-03T12:00:00', status: 'confirmed' });

    // A NULL-typed booking still shows.
    const untyped = createReservation(testDb, tripOwned.id, { title: 'Untyped' });
    await setReservation(untyped.id, { type: null, reservation_time: '2999-01-04T10:00:00' });

    // A bare-clock, day-anchored booking.
    const bareDay = createDay(testDb, tripOwned.id, { date: '2999-01-05' });
    const bareClock = createReservation(testDb, tripOwned.id, { title: 'Bare clock', day_id: bareDay.id });
    await setReservation(bareClock.id, { reservation_time: '07:00' });

    // A member-trip booking.
    const memberBooking = createReservation(testDb, tripMember.id, { title: 'Member trip' });
    await setReservation(memberBooking.id, { reservation_time: '2999-01-02T12:00:00' });

    // A hidden-trip booking — must never appear.
    const hiddenBooking = createReservation(testDb, tripHidden.id, { title: 'Hidden' });
    await setReservation(hiddenBooking.id, { reservation_time: '2999-01-02T08:00:00' });

    // A NAMED stay (check-in + check-out).
    const namedPlace = createPlace(testDb, tripOwned.id, { name: 'The Plaza' });
    const days = await dayIds(tripOwned.id);
    createDayAccommodation(testDb, tripOwned.id, namedPlace.id, days[5].id, days[7].id, {
      check_in: '15:00',
      check_out: '11:00',
    });

    // A NAMELESS stay whose only (cancelled-excluded) booking's
    // accommodation_id is stored in the "<id>.0" TEXT shape.
    const namelessId = await insertRow(await orm(), DayAccommodations, {
      trip: tripOwned.id,
      place: null,
      startDay: days[5].id,
      endDay: days[6].id,
      check_in: '16:00',
      check_out: '10:00',
    });
    const namelessBooking = createReservation(testDb, tripOwned.id, { title: 'Hotel Ibis', type: 'hotel' });
    await setReservation(namelessBooking.id, { accommodation_id: `${namelessId}.0` });

    const settings: [string, string, number][] = [
      ['2000-01-01', '2000-01-01T00:00', 20], // far past today — everything future-dated shows
      ['2999-01-02', '2999-01-02T09:00', 20], // exactly on the tie moment — inclusive, both tie rows stay
      ['2999-01-02', '2999-01-02T09:01', 20], // one minute past the tie — both tie rows drop
      ['2000-01-01', '2000-01-01T00:00', 3], // straddles the tie: limit cuts inside the tied pair
      ['2999-06-01', '2999-06-01T00:00', 20], // nothing left — empty result
    ];

    for (const [today, nowHHMM, limit] of settings) {
      const typed = await reservationsRepo.listUpcomingForUser(owner.id, today, nowHHMM, limit);
      const legacy = legacyListUpcoming(owner.id, today, nowHHMM, limit);
      expect(typed).toEqual(legacy);
    }

    // Sanity — the first setting's shape is what the matrix above claims:
    // hidden/archived excluded, cancelled/hotel-dated excluded, the tie
    // resolves in id order, the nameless stay reads its title off the
    // linked "<id>.0" booking.
    const baseline = await reservationsRepo.listUpcomingForUser(owner.id, '2000-01-01', '2000-01-01T00:00', 20);
    expect(baseline.map((r) => r.title)).not.toContain('Hidden');
    expect(baseline.map((r) => r.title)).not.toContain('Metro A to B');
    expect(baseline.map((r) => r.title)).toContain('Booked train');
    const tieIndex = baseline.findIndex((r) => r.title === 'Tie 1');
    expect(baseline[tieIndex + 1]?.title).toBe('Tie 2');
    expect(baseline.some((r) => r.title === 'Hotel Ibis')).toBe(true);
  });
});

// ─────────────────────────────────────────────────────────────────────────────
// M3 (Plan 3d Task 7 whole-plan review): AC37/AC40 reverted to the legacy
// REAL-bound compare (parity is law — the widening had no reason to exist
// once H1 makes every write in this cluster store the legacy `'<id>.0'`
// shape). Pinned against the SAME five-shape matrix the review measured.
// ─────────────────────────────────────────────────────────────────────────────

describe('ReservationsRepository — AC37/AC40 (listIdMetadataByStay/listIdsByStay), M3', () => {
  it('AC37/AC40 match the legacy REAL-bound compare — only the "<id>.0" TEXT shape resolves, not the bare integer, a trailing/leading space or a prefix-numeric value', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-09-01', end_date: '2026-09-05' });
    const place = createPlace(testDb, trip.id);
    const days = await dayIds(trip.id);
    const stay = createDayAccommodation(testDb, trip.id, place.id, days[0].id, days[1].id);

    // `shapes[1]` ('<id>.0') is the ONLY one the legacy `Number(id)` bind —
    // and this repository's own reverted compare — ever matched.
    const shapes = [`${stay.id}`, `${stay.id}.0`, `${stay.id} `, `${stay.id}abc`, ` ${stay.id}`];
    const ids: number[] = [];
    for (const [i, shape] of shapes.entries()) {
      const r = createReservation(testDb, trip.id, { title: `Link ${i}`, type: 'hotel' });
      await setReservation(r.id, { accommodation_id: shape });
      ids.push(r.id);
    }

    const typedMeta = await reservationsRepo.listIdMetadataByStay(stay.id);
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacyMeta = testDb
      .prepare('SELECT id, metadata FROM reservations WHERE accommodation_id = ?')
      .all(stay.id) as { id: number; metadata: string | null }[];
    expect(typedMeta).toEqual(legacyMeta);
    expect(typedMeta.map((r) => r.id)).toEqual([ids[1]]);

    const typedIds = await reservationsRepo.listIdsByStay(stay.id);
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacyIds = testDb.prepare('SELECT id FROM reservations WHERE accommodation_id = ?').all(stay.id) as {
      id: number;
    }[];
    expect(typedIds).toEqual(legacyIds);
    expect(typedIds.map((r) => r.id)).toEqual([ids[1]]);
  });
});

// ─────────────────────────────────────────────────────────────────────────────
// Plan 3d Task 7 whole-plan review, item 9 ("missing parity tests"): Task 2's
// own `ReservationsRepository` reads (`listForTrip`, `findWithJoins`,
// `findInTrip`, `listResyncCandidates`, `listForRestamp`) had no
// repository-level parity test — they rested on the reservations.service
// REAL-LEGACY suite only. ONE seeded world, one `toEqual(<legacy raw>)`
// test per method.
// ─────────────────────────────────────────────────────────────────────────────

describe('ReservationsRepository — Task 2 reads (listForTrip / findWithJoins / findInTrip / listResyncCandidates / listForRestamp)', () => {
  const legacyJoinedSql = `
    SELECT r.*, d.day_number as day_number, p.name as place_name, ap.place_id as accommodation_place_id,
      acc_p.name as accommodation_name, ap.start_day_id as accommodation_start_day_id, ap.end_day_id as accommodation_end_day_id,
      ap.check_in as accommodation_check_in, ap.check_in_end as accommodation_check_in_end, ap.check_out as accommodation_check_out
    FROM reservations r
    LEFT JOIN days d ON d.id = r.day_id
    LEFT JOIN places p ON p.id = r.place_id
    LEFT JOIN day_accommodations ap ON ap.id = r.accommodation_id
    LEFT JOIN places acc_p ON acc_p.id = ap.place_id`;

  const seed = async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-09-01', end_date: '2026-09-05' });
    const place = createPlace(testDb, trip.id, { name: 'Museum' });
    const days = await dayIds(trip.id);
    const hotelPlace = createPlace(testDb, trip.id, { name: 'Grand Hotel' });
    const stay = createDayAccommodation(testDb, trip.id, hotelPlace.id, days[0].id, days[1].id);

    // A dated, non-hotel booking — a restamp/resync candidate.
    const dinner = createReservation(testDb, trip.id, { title: 'Dinner', type: 'restaurant', day_id: days[0].id });
    await setReservation(dinner.id, { reservation_time: '2026-09-01T19:00' });

    // A hotel booking linked to the stay above — the joined projection's place/accommodation columns.
    const hotel = createReservation(testDb, trip.id, { title: 'Grand Hotel Stay', type: 'hotel', day_id: days[0].id });
    await setReservation(hotel.id, { accommodation_id: `${stay.id}.0`, place: place.id });

    return { trip, days, place, hotelPlace, stay, dinner, hotel };
  };

  it('RS18 listForTrip — matches the legacy joined statement, ordered by reservation_time then created_at', async () => {
    const { trip } = await seed();
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(`${legacyJoinedSql} WHERE r.trip_id = ? ORDER BY r.reservation_time ASC, r.created_at ASC`)
      .all(trip.id);
    const typed = await reservationsRepo.listForTrip(trip.id);
    expect(typed).toEqual(legacy);
  });

  it('RR1 findWithJoins — matches the legacy joined statement for one row, place/accommodation columns populated', async () => {
    const { trip, hotel, stay, hotelPlace } = await seed();
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb.prepare(`${legacyJoinedSql} WHERE r.id = ?`).get(hotel.id);
    const typed = await reservationsRepo.findWithJoins(hotel.id, trip.id);
    expect(typed).toEqual(legacy);
    expect(typed).toMatchObject({
      accommodation_place_id: hotelPlace.id,
      accommodation_name: hotelPlace.name,
      accommodation_start_day_id: stay.start_day_id,
    });
  });

  it('RR1 findWithJoins — a booking of another trip reads as missing', async () => {
    const { trip, hotel } = await seed();
    const { user } = createUser(testDb);
    const other = createTrip(testDb, user.id);
    expect(await reservationsRepo.findWithJoins(hotel.id, other.id)).toBeUndefined();
    expect(await reservationsRepo.findWithJoins(hotel.id, String(trip.id))).toMatchObject({ id: hotel.id });
  });

  it('RS35 findInTrip — matches the legacy statement, scoped by trip', async () => {
    const { trip, dinner } = await seed();
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb.prepare('SELECT * FROM reservations WHERE id = ? AND trip_id = ?').get(dinner.id, trip.id);
    expect(await reservationsRepo.findInTrip(dinner.id, trip.id)).toEqual(legacy);
    expect(await reservationsRepo.findInTrip(999999, trip.id)).toBeUndefined();
  });

  it('RS12 listResyncCandidates — matches the legacy statement (dated, non-hotel-or-unlinked)', async () => {
    const { trip, dinner } = await seed();
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT id, reservation_time, reservation_end_time, day_id, end_day_id FROM reservations
      WHERE trip_id = ? AND (type != 'hotel' OR accommodation_id IS NULL) AND reservation_time IS NOT NULL`,
      )
      .all(trip.id);
    const typed = await reservationsRepo.listResyncCandidates(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.map((r) => r.id)).toEqual([dinner.id]);
  });

  it('DY14 listForRestamp — matches the legacy statement, every reservation of the trip regardless of type', async () => {
    const { trip, dinner, hotel } = await seed();
    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        'SELECT id, day_id, end_day_id, reservation_time, reservation_end_time FROM reservations WHERE trip_id = ?',
      )
      .all(trip.id);
    const typed = await reservationsRepo.listForRestamp(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.map((r) => r.id).sort((a, b) => a - b)).toEqual([dinner.id, hotel.id].sort((a, b) => a - b));
  });
});

// ---------------------------------------------------------------------------
// Plan 3h Task 7 review, M1 coverage — the AirTrail leaf's writes/reads,
// untested at the repository level (the airtrail-link/sync service tests
// stub this repository).
// ---------------------------------------------------------------------------

describe('ReservationsRepository — AirTrail leaf (ATL1/ATL2/ATL4, airports)', () => {
  it('ATL1 linkAirtrailSingleFlight — stamps every AirTrail column and switches sync on, matching the legacy statement', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id);
    const res = createReservation(testDb, trip.id, { title: 'Flight', type: 'flight' });

    await reservationsRepo.linkAirtrailSingleFlight(res.id, 'AT-42', user.id, 'hash-1', '2026-09-24T10:00:00Z');

    const stored = await reservationRow(res.id);
    const row = {
      external_source: stored.external_source,
      external_id: stored.external_id,
      external_owner_user_id: stored.external_owner_user_id,
      sync_enabled: stored.sync_enabled,
      external_hash: stored.external_hash,
      external_synced_at: stored.external_synced_at,
    };
    expect(row).toEqual({
      external_source: 'airtrail',
      external_id: 'AT-42',
      external_owner_user_id: user.id,
      sync_enabled: 1,
      external_hash: 'hash-1',
      external_synced_at: '2026-09-24T10:00:00Z',
    });
  });

  it('ATL2 setAirtrailSyncDisabled — turns sync_enabled off, matching the legacy statement, and leaves every other column alone', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id);
    const res = createReservation(testDb, trip.id, { title: 'Flight', type: 'flight' });
    await reservationsRepo.linkAirtrailSingleFlight(res.id, 'AT-42', user.id, 'hash-1', '2026-09-24T10:00:00Z');

    await reservationsRepo.setAirtrailSyncDisabled(res.id);

    const { sync_enabled, external_id } = await reservationRow(res.id);
    expect({ sync_enabled, external_id }).toEqual({ sync_enabled: 0, external_id: 'AT-42' });
  });

  it("ATL4 findAirtrailLinked — matches the legacy statement, scoped to external_source = 'airtrail' (a plain flight misses)", async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id);
    const linked = createReservation(testDb, trip.id, { title: 'Flight', type: 'flight' });
    await reservationsRepo.linkAirtrailSingleFlight(linked.id, 'AT-7', user.id, 'hash-2', '2026-09-24T11:00:00Z');
    const plain = createReservation(testDb, trip.id, { title: 'Dinner', type: 'restaurant' });

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        "SELECT id, trip_id, external_id, external_owner_user_id, sync_enabled FROM reservations WHERE id = ? AND external_source = 'airtrail'",
      )
      .get(linked.id);
    expect(await reservationsRepo.findAirtrailLinked(linked.id)).toEqual(legacy);
    expect(await reservationsRepo.findAirtrailLinked(plain.id)).toBeUndefined();
  });

  it('markNeedsReview (airports) — sets needs_review, matching the legacy statement', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id);
    const res = createReservation(testDb, trip.id, { title: 'Flight', type: 'flight' });
    expect((await reservationRow(res.id)).needs_review).toBe(0);

    await reservationsRepo.markNeedsReview(res.id);

    expect((await reservationRow(res.id)).needs_review).toBe(1);
  });
});

// ─────────────────────────────────────────────────────────────────────────────
// Plan 4 Task 8b-2, item 1 (3d Task 7 review's "share/public-api ×8 reads",
// `phase3d-reports/task-7-review.md`'s "Parity tests: read models with no
// full-key `toEqual(<legacy raw>)`" list): `share.service.ts`'s 4 reads
// (SH-family: listEndpointsForShare, listDayPositionsForShare,
// listPublicForShare, listPublicAccommodationsForShare) and
// `public-api.service.ts`'s 4 reads (listScheduledForPublicApi,
// listUnscheduledForPublicApi, listAccommodationsForPublicApi,
// listUnplannedPlacesForPublicApi) — service-level coverage only until now.
// ONE seeded world per describe block, `toEqual(<legacy raw>)` per method.
// ─────────────────────────────────────────────────────────────────────────────

const insertReservationEndpoint = async (
  reservationId: number,
  role: string,
  sequence: number,
  overrides: Partial<{
    name: string;
    code: string | null;
    lat: number;
    lng: number;
    timezone: string | null;
    local_time: string | null;
    local_date: string | null;
  }> = {},
) => {
  await insertRow(await orm(), ReservationEndpoints, {
    reservation: reservationId,
    role,
    sequence,
    name: overrides.name ?? `${role} endpoint`,
    code: overrides.code ?? null,
    lat: overrides.lat ?? 48.0,
    lng: overrides.lng ?? 11.0,
    timezone: overrides.timezone ?? null,
    local_time: overrides.local_time ?? null,
    local_date: overrides.local_date ?? null,
  });
};

const insertDayPosition = async (reservationId: number, dayId: number, position: number) => {
  await insertRow(await orm(), ReservationDayPositions, { reservation: reservationId, day: dayId, position });
};

describe('ReservationsRepository — share.service.ts reads (SH-family)', () => {
  it('listEndpointsForShare — matches the legacy JOIN, ordered by reservation then sequence, scoped by trip', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id);
    const other = createTrip(testDb, user.id);
    const flight = createReservation(testDb, trip.id, { title: 'Flight', type: 'flight' });
    await insertReservationEndpoint(flight.id, 'from', 1, {
      name: 'JFK',
      code: 'JFK',
      lat: 40.6,
      lng: -73.7,
      timezone: 'America/New_York',
      local_time: '10:00',
      local_date: '2026-09-01',
    });
    await insertReservationEndpoint(flight.id, 'to', 0, {
      name: 'MUC',
      code: null,
      lat: 48.35,
      lng: 11.78,
      timezone: null,
      local_time: null,
      local_date: null,
    });
    const foreign = createReservation(testDb, other.id, { title: 'Foreign', type: 'flight' });
    await insertReservationEndpoint(foreign.id, 'from', 0, { name: 'LHR' });

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT e.reservation_id, e.role, e.sequence, e.name, e.code, e.lat, e.lng, e.timezone, e.local_date, e.local_time
      FROM reservation_endpoints e JOIN reservations r ON r.id = e.reservation_id
      WHERE r.trip_id = ? ORDER BY e.reservation_id ASC, e.sequence ASC`,
      )
      .all(trip.id);

    const typed = await reservationsRepo.listEndpointsForShare(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.map((e) => e.sequence)).toEqual([0, 1]);
  });

  it('listDayPositionsForShare — matches the legacy JOIN, scoped by trip', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-09-01', end_date: '2026-09-03' });
    const other = createTrip(testDb, user.id);
    const days = await dayIds(trip.id);
    const res = createReservation(testDb, trip.id, { title: 'Multi-day rental', type: 'car_rental' });
    await insertDayPosition(res.id, days[0].id, 0);
    await insertDayPosition(res.id, days[1].id, 1);
    const foreignRes = createReservation(testDb, other.id, { title: 'Foreign', type: 'car_rental' });
    const foreignDay = {
      id: await insertRow(await orm(), Days, { trip: other.id, day_number: 1, date: '2026-01-01' }),
    };
    await insertDayPosition(foreignRes.id, foreignDay.id, 0);

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT rdp.reservation_id, rdp.day_id, rdp.position
      FROM reservation_day_positions rdp JOIN reservations r ON r.id = rdp.reservation_id
      WHERE r.trip_id = ?`,
      )
      .all(trip.id);

    const typed = await reservationsRepo.listDayPositionsForShare(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.length).toBe(2);
  });

  it('listPublicForShare — matches the legacy statement, live bookings only, staged excluded', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-09-01', end_date: '2026-09-03' });
    const days = await dayIds(trip.id);
    const place = createPlace(testDb, trip.id);
    const live = createReservation(testDb, trip.id, { title: 'Live', type: 'flight', day_id: days[0].id });
    await setReservation(live.id, {
      endDay: days[1].id,
      place: place.id,
      accommodation_id: null,
      status: 'confirmed',
      location: 'Gate 4',
      reservation_time: '2026-09-01T10:00',
      reservation_end_time: '2026-09-01T12:00',
      notes: 'note',
      url: 'https://example.com',
      metadata: '{"k":"v"}',
    });
    const staged = createReservation(testDb, trip.id, { title: 'Staged', type: 'flight' });
    await setReservation(staged.id, { ingest_state: 'staged' });

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT id, trip_id, day_id, end_day_id, place_id, accommodation_id, title, type, status, location,
             reservation_time, reservation_end_time, notes, url, metadata, created_at
      FROM reservations WHERE trip_id = ? AND ${publicReservationSql('reservations')}
      ORDER BY reservation_time ASC`,
      )
      .all(trip.id);

    const typed = await reservationsRepo.listPublicForShare(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.map((r) => r.id)).toEqual([live.id]);
  });

  it('listPublicAccommodationsForShare — matches the legacy statement, only stays with a public booking (or none) are included', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-09-01', end_date: '2026-09-05' });
    const days = await dayIds(trip.id);
    const place = createPlace(testDb, trip.id, { name: 'Grand Hotel' });
    const noBooking = createDayAccommodation(testDb, trip.id, place.id, days[0].id, days[1].id, {
      check_in: '15:00',
      check_out: '11:00',
    });
    const liveLinked = createDayAccommodation(testDb, trip.id, place.id, days[2].id, days[3].id);
    const liveBooking = createReservation(testDb, trip.id, { title: 'Live', type: 'hotel' });
    await setReservation(liveBooking.id, { accommodation_id: String(liveLinked.id) });
    const stagedOnly = createDayAccommodation(testDb, trip.id, place.id, days[3].id, days[4].id);
    const stagedBooking = createReservation(testDb, trip.id, { title: 'Staged', type: 'hotel' });
    await setReservation(stagedBooking.id, { accommodation_id: String(stagedOnly.id), ingest_state: 'staged' });

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT a.id, a.trip_id, a.place_id, a.start_day_id, a.end_day_id, a.check_in, a.check_in_end, a.check_out, a.notes,
             p.name as place_name, p.address as place_address, p.lat as place_lat, p.lng as place_lng
      FROM day_accommodations a JOIN places p ON p.id = a.place_id
      WHERE a.trip_id = ? AND ${publicStaySql('a')}`,
      )
      .all(trip.id);

    const typed = await reservationsRepo.listPublicAccommodationsForShare(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.map((s) => s.id).sort((a, b) => a - b)).toEqual([noBooking.id, liveLinked.id].sort((a, b) => a - b));
  });

  it('listPublicStayPlaceIdsForShare — matches the legacy DISTINCT read, hand-added and live-booked stays in, staged-booked stays out', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id);
    const other = createTrip(testDb, user.id);
    const day = createDay(testDb, trip.id);
    const otherDay = createDay(testDb, other.id);
    const handAdded = createPlace(testDb, trip.id, { name: 'Hand added' });
    const live = createPlace(testDb, trip.id, { name: 'Live booking' });
    const staged = createPlace(testDb, trip.id, { name: 'Staged booking' });
    const elsewhere = createPlace(testDb, other.id, { name: 'Other trip' });
    createDayAccommodation(testDb, trip.id, handAdded.id, day.id, day.id);
    // Two nights at the same place: DISTINCT folds them into one id.
    createDayAccommodation(testDb, trip.id, handAdded.id, day.id, day.id);
    const liveStay = createDayAccommodation(testDb, trip.id, live.id, day.id, day.id);
    const stagedStay = createDayAccommodation(testDb, trip.id, staged.id, day.id, day.id);
    createDayAccommodation(testDb, other.id, elsewhere.id, otherDay.id, otherDay.id);
    const liveRes = createReservation(testDb, trip.id, { type: 'hotel', title: 'Live' });
    const stagedRes = createReservation(testDb, trip.id, { type: 'hotel', title: 'Staged' });
    await setReservation(liveRes.id, { accommodation_id: String(liveStay.id) });
    await setReservation(stagedRes.id, { accommodation_id: String(stagedStay.id), ingest_state: 'staged' });

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = (
      testDb
        .prepare(
          `
      SELECT DISTINCT a.place_id FROM day_accommodations a
      WHERE a.trip_id = ? AND ${publicStaySql('a')}`,
        )
        .all(trip.id) as { place_id: number }[]
    ).map((r) => r.place_id);

    const typed = await reservationsRepo.listPublicStayPlaceIdsForShare(trip.id);
    expect([...typed].sort((a, b) => a - b)).toEqual([...legacy].sort((a, b) => a - b));
    expect([...typed].sort((a, b) => a - b)).toEqual([handAdded.id, live.id].sort((a, b) => a - b));
  });
});

describe('ReservationsRepository — public-api.service.ts reads', () => {
  it('listScheduledForPublicApi — matches the legacy statement, scoped by trip, ordered by day then time', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-09-01', end_date: '2026-09-03' });
    const days = await dayIds(trip.id);
    const scheduled = createReservation(testDb, trip.id, { title: 'Dinner', type: 'restaurant', day_id: days[0].id });
    await setReservation(scheduled.id, {
      reservation_time: '2026-09-01T19:00',
      location: 'Downtown',
      notes: 'window seat',
    });
    createReservation(testDb, trip.id, { title: 'Unscheduled', type: 'restaurant' });

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT day_id, type, title, location, reservation_time, reservation_end_time, status, notes
      FROM reservations WHERE trip_id = ? AND day_id IS NOT NULL
      ORDER BY day_id ASC, reservation_time ASC`,
      )
      .all(trip.id);

    const typed = await reservationsRepo.listScheduledForPublicApi(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.length).toBe(1);
  });

  it('listUnscheduledForPublicApi — matches the legacy statement, day_id IS NULL only', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-09-01', end_date: '2026-09-02' });
    const days = await dayIds(trip.id);
    const unscheduled = createReservation(testDb, trip.id, { title: 'Unscheduled', type: 'restaurant' });
    await setReservation(unscheduled.id, { reservation_time: null, location: null, notes: null });
    createReservation(testDb, trip.id, { title: 'Scheduled', type: 'restaurant', day_id: days[0].id });

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT type, title, location, reservation_time, reservation_end_time, status, notes
      FROM reservations WHERE trip_id = ? AND day_id IS NULL
      ORDER BY reservation_time ASC, id ASC`,
      )
      .all(trip.id);

    const typed = await reservationsRepo.listUnscheduledForPublicApi(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.map((r) => r.title)).toEqual(['Unscheduled']);
  });

  it('listAccommodationsForPublicApi — matches the legacy LEFT JOIN statement, ordered by start date', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-09-01', end_date: '2026-09-05' });
    const days = await dayIds(trip.id);
    const place = createPlace(testDb, trip.id, { name: 'Grand Hotel' });
    createDayAccommodation(testDb, trip.id, place.id, days[2].id, days[3].id, {
      check_in: '15:00',
      check_out: '11:00',
    });
    createDayAccommodation(testDb, trip.id, place.id, days[0].id, days[1].id);

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT p.name, p.address, p.lat, p.lng, ds.date as start_date, de.date as end_date, a.check_in, a.check_out, a.notes
      FROM day_accommodations a
      LEFT JOIN places p ON p.id = a.place_id
      LEFT JOIN days ds ON ds.id = a.start_day_id
      LEFT JOIN days de ON de.id = a.end_day_id
      WHERE a.trip_id = ? ORDER BY ds.date ASC`,
      )
      .all(trip.id) as { start_date: string | null }[];

    const typed = await reservationsRepo.listAccommodationsForPublicApi(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.map((a) => a.start_date)).toEqual([legacy[0].start_date, legacy[1].start_date]);
  });

  it('listUnplannedPlacesForPublicApi — matches the legacy NOT EXISTS statement, excluding assigned and staying places', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-09-01', end_date: '2026-09-02' });
    const days = await dayIds(trip.id);
    const unplanned = createPlace(testDb, trip.id, { name: 'Unplanned Cafe' });
    const assigned = createPlace(testDb, trip.id, { name: 'Assigned Museum' });
    createDayAssignment(testDb, days[0].id, assigned.id);
    const staying = createPlace(testDb, trip.id, { name: 'Hotel' });
    createDayAccommodation(testDb, trip.id, staying.id, days[0].id, days[1].id);

    // test-sql-allow: the legacy statement is the oracle the repository read is held to.
    const legacy = testDb
      .prepare(
        `
      SELECT p.name, p.address, p.lat, p.lng, p.place_time, p.end_time, p.duration_minutes, p.notes, p.transport_mode, c.name as category
      FROM places p LEFT JOIN categories c ON c.id = p.category_id
      WHERE p.trip_id = ?
        AND NOT EXISTS (SELECT 1 FROM day_assignments da WHERE da.place_id = p.id)
        AND NOT EXISTS (SELECT 1 FROM day_accommodations a WHERE a.place_id = p.id)
      ORDER BY p.created_at ASC, p.id ASC`,
      )
      .all(trip.id);

    const typed = await reservationsRepo.listUnplannedPlacesForPublicApi(trip.id);
    expect(typed).toEqual(legacy);
    expect(typed.map((p) => p.name)).toEqual([unplanned.name]);
  });
});

describe('ReservationsRepository — confirmIfPending', () => {
  it('RES-CONFIRM-001: confirms a pending booking and leaves any other status alone', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { start_date: '2026-06-01', end_date: '2026-06-05' });
    const pending = createReservation(testDb, trip.id, { title: 'Pending hotel', type: 'hotel' });
    await setReservation(pending.id, { status: 'pending' });
    const cancelled = createReservation(testDb, trip.id, { title: 'Cancelled hotel', type: 'hotel' });
    await setReservation(cancelled.id, { status: 'cancelled' });

    await reservationsRepo.confirmIfPending(pending.id);
    await reservationsRepo.confirmIfPending(cancelled.id);

    expect((await findRow(await orm(), Reservations, { id: pending.id }))?.status).toBe('confirmed');
    expect((await findRow(await orm(), Reservations, { id: cancelled.id }))?.status).toBe('cancelled');
  });
});

describe('ReservationsRepository — findPassContext', () => {
  it('RES-PASS-001: reads the trip title and the place of the booking, else of its stay, within the trip only', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { title: 'Japan 2027', start_date: '2027-04-01', end_date: '2027-04-06' });
    const day1 = createDay(testDb, trip.id, { date: '2027-04-01' });
    const day2 = createDay(testDb, trip.id, { date: '2027-04-02' });
    const restaurant = createPlace(testDb, trip.id, { name: 'Gion Karyo', lat: 35.0, lng: 135.77 });
    const hotel = createPlace(testDb, trip.id, { name: 'Hotel Granvia Kyoto', lat: 34.985, lng: 135.758 });
    const stay = createDayAccommodation(testDb, trip.id, hotel.id, day1.id, day2.id);

    const dinner = createReservation(testDb, trip.id, { title: 'Dinner', type: 'restaurant' });
    await setReservation(dinner.id, { place: restaurant.id });
    const room = createReservation(testDb, trip.id, { title: 'Room', type: 'hotel' });
    await setReservation(room.id, { accommodation_id: String(stay.id) });
    const bare = createReservation(testDb, trip.id, { title: 'Museum', type: 'event' });

    expect(await reservationsRepo.findPassContext(dinner.id, trip.id)).toMatchObject({
      trip_title: 'Japan 2027',
      place_lat: 35.0,
      place_lng: 135.77,
      place_name: 'Gion Karyo',
    });
    expect(await reservationsRepo.findPassContext(room.id, trip.id)).toMatchObject({
      place_lat: 34.985,
      place_name: 'Hotel Granvia Kyoto',
    });
    expect(await reservationsRepo.findPassContext(bare.id, trip.id)).toEqual({
      trip_title: 'Japan 2027',
      place_lat: null,
      place_lng: null,
      place_name: null,
      day_date: null,
      end_day_date: null,
      stay_start_date: null,
      stay_end_date: null,
      stay_check_in: null,
      stay_check_out: null,
    });
    const other = createTrip(testDb, user.id, { title: 'Elsewhere' });
    expect(await reservationsRepo.findPassContext(dinner.id, other.id)).toBeUndefined();
  });

  it('RES-PASS-002: reads the dates of the linked days and of a hotel stay, with its check-in and check-out', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id, { title: 'Valencia', start_date: '2027-01-08', end_date: '2027-01-11' });
    const first = createDay(testDb, trip.id, { date: '2027-01-08' });
    const last = createDay(testDb, trip.id, { date: '2027-01-11' });
    const hotel = createPlace(testDb, trip.id, { name: 'Only You Hotel Valencia', lat: 39.47, lng: -0.376 });
    const stay = createDayAccommodation(testDb, trip.id, hotel.id, first.id, last.id);
    await updateRows(await orm(), DayAccommodations, { id: stay.id }, { check_in: '15:00', check_out: '10:00' });

    const room = createReservation(testDb, trip.id, { title: 'Only You', type: 'hotel' });
    await setReservation(room.id, { accommodation_id: String(stay.id) });
    const dinner = createReservation(testDb, trip.id, { title: 'Dinner', type: 'restaurant' });
    await setReservation(dinner.id, { day: first.id, endDay: last.id });

    expect(await reservationsRepo.findPassContext(room.id, trip.id)).toMatchObject({
      stay_start_date: '2027-01-08',
      stay_end_date: '2027-01-11',
      stay_check_in: '15:00',
      stay_check_out: '10:00',
      day_date: null,
    });
    expect(await reservationsRepo.findPassContext(dinner.id, trip.id)).toMatchObject({
      day_date: '2027-01-08',
      end_day_date: '2027-01-11',
      stay_start_date: null,
    });
  });
});

describe('ReservationsRepository — the origin of an imported booking', () => {
  const baseRow = (tripId: number) => ({
    trip_id: tripId,
    day_id: null,
    end_day_id: null,
    place_id: null,
    assignment_id: null,
    title: 'LH 716',
    reservation_time: null,
    reservation_end_time: null,
    location: null,
    confirmation_number: 'ABC123',
    notes: null,
    url: null,
    status: 'pending',
    type: 'flight',
    accommodation_id: null,
    metadata: null,
    needs_review: 0,
  });

  it('RES-ORIGIN-001: a hand-made booking keeps the column defaults, no origin and sync on', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id);
    const id = await reservationsRepo.insertReservation(baseRow(trip.id));
    // test-sql-allow: reads back the raw columns the insert left to their defaults.
    const row = testDb
      .prepare('SELECT external_source, external_id, sync_enabled FROM reservations WHERE id = ?')
      .get(id);
    expect(row).toEqual({ external_source: null, external_id: null, sync_enabled: 1 });
  });

  it('RES-ORIGIN-002: an imported booking records its origin and is found by it in its own trip only', async () => {
    const { user } = createUser(testDb);
    const trip = createTrip(testDb, user.id);
    const other = createTrip(testDb, user.id);
    const id = await reservationsRepo.insertReservation({
      ...baseRow(trip.id),
      external_source: 'import',
      external_id: 'ABC123|flight|2026-09-01|FRA-NRT',
      sync_enabled: 0,
    });
    // test-sql-allow: reads back the raw origin columns the insert named.
    const row = testDb
      .prepare('SELECT external_source, external_id, sync_enabled FROM reservations WHERE id = ?')
      .get(id);
    expect(row).toEqual({
      external_source: 'import',
      external_id: 'ABC123|flight|2026-09-01|FRA-NRT',
      sync_enabled: 0,
    });

    expect(await reservationsRepo.findIdByExternal(trip.id, 'import', 'ABC123|flight|2026-09-01|FRA-NRT')).toBe(id);
    expect(
      await reservationsRepo.findIdByExternal(other.id, 'import', 'ABC123|flight|2026-09-01|FRA-NRT'),
    ).toBeUndefined();
    expect(
      await reservationsRepo.findIdByExternal(trip.id, 'mail', 'ABC123|flight|2026-09-01|FRA-NRT'),
    ).toBeUndefined();
  });
});