diff --git a/server/scripts/test-mocks-baseline.json b/server/scripts/test-mocks-baseline.json index a9b8bb743..965163146 100644 --- a/server/scripts/test-mocks-baseline.json +++ b/server/scripts/test-mocks-baseline.json @@ -63,11 +63,7 @@ "tests/setup.ts": 1, "tests/unit/auto-backup.test.ts": 1, "tests/unit/db/database-lazy-open.test.ts": 1, - "tests/unit/mcp/tools-days-accommodations.test.ts": 1, - "tests/unit/mcp/tools-days.test.ts": 1, "tests/unit/mcp/tools-feeds-invite.test.ts": 1, - "tests/unit/mcp/tools-files.test.ts": 1, - "tests/unit/mcp/tools-help-addons.test.ts": 1, "tests/unit/mcp/tools-journey.test.ts": 1, "tests/unit/mcp/tools-leg-transport-mode.test.ts": 1, "tests/unit/mcp/tools-memories.test.ts": 1, diff --git a/server/tests/postgres/snapshot-suites-baseline.json b/server/tests/postgres/snapshot-suites-baseline.json index 714d9bb36..c0c5cb871 100644 --- a/server/tests/postgres/snapshot-suites-baseline.json +++ b/server/tests/postgres/snapshot-suites-baseline.json @@ -1,9 +1,5 @@ [ - "tests/unit/mcp/tools-days-accommodations.test.ts", - "tests/unit/mcp/tools-days.test.ts", "tests/unit/mcp/tools-feeds-invite.test.ts", - "tests/unit/mcp/tools-files.test.ts", - "tests/unit/mcp/tools-help-addons.test.ts", "tests/unit/mcp/tools-journey.test.ts", "tests/unit/mcp/tools-leg-transport-mode.test.ts", "tests/unit/mcp/tools-memories.test.ts", diff --git a/server/tests/unit/mcp/tools-days-accommodations.test.ts b/server/tests/unit/mcp/tools-days-accommodations.test.ts index 7e0a850db..db2777232 100644 --- a/server/tests/unit/mcp/tools-days-accommodations.test.ts +++ b/server/tests/unit/mcp/tools-days-accommodations.test.ts @@ -6,7 +6,6 @@ * resource (moved from resources.test.ts when the legacy registrar was * ported to the DI-discovered DaysMcp). */ -import { db as testDb } from '../../../src/db/database'; import { AppSettings } from '../../../src/db/entities/AppSettings.entity'; import { DayAccommodations } from '../../../src/db/entities/DayAccommodations.entity'; import { DayAssignments } from '../../../src/db/entities/DayAssignments.entity'; @@ -17,36 +16,28 @@ import { Trips } from '../../../src/db/entities/Trips.entity'; import { legacyBoundIntegerText } from '../../../src/nest/common/row-id'; import { DayRemovalService } from '../../../src/nest/days/day-removal.service'; import { invalidatePermissionsCache } from '../../../src/nest/permissions/permissions-cache'; -import { - createUser, - createTrip, - createDay, - createPlace, - createDayAccommodation, - addTripMember, -} from '../../helpers/factories'; +import { makeDayAccommodation } from '../../helpers/factories/itinerary'; +import { makePlace } from '../../helpers/factories/places'; import { makeReservation } from '../../helpers/factories/reservations'; import { countRows, deleteRows, findRow, findRows } from '../../helpers/factories/rows'; import { setAppSetting } from '../../helpers/factories/settings'; +import { addTripMember, makeDay, 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 { createTestOrm, type TestOrm } from '../../helpers/test-orm'; +import { resetDatabase } from '../../helpers/reset-database'; +import { bootTestApp } from '../../helpers/test-app'; +import { MikroORM } from '@mikro-orm/core'; import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; -vi.mock('../../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - const realtime = new FakeRealtimeService(); const broadcastMock = realtime.broadcastMock; -let orm: TestOrm; +let orm: MikroORM; beforeAll(async () => { - orm = await createTestOrm(testDb); + orm = (await bootTestApp(realtime)).get(MikroORM); }); async function placeWebsite(id: number) { @@ -54,15 +45,14 @@ async function placeWebsite(id: number) { return row ? { website: row.website } : undefined; } -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) { @@ -80,8 +70,8 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise) describe('Tool: create_day', () => { it('creates a day with a date', 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_day', @@ -95,8 +85,8 @@ describe('Tool: create_day', () => { }); it('creates a dateless day', 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_day', @@ -112,9 +102,9 @@ describe('Tool: create_day', () => { // The validator returns a list and the tool joins it. Without a case that // produces more than zero errors, that join is never executed, and a tool // that silently created a stay against a foreign place would look fine. - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id, { date: '2025-06-15' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id, { date: '2025-06-15' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_accommodation', @@ -130,9 +120,9 @@ describe('Tool: create_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 { 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_day', arguments: { tripId: trip.id } }); expect(result.isError).toBe(true); @@ -141,8 +131,8 @@ describe('Tool: create_day', () => { 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_day', arguments: { tripId: trip.id } }); expect(result.isError).toBe(true); @@ -163,8 +153,8 @@ describe('Tool: delete_day', () => { })); it('deletes a day the way REST does: the gap closes, the dates stay on their slots, and every screen hears it', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2026-01-01', end_date: '2026-01-03' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2026-01-01', end_date: '2026-01-03' }); const [d1, d2, d3] = await dayRows(trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -196,12 +186,12 @@ describe('Tool: delete_day', () => { }); it('cancels a stay checking in on the day with its booking, and says so', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const d1 = createDay(testDb, trip.id); - const d2 = createDay(testDb, trip.id); - const place = createPlace(testDb, trip.id, { name: 'Harbour Hotel' }); - const stay = createDayAccommodation(testDb, trip.id, place.id, d2.id, d2.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const d1 = await makeDay(orm, trip.id); + const d2 = await makeDay(orm, trip.id); + const place = await makePlace(orm, trip.id, { name: 'Harbour Hotel' }); + const stay = await makeDayAccommodation(orm, trip.id, place.id, d2.id, d2.id); const { id: reservationId } = await makeReservation(orm, trip.id, { day: d2.id, title: 'Harbour Hotel', @@ -228,9 +218,9 @@ describe('Tool: delete_day', () => { }); it('refuses the last day of a trip with the REST sentence and leaves it in place', 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: 'delete_day', arguments: { tripId: trip.id, dayId: day.id } }); expect(result.isError).toBe(true); @@ -241,10 +231,10 @@ describe('Tool: delete_day', () => { }); it('lets an unexpected failure surface as one, not as the last-day refusal', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + await makeDay(orm, trip.id); const boom = vi.spyOn(DayRemovalService.prototype, 'remove').mockImplementation(() => { throw new Error('disk full'); }); @@ -261,9 +251,9 @@ describe('Tool: delete_day', () => { }); it('reports a day that is not on the trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'delete_day', arguments: { tripId: trip.id, dayId: 999999 } }); expect(result.isError).toBe(true); @@ -272,10 +262,10 @@ describe('Tool: delete_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 { 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: 'delete_day', arguments: { tripId: trip.id, dayId: day.id } }); expect(result.isError).toBe(true); @@ -283,12 +273,12 @@ describe('Tool: delete_day', () => { }); it('refuses a member without day_edit, the same right the REST route asks for', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); - const day = createDay(testDb, trip.id); - createDay(testDb, trip.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); + const day = await makeDay(orm, trip.id); + await makeDay(orm, trip.id); await setAppSetting(orm, 'perm_day_edit', 'trip_owner'); await invalidatePermissionsCache(); try { @@ -305,10 +295,10 @@ describe('Tool: delete_day', () => { 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); - 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 makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'delete_day', arguments: { tripId: trip.id, dayId: day.id } }); expect(result.isError).toBe(true); @@ -323,11 +313,11 @@ describe('Tool: delete_day', () => { describe('Tool: create_accommodation', () => { it('creates an accommodation and broadcasts', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id, { name: 'Hotel du Louvre' }); - const day1 = createDay(testDb, trip.id, { date: '2025-06-15' }); - const day2 = createDay(testDb, trip.id, { date: '2025-06-17' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id, { name: 'Hotel du Louvre' }); + const day1 = await makeDay(orm, trip.id, { date: '2025-06-15' }); + const day2 = await makeDay(orm, trip.id, { date: '2025-06-17' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_accommodation', @@ -350,11 +340,11 @@ describe('Tool: create_accommodation', () => { }); it('MCP-ACCOM-EVT-01: announces the partner hotel reservation, as the REST route does', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id); - const day1 = createDay(testDb, trip.id); - const day2 = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id); + const day1 = await makeDay(orm, trip.id); + const day2 = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'create_accommodation', @@ -367,11 +357,11 @@ describe('Tool: create_accommodation', () => { }); it('MCP-ACCOM-EVT-02: a place from another trip is refused with every miss listed, nothing written', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const otherTrip = createTrip(testDb, user.id); - const foreign = createPlace(testDb, otherTrip.id); - const day = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const otherTrip = await makeTrip(orm, user.id); + const foreign = await makePlace(orm, otherTrip.id); + const day = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_accommodation', @@ -384,11 +374,11 @@ describe('Tool: create_accommodation', () => { }); it('returns access denied for non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const place = createPlace(testDb, trip.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 place = await makePlace(orm, trip.id); + const day = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_accommodation', @@ -401,10 +391,10 @@ describe('Tool: create_accommodation', () => { 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 place = createPlace(testDb, trip.id); - const day = createDay(testDb, trip.id); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id); + const day = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_accommodation', @@ -421,12 +411,12 @@ describe('Tool: create_accommodation', () => { describe('Tool: update_accommodation', () => { it('updates accommodation fields and broadcasts', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id); - const day1 = createDay(testDb, trip.id); - const day2 = createDay(testDb, trip.id); - const acc = createDayAccommodation(testDb, trip.id, place.id, day1.id, day2.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id); + const day1 = await makeDay(orm, trip.id); + const day2 = await makeDay(orm, trip.id); + const acc = await makeDayAccommodation(orm, trip.id, place.id, day1.id, day2.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_accommodation', @@ -439,8 +429,8 @@ describe('Tool: update_accommodation', () => { }); it('returns error for non-existent accommodation', 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_accommodation', @@ -453,13 +443,13 @@ describe('Tool: update_accommodation', () => { it('MCP-ACCOM-EVT-03: moving a stay onto a place of another trip is refused and changes nothing', async () => { // The tool used to write whatever place id it was given; REST has always 404ed it. - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id); - const foreign = createPlace(testDb, createTrip(testDb, user.id).id); - const day1 = createDay(testDb, trip.id); - const day2 = createDay(testDb, trip.id); - const acc = createDayAccommodation(testDb, trip.id, place.id, day1.id, day2.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id); + const foreign = await makePlace(orm, (await makeTrip(orm, user.id)).id); + const day1 = await makeDay(orm, trip.id); + const day2 = await makeDay(orm, trip.id); + const acc = await makeDayAccommodation(orm, trip.id, place.id, day1.id, day2.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_accommodation', @@ -472,9 +462,9 @@ describe('Tool: update_accommodation', () => { }); 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: 'update_accommodation', @@ -491,12 +481,12 @@ describe('Tool: update_accommodation', () => { describe('Tool: delete_accommodation', () => { it('deletes accommodation and broadcasts', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id); - const day1 = createDay(testDb, trip.id); - const day2 = createDay(testDb, trip.id); - const acc = createDayAccommodation(testDb, trip.id, place.id, day1.id, day2.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id); + const day1 = await makeDay(orm, trip.id); + const day2 = await makeDay(orm, trip.id); + const acc = await makeDayAccommodation(orm, trip.id, place.id, day1.id, day2.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'delete_accommodation', @@ -514,11 +504,11 @@ describe('Tool: delete_accommodation', () => { }); it('MCP-ACCOM-EVT-04: cancelling a booked stay announces the reservation it took along, then the stay', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id); - const day1 = createDay(testDb, trip.id); - const day2 = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id); + const day1 = await makeDay(orm, trip.id); + const day2 = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const created = parseToolResult( await h.client.callTool({ @@ -548,9 +538,9 @@ describe('Tool: delete_accommodation', () => { }); 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_accommodation', @@ -569,9 +559,9 @@ describe('Tool: create_place_accommodation', () => { // #2483: the place it creates takes its website through the same contract as // create_place, so a bare host from search_place lands as https. it('MCP-ACCOM-2483-01: a website without a scheme is stored with https', 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 data = parseToolResult( await h.client.callTool({ @@ -593,9 +583,9 @@ describe('Tool: create_place_accommodation', () => { // went through before fails now; a value the place contract refuses is left // off rather than stored, and '' stays "no website" as it is on REST. it('MCP-ACCOM-2483-02: a website that is empty, free text or a script link books the stay without one', 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) => { for (const [name, website] of [ ['Empty', ''], @@ -619,9 +609,9 @@ describe('Tool: create_place_accommodation', () => { }); it('creates the place and the accommodation atomically and broadcasts both', 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: 'create_place_accommodation', @@ -661,8 +651,8 @@ describe('Tool: create_place_accommodation', () => { }); it('reports the day-validation errors without creating anything', 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_place_accommodation', @@ -678,9 +668,9 @@ describe('Tool: create_place_accommodation', () => { // all. A category that is not there is the cheapest way to make the first insert // fail on a foreign key; what matters is that the tool answers with an error // instead of a half-written trip. - 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: 'create_place_accommodation', @@ -708,12 +698,12 @@ describe('Tool: create_place_accommodation', () => { describe('Resource: trek://trips/{tripId}/accommodations', () => { it('returns accommodations for a trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day1 = createDay(testDb, trip.id, { day_number: 1 }); - const day2 = createDay(testDb, trip.id, { day_number: 2 }); - const place = createPlace(testDb, trip.id, { name: 'Grand Hotel' }); - createDayAccommodation(testDb, trip.id, place.id, day1.id, day2.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day1 = await makeDay(orm, trip.id, { day_number: 1 }); + const day2 = await makeDay(orm, trip.id, { day_number: 2 }); + const place = await makePlace(orm, trip.id, { name: 'Grand Hotel' }); + await makeDayAccommodation(orm, trip.id, place.id, day1.id, day2.id); await withHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: `trek://trips/${trip.id}/accommodations` }); @@ -724,9 +714,9 @@ describe('Resource: trek://trips/{tripId}/accommodations', () => { }); it('returns access denied for unauthorized trip', 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.readResource({ uri: `trek://trips/${trip.id}/accommodations` }); diff --git a/server/tests/unit/mcp/tools-days.test.ts b/server/tests/unit/mcp/tools-days.test.ts index a88a106cb..81d24f1ab 100644 --- a/server/tests/unit/mcp/tools-days.test.ts +++ b/server/tests/unit/mcp/tools-days.test.ts @@ -4,51 +4,40 @@ * resource (moved from resources.test.ts when the legacy registrar was ported). * create_day's plain append is covered in tools-days-accommodations.test.ts. */ -import { db as testDb } from '../../../src/db/database'; import { DayAssignments } from '../../../src/db/entities/DayAssignments.entity'; import { Days } from '../../../src/db/entities/Days.entity'; import { Trips } from '../../../src/db/entities/Trips.entity'; import { DaysService } from '../../../src/nest/days/days.service'; -import { - createUser, - createTrip, - createDay, - createPlace, - createDayAssignment, - createDayAccommodation, -} from '../../helpers/factories'; +import { makeDayAccommodation, makeDayAssignment } from '../../helpers/factories/itinerary'; +import { makePlace } from '../../helpers/factories/places'; import { findRow, updateRows } from '../../helpers/factories/rows'; -import { readTripDays } from '../../helpers/factories/trips'; +import { makeDay, makeTrip, readTripDays } 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 { createTestOrm, type TestOrm } from '../../helpers/test-orm'; +import { resetDatabase } from '../../helpers/reset-database'; +import { bootTestApp } from '../../helpers/test-app'; +import { MikroORM } from '@mikro-orm/core'; import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; -vi.mock('../../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - const realtime = new FakeRealtimeService(); const broadcastMock = realtime.broadcastMock; -beforeEach(() => { - resetTestDb(testDb); +beforeEach(async () => { + await resetDatabase(orm); broadcastMock.mockClear(); delete process.env.DEMO_MODE; }); -let orm: TestOrm; +let orm: MikroORM; beforeAll(async () => { - orm = await createTestOrm(testDb); + orm = (await bootTestApp(realtime)).get(MikroORM); }); afterAll(async () => { - await orm.close(); - testDb.close(); + await (await bootTestApp(realtime)).close(); }); async function withHarness(userId: number, fn: (h: McpHarness) => Promise) { @@ -82,9 +71,9 @@ async function dayDatesInOrder(tripId: number): Promise<(string | null)[]> { describe('Tool: update_day', () => { it('sets a day title', 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({ @@ -97,9 +86,9 @@ describe('Tool: update_day', () => { }); it('clears a day title with null', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id, { title: 'Old Title' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id, { title: 'Old Title' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -112,9 +101,9 @@ describe('Tool: update_day', () => { }); it('setting a title preserves the day notes (post-port defect fix)', 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 updateRows(orm, Days, { id: day.id }, { notes: 'Walking day' }); await withHarness(user.id, async (h) => { @@ -128,9 +117,9 @@ describe('Tool: update_day', () => { }); it('writes notes without being sent a title, and keeps the title', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id, { title: 'Arrival in Paris' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id, { title: 'Arrival in Paris' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -145,9 +134,9 @@ describe('Tool: update_day', () => { }); it('sets a title and notes in one call', 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) => { await h.client.callTool({ @@ -160,9 +149,9 @@ describe('Tool: update_day', () => { }); it('clears the notes with an empty string', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id, { title: 'Arrival' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id, { title: 'Arrival' }); await updateRows(orm, Days, { id: day.id }, { notes: 'Ferry leaves at 08:00' }); await withHarness(user.id, async (h) => { @@ -176,9 +165,9 @@ describe('Tool: update_day', () => { }); it('refuses a non-string notes value', 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({ @@ -193,9 +182,9 @@ describe('Tool: update_day', () => { }); it('broadcasts day:updated event', 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) => { await h.client.callTool({ name: 'update_day', arguments: { tripId: trip.id, dayId: day.id, title: 'Day 1' } }); expect(broadcastMock).toHaveBeenCalledWith(trip.id, 'day:updated', expect.any(Object)); @@ -203,10 +192,10 @@ describe('Tool: update_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 { 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); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -218,10 +207,10 @@ describe('Tool: update_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 { 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: 'update_day', @@ -233,9 +222,9 @@ describe('Tool: update_day', () => { 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: 'update_day', @@ -252,11 +241,11 @@ describe('Tool: update_day', () => { describe('Tool: create_day (position)', () => { it('inserts an empty day into the middle of a dateless trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const d1 = createDay(testDb, trip.id); - const d2 = createDay(testDb, trip.id); - const d3 = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const d1 = await makeDay(orm, trip.id); + const d2 = await makeDay(orm, trip.id); + const d3 = await makeDay(orm, trip.id); let insertedId = 0; await withHarness(user.id, async (h) => { @@ -272,8 +261,8 @@ describe('Tool: create_day (position)', () => { }); it('re-pins the dates and extends the trip when inserting into a dated trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-03' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2025-06-01', end_date: '2025-06-03' }); const [d1, d2, d3] = await dayIdsInOrder(trip.id); let insertedId = 0; @@ -293,9 +282,9 @@ describe('Tool: create_day (position)', () => { }); it('appends when position is omitted and still honours date and notes', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const d1 = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const d1 = await makeDay(orm, trip.id); let appendedId = 0; await withHarness(user.id, async (h) => { @@ -311,9 +300,9 @@ describe('Tool: create_day (position)', () => { }); it('broadcasts day:reordered for an insert and day:created for an append', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'create_day', arguments: { tripId: trip.id, position: 1 } }); @@ -327,9 +316,9 @@ describe('Tool: create_day (position)', () => { }); it('refuses a position below 1', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -344,13 +333,13 @@ describe('Tool: create_day (position)', () => { }); it('refuses an insert that would invert a stay, leaving the trip untouched', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-03' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2025-06-01', end_date: '2025-06-03' }); const [d1, d2, d3] = await dayIdsInOrder(trip.id); - const place = createPlace(testDb, trip.id); + const place = await makePlace(orm, trip.id); // The stay ends on the last day, so pushing a day in front of its start // cannot invert it; anchoring it the other way round is what does. - createDayAccommodation(testDb, trip.id, place.id, d3, d1); + await makeDayAccommodation(orm, trip.id, place.id, d3, d1); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -367,9 +356,9 @@ describe('Tool: create_day (position)', () => { it('blocks demo user on an insert', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@nomad.app' }); - const trip = createTrip(testDb, user.id); - createDay(testDb, trip.id); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); + const trip = await makeTrip(orm, user.id); + await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_day', arguments: { tripId: trip.id, position: 1 } }); expect(result.isError).toBe(true); @@ -386,10 +375,10 @@ describe('Tool: create_day (dated)', () => { const endDate = async (tripId: number) => (await findRow(orm, Trips, { id: tripId }))?.end_date; it('appends the next date behind the dated days, extends the trip and answers with both', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); const [d1, d2] = await dayIdsInOrder(trip.id); - const spare = createDay(testDb, trip.id); + const spare = await makeDay(orm, trip.id); let data = {} as { day: { id: number; date: string; notes: string | null }; @@ -423,8 +412,8 @@ describe('Tool: create_day (dated)', () => { }); it('refuses dated next to a position or a date, the way the REST contract does', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); await withHarness(user.id, async (h) => { for (const extra of [{ position: 1 }, { date: '2025-07-01' }]) { @@ -445,9 +434,9 @@ describe('Tool: create_day (dated)', () => { }); it('refuses a trip without dates and writes nothing', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_day', arguments: { tripId: trip.id, dated: true } }); @@ -460,8 +449,8 @@ describe('Tool: create_day (dated)', () => { }); it('lets an unexpected failure surface as one, not as a refusal of the trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); const boom = vi.spyOn(DaysService.prototype, 'appendDated').mockImplementation(() => { throw new Error('disk full'); }); @@ -479,8 +468,8 @@ describe('Tool: create_day (dated)', () => { it('blocks a demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@nomad.app' }); - const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); + const trip = await makeTrip(orm, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_day', arguments: { tripId: trip.id, dated: true } }); @@ -492,8 +481,8 @@ describe('Tool: create_day (dated)', () => { }); it('an insert at a position announces the grown trip as well', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2025-06-01', end_date: '2025-06-02' }); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'create_day', arguments: { tripId: trip.id, position: 1 } }); @@ -513,13 +502,13 @@ describe('Tool: create_day (dated)', () => { describe('Tool: reorder_days', () => { it('renumbers the days and carries each day content along', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const d1 = createDay(testDb, trip.id, { title: 'First' }); - const d2 = createDay(testDb, trip.id, { title: 'Second' }); - const d3 = createDay(testDb, trip.id, { title: 'Third' }); - const place = createPlace(testDb, trip.id); - const assignment = createDayAssignment(testDb, d3.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const d1 = await makeDay(orm, trip.id, { title: 'First' }); + const d2 = await makeDay(orm, trip.id, { title: 'Second' }); + const d3 = await makeDay(orm, trip.id, { title: 'Third' }); + const place = await makePlace(orm, trip.id); + const assignment = await makeDayAssignment(orm, d3.id, place.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -535,8 +524,8 @@ describe('Tool: reorder_days', () => { }); it('keeps the dates pinned to their slots so the content moves across them', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-03' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2025-06-01', end_date: '2025-06-03' }); const [d1, d2, d3] = await dayIdsInOrder(trip.id); await withHarness(user.id, async (h) => { @@ -552,10 +541,10 @@ describe('Tool: reorder_days', () => { }); it('broadcasts day:reordered with the ordered ids', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const d1 = createDay(testDb, trip.id); - const d2 = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const d1 = await makeDay(orm, trip.id); + const d2 = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'reorder_days', arguments: { tripId: trip.id, orderedIds: [d2.id, d1.id] } }); @@ -569,11 +558,11 @@ describe('Tool: reorder_days', () => { }); it('refuses a list that is not a permutation of the trip days', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const d1 = createDay(testDb, trip.id); - const d2 = createDay(testDb, trip.id); - const d3 = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const d1 = await makeDay(orm, trip.id); + const d2 = await makeDay(orm, trip.id); + const d3 = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -589,12 +578,12 @@ describe('Tool: reorder_days', () => { }); it('refuses a day id from another trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const other = createTrip(testDb, user.id); - const d1 = createDay(testDb, trip.id); - const d2 = createDay(testDb, trip.id); - const foreign = createDay(testDb, other.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const other = await makeTrip(orm, user.id); + const d1 = await makeDay(orm, trip.id); + const d2 = await makeDay(orm, trip.id); + const foreign = await makeDay(orm, other.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -609,11 +598,11 @@ describe('Tool: reorder_days', () => { }); it('refuses a move that would make a stay end before it starts, and rolls back', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { start_date: '2025-06-01', end_date: '2025-06-03' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { start_date: '2025-06-01', end_date: '2025-06-03' }); const [d1, d2, d3] = await dayIdsInOrder(trip.id); - const place = createPlace(testDb, trip.id); - createDayAccommodation(testDb, trip.id, place.id, d1, d2); + const place = await makePlace(orm, trip.id); + await makeDayAccommodation(orm, trip.id, place.id, d1, d2); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -629,9 +618,9 @@ describe('Tool: reorder_days', () => { }); it('refuses an empty list', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -644,11 +633,11 @@ describe('Tool: reorder_days', () => { }); it('returns access denied for non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const d1 = createDay(testDb, trip.id); - const d2 = createDay(testDb, trip.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.id); + const d1 = await makeDay(orm, trip.id); + const d2 = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -663,10 +652,10 @@ describe('Tool: reorder_days', () => { 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 d1 = createDay(testDb, trip.id); - const d2 = createDay(testDb, trip.id); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); + const trip = await makeTrip(orm, user.id); + const d1 = await makeDay(orm, trip.id); + const d2 = await makeDay(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -686,12 +675,12 @@ describe('Tool: reorder_days', () => { describe('Resource: trek://trips/{tripId}/days', () => { it('returns days with assignments in order', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day1 = createDay(testDb, trip.id, { day_number: 1 }); - const day2 = createDay(testDb, trip.id, { day_number: 2 }); - const place = createPlace(testDb, trip.id); - 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, { day_number: 1 }); + const day2 = await makeDay(orm, trip.id, { day_number: 2 }); + const place = await makePlace(orm, trip.id); + await makeDayAssignment(orm, day1.id, place.id); void day2; await withHarness(user.id, async (h) => { @@ -706,9 +695,9 @@ describe('Resource: trek://trips/{tripId}/days', () => { }); it('returns access denied for unauthorized trip', 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.readResource({ uri: `trek://trips/${trip.id}/days` }); diff --git a/server/tests/unit/mcp/tools-files.test.ts b/server/tests/unit/mcp/tools-files.test.ts index 24bd25a68..7733cfab6 100644 --- a/server/tests/unit/mcp/tools-files.test.ts +++ b/server/tests/unit/mcp/tools-files.test.ts @@ -8,7 +8,6 @@ * read_trip_file has to stay behind files:content while list_trip_files answers * on files:read alone. */ -import { db as testDb } from '../../../src/db/database'; import { AppSettings } from '../../../src/db/entities/AppSettings.entity'; import { FileLinks } from '../../../src/db/entities/FileLinks.entity'; import { TripFiles } from '../../../src/db/entities/TripFiles.entity'; @@ -17,33 +16,25 @@ import { FilesModule } from '../../../src/nest/files/files.module'; import { FILE_CONTENT_MAX } from '../../../src/nest/files/files.service'; import { invalidatePermissionsCache } from '../../../src/nest/permissions/permissions-cache'; import { StorageService } from '../../../src/nest/storage/storage.service'; -import { - createUser, - createTrip, - createDay, - createPlace, - createDayAssignment, - createReservation, - addTripMember, -} from '../../helpers/factories'; import { linkFile, makeTripFile } from '../../helpers/factories/files'; +import { makeDayAssignment } from '../../helpers/factories/itinerary'; +import { makePlace } from '../../helpers/factories/places'; +import { makeReservation } from '../../helpers/factories/reservations'; import { countRows, findRow, findRows, updateRows } from '../../helpers/factories/rows'; import { setAppSetting } from '../../helpers/factories/settings'; +import { addTripMember, 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 { expectRegisteredProvider } from '../../helpers/module-providers'; -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 fs from 'node:fs'; import nodePath from 'node:path'; 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 { fixture } = vi.hoisted(() => ({ fixture: {} as { root: string } })); // The booted container's StorageService is a makeStorageFixture('') over its own // mkdtemp root (tests/helpers/test-app.ts). Pin one shared fixture and remember @@ -59,23 +50,22 @@ const realtime = new FakeRealtimeService(); const broadcastMock = realtime.broadcastMock; beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); broadcastMock.mockClear(); delete process.env.DEMO_MODE; - // resetTestDb truncates app_settings, but the permission cache is module-scoped + // resetDatabase truncates app_settings, but the permission cache is module-scoped // and would keep serving whatever the previous case configured. await invalidatePermissionsCache(); }); -let orm: TestOrm; +let orm: MikroORM; beforeAll(async () => { - orm = await createTestOrm(testDb); + orm = (await bootTestApp(realtime)).get(MikroORM); }); afterAll(async () => { - await orm.close(); - testDb.close(); + await (await bootTestApp(realtime)).close(); }); async function withHarness(userId: number, fn: (h: McpHarness) => Promise) { @@ -145,10 +135,10 @@ function linkRows(fileId: number) { describe('Tool: list_trip_files', () => { it('reports name, size, uploader, links and starred state', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id); - const reservation = createReservation(testDb, trip.id, { title: 'Hotel Ritz' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id); + const reservation = await makeReservation(orm, trip.id, { title: 'Hotel Ritz' }); const file = await insertFile(trip.id, { original_name: 'boarding-pass.pdf', file_size: 4242, @@ -178,8 +168,8 @@ describe('Tool: list_trip_files', () => { }); it('lists the trash only when asked for it', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await insertFile(trip.id, { original_name: 'live.pdf' }); await insertFile(trip.id, { original_name: 'trashed.pdf', deleted_at: '2027-01-01 10:00:00' }); @@ -197,10 +187,10 @@ describe('Tool: list_trip_files', () => { }); it('reports the bookings and places a file is linked to', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id); - const reservation = createReservation(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id); + const reservation = await makeReservation(orm, trip.id); const file = await insertFile(trip.id); await linkFile(orm, file.id, { reservation: reservation.id }); await linkFile(orm, file.id, { place: place.id }); @@ -215,9 +205,9 @@ describe('Tool: list_trip_files', () => { }); it('denies a 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 insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -233,8 +223,8 @@ describe('Tool: list_trip_files', () => { describe('Tool: read_trip_file', () => { it('returns a text document as text', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const file = await insertFile(trip.id, { filename: 'notes.txt', original_name: 'packing-notes.txt', @@ -262,8 +252,8 @@ describe('Tool: read_trip_file', () => { }); it('returns a binary document base64-encoded', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const bytes = Buffer.from([0x25, 0x50, 0x44, 0x46, 0x00, 0xff]); const file = await insertFile(trip.id, { filename: 'stored.pdf', @@ -285,8 +275,8 @@ describe('Tool: read_trip_file', () => { }); it('falls back to a binary mimetype when the row has none', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const file = await insertFile(trip.id, { filename: 'unknown.bin', mime_type: null, file_size: null }); storeBytes('unknown.bin', 'hi'); @@ -303,9 +293,9 @@ describe('Tool: read_trip_file', () => { }); it('refuses a file that belongs to another trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const otherTrip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const otherTrip = await makeTrip(orm, user.id); const foreign = await insertFile(otherTrip.id, { filename: 'foreign.txt', mime_type: 'text/plain' }); storeBytes('foreign.txt', 'secret'); @@ -320,8 +310,8 @@ describe('Tool: read_trip_file', () => { }); it('refuses a trashed file', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const file = await insertFile(trip.id, { filename: 'trashed.txt', mime_type: 'text/plain', @@ -340,8 +330,8 @@ describe('Tool: read_trip_file', () => { }); it('refuses a file over the 10 MB cap without touching the bytes', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); // Recorded size only: the refusal happens before the storage layer is asked, // which is the whole point of capping on the row. const file = await insertFile(trip.id, { @@ -361,8 +351,8 @@ describe('Tool: read_trip_file', () => { }); it('reports a row whose bytes are gone as unavailable, not as a crash', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const file = await insertFile(trip.id, { filename: 'never-stored.pdf' }); await withHarness(user.id, async (h) => { @@ -376,8 +366,8 @@ describe('Tool: read_trip_file', () => { }); it('lets an unexpected storage failure surface instead of calling the file missing', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); const file = await insertFile(trip.id, { filename: 'flaky.txt', mime_type: 'text/plain' }); storeBytes('flaky.txt', 'hi'); // A backend outage is not one of the three refusals: reporting it as "not @@ -401,9 +391,9 @@ describe('Tool: read_trip_file', () => { }); it('denies a 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); const file = await insertFile(trip.id, { filename: 'members-only.txt', mime_type: 'text/plain' }); storeBytes('members-only.txt', 'secret'); @@ -424,9 +414,9 @@ describe('Tool: read_trip_file', () => { describe('Tool: update_trip_file', () => { it('sets the description and attaches the file to a place', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -443,9 +433,9 @@ describe('Tool: update_trip_file', () => { }); it('leaves out fields the caller did not send', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id); const file = await insertFile(trip.id, { description: 'Keep me' }); await withHarness(user.id, async (h) => { @@ -458,9 +448,9 @@ describe('Tool: update_trip_file', () => { }); it('detaches a booking when reservation_id is null', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id); const file = await insertFile(trip.id, { reservation_id: reservation.id }); await withHarness(user.id, async (h) => { @@ -473,10 +463,10 @@ describe('Tool: update_trip_file', () => { }); it('refuses a place that belongs to another trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const otherTrip = createTrip(testDb, user.id); - const foreignPlace = createPlace(testDb, otherTrip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const otherTrip = await makeTrip(orm, user.id); + const foreignPlace = await makePlace(orm, otherTrip.id); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -491,9 +481,9 @@ describe('Tool: update_trip_file', () => { }); it('refuses a file id from another trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const otherTrip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const otherTrip = await makeTrip(orm, user.id); const foreign = await insertFile(otherTrip.id); await withHarness(user.id, async (h) => { @@ -507,10 +497,10 @@ describe('Tool: update_trip_file', () => { }); it('refuses a member once file_edit is owner-only', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); + const { user: owner } = await makeUser(orm); + const { user: member } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + await addTripMember(orm, trip.id, member.id); const file = await insertFile(trip.id); await setPermission('file_edit', 'trip_owner'); @@ -525,9 +515,9 @@ describe('Tool: update_trip_file', () => { }); it('denies a 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); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -542,8 +532,8 @@ describe('Tool: update_trip_file', () => { it('blocks a demo user before it looks at the trip', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@trek.app' }); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm, { email: 'demo@trek.app' }); + const trip = await makeTrip(orm, user.id); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -565,9 +555,9 @@ describe('Tool: upload_trip_file', () => { const pdf = Buffer.from('%PDF-1.4 booking').toString('base64'); it('stores the bytes, records the row and attaches it to a booking', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const booking = createReservation(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const booking = await makeReservation(orm, trip.id); await withHarness(user.id, async (h) => { const data = parseToolResult( @@ -602,8 +592,8 @@ describe('Tool: upload_trip_file', () => { }); it('refuses an extension the file manager would refuse', 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) => { for (const filename of ['evil.html', 'logo.svg', 'tool.exe', 'noext']) { const result = await h.client.callTool({ @@ -618,8 +608,8 @@ describe('Tool: upload_trip_file', () => { }); it('follows the operator list, including the wildcard', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await setAppSetting(orm, 'allowed_file_types', 'pdf'); await withHarness(user.id, async (h) => { const refused = await h.client.callTool({ @@ -639,8 +629,8 @@ describe('Tool: upload_trip_file', () => { }); it('refuses content that is not base64, or decodes to nothing', 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 bad = await h.client.callTool({ name: 'upload_trip_file', @@ -656,8 +646,8 @@ describe('Tool: upload_trip_file', () => { }); it('refuses a file over the cap', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); // Just past the cap after decoding, still inside the schema's base64 length. const big = Buffer.alloc(FILE_CONTENT_MAX + 1, 1).toString('base64'); await withHarness(user.id, async (h) => { @@ -671,10 +661,10 @@ describe('Tool: upload_trip_file', () => { }); it('refuses a place from another trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const foreignTrip = createTrip(testDb, user.id); - const foreignPlace = createPlace(testDb, foreignTrip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const foreignTrip = await makeTrip(orm, user.id); + const foreignPlace = await makePlace(orm, foreignTrip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'upload_trip_file', @@ -687,10 +677,10 @@ describe('Tool: upload_trip_file', () => { }); it('refuses a member once file_upload is owner-only', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); + const { user: owner } = await makeUser(orm); + const { user: member } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + await addTripMember(orm, trip.id, member.id); await setPermission('file_upload', 'trip_owner'); await withHarness(member.id, async (h) => { const result = await h.client.callTool({ @@ -702,9 +692,9 @@ describe('Tool: upload_trip_file', () => { }); it('denies a non-member, and blocks a demo user', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const foreign = createTrip(testDb, other.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const foreign = await makeTrip(orm, other.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'upload_trip_file', @@ -713,8 +703,8 @@ describe('Tool: upload_trip_file', () => { expect(JSON.stringify(result.content)).toContain('Trip not found or access denied.'); }); process.env.DEMO_MODE = 'true'; - const { user: demo } = createUser(testDb, { email: 'demo@trek.app' }); - const own = createTrip(testDb, demo.id); + const { user: demo } = await makeUser(orm, { email: 'demo@trek.app' }); + const own = await makeTrip(orm, demo.id); await withHarness(demo.id, async (h) => { const result = await h.client.callTool({ name: 'upload_trip_file', @@ -732,12 +722,12 @@ describe('Tool: upload_trip_file', () => { describe('Tool: link_trip_file', () => { it('attaches one file to a booking, a place and a day assignment', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id); - const place = createPlace(testDb, trip.id); - const day = createDay(testDb, trip.id); - const assignment = createDayAssignment(testDb, day.id, place.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id); + const place = await makePlace(orm, trip.id); + const day = await makeDay(orm, trip.id); + const assignment = await makeDayAssignment(orm, day.id, place.id); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -763,8 +753,8 @@ describe('Tool: link_trip_file', () => { }); it('refuses a call with no target instead of storing an empty 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 file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -779,10 +769,10 @@ describe('Tool: link_trip_file', () => { }); it('refuses a booking from another trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const otherTrip = createTrip(testDb, user.id); - const foreignReservation = createReservation(testDb, otherTrip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const otherTrip = await makeTrip(orm, user.id); + const foreignReservation = await makeReservation(orm, otherTrip.id); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -797,12 +787,12 @@ describe('Tool: link_trip_file', () => { }); it('refuses a day assignment from 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 foreignPlace = createPlace(testDb, otherTrip.id); - const foreignAssignment = createDayAssignment(testDb, foreignDay.id, foreignPlace.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); + const foreignPlace = await makePlace(orm, otherTrip.id); + const foreignAssignment = await makeDayAssignment(orm, foreignDay.id, foreignPlace.id); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -816,11 +806,11 @@ describe('Tool: link_trip_file', () => { }); it('refuses a file id from another trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const otherTrip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const otherTrip = await makeTrip(orm, user.id); const foreign = await insertFile(otherTrip.id); - const reservation = createReservation(testDb, trip.id); + const reservation = await makeReservation(orm, trip.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -835,11 +825,11 @@ describe('Tool: link_trip_file', () => { }); it('refuses a member once file_edit is owner-only', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); - const reservation = createReservation(testDb, trip.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); + const reservation = await makeReservation(orm, trip.id); const file = await insertFile(trip.id); await setPermission('file_edit', 'trip_owner'); @@ -854,10 +844,10 @@ describe('Tool: link_trip_file', () => { }); it('denies a non-member', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const trip = createTrip(testDb, other.id); - const reservation = createReservation(testDb, trip.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.id); + const reservation = await makeReservation(orm, trip.id); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -872,9 +862,9 @@ describe('Tool: link_trip_file', () => { it('blocks a demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@trek.app' }); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id); + const { user } = await makeUser(orm, { email: 'demo@trek.app' }); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -894,10 +884,10 @@ describe('Tool: link_trip_file', () => { describe('Tool: unlink_trip_file', () => { it('removes one link and leaves the others alone', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id); - const place = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id); + const place = await makePlace(orm, trip.id); const file = await insertFile(trip.id); await linkFile(orm, file.id, { reservation: reservation.id }); await linkFile(orm, file.id, { place: place.id }); @@ -918,11 +908,11 @@ describe('Tool: unlink_trip_file', () => { }); it('refuses a file id from another trip, so a foreign link survives', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const otherTrip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const otherTrip = await makeTrip(orm, user.id); const foreignFile = await insertFile(otherTrip.id); - const foreignReservation = createReservation(testDb, otherTrip.id); + const foreignReservation = await makeReservation(orm, otherTrip.id); await linkFile(orm, foreignFile.id, { reservation: foreignReservation.id }); const linkId = (await linkRows(foreignFile.id))[0].id; @@ -938,11 +928,11 @@ describe('Tool: unlink_trip_file', () => { }); it('refuses a member once file_edit is owner-only', async () => { - const { user: owner } = createUser(testDb); - const { user: member } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, member.id); - const reservation = createReservation(testDb, trip.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); + const reservation = await makeReservation(orm, trip.id); const file = await insertFile(trip.id); await linkFile(orm, file.id, { reservation: reservation.id }); const linkId = (await linkRows(file.id))[0].id; @@ -959,9 +949,9 @@ describe('Tool: unlink_trip_file', () => { }); it('denies a 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); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -976,9 +966,9 @@ describe('Tool: unlink_trip_file', () => { it('blocks a demo user', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@nomad.app' }); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id); + const { user } = await makeUser(orm, { email: 'demo@nomad.app' }); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id); const file = await insertFile(trip.id); await linkFile(orm, file.id, { reservation: reservation.id }); const linkId = (await linkRows(file.id))[0].id; @@ -1006,10 +996,10 @@ describe('Link changes reach every tab', () => { } it('MCP-FILES-001: link_trip_file sends the file again with every link it now carries', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id); - const place = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id); + const place = await makePlace(orm, trip.id); const file = await insertFile(trip.id); await linkFile(orm, file.id, { reservation: reservation.id }); await withHarness(user.id, async (h) => { @@ -1026,10 +1016,10 @@ describe('Link changes reach every tab', () => { }); it('MCP-FILES-002: unlink_trip_file sends the file with the links it has left', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id); - const place = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id); + const place = await makePlace(orm, trip.id); const file = await insertFile(trip.id); await linkFile(orm, file.id, { reservation: reservation.id }); await linkFile(orm, file.id, { place: place.id }); @@ -1050,9 +1040,9 @@ describe('Link changes reach every tab', () => { describe('Tool: list_trip_file_links', () => { it('returns every link with its id and the booking title', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id, { title: 'Nightjet 421' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id, { title: 'Nightjet 421' }); const file = await insertFile(trip.id); await linkFile(orm, file.id, { reservation: reservation.id }); @@ -1074,9 +1064,9 @@ describe('Tool: list_trip_file_links', () => { }); it('refuses a file id from another trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const otherTrip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const otherTrip = await makeTrip(orm, user.id); const foreign = await insertFile(otherTrip.id); await withHarness(user.id, async (h) => { @@ -1090,9 +1080,9 @@ describe('Tool: list_trip_file_links', () => { }); it('denies a 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); const file = await insertFile(trip.id); await withHarness(user.id, async (h) => { @@ -1123,34 +1113,34 @@ describe('File tools: scope gating', () => { } it('registers all seven tools for a full-access session', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, null); for (const tool of [...READ_TOOLS, ...WRITE_TOOLS, 'read_trip_file']) expect(names).toContain(tool); }); it('files:read lists the documents but does not open them', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['files:read']); for (const tool of READ_TOOLS) expect(names).toContain(tool); for (const tool of [...WRITE_TOOLS, 'read_trip_file']) expect(names).not.toContain(tool); }); it('files:content opens a document without granting the listing', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['files:content']); expect(names).toContain('read_trip_file'); for (const tool of [...READ_TOOLS, ...WRITE_TOOLS]) expect(names).not.toContain(tool); }); it('files:write carries the reads, and still not the contents', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['files:write']); for (const tool of [...READ_TOOLS, ...WRITE_TOOLS]) expect(names).toContain(tool); expect(names).not.toContain('read_trip_file'); }); it('a token without any files scope gets none of them', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['trips:read']); for (const tool of [...READ_TOOLS, ...WRITE_TOOLS, 'read_trip_file']) expect(names).not.toContain(tool); }); diff --git a/server/tests/unit/mcp/tools-help-addons.test.ts b/server/tests/unit/mcp/tools-help-addons.test.ts index 6ef209e46..2011d812d 100644 --- a/server/tests/unit/mcp/tools-help-addons.test.ts +++ b/server/tests/unit/mcp/tools-help-addons.test.ts @@ -3,25 +3,21 @@ * list_help_topics, get_help_page, search_help, list_addons. */ import { ADDON_IDS } from '../../../src/addons'; -import { db as testDb } from '../../../src/db/database'; import { Addons } from '../../../src/db/entities/Addons.entity'; import { PhotoProviders } from '../../../src/db/entities/PhotoProviders.entity'; -import { createUser } from '../../helpers/factories'; +import { AddonsService } from '../../../src/nest/addons/addons.service'; +import { withRequestContext } from '../../../src/nest/database/request-context'; import { findRow, updateRows } from '../../helpers/factories/rows'; -import { setAppSetting } from '../../helpers/factories/settings'; +import { setAddonEnabled, setAppSetting } from '../../helpers/factories/settings'; +import { makeUser } from '../../helpers/factories/users'; import { FakeRealtimeService } from '../../helpers/fake-realtime'; import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness'; -import { createTestAddonsService } from '../../helpers/test-addons'; -import { resetTestDb, 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()); -}); - // The wiki reader is stubbed at the module boundary, the way the weather tools // stub weather.impl: help.mcp.ts calls these functions directly, and the reader // has its own tests in tests/unit/nest/wiki.test.ts. Stubbing keeps these cases @@ -97,8 +93,8 @@ function toolText(result: unknown): string { return content.find((c) => c.type === 'text')?.text ?? ''; } -beforeEach(() => { - resetTestDb(testDb); +beforeEach(async () => { + await resetDatabase(orm); broadcastMock.mockClear(); delete process.env.DEMO_MODE; wiki.getWikiIndex.mockReset(); @@ -121,22 +117,21 @@ beforeEach(() => { if (!page) throw new wiki.WikiNotFound(slug); return page; }); - // resetTestDb leaves the addons table alone, so a case that flips a toggle + // resetDatabase leaves the addons table alone, so a case that flips a toggle // would otherwise leak into the next one. for (const id of [ADDON_IDS.BUDGET, ADDON_IDS.PACKING, ADDON_IDS.COLLAB]) { - setAddonEnabled(testDb, id, true); + await setAddonEnabled(orm, id, true); } }); -let orm: TestOrm; +let orm: MikroORM; beforeAll(async () => { - orm = await createTestOrm(testDb); + orm = (await bootTestApp(realtime)).get(MikroORM); }); afterAll(async () => { - await orm.close(); - testDb.close(); + await (await bootTestApp(realtime)).close(); }); async function withHarness(userId: number, fn: (h: McpHarness) => Promise, scopes?: string[] | null) { @@ -154,7 +149,7 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise, describe('Tool: list_help_topics', () => { it('returns the sections the wiki reader parsed out of the sidebar', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_help_topics', arguments: {} }); expect(result.isError).toBeFalsy(); @@ -166,7 +161,7 @@ describe('Tool: list_help_topics', () => { it('answers with a refusal instead of throwing when the reader fails', async () => { wiki.getWikiIndex.mockRejectedValue(new Error('ENOENT')); - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_help_topics', arguments: {} }); expect(result.isError).toBe(true); @@ -175,7 +170,7 @@ describe('Tool: list_help_topics', () => { }); it('stays registered for a token holding an unrelated scope', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness( user.id, async (h) => { @@ -196,7 +191,7 @@ describe('Tool: list_help_topics', () => { describe('Tool: get_help_page', () => { it('returns a short page whole, with nothing left to fetch', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_help_page', @@ -216,7 +211,7 @@ describe('Tool: get_help_page', () => { }); it('refuses a slug this instance does not ship, and says where to look', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_help_page', @@ -230,7 +225,7 @@ describe('Tool: get_help_page', () => { it('separates an unavailable reader from a missing page', async () => { wiki.getWikiPage.mockRejectedValue(new Error('socket hang up')); - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_help_page', @@ -242,7 +237,7 @@ describe('Tool: get_help_page', () => { }); it('caps a long page and hands back the offset to resume from', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_help_page', @@ -258,7 +253,7 @@ describe('Tool: get_help_page', () => { }); it('resumes from the offset the previous chunk reported', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_help_page', @@ -273,7 +268,7 @@ describe('Tool: get_help_page', () => { }); it('refuses an offset past the end of the page', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_help_page', @@ -286,7 +281,7 @@ describe('Tool: get_help_page', () => { it('serves a demo account, the way the public REST route does', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@trek.app' }); + const { user } = await makeUser(orm, { email: 'demo@trek.app' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_help_page', @@ -305,7 +300,7 @@ describe('Tool: get_help_page', () => { describe('Tool: search_help', () => { it('hands the query and limit to the wiki search and returns its hits', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'search_help', arguments: { query: 'create trip', limit: 3 } }); expect(result.isError).toBeFalsy(); @@ -317,7 +312,7 @@ describe('Tool: search_help', () => { }); it('rejects an empty query before the search runs', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'search_help', arguments: { query: '' } }); expect(result.isError).toBe(true); @@ -327,7 +322,7 @@ describe('Tool: search_help', () => { it('reports an unavailable search instead of throwing', async () => { wiki.searchWiki.mockRejectedValue(new Error('ENOENT')); - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'search_help', arguments: { query: 'trip' } }); expect(result.isError).toBe(true); @@ -336,7 +331,7 @@ describe('Tool: search_help', () => { }); it('stays registered for a token holding an unrelated scope', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness( user.id, async (h) => { @@ -354,7 +349,7 @@ describe('Tool: search_help', () => { describe('Tool: list_addons', () => { it('lists the enabled addons with the collab sub-features and bag tracking', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const row = await findRow(orm, Addons, { id: 'budget' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_addons', arguments: {} }); @@ -374,9 +369,9 @@ describe('Tool: list_addons', () => { }); it('lists the enabled photo providers alongside the addons', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); // Providers ride the journey addon now; migration 84 seeds it off. - setAddonEnabled(testDb, ADDON_IDS.JOURNEY, true); + await setAddonEnabled(orm, ADDON_IDS.JOURNEY, true); // PhotoProviderSeeder seeds immich disabled by default (enabled: 0) — flip // it on the way the admin panel would, same idiom as setAddonEnabled above. await updateRows(orm, PhotoProviders, { id: 'immich' }, { enabled: 1 }); @@ -394,8 +389,8 @@ describe('Tool: list_addons', () => { }); it('drops an addon the admin switched off, so absence means disabled', async () => { - setAddonEnabled(testDb, ADDON_IDS.BUDGET, false); - const { user } = createUser(testDb); + await setAddonEnabled(orm, ADDON_IDS.BUDGET, false); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_addons', arguments: {} }); const data = parseToolResult(result) as AddonsPayload; @@ -409,15 +404,15 @@ describe('Tool: list_addons', () => { // Written through the same service the admin panel writes through rather // than through a hand-rolled app_settings row, so the tool is checked // against the real writer and not against a restatement of it. - const addonsService = await createTestAddonsService(testDb); - const { user } = createUser(testDb); + const addonsService = (await bootTestApp(realtime)).get(AddonsService); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { - await addonsService.updateCollabFeatures({ polls: false }); + await withRequestContext(orm, () => addonsService.updateCollabFeatures({ polls: false })); const off = parseToolResult(await h.client.callTool({ name: 'list_addons', arguments: {} })) as AddonsPayload; expect(off.collabFeatures.polls).toBe(false); expect(off.collabFeatures.chat).toBe(true); - await addonsService.updateCollabFeatures({ polls: true }); + await withRequestContext(orm, () => addonsService.updateCollabFeatures({ polls: true })); const on = parseToolResult(await h.client.callTool({ name: 'list_addons', arguments: {} })) as AddonsPayload; expect(on.collabFeatures.polls).toBe(true); }); @@ -425,7 +420,7 @@ describe('Tool: list_addons', () => { it('reports bag tracking once it is switched on', async () => { await setAppSetting(orm, 'bag_tracking_enabled', 'true'); - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_addons', arguments: {} }); const data = parseToolResult(result) as AddonsPayload; @@ -434,7 +429,7 @@ describe('Tool: list_addons', () => { }); it('stays registered for a token holding an unrelated scope', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness( user.id, async (h) => { @@ -449,7 +444,7 @@ describe('Tool: list_addons', () => { it('serves a demo account, the way the authenticated REST route does', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@trek.app' }); + const { user } = await makeUser(orm, { email: 'demo@trek.app' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_addons', arguments: {} }); expect(result.isError).toBeFalsy();