Files
TREK/server/tests/unit/nest/places-library/place-identities.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

70 lines
3.4 KiB
TypeScript

/**
* 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<PlaceIdentitiesService>, duplicates: Partial<PlaceDuplicatesService> = {}) {
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);
});
});