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
@@ -4,20 +4,26 @@
* an admin depends on: re-activating a plugin that died stays possible, and a
* crash-restart cycle doesn't leak the dead child's cron tasks.
*/
import { describe, it, expect, vi, beforeAll, afterAll, afterEach } from 'vitest';
import Database from 'better-sqlite3';
import { PluginSupervisor } from '../../../src/nest/plugins/supervisor/plugin-supervisor';
import { RpcRateLimiter, TokenBucket } from '../../../src/nest/plugins/host/rate-limit';
import { PluginSupervisor } from '../../../src/nest/plugins/supervisor/plugin-supervisor';
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
import Database from 'better-sqlite3';
import { describe, it, expect, vi, beforeAll, afterAll, afterEach } from 'vitest';
// task-6-fix-brief.md item 1: onMessage's 'req' branch now THROWS without a
// resolveOrm thunk (no more silent unwrapped dispatch), so the one case below
// that dispatches a real 'req' message ("refuses a throttled call...") needs a
// real ORM even though nothing it exercises reaches a repository.
const ormDb = new Database(':memory:');
let ormHandle: TestOrm;
beforeAll(async () => { ormHandle = await createTestOrm(ormDb); });
afterAll(async () => { await ormHandle.close(); ormDb.close(); });
beforeAll(async () => {
ormHandle = await createTestOrm(ormDb);
});
afterAll(async () => {
await ormHandle.close();
ormDb.close();
});
function makeSupervisor() {
const dispose = vi.fn();
@@ -34,48 +40,79 @@ const entry = (s: PluginSupervisor, id: string): { status: string } => (s as any
describe('supervisor re-activation after failure', () => {
const supers: PluginSupervisor[] = [];
afterEach(() => { for (const s of supers) { void s.shutdownAll().catch(() => {}); } supers.length = 0; });
afterEach(() => {
for (const s of supers) {
void s.shutdownAll().catch(() => {});
}
supers.length = 0;
});
it('re-activating a plugin left in error state re-spawns instead of silently no-op-ing', () => {
const { s, spawn } = makeSupervisor();
supers.push(s);
// A prior crash-auto-disable / load-error leaves a dead entry in running.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).running.set('p', { id: 'p', status: 'error', jobTasks: undefined, pending: new Map(), invocations: new Map(), crashes: [] });
(s as any).running.set('p', {
id: 'p',
status: 'error',
jobTasks: undefined,
pending: new Map(),
invocations: new Map(),
crashes: [],
});
void s.activate('p', new Set());
expect(spawn).toHaveBeenCalledTimes(1); // it re-spawned, not no-op
expect(entry(s, 'p').status).toBe('starting'); // fresh entry replaced the dead one
expect(spawn).toHaveBeenCalledTimes(1); // it re-spawned, not no-op
expect(entry(s, 'p').status).toBe('starting'); // fresh entry replaced the dead one
});
it('re-activating a LIVE plugin is an idempotent no-op', () => {
const { s, spawn } = makeSupervisor();
supers.push(s);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).running.set('p', { id: 'p', status: 'active', jobTasks: undefined, pending: new Map(), invocations: new Map(), crashes: [] });
(s as any).running.set('p', {
id: 'p',
status: 'active',
jobTasks: undefined,
pending: new Map(),
invocations: new Map(),
crashes: [],
});
void s.activate('p', new Set());
expect(spawn).not.toHaveBeenCalled();
expect(entry(s, 'p').status).toBe('active'); // untouched
expect(entry(s, 'p').status).toBe('active'); // untouched
});
});
describe('supervisor shutdownAll is a clean stop, not a crash', () => {
it('marks plugins stopped BEFORE killing, so a child exit during shutdown logs no phantom crash', async () => {
const onStatus = vi.fn(); const onLog = vi.fn();
const onStatus = vi.fn();
const onLog = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const s = new PluginSupervisor((() => ({ dispose: vi.fn() })) as any, { onStatus, onLog }, {});
const sup = { id: 'p', status: 'active', child: {}, rpcHost: { dispose: vi.fn() }, crashes: [], jobTasks: undefined, pending: new Map(), invocations: new Map() };
const sup = {
id: 'p',
status: 'active',
child: {},
rpcHost: { dispose: vi.fn() },
crashes: [],
jobTasks: undefined,
pending: new Map(),
invocations: new Map(),
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).running.set('p', sup);
// kill() simulates the child actually exiting during shutdown → fires onExit
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const internals = s as any;
internals.kill = vi.fn(async (x: unknown) => { internals.onExit(x, 0, 'SIGTERM'); });
internals.kill = vi.fn(async (x: unknown) => {
internals.onExit(x, 0, 'SIGTERM');
});
await s.shutdownAll();
expect(sup.status).toBe('stopped'); // stopped before kill
expect(sup.status).toBe('stopped'); // stopped before kill
// onExit took the early-return path — no crash bookkeeping written to the DB hooks
expect(onLog).not.toHaveBeenCalledWith('p', 'warn', expect.stringContaining('crashed'));
expect(onStatus).not.toHaveBeenCalledWith('p', 'error', expect.anything());
@@ -84,12 +121,17 @@ describe('supervisor shutdownAll is a clean stop, not a crash', () => {
});
describe('supervisor crash-restart does not leak cron tasks', () => {
it('onExit stops the dead child\'s jobTasks before re-scheduling', () => {
it("onExit stops the dead child's jobTasks before re-scheduling", () => {
const { s } = makeSupervisor();
const stop = vi.fn();
const sup = {
id: 'p', status: 'active', child: {}, jobTasks: [{ stop }],
pending: new Map(), invocations: new Map(), crashes: [],
id: 'p',
status: 'active',
child: {},
jobTasks: [{ stop }],
pending: new Map(),
invocations: new Map(),
crashes: [],
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).running.set('p', sup);
@@ -97,14 +139,17 @@ describe('supervisor crash-restart does not leak cron tasks', () => {
(s as any).onExit(sup, 1, null);
expect(stop).toHaveBeenCalledTimes(1); // the previous incarnation's tasks were stopped
expect(sup.jobTasks).toBeUndefined(); // reference cleared so it can't be double-stopped
expect(sup.jobTasks).toBeUndefined(); // reference cleared so it can't be double-stopped
});
});
describe('supervisor buffers events across a restart and replays on activation', () => {
// A subscriber with a grant + one subscription, at an arbitrary status.
const sub = (status: string) => ({
id: 'p', status, granted: new Set(['events:subscribe']), events: ['trip:updated'],
id: 'p',
status,
granted: new Set(['events:subscribe']),
events: ['trip:updated'],
});
it('an active subscriber gets the event immediately, nothing buffered', () => {
@@ -130,14 +175,14 @@ describe('supervisor buffers events across a restart and replays on activation',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).running.set('p', sup);
s.deliverEvent(7, 'trip:updated');
expect(invoke).not.toHaveBeenCalled(); // held, not delivered mid-restart
expect(invoke).not.toHaveBeenCalled(); // held, not delivered mid-restart
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((s as any).pendingEvents.get('p')).toHaveLength(1);
// plugin comes back
sup.status = 'active';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).flushPendingEvents(sup);
expect(invoke).toHaveBeenCalledTimes(1); // replayed exactly once
expect(invoke).toHaveBeenCalledTimes(1); // replayed exactly once
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((s as any).pendingEvents.get('p')).toBeUndefined(); // buffer drained
});
@@ -169,12 +214,12 @@ describe('supervisor buffers events across a restart and replays on activation',
]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).flushPendingEvents(sup);
expect(invoke).toHaveBeenCalledTimes(1); // only the unexpired one
expect(invoke).toHaveBeenCalledTimes(1); // only the unexpired one
// grant revoked while down → flush delivers nothing
invoke.mockClear();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).pendingEvents.set('p', [{ tripId: 1, event: 'trip:updated', expiresAt: Date.now() + 60_000 }]);
sup.granted = new Set(); // grant gone
sup.granted = new Set(); // grant gone
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).flushPendingEvents(sup);
expect(invoke).not.toHaveBeenCalled();
@@ -189,14 +234,24 @@ describe('supervisor buffers events across a restart and replays on activation',
for (let i = 0; i < 250; i++) s.deliverEvent(i, 'trip:updated');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const q = (s as any).pendingEvents.get('p');
expect(q).toHaveLength(200); // capped
expect(q[0].tripId).toBe(50); // oldest 50 dropped
expect(q).toHaveLength(200); // capped
expect(q[0].tripId).toBe(50); // oldest 50 dropped
});
it('disable() clears a plugin\'s buffered events', () => {
it("disable() clears a plugin's buffered events", () => {
const { s } = makeSupervisor();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).running.set('p', { id: 'p', status: 'starting', granted: new Set(), events: [], jobTasks: undefined, pending: new Map(), invocations: new Map(), crashes: [], rpcHost: { dispose: vi.fn() } });
(s as any).running.set('p', {
id: 'p',
status: 'starting',
granted: new Set(),
events: [],
jobTasks: undefined,
pending: new Map(),
invocations: new Map(),
crashes: [],
rpcHost: { dispose: vi.fn() },
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).pendingEvents.set('p', [{ tripId: 1, event: 'trip:updated', expiresAt: Date.now() + 60_000 }]);
void s.disable('p');
@@ -216,7 +271,12 @@ describe('supervisor GDPR user-data hooks are grant- and status-gated', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).running.set('p', withGrant('active'));
expect(await s.deliverUserErasure('p', 42)).toBe(true);
expect(invoke).toHaveBeenCalledWith('p', 'invoke.deleteUserData', { userId: 42 }, expect.objectContaining({ actingUserId: undefined }));
expect(invoke).toHaveBeenCalledWith(
'p',
'invoke.deleteUserData',
{ userId: 42 },
expect.objectContaining({ actingUserId: undefined }),
);
// inactive → not delivered, not acked (stays queued by the caller)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(s as any).running.set('p', withGrant('starting'));
@@ -265,8 +325,12 @@ describe('supervisor rate-limits ctx.* dispatch', () => {
const dispatch = vi.fn(async () => ({ k: 'res', id: 'x', ok: true, result: 1 }));
const send = vi.fn();
const sup = {
id: 'p', status: 'active', child: { send }, rpcHost: { dispatch },
invocations: new Map(), pending: new Map(),
id: 'p',
status: 'active',
child: { send },
rpcHost: { dispatch },
invocations: new Map(),
pending: new Map(),
rpcLimiter: new RpcRateLimiter({ burst: 1, perSec: 0, maxInFlight: 8 }, 0),
};
const req = { k: 'req', id: 'r1', method: 'db.query', params: {} };