Files
TREK/server/tests/e2e/trip-collections.e2e.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

142 lines
5.7 KiB
TypeScript

/**
* 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: [] });
});
});