/** * TripCollectionsController over a stub service: each route hands the trip * the guard resolved, the user and its input (and the X-Socket-Id on writes) * to one service method. Guards, pipes and contracts run in the e2e suite. */ import type { TripAccess } from '../../../../src/db/repositories/Trips.repository'; import { RESPONSE_CONTRACT_KEY } from '../../../../src/nest/common/response-contract'; import type { TripWriter } from '../../../../src/nest/common/trip-writer'; import { TRIP_PERMISSION_KEY } from '../../../../src/nest/permissions/trip-access.guard'; import { TripCollectionsController } from '../../../../src/nest/trip-collections/trip-collections.controller'; import type { TripCollectionsService } from '../../../../src/nest/trip-collections/trip-collections.service'; import type { User } from '../../../../src/types'; import { tripCollectionLayerResponseSchema, tripCollectionLinksResponseSchema } 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 trip = { id: 4, user_id: 7 } as TripAccess; const answer = { links: [], can_link: true }; describe('TripCollectionsController', () => { it('TCLC-001: reads the links and the map layer of the trip for the user', async () => { const list = vi.fn().mockResolvedValue(answer); const layer = vi.fn().mockResolvedValue({ places: [] }); const c = new TripCollectionsController({ list, layer } as unknown as TripCollectionsService); expect(await c.list(user, trip)).toBe(answer); expect(await c.layer(user, trip)).toEqual({ places: [] }); expect(list).toHaveBeenCalledWith(4, user); expect(layer).toHaveBeenCalledWith(4, user); }); it('TCLC-002: links and unlinks with a REST writer that carries the socket id', async () => { const writer = { userId: 7 } as TripWriter; const restWriter = vi.fn().mockReturnValue(writer); const link = vi.fn().mockResolvedValue(answer); const unlink = vi.fn().mockResolvedValue(answer); const c = new TripCollectionsController({ restWriter, link, unlink } as unknown as TripCollectionsService); await c.link(user, trip, { collection_id: 9 }, 'sock-2'); await c.unlink(user, trip, 9, 'sock-2'); expect(restWriter).toHaveBeenCalledWith(4, user, 'sock-2'); expect(link).toHaveBeenCalledWith(4, 9, writer); expect(unlink).toHaveBeenCalledWith(4, 9, writer); }); it('TCLC-003: the writes take place_edit, and every route declares its contract', () => { const proto = TripCollectionsController.prototype; const meta = (key: string, handler: unknown) => Reflect.getMetadata(key, handler as object) as unknown; expect(meta(TRIP_PERMISSION_KEY, proto.link)).toBe('place_edit'); expect(meta(TRIP_PERMISSION_KEY, proto.unlink)).toBe('place_edit'); expect(meta(TRIP_PERMISSION_KEY, proto.list)).toBeUndefined(); expect(meta(RESPONSE_CONTRACT_KEY, proto.list)).toBe(tripCollectionLinksResponseSchema); expect(meta(RESPONSE_CONTRACT_KEY, proto.link)).toBe(tripCollectionLinksResponseSchema); expect(meta(RESPONSE_CONTRACT_KEY, proto.unlink)).toBe(tripCollectionLinksResponseSchema); expect(meta(RESPONSE_CONTRACT_KEY, proto.layer)).toBe(tripCollectionLayerResponseSchema); }); });