style: format server and shared with prettier

A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
This commit is contained in:
Maurice
2026-10-10 21:23:37 +02:00
parent f4e518b2df
commit 724c88c201
1978 changed files with 78448 additions and 39347 deletions
+216 -64
View File
@@ -8,36 +8,51 @@
* collected here rather than scattered across the domain suites, because what they
* have in common is being defence in depth rather than behaviour a plugin sees.
*/
import { describe, it, expect, vi } from 'vitest';
import { PluginRpcHost } from '../../../src/nest/plugins/host/rpc-host';
import { createTestPluginRegistry } from '../../../src/nest-rpc/rpc-kit/testing';
import { PluginGuards } from '../../../src/nest-rpc/plugin-guards.service';
import { TagsRpc } from '../../../src/nest/tags/tags.rpc';
import { DayNotesRpc } from '../../../src/nest/day-notes/day-notes.rpc';
import { PlacesRpc } from '../../../src/nest/places/places.rpc';
import { PackingRpc } from '../../../src/nest/packing/packing.rpc';
import { FilesRpc } from '../../../src/nest/files/files.rpc';
import { TripsRpc } from '../../../src/nest/trips/trips.rpc';
import { AccommodationsRpc } from '../../../src/nest/accommodations/accommodations.rpc';
import { ReservationsRpc } from '../../../src/nest/reservations/reservations.rpc';
import { DaysRpc } from '../../../src/nest/days/days.rpc';
import { VacayRpc } from '../../../src/nest/vacay/vacay.rpc';
import { NotFoundError, ValidationError } from '../../../src/nest/trips/trips.service';
import type { EntityManager } from '@mikro-orm/core';
import type { TripsRepository } from '../../../src/db/repositories/Trips.repository';
import type { PermissionsService } from '../../../src/nest/permissions/permissions.service';
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import type { UsersRepository } from '../../../src/db/repositories/Users.repository';
import type { RpcRequest, RpcError } from '../../../src/nest/plugins/protocol/envelope';
import { makeDeps } from '../../helpers/rpc-host-deps';
import { PluginGuards } from '../../../src/nest-rpc/plugin-guards.service';
import { createTestPluginRegistry } from '../../../src/nest-rpc/rpc-kit/testing';
import { schemaMessage } from '../../../src/nest-rpc/rpc-params';
import { AccommodationsRpc } from '../../../src/nest/accommodations/accommodations.rpc';
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import { DayNotesRpc } from '../../../src/nest/day-notes/day-notes.rpc';
import { DaysRpc } from '../../../src/nest/days/days.rpc';
import { FilesRpc } from '../../../src/nest/files/files.rpc';
import { PackingRpc } from '../../../src/nest/packing/packing.rpc';
import type { PermissionsService } from '../../../src/nest/permissions/permissions.service';
import { PlacesRpc } from '../../../src/nest/places/places.rpc';
import { PluginRpcHost } from '../../../src/nest/plugins/host/rpc-host';
import type { RpcRequest, RpcError } from '../../../src/nest/plugins/protocol/envelope';
import { ReservationsRpc } from '../../../src/nest/reservations/reservations.rpc';
import { TagsRpc } from '../../../src/nest/tags/tags.rpc';
import { TripsRpc } from '../../../src/nest/trips/trips.rpc';
import { NotFoundError, ValidationError } from '../../../src/nest/trips/trips.service';
import { VacayRpc } from '../../../src/nest/vacay/vacay.rpc';
import { makeDeps } from '../../helpers/rpc-host-deps';
import type { EntityManager } from '@mikro-orm/core';
const req = (method: string, params: Record<string, unknown> = {}): RpcRequest => ({ k: 'req', id: 'x', method, params });
import { describe, it, expect, vi } from 'vitest';
const req = (method: string, params: Record<string, unknown> = {}): RpcRequest => ({
k: 'req',
id: 'x',
method,
params,
});
const err = (r: unknown) => (r as RpcError).error;
const ALL = new Set([
'db:read:tags', 'db:write:tags', 'db:write:daynotes', 'db:write:places', 'db:write:packing',
'db:write:files', 'db:write:trips', 'db:create:trips', 'db:write:accommodations', 'db:write:reservations',
'db:write:days', 'db:write:itinerary',
'db:read:tags',
'db:write:tags',
'db:write:daynotes',
'db:write:places',
'db:write:packing',
'db:write:files',
'db:write:trips',
'db:create:trips',
'db:write:accommodations',
'db:write:reservations',
'db:write:days',
'db:write:itinerary',
]);
function guardsFor() {
@@ -77,7 +92,13 @@ describe('optional fields fall back rather than reaching the service as undefine
it('FALLBACK-001 a tag update without name or colour forwards undefined for both', async () => {
const { guards } = guardsFor();
const update = vi.fn(() => ({ id: 1 }));
const tags = { getByIdAndUser: vi.fn(() => ({ id: 1 })), update, list: vi.fn(() => []), create: vi.fn(), remove: vi.fn() } as never;
const tags = {
getByIdAndUser: vi.fn(() => ({ id: 1 })),
update,
list: vi.fn(() => []),
create: vi.fn(),
remove: vi.fn(),
} as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new TagsRpc(tags)]));
await host.dispatch(req('tags.update', { tagId: 1, input: { name: 42, color: 42 } }), 42);
expect(update).toHaveBeenCalledWith(1, undefined, undefined);
@@ -86,8 +107,20 @@ describe('optional fields fall back rather than reaching the service as undefine
it('FALLBACK-002 a day note without a text falls back to an empty string', async () => {
const { guards } = guardsFor();
const create = vi.fn(() => ({ id: 1 }));
const notes = { dayExists: vi.fn(() => true), create, getNote: vi.fn(() => ({})), update: vi.fn(), remove: vi.fn(), list: vi.fn(() => []) } as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new DayNotesRpc(notes, realtime(), guards)]));
const notes = {
dayExists: vi.fn(() => true),
create,
getNote: vi.fn(() => ({})),
update: vi.fn(),
remove: vi.fn(),
list: vi.fn(() => []),
} as never;
const host = new PluginRpcHost(
'p',
ALL,
makeDeps(),
createTestPluginRegistry([new DayNotesRpc(notes, realtime(), guards)]),
);
// The handler rejects a blank text, so this reaches create only with a real one;
// what the fallback covers is a text that vanished between check and use.
await host.dispatch(req('daynotes.create', { tripId: 1, dayId: 3, input: { text: 'note', time: '09:00' } }), 42);
@@ -98,7 +131,12 @@ describe('optional fields fall back rather than reaching the service as undefine
const { guards } = guardsFor();
const createFileLink = vi.fn(() => []);
const files = { getFileById: vi.fn(() => ({})), findForeignLinkTarget: vi.fn(() => null), createFileLink } as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new FilesRpc(files, realtime(), {} as never, guards, {} as never)]));
const host = new PluginRpcHost(
'p',
ALL,
makeDeps(),
createTestPluginRegistry([new FilesRpc(files, realtime(), {} as never, guards, {} as never)]),
);
await host.dispatch(req('files.createLink', { tripId: 1, fileId: 2, opts: {} }), 42);
expect(createFileLink).toHaveBeenCalledWith(2, { reservation_id: null, assignment_id: null, place_id: null });
});
@@ -107,16 +145,33 @@ describe('optional fields fall back rather than reaching the service as undefine
const { guards } = guardsFor();
const updateFile = vi.fn((id: number) => ({ id }));
const files = { getFileById: vi.fn(() => ({})), findForeignLinkTarget: vi.fn(() => null), updateFile } as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new FilesRpc(files, realtime(), {} as never, guards, {} as never)]));
await host.dispatch(req('files.update', { tripId: 1, fileId: 2, input: { reservation_id: null, place_id: 7 } }), 42);
expect(updateFile).toHaveBeenLastCalledWith(2, expect.anything(), expect.objectContaining({ reservation_id: null, place_id: '7' }));
const host = new PluginRpcHost(
'p',
ALL,
makeDeps(),
createTestPluginRegistry([new FilesRpc(files, realtime(), {} as never, guards, {} as never)]),
);
await host.dispatch(
req('files.update', { tripId: 1, fileId: 2, input: { reservation_id: null, place_id: 7 } }),
42,
);
expect(updateFile).toHaveBeenLastCalledWith(
2,
expect.anything(),
expect.objectContaining({ reservation_id: null, place_id: '7' }),
);
});
it('FALLBACK-005 a bag colour that is not a string is dropped', async () => {
const { guards } = guardsFor();
const createBag = vi.fn(() => ({ id: 80 }));
const packing = { createBag } as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new PackingRpc(packing, realtime(), guards)]));
const host = new PluginRpcHost(
'p',
ALL,
makeDeps(),
createTestPluginRegistry([new PackingRpc(packing, realtime(), guards)]),
);
await host.dispatch(req('packing.createBag', { tripId: 1, input: { name: 'Bag' } }), 42);
expect(createBag).toHaveBeenCalledWith('1', { name: 'Bag', color: undefined });
});
@@ -128,19 +183,47 @@ describe('a service that says no becomes a refusal, not a crash', () => {
// remove is async in production; a Promise-returning double pins the fix that
// awaits it — an un-awaited call compares a Promise (always truthy) against
// falsy and never refuses.
const places = { get: vi.fn(() => ({ id: 7 })), remove: vi.fn(() => Promise.resolve(false)), create: vi.fn(), update: vi.fn(), linkedExpenseIds: vi.fn(() => []) } as never;
const places = {
get: vi.fn(() => ({ id: 7 })),
remove: vi.fn(() => Promise.resolve(false)),
create: vi.fn(),
update: vi.fn(),
linkedExpenseIds: vi.fn(() => []),
} as never;
const journey = { onPlaceDeleted: vi.fn(), onPlaceCreated: vi.fn(), onPlaceUpdated: vi.fn() } as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new PlacesRpc(places, journey, realtime(), guards)]));
expect(err(await host.dispatch(req('places.delete', { tripId: 1, placeId: 7 }), 42)).message).toBe('no place 7 on trip 1');
const host = new PluginRpcHost(
'p',
ALL,
makeDeps(),
createTestPluginRegistry([new PlacesRpc(places, journey, realtime(), guards)]),
);
expect(err(await host.dispatch(req('places.delete', { tripId: 1, placeId: 7 }), 42)).message).toBe(
'no place 7 on trip 1',
);
});
it('FALLBACK-007 a bag that will not update or delete is refused', async () => {
const { guards } = guardsFor();
const packing = { updateBag: vi.fn(() => null), deleteBag: vi.fn(() => false), setBagMembers: vi.fn(() => null) } as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new PackingRpc(packing, realtime(), guards)]));
expect(err(await host.dispatch(req('packing.updateBag', { tripId: 1, bagId: 80, input: {} }), 42)).message).toBe('no packing bag 80 on trip 1');
expect(err(await host.dispatch(req('packing.deleteBag', { tripId: 1, bagId: 80 }), 42)).message).toBe('no packing bag 80 on trip 1');
expect(err(await host.dispatch(req('packing.setBagMembers', { tripId: 1, bagId: 80, userIds: [] }), 42)).message).toBe('no packing bag 80 on trip 1');
const packing = {
updateBag: vi.fn(() => null),
deleteBag: vi.fn(() => false),
setBagMembers: vi.fn(() => null),
} as never;
const host = new PluginRpcHost(
'p',
ALL,
makeDeps(),
createTestPluginRegistry([new PackingRpc(packing, realtime(), guards)]),
);
expect(err(await host.dispatch(req('packing.updateBag', { tripId: 1, bagId: 80, input: {} }), 42)).message).toBe(
'no packing bag 80 on trip 1',
);
expect(err(await host.dispatch(req('packing.deleteBag', { tripId: 1, bagId: 80 }), 42)).message).toBe(
'no packing bag 80 on trip 1',
);
expect(
err(await host.dispatch(req('packing.setBagMembers', { tripId: 1, bagId: 80, userIds: [] }), 42)).message,
).toBe('no packing bag 80 on trip 1');
});
it('FALLBACK-008 an accommodation whose refs do not validate is refused with the validator message', async () => {
@@ -151,9 +234,20 @@ describe('a service that says no becomes a refusal, not a crash', () => {
createAccommodation: vi.fn(),
updateAccommodation: vi.fn(),
} as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new AccommodationsRpc(accommodations, realtime(), guards)]));
expect(err(await host.dispatch(req('accommodations.update', { tripId: 1, accommodationId: 11, input: { start_day_id: 9 } }), 42)).message)
.toBe('day 9 is not on this trip');
const host = new PluginRpcHost(
'p',
ALL,
makeDeps(),
createTestPluginRegistry([new AccommodationsRpc(accommodations, realtime(), guards)]),
);
expect(
err(
await host.dispatch(
req('accommodations.update', { tripId: 1, accommodationId: 11, input: { start_day_id: 9 } }),
42,
),
).message,
).toBe('day 9 is not on this trip');
});
});
@@ -162,46 +256,81 @@ describe('service errors are translated into the RPC taxonomy', () => {
const { guards, em } = guardsFor();
const trips = {
list: vi.fn(() => []),
updateTrip: vi.fn(() => { if (updateThrows) throw updateThrows; return { updatedTrip: { id: 1 } }; }),
create: vi.fn(() => { if (createThrows) throw createThrows; return { trip: { id: 99 } }; }),
updateTrip: vi.fn(() => {
if (updateThrows) throw updateThrows;
return { updatedTrip: { id: 1 } };
}),
create: vi.fn(() => {
if (createThrows) throw createThrows;
return { trip: { id: 99 } };
}),
removeMember: vi.fn(),
} as never;
const rpc = new TripsRpc(trips, {} as never, {} as never, {} as never, realtime(), guards, {} as never, {} as never, em);
const rpc = new TripsRpc(
trips,
{} as never,
{} as never,
{} as never,
realtime(),
guards,
{} as never,
{} as never,
em,
);
return new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([rpc]));
}
it('FALLBACK-009 a trip update maps ValidationError and NotFoundError separately', async () => {
const invalid = tripsHost(new ValidationError('end before start'));
expect(err(await invalid.dispatch(req('trips.update', { tripId: 1, input: { title: 'x' } }), 42)))
.toMatchObject({ code: 'BAD_PARAMS', message: 'end before start' });
expect(err(await invalid.dispatch(req('trips.update', { tripId: 1, input: { title: 'x' } }), 42))).toMatchObject({
code: 'BAD_PARAMS',
message: 'end before start',
});
const missing = tripsHost(new NotFoundError('trip is gone'));
expect(err(await missing.dispatch(req('trips.update', { tripId: 1, input: { title: 'x' } }), 42)))
.toMatchObject({ code: 'RESOURCE_FORBIDDEN', message: 'trip is gone' });
expect(err(await missing.dispatch(req('trips.update', { tripId: 1, input: { title: 'x' } }), 42))).toMatchObject({
code: 'RESOURCE_FORBIDDEN',
message: 'trip is gone',
});
});
it('FALLBACK-010 an unknown trip failure keeps its own taxonomy', async () => {
const boom = tripsHost(new Error('disk on fire'));
expect(err(await boom.dispatch(req('trips.update', { tripId: 1, input: { title: 'x' } }), 42)))
.toMatchObject({ code: 'HOST_ERROR', message: 'disk on fire' });
expect(err(await boom.dispatch(req('trips.update', { tripId: 1, input: { title: 'x' } }), 42))).toMatchObject({
code: 'HOST_ERROR',
message: 'disk on fire',
});
});
it('FALLBACK-011 trips.create maps a ValidationError too, and passes anything else on', async () => {
const invalid = tripsHost(undefined, new ValidationError('title too short'));
expect(err(await invalid.dispatch(req('trips.create', { input: { title: 'x' } }), 42)))
.toMatchObject({ code: 'BAD_PARAMS', message: 'title too short' });
expect(err(await invalid.dispatch(req('trips.create', { input: { title: 'x' } }), 42))).toMatchObject({
code: 'BAD_PARAMS',
message: 'title too short',
});
const boom = tripsHost(undefined, new Error('db locked'));
expect(err(await boom.dispatch(req('trips.create', { input: { title: 'x' } }), 42)))
.toMatchObject({ code: 'HOST_ERROR' });
expect(err(await boom.dispatch(req('trips.create', { input: { title: 'x' } }), 42))).toMatchObject({
code: 'HOST_ERROR',
});
});
it('FALLBACK-012 a reservation delete without a type still notifies with an empty one', async () => {
const { guards } = guardsFor();
const notifyBookingChange = vi.fn();
const reservations = {
remove: vi.fn(() => ({ deleted: { title: 'Hotel', type: null, accommodation_id: null }, accommodationDeleted: false, deletedBudgetItemId: null, deletedBudgetItemIds: [] })),
remove: vi.fn(() => ({
deleted: { title: 'Hotel', type: null, accommodation_id: null },
accommodationDeleted: false,
deletedBudgetItemId: null,
deletedBudgetItemIds: [],
})),
notifyBookingChange,
} as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new ReservationsRpc(reservations, realtime(), guards)]));
const host = new PluginRpcHost(
'p',
ALL,
makeDeps(),
createTestPluginRegistry([new ReservationsRpc(reservations, realtime(), guards)]),
);
await host.dispatch(req('reservations.delete', { tripId: 1, reservationId: 5 }), 42);
expect(notifyBookingChange).toHaveBeenCalledWith(1, 42, 'Hotel', '');
});
@@ -217,15 +346,25 @@ describe('service errors are translated into the RPC taxonomy', () => {
unresolvedReferences: vi.fn(() => []),
notifyBookingChange,
} as never;
const host = new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([new ReservationsRpc(reservations, realtime(), guards)]));
await host.dispatch(req('reservations.update', { tripId: 1, reservationId: 5, input: { title: 'Renamed', type: 'lodging' } }), 42);
const host = new PluginRpcHost(
'p',
ALL,
makeDeps(),
createTestPluginRegistry([new ReservationsRpc(reservations, realtime(), guards)]),
);
await host.dispatch(
req('reservations.update', { tripId: 1, reservationId: 5, input: { title: 'Renamed', type: 'lodging' } }),
42,
);
expect(notifyBookingChange).toHaveBeenCalledWith(1, 42, 'Renamed', 'lodging');
});
});
describe('schemaMessage is the one copy of a fallback that used to exist twenty times', () => {
it('FALLBACK-014 it reports the first issue when there is one', () => {
expect(schemaMessage({ issues: [{ message: 'name is required' }, { message: 'second' }] })).toBe('name is required');
expect(schemaMessage({ issues: [{ message: 'name is required' }, { message: 'second' }] })).toBe(
'name is required',
);
});
it('FALLBACK-015 it falls back when a schema reports no issues at all', () => {
@@ -264,7 +403,10 @@ const SCHEMA_REJECTS: Array<[string, Record<string, unknown>]> = [
describe('every schema-validated method rejects a payload its schema refuses', () => {
function everything() {
const { guards, em } = guardsFor();
const anything = new Proxy({}, { get: () => vi.fn(() => ({ id: 1, trip: { id: 1 }, updatedTrip: { id: 1 }, reservation: { id: 1 } })) }) as never;
const anything = new Proxy(
{},
{ get: () => vi.fn(() => ({ id: 1, trip: { id: 1 }, updatedTrip: { id: 1 }, reservation: { id: 1 } })) },
) as never;
const registry = createTestPluginRegistry([
new PlacesRpc(anything, anything, realtime(), guards),
new DaysRpc(anything, realtime(), guards, anything),
@@ -285,7 +427,12 @@ describe('every schema-validated method rejects a payload its schema refuses', (
it('FALLBACK-016 a reservation with malformed endpoints is rejected on update too', async () => {
const res = await everything().dispatch(
req('reservations.update', { tripId: 1, reservationId: 5, input: { title: 'x', type: 'lodging', endpoints: [{ junk: 1 }] } }), 42,
req('reservations.update', {
tripId: 1,
reservationId: 5,
input: { title: 'x', type: 'lodging', endpoints: [{ junk: 1 }] },
}),
42,
);
expect(err(res).code).toBe('BAD_PARAMS');
});
@@ -293,7 +440,12 @@ describe('every schema-validated method rejects a payload its schema refuses', (
it('FALLBACK-017 an absent vacay note and an absent tag colour both pass undefined', async () => {
const { guards } = guardsFor();
const toggleCompanyHoliday = vi.fn(() => ({}));
const vacay = { getActivePlanId: vi.fn(() => 1), toggleCompanyHoliday, toggleEntry: vi.fn(), getPlanData: vi.fn() } as never;
const vacay = {
getActivePlanId: vi.fn(() => 1),
toggleCompanyHoliday,
toggleEntry: vi.fn(),
getPlanData: vi.fn(),
} as never;
const update = vi.fn(() => ({ id: 1 }));
const tags = { getByIdAndUser: vi.fn(() => ({ id: 1 })), update } as never;
const host = new PluginRpcHost(