refactor(assignments, public-api, accommodations, places): async DB-touching methods; transactions through UnitOfWork

This commit is contained in:
jubnl
2026-09-22 05:21:48 +02:00
parent 3a8b54d761
commit 5c8decd3ea
37 changed files with 616 additions and 589 deletions
@@ -36,6 +36,7 @@ export async function accommodationsOver(dbs: DatabaseService): Promise<Accommod
dbs, permissions, realtime,
new QueryHelpersService(dbs),
new JourneyDomainService(dbs, realtime, new TrekPhotosRepository(dbs), await createTestUnitOfWork(dbs.connection)),
await createTestUnitOfWork(dbs.connection),
);
return new AccommodationsService(dbs, permissions, realtime, assignments, await createTestUnitOfWork(dbs.connection));
}
+1 -1
View File
@@ -164,7 +164,7 @@ export async function createMcpTestRegistry(): Promise<McpRegistry> {
// dependency fails the build — pass them for real regardless of the gate.
// One instance, four consumers: AssignmentsMcp, ReservationsMcp, PlacesMcp and
// AccommodationsService, which writes the day stop a booked night implies.
const assignmentsService = new AssignmentsService(dbService, permissionsService, realtimeService, queryHelpersService, journeyDomain);
const assignmentsService = new AssignmentsService(dbService, permissionsService, realtimeService, queryHelpersService, journeyDomain, await createTestUnitOfWork(dbService.connection));
const accommodationsService = new AccommodationsService(dbService, permissionsService, realtimeService, assignmentsService, await createTestUnitOfWork(dbService.connection));
// Built after it: deleting a place cancels the nights booked at it through this one.
const placesService = new PlacesService(
+1 -1
View File
@@ -105,7 +105,7 @@ export async function createPluginRpcHostFactory(dbs: DatabaseService): Promise<
const collections = new CollectionsService(dbs, permissions, realtime, notificationsStub(), generalStorage, await createTestUnitOfWork(dbs.connection));
const atlas = new AtlasService(dbs, await createTestUnitOfWork(dbs.connection));
const dayNotes = new DayNotesService(dbs, permissions, realtime);
const assignments = new AssignmentsService(dbs, permissions, realtime, queryHelpers, journey);
const assignments = new AssignmentsService(dbs, permissions, realtime, queryHelpers, journey, await createTestUnitOfWork(dbs.connection));
const membership = new TripMembershipService(dbs);
const notifications = await makeNotificationsService(dbs, realtime);
const llmConfig = new LlmConfigResolver(new SettingsService(dbs, await createTestUnitOfWork(dbs.connection)), dbs, addons);
@@ -5,10 +5,16 @@
* FK, ON DELETE SET NULL) is actually deleted — otherwise the hook either
* never runs, or races the delete and finds nothing left to annotate.
*
* Wires PlacesController → PlacesService → JourneyDomainService for real
* (no journey mock), through the real buildApp() HTTP surface, so a future
* change that turns the await back into fire-and-forget shows up here rather
* than only in a unit test with a stub.
* Wires PlacesController → PlacesService → JourneyDomainService for real (no
* journey mock), through the real buildApp() HTTP surface, and pins the
* end-to-end detach-then-annotate behaviour that path produces. It is NOT by
* itself a guard against a revert to fire-and-forget: the hook body today has
* no internal await, so it still runs to completion inside the same
* synchronous/microtask turn even if the controller stopped awaiting it, and
* this test would not notice. It becomes a true regression guard once the
* hook body awaits real async DB work of its own — see
* tests/unit/nest/places.controller.test.ts for the unit-level ordering test
* that stubs a macrotask hop and does catch a detached hook today.
*/
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
import request from 'supertest';
@@ -32,9 +32,9 @@ async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number
describe('AccommodationsController (parity with the legacy accommodations sub-router)', () => {
it('GET / lists (no permission gate)', () => {
it('GET / lists (no permission gate)', async () => {
const svc = makeService({ list: vi.fn().mockReturnValue([{ id: 1 }]) } as Partial<AccommodationsService>);
expect(new AccommodationsController(svc).list(user, '5')).toEqual({ accommodations: [{ id: 1 }] });
expect(await new AccommodationsController(svc).list(user, '5')).toEqual({ accommodations: [{ id: 1 }] });
});
describe('POST /', () => {
@@ -72,48 +72,48 @@ afterAll(() => {
});
describe('validateAccommodationRefs', () => {
it('DAY-SVC-016 — returns no errors when all refs are valid', () => {
it('DAY-SVC-016 — returns no errors when all refs are valid', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const errors = svc.validateAccommodationRefs(trip.id, place.id, day.id, day.id);
const errors = await svc.validateAccommodationRefs(trip.id, place.id, day.id, day.id);
expect(errors).toHaveLength(0);
});
it('DAY-SVC-017 — returns error when place does not exist in trip', () => {
it('DAY-SVC-017 — returns error when place does not exist in trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const errors = svc.validateAccommodationRefs(trip.id, 99999, day.id, day.id);
const errors = await svc.validateAccommodationRefs(trip.id, 99999, day.id, day.id);
expect(errors.some((e: any) => e.field === 'place_id')).toBe(true);
});
it('DAY-SVC-018 — returns error when start_day_id is invalid', () => {
it('DAY-SVC-018 — returns error when start_day_id is invalid', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const errors = svc.validateAccommodationRefs(trip.id, place.id, 99999, day.id);
const errors = await svc.validateAccommodationRefs(trip.id, place.id, 99999, day.id);
expect(errors.some((e: any) => e.field === 'start_day_id')).toBe(true);
});
it('ACC-003 — reports every bad ref, end_day_id included', () => {
it('ACC-003 — reports every bad ref, end_day_id included', async () => {
// The controller only surfaces errors[0], so an unchecked end_day_id would let a
// stay point at a day from another trip and never be reported.
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const errors = svc.validateAccommodationRefs(trip.id, 99999, 99998, 99997);
const errors = await svc.validateAccommodationRefs(trip.id, 99999, 99998, 99997);
expect(errors.map((e: any) => e.field)).toEqual(['place_id', 'start_day_id', 'end_day_id']);
});
it('ACC-004 — skips the refs the caller left undefined', () => {
it('ACC-004 — skips the refs the caller left undefined', async () => {
// The plugin update path (accommodations.rpc) passes only the fields it changes.
// If an absent ref were validated, every partial update would 403 "Place not found".
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(svc.validateAccommodationRefs(trip.id)).toHaveLength(0);
expect(svc.validateAccommodationRefs(trip.id, undefined, undefined, undefined)).toHaveLength(0);
expect(await svc.validateAccommodationRefs(trip.id)).toHaveLength(0);
expect(await svc.validateAccommodationRefs(trip.id, undefined, undefined, undefined)).toHaveLength(0);
});
});
@@ -171,21 +171,21 @@ describe('createAccommodation', () => {
});
describe('getAccommodation', () => {
it('DAY-SVC-021 — returns accommodation for valid id and trip', () => {
it('DAY-SVC-021 — returns accommodation for valid id and trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const accom = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id) as any;
const found = svc.getAccommodation(accom.id, trip.id) as any;
const found = await svc.getAccommodation(accom.id, trip.id) as any;
expect(found).toBeDefined();
expect(found.id).toBe(accom.id);
});
it('DAY-SVC-022 — returns undefined for non-existent accommodation', () => {
it('DAY-SVC-022 — returns undefined for non-existent accommodation', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(svc.getAccommodation(99999, trip.id)).toBeUndefined();
expect(await svc.getAccommodation(99999, trip.id)).toBeUndefined();
});
});
@@ -199,7 +199,7 @@ describe('updateAccommodation', () => {
place_id: place.id, start_day_id: day.id, end_day_id: day.id,
})) as any;
const existing = svc.getAccommodation(accom.id, trip.id)!;
const existing = (await svc.getAccommodation(accom.id, trip.id))!;
const { accommodation: updated } = (await svc.updateAccommodation(accom.id, existing as any, { check_in: '16:00', check_out: '12:00' })) as any;
expect(updated).toBeDefined();
@@ -221,10 +221,10 @@ describe('updateAccommodation', () => {
confirmation: 'ABC123',
})) as any;
const existing = svc.getAccommodation(accom.id, trip.id)!;
const existing = (await svc.getAccommodation(accom.id, trip.id))!;
await svc.updateAccommodation(accom.id, existing as any, { check_in: '14:00' });
const row = svc.getAccommodation(accom.id, trip.id) as any;
const row = await svc.getAccommodation(accom.id, trip.id) as any;
expect(row.confirmation).toBe('ABC123');
});
@@ -238,7 +238,7 @@ describe('updateAccommodation', () => {
// The factory writes the stay row directly, without the partner reservation.
const accom = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id) as any;
const existing = svc.getAccommodation(accom.id, trip.id)!;
const existing = (await svc.getAccommodation(accom.id, trip.id))!;
const { accommodation: updated } = (await svc.updateAccommodation(accom.id, existing as any, { check_in: '15:00' })) as any;
expect(updated.check_in).toBe('15:00');
@@ -257,7 +257,7 @@ describe('updateAccommodation', () => {
place_id: place.id, start_day_id: day.id, end_day_id: day.id, check_in: '15:00',
})) as any;
const existing = svc.getAccommodation(accom.id, trip.id)!;
const existing = (await svc.getAccommodation(accom.id, trip.id))!;
await svc.updateAccommodation(accom.id, existing as any, { check_in_end: '20:00', check_out: '11:00' });
const reservation = testDb.prepare('SELECT metadata FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
@@ -278,7 +278,7 @@ describe('updateAccommodation', () => {
})) as any;
testDb.prepare('UPDATE reservations SET confirmation_number = ? WHERE accommodation_id = ?').run('RES-9', accom.id);
const existing = svc.getAccommodation(accom.id, trip.id)!;
const existing = (await svc.getAccommodation(accom.id, trip.id))!;
await svc.updateAccommodation(accom.id, existing as any, { check_in: '15:00' });
const reservation = testDb.prepare('SELECT confirmation_number FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
@@ -302,7 +302,7 @@ describe('deleteAccommodation', () => {
expect(result.linkedReservationId).toBe(reservation.id);
// Accommodation is gone
expect(svc.getAccommodation(accom.id, trip.id)).toBeUndefined();
expect(await svc.getAccommodation(accom.id, trip.id)).toBeUndefined();
// Reservation is gone
const deletedRes = testDb.prepare('SELECT id FROM reservations WHERE id = ?').get(reservation.id);
@@ -400,13 +400,13 @@ describe('deleteAccommodation', () => {
});
describe('listAccommodations', () => {
it('DAY-SVC-029 — listAccommodations returns the hydrated stays', () => {
it('DAY-SVC-029 — listAccommodations returns the hydrated stays', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Ryokan' });
createDayAccommodation(testDb, trip.id, place.id, day.id, day.id);
const rows = svc.listAccommodations(trip.id) as { place_name: string }[];
const rows = await svc.listAccommodations(trip.id) as { place_name: string }[];
expect(rows).toHaveLength(1);
expect(rows[0].place_name).toBe('Ryokan');
});
@@ -438,26 +438,26 @@ describe('route-facing delegators', () => {
// on the same SQL, and the short ones get raw route params — strings where the long
// ones get numbers.
it('ACC-010 — list() returns what listAccommodations() returns, for a string and a number trip id', () => {
it('ACC-010 — list() returns what listAccommodations() returns, for a string and a number trip id', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Pension Alpen' }) as any;
createDayAccommodation(testDb, trip.id, place.id, day.id, day.id);
expect(svc.list(String(trip.id))).toEqual(svc.listAccommodations(trip.id));
expect((svc.list(trip.id) as any[])[0].place_name).toBe('Pension Alpen');
expect(await svc.list(String(trip.id))).toEqual(await svc.listAccommodations(trip.id));
expect((await svc.list(trip.id) as any[])[0].place_name).toBe('Pension Alpen');
});
it('ACC-011 — validateRefs() returns the same errors validateAccommodationRefs() does', () => {
it('ACC-011 — validateRefs() returns the same errors validateAccommodationRefs() does', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id) as any;
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
expect(svc.validateRefs(trip.id, place.id, day.id, day.id)).toHaveLength(0);
expect(await svc.validateRefs(trip.id, place.id, day.id, day.id)).toHaveLength(0);
// The controller 404s with errors[0].message, so the shape matters, not just the count.
expect(svc.validateRefs(trip.id, 99999, day.id, day.id)).toEqual([{ field: 'place_id', message: 'Place not found' }]);
expect(await svc.validateRefs(trip.id, 99999, day.id, day.id)).toEqual([{ field: 'place_id', message: 'Place not found' }]);
});
it('ACC-012 — create() writes the stay, get() reads it back trip-scoped', async () => {
@@ -472,9 +472,9 @@ describe('route-facing delegators', () => {
place_id: place.id, start_day_id: day.id, end_day_id: day.id, confirmation: 'XY-1',
})) as any;
expect(svc.get(String(created.id), String(trip.id))).toMatchObject({ id: created.id, confirmation: 'XY-1' });
expect(await svc.get(String(created.id), String(trip.id))).toMatchObject({ id: created.id, confirmation: 'XY-1' });
// Trip-scoped: a stay must not be readable through another trip's URL.
expect(svc.get(created.id, otherTrip.id)).toBeUndefined();
expect(await svc.get(created.id, otherTrip.id)).toBeUndefined();
});
it('ACC-013 — update() syncs the partner reservation even when the id is a string', async () => {
@@ -486,7 +486,7 @@ describe('route-facing delegators', () => {
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
const { accommodation: accom } = (await svc.create(trip.id, { place_id: place.id, start_day_id: day.id, end_day_id: day.id })) as any;
const existing = svc.get(accom.id, trip.id)!;
const existing = (await svc.get(accom.id, trip.id))!;
const { accommodation: updated } = (await svc.update(String(accom.id), existing as any, { check_in: '16:00', notes: 'late arrival' })) as any;
expect(updated).toMatchObject({ check_in: '16:00', notes: 'late arrival' });
@@ -510,33 +510,33 @@ describe('route-facing delegators', () => {
deletedBudgetItemIds: [],
mirror: { created: null, moved: null, updated: [], removed: [{ id: expect.any(Number), dayId: day.id }], stamped: null },
});
expect(svc.get(accom.id, trip.id)).toBeUndefined();
expect(await svc.get(accom.id, trip.id)).toBeUndefined();
});
});
describe('trip access and edit permission', () => {
it('ACC-015 — verifyTripAccess resolves the trip from a string id and from a number', () => {
it('ACC-015 — verifyTripAccess resolves the trip from a string id and from a number', async () => {
// The REST path hands over the raw :tripId param, the MCP tools the parsed number.
// Without the Number() coercion one of the two callers stops finding its trip.
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(svc.verifyTripAccess(String(trip.id), user.id)).toMatchObject({ id: trip.id, user_id: user.id });
expect(svc.verifyTripAccess(trip.id, user.id)).toMatchObject({ id: trip.id, user_id: user.id });
expect(await svc.verifyTripAccess(String(trip.id), user.id)).toMatchObject({ id: trip.id, user_id: user.id });
expect(await svc.verifyTripAccess(trip.id, user.id)).toMatchObject({ id: trip.id, user_id: user.id });
});
it('ACC-016 — verifyTripAccess returns nothing for a user who is neither owner nor member', () => {
it('ACC-016 — verifyTripAccess returns nothing for a user who is neither owner nor member', async () => {
const { user: owner } = createUser(testDb);
const { user: stranger } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
expect(svc.verifyTripAccess(trip.id, stranger.id)).toBeUndefined();
expect(await svc.verifyTripAccess(trip.id, stranger.id)).toBeUndefined();
});
it('ACC-017 — canEdit lets the owner through on the default day_edit level', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const access = svc.verifyTripAccess(trip.id, user.id)!;
const access = (await svc.verifyTripAccess(trip.id, user.id))!;
expect(await svc.canEdit(access, user as any)).toBe(true);
});
@@ -549,7 +549,7 @@ describe('trip access and edit permission', () => {
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const access = svc.verifyTripAccess(trip.id, member.id)!;
const access = (await svc.verifyTripAccess(trip.id, member.id))!;
expect(await svc.canEdit(access, member as any)).toBe(true);
});
@@ -563,9 +563,9 @@ describe('trip access and edit permission', () => {
try {
await permissions.savePermissions({ day_edit: 'trip_owner' });
expect(await svc.canEdit(svc.verifyTripAccess(trip.id, member.id)!, member as any)).toBe(false);
expect(await svc.canEdit((await svc.verifyTripAccess(trip.id, member.id))!, member as any)).toBe(false);
// The owner keeps the right, otherwise the setting would lock out the whole trip.
expect(await svc.canEdit(svc.verifyTripAccess(trip.id, owner.id)!, owner as any)).toBe(true);
expect(await svc.canEdit((await svc.verifyTripAccess(trip.id, owner.id))!, owner as any)).toBe(true);
} finally {
// The permission cache is module-scoped and outlives resetTestDb — drop the row and
// the cache together, or every later case in this file inherits the raised level.
@@ -718,12 +718,12 @@ describe('the day stop a booking implies', () => {
testDb.prepare('UPDATE day_assignments SET order_index = 2 WHERE id = ?').run(own.id);
expect(stopsOn(day.id).map(a => a.place_id)).toEqual([harbour.id, market.id, hotel.id]);
let existing = svc.getAccommodation(accommodation.id, trip.id)!;
let existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
const quiet = (await svc.updateAccommodation(accommodation.id, existing, { notes: 'late arrival' })) as any;
expect(quiet.mirror.moved).toBeNull();
expect(stopsOn(day.id).map(a => a.place_id)).toEqual([harbour.id, market.id, hotel.id]);
existing = svc.getAccommodation(accommodation.id, trip.id)!;
existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
const reseated = (await svc.updateAccommodation(accommodation.id, existing, { check_in: '10:00' })) as any;
expect(reseated.mirror.moved).toMatchObject({ oldDayId: day.id, assignment: { id: own.id } });
expect(stopsOn(day.id).map(a => a.place_id)).toEqual([hotel.id, harbour.id, market.id]);
@@ -742,7 +742,7 @@ describe('the day stop a booking implies', () => {
})) as any;
expect(stopsOn(day.id).map(a => a.place_id)).toEqual([hotel.id, afternoon.id]);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
await svc.updateAccommodation(accommodation.id, existing, { check_in: '20:00' });
expect(stopsOn(day.id).map(a => a.place_id)).toEqual([afternoon.id, hotel.id]);
@@ -781,7 +781,7 @@ describe('the day stop a booking implies', () => {
// Behind the harbour only: allowed, and left alone.
testDb.prepare('UPDATE day_assignments SET order_index = 0 WHERE day_id = ? AND place_id = ?').run(day.id, harbour.id);
testDb.prepare('UPDATE day_assignments SET order_index = 1 WHERE id = ?').run(own.id);
let existing = svc.getAccommodation(accommodation.id, trip.id)!;
let existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
expect(((await svc.updateAccommodation(accommodation.id, existing, { notes: 'late' })) as any).mirror.moved).toBeNull();
expect(stopsOn(day.id).map(a => a.place_id)).toEqual([harbour.id, hotel.id, museum.id]);
@@ -789,7 +789,7 @@ describe('the day stop a booking implies', () => {
// the front, ahead of the harbour that has no hour of its own.
testDb.prepare('UPDATE day_assignments SET order_index = 1 WHERE day_id = ? AND place_id = ?').run(day.id, museum.id);
testDb.prepare('UPDATE day_assignments SET order_index = 2 WHERE id = ?').run(own.id);
existing = svc.getAccommodation(accommodation.id, trip.id)!;
existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
expect(((await svc.updateAccommodation(accommodation.id, existing, { notes: 'later' })) as any).mirror.moved).not.toBeNull();
expect(stopsOn(day.id).map(a => a.place_id)).toEqual([hotel.id, harbour.id, museum.id]);
});
@@ -877,7 +877,7 @@ describe('the day stop a booking implies', () => {
const { accommodation } = await book(trip.id, place.id, day1.id, day1.id);
const before = stopsOn(day1.id)[0];
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
const { mirror } = (await svc.updateAccommodation(accommodation.id, existing, { start_day_id: day2.id, end_day_id: day2.id })) as any;
expect(stopsOn(day1.id)).toHaveLength(0);
@@ -900,7 +900,7 @@ describe('the day stop a booking implies', () => {
testDb.prepare('UPDATE day_assignments SET notes = ?, assignment_time = ?, end_day = 1 WHERE id = ?')
.run('ask for the quiet side', '15:30', stop.id);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
await svc.updateAccommodation(accommodation.id, existing, { start_day_id: day2.id, end_day_id: day2.id });
const after = testDb.prepare('SELECT * FROM day_assignments WHERE id = ?').get(stop.id) as Record<string, unknown>;
@@ -919,7 +919,7 @@ describe('the day stop a booking implies', () => {
// re-insert would take these with it without a word.
testDb.prepare('INSERT INTO assignment_participants (assignment_id, user_id) VALUES (?, ?)').run(stop.id, user.id);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
await svc.updateAccommodation(accommodation.id, existing, { start_day_id: day2.id, end_day_id: day2.id });
const kept = testDb.prepare('SELECT user_id FROM assignment_participants WHERE assignment_id = ?').all(stop.id);
@@ -936,7 +936,7 @@ describe('the day stop a booking implies', () => {
const own = stopsOn(day1.id)[0];
const byHand = createDayAssignment(testDb, day2.id, place.id) as { id: number };
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
const { mirror } = (await svc.updateAccommodation(accommodation.id, existing, { start_day_id: day2.id, end_day_id: day2.id })) as any;
expect(stopsOn(day1.id)).toHaveLength(0);
@@ -955,7 +955,7 @@ describe('the day stop a booking implies', () => {
const { accommodation } = await book(trip.id, hotel.id, day1.id, day1.id);
createDayAssignment(testDb, day1.id, museum.id);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
await svc.updateAccommodation(accommodation.id, existing, { start_day_id: day2.id, end_day_id: day2.id });
expect(stopsOn(day1.id).map((row: { order_index: number }) => row.order_index)).toEqual([0]);
@@ -969,7 +969,7 @@ describe('the day stop a booking implies', () => {
const { accommodation } = await book(trip.id, place.id, day.id, day.id);
const before = stopsOn(day.id);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
const { mirror } = (await svc.updateAccommodation(accommodation.id, existing, { check_in: '16:00' })) as any;
expect(stopsOn(day.id)).toEqual(before);
@@ -994,7 +994,7 @@ describe('the day stop a booking implies', () => {
testDb.prepare('UPDATE day_assignments SET order_index = 1 WHERE id = ?').run(stop.id);
expect(stopsOn(day.id).map((row: { place_id: number }) => row.place_id)).toEqual([dinner.id, hotel.id]);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
const { mirror } = (await svc.updateAccommodation(accommodation.id, existing, { check_in: '15:00' })) as any;
expect(stopsOn(day.id).map((row: { place_id: number }) => row.place_id)).toEqual([hotel.id, dinner.id]);
@@ -1014,7 +1014,7 @@ describe('the day stop a booking implies', () => {
createDayAssignment(testDb, day.id, place.id);
const stay = createDayAccommodation(testDb, trip.id, place.id, day.id, day.id) as any;
const existing = svc.getAccommodation(stay.id, trip.id)!;
const existing = (await svc.getAccommodation(stay.id, trip.id))!;
await svc.updateAccommodation(stay.id, existing, { start_day_id: second.id, end_day_id: second.id });
expect(stopsOn(second.id)).toEqual([]);
@@ -1219,7 +1219,7 @@ describe('the drawn roads a booking moves', () => {
const afterB = addVia(day.id, 2);
const afterC = addVia(day.id, 3);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
const { mirror } = (await svc.updateAccommodation(accommodation.id, existing, { check_in: '20:00' })) as any;
// The night is last now. The road out of it has no leg left and goes; the roads
@@ -1284,7 +1284,7 @@ describe('the drawn roads a booking moves', () => {
const afterHotel = addVia(day1.id, 1);
const outOfC = addVia(day2.id, 0);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
const { mirror } = (await svc.updateAccommodation(accommodation.id, existing, { start_day_id: day2.id, end_day_id: day2.id })) as any;
expect(stopsOn(day1.id)).toEqual([a.id, b.id]);
@@ -1322,7 +1322,7 @@ describe('the drawn roads a booking moves', () => {
const afterB = addVia(day.id, 2);
addVia(day.id, 3);
const existing = svc.getAccommodation(accommodation.id, trip.id)!;
const existing = (await svc.getAccommodation(accommodation.id, trip.id))!;
const { mirror } = (await svc.updateAccommodation(accommodation.id, existing, { notes: 'late arrival' })) as any;
expect(stopsOn(day.id)).toEqual([a.id, hotel.id, b.id, c.id]);
@@ -15,15 +15,6 @@ function svc(o: Partial<AssignmentsService> = {}): AssignmentsService {
} as unknown as AssignmentsService;
}
function thrown(fn: () => unknown): { status: number; body: unknown } {
try { fn(); } catch (err) {
expect(err).toBeInstanceOf(HttpException);
const e = err as HttpException;
return { status: e.getStatus(), body: e.getResponse() };
}
throw new Error('expected throw');
}
async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
try { await fn(); } catch (err) {
expect(err).toBeInstanceOf(HttpException);
@@ -58,10 +49,10 @@ describe('DayAssignmentsController (parity with the legacy day-assignments route
});
});
it('PUT /reorder 404 day, else reorders + broadcasts', () => {
expect(thrown(() => new DayAssignmentsController(svc({ dayExists: vi.fn().mockReturnValue(false) } as Partial<AssignmentsService>)).reorder(user, '5', '3', { orderedIds: [1, 2] }))).toEqual({ status: 404, body: { error: 'Day not found' } });
it('PUT /reorder 404 day, else reorders + broadcasts', async () => {
expect(await thrownAsync(() => new DayAssignmentsController(svc({ dayExists: vi.fn().mockReturnValue(false) } as Partial<AssignmentsService>)).reorder(user, '5', '3', { orderedIds: [1, 2] }))).toEqual({ status: 404, body: { error: 'Day not found' } });
const reorderAssignments = vi.fn(); const broadcast = vi.fn();
expect(new DayAssignmentsController(svc({ reorderAssignments, broadcast } as Partial<AssignmentsService>)).reorder(user, '5', '3', { orderedIds: [2, 1] }, 'sock')).toEqual({ success: true });
expect(await new DayAssignmentsController(svc({ reorderAssignments, broadcast } as Partial<AssignmentsService>)).reorder(user, '5', '3', { orderedIds: [2, 1] }, 'sock')).toEqual({ success: true });
expect(reorderAssignments).toHaveBeenCalledWith('3', [2, 1]);
expect(broadcast).toHaveBeenCalledWith('5', 'assignment:reordered', { dayId: 3, orderedIds: [2, 1] }, 'sock');
});
@@ -99,11 +90,11 @@ describe('AssignmentOpsController (parity with the per-assignment op routes)', (
expect(reconcile).toHaveBeenCalledWith('5', 'sock');
});
it('GET /:id/participants 404 when the assignment is on another trip, else returns participants (access-only)', () => {
expect(thrown(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined) } as Partial<AssignmentsService>)).participants(user, '5', '9')))
it('GET /:id/participants 404 when the assignment is on another trip, else returns participants (access-only)', async () => {
expect(await thrownAsync(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined) } as Partial<AssignmentsService>)).participants(user, '5', '9')))
.toEqual({ status: 404, body: { error: 'Assignment not found' } });
const s = svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ id: 9 }), getParticipants: vi.fn().mockReturnValue([{ user_id: 2 }]) } as Partial<AssignmentsService>);
expect(new AssignmentOpsController(s).participants(user, '5', '9')).toEqual({ participants: [{ user_id: 2 }] });
expect(await new AssignmentOpsController(s).participants(user, '5', '9')).toEqual({ participants: [{ user_id: 2 }] });
});
it('PUT /:id/time 404 missing, else updates', async () => {
@@ -145,13 +136,13 @@ describe('AssignmentOpsController (parity with the per-assignment op routes)', (
expect(reconcile).not.toHaveBeenCalled();
});
it('PUT /:id/participants 404 on a foreign assignment, else sets + broadcasts (non-array bodies are the Zod pipe\'s 400, covered in e2e)', () => {
it('PUT /:id/participants 404 on a foreign assignment, else sets + broadcasts (non-array bodies are the Zod pipe\'s 400, covered in e2e)', async () => {
const setParticipants = vi.fn().mockReturnValue([{ user_id: 2 }]); const broadcast = vi.fn();
expect(thrown(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined), setParticipants } as Partial<AssignmentsService>)).setParticipants(user, '5', '9', { user_ids: [2] })))
expect(await thrownAsync(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined), setParticipants } as Partial<AssignmentsService>)).setParticipants(user, '5', '9', { user_ids: [2] })))
.toEqual({ status: 404, body: { error: 'Assignment not found' } });
expect(setParticipants).not.toHaveBeenCalled();
const s = svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ id: 9 }), setParticipants, broadcast } as Partial<AssignmentsService>);
expect(new AssignmentOpsController(s).setParticipants(user, '5', '9', { user_ids: [2] }, 'sock')).toEqual({ participants: [{ user_id: 2 }] });
expect(await new AssignmentOpsController(s).setParticipants(user, '5', '9', { user_ids: [2] }, 'sock')).toEqual({ participants: [{ user_id: 2 }] });
// The trip comes along so the service can confine the ids to its roster.
expect(setParticipants).toHaveBeenCalledWith('9', [2], '5');
expect(broadcast).toHaveBeenCalledWith('5', 'assignment:participants', { assignmentId: 9, participants: [{ user_id: 2 }] }, 'sock');
@@ -70,6 +70,7 @@ beforeAll(async () => {
// Real collaborator rather than a stub: reconcile() runs after every mutation
// and needs the same connection to see the rows these cases write.
new JourneyDomainService(dbs, realtime, new TrekPhotosRepository(dbs), await createTestUnitOfWork(dbs.connection)),
await createTestUnitOfWork(dbs.connection),
);
});
@@ -107,17 +108,17 @@ function fixture() {
// ── verifyTripAccess / canEdit ────────────────────────────────────────────────
describe('verifyTripAccess', () => {
it('ASG-SVC-001: returns trip for owner', () => {
it('ASG-SVC-001: returns trip for owner', async () => {
const { user, trip } = fixture();
const result = svc.verifyTripAccess(trip.id, user.id);
const result = await svc.verifyTripAccess(trip.id, user.id);
expect(result).toBeDefined();
expect((result as { user_id: number }).user_id).toBe(user.id);
});
it('ASG-SVC-002: returns falsy for non-member', () => {
it('ASG-SVC-002: returns falsy for non-member', async () => {
const { trip } = fixture();
const { user: stranger } = createUser(testDb);
expect(svc.verifyTripAccess(trip.id, stranger.id)).toBeFalsy();
expect(await svc.verifyTripAccess(trip.id, stranger.id)).toBeFalsy();
});
});
@@ -134,30 +135,30 @@ describe('canEdit', () => {
// ── existence checks ──────────────────────────────────────────────────────────
describe('dayExists / placeExists / assignmentExistsInDay', () => {
it('ASG-SVC-004: dayExists is trip-scoped', () => {
it('ASG-SVC-004: dayExists is trip-scoped', async () => {
const { trip, day } = fixture();
const { user: other } = createUser(testDb);
const otherTrip = createTrip(testDb, other.id);
expect(svc.dayExists(day.id, trip.id)).toBe(true);
expect(svc.dayExists(day.id, otherTrip.id)).toBe(false);
expect(svc.dayExists(9999, trip.id)).toBe(false);
expect(await svc.dayExists(day.id, trip.id)).toBe(true);
expect(await svc.dayExists(day.id, otherTrip.id)).toBe(false);
expect(await svc.dayExists(9999, trip.id)).toBe(false);
});
it('ASG-SVC-005: placeExists is trip-scoped', () => {
it('ASG-SVC-005: placeExists is trip-scoped', async () => {
const { trip, place } = fixture();
const { user: other } = createUser(testDb);
const otherTrip = createTrip(testDb, other.id);
expect(svc.placeExists(place.id, trip.id)).toBe(true);
expect(svc.placeExists(place.id, otherTrip.id)).toBe(false);
expect(await svc.placeExists(place.id, trip.id)).toBe(true);
expect(await svc.placeExists(place.id, otherTrip.id)).toBe(false);
});
it('ASG-SVC-006: assignmentExistsInDay requires matching day AND trip', () => {
it('ASG-SVC-006: assignmentExistsInDay requires matching day AND trip', async () => {
const { trip, day, place } = fixture();
const otherDay = createDay(testDb, trip.id);
const a = createDayAssignment(testDb, day.id, place.id);
expect(svc.assignmentExistsInDay(a.id, day.id, trip.id)).toBe(true);
expect(svc.assignmentExistsInDay(a.id, otherDay.id, trip.id)).toBe(false);
expect(svc.assignmentExistsInDay(a.id, day.id, trip.id + 1)).toBe(false);
expect(await svc.assignmentExistsInDay(a.id, day.id, trip.id)).toBe(true);
expect(await svc.assignmentExistsInDay(a.id, otherDay.id, trip.id)).toBe(false);
expect(await svc.assignmentExistsInDay(a.id, day.id, trip.id + 1)).toBe(false);
});
});
@@ -166,10 +167,10 @@ describe('dayExists / placeExists / assignmentExistsInDay', () => {
describe('createAssignment', () => {
it('ASG-SVC-007: first assignment gets order_index 0, then MAX+1 appends', async () => {
const { day, place } = fixture();
const first = svc.createAssignment(day.id, place.id, null);
const second = svc.createAssignment(day.id, place.id, null);
expect((await first!).order_index).toBe(0);
expect((await second!).order_index).toBe(1);
const first = await svc.createAssignment(day.id, place.id, null);
const second = await svc.createAssignment(day.id, place.id, null);
expect(first!.order_index).toBe(0);
expect(second!.order_index).toBe(1);
});
it('ASG-SVC-008: empty-string notes coerce to null (`notes || null`)', async () => {
@@ -210,7 +211,7 @@ describe('listDayAssignments', () => {
testDb.prepare('INSERT INTO place_tags (place_id, tag_id) VALUES (?, ?)').run(place.id, tag.id);
const a1 = createDayAssignment(testDb, day.id, place.id, { order_index: 1 });
const a2 = createDayAssignment(testDb, day.id, second.id, { order_index: 0 });
svc.setParticipants(a1.id, [user.id], trip.id);
await svc.setParticipants(a1.id, [user.id], trip.id);
const list = await svc.listDayAssignments(day.id);
expect(list.map(a => a.id)).toEqual([a2.id, a1.id]);
@@ -230,21 +231,21 @@ describe('listDayAssignments', () => {
// ── delete / reorder ──────────────────────────────────────────────────────────
describe('deleteAssignment / reorderAssignments', () => {
it('ASG-SVC-012: deleteAssignment removes the row', () => {
it('ASG-SVC-012: deleteAssignment removes the row', async () => {
const { day, place } = fixture();
const a = createDayAssignment(testDb, day.id, place.id);
svc.deleteAssignment(a.id);
await svc.deleteAssignment(a.id);
expect(testDb.prepare('SELECT id FROM day_assignments WHERE id = ?').get(a.id)).toBeUndefined();
});
it('ASG-SVC-013: reorderAssignments applies positional order, scoped to the day', () => {
it('ASG-SVC-013: reorderAssignments applies positional order, scoped to the day', async () => {
const { trip, day, place } = fixture();
const otherDay = createDay(testDb, trip.id);
const a1 = createDayAssignment(testDb, day.id, place.id, { order_index: 0 });
const a2 = createDayAssignment(testDb, day.id, place.id, { order_index: 1 });
const foreign = createDayAssignment(testDb, otherDay.id, place.id, { order_index: 5 });
svc.reorderAssignments(day.id, [a2.id, a1.id, foreign.id]);
await svc.reorderAssignments(day.id, [a2.id, a1.id, foreign.id]);
const order = (id: number) => (testDb.prepare('SELECT order_index FROM day_assignments WHERE id = ?').get(id) as { order_index: number }).order_index;
expect(order(a2.id)).toBe(0);
@@ -257,11 +258,11 @@ describe('deleteAssignment / reorderAssignments', () => {
// ── getAssignmentForTrip / moveAssignment ─────────────────────────────────────
describe('getAssignmentForTrip', () => {
it('ASG-SVC-014: returns the raw row for the trip, undefined cross-trip', () => {
it('ASG-SVC-014: returns the raw row for the trip, undefined cross-trip', async () => {
const { trip, day, place } = fixture();
const a = createDayAssignment(testDb, day.id, place.id);
expect(svc.getAssignmentForTrip(a.id, trip.id)).toMatchObject({ id: a.id, day_id: day.id });
expect(svc.getAssignmentForTrip(a.id, trip.id + 1)).toBeUndefined();
expect(await svc.getAssignmentForTrip(a.id, trip.id)).toMatchObject({ id: a.id, day_id: day.id });
expect(await svc.getAssignmentForTrip(a.id, trip.id + 1)).toBeUndefined();
});
});
@@ -288,37 +289,37 @@ describe('moveAssignment', () => {
// ── participants ──────────────────────────────────────────────────────────────
describe('getParticipants / setParticipants', () => {
it('ASG-SVC-017: setParticipants replaces the list and returns the joined rows', () => {
it('ASG-SVC-017: setParticipants replaces the list and returns the joined rows', async () => {
const { user, trip, day, place } = fixture();
const { user: peer } = createUser(testDb);
addTripMember(testDb, trip.id, peer.id);
const a = createDayAssignment(testDb, day.id, place.id);
svc.setParticipants(a.id, [user.id], trip.id);
const replaced = svc.setParticipants(a.id, [peer.id], trip.id);
await svc.setParticipants(a.id, [user.id], trip.id);
const replaced = await svc.setParticipants(a.id, [peer.id], trip.id);
expect(replaced).toEqual([{ user_id: peer.id, username: peer.username, avatar: null }]);
expect(svc.getParticipants(a.id)).toEqual(replaced);
expect(await svc.getParticipants(a.id)).toEqual(replaced);
});
it('ASG-SVC-018: empty array clears all participants', () => {
it('ASG-SVC-018: empty array clears all participants', async () => {
const { user, trip, day, place } = fixture();
const a = createDayAssignment(testDb, day.id, place.id);
svc.setParticipants(a.id, [user.id], trip.id);
expect(svc.setParticipants(a.id, [], trip.id)).toEqual([]);
expect(svc.getParticipants(a.id)).toEqual([]);
await svc.setParticipants(a.id, [user.id], trip.id);
expect(await svc.setParticipants(a.id, [], trip.id)).toEqual([]);
expect(await svc.getParticipants(a.id)).toEqual([]);
});
it('ASG-SVC-019: username COALESCEs display_name over username', () => {
it('ASG-SVC-019: username COALESCEs display_name over username', async () => {
const { user, trip, day, place } = fixture();
testDb.prepare('UPDATE users SET display_name = ? WHERE id = ?').run('Fancy Name', user.id);
const a = createDayAssignment(testDb, day.id, place.id);
const rows = svc.setParticipants(a.id, [user.id], trip.id);
const rows = await svc.setParticipants(a.id, [user.id], trip.id);
expect(rows).toEqual([{ user_id: user.id, username: 'Fancy Name', avatar: null }]);
});
it('ASG-SVC-020: duplicate user ids collapse via INSERT OR IGNORE', () => {
it('ASG-SVC-020: duplicate user ids collapse via INSERT OR IGNORE', async () => {
const { user, trip, day, place } = fixture();
const a = createDayAssignment(testDb, day.id, place.id);
const rows = svc.setParticipants(a.id, [user.id, user.id], trip.id);
const rows = await svc.setParticipants(a.id, [user.id, user.id], trip.id);
expect(rows).toHaveLength(1);
});
@@ -328,22 +329,22 @@ describe('getParticipants / setParticipants', () => {
// list, and the delete stands. The rollback itself is still covered — the
// transaction wraps delete and insert together, and ASG-SVC-018 pins that an
// explicit empty list clears.
it('ASG-SVC-028: a list of ids that are not on the trip clears rather than throwing', () => {
it('ASG-SVC-028: a list of ids that are not on the trip clears rather than throwing', async () => {
const { user, trip, day, place } = fixture();
const a = createDayAssignment(testDb, day.id, place.id);
svc.setParticipants(a.id, [user.id], trip.id);
expect(() => svc.setParticipants(a.id, [999999], trip.id)).not.toThrow();
expect(svc.getParticipants(a.id)).toEqual([]);
await svc.setParticipants(a.id, [user.id], trip.id);
await expect(svc.setParticipants(a.id, [999999], trip.id)).resolves.toBeDefined();
expect(await svc.getParticipants(a.id)).toEqual([]);
});
it('ASG-SVC-029: keeps the trip members and drops the stranger in one call', () => {
it('ASG-SVC-029: keeps the trip members and drops the stranger in one call', async () => {
const { user, trip, day, place } = fixture();
const { user: member } = createUser(testDb);
const { user: stranger } = createUser(testDb);
addTripMember(testDb, trip.id, member.id);
const a = createDayAssignment(testDb, day.id, place.id);
const rows = svc.setParticipants(a.id, [user.id, stranger.id, member.id], trip.id) as { user_id: number }[];
const rows = await svc.setParticipants(a.id, [user.id, stranger.id, member.id], trip.id) as { user_id: number }[];
expect(rows.map(r => r.user_id).sort()).toEqual([user.id, member.id].sort());
expect(JSON.stringify(rows)).not.toContain(stranger.username);
@@ -429,7 +430,7 @@ describe('updateTime', () => {
expect(dayOrder(day.id)).toEqual([b, a, c]);
// Dragged back out of time order on purpose: a sort now would put b first again,
// with c behind it.
svc.reorderAssignments(day.id, [a, b, c]);
await svc.reorderAssignments(day.id, [a, b, c]);
const { assignment } = await svc.updateTime(c, null, null); // clear, no re-sort
expect((await assignment!).assignment_time).toBeNull();
@@ -440,7 +441,7 @@ describe('updateTime', () => {
const { day, ids: [a, b, c] } = dayOf([['09:00', 0], [null, 1], ['12:00', 2]]);
await svc.updateTime(b, '06:00', '07:00'); // sorts b first
expect(dayOrder(day.id)).toEqual([b, a, c]);
svc.reorderAssignments(day.id, [a, b, c]);
await svc.reorderAssignments(day.id, [a, b, c]);
const { assignment } = await svc.updateTime(c, '', '');
expect((await assignment!).assignment_time).toBeNull();
@@ -23,14 +23,6 @@ function svc(o: Partial<PlacesService> = {}): PlacesService {
} as unknown as PlacesService;
}
function thrown(fn: () => unknown): { status: number; body: unknown } {
try { fn(); } catch (err) {
expect(err).toBeInstanceOf(HttpException);
const e = err as HttpException;
return { status: e.getStatus(), body: e.getResponse() };
}
throw new Error('expected throw');
}
async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
try { await fn(); } catch (err) {
expect(err).toBeInstanceOf(HttpException);
@@ -151,18 +143,18 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
const makeRes = () => ({ setHeader: vi.fn(), send: vi.fn() });
const exportQuery = {} as never;
it('an ASCII filename keeps the exact legacy header', () => {
it('an ASCII filename keeps the exact legacy header', async () => {
const res = makeRes();
const s = svc({ exportGpx: vi.fn().mockReturnValue({ gpx: '<gpx/>', filename: 'Alpine-week.gpx' }) } as Partial<PlacesService>);
new PlacesController(s, new RuntimeEnvService(), storageStub).exportGpx(user, '5', exportQuery, res as never);
await new PlacesController(s, new RuntimeEnvService(), storageStub).exportGpx(user, '5', exportQuery, res as never);
expect(res.setHeader).toHaveBeenCalledWith('Content-Disposition', 'attachment; filename="Alpine-week.gpx"');
expect(res.send).toHaveBeenCalledWith('<gpx/>');
});
it('a non-ASCII filename no longer reaches setHeader raw: ASCII fallback + filename*', () => {
it('a non-ASCII filename no longer reaches setHeader raw: ASCII fallback + filename*', async () => {
const res = makeRes();
const s = svc({ exportGpx: vi.fn().mockReturnValue({ gpx: '<gpx/>', filename: '沖縄-4泊5日.gpx' }) } as Partial<PlacesService>);
new PlacesController(s, new RuntimeEnvService(), storageStub).exportGpx(user, '5', exportQuery, res as never);
await new PlacesController(s, new RuntimeEnvService(), storageStub).exportGpx(user, '5', exportQuery, res as never);
expect(res.setHeader).toHaveBeenCalledWith(
'Content-Disposition',
'attachment; filename="__-4_5_.gpx"; filename*=UTF-8\'\'%E6%B2%96%E7%B8%84-4%E6%B3%8A5%E6%97%A5.gpx',
@@ -264,6 +256,26 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
expect(onDeleted.mock.invocationCallOrder[0]).toBeLessThan(removeMany.mock.invocationCallOrder[0]);
});
// invocationCallOrder above only proves the hook was CALLED first — a mock
// that resolves synchronously can't tell an awaited hook from a detached
// (fire-and-forget) one, since both invoke onDeleted() before removeMany() is
// invoked either way. A hook stubbed with a real macrotask hop, and a delete
// that records when it actually RUNS, catches a regression to detached.
it('the journey hook actually finishes before the delete runs, not just gets called first', async () => {
const order: string[] = [];
const onDeleted = vi.fn(async () => {
await new Promise((resolve) => setTimeout(resolve, 0));
order.push('hook');
});
const removeMany = vi.fn(async () => {
order.push('delete');
return { deleted: [1], cancelled: { reservationIds: [], budgetItemIds: [] } };
});
const s = svc({ removeMany, onDeleted, scopedIds: vi.fn().mockReturnValue([1]), broadcast: vi.fn() } as Partial<PlacesService>);
await new PlacesController(s, new RuntimeEnvService(), storageStub).bulkDelete(user, '5', { ids: [1] });
expect(order).toEqual(['hook', 'delete']);
});
// #1298: the link is gone once the place is, so the ids have to be read first.
it('announces the expenses the deleted places took with them', async () => {
const removeMany = vi.fn().mockReturnValue({ deleted: [1, 2], cancelled: { reservationIds: [], budgetItemIds: [] } });
@@ -325,10 +337,10 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
});
});
it('GET /:id returns the place when found, 404 when missing', () => {
expect(thrown(() => new PlacesController(svc({ get: vi.fn().mockReturnValue(undefined) } as Partial<PlacesService>), new RuntimeEnvService(), storageStub).get(user, '5', '9'))).toEqual({ status: 404, body: { error: 'Place not found' } });
it('GET /:id returns the place when found, 404 when missing', async () => {
expect(await thrownAsync(() => new PlacesController(svc({ get: vi.fn().mockReturnValue(undefined) } as Partial<PlacesService>), new RuntimeEnvService(), storageStub).get(user, '5', '9'))).toEqual({ status: 404, body: { error: 'Place not found' } });
const s = svc({ get: vi.fn().mockReturnValue({ id: 9 }) } as Partial<PlacesService>);
expect(new PlacesController(s, new RuntimeEnvService(), storageStub).get(user, '5', '9')).toEqual({ place: { id: 9 } });
expect(await new PlacesController(s, new RuntimeEnvService(), storageStub).get(user, '5', '9')).toEqual({ place: { id: 9 } });
});
it('PUT /:id 404 when missing, else updates + hooks', async () => {
@@ -446,6 +458,26 @@ describe('PlacesController (parity with the legacy /api/trips/:tripId/places rou
expect(await new PlacesController(s, new RuntimeEnvService(), storageStub).remove(user, '5', '9')).toEqual({ success: true });
});
// invocationCallOrder in the case above only proves the hook was CALLED
// first — a synchronously-resolving mock can't tell an awaited hook from a
// detached (fire-and-forget) one. A hook stubbed with a real macrotask hop,
// and a delete that records when it actually RUNS, catches a regression to
// detached (the trap this pins: onDeleted must finish before the DELETE).
it('DELETE /:id — the journey hook actually finishes before the delete runs, not just gets called first', async () => {
const order: string[] = [];
const onDeleted = vi.fn(async () => {
await new Promise((resolve) => setTimeout(resolve, 0));
order.push('hook');
});
const remove = vi.fn(async () => {
order.push('delete');
return { deleted: true, cancelled: { reservationIds: [], budgetItemIds: [] } };
});
const s = svc({ remove, onDeleted, broadcast: vi.fn() } as Partial<PlacesService>);
await new PlacesController(s, new RuntimeEnvService(), storageStub).remove(user, '5', '9');
expect(order).toEqual(['hook', 'delete']);
});
it('DELETE /:id announces the booking and the expense a cancelled night took down', async () => {
// place:deleted says nothing about either, and an expense linked by
// reservation_id is not one linkedExpenseIds finds.
+111 -111
View File
@@ -197,20 +197,20 @@ describe('list', () => {
// ── create ────────────────────────────────────────────────────────────────────
describe('create', () => {
it('PLACE-SVC-007 — creates a place and returns it with tags array', () => {
it('PLACE-SVC-007 — creates a place and returns it with tags array', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = svc.create(String(trip.id), { name: 'New Place', lat: 48.8, lng: 2.3 }) as any;
const place = await svc.create(String(trip.id), { name: 'New Place', lat: 48.8, lng: 2.3 }) as any;
expect(place).toBeDefined();
expect(place.name).toBe('New Place');
expect(Array.isArray(place.tags)).toBe(true);
});
it('PLACE-SVC-008 — creates a place with tags', () => {
it('PLACE-SVC-008 — creates a place with tags', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const tag = createTag(testDb, user.id, { name: 'Highlight' }) as any;
const place = svc.create(String(trip.id), { name: 'Tagged Place', tags: [tag.id] }) as any;
const place = await svc.create(String(trip.id), { name: 'Tagged Place', tags: [tag.id] }) as any;
expect(place.tags).toHaveLength(1);
expect(place.tags[0].id).toBe(tag.id);
});
@@ -218,34 +218,34 @@ describe('create', () => {
// A tag belongs to a user, not a trip, and the place body is an open record, so
// an id from someone outside the trip could be attached and then read straight
// back — the tag projection carries the owner's user_id with it.
it('PLACE-SVC-008a — drops a tag owned by someone outside the trip', () => {
it('PLACE-SVC-008a — drops a tag owned by someone outside the trip', async () => {
const { user } = createUser(testDb);
const { user: outsider } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const mine = createTag(testDb, user.id, { name: 'Mine' }) as any;
const theirs = createTag(testDb, outsider.id, { name: 'Theirs' }) as any;
const place = svc.create(String(trip.id), { name: 'Tagged Place', tags: [mine.id, theirs.id] }) as any;
const place = await svc.create(String(trip.id), { name: 'Tagged Place', tags: [mine.id, theirs.id] }) as any;
expect(place.tags.map((t: any) => t.id)).toEqual([mine.id]);
});
it('PLACE-SVC-008b — keeps a co-traveller tag, so a shared place survives a foreign re-save', () => {
it('PLACE-SVC-008b — keeps a co-traveller tag, so a shared place survives a foreign re-save', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const theirTag = createTag(testDb, member.id, { name: 'Theirs' }) as any;
const place = svc.create(String(trip.id), { name: 'Shared Place', tags: [theirTag.id] }) as any;
const place = await svc.create(String(trip.id), { name: 'Shared Place', tags: [theirTag.id] }) as any;
expect(place.tags.map((t: any) => t.id)).toEqual([theirTag.id]);
});
it('PLACE-SVC-009 — place is associated with correct trip', () => {
it('PLACE-SVC-009 — place is associated with correct trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = svc.create(String(trip.id), { name: 'My Place' }) as any;
const place = await svc.create(String(trip.id), { name: 'My Place' }) as any;
const row = testDb.prepare('SELECT trip_id FROM places WHERE id = ?').get(place.id) as any;
expect(row.trip_id).toBe(trip.id);
});
@@ -254,27 +254,27 @@ describe('create', () => {
// ── get ───────────────────────────────────────────────────────────────────────
describe('get', () => {
it('PLACE-SVC-010 — returns the place when tripId and placeId match', () => {
it('PLACE-SVC-010 — returns the place when tripId and placeId match', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Find Me' }) as any;
const found = svc.get(String(trip.id), String(place.id)) as any;
const found = await svc.get(String(trip.id), String(place.id)) as any;
expect(found).toBeDefined();
expect(found.name).toBe('Find Me');
});
it('PLACE-SVC-011 — returns null when place belongs to different trip', () => {
it('PLACE-SVC-011 — returns null when place belongs to different trip', async () => {
const { user } = createUser(testDb);
const t1 = createTrip(testDb, user.id);
const t2 = createTrip(testDb, user.id);
const place = createPlace(testDb, t1.id, { name: 'T1 Place' }) as any;
expect(svc.get(String(t2.id), String(place.id))).toBeNull();
expect(await svc.get(String(t2.id), String(place.id))).toBeNull();
});
it('PLACE-SVC-012 — returns null for non-existent placeId', () => {
it('PLACE-SVC-012 — returns null for non-existent placeId', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(svc.get(String(trip.id), '99999')).toBeNull();
expect(await svc.get(String(trip.id), '99999')).toBeNull();
});
});
@@ -302,7 +302,7 @@ describe('update', () => {
const trip = createTrip(testDb, user.id);
const tag1 = createTag(testDb, user.id, { name: 'Old Tag' }) as any;
const tag2 = createTag(testDb, user.id, { name: 'New Tag' }) as any;
const place = svc.create(String(trip.id), { name: 'Taggable', tags: [tag1.id] }) as any;
const place = await svc.create(String(trip.id), { name: 'Taggable', tags: [tag1.id] }) as any;
const updated = await svc.update(String(trip.id), String(place.id), { tags: [tag2.id] }) as any;
expect(updated.tags).toHaveLength(1);
@@ -313,7 +313,7 @@ describe('update', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const tag = createTag(testDb, user.id, { name: 'Temp' }) as any;
const place = svc.create(String(trip.id), { name: 'Untaggable', tags: [tag.id] }) as any;
const place = await svc.create(String(trip.id), { name: 'Untaggable', tags: [tag.id] }) as any;
const updated = await svc.update(String(trip.id), String(place.id), { tags: [] }) as any;
expect(updated.tags).toHaveLength(0);
@@ -350,10 +350,10 @@ describe('update', () => {
expect(renamed.route_color).toBe('#059669');
});
it('PLACE-SVC-055 — create carries geometry and colour instead of dropping them', () => {
it('PLACE-SVC-055 — create carries geometry and colour instead of dropping them', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const created = svc.create(String(trip.id), {
const created = await svc.create(String(trip.id), {
name: 'Restored track',
route_geometry: '[[48.0,2.0],[49.0,3.0]]',
route_color: '#7c3aed',
@@ -396,7 +396,7 @@ describe('updateMany', () => {
expect(updated).toHaveLength(1);
expect((updated[0] as any).id).toBe(mine.id);
// The place from the other trip stays untouched.
expect((svc.get(String(other.id), String(foreign.id)) as any).notes).toBeNull();
expect((await svc.get(String(other.id), String(foreign.id)) as any).notes).toBeNull();
});
it('PLACE-SVC-041 — returns [] for an empty id list', async () => {
@@ -414,7 +414,7 @@ describe('remove', () => {
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'To Delete' }) as any;
expect((await svc.remove(String(trip.id), String(place.id))).deleted).toBe(true);
expect(svc.get(String(trip.id), String(place.id))).toBeNull();
expect(await svc.get(String(trip.id), String(place.id))).toBeNull();
});
it('PLACE-SVC-018 — returns false for non-existent place', async () => {
@@ -480,7 +480,7 @@ describe('remove', () => {
const standalone = Number(testDb.prepare("INSERT INTO budget_items (trip_id, name, total_price) VALUES (?, 'Coffee', 3)").run(trip.id).lastInsertRowid);
// Read the link before the delete — that is what the controller broadcasts.
expect(svc.linkedExpenseIds(trip.id, [place.id])).toEqual([linked]);
expect(await svc.linkedExpenseIds(trip.id, [place.id])).toEqual([linked]);
expect((await svc.remove(String(trip.id), String(place.id))).deleted).toBe(true);
const rows = testDb.prepare('SELECT id FROM budget_items ORDER BY id').all() as { id: number }[];
@@ -497,22 +497,22 @@ describe('remove', () => {
testDb.prepare("INSERT INTO budget_items (trip_id, name, total_price, place_id) VALUES (?, 'x', 1, ?)").run(trip.id, p.id);
}
expect(svc.linkedExpenseIds(trip.id, [a.id, b.id])).toHaveLength(2);
expect(await svc.linkedExpenseIds(trip.id, [a.id, b.id])).toHaveLength(2);
await svc.removeMany(String(trip.id), [a.id, b.id]);
const rows = testDb.prepare('SELECT place_id FROM budget_items').all() as { place_id: number }[];
expect(rows.map(r => r.place_id)).toEqual([keep.id]);
});
it('PLACE-SVC-019e — linkedExpenseIds ignores places of another trip and an empty list', () => {
it('PLACE-SVC-019e — linkedExpenseIds ignores places of another trip and an empty list', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const other = createTrip(testDb, user.id);
const place = createPlace(testDb, other.id, { name: 'Elsewhere' }) as any;
testDb.prepare("INSERT INTO budget_items (trip_id, name, total_price, place_id) VALUES (?, 'x', 1, ?)").run(other.id, place.id);
expect(svc.linkedExpenseIds(trip.id, [place.id])).toEqual([]);
expect(svc.linkedExpenseIds(trip.id, [])).toEqual([]);
expect(await svc.linkedExpenseIds(trip.id, [place.id])).toEqual([]);
expect(await svc.linkedExpenseIds(trip.id, [])).toEqual([]);
});
it('PLACE-SVC-019b — reclaims the photo cache for the deleted place', async () => {
@@ -542,7 +542,7 @@ describe('removeMany', () => {
const { deleted } = await svc.removeMany(String(trip.id), [a.id, b.id, foreign.id, 99999]);
expect(deleted.sort()).toEqual([a.id, b.id].sort());
expect(svc.get(String(other.id), String(foreign.id))).not.toBeNull();
expect(await svc.get(String(other.id), String(foreign.id))).not.toBeNull();
});
it('PLACE-SVC-057b — a place delete reports the booking and expense its cancelled night took down', async () => {
@@ -580,27 +580,27 @@ describe('removeMany', () => {
// ── importGpx ─────────────────────────────────────────────────────────────────
describe('importGpx', () => {
it('PLACE-SVC-020 — returns null when buffer has no <gpx> root', () => {
it('PLACE-SVC-020 — returns null when buffer has no <gpx> root', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const result = svc.importGpx(String(trip.id), Buffer.from('<not-gpx/>'));
const result = await svc.importGpx(String(trip.id), Buffer.from('<not-gpx/>'));
expect(result).toBeNull();
});
it('PLACE-SVC-021 — imports <wpt> waypoints as places', () => {
it('PLACE-SVC-021 — imports <wpt> waypoints as places', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const gpx = Buffer.from(`<?xml version="1.0"?><gpx version="1.1">
<wpt lat="48.8566" lon="2.3522"><name>Paris</name></wpt>
<wpt lat="51.5074" lon="-0.1278"><name>London</name></wpt>
</gpx>`);
const result = svc.importGpx(String(trip.id), gpx) as any;
const result = await svc.importGpx(String(trip.id), gpx) as any;
expect(result.places).toHaveLength(2);
expect(result.places[0].name).toBe('Paris');
expect(result.places[1].name).toBe('London');
});
it('PLACE-SVC-022 — imports <rte> as a single polyline-place with routeGeometry', () => {
it('PLACE-SVC-022 — imports <rte> as a single polyline-place with routeGeometry', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const gpx = Buffer.from(`<?xml version="1.0"?><gpx version="1.1">
@@ -610,7 +610,7 @@ describe('importGpx', () => {
<rtept lat="51.5074" lon="-0.1278"><name>End</name></rtept>
</rte>
</gpx>`);
const result = svc.importGpx(String(trip.id), gpx) as any;
const result = await svc.importGpx(String(trip.id), gpx) as any;
expect(result.places).toHaveLength(1);
expect(result.places[0].name).toBe('My Route');
expect(result.places[0].lat).toBe(48.8566);
@@ -620,7 +620,7 @@ describe('importGpx', () => {
expect(coords).toHaveLength(2);
});
it('PLACE-SVC-023 — imports <trk> track as a single place with routeGeometry', () => {
it('PLACE-SVC-023 — imports <trk> track as a single place with routeGeometry', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const gpx = Buffer.from(`<?xml version="1.0"?><gpx version="1.1">
@@ -632,7 +632,7 @@ describe('importGpx', () => {
</trkseg>
</trk>
</gpx>`);
const result = svc.importGpx(String(trip.id), gpx) as any;
const result = await svc.importGpx(String(trip.id), gpx) as any;
expect(result.places).toHaveLength(1);
expect(result.places[0].name).toBe('My Track');
const geometry = JSON.parse(result.places[0].route_geometry);
@@ -640,7 +640,7 @@ describe('importGpx', () => {
expect(geometry).toHaveLength(2);
});
it('PLACE-SVC-024 — <wpt> and <trk> together: waypoints plus track appended', () => {
it('PLACE-SVC-024 — <wpt> and <trk> together: waypoints plus track appended', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const gpx = Buffer.from(`<?xml version="1.0"?><gpx version="1.1">
@@ -653,7 +653,7 @@ describe('importGpx', () => {
</trkseg>
</trk>
</gpx>`);
const result = svc.importGpx(String(trip.id), gpx) as any;
const result = await svc.importGpx(String(trip.id), gpx) as any;
// 1 wpt + 1 trk
expect(result.places).toHaveLength(2);
const trackPlace = result.places.find((p: any) => p.name === 'Track') as any;
@@ -662,15 +662,15 @@ describe('importGpx', () => {
expect(geometry).toHaveLength(2);
});
it('PLACE-SVC-025 — returns null when GPX has no usable elements', () => {
it('PLACE-SVC-025 — returns null when GPX has no usable elements', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const gpx = Buffer.from(`<?xml version="1.0"?><gpx version="1.1"></gpx>`);
const result = svc.importGpx(String(trip.id), gpx);
const result = await svc.importGpx(String(trip.id), gpx);
expect(result).toBeNull();
});
it('PLACE-SVC-037 — multiple unnamed tracks in one file get distinct names instead of collapsing to one', () => {
it('PLACE-SVC-037 — multiple unnamed tracks in one file get distinct names instead of collapsing to one', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const gpx = Buffer.from(`<?xml version="1.0"?><gpx version="1.1">
@@ -683,13 +683,13 @@ describe('importGpx', () => {
<trkpt lat="40.1000" lon="-3.1000"></trkpt>
</trkseg></trk>
</gpx>`);
const result = svc.importGpx(String(trip.id), gpx) as any;
const result = await svc.importGpx(String(trip.id), gpx) as any;
expect(result.places).toHaveLength(2);
const names = result.places.map((p: any) => p.name);
expect(new Set(names).size).toBe(2);
});
it('PLACE-SVC-038 — unnamed tracks fall back to the source filename', () => {
it('PLACE-SVC-038 — unnamed tracks fall back to the source filename', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const gpx = Buffer.from(`<?xml version="1.0"?><gpx version="1.1">
@@ -698,7 +698,7 @@ describe('importGpx', () => {
<trkpt lat="48.8570" lon="2.3530"></trkpt>
</trkseg></trk>
</gpx>`);
const result = svc.importGpx(String(trip.id), gpx, { defaultName: 'morning-hike.gpx' }) as any;
const result = await svc.importGpx(String(trip.id), gpx, { defaultName: 'morning-hike.gpx' }) as any;
expect(result.places).toHaveLength(1);
expect(result.places[0].name).toBe('morning-hike');
});
@@ -985,11 +985,11 @@ describe('importGpx deduplication', () => {
const buf = fs.readFileSync(GPX_FIXTURE);
// First import
const first = svc.importGpx(String(trip.id), buf) as any;
const first = await svc.importGpx(String(trip.id), buf) as any;
expect(first.count).toBeGreaterThan(0);
// Second import — all names already present, nothing new created
const second = svc.importGpx(String(trip.id), buf) as any;
const second = await svc.importGpx(String(trip.id), buf) as any;
expect(second.count).toBe(0);
expect(second.skipped).toBe(first.count);
@@ -1003,12 +1003,12 @@ describe('importGpx deduplication', () => {
const trip = createTrip(testDb, user.id);
const buf = fs.readFileSync(GPX_FIXTURE);
const first = svc.importGpx(String(trip.id), buf) as any;
const first = await svc.importGpx(String(trip.id), buf) as any;
// Manually add a brand-new place so total > first.count
createPlace(testDb, trip.id, { name: 'Unique Extra Place', lat: 99, lng: 99 });
// Re-import: the fixture places are skipped, the extra place remains untouched
const second = svc.importGpx(String(trip.id), buf) as any;
const second = await svc.importGpx(String(trip.id), buf) as any;
expect(second.count).toBe(0);
const total = ((await svc.list(String(trip.id), {})) as any[]).length;
@@ -1017,21 +1017,21 @@ describe('importGpx deduplication', () => {
});
describe('importKmlPlaces deduplication', () => {
it('PLACE-SVC-035 — skips placemarks already in trip by name', () => {
it('PLACE-SVC-035 — skips placemarks already in trip by name', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const buf = fs.readFileSync(KML_FIXTURE);
const first = svc.importKmlPlaces(String(trip.id), buf);
const first = await svc.importKmlPlaces(String(trip.id), buf);
expect(first.count).toBeGreaterThan(0);
const second = svc.importKmlPlaces(String(trip.id), buf);
const second = await svc.importKmlPlaces(String(trip.id), buf);
expect(second.count).toBe(0);
expect(second.summary.skippedCount).toBeGreaterThanOrEqual(first.count);
expect(second.summary.warnings.some((w: string) => w.includes('skipped'))).toBe(true);
});
it('PLACE-SVC-036 — deduplicates within the same file (intra-batch)', () => {
it('PLACE-SVC-036 — deduplicates within the same file (intra-batch)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
// Craft a KML with two placemarks sharing the same name
@@ -1041,7 +1041,7 @@ describe('importKmlPlaces deduplication', () => {
<Placemark><name>Dupe Place</name><Point><coordinates>2.1,48.1,0</coordinates></Point></Placemark>
</Document></kml>`);
const result = svc.importKmlPlaces(String(trip.id), kml);
const result = await svc.importKmlPlaces(String(trip.id), kml);
expect(result.count).toBe(1);
expect(result.summary.skippedCount).toBe(1);
});
@@ -1113,31 +1113,31 @@ describe('custom place image reclaim', () => {
});
// ── Collaborative ratings (#1435) ──────────────────────────────────────────
it('PLACE-SVC-050 — rate stores one vote per user, replaces on re-vote, clears with null', () => {
it('PLACE-SVC-050 — rate stores one vote per user, replaces on re-vote, clears with null', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Rated' }) as { id: number };
svc.rate(String(trip.id), String(place.id), user.id, 5);
await svc.rate(String(trip.id), String(place.id), user.id, 5);
let rows = testDb.prepare('SELECT rating FROM place_ratings WHERE place_id = ? AND user_id = ?').all(place.id, user.id) as { rating: number }[];
expect(rows).toEqual([{ rating: 5 }]);
svc.rate(String(trip.id), String(place.id), user.id, 2); // re-vote replaces via the UNIQUE upsert
await svc.rate(String(trip.id), String(place.id), user.id, 2); // re-vote replaces via the UNIQUE upsert
rows = testDb.prepare('SELECT rating FROM place_ratings WHERE place_id = ? AND user_id = ?').all(place.id, user.id) as { rating: number }[];
expect(rows).toEqual([{ rating: 2 }]);
svc.rate(String(trip.id), String(place.id), user.id, null); // clear
await svc.rate(String(trip.id), String(place.id), user.id, null); // clear
const count = testDb.prepare('SELECT COUNT(*) AS n FROM place_ratings WHERE place_id = ?').get(place.id) as { n: number };
expect(count.n).toBe(0);
});
it('PLACE-SVC-051 — rate returns null and writes nothing when the place is not in the trip', () => {
it('PLACE-SVC-051 — rate returns null and writes nothing when the place is not in the trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const otherTrip = createTrip(testDb, user.id);
const place = createPlace(testDb, otherTrip.id, { name: 'Elsewhere' }) as { id: number };
expect(svc.rate(String(trip.id), String(place.id), user.id, 4)).toBeNull();
expect(await svc.rate(String(trip.id), String(place.id), user.id, 4)).toBeNull();
const count = testDb.prepare('SELECT COUNT(*) AS n FROM place_ratings WHERE place_id = ?').get(place.id) as { n: number };
expect(count.n).toBe(0);
});
@@ -1161,8 +1161,8 @@ const GPX_WITH_TRACKS = `<?xml version="1.0" encoding="UTF-8"?>
</trkseg></trk>
</gpx>`;
function importFixture(tripId: number) {
return svc.importGpx(String(tripId), Buffer.from(GPX_WITH_TRACKS), {
async function importFixture(tripId: number) {
return await svc.importGpx(String(tripId), Buffer.from(GPX_WITH_TRACKS), {
importWaypoints: true, importRoutes: true, importTracks: true,
});
}
@@ -1173,10 +1173,10 @@ function tracksOf(tripId: number) {
}
describe('PlacesService — automatic track colours (#776)', () => {
it('PLACES-SVC-001 — every imported track gets a colour from the shared palette', () => {
it('PLACES-SVC-001 — every imported track gets a colour from the shared palette', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
importFixture(trip.id);
await importFixture(trip.id);
const tracks = tracksOf(trip.id);
expect(tracks.length).toBeGreaterThan(0);
@@ -1185,10 +1185,10 @@ describe('PlacesService — automatic track colours (#776)', () => {
}
});
it('PLACES-SVC-002 — the returned places carry the colour, not just the DB rows', () => {
it('PLACES-SVC-002 — the returned places carry the colour, not just the DB rows', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const result = importFixture(trip.id) as { places: any[] } | null;
const result = await importFixture(trip.id) as { places: any[] } | null;
const returnedTracks = (result?.places ?? []).filter(p => p.route_geometry);
expect(returnedTracks.length).toBeGreaterThan(0);
@@ -1197,10 +1197,10 @@ describe('PlacesService — automatic track colours (#776)', () => {
}
});
it('PLACES-SVC-003 — plain waypoints keep no colour at all', () => {
it('PLACES-SVC-003 — plain waypoints keep no colour at all', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
importFixture(trip.id);
await importFixture(trip.id);
const waypoints = testDb.prepare(
'SELECT route_color FROM places WHERE trip_id = ? AND route_geometry IS NULL',
@@ -1210,10 +1210,10 @@ describe('PlacesService — automatic track colours (#776)', () => {
}
});
it('PLACES-SVC-004 — a second import continues the palette instead of repeating it', () => {
it('PLACES-SVC-004 — a second import continues the palette instead of repeating it', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
importFixture(trip.id);
await importFixture(trip.id);
const first = tracksOf(trip.id).map(t => t.route_color);
// Same fixture again: dedup skips the identical rows, so seed a distinct
@@ -1222,7 +1222,7 @@ describe('PlacesService — automatic track colours (#776)', () => {
"INSERT INTO places (trip_id, name, lat, lng, route_geometry) VALUES (?, 'Second walk', 1, 1, '[[1,1],[2,2]]')",
).run(trip.id);
const seeded = testDb.prepare('SELECT id FROM places WHERE name = ?').get('Second walk') as { id: number };
(svc as any).colorizeImportedTracks(String(trip.id), {
await (svc as any).colorizeImportedTracks(String(trip.id), {
places: [{ id: seeded.id, route_geometry: '[[1,1],[2,2]]', route_color: null }],
});
@@ -1231,7 +1231,7 @@ describe('PlacesService — automatic track colours (#776)', () => {
expect(first).not.toContain(seededColor);
});
it('PLACES-SVC-006 — a colour already in use by hand is not handed out again', () => {
it('PLACES-SVC-006 — a colour already in use by hand is not handed out again', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
// Someone recoloured an existing track to the palette's second entry. A
@@ -1240,16 +1240,16 @@ describe('PlacesService — automatic track colours (#776)', () => {
"INSERT INTO places (trip_id, name, lat, lng, route_geometry, route_color) VALUES (?, 'Old walk', 1, 1, '[[1,1],[2,2]]', ?)",
).run(trip.id, TRACK_COLORS[1]);
importFixture(trip.id);
await importFixture(trip.id);
const colors = tracksOf(trip.id).map(t => t.route_color);
expect(new Set(colors).size).toBe(colors.length);
});
it('PLACES-SVC-007 — gaps left by deleted tracks are reused, not skipped past', () => {
it('PLACES-SVC-007 — gaps left by deleted tracks are reused, not skipped past', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
importFixture(trip.id);
await importFixture(trip.id);
// Drop the first track; its colour becomes free again.
const first = tracksOf(trip.id)[0];
testDb.prepare('DELETE FROM places WHERE id = ?').run(first.id);
@@ -1257,7 +1257,7 @@ describe('PlacesService — automatic track colours (#776)', () => {
const seeded = testDb.prepare(
"INSERT INTO places (trip_id, name, lat, lng, route_geometry) VALUES (?, 'Later walk', 9, 9, '[[9,9],[8,8]]') RETURNING id",
).get(trip.id) as { id: number };
(svc as any).colorizeImportedTracks(String(trip.id), {
await (svc as any).colorizeImportedTracks(String(trip.id), {
places: [{ id: seeded.id, route_geometry: '[[9,9],[8,8]]', route_color: null }],
});
@@ -1265,12 +1265,12 @@ describe('PlacesService — automatic track colours (#776)', () => {
expect(new Set(colors).size).toBe(colors.length);
});
it('PLACES-SVC-005 — an import that yields nothing colours nothing', () => {
it('PLACES-SVC-005 — an import that yields nothing colours nothing', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(() => (svc as any).colorizeImportedTracks(String(trip.id), null)).not.toThrow();
expect(() => (svc as any).colorizeImportedTracks(String(trip.id), { places: [] })).not.toThrow();
await expect((svc as any).colorizeImportedTracks(String(trip.id), null)).resolves.toBeUndefined();
await expect((svc as any).colorizeImportedTracks(String(trip.id), { places: [] })).resolves.toBeUndefined();
expect(tracksOf(trip.id)).toEqual([]);
});
@@ -1394,26 +1394,26 @@ describe('enrichImportedPlaces', () => {
// replaced by the default.
describe('zero-valued numeric fields', () => {
it('PLACE-SVC-065 — create keeps lat/lng of exactly 0 instead of nulling them', () => {
it('PLACE-SVC-065 — create keeps lat/lng of exactly 0 instead of nulling them', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = svc.create(String(trip.id), { name: 'Null Island', lat: 0, lng: 0 }) as any;
const place = await svc.create(String(trip.id), { name: 'Null Island', lat: 0, lng: 0 }) as any;
expect(place.lat).toBe(0);
expect(place.lng).toBe(0);
// A genuinely absent coordinate still lands as NULL.
const noCoords = svc.create(String(trip.id), { name: 'Unlocated' }) as any;
const noCoords = await svc.create(String(trip.id), { name: 'Unlocated' }) as any;
expect(noCoords.lat).toBeNull();
expect(noCoords.lng).toBeNull();
});
it('PLACE-SVC-066 — create keeps duration_minutes and price of 0', () => {
it('PLACE-SVC-066 — create keeps duration_minutes and price of 0', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = svc.create(String(trip.id), { name: 'Drive-by', duration_minutes: 0, price: 0 }) as any;
const place = await svc.create(String(trip.id), { name: 'Drive-by', duration_minutes: 0, price: 0 }) as any;
expect(place.duration_minutes).toBe(0);
expect(place.price).toBe(0);
// The 60-minute default still applies when the field is absent.
expect((svc.create(String(trip.id), { name: 'Default' }) as any).duration_minutes).toBe(60);
expect((await svc.create(String(trip.id), { name: 'Default' }) as any).duration_minutes).toBe(60);
});
it('PLACE-SVC-067 — update can set duration_minutes to 0', async () => {
@@ -1473,7 +1473,7 @@ describe('list search escaping', () => {
// ── Trip-scoped id filter for the journey delete hook (#1745) ─────────────────
describe('scopedIds', () => {
it('PLACE-SVC-070 — returns only the ids that belong to the trip, preserving input order', () => {
it('PLACE-SVC-070 — returns only the ids that belong to the trip, preserving input order', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const other = createTrip(testDb, user.id);
@@ -1481,8 +1481,8 @@ describe('scopedIds', () => {
const b = createPlace(testDb, trip.id, { name: 'B' }) as any;
const foreign = createPlace(testDb, other.id, { name: 'Foreign' }) as any;
expect(svc.scopedIds(String(trip.id), [b.id, foreign.id, a.id, 99999])).toEqual([b.id, a.id]);
expect(svc.scopedIds(String(trip.id), [])).toEqual([]);
expect(await svc.scopedIds(String(trip.id), [b.id, foreign.id, a.id, 99999])).toEqual([b.id, a.id]);
expect(await svc.scopedIds(String(trip.id), [])).toEqual([]);
});
});
@@ -1702,26 +1702,26 @@ describe('backfillMissingAddresses', () => {
* that this service interprets it faithfully against real SQL.
*/
describe('findMatchingPlaceId', () => {
it('PLACES-SVC-008 — matches an existing place by name, ignoring case and surrounding space', () => {
it('PLACES-SVC-008 — matches an existing place by name, ignoring case and surrounding space', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Eiffel Tower' });
expect(svc.findMatchingPlaceId(String(trip.id), { name: ' eiffel tower ' })).toBe(place.id);
expect(await svc.findMatchingPlaceId(String(trip.id), { name: ' eiffel tower ' })).toBe(place.id);
});
it('PLACES-SVC-009 — matches on a provider id even after the place was renamed (#1550)', () => {
it('PLACES-SVC-009 — matches on a provider id even after the place was renamed (#1550)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Original Name' });
testDb.prepare('UPDATE places SET google_place_id = ? WHERE id = ?').run('ChIJ_abc', place.id);
expect(
svc.findMatchingPlaceId(String(trip.id), { name: 'Renamed By User', google_place_id: 'ChIJ_abc' }),
await svc.findMatchingPlaceId(String(trip.id), { name: 'Renamed By User', google_place_id: 'ChIJ_abc' }),
).toBe(place.id);
});
it('PLACES-SVC-010 — does NOT match a NAMED candidate to a different place at the same coordinates', () => {
it('PLACES-SVC-010 — does NOT match a NAMED candidate to a different place at the same coordinates', async () => {
// The restaurant and the bar in the same building are two places. This is the
// rule isPlaceDuplicate has always applied; the SQL copy used to disagree.
const { user } = createUser(testDb);
@@ -1729,17 +1729,17 @@ describe('findMatchingPlaceId', () => {
createPlace(testDb, trip.id, { name: 'Ground Floor Diner', lat: 52.52, lng: 13.405 });
expect(
svc.findMatchingPlaceId(String(trip.id), { name: 'Rooftop Bar', lat: 52.52, lng: 13.405 }),
await svc.findMatchingPlaceId(String(trip.id), { name: 'Rooftop Bar', lat: 52.52, lng: 13.405 }),
).toBeNull();
});
it('PLACES-SVC-011 — matches an UNNAMED candidate by coordinates within tolerance', () => {
it('PLACES-SVC-011 — matches an UNNAMED candidate by coordinates within tolerance', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: 'Anything', lat: 48.85, lng: 2.35 });
expect(
svc.findMatchingPlaceId(String(trip.id), {
await svc.findMatchingPlaceId(String(trip.id), {
name: null,
lat: 48.85 + COORD_DEDUP_TOLERANCE / 2,
lng: 2.35,
@@ -1747,25 +1747,25 @@ describe('findMatchingPlaceId', () => {
).toBe(place.id);
});
it('PLACES-SVC-012 — returns null when nothing recognises the candidate', () => {
it('PLACES-SVC-012 — returns null when nothing recognises the candidate', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
createPlace(testDb, trip.id, { name: 'Somewhere Else' });
expect(svc.findMatchingPlaceId(String(trip.id), { name: 'Unseen Place' })).toBeNull();
expect(svc.findMatchingPlaceId(String(trip.id), { name: null, lat: null, lng: null })).toBeNull();
expect(await svc.findMatchingPlaceId(String(trip.id), { name: 'Unseen Place' })).toBeNull();
expect(await svc.findMatchingPlaceId(String(trip.id), { name: null, lat: null, lng: null })).toBeNull();
});
it('PLACES-SVC-013 — never matches a place belonging to another trip', () => {
it('PLACES-SVC-013 — never matches a place belonging to another trip', async () => {
const { user } = createUser(testDb);
const mine = createTrip(testDb, user.id);
const theirs = createTrip(testDb, user.id);
createPlace(testDb, theirs.id, { name: 'Shared Name' });
expect(svc.findMatchingPlaceId(String(mine.id), { name: 'Shared Name' })).toBeNull();
expect(await svc.findMatchingPlaceId(String(mine.id), { name: 'Shared Name' })).toBeNull();
});
it('PLACES-SVC-014 — a provider id still wins over a name that points elsewhere', () => {
it('PLACES-SVC-014 — a provider id still wins over a name that points elsewhere', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
// The place the user renamed, carrying the id the importer knows it by.
@@ -1775,11 +1775,11 @@ describe('findMatchingPlaceId', () => {
createPlace(testDb, trip.id, { name: 'Trattoria da Enzo', lat: 41.9, lng: 12.5 });
expect(
svc.findMatchingPlaceId(String(trip.id), { name: 'Trattoria da Enzo', google_ftid: '0x1:0x2' }),
await svc.findMatchingPlaceId(String(trip.id), { name: 'Trattoria da Enzo', google_ftid: '0x1:0x2' }),
).toBe(renamed.id);
});
it('PLACES-SVC-015 — the name comparison is ASCII-only, so an accented capital does not match', () => {
it('PLACES-SVC-015 — the name comparison is ASCII-only, so an accented capital does not match', async () => {
// Not a wish, a boundary: `lower(trim(name))` is SQLite's ASCII lowercase,
// while normalizePlaceName uses JavaScript's Unicode one. isPlaceDuplicate
// does match this pair in memory. Before the shared strategies, the
@@ -1789,13 +1789,13 @@ describe('findMatchingPlaceId', () => {
const trip = createTrip(testDb, user.id);
createPlace(testDb, trip.id, { name: 'CAFÉ CENTRAL', lat: 48.85, lng: 2.35 });
expect(svc.findMatchingPlaceId(String(trip.id), { name: 'Café Central', lat: 48.85, lng: 2.35 })).toBeNull();
expect(await svc.findMatchingPlaceId(String(trip.id), { name: 'Café Central', lat: 48.85, lng: 2.35 })).toBeNull();
// The all-ASCII spelling of the same shape does match.
createPlace(testDb, trip.id, { name: 'CAFE CENTRAL', lat: 48.86, lng: 2.36 });
expect(svc.findMatchingPlaceId(String(trip.id), { name: 'Cafe Central' })).not.toBeNull();
expect(await svc.findMatchingPlaceId(String(trip.id), { name: 'Cafe Central' })).not.toBeNull();
});
it('PLACES-SVC-016 — an unnamed candidate can match a NAMED row on coordinates', () => {
it('PLACES-SVC-016 — an unnamed candidate can match a NAMED row on coordinates', async () => {
// The other place the two halves differ: buildDedupSet collects coordinates
// only for unnamed rows, so isPlaceDuplicate would say no here. This is the
// answer findMatchingPlaceId wants — a booking with no place name should
@@ -1804,10 +1804,10 @@ describe('findMatchingPlaceId', () => {
const trip = createTrip(testDb, user.id);
const hotel = createPlace(testDb, trip.id, { name: 'Hotel Lutetia', lat: 48.851, lng: 2.326 });
expect(svc.findMatchingPlaceId(String(trip.id), { name: null, lat: 48.851, lng: 2.326 })).toBe(hotel.id);
expect(await svc.findMatchingPlaceId(String(trip.id), { name: null, lat: 48.851, lng: 2.326 })).toBe(hotel.id);
});
it('PLACES-SVC-017 — the coordinate tolerance is a box roughly 11 m wide, and it closes', () => {
it('PLACES-SVC-017 — the coordinate tolerance is a box roughly 11 m wide, and it closes', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = createPlace(testDb, trip.id, { name: null, lat: 48.85, lng: 2.35 });
@@ -1818,12 +1818,12 @@ describe('findMatchingPlaceId', () => {
const inside = { name: null, lat: 48.85 + COORD_DEDUP_TOLERANCE * 0.9, lng: 2.35 };
const outside = { name: null, lat: 48.85 + COORD_DEDUP_TOLERANCE * 1.5, lng: 2.35 };
expect(svc.findMatchingPlaceId(String(trip.id), inside)).toBe(place.id);
expect(svc.findMatchingPlaceId(String(trip.id), outside)).toBeNull();
expect(await svc.findMatchingPlaceId(String(trip.id), inside)).toBe(place.id);
expect(await svc.findMatchingPlaceId(String(trip.id), outside)).toBeNull();
// Exactly one tolerance away is NOT a match: 48.85 + 0.0001 lands on
// 48.850100000000004 in binary floating point, a hair over the bound. The
// edge is fuzzy by design of the arithmetic, so nothing should lean on it.
const edge = { name: null, lat: 48.85 + COORD_DEDUP_TOLERANCE, lng: 2.35 };
expect(svc.findMatchingPlaceId(String(trip.id), edge)).toBeNull();
expect(await svc.findMatchingPlaceId(String(trip.id), edge)).toBeNull();
});
});
@@ -282,8 +282,8 @@ describe('PublicApiController — what a narrowed key reaches', () => {
const getTrip = vi.fn().mockReturnValue(TRIP);
const ctl = apiController({ listTrips, listBucketList, getTrip });
expect(await ctl.listTrips(req(ALL))).toEqual({ trips: [TRIP] });
expect(ctl.listBucketList(req(ALL))).toEqual({ items: [] });
expect(ctl.getTrip(req(ALL), '12', undefined)).toEqual(TRIP);
expect(await ctl.listBucketList(req(ALL))).toEqual({ items: [] });
expect(await ctl.getTrip(req(ALL), '12', undefined)).toEqual(TRIP);
expect(getTrip).toHaveBeenCalledWith(12, 7, [...PUBLIC_API_INCLUDES], [...PUBLIC_API_SCOPES]);
});
@@ -292,9 +292,9 @@ describe('PublicApiController — what a narrowed key reaches', () => {
expect(await apiController({ listTrips }).listTrips(req(limited('trips')))).toEqual({ trips: [TRIP] });
});
it('PUBAPI-SCOPE-U022: and is refused the bucket list before the service is ever asked', () => {
it('PUBAPI-SCOPE-U022: and is refused the bucket list before the service is ever asked', async () => {
const listBucketList = vi.fn();
const res = thrown(() => apiController({ listBucketList }).listBucketList(req(limited('trips'))));
const res = await thrownAsync(() => apiController({ listBucketList }).listBucketList(req(limited('trips'))));
expect(res.status).toBe(403);
expect(res.body).toMatchObject({ code: 'API_SCOPE_FORBIDDEN', required_scope: 'bucket-list' });
// The refusal has to happen before the read, not after it: a 403 carrying
@@ -307,35 +307,35 @@ describe('PublicApiController — what a narrowed key reaches', () => {
const getTrip = vi.fn();
const ctl = apiController({ listTrips, getTrip });
expect((await thrownAsync(() => ctl.listTrips(req(limited('bucket-list'))))).status).toBe(403);
expect(thrown(() => ctl.getTrip(req(limited('bucket-list')), '12', undefined)).status).toBe(403);
expect((await thrownAsync(() => ctl.getTrip(req(limited('bucket-list')), '12', undefined))).status).toBe(403);
expect(listTrips).not.toHaveBeenCalled();
expect(getTrip).not.toHaveBeenCalled();
});
it('PUBAPI-SCOPE-U024: a trip asked for without include is narrowed, not refused', () => {
it('PUBAPI-SCOPE-U024: a trip asked for without include is narrowed, not refused', async () => {
const getTrip = vi.fn().mockReturnValue(TRIP);
apiController({ getTrip }).getTrip(req(limited('trips', 'days', 'notes')), '12', undefined);
await apiController({ getTrip }).getTrip(req(limited('trips', 'days', 'notes')), '12', undefined);
// `include` absent means "everything"; refusing a key for wanting sections it
// never named would make a narrow key unable to read a trip at all. The grant
// rides along so the service can tell an implied day block from a granted one.
expect(getTrip).toHaveBeenCalledWith(12, 7, ['days', 'notes'], ['trips', 'days', 'notes']);
});
it('PUBAPI-SCOPE-U025: a trips-only key gets the summary and nothing that hangs off it', () => {
it('PUBAPI-SCOPE-U025: a trips-only key gets the summary and nothing that hangs off it', async () => {
const getTrip = vi.fn().mockReturnValue(TRIP);
apiController({ getTrip }).getTrip(req(limited('trips')), '12', undefined);
await apiController({ getTrip }).getTrip(req(limited('trips')), '12', undefined);
expect(getTrip).toHaveBeenCalledWith(12, 7, [], ['trips']);
});
it('PUBAPI-SCOPE-U026: an empty include is treated like an absent one, and narrowed too', () => {
it('PUBAPI-SCOPE-U026: an empty include is treated like an absent one, and narrowed too', async () => {
const getTrip = vi.fn().mockReturnValue(TRIP);
apiController({ getTrip }).getTrip(req(limited('trips', 'days')), '12', ' ');
await apiController({ getTrip }).getTrip(req(limited('trips', 'days')), '12', ' ');
expect(getTrip).toHaveBeenCalledWith(12, 7, ['days'], ['trips', 'days']);
});
it('PUBAPI-SCOPE-U027: a section named explicitly and not granted is a 403, not a silent drop', () => {
it('PUBAPI-SCOPE-U027: a section named explicitly and not granted is a 403, not a silent drop', async () => {
const getTrip = vi.fn();
const res = thrown(() =>
const res = await thrownAsync(() =>
apiController({ getTrip }).getTrip(req(limited('trips', 'days')), '12', 'days,places'),
);
expect(res).toEqual({
@@ -349,15 +349,15 @@ describe('PublicApiController — what a narrowed key reaches', () => {
expect(getTrip).not.toHaveBeenCalled();
});
it('PUBAPI-SCOPE-U028: an include the key covers is passed through untouched', () => {
it('PUBAPI-SCOPE-U028: an include the key covers is passed through untouched', async () => {
const getTrip = vi.fn().mockReturnValue(TRIP);
apiController({ getTrip }).getTrip(req(limited('trips', 'days', 'notes')), '12', 'days, notes');
await apiController({ getTrip }).getTrip(req(limited('trips', 'days', 'notes')), '12', 'days, notes');
expect(getTrip).toHaveBeenCalledWith(12, 7, ['days', 'notes'], ['trips', 'days', 'notes']);
});
it('PUBAPI-SCOPE-U029: a bad id is still a 400 — the scope check does not swallow it', () => {
it('PUBAPI-SCOPE-U029: a bad id is still a 400 — the scope check does not swallow it', async () => {
const getTrip = vi.fn();
expect(thrown(() => apiController({ getTrip }).getTrip(req(ALL), 'abc', undefined)).status).toBe(400);
expect((await thrownAsync(() => apiController({ getTrip }).getTrip(req(ALL), 'abc', undefined))).status).toBe(400);
expect(getTrip).not.toHaveBeenCalled();
});
});
@@ -50,17 +50,6 @@ const req = (userId = 7) => ({ user: { id: userId }, apiToken: FULL_GRANT }) as
/** A request the guard left no user on — the shape the controller must refuse. */
const reqWithoutUser = () => ({ apiToken: FULL_GRANT }) as Request;
function thrown(fn: () => unknown): { status: number; body: unknown } {
try {
fn();
} catch (err) {
expect(err).toBeInstanceOf(HttpException);
const e = err as HttpException;
return { status: e.getStatus(), body: e.getResponse() };
}
throw new Error('expected the handler to throw');
}
async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
try {
await fn();
@@ -74,24 +63,24 @@ async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number
describe('PublicApiController', () => {
describe('GET /api/v1/bucket-list', () => {
it('returns the caller’s wishlist and passes the id from the guard, never the query', () => {
it('returns the caller’s wishlist and passes the id from the guard, never the query', async () => {
const listBucketList = vi.fn().mockReturnValue([ITEM]);
expect(makeController({ listBucketList }).listBucketList(req(7))).toEqual({ items: [ITEM] });
expect(await makeController({ listBucketList }).listBucketList(req(7))).toEqual({ items: [ITEM] });
expect(listBucketList).toHaveBeenCalledWith(7);
});
it('401s when the guard left no user behind', () => {
it('401s when the guard left no user behind', async () => {
const listBucketList = vi.fn();
expect(thrown(() => makeController({ listBucketList }).listBucketList(reqWithoutUser()))).toEqual({
expect(await thrownAsync(() => makeController({ listBucketList }).listBucketList(reqWithoutUser()))).toEqual({
status: 401,
body: { error: 'API token required', code: 'API_TOKEN_REQUIRED' },
});
expect(listBucketList).not.toHaveBeenCalled();
});
it('counts against the same rate budget as everything else', () => {
it('counts against the same rate budget as everything else', async () => {
const listBucketList = vi.fn();
expect(thrown(() => makeController({ listBucketList }, false).listBucketList(req(7))).status).toBe(429);
expect((await thrownAsync(() => makeController({ listBucketList }, false).listBucketList(req(7)))).status).toBe(429);
expect(listBucketList).not.toHaveBeenCalled();
});
});
@@ -119,22 +108,22 @@ describe('PublicApiController', () => {
});
describe('GET /api/v1/trips/:id', () => {
it('passes the parsed id and user through, and defaults include to every section', () => {
it('passes the parsed id and user through, and defaults include to every section', async () => {
const getTrip = vi.fn().mockReturnValue(TRIP);
const res = makeController({ getTrip }).getTrip(req(7), '12', undefined);
const res = await makeController({ getTrip }).getTrip(req(7), '12', undefined);
expect(res).toEqual(TRIP);
expect(getTrip).toHaveBeenCalledWith(12, 7, [...PUBLIC_API_INCLUDES], expect.arrayContaining(['days']));
});
it('treats an empty include the same as an absent one', () => {
it('treats an empty include the same as an absent one', async () => {
const getTrip = vi.fn().mockReturnValue(TRIP);
makeController({ getTrip }).getTrip(req(7), '12', ' ');
await makeController({ getTrip }).getTrip(req(7), '12', ' ');
expect(getTrip).toHaveBeenCalledWith(12, 7, [...PUBLIC_API_INCLUDES], expect.arrayContaining(['days']));
});
it('narrows to the requested sections and tolerates spacing', () => {
it('narrows to the requested sections and tolerates spacing', async () => {
const getTrip = vi.fn().mockReturnValue(TRIP);
makeController({ getTrip }).getTrip(req(7), '12', 'days, notes');
await makeController({ getTrip }).getTrip(req(7), '12', 'days, notes');
expect(getTrip).toHaveBeenCalledWith(12, 7, ['days', 'notes'], expect.arrayContaining(['days']));
});
@@ -143,9 +132,9 @@ describe('PublicApiController', () => {
* trip that was never created. Anything else turns the endpoint into a way to
* count another user's trips.
*/
it('404s for a trip the caller may not read, with no hint that it exists', () => {
it('404s for a trip the caller may not read, with no hint that it exists', async () => {
const getTrip = vi.fn().mockReturnValue(null);
expect(thrown(() => makeController({ getTrip }).getTrip(req(7), '999', undefined))).toEqual({
expect(await thrownAsync(() => makeController({ getTrip }).getTrip(req(7), '999', undefined))).toEqual({
status: 404,
body: { error: 'Trip not found' },
});
@@ -161,9 +150,9 @@ describe('PublicApiController', () => {
['whitespace', ' 12 '],
['a sql fragment', "1 OR 1=1"],
['an id past the safe integer range', '9007199254740993'],
])('400s on %s without touching the service', (_label, raw) => {
])('400s on %s without touching the service', async (_label, raw) => {
const getTrip = vi.fn();
expect(thrown(() => makeController({ getTrip }).getTrip(req(7), raw, undefined)).status).toBe(400);
expect((await thrownAsync(() => makeController({ getTrip }).getTrip(req(7), raw, undefined))).status).toBe(400);
expect(getTrip).not.toHaveBeenCalled();
});
@@ -171,9 +160,9 @@ describe('PublicApiController', () => {
['an unknown section', 'days,everything'],
['a typo', 'dayz'],
['only commas', ',,,'],
])('400s on %s rather than silently dropping it', (_label, include) => {
])('400s on %s rather than silently dropping it', async (_label, include) => {
const getTrip = vi.fn();
const res = thrown(() => makeController({ getTrip }).getTrip(req(7), '12', include));
const res = await thrownAsync(() => makeController({ getTrip }).getTrip(req(7), '12', include));
expect(res.status).toBe(400);
expect(getTrip).not.toHaveBeenCalled();
});
@@ -187,9 +176,9 @@ describe('PublicApiController', () => {
expect(listTrips).not.toHaveBeenCalled();
});
it('429s the detail route the same way', () => {
it('429s the detail route the same way', async () => {
const getTrip = vi.fn();
expect(thrown(() => makeController({ getTrip }, false).getTrip(req(7), '12', undefined)).status).toBe(429);
expect((await thrownAsync(() => makeController({ getTrip }, false).getTrip(req(7), '12', undefined))).status).toBe(429);
expect(getTrip).not.toHaveBeenCalled();
});
});
@@ -1068,11 +1068,11 @@ describe('ReservationsService — referenced ids stay inside the trip', () => {
try { plant(); } finally { testDb.exec('PRAGMA foreign_keys = ON'); }
}
it('RESV-SCOPE-006: an id that resolves to nothing is named too, accommodation_id excepted', () => {
it('RESV-SCOPE-006: an id that resolves to nothing is named too, accommodation_id excepted', async () => {
const { mine } = twoTrips();
const gone = 999999;
expect(svc.unresolvedReferences(String(mine.id), {
expect(await svc.unresolvedReferences(String(mine.id), {
title: 'x', type: 'hotel',
day_id: gone, end_day_id: gone, place_id: gone, assignment_id: gone, accommodation_id: gone,
create_accommodation: { place_id: gone, start_day_id: gone, end_day_id: gone },
@@ -1082,37 +1082,37 @@ describe('ReservationsService — referenced ids stay inside the trip', () => {
]);
});
it('RESV-SCOPE-007: ids on the trip pass, and a falsy one is not a reference at all', () => {
it('RESV-SCOPE-007: ids on the trip pass, and a falsy one is not a reference at all', async () => {
const { mine } = twoTrips();
const place = createPlace(testDb, mine.id);
const day = testDb.prepare('SELECT id FROM days WHERE trip_id = ? ORDER BY day_number').get(mine.id) as { id: number };
const assignment = createDayAssignment(testDb, day.id, place.id);
expect(svc.unresolvedReferences(String(mine.id), {
expect(await svc.unresolvedReferences(String(mine.id), {
title: 'x', type: 'hotel',
day_id: day.id, end_day_id: day.id, place_id: place.id, assignment_id: assignment.id,
create_accommodation: { place_id: place.id, start_day_id: day.id, end_day_id: day.id },
})).toEqual([]);
// The write paths coerce these to NULL before they reach SQL, so naming
// them would 400 a body that stores a null today.
expect(svc.unresolvedReferences(String(mine.id), { title: 'x', day_id: 0, end_day_id: 0, place_id: 0, assignment_id: 0 })).toEqual([]);
expect(await svc.unresolvedReferences(String(mine.id), { title: 'x', day_id: 0, end_day_id: 0, place_id: 0, assignment_id: 0 })).toEqual([]);
// Only a hotel booking writes the stay row.
expect(svc.unresolvedReferences(String(mine.id), {
expect(await svc.unresolvedReferences(String(mine.id), {
title: 'x', type: 'flight', create_accommodation: { place_id: 999999, start_day_id: 999999, end_day_id: 999999 },
})).toEqual([]);
// A stay without a place is a state the booking form writes.
expect(svc.unresolvedReferences(String(mine.id), {
expect(await svc.unresolvedReferences(String(mine.id), {
title: 'x', type: 'hotel', create_accommodation: { start_day_id: day.id, end_day_id: day.id },
})).toEqual([]);
});
it('RESV-SCOPE-008: an id from another trip is named by both guards, so the older one answers first', () => {
it('RESV-SCOPE-008: an id from another trip is named by both guards, so the older one answers first', async () => {
const { mine, theirs } = twoTrips();
const foreignPlace = createPlace(testDb, theirs.id);
const body = { title: 'x', place_id: foreignPlace.id };
expect(svc.referencesOutsideTrip(String(mine.id), body)).toEqual(['place_id']);
expect(svc.unresolvedReferences(String(mine.id), body)).toEqual(['place_id']);
expect(await svc.unresolvedReferences(String(mine.id), body)).toEqual(['place_id']);
});
it('RESV-SCOPE-009: an update heals the stored ids whose rows are gone instead of failing on them', async () => {
@@ -96,8 +96,8 @@ afterAll(() => {
testDb.close();
});
function freshPlace(tripId: number) {
const place = places.create(String(tripId), { name: 'Original' }) as unknown as { id: number; updated_at: string };
async function freshPlace(tripId: number) {
const place = await places.create(String(tripId), { name: 'Original' }) as unknown as { id: number; updated_at: string };
return place;
}
@@ -105,7 +105,7 @@ describe('PlacesService.update — optimistic concurrency', () => {
it('updates normally when no If-Match token is sent (back-compat)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = freshPlace(trip.id);
const place = await freshPlace(trip.id);
const result = await places.update(String(trip.id), String(place.id), { name: 'Edited' });
expect(isUpdateConflict(result)).toBe(false);
@@ -115,7 +115,7 @@ describe('PlacesService.update — optimistic concurrency', () => {
it('updates when the If-Match token matches the current updated_at', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = freshPlace(trip.id);
const place = await freshPlace(trip.id);
const result = await places.update(String(trip.id), String(place.id), { name: 'Edited' }, place.updated_at);
expect(isUpdateConflict(result)).toBe(false);
@@ -125,7 +125,7 @@ describe('PlacesService.update — optimistic concurrency', () => {
it('returns a conflict (with the server row) when the token is stale', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const place = freshPlace(trip.id);
const place = await freshPlace(trip.id);
const result = await places.update(String(trip.id), String(place.id), { name: 'Mine' }, '1999-01-01 00:00:00');
expect(isUpdateConflict(result)).toBe(true);