mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
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:
@@ -2,6 +2,24 @@
|
||||
* The read-side plugin service + controller (#plugins, M0). Lists installed
|
||||
* plugins and reports whether the runtime is enabled (TREK_PLUGINS_ENABLED).
|
||||
*/
|
||||
import { db as testDb } from '../../../src/db/database';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
import { PluginCapabilityAudit } from '../../../src/db/entities/PluginCapabilityAudit.entity';
|
||||
import { PluginEgressHosts } from '../../../src/db/entities/PluginEgressHosts.entity';
|
||||
import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity';
|
||||
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
|
||||
import { PluginUserConfig } from '../../../src/db/entities/PluginUserConfig.entity';
|
||||
import { Plugins } from '../../../src/db/entities/Plugins.entity';
|
||||
import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
||||
import type { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
||||
import { PluginsFeedController } from '../../../src/nest/plugins/plugins-feed.controller';
|
||||
import { PluginsController } from '../../../src/nest/plugins/plugins.controller';
|
||||
import { PluginsService } from '../../../src/nest/plugins/plugins.service';
|
||||
import { deleteRows, findRow, insertRow, updateRows } from '../../helpers/factories/rows';
|
||||
import { createTestAddonsService } from '../../helpers/test-addons';
|
||||
import type { TestOrm } from '../../helpers/test-orm';
|
||||
import { sharedTestOrm } from '../../helpers/test-uow';
|
||||
|
||||
import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from 'vitest';
|
||||
|
||||
vi.mock('../../../src/db/database', async () => {
|
||||
@@ -9,24 +27,6 @@ vi.mock('../../../src/db/database', async () => {
|
||||
const db = createSnapshotTestDb();
|
||||
return { db };
|
||||
});
|
||||
import { db as testDb } from '../../../src/db/database';
|
||||
|
||||
import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
||||
import { createTestAddonsService } from '../../helpers/test-addons';
|
||||
import { sharedTestOrm } from '../../helpers/test-uow';
|
||||
import type { TestOrm } from '../../helpers/test-orm';
|
||||
import { deleteRows, findRow, insertRow, updateRows } from '../../helpers/factories/rows';
|
||||
import { Plugins } from '../../../src/db/entities/Plugins.entity';
|
||||
import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity';
|
||||
import { PluginEgressHosts } from '../../../src/db/entities/PluginEgressHosts.entity';
|
||||
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
import { PluginUserConfig } from '../../../src/db/entities/PluginUserConfig.entity';
|
||||
import { PluginCapabilityAudit } from '../../../src/db/entities/PluginCapabilityAudit.entity';
|
||||
import { PluginsService } from '../../../src/nest/plugins/plugins.service';
|
||||
import { PluginsController } from '../../../src/nest/plugins/plugins.controller';
|
||||
import { PluginsFeedController } from '../../../src/nest/plugins/plugins-feed.controller';
|
||||
import type { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
||||
|
||||
// AddonsService only feeds PluginsService.list()'s required-addon-dependency
|
||||
// resolution; none of the fixtures below declare any, so it is built once
|
||||
@@ -85,7 +85,14 @@ afterEach(() => {
|
||||
|
||||
describe('PluginsService.list', () => {
|
||||
it('returns the installed plugins and the runtime-enabled flag', async () => {
|
||||
await insertRow(orm, Plugins, { id: 'flight', name: 'Flight', description: 'desc', type: 'widget', status: 'inactive', version: '1.0.0' });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'flight',
|
||||
name: 'Flight',
|
||||
description: 'desc',
|
||||
type: 'widget',
|
||||
status: 'inactive',
|
||||
version: '1.0.0',
|
||||
});
|
||||
process.env.TREK_PLUGINS_ENABLED = 'true';
|
||||
|
||||
const out = await (await makeService()).list();
|
||||
@@ -102,7 +109,14 @@ describe('PluginsService.list', () => {
|
||||
else process.env.APP_VERSION = APP_VERSION;
|
||||
});
|
||||
const seed = () =>
|
||||
insertRow(orm, Plugins, { id: 'old', name: 'Old', type: 'widget', status: 'inactive', version: '1.0.0', trek_range: '>=3.0.0 <4.0.0' });
|
||||
insertRow(orm, Plugins, {
|
||||
id: 'old',
|
||||
name: 'Old',
|
||||
type: 'widget',
|
||||
status: 'inactive',
|
||||
version: '1.0.0',
|
||||
trek_range: '>=3.0.0 <4.0.0',
|
||||
});
|
||||
|
||||
it('reports the switch off and an outgrown plugin as hostIncompatible by default', async () => {
|
||||
process.env.APP_VERSION = '4.1.0';
|
||||
@@ -134,7 +148,14 @@ describe('PluginsService.list', () => {
|
||||
});
|
||||
|
||||
it('surfaces updateHold as a boolean (held plugins leave the update banner)', async () => {
|
||||
await insertRow(orm, Plugins, { id: 'held', name: 'Held', type: 'widget', status: 'inactive', version: '1.0.0', update_hold: 1 });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'held',
|
||||
name: 'Held',
|
||||
type: 'widget',
|
||||
status: 'inactive',
|
||||
version: '1.0.0',
|
||||
update_hold: 1,
|
||||
});
|
||||
await insertRow(orm, Plugins, { id: 'free', name: 'Free', type: 'widget', status: 'inactive', version: '1.0.0' });
|
||||
|
||||
const out = await (await makeService()).list();
|
||||
@@ -143,7 +164,14 @@ describe('PluginsService.list', () => {
|
||||
});
|
||||
|
||||
it('resumeUpdates clears the hold and reports whether the plugin existed', async () => {
|
||||
await insertRow(orm, Plugins, { id: 'held', name: 'Held', type: 'widget', status: 'inactive', version: '1.0.0', update_hold: 1 });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'held',
|
||||
name: 'Held',
|
||||
type: 'widget',
|
||||
status: 'inactive',
|
||||
version: '1.0.0',
|
||||
update_hold: 1,
|
||||
});
|
||||
const svc = await makeService();
|
||||
|
||||
expect(await svc.resumeUpdates('held')).toBe(true);
|
||||
@@ -152,7 +180,14 @@ describe('PluginsService.list', () => {
|
||||
});
|
||||
|
||||
it('reports enabled by default (no kill switch set)', async () => {
|
||||
await insertRow(orm, Plugins, { id: 'flight', name: 'Flight', description: 'desc', type: 'widget', status: 'inactive', version: '1.0.0' });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'flight',
|
||||
name: 'Flight',
|
||||
description: 'desc',
|
||||
type: 'widget',
|
||||
status: 'inactive',
|
||||
version: '1.0.0',
|
||||
});
|
||||
|
||||
const out = await (await makeService()).list();
|
||||
expect(out.enabled).toBe(true);
|
||||
@@ -174,7 +209,13 @@ describe('PluginsService.list', () => {
|
||||
describe('signature status', () => {
|
||||
const insert = (id: string, sourceRepo: string | null, pubkey: string | null) =>
|
||||
insertRow(orm, Plugins, {
|
||||
id, name: id, type: 'widget', status: 'inactive', version: '1.0.0', source_repo: sourceRepo, author_pubkey: pubkey,
|
||||
id,
|
||||
name: id,
|
||||
type: 'widget',
|
||||
status: 'inactive',
|
||||
version: '1.0.0',
|
||||
source_repo: sourceRepo,
|
||||
author_pubkey: pubkey,
|
||||
});
|
||||
|
||||
it('reports signed + a display fingerprint for a registry plugin with a pinned key', async () => {
|
||||
@@ -209,12 +250,23 @@ describe('PluginsService.list', () => {
|
||||
it('surfaces a recorded update block, and reports none when there is none', async () => {
|
||||
await insert('blocked', 'acme/blocked', 'RWTvBn0aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789abcd');
|
||||
await insert('fine', 'acme/fine', null);
|
||||
await updateRows(orm, Plugins, { id: 'blocked' }, {
|
||||
update_block_code: 'SIGNATURE_KEY_CHANGED', update_block_detail: 'the key changed', update_block_version: '2.0.0',
|
||||
});
|
||||
await updateRows(
|
||||
orm,
|
||||
Plugins,
|
||||
{ id: 'blocked' },
|
||||
{
|
||||
update_block_code: 'SIGNATURE_KEY_CHANGED',
|
||||
update_block_detail: 'the key changed',
|
||||
update_block_version: '2.0.0',
|
||||
},
|
||||
);
|
||||
|
||||
const byId = Object.fromEntries((await (await makeService()).list()).plugins.map((p) => [p.id, p]));
|
||||
expect(byId.blocked.updateBlock).toEqual({ code: 'SIGNATURE_KEY_CHANGED', detail: 'the key changed', version: '2.0.0' });
|
||||
expect(byId.blocked.updateBlock).toEqual({
|
||||
code: 'SIGNATURE_KEY_CHANGED',
|
||||
detail: 'the key changed',
|
||||
version: '2.0.0',
|
||||
});
|
||||
expect(byId.fine.updateBlock).toBeNull();
|
||||
});
|
||||
|
||||
@@ -223,8 +275,7 @@ describe('PluginsService.list', () => {
|
||||
// PluginListItem is an interface, so it has no implicit index signature and cannot
|
||||
// be narrowed to a record directly. Widening through unknown is what lets this case
|
||||
// probe for a key the contract deliberately does not declare.
|
||||
const p = (await (await makeService()).list())
|
||||
.plugins[0] as unknown as Record<string, unknown>;
|
||||
const p = (await (await makeService()).list()).plugins[0] as unknown as Record<string, unknown>;
|
||||
expect(p.author_pubkey).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -232,7 +283,12 @@ describe('PluginsService.list', () => {
|
||||
it('controller delegates to the service', async () => {
|
||||
const svc = { list: vi.fn(async () => ({ enabled: false, plugins: [] })) } as unknown as PluginsService;
|
||||
const runtime = {} as unknown as import('../../../src/nest/plugins/plugin-runtime.service').PluginRuntimeService;
|
||||
const res = await new PluginsController(svc, runtime, {} as never, { isManaged: () => false } as unknown as RuntimeEnvService).list();
|
||||
const res = await new PluginsController(
|
||||
svc,
|
||||
runtime,
|
||||
{} as never,
|
||||
{ isManaged: () => false } as unknown as RuntimeEnvService,
|
||||
).list();
|
||||
expect(svc.list).toHaveBeenCalled();
|
||||
expect(res).toEqual({ enabled: false, plugins: [] });
|
||||
});
|
||||
@@ -253,8 +309,22 @@ describe('PluginsFeedController (client feed)', () => {
|
||||
});
|
||||
|
||||
it('exposes the widget slot from capabilities (hero) and defaults on bad JSON', async () => {
|
||||
await insertRow(orm, Plugins, { id: 'h', name: 'H', type: 'widget', icon: 'Box', status: 'active', capabilities: '{"widget":{"slot":"hero"}}' });
|
||||
await insertRow(orm, Plugins, { id: 'b', name: 'B', type: 'widget', icon: 'Box', status: 'active', capabilities: 'not-json' });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'h',
|
||||
name: 'H',
|
||||
type: 'widget',
|
||||
icon: 'Box',
|
||||
status: 'active',
|
||||
capabilities: '{"widget":{"slot":"hero"}}',
|
||||
});
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'b',
|
||||
name: 'B',
|
||||
type: 'widget',
|
||||
icon: 'Box',
|
||||
status: 'active',
|
||||
capabilities: 'not-json',
|
||||
});
|
||||
process.env.TREK_PLUGINS_ENABLED = 'true';
|
||||
const out = await (await makeFeedController()).list();
|
||||
expect(out.plugins.find((p) => p.id === 'h')?.slot).toBe('hero');
|
||||
@@ -262,14 +332,35 @@ describe('PluginsFeedController (client feed)', () => {
|
||||
});
|
||||
|
||||
it('exposes the day-detail slot (a day-panel widget must not fall back to the dashboard)', async () => {
|
||||
await insertRow(orm, Plugins, { id: 'd', name: 'D', type: 'widget', icon: 'Box', status: 'active', capabilities: '{"widget":{"slot":"day-detail"}}' });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'd',
|
||||
name: 'D',
|
||||
type: 'widget',
|
||||
icon: 'Box',
|
||||
status: 'active',
|
||||
capabilities: '{"widget":{"slot":"day-detail"}}',
|
||||
});
|
||||
process.env.TREK_PLUGINS_ENABLED = 'true';
|
||||
expect((await (await makeFeedController()).list()).plugins.find((p) => p.id === 'd')?.slot).toBe('day-detail');
|
||||
});
|
||||
|
||||
it('exposes settingsUi only when the capability is exactly true', async () => {
|
||||
await insertRow(orm, Plugins, { id: 'su', name: 'S', type: 'widget', icon: 'Box', status: 'active', capabilities: '{"settingsUi":true}' });
|
||||
await insertRow(orm, Plugins, { id: 'no', name: 'N', type: 'widget', icon: 'Box', status: 'active', capabilities: '{"settingsUi":"yes"}' });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'su',
|
||||
name: 'S',
|
||||
type: 'widget',
|
||||
icon: 'Box',
|
||||
status: 'active',
|
||||
capabilities: '{"settingsUi":true}',
|
||||
});
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'no',
|
||||
name: 'N',
|
||||
type: 'widget',
|
||||
icon: 'Box',
|
||||
status: 'active',
|
||||
capabilities: '{"settingsUi":"yes"}',
|
||||
});
|
||||
process.env.TREK_PLUGINS_ENABLED = 'true';
|
||||
const out = await (await makeFeedController()).list();
|
||||
expect(out.plugins.find((p) => p.id === 'su')?.settingsUi).toBe(true);
|
||||
@@ -277,20 +368,53 @@ describe('PluginsFeedController (client feed)', () => {
|
||||
});
|
||||
|
||||
it('exposes the reservation-detail slot (a booking-card widget must not fall back to the dashboard)', async () => {
|
||||
await insertRow(orm, Plugins, { id: 'r', name: 'R', type: 'widget', icon: 'Box', status: 'active', capabilities: '{"widget":{"slot":"reservation-detail"}}' });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'r',
|
||||
name: 'R',
|
||||
type: 'widget',
|
||||
icon: 'Box',
|
||||
status: 'active',
|
||||
capabilities: '{"widget":{"slot":"reservation-detail"}}',
|
||||
});
|
||||
process.env.TREK_PLUGINS_ENABLED = 'true';
|
||||
expect((await (await makeFeedController()).list()).plugins.find((p) => p.id === 'r')?.slot).toBe('reservation-detail');
|
||||
expect((await (await makeFeedController()).list()).plugins.find((p) => p.id === 'r')?.slot).toBe(
|
||||
'reservation-detail',
|
||||
);
|
||||
});
|
||||
|
||||
it('exposes tripPage for trip-page plugins, re-validated against the replaceable-tab whitelist', async () => {
|
||||
await insertRow(orm, Plugins, { id: 't', name: 'T', type: 'trip-page', icon: 'Box', status: 'active', capabilities: '{"tripPage":{"replaces":["transports","buchungen"],"position":1}}' });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 't',
|
||||
name: 'T',
|
||||
type: 'trip-page',
|
||||
icon: 'Box',
|
||||
status: 'active',
|
||||
capabilities: '{"tripPage":{"replaces":["transports","buchungen"],"position":1}}',
|
||||
});
|
||||
// a hand-edited row trying to hide 'plan' (or junk) is filtered here, not just at install
|
||||
await insertRow(orm, Plugins, { id: 'evil', name: 'E', type: 'trip-page', icon: 'Box', status: 'active', capabilities: '{"tripPage":{"replaces":["plan","nope"],"position":-3}}' });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'evil',
|
||||
name: 'E',
|
||||
type: 'trip-page',
|
||||
icon: 'Box',
|
||||
status: 'active',
|
||||
capabilities: '{"tripPage":{"replaces":["plan","nope"],"position":-3}}',
|
||||
});
|
||||
// the capability is meaningless off a trip-page and must not leak onto widgets
|
||||
await insertRow(orm, Plugins, { id: 'w2', name: 'W2', type: 'widget', icon: 'Box', status: 'active', capabilities: '{"tripPage":{"replaces":["transports"]}}' });
|
||||
await insertRow(orm, Plugins, {
|
||||
id: 'w2',
|
||||
name: 'W2',
|
||||
type: 'widget',
|
||||
icon: 'Box',
|
||||
status: 'active',
|
||||
capabilities: '{"tripPage":{"replaces":["transports"]}}',
|
||||
});
|
||||
process.env.TREK_PLUGINS_ENABLED = 'true';
|
||||
const out = await (await makeFeedController()).list();
|
||||
expect(out.plugins.find((p) => p.id === 't')?.tripPage).toEqual({ replaces: ['transports', 'buchungen'], position: 1 });
|
||||
expect(out.plugins.find((p) => p.id === 't')?.tripPage).toEqual({
|
||||
replaces: ['transports', 'buchungen'],
|
||||
position: 1,
|
||||
});
|
||||
expect(out.plugins.find((p) => p.id === 'evil')?.tripPage).toBeUndefined();
|
||||
expect(out.plugins.find((p) => p.id === 'w2')?.tripPage).toBeUndefined();
|
||||
});
|
||||
@@ -313,7 +437,13 @@ describe('PluginsController M2 endpoints', () => {
|
||||
});
|
||||
|
||||
it('get/update config delegate to the service (get carries the form fields, update the restart)', async () => {
|
||||
const rt = { activate: vi.fn(), deactivate: vi.fn(), isActive: vi.fn(), respawnIfActive: vi.fn(async () => false), actionsOf: vi.fn(async () => []) } as never;
|
||||
const rt = {
|
||||
activate: vi.fn(),
|
||||
deactivate: vi.fn(),
|
||||
isActive: vi.fn(),
|
||||
respawnIfActive: vi.fn(async () => false),
|
||||
actionsOf: vi.fn(async () => []),
|
||||
} as never;
|
||||
const c = new PluginsController(svc, rt, {} as never, envStub);
|
||||
expect(await c.getConfig('x')).toEqual({ fields: [{ key: 'a' }], config: { a: 1 }, actions: [] });
|
||||
expect(await c.updateConfig('x', { a: 2 })).toEqual({ config: { a: 2 }, restarted: false });
|
||||
@@ -328,12 +458,21 @@ describe('PluginsController M2 endpoints', () => {
|
||||
it('activate is 503 when the runtime is disabled', async () => {
|
||||
process.env.TREK_PLUGINS_ENABLED = 'false';
|
||||
const rt = { activate: vi.fn(), isActive: vi.fn() } as never;
|
||||
await expect(new PluginsController(svc, rt, {} as never, envStub).activate('x', {})).rejects.toMatchObject({ status: 503 });
|
||||
await expect(new PluginsController(svc, rt, {} as never, envStub).activate('x', {})).rejects.toMatchObject({
|
||||
status: 503,
|
||||
});
|
||||
});
|
||||
|
||||
it('activate surfaces an activation error as 400', async () => {
|
||||
const rt = { activate: vi.fn(async () => { throw new Error('bad code'); }), isActive: vi.fn(() => false) } as never;
|
||||
await expect(new PluginsController(svc, rt, {} as never, envStub).activate('x', {})).rejects.toMatchObject({ status: 400 });
|
||||
const rt = {
|
||||
activate: vi.fn(async () => {
|
||||
throw new Error('bad code');
|
||||
}),
|
||||
isActive: vi.fn(() => false),
|
||||
} as never;
|
||||
await expect(new PluginsController(svc, rt, {} as never, envStub).activate('x', {})).rejects.toMatchObject({
|
||||
status: 400,
|
||||
});
|
||||
});
|
||||
|
||||
it('deactivate stops the plugin (and cascades to dependents)', async () => {
|
||||
|
||||
Reference in New Issue
Block a user