mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
382ba79584
A visit is a place planned on a day of one of the reader's trips; nothing is kept or confirmed by hand. A place keeps a log of changes to its details, links the tours and bookings it is about, and carries tags private to the reader, which a copy into a trip takes along only when picked. Contracts, routes, MCP tools and migrations for both engines.
106 lines
5.8 KiB
TypeScript
106 lines
5.8 KiB
TypeScript
/**
|
|
* The three PLC-18 controllers over stub services: each route hands the trip
|
|
* the guard resolved, the user, its ids and input (and the X-Socket-Id on
|
|
* writes) to one service method. Guards, pipes and contracts run in the e2e suite.
|
|
*/
|
|
import type { TripAccess } from '../../../../src/db/repositories/Trips.repository';
|
|
import { RESPONSE_CONTRACT_KEY } from '../../../../src/nest/common/response-contract';
|
|
import type { TripWriter } from '../../../../src/nest/common/trip-writer';
|
|
import { TRIP_PERMISSION_KEY } from '../../../../src/nest/permissions/trip-access.guard';
|
|
import { PlaceActivitiesLibraryController } from '../../../../src/nest/place-activities/place-activities-library.controller';
|
|
import type { PlaceActivitiesLibraryService } from '../../../../src/nest/place-activities/place-activities-library.service';
|
|
import { PlaceActivitiesController } from '../../../../src/nest/place-activities/place-activities.controller';
|
|
import type { PlaceActivitiesService } from '../../../../src/nest/place-activities/place-activities.service';
|
|
import { SavedPlaceActivitiesController } from '../../../../src/nest/place-activities/saved-place-activities.controller';
|
|
import type { User } from '../../../../src/types';
|
|
import {
|
|
placeActivitiesResponseSchema,
|
|
placeActivityCandidatesResponseSchema,
|
|
placeLibraryActivitiesResponseSchema,
|
|
} from '@trek/shared';
|
|
|
|
import { describe, expect, it, vi } from 'vitest';
|
|
|
|
const user = { id: 7, role: 'user', username: 'ann', email: 'ann@example.test' } as User;
|
|
const trip = { id: 4, user_id: 7 } as TripAccess;
|
|
const answer = { activities: [], can_link: true };
|
|
const meta = (key: string, handler: unknown) => Reflect.getMetadata(key, handler as object) as unknown;
|
|
|
|
describe('PlaceActivitiesController', () => {
|
|
it('PAC-001: reads the activities and the candidates of the trip place for the user', async () => {
|
|
const tripPlace = vi.fn().mockResolvedValue(answer);
|
|
const tripCandidates = vi.fn().mockResolvedValue({ candidates: [] });
|
|
const c = new PlaceActivitiesController({ tripPlace, tripCandidates } as unknown as PlaceActivitiesService);
|
|
expect(await c.list(user, trip, 9)).toBe(answer);
|
|
expect(await c.candidates(user, trip, 9)).toEqual({ candidates: [] });
|
|
expect(tripPlace).toHaveBeenCalledWith(4, 9, { id: 7, role: 'user' });
|
|
expect(tripCandidates).toHaveBeenCalledWith(4, 9, { id: 7, role: 'user' });
|
|
});
|
|
|
|
it('PAC-002: links and unlinks with a REST writer that carries the socket id', async () => {
|
|
const writer = { userId: 7 } as TripWriter;
|
|
const restWriter = vi.fn().mockReturnValue(writer);
|
|
const linkTrip = vi.fn().mockResolvedValue(answer);
|
|
const unlinkTrip = vi.fn().mockResolvedValue(answer);
|
|
const c = new PlaceActivitiesController({ restWriter, linkTrip, unlinkTrip } as unknown as PlaceActivitiesService);
|
|
await c.link(user, trip, 9, { reservation_id: 3 }, 'sock-2');
|
|
await c.unlinkBooking(user, trip, 9, 3, 'sock-2');
|
|
await c.unlinkTour(user, trip, 9, 5, 'sock-2');
|
|
expect(restWriter).toHaveBeenCalledWith(4, user, 'sock-2');
|
|
expect(linkTrip).toHaveBeenCalledWith(4, 9, { reservation_id: 3 }, writer);
|
|
expect(unlinkTrip).toHaveBeenCalledWith(4, 9, { reservation_id: 3 }, writer);
|
|
expect(unlinkTrip).toHaveBeenCalledWith(4, 9, { tour_id: 5 }, writer);
|
|
});
|
|
|
|
it('PAC-003: the writes take place_edit, and every route declares its contract', () => {
|
|
const proto = PlaceActivitiesController.prototype;
|
|
for (const handler of [proto.link, proto.unlinkBooking, proto.unlinkTour]) {
|
|
expect(meta(TRIP_PERMISSION_KEY, handler)).toBe('place_edit');
|
|
expect(meta(RESPONSE_CONTRACT_KEY, handler)).toBe(placeActivitiesResponseSchema);
|
|
}
|
|
expect(meta(TRIP_PERMISSION_KEY, proto.list)).toBeUndefined();
|
|
expect(meta(RESPONSE_CONTRACT_KEY, proto.list)).toBe(placeActivitiesResponseSchema);
|
|
expect(meta(RESPONSE_CONTRACT_KEY, proto.candidates)).toBe(placeActivityCandidatesResponseSchema);
|
|
});
|
|
});
|
|
|
|
describe('SavedPlaceActivitiesController', () => {
|
|
it('PAC-101: hands the user, the saved place and the socket id to the service', async () => {
|
|
const savedPlace = vi.fn().mockResolvedValue(answer);
|
|
const savedCandidates = vi.fn().mockResolvedValue({ candidates: [] });
|
|
const linkSaved = vi.fn().mockResolvedValue(answer);
|
|
const unlinkSaved = vi.fn().mockResolvedValue(answer);
|
|
const c = new SavedPlaceActivitiesController({
|
|
savedPlace,
|
|
savedCandidates,
|
|
linkSaved,
|
|
unlinkSaved,
|
|
} as unknown as PlaceActivitiesService);
|
|
expect(await c.list(user, 8)).toBe(answer);
|
|
expect(await c.candidates(user, 8)).toEqual({ candidates: [] });
|
|
await c.link(user, 8, { collection_tour_id: 5 }, 'sock-3');
|
|
await c.unlink(user, 8, 5, 'sock-3');
|
|
expect(savedPlace).toHaveBeenCalledWith(7, 8);
|
|
expect(savedCandidates).toHaveBeenCalledWith(7, 8);
|
|
expect(linkSaved).toHaveBeenCalledWith(7, 8, { collection_tour_id: 5 }, 'sock-3');
|
|
expect(unlinkSaved).toHaveBeenCalledWith(7, 8, 5, 'sock-3');
|
|
const proto = SavedPlaceActivitiesController.prototype;
|
|
expect(meta(RESPONSE_CONTRACT_KEY, proto.link)).toBe(placeActivitiesResponseSchema);
|
|
expect(meta(RESPONSE_CONTRACT_KEY, proto.candidates)).toBe(placeActivityCandidatesResponseSchema);
|
|
});
|
|
});
|
|
|
|
describe('PlaceActivitiesLibraryController', () => {
|
|
it('PAC-201: reads an entry from a trip place or a saved place', async () => {
|
|
const entry = vi.fn().mockResolvedValue({ activities: [], sources: [] });
|
|
const c = new PlaceActivitiesLibraryController({ entry } as unknown as PlaceActivitiesLibraryService);
|
|
await c.tripPlace(user, 9);
|
|
await c.savedPlace(user, 8);
|
|
expect(entry).toHaveBeenCalledWith({ id: 7, role: 'user' }, 'trip', 9);
|
|
expect(entry).toHaveBeenCalledWith({ id: 7, role: 'user' }, 'list', 8);
|
|
expect(meta(RESPONSE_CONTRACT_KEY, PlaceActivitiesLibraryController.prototype.tripPlace)).toBe(
|
|
placeLibraryActivitiesResponseSchema,
|
|
);
|
|
});
|
|
});
|