Files
TREK/server/tests/unit/nest/places-library/places-library.controller.test.ts
T
Maurice 0ecca2b46c feat(places): read every place of the trips and lists in one view
A read-only overview of every place a user can reach: the saved places of
their lists and the places of their trips, viewer trips included, with tours,
tracks and the stops of a drive left out. A saved place and a trip place are
one entry only when the saved place came from that trip place, the trip is
still open to the user, and both agree (a provider id, or the same name at the
same spot); anything else stays apart. Every entry names where it comes from
and whether it may be changed there.

- GET /api/addons/collections/library/places: paged by cursor, search,
  category, kind, list, trip and status filters, counts per kind.
- GET /api/addons/collections/library/places/{trip|list}/🆔 one entry with
  what each origin keeps on its own (notes after the existing rights, labels,
  ratings, a trip's planned days, whether a stay or booking uses it).
- MCP tools list_all_places and get_library_place.

Nothing is written and nothing migrates; the rights are the existing list and
trip checks, read on every call.
2026-11-29 14:57:10 +01:00

50 lines
2.4 KiB
TypeScript

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