/** * PlaceIdentitiesController over stub services: each route hands the user and * its input to one service method and answers what it returns. Guards, pipes * and the response contracts run in the e2e suite. */ import { RESPONSE_CONTRACT_KEY } from '../../../../src/nest/common/response-contract'; import type { PlaceDuplicatesService } from '../../../../src/nest/places-library/place-duplicates.service'; import { PlaceIdentitiesController } from '../../../../src/nest/places-library/place-identities.controller'; import type { PlaceIdentitiesService } from '../../../../src/nest/places-library/place-identities.service'; import type { User } from '../../../../src/types'; import { placesLibraryDuplicatesResponseSchema, placesLibraryEntryResponseSchema, placesLibraryMergeResponseSchema, successResponseSchema, } from '@trek/shared'; import { describe, expect, it, vi } from 'vitest'; const user = { id: 7, role: 'user', username: 'ann', email: 'ann@example.test' } as User; const me = { id: 7, role: 'user' }; function controller(identities: Partial, duplicates: Partial = {}) { return new PlaceIdentitiesController( identities as unknown as PlaceIdentitiesService, duplicates as unknown as PlaceDuplicatesService, ); } describe('PlaceIdentitiesController', () => { it('PIDC-001: link and unlink hand the uses on with the user', async () => { const answer = { entry: null }; const link = vi.fn().mockResolvedValue(answer); const unlink = vi.fn().mockResolvedValue(answer); const c = controller({ link, unlink }); const body = { uses: [{ kind: 'trip' as const, id: 3 }] }; expect(await c.link(user, body)).toBe(answer); expect(await c.unlink(user, { kind: 'list', id: 4 })).toBe(answer); expect(link).toHaveBeenCalledWith(me, body); expect(unlink).toHaveBeenCalledWith(me, { kind: 'list', id: 4 }); }); it('PIDC-002: duplicates, dismiss, merge and undo reach their service methods', async () => { const list = vi.fn().mockResolvedValue({ duplicates: [], next_cursor: null }); const dismiss = vi.fn().mockResolvedValue({ success: true }); const merge = vi.fn().mockResolvedValue({ merge_id: 2, entry: null }); const undoMerge = vi.fn().mockResolvedValue({ success: true }); const c = controller({ dismiss, merge, undoMerge }, { list }); await c.listDuplicates(user, { limit: 5 }); await c.dismiss(user, { a: 'trip:1', b: 'list:2' }); await c.merge(user, { leading: 'trip:1', other: 'list:2' }); await c.undoMerge(user, 2); expect(list).toHaveBeenCalledWith(me, { limit: 5 }); expect(dismiss).toHaveBeenCalledWith(7, 'trip:1', 'list:2'); expect(merge).toHaveBeenCalledWith(me, 'trip:1', 'list:2'); expect(undoMerge).toHaveBeenCalledWith(me, 2); }); it('PIDC-003: declares the shared response contracts', () => { const proto = PlaceIdentitiesController.prototype; const contract = (handler: unknown) => Reflect.getMetadata(RESPONSE_CONTRACT_KEY, handler as object) as unknown; expect(contract(proto.link)).toBe(placesLibraryEntryResponseSchema); expect(contract(proto.unlink)).toBe(placesLibraryEntryResponseSchema); expect(contract(proto.listDuplicates)).toBe(placesLibraryDuplicatesResponseSchema); expect(contract(proto.dismiss)).toBe(successResponseSchema); expect(contract(proto.merge)).toBe(placesLibraryMergeResponseSchema); expect(contract(proto.undoMerge)).toBe(successResponseSchema); }); });