Files
TREK/server/tests/e2e/places-library.e2e.test.ts
T
Maurice 0ecca2b46c feat(places): read every place of the trips and lists in one view
A read-only overview of every place a user can reach: the saved places of
their lists and the places of their trips, viewer trips included, with tours,
tracks and the stops of a drive left out. A saved place and a trip place are
one entry only when the saved place came from that trip place, the trip is
still open to the user, and both agree (a provider id, or the same name at the
same spot); anything else stays apart. Every entry names where it comes from
and whether it may be changed there.

- GET /api/addons/collections/library/places: paged by cursor, search,
  category, kind, list, trip and status filters, counts per kind.
- GET /api/addons/collections/library/places/{trip|list}/🆔 one entry with
  what each origin keeps on its own (notes after the existing rights, labels,
  ratings, a trip's planned days, whether a stay or booking uses it).
- MCP tools list_all_places and get_library_place.

Nothing is written and nothing migrates; the rights are the existing list and
trip checks, read on every call.
2026-11-29 14:57:10 +01:00

138 lines
5.7 KiB
TypeScript

/**
* "All places" (PLC-07/PLC-08) through the real AddonGuard and JwtAuthGuard
* against a migrated and seeded temp SQLite db, every answer checked against
* its response contract. The collections' own routes are mounted beside it,
* so the list is shown to reach its handler and not the collections' `GET /:id`.
*/
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
import { ZodValidationPipe } from '../../src/nest/common/zod-validation.pipe';
import { PlacesLibraryModule } from '../../src/nest/places-library/places-library.module';
import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
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 { expectedKeysByName, 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, vi } from 'vitest';
vi.mock('../../src/nest/audit/audit-log.logger', () => ({
LOG_LEVEL: 'error',
logInfo: vi.fn(),
logDebug: vi.fn(),
logError: vi.fn(),
logWarn: vi.fn(),
}));
const db = createSnapshotTestDb();
const BASE = '/api/addons/collections/library/places';
let app: INestApplication;
let server: Server;
let orm: TestOrm;
let f: LibraryFixture;
const as = (userId: number) => ({ Cookie: sessionCookie(userId) });
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
await TestUnitOfWorkModule.forRoot(db),
await createTestMikroOrmModule(db),
RealtimeModule,
PlacesLibraryModule,
],
})
.overrideProvider(RealtimeService)
.useValue(new FakeRealtimeService())
.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();
f = await seedLibrary(orm);
await setAddonEnabled(orm, 'collections', true);
});
afterAll(async () => {
await app.close();
await orm.close();
db.close();
});
describe('Places library e2e (real guards + temp SQLite)', () => {
it('answers 404 while Collections is off, before any sign-in check, and 401 without a session', async () => {
await setAddonEnabled(orm, 'collections', false);
expect((await request(server).get(BASE)).status).toBe(404);
expect((await request(server).get(`${BASE}/trip/${f.places.cafe.id}`)).status).toBe(404);
await setAddonEnabled(orm, 'collections', true);
expect((await request(server).get(BASE)).status).toBe(401);
});
it('lists every entry the user reaches, page by page', async () => {
const res = await request(server).get(BASE).set(as(f.me.id));
expect(res.status).toBe(200);
expect(res.body.entries.map((entry: { key: string }) => entry.key)).toEqual(expectedKeysByName(f));
expect(res.body.counts).toEqual({ all: 10, trip: 5, list: 7 });
const first = await request(server).get(BASE).query({ limit: '4' }).set(as(f.me.id));
expect(first.body.entries).toHaveLength(4);
const next = await request(server)
.get(BASE)
.query({ limit: '50', cursor: first.body.next_cursor })
.set(as(f.me.id));
expect(next.body.entries).toHaveLength(6);
expect(next.body.next_cursor).toBeNull();
});
it('reads the filters from the query string and refuses what it does not take', async () => {
const trips = await request(server)
.get(BASE)
.query({ kind: 'trip', trip_id: String(f.rome.id) })
.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 both = await request(server)
.get(BASE)
.query({ kind: 'list', trip_id: String(f.alps.id) })
.set(as(f.me.id));
expect(both.status).toBe(400);
expect(both.body).toEqual({ error: 'Filter by a trip or by a list, not both' });
});
it('opens one entry from a trip place or a saved place, and nothing out of reach', async () => {
const trip = await request(server).get(`${BASE}/trip/${f.places.cafe.id}`).set(as(f.me.id));
expect(trip.status).toBe(200);
expect(trip.body.entry.key).toBe(`trip:${f.places.cafe.id}`);
expect(trip.body.details.map((detail: { kind: string }) => detail.kind)).toEqual(['trip', 'list']);
const saved = await request(server).get(`${BASE}/list/${f.saved.pantheon.id}`).set(as(f.me.id));
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));
expect(res.status).toBe(404);
expect(res.body).toEqual({ error: 'Place not found' });
}
});
});