Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

51.8 KB
Raw Blame
/**
 * Unit tests for the trip MCP surface (TripsMcp, DI-discovered): create_trip,
 * update_trip, delete_trip, list_trips, get_trip_summary tools, the
 * trek://trips / trek://trips/{tripId} resources (moved here from
 * resources.test.ts with the DI port — the withTools harness attaches the
 * nest-mcp registry that now registers them), the fire-once deprecation
 * notice riding the attach ctx, and the scope gating (declarative
 * trips:write markers + the canReadTrips/canDeleteTrips predicates).
 */
import { Days } from '../../../src/db/entities/Days.entity';
import { RoadtripDayBoundaries } from '../../../src/db/entities/RoadtripDayBoundaries.entity';
import { Trips } from '../../../src/db/entities/Trips.entity';
import { VacayEntries } from '../../../src/db/entities/VacayEntries.entity';
import { VacayUserYears } from '../../../src/db/entities/VacayUserYears.entity';
import { VacayYears } from '../../../src/db/entities/VacayYears.entity';
import { invalidatePermissionsCache } from '../../../src/nest/permissions/permissions-cache';
import { makeBudgetItem } from '../../helpers/factories/budget';
import { makeCollabNote } from '../../helpers/factories/collab';
import { makeDayAccommodation, makeDayAssignment, makeDayNote } from '../../helpers/factories/itinerary';
import { makePackingItem } from '../../helpers/factories/packing';
import { makePlace } from '../../helpers/factories/places';
import { makeReservation } from '../../helpers/factories/reservations';
import { countRows, findRow, findRows, insertRow, insertRows, updateRows } from '../../helpers/factories/rows';
import { setAppSetting, setUserSetting } from '../../helpers/factories/settings';
import { addTripMember, makeDay, makeTrip } from '../../helpers/factories/trips';
import { makeUser } from '../../helpers/factories/users';
import { makeVacayEntry, makeVacayPlan, addVacayPlanMember } from '../../helpers/factories/vacay';
import { FakeRealtimeService } from '../../helpers/fake-realtime';
import { createMcpHarness, parseToolResult, parseResourceResult, type McpHarness } from '../../helpers/mcp-harness';
import { resetDatabase } from '../../helpers/reset-database';
import { bootTestApp } from '../../helpers/test-app';
import { MikroORM } from '@mikro-orm/core';
import { MAX_TRIP_DAYS } from '@trek/shared';

import { describe, it, expect, vi, beforeAll, beforeEach, afterEach, afterAll } from 'vitest';

const realtime = new FakeRealtimeService();
const broadcastMock = realtime.broadcastMock;

let orm: MikroORM;

beforeAll(async () => {
  orm = (await bootTestApp(realtime)).get(MikroORM);
});

beforeEach(async () => {
  await resetDatabase(orm);
  broadcastMock.mockClear();
  delete process.env.DEMO_MODE;
});

// Only search_cover_images stubs fetch, and it stubs a different response per case.
afterEach(() => {
  vi.unstubAllGlobals();
});

afterAll(async () => {
  await (await bootTestApp(realtime)).close();
});

async function tripRow(id: number) {
  return (await findRow(orm, Trips, { id }))!;
}

/** The vacay entry dates of the user in the plan between `from` and `to`, both included, in date order. */
async function vacayDatesBetween(planId: number, userId: number, from: string, to: string): Promise<string[]> {
  const rows = await findRows(
    orm,
    VacayEntries,
    { plan: planId, user: userId, date: { $gte: from, $lte: to } },
    { date: 'asc' },
  );
  return rows.map((r) => r.date);
}

/** A vacay plan for the user with the year 2026 open and 30 days in it. */
async function vacayPlanWithYear(userId: number): Promise<number> {
  const { id: planId } = await makeVacayPlan(orm, userId);
  await insertRow(orm, VacayYears, { plan: planId, year: 2026 });
  await insertRow(orm, VacayUserYears, { user: userId, plan: planId, year: 2026, vacation_days: 30, carried_over: 0 });
  return planId;
}

async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>) {
  const h = await createMcpHarness({ realtime, userId, withResources: false });
  try {
    await fn(h);
  } finally {
    await h.cleanup();
  }
}

// ---------------------------------------------------------------------------
// create_trip
// ---------------------------------------------------------------------------

