test(db): run the transports, trip members and trip warnings MCP suites on the real database module

They seed through the booted container's ORM instead of the SQLite snapshot
mock and reset through resetDatabase, so they run in the Postgres lane as
well. The trip warnings suite seeded through the factories but only closed
the snapshot handle; it now boots the container to reach the ORM the reset
needs.
This commit is contained in:
Maurice
2026-11-17 19:56:40 +01:00
parent bedb70c479
commit fff7d123f6
5 changed files with 406 additions and 411 deletions
-3
View File
@@ -66,9 +66,6 @@
"tests/unit/mcp/tools-notifications.test.ts": 1,
"tests/unit/mcp/tools-packing-advanced.test.ts": 1,
"tests/unit/mcp/tools-prompts.test.ts": 1,
"tests/unit/mcp/tools-transports.test.ts": 1,
"tests/unit/mcp/tools-trip-members.test.ts": 1,
"tests/unit/mcp/tools-trip-warnings.test.ts": 1,
"tests/unit/mcp/tools-trips.test.ts": 1,
"tests/unit/mcp/tools-vacay.test.ts": 1,
"tests/unit/nest/accommodations.service.test.ts": 1,
@@ -1,9 +1,6 @@
[
"tests/unit/mcp/tools-notifications.test.ts",
"tests/unit/mcp/tools-packing-advanced.test.ts",
"tests/unit/mcp/tools-transports.test.ts",
"tests/unit/mcp/tools-trip-members.test.ts",
"tests/unit/mcp/tools-trip-warnings.test.ts",
"tests/unit/mcp/tools-trips.test.ts",
"tests/unit/mcp/tools-vacay.test.ts",
"tests/unit/nest/route-inventory.test.ts",
+153 -158
View File
@@ -4,7 +4,6 @@
* lat/lng/timezone from the airport database (the columns are NOT NULL), and
* endpoints that can't be resolved produce a clean error instead of a SQL crash.
*/
import { db as testDb } from '../../../src/db/database';
import { BudgetItems } from '../../../src/db/entities/BudgetItems.entity';
import { DayAccommodations } from '../../../src/db/entities/DayAccommodations.entity';
import { ReservationEndpoints } from '../../../src/db/entities/ReservationEndpoints.entity';
@@ -12,23 +11,20 @@ import { Reservations } from '../../../src/db/entities/Reservations.entity';
import { Trips } from '../../../src/db/entities/Trips.entity';
import { AirtrailLinkService } from '../../../src/nest/integrations/airtrail-link.service';
import { ReservationsService } from '../../../src/nest/reservations/reservations.service';
import { createUser, createTrip, createDay, createPlace } from '../../helpers/factories';
import { makeBudgetItem } from '../../helpers/factories/budget';
import { makePlace } from '../../helpers/factories/places';
import { countRows, findRow, findRows, insertRow, updateRows } from '../../helpers/factories/rows';
import { makeDay } from '../../helpers/factories/trips';
import { makeDay, makeTrip } from '../../helpers/factories/trips';
import { makeUser } from '../../helpers/factories/users';
import { FakeRealtimeService } from '../../helpers/fake-realtime';
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
import { oneByOne } from '../../helpers/one-by-one';
import { resetTestDb } from '../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
import { resetDatabase } from '../../helpers/reset-database';
import { bootTestApp } from '../../helpers/test-app';
import { MikroORM } from '@mikro-orm/core';
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
vi.mock('../../../src/db/database', async () => {
const { createSnapshotTestDb, buildDbMock } = await import('../../helpers/db-mock');
return buildDbMock(createSnapshotTestDb());
});
const realtime = new FakeRealtimeService();
const broadcastMock = realtime.broadcastMock;
@@ -37,21 +33,20 @@ async function setMetadata(metadata: string, reservationId: number): Promise<voi
await updateRows(orm, Reservations, { id: reservationId }, { metadata });
}
beforeEach(() => {
resetTestDb(testDb);
beforeEach(async () => {
await resetDatabase(orm);
broadcastMock.mockClear();
delete process.env.DEMO_MODE;
});
let orm: TestOrm;
let orm: MikroORM;
beforeAll(async () => {
orm = await createTestOrm(testDb);
orm = (await bootTestApp(realtime)).get(MikroORM);
});
afterAll(async () => {
await orm.close();
testDb.close();
await (await bootTestApp(realtime)).close();
});
async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>) {
@@ -88,8 +83,8 @@ const foreignDayOn = async (tripId: number, date: string) => (await makeDay(orm,
describe('Tool: create_transport', () => {
it('backfills lat/lng/timezone for code-only flight endpoints', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -109,8 +104,8 @@ describe('Tool: create_transport', () => {
});
it('keeps manually-supplied coordinates and the caller timezone', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -132,8 +127,8 @@ describe('Tool: create_transport', () => {
});
it('errors on an unresolvable airport code instead of crashing', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -150,8 +145,8 @@ describe('Tool: create_transport', () => {
});
it('errors on an endpoint missing both coordinates and a code', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -168,8 +163,8 @@ describe('Tool: create_transport', () => {
});
it('creates a transport with no endpoints', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -183,8 +178,8 @@ describe('Tool: create_transport', () => {
describe('Tool: update_transport', () => {
it('backfills coords when replacing endpoints', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = parseToolResult(
await h.client.callTool({
@@ -211,8 +206,8 @@ describe('Tool: update_transport', () => {
});
it('leaves endpoints untouched when not provided', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = parseToolResult(
await h.client.callTool({
@@ -231,8 +226,8 @@ describe('Tool: update_transport', () => {
});
it('re-files a linked expense on a type change in the same write, as REST and the plugin RPC do', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = parseToolResult(
await h.client.callTool({
@@ -289,9 +284,9 @@ describe('Tool: update_transport', () => {
describe('Transport tools: access and validation', () => {
it('create_transport refuses a trip the caller cannot see', async () => {
const { user } = createUser(testDb);
const { user: stranger } = createUser(testDb, { username: 'stranger' });
const trip = createTrip(testDb, stranger.id);
const { user } = await makeUser(orm);
const { user: stranger } = await makeUser(orm, { username: 'stranger' });
const trip = await makeTrip(orm, stranger.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -303,9 +298,9 @@ describe('Transport tools: access and validation', () => {
});
it('create_transport rejects a start_day_id from another trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const otherTrip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const otherTrip = await makeTrip(orm, user.id);
const foreignDay = await foreignDayOn(otherTrip.id, '2026-07-01');
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
@@ -320,9 +315,9 @@ describe('Transport tools: access and validation', () => {
});
it('create_transport rejects an end_day_id from another trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const otherTrip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const otherTrip = await makeTrip(orm, user.id);
const foreignDay = await foreignDayOn(otherTrip.id, '2026-07-02');
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
@@ -339,8 +334,8 @@ describe('Transport tools: access and validation', () => {
describe('Transport tools: the price link', () => {
it('create_transport with a price creates the budget item and broadcasts it', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -357,8 +352,8 @@ describe('Transport tools: the price link', () => {
});
it('create_transport falls back to the transport type as the budget category', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -372,8 +367,8 @@ describe('Transport tools: the price link', () => {
});
it('books a ticket with changes as one booking with legs and one cost (#2895)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -405,8 +400,8 @@ describe('Transport tools: the price link', () => {
});
it('warns when a segment repeats the fare an earlier booking of the same reference carries (#2895)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const segment = (title: string, extra: Record<string, unknown> = {}) =>
h.client.callTool({
@@ -450,8 +445,8 @@ describe('Transport tools: the price link', () => {
});
it('compares an earlier cost in its own currency against the trip currency after a switch', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const segment = (title: string) =>
h.client.callTool({
@@ -475,8 +470,8 @@ describe('Transport tools: the price link', () => {
});
it('create_reservation warns about a repeated booking total too', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const room = (title: string) =>
h.client.callTool({
@@ -489,8 +484,8 @@ describe('Transport tools: the price link', () => {
});
it('create_transport with price 0 records no budget item', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -504,8 +499,8 @@ describe('Transport tools: the price link', () => {
describe('Tool: update_transport (guards)', () => {
it('404s an unknown reservation id', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'update_transport',
@@ -517,8 +512,8 @@ describe('Tool: update_transport (guards)', () => {
});
it('refuses a reservation that is not a transport type', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await h.client.callTool({
name: 'create_reservation',
@@ -538,9 +533,9 @@ describe('Tool: update_transport (guards)', () => {
});
it('rejects a start_day_id from another trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const otherTrip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const otherTrip = await makeTrip(orm, user.id);
const foreignDay = await foreignDayOn(otherTrip.id, '2026-08-01');
await withHarness(user.id, async (h) => {
const created = await h.client.callTool({
@@ -561,8 +556,8 @@ describe('Tool: update_transport (guards)', () => {
});
it('errors on an unresolvable airport code when replacing endpoints', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await h.client.callTool({
name: 'create_transport',
@@ -586,8 +581,8 @@ describe('Tool: update_transport (guards)', () => {
describe('Tool: delete_transport', () => {
it('deletes the booking and broadcasts it', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await h.client.callTool({
name: 'create_transport',
@@ -608,8 +603,8 @@ describe('Tool: delete_transport', () => {
});
it('takes every expense linked to the transport with it and announces each one (#2084)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await h.client.callTool({
name: 'create_transport',
@@ -652,11 +647,11 @@ describe('Tool: delete_transport', () => {
// tool used to take the stay along without saying so, so the planner's hotel
// block outlived it until a reload; REST always sent accommodation:deleted.
it('takes along the stay a booking retyped from a hotel still carries, and says so as REST does', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day1 = createDay(testDb, trip.id, { day_number: 1 });
const day2 = createDay(testDb, trip.id, { day_number: 2 });
const hotel = createPlace(testDb, trip.id, { name: 'Grand Hotel' });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const day1 = await makeDay(orm, trip.id, { day_number: 1 });
const day2 = await makeDay(orm, trip.id, { day_number: 2 });
const hotel = await makePlace(orm, trip.id, { name: 'Grand Hotel' });
await withHarness(user.id, async (h) => {
const created = await h.client.callTool({
name: 'create_reservation',
@@ -691,8 +686,8 @@ describe('Tool: delete_transport', () => {
});
it('404s an unknown reservation id', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'delete_transport',
@@ -704,9 +699,9 @@ describe('Tool: delete_transport', () => {
});
it('refuses a trip the caller cannot see', async () => {
const { user } = createUser(testDb);
const { user: stranger } = createUser(testDb, { username: 'stranger2' });
const trip = createTrip(testDb, stranger.id);
const { user } = await makeUser(orm);
const { user: stranger } = await makeUser(orm, { username: 'stranger2' });
const trip = await makeTrip(orm, stranger.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'delete_transport',
@@ -726,8 +721,8 @@ describe('Transport tools: the booking notice and the AirTrail push', () => {
it('create, update and delete each tell the fellow travellers, as the booking routes do', async () => {
const notify = vi.spyOn(ReservationsService.prototype, 'notifyBookingChange').mockResolvedValue(undefined);
try {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = parseToolResult(
await h.client.callTool({
@@ -757,8 +752,8 @@ describe('Transport tools: the booking notice and the AirTrail push', () => {
it('pushes an edit of a linked AirTrail flight back to AirTrail, and only of a linked one', async () => {
const push = vi.spyOn(AirtrailLinkService.prototype, 'pushReservationToAirtrail').mockResolvedValue(undefined);
try {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
let linked = 0;
await withHarness(user.id, async (h) => {
const create = async (title: string) =>
@@ -798,9 +793,9 @@ describe('Transport tools: the booking notice and the AirTrail push', () => {
describe('Tool: create_transport (multi-leg)', () => {
it('stores the per-leg times of the reported AMS to CDG to FCO stopover', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id, { date: '2026-02-04' });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const day = await makeDay(orm, trip.id, { date: '2026-02-04' });
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -832,8 +827,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('mirrors the flat metadata keys off the first and last leg', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const data = parseToolResult(
await h.client.callTool({
@@ -860,8 +855,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('stores a per-segment booking reference and leaves the booking-level one alone (#1943)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const data = parseToolResult(
await h.client.callTool({
@@ -891,8 +886,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('never overwrites a flat key the caller set itself', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const data = parseToolResult(
await h.client.callTool({
@@ -917,8 +912,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('fills from/to from the endpoint codes when the leg omits them', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const data = parseToolResult(
await h.client.callTool({
@@ -944,8 +939,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('accepts a multi-leg train and mirrors its flat keys', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const data = parseToolResult(
await h.client.callTool({
@@ -979,8 +974,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('rejects a leg count that does not match the endpoints', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -1003,8 +998,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('rejects a single leg, which no reader would treat as multi-segment', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -1022,8 +1017,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('rejects legs on a transport type that has none', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -1043,10 +1038,10 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('rejects a leg day that belongs to another trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const otherTrip = createTrip(testDb, user.id);
const foreignDay = createDay(testDb, otherTrip.id, { date: '2026-02-04' });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const otherTrip = await makeTrip(orm, user.id);
const foreignDay = await makeDay(orm, otherTrip.id, { date: '2026-02-04' });
await withHarness(user.id, async (h) => {
const depResult = await h.client.callTool({
name: 'create_transport',
@@ -1081,8 +1076,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('rejects a leg airport that contradicts its endpoint', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const wrongTo = await h.client.callTool({
name: 'create_transport',
@@ -1117,8 +1112,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('rejects a leg time that contradicts an endpoint time already set', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const withTimes = [
{ ...stopoverEndpoints[0], local_time: '09:00' },
{ ...stopoverEndpoints[1], local_time: '11:30' },
@@ -1158,10 +1153,10 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('rejects a span that contradicts the first or last leg', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const dayOne = createDay(testDb, trip.id, { date: '2026-02-04' });
const dayTwo = createDay(testDb, trip.id, { date: '2026-02-05' });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const dayOne = await makeDay(orm, trip.id, { date: '2026-02-04' });
const dayTwo = await makeDay(orm, trip.id, { date: '2026-02-05' });
await withHarness(user.id, async (h) => {
const startClash = await h.client.callTool({
name: 'create_transport',
@@ -1200,8 +1195,8 @@ describe('Tool: create_transport (multi-leg)', () => {
});
it('refuses segments smuggled through metadata.legs and names the legs parameter', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -1238,8 +1233,8 @@ describe('Tool: update_transport (multi-leg)', () => {
) as any;
it('keeps the stored metadata and the day-plan positions when only legs are sent', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await seedStopover(h, trip.id);
// What an AirTrail import leaves behind: sync ids plus per-leg planner positions.
@@ -1277,8 +1272,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('keeps a per-segment booking reference through a legs-only update (#1943)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await seedStopover(h, trip.id);
const result = await h.client.callTool({
@@ -1301,8 +1296,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('uses the supplied metadata as the base when both are sent', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await seedStopover(h, trip.id);
const result = await h.client.callTool({
@@ -1326,8 +1321,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('leaves the metadata alone when neither legs nor metadata are sent', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await seedStopover(h, trip.id);
const before = await storedMetadata(created.reservation.id);
@@ -1340,8 +1335,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('counts the stored endpoints when the caller does not replace them', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = parseToolResult(
await h.client.callTool({
@@ -1368,8 +1363,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('leaves the endpoint rows untouched when the legs change nothing about them', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await seedStopover(h, trip.id);
const idsBefore = await endpointIds(created.reservation.id);
@@ -1390,8 +1385,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('fills the endpoint times when the stored endpoints have none', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = parseToolResult(
await h.client.callTool({
@@ -1416,8 +1411,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('survives a booking whose stored metadata is not valid JSON', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await seedStopover(h, trip.id);
await setMetadata('not json at all', created.reservation.id);
@@ -1438,8 +1433,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('recovers legs from double-encoded metadata instead of losing the positions', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await seedStopover(h, trip.id);
await setMetadata(
@@ -1471,8 +1466,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('rejects legs on a booking that is not a flight or train', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = parseToolResult(
await h.client.callTool({
@@ -1497,8 +1492,8 @@ describe('Tool: update_transport (multi-leg)', () => {
});
it('refuses segments smuggled through metadata.legs', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await seedStopover(h, trip.id);
const result = await h.client.callTool({
@@ -1542,8 +1537,8 @@ const NEWLY_ACCEPTED = ['bus', 'taxi', 'bicycle', 'ferry', 'transport_other'] as
describe('Transport tools: the full type list', () => {
it.each(PICKER_TRANSPORT_TYPES)('create_transport accepts %s', async (type) => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -1557,10 +1552,10 @@ describe('Transport tools: the full type list', () => {
});
it('stores a bus booking with its stops, times and booking reference', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const depDay = createDay(testDb, trip.id, { day_number: 1 });
const arrDay = createDay(testDb, trip.id, { day_number: 2 });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const depDay = await makeDay(orm, trip.id, { day_number: 1 });
const arrDay = await makeDay(orm, trip.id, { day_number: 2 });
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -1590,8 +1585,8 @@ describe('Transport tools: the full type list', () => {
});
it('reports a missing coordinate on a non-flight endpoint rather than failing the insert', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -1608,8 +1603,8 @@ describe('Transport tools: the full type list', () => {
});
it.each(NEWLY_ACCEPTED)('update_transport accepts %s as the new type', async (type) => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await h.client.callTool({
name: 'create_transport',
@@ -1627,8 +1622,8 @@ describe('Transport tools: the full type list', () => {
});
it('update_transport reaches a booking created as a bus', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const created = await h.client.callTool({
name: 'create_transport',
@@ -1646,8 +1641,8 @@ describe('Transport tools: the full type list', () => {
});
it('still refuses a type the picker does not offer', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -1658,8 +1653,8 @@ describe('Transport tools: the full type list', () => {
});
it('refuses to hand-make a transit booking, which create_transit_journey owns', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
@@ -1670,8 +1665,8 @@ describe('Transport tools: the full type list', () => {
});
it('still lets update_transport reach a stored transit booking', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
// As create_transit_journey leaves it: a transport row carrying an itinerary.
const reservationId = await insertRow(orm, Reservations, {
trip: trip.id,
@@ -1691,9 +1686,9 @@ describe('Transport tools: the full type list', () => {
});
it('still refuses legs on a mode whose form never writes them', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id, { day_number: 1 });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
const day = await makeDay(orm, trip.id, { day_number: 1 });
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_transport',
+204 -202
View File
@@ -4,7 +4,6 @@
* create_trip_guest, rename_trip_guest, delete_trip_guest,
* copy_trip, export_trip_ics, get_share_link, create_share_link, delete_share_link.
*/
import { db as testDb } from '../../../src/db/database';
import { BudgetItems } from '../../../src/db/entities/BudgetItems.entity';
import { Days } from '../../../src/db/entities/Days.entity';
import { ShareTokens } from '../../../src/db/entities/ShareTokens.entity';
@@ -14,31 +13,27 @@ import { Users } from '../../../src/db/entities/Users.entity';
import { GUEST_HAS_PAYMENTS } from '../../../src/nest/budget/budget-departure.helpers';
import { NotificationsService } from '../../../src/nest/notifications/notifications.service';
import { invalidatePermissionsCache } from '../../../src/nest/permissions/permissions-cache';
import { createUser, createAdmin, createTrip, addTripMember, createDayNote } from '../../helpers/factories';
import { addBudgetItemMember, addBudgetItemPayer, makeBudgetItem } from '../../helpers/factories/budget';
import { makeDayNote } from '../../helpers/factories/itinerary';
import { countRows, findRow, findRows, updateRows } from '../../helpers/factories/rows';
import { setAppSetting } from '../../helpers/factories/settings';
import { makeShareToken } from '../../helpers/factories/trips';
import { addTripMember, makeShareToken, makeTrip } from '../../helpers/factories/trips';
import { makeAdmin, makeUser } from '../../helpers/factories/users';
import { FakeRealtimeService } from '../../helpers/fake-realtime';
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
import { resetDatabase } from '../../helpers/reset-database';
import { bootTestApp } from '../../helpers/test-app';
import { resetTestDb } from '../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
import { MikroORM } from '@mikro-orm/core';
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
vi.mock('../../../src/db/database', async () => {
const { createSnapshotTestDb, buildDbMock } = await import('../../helpers/db-mock');
return buildDbMock(createSnapshotTestDb());
});
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest';
const realtime = new FakeRealtimeService();
const broadcastMock = realtime.broadcastMock;
let orm: TestOrm;
let orm: MikroORM;
beforeAll(async () => {
orm = await createTestOrm(testDb);
orm = (await bootTestApp(realtime)).get(MikroORM);
});
function memberRow(tripId: number, userId: number) {
@@ -50,17 +45,16 @@ async function userRow(id: number) {
}
beforeEach(async () => {
resetTestDb(testDb);
await resetDatabase(orm);
broadcastMock.mockClear();
delete process.env.DEMO_MODE;
// resetTestDb truncates app_settings, but the permission cache is module-scoped
// resetDatabase truncates app_settings, but the permission cache is module-scoped
// and would keep serving whatever the previous case configured.
await invalidatePermissionsCache();
});
afterAll(async () => {
await orm.close();
testDb.close();
await (await bootTestApp(realtime)).close();
});
async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>) {
@@ -84,8 +78,8 @@ async function setPermission(action: string, level: string): Promise<void> {
describe('Tool: list_trip_members', () => {
it('returns owner and empty members list for own trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'list_trip_members', arguments: { tripId: trip.id } });
const data = parseToolResult(result) as any;
@@ -96,9 +90,9 @@ describe('Tool: list_trip_members', () => {
});
it('returns access denied for non-member', async () => {
const { user } = createUser(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, other.id);
const { user } = await makeUser(orm);
const { user: other } = await makeUser(orm);
const trip = await makeTrip(orm, other.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'list_trip_members', arguments: { tripId: trip.id } });
expect(result.isError).toBe(true);
@@ -112,9 +106,9 @@ describe('Tool: list_trip_members', () => {
describe('Tool: add_trip_member', () => {
it('adds a member by username', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const result = await h.client.callTool({
name: 'add_trip_member',
@@ -127,9 +121,9 @@ describe('Tool: add_trip_member', () => {
});
it('broadcasts member:added event', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
await h.client.callTool({
name: 'add_trip_member',
@@ -140,8 +134,8 @@ describe('Tool: add_trip_member', () => {
});
it('returns error when user not found', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const result = await h.client.callTool({
name: 'add_trip_member',
@@ -152,11 +146,11 @@ describe('Tool: add_trip_member', () => {
});
it('returns error when non-owner tries to add', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const { user: outsider } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const { user: outsider } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({
name: 'add_trip_member',
@@ -168,8 +162,8 @@ describe('Tool: add_trip_member', () => {
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm, { email: 'demo@nomad.app' });
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'add_trip_member',
@@ -180,11 +174,11 @@ describe('Tool: add_trip_member', () => {
});
it('lets a collaborator add when member_manage sits at trip_member', async () => {
const { user: owner } = createUser(testDb);
const { user: collaborator } = createUser(testDb);
const { user: outsider } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, collaborator.id);
const { user: owner } = await makeUser(orm);
const { user: collaborator } = await makeUser(orm);
const { user: outsider } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, collaborator.id);
await setPermission('member_manage', 'trip_member');
await withHarness(collaborator.id, async (h) => {
const result = await h.client.callTool({
@@ -199,11 +193,11 @@ describe('Tool: add_trip_member', () => {
});
it('lets a site admin on the trip add a member without owning it', async () => {
const { user: owner } = createUser(testDb);
const { user: admin } = createAdmin(testDb);
const { user: outsider } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, admin.id);
const { user: owner } = await makeUser(orm);
const { user: admin } = await makeAdmin(orm);
const { user: outsider } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, admin.id);
await withHarness(admin.id, async (h) => {
const result = await h.client.callTool({
name: 'add_trip_member',
@@ -215,11 +209,11 @@ describe('Tool: add_trip_member', () => {
});
it('still refuses a collaborator while member_manage sits at its trip_owner default', async () => {
const { user: owner } = createUser(testDb);
const { user: collaborator } = createUser(testDb);
const { user: outsider } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, collaborator.id);
const { user: owner } = await makeUser(orm);
const { user: collaborator } = await makeUser(orm);
const { user: outsider } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, collaborator.id);
await withHarness(collaborator.id, async (h) => {
const result = await h.client.callTool({
name: 'add_trip_member',
@@ -237,10 +231,10 @@ describe('Tool: add_trip_member', () => {
describe('Tool: remove_trip_member', () => {
it('removes a member', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await withHarness(owner.id, async (h) => {
const result = await h.client.callTool({
name: 'remove_trip_member',
@@ -254,10 +248,10 @@ describe('Tool: remove_trip_member', () => {
});
it('broadcasts member:removed event', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await withHarness(owner.id, async (h) => {
await h.client.callTool({ name: 'remove_trip_member', arguments: { tripId: trip.id, memberId: member.id } });
expect(broadcastMock).toHaveBeenCalledWith(trip.id, 'member:removed', expect.any(Object));
@@ -265,10 +259,10 @@ describe('Tool: remove_trip_member', () => {
});
it('returns error when non-owner tries to remove', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({
name: 'remove_trip_member',
@@ -279,12 +273,12 @@ describe('Tool: remove_trip_member', () => {
});
it('leaves another member in place when a collaborator has no member_manage', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
addTripMember(testDb, trip.id, other.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const { user: other } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await addTripMember(orm, trip.id, other.id);
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({
name: 'remove_trip_member',
@@ -296,12 +290,12 @@ describe('Tool: remove_trip_member', () => {
});
it('lets a collaborator remove another member when member_manage sits at trip_member', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
addTripMember(testDb, trip.id, other.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const { user: other } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await addTripMember(orm, trip.id, other.id);
await setPermission('member_manage', 'trip_member');
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({
@@ -314,12 +308,12 @@ describe('Tool: remove_trip_member', () => {
});
it('lets a site admin on the trip remove a member without owning it', async () => {
const { user: owner } = createUser(testDb);
const { user: admin } = createAdmin(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, admin.id);
addTripMember(testDb, trip.id, other.id);
const { user: owner } = await makeUser(orm);
const { user: admin } = await makeAdmin(orm);
const { user: other } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, admin.id);
await addTripMember(orm, trip.id, other.id);
await withHarness(admin.id, async (h) => {
const result = await h.client.callTool({
name: 'remove_trip_member',
@@ -331,10 +325,10 @@ describe('Tool: remove_trip_member', () => {
});
it('lets a member remove themselves without member_manage', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({
name: 'remove_trip_member',
@@ -352,10 +346,10 @@ describe('Tool: remove_trip_member', () => {
describe('Tool: leave_trip', () => {
it('drops the caller from the roster', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({ name: 'leave_trip', arguments: { tripId: trip.id } });
expect((parseToolResult(result) as any).success).toBe(true);
@@ -365,10 +359,10 @@ describe('Tool: leave_trip', () => {
});
it('broadcasts member:removed for the leaving user', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await withHarness(member.id, async (h) => {
await h.client.callTool({ name: 'leave_trip', arguments: { tripId: trip.id } });
expect(broadcastMock).toHaveBeenCalledWith(
@@ -380,12 +374,12 @@ describe('Tool: leave_trip', () => {
});
it('leaves the other members alone', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
addTripMember(testDb, trip.id, other.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const { user: other } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
await addTripMember(orm, trip.id, other.id);
await withHarness(member.id, async (h) => {
await h.client.callTool({ name: 'leave_trip', arguments: { tripId: trip.id } });
expect(await memberRow(trip.id, other.id)).toBeTruthy();
@@ -393,8 +387,8 @@ describe('Tool: leave_trip', () => {
});
it('refuses the owner and keeps the trip theirs', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const result = await h.client.callTool({ name: 'leave_trip', arguments: { tripId: trip.id } });
expect(result.isError).toBe(true);
@@ -404,9 +398,9 @@ describe('Tool: leave_trip', () => {
});
it('returns access denied for a non-member', async () => {
const { user: outsider } = createUser(testDb);
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: outsider } = await makeUser(orm);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(outsider.id, async (h) => {
const result = await h.client.callTool({ name: 'leave_trip', arguments: { tripId: trip.id } });
expect(result.isError).toBe(true);
@@ -415,10 +409,10 @@ describe('Tool: leave_trip', () => {
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user: owner } = createUser(testDb);
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, user.id);
const { user: owner } = await makeUser(orm);
const { user } = await makeUser(orm, { email: 'demo@nomad.app' });
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'leave_trip', arguments: { tripId: trip.id } });
expect(result.isError).toBe(true);
@@ -433,8 +427,8 @@ describe('Tool: leave_trip', () => {
describe('Tool: copy_trip', () => {
it('duplicates a trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Original', start_date: '2025-01-01', end_date: '2025-01-03' });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id, { title: 'Original', start_date: '2025-01-01', end_date: '2025-01-03' });
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'copy_trip', arguments: { tripId: trip.id } });
const data = parseToolResult(result) as any;
@@ -446,8 +440,8 @@ describe('Tool: copy_trip', () => {
});
it('uses custom title when provided', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Original' });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id, { title: 'Original' });
await withHarness(user.id, async (h) => {
await h.client.callTool({ name: 'copy_trip', arguments: { tripId: trip.id, title: 'My Copy' } });
const newTrip = await findRow(orm, Trips, { title: 'My Copy' });
@@ -456,9 +450,9 @@ describe('Tool: copy_trip', () => {
});
it('returns access denied for non-member', async () => {
const { user } = createUser(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, other.id);
const { user } = await makeUser(orm);
const { user: other } = await makeUser(orm);
const trip = await makeTrip(orm, other.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'copy_trip', arguments: { tripId: trip.id } });
expect(result.isError).toBe(true);
@@ -467,8 +461,8 @@ describe('Tool: copy_trip', () => {
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm, { email: 'demo@nomad.app' });
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'copy_trip', arguments: { tripId: trip.id } });
expect(result.isError).toBe(true);
@@ -482,8 +476,12 @@ describe('Tool: copy_trip', () => {
describe('Tool: export_trip_ics', () => {
it('returns ICS content for a trip', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Paris Trip', start_date: '2025-06-01', end_date: '2025-06-05' });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id, {
title: 'Paris Trip',
start_date: '2025-06-01',
end_date: '2025-06-05',
});
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'export_trip_ics', arguments: { tripId: trip.id } });
const data = parseToolResult(result) as any;
@@ -494,11 +492,15 @@ describe('Tool: export_trip_ics', () => {
});
it('leaves the per-day summaries out with dayEvents false, like days=0 on the route (#2602)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Paris Trip', start_date: '2025-06-01', end_date: '2025-06-05' });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id, {
title: 'Paris Trip',
start_date: '2025-06-01',
end_date: '2025-06-05',
});
// A dated trip comes with its days; the first one is the 1st of June.
const [day] = await findRows(orm, Days, { trip: trip.id }, { id: 'asc' });
createDayNote(testDb, day.id, trip.id, { text: 'Pick up the museum pass' });
await makeDayNote(orm, day.id, trip.id, { text: 'Pick up the museum pass' });
await withHarness(user.id, async (h) => {
const call = async (args: Record<string, unknown>) =>
(
@@ -514,9 +516,9 @@ describe('Tool: export_trip_ics', () => {
});
it('returns access denied for non-member', async () => {
const { user } = createUser(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, other.id);
const { user } = await makeUser(orm);
const { user: other } = await makeUser(orm);
const trip = await makeTrip(orm, other.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'export_trip_ics', arguments: { tripId: trip.id } });
expect(result.isError).toBe(true);
@@ -526,8 +528,8 @@ describe('Tool: export_trip_ics', () => {
// Same calendar filename as the REST route (#2165): U+3000 used to slip
// through the \s keep-class and crash the header there; it folds to _ now.
it('folds header-hostile whitespace out of the filename', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, {
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id, {
title: '沖縄 4泊5日',
start_date: '2025-06-01',
end_date: '2025-06-05',
@@ -546,8 +548,8 @@ describe('Tool: export_trip_ics', () => {
describe('Tool: get_share_link', () => {
it('returns null when no share link exists', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'get_share_link', arguments: { tripId: trip.id } });
const data = parseToolResult(result) as any;
@@ -556,8 +558,8 @@ describe('Tool: get_share_link', () => {
});
it('returns share link info when it exists', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
// Create a share link directly
await makeShareToken(orm, trip.id, user.id, {
token: 'test-token-123',
@@ -578,8 +580,8 @@ describe('Tool: get_share_link', () => {
describe('Tool: create_share_link', () => {
it('creates a new share link', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_share_link',
@@ -592,8 +594,8 @@ describe('Tool: create_share_link', () => {
});
it('updates existing share link permissions', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await makeShareToken(orm, trip.id, user.id, {
token: 'existing-token',
share_map: 1,
@@ -614,8 +616,8 @@ describe('Tool: create_share_link', () => {
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm, { email: 'demo@nomad.app' });
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({ name: 'create_share_link', arguments: { tripId: trip.id } });
expect(result.isError).toBe(true);
@@ -625,8 +627,8 @@ describe('Tool: create_share_link', () => {
describe('Tool: delete_share_link', () => {
it('revokes the share link', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await makeShareToken(orm, trip.id, user.id, {
token: 'to-delete',
share_map: 1,
@@ -662,8 +664,8 @@ async function makeGuest(h: McpHarness, tripId: number, name: string): Promise<n
describe('Tool: create_trip_guest', () => {
it('creates a credential-less guest and returns the display name', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const result = await h.client.callTool({
name: 'create_trip_guest',
@@ -684,8 +686,8 @@ describe('Tool: create_trip_guest', () => {
});
it('joins the guest into the trip roster', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
await makeGuest(h, trip.id, 'Jake');
const listed = await h.client.callTool({ name: 'list_trip_members', arguments: { tripId: trip.id } });
@@ -695,8 +697,8 @@ describe('Tool: create_trip_guest', () => {
});
it('broadcasts member:added event', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
await makeGuest(h, trip.id, 'Anna');
expect(broadcastMock).toHaveBeenCalledWith(trip.id, 'member:added', expect.any(Object));
@@ -704,8 +706,8 @@ describe('Tool: create_trip_guest', () => {
});
it('rejects a whitespace-only name', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const result = await h.client.callTool({
name: 'create_trip_guest',
@@ -717,10 +719,10 @@ describe('Tool: create_trip_guest', () => {
});
it('returns error when a member who is not the owner tries to add a guest', async () => {
const { user: owner } = createUser(testDb);
const { user: collaborator } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, collaborator.id);
const { user: owner } = await makeUser(orm);
const { user: collaborator } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, collaborator.id);
await withHarness(collaborator.id, async (h) => {
const result = await h.client.callTool({
name: 'create_trip_guest',
@@ -731,9 +733,9 @@ describe('Tool: create_trip_guest', () => {
});
it('returns access denied for a non-member', async () => {
const { user: outsider } = createUser(testDb);
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: outsider } = await makeUser(orm);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(outsider.id, async (h) => {
const result = await h.client.callTool({
name: 'create_trip_guest',
@@ -745,8 +747,8 @@ describe('Tool: create_trip_guest', () => {
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm, { email: 'demo@nomad.app' });
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'create_trip_guest',
@@ -759,8 +761,8 @@ describe('Tool: create_trip_guest', () => {
describe('Tool: rename_trip_guest', () => {
it('renames the guest', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, trip.id, 'Anna');
const result = await h.client.callTool({
@@ -774,8 +776,8 @@ describe('Tool: rename_trip_guest', () => {
});
it('rejects a whitespace-only name', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, trip.id, 'Anna');
const result = await h.client.callTool({
@@ -789,9 +791,9 @@ describe('Tool: rename_trip_guest', () => {
});
it('will not rename a guest of another trip', async () => {
const { user: owner } = createUser(testDb);
const ownTrip = createTrip(testDb, owner.id);
const otherTrip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const ownTrip = await makeTrip(orm, owner.id);
const otherTrip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, otherTrip.id, 'Anna');
broadcastMock.mockClear();
@@ -808,10 +810,10 @@ describe('Tool: rename_trip_guest', () => {
});
it('returns error when a member who is not the owner tries to rename', async () => {
const { user: owner } = createUser(testDb);
const { user: collaborator } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, collaborator.id);
const { user: owner } = await makeUser(orm);
const { user: collaborator } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, collaborator.id);
let guestId = 0;
await withHarness(owner.id, async (h) => {
guestId = await makeGuest(h, trip.id, 'Anna');
@@ -827,8 +829,8 @@ describe('Tool: rename_trip_guest', () => {
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm, { email: 'demo@nomad.app' });
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'rename_trip_guest',
@@ -841,8 +843,8 @@ describe('Tool: rename_trip_guest', () => {
describe('Tool: delete_trip_guest', () => {
it('deletes the guest outright', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, trip.id, 'Anna');
const result = await h.client.callTool({
@@ -856,8 +858,8 @@ describe('Tool: delete_trip_guest', () => {
});
it('broadcasts member:removed event', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, trip.id, 'Anna');
broadcastMock.mockClear();
@@ -871,8 +873,8 @@ describe('Tool: delete_trip_guest', () => {
});
it('refuses a guest who paid an expense, with the same text REST answers (#2604)', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, trip.id, 'Anna');
const item = await makeBudgetItem(orm, trip.id, { total_price: 60 });
@@ -888,9 +890,9 @@ describe('Tool: delete_trip_guest', () => {
});
it('will not delete a guest of another trip', async () => {
const { user: owner } = createUser(testDb);
const ownTrip = createTrip(testDb, owner.id);
const otherTrip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const ownTrip = await makeTrip(orm, owner.id);
const otherTrip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, otherTrip.id, 'Anna');
const result = await h.client.callTool({
@@ -903,10 +905,10 @@ describe('Tool: delete_trip_guest', () => {
});
it('will not delete a real member through the guest route', async () => {
const { user: owner } = createUser(testDb);
const { user: collaborator } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, collaborator.id);
const { user: owner } = await makeUser(orm);
const { user: collaborator } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, collaborator.id);
await withHarness(owner.id, async (h) => {
const result = await h.client.callTool({
name: 'delete_trip_guest',
@@ -918,10 +920,10 @@ describe('Tool: delete_trip_guest', () => {
});
it('returns error when a member who is not the owner tries to delete', async () => {
const { user: owner } = createUser(testDb);
const { user: collaborator } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, collaborator.id);
const { user: owner } = await makeUser(orm);
const { user: collaborator } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, collaborator.id);
let guestId = 0;
await withHarness(owner.id, async (h) => {
guestId = await makeGuest(h, trip.id, 'Anna');
@@ -938,8 +940,8 @@ describe('Tool: delete_trip_guest', () => {
it('blocks demo user', async () => {
process.env.DEMO_MODE = 'true';
const { user } = createUser(testDb, { email: 'demo@nomad.app' });
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm, { email: 'demo@nomad.app' });
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const result = await h.client.callTool({
name: 'delete_trip_guest',
@@ -959,10 +961,10 @@ describe('Tool: delete_trip_guest', () => {
describe('Guest tools stay owner-only', () => {
it('refuses a collaborator all three guest tools even with member_manage at trip_member', async () => {
const { user: owner } = createUser(testDb);
const { user: collaborator } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, collaborator.id);
const { user: owner } = await makeUser(orm);
const { user: collaborator } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, collaborator.id);
let guestId = 0;
await withHarness(owner.id, async (h) => {
guestId = await makeGuest(h, trip.id, 'Anna');
@@ -1009,9 +1011,9 @@ describe('Guest tools stay owner-only', () => {
describe('Roster writes, as REST makes them', () => {
it('MCP-MEMBERS-001: add_trip_member sends the invitation the planner sends, naming the inviter by name', async () => {
const { user: owner } = createUser(testDb);
const { user: invitee } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const { user: invitee } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
const send = vi
.spyOn((await bootTestApp(realtime)).get(NotificationsService), 'send')
.mockResolvedValue({ attempted: 0, delivered: 0 });
@@ -1034,8 +1036,8 @@ describe('Roster writes, as REST makes them', () => {
});
it('MCP-MEMBERS-002: rename_trip_guest tells the room the new name, and nothing else of the row', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, trip.id, 'Anna');
broadcastMock.mockClear();
@@ -1050,8 +1052,8 @@ describe('Roster writes, as REST makes them', () => {
});
it('MCP-MEMBERS-003: delete_trip_guest sends each expense the guest shared again, split among the rest, after the member', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, trip.id, 'Anna');
const shared = await makeBudgetItem(orm, trip.id, { total_price: 60 });
@@ -1069,8 +1071,8 @@ describe('Roster writes, as REST makes them', () => {
});
it('MCP-MEMBERS-004: delete_trip_guest also sends an expense the guest was only the legacy payer of, cleared', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await withHarness(owner.id, async (h) => {
const guestId = await makeGuest(h, trip.id, 'Anna');
const legacy = await makeBudgetItem(orm, trip.id, { total_price: 40 });
@@ -2,12 +2,14 @@
* Unit tests for the get_trip_warnings MCP tool (src/nest/plugins/contributions/
* trip-warnings.mcp.ts), the MCP counterpart of GET /api/trip-warnings/:tripId.
*/
import { db as testDb } from '../../../src/db/database';
import { PluginHooks } from '../../../src/nest/plugins/plugin-hooks.service';
import { addTripMember, createTrip, createUser } from '../../helpers/factories';
import { addTripMember, makeTrip } from '../../helpers/factories/trips';
import { makeUser } from '../../helpers/factories/users';
import { FakeRealtimeService } from '../../helpers/fake-realtime';
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
import { resetTestDb } from '../../helpers/test-db';
import { resetDatabase } from '../../helpers/reset-database';
import { bootTestApp } from '../../helpers/test-app';
import { MikroORM } from '@mikro-orm/core';
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -15,10 +17,6 @@ const { pluginsEnabled } = vi.hoisted(() => ({
pluginsEnabled: vi.fn(() => true),
}));
vi.mock('../../../src/db/database', async () => {
const { createSnapshotTestDb, buildDbMock } = await import('../../helpers/db-mock');
return buildDbMock(createSnapshotTestDb());
});
// The admin kill switch reads live env; drive it from the test instead of the process.
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
@@ -31,16 +29,22 @@ const broadcastMock = realtime.broadcastMock;
const providersOfMock = vi.spyOn(PluginHooks.prototype, 'providersOf');
const tripWarningsMock = vi.spyOn(PluginHooks.prototype, 'tripWarnings');
beforeEach(() => {
resetTestDb(testDb);
let orm: MikroORM;
beforeAll(async () => {
orm = (await bootTestApp(realtime)).get(MikroORM);
});
beforeEach(async () => {
await resetDatabase(orm);
broadcastMock.mockClear();
pluginsEnabled.mockReturnValue(true);
providersOfMock.mockReset().mockReturnValue([]);
tripWarningsMock.mockReset().mockResolvedValue([]);
});
afterAll(() => {
testDb.close();
afterAll(async () => {
await (await bootTestApp(realtime)).close();
});
async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>, scopes?: string[] | null) {
@@ -65,8 +69,8 @@ const call = async (h: McpHarness, tripId: number) =>
describe('Tool: get_trip_warnings', () => {
it('merges every provider, tags each warning with the plugin that raised it', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id, { title: 'Kyoto' });
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id, { title: 'Kyoto' });
providersOfMock.mockReturnValue(['seasons', 'logistics']);
tripWarningsMock.mockImplementation(async (pluginId: string) =>
pluginId === 'seasons'
@@ -86,8 +90,8 @@ describe('Tool: get_trip_warnings', () => {
});
it('carries the place link through and defaults an unknown level to warning', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
providersOfMock.mockReturnValue(['opening-hours']);
tripWarningsMock.mockResolvedValue([{ level: 'catastrophe', message: 'Museum closed Mondays', placeId: 7 }]);
@@ -100,8 +104,8 @@ describe('Tool: get_trip_warnings', () => {
});
it('answers with an empty list when no plugin provides warnings', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
await withHarness(user.id, async (h) => {
const payload = parseToolResult(await call(h, trip.id)) as WarningsPayload;
@@ -111,10 +115,10 @@ describe('Tool: get_trip_warnings', () => {
});
it('is readable by a collaborator, not just the owner', async () => {
const { user: owner } = createUser(testDb);
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const { user: owner } = await makeUser(orm);
const { user: member } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
await addTripMember(orm, trip.id, member.id);
providersOfMock.mockReturnValue(['logistics']);
tripWarningsMock.mockResolvedValue([{ level: 'warning', message: 'Tight connection' }]);
@@ -132,9 +136,9 @@ describe('Tool: get_trip_warnings', () => {
describe('get_trip_warnings access', () => {
it('refuses a trip the caller is not on, without asking any provider', async () => {
const { user: owner } = createUser(testDb);
const { user: stranger } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const { user: stranger } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
providersOfMock.mockReturnValue(['logistics']);
await withHarness(stranger.id, async (h) => {
@@ -146,7 +150,7 @@ describe('get_trip_warnings access', () => {
});
it('refuses a trip that does not exist', async () => {
const { user } = createUser(testDb);
const { user } = await makeUser(orm);
await withHarness(user.id, async (h) => {
const result = await call(h, 999999);
@@ -156,9 +160,9 @@ describe('get_trip_warnings access', () => {
});
it('checks trip access before the kill switch, so a stranger never learns the switch is off', async () => {
const { user: owner } = createUser(testDb);
const { user: stranger } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const { user: owner } = await makeUser(orm);
const { user: stranger } = await makeUser(orm);
const trip = await makeTrip(orm, owner.id);
pluginsEnabled.mockReturnValue(false);
await withHarness(stranger.id, async (h) => {
@@ -168,7 +172,7 @@ describe('get_trip_warnings access', () => {
});
it('rides trips:read: present with it, absent without it', async () => {
const { user } = createUser(testDb);
const { user } = await makeUser(orm);
await withHarness(
user.id,
@@ -196,8 +200,8 @@ describe('get_trip_warnings access', () => {
describe('get_trip_warnings resilience', () => {
it('answers empty when an admin has the plugin system switched off', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
pluginsEnabled.mockReturnValue(false);
providersOfMock.mockReturnValue(['logistics']);
@@ -209,8 +213,8 @@ describe('get_trip_warnings resilience', () => {
});
it('drops a provider that throws or times out and keeps the rest', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
providersOfMock.mockReturnValue(['broken', 'healthy']);
tripWarningsMock.mockImplementation(async (pluginId: string) => {
if (pluginId === 'broken') throw new Error('hook timed out after 5000ms');
@@ -226,8 +230,8 @@ describe('get_trip_warnings resilience', () => {
});
it('ignores a provider that answers with something other than an array', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
providersOfMock.mockReturnValue(['confused']);
tripWarningsMock.mockResolvedValue({ warnings: 'lots' });
@@ -238,8 +242,8 @@ describe('get_trip_warnings resilience', () => {
});
it('skips null entries instead of losing the whole provider to them', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
providersOfMock.mockReturnValue(['sloppy']);
tripWarningsMock.mockResolvedValue([null, { level: 'info', message: 'Survives' }, 'nope']);
@@ -250,8 +254,8 @@ describe('get_trip_warnings resilience', () => {
});
it('drops a warning with no message rather than emitting a blank line', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
providersOfMock.mockReturnValue(['sloppy']);
tripWarningsMock.mockResolvedValue([{ level: 'error' }, { level: 'error', message: '' }]);
@@ -268,8 +272,8 @@ describe('get_trip_warnings resilience', () => {
describe('get_trip_warnings caps', () => {
it('caps a flooding provider at 20 warnings and truncates an oversized message', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
providersOfMock.mockReturnValue(['flood']);
tripWarningsMock.mockResolvedValue([
{ level: 'warning', message: 'z'.repeat(1000) },
@@ -284,8 +288,8 @@ describe('get_trip_warnings caps', () => {
});
it('caps each provider separately, so two providers can contribute 40', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
providersOfMock.mockReturnValue(['a', 'b']);
tripWarningsMock.mockResolvedValue(Array.from({ length: 50 }, (_v, i) => ({ level: 'info', message: `w${i}` })));
@@ -296,8 +300,8 @@ describe('get_trip_warnings caps', () => {
});
it('strips emojis, because the assistant relays this text as TREK chrome', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id);
providersOfMock.mockReturnValue(['loud']);
tripWarningsMock.mockResolvedValue([{ level: 'error', message: '🔥 Overbooked!' }]);