diff --git a/server/scripts/test-mocks-baseline.json b/server/scripts/test-mocks-baseline.json index 772284f33..a63827f77 100644 --- a/server/scripts/test-mocks-baseline.json +++ b/server/scripts/test-mocks-baseline.json @@ -63,13 +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/demo-tool-gate.test.ts": 1, - "tests/unit/mcp/tools-addon-gating.test.ts": 1, "tests/unit/mcp/tools-airtrail.test.ts": 1, - "tests/unit/mcp/tools-assignments-reservations-extra.test.ts": 1, - "tests/unit/mcp/tools-assignments.test.ts": 1, "tests/unit/mcp/tools-atlas-expanded.test.ts": 1, - "tests/unit/mcp/tools-atlas.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, diff --git a/server/tests/postgres/snapshot-suites-baseline.json b/server/tests/postgres/snapshot-suites-baseline.json index a0f9adac0..20f0b1aa8 100644 --- a/server/tests/postgres/snapshot-suites-baseline.json +++ b/server/tests/postgres/snapshot-suites-baseline.json @@ -1,11 +1,6 @@ [ - "tests/unit/mcp/demo-tool-gate.test.ts", - "tests/unit/mcp/tools-addon-gating.test.ts", "tests/unit/mcp/tools-airtrail.test.ts", - "tests/unit/mcp/tools-assignments-reservations-extra.test.ts", - "tests/unit/mcp/tools-assignments.test.ts", "tests/unit/mcp/tools-atlas-expanded.test.ts", - "tests/unit/mcp/tools-atlas.test.ts", "tests/unit/mcp/tools-budget-advanced.test.ts", "tests/unit/mcp/tools-budget.test.ts", "tests/unit/mcp/tools-categories.test.ts", diff --git a/server/tests/unit/mcp/demo-tool-gate.test.ts b/server/tests/unit/mcp/demo-tool-gate.test.ts index 372992254..741cb87e3 100644 --- a/server/tests/unit/mcp/demo-tool-gate.test.ts +++ b/server/tests/unit/mcp/demo-tool-gate.test.ts @@ -11,24 +11,19 @@ * fails here instead of slipping past the gate. */ import { ADDON_IDS } from '../../../src/addons'; -import { db as testDb } from '../../../src/db/database'; import { DEMO_UNGATED_WRITE_TOOLS, isDemoGatedTool, trekDemoToolGate } from '../../../src/mcp/nest-mcp-policy'; import type { McpContext, McpRegistry, McpRegistryListing } from '../../../src/nest-mcp'; import { withRequestContext } from '../../../src/nest/database/request-context'; -import { createUser, setAppSetting } from '../../helpers/factories'; +import { setAddonEnabled, setAppSetting } from '../../helpers/factories/settings'; +import { makeUser } from '../../helpers/factories/users'; import { createMcpTestRegistry } from '../../helpers/mcp-test-controllers'; +import { resetDatabase } from '../../helpers/reset-database'; import { bootTestApp } from '../../helpers/test-app'; -import { resetTestDb, setAddonEnabled } from '../../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp'; import { describe, it, expect, vi, beforeAll, beforeEach, afterEach, afterAll } from 'vitest'; -vi.mock('../../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - const DEMO_REFUSAL = { content: [{ type: 'text', text: 'Write operations are disabled in demo mode.' }], isError: true, @@ -74,25 +69,25 @@ beforeAll(async () => { toolListings = registry.list().filter((entry) => entry.kind === 'tool'); }); -beforeEach(() => { - resetTestDb(testDb); - for (const id of Object.values(ADDON_IDS)) setAddonEnabled(testDb, id, true); +beforeEach(async () => { + await resetDatabase(orm); + for (const id of Object.values(ADDON_IDS)) await setAddonEnabled(orm, id, true); // The wanderer tools show only while wanderer is allowed under Tours. - setAppSetting(testDb, 'tours_wanderer_enabled', 'true'); + await setAppSetting(orm, 'tours_wanderer_enabled', 'true'); }); afterEach(() => { delete process.env.DEMO_MODE; }); -afterAll(() => { - testDb.close(); +afterAll(async () => { + await (await bootTestApp()).close(); }); describe('demo tool gate over the whole registry', () => { it('DEMOGATE-001: every write tool refuses the demo account with the canned body, before its handler', async () => { process.env.DEMO_MODE = 'true'; - const { user: demo } = createUser(testDb, { email: 'demo@trek.app' }); + const { user: demo } = await makeUser(orm, { email: 'demo@trek.app' }); const tools = await captureTools(registry, { userId: demo.id, scopes: null, isStaticToken: false }); const writes = [...tools].filter( @@ -105,7 +100,7 @@ describe('demo tool gate over the whole registry', () => { it('DEMOGATE-007: the write tools that never had a demo check stay open to the demo account', async () => { process.env.DEMO_MODE = 'true'; - const { user: demo } = createUser(testDb, { email: 'demo@trek.app' }); + const { user: demo } = await makeUser(orm, { email: 'demo@trek.app' }); const ctx: McpContext = { userId: demo.id, scopes: null, isStaticToken: false }; const tools = await captureTools(registry, ctx); const gate = trekDemoToolGate(async () => true); @@ -133,7 +128,7 @@ describe('demo tool gate over the whole registry', () => { it('DEMOGATE-002: no read-only tool is held back for the demo account', async () => { process.env.DEMO_MODE = 'true'; - const { user: demo } = createUser(testDb, { email: 'demo@trek.app' }); + const { user: demo } = await makeUser(orm, { email: 'demo@trek.app' }); const ctx: McpContext = { userId: demo.id, scopes: null, isStaticToken: false }; const gate = trekDemoToolGate(async () => true); const tools = await captureTools(registry, ctx); @@ -148,7 +143,7 @@ describe('demo tool gate over the whole registry', () => { it('DEMOGATE-003: an ordinary account reaches the write handlers in demo mode', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'alice@example.com' }); + const { user } = await makeUser(orm, { email: 'alice@example.com' }); const tools = await captureTools(registry, { userId: user.id, scopes: null, isStaticToken: false }); // create_tag has no trip to check and no addon gate: it writes and answers. const result = (await tools.get('create_tag')?.call({ name: 'Mine' })) as { @@ -160,14 +155,14 @@ describe('demo tool gate over the whole registry', () => { }); it('DEMOGATE-004: outside demo mode the demo account is an ordinary account', async () => { - const { user: demo } = createUser(testDb, { email: 'demo@trek.app' }); + const { user: demo } = await makeUser(orm, { email: 'demo@trek.app' }); const tools = await captureTools(registry, { userId: demo.id, scopes: null, isStaticToken: false }); const result = (await tools.get('create_tag')?.call({ name: 'Mine' })) as { isError?: boolean }; expect(result.isError).toBeUndefined(); }); it('DEMOGATE-005: with every addon on, only the photo-library tools stay detached, so the sweep above covers the rest', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const tools = await captureTools(registry, { userId: user.id, scopes: null, isStaticToken: false }); const missing = toolListings.map((t) => t.name).filter((name) => !tools.has(name)); // These three wait for a connected Immich or Synology provider @@ -176,7 +171,7 @@ describe('demo tool gate over the whole registry', () => { }); it('DEMOGATE-006: the scope mode and the read-only annotation agree on every declarative tool', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const tools = await captureTools(registry, { userId: user.id, scopes: null, isStaticToken: false }); const mismatched: string[] = []; for (const listing of toolListings) { diff --git a/server/tests/unit/mcp/tools-addon-gating.test.ts b/server/tests/unit/mcp/tools-addon-gating.test.ts index 846007ab4..398eae693 100644 --- a/server/tests/unit/mcp/tools-addon-gating.test.ts +++ b/server/tests/unit/mcp/tools-addon-gating.test.ts @@ -2,36 +2,40 @@ * Unit tests for MCP addon gating and scope enforcement in tools. */ import { ADDON_IDS } from '../../../src/addons'; -import { db as testDb } from '../../../src/db/database'; -import { createUser, createTrip } from '../../helpers/factories'; +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, type McpHarness } from '../../helpers/mcp-harness'; -import { resetTestDb } from '../../helpers/test-db'; -import { setAddonEnabled } from '../../helpers/test-db'; +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(() => { - resetTestDb(testDb); +// The booted container's own ORM, the one the tools write through. +let orm: MikroORM; + +beforeAll(async () => { + orm = (await bootTestApp(realtime)).get(MikroORM); +}); + +beforeEach(async () => { + await resetDatabase(orm); broadcastMock.mockClear(); // The summary reads these three off the injected AddonsService, so the case - // that wants one off writes the row the admin panel writes. resetTestDb - // leaves the addons table alone, so each case restates what it needs. + // that wants one off writes the row the admin panel writes. resetDatabase + // puts back the default addon rows, so each case restates what it needs. for (const id of [ADDON_IDS.BUDGET, ADDON_IDS.PACKING, ADDON_IDS.COLLAB]) { - setAddonEnabled(testDb, id, true); + await setAddonEnabled(orm, id, true); } }); -afterAll(() => { - testDb.close(); +afterAll(async () => { + await (await bootTestApp(realtime)).close(); }); async function withHarness(userId: number, fn: (h: McpHarness) => Promise, scopes?: string[] | null) { @@ -49,8 +53,8 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise, describe('get_trip_summary — addon gating', () => { it('when all addons enabled: packing, budget, collab_notes, todos are present', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Full Trip' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Full Trip' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } }); @@ -64,10 +68,10 @@ describe('get_trip_summary — addon gating', () => { }); it('when budget disabled: budget is undefined in response', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'No Budget Trip' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'No Budget Trip' }); - setAddonEnabled(testDb, ADDON_IDS.BUDGET, false); + await setAddonEnabled(orm, ADDON_IDS.BUDGET, false); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } }); @@ -81,10 +85,10 @@ describe('get_trip_summary — addon gating', () => { }); it('when packing disabled: packing is undefined and todos is empty array', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'No Packing Trip' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'No Packing Trip' }); - setAddonEnabled(testDb, ADDON_IDS.PACKING, false); + await setAddonEnabled(orm, ADDON_IDS.PACKING, false); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } }); @@ -97,10 +101,10 @@ describe('get_trip_summary — addon gating', () => { }); it('when collab disabled: collab_notes is empty array, pollCount is 0, messageCount is 0', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'No Collab Trip' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'No Collab Trip' }); - setAddonEnabled(testDb, ADDON_IDS.COLLAB, false); + await setAddonEnabled(orm, ADDON_IDS.COLLAB, false); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } }); @@ -120,9 +124,9 @@ describe('get_trip_summary — addon gating', () => { describe('Budget tools — addon gating', () => { it('when budget addon disabled, create_budget_item is not registered', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); - setAddonEnabled(testDb, ADDON_IDS.BUDGET, false); + await setAddonEnabled(orm, ADDON_IDS.BUDGET, false); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -140,9 +144,9 @@ describe('Budget tools — addon gating', () => { describe('Packing tools — addon gating', () => { it('when packing addon disabled, create_packing_item is not registered', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); - setAddonEnabled(testDb, ADDON_IDS.PACKING, false); + await setAddonEnabled(orm, ADDON_IDS.PACKING, false); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -160,9 +164,9 @@ describe('Packing tools — addon gating', () => { describe('Collab tools — addon gating', () => { it('when collab addon disabled, create_collab_note is not registered', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); - setAddonEnabled(testDb, ADDON_IDS.COLLAB, false); + await setAddonEnabled(orm, ADDON_IDS.COLLAB, false); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -180,9 +184,9 @@ describe('Collab tools — addon gating', () => { describe('Atlas tools — addon gating', () => { it('when atlas addon disabled, mark_country_visited is not registered', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); - setAddonEnabled(testDb, ADDON_IDS.ATLAS, false); + await setAddonEnabled(orm, ADDON_IDS.ATLAS, false); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'mark_country_visited', arguments: { country_code: 'FR' } }); @@ -191,9 +195,9 @@ describe('Atlas tools — addon gating', () => { }); it('when atlas addon disabled, create_bucket_list_item is not registered', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); - setAddonEnabled(testDb, ADDON_IDS.ATLAS, false); + await setAddonEnabled(orm, ADDON_IDS.ATLAS, false); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_bucket_list_item', arguments: { name: 'Paris' } }); @@ -208,7 +212,7 @@ describe('Atlas tools — addon gating', () => { describe('Scope enforcement in tools', () => { it('with scopes trips:read, create_trip is not registered (write not in scopes)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness( user.id, @@ -221,7 +225,7 @@ describe('Scope enforcement in tools', () => { }); it('with scopes trips:write, create_trip is registered and works', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness( user.id, @@ -236,7 +240,7 @@ describe('Scope enforcement in tools', () => { }); it('with scopes null (full access), create_trip is registered', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness( user.id, @@ -249,7 +253,7 @@ describe('Scope enforcement in tools', () => { }); it('with scopes trips:read, create_budget_item is not registered (budget:write not in scopes)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness( user.id, @@ -265,8 +269,8 @@ describe('Scope enforcement in tools', () => { }); it('with scopes budget:write and trips:read, create_budget_item is registered (budget addon enabled)', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Budget Trip' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Budget Trip' }); await withHarness( user.id, diff --git a/server/tests/unit/mcp/tools-assignments-reservations-extra.test.ts b/server/tests/unit/mcp/tools-assignments-reservations-extra.test.ts index 1b016bba9..43e44c9b8 100644 --- a/server/tests/unit/mcp/tools-assignments-reservations-extra.test.ts +++ b/server/tests/unit/mcp/tools-assignments-reservations-extra.test.ts @@ -2,48 +2,40 @@ * Unit tests for MCP extra assignment/reservation tools: * move_assignment, get_assignment_participants, set_assignment_participants, reorder_reservations. */ -import { db as testDb } from '../../../src/db/database'; import { AssignmentParticipants } from '../../../src/db/entities/AssignmentParticipants.entity'; import { DayAssignments } from '../../../src/db/entities/DayAssignments.entity'; -import { - createUser, - createTrip, - createDay, - createPlace, - createDayAssignment, - createReservation, -} from '../../helpers/factories'; +import { makeDayAssignment } from '../../helpers/factories/itinerary'; +import { makePlace } from '../../helpers/factories/places'; +import { makeReservation } from '../../helpers/factories/reservations'; import { findRow, insertRow } from '../../helpers/factories/rows'; +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 { 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()); -}); +import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; const realtime = new FakeRealtimeService(); const broadcastMock = realtime.broadcastMock; -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); }); -beforeEach(() => { - resetTestDb(testDb); +beforeEach(async () => { + await resetDatabase(orm); broadcastMock.mockClear(); delete process.env.DEMO_MODE; }); afterAll(async () => { - await orm.close(); - testDb.close(); + await (await bootTestApp(realtime)).close(); }); async function withHarness(userId: number, fn: (h: McpHarness) => Promise) { @@ -61,12 +53,12 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise) describe('Tool: move_assignment', () => { it('moves assignment to a different day and broadcasts', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day1 = createDay(testDb, trip.id); - const day2 = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day1.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day1 = await makeDay(orm, trip.id); + const day2 = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day1.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -93,10 +85,10 @@ describe('Tool: move_assignment', () => { }); it('returns access denied for non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const day = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.id); + const day = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'move_assignment', @@ -108,9 +100,9 @@ describe('Tool: move_assignment', () => { 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 day = createDay(testDb, trip.id); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'move_assignment', @@ -127,11 +119,11 @@ describe('Tool: move_assignment', () => { describe('Tool: get_assignment_participants', () => { it('returns empty participants array initially', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_assignment_participants', @@ -144,9 +136,9 @@ describe('Tool: get_assignment_participants', () => { }); 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: 'get_assignment_participants', @@ -163,11 +155,11 @@ describe('Tool: get_assignment_participants', () => { describe('Tool: set_assignment_participants', () => { it('sets participants and broadcasts', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'set_assignment_participants', @@ -180,11 +172,11 @@ describe('Tool: set_assignment_participants', () => { }); it('empty array clears participants', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); // First set await insertRow(orm, AssignmentParticipants, { assignment: assignment.id, user: user.id }); await withHarness(user.id, async (h) => { @@ -198,9 +190,9 @@ describe('Tool: set_assignment_participants', () => { }); 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: 'set_assignment_participants', @@ -217,10 +209,10 @@ describe('Tool: set_assignment_participants', () => { describe('Tool: reorder_reservations', () => { it('returns success and broadcasts reservation:positions', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const res1 = createReservation(testDb, trip.id, { title: 'Flight', type: 'flight' }); - const res2 = createReservation(testDb, trip.id, { title: 'Hotel', type: 'hotel' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const res1 = await makeReservation(orm, trip.id, { title: 'Flight', type: 'flight' }); + const res2 = await makeReservation(orm, trip.id, { title: 'Hotel', type: 'hotel' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'reorder_reservations', @@ -239,10 +231,10 @@ describe('Tool: reorder_reservations', () => { }); it("names the day under REST's day_id and keeps the dayId the tool always sent", async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const res = createReservation(testDb, trip.id, { title: 'Train', type: 'train', day_id: day.id }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const res = await makeReservation(orm, trip.id, { title: 'Train', type: 'train', day: day.id }); const positions = [{ id: res.id, day_plan_position: 3 }]; await withHarness(user.id, async (h) => { await h.client.callTool({ @@ -256,9 +248,9 @@ describe('Tool: reorder_reservations', () => { }); 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: 'reorder_reservations', diff --git a/server/tests/unit/mcp/tools-assignments.test.ts b/server/tests/unit/mcp/tools-assignments.test.ts index ff1a6f127..a944ae877 100644 --- a/server/tests/unit/mcp/tools-assignments.test.ts +++ b/server/tests/unit/mcp/tools-assignments.test.ts @@ -2,41 +2,33 @@ * Unit tests for MCP assignment tools: assign_place_to_day, unassign_place, * reorder_day_assignments, update_assignment_time, update_assignment_notes. */ -import { db as testDb } from '../../../src/db/database'; import { DayAssignments } from '../../../src/db/entities/DayAssignments.entity'; import { JourneyEntries } from '../../../src/db/entities/JourneyEntries.entity'; import { RoadtripVias } from '../../../src/db/entities/RoadtripVias.entity'; import { TourWaypoints } from '../../../src/db/entities/TourWaypoints.entity'; -import { - createUser, - createTrip, - createDay, - createPlace, - createDayAssignment, - createJourney, -} from '../../helpers/factories'; -import { linkTripToJourney } from '../../helpers/factories/journeys'; +import { makeDayAssignment } from '../../helpers/factories/itinerary'; +import { makeJourney, linkTripToJourney } from '../../helpers/factories/journeys'; +import { makePlace } from '../../helpers/factories/places'; import { countRows, findRow, findRows, insertRow, insertRows, updateRows } from '../../helpers/factories/rows'; +import { makeTour } from '../../helpers/factories/tours'; +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 { resetTestDb } from '../../helpers/test-db'; -import { createTestOrm, type TestOrm } from '../../helpers/test-orm'; -import { createTour } from '../../helpers/tours-repos'; +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; -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); }); /** The day's assignment ids in order_index order. */ @@ -62,15 +54,14 @@ function skeletonFor(journeyId: number, placeId: number) { return findRow(orm, JourneyEntries, { journey: journeyId, sourcePlace: placeId }); } -beforeEach(() => { - resetTestDb(testDb); +beforeEach(async () => { + await resetDatabase(orm); broadcastMock.mockClear(); delete process.env.DEMO_MODE; }); afterAll(async () => { - await orm.close(); - testDb.close(); + await (await bootTestApp(realtime)).close(); }); async function withHarness(userId: number, fn: (h: McpHarness) => Promise) { @@ -88,11 +79,11 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise) describe('Tool: assign_place_to_day', () => { it('sets and clears a visit day end through the shared service', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const visit = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const visit = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { for (const end_day of [true, false]) { const changed = await h.client.callTool({ @@ -101,7 +92,7 @@ describe('Tool: assign_place_to_day', () => { }); expect(parseToolResult(changed)).toMatchObject({ assignment: { end_day } }); } - const other = createTrip(testDb, user.id); + const other = await makeTrip(orm, user.id); const refused = await h.client.callTool({ name: 'set_assignment_end_day', arguments: { tripId: other.id, assignmentId: visit.id, end_day: true }, @@ -111,19 +102,19 @@ describe('Tool: assign_place_to_day', () => { }); it('set_assignment_stay: changes the stay of one visit and leaves the other visit of the place alone (#3092)', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const first = createDayAssignment(testDb, day.id, place.id); - const second = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const first = await makeDayAssignment(orm, day.id, place.id); + const second = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const changed = await h.client.callTool({ name: 'set_assignment_stay', arguments: { tripId: trip.id, assignmentId: second.id, stay_minutes: 5 }, }); expect(parseToolResult(changed)).toMatchObject({ assignment: { id: second.id, stay_minutes: 5 } }); - const other = createTrip(testDb, user.id); + const other = await makeTrip(orm, user.id); const refused = await h.client.callTool({ name: 'set_assignment_stay', arguments: { tripId: other.id, assignmentId: second.id, stay_minutes: 5 }, @@ -135,12 +126,12 @@ describe('Tool: assign_place_to_day', () => { }); it('MDT-MCP-001:puts one stage of a multi-day tour on a day, refusing as REST does', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const secondDay = createDay(testDb, trip.id); - const tour = createPlace(testDb, trip.id); - createTour(testDb, tour.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const secondDay = await makeDay(orm, trip.id); + const tour = await makePlace(orm, trip.id); + await makeTour(orm, tour.id, { max_hiking_difficulty: 2 }); await insertRows( orm, TourWaypoints, @@ -178,10 +169,10 @@ describe('Tool: assign_place_to_day', () => { }); it('assigns a place to a day', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -197,11 +188,11 @@ describe('Tool: assign_place_to_day', () => { }); it('creates a skeleton suggestion in a linked journey', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id, { name: 'Linked Place' }); - const journey = createJourney(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id, { name: 'Linked Place' }); + const journey = await makeJourney(orm, user.id); await linkJourney(journey.id, trip.id); await withHarness(user.id, async (h) => { @@ -217,12 +208,12 @@ describe('Tool: assign_place_to_day', () => { }); it('auto-increments order_index for subsequent assignments', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place1 = createPlace(testDb, trip.id, { name: 'P1' }); - const place2 = createPlace(testDb, trip.id, { name: 'P2' }); - createDayAssignment(testDb, day.id, place1.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place1 = await makePlace(orm, trip.id, { name: 'P1' }); + const place2 = await makePlace(orm, trip.id, { name: 'P2' }); + await makeDayAssignment(orm, day.id, place1.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -235,10 +226,10 @@ describe('Tool: assign_place_to_day', () => { }); it('broadcasts assignment:created event', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'assign_place_to_day', @@ -249,11 +240,11 @@ describe('Tool: assign_place_to_day', () => { }); it('returns error when day does not belong to trip', async () => { - const { user } = createUser(testDb); - const trip1 = createTrip(testDb, user.id); - const trip2 = createTrip(testDb, user.id); - const dayFromTrip2 = createDay(testDb, trip2.id); - const place = createPlace(testDb, trip1.id); + const { user } = await makeUser(orm); + const trip1 = await makeTrip(orm, user.id); + const trip2 = await makeTrip(orm, user.id); + const dayFromTrip2 = await makeDay(orm, trip2.id); + const place = await makePlace(orm, trip1.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -265,11 +256,11 @@ describe('Tool: assign_place_to_day', () => { }); it('returns error when place does not belong to trip', async () => { - const { user } = createUser(testDb); - const trip1 = createTrip(testDb, user.id); - const trip2 = createTrip(testDb, user.id); - const day = createDay(testDb, trip1.id); - const placeFromTrip2 = createPlace(testDb, trip2.id); + const { user } = await makeUser(orm); + const trip1 = await makeTrip(orm, user.id); + const trip2 = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip1.id); + const placeFromTrip2 = await makePlace(orm, trip2.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -281,11 +272,11 @@ describe('Tool: assign_place_to_day', () => { }); it('returns access denied for non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'assign_place_to_day', @@ -302,11 +293,11 @@ describe('Tool: assign_place_to_day', () => { describe('Tool: unassign_place', () => { it('removes a place assignment from a day', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -320,11 +311,11 @@ describe('Tool: unassign_place', () => { }); it('broadcasts assignment:deleted event', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'unassign_place', @@ -335,11 +326,11 @@ describe('Tool: unassign_place', () => { }); it('removes the linked journey skeleton when the place is unassigned', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const journey = createJourney(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const journey = await makeJourney(orm, user.id); await linkJourney(journey.id, trip.id); await withHarness(user.id, async (h) => { @@ -361,9 +352,9 @@ describe('Tool: unassign_place', () => { }); it('returns error when assignment is not found', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'unassign_place', @@ -374,12 +365,12 @@ describe('Tool: unassign_place', () => { }); it('returns access denied for non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'unassign_place', @@ -396,12 +387,12 @@ describe('Tool: unassign_place', () => { describe('Tool: clear_day_assignments (#2470)', () => { it('removes every place from the day and broadcasts each removal', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const a1 = createDayAssignment(testDb, day.id, place.id); - const a2 = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const a1 = await makeDayAssignment(orm, day.id, place.id); + const a2 = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const data = parseToolResult( await h.client.callTool({ name: 'clear_day_assignments', arguments: { tripId: trip.id, dayId: day.id } }), @@ -418,10 +409,10 @@ describe('Tool: clear_day_assignments (#2470)', () => { }); it('returns an error for a day on 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); + 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); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'clear_day_assignments', @@ -432,10 +423,10 @@ describe('Tool: clear_day_assignments (#2470)', () => { }); it('returns access denied for non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const day = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.id); + const day = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'clear_day_assignments', @@ -448,13 +439,13 @@ describe('Tool: clear_day_assignments (#2470)', () => { describe('Tool: reorder_day_assignments', () => { it('reorders assignments by updating order_index', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place1 = createPlace(testDb, trip.id, { name: 'First' }); - const place2 = createPlace(testDb, trip.id, { name: 'Second' }); - const a1 = createDayAssignment(testDb, day.id, place1.id, { order_index: 0 }); - const a2 = createDayAssignment(testDb, day.id, place2.id, { order_index: 1 }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place1 = await makePlace(orm, trip.id, { name: 'First' }); + const place2 = await makePlace(orm, trip.id, { name: 'Second' }); + const a1 = await makeDayAssignment(orm, day.id, place1.id, { order_index: 0 }); + const a2 = await makeDayAssignment(orm, day.id, place2.id, { order_index: 1 }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -472,11 +463,11 @@ describe('Tool: reorder_day_assignments', () => { }); it('broadcasts assignment:reordered event', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const a = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const a = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'reorder_day_assignments', @@ -491,10 +482,10 @@ describe('Tool: reorder_day_assignments', () => { }); it('returns error when day does not belong to trip', async () => { - const { user } = createUser(testDb); - const trip1 = createTrip(testDb, user.id); - const trip2 = createTrip(testDb, user.id); - const day = createDay(testDb, trip2.id); + const { user } = await makeUser(orm); + const trip1 = await makeTrip(orm, user.id); + const trip2 = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip2.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'reorder_day_assignments', @@ -505,10 +496,10 @@ describe('Tool: reorder_day_assignments', () => { }); it('returns access denied for non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const day = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.id); + const day = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'reorder_day_assignments', @@ -525,11 +516,11 @@ describe('Tool: reorder_day_assignments', () => { describe('Tool: update_assignment_time', () => { it('sets start and end times for an assignment', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -543,11 +534,11 @@ describe('Tool: update_assignment_time', () => { }); it('clears times with null', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await updateRows( orm, DayAssignments, @@ -567,11 +558,11 @@ describe('Tool: update_assignment_time', () => { }); it('broadcasts assignment:updated event', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'update_assignment_time', @@ -584,11 +575,13 @@ describe('Tool: update_assignment_time', () => { // Same service method as PUT /assignments/:id/time, so the same order and the same // three events. A: untimed, B: 15:00, C: untimed, D gets 10:00. it('keeps untimed stops in place, sorts the timed ones and sends the day and its vias like REST', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const [a, b, c, d] = [0, 1, 2, 3].map((i) => createDayAssignment(testDb, day.id, place.id, { order_index: i }).id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const ids: number[] = []; + for (const i of [0, 1, 2, 3]) ids.push((await makeDayAssignment(orm, day.id, place.id, { order_index: i })).id); + const [a, b, c, d] = ids; await updateRows(orm, DayAssignments, { id: b }, { assignment_time: '15:00' }); const via = await insertRow(orm, RoadtripVias, { day: day.id, @@ -627,11 +620,13 @@ describe('Tool: update_assignment_time', () => { }); it('sends only the row when the start leaves the day as it was', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const [a, b, c] = [0, 1, 2].map((i) => createDayAssignment(testDb, day.id, place.id, { order_index: i }).id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const ids: number[] = []; + for (const i of [0, 1, 2]) ids.push((await makeDayAssignment(orm, day.id, place.id, { order_index: i })).id); + const [a, b, c] = ids; await withHarness(user.id, async (h) => { await h.client.callTool({ @@ -646,12 +641,14 @@ describe('Tool: update_assignment_time', () => { }); it('leaves a day out of time order as it is when the call names only the end', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); // B starts before A and was put behind it on purpose. - const [a, b] = [0, 1].map((i) => createDayAssignment(testDb, day.id, place.id, { order_index: i }).id); + const ids: number[] = []; + for (const i of [0, 1]) ids.push((await makeDayAssignment(orm, day.id, place.id, { order_index: i })).id); + const [a, b] = ids; await updateRows(orm, DayAssignments, { id: a }, { assignment_time: '14:00' }); await updateRows(orm, DayAssignments, { id: b }, { assignment_time: '10:00' }); const via = await insertRow(orm, RoadtripVias, { @@ -680,8 +677,8 @@ describe('Tool: update_assignment_time', () => { }); it('returns error when assignment not found', 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_assignment_time', @@ -692,12 +689,12 @@ describe('Tool: update_assignment_time', () => { }); it('returns access denied for non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_assignment_time', @@ -714,11 +711,11 @@ describe('Tool: update_assignment_time', () => { describe('Tool: update_assignment_notes', () => { it('sets the day-specific note on an assignment', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -738,11 +735,11 @@ describe('Tool: update_assignment_notes', () => { }); it('clears the note with null and with an empty string', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await updateRows(orm, DayAssignments, { id: assignment.id }, { notes: 'old' }); await withHarness(user.id, async (h) => { @@ -765,11 +762,11 @@ describe('Tool: update_assignment_notes', () => { }); it('broadcasts assignment:updated event', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'update_assignment_notes', @@ -780,8 +777,8 @@ describe('Tool: update_assignment_notes', () => { }); it('returns error when assignment not found', 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_assignment_notes', @@ -792,12 +789,12 @@ describe('Tool: update_assignment_notes', () => { }); it('returns access denied for non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const day = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.id); + const day = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_assignment_notes', diff --git a/server/tests/unit/mcp/tools-atlas.test.ts b/server/tests/unit/mcp/tools-atlas.test.ts index ba32543e3..fdd2b0017 100644 --- a/server/tests/unit/mcp/tools-atlas.test.ts +++ b/server/tests/unit/mcp/tools-atlas.test.ts @@ -3,37 +3,33 @@ * mark_country_visited, unmark_country_visited, create_bucket_list_item, delete_bucket_list_item, * list_bucket_list. */ -import { db as testDb } from '../../../src/db/database'; import { BucketList } from '../../../src/db/entities/BucketList.entity'; import { VisitedCountries } from '../../../src/db/entities/VisitedCountries.entity'; import { AtlasService } from '../../../src/nest/atlas/atlas.service'; -import { createUser, createBucketListItem, createVisitedCountry } from '../../helpers/factories'; +import { makeBucketListItem, markCountryVisited } from '../../helpers/factories/atlas'; import { countRows, findRow } from '../../helpers/factories/rows'; +import { makeUser } from '../../helpers/factories/users'; import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness'; -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()); -}); - -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); }); -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) { @@ -51,7 +47,7 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise) describe('Tool: mark_country_visited', () => { it('marks a country as visited', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'mark_country_visited', arguments: { country_code: 'FR' } }); const data = parseToolResult(result) as any; @@ -65,7 +61,7 @@ describe('Tool: mark_country_visited', () => { }); it('MCP-ATLAS-AFTER-001: a failed read after the mark leaves `after` out, not the done write', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const stats = vi.spyOn(AtlasService.prototype, 'stats').mockRejectedValue(new Error('stats unavailable')); try { await withHarness(user.id, async (h) => { @@ -80,8 +76,8 @@ describe('Tool: mark_country_visited', () => { }); it('is idempotent — marking twice does not error', async () => { - const { user } = createUser(testDb); - createVisitedCountry(testDb, user.id, 'JP'); + const { user } = await makeUser(orm); + await markCountryVisited(orm, user.id, 'JP'); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'mark_country_visited', arguments: { country_code: 'JP' } }); const data = parseToolResult(result) as any; @@ -93,7 +89,7 @@ describe('Tool: mark_country_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_country_visited', arguments: { country_code: 'DE' } }); expect(result.isError).toBe(true); @@ -107,8 +103,8 @@ describe('Tool: mark_country_visited', () => { describe('Tool: unmark_country_visited', () => { it('removes a visited country', async () => { - const { user } = createUser(testDb); - createVisitedCountry(testDb, user.id, 'ES'); + const { user } = await makeUser(orm); + await markCountryVisited(orm, user.id, 'ES'); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'unmark_country_visited', arguments: { country_code: 'ES' } }); expect(parseToolResult(result)).toEqual({ @@ -122,7 +118,7 @@ describe('Tool: unmark_country_visited', () => { }); it('succeeds even when country 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_country_visited', arguments: { country_code: 'AU' } }); const data = parseToolResult(result) as any; @@ -132,8 +128,8 @@ describe('Tool: unmark_country_visited', () => { it('blocks demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@nomad.app' }); - createVisitedCountry(testDb, user.id, 'IT'); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); + await markCountryVisited(orm, user.id, 'IT'); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'unmark_country_visited', arguments: { country_code: 'IT' } }); expect(result.isError).toBe(true); @@ -147,7 +143,7 @@ describe('Tool: unmark_country_visited', () => { describe('Tool: create_bucket_list_item', () => { it('stores a wished-for region and refuses one outside its country (#1901)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const ok = parseToolResult( await h.client.callTool({ @@ -165,7 +161,7 @@ describe('Tool: create_bucket_list_item', () => { }); it('creates a bucket list item with all fields', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_bucket_list_item', @@ -179,7 +175,7 @@ describe('Tool: create_bucket_list_item', () => { }); it('creates a minimal item (name only)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_bucket_list_item', arguments: { name: 'Antarctica' } }); const data = parseToolResult(result) as any; @@ -189,7 +185,7 @@ describe('Tool: create_bucket_list_item', () => { }); it('takes a target date, so the same place can be planned for several dates (#1898)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const first = await h.client.callTool({ name: 'create_bucket_list_item', @@ -206,8 +202,8 @@ describe('Tool: create_bucket_list_item', () => { }); it('reports the duplicate instead of appending a second row (#1898)', async () => { - const { user } = createUser(testDb); - createBucketListItem(testDb, user.id, { name: 'Japan', country_code: 'JP' }); + const { user } = await makeUser(orm); + await makeBucketListItem(orm, user.id, { name: 'Japan', country_code: 'JP' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_bucket_list_item', @@ -220,7 +216,7 @@ describe('Tool: create_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' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_bucket_list_item', arguments: { name: 'Nowhere' } }); expect(result.isError).toBe(true); @@ -234,8 +230,8 @@ describe('Tool: create_bucket_list_item', () => { describe('Tool: delete_bucket_list_item', () => { it('deletes a bucket list item owned by the user', async () => { - const { user } = createUser(testDb); - const item = createBucketListItem(testDb, user.id, { name: 'Machu Picchu' }); + const { user } = await makeUser(orm); + const item = await makeBucketListItem(orm, user.id, { name: 'Machu Picchu' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'delete_bucket_list_item', arguments: { itemId: item.id } }); const data = parseToolResult(result) as any; @@ -245,9 +241,9 @@ describe('Tool: delete_bucket_list_item', () => { }); it('returns error for item not found (wrong user)', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const item = createBucketListItem(testDb, other.id, { name: "Other's Wish" }); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const item = await makeBucketListItem(orm, other.id, { name: "Other's Wish" }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'delete_bucket_list_item', arguments: { itemId: item.id } }); expect(result.isError).toBe(true); @@ -255,7 +251,7 @@ describe('Tool: delete_bucket_list_item', () => { }); it('returns error 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: 'delete_bucket_list_item', arguments: { itemId: 99999 } }); expect(result.isError).toBe(true); @@ -264,8 +260,8 @@ describe('Tool: delete_bucket_list_item', () => { it('blocks demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@nomad.app' }); - const item = createBucketListItem(testDb, user.id); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); + const item = await makeBucketListItem(orm, user.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'delete_bucket_list_item', arguments: { itemId: item.id } }); expect(result.isError).toBe(true); @@ -279,10 +275,10 @@ describe('Tool: delete_bucket_list_item', () => { describe('Tool: list_bucket_list', () => { it("MCP-ATLAS-BUCKET-001: answers the user's own wishes with their ids, shaped like GET /bucket-list", async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const kyoto = createBucketListItem(testDb, user.id, { name: 'Kyoto', country_code: 'JP', notes: 'In autumn' }); - createBucketListItem(testDb, other.id, { name: "Other's Wish" }); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const kyoto = await makeBucketListItem(orm, user.id, { name: 'Kyoto', country_code: 'JP', notes: 'In autumn' }); + await makeBucketListItem(orm, other.id, { name: "Other's Wish" }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_bucket_list', arguments: {} }); const data = parseToolResult(result) as { items: Array> }; @@ -293,7 +289,7 @@ describe('Tool: list_bucket_list', () => { it('MCP-ATLAS-BUCKET-002: answers an empty list, and reads for a demo user too', 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: 'list_bucket_list', arguments: {} }); expect(result.isError).toBeFalsy();