diff --git a/server/scripts/test-mocks-baseline.json b/server/scripts/test-mocks-baseline.json index 56bb729d9..1d6f67d0e 100644 --- a/server/scripts/test-mocks-baseline.json +++ b/server/scripts/test-mocks-baseline.json @@ -66,9 +66,6 @@ "tests/unit/mcp/tools-notifications.test.ts": 1, "tests/unit/mcp/tools-packing-advanced.test.ts": 1, "tests/unit/mcp/tools-prompts.test.ts": 1, - "tests/unit/mcp/tools-reservations.test.ts": 1, - "tests/unit/mcp/tools-roadtrip.test.ts": 1, - "tests/unit/mcp/tools-settings.test.ts": 1, "tests/unit/mcp/tools-share.test.ts": 1, "tests/unit/mcp/tools-tags-maps-weather.test.ts": 1, "tests/unit/mcp/tools-transit.test.ts": 1, diff --git a/server/scripts/test-new-service-baseline.json b/server/scripts/test-new-service-baseline.json index 47e18e771..e5550005b 100644 --- a/server/scripts/test-new-service-baseline.json +++ b/server/scripts/test-new-service-baseline.json @@ -24,7 +24,6 @@ "tests/unit/db/repositories/UserConnections.repository.test.ts": 3, "tests/unit/demo/demo-reset-sessions.test.ts": 3, "tests/unit/mcp/tools-prompts.test.ts": 7, - "tests/unit/mcp/tools-roadtrip.test.ts": 1, "tests/unit/mcp/tools-transit.test.ts": 1, "tests/unit/nest-rpc/plugin-guards.test.ts": 1, "tests/unit/nest-rpc/rpc-kit/registry.service.test.ts": 3, diff --git a/server/tests/postgres/snapshot-suites-baseline.json b/server/tests/postgres/snapshot-suites-baseline.json index 852106339..17203bd04 100644 --- a/server/tests/postgres/snapshot-suites-baseline.json +++ b/server/tests/postgres/snapshot-suites-baseline.json @@ -1,9 +1,6 @@ [ "tests/unit/mcp/tools-notifications.test.ts", "tests/unit/mcp/tools-packing-advanced.test.ts", - "tests/unit/mcp/tools-reservations.test.ts", - "tests/unit/mcp/tools-roadtrip.test.ts", - "tests/unit/mcp/tools-settings.test.ts", "tests/unit/mcp/tools-share.test.ts", "tests/unit/mcp/tools-tags-maps-weather.test.ts", "tests/unit/mcp/tools-transit.test.ts", diff --git a/server/tests/unit/mcp/tools-reservations.test.ts b/server/tests/unit/mcp/tools-reservations.test.ts index e4131991a..16947b225 100644 --- a/server/tests/unit/mcp/tools-reservations.test.ts +++ b/server/tests/unit/mcp/tools-reservations.test.ts @@ -2,7 +2,6 @@ * Unit tests for MCP reservation tools: create_reservation, update_reservation, * delete_reservation, link_hotel_accommodation. */ -import { db as testDb } from '../../../src/db/database'; import { BudgetItems } from '../../../src/db/entities/BudgetItems.entity'; import { DayAccommodations } from '../../../src/db/entities/DayAccommodations.entity'; import { ReservationDayPositions } from '../../../src/db/entities/ReservationDayPositions.entity'; @@ -10,41 +9,28 @@ import { ReservationTravelers } from '../../../src/db/entities/ReservationTravel import { Reservations } from '../../../src/db/entities/Reservations.entity'; import { Trips } from '../../../src/db/entities/Trips.entity'; import { ReservationsService } from '../../../src/nest/reservations/reservations.service'; -import { - createUser, - createTrip, - createDay, - createPlace, - createReservation, - createBudgetItem, - createDayAssignment, - createDayAccommodation, - createCategory, - addTripMember, -} from '../../helpers/factories'; +import { makeBudgetItem } from '../../helpers/factories/budget'; +import { makeDayAccommodation, makeDayAssignment } from '../../helpers/factories/itinerary'; +import { makeCategory, makePlace } from '../../helpers/factories/places'; import { makeReservation } from '../../helpers/factories/reservations'; import { countRows, findRow, findRows, insertRow, updateRows } from '../../helpers/factories/rows'; -import { makeTrip, readTripDays } from '../../helpers/factories/trips'; +import { addTripMember, 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; -let orm: TestOrm; +let orm: MikroORM; beforeAll(async () => { - orm = await createTestOrm(testDb); + orm = (await bootTestApp(realtime)).get(MikroORM); }); async function reservationRow(id: number) { @@ -56,15 +42,14 @@ async function reservationUrl(id: number) { return row ? { url: row.url } : 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) { @@ -82,8 +67,8 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise) describe('Tool: create_reservation', () => { it('creates a basic reservation', 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_reservation', @@ -97,8 +82,8 @@ describe('Tool: create_reservation', () => { }); it('files the cost of a priced booking under its Costs key when no budget_category is given', 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_reservation', @@ -112,8 +97,8 @@ describe('Tool: create_reservation', () => { }); it('creates a parking reservation (#1444)', 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_reservation', @@ -126,11 +111,11 @@ describe('Tool: create_reservation', () => { }); it('creates a hotel reservation and links accommodation', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day1 = createDay(testDb, trip.id, { day_number: 1 }); - const day2 = createDay(testDb, trip.id, { day_number: 2 }); - const hotel = createPlace(testDb, trip.id, { name: 'Grand Hotel' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day1 = await makeDay(orm, trip.id, { day_number: 1 }); + const day2 = await makeDay(orm, trip.id, { day_number: 2 }); + const hotel = await makePlace(orm, trip.id, { name: 'Grand Hotel' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -157,10 +142,10 @@ describe('Tool: create_reservation', () => { }); it('validates day_id belongs 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({ name: 'create_reservation', @@ -171,12 +156,12 @@ describe('Tool: create_reservation', () => { }); it('validates assignment_id belongs to trip', async () => { - const { user } = createUser(testDb); - const trip1 = createTrip(testDb, user.id); - const trip2 = createTrip(testDb, user.id); - const day2 = createDay(testDb, trip2.id); - const place2 = createPlace(testDb, trip2.id); - const assignment = createDayAssignment(testDb, day2.id, place2.id); + const { user } = await makeUser(orm); + const trip1 = await makeTrip(orm, user.id); + const trip2 = await makeTrip(orm, user.id); + const day2 = await makeDay(orm, trip2.id); + const place2 = await makePlace(orm, trip2.id); + const assignment = await makeDayAssignment(orm, day2.id, place2.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'create_reservation', @@ -187,8 +172,8 @@ describe('Tool: create_reservation', () => { }); it('broadcasts reservation:created event', 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) => { await h.client.callTool({ name: 'create_reservation', @@ -199,11 +184,11 @@ describe('Tool: create_reservation', () => { }); it('broadcasts accommodation:created for hotel type', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day1 = createDay(testDb, trip.id, { day_number: 1 }); - const day2 = createDay(testDb, trip.id, { day_number: 2 }); - const hotel = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day1 = await makeDay(orm, trip.id, { day_number: 1 }); + const day2 = await makeDay(orm, trip.id, { day_number: 2 }); + const hotel = await makePlace(orm, trip.id); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'create_reservation', @@ -221,9 +206,9 @@ describe('Tool: create_reservation', () => { }); 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_reservation', @@ -263,9 +248,9 @@ describe('Tool: update_reservation', () => { }); it('updates reservation fields', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id, { title: 'Old Title', type: 'flight' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id, { title: 'Old Title', type: 'flight' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_reservation', @@ -278,9 +263,9 @@ describe('Tool: update_reservation', () => { }); it('updates reservation status to confirmed', 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); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_reservation', @@ -292,9 +277,9 @@ describe('Tool: update_reservation', () => { }); it('broadcasts reservation:updated event', 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); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'update_reservation', @@ -305,11 +290,11 @@ describe('Tool: update_reservation', () => { }); it('re-files a linked expense on a type change in the same write, as REST and the plugin RPC do', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id, { title: 'Dinner', type: 'restaurant' }); - const auto = createBudgetItem(testDb, trip.id, { name: 'Dinner', category: 'food' }); - const picked = createBudgetItem(testDb, trip.id, { name: 'Taxi', category: 'transport' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id, { title: 'Dinner', type: 'restaurant' }); + const auto = await makeBudgetItem(orm, trip.id, { name: 'Dinner', category: 'food' }); + const picked = await makeBudgetItem(orm, trip.id, { name: 'Taxi', category: 'transport' }); await updateRows(orm, BudgetItems, { id: { $in: [auto.id, picked.id] } }, { reservation: reservation.id }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -339,10 +324,10 @@ describe('Tool: update_reservation', () => { // #2631: the day planner puts a booking moved on a day by its day slot, and the // edit's answer and broadcast replace the booking in every open list. it('keeps the day slots in the answer and the broadcast', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day = createDay(testDb, trip.id); - const reservation = createReservation(testDb, trip.id, { title: 'Bus', type: 'bus' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day = await makeDay(orm, trip.id); + const reservation = await makeReservation(orm, trip.id, { title: 'Bus', type: 'bus' }); await insertRow(orm, ReservationDayPositions, { reservation: reservation.id, day: day.id, position: 2.5 }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -357,8 +342,8 @@ describe('Tool: update_reservation', () => { }); it('returns error for reservation not found', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_reservation', @@ -369,11 +354,11 @@ describe('Tool: update_reservation', () => { }); it('validates place_id belongs to trip', async () => { - const { user } = createUser(testDb); - const trip1 = createTrip(testDb, user.id); - const trip2 = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip1.id); - const placeFromTrip2 = createPlace(testDb, trip2.id); + const { user } = await makeUser(orm); + const trip1 = await makeTrip(orm, user.id); + const trip2 = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip1.id); + const placeFromTrip2 = await makePlace(orm, trip2.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_reservation', @@ -384,10 +369,10 @@ describe('Tool: update_reservation', () => { }); it('returns access denied for 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); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'update_reservation', @@ -404,9 +389,9 @@ describe('Tool: update_reservation', () => { describe('Tool: delete_reservation', () => { it('deletes a reservation', 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); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'delete_reservation', @@ -419,11 +404,11 @@ describe('Tool: delete_reservation', () => { }); it('cascades to accommodation when linked', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day1 = createDay(testDb, trip.id, { day_number: 1 }); - const day2 = createDay(testDb, trip.id, { day_number: 2 }); - const hotel = createPlace(testDb, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day1 = await makeDay(orm, trip.id, { day_number: 1 }); + const day2 = await makeDay(orm, trip.id, { day_number: 2 }); + const hotel = await makePlace(orm, trip.id); // Create reservation via tool so accommodation is linked let reservationId: number; await withHarness(user.id, async (h) => { @@ -452,9 +437,9 @@ describe('Tool: delete_reservation', () => { }); it('broadcasts reservation:deleted event', 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); await withHarness(user.id, async (h) => { await h.client.callTool({ name: 'delete_reservation', @@ -467,12 +452,12 @@ describe('Tool: delete_reservation', () => { }); it('takes every linked expense with it and announces each one, as the REST route does (#2084)', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const reservation = createReservation(testDb, trip.id); - const fare = createBudgetItem(testDb, trip.id, { name: 'Fare' }); - const luggage = createBudgetItem(testDb, trip.id, { name: 'Luggage' }); - const unrelated = createBudgetItem(testDb, trip.id, { name: 'Coffee' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const reservation = await makeReservation(orm, trip.id); + const fare = await makeBudgetItem(orm, trip.id, { name: 'Fare' }); + const luggage = await makeBudgetItem(orm, trip.id, { name: 'Luggage' }); + const unrelated = await makeBudgetItem(orm, trip.id, { name: 'Coffee' }); await updateRows(orm, BudgetItems, { id: { $in: [fare.id, luggage.id] } }, { reservation: reservation.id }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -494,8 +479,8 @@ describe('Tool: delete_reservation', () => { }); it('returns error for reservation not found', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'delete_reservation', @@ -506,10 +491,10 @@ describe('Tool: delete_reservation', () => { }); it('returns access denied for 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); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'delete_reservation', @@ -529,11 +514,11 @@ describe('Reservation tools: the booking notice', () => { it('create, update, the hotel link and delete each tell the fellow travellers, as REST does', async () => { const notify = vi.spyOn(ReservationsService.prototype, 'notifyBookingChange').mockResolvedValue(undefined); try { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day1 = createDay(testDb, trip.id, { day_number: 1 }); - const day2 = createDay(testDb, trip.id, { day_number: 2 }); - const hotel = createPlace(testDb, trip.id, { name: 'Ritz' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day1 = await makeDay(orm, trip.id, { day_number: 1 }); + const day2 = await makeDay(orm, trip.id, { day_number: 2 }); + const hotel = await makePlace(orm, trip.id, { name: 'Ritz' }); await withHarness(user.id, async (h) => { const created = parseToolResult( await h.client.callTool({ @@ -571,12 +556,12 @@ describe('Reservation tools: the booking notice', () => { describe('Tool: link_hotel_accommodation', () => { it('creates new accommodation link for a hotel reservation', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const day1 = createDay(testDb, trip.id, { day_number: 1 }); - const day2 = createDay(testDb, trip.id, { day_number: 2 }); - const hotel = createPlace(testDb, trip.id, { name: 'Ritz' }); - const reservation = createReservation(testDb, trip.id, { type: 'hotel' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day1 = await makeDay(orm, trip.id, { day_number: 1 }); + const day2 = await makeDay(orm, trip.id, { day_number: 2 }); + const hotel = await makePlace(orm, trip.id, { name: 'Ritz' }); + const reservation = await makeReservation(orm, trip.id, { type: 'hotel' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ @@ -605,14 +590,14 @@ describe('Tool: link_hotel_accommodation', () => { }); it('updates existing accommodation link', 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 day3 = createDay(testDb, trip.id, { day_number: 3 }); - const hotel = createPlace(testDb, trip.id, { name: 'Hotel A' }); - const hotel2 = createPlace(testDb, trip.id, { name: 'Hotel B' }); - const reservation = createReservation(testDb, trip.id, { type: 'hotel' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const day1 = await makeDay(orm, trip.id, { day_number: 1 }); + const day2 = await makeDay(orm, trip.id, { day_number: 2 }); + const day3 = await makeDay(orm, trip.id, { day_number: 3 }); + const hotel = await makePlace(orm, trip.id, { name: 'Hotel A' }); + const hotel2 = await makePlace(orm, trip.id, { name: 'Hotel B' }); + const reservation = await makeReservation(orm, trip.id, { type: 'hotel' }); // First link await withHarness(user.id, async (h) => { @@ -648,12 +633,12 @@ describe('Tool: link_hotel_accommodation', () => { }); it('returns error for non-hotel reservation', 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); - const reservation = createReservation(testDb, trip.id, { type: 'flight' }); + 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); + const reservation = await makeReservation(orm, trip.id, { type: 'flight' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'link_hotel_accommodation', @@ -670,13 +655,13 @@ describe('Tool: link_hotel_accommodation', () => { }); it('validates place_id belongs to trip', async () => { - const { user } = createUser(testDb); - const trip1 = createTrip(testDb, user.id); - const trip2 = createTrip(testDb, user.id); - const day1 = createDay(testDb, trip1.id, { day_number: 1 }); - const day2 = createDay(testDb, trip1.id, { day_number: 2 }); - const placeFromTrip2 = createPlace(testDb, trip2.id); - const reservation = createReservation(testDb, trip1.id, { type: 'hotel' }); + const { user } = await makeUser(orm); + const trip1 = await makeTrip(orm, user.id); + const trip2 = await makeTrip(orm, user.id); + const day1 = await makeDay(orm, trip1.id, { day_number: 1 }); + const day2 = await makeDay(orm, trip1.id, { day_number: 2 }); + const placeFromTrip2 = await makePlace(orm, trip2.id); + const reservation = await makeReservation(orm, trip1.id, { type: 'hotel' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'link_hotel_accommodation', @@ -693,13 +678,13 @@ describe('Tool: link_hotel_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 day1 = createDay(testDb, trip.id, { day_number: 1 }); - const day2 = createDay(testDb, trip.id, { day_number: 2 }); - const place = createPlace(testDb, trip.id); - const reservation = createReservation(testDb, trip.id, { type: 'hotel' }); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const trip = await makeTrip(orm, other.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); + const reservation = await makeReservation(orm, trip.id, { type: 'hotel' }); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'link_hotel_accommodation', @@ -723,9 +708,9 @@ describe('Tool: link_hotel_accommodation', () => { describe('Resource: trek://trips/{tripId}/reservations', () => { it('returns reservations for a trip', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - createReservation(testDb, trip.id, { title: 'Flight to Paris', type: 'flight' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + await makeReservation(orm, trip.id, { title: 'Flight to Paris', type: 'flight' }); await withHarness(user.id, async (h) => { const result = await h.client.readResource({ uri: `trek://trips/${trip.id}/reservations` }); @@ -736,9 +721,9 @@ describe('Resource: trek://trips/{tripId}/reservations', () => { }); 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}/reservations` }); @@ -768,10 +753,10 @@ describe('Tool: set_reservation_travelers', () => { } it('sets the travellers on a booking', async () => { - const { user: owner } = createUser(testDb); - const { user: friend } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, friend.id); + const { user: owner } = await makeUser(orm); + const { user: friend } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + await addTripMember(orm, trip.id, friend.id); await withHarness(owner.id, async (h) => { const reservationId = await makeBooking(h, trip.id); const result = await h.client.callTool({ @@ -786,8 +771,8 @@ describe('Tool: set_reservation_travelers', () => { }); it('assigns a guest, who has no account to look up', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); await withHarness(owner.id, async (h) => { const guestResult = await h.client.callTool({ name: 'create_trip_guest', @@ -808,10 +793,10 @@ describe('Tool: set_reservation_travelers', () => { }); it('replaces the previous list rather than adding to it', async () => { - const { user: owner } = createUser(testDb); - const { user: friend } = createUser(testDb); - const trip = createTrip(testDb, owner.id); - addTripMember(testDb, trip.id, friend.id); + const { user: owner } = await makeUser(orm); + const { user: friend } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); + await addTripMember(orm, trip.id, friend.id); await withHarness(owner.id, async (h) => { const reservationId = await makeBooking(h, trip.id); await h.client.callTool({ @@ -828,8 +813,8 @@ describe('Tool: set_reservation_travelers', () => { }); it('clears the list on an empty array', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); await withHarness(owner.id, async (h) => { const reservationId = await makeBooking(h, trip.id); await h.client.callTool({ @@ -846,9 +831,9 @@ describe('Tool: set_reservation_travelers', () => { }); it('drops a user who is not on the trip instead of attaching them', async () => { - const { user: owner } = createUser(testDb); - const { user: outsider } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const { user: outsider } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); await withHarness(owner.id, async (h) => { const reservationId = await makeBooking(h, trip.id); const result = await h.client.callTool({ @@ -864,8 +849,8 @@ describe('Tool: set_reservation_travelers', () => { }); it('says nothing about ignored ids when every id was on the trip', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); await withHarness(owner.id, async (h) => { const reservationId = await makeBooking(h, trip.id); const result = await h.client.callTool({ @@ -878,8 +863,8 @@ describe('Tool: set_reservation_travelers', () => { }); it('broadcasts reservation:travelers-updated', async () => { - const { user: owner } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); await withHarness(owner.id, async (h) => { const reservationId = await makeBooking(h, trip.id); broadcastMock.mockClear(); @@ -896,9 +881,9 @@ describe('Tool: set_reservation_travelers', () => { }); it('404s a booking of another trip', async () => { - const { user: owner } = createUser(testDb); - const ownTrip = createTrip(testDb, owner.id); - const otherTrip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const ownTrip = await makeTrip(orm, owner.id); + const otherTrip = await makeTrip(orm, owner.id); await withHarness(owner.id, async (h) => { const reservationId = await makeBooking(h, otherTrip.id); const result = await h.client.callTool({ @@ -910,9 +895,9 @@ describe('Tool: set_reservation_travelers', () => { }); it('returns access denied for a non-member', async () => { - const { user: owner } = createUser(testDb); - const { user: outsider } = createUser(testDb); - const trip = createTrip(testDb, owner.id); + const { user: owner } = await makeUser(orm); + const { user: outsider } = await makeUser(orm); + const trip = await makeTrip(orm, owner.id); let reservationId = 0; await withHarness(owner.id, async (h) => { reservationId = await makeBooking(h, trip.id); @@ -928,8 +913,8 @@ describe('Tool: set_reservation_travelers', () => { 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: 'set_reservation_travelers', @@ -950,8 +935,8 @@ describe('Tool: set_reservation_travelers', () => { describe('Reservation tools: url and reservation_end_time', () => { it('stores the booking link on create', 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_reservation', @@ -970,8 +955,8 @@ describe('Reservation tools: url and reservation_end_time', () => { }); it('stores an end time on a non-transport booking', 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_reservation', @@ -991,8 +976,8 @@ describe('Reservation tools: url and reservation_end_time', () => { }); it('updates the link and the end time on an existing booking', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await withHarness(user.id, async (h) => { const created = await h.client.callTool({ name: 'create_reservation', @@ -1016,8 +1001,8 @@ describe('Reservation tools: url and reservation_end_time', () => { }); it('refuses a javascript: link, like the REST contract does', 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_reservation', @@ -1029,8 +1014,8 @@ describe('Reservation tools: url and reservation_end_time', () => { }); it('refuses a javascript: link split by a control character', 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_reservation', @@ -1041,8 +1026,8 @@ describe('Reservation tools: url and reservation_end_time', () => { }); it('stores the booking link on a transport booking too', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await withHarness(user.id, async (h) => { const created = await h.client.callTool({ name: 'create_transport', @@ -1111,12 +1096,12 @@ describe('Tool: list_upcoming_reservations', () => { } it('spans every trip the user can see, soonest first, and stops at the trips they cannot', async () => { - const { user } = createUser(testDb); - const { user: other } = createUser(testDb); - const owned = createTrip(testDb, user.id); - const joined = createTrip(testDb, other.id); - const stranger = createTrip(testDb, other.id); - addTripMember(testDb, joined.id, user.id); + const { user } = await makeUser(orm); + const { user: other } = await makeUser(orm); + const owned = await makeTrip(orm, user.id); + const joined = await makeTrip(orm, other.id); + const stranger = await makeTrip(orm, other.id); + await addTripMember(orm, joined.id, user.id); await seedBooking(owned.id, 'Flight to Paris', `${dateInDays(4)}T08:00:00`, { type: 'flight' }); await seedBooking(joined.id, 'Group dinner', `${dateInDays(2)}T19:00:00`); @@ -1127,13 +1112,13 @@ describe('Tool: list_upcoming_reservations', () => { }); it('reports a hotel stay as its check-in and check-out moments', async () => { - const { user } = createUser(testDb); - createCategory(testDb); - const trip = createTrip(testDb, user.id); - const arrival = createDay(testDb, trip.id, { date: dateInDays(3) }); - const departure = createDay(testDb, trip.id, { date: dateInDays(6) }); - const hotel = createPlace(testDb, trip.id, { name: 'Hotel Astoria' }); - createDayAccommodation(testDb, trip.id, hotel.id, arrival.id, departure.id, { + const { user } = await makeUser(orm); + await makeCategory(orm); + const trip = await makeTrip(orm, user.id); + const arrival = await makeDay(orm, trip.id, { date: dateInDays(3) }); + const departure = await makeDay(orm, trip.id, { date: dateInDays(6) }); + const hotel = await makePlace(orm, trip.id, { name: 'Hotel Astoria' }); + await makeDayAccommodation(orm, trip.id, hotel.id, arrival.id, departure.id, { check_in: '15:00', check_out: '11:00', }); @@ -1146,9 +1131,9 @@ describe('Tool: list_upcoming_reservations', () => { }); it('leaves out cancelled bookings and archived trips', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const archived = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const archived = await makeTrip(orm, user.id); await updateRows(orm, Trips, { id: archived.id }, { is_archived: 1 }); await seedBooking(trip.id, 'Still on', `${dateInDays(2)}T12:00:00`); @@ -1160,8 +1145,8 @@ describe('Tool: list_upcoming_reservations', () => { }); it('leaves out planned public-transit journeys and keeps a booked train, as the dashboard does', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); await seedBooking(trip.id, 'Metro to the museum', `${dateInDays(1)}T09:00:00`, { type: 'transit', status: 'confirmed', @@ -1173,8 +1158,8 @@ describe('Tool: list_upcoming_reservations', () => { }); it('returns the dashboard six by default and honours a limit', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); for (let i = 1; i <= 8; i++) await seedBooking(trip.id, `Booking ${i}`, `${dateInDays(i)}T09:00:00`); expect((await upcoming(user.id)).map((r) => r.title)).toEqual([ @@ -1189,7 +1174,7 @@ describe('Tool: list_upcoming_reservations', () => { }); it('refuses a limit outside the allowed range', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'list_upcoming_reservations', arguments: { limit: 0 } }); expect(result.isError).toBe(true); @@ -1235,9 +1220,9 @@ async function listed(userId: number, args: Record): Promise
  • { it('RESREAD-MCP-001: lists the bookings with their readable details and without the raw metadata', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const { trip } = await seedTrip(user.id); const bookings = await listed(user.id, { tripId: trip.id }); expect(bookings.map((b) => b.title).sort()).toEqual(['Dinner', 'Frankfurt to New York']); @@ -1286,7 +1271,7 @@ describe('Tool: list_reservations', () => { }); it('RESREAD-MCP-005: says that a leg holds a reference, never the reference itself', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const { trip } = await seedTrip(user.id); const bookings = await listed(user.id, { tripId: trip.id }); expect(bookings.find((b) => b.type === 'flight')?.details.has_leg_reference).toBe(true); @@ -1294,7 +1279,7 @@ describe('Tool: list_reservations', () => { }); it('RESREAD-MCP-002: keeps one kind, or the bookings on one day (a leg arriving on it counts)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const { trip, day1, day2 } = await seedTrip(user.id); expect((await listed(user.id, { tripId: trip.id, kinds: ['transport'] })).map((b) => b.type)).toEqual(['flight']); expect((await listed(user.id, { tripId: trip.id, kinds: ['booking'] })).map((b) => b.type)).toEqual(['restaurant']); @@ -1306,8 +1291,8 @@ describe('Tool: list_reservations', () => { }); it('RESREAD-MCP-003: refuses a trip the user cannot see', async () => { - const { user } = createUser(testDb); - const { user: stranger } = createUser(testDb); + const { user } = await makeUser(orm); + const { user: stranger } = await makeUser(orm); const { trip } = await seedTrip(user.id); await withScopedHarness(stranger.id, async (h) => { const result = await h.client.callTool({ name: 'list_reservations', arguments: { tripId: trip.id } }); @@ -1316,7 +1301,7 @@ describe('Tool: list_reservations', () => { }); it('RESREAD-MCP-004: rides reservations:read and is hidden from a token without it', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withScopedHarness( user.id, async (h) => { @@ -1336,10 +1321,10 @@ describe('Tool: list_reservations', () => { describe('Tool: list_accommodations', () => { it('RESREAD-MCP-010: lists the stays with their place and check-in and check-out', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const { trip, day1, day2 } = await seedTrip(user.id); - const hotel = createPlace(testDb, trip.id, { name: 'Mercure', lat: 52.53, lng: 13.38 }); - createDayAccommodation(testDb, trip.id, hotel.id, day1.id, day2.id, { check_in: '15:00', confirmation: 'H-1' }); + const hotel = await makePlace(orm, trip.id, { name: 'Mercure', lat: 52.53, lng: 13.38 }); + await makeDayAccommodation(orm, trip.id, hotel.id, day1.id, day2.id, { check_in: '15:00', confirmation: 'H-1' }); await withScopedHarness(user.id, async (h) => { const data = parseToolResult( await h.client.callTool({ name: 'list_accommodations', arguments: { tripId: trip.id } }), @@ -1356,8 +1341,8 @@ describe('Tool: list_accommodations', () => { }); it('RESREAD-MCP-011: refuses a trip the user cannot see, and is hidden from a token without trip reads', async () => { - const { user } = createUser(testDb); - const { user: stranger } = createUser(testDb); + const { user } = await makeUser(orm); + const { user: stranger } = await makeUser(orm); const { trip } = await seedTrip(user.id); await withScopedHarness(stranger.id, async (h) => { const result = await h.client.callTool({ name: 'list_accommodations', arguments: { tripId: trip.id } }); diff --git a/server/tests/unit/mcp/tools-roadtrip.test.ts b/server/tests/unit/mcp/tools-roadtrip.test.ts index b220078a2..e3916ff0d 100644 --- a/server/tests/unit/mcp/tools-roadtrip.test.ts +++ b/server/tests/unit/mcp/tools-roadtrip.test.ts @@ -9,55 +9,49 @@ * track check on the batch tool. */ import { ADDON_IDS } from '../../../src/addons'; -import { db as testDb } from '../../../src/db/database'; import { Places } from '../../../src/db/entities/Places.entity'; import { RoadtripPreferences } from '../../../src/db/entities/RoadtripPreferences.entity'; import { RoadtripVias } from '../../../src/db/entities/RoadtripVias.entity'; import { DEMO_EMAIL_PRIMARY } from '../../../src/nest/common/demo'; +import { withRequestContext } from '../../../src/nest/database/request-context'; import { PermissionsService } from '../../../src/nest/permissions/permissions.service'; -import { createUser, createTrip, createDay, createPlace } from '../../helpers/factories'; -import { addTripMember } from '../../helpers/factories'; +import { makePlace } from '../../helpers/factories/places'; import { countRows, findRow, updateRows } from '../../helpers/factories/rows'; +import { setAddonEnabled } 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 { resetTestDb, setAddonEnabled } from '../../helpers/test-db'; -import { createTestOrm, type TestOrm } from '../../helpers/test-orm'; -import { createTestUnitOfWork, createTestAppSettingsRepo } from '../../helpers/test-uow'; +import { resetDatabase } from '../../helpers/reset-database'; +import { bootTestApp } from '../../helpers/test-app'; +import { MikroORM } from '@mikro-orm/core'; -import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; +import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; -vi.mock('../../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - -let orm: TestOrm; +let orm: MikroORM; // Every tool here broadcasts through this one, so a case can read what went out. const realtime = new FakeRealtimeService(); -// The permissions cache is module-scoped, so a write through any instance is -// what the tool's own check reads back. +// The container's own PermissionsService, the one the tools check against, so +// a write through it is what the tool's own check reads back. let savePermissions: PermissionsService['savePermissions']; beforeAll(async () => { - orm = await createTestOrm(testDb); - const permissionsService = new PermissionsService( - await createTestAppSettingsRepo(testDb), - await createTestUnitOfWork(testDb), - ); - savePermissions = permissionsService.savePermissions.bind(permissionsService); + const app = await bootTestApp(realtime); + orm = app.get(MikroORM); + const permissionsService = app.get(PermissionsService); + savePermissions = (settings) => withRequestContext(orm, () => permissionsService.savePermissions(settings)); }); -beforeEach(() => { - resetTestDb(testDb); +beforeEach(async () => { + await resetDatabase(orm); realtime.reset(); - // resetTestDb leaves the addons table alone, so each run restates it. The + // resetDatabase leaves the addons table alone, so each run restates it. The // addon ships disabled, which is why the tools were invisible before. - setAddonEnabled(testDb, ADDON_IDS.ROADTRIP, true); + await setAddonEnabled(orm, ADDON_IDS.ROADTRIP, true); }); afterAll(async () => { - await orm.close(); - testDb.close(); + await (await bootTestApp(realtime)).close(); }); async function withHarness(userId: number, fn: (h: McpHarness) => Promise) { @@ -71,18 +65,18 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise) /** A trip with one day, plus a place carrying a route geometry to use as a track. */ async function scenario() { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Norway' }); - const day = createDay(testDb, trip.id, { day_number: 1 }); - const track = createPlace(testDb, trip.id, { name: 'Scenic' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Norway' }); + const day = await makeDay(orm, trip.id, { day_number: 1 }); + const track = await makePlace(orm, trip.id, { name: 'Scenic' }); await updateRows(orm, Places, { id: track.id }, { route_geometry: '[[1,2],[3,4]]' }); return { user, trip, day, track }; } describe('road-trip MCP tools', () => { it('MCP-ROADTRIP-001: the five tools are gone while the addon is off', async () => { - const { user } = createUser(testDb); - setAddonEnabled(testDb, ADDON_IDS.ROADTRIP, false); + const { user } = await makeUser(orm); + await setAddonEnabled(orm, ADDON_IDS.ROADTRIP, false); await withHarness(user.id, async (h) => { const names = (await h.client.listTools()).tools.map((t) => t.name); @@ -99,7 +93,7 @@ describe('road-trip MCP tools', () => { }); it('MCP-ROADTRIP-002: and there while it is on', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const names = (await h.client.listTools()).tools.map((t) => t.name); for (const tool of [ @@ -150,8 +144,8 @@ describe('road-trip MCP tools', () => { // The same check the REST routes make, which is what the repo means by the // two surfaces moving together. const { user, trip } = await scenario(); - const other = createTrip(testDb, user.id, { title: 'Elsewhere' }); - const otherDay = createDay(testDb, other.id, { day_number: 1 }); + const other = await makeTrip(orm, user.id, { title: 'Elsewhere' }); + const otherDay = await makeDay(orm, other.id, { day_number: 1 }); await withHarness(user.id, async (h) => { const calls: [string, Record][] = [ @@ -170,10 +164,10 @@ describe('road-trip MCP tools', () => { }); it('MCP-ROADTRIP-005: a trip the caller cannot reach is refused', async () => { - const { user } = createUser(testDb); - const stranger = createUser(testDb, { email: 'other@example.test' }); - const theirs = createTrip(testDb, stranger.user.id, { title: 'Theirs' }); - const theirDay = createDay(testDb, theirs.id, { day_number: 1 }); + const { user } = await makeUser(orm); + const stranger = await makeUser(orm, { email: 'other@example.test' }); + const theirs = await makeTrip(orm, stranger.user.id, { title: 'Theirs' }); + const theirDay = await makeDay(orm, theirs.id, { day_number: 1 }); await withHarness(user.id, async (h) => { const res = await h.client.callTool({ @@ -215,9 +209,9 @@ describe('road-trip MCP tools', () => { it('MCP-ROADTRIP-007: a track from another trip cannot label this day', async () => { const { user, trip, day } = await scenario(); - const stranger = createUser(testDb, { email: 'other2@example.test' }); - const theirs = createTrip(testDb, stranger.user.id, { title: 'Theirs' }); - const theirTrack = createPlace(testDb, theirs.id, { name: 'Not yours' }); + const stranger = await makeUser(orm, { email: 'other2@example.test' }); + const theirs = await makeTrip(orm, stranger.user.id, { title: 'Theirs' }); + const theirTrack = await makePlace(orm, theirs.id, { name: 'Not yours' }); await updateRows(orm, Places, { id: theirTrack.id }, { route_geometry: '[[1,2]]' }); await withHarness(user.id, async (h) => { @@ -274,8 +268,8 @@ describe('road-trip MCP tools', () => { // an assistant must never be able to do through a tool what the person it // is acting for cannot do through the UI. const { user, trip, day } = await scenario(); - const member = createUser(testDb, { email: 'member@example.test' }); - addTripMember(testDb, trip.id, member.user.id); + const member = await makeUser(orm, { email: 'member@example.test' }); + await addTripMember(orm, trip.id, member.user.id); await savePermissions({ day_edit: 'trip_owner' }); try { @@ -312,8 +306,8 @@ describe('road-trip MCP tools', () => { it('MCP-ROADTRIP-011: the demo account may look but never write', async () => { const { trip, day } = await scenario(); - const demo = createUser(testDb, { email: DEMO_EMAIL_PRIMARY }); - addTripMember(testDb, trip.id, demo.user.id); + const demo = await makeUser(orm, { email: DEMO_EMAIL_PRIMARY }); + await addTripMember(orm, trip.id, demo.user.id); process.env.DEMO_MODE = 'true'; try { @@ -387,7 +381,7 @@ describe('road-trip MCP tools', () => { it('MCP-ROADTRIP-014: both via tools say that a via on the drive into a booked night is kept and not used', async () => { // The planner refuses that click with a sentence; a tool stores the via as asked, so // the note on the tool is the only place the assistant can learn it changes nothing. - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const tools = (await h.client.listTools()).tools; for (const name of ['add_route_via', 'add_route_vias']) { diff --git a/server/tests/unit/mcp/tools-settings.test.ts b/server/tests/unit/mcp/tools-settings.test.ts index 84562144b..7b9f6f804 100644 --- a/server/tests/unit/mcp/tools-settings.test.ts +++ b/server/tests/unit/mcp/tools-settings.test.ts @@ -9,7 +9,6 @@ * touch: a stored mapbox_access_token must not appear in a read, and neither * that key nor llm_api_key nor an unknown name may be written. */ -import { db as testDb } from '../../../src/db/database'; import { AppSettings } from '../../../src/db/entities/AppSettings.entity'; import { Settings } from '../../../src/db/entities/Settings.entity'; import { encrypt_api_key } from '../../../src/nest/common/crypto/apiKeyCrypto'; @@ -21,37 +20,31 @@ import { ENCRYPTED_SETTING_KEYS, MASKED_SETTING_KEYS, } from '../../../src/nest/settings/settings.service'; -import { createUser } from '../../helpers/factories'; import { countRows, findRow, insertRow } from '../../helpers/factories/rows'; import { setUserSetting } from '../../helpers/factories/settings'; +import { makeUser } from '../../helpers/factories/users'; import type { FakeRealtimeService } from '../../helpers/fake-realtime'; import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness'; +import { resetDatabase } from '../../helpers/reset-database'; import { bootTestApp } from '../../helpers/test-app'; -import { resetTestDb } from '../../helpers/test-db'; -import { createTestOrm, type TestOrm } from '../../helpers/test-orm'; +import { MikroORM } from '@mikro-orm/core'; import { MASKED_SETTING_VALUE } from '@trek/shared'; -import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; +import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; -vi.mock('../../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - -beforeEach(() => { - resetTestDb(testDb); +beforeEach(async () => { + await resetDatabase(orm); delete process.env.DEMO_MODE; }); -let orm: TestOrm; +let orm: MikroORM; beforeAll(async () => { - orm = await createTestOrm(testDb); + orm = (await bootTestApp()).get(MikroORM); }); afterAll(async () => { - await orm.close(); - testDb.close(); + await (await bootTestApp()).close(); }); // --------------------------------------------------------------------------- @@ -104,7 +97,7 @@ function errorText(result: any): string { describe('Tool: get_display_settings', () => { it('returns an empty object for a user who has set nothing', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_display_settings', arguments: {} }); const data = parseToolResult(result) as any; @@ -113,7 +106,7 @@ describe('Tool: get_display_settings', () => { }); it('returns the display preferences the user has stored', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await setSetting(user.id, 'temperature_unit', '"fahrenheit"'); await setSetting(user.id, 'distance_unit', '"imperial"'); await setSetting(user.id, 'time_format', '"12h"'); @@ -138,7 +131,7 @@ describe('Tool: get_display_settings', () => { }); it('falls back to the admin-set instance default for a key the user has not set', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await insertRow(orm, AppSettings, { key: 'default_user_setting_temperature_unit', value: '"fahrenheit"' }); await withHarness(user.id, async (h) => { @@ -149,7 +142,7 @@ describe('Tool: get_display_settings', () => { }); it('does not return stored credentials, not even to a settings:read token', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); // mapbox_access_token and carto_api_key are encrypted-but-NOT-masked, so // getUserSettings hands them back in cleartext. That is the leak this // surface must not have: store them the way the real write path does and @@ -178,7 +171,7 @@ describe('Tool: get_display_settings', () => { }); it('leaves out settings that are neither display preferences nor credentials', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await setSetting(user.id, 'map_tile_url', '"https://tiles.example/{z}/{x}/{y}.png"'); await setSetting(user.id, 'dashboard_fx_from', '"EUR"'); await setSetting(user.id, 'time_format', '"24h"'); @@ -191,8 +184,8 @@ describe('Tool: get_display_settings', () => { }); it("does not leak another user's preferences", async () => { - const { user: mine } = createUser(testDb); - const { user: theirs } = createUser(testDb); + const { user: mine } = await makeUser(orm); + const { user: theirs } = await makeUser(orm); await setSetting(theirs.id, 'temperature_unit', '"fahrenheit"'); await withHarness(mine.id, async (h) => { @@ -209,7 +202,7 @@ describe('Tool: get_display_settings', () => { describe('Tool: update_display_settings', () => { it('writes several preferences at once and reports what changed', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { temperature_unit: 'fahrenheit', distance_unit: 'imperial' }); const data = parseToolResult(result) as any; @@ -222,7 +215,7 @@ describe('Tool: update_display_settings', () => { }); it('leaves untouched keys alone', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await setSetting(user.id, 'time_format', '"12h"'); await withHarness(user.id, async (h) => { await update(h, { temperature_unit: 'celsius' }); @@ -231,7 +224,7 @@ describe('Tool: update_display_settings', () => { }); it('accepts every non-boolean display preference in one call', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { start_page: 'active_trip', @@ -252,7 +245,7 @@ describe('Tool: update_display_settings', () => { }); it('writes place_language, clears it with "", and refuses a language TREK does not offer (#1799)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const set = parseToolResult(await update(h, { place_language: 'en' })) as any; expect(set.settings.place_language).toBe('en'); @@ -265,7 +258,7 @@ describe('Tool: update_display_settings', () => { }); it('writes date_format and refuses a format the app does not offer (#2971)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const ok = parseToolResult(await update(h, { date_format: 'DD.MM.YYYY' })) as { settings: Record; @@ -278,7 +271,7 @@ describe('Tool: update_display_settings', () => { }); it('writes week_start and refuses a day outside the three the app offers (#2029)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const ok = parseToolResult(await update(h, { week_start: 'sunday' })) as any; expect(ok.settings.week_start).toBe('sunday'); @@ -289,7 +282,7 @@ describe('Tool: update_display_settings', () => { }); it('writes dashboard_timezones as the stored list and reads it back (#3044)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const data = parseToolResult(await update(h, { dashboard_timezones: ['Europe/Madrid', 'Africa/Cairo'] })) as { settings: Record; @@ -300,7 +293,7 @@ describe('Tool: update_display_settings', () => { }); it('accepts an empty dashboard_timezones list, which shows no clocks (#3044)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const data = parseToolResult(await update(h, { dashboard_timezones: [] })) as { settings: Record; @@ -310,7 +303,7 @@ describe('Tool: update_display_settings', () => { }); it('refuses dashboard_timezones that are unknown, repeated, not a list or too many (#3044)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const tooMany = Intl.supportedValuesOf('timeZone').slice(0, MAX_DASHBOARD_TIMEZONES + 1); await withHarness(user.id, async (h) => { const unknown = await update(h, { dashboard_timezones: ['Europe/Madrid', 'Mars/Olympus_Mons'] }); @@ -327,7 +320,7 @@ describe('Tool: update_display_settings', () => { }); it('accepts every boolean display preference', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { map_booking_labels: true, @@ -349,7 +342,7 @@ describe('Tool: update_display_settings', () => { }); it('accepts a boolean dark_mode, the pre-auto form still sitting in older rows', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { dark_mode: false }); const data = parseToolResult(result) as any; @@ -359,7 +352,7 @@ describe('Tool: update_display_settings', () => { }); it("accepts an empty default_currency, which falls back to each trip's own", async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await setSetting(user.id, 'default_currency', '"USD"'); await withHarness(user.id, async (h) => { const result = await update(h, { default_currency: '' }); @@ -370,7 +363,7 @@ describe('Tool: update_display_settings', () => { }); it('reads back the admin default rather than echoing the input', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await insertRow(orm, AppSettings, { key: 'default_user_setting_distance_unit', value: '"imperial"' }); await withHarness(user.id, async (h) => { const result = await update(h, { temperature_unit: 'celsius' }); @@ -386,7 +379,7 @@ describe('Tool: update_display_settings', () => { describe('Tool: update_display_settings, refusals', () => { it('refuses a credential key and stores nothing', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { mapbox_access_token: 'pk.attacker-token' }); expect(result.isError).toBe(true); @@ -396,7 +389,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses llm_api_key and stores nothing, even for a settings:write token', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withScopedHarness(user.id, ['settings:write'], async (h) => { const result = await update(h, { llm_api_key: 'sk-attacker' }); expect(result.isError).toBe(true); @@ -405,7 +398,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses the two admin-only LLM endpoint keys the REST route answers 403 for', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { for (const settings of [{ llm_base_url: 'http://127.0.0.1:11434' }, { llm_provider: 'local' }]) { const result = await update(h, settings); @@ -416,7 +409,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses a key nobody has heard of rather than storing it', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { made_up_preference: 'whatever' }); expect(result.isError).toBe(true); @@ -427,7 +420,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses an inherited Object property name, which a plain `in` check would wave through', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { toString: 'gotcha' }); expect(result.isError).toBe(true); @@ -436,7 +429,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('writes nothing at all when one key of a multi-key call is refused', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { temperature_unit: 'fahrenheit', llm_api_key: 'sk-attacker' }); expect(result.isError).toBe(true); @@ -445,7 +438,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('writes nothing when one value of a multi-key call is invalid', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { temperature_unit: 'fahrenheit', time_format: '36h' }); expect(result.isError).toBe(true); @@ -454,7 +447,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses a value outside the allowed set and names the key', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { temperature_unit: 'kelvin' }); expect(result.isError).toBe(true); @@ -464,7 +457,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses an unsupported language code', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const bad = await update(h, { language: 'xx' }); expect(bad.isError).toBe(true); @@ -475,7 +468,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses a currency that is not a three-letter uppercase code', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { for (const value of ['eur', 'EUROS', 'E']) { const result = await update(h, { default_currency: value }); @@ -486,7 +479,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses a non-boolean for a boolean preference', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { blur_booking_codes: 'yes' }); expect(result.isError).toBe(true); @@ -495,7 +488,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses a start_trip_tab that is empty or absurdly long', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { expect((await update(h, { start_trip_tab: '' })).isError).toBe(true); expect((await update(h, { start_trip_tab: 'x'.repeat(65) })).isError).toBe(true); @@ -504,7 +497,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses the redaction placeholder instead of silently skipping it', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, { start_trip_tab: MASKED_SETTING_VALUE }); expect(result.isError).toBe(true); @@ -514,7 +507,7 @@ describe('Tool: update_display_settings, refusals', () => { }); it('refuses an empty settings object and says what it takes', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); await withHarness(user.id, async (h) => { const result = await update(h, {}); expect(result.isError).toBe(true); @@ -525,7 +518,7 @@ describe('Tool: update_display_settings, refusals', () => { it('blocks the demo user', 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 update(h, { temperature_unit: 'fahrenheit' }); expect(result.isError).toBe(true); @@ -535,7 +528,7 @@ describe('Tool: update_display_settings, refusals', () => { it('lets the demo user read', async () => { process.env.DEMO_MODE = 'true'; - const { user } = createUser(testDb, { email: 'demo@trek.app' }); + const { user } = await makeUser(orm, { email: 'demo@trek.app' }); await setSetting(user.id, 'temperature_unit', '"celsius"'); await withHarness(user.id, async (h) => { const result = await h.client.callTool({ name: 'get_display_settings', arguments: {} }); @@ -601,28 +594,28 @@ describe('Settings tools, scope gating', () => { } it('registers both tools with null scopes (full access)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, null); expect(names).toContain('get_display_settings'); expect(names).toContain('update_display_settings'); }); it('registers only the read tool with settings:read', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['settings:read']); expect(names).toContain('get_display_settings'); expect(names).not.toContain('update_display_settings'); }); it('registers both with settings:write, since write implies read', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['settings:write']); expect(names).toContain('get_display_settings'); expect(names).toContain('update_display_settings'); }); it('registers neither for a token without a settings scope', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const names = await listToolNames(user.id, ['trips:read']); expect(names).not.toContain('get_display_settings'); expect(names).not.toContain('update_display_settings'); @@ -631,7 +624,7 @@ describe('Settings tools, scope gating', () => { describe('update_display_settings reaches every tab', () => { it('MCP-SETTINGS-001: tells every tab of the user which keys changed, never their values', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); // The fake the harness container was booted with, which records every user message. const realtime = (await bootTestApp()).get(RealtimeService) as FakeRealtimeService; realtime.broadcastToUserMock.mockClear(); @@ -644,7 +637,7 @@ describe('update_display_settings reaches every tab', () => { }); it('MCP-SETTINGS-002: a refused change sends nothing', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const realtime = (await bootTestApp()).get(RealtimeService) as FakeRealtimeService; realtime.broadcastToUserMock.mockClear(); await withHarness(user.id, async (h) => {