describe('Tool: create_trip', () => {
  it('creates a trip with title only and generates 7 default days', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'create_trip', arguments: { title: 'Summer Escape' } });
      const data = parseToolResult(result) as any;
      expect(data.trip).toBeTruthy();
      expect(data.trip.title).toBe('Summer Escape');
      const days = await countRows(orm, Days, { trip: data.trip.id });
      expect(days).toBe(7);
    });
  });

  it('creates a trip with dates and auto-generates correct number of days', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'create_trip',
        arguments: { title: 'Paris Trip', start_date: '2026-07-01', end_date: '2026-07-05' },
      });
      const data = parseToolResult(result) as any;
      const days = await countRows(orm, Days, { trip: data.trip.id });
      expect(days).toBe(5);
    });
  });

  it('generates every day of a trip longer than a year (#2403)', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'create_trip',
        arguments: { title: 'Long Trip', start_date: '2025-01-26', end_date: '2026-01-28' },
      });
      const data = parseToolResult(result) as any;
      const days = await countRows(orm, Days, { trip: data.trip.id });
      expect(days).toBe(368);
    });
  });

  it('refuses a date range longer than MAX_TRIP_DAYS instead of cutting the days short', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'create_trip',
        arguments: { title: 'Decade', start_date: '2026-01-01', end_date: '2036-01-01' },
      });
      expect(result.isError).toBe(true);
      expect(JSON.stringify(result.content)).toContain(`at most ${MAX_TRIP_DAYS} days`);
      expect(await countRows(orm, Trips)).toBe(0);
    });
  });

  it('returns error for invalid start_date format', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'create_trip',
        arguments: { title: 'Trip', start_date: 'not-a-date' },
      });
      expect(result.isError).toBe(true);
    });
  });

  it('returns error when end_date is before start_date', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'create_trip',
        arguments: { title: 'Trip', start_date: '2026-07-05', end_date: '2026-07-01' },
      });
      expect(result.isError).toBe(true);
    });
  });

  it('blocks demo user', async () => {
    process.env.DEMO_MODE = 'true';
    const { user } = await makeUser(orm, { email: '[email protected]' });
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'create_trip', arguments: { title: 'Demo Trip' } });
      expect(result.isError).toBe(true);
    });
  });

  it('gives a dateless trip the requested day_count instead of the default 7', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'create_trip',
        arguments: { title: 'Open Ended', day_count: 12 },
      });
      const data = parseToolResult(result) as any;
      const days = await countRows(orm, Days, { trip: data.trip.id });
      expect(days).toBe(12);
      const row = await tripRow(data.trip.id);
      expect(row.start_date).toBeNull();
      expect(row.end_date).toBeNull();
    });
  });

  it('refuses a day_count outside 1..MAX_TRIP_DAYS', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      expect(
        (await h.client.callTool({ name: 'create_trip', arguments: { title: 'Zero', day_count: 0 } })).isError,
      ).toBe(true);
      expect(
        (await h.client.callTool({ name: 'create_trip', arguments: { title: 'Huge', day_count: MAX_TRIP_DAYS + 1 } }))
          .isError,
      ).toBe(true);
      expect(await countRows(orm, Trips)).toBe(0);
    });
  });

  it('stores the requested reminder_days', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'create_trip',
        arguments: { title: 'Reminded', reminder_days: 14 },
      });
      const data = parseToolResult(result) as any;
      const row = await tripRow(data.trip.id);
      expect(row.reminder_days).toBe(14);
    });
  });

  it('turns the reminder off with reminder_days 0 rather than falling back to 3', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'create_trip', arguments: { title: 'Quiet', reminder_days: 0 } });
      const data = parseToolResult(result) as any;
      const row = await tripRow(data.trip.id);
      expect(row.reminder_days).toBe(0);
    });
  });

  it('refuses a reminder_days outside 0..30', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      expect(
        (await h.client.callTool({ name: 'create_trip', arguments: { title: 'Early', reminder_days: 31 } })).isError,
      ).toBe(true);
      expect(
        (await h.client.callTool({ name: 'create_trip', arguments: { title: 'Negative', reminder_days: -1 } })).isError,
      ).toBe(true);
      expect(await countRows(orm, Trips)).toBe(0);
    });
  });

  it('gives a trip without a currency the display currency from the settings, not EUR', async () => {
    const { user } = await makeUser(orm);
    await setUserSetting(orm, user.id, 'default_currency', JSON.stringify('USD'));
    await withHarness(user.id, async (h) => {
      const fromSettings = parseToolResult(
        await h.client.callTool({ name: 'create_trip', arguments: { title: 'Road trip' } }),
      ) as any;
      expect(fromSettings.trip.currency).toBe('USD');
      const explicit = parseToolResult(
        await h.client.callTool({ name: 'create_trip', arguments: { title: 'Tokyo', currency: 'JPY' } }),
      ) as any;
      expect(explicit.trip.currency).toBe('JPY');
    });
  });

  it('falls back to EUR when neither the user nor the admin set a display currency', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const data = parseToolResult(
        await h.client.callTool({ name: 'create_trip', arguments: { title: 'Plain' } }),
      ) as any;
      expect(data.trip.currency).toBe('EUR');
    });
  });
});

// ---------------------------------------------------------------------------
// update_trip
// ---------------------------------------------------------------------------

