mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
6680bc7c74
PluginsModule was 21 controllers, 24 domain imports and 10 providers in one class, which made it the hub most of the Nest graph ran through: AdminModule imported it for a single cascade-disable call and inherited all of it. It is now PluginsRuntimeModule (supervisor, capability router, hook contracts), PluginContributionsModule (the 14 read-only hook controllers, moved to contributions/), PluginOAuthModule (the credential leaf, moved to oauth/) and a composition root that keeps the install and delivery controllers. AdminModule imports the runtime alone. The three plugin files that reached for the db singleton alongside the injected DatabaseService no longer do. plugin-host-state and signature-status take the connection from the caller, and the three per-user settings functions that were bare exports 'so the RPC host wiring can call them directly' are now PluginUserSettingsService - the host wiring is providers, so it injects them. Coverage moves from one repo-wide 80% to a per-domain ratchet. The single number let a well-covered domain subsidise a thin one: booking-import sits at 50% and integrations at 20%, and the aggregate still cleared the bar, so either could have lost another ten points unnoticed. Each domain now carries its own measured floor, regenerated by scripts/coverage-thresholds.mjs. Two test-side findings on the way: expect(undefined).toContain(SomeClass) PASSES in this vitest version - it only throws for a string needle. Every '<X> is listed in its module providers' guard was written that way, so all twelve of them silently stopped guarding the moment their class moved. They go through expectRegisteredProvider now, which asserts the array shape first. The atlas region test paid for the one-off decompression of the multi-MB admin1 bundle inside its own 5s budget, which is why it failed under load and passed alone. The bundle is warmed in beforeAll instead.
138 lines
8.0 KiB
TypeScript
138 lines
8.0 KiB
TypeScript
/**
|
|
* The place plugin surface after it moved onto @PluginMethod.
|
|
*
|
|
* The delete path carries an ordering constraint that is easy to lose in a move and
|
|
* silent when broken, so it gets its own cases: the journey hook has to run BEFORE
|
|
* the row is deleted, and the id has to be scoped to the trip before the hook runs
|
|
* at all.
|
|
*/
|
|
import { describe, it, expect, vi } from 'vitest';
|
|
import { expectRegisteredProvider } from '../../helpers/module-providers';
|
|
import { PluginRpcHost } from '../../../src/nest/plugins/host/rpc-host';
|
|
import { createTestPluginRegistry } from '../../../src/nest/plugins/host/rpc-kit/testing';
|
|
import { PluginGuards } from '../../../src/nest/plugins/host/plugin-guards.service';
|
|
import { PlacesRpc } from '../../../src/nest/places/places.rpc';
|
|
import { PlacesModule } from '../../../src/nest/places/places.module';
|
|
import type { PlacesService } from '../../../src/nest/places/places.service';
|
|
import type { JourneyDomainService } from '../../../src/nest/journey/journey-domain.service';
|
|
import type { RealtimeService } from '../../../src/nest/realtime/realtime.service';
|
|
import type { DatabaseService } from '../../../src/nest/database/database.service';
|
|
import type { PermissionsService } from '../../../src/nest/permissions/permissions.service';
|
|
import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
|
import type { RpcRequest, RpcError } from '../../../src/nest/plugins/protocol/envelope';
|
|
import { makeDeps } from '../../helpers/rpc-host-deps';
|
|
|
|
const req = (method: string, params: Record<string, unknown> = {}): RpcRequest => ({ k: 'req', id: 'x', method, params });
|
|
|
|
/** Trip 1 belongs to user 42; place 7 sits on it. */
|
|
function build(opts: { canEdit?: boolean; journeyThrows?: boolean } = {}) {
|
|
const places = {
|
|
create: vi.fn((_t: string, i: Record<string, unknown>) => ({ id: 10, ...i })),
|
|
update: vi.fn((_t: string, id: string) => (id === '7' ? { id: 7, name: 'updated' } : null)),
|
|
get: vi.fn((_t: string, id: string) => (id === '7' ? { id: 7 } : undefined)),
|
|
remove: vi.fn((_t: string, id: string) => id === '7'),
|
|
} as unknown as PlacesService & Record<string, ReturnType<typeof vi.fn>>;
|
|
const journey = {
|
|
onPlaceCreated: vi.fn(() => { if (opts.journeyThrows) throw new Error('journey down'); }),
|
|
onPlaceUpdated: vi.fn(() => { if (opts.journeyThrows) throw new Error('journey down'); }),
|
|
onPlaceDeleted: vi.fn(() => { if (opts.journeyThrows) throw new Error('journey down'); }),
|
|
} as unknown as JourneyDomainService & Record<string, ReturnType<typeof vi.fn>>;
|
|
const realtime = { broadcast: vi.fn() } as unknown as RealtimeService & { broadcast: ReturnType<typeof vi.fn> };
|
|
const guards = new PluginGuards(
|
|
{
|
|
canAccessTrip: vi.fn((tripId: number, userId: number) => (tripId === 1 && userId === 42 ? { id: 1, user_id: 42 } : undefined)),
|
|
prepare: vi.fn(() => ({ get: () => ({ role: 'user' }) })),
|
|
} as unknown as DatabaseService,
|
|
{ checkPermission: vi.fn(() => opts.canEdit ?? true) } as unknown as PermissionsService,
|
|
{ isAddonEnabled: vi.fn(() => true) } as unknown as AddonsService,
|
|
);
|
|
const rpc = new PlacesRpc(places, journey, realtime, guards);
|
|
const host = (...grants: string[]) => new PluginRpcHost('p', new Set(grants), makeDeps(), createTestPluginRegistry([rpc]));
|
|
return { places, journey, realtime, host };
|
|
}
|
|
|
|
describe('PlacesRpc', () => {
|
|
it('PLACES-RPC-001 writes need the grant, the edit right and a bound user', async () => {
|
|
const f = build();
|
|
const host = f.host('db:write:places');
|
|
expect((await host.dispatch(req('places.create', { tripId: 1, input: { name: 'Shrine' } }), 42)).ok).toBe(true);
|
|
expect(((await host.dispatch(req('places.create', { tripId: 2, input: { name: 'x' } }), 42)) as RpcError).error.code).toBe('RESOURCE_FORBIDDEN');
|
|
const noUser = (await host.dispatch(req('places.create', { tripId: 1, input: { name: 'x' } }), undefined)) as RpcError;
|
|
expect(noUser.error.message).toBe('place writes require an authenticated user context');
|
|
const denied = (await f.host().dispatch(req('places.create', { tripId: 1, input: { name: 'x' } }), 42)) as RpcError;
|
|
expect(denied.error.code).toBe('PERMISSION_DENIED');
|
|
});
|
|
|
|
it('PLACES-RPC-002 an invalid place is BAD_PARAMS with the schema message', async () => {
|
|
const f = build();
|
|
const res = (await f.host('db:write:places').dispatch(req('places.create', { tripId: 1, input: {} }), 42)) as RpcError;
|
|
expect(res.error.code).toBe('BAD_PARAMS');
|
|
expect(res.error.message).toMatch(/^invalid place: /);
|
|
});
|
|
|
|
it('PLACES-RPC-003 an oversized field is capped like the REST controller caps it', async () => {
|
|
const f = build();
|
|
const res = (await f.host('db:write:places').dispatch(
|
|
req('places.create', { tripId: 1, input: { name: 'x'.repeat(201) } }), 42,
|
|
)) as RpcError;
|
|
expect(res.error.message).toBe('name must be 200 characters or fewer');
|
|
expect(f.places.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('PLACES-RPC-004 a place on another trip is refused, naming it', async () => {
|
|
const f = build();
|
|
const host = f.host('db:write:places');
|
|
expect(((await host.dispatch(req('places.update', { tripId: 1, placeId: 404, input: { name: 'x' } }), 42)) as RpcError).error.message)
|
|
.toBe('no place 404 on trip 1');
|
|
expect(((await host.dispatch(req('places.delete', { tripId: 1, placeId: 404 }), 42)) as RpcError).error.message)
|
|
.toBe('no place 404 on trip 1');
|
|
});
|
|
|
|
it('PLACES-RPC-005 delete runs the journey hook BEFORE the row goes', async () => {
|
|
const f = build();
|
|
await f.host('db:write:places').dispatch(req('places.delete', { tripId: 1, placeId: 7 }), 42);
|
|
// journey_entries.source_place_id is ON DELETE SET NULL. Run the hook afterwards
|
|
// and it finds nothing left to detach, leaving the entries as orphans.
|
|
expect(f.journey.onPlaceDeleted.mock.invocationCallOrder[0]).toBeLessThan(f.places.remove.mock.invocationCallOrder[0]);
|
|
});
|
|
|
|
it('PLACES-RPC-006 a foreign place id never reaches the journey hook', async () => {
|
|
const f = build();
|
|
await f.host('db:write:places').dispatch(req('places.delete', { tripId: 1, placeId: 404 }), 42);
|
|
// The hook keys on the place alone, so an unscoped id would detach ANOTHER
|
|
// trip's journey entries even though the delete itself is refused.
|
|
expect(f.journey.onPlaceDeleted).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('PLACES-RPC-007 a broken journey link never fails a write that already succeeded', async () => {
|
|
const f = build({ journeyThrows: true });
|
|
const host = f.host('db:write:places');
|
|
expect((await host.dispatch(req('places.create', { tripId: 1, input: { name: 'Shrine' } }), 42)).ok).toBe(true);
|
|
expect((await host.dispatch(req('places.update', { tripId: 1, placeId: 7, input: { name: 'x' } }), 42)).ok).toBe(true);
|
|
expect((await host.dispatch(req('places.delete', { tripId: 1, placeId: 7 }), 42)).ok).toBe(true);
|
|
});
|
|
|
|
it('PLACES-RPC-008 every write broadcasts its event, a refused one broadcasts nothing', async () => {
|
|
const f = build();
|
|
const host = f.host('db:write:places');
|
|
await host.dispatch(req('places.create', { tripId: 1, input: { name: 'Shrine' } }), 42);
|
|
await host.dispatch(req('places.update', { tripId: 1, placeId: 7, input: { name: 'x' } }), 42);
|
|
await host.dispatch(req('places.delete', { tripId: 1, placeId: 7 }), 42);
|
|
expect(f.realtime.broadcast.mock.calls.map((c) => c[1])).toEqual(['place:created', 'place:updated', 'place:deleted']);
|
|
f.realtime.broadcast.mockClear();
|
|
await host.dispatch(req('places.delete', { tripId: 1, placeId: 404 }), 42);
|
|
expect(f.realtime.broadcast).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('PLACES-RPC-009 without the edit right nothing is written', async () => {
|
|
const f = build({ canEdit: false });
|
|
const res = (await f.host('db:write:places').dispatch(req('places.create', { tripId: 1, input: { name: 'x' } }), 42)) as RpcError;
|
|
expect(res.error.message).toBe('no permission to edit trip 1');
|
|
expect(f.places.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('PLACES-RPC-010 the class is listed in its module providers', () => {
|
|
expectRegisteredProvider(PlacesModule, PlacesRpc);
|
|
});
|
|
});
|