Files
TREK/server/tests/unit/nest/trip-collections/trip-collections.controller.test.ts
T
Maurice f69a4c1a24 feat(places): own place identities, duplicate merging and lists linked to a trip
- A user can group the uses of one place (trip places and saved places) into a
  place identity of their own (user_places, user_place_links). It is personal:
  it groups their "All places" and is never written into a shared trip or list
  row. An identity without a use they can still reach is not shown, and one
  left without links is pruned on their next write and nightly.
- Possible duplicates are suggested server side (same provider id, or the same
  name at the same spot), each pair with what differs and whether the user may
  merge it. A merge joins two identities in one transaction and can be undone;
  a dismissed pair does not come back. No trip or list row is deleted or changed.
- A trip can link lists (trip_collection_links, needs place_edit and access to
  the list). A member sees a linked list only with their own access to it; the
  map layer lists its places and whether each is already in the trip.
  tripCollections:changed tells the members to read their view again.
- REST, MCP tools and client data layer for all of it; migrations on both
  engines, additive only.
- CI: the stage 1 to 2 places-library helper types, and the await-in-loop and
  non-null counts it had raised.
2026-11-29 15:57:05 +01:00

57 lines
3.2 KiB
TypeScript

/**
* 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);
});
});