describe('Tool: update_trip', () => {
  it('updates trip title', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { title: 'Old Title' });
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, title: 'New Title' },
      });
      const data = parseToolResult(result) as any;
      expect(data.trip.title).toBe('New Title');
    });
  });

  it('partial update preserves unspecified fields', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { title: 'My Trip', description: 'A great trip' });
    await withHarness(user.id, async (h) => {
      await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, title: 'Renamed' } });
      const updated = await tripRow(trip.id);
      expect(updated.title).toBe('Renamed');
      expect(updated.description).toBe('A great trip');
    });
  });

  it('broadcasts trip:updated event', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id);
    await withHarness(user.id, async (h) => {
      await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, title: 'Updated' } });
      expect(broadcastMock).toHaveBeenCalledWith(trip.id, 'trip:updated', expect.any(Object));
    });
  });

  it('a day added before the start moves the road trip day boundaries with their days and announces them (#3119)', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { start_date: '2026-05-10', end_date: '2026-05-12' });
    const day2 = await findRow(orm, Days, { trip: trip.id, day_number: 2 });
    if (!day2) throw new Error('the trip has no second day');
    const place = await makePlace(orm, trip.id, { name: 'Viewpoint' });
    const stop = await makeDayAssignment(orm, day2.id, place.id);
    await insertRow(orm, RoadtripDayBoundaries, {
      trip: trip.id,
      day_number: 2,
      fromAssignment: stop.id,
      fraction: 0.5,
    });
    await withHarness(user.id, async (h) => {
      await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, start_date: '2026-05-09', end_date: '2026-05-12' },
      });
    });
    const boundaries = await findRows(orm, RoadtripDayBoundaries, { trip: trip.id });
    expect(boundaries.map((b) => b.day_number)).toEqual([3]);
    expect(broadcastMock).toHaveBeenCalledWith(
      trip.id,
      'roadtripBoundary:changed',
      expect.objectContaining({
        boundaries: [expect.objectContaining({ day_number: 3, from_assignment_id: stop.id })],
      }),
    );
  });

  it('returns access denied for non-member', async () => {
    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_trip', arguments: { tripId: trip.id, title: 'Hack' } });
      expect(result.isError).toBe(true);
    });
  });

  it('blocks demo user', async () => {
    process.env.DEMO_MODE = 'true';
    const { user } = await makeUser(orm, { email: '[email protected]' });
    const trip = await makeTrip(orm, user.id);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, title: 'New' } });
      expect(result.isError).toBe(true);
    });
  });

  it('shifts owner vacay entries when update_trip moves trip window by fixed offset', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { start_date: '2026-08-01', end_date: '2026-08-09' });

    // Materialize active vacay plan for owner and entries in old trip window.
    const planId = await vacayPlanWithYear(user.id);
    for (const d of ['2026-08-03', '2026-08-04', '2026-08-05', '2026-08-06', '2026-08-07']) {
      await makeVacayEntry(orm, planId, user.id, d, { note: '' });
    }

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, start_date: '2026-08-08', end_date: '2026-08-16' },
      });
      const data = parseToolResult(result) as any;
      expect(data.trip.start_date).toBe('2026-08-08');
      expect(data.trip.end_date).toBe('2026-08-16');
    });

    const oldWindow = await vacayDatesBetween(planId, user.id, '2026-08-01', '2026-08-09');
    expect(oldWindow).toHaveLength(0);

    const shifted = await vacayDatesBetween(planId, user.id, '2026-08-08', '2026-08-16');
    expect(shifted).toEqual(['2026-08-10', '2026-08-11', '2026-08-12', '2026-08-13', '2026-08-14']);
  });

  it('shifts the owner entries in the plan they are active in, a shared plan they joined included (#2604)', async () => {
    const { user } = await makeUser(orm);
    const { user: otherOwner } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { start_date: '2026-09-01', end_date: '2026-09-07' });

    // Joining a plan moves the owner's days into it, so that is where they live.
    const { id: sharedPlanId } = await makeVacayPlan(orm, otherOwner.id);
    await addVacayPlanMember(orm, sharedPlanId, user.id, 'accepted');
    for (const d of ['2026-09-02', '2026-09-03']) {
      await makeVacayEntry(orm, sharedPlanId, user.id, d, { note: '' });
    }

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, start_date: '2026-09-08', end_date: '2026-09-14' },
      });
      expect(result.isError).toBeFalsy();
    });

    const oldWindow = await vacayDatesBetween(sharedPlanId, user.id, '2026-09-01', '2026-09-07');
    expect(oldWindow).toHaveLength(0);

    const shifted = await vacayDatesBetween(sharedPlanId, user.id, '2026-09-08', '2026-09-14');
    expect(shifted).toEqual(['2026-09-09', '2026-09-10']);
  });

  it('clear_dates turns a dated trip back into a dateless one and un-dates its days', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { start_date: '2026-07-01', end_date: '2026-07-05' });
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, clear_dates: true },
      });
      expect(result.isError).toBeFalsy();
      const row = await tripRow(trip.id);
      expect(row.start_date).toBeNull();
      expect(row.end_date).toBeNull();
      const dated = await countRows(orm, Days, { trip: trip.id, date: { $ne: null } });
      expect(dated).toBe(0);
    });
  });

  it('clear_dates with a day_count resizes the now dateless trip', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { start_date: '2026-07-01', end_date: '2026-07-05' });
    await withHarness(user.id, async (h) => {
      await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, clear_dates: true, day_count: 3 } });
      const days = await countRows(orm, Days, { trip: trip.id });
      expect(days).toBe(3);
    });
  });

  it('refuses clear_dates alongside a start_date and leaves the trip untouched', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { start_date: '2026-07-01', end_date: '2026-07-05' });
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, clear_dates: true, start_date: '2026-08-01' },
      });
      expect(result.isError).toBe(true);
      const row = await tripRow(trip.id);
      expect(row.start_date).toBe('2026-07-01');
    });
  });

  it('resizes a dateless trip with day_count', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id);
    await insertRows(orm, Days, [
      { trip: trip.id, day_number: 1, date: null },
      { trip: trip.id, day_number: 2, date: null },
    ]);
    await withHarness(user.id, async (h) => {
      await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, day_count: 6 } });
      const days = await countRows(orm, Days, { trip: trip.id });
      expect(days).toBe(6);
    });
  });

  it('refuses a day_count outside 1..MAX_TRIP_DAYS', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { title: 'Untouched' });
    await withHarness(user.id, async (h) => {
      expect(
        (await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, day_count: 0 } })).isError,
      ).toBe(true);
      expect(
        (await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, day_count: MAX_TRIP_DAYS + 1 } }))
          .isError,
      ).toBe(true);
      expect(await countRows(orm, Days, { trip: trip.id })).toBe(0);
    });
  });

  it('says in its description what shortening a dated trip takes, and where the result lists it', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const tool = (await h.client.listTools()).tools.find((t) => t.name === 'update_trip');
      expect(tool?.description).toContain('Shortening a dated trip deletes its last days by position');
      expect(tool?.description).toContain('removed_days');
    });
  });

  it('lists the days an earlier end removed, as they stood before, and none for a rename', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { start_date: '2026-07-01', end_date: '2026-07-05' });
    const days = await findRows(orm, Days, { trip: trip.id }, { day_number: 'asc' });
    await withHarness(user.id, async (h) => {
      type Answer = { trip: { end_date: string; title: string }; removed_days?: unknown[] };
      const shortened = parseToolResult(
        await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, end_date: '2026-07-03' } }),
      ) as Answer;
      expect(shortened.trip.end_date).toBe('2026-07-03');
      expect(shortened.removed_days).toEqual([
        { id: days[3].id, day_number: 4, date: '2026-07-04', reason: 'overflow' },
        { id: days[4].id, day_number: 5, date: '2026-07-05', reason: 'overflow' },
      ]);
      const renamed = parseToolResult(
        await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, title: 'Shorter' } }),
      ) as Answer;
      expect(renamed.trip.title).toBe('Shorter');
      expect(renamed).not.toHaveProperty('removed_days');
    });
  });

  it('refuses a date range longer than MAX_TRIP_DAYS and leaves the trip untouched', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { title: 'Untouched', start_date: '2026-07-01', end_date: '2026-07-07' });
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, end_date: '2036-07-01' },
      });
      expect(result.isError).toBe(true);
      expect(JSON.stringify(result.content)).toContain(`at most ${MAX_TRIP_DAYS} days`);
      const row = await tripRow(trip.id);
      expect(row.end_date).toBe('2026-07-07');
    });
  });

  it('changes reminder_days and can turn the reminder off', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id);
    await withHarness(user.id, async (h) => {
      await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, reminder_days: 10 } });
      expect((await tripRow(trip.id)).reminder_days).toBe(10);
      await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, reminder_days: 0 } });
      expect((await tripRow(trip.id)).reminder_days).toBe(0);
    });
  });

  it('refuses a reminder_days outside 0..30 and keeps the stored value', async () => {
    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_trip',
        arguments: { tripId: trip.id, reminder_days: 31 },
      });
      expect(result.isError).toBe(true);
      expect((await tripRow(trip.id)).reminder_days).toBe(3);
    });
  });

  it('clears the description with an explicit null', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { description: 'A great trip' });
    await withHarness(user.id, async (h) => {
      await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, description: null } });
      const row = await tripRow(trip.id);
      expect(row.description).toBeNull();
    });
  });

  it('clears the cover image with an explicit null', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id);
    await updateRows(orm, Trips, { id: trip.id }, { cover_image: '/uploads/covers/old.jpg' });
    await withHarness(user.id, async (h) => {
      await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, cover_image: null } });
      const row = await tripRow(trip.id);
      expect(row.cover_image).toBeNull();
    });
  });
});

