Files
TREK/shared/src/collection/collection.schema.spec.ts
T
Maurice 82165855c7 feat(collections): own order, KML/KMZ import, copy options, position search, place names on the trip map
- A list keeps its own order: drag a place by its grip (desktop and phone) or
  move it with the arrow keys. Reordering is off while a search or filter hides
  places and the list says why. New PUT /collections/:id/places/reorder and the
  MCP tool reorder_collection_places; new places go to the end. Client and
  server now order ties the same way (sort_order, oldest first, id).
- Copying saved places into a trip no longer takes their notes and tags along
  unasked. The picker offers both; a tag taken along becomes a trip tag (a trip
  tag of the same name is reused), never someone's personal tag.
- The source trip and place a saved place names are only kept when the user can
  open that trip and the place belongs to it.
- A KML or KMZ map can be read into a list: the dialog lists every placemark to
  keep or leave out and says which entries were skipped and why
  (POST /collections/map/read writes nothing; the import does).
- A saved place without coordinates says so and can be looked up on the map
  from its edit form, on the phone too, where a missing position could not be
  added at all. A typed address without a pick says it will not be on the map.
- The trip map can show the place names beside the pins (desktop and phone,
  both renderers). Names that would cover another pin or a control give way,
  the selected place and the planned stops first.
2026-11-29 14:57:00 +01:00

197 lines
9.0 KiB
TypeScript

