mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
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.
This commit is contained in:
@@ -25,6 +25,7 @@ import {
|
||||
createDayAssignment,
|
||||
} from '../helpers/factories';
|
||||
import { countRows, findRow } from '../helpers/factories/rows';
|
||||
import { oneByOne } from '../helpers/one-by-one';
|
||||
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
|
||||
import { TestUnitOfWorkModule } from '../helpers/test-uow';
|
||||
import { sessionCookie } from './harness';
|
||||
@@ -892,13 +893,10 @@ describe('Collections e2e (real auth guard + real service + temp SQLite)', () =>
|
||||
const as = (userId: number) => ({ Cookie: sessionCookie(userId) });
|
||||
const col = (await request(server).post('/api/addons/collections').set(as(ownerId)).send({ name: 'Ordered' })).body;
|
||||
const ids: number[] = [];
|
||||
for (const name of ['One', 'Two', 'Three']) {
|
||||
const saved = await request(server)
|
||||
.post('/api/addons/collections/places')
|
||||
.set(as(ownerId))
|
||||
.send({ collection_id: col.id, name });
|
||||
ids.push(saved.body.place.id);
|
||||
}
|
||||
const saves = await oneByOne(['One', 'Two', 'Three'], (name) =>
|
||||
request(server).post('/api/addons/collections/places').set(as(ownerId)).send({ collection_id: col.id, name }),
|
||||
);
|
||||
ids.push(...saves.map((saved) => saved.body.place.id as number));
|
||||
const reorder = (userId: number | null, orderedIds: number[]) => {
|
||||
const req = request(server).put(`/api/addons/collections/${col.id}/places/reorder`);
|
||||
return (userId === null ? req : req.set(as(userId))).send({ orderedIds });
|
||||
|
||||
@@ -12,6 +12,7 @@ import { RealtimeService } from '../../src/nest/realtime/realtime.service';
|
||||
import { createSnapshotTestDb } from '../helpers/db-mock';
|
||||
import { setAddonEnabled } from '../helpers/factories/settings';
|
||||
import { FakeRealtimeService } from '../helpers/fake-realtime';
|
||||
import { oneByOne } from '../helpers/one-by-one';
|
||||
import { expectedKeysByName, seedLibrary, type LibraryFixture } from '../helpers/places-library-fixture';
|
||||
import { resetTestDb } from '../helpers/test-db';
|
||||
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
|
||||
@@ -107,9 +108,11 @@ describe('Places library e2e (real guards + temp SQLite)', () => {
|
||||
.set(as(f.me.id));
|
||||
expect(trips.status).toBe(200);
|
||||
expect(trips.body.entries.map((entry: { key: string }) => entry.key)).toEqual([`trip:${f.places.forum.id}`]);
|
||||
for (const query of [{ category_id: '0x5' }, { kind: 'tour' }, { limit: '1000' }, { notes: 'x' }]) {
|
||||
expect((await request(server).get(BASE).query(query).set(as(f.me.id))).status).toBe(400);
|
||||
}
|
||||
const refused = await oneByOne(
|
||||
[{ category_id: '0x5' }, { kind: 'tour' }, { limit: '1000' }, { notes: 'x' }],
|
||||
(query) => request(server).get(BASE).query(query).set(as(f.me.id)),
|
||||
);
|
||||
expect(refused.map((res) => res.status)).toEqual([400, 400, 400, 400]);
|
||||
const both = await request(server)
|
||||
.get(BASE)
|
||||
.query({ kind: 'list', trip_id: String(f.alps.id) })
|
||||
@@ -128,10 +131,95 @@ describe('Places library e2e (real guards + temp SQLite)', () => {
|
||||
expect(saved.status).toBe(200);
|
||||
expect(saved.body.entry.origins[0]).toMatchObject({ kind: 'list', can_edit: false });
|
||||
|
||||
for (const path of [`trip/${f.places.hidden.id}`, `list/${f.saved.secret.id}`, 'trip/abc', 'list/0x5']) {
|
||||
const res = await request(server).get(`${BASE}/${path}`).set(as(f.me.id));
|
||||
const missing = await oneByOne(
|
||||
[`trip/${f.places.hidden.id}`, `list/${f.saved.secret.id}`, 'trip/abc', 'list/0x5'],
|
||||
(path) => request(server).get(`${BASE}/${path}`).set(as(f.me.id)),
|
||||
);
|
||||
for (const res of missing) {
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Place not found' });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('Own places, duplicates and merges e2e (real guards + temp SQLite)', () => {
|
||||
const LIBRARY = '/api/addons/collections/library';
|
||||
|
||||
it('answers 404 while Collections is off and 401 without a session', async () => {
|
||||
await setAddonEnabled(orm, 'collections', false);
|
||||
expect((await request(server).get(`${LIBRARY}/duplicates`)).status).toBe(404);
|
||||
await setAddonEnabled(orm, 'collections', true);
|
||||
expect((await request(server).post(`${LIBRARY}/places/link`).send({ uses: [] })).status).toBe(401);
|
||||
});
|
||||
|
||||
it('links, unlinks, suggests, merges and undoes, each answer checked against its contract', async () => {
|
||||
const link = await request(server)
|
||||
.post(`${LIBRARY}/places/link`)
|
||||
.set(as(f.me.id))
|
||||
.send({
|
||||
uses: [
|
||||
{ kind: 'trip', id: f.places.forum.id },
|
||||
{ kind: 'list', id: f.saved.pantheon.id },
|
||||
],
|
||||
});
|
||||
expect(link.status).toBe(200);
|
||||
expect(link.body.entry.key).toBe(`own:${link.body.entry.identity_id}`);
|
||||
const unlink = await request(server)
|
||||
.post(`${LIBRARY}/places/unlink`)
|
||||
.set(as(f.me.id))
|
||||
.send({ kind: 'list', id: f.saved.pantheon.id });
|
||||
expect(unlink.status).toBe(200);
|
||||
expect(unlink.body.entry.key).toBe(`list:${f.saved.pantheon.id}`);
|
||||
|
||||
const duplicates = await request(server).get(`${LIBRARY}/duplicates`).query({ limit: '1' }).set(as(f.me.id));
|
||||
expect(duplicates.status).toBe(200);
|
||||
expect(duplicates.body.duplicates).toHaveLength(1);
|
||||
expect(duplicates.body.next_cursor).not.toBeNull();
|
||||
const [pair] = duplicates.body.duplicates;
|
||||
|
||||
const merge = await request(server)
|
||||
.post(`${LIBRARY}/merges`)
|
||||
.set(as(f.me.id))
|
||||
.send({ leading: pair.a.key, other: pair.b.key });
|
||||
expect(merge.status).toBe(200);
|
||||
const undo = await request(server).post(`${LIBRARY}/merges/${merge.body.merge_id}/undo`).set(as(f.me.id));
|
||||
expect(undo.status).toBe(200);
|
||||
expect(undo.body).toEqual({ success: true });
|
||||
const dismiss = await request(server)
|
||||
.post(`${LIBRARY}/duplicates/dismiss`)
|
||||
.set(as(f.me.id))
|
||||
.send({ a: pair.a.key, b: pair.b.key });
|
||||
expect(dismiss.body).toEqual({ success: true });
|
||||
});
|
||||
|
||||
it('refuses what it does not take, and what the user cannot reach', async () => {
|
||||
const bad = [
|
||||
{ uses: [{ kind: 'trip', id: '0x5' }] },
|
||||
{ uses: [] },
|
||||
{ uses: [{ kind: 'tour', id: 1 }] },
|
||||
{ uses: [{ kind: 'trip', id: 1 }], extra: true },
|
||||
];
|
||||
const refused = await oneByOne(bad, (body) =>
|
||||
request(server).post(`${LIBRARY}/places/link`).set(as(f.me.id)).send(body),
|
||||
);
|
||||
expect(refused.map((res) => res.status)).toEqual(bad.map(() => 400));
|
||||
const hidden = await request(server)
|
||||
.post(`${LIBRARY}/places/link`)
|
||||
.set(as(f.me.id))
|
||||
.send({ uses: [{ kind: 'trip', id: f.places.hidden.id }] });
|
||||
expect(hidden.status).toBe(404);
|
||||
expect(hidden.body).toEqual({ error: 'Place not found' });
|
||||
const merge = await request(server)
|
||||
.post(`${LIBRARY}/merges`)
|
||||
.set(as(f.me.id))
|
||||
.send({ leading: 'trip:abc', other: 'list:1' });
|
||||
expect(merge.status).toBe(400);
|
||||
const undos = await oneByOne(['abc', '0x5', '999999'], (id) =>
|
||||
request(server).post(`${LIBRARY}/merges/${id}/undo`).set(as(f.me.id)),
|
||||
);
|
||||
for (const undo of undos) {
|
||||
expect(undo.status).toBe(404);
|
||||
expect(undo.body).toEqual({ error: 'Merge not found' });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* The lists a trip draws on (PLC-15/PLC-16) through the real AddonGuard,
|
||||
* JwtAuthGuard and TripAccessGuard against a migrated and seeded temp SQLite
|
||||
* db, every answer checked against its response contract.
|
||||
*/
|
||||
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
|
||||
import { ZodValidationPipe } from '../../src/nest/common/zod-validation.pipe';
|
||||
import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
|
||||
import { RealtimeService } from '../../src/nest/realtime/realtime.service';
|
||||
import { TripCollectionsModule } from '../../src/nest/trip-collections/trip-collections.module';
|
||||
import { createSnapshotTestDb } from '../helpers/db-mock';
|
||||
import { setAddonEnabled } from '../helpers/factories/settings';
|
||||
import { addTripMember } from '../helpers/factories/trips';
|
||||
import { FakeRealtimeService } from '../helpers/fake-realtime';
|
||||
import { oneByOne } from '../helpers/one-by-one';
|
||||
import { seedLibrary, type LibraryFixture } from '../helpers/places-library-fixture';
|
||||
import { resetTestDb } from '../helpers/test-db';
|
||||
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
|
||||
import { TestUnitOfWorkModule } from '../helpers/test-uow';
|
||||
import { sessionCookie } from './harness';
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
import { Test } from '@nestjs/testing';
|
||||
|
||||
import cookieParser from 'cookie-parser';
|
||||
import type { Server } from 'http';
|
||||
import request from 'supertest';
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
||||
|
||||
const db = createSnapshotTestDb();
|
||||
const realtime = new FakeRealtimeService();
|
||||
|
||||
let app: INestApplication;
|
||||
let server: Server;
|
||||
let orm: TestOrm;
|
||||
let f: LibraryFixture;
|
||||
|
||||
const as = (userId: number) => ({ Cookie: sessionCookie(userId) });
|
||||
const base = (tripId: number) => `/api/trips/${tripId}/collections`;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
imports: [
|
||||
await TestUnitOfWorkModule.forRoot(db),
|
||||
await createTestMikroOrmModule(db),
|
||||
RealtimeModule,
|
||||
TripCollectionsModule,
|
||||
],
|
||||
})
|
||||
.overrideProvider(RealtimeService)
|
||||
.useValue(realtime)
|
||||
.compile();
|
||||
app = moduleRef.createNestApplication();
|
||||
app.use(cookieParser());
|
||||
app.useGlobalPipes(new ZodValidationPipe());
|
||||
app.useGlobalFilters(new TrekExceptionFilter());
|
||||
await app.init();
|
||||
server = app.getHttpServer() as Server;
|
||||
orm = await createTestOrm(db);
|
||||
});
|
||||
beforeEach(async () => {
|
||||
resetTestDb(db);
|
||||
orm.clear();
|
||||
realtime.reset();
|
||||
f = await seedLibrary(orm);
|
||||
await setAddonEnabled(orm, 'collections', true);
|
||||
});
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
await orm.close();
|
||||
db.close();
|
||||
});
|
||||
|
||||
describe('Trip collections e2e (real guards + temp SQLite)', () => {
|
||||
it('answers 404 while Collections is off, 401 without a session and 404 for a trip out of reach', async () => {
|
||||
await setAddonEnabled(orm, 'collections', false);
|
||||
expect((await request(server).get(base(f.alps.id))).status).toBe(404);
|
||||
await setAddonEnabled(orm, 'collections', true);
|
||||
expect((await request(server).get(base(f.alps.id))).status).toBe(401);
|
||||
const hidden = await request(server).get(base(f.elsewhere.id)).set(as(f.me.id));
|
||||
expect(hidden.status).toBe(404);
|
||||
expect(hidden.body).toEqual({ error: 'Trip not found' });
|
||||
});
|
||||
|
||||
it('links a list, lists it, maps its places and unlinks it, skipping the sender socket', async () => {
|
||||
const link = await request(server)
|
||||
.post(base(f.alps.id))
|
||||
.set(as(f.me.id))
|
||||
.set('X-Socket-Id', '42')
|
||||
.send({ collection_id: f.lists.food.id });
|
||||
expect(link.status).toBe(200);
|
||||
expect(link.body.links).toEqual([expect.objectContaining({ collection_id: f.lists.food.id, role: 'owner' })]);
|
||||
expect(realtime.broadcastMock).toHaveBeenCalledWith(f.alps.id, 'tripCollections:changed', {}, '42');
|
||||
|
||||
const layer = await request(server)
|
||||
.get(`${base(f.alps.id)}/places`)
|
||||
.set(as(f.me.id));
|
||||
expect(layer.status).toBe(200);
|
||||
expect(layer.body.places.some((place: { in_trip: boolean }) => place.in_trip)).toBe(true);
|
||||
|
||||
const unlink = await request(server)
|
||||
.delete(`${base(f.alps.id)}/${f.lists.food.id}`)
|
||||
.set(as(f.me.id));
|
||||
expect(unlink.status).toBe(200);
|
||||
expect(unlink.body).toEqual({ links: [], can_link: true });
|
||||
});
|
||||
|
||||
it('refuses a body it does not take, a list out of reach and a bad id', async () => {
|
||||
const refused = await oneByOne(
|
||||
[{}, { collection_id: '0x5' }, { collection_id: 1.5 }, { collection_id: 1, x: 1 }],
|
||||
(body) => request(server).post(base(f.alps.id)).set(as(f.me.id)).send(body),
|
||||
);
|
||||
expect(refused.map((res) => res.status)).toEqual([400, 400, 400, 400]);
|
||||
const secret = await request(server)
|
||||
.post(base(f.alps.id))
|
||||
.set(as(f.me.id))
|
||||
.send({ collection_id: f.lists.secrets.id });
|
||||
expect(secret.status).toBe(404);
|
||||
expect(secret.body).toEqual({ error: 'Collection not found' });
|
||||
const missing = await oneByOne(['abc', '0x5'], (id) =>
|
||||
request(server)
|
||||
.delete(`${base(f.alps.id)}/${id}`)
|
||||
.set(as(f.me.id)),
|
||||
);
|
||||
for (const res of missing) {
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Collection not found' });
|
||||
}
|
||||
});
|
||||
|
||||
it('shows a member nothing of a list closed to them', async () => {
|
||||
await request(server).post(base(f.alps.id)).set(as(f.me.id)).send({ collection_id: f.lists.food.id });
|
||||
await addTripMember(orm, f.alps.id, f.friend.id);
|
||||
const res = await request(server).get(base(f.alps.id)).set(as(f.friend.id));
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.links).toEqual([]);
|
||||
const layer = await request(server)
|
||||
.get(`${base(f.alps.id)}/places`)
|
||||
.set(as(f.friend.id));
|
||||
expect(layer.body).toEqual({ places: [] });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user