mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
refactor(plugins): runtime lifecycle, config CRUD and the uninstall cascade through repositories
This commit is contained in:
@@ -31,6 +31,14 @@ import { createPluginRuntime } from '../../helpers/plugin-host';
|
||||
import { parseManifest, ManifestError } from '../../../src/nest/plugins/install/manifest';
|
||||
import { makeHostAllow } from '../../../src/nest/plugins/runtime/egress-policy';
|
||||
import { createTestAddonsService } from '../../helpers/test-addons';
|
||||
import { sharedTestOrm } from '../../helpers/test-uow';
|
||||
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';
|
||||
|
||||
function install(id: string, operatorEgress: boolean, perms: string[] = ['http:outbound:gotify.net']) {
|
||||
testDb.prepare(
|
||||
@@ -174,7 +182,19 @@ describe('the admin list surfaces operator egress (so the chip can be shown)', (
|
||||
// one over the same DB. These fixtures declare no dependencies, so it is never consulted.
|
||||
const listPlugins = async () => {
|
||||
const dbs = new DatabaseService(dbConn);
|
||||
return (await new PluginsService(dbs, await createTestAddonsService(testDb, dbs)).list()).plugins;
|
||||
const orm = await sharedTestOrm(dbConn);
|
||||
const service = new PluginsService(
|
||||
dbs,
|
||||
await createTestAddonsService(testDb, dbs),
|
||||
orm.repo(Plugins),
|
||||
orm.repo(PluginEgressHosts),
|
||||
orm.repo(PluginSettingsFields),
|
||||
orm.repo(PluginActions),
|
||||
orm.repo(PluginUserConfig),
|
||||
orm.repo(PluginErrorLog),
|
||||
orm.repo(PluginCapabilityAudit),
|
||||
);
|
||||
return (await service.list()).plugins;
|
||||
};
|
||||
|
||||
const before = await listPlugins();
|
||||
|
||||
@@ -34,6 +34,21 @@ import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
||||
import { createTestAddonsService } from '../../helpers/test-addons';
|
||||
import { createPluginRuntime } from '../../helpers/plugin-host';
|
||||
import { discoverPlugins } from '../../../src/nest/plugins/install/discovery';
|
||||
import { sharedTestOrm } from '../../helpers/test-uow';
|
||||
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 type { PluginsRepository } from '../../../src/db/repositories/Plugins.repository';
|
||||
import type { PluginEgressHostsRepository } from '../../../src/db/repositories/PluginEgressHosts.repository';
|
||||
import type { PluginSettingsFieldsRepository } from '../../../src/db/repositories/PluginSettingsFields.repository';
|
||||
import type { PluginActionsRepository } from '../../../src/db/repositories/PluginActions.repository';
|
||||
import type { PluginUserConfigRepository } from '../../../src/db/repositories/PluginUserConfig.repository';
|
||||
import type { PluginErrorLogRepository } from '../../../src/db/repositories/PluginErrorLog.repository';
|
||||
import type { PluginCapabilityAuditRepository } from '../../../src/db/repositories/PluginCapabilityAudit.repository';
|
||||
|
||||
function install(id: string) {
|
||||
testDb
|
||||
@@ -54,7 +69,28 @@ function declareField(pluginId: string, key: string, scope: 'instance' | 'user',
|
||||
}
|
||||
|
||||
let addonsService: AddonsService;
|
||||
const svc = () => new PluginsService(new DatabaseService(dbConn), addonsService);
|
||||
// Plan 3j Task 2 — PluginsService's own six repositories, resolved ONCE in `beforeAll`
|
||||
// (the file's `testDb` is fixed for the whole suite) so `svc()` below can stay a plain
|
||||
// synchronous factory — every one of its ~20 call sites below chains straight off it.
|
||||
let pluginsRepo: PluginsRepository;
|
||||
let pluginEgressHostsRepo: PluginEgressHostsRepository;
|
||||
let pluginSettingsFieldsRepo: PluginSettingsFieldsRepository;
|
||||
let pluginActionsRepo: PluginActionsRepository;
|
||||
let pluginUserConfigRepo: PluginUserConfigRepository;
|
||||
let pluginErrorLogRepo: PluginErrorLogRepository;
|
||||
let pluginCapabilityAuditRepo: PluginCapabilityAuditRepository;
|
||||
const svc = () =>
|
||||
new PluginsService(
|
||||
new DatabaseService(dbConn),
|
||||
addonsService,
|
||||
pluginsRepo,
|
||||
pluginEgressHostsRepo,
|
||||
pluginSettingsFieldsRepo,
|
||||
pluginActionsRepo,
|
||||
pluginUserConfigRepo,
|
||||
pluginErrorLogRepo,
|
||||
pluginCapabilityAuditRepo,
|
||||
);
|
||||
|
||||
/**
|
||||
* Installs a real plugin through the manifest -> discovery pipeline (not a direct
|
||||
@@ -77,6 +113,14 @@ beforeAll(async () => {
|
||||
createTables(testDb);
|
||||
runMigrations(testDb);
|
||||
addonsService = await createTestAddonsService(testDb, new DatabaseService(dbConn));
|
||||
const orm = await sharedTestOrm(dbConn);
|
||||
pluginsRepo = orm.repo(Plugins);
|
||||
pluginEgressHostsRepo = orm.repo(PluginEgressHosts);
|
||||
pluginSettingsFieldsRepo = orm.repo(PluginSettingsFields);
|
||||
pluginActionsRepo = orm.repo(PluginActions);
|
||||
pluginUserConfigRepo = orm.repo(PluginUserConfig);
|
||||
pluginErrorLogRepo = orm.repo(PluginErrorLog);
|
||||
pluginCapabilityAuditRepo = orm.repo(PluginCapabilityAudit);
|
||||
});
|
||||
beforeEach(() => {
|
||||
testDb.prepare('DELETE FROM plugins').run();
|
||||
|
||||
@@ -22,6 +22,21 @@ import { createSnapshotTestDb } from '../../helpers/db-mock';
|
||||
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
import { Users } from '../../../src/db/entities/Users.entity';
|
||||
import { Plugins } from '../../../src/db/entities/Plugins.entity';
|
||||
import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity';
|
||||
import { PluginScheduledTasks } from '../../../src/db/entities/PluginScheduledTasks.entity';
|
||||
import { PluginUserErasureQueue } from '../../../src/db/entities/PluginUserErasureQueue.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 { PluginEntityMetadata } from '../../../src/db/entities/PluginEntityMetadata.entity';
|
||||
import { PluginOauthTokens } from '../../../src/db/entities/PluginOauthTokens.entity';
|
||||
import { PluginOauthState } from '../../../src/db/entities/PluginOauthState.entity';
|
||||
import { PluginMetaMigrations } from '../../../src/db/entities/PluginMetaMigrations.entity';
|
||||
import { PluginCapabilityAudit } from '../../../src/db/entities/PluginCapabilityAudit.entity';
|
||||
import { Settings } from '../../../src/db/entities/Settings.entity';
|
||||
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestOrm;
|
||||
@@ -50,21 +65,50 @@ async function buildRuntime(withOrm: boolean): Promise<PluginRuntimeService> {
|
||||
const audit = new AuditService(t.repo(AuditLog), t.repo(Users));
|
||||
const addons = await createTestAddonsService(testDb, dbs);
|
||||
const userSettings = new PluginUserSettingsService(dbs);
|
||||
return new PluginRuntimeService(dbs, audit, addons, userSettings, undefined, undefined, undefined, withOrm ? t.orm : undefined);
|
||||
return new PluginRuntimeService(
|
||||
dbs,
|
||||
audit,
|
||||
addons,
|
||||
userSettings,
|
||||
t.repo(Plugins),
|
||||
t.repo(PluginErrorLog),
|
||||
t.repo(PluginScheduledTasks),
|
||||
t.repo(PluginUserErasureQueue),
|
||||
t.repo(PluginEgressHosts),
|
||||
t.repo(PluginSettingsFields),
|
||||
t.repo(PluginActions),
|
||||
t.repo(PluginUserConfig),
|
||||
t.repo(PluginEntityMetadata),
|
||||
t.repo(PluginOauthTokens),
|
||||
t.repo(PluginOauthState),
|
||||
t.repo(PluginMetaMigrations),
|
||||
t.repo(PluginCapabilityAudit),
|
||||
t.repo(Settings),
|
||||
t.repo(NotificationChannelPreferences),
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
withOrm ? t.orm : undefined,
|
||||
);
|
||||
}
|
||||
|
||||
describe('PluginRuntimeService.onApplicationBootstrap — absent-ORM boot branch (task-6-rereview2.md I-A)', () => {
|
||||
it('RT-BOOT-NOORM-001: without an ORM, boot logs the skip line naming the plugin id and never calls activate', async () => {
|
||||
it('RT-BOOT-NOORM-001: without an ORM, boot logs the skip line and never calls activate', async () => {
|
||||
installEnabledPlugin('rtboot-noorm');
|
||||
const rt = await buildRuntime(false);
|
||||
const activateSpy = vi.spyOn(rt, 'activate');
|
||||
|
||||
await rt.onApplicationBootstrap();
|
||||
|
||||
// Plan 3j Task 2 (deviation, task-2-report.md): `installedDepRows` is now a
|
||||
// repository read that needs a request context this branch cannot provide, so
|
||||
// it is skipped rather than attempted unwrapped — the log line can no longer
|
||||
// name which plugin(s) it skipped, only that it skipped activation entirely.
|
||||
// Still fail-closed (never throws out of onApplicationBootstrap, never abandons
|
||||
// app.init()), still logged exactly once, still never calls activate.
|
||||
expect(logMock.logError).toHaveBeenCalledTimes(1);
|
||||
const [message] = logMock.logError.mock.calls[0] as [string];
|
||||
expect(message).toMatch(/PluginRuntimeService\.onApplicationBootstrap: no MikroORM available/);
|
||||
expect(message).toContain('rtboot-noorm');
|
||||
expect(activateSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
@@ -22,6 +22,30 @@ import { createTestAddonsService } from '../../helpers/test-addons';
|
||||
import Database from 'better-sqlite3';
|
||||
import { PluginsService } from '../../../src/nest/plugins/plugins.service';
|
||||
import { PluginUserSettingsService } from '../../../src/nest/plugins/plugin-user-settings.service';
|
||||
import { sharedTestOrm } from '../../helpers/test-uow';
|
||||
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';
|
||||
|
||||
/** Plan 3j Task 2 — PluginsService's own six repositories, over whichever fresh `getDb.current` the caller just set. */
|
||||
async function makePluginsService(dbs: DatabaseService): Promise<PluginsService> {
|
||||
const orm = await sharedTestOrm(getDb.current as Database.Database);
|
||||
return new PluginsService(
|
||||
dbs,
|
||||
await createTestAddonsService(getDb.current as Database.Database, dbs),
|
||||
orm.repo(Plugins),
|
||||
orm.repo(PluginEgressHosts),
|
||||
orm.repo(PluginSettingsFields),
|
||||
orm.repo(PluginActions),
|
||||
orm.repo(PluginUserConfig),
|
||||
orm.repo(PluginErrorLog),
|
||||
orm.repo(PluginCapabilityAudit),
|
||||
);
|
||||
}
|
||||
/** The host-side settings reads, over the same connection the test seeded. */
|
||||
const userSettings = () => new PluginUserSettingsService(new DatabaseService(dbConn));
|
||||
|
||||
@@ -46,7 +70,7 @@ describe('per-user plugin settings', () => {
|
||||
beforeEach(async () => {
|
||||
getDb.current = freshDb();
|
||||
const dbs = new DatabaseService(dbConn);
|
||||
svc = new PluginsService(dbs, await createTestAddonsService(getDb.current as Database.Database, dbs));
|
||||
svc = await makePluginsService(dbs);
|
||||
});
|
||||
|
||||
it('lists only the user-scope fields, in order', async () => {
|
||||
@@ -98,7 +122,7 @@ describe('manifest defaults reach the runtime reads', () => {
|
||||
beforeEach(async () => {
|
||||
getDb.current = freshDb();
|
||||
const dbs = new DatabaseService(dbConn);
|
||||
svc = new PluginsService(dbs, await createTestAddonsService(getDb.current as Database.Database, dbs));
|
||||
svc = await makePluginsService(dbs);
|
||||
const ins = (getDb.current as import('better-sqlite3').Database).prepare(
|
||||
'INSERT INTO plugin_settings_fields (plugin_id, field_key, input_type, required, secret, scope, sort_order, default_value) VALUES (?,?,?,?,?,?,?,?)',
|
||||
);
|
||||
@@ -141,7 +165,7 @@ describe('hasRequired applies the same "filled" rule as the save gate', () => {
|
||||
beforeEach(async () => {
|
||||
getDb.current = freshDb();
|
||||
const dbs = new DatabaseService(dbConn);
|
||||
svc = new PluginsService(dbs, await createTestAddonsService(getDb.current as Database.Database, dbs));
|
||||
svc = await makePluginsService(dbs);
|
||||
(getDb.current as import('better-sqlite3').Database)
|
||||
.prepare('INSERT INTO plugin_settings_fields (plugin_id, field_key, input_type, required, secret, scope, sort_order) VALUES (?,?,?,?,?,?,?)')
|
||||
.run('p', 'consent', 'checkbox', 1, 0, 'user', 9);
|
||||
|
||||
@@ -12,8 +12,11 @@ const { testDb } = vi.hoisted(() => {
|
||||
status TEXT, enabled INTEGER DEFAULT 0, last_error TEXT, reviewed_at TEXT, source_repo TEXT, config TEXT DEFAULT '{}', permissions TEXT DEFAULT '[]', granted_permissions TEXT DEFAULT '[]', capabilities TEXT DEFAULT '{}', dependencies TEXT DEFAULT '{}', operator_egress INTEGER DEFAULT 0, updated_at TEXT,
|
||||
author_pubkey TEXT, update_block_code TEXT, update_block_detail TEXT, update_block_version TEXT,
|
||||
trek_range TEXT, sort_order INTEGER DEFAULT 0, update_hold INTEGER NOT NULL DEFAULT 0);
|
||||
CREATE TABLE plugin_settings_fields (plugin_id TEXT, field_key TEXT, scope TEXT, secret INTEGER, required INTEGER DEFAULT 0, input_type TEXT DEFAULT 'text', default_value TEXT);
|
||||
CREATE TABLE plugin_error_log (id INTEGER PRIMARY KEY AUTOINCREMENT, plugin_id TEXT, level TEXT, message TEXT, ts TEXT DEFAULT '2026-01-01');`);
|
||||
CREATE TABLE plugin_settings_fields (id INTEGER PRIMARY KEY AUTOINCREMENT, plugin_id TEXT, field_key TEXT, scope TEXT, secret INTEGER, required INTEGER DEFAULT 0, input_type TEXT DEFAULT 'text', default_value TEXT, sort_order INTEGER DEFAULT 0);
|
||||
CREATE TABLE plugin_error_log (id INTEGER PRIMARY KEY AUTOINCREMENT, plugin_id TEXT, level TEXT, message TEXT, ts TEXT DEFAULT '2026-01-01');
|
||||
CREATE TABLE plugin_actions (plugin_id TEXT, action_key TEXT, label TEXT, hint TEXT, danger INTEGER DEFAULT 0, sort_order INTEGER DEFAULT 0, scope TEXT DEFAULT 'user', PRIMARY KEY (plugin_id, action_key));
|
||||
CREATE TABLE plugin_egress_hosts (id INTEGER PRIMARY KEY AUTOINCREMENT, plugin_id TEXT, host TEXT, created_at TEXT DEFAULT (datetime('now')));
|
||||
CREATE TABLE plugin_user_config (plugin_id TEXT, user_id INTEGER, config TEXT DEFAULT '{}', updated_at TEXT DEFAULT (datetime('now')), PRIMARY KEY (plugin_id, user_id));`);
|
||||
return { testDb: db };
|
||||
});
|
||||
vi.mock('../../../src/db/database', () => ({ db: testDb }));
|
||||
@@ -22,6 +25,14 @@ import { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
|
||||
import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
||||
import { createTestAddonsService } from '../../helpers/test-addons';
|
||||
import { sharedTestOrm } from '../../helpers/test-uow';
|
||||
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';
|
||||
@@ -35,6 +46,27 @@ beforeAll(async () => {
|
||||
addonsService = await createTestAddonsService(testDb, new DatabaseService(dbConn));
|
||||
});
|
||||
|
||||
/**
|
||||
* Plan 3j Task 2 — PluginsService's own repository-backed constructor. One
|
||||
* factory, reused by every call site below (`sharedTestOrm` memoises the ORM
|
||||
* per connection, so this is cheap): the six PS1-PS13 repositories, resolved
|
||||
* over the SAME `testDb` handle every raw-SQL fixture line above writes to.
|
||||
*/
|
||||
async function makeService(): Promise<PluginsService> {
|
||||
const orm = await sharedTestOrm(testDb);
|
||||
return new PluginsService(
|
||||
new DatabaseService(dbConn),
|
||||
addonsService,
|
||||
orm.repo(Plugins),
|
||||
orm.repo(PluginEgressHosts),
|
||||
orm.repo(PluginSettingsFields),
|
||||
orm.repo(PluginActions),
|
||||
orm.repo(PluginUserConfig),
|
||||
orm.repo(PluginErrorLog),
|
||||
orm.repo(PluginCapabilityAudit),
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
testDb.exec('DELETE FROM plugins');
|
||||
testDb.exec('DELETE FROM plugin_settings_fields');
|
||||
@@ -51,7 +83,7 @@ describe('PluginsService.list', () => {
|
||||
.run('flight', 'Flight', 'desc', 'widget', 'inactive', '1.0.0');
|
||||
process.env.TREK_PLUGINS_ENABLED = 'true';
|
||||
|
||||
const out = await new PluginsService(new DatabaseService(dbConn), addonsService).list();
|
||||
const out = await (await makeService()).list();
|
||||
expect(out.enabled).toBe(true);
|
||||
expect(out.plugins).toHaveLength(1);
|
||||
expect(out.plugins[0]).toMatchObject({ id: 'flight', name: 'Flight', status: 'inactive' });
|
||||
@@ -72,7 +104,7 @@ describe('PluginsService.list', () => {
|
||||
it('reports the switch off and an outgrown plugin as hostIncompatible by default', async () => {
|
||||
process.env.APP_VERSION = '4.1.0';
|
||||
seed();
|
||||
const out = await new PluginsService(new DatabaseService(dbConn), addonsService).list();
|
||||
const out = await (await makeService()).list();
|
||||
expect(out.ignoreTrekRange).toBe(false);
|
||||
expect(out.plugins[0]).toMatchObject({ dependencyStatus: 'hostIncompatible', trekRangeBypassed: null });
|
||||
});
|
||||
@@ -81,7 +113,7 @@ describe('PluginsService.list', () => {
|
||||
process.env.APP_VERSION = '4.1.0';
|
||||
process.env.TREK_PLUGINS_IGNORE_TREK_RANGE = '1';
|
||||
seed();
|
||||
const out = await new PluginsService(new DatabaseService(dbConn), addonsService).list();
|
||||
const out = await (await makeService()).list();
|
||||
expect(out.ignoreTrekRange).toBe(true);
|
||||
expect(out.plugins[0]).toMatchObject({
|
||||
dependencyStatus: 'ok',
|
||||
@@ -93,7 +125,7 @@ describe('PluginsService.list', () => {
|
||||
process.env.APP_VERSION = '3.5.0';
|
||||
process.env.TREK_PLUGINS_IGNORE_TREK_RANGE = '1';
|
||||
seed();
|
||||
const out = await new PluginsService(new DatabaseService(dbConn), addonsService).list();
|
||||
const out = await (await makeService()).list();
|
||||
expect(out.plugins[0]).toMatchObject({ dependencyStatus: 'ok', trekRangeBypassed: null });
|
||||
});
|
||||
});
|
||||
@@ -106,7 +138,7 @@ describe('PluginsService.list', () => {
|
||||
.prepare("INSERT INTO plugins (id, name, type, status, version) VALUES ('free','Free','widget','inactive','1.0.0')")
|
||||
.run();
|
||||
|
||||
const out = await new PluginsService(new DatabaseService(dbConn), addonsService).list();
|
||||
const out = await (await makeService()).list();
|
||||
expect(out.plugins.find((p) => p.id === 'held')).toMatchObject({ updateHold: true });
|
||||
expect(out.plugins.find((p) => p.id === 'free')).toMatchObject({ updateHold: false });
|
||||
});
|
||||
@@ -115,7 +147,7 @@ describe('PluginsService.list', () => {
|
||||
testDb
|
||||
.prepare("INSERT INTO plugins (id, name, type, status, version, update_hold) VALUES ('held','Held','widget','inactive','1.0.0',1)")
|
||||
.run();
|
||||
const svc = new PluginsService(new DatabaseService(dbConn), addonsService);
|
||||
const svc = await makeService();
|
||||
|
||||
expect(await svc.resumeUpdates('held')).toBe(true);
|
||||
expect(testDb.prepare("SELECT update_hold FROM plugins WHERE id='held'").get()).toMatchObject({ update_hold: 0 });
|
||||
@@ -127,14 +159,14 @@ describe('PluginsService.list', () => {
|
||||
.prepare('INSERT INTO plugins (id, name, description, type, status, version) VALUES (?,?,?,?,?,?)')
|
||||
.run('flight', 'Flight', 'desc', 'widget', 'inactive', '1.0.0');
|
||||
|
||||
const out = await new PluginsService(new DatabaseService(dbConn), addonsService).list();
|
||||
const out = await (await makeService()).list();
|
||||
expect(out.enabled).toBe(true);
|
||||
expect(out.plugins).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('reports disabled when the kill switch is off (TREK_PLUGINS_ENABLED=false)', async () => {
|
||||
process.env.TREK_PLUGINS_ENABLED = 'false';
|
||||
const out = await new PluginsService(new DatabaseService(dbConn), addonsService).list();
|
||||
const out = await (await makeService()).list();
|
||||
expect(out.enabled).toBe(false);
|
||||
expect(out.plugins).toEqual([]);
|
||||
});
|
||||
@@ -154,7 +186,7 @@ describe('PluginsService.list', () => {
|
||||
const key = 'RWTvBn0aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789abcd';
|
||||
insert('signed-one', 'acme/signed-one', key);
|
||||
|
||||
const p = (await new PluginsService(new DatabaseService(dbConn), addonsService).list()).plugins[0];
|
||||
const p = (await (await makeService()).list()).plugins[0];
|
||||
expect(p.signed).toBe(true);
|
||||
// Short head…tail, for eyeballing against what the author reads out over the
|
||||
// phone. NOT a confidentiality measure — the key is public, and the re-trust
|
||||
@@ -164,7 +196,7 @@ describe('PluginsService.list', () => {
|
||||
|
||||
it('reports unsigned for a registry plugin with no pinned key', async () => {
|
||||
insert('plain', 'acme/plain', null);
|
||||
const p = (await new PluginsService(new DatabaseService(dbConn), addonsService).list()).plugins[0];
|
||||
const p = (await (await makeService()).list()).plugins[0];
|
||||
expect(p.signed).toBe(false);
|
||||
expect(p.keyFingerprint).toBeNull();
|
||||
});
|
||||
@@ -172,7 +204,7 @@ describe('PluginsService.list', () => {
|
||||
it('reports unsigned for a sideloaded and a dev-linked plugin (they carry no key)', async () => {
|
||||
insert('uploaded', 'local:upload', null);
|
||||
insert('linked', 'local:link', null);
|
||||
const plugins = (await new PluginsService(new DatabaseService(dbConn), addonsService).list()).plugins;
|
||||
const plugins = (await (await makeService()).list()).plugins;
|
||||
expect(plugins.map((p) => [p.id, p.signed, p.source_repo])).toEqual([
|
||||
['linked', false, 'local:link'],
|
||||
['uploaded', false, 'local:upload'],
|
||||
@@ -186,7 +218,7 @@ describe('PluginsService.list', () => {
|
||||
.prepare('UPDATE plugins SET update_block_code = ?, update_block_detail = ?, update_block_version = ? WHERE id = ?')
|
||||
.run('SIGNATURE_KEY_CHANGED', 'the key changed', '2.0.0', 'blocked');
|
||||
|
||||
const byId = Object.fromEntries((await new PluginsService(new DatabaseService(dbConn), addonsService).list()).plugins.map((p) => [p.id, p]));
|
||||
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.fine.updateBlock).toBeNull();
|
||||
});
|
||||
@@ -196,7 +228,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 new PluginsService(new DatabaseService(dbConn), addonsService).list())
|
||||
const p = (await (await makeService()).list())
|
||||
.plugins[0] as unknown as Record<string, unknown>;
|
||||
expect(p.author_pubkey).toBeUndefined();
|
||||
});
|
||||
@@ -323,7 +355,7 @@ describe('PluginsService instance config', () => {
|
||||
testDb.prepare("INSERT INTO plugin_settings_fields (plugin_id, field_key, scope, secret) VALUES ('x','api_key','instance',1)").run();
|
||||
testDb.prepare("INSERT INTO plugin_settings_fields (plugin_id, field_key, scope, secret) VALUES ('x','server','instance',0)").run();
|
||||
|
||||
const svc = new PluginsService(new DatabaseService(dbConn), addonsService);
|
||||
const svc = await makeService();
|
||||
const masked = await svc.updateInstanceConfig('x', { api_key: 'super-secret', server: 'https://h' });
|
||||
// client gets the masked view
|
||||
expect(masked.api_key).toBe('••••••••');
|
||||
@@ -347,7 +379,7 @@ describe('PluginsService instance config', () => {
|
||||
testDb.prepare("INSERT INTO plugins (id, name, status, config) VALUES ('y','Y','inactive','{}')").run();
|
||||
testDb.prepare("INSERT INTO plugin_settings_fields (plugin_id, field_key, scope, secret) VALUES ('y','server','instance',0)").run();
|
||||
|
||||
const svc = new PluginsService(new DatabaseService(dbConn), addonsService);
|
||||
const svc = await makeService();
|
||||
const masked = await svc.updateInstanceConfig('y', { server: 'https://h', smuggled: 'nope' });
|
||||
|
||||
expect(masked.server).toBe('https://h');
|
||||
@@ -357,8 +389,8 @@ describe('PluginsService instance config', () => {
|
||||
});
|
||||
|
||||
it('throws for an unknown plugin', async () => {
|
||||
await expect(new PluginsService(new DatabaseService(dbConn), addonsService).updateInstanceConfig('nope', {})).rejects.toThrow(/not found/);
|
||||
await expect(new PluginsService(new DatabaseService(dbConn), addonsService).getInstanceConfig('nope')).rejects.toThrow(/not found/);
|
||||
await expect((await makeService()).updateInstanceConfig('nope', {})).rejects.toThrow(/not found/);
|
||||
await expect((await makeService()).getInstanceConfig('nope')).rejects.toThrow(/not found/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -366,7 +398,7 @@ describe('PluginsService error log', () => {
|
||||
beforeEach(() => testDb.exec('DELETE FROM plugin_error_log'));
|
||||
it('lists and clears a plugin error log', async () => {
|
||||
testDb.prepare("INSERT INTO plugin_error_log (plugin_id, level, message) VALUES ('p','error','boom')").run();
|
||||
const svc = new PluginsService(new DatabaseService(dbConn), addonsService);
|
||||
const svc = await makeService();
|
||||
expect(await svc.errors('p')).toEqual([{ ts: '2026-01-01', level: 'error', message: 'boom' }]);
|
||||
await svc.clearErrors('p');
|
||||
expect(await svc.errors('p')).toEqual([]);
|
||||
|
||||
@@ -31,6 +31,21 @@ import { parseManifest, ManifestError } from '../../../src/nest/plugins/install/
|
||||
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
import { Users } from '../../../src/db/entities/Users.entity';
|
||||
import { Plugins } from '../../../src/db/entities/Plugins.entity';
|
||||
import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity';
|
||||
import { PluginScheduledTasks } from '../../../src/db/entities/PluginScheduledTasks.entity';
|
||||
import { PluginUserErasureQueue } from '../../../src/db/entities/PluginUserErasureQueue.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 { PluginEntityMetadata } from '../../../src/db/entities/PluginEntityMetadata.entity';
|
||||
import { PluginOauthTokens } from '../../../src/db/entities/PluginOauthTokens.entity';
|
||||
import { PluginOauthState } from '../../../src/db/entities/PluginOauthState.entity';
|
||||
import { PluginMetaMigrations } from '../../../src/db/entities/PluginMetaMigrations.entity';
|
||||
import { PluginCapabilityAudit } from '../../../src/db/entities/PluginCapabilityAudit.entity';
|
||||
import { Settings } from '../../../src/db/entities/Settings.entity';
|
||||
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
|
||||
/** The host-side settings reads, over the same connection the test seeded. */
|
||||
const userSettings = () => new PluginUserSettingsService(new DatabaseService(dbConn));
|
||||
|
||||
@@ -140,6 +155,21 @@ describe('a plugin channel label is bounded by the host', () => {
|
||||
new AuditService(t.repo(AuditLog), t.repo(Users)),
|
||||
await createTestAddonsService(dbConn),
|
||||
userSettings(),
|
||||
t.repo(Plugins),
|
||||
t.repo(PluginErrorLog),
|
||||
t.repo(PluginScheduledTasks),
|
||||
t.repo(PluginUserErasureQueue),
|
||||
t.repo(PluginEgressHosts),
|
||||
t.repo(PluginSettingsFields),
|
||||
t.repo(PluginActions),
|
||||
t.repo(PluginUserConfig),
|
||||
t.repo(PluginEntityMetadata),
|
||||
t.repo(PluginOauthTokens),
|
||||
t.repo(PluginOauthState),
|
||||
t.repo(PluginMetaMigrations),
|
||||
t.repo(PluginCapabilityAudit),
|
||||
t.repo(Settings),
|
||||
t.repo(NotificationChannelPreferences),
|
||||
);
|
||||
// Stand the plugin up as a granted, active notificationChannel provider.
|
||||
(rt as unknown as { supervisor: { running: Map<string, unknown> } }).supervisor.running.set('loud', {
|
||||
|
||||
Reference in New Issue
Block a user