/** * PlacesLibraryController over a stub service: each route hands the signed-in * user (id and role) and its input to the one service method, and answers * with what it returns. Guards, the id pipe and the response contracts run in * the e2e suite. */ import { RESPONSE_CONTRACT_KEY } from '../../../../src/nest/common/response-contract'; import { PlacesLibraryController } from '../../../../src/nest/places-library/places-library.controller'; import type { PlacesLibraryService } from '../../../../src/nest/places-library/places-library.service'; import type { User } from '../../../../src/types'; import { placesLibraryDetailResponseSchema, placesLibraryPageResponseSchema } from '@trek/shared'; import { describe, expect, it, vi } from 'vitest'; const user = { id: 7, role: 'admin', username: 'ann', email: 'ann@example.test' } as User; function controller(service: Partial) { return new PlacesLibraryController(service as unknown as PlacesLibraryService); } describe('PlacesLibraryController', () => { it('GET library/places hands the query to the list with the user and its role', async () => { const page = { entries: [], total: 0, counts: { all: 0, trip: 0, list: 0 }, next_cursor: null }; const list = vi.fn().mockResolvedValue(page); const query = { q: 'cafe', kind: 'list' as const, limit: 20 }; expect(await controller({ list }).list(user, query)).toBe(page); expect(list).toHaveBeenCalledWith({ id: 7, role: 'admin' }, query); }); it('GET library/places/trip/:id and list/:id open one entry by the origin they name', async () => { const answer = { entry: { key: 'trip:3' }, details: [] }; const detail = vi.fn().mockResolvedValue(answer); const c = controller({ detail }); expect(await c.tripPlace(user, 3)).toBe(answer); expect(await c.savedPlace(user, 9)).toBe(answer); expect(detail.mock.calls).toEqual([ [{ id: 7, role: 'admin' }, 'trip', 3], [{ id: 7, role: 'admin' }, 'list', 9], ]); }); it('declares the shared response contracts', () => { const proto = PlacesLibraryController.prototype; const contract = (handler: unknown) => Reflect.getMetadata(RESPONSE_CONTRACT_KEY, handler as object) as unknown; expect(contract(proto.list)).toBe(placesLibraryPageResponseSchema); expect(contract(proto.tripPlace)).toBe(placesLibraryDetailResponseSchema); expect(contract(proto.savedPlace)).toBe(placesLibraryDetailResponseSchema); }); });