Files
TREK/server/tests/unit/nest/places-library/place-duplicates.helpers.test.ts
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

94 lines
3.6 KiB
TypeScript

/**
* The pure half of the duplicate suggestions (PLC-13): which entries pair up,
* found by bucket and never by comparing every entry with every other, which
* facts differ, and the page cursor.
*/
import {
decodeDuplicatesCursor,
differingFields,
DUPLICATE_BUCKET_MAX,
encodeDuplicatesCursor,
findDuplicates,
pairKey,
type DuplicateCandidate,
} from '../../../../src/nest/places-library/place-duplicates.helpers';
import { COORD_DEDUP_TOLERANCE } from '@trek/shared';
import { describe, expect, it } from 'vitest';
const candidate = (key: string, extra: Partial<DuplicateCandidate> = {}): DuplicateCandidate => ({
key,
name: 'Café',
lat: 48.2,
lng: 16.3,
address: null,
category_id: null,
google_place_id: null,
google_ftid: null,
osm_id: null,
provider_ids: [],
...extra,
});
describe('place duplicates helpers', () => {
it('PDUP-001: pairs the same name within the tolerance, across a cell border too, and nothing farther', () => {
const edge = Math.floor(48.2 / COORD_DEDUP_TOLERANCE) * COORD_DEDUP_TOLERANCE;
const pairs = findDuplicates([
candidate('trip:1', { lat: edge - COORD_DEDUP_TOLERANCE / 4 }),
candidate('list:2', { name: ' CAFÉ ', lat: edge + COORD_DEDUP_TOLERANCE / 4 }),
candidate('list:3', { lat: 48.3 }),
candidate('list:4', { name: 'Bar' }),
candidate('list:5', { lat: null, lng: null }),
]);
expect(pairs.map((pair) => [pair.a.key, pair.b.key, pair.reasons])).toEqual([
['list:2', 'trip:1', ['name_nearby']],
]);
});
it('PDUP-002: pairs a shared provider id whatever the names and positions, once per pair with both reasons', () => {
const pairs = findDuplicates([
candidate('own:1', { provider_ids: ['g-1', 'node/2'] }),
candidate('trip:9', { name: 'Elsewhere', lat: 1, lng: 1, provider_ids: ['g-1'] }),
candidate('list:3', { provider_ids: ['g-1'] }),
]);
expect(pairs.map((pair) => pairKey(pair.a.key, pair.b.key))).toEqual([
'list:3|own:1',
'list:3|trip:9',
'own:1|trip:9',
]);
expect(pairs[0]?.reasons).toEqual(['provider_id', 'name_nearby']);
expect(pairs[1]?.reasons).toEqual(['provider_id']);
});
it('PDUP-003: leaves a bucket out that is too full to be a duplicate', () => {
const crowd = Array.from({ length: DUPLICATE_BUCKET_MAX + 1 }, (_, index) =>
candidate(`list:${index + 1}`, { provider_ids: ['chain'], lat: index, lng: index, name: `Shop ${index}` }),
);
expect(findDuplicates(crowd)).toEqual([]);
});
it('PDUP-004: names the facts two sides differ in', () => {
const a = candidate('trip:1', { address: 'Main 1', category_id: 2, google_place_id: 'g' });
expect(
differingFields(a, candidate('list:2', { address: ' main 1 ', category_id: 2, google_place_id: 'g' })),
).toEqual([]);
expect(differingFields(a, candidate('list:3', { name: 'Bar', lat: 50, osm_id: 'n' }))).toEqual([
'name',
'position',
'address',
'category',
'google_place_id',
'osm_id',
]);
expect(differingFields(candidate('a:1', { lat: null }), candidate('b:1', { lng: null }))).not.toContain('position');
expect(differingFields(candidate('a:1', { lat: null }), candidate('b:1'))).toContain('position');
});
it('PDUP-005: hands out a cursor it reads back, and refuses anything else', () => {
const cursor = encodeDuplicatesCursor('list:3|trip:9');
expect(decodeDuplicatesCursor(cursor)).toBe('list:3|trip:9');
expect(decodeDuplicatesCursor('nonsense')).toBeNull();
expect(decodeDuplicatesCursor(encodeDuplicatesCursor('list:3'))).toBeNull();
});
});