// ---------------------------------------------------------------------------
// search_cover_images
// ---------------------------------------------------------------------------

describe('Tool: search_cover_images', () => {
  function stubUnsplash(body: unknown, init: { ok?: boolean; status?: number } = {}) {
    vi.stubGlobal(
      'fetch',
      vi.fn(async () => ({
        ok: init.ok ?? true,
        status: init.status ?? 200,
        json: async () => body,
      })),
    );
  }

  it('returns the photo candidates for a query', async () => {
    const { user } = await makeUser(orm);
    stubUnsplash({
      results: [
        {
          id: 'p1',
          urls: {
            regular: 'https://images.unsplash.com/photo-1.jpg',
            small: 'https://images.unsplash.com/thumb-1.jpg',
          },
          alt_description: 'Rooftops at sunset',
          user: { name: 'Ada L.' },
          links: { html: 'https://unsplash.com/photos/p1' },
        },
      ],
    });
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'search_cover_images', arguments: { query: 'Lisbon rooftops' } });
      const data = parseToolResult(result) as any;
      expect(data.photos).toHaveLength(1);
      expect(data.photos[0].url).toBe('https://images.unsplash.com/photo-1.jpg');
      expect(data.photos[0].photographer).toBe('Ada L.');
    });
  });

  it('reports an upstream failure as a tool error', async () => {
    const { user } = await makeUser(orm);
    stubUnsplash({ errors: ['Rate Limit Exceeded'] }, { ok: false, status: 429 });
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'search_cover_images', arguments: { query: 'Lisbon' } });
      expect(result.isError).toBe(true);
      expect((result.content as { text: string }[])[0].text).toBe('Rate Limit Exceeded');
    });
  });

  it('refuses an empty query without reaching Unsplash', async () => {
    const { user } = await makeUser(orm);
    const fetchMock = vi.fn();
    vi.stubGlobal('fetch', fetchMock);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'search_cover_images', arguments: { query: '' } });
      expect(result.isError).toBe(true);
      expect(fetchMock).not.toHaveBeenCalled();
    });
  });

  it('saves nothing on the trip', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id);
    stubUnsplash({ results: [] });
    await withHarness(user.id, async (h) => {
      await h.client.callTool({ name: 'search_cover_images', arguments: { query: 'Lisbon' } });
      const row = await tripRow(trip.id);
      expect(row.cover_image).toBeNull();
    });
  });
});

// ---------------------------------------------------------------------------
// delete_trip
// ---------------------------------------------------------------------------

