mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26: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.
81 lines
4.2 KiB
TypeScript
81 lines
4.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
const { canAccessTrip, pluginsEnabled } = vi.hoisted(() => ({
|
|
canAccessTrip: vi.fn((tripId: number, userId: number) => (tripId === 1 && userId === 5 ? { id: 1 } : undefined)),
|
|
pluginsEnabled: vi.fn(() => true),
|
|
}));
|
|
vi.mock('../../../src/db/database', () => ({ db: {}, canAccessTrip }));
|
|
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
|
|
|
|
import { TripWarningsController } from '../../../src/nest/plugins/contributions/trip-warnings.controller';
|
|
import type { PluginHooks } from '../../../src/nest/plugins/plugin-hooks.service';
|
|
import type { DatabaseService } from '../../../src/nest/database/database.service';
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const req = (id?: number) => ({ user: id === undefined ? undefined : { id } }) as any;
|
|
function controller(over: Partial<PluginHooks> = {}) {
|
|
const runtime = {
|
|
providersOf: vi.fn(() => ['p1', 'p2']),
|
|
tripWarnings: vi.fn(async (id: string) =>
|
|
id === 'p2' ? [{ level: 'error', message: 'Day 3 is overpacked', dayId: 3 }] : [{ level: 'warning', message: 'Museum closed Mon', placeId: 7 }]),
|
|
...over,
|
|
} as unknown as PluginHooks;
|
|
return { c: new TripWarningsController(runtime, { canAccessTrip } as unknown as DatabaseService), runtime };
|
|
}
|
|
|
|
describe('TripWarningsController', () => {
|
|
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ id: 1 } as never); });
|
|
|
|
it('returns [] when disabled / no user / no access', async () => {
|
|
pluginsEnabled.mockReturnValue(false);
|
|
expect(await controller().c.get('1', req(5))).toEqual({ warnings: [] });
|
|
pluginsEnabled.mockReturnValue(true);
|
|
expect(await controller().c.get('1', req(undefined))).toEqual({ warnings: [] });
|
|
canAccessTrip.mockReturnValue(undefined as never);
|
|
expect(await controller().c.get('1', req(5))).toEqual({ warnings: [] });
|
|
});
|
|
|
|
it('merges provider warnings, tags each with the plugin id, defaults an odd level to warning', async () => {
|
|
const { c } = controller({
|
|
tripWarnings: vi.fn(async (id: string) =>
|
|
id === 'p1' ? [{ level: 'bogus', message: 'Check timings' }, { message: '' }] : [{ level: 'info', message: 'Rainy week' }]) as unknown as PluginHooks['tripWarnings'],
|
|
});
|
|
const res = await c.get('1', req(5));
|
|
expect(res.warnings).toEqual([
|
|
{ pluginId: 'p1', level: 'warning', message: 'Check timings', dayId: undefined, placeId: undefined }, // unknown level → warning; empty-message row dropped
|
|
{ pluginId: 'p2', level: 'info', message: 'Rainy week', dayId: undefined, placeId: undefined },
|
|
]);
|
|
});
|
|
|
|
it('skips a provider that throws (graceful) and calls the hook with the trip + acting user', async () => {
|
|
const { c, runtime } = controller({
|
|
tripWarnings: vi.fn(async (id: string) => { if (id === 'p2') throw new Error('slow'); return [{ level: 'warning', message: 'ok' }]; }) as unknown as PluginHooks['tripWarnings'],
|
|
});
|
|
const res = await c.get('1', req(5));
|
|
expect(res.warnings).toEqual([{ pluginId: 'p1', level: 'warning', message: 'ok', dayId: undefined, placeId: undefined }]);
|
|
expect(runtime.tripWarnings).toHaveBeenCalledWith('p1', 1, 5);
|
|
});
|
|
|
|
it('strips emojis from the warning message (rendered natively in TREK chrome)', async () => {
|
|
const { c } = controller({
|
|
providersOf: vi.fn(() => ['p1']),
|
|
tripWarnings: vi.fn(async () => [{ level: 'error', message: '🔥 Overbooked!' }]) as unknown as PluginHooks['tripWarnings'],
|
|
});
|
|
const res = await c.get('1', req(5));
|
|
expect(res.warnings[0].message).toBe('Overbooked!');
|
|
});
|
|
|
|
it('caps a flooding provider at 20 warnings and truncates an oversized message', async () => {
|
|
const { c } = controller({
|
|
providersOf: vi.fn(() => ['flood']),
|
|
tripWarnings: vi.fn(async () => [
|
|
{ level: 'warning', message: 'z'.repeat(1000) },
|
|
...Array.from({ length: 50 }, (_v, i) => ({ level: 'info', message: `w${i}` })),
|
|
]) as unknown as PluginHooks['tripWarnings'],
|
|
});
|
|
const res = await c.get('1', req(5));
|
|
expect(res.warnings).toHaveLength(20); // per-provider count cap
|
|
expect(res.warnings[0].message).toHaveLength(300); // message length cap
|
|
});
|
|
});
|