mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
test(server): suites await the methods earlier sweep groups made async
This commit is contained in:
@@ -21,8 +21,8 @@ function makeService(overrides: Partial<AccommodationsService> = {}): Accommodat
|
||||
} as unknown as AccommodationsService;
|
||||
}
|
||||
|
||||
function thrown(fn: () => unknown): { status: number; body: unknown } {
|
||||
try { fn(); } catch (err) {
|
||||
async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
|
||||
try { await fn(); } catch (err) {
|
||||
expect(err).toBeInstanceOf(HttpException);
|
||||
const e = err as HttpException;
|
||||
return { status: e.getStatus(), body: e.getResponse() };
|
||||
@@ -39,15 +39,15 @@ describe('AccommodationsController (parity with the legacy accommodations sub-ro
|
||||
|
||||
describe('POST /', () => {
|
||||
|
||||
it('400 when refs are missing', () => {
|
||||
expect(thrown(() => new AccommodationsController(makeService()).create(user, '5', { place_id: 2 }))).toEqual({
|
||||
it('400 when refs are missing', async () => {
|
||||
expect(await thrownAsync(() => new AccommodationsController(makeService()).create(user, '5', { place_id: 2 }))).toEqual({
|
||||
status: 400, body: { error: 'place_id, start_day_id, and end_day_id are required' },
|
||||
});
|
||||
});
|
||||
|
||||
it('404 with the first validateRefs error message', () => {
|
||||
it('404 with the first validateRefs error message', async () => {
|
||||
const svc = makeService({ validateRefs: vi.fn().mockReturnValue([{ field: 'place_id', message: 'Place not found' }]) } as Partial<AccommodationsService>);
|
||||
expect(thrown(() => new AccommodationsController(svc).create(user, '5', refs))).toEqual({ status: 404, body: { error: 'Place not found' } });
|
||||
expect(await thrownAsync(() => new AccommodationsController(svc).create(user, '5', refs))).toEqual({ status: 404, body: { error: 'Place not found' } });
|
||||
});
|
||||
|
||||
it('creates and emits accommodation:created + reservation:created', async () => {
|
||||
@@ -72,9 +72,9 @@ describe('AccommodationsController (parity with the legacy accommodations sub-ro
|
||||
});
|
||||
|
||||
describe('PUT /:id', () => {
|
||||
it('404 when the accommodation is missing', () => {
|
||||
it('404 when the accommodation is missing', async () => {
|
||||
const svc = makeService({ get: vi.fn().mockReturnValue(undefined) } as Partial<AccommodationsService>);
|
||||
expect(thrown(() => new AccommodationsController(svc).update(user, '5', '9', refs))).toEqual({ status: 404, body: { error: 'Accommodation not found' } });
|
||||
expect(await thrownAsync(() => new AccommodationsController(svc).update(user, '5', '9', refs))).toEqual({ status: 404, body: { error: 'Accommodation not found' } });
|
||||
});
|
||||
|
||||
it('updates and broadcasts', async () => {
|
||||
@@ -97,12 +97,12 @@ describe('AccommodationsController (parity with the legacy accommodations sub-ro
|
||||
expect(announceMirror).toHaveBeenCalledWith('5', mirror, expect.any(Function), 'sock');
|
||||
});
|
||||
|
||||
it('ACC-CTL-003 404s a place or day that is not on this trip, before the write', () => {
|
||||
it('ACC-CTL-003 404s a place or day that is not on this trip, before the write', async () => {
|
||||
const get = vi.fn().mockReturnValue({ id: 9 });
|
||||
const update = vi.fn();
|
||||
const validateRefs = vi.fn().mockReturnValue([{ message: 'Place not found' }]);
|
||||
const svc = makeService({ get, update, validateRefs } as Partial<AccommodationsService>);
|
||||
expect(thrown(() => new AccommodationsController(svc).update(user, '5', '9', refs)))
|
||||
expect(await thrownAsync(() => new AccommodationsController(svc).update(user, '5', '9', refs)))
|
||||
.toEqual({ status: 404, body: { error: 'Place not found' } });
|
||||
expect(update).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -137,9 +137,9 @@ describe('AccommodationsController (parity with the legacy accommodations sub-ro
|
||||
});
|
||||
|
||||
describe('DELETE /:id', () => {
|
||||
it('404 when missing', () => {
|
||||
it('404 when missing', async () => {
|
||||
const svc = makeService({ get: vi.fn().mockReturnValue(undefined) } as Partial<AccommodationsService>);
|
||||
expect(thrown(() => new AccommodationsController(svc).remove(user, '5', '9'))).toEqual({ status: 404, body: { error: 'Accommodation not found' } });
|
||||
expect(await thrownAsync(() => new AccommodationsController(svc).remove(user, '5', '9'))).toEqual({ status: 404, body: { error: 'Accommodation not found' } });
|
||||
});
|
||||
|
||||
it('emits the linked reservation/budget cascade then accommodation:deleted', async () => {
|
||||
|
||||
@@ -460,7 +460,7 @@ describe('route-facing delegators', () => {
|
||||
expect(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', () => {
|
||||
it('ACC-012 — create() writes the stay, get() reads it back trip-scoped', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const otherTrip = createTrip(testDb, user.id);
|
||||
@@ -468,42 +468,42 @@ describe('route-facing delegators', () => {
|
||||
const place = createPlace(testDb, trip.id, { name: 'Hotel' }) as any;
|
||||
|
||||
// Both ids arrive from the route as strings.
|
||||
const { accommodation: created } = svc.create(String(trip.id), {
|
||||
const { accommodation: created } = (await svc.create(String(trip.id), {
|
||||
place_id: place.id, start_day_id: day.id, end_day_id: day.id, confirmation: 'XY-1',
|
||||
}) as any;
|
||||
})) as any;
|
||||
|
||||
expect(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();
|
||||
});
|
||||
|
||||
it('ACC-013 — update() syncs the partner reservation even when the id is a string', () => {
|
||||
it('ACC-013 — update() syncs the partner reservation even when the id is a string', async () => {
|
||||
// updateAccommodation looks the reservation up with Number(id); if that coercion
|
||||
// regresses, the REST path silently stops syncing check-in times.
|
||||
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 { accommodation: accom } = svc.create(trip.id, { place_id: place.id, start_day_id: day.id, end_day_id: day.id }) 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 { accommodation: updated } = svc.update(String(accom.id), existing as any, { check_in: '16:00', notes: 'late arrival' }) as any;
|
||||
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' });
|
||||
const reservation = testDb.prepare('SELECT metadata FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
|
||||
expect(JSON.parse(reservation.metadata).check_in_time).toBe('16:00');
|
||||
});
|
||||
|
||||
it('ACC-014 — remove() reports the partner reservation it took with it', () => {
|
||||
it('ACC-014 — remove() reports the partner reservation it took with it', 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 { accommodation: accom } = svc.create(trip.id, { place_id: place.id, start_day_id: day.id, end_day_id: day.id }) 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 reservation = testDb.prepare('SELECT id FROM reservations WHERE accommodation_id = ?').get(accom.id) as any;
|
||||
|
||||
// The controller broadcasts reservation:deleted off this return value.
|
||||
expect(svc.remove(String(accom.id))).toEqual({
|
||||
expect(await svc.remove(String(accom.id))).toEqual({
|
||||
linkedReservationId: reservation.id,
|
||||
deletedBudgetItemId: null,
|
||||
linkedReservationIds: [reservation.id],
|
||||
@@ -1124,7 +1124,7 @@ describe('the day stop a booking implies', () => {
|
||||
const { accommodation } = await book(trip.id, place.id, day.id, day.id);
|
||||
const stop = stopsOn(day.id)[0];
|
||||
|
||||
const { mirror } = svc.remove(accommodation.id, { keepStop: true }) as any;
|
||||
const { mirror } = (await svc.remove(accommodation.id, { keepStop: true })) as any;
|
||||
|
||||
// Days hides a stop that carries an accommodation_id, so a client still holding
|
||||
// the old row keeps the place invisible on a day it is standing on.
|
||||
|
||||
@@ -24,12 +24,21 @@ function thrown(fn: () => unknown): { status: number; body: unknown } {
|
||||
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);
|
||||
const e = err as HttpException;
|
||||
return { status: e.getStatus(), body: e.getResponse() };
|
||||
}
|
||||
throw new Error('expected throw');
|
||||
}
|
||||
|
||||
describe('DayAssignmentsController (parity with the legacy day-assignments routes)', () => {
|
||||
// The 404 "Trip not found" and 403 "No permission" cases moved to
|
||||
// trip-access.guard.test.ts with the check itself.
|
||||
it('404 day on GET', () => {
|
||||
it('404 day on GET', async () => {
|
||||
const s = svc({ dayExists: vi.fn().mockReturnValue(false) } as Partial<AssignmentsService>);
|
||||
expect(thrown(() => new DayAssignmentsController(s).list(user, '5', '9'))).toEqual({ status: 404, body: { error: 'Day not found' } });
|
||||
expect(await thrownAsync(() => new DayAssignmentsController(s).list(user, '5', '9'))).toEqual({ status: 404, body: { error: 'Day not found' } });
|
||||
});
|
||||
|
||||
it('GET returns assignments (access-only, no permission gate)', async () => {
|
||||
@@ -38,11 +47,11 @@ describe('DayAssignmentsController (parity with the legacy day-assignments route
|
||||
});
|
||||
|
||||
describe('POST', () => {
|
||||
it('404 place not found; then creates + hooks', () => {
|
||||
expect(thrown(() => new DayAssignmentsController(svc({ placeExists: vi.fn().mockReturnValue(false) } as Partial<AssignmentsService>)).create(user, '5', '3', { place_id: 2 }))).toEqual({ status: 404, body: { error: 'Place not found' } });
|
||||
it('404 place not found; then creates + hooks', async () => {
|
||||
expect(await thrownAsync(() => new DayAssignmentsController(svc({ placeExists: vi.fn().mockReturnValue(false) } as Partial<AssignmentsService>)).create(user, '5', '3', { place_id: 2 }))).toEqual({ status: 404, body: { error: 'Place not found' } });
|
||||
const createAssignment = vi.fn().mockReturnValue({ id: 9 }); const broadcast = vi.fn(); const reconcile = vi.fn();
|
||||
const s = svc({ createAssignment, broadcast, reconcile } as Partial<AssignmentsService>);
|
||||
expect(new DayAssignmentsController(s).create(user, '5', '3', { place_id: 2, notes: 'n' }, 'sock')).toEqual({ assignment: { id: 9 } });
|
||||
expect(await new DayAssignmentsController(s).create(user, '5', '3', { place_id: 2, notes: 'n' }, 'sock')).toEqual({ assignment: { id: 9 } });
|
||||
expect(createAssignment).toHaveBeenCalledWith('3', 2, 'n');
|
||||
expect(broadcast).toHaveBeenCalledWith('5', 'assignment:created', { assignment: { id: 9 } }, 'sock');
|
||||
expect(reconcile).toHaveBeenCalledWith('5', 'sock');
|
||||
@@ -67,21 +76,21 @@ describe('DayAssignmentsController (parity with the legacy day-assignments route
|
||||
});
|
||||
|
||||
describe('AssignmentOpsController (parity with the per-assignment op routes)', () => {
|
||||
it('scopes explicit day ends to the trip and broadcasts the saved visit', () => {
|
||||
it('scopes explicit day ends to the trip and broadcasts the saved visit', async () => {
|
||||
const setEndDay = vi.fn().mockReturnValue({ id: 9, end_day: true });
|
||||
const s = svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ id: 9 }), setEndDay });
|
||||
const controller = new AssignmentOpsController(s);
|
||||
expect(controller.endDay('5', '9', { end_day: true }, 'sock')).toEqual({ assignment: { id: 9, end_day: true } });
|
||||
expect(await controller.endDay('5', '9', { end_day: true }, 'sock')).toEqual({ assignment: { id: 9, end_day: true } });
|
||||
expect(s.getAssignmentForTrip).toHaveBeenCalledWith('9', '5');
|
||||
expect(setEndDay).toHaveBeenCalledWith('9', true);
|
||||
expect(s.broadcast).toHaveBeenCalledWith('5', 'assignment:updated', { assignment: { id: 9, end_day: true } }, 'sock');
|
||||
vi.mocked(s.getAssignmentForTrip).mockReturnValue(undefined);
|
||||
expect(thrown(() => controller.endDay('5', '9', { end_day: false }))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
expect(await thrownAsync(() => controller.endDay('5', '9', { end_day: false }))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
});
|
||||
|
||||
it('PUT /:id/move 404 assignment, 404 target day, else moves', async () => {
|
||||
expect(thrown(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined) } as Partial<AssignmentsService>)).move(user, '5', '9', { new_day_id: 4 }))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
expect(thrown(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ day_id: 3 }), dayExists: vi.fn().mockReturnValue(false) } as Partial<AssignmentsService>)).move(user, '5', '9', { new_day_id: 4 }))).toEqual({ status: 404, body: { error: 'Target day not found' } });
|
||||
expect(await thrownAsync(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined) } as Partial<AssignmentsService>)).move(user, '5', '9', { new_day_id: 4 }))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
expect(await thrownAsync(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ day_id: 3 }), dayExists: vi.fn().mockReturnValue(false) } as Partial<AssignmentsService>)).move(user, '5', '9', { new_day_id: 4 }))).toEqual({ status: 404, body: { error: 'Target day not found' } });
|
||||
const moveAssignment = vi.fn().mockReturnValue({ assignment: { id: 9 }, oldDayId: 3 }); const broadcast = vi.fn(); const reconcile = vi.fn();
|
||||
const s = svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ day_id: 3 }), moveAssignment, broadcast, reconcile } as Partial<AssignmentsService>);
|
||||
expect(await new AssignmentOpsController(s).move(user, '5', '9', { new_day_id: 4, order_index: 0 }, 'sock')).toEqual({ assignment: { id: 9 } });
|
||||
@@ -98,7 +107,7 @@ describe('AssignmentOpsController (parity with the per-assignment op routes)', (
|
||||
});
|
||||
|
||||
it('PUT /:id/time 404 missing, else updates', async () => {
|
||||
expect(thrown(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined) } as Partial<AssignmentsService>)).time(user, '5', '9', {}))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
expect(await thrownAsync(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined) } as Partial<AssignmentsService>)).time(user, '5', '9', {}))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
const updateTime = vi.fn().mockReturnValue({ assignment: { id: 9 }, reordered: null, vias: null }); const broadcast = vi.fn(); const reconcile = vi.fn();
|
||||
const s = svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ id: 9 }), updateTime, broadcast, reconcile } as Partial<AssignmentsService>);
|
||||
expect(await new AssignmentOpsController(s).time(user, '5', '9', { place_time: '10:00' }, 'sock')).toEqual({ assignment: { id: 9 } });
|
||||
@@ -125,11 +134,11 @@ describe('AssignmentOpsController (parity with the per-assignment op routes)', (
|
||||
]);
|
||||
});
|
||||
|
||||
it('PUT /:id/notes 404 missing, else updates + broadcasts (#2163)', () => {
|
||||
expect(thrown(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined) } as Partial<AssignmentsService>)).notes(user, '5', '9', { notes: 'n' }))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
it('PUT /:id/notes 404 missing, else updates + broadcasts (#2163)', async () => {
|
||||
expect(await thrownAsync(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined) } as Partial<AssignmentsService>)).notes(user, '5', '9', { notes: 'n' }))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
const updateNotes = vi.fn().mockReturnValue({ id: 9, notes: 'Book the 10:00 entry' }); const broadcast = vi.fn(); const reconcile = vi.fn();
|
||||
const s = svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ id: 9 }), updateNotes, broadcast, reconcile } as Partial<AssignmentsService>);
|
||||
expect(new AssignmentOpsController(s).notes(user, '5', '9', { notes: 'Book the 10:00 entry' }, 'sock')).toEqual({ assignment: { id: 9, notes: 'Book the 10:00 entry' } });
|
||||
expect(await new AssignmentOpsController(s).notes(user, '5', '9', { notes: 'Book the 10:00 entry' }, 'sock')).toEqual({ assignment: { id: 9, notes: 'Book the 10:00 entry' } });
|
||||
expect(updateNotes).toHaveBeenCalledWith('9', 'Book the 10:00 entry');
|
||||
expect(broadcast).toHaveBeenCalledWith('5', 'assignment:updated', { assignment: { id: 9, notes: 'Book the 10:00 entry' } }, 'sock');
|
||||
// The note touches neither day order nor the journey skeleton — no reconcile.
|
||||
|
||||
@@ -182,7 +182,7 @@ describe('createAssignment', () => {
|
||||
const { user, trip, day, place } = fixture();
|
||||
const tag = createTag(testDb, user.id, { name: 'museum' });
|
||||
testDb.prepare('INSERT INTO place_tags (place_id, tag_id) VALUES (?, ?)').run(place.id, tag.id);
|
||||
const a = svc.createAssignment(day.id, place.id, 'skip the line');
|
||||
const a = await svc.createAssignment(day.id, place.id, 'skip the line');
|
||||
expect(a).toMatchObject({
|
||||
day_id: day.id,
|
||||
place_id: place.id,
|
||||
@@ -616,10 +616,10 @@ describe('updateTime', () => {
|
||||
});
|
||||
|
||||
describe('updateNotes (#2163)', () => {
|
||||
it('ASG-SVC-030: persists the note and re-selects the nested shape', () => {
|
||||
it('ASG-SVC-030: persists the note and re-selects the nested shape', async () => {
|
||||
const { day, place } = fixture();
|
||||
const a = createDayAssignment(testDb, day.id, place.id);
|
||||
const updated = svc.updateNotes(a.id, 'Book the 10:00 timed entry');
|
||||
const updated = await svc.updateNotes(a.id, 'Book the 10:00 timed entry');
|
||||
expect(updated).toMatchObject({ id: a.id, notes: 'Book the 10:00 timed entry' });
|
||||
expect(testDb.prepare('SELECT notes FROM day_assignments WHERE id = ?').get(a.id)).toEqual({ notes: 'Book the 10:00 timed entry' });
|
||||
});
|
||||
@@ -638,7 +638,7 @@ describe('updateNotes (#2163)', () => {
|
||||
const { day, place } = fixture();
|
||||
const a = createDayAssignment(testDb, day.id, place.id, { order_index: 3 });
|
||||
await svc.updateTime(a.id, '09:00', '10:00');
|
||||
const updated = svc.updateNotes(a.id, 'note');
|
||||
const updated = await svc.updateNotes(a.id, 'note');
|
||||
expect(updated).toMatchObject({ assignment_time: '09:00', assignment_end_time: '10:00' });
|
||||
// 3, not 0: a start on a day already in order no longer renumbers it, so the key
|
||||
// this note edit must not touch is still the one the stop was created with.
|
||||
|
||||
@@ -90,20 +90,21 @@ describe('Google route import', () => {
|
||||
});
|
||||
|
||||
it('rolls back every created stop when any assignment fails and sends no events', async () => {
|
||||
const { service, db, places, assignments } = await setup();
|
||||
const sqlite = new Database(':memory:');
|
||||
sqlite.exec('CREATE TABLE stops (name TEXT)');
|
||||
db.transaction = fn => sqlite.transaction(fn)();
|
||||
// The write goes through this.uow.transactional now, not db.transaction, and
|
||||
// the UnitOfWork MikroORM binds in setup() shares routeTestDb's connection —
|
||||
// so a raw statement against that same handle is what proves the rollback.
|
||||
const { service, places, assignments } = await setup();
|
||||
routeTestDb.exec('CREATE TABLE stops (name TEXT)');
|
||||
places.create.mockImplementation((_trip, stop) => {
|
||||
sqlite.prepare('INSERT INTO stops VALUES (?)').run(stop.name);
|
||||
routeTestDb.prepare('INSERT INTO stops VALUES (?)').run(stop.name);
|
||||
return { id: stop.name };
|
||||
});
|
||||
assignments.createAssignment.mockImplementationOnce(() => ({ dayId: 3, placeId: 'Munich' })).mockImplementationOnce(() => { throw new Error('write failed'); });
|
||||
try {
|
||||
await expect(service.import(1, 7, input)).rejects.toThrow('write failed');
|
||||
expect(sqlite.prepare('SELECT * FROM stops').all()).toEqual([]);
|
||||
expect(routeTestDb.prepare('SELECT * FROM stops').all()).toEqual([]);
|
||||
expect(places.broadcast).not.toHaveBeenCalled();
|
||||
expect(assignments.broadcast).not.toHaveBeenCalled();
|
||||
} finally { sqlite.close(); }
|
||||
} finally { routeTestDb.exec('DROP TABLE stops'); }
|
||||
});
|
||||
});
|
||||
|
||||
@@ -61,6 +61,17 @@ function thrown(fn: () => unknown): { status: number; body: unknown } {
|
||||
throw new Error('expected the handler to throw');
|
||||
}
|
||||
|
||||
async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
|
||||
try {
|
||||
await 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');
|
||||
}
|
||||
|
||||
describe('PublicApiController', () => {
|
||||
describe('GET /api/v1/bucket-list', () => {
|
||||
it('returns the caller’s wishlist and passes the id from the guard, never the query', () => {
|
||||
@@ -97,9 +108,9 @@ describe('PublicApiController', () => {
|
||||
expect(await makeController({ listTrips }).listTrips(req(7))).toEqual({ trips: [] });
|
||||
});
|
||||
|
||||
it('401s if the guard was somehow bypassed and no user is attached', () => {
|
||||
it('401s if the guard was somehow bypassed and no user is attached', async () => {
|
||||
const listTrips = vi.fn();
|
||||
expect(thrown(() => makeController({ listTrips }).listTrips(reqWithoutUser()))).toEqual({
|
||||
expect(await thrownAsync(() => makeController({ listTrips }).listTrips(reqWithoutUser()))).toEqual({
|
||||
status: 401,
|
||||
body: { error: 'API token required', code: 'API_TOKEN_REQUIRED' },
|
||||
});
|
||||
@@ -169,9 +180,9 @@ describe('PublicApiController', () => {
|
||||
});
|
||||
|
||||
describe('rate limiting', () => {
|
||||
it('429s the list once the budget is spent, without reaching the service', () => {
|
||||
it('429s the list once the budget is spent, without reaching the service', async () => {
|
||||
const listTrips = vi.fn();
|
||||
const res = thrown(() => makeController({ listTrips }, false).listTrips(req(7)));
|
||||
const res = await thrownAsync(() => makeController({ listTrips }, false).listTrips(req(7)));
|
||||
expect(res).toEqual({ status: 429, body: { error: 'Too many requests. Please slow down.' } });
|
||||
expect(listTrips).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -17,9 +17,9 @@ const notice: SystemNoticeDto = {
|
||||
};
|
||||
|
||||
/** Run `fn`, expecting an HttpException; return its { status, body }. */
|
||||
function thrown(fn: () => unknown): { status: number; body: unknown } {
|
||||
async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
|
||||
try {
|
||||
fn();
|
||||
await fn();
|
||||
} catch (err) {
|
||||
expect(err).toBeInstanceOf(HttpException);
|
||||
const e = err as HttpException;
|
||||
@@ -79,9 +79,9 @@ describe('SystemNoticesController (parity with the legacy /api/system-notices ro
|
||||
expect(dismiss).toHaveBeenCalledWith(7, 'welcome');
|
||||
});
|
||||
|
||||
it('404 { error: NOTICE_NOT_FOUND } when the id is unknown', () => {
|
||||
it('404 { error: NOTICE_NOT_FOUND } when the id is unknown', async () => {
|
||||
const dismiss = vi.fn().mockReturnValue(false);
|
||||
expect(thrown(() => makeController({ dismiss }).dismiss(user, 'nope'))).toEqual({
|
||||
expect(await thrownAsync(() => makeController({ dismiss }).dismiss(user, 'nope'))).toEqual({
|
||||
status: 404,
|
||||
body: { error: 'NOTICE_NOT_FOUND' },
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user