Files
TREK/server/tests/unit/nest/trip-addons.controller.test.ts
T
Maurice 3f38621024 feat(server): let a trip owner switch trip addons for one trip
A trip can now turn a trip addon (lists, costs, documents, collab, road
trip, tours) on or off for itself, whatever the instance switch says. No
row in the new trip_addon_overrides table means the trip follows the
instance; a row is the owner's choice. Going back to the instance setting
deletes the row, and no addon data is touched either way.

- GET/PUT /api/trips/:tripId/addons[/:addonId] (every member reads, the
  owner writes) and the MCP tools get_trip_addons and set_trip_addon, on
  the shared tripAddons contract; tripAddons:changed tells the members.
- Every trip-scoped addon surface asks the trip: REST through
  TripAddonGuard, MCP through a per-call gate on the named or pinned trip,
  plugin RPC and events, the share link, the offline bundle, reminders,
  doc sync, the assistant (tools, keep_attached_file), booking costs, the
  trip summary prompt, and the cross-trip tour reads (library, Atlas
  stats, collection save).
- Routes without a trip open while the addon is on in any of the user's
  trips. Switching an addon on that the instance has off ends the members'
  MCP sessions so they reconnect with its tools.
- A copied trip takes its owner's choices along.

Behaviour changes: an anonymous call to an addon route that is off now
gets 401 instead of 404, and with an instance switch off the packing,
todo and files plugin RPC methods, the files MCP tools, todo reminder
mails, core plugin events, plugin view contributions, the share link's
lists, costs and chat sections and the offline bundle's lists follow it.
2026-11-21 13:23:09 +01:00

72 lines
4.0 KiB
TypeScript

/**
* TripAddonsController: the route shapes, the guard chain and the owner
* refusal it declares, and that each handler hands the guard-resolved trip and
* the caller straight to TripAddonsService. The behaviour behind it is pinned
* in trip-addons.service.test.ts and over HTTP in trip-addons.e2e.test.ts.
*/
import type { TripAccess } from '../../../src/db/repositories/Trips.repository';
import { TripAddonsController, TripAddonUpdateDto } from '../../../src/nest/addons/trip-addons.controller';
import { TRIP_ADDON_OWNER_ONLY, type TripAddonsService } from '../../../src/nest/addons/trip-addons.service';
import { JwtAuthGuard } from '../../../src/nest/auth-core/jwt-auth.guard';
import { RESPONSE_CONTRACT_KEY } from '../../../src/nest/common/response-contract';
import { TripAccessGuard } from '../../../src/nest/permissions/trip-access.guard';
import { TRIP_OWNER_KEY, TripOwnerGuard } from '../../../src/nest/permissions/trip-owner.guard';
import type { User } from '../../../src/types';
import { RequestMethod } from '@nestjs/common';
import { GUARDS_METADATA, METHOD_METADATA, PATH_METADATA } from '@nestjs/common/constants';
import { tripAddonsResponseSchema, tripAddonUpdateRequestSchema, type TripAddon } from '@trek/shared';
import { describe, expect, it, vi } from 'vitest';
const trip: TripAccess = { id: 7, user_id: 3, currency: 'EUR' };
const user = { id: 3 } as User;
const addons: TripAddon[] = [
{ id: 'budget', name: 'Costs', icon: 'Wallet', enabled: false, instanceEnabled: true, overridden: true },
];
function makeController() {
const service = {
effectiveTripAddons: vi.fn().mockResolvedValue(addons),
setTripOverride: vi.fn().mockResolvedValue(addons),
};
return { service, controller: new TripAddonsController(service as unknown as TripAddonsService) };
}
const proto = TripAddonsController.prototype;
describe('TripAddonsController', () => {
it('TRIPADDON-CTRL-001: lives under the trip, behind auth and trip access, with no addon gate', () => {
expect(Reflect.getMetadata(PATH_METADATA, TripAddonsController)).toBe('api/trips/:tripId/addons');
expect(Reflect.getMetadata(GUARDS_METADATA, TripAddonsController)).toEqual([JwtAuthGuard, TripAccessGuard]);
});
it('TRIPADDON-CTRL-002: GET / answers the trip list, declared by the shared response schema', async () => {
expect(Reflect.getMetadata(METHOD_METADATA, proto.list)).toBe(RequestMethod.GET);
expect(Reflect.getMetadata(PATH_METADATA, proto.list)).toBe('/');
expect(Reflect.getMetadata(RESPONSE_CONTRACT_KEY, proto.list)).toBe(tripAddonsResponseSchema);
expect(Reflect.getMetadata(GUARDS_METADATA, proto.list)).toBeUndefined();
const { service, controller } = makeController();
expect(await controller.list(trip)).toEqual({ tripId: 7, addons });
expect(service.effectiveTripAddons).toHaveBeenCalledWith(7);
});
it('TRIPADDON-CTRL-003: PUT /:addonId is owner-only with the REST refusal text and the shared body', () => {
expect(Reflect.getMetadata(METHOD_METADATA, proto.update)).toBe(RequestMethod.PUT);
expect(Reflect.getMetadata(PATH_METADATA, proto.update)).toBe(':addonId');
expect(Reflect.getMetadata(GUARDS_METADATA, proto.update)).toEqual([TripOwnerGuard]);
expect(Reflect.getMetadata(TRIP_OWNER_KEY, proto.update)).toEqual({ message: TRIP_ADDON_OWNER_ONLY });
expect(Reflect.getMetadata(RESPONSE_CONTRACT_KEY, proto.update)).toBe(tripAddonsResponseSchema);
expect(TripAddonUpdateDto.schema).toBe(tripAddonUpdateRequestSchema);
});
it('TRIPADDON-CTRL-004: PUT hands trip, caller, addon, choice and socket id to the service', async () => {
const { service, controller } = makeController();
expect(await controller.update(user, trip, 'budget', { enabled: false }, 'sock-9')).toEqual({ tripId: 7, addons });
expect(service.setTripOverride).toHaveBeenCalledWith(7, 3, 'budget', false, 'sock-9');
await controller.update(user, trip, 'budget', { enabled: null });
expect(service.setTripOverride).toHaveBeenLastCalledWith(7, 3, 'budget', null, undefined);
});
});