mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 13:36:48 +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.
103 lines
5.0 KiB
TypeScript
103 lines
5.0 KiB
TypeScript
/**
|
|
* DayScheduleController (#plugins): bounded time contributions plugins attach to
|
|
* the day plan via the dayScheduleProvider hook. Mirrors the map-markers
|
|
* hardening — gate + fail-safe + server-side normalization — plus the schedule
|
|
* specifics: dayIds are validated against the trip's own days and minutes are
|
|
* clamped to a day, because this output feeds displayed timing totals.
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
const { canAccessTrip, pluginsEnabled, tripDays } = vi.hoisted(() => ({
|
|
canAccessTrip: vi.fn((tripId: number, userId: number) => (tripId === 1 && userId === 5 ? { id: 1 } : undefined)),
|
|
pluginsEnabled: vi.fn(() => true),
|
|
tripDays: { value: [{ id: 10 }, { id: 11 }] as Array<{ id: number }> },
|
|
}));
|
|
vi.mock('../../../src/db/database', () => ({
|
|
db: { prepare: () => ({ all: () => tripDays.value }) },
|
|
canAccessTrip,
|
|
}));
|
|
import { db as dbConn } from '../../../src/db/database';
|
|
import { DatabaseService } from '../../../src/nest/database/database.service';
|
|
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
|
|
|
|
import { DayScheduleController } from '../../../src/nest/plugins/contributions/day-schedule.controller';
|
|
import type { PluginHooks } from '../../../src/nest/plugins/plugin-hooks.service';
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const req = (id?: number) => ({ user: id === undefined ? undefined : { id } }) as any;
|
|
function controller(invoke: (id: string) => unknown, providers = ['p1']) {
|
|
const runtime = {
|
|
providersOf: vi.fn(() => providers),
|
|
daySchedule: vi.fn(async (id: string) => invoke(id)),
|
|
} as unknown as PluginHooks;
|
|
return { c: new DayScheduleController(runtime, new DatabaseService(dbConn)), runtime };
|
|
}
|
|
const item = (over: Record<string, unknown> = {}) => ({ id: 's1', dayId: 10, label: 'Charging', ...over });
|
|
|
|
describe('DayScheduleController', () => {
|
|
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ id: 1 } as never); });
|
|
|
|
it('gates: disabled / no user / non-member all return [] (no plugin calls on the first)', async () => {
|
|
pluginsEnabled.mockReturnValue(false);
|
|
const x = controller(() => [item()]);
|
|
expect(await x.c.get('1', req(5))).toEqual({ items: [] });
|
|
expect(x.runtime.providersOf).not.toHaveBeenCalled();
|
|
pluginsEnabled.mockReturnValue(true);
|
|
|
|
expect((await controller(() => [item()]).c.get('1', req(undefined))).items).toEqual([]);
|
|
canAccessTrip.mockReturnValue(undefined as never);
|
|
expect((await controller(() => [item()]).c.get('1', req(5))).items).toEqual([]);
|
|
});
|
|
|
|
it('keeps a valid item, stamps pluginId, rounds + clamps minutes, defaults the tone', async () => {
|
|
const { c } = controller(() => [
|
|
item({ assignmentId: 42, minutes: 35.6, tone: 'success' }),
|
|
item({ id: 's2', minutes: 999999, position: 'start' }),
|
|
]);
|
|
const out = (await c.get('1', req(5))).items;
|
|
expect(out[0]).toMatchObject({ pluginId: 'p1', dayId: 10, assignmentId: 42, minutes: 36, tone: 'success' });
|
|
expect(out[1]).toMatchObject({ minutes: 1440, position: 'start', tone: 'default' });
|
|
});
|
|
|
|
it("drops items anchored to another trip's day, without id/label, or non-objects", async () => {
|
|
const { c } = controller(() => [
|
|
item({ dayId: 999 }), // not a day of this trip
|
|
item({ id: '' }), // no id
|
|
item({ label: '' }), // no label
|
|
item({ dayId: 'x' }), // non-numeric day
|
|
null, // non-object
|
|
item({ id: 'good', dayId: 11 }),
|
|
]);
|
|
const out = (await c.get('1', req(5))).items;
|
|
expect(out.map(i => i.id)).toEqual(['good']);
|
|
});
|
|
|
|
it('ignores bogus anchors and positions instead of failing the item', async () => {
|
|
const { c } = controller(() => [item({ assignmentId: 'x', reservationId: 1.5, position: 'middle', minutes: -10 })]);
|
|
const out = (await c.get('1', req(5))).items;
|
|
expect(out[0].assignmentId).toBeUndefined();
|
|
expect(out[0].reservationId).toBeUndefined();
|
|
expect(out[0].position).toBeUndefined();
|
|
expect(out[0].minutes).toBeUndefined();
|
|
});
|
|
|
|
it('caps items at 60 per provider and skips a failing provider', async () => {
|
|
const many = Array.from({ length: 80 }, (_, i) => item({ id: `s${i}` }));
|
|
const { c } = controller((id) => (id === 'bad' ? (() => { throw new Error('boom'); })() : many), ['good', 'bad']);
|
|
const out = (await c.get('1', req(5))).items;
|
|
expect(out).toHaveLength(60);
|
|
});
|
|
|
|
it('bounds work on an all-invalid oversized payload', async () => {
|
|
const huge = Array.from({ length: 100_000 }, () => ({ id: 's', dayId: 999, label: 'x' })); // foreign day -> all dropped
|
|
const { c } = controller(() => huge);
|
|
expect((await c.get('1', req(5))).items).toEqual([]);
|
|
});
|
|
|
|
it('skips the day lookup entirely when no provider is active', async () => {
|
|
const { c, runtime } = controller(() => [item()], []);
|
|
expect((await c.get('1', req(5))).items).toEqual([]);
|
|
expect(runtime.daySchedule).not.toHaveBeenCalled();
|
|
});
|
|
});
|