describe('Tool: delete_trip', () => {
  it('owner can delete trip', async () => {
    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_trip', arguments: { tripId: trip.id } });
      const data = parseToolResult(result) as any;
      expect(data.success).toBe(true);
      const gone = await findRow(orm, Trips, { id: trip.id });
      expect(gone).toBeNull();
    });
  });

  it('non-owner member cannot delete trip', async () => {
    const { user } = await makeUser(orm);
    const { user: owner } = await makeUser(orm);
    const trip = await makeTrip(orm, owner.id);
    await addTripMember(orm, trip.id, user.id);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'delete_trip', arguments: { tripId: trip.id } });
      expect(result.isError).toBe(true);
      const stillExists = await findRow(orm, Trips, { id: trip.id });
      expect(stillExists).toBeTruthy();
    });
  });

  it('blocks demo user', async () => {
    process.env.DEMO_MODE = 'true';
    const { user } = await makeUser(orm, { email: '[email protected]' });
    const trip = await makeTrip(orm, user.id);
    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'delete_trip', arguments: { tripId: trip.id } });
      expect(result.isError).toBe(true);
    });
  });
});

// ---------------------------------------------------------------------------
// list_trips
// ---------------------------------------------------------------------------

describe('Tool: list_trips', () => {
  it('returns owned and member trips', async () => {
    const { user } = await makeUser(orm);
    const { user: other } = await makeUser(orm);
    await makeTrip(orm, user.id, { title: 'My Trip' });
    const shared = await makeTrip(orm, other.id, { title: 'Shared' });
    await addTripMember(orm, shared.id, user.id);
    await makeTrip(orm, other.id, { title: 'Inaccessible' });

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'list_trips', arguments: {} });
      const data = parseToolResult(result) as any;
      expect(data.trips).toHaveLength(2);
      const titles = data.trips.map((t: any) => t.title);
      expect(titles).toContain('My Trip');
      expect(titles).toContain('Shared');
    });
  });

  it('excludes archived trips by default', async () => {
    const { user } = await makeUser(orm);
    await makeTrip(orm, user.id, { title: 'Active' });
    const archived = await makeTrip(orm, user.id, { title: 'Archived' });
    await updateRows(orm, Trips, { id: archived.id }, { is_archived: 1 });

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'list_trips', arguments: {} });
      const data = parseToolResult(result) as any;
      expect(data.trips).toHaveLength(1);
      expect(data.trips[0].title).toBe('Active');
    });
  });

  it('includes archived trips when include_archived is true', async () => {
    const { user } = await makeUser(orm);
    await makeTrip(orm, user.id, { title: 'Active' });
    const archived = await makeTrip(orm, user.id, { title: 'Archived' });
    await updateRows(orm, Trips, { id: archived.id }, { is_archived: 1 });

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'list_trips', arguments: { include_archived: true } });
      const data = parseToolResult(result) as any;
      expect(data.trips).toHaveLength(2);
    });
  });
});

// ---------------------------------------------------------------------------
// get_trip_summary
// ---------------------------------------------------------------------------