import {
collectionCopyToTripRequestSchema,
collectionDeleteManyRequestSchema,
collectionItemsRequestSchema,
collectionMembershipSchema,
collectionPlacesPageRequestSchema,
collectionPlacesReorderRequestSchema,
collectionPlaceUpdateRequestSchema,
collectionReorderRequestSchema,
COLLECTION_PLACES_PAGE_DEFAULT,
collectionSavePlaceRequestSchema,
collectionStatusSchema,
isVisitedCollectionStatus,
} from './collection.schema';
import { describe, expect, it } from 'vitest';
describe('collectionStatusSchema', () => {
// #2567: "want to revisit" is a status of its own, stored as it is.
it('accepts revisit next to the first three', () => {
for (const status of ['idea', 'want', 'visited', 'revisit'] as const) {
expect(collectionStatusSchema.parse(status)).toBe(status);
}
});
it('counts revisit as visited, and nothing before it', () => {
expect(isVisitedCollectionStatus('visited')).toBe(true);
expect(isVisitedCollectionStatus('revisit')).toBe(true);
expect(isVisitedCollectionStatus('want')).toBe(false);
expect(isVisitedCollectionStatus('idea')).toBe(false);
});
});
describe('collectionPlaceUpdateRequestSchema', () => {
// #2646: the fields saving takes, with the same checks, so a place is corrected in place.
it('takes the provider ids, website, phone and price, and null clears each', () => {
const set = {
google_place_id: 'ChIJseptime',
google_ftid: '0x47e6:0x1b2',
osm_id: 'node/42',
website: 'https://septime-charonne.fr',
phone: '+33 1 43 67 38 29',
price: 55,
currency: 'EUR',
};
expect(collectionPlaceUpdateRequestSchema.parse(set)).toEqual(set);
const cleared = Object.fromEntries(Object.keys(set).map((key) => [key, null]));
expect(collectionPlaceUpdateRequestSchema.parse(cleared)).toEqual(cleared);
});
it('normalises a bare domain as website and refuses a script link, as saving does', () => {
expect(collectionPlaceUpdateRequestSchema.parse({ website: 'septime-charonne.fr' }).website).toBe(
'https://septime-charonne.fr',
);
expect(collectionPlaceUpdateRequestSchema.safeParse({ website: 'javascript:alert(1)' }).success).toBe(false);
});
it('leaves the new fields absent when an update does not name them', () => {
const parsed = collectionPlaceUpdateRequestSchema.parse({ name: 'Septime' });
for (const key of ['google_place_id', 'google_ftid', 'osm_id', 'website', 'phone', 'price', 'currency']) {
expect(key in parsed, key).toBe(false);
}
});
// Regression for #1437: an update that omits `status` must NOT inject a default
// of 'idea', otherwise the server overwrites the stored status on every edit-save.
it('does not inject a status when the field is absent', () => {
const parsed = collectionPlaceUpdateRequestSchema.parse({ name: 'Trevi Fountain' });
expect('status' in parsed).toBe(false);
expect(parsed.status).toBeUndefined();
});
it('passes through an explicitly provided status', () => {
expect(collectionPlaceUpdateRequestSchema.parse({ status: 'want' }).status).toBe('want');
expect(collectionPlaceUpdateRequestSchema.parse({ status: 'visited' }).status).toBe('visited');
});
it('catches an invalid status back to idea rather than throwing', () => {
expect(collectionPlaceUpdateRequestSchema.parse({ status: 'bogus' as never }).status).toBe('idea');
});
// #1870: the update contract knew no `address`, so the validation pipe stripped
// it and the address of a saved place could never be corrected.
it('passes an address through and accepts null to clear it', () => {
expect(collectionPlaceUpdateRequestSchema.parse({ address: 'Via Nuova 1' }).address).toBe('Via Nuova 1');
expect(collectionPlaceUpdateRequestSchema.parse({ address: null }).address).toBeNull();
});
it('leaves an absent address absent, so an unrelated edit keeps the stored one', () => {
const parsed = collectionPlaceUpdateRequestSchema.parse({ name: 'Trevi Fountain' });
expect('address' in parsed).toBe(false);
expect(parsed.address).toBeUndefined();
});
// Save and update must cap the address the same way (neither does): a limit on
// one side only would reject on edit what saving accepted.
it('caps the address on neither the save nor the update side', () => {
const long = 'x'.repeat(5000);
expect(collectionPlaceUpdateRequestSchema.parse({ address: long }).address).toBe(long);
expect(collectionSavePlaceRequestSchema.parse({ collection_id: 1, name: 'X', address: long }).address).toBe(long);
});
});
// The two ratchet schemas replace hand-rolled "array of numbers" checks — they
// must stay exactly that permissive (empty arrays included, no int/positive).
describe('collectionReorderRequestSchema / collectionDeleteManyRequestSchema', () => {
it('accepts any array of numbers, empty included', () => {
expect(collectionReorderRequestSchema.parse({ orderedIds: [] }).orderedIds).toEqual([]);
expect(collectionReorderRequestSchema.parse({ orderedIds: [3, 1, 2] }).orderedIds).toEqual([3, 1, 2]);
expect(collectionDeleteManyRequestSchema.parse({ ids: [] }).ids).toEqual([]);
expect(collectionDeleteManyRequestSchema.parse({ ids: [7] }).ids).toEqual([7]);
});
it('rejects non-arrays and non-numeric members', () => {
expect(() => collectionReorderRequestSchema.parse({ orderedIds: 'nope' })).toThrow();
expect(() => collectionReorderRequestSchema.parse({})).toThrow();
expect(() => collectionDeleteManyRequestSchema.parse({ ids: ['a'] })).toThrow();
});
});
describe('collectionItemsRequestSchema', () => {
it('takes saved place and label ids, each optional, at most 50 places and 20 labels', () => {
expect(collectionItemsRequestSchema.parse({})).toEqual({});
const ids = (count: number) => Array.from({ length: count }, (_, i) => i + 1);
const asked = { placeIds: [1, 2], labelIds: [3] };
expect(collectionItemsRequestSchema.parse(asked)).toEqual(asked);
expect(() => collectionItemsRequestSchema.parse({ placeIds: ids(51) })).toThrow();
expect(() => collectionItemsRequestSchema.parse({ labelIds: ids(21) })).toThrow();
expect(() => collectionItemsRequestSchema.parse({ placeIds: [0] })).toThrow();
});
});
describe('collectionMembershipSchema', () => {
it("reads a list's colour where the server sends it, and a list without one from an older server", () => {
const list = { collection_id: 1, name: 'Paris', place_id: 2, status: 'want', can_edit: true };
const coloured = collectionMembershipSchema.parse({ saved: true, lists: [{ ...list, color: '#e11d48' }] });
expect(coloured.lists[0]?.color).toBe('#e11d48');
expect(collectionMembershipSchema.parse({ saved: true, lists: [list] }).lists[0]?.color).toBeUndefined();
});
});
describe('collectionPlacesPageRequestSchema', () => {
// #2647: a page of a list for list_collection_places.
it('takes a list alone, or a cursor, a limit of 1 to 500 and the filters', () => {
expect(collectionPlacesPageRequestSchema.safeParse({ collectionId: 4 }).success).toBe(true);
const full = {
collectionId: 4,
cursor: 'WzAsIiIsMV0',
limit: 500,
status: 'revisit',
labelId: 7,
unlabeled: false,
};
expect(collectionPlacesPageRequestSchema.parse(full)).toEqual(full);
for (const page of [
{},
{ collectionId: 4, limit: 0 },
{ collectionId: 4, limit: 501 },
{ collectionId: 4, status: 'gone' },
]) {
expect(collectionPlacesPageRequestSchema.safeParse(page).success, JSON.stringify(page)).toBe(false);
}
expect(COLLECTION_PLACES_PAGE_DEFAULT).toBe(100);
});
});
describe('collectionCopyToTripRequestSchema', () => {
// D9: notes and tags only travel when asked for, so an older body still parses and carries neither.
it('takes the old body as it was, and the two opt-ins beside it', () => {
expect(collectionCopyToTripRequestSchema.parse({ trip_id: 3, place_ids: [1] })).toEqual({
trip_id: 3,
place_ids: [1],
});
const full = { trip_id: 3, place_ids: [1, 2], force: true, include_notes: true, tag_ids: [7, 8] };
expect(collectionCopyToTripRequestSchema.parse(full)).toEqual(full);
});
it('refuses a fractional tag id and more than 50 of them', () => {
expect(collectionCopyToTripRequestSchema.safeParse({ trip_id: 3, place_ids: [1], tag_ids: [1.5] }).success).toBe(
false,
);
const many = Array.from({ length: 51 }, (_, i) => i + 1);
expect(collectionCopyToTripRequestSchema.safeParse({ trip_id: 3, place_ids: [1], tag_ids: many }).success).toBe(
false,
);
});
});
describe('collectionPlacesReorderRequestSchema', () => {
// PLC-03: a list's places in the order its members want them.
it('takes the ids in order and refuses an empty list or a fractional id', () => {
expect(collectionPlacesReorderRequestSchema.parse({ orderedIds: [5, 3, 4] })).toEqual({ orderedIds: [5, 3, 4] });
expect(collectionPlacesReorderRequestSchema.safeParse({ orderedIds: [] }).success).toBe(false);
expect(collectionPlacesReorderRequestSchema.safeParse({ orderedIds: [1.5] }).success).toBe(false);
});
});