mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
724c88c201
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.
84 lines
3.4 KiB
TypeScript
84 lines
3.4 KiB
TypeScript
/**
|
|
* The dev-link controller endpoints (#plugins): POST /link and POST /:id/reload.
|
|
* Verifies both are gated by the kill-switch AND the dev-only TREK_PLUGINS_DEV_LINK
|
|
* flag, that link validates its body, and that reload maps a re-consent error to 409
|
|
* exactly like activate does. The runtime is mocked — this is the HTTP glue only.
|
|
*/
|
|
import type { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
|
import { PluginConsentRequired } from '../../../src/nest/plugins/plugin-runtime.service';
|
|
import { PluginsController } from '../../../src/nest/plugins/plugins.controller';
|
|
import { HttpException } from '@nestjs/common';
|
|
|
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const ctrl = (runtime: any) =>
|
|
new PluginsController({} as never, runtime, {} as never, { isManaged: () => false } as unknown as RuntimeEnvService);
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const status = async (p: Promise<any>): Promise<number> =>
|
|
p.then(
|
|
() => -1,
|
|
(e) => (e instanceof HttpException ? e.getStatus() : 0),
|
|
);
|
|
|
|
describe('PluginsController dev-link endpoints', () => {
|
|
beforeEach(() => {
|
|
process.env.TREK_PLUGINS_ENABLED = 'true';
|
|
});
|
|
afterEach(() => {
|
|
delete process.env.TREK_PLUGINS_DEV_LINK;
|
|
delete process.env.TREK_PLUGINS_ENABLED;
|
|
});
|
|
|
|
it('POST /link is 403 unless dev-link is enabled, 400 without a path, and delegates when enabled', async () => {
|
|
const runtime = { link: vi.fn(async () => ({ id: 'x', version: '1.0.0', replaced: false })) };
|
|
delete process.env.TREK_PLUGINS_DEV_LINK;
|
|
expect(await status(ctrl(runtime).link({ path: '/abs/dir' }))).toBe(403);
|
|
expect(runtime.link).not.toHaveBeenCalled();
|
|
|
|
process.env.TREK_PLUGINS_DEV_LINK = '1';
|
|
expect(await status(ctrl(runtime).link({}))).toBe(400); // no path
|
|
expect(await ctrl(runtime).link({ path: ' /abs/dir ' })).toMatchObject({ id: 'x' });
|
|
expect(runtime.link).toHaveBeenCalledWith('/abs/dir'); // trimmed
|
|
});
|
|
|
|
it('POST /link surfaces a runtime error as 400', async () => {
|
|
process.env.TREK_PLUGINS_DEV_LINK = '1';
|
|
const runtime = {
|
|
link: vi.fn(async () => {
|
|
throw new Error('no build');
|
|
}),
|
|
};
|
|
expect(await status(ctrl(runtime).link({ path: '/abs' }))).toBe(400);
|
|
});
|
|
|
|
it('POST /:id/reload is 403 unless dev-link is enabled', async () => {
|
|
const runtime = { reload: vi.fn(async () => {}), isActive: () => true };
|
|
delete process.env.TREK_PLUGINS_DEV_LINK;
|
|
expect(await status(ctrl(runtime).reload('p'))).toBe(403);
|
|
expect(runtime.reload).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('POST /:id/reload maps a re-consent error to 409 and otherwise returns the live status', async () => {
|
|
process.env.TREK_PLUGINS_DEV_LINK = '1';
|
|
const consent = {
|
|
reload: vi.fn(async () => {
|
|
throw new PluginConsentRequired('needs consent', ['db:read:trips'], []);
|
|
}),
|
|
isActive: () => false,
|
|
};
|
|
expect(await status(ctrl(consent).reload('p'))).toBe(409);
|
|
|
|
const ok = { reload: vi.fn(async () => {}), isActive: () => true };
|
|
expect(await ctrl(ok).reload('p')).toEqual({ status: 'active' });
|
|
|
|
const bad = {
|
|
reload: vi.fn(async () => {
|
|
throw new Error('boom');
|
|
}),
|
|
isActive: () => false,
|
|
};
|
|
expect(await status(ctrl(bad).reload('p'))).toBe(400);
|
|
});
|
|
});
|