describe('Tool: get_trip_summary', () => {
  it('returns full denormalized trip snapshot', async () => {
    const { user } = await makeUser(orm);
    const { user: member } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { title: 'Full Trip' });
    await addTripMember(orm, trip.id, member.id);
    const day = await makeDay(orm, trip.id);
    const place = await makePlace(orm, trip.id, { name: 'Colosseum' });
    const assignment = await makeDayAssignment(orm, day.id, place.id);
    await makeDayNote(orm, day.id, trip.id, { text: 'Check in' });
    await makeBudgetItem(orm, trip.id, { name: 'Hotel', total_price: 300 });
    await makePackingItem(orm, trip.id, { name: 'Passport' });
    await makeReservation(orm, trip.id, { title: 'Flight', type: 'flight' });
    await makeCollabNote(orm, trip.id, user.id, { title: 'Plan' });

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      const data = parseToolResult(result) as any;
      expect(data.trip.title).toBe('Full Trip');
      expect(data.members.owner.id).toBe(user.id);
      expect(data.members.collaborators).toHaveLength(1);
      expect(data.days).toHaveLength(1);
      expect(data.days[0].assignments).toHaveLength(1);
      expect(data.days[0].notes).toHaveLength(1);
      expect(data.budget.item_count).toBe(1);
      expect(data.budget.total).toBe(300);
      expect(data.packing.total).toBe(1);
      expect(data.reservations).toHaveLength(1);
      expect(data.collab_notes).toHaveLength(1);
    });
  });

  it('returns access denied for non-member', async () => {
    const { user } = await makeUser(orm);
    const { user: other } = await makeUser(orm);
    const trip = await makeTrip(orm, other.id);

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      expect(result.isError).toBe(true);
    });
  });

  it('is not blocked for demo user (read-only tool)', async () => {
    process.env.DEMO_MODE = 'true';
    const { user } = await makeUser(orm, { email: '[email protected]' });
    const trip = await makeTrip(orm, user.id, { title: 'Demo Trip' });

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      expect(result.isError).toBeFalsy();
      const data = parseToolResult(result) as any;
      expect(data.trip.title).toBe('Demo Trip');
    });
  });

  it('includes todos, files, pollCount, messageCount in response', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { title: 'Summary Test' });

    await withHarness(user.id, async (h) => {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      const data = parseToolResult(result) as any;
      expect(Array.isArray(data.todos)).toBe(true);
      expect(typeof data.pollCount).toBe('number');
      expect(typeof data.messageCount).toBe('number');
    });
  });

  // Regression — GHSA-qvw8-w937-vcmq: a token WITHOUT trips:read must not receive
  // member emails, the itinerary, or accommodations. The tool stays registered for
  // navigation but only surfaces the trip id + title.
  it('withholds members, days and accommodations from a token without trips:read', async () => {
    const { user } = await makeUser(orm, { email: '[email protected]' });
    const { user: member } = await makeUser(orm, { email: '[email protected]' });
    const trip = await makeTrip(orm, user.id, { title: 'Confidential Trip' });
    await addTripMember(orm, trip.id, member.id);
    const day = await makeDay(orm, trip.id);
    const place = await makePlace(orm, trip.id, { name: 'Colosseum', lat: 41.89, lng: 12.49 });
    await makeDayAssignment(orm, day.id, place.id);
    await makeDayAccommodation(orm, trip.id, place.id, day.id, day.id);

    const h = await createMcpHarness({ realtime, userId: user.id, withResources: false, scopes: ['weather:read'] });
    try {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      const data = parseToolResult(result) as any;
      // Navigation still works…
      expect(data.trip.id).toBe(trip.id);
      expect(data.trip.title).toBe('Confidential Trip');
      // …but the confidential core bucket is withheld.
      expect(data.members).toBeUndefined();
      expect(data.days).toBeUndefined();
      expect(data.accommodations).toBeUndefined();
      // No member email must leak anywhere in the payload.
      expect(JSON.stringify(data)).not.toContain('[email protected]');
      expect(JSON.stringify(data)).not.toContain('[email protected]');
    } finally {
      await h.cleanup();
    }
  });

  it('returns the full core bucket for a token that has trips:read', async () => {
    const { user } = await makeUser(orm);
    const { user: member } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { title: 'Scoped Trip' });
    await addTripMember(orm, trip.id, member.id);
    await makeDay(orm, trip.id);

    const h = await createMcpHarness({ realtime, userId: user.id, withResources: false, scopes: ['trips:read'] });
    try {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      const data = parseToolResult(result) as any;
      expect(data.trip.title).toBe('Scoped Trip');
      expect(data.members.owner.id).toBe(user.id);
      expect(data.members.collaborators).toHaveLength(1);
      expect(data.days).toHaveLength(1);
    } finally {
      await h.cleanup();
    }
  });

  // Regression: get_trip_summary must hide another member's private packing items (#858).
  it("hides another member's private packing item from the summary", async () => {
    const { user: owner } = await makeUser(orm);
    const { user: member } = await makeUser(orm);
    const trip = await makeTrip(orm, owner.id, { title: 'Shared Trip' });
    await addTripMember(orm, trip.id, member.id);
    await makePackingItem(orm, trip.id, {
      name: 'Secret gift',
      category: 'Misc',
      checked: 0,
      is_private: 1,
      owner: owner.id,
    });
    await makePackingItem(orm, trip.id, {
      name: 'Sunscreen',
      category: 'Misc',
      checked: 0,
      is_private: 0,
      owner: owner.id,
    });

    await withHarness(member.id, async (h) => {
      const result = await h.client.callTool({ name: 'get_trip_summary', arguments: { tripId: trip.id } });
      const data = parseToolResult(result) as any;
      const names = (data.packing?.items || []).map((i: any) => i.name);
      expect(names).toContain('Sunscreen'); // common item visible
      expect(names).not.toContain('Secret gift'); // owner's private item hidden from the member
    });
  });
});

// ---------------------------------------------------------------------------
// Resources (moved from resources.test.ts with the DI port — the registry the
// withTools harness attaches now registers them)
// ---------------------------------------------------------------------------

describe('Resource: trek://trips', () => {
  it('returns all trips the user owns or is a member of', async () => {
    const { user } = await makeUser(orm);
    const { user: other } = await makeUser(orm);
    await makeTrip(orm, user.id, { title: 'My Trip' });
    const sharedTrip = await makeTrip(orm, other.id, { title: 'Shared Trip' });
    await addTripMember(orm, sharedTrip.id, user.id);
    // Trip from another user (not accessible)
    await makeTrip(orm, other.id, { title: 'Other Trip' });

    await withHarness(user.id, async (harness) => {
      const result = await harness.client.readResource({ uri: 'trek://trips' });
      const trips = parseResourceResult(result) as any[];
      expect(trips).toHaveLength(2);
      const titles = trips.map((t) => t.title);
      expect(titles).toContain('My Trip');
      expect(titles).toContain('Shared Trip');
      expect(titles).not.toContain('Other Trip');
    });
  });

  it('excludes archived trips', async () => {
    const { user } = await makeUser(orm);
    await makeTrip(orm, user.id, { title: 'Active Trip' });
    const archived = await makeTrip(orm, user.id, { title: 'Archived Trip' });
    await updateRows(orm, Trips, { id: archived.id }, { is_archived: 1 });

    await withHarness(user.id, async (harness) => {
      const result = await harness.client.readResource({ uri: 'trek://trips' });
      const trips = parseResourceResult(result) as any[];
      expect(trips).toHaveLength(1);
      expect(trips[0].title).toBe('Active Trip');
    });
  });

  it('returns empty array when user has no trips', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (harness) => {
      const result = await harness.client.readResource({ uri: 'trek://trips' });
      const trips = parseResourceResult(result) as any[];
      expect(trips).toEqual([]);
    });
  });
});

