Files
TREK/client/src/api/placeIdentities.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

58 lines
2.7 KiB
TypeScript

// FE-API-PLACE-IDENTITIES-001 to FE-API-PLACE-IDENTITIES-002
//
// D5/PLC-13: each method pins its verb, its path and its body or query, and
// hands back what the server answered.
import { http, HttpResponse } from 'msw';
import { beforeEach, describe, expect, it } from 'vitest';
import { server } from '../../tests/helpers/msw/server';
import { placeIdentitiesApi } from './placeIdentities';
const BASE = '/api/addons/collections/library';
let calls: Array<[string, string, Record<string, string>, unknown]> = [];
beforeEach(() => {
calls = [];
});
function record(data: object) {
return async ({ request }: { request: Request }) => {
const url = new URL(request.url);
const text = request.method === 'GET' ? '' : await request.text();
calls.push([request.method, url.pathname, Object.fromEntries(url.searchParams), text ? JSON.parse(text) : null]);
return HttpResponse.json(data);
};
}
describe('placeIdentitiesApi', () => {
it('FE-API-PLACE-IDENTITIES-001: links and unlinks uses of an own place', async () => {
const answer = { entry: null };
server.use(http.post(`${BASE}/places/link`, record(answer)), http.post(`${BASE}/places/unlink`, record(answer)));
expect(await placeIdentitiesApi.link({ uses: [{ kind: 'trip', id: 3 }], identity_id: 2 })).toEqual(answer);
expect(await placeIdentitiesApi.unlink({ kind: 'list', id: 4 })).toEqual(answer);
expect(calls).toEqual([
['POST', `${BASE}/places/link`, {}, { uses: [{ kind: 'trip', id: 3 }], identity_id: 2 }],
['POST', `${BASE}/places/unlink`, {}, { kind: 'list', id: 4 }],
]);
});
it('FE-API-PLACE-IDENTITIES-002: reads duplicates, dismisses, merges and undoes', async () => {
server.use(
http.get(`${BASE}/duplicates`, record({ duplicates: [], next_cursor: null })),
http.post(`${BASE}/duplicates/dismiss`, record({ success: true })),
http.post(`${BASE}/merges`, record({ merge_id: 5, entry: null })),
http.post(`${BASE}/merges/5/undo`, record({ success: true }))
);
expect(await placeIdentitiesApi.duplicates({ limit: 10 })).toEqual({ duplicates: [], next_cursor: null });
expect(await placeIdentitiesApi.dismiss('trip:1', 'list:2')).toEqual({ success: true });
expect(await placeIdentitiesApi.merge('trip:1', 'list:2')).toEqual({ merge_id: 5, entry: null });
expect(await placeIdentitiesApi.undoMerge(5)).toEqual({ success: true });
expect(calls).toEqual([
['GET', `${BASE}/duplicates`, { limit: '10' }, null],
['POST', `${BASE}/duplicates/dismiss`, {}, { a: 'trip:1', b: 'list:2' }],
['POST', `${BASE}/merges`, {}, { leading: 'trip:1', other: 'list:2' }],
['POST', `${BASE}/merges/5/undo`, {}, null],
]);
});
});