mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
f69a4c1a24
- 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.
226 lines
9.2 KiB
TypeScript
226 lines
9.2 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 { 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';
|
|
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}`]);
|
|
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) })
|
|
.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 });
|
|
|
|
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' });
|
|
}
|
|
});
|
|
});
|