describe('Resource: trek://trips/{tripId}', () => {
  it('returns trip data for an accessible trip', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id, { title: 'Paris Trip' });

    await withHarness(user.id, async (harness) => {
      const result = await harness.client.readResource({ uri: `trek://trips/${trip.id}` });
      const data = parseResourceResult(result) as any;
      expect(data.title).toBe('Paris Trip');
      expect(data.id).toBe(trip.id);
    });
  });

  it('returns access denied for inaccessible trip', async () => {
    const { user } = await makeUser(orm);
    const { user: other } = await makeUser(orm);
    const otherTrip = await makeTrip(orm, other.id, { title: 'Private' });

    await withHarness(user.id, async (harness) => {
      const result = await harness.client.readResource({ uri: `trek://trips/${otherTrip.id}` });
      const data = parseResourceResult(result) as any;
      expect(data.error).toBeTruthy();
    });
  });

  it('returns access denied for non-existent ID', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (harness) => {
      const result = await harness.client.readResource({ uri: 'trek://trips/99999' });
      const data = parseResourceResult(result) as any;
      expect(data.error).toBeTruthy();
    });
  });
});

describe('Resource: trek://trips/{tripId}/members', () => {
  it('returns owner and collaborators', async () => {
    const { user } = await makeUser(orm);
    const { user: member } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id);
    await addTripMember(orm, trip.id, member.id);

    await withHarness(user.id, async (harness) => {
      const result = await harness.client.readResource({ uri: `trek://trips/${trip.id}/members` });
      const data = parseResourceResult(result) as any;
      expect(data.owner).toBeTruthy();
      expect(data.owner.id).toBe(user.id);
      expect(data.members).toHaveLength(1);
      expect(data.members[0].id).toBe(member.id);
    });
  });

  it('returns access denied for unauthorized trip', async () => {
    const { user } = await makeUser(orm);
    const { user: other } = await makeUser(orm);
    const trip = await makeTrip(orm, other.id);

    await withHarness(user.id, async (harness) => {
      const result = await harness.client.readResource({ uri: `trek://trips/${trip.id}/members` });
      const data = parseResourceResult(result) as any;
      expect(data.error).toBeTruthy();
    });
  });
});

// ---------------------------------------------------------------------------
// Deprecation notice (fire-once closure riding the registry attach ctx)
// ---------------------------------------------------------------------------

describe('static-token deprecation notice', () => {
  it('list_trips surfaces the notice exactly once and still carries the payload', async () => {
    const { user } = await makeUser(orm);
    await makeTrip(orm, user.id, { title: 'Noticed' });
    // Mirror the fire-once closure built per session in src/mcp/index.ts.
    let emitted = false;
    const getDeprecationNotice = () => {
      if (emitted) return null;
      emitted = true;
      return 'static tokens are deprecated';
    };
    const h = await createMcpHarness({
      realtime,
      userId: user.id,
      withResources: false,
      isStaticToken: true,
      getDeprecationNotice,
    });
    try {
      const first = await h.client.callTool({ name: 'list_trips', arguments: {} });
      expect(first.isError).toBe(true);
      const texts = (first.content as { type: string; text: string }[]).map((c) => c.text);
      expect(texts[0]).toContain('deprecated');
      expect(JSON.parse(texts[1]).trips[0].title).toBe('Noticed');
      // Second call: the closure already fired — plain payload, no error.
      const second = await h.client.callTool({ name: 'list_trips', arguments: {} });
      expect(second.isError).toBeFalsy();
      expect((parseToolResult(second) as any).trips).toHaveLength(1);
    } finally {
      await h.cleanup();
    }
  });
});

// ---------------------------------------------------------------------------
// Scope gating — declarative trips:write markers plus the predicate escape
// hatches (canReadTrips accepts trips:delete / trips:share; delete_trip rides
// trips:delete, the share tools trips:share)
// ---------------------------------------------------------------------------

describe('scope gating', () => {
  async function toolNames(scopes: string[] | null): Promise<string[]> {
    const h = await createMcpHarness({ realtime, userId: 1, withResources: false, scopes });
    try {
      const { tools } = await h.client.listTools();
      return tools.map((t) => t.name);
    } finally {
      await h.cleanup();
    }
  }

  const WRITE_TOOLS = [
    'create_trip',
    'update_trip',
    'add_trip_member',
    'remove_trip_member',
    'leave_trip',
    'copy_trip',
  ];
  const READ_TOOLS = ['list_trip_members', 'export_trip_ics', 'search_cover_images'];
  const NAV_TOOLS = ['list_trips', 'get_trip_summary'];
  const SHARE_TOOLS = ['get_share_link', 'create_share_link', 'delete_share_link'];

  it('null scopes (trek_ PAT) exposes the full surface', async () => {
    const names = await toolNames(null);
    for (const t of [...WRITE_TOOLS, ...READ_TOOLS, ...NAV_TOOLS, 'delete_trip', ...SHARE_TOOLS]) {
      expect(names).toContain(t);
    }
  });

  it('trips:read exposes reads + navigation but no writes/delete/share', async () => {
    const names = await toolNames(['trips:read']);
    for (const t of [...READ_TOOLS, ...NAV_TOOLS]) expect(names).toContain(t);
    for (const t of [...WRITE_TOOLS, 'delete_trip', ...SHARE_TOOLS]) expect(names).not.toContain(t);
  });

  it('trips:delete alone still grants the trip reads (canReadTrips parity) plus delete_trip', async () => {
    const names = await toolNames(['trips:delete']);
    expect(names).toContain('delete_trip');
    for (const t of [...READ_TOOLS, ...NAV_TOOLS]) expect(names).toContain(t);
    for (const t of WRITE_TOOLS) expect(names).not.toContain(t);
  });

  it('trips:share alone grants the share tools and the trip reads, nothing else', async () => {
    const names = await toolNames(['trips:share']);
    for (const t of [...SHARE_TOOLS, ...READ_TOOLS, ...NAV_TOOLS]) expect(names).toContain(t);
    for (const t of [...WRITE_TOOLS, 'delete_trip']) expect(names).not.toContain(t);
  });

  it('an unrelated scope keeps only the navigation tools', async () => {
    const names = await toolNames(['weather:read']);
    for (const t of NAV_TOOLS) expect(names).toContain(t);
    for (const t of [...WRITE_TOOLS, ...READ_TOOLS, 'delete_trip', ...SHARE_TOOLS]) expect(names).not.toContain(t);
  });
});

