diff --git a/server/scripts/test-mocks-baseline.json b/server/scripts/test-mocks-baseline.json index a63827f77..9447ae752 100644 --- a/server/scripts/test-mocks-baseline.json +++ b/server/scripts/test-mocks-baseline.json @@ -63,12 +63,8 @@ "tests/setup.ts": 1, "tests/unit/auto-backup.test.ts": 1, "tests/unit/db/database-lazy-open.test.ts": 1, - "tests/unit/mcp/tools-airtrail.test.ts": 1, - "tests/unit/mcp/tools-atlas-expanded.test.ts": 1, "tests/unit/mcp/tools-budget-advanced.test.ts": 1, "tests/unit/mcp/tools-budget.test.ts": 1, - "tests/unit/mcp/tools-categories.test.ts": 1, - "tests/unit/mcp/tools-collab-polls-chat.test.ts": 1, "tests/unit/mcp/tools-collections.test.ts": 1, "tests/unit/mcp/tools-days-accommodations.test.ts": 1, "tests/unit/mcp/tools-days.test.ts": 1, diff --git a/server/scripts/test-new-service-baseline.json b/server/scripts/test-new-service-baseline.json index d1c28df73..e94c500b7 100644 --- a/server/scripts/test-new-service-baseline.json +++ b/server/scripts/test-new-service-baseline.json @@ -23,9 +23,7 @@ "tests/unit/db/repositories/MaintenanceRepository.test.ts": 2, "tests/unit/db/repositories/UserConnections.repository.test.ts": 3, "tests/unit/demo/demo-reset-sessions.test.ts": 3, - "tests/unit/mcp/tools-airtrail.test.ts": 1, "tests/unit/mcp/tools-budget-advanced.test.ts": 2, - "tests/unit/mcp/tools-categories.test.ts": 4, "tests/unit/mcp/tools-prompts.test.ts": 7, "tests/unit/mcp/tools-roadtrip.test.ts": 1, "tests/unit/mcp/tools-transit.test.ts": 1, diff --git a/server/tests/postgres/snapshot-suites-baseline.json b/server/tests/postgres/snapshot-suites-baseline.json index 20f0b1aa8..4e33e044a 100644 --- a/server/tests/postgres/snapshot-suites-baseline.json +++ b/server/tests/postgres/snapshot-suites-baseline.json @@ -1,10 +1,6 @@ [ - "tests/unit/mcp/tools-airtrail.test.ts", - "tests/unit/mcp/tools-atlas-expanded.test.ts", "tests/unit/mcp/tools-budget-advanced.test.ts", "tests/unit/mcp/tools-budget.test.ts", - "tests/unit/mcp/tools-categories.test.ts", - "tests/unit/mcp/tools-collab-polls-chat.test.ts", "tests/unit/mcp/tools-collections.test.ts", "tests/unit/mcp/tools-days-accommodations.test.ts", "tests/unit/mcp/tools-days.test.ts", diff --git a/server/tests/unit/mcp/tools-airtrail.test.ts b/server/tests/unit/mcp/tools-airtrail.test.ts index a8da45f9c..fa89355c0 100644 --- a/server/tests/unit/mcp/tools-airtrail.test.ts +++ b/server/tests/unit/mcp/tools-airtrail.test.ts @@ -9,9 +9,9 @@ * reservations land in the test DB. */ import { ADDON_IDS } from '../../../src/addons'; -import { db as testDb } from '../../../src/db/database'; import { Reservations } from '../../../src/db/entities/Reservations.entity'; import { Users } from '../../../src/db/entities/Users.entity'; +import { withRequestContext } from '../../../src/nest/database/request-context'; import { AirtrailImportService } from '../../../src/nest/integrations/airtrail-import.service'; import { AirtrailClient, @@ -19,61 +19,52 @@ import { type AirtrailFlightRaw, } from '../../../src/nest/integrations/airtrail.client'; import { PermissionsService } from '../../../src/nest/permissions/permissions.service'; -import { createUser, createTrip, addTripMember } from '../../helpers/factories'; import { findRows, updateRows } from '../../helpers/factories/rows'; +import { setAddonEnabled } from '../../helpers/factories/settings'; +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, setAddonEnabled } from '../../helpers/test-db'; -import { createTestOrm, type TestOrm } from '../../helpers/test-orm'; -import { createTestUnitOfWork, createTestAppSettingsRepo } from '../../helpers/test-uow'; +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; -// The permissions cache is module-scoped, so a write through any instance is -// what the tool's own checkPermission call reads back. - +// The booted container's own ORM and PermissionsService, the ones the tools +// read through. The permissions cache is module-scoped, so a write through the +// service is what the tool's own checkPermission call reads back. +let orm: MikroORM; let permissionsService: PermissionsService; -let savePermissions: typeof permissionsService.savePermissions; beforeAll(async () => { - permissionsService = new PermissionsService( - await createTestAppSettingsRepo(testDb), - await createTestUnitOfWork(testDb), - ); - savePermissions = permissionsService.savePermissions.bind(permissionsService); + const app = await bootTestApp(realtime); + orm = app.get(MikroORM); + permissionsService = app.get(PermissionsService); }); +const savePermissions = (perms: Parameters[0]) => + withRequestContext(orm, () => permissionsService.savePermissions(perms)); // The registry builds its own AirtrailClient per harness, so the stub goes on // the prototype. Everything below listFlights (creds, mapper, dedupe) is real. const listFlightsMock = vi.spyOn(AirtrailClient.prototype, 'listFlights'); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); broadcastMock.mockClear(); listFlightsMock.mockReset(); listFlightsMock.mockResolvedValue([]); delete process.env.DEMO_MODE; - // resetTestDb leaves the addons table alone, and both tools are gated on the - // airtrail addon, so every case restates the toggle it needs. - setAddonEnabled(testDb, ADDON_IDS.AIRTRAIL, true); + // resetDatabase puts back the default addon rows, and both tools are gated on + // the airtrail addon, so every case restates the toggle it needs. + await setAddonEnabled(orm, ADDON_IDS.AIRTRAIL, true); await savePermissions({ reservation_edit: 'trip_member' }); }); -let orm: TestOrm; -beforeAll(async () => { - orm = await createTestOrm(testDb); -}); - afterAll(async () => { - await orm.close(); - testDb.close(); + await (await bootTestApp(realtime)).close(); }); async function withHarness(userId: number, fn: (h: McpHarness) => Promise, scopes: string[] | null = null) { @@ -151,7 +142,7 @@ function reservationRows(tripId: number) { describe('Tool: list_airtrail_flights', () => { it('returns the caller flights, normalized and oldest departure first', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); listFlightsMock.mockResolvedValue([ flight({ @@ -192,7 +183,7 @@ describe('Tool: list_airtrail_flights', () => { }); it('narrows to a date window and keeps undated flights', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); listFlightsMock.mockResolvedValue([ flight({ id: 1, date: '2026-08-01', departure: '2026-08-01T06:00:00Z' }), @@ -218,7 +209,7 @@ describe('Tool: list_airtrail_flights', () => { }); it('falls back to its own wording when AirTrail fails without a message', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); listFlightsMock.mockRejectedValue(new Error('')); @@ -230,7 +221,7 @@ describe('Tool: list_airtrail_flights', () => { }); it('caps the result and says it was truncated', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); listFlightsMock.mockResolvedValue( Array.from({ length: 5 }, (_, i) => flight({ id: i + 1, departure: `2026-09-1${i}T06:00:00Z` })), @@ -246,7 +237,7 @@ describe('Tool: list_airtrail_flights', () => { }); it('reports an unconnected account instead of an empty list', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_airtrail_flights', arguments: {} }); @@ -257,7 +248,7 @@ describe('Tool: list_airtrail_flights', () => { }); it('passes an upstream failure through as the tool error', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); listFlightsMock.mockRejectedValue(new AirtrailRequestError('AirTrail list failed (HTTP 500)', 500)); @@ -269,9 +260,9 @@ describe('Tool: list_airtrail_flights', () => { }); it('is hidden while the airtrail addon is off', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); - setAddonEnabled(testDb, ADDON_IDS.AIRTRAIL, false); + await setAddonEnabled(orm, ADDON_IDS.AIRTRAIL, false); await withHarness(user.id, async (h) => { const names = (await h.client.listTools()).tools.map((t) => t.name); @@ -283,7 +274,7 @@ describe('Tool: list_airtrail_flights', () => { }); it('rides reservations:read and is hidden from a token without it', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); await withHarness( @@ -310,9 +301,9 @@ describe('Tool: list_airtrail_flights', () => { describe('Tool: import_airtrail_flights', () => { it('imports the listed flights as linked flight bookings', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); - const trip = createTrip(testDb, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); + const trip = await makeTrip(orm, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); listFlightsMock.mockResolvedValue([flight({ id: 11 }), flight({ id: 12, flightNumber: 'LH1202' })]); await withHarness(user.id, async (h) => { @@ -343,9 +334,9 @@ describe('Tool: import_airtrail_flights', () => { }); it('skips a flight that is already linked to the trip', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); - const trip = createTrip(testDb, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); + const trip = await makeTrip(orm, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); listFlightsMock.mockResolvedValue([flight({ id: 11 })]); await withHarness(user.id, async (h) => { @@ -362,9 +353,9 @@ describe('Tool: import_airtrail_flights', () => { }); it('joins a connection into one multi-leg booking detached from sync', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); - const trip = createTrip(testDb, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); + const trip = await makeTrip(orm, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); listFlightsMock.mockResolvedValue([ flight({ id: 11 }), flight({ @@ -393,9 +384,9 @@ describe('Tool: import_airtrail_flights', () => { }); it('refuses a connection naming a flight that is not being imported', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); - const trip = createTrip(testDb, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); + const trip = await makeTrip(orm, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); listFlightsMock.mockResolvedValue([flight({ id: 11 })]); await withHarness(user.id, async (h) => { @@ -411,9 +402,9 @@ describe('Tool: import_airtrail_flights', () => { }); it('refuses more than fifty flights in one call', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); - const trip = createTrip(testDb, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); + const trip = await makeTrip(orm, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -428,8 +419,8 @@ describe('Tool: import_airtrail_flights', () => { }); it('reports an unconnected account', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -443,9 +434,9 @@ describe('Tool: import_airtrail_flights', () => { }); it('falls back to its own wording when the import fails without a message', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); - const trip = createTrip(testDb, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); + const trip = await makeTrip(orm, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); const importMock = vi .spyOn(AirtrailImportService.prototype, 'importAirtrailFlights') .mockRejectedValue(new Error('')); @@ -467,13 +458,13 @@ describe('Tool: import_airtrail_flights', () => { }); it('enforces demo, trip access and the reservation permission', async () => { - const { user: owner } = createUser(testDb); - const { user: demo } = createUser(testDb, { email: 'demo@trek.app' }); - const { user: stranger } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); - addTripMember(testDb, trip.id, demo.id); - addTripMember(testDb, trip.id, member.id); + const { user: owner } = await makeUser(orm); + const { user: demo } = await makeUser(orm, { email: 'demo@trek.app' }); + const { user: stranger } = await makeUser(orm); + const { user: member } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); + await addTripMember(orm, trip.id, demo.id); + await addTripMember(orm, trip.id, member.id); for (const u of [owner, demo, stranger, member]) await connectAirtrail(u.id); listFlightsMock.mockResolvedValue([flight({ id: 11 })]); @@ -513,10 +504,10 @@ describe('Tool: import_airtrail_flights', () => { }); it('is hidden while the airtrail addon is off', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); - const trip = createTrip(testDb, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); - setAddonEnabled(testDb, ADDON_IDS.AIRTRAIL, false); + const trip = await makeTrip(orm, user.id, { start_date: '2026-09-10', end_date: '2026-09-12' }); + await setAddonEnabled(orm, ADDON_IDS.AIRTRAIL, false); await withHarness(user.id, async (h) => { const names = (await h.client.listTools()).tools.map((t) => t.name); @@ -532,7 +523,7 @@ describe('Tool: import_airtrail_flights', () => { }); it('rides reservations:write and is hidden from a read-only token', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await connectAirtrail(user.id); await withHarness( diff --git a/server/tests/unit/mcp/tools-atlas-expanded.test.ts b/server/tests/unit/mcp/tools-atlas-expanded.test.ts index bd9b697cc..a7ed73c78 100644 --- a/server/tests/unit/mcp/tools-atlas-expanded.test.ts +++ b/server/tests/unit/mcp/tools-atlas-expanded.test.ts @@ -5,7 +5,6 @@ * Also covers resources trek://atlas/stats and trek://atlas/regions. */ import { ADDON_IDS } from '../../../src/addons'; -import { db as testDb } from '../../../src/db/database'; import { BucketList } from '../../../src/db/entities/BucketList.entity'; import { HiddenRegions } from '../../../src/db/entities/HiddenRegions.entity'; import { PlaceRegions } from '../../../src/db/entities/PlaceRegions.entity'; @@ -13,49 +12,40 @@ import { Places } from '../../../src/db/entities/Places.entity'; import { ReservationEndpoints } from '../../../src/db/entities/ReservationEndpoints.entity'; import { VisitedCountries } from '../../../src/db/entities/VisitedCountries.entity'; import { VisitedRegions } from '../../../src/db/entities/VisitedRegions.entity'; -import { - createUser, - createBucketListItem, - createVisitedCountry, - createTrip, - createReservation, -} from '../../helpers/factories'; -import { markCountryVisited } from '../../helpers/factories/atlas'; +import { makeBucketListItem, markCountryVisited } from '../../helpers/factories/atlas'; import { makePlace } from '../../helpers/factories/places'; +import { makeReservation } from '../../helpers/factories/reservations'; import { countRows, findRow, insertRow, upsertRow } from '../../helpers/factories/rows'; +import { setAddonEnabled } from '../../helpers/factories/settings'; import { makeTrip } from '../../helpers/factories/trips'; +import { makeUser } from '../../helpers/factories/users'; import { FakeRealtimeService } from '../../helpers/fake-realtime'; import { createMcpHarness, parseToolResult, parseResourceResult, type McpHarness } from '../../helpers/mcp-harness'; -import { resetTestDb } from '../../helpers/test-db'; -import { setAddonEnabled } 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()); -}); +import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; const realtime = new FakeRealtimeService(); const broadcastMock = realtime.broadcastMock; -beforeEach(() => { - setAddonEnabled(testDb, ADDON_IDS.ATLAS, true); - resetTestDb(testDb); +beforeEach(async () => { + await setAddonEnabled(orm, ADDON_IDS.ATLAS, true); + await resetDatabase(orm); broadcastMock.mockClear(); delete process.env.DEMO_MODE; }); -let orm: TestOrm; +// The booted container's own ORM, the one the tools write through. +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) { @@ -153,7 +143,7 @@ const PAST_END = '2023-05-10'; describe('Tool: get_atlas_stats', () => { it('returns stats object without error for empty data', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_atlas_stats', arguments: {} }); expect(result.isError).toBeFalsy(); @@ -171,10 +161,10 @@ describe('Tool: get_atlas_stats', () => { }); it('carries the passport figures REST answers with: the cities by name and the distance flown', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); await insertPlace(trip.id, 'Colosseum', 'Colosseum, Rome, Italy', 41.8902, 12.4922); - const flight = createReservation(testDb, trip.id, { type: 'flight', title: 'FCO-JFK' }); + const flight = await makeReservation(orm, trip.id, { type: 'flight', title: 'FCO-JFK' }); await insertEndpoint(flight.id, 'from', 0, 41.8003, 12.2389); await insertEndpoint(flight.id, 'to', 1, 40.6413, -73.7781); @@ -195,8 +185,8 @@ describe('Tool: get_atlas_stats', () => { }); it('include_coords adds the per-place coordinates the dashboard map plots', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); await insertPlace(trip.id, 'Colosseum', 'Colosseum, Rome, Italy', 41.8902, 12.4922); await insertPlace(trip.id, 'Pantheon', 'Pantheon, Rome, Italy', 41.8986, 12.4769); @@ -205,12 +195,19 @@ describe('Tool: get_atlas_stats', () => { const data = parseToolResult(result) as any; const withCoords = await countRows(orm, Places, { trip: trip.id, lat: { $ne: null }, lng: { $ne: null } }); expect(data.travel.coords).toHaveLength(withCoords); - expect(data.travel.coords[0]).toEqual({ lat: 41.8902, lng: 12.4922 }); + // One entry per place; the query is a SELECT DISTINCT without an order, + // so the engine picks the order (Postgres hashes) and only the set counts. + expect(data.travel.coords).toEqual( + expect.arrayContaining([ + { lat: 41.8902, lng: 12.4922 }, + { lat: 41.8986, lng: 12.4769 }, + ]), + ); }); }); it('refuses a non-boolean include_coords', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_atlas_stats', arguments: { include_coords: 'yes' } }); expect(result.isError).toBe(true); @@ -225,7 +222,7 @@ describe('Tool: get_atlas_stats', () => { describe('Tool: list_visited_regions', () => { it('returns an empty grouping initially', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_visited_regions', arguments: {} }); const data = parseToolResult(result) as any; @@ -234,7 +231,7 @@ describe('Tool: list_visited_regions', () => { }); it('groups a manual mark under its country, in the shape the map reads', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await markRegion(user.id, 'FR-75', 'Paris', 'FR'); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_visited_regions', arguments: {} }); @@ -246,8 +243,8 @@ describe('Tool: list_visited_regions', () => { }); it('reports regions derived from trip places, which the manual table never held', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); await cacheRegion(await insertPlace(trip.id, 'Colosseum', null, 41.8902, 12.4922), 'IT', 'IT-62', 'Lazio'); await cacheRegion(await insertPlace(trip.id, 'Pantheon', null, 41.8986, 12.4769), 'IT', 'IT-62', 'Lazio'); @@ -262,8 +259,8 @@ describe('Tool: list_visited_regions', () => { }); it('drops a region the user dismissed, matching the map', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); await cacheRegion(await insertPlace(trip.id, 'Colosseum', null, 41.8902, 12.4922), 'IT', 'IT-62', 'Lazio'); await withHarness(user.id, async (h) => { @@ -289,7 +286,7 @@ describe('Tool: list_visited_regions', () => { describe('Tool: locate_atlas_region', () => { it('resolves a coordinate to the country and the region the map can highlight', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'locate_atlas_region', @@ -306,7 +303,7 @@ describe('Tool: locate_atlas_region', () => { }); it('feeds mark_region_visited without the caller knowing any codes', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const located = parseToolResult( await h.client.callTool({ name: 'locate_atlas_region', arguments: { lat: 41.9028, lng: 12.4964 } }), @@ -330,7 +327,7 @@ describe('Tool: locate_atlas_region', () => { }); it('answers with nulls out at sea rather than failing', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'locate_atlas_region', arguments: { lat: 0, lng: -140 } }); expect(result.isError).toBeFalsy(); @@ -339,7 +336,7 @@ describe('Tool: locate_atlas_region', () => { }); it('refuses an out-of-range coordinate, like the REST route does', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const badLat = await h.client.callTool({ name: 'locate_atlas_region', arguments: { lat: 91, lng: 12.5 } }); expect(badLat.isError).toBe(true); @@ -358,7 +355,7 @@ describe('Tool: locate_atlas_region', () => { describe('Tool: mark_region_visited', () => { it('uppercases both codes like REST does (post-fold quirk fix)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'mark_region_visited', @@ -379,7 +376,7 @@ describe('Tool: mark_region_visited', () => { }); it('inserts region and returns region object', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'mark_region_visited', @@ -401,7 +398,7 @@ describe('Tool: mark_region_visited', () => { it('blocks demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@nomad.app' }); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'mark_region_visited', @@ -418,7 +415,7 @@ describe('Tool: mark_region_visited', () => { describe('Tool: unmark_region_visited', () => { it('removes region and returns success', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await markRegion(user.id, 'IT-LO', 'Lombardy', 'IT'); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -435,7 +432,7 @@ describe('Tool: unmark_region_visited', () => { }); it('keeps a country the user marked itself when its last region goes, like REST (#2597)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await markCountryVisited(orm, user.id, 'NO'); await markRegion(user.id, 'NO-03', 'Oslo', 'NO'); await insertRow(orm, VisitedCountries, { user: user.id, country_code: 'SE', source: 'region' }); @@ -450,7 +447,7 @@ describe('Tool: unmark_region_visited', () => { }); it('succeeds even when region was not marked (no-op)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'unmark_region_visited', @@ -468,7 +465,7 @@ describe('Tool: unmark_region_visited', () => { describe('Tool: get_country_atlas_places', () => { it('returns empty places array for a new user', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_country_atlas_places', @@ -481,7 +478,7 @@ describe('Tool: get_country_atlas_places', () => { }); it('uppercases the country code like REST does (post-fold quirk fix)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await markCountryVisited(orm, user.id, 'JP'); // A trip so countryPlaces reaches the visited_countries lookup pre-quirk-fix too. await makeTrip(orm, user.id, { title: 'Japan' }); @@ -503,7 +500,7 @@ describe('Tool: get_country_atlas_places', () => { describe('Tool: update_bucket_list_item', () => { it('updates notes and returns item', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const itemId = await insertWish(user.id, 'Visit Tokyo'); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -517,7 +514,7 @@ describe('Tool: update_bucket_list_item', () => { }); it('updates name of existing item', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const itemId = await insertWish(user.id, 'Old Name'); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -530,7 +527,7 @@ describe('Tool: update_bucket_list_item', () => { }); it('returns isError for non-existent item', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_bucket_list_item', @@ -541,7 +538,7 @@ describe('Tool: update_bucket_list_item', () => { }); it('returns isError when the edit would land on another wish (#1898)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await insertWish(user.id, 'Japan', '2027-05'); const itemId = await insertWish(user.id, 'Japan', '2028-09'); await withHarness(user.id, async (h) => { @@ -557,7 +554,7 @@ describe('Tool: update_bucket_list_item', () => { it('blocks demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@nomad.app' }); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); const itemId = await insertWish(user.id, 'Bucket Item'); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -575,7 +572,7 @@ describe('Tool: update_bucket_list_item', () => { describe('Resource: trek://atlas/stats', () => { it('returns stats object', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withResourceHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: 'trek://atlas/stats' }); const data = parseResourceResult(result) as any; @@ -590,7 +587,7 @@ describe('Resource: trek://atlas/stats', () => { describe('Resource: trek://atlas/regions', () => { it('returns the country grouping', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withResourceHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: 'trek://atlas/regions' }); const data = parseResourceResult(result) as any; @@ -599,9 +596,9 @@ describe('Resource: trek://atlas/regions', () => { }); it('returns marked and derived regions together, as the tool does', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await markRegion(user.id, 'ES-CT', 'Catalonia', 'ES'); - const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); + const trip = await makeTrip(orm, user.id, { title: 'Rome', start_date: PAST_START, end_date: PAST_END }); await cacheRegion(await insertPlace(trip.id, 'Colosseum', null, 41.8902, 12.4922), 'IT', 'IT-62', 'Lazio'); await withResourceHarness(user.id, async (h) => { @@ -622,10 +619,10 @@ describe('Resource: trek://atlas/regions', () => { describe('Resource: trek://bucket-list', () => { it("returns only the current user's bucket list items", async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - createBucketListItem(testDb, user.id, { name: 'Tokyo' }); - createBucketListItem(testDb, other.id, { name: 'Rome' }); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + await makeBucketListItem(orm, user.id, { name: 'Tokyo' }); + await makeBucketListItem(orm, other.id, { name: 'Rome' }); await withResourceHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: 'trek://bucket-list' }); @@ -636,7 +633,7 @@ describe('Resource: trek://bucket-list', () => { }); it('returns empty array for user with no items', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withResourceHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: 'trek://bucket-list' }); @@ -652,11 +649,11 @@ describe('Resource: trek://bucket-list', () => { describe('Resource: trek://visited-countries', () => { it("returns only the current user's visited countries", async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - createVisitedCountry(testDb, user.id, 'FR'); - createVisitedCountry(testDb, user.id, 'JP'); - createVisitedCountry(testDb, other.id, 'DE'); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + await markCountryVisited(orm, user.id, 'FR'); + await markCountryVisited(orm, user.id, 'JP'); + await markCountryVisited(orm, other.id, 'DE'); await withResourceHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: 'trek://visited-countries' }); @@ -670,7 +667,7 @@ describe('Resource: trek://visited-countries', () => { }); it('returns empty array for user with no visited countries', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withResourceHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: 'trek://visited-countries' }); diff --git a/server/tests/unit/mcp/tools-categories.test.ts b/server/tests/unit/mcp/tools-categories.test.ts index 7b47e8d90..a578682a5 100644 --- a/server/tests/unit/mcp/tools-categories.test.ts +++ b/server/tests/unit/mcp/tools-categories.test.ts @@ -7,62 +7,45 @@ * harness here keeps withTools on (the resource is NOT registered by the legacy * registerResources fan-out anymore). */ -import { db as testDb } from '../../../src/db/database'; import { Categories } from '../../../src/db/entities/Categories.entity'; import { Places } from '../../../src/db/entities/Places.entity'; import { trekDemoToolGate, trekMcpAccessPolicy, trekMcpValidateAccess } from '../../../src/mcp/nest-mcp-policy'; import { createTestRegistry } from '../../../src/nest-mcp'; -import { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service'; import { CategoriesMcp } from '../../../src/nest/categories/categories.mcp'; -import { CategoriesService } from '../../../src/nest/categories/categories.service'; import { DemoService } from '../../../src/nest/common/demo.service'; -import { McpToolGuardsService } from '../../../src/nest/mcp-shared/mcp-tool-guards.service'; -import { PermissionsService } from '../../../src/nest/permissions/permissions.service'; -import { RealtimeService } from '../../../src/nest/realtime/realtime.service'; -import { createUser, createAdmin } from '../../helpers/factories'; +import { withRequestContext } from '../../../src/nest/database/request-context'; import { makeCategory, makePlace } from '../../helpers/factories/places'; import { findRow } from '../../helpers/factories/rows'; import { makeTrip } from '../../helpers/factories/trips'; +import { makeAdmin, makeUser } from '../../helpers/factories/users'; import { createMcpHarness, parseToolResult, parseResourceResult, type McpHarness } from '../../helpers/mcp-harness'; -import { resetTestDb } from '../../helpers/test-db'; -import { createTestOrm, type TestOrm } from '../../helpers/test-orm'; -import { - createTestUnitOfWork, - createTestAppSettingsRepo, - createTestCategoriesRepo, - createTestTripsRepo, - createTestUsersRepo, - sharedTestOrm, -} from '../../helpers/test-uow'; +import { resetDatabase } from '../../helpers/reset-database'; +import { bootTestApp } from '../../helpers/test-app'; +import { MikroORM } from '@mikro-orm/core'; import { Client } from '@modelcontextprotocol/sdk/client/index'; import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory'; import { McpServer } from '@modelcontextprotocol/sdk/server/mcp'; -import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; +import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; -vi.mock('../../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - -let orm: TestOrm; +// The booted container's own ORM, the one the tools write through. +let orm: MikroORM; beforeAll(async () => { - orm = await createTestOrm(testDb); + orm = (await bootTestApp()).get(MikroORM); }); async function categoryRow(id: number) { return (await findRow(orm, Categories, { id }))!; } -beforeEach(() => { - resetTestDb(testDb); +beforeEach(async () => { + await resetDatabase(orm); delete process.env.DEMO_MODE; }); afterAll(async () => { - await orm.close(); - testDb.close(); + await (await bootTestApp()).close(); }); async function withHarness(userId: number, fn: (h: McpHarness) => Promise, scopes: string[] | null = null) { @@ -74,34 +57,34 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise, } } -// The write tools reach the guard collaborator, so they run against a controller -// wired here rather than through the shared harness. Everything points at this -// file's DB, which is what lets the admin gate and the demo gate be driven from -// the users table instead of from a stub. +// The write tools reach the guard collaborator, so they run against a registry +// of the container's own CategoriesMcp with the production policies rather than +// through the shared harness. That is what lets the admin gate and the demo +// gate be driven from the users table instead of from a stub. let categoriesMcp: CategoriesMcp; let categoriesDemo: DemoService; beforeAll(async () => { - const categoriesEm = (await sharedTestOrm(testDb)).em; - categoriesMcp = new CategoriesMcp( - new CategoriesService(await createTestCategoriesRepo(testDb)), - new RuntimeEnvService(), - new McpToolGuardsService( - await createTestTripsRepo(testDb), - await createTestUsersRepo(testDb), - new PermissionsService(await createTestAppSettingsRepo(testDb), await createTestUnitOfWork(testDb)), - new RealtimeService(), - ), - ); - categoriesDemo = new DemoService(new RuntimeEnvService(), categoriesEm); + const app = await bootTestApp(); + categoriesMcp = app.get(CategoriesMcp); + categoriesDemo = app.get(DemoService); }); async function withWriteHarness(userId: number, fn: (client: Client) => Promise) { const server = new McpServer({ name: 'trek-test', version: '1.0.0' }); - await createTestRegistry([categoriesMcp], { + const registry = createTestRegistry([categoriesMcp], { accessPolicy: trekMcpAccessPolicy, validateAccess: trekMcpValidateAccess, toolGate: trekDemoToolGate((id) => categoriesDemo.isDemoUserId(id)), - }).attach(server, { userId, scopes: null, isStaticToken: false }); + }); + // Each call runs in a request context of its own, as the transport's + // requests do, so the container's repositories have an EntityManager. + await withRequestContext(orm, () => + registry.attach( + server, + { userId, scopes: null, isStaticToken: false }, + { around: (_info, call) => withRequestContext(orm, call) }, + ), + ); const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); const client = new Client({ name: 'test-client', version: '1.0.0' }); await server.connect(serverTransport); @@ -132,7 +115,7 @@ async function insertCategory(name: string, color = '#111111', icon = '🅰️') describe('Tool: list_categories', () => { it('returns all categories with id, name, color, icon', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_categories', arguments: {} }); const data = parseToolResult(result) as any; @@ -147,8 +130,8 @@ describe('Tool: list_categories', () => { }); it('returns categories from all users, ordered by name', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); await makeCategory(orm, { name: 'Zzz Mine', color: '#111111', icon: '🅰️', user: user.id }); await makeCategory(orm, { name: 'Zzz Other', color: '#222222', icon: '🅱️', user: other.id }); await withHarness(user.id, async (h) => { @@ -167,7 +150,7 @@ describe('Tool: list_categories', () => { describe('Tool: create_category', () => { it('persists the new category and credits the admin who minted it', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); await withWriteHarness(admin.id, async (client) => { const result = await client.callTool({ name: 'create_category', @@ -185,7 +168,7 @@ describe('Tool: create_category', () => { }); it('falls back to the palette defaults when color and icon are omitted', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); await withWriteHarness(admin.id, async (client) => { const result = await client.callTool({ name: 'create_category', arguments: { name: 'Bare minimum' } }); const data = parseToolResult(result) as any; @@ -195,7 +178,7 @@ describe('Tool: create_category', () => { }); it('refuses a non-admin and writes nothing', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withWriteHarness(user.id, async (client) => { const result = await client.callTool({ name: 'create_category', arguments: { name: 'Sneaky' } }); expect(result.isError).toBe(true); @@ -205,7 +188,7 @@ describe('Tool: create_category', () => { it('blocks demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createAdmin(testDb, { email: 'demo@trek.app' }); + const { user } = await makeAdmin(orm, { email: 'demo@trek.app' }); await withWriteHarness(user.id, async (client) => { const result = await client.callTool({ name: 'create_category', arguments: { name: 'Demo made this' } }); expect(result.isError).toBe(true); @@ -214,7 +197,7 @@ describe('Tool: create_category', () => { }); it('refuses a color that is not a hex value', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); await withWriteHarness(admin.id, async (client) => { const result = await client.callTool({ name: 'create_category', @@ -226,7 +209,7 @@ describe('Tool: create_category', () => { }); it('refuses an empty name', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); await withWriteHarness(admin.id, async (client) => { const result = await client.callTool({ name: 'create_category', arguments: { name: '' } }); expect(result.isError).toBe(true); @@ -240,7 +223,7 @@ describe('Tool: create_category', () => { describe('Tool: update_category', () => { it('renames and recolours an existing category', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); const id = await insertCategory('Old name', '#111111', '🅰️'); await withWriteHarness(admin.id, async (client) => { await client.callTool({ @@ -254,7 +237,7 @@ describe('Tool: update_category', () => { }); it('leaves the fields it was not given alone', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); const id = await insertCategory('Keep me', '#0891b2', '🚕'); await withWriteHarness(admin.id, async (client) => { await client.callTool({ name: 'update_category', arguments: { categoryId: id, icon: '🚗' } }); @@ -265,7 +248,7 @@ describe('Tool: update_category', () => { }); it('reports an unknown category', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); await withWriteHarness(admin.id, async (client) => { const result = await client.callTool({ name: 'update_category', @@ -276,7 +259,7 @@ describe('Tool: update_category', () => { }); it('refuses a non-admin and leaves the row untouched', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const id = await insertCategory('Not yours', '#9333ea', '🔒'); await withWriteHarness(user.id, async (client) => { const result = await client.callTool({ @@ -290,7 +273,7 @@ describe('Tool: update_category', () => { it('blocks demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createAdmin(testDb, { email: 'demo@trek.app' }); + const { user } = await makeAdmin(orm, { email: 'demo@trek.app' }); const id = await insertCategory('Demo untouchable', '#ea580c', '🙅'); await withWriteHarness(user.id, async (client) => { const result = await client.callTool({ name: 'update_category', arguments: { categoryId: id, name: 'Changed' } }); @@ -300,7 +283,7 @@ describe('Tool: update_category', () => { }); it('refuses a color that is not a hex value', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); const id = await insertCategory('Colour guard', '#2563eb', '🎨'); await withWriteHarness(admin.id, async (client) => { const result = await client.callTool({ @@ -319,7 +302,7 @@ describe('Tool: update_category', () => { describe('Tool: delete_category', () => { it('removes the category and unassigns the places that carried it', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); const id = await insertCategory('Doomed', '#d97706', '💀'); const { id: trip } = await makeTrip(orm, admin.id, { title: 'Trip' }); const { id: place } = await makePlace(orm, trip, { name: 'Somewhere', category: id }); @@ -332,7 +315,7 @@ describe('Tool: delete_category', () => { }); it('reports an unknown category', async () => { - const { user: admin } = createAdmin(testDb); + const { user: admin } = await makeAdmin(orm); await withWriteHarness(admin.id, async (client) => { const result = await client.callTool({ name: 'delete_category', arguments: { categoryId: 999999 } }); expect(result.isError).toBe(true); @@ -340,7 +323,7 @@ describe('Tool: delete_category', () => { }); it('refuses a non-admin and keeps the row', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const id = await insertCategory('Survivor', '#16a34a', '🌿'); await withWriteHarness(user.id, async (client) => { const result = await client.callTool({ name: 'delete_category', arguments: { categoryId: id } }); @@ -351,7 +334,7 @@ describe('Tool: delete_category', () => { it('blocks demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createAdmin(testDb, { email: 'demo@trek.app' }); + const { user } = await makeAdmin(orm, { email: 'demo@trek.app' }); const id = await insertCategory('Demo survivor', '#0891b2', '🛟'); await withWriteHarness(user.id, async (client) => { const result = await client.callTool({ name: 'delete_category', arguments: { categoryId: id } }); @@ -379,31 +362,31 @@ describe('Category tools: scope gating', () => { } it('registers with null scopes (full access)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, null); expect(names).toContain('list_categories'); for (const tool of WRITE_TOOLS) expect(names).toContain(tool); }); it('registers with places:read', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); expect(await listToolNames(user.id, ['places:read'])).toContain('list_categories'); }); it('registers no palette writes with places:read only', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['places:read']); for (const tool of WRITE_TOOLS) expect(names).not.toContain(tool); }); it('registers the palette writes with places:write', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['places:write']); for (const tool of WRITE_TOOLS) expect(names).toContain(tool); }); it('does not register for an unrelated scope', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['budget:read']); expect(names).not.toContain('list_categories'); for (const tool of WRITE_TOOLS) expect(names).not.toContain(tool); @@ -416,7 +399,7 @@ describe('Category tools: scope gating', () => { describe('Resource: trek://categories', () => { it('returns all categories', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: 'trek://categories' }); const categories = parseResourceResult(result) as any[]; @@ -429,7 +412,7 @@ describe('Resource: trek://categories', () => { }); it('stays readable under restricted non-places scopes (legacy ungated behavior)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness( user.id, async (h) => { diff --git a/server/tests/unit/mcp/tools-collab-polls-chat.test.ts b/server/tests/unit/mcp/tools-collab-polls-chat.test.ts index 3cf9a161a..7907f4ce0 100644 --- a/server/tests/unit/mcp/tools-collab-polls-chat.test.ts +++ b/server/tests/unit/mcp/tools-collab-polls-chat.test.ts @@ -7,50 +7,44 @@ * Resources: trek://trips/{tripId}/collab/polls, trek://trips/{tripId}/collab/messages. */ import { ADDON_IDS } from '../../../src/addons'; -import { db as testDb } from '../../../src/db/database'; import { CollabLinks } from '../../../src/db/entities/CollabLinks.entity'; import { CollabPollVotes } from '../../../src/db/entities/CollabPollVotes.entity'; import { CollabPolls } from '../../../src/db/entities/CollabPolls.entity'; import { TripMembers } from '../../../src/db/entities/TripMembers.entity'; import { NotificationsService } from '../../../src/nest/notifications/notifications.service'; import { invalidatePermissionsCache } from '../../../src/nest/permissions/permissions-cache'; -import { addTripMember, createUser, createTrip } from '../../helpers/factories'; import { makeCollabMessage } from '../../helpers/factories/collab'; import { countRows, findRow, insertRow } from '../../helpers/factories/rows'; -import { setAppSetting } from '../../helpers/factories/settings'; +import { setAddonEnabled, setCollabFeature, setAppSetting } from '../../helpers/factories/settings'; +import { addTripMember, makeTrip } from '../../helpers/factories/trips'; +import { makeUser } from '../../helpers/factories/users'; import { FakeRealtimeService } from '../../helpers/fake-realtime'; import { createMcpHarness, parseToolResult, parseResourceResult, 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 { setAddonEnabled, setCollabFeature } 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()); -}); - const realtime = new FakeRealtimeService(); const broadcastMock = realtime.broadcastMock; -beforeEach(() => { - setAddonEnabled(testDb, ADDON_IDS.COLLAB, true); - resetTestDb(testDb); +beforeEach(async () => { + await setAddonEnabled(orm, ADDON_IDS.COLLAB, true); + await resetDatabase(orm); broadcastMock.mockClear(); delete process.env.DEMO_MODE; }); -let orm: TestOrm; +// The booted container's own ORM, the one the tools write through. +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) { @@ -77,8 +71,8 @@ async function withResourceHarness(userId: number, fn: (h: McpHarness) => Promis describe('Tool: list_collab_polls', () => { it('returns empty array initially', 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_collab_polls', @@ -91,9 +85,9 @@ describe('Tool: list_collab_polls', () => { }); 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_collab_polls', arguments: { tripId: trip.id } }); expect(result.isError).toBe(true); @@ -107,8 +101,8 @@ describe('Tool: list_collab_polls', () => { describe('Tool: create_collab_poll', () => { it('inserts poll with votes structure and broadcasts', 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_collab_poll', @@ -127,8 +121,8 @@ describe('Tool: create_collab_poll', () => { 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_collab_poll', @@ -139,9 +133,9 @@ describe('Tool: create_collab_poll', () => { }); 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: 'create_collab_poll', @@ -158,8 +152,8 @@ describe('Tool: create_collab_poll', () => { describe('Tool: vote_collab_poll', () => { it('records vote and broadcasts collab:poll:voted', 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 poll directly in the DB const pollId = await insertRow(orm, CollabPolls, { trip: trip.id, @@ -181,8 +175,8 @@ describe('Tool: vote_collab_poll', () => { it('blocks demo user (gate added with the DI migration — the legacy registrar missed it)', 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); const pollId = await insertRow(orm, CollabPolls, { trip: trip.id, user: user.id, @@ -201,9 +195,9 @@ describe('Tool: vote_collab_poll', () => { }); 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: 'vote_collab_poll', @@ -220,8 +214,8 @@ describe('Tool: vote_collab_poll', () => { describe('Tool: close_collab_poll', () => { it('sets closed flag and broadcasts collab:poll:closed', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const pollId = await insertRow(orm, CollabPolls, { trip: trip.id, user: user.id, @@ -241,8 +235,8 @@ describe('Tool: close_collab_poll', () => { }); it('returns error for non-existent poll', 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: 'close_collab_poll', @@ -253,9 +247,9 @@ describe('Tool: close_collab_poll', () => { }); 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: 'close_collab_poll', arguments: { tripId: trip.id, pollId: 1 } }); expect(result.isError).toBe(true); @@ -269,8 +263,8 @@ describe('Tool: close_collab_poll', () => { describe('Tool: delete_collab_poll', () => { it('removes poll and broadcasts collab:poll:deleted', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const pollId = await insertRow(orm, CollabPolls, { trip: trip.id, user: user.id, @@ -295,9 +289,9 @@ describe('Tool: delete_collab_poll', () => { }); 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: 'delete_collab_poll', arguments: { tripId: trip.id, pollId: 1 } }); expect(result.isError).toBe(true); @@ -311,8 +305,8 @@ describe('Tool: delete_collab_poll', () => { describe('Tool: list_collab_messages', () => { it('returns empty array initially', 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_collab_messages', @@ -325,9 +319,9 @@ describe('Tool: list_collab_messages', () => { }); 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_collab_messages', arguments: { tripId: trip.id } }); expect(result.isError).toBe(true); @@ -341,8 +335,8 @@ describe('Tool: list_collab_messages', () => { describe('Tool: send_collab_message', () => { it('inserts message and broadcasts collab:message:created', 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: 'send_collab_message', @@ -355,8 +349,8 @@ describe('Tool: send_collab_message', () => { }); it('sends message with replyTo when parent exists', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const msgId = (await makeCollabMessage(orm, trip.id, user.id, { text: 'Original message' })).id; await withHarness(user.id, async (h) => { @@ -371,8 +365,8 @@ describe('Tool: send_collab_message', () => { 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: 'send_collab_message', @@ -383,9 +377,9 @@ describe('Tool: send_collab_message', () => { }); 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: 'send_collab_message', @@ -402,8 +396,8 @@ describe('Tool: send_collab_message', () => { describe('Tool: delete_collab_message', () => { it('soft-deletes message and broadcasts collab:message:deleted', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const msgId = (await makeCollabMessage(orm, trip.id, user.id, { text: 'To be deleted' })).id; await withHarness(user.id, async (h) => { @@ -423,9 +417,9 @@ describe('Tool: delete_collab_message', () => { }); it('returns error when message belongs to different user', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); // Add other as trip member await insertRow(orm, TripMembers, { trip: trip.id, user: other.id }); const msgId = (await makeCollabMessage(orm, trip.id, user.id, { text: 'Owner message' })).id; @@ -440,9 +434,9 @@ describe('Tool: delete_collab_message', () => { }); 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: 'delete_collab_message', @@ -459,8 +453,8 @@ describe('Tool: delete_collab_message', () => { describe('Tool: react_collab_message', () => { it('toggles reaction and broadcasts collab:message:reacted', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const msgId = (await makeCollabMessage(orm, trip.id, user.id, { text: 'React to me' })).id; await withHarness(user.id, async (h) => { @@ -475,8 +469,8 @@ describe('Tool: react_collab_message', () => { }); it('returns error for non-existent message', 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: 'react_collab_message', @@ -487,9 +481,9 @@ describe('Tool: react_collab_message', () => { }); 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: 'react_collab_message', @@ -506,8 +500,8 @@ describe('Tool: react_collab_message', () => { describe('Resource: trek://trips/{tripId}/collab/polls', () => { it('returns polls list', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await withResourceHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: `trek://trips/${trip.id}/collab/polls` }); const data = parseResourceResult(result) as any; @@ -516,9 +510,9 @@ describe('Resource: trek://trips/{tripId}/collab/polls', () => { }); 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 withResourceHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: `trek://trips/${trip.id}/collab/polls` }); const data = parseResourceResult(result) as any; @@ -529,8 +523,8 @@ describe('Resource: trek://trips/{tripId}/collab/polls', () => { describe('Resource: trek://trips/{tripId}/collab/messages', () => { it('returns messages list', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await withResourceHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: `trek://trips/${trip.id}/collab/messages` }); const data = parseResourceResult(result) as any; @@ -541,8 +535,8 @@ describe('Resource: trek://trips/{tripId}/collab/messages', () => { describe('send_collab_message, as the chat posts it', () => { it('MCP-COLLAB-001: notifies the fellow travellers with the preview a typed message gets, after the room heard it', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const send = vi .spyOn((await bootTestApp(realtime)).get(NotificationsService), 'send') .mockResolvedValue({ attempted: 0, delivered: 0 }); @@ -574,8 +568,8 @@ describe('send_collab_message, as the chat posts it', () => { */ describe('Collab link tools (#3076)', () => { beforeEach(async () => { - setAddonEnabled(testDb, ADDON_IDS.COLLAB, true); - // resetTestDb truncates app_settings, but the permission cache is module-scoped + await setAddonEnabled(orm, ADDON_IDS.COLLAB, true); + // resetDatabase empties app_settings, but the permission cache is module-scoped // and would keep serving whatever the previous case configured. await invalidatePermissionsCache(); }); @@ -610,10 +604,10 @@ describe('Collab link tools (#3076)', () => { /** A member of the owner's trip, once collab_edit is lowered to the owner alone. */ async function memberWithoutEdit() { - 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 setAppSetting(orm, 'perm_collab_edit', 'trip_owner'); await invalidatePermissionsCache(); return { owner, member, trip }; @@ -623,8 +617,8 @@ describe('Collab link tools (#3076)', () => { describe('Tool: list_collab_links', () => { it('MCP-COLLAB-LINK-001: lists the links pinned first, then newest first, with their author', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await insertLink(trip.id, user.id, 'Timetable'); const pinned = await insertLink(trip.id, user.id, 'Hotel', 1); const data = parseToolResult(await call(user.id, 'list_collab_links', { tripId: trip.id })) as { @@ -635,9 +629,9 @@ describe('Collab link tools (#3076)', () => { }); it('MCP-COLLAB-LINK-002: refuses a trip the user is not part of', async () => { - const { user } = createUser(testDb); - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user } = await makeUser(orm); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); await insertLink(trip.id, owner.id); expect((await call(user.id, 'list_collab_links', { tripId: trip.id })).isError).toBe(true); }); @@ -653,8 +647,8 @@ describe('Collab link tools (#3076)', () => { describe('Tool: create_collab_link', () => { it('MCP-COLLAB-LINK-004: saves the link with the caller as author 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); const result = await call(user.id, 'create_collab_link', { tripId: trip.id, title: ' Ferry tickets ', @@ -678,8 +672,8 @@ describe('Collab link tools (#3076)', () => { }); it('MCP-COLLAB-LINK-005: leaves a link unpinned unless asked', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const result = await call(user.id, 'create_collab_link', { tripId: trip.id, title: 'Map', @@ -689,8 +683,8 @@ describe('Collab link tools (#3076)', () => { }); it('MCP-COLLAB-LINK-006: refuses a URL that is not http(s), or an empty title, like the REST route', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const refused = await Promise.all( [ { title: 'Script', url: 'javascript:alert(1)' }, @@ -715,9 +709,9 @@ describe('Collab link tools (#3076)', () => { }); it('MCP-COLLAB-LINK-008: refuses a trip the user is not part of', async () => { - const { user } = createUser(testDb); - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user } = await makeUser(orm); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); const result = await call(user.id, 'create_collab_link', { tripId: trip.id, title: 'Ferry', @@ -729,8 +723,8 @@ describe('Collab link tools (#3076)', () => { it('MCP-COLLAB-LINK-009: blocks the 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); const result = await call(user.id, 'create_collab_link', { tripId: trip.id, title: 'Ferry', @@ -742,8 +736,8 @@ describe('Collab link tools (#3076)', () => { describe('Tool: update_collab_link', () => { it('MCP-COLLAB-LINK-010: changes only the fields given and broadcasts the link', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const id = await insertLink(trip.id, user.id, 'Ferry'); const result = await call(user.id, 'update_collab_link', { tripId: trip.id, linkId: id, pinned: true }); expect((parseToolResult(result) as { link: Record }).link).toMatchObject({ @@ -760,8 +754,8 @@ describe('Collab link tools (#3076)', () => { }); it('MCP-COLLAB-LINK-011: renames, moves and unpins a link', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const id = await insertLink(trip.id, user.id, 'Ferry', 1); await call(user.id, 'update_collab_link', { tripId: trip.id, @@ -778,9 +772,9 @@ describe('Collab link tools (#3076)', () => { }); it('MCP-COLLAB-LINK-012: says the link is not found when it belongs to another trip, and leaves it alone', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const other = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const other = await makeTrip(orm, user.id); const id = await insertLink(other.id, user.id, 'Ferry'); const result = await call(user.id, 'update_collab_link', { tripId: trip.id, linkId: id, title: 'Moved' }); expect(result.isError).toBe(true); @@ -790,8 +784,8 @@ describe('Collab link tools (#3076)', () => { }); it('MCP-COLLAB-LINK-013: refuses a URL that is not http(s)', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const id = await insertLink(trip.id, user.id); const result = await call(user.id, 'update_collab_link', { tripId: trip.id, @@ -813,8 +807,8 @@ describe('Collab link tools (#3076)', () => { describe('Tool: delete_collab_link', () => { it('MCP-COLLAB-LINK-015: removes the link and broadcasts its id', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const id = await insertLink(trip.id, user.id); const result = await call(user.id, 'delete_collab_link', { tripId: trip.id, linkId: id }); expect(parseToolResult(result)).toEqual({ success: true }); @@ -827,9 +821,9 @@ describe('Collab link tools (#3076)', () => { }); it('MCP-COLLAB-LINK-016: says the link is not found for a link of another trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const other = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const other = await makeTrip(orm, user.id); const id = await insertLink(other.id, user.id); const result = await call(user.id, 'delete_collab_link', { tripId: trip.id, linkId: id }); expect(result.isError).toBe(true); @@ -846,21 +840,21 @@ describe('Collab link tools (#3076)', () => { describe('Collab link tools, gating', () => { it('MCP-COLLAB-LINK-018: are offered only while the links are switched on', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); expect(await toolNames(user.id)).toEqual(expect.arrayContaining(LINK_TOOLS)); - setCollabFeature(testDb, 'links', false); + await setCollabFeature(orm, 'links', false); try { const names = await toolNames(user.id); expect(names.filter((name) => LINK_TOOLS.includes(name))).toEqual([]); expect(names).toContain('list_collab_notes'); } finally { - setCollabFeature(testDb, 'links', true); + await setCollabFeature(orm, 'links', true); } }); it('MCP-COLLAB-LINK-019: are gone with the collab addon', async () => { - const { user } = createUser(testDb); - setAddonEnabled(testDb, ADDON_IDS.COLLAB, false); + const { user } = await makeUser(orm); + await setAddonEnabled(orm, ADDON_IDS.COLLAB, false); const names = await toolNames(user.id); expect(names.filter((name) => LINK_TOOLS.includes(name))).toEqual([]); });