// ---------------------------------------------------------------------------
// The trip writes REST shares (TripWritesService) and the rights PUT
// /api/trips/:id checks field by field.
// ---------------------------------------------------------------------------

describe('Trip writes, as REST makes them', () => {
  /** Lower or raise a configurable action the way the admin permission panel does. */
  async function setPermission(action: string, level: string): Promise<void> {
    await setAppSetting(orm, `perm_${action}`, level);
    await invalidatePermissionsCache();
  }

  beforeEach(() => {
    realtime.broadcastToUserMock.mockClear();
  });

  afterEach(async () => {
    await invalidatePermissionsCache();
  });

  it('MCP-TRIPS-001: create_trip and copy_trip tell every tab of the owner that the trip list changed', async () => {
    const { user } = await makeUser(orm);
    await withHarness(user.id, async (h) => {
      const created = parseToolResult(
        await h.client.callTool({ name: 'create_trip', arguments: { title: 'Lisbon' } }),
      ) as { trip: { id: number } };
      await h.client.callTool({ name: 'copy_trip', arguments: { tripId: created.trip.id } });
    });
    expect(realtime.broadcastToUserMock.mock.calls).toEqual([
      [user.id, { type: 'trips:changed' }, undefined],
      [user.id, { type: 'trips:changed' }, undefined],
    ]);
  });

  it('MCP-TRIPS-002: create_trip and copy_trip are refused without trip_create, as the routes refuse them', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id);
    await setPermission('trip_create', 'admin');
    await withHarness(user.id, async (h) => {
      const created = await h.client.callTool({ name: 'create_trip', arguments: { title: 'Lisbon' } });
      const copied = await h.client.callTool({ name: 'copy_trip', arguments: { tripId: trip.id } });
      for (const result of [created, copied]) {
        expect(result.isError).toBe(true);
        expect(JSON.stringify(result.content)).toContain('You do not have permission to create trips.');
      }
    });
    expect(await countRows(orm, Trips, { user: user.id })).toBe(1);
    expect(realtime.broadcastToUserMock).not.toHaveBeenCalled();
  });

  it('MCP-TRIPS-003: delete_trip tells the room the trip is gone, as the route does', async () => {
    const { user } = await makeUser(orm);
    const trip = await makeTrip(orm, user.id);
    await withHarness(user.id, async (h) => {
      await h.client.callTool({ name: 'delete_trip', arguments: { tripId: trip.id } });
    });
    expect(broadcastMock.mock.calls).toEqual([[trip.id, 'trip:deleted', { id: trip.id, _source: 'mcp' }]]);
  });

  it('MCP-TRIPS-004: update_trip asks trip_archive and trip_cover_upload for those fields, not trip_edit alone', async () => {
    const { user: owner } = await makeUser(orm);
    const { user: member } = await makeUser(orm);
    const trip = await makeTrip(orm, owner.id, { title: 'Before' });
    await addTripMember(orm, trip.id, member.id);
    await setPermission('trip_edit', 'trip_member');
    await withHarness(member.id, async (h) => {
      const archive = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, is_archived: true },
      });
      const cover = await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, cover_image: null } });
      expect(archive.isError).toBe(true);
      expect(cover.isError).toBe(true);
      expect((await tripRow(trip.id)).is_archived).toBe(0);
      const rename = await h.client.callTool({ name: 'update_trip', arguments: { tripId: trip.id, title: 'After' } });
      expect(rename.isError).toBeFalsy();
    });
    await setPermission('trip_archive', 'trip_member');
    await withHarness(member.id, async (h) => {
      const archive = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, is_archived: true },
      });
      expect(archive.isError).toBeFalsy();
    });
    expect(await tripRow(trip.id)).toMatchObject({ title: 'After', is_archived: 1 });
  });

  it('MCP-TRIPS-005: update_trip sets the costs mode on budget_edit, as the route does (#1714)', async () => {
    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);
    expect((await tripRow(trip.id)).costs_mode).toBe('split');
    // A member may edit the costs by default, though not the trip's own details.
    await withHarness(member.id, async (h) => {
      const result = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, costs_mode: 'totals' },
      });
      expect(result.isError).toBeFalsy();
      expect((parseToolResult(result) as { trip: { costs_mode: string } }).trip.costs_mode).toBe('totals');
    });
    await setPermission('budget_edit', 'trip_owner');
    await withHarness(member.id, async (h) => {
      const result = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, costs_mode: 'split' },
      });
      expect(result.isError).toBe(true);
      const bogus = await h.client.callTool({
        name: 'update_trip',
        arguments: { tripId: trip.id, costs_mode: 'shares' },
      });
      expect(bogus.isError).toBe(true);
    });
    expect((await tripRow(trip.id)).costs_mode).toBe('totals');
  });
});