mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
feat(plugins): store plugin data on the engine TREK runs on
PLUGIN_DATA binds the plugin data backend to the engine (the same
{ sqlite, postgres }[engine.id] binding the backup port uses): the SQLite
file per plugin, unchanged, or the Postgres schema per plugin. The host
opens a plugin's store through it and an uninstall that deletes data erases
through it, which drops the schema on Postgres. Nothing outside db/engine
asks which engine runs.
A plugin's SQL is passed through as written, so a plugin needs to know the
dialect: db.engine (db:own) answers 'sqlite' or 'postgres', and the SDK
exposes it as ctx.db.engine(), in the mock host (dbEngine option) and in
dev. The plugin facts are regenerated and the SDK goes to 1.10.0.
The Postgres lane's PGPLUG suite installs, uses and erases plugin data on
Postgres and shows that a plugin reaches neither TREK's users table nor
another plugin's schema.
This commit is contained in:
@@ -63,6 +63,7 @@ import { PlacePhotoCacheService } from '../../src/nest/place-photos/place-photo-
|
||||
import type { PlaceOriginsService } from '../../src/nest/places/place-origins.service';
|
||||
import { PlacesRpc } from '../../src/nest/places/places.rpc';
|
||||
import { PlacesService } from '../../src/nest/places/places.service';
|
||||
import { SqlitePluginDataBackend } from '../../src/nest/plugins/host/plugin-data.backend';
|
||||
import { PluginRpcHostFactory } from '../../src/nest/plugins/host/plugin-rpc-host.factory';
|
||||
import { DbRpc } from '../../src/nest/plugins/host/rpc/db.rpc';
|
||||
import { HostSurfaceRpc } from '../../src/nest/plugins/host/rpc/host-surface.rpc';
|
||||
@@ -662,7 +663,11 @@ export async function createPluginRpcHostParts(
|
||||
];
|
||||
const registry = createTestPluginRegistry(controllers);
|
||||
return {
|
||||
factory: new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService),
|
||||
factory: new PluginRpcHostFactory(
|
||||
pluginAuditRepo,
|
||||
registry as unknown as PluginRpcRegistryService,
|
||||
new SqlitePluginDataBackend(),
|
||||
),
|
||||
controllers,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ import type { PluginRpcRegistryService } from '../../../src/nest-rpc/rpc-kit/reg
|
||||
import { AddonsService } from '../../../src/nest/addons/addons.service';
|
||||
import { AuditService } from '../../../src/nest/audit/audit.service';
|
||||
import { UnitOfWork } from '../../../src/nest/database/unit-of-work';
|
||||
import { SqlitePluginDataBackend } from '../../../src/nest/plugins/host/plugin-data.backend';
|
||||
import { PluginRpcHostFactory } from '../../../src/nest/plugins/host/plugin-rpc-host.factory';
|
||||
import { DbRpc } from '../../../src/nest/plugins/host/rpc/db.rpc';
|
||||
import { PluginRuntimeService } from '../../../src/nest/plugins/plugin-runtime.service';
|
||||
@@ -131,6 +132,7 @@ describe('plugin boot vs registry scan ordering', () => {
|
||||
const hostFactory = new PluginRpcHostFactory(
|
||||
t.repo(PluginCapabilityAudit),
|
||||
registry as unknown as PluginRpcRegistryService,
|
||||
new SqlitePluginDataBackend(),
|
||||
);
|
||||
const userSettings = new PluginUserSettingsService(
|
||||
(t as TestOrm).repo(PluginSettingsFields),
|
||||
@@ -256,6 +258,7 @@ describe('plugin boot vs registry scan ordering', () => {
|
||||
const hostFactory2 = new PluginRpcHostFactory(
|
||||
t2.repo(PluginCapabilityAudit),
|
||||
registry2 as unknown as PluginRpcRegistryService,
|
||||
new SqlitePluginDataBackend(),
|
||||
);
|
||||
// Built directly on t2 (not createTestAddonsService's sharedTestOrm) so
|
||||
// its repositories share t2's allowGlobalContext: false ORM.
|
||||
|
||||
@@ -0,0 +1,234 @@
|
||||
/**
|
||||
* PGPLUG-001..008 — a plugin's own data (`db:own`) on Postgres, against the
|
||||
* real app on the lane's database: each plugin gets a schema of its own and no
|
||||
* SQLite file, its SQL keeps the SQLite store's semantics (migrate once per
|
||||
* id, a script's statements committed one by one, an atomic tx, the row cap,
|
||||
* the time budget, the quota), it cannot reach TREK's tables or another
|
||||
* plugin's schema however it spells the name, and erasing a plugin drops its
|
||||
* schema and nothing else.
|
||||
*/
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { PluginDataSlots, PostgresPluginDataStore } from '../../src/db/engine/postgres/plugin-data-store';
|
||||
import { withRequestContext } from '../../src/nest/database/request-context';
|
||||
import { PLUGIN_DATA, type PluginDataBackend } from '../../src/nest/plugins/host/plugin-data.backend';
|
||||
import { closePluginDataDb } from '../../src/nest/plugins/host/plugin-host-state';
|
||||
import { PluginRpcHostFactory } from '../../src/nest/plugins/host/plugin-rpc-host.factory';
|
||||
import { MikroORM } from '@mikro-orm/core';
|
||||
import type { AbstractSqlConnection } from '@mikro-orm/sql';
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
|
||||
let app: INestApplication;
|
||||
let orm: MikroORM;
|
||||
let data: PluginDataBackend;
|
||||
let dataDir: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-pg-plugin-data-'));
|
||||
process.env.TREK_PLUGINS_DATA_DIR = dataDir;
|
||||
app = await buildApp();
|
||||
orm = app.get(MikroORM);
|
||||
data = app.get<PluginDataBackend>(PLUGIN_DATA, { strict: false });
|
||||
}, 60_000);
|
||||
|
||||
afterAll(async () => {
|
||||
await app?.close();
|
||||
delete process.env.TREK_PLUGINS_DATA_DIR;
|
||||
fs.rmSync(dataDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
/** The plugin schemas the database holds now. */
|
||||
async function pluginSchemas(): Promise<string[]> {
|
||||
const rows = await withRequestContext(orm, () =>
|
||||
orm.em
|
||||
.getConnection()
|
||||
.execute<Array<{ nspname: string }>>(
|
||||
"select nspname from pg_namespace where starts_with(nspname, 'trek_plugin_') order by nspname",
|
||||
[],
|
||||
'all',
|
||||
),
|
||||
);
|
||||
return rows.map((row) => row.nspname);
|
||||
}
|
||||
|
||||
/** A store on the app's pool with limits small enough to reach in a test. */
|
||||
function cappedStore(id: string, limits: { maxRows?: number; timeBudgetMs?: number; quotaBytes?: number }) {
|
||||
const client = (orm.em.getConnection() as AbstractSqlConnection).getClient<object>();
|
||||
return new PostgresPluginDataStore(id, client, new PluginDataSlots(1), limits);
|
||||
}
|
||||
|
||||
describe('plugin data on Postgres', () => {
|
||||
it('PGPLUG-001: a plugin gets a schema of its own, migrates once per id, reads and writes there, and no SQLite file', async () => {
|
||||
const store = data.open('pg-alpha');
|
||||
expect(store.engine).toBe('postgres');
|
||||
expect(await store.migrate('001', 'CREATE TABLE notes (id bigserial PRIMARY KEY, body text NOT NULL)')).toEqual({
|
||||
applied: true,
|
||||
});
|
||||
expect(await store.migrate('001', 'CREATE TABLE never (x int)')).toEqual({ applied: false });
|
||||
expect(await store.exec('INSERT INTO notes (body) VALUES ($1), ($2)', ['a', 'b'])).toEqual({ changes: 2 });
|
||||
expect(await store.query('SELECT body FROM notes WHERE body <> $1 ORDER BY id', ['x'])).toEqual([
|
||||
{ body: 'a' },
|
||||
{ body: 'b' },
|
||||
]);
|
||||
|
||||
expect(await pluginSchemas()).toContain('trek_plugin_pg_alpha');
|
||||
expect(fs.readdirSync(path.join(dataDir, 'pg-alpha'))).toEqual([]);
|
||||
});
|
||||
|
||||
it('PGPLUG-002: a script commits statement by statement, a tx all or nothing', async () => {
|
||||
const store = data.open('pg-script');
|
||||
await store.migrate('001', 'CREATE TABLE kv (k text PRIMARY KEY, n int NOT NULL)');
|
||||
|
||||
await expect(store.exec("INSERT INTO kv VALUES ('one', 1); INSERT INTO kv VALUES ('one', 2)")).rejects.toThrow(
|
||||
/duplicate key/,
|
||||
);
|
||||
expect(await store.query('SELECT k, n FROM kv')).toEqual([{ k: 'one', n: 1 }]);
|
||||
|
||||
await expect(
|
||||
store.tx([
|
||||
{ sql: 'UPDATE kv SET n = n + 1 WHERE k = $1', args: ['one'] },
|
||||
{ sql: "INSERT INTO kv VALUES ('one', 9)" },
|
||||
]),
|
||||
).rejects.toThrow(/duplicate key/);
|
||||
expect(await store.query('SELECT n FROM kv')).toEqual([{ n: 1 }]);
|
||||
|
||||
const batch = await store.tx([
|
||||
{ sql: "INSERT INTO kv VALUES ('two', 2)" },
|
||||
{ sql: 'SELECT k FROM kv ORDER BY k' },
|
||||
{ sql: "UPDATE kv SET n = 3 WHERE k = 'two' RETURNING n" },
|
||||
]);
|
||||
expect(batch.results).toEqual([{ changes: 1 }, { rows: [{ k: 'one' }, { k: 'two' }] }, { rows: [{ n: 3 }] }]);
|
||||
await expect(store.tx([{ sql: 'COMMIT' }])).rejects.toThrow(/transaction-control/);
|
||||
await expect(store.query('SELECT n FROM kv WHERE k = ?', ['one'])).rejects.toThrow(/\$1, \$2/);
|
||||
});
|
||||
|
||||
it("PGPLUG-003: a plugin cannot read TREK's tables, however it spells their name", async () => {
|
||||
const store = data.open('pg-alpha');
|
||||
// Unqualified, the name finds nothing: only the plugin's schema is on the search_path.
|
||||
await expect(store.query('SELECT email FROM users')).rejects.toThrow(/does not exist/);
|
||||
for (const sql of [
|
||||
'SELECT email FROM public.users',
|
||||
'SELECT email FROM "public"."users"',
|
||||
'SELECT email FROM PUBLIC . users',
|
||||
'SELECT email FROM public/* x */.users',
|
||||
"SELECT query_to_xml('select email from public.users', true, true, '')",
|
||||
"SELECT ts_stat('select to_tsvector(email) from public.users')",
|
||||
'SELECT * FROM pg_catalog.pg_tables',
|
||||
'SELECT * FROM pg_stats',
|
||||
'SELECT * FROM information_schema.tables',
|
||||
"SELECT set_config('search_path', 'public', true)",
|
||||
"SELECT setval('public.users_id_seq', 1)",
|
||||
"SELECT 'public.users'::regclass",
|
||||
'SELECT database_to_xml(true, true, $$x$$)',
|
||||
"SELECT E'\\'' || 'x'",
|
||||
]) {
|
||||
await expect(store.query(sql), sql).rejects.toThrow(/not allowed for plugin databases/);
|
||||
}
|
||||
for (const sql of [
|
||||
'CREATE TEMP TABLE t (x int)',
|
||||
'SELECT 1 AS x INTO t2',
|
||||
'CREATE FUNCTION f() RETURNS int LANGUAGE sql AS $$ SELECT 1 $$',
|
||||
'ALTER TABLE notes SET SCHEMA public',
|
||||
'ALTER TABLE notes OWNER TO postgres',
|
||||
'CREATE SCHEMA mine',
|
||||
'DROP SCHEMA trek_plugin_pg_alpha CASCADE',
|
||||
'SET search_path TO public',
|
||||
"COPY notes TO '/tmp/x'",
|
||||
'CREATE TABLE copy AS SELECT * FROM public.users',
|
||||
'CREATE VIEW v AS SELECT email FROM public.users',
|
||||
'GRANT SELECT ON notes TO PUBLIC',
|
||||
'DROP OWNED BY CURRENT_USER',
|
||||
]) {
|
||||
await expect(store.exec(sql), sql).rejects.toThrow(/not allowed/);
|
||||
}
|
||||
// Nothing of TREK's was touched along the way.
|
||||
const [{ n }] = await withRequestContext(orm, () =>
|
||||
orm.em.getConnection().execute<Array<{ n: number }>>('select count(*) as n from users', [], 'all'),
|
||||
);
|
||||
expect(n).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
it("PGPLUG-004: two plugins never see each other's schema, even with tables of one name", async () => {
|
||||
const alpha = data.open('pg-alpha');
|
||||
const beta = data.open('pg-beta');
|
||||
await beta.migrate('001', 'CREATE TABLE notes (id bigserial PRIMARY KEY, body text NOT NULL)');
|
||||
await beta.exec('INSERT INTO notes (body) VALUES ($1)', ['beta secret']);
|
||||
|
||||
expect(await alpha.query('SELECT body FROM notes ORDER BY id')).toEqual([{ body: 'a' }, { body: 'b' }]);
|
||||
expect(await beta.query('SELECT body FROM notes')).toEqual([{ body: 'beta secret' }]);
|
||||
for (const sql of [
|
||||
'SELECT body FROM trek_plugin_pg_beta.notes',
|
||||
'SELECT body FROM "trek_plugin_pg_beta".notes',
|
||||
'SELECT body FROM trek_plugin_not_yet_installed.notes',
|
||||
]) {
|
||||
await expect(alpha.query(sql), sql).rejects.toThrow(/another schema/);
|
||||
}
|
||||
// Its own schema, qualified, is fine.
|
||||
expect(await alpha.query('SELECT count(*) AS n FROM trek_plugin_pg_alpha.notes')).toEqual([{ n: 2 }]);
|
||||
});
|
||||
|
||||
it('PGPLUG-005: the row cap and the time budget hold, the budget even inside one statement', async () => {
|
||||
const store = cappedStore('pg-capped', { maxRows: 5, timeBudgetMs: 300 });
|
||||
await expect(store.query('SELECT x FROM generate_series(1, 10) AS g(x)')).rejects.toThrow(
|
||||
'query returned more than 5 rows',
|
||||
);
|
||||
expect(await store.query('SELECT x FROM generate_series(1, 5) AS g(x)')).toHaveLength(5);
|
||||
await expect(
|
||||
store.tx([{ sql: 'SELECT 1 FROM generate_series(1, 3)' }, { sql: 'SELECT 1 FROM generate_series(1, 3)' }]),
|
||||
).rejects.toThrow('tx returned more than 5 rows in total');
|
||||
await expect(store.query('SELECT count(*) FROM generate_series(1, 2000000000)')).rejects.toThrow(
|
||||
'query exceeded its 300 ms time budget',
|
||||
);
|
||||
});
|
||||
|
||||
it('PGPLUG-006: a write that takes the schema past its quota is rolled back, a delete still runs', async () => {
|
||||
const store = cappedStore('pg-quota', { quotaBytes: 64 * 1024 });
|
||||
await store.migrate('001', 'CREATE TABLE blobs (body text)');
|
||||
await store.exec("INSERT INTO blobs SELECT repeat('x', 100) FROM generate_series(1, 10)");
|
||||
await expect(
|
||||
store.exec('INSERT INTO blobs SELECT md5(x::text) FROM generate_series(1, 20000) AS g(x)'),
|
||||
).rejects.toThrow(/past its 0\.0625 MB quota/);
|
||||
expect(await store.query('SELECT count(*) AS n FROM blobs')).toEqual([{ n: 10 }]);
|
||||
expect(await store.exec('DELETE FROM blobs WHERE length(body) > $1', [0])).toEqual({ changes: 10 });
|
||||
});
|
||||
|
||||
it('PGPLUG-007: erasing a plugin drops its schema and its data directory, and only its own', async () => {
|
||||
data.open('pg-gone');
|
||||
await data.open('pg-gone').migrate('001', 'CREATE TABLE t (x int)');
|
||||
expect(fs.existsSync(path.join(dataDir, 'pg-gone'))).toBe(true);
|
||||
|
||||
await data.erase('pg-gone');
|
||||
|
||||
const schemas = await pluginSchemas();
|
||||
expect(schemas).not.toContain('trek_plugin_pg_gone');
|
||||
expect(schemas).toEqual(expect.arrayContaining(['trek_plugin_pg_alpha', 'trek_plugin_pg_beta']));
|
||||
expect(fs.existsSync(path.join(dataDir, 'pg-gone'))).toBe(false);
|
||||
// An id no schema can be made from has nothing to drop.
|
||||
await expect(data.erase('Not_An_Id')).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('PGPLUG-008: through the RPC host, db.* reaches the plugin schema and db.engine says postgres', async () => {
|
||||
const router = { callPlugin: async () => undefined, emitPluginEvent: async () => {} };
|
||||
const host = app.get(PluginRpcHostFactory).create('pg-rpc', new Set(['db:own']), router);
|
||||
const call = (method: string, params: Record<string, unknown>) =>
|
||||
host.dispatch({ k: 'req', id: method, method, params });
|
||||
|
||||
expect(await call('db.engine', {})).toMatchObject({ ok: true, result: { engine: 'postgres' } });
|
||||
expect(await call('db.migrate', { id: '001', sql: 'CREATE TABLE t (v text)' })).toMatchObject({ ok: true });
|
||||
expect(await call('db.exec', { sql: 'INSERT INTO t VALUES ($1)', args: ['x'] })).toMatchObject({
|
||||
ok: true,
|
||||
result: { changes: 1 },
|
||||
});
|
||||
expect(await call('db.query', { sql: 'SELECT v FROM t' })).toMatchObject({ ok: true, result: [{ v: 'x' }] });
|
||||
expect(await call('db.query', { sql: 'SELECT email FROM public.users' })).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: 'HOST_ERROR' },
|
||||
});
|
||||
expect(await pluginSchemas()).toContain('trek_plugin_pg_rpc');
|
||||
closePluginDataDb('pg-rpc');
|
||||
});
|
||||
});
|
||||
@@ -28,6 +28,7 @@ import type { LlmConfigResolver } from '../../../src/nest/llm-parse/llm-config.r
|
||||
import type { NotificationsService } from '../../../src/nest/notifications/notifications.service';
|
||||
import type { PermissionsService } from '../../../src/nest/permissions/permissions.service';
|
||||
import { verifyChain } from '../../../src/nest/plugins/host/plugin-audit';
|
||||
import { SqlitePluginDataBackend } from '../../../src/nest/plugins/host/plugin-data.backend';
|
||||
import { getPluginDataDb, closePluginDataDb } from '../../../src/nest/plugins/host/plugin-host-state';
|
||||
import { PluginRpcHostFactory, type PluginCallRouter } from '../../../src/nest/plugins/host/plugin-rpc-host.factory';
|
||||
import { DbRpc } from '../../../src/nest/plugins/host/rpc/db.rpc';
|
||||
@@ -416,7 +417,8 @@ const registry = createTestPluginRegistry([
|
||||
uow,
|
||||
),
|
||||
]);
|
||||
const factory = new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService);
|
||||
const sqliteData = new SqlitePluginDataBackend();
|
||||
const factory = new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService, sqliteData);
|
||||
const stubRouter: PluginCallRouter = { callPlugin: async () => undefined, emitPluginEvent: async () => {} };
|
||||
const makeHost = (id: string, ...perms: string[]) => factory.create(id, new Set(perms), stubRouter);
|
||||
/**
|
||||
@@ -457,7 +459,7 @@ afterAll(() => {
|
||||
|
||||
describe("DbRpc — the plugin's own sqlite", () => {
|
||||
it('HOSTRPC-001 caches one data db per plugin id', () => {
|
||||
expect(getPluginDataDb('wired')).toBe(getPluginDataDb('wired'));
|
||||
expect(getPluginDataDb('wired', sqliteData)).toBe(getPluginDataDb('wired', sqliteData));
|
||||
});
|
||||
|
||||
it('HOSTRPC-002 a granted db:own call runs against the plugin db', async () => {
|
||||
@@ -482,13 +484,19 @@ describe("DbRpc — the plugin's own sqlite", () => {
|
||||
closePluginDataDb('wtx');
|
||||
});
|
||||
|
||||
it('HOSTRPC-004 without db:own not one of the four is reachable', async () => {
|
||||
it('HOSTRPC-004 without db:own not one of the five is reachable', async () => {
|
||||
const host = makeHost('wnogrant');
|
||||
for (const method of ['db.query', 'db.exec', 'db.migrate', 'db.tx']) {
|
||||
for (const method of ['db.query', 'db.exec', 'db.migrate', 'db.tx', 'db.engine']) {
|
||||
expect((await call(host, method, { sql: 'SELECT 1', id: '1', ops: [] })).error?.code).toBe('PERMISSION_DENIED');
|
||||
}
|
||||
});
|
||||
|
||||
it('HOSTRPC-004b db.engine names the engine the plugin SQL runs on', async () => {
|
||||
const host = makeHost('wengine', 'db:own');
|
||||
expect((await call(host, 'db.engine', {})).result).toEqual({ engine: 'sqlite' });
|
||||
closePluginDataDb('wengine');
|
||||
});
|
||||
|
||||
it("HOSTRPC-005 two plugins never see each other's tables", async () => {
|
||||
// The regression this guards: DbRpc is a SINGLETON that serves every plugin, so a
|
||||
// handle captured in a field or a closure would hand plugin B the rows of whichever
|
||||
@@ -508,20 +516,20 @@ describe("DbRpc — the plugin's own sqlite", () => {
|
||||
});
|
||||
|
||||
it('HOSTRPC-006 a handle closed WITHOUT eviction is recreated, not reused dead', () => {
|
||||
const a = getPluginDataDb('closedcache');
|
||||
const a = getPluginDataDb('closedcache', sqliteData);
|
||||
expect(a.isOpen()).toBe(true);
|
||||
// The supervisor's dispose() path: close the handle but leave it cached.
|
||||
a.close();
|
||||
const b = getPluginDataDb('closedcache');
|
||||
const b = getPluginDataDb('closedcache', sqliteData);
|
||||
expect(b).not.toBe(a);
|
||||
expect(b.isOpen()).toBe(true);
|
||||
closePluginDataDb('closedcache');
|
||||
});
|
||||
|
||||
it('HOSTRPC-007 closePluginDataDb drops the cached handle', () => {
|
||||
const a = getPluginDataDb('transient');
|
||||
const a = getPluginDataDb('transient', sqliteData);
|
||||
closePluginDataDb('transient');
|
||||
const b = getPluginDataDb('transient');
|
||||
const b = getPluginDataDb('transient', sqliteData);
|
||||
expect(a).not.toBe(b);
|
||||
closePluginDataDb('transient');
|
||||
});
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* The plugin data port (src/nest/plugins/host/plugin-data.backend.ts): the
|
||||
* backend `PLUGIN_DATA` binds is the engine's, the SQLite one keeps a file per
|
||||
* plugin, and the Postgres one opens a schema store and writes no SQLite file.
|
||||
* The Postgres store against a real database is the Postgres lane's
|
||||
* tests/postgres/plugin-data.test.ts.
|
||||
*/
|
||||
import type { DbSettings } from '../../../src/app-config';
|
||||
import type { DatabaseEngine } from '../../../src/db/engine';
|
||||
import { PostgresPluginDataStore } from '../../../src/db/engine/postgres/plugin-data-store';
|
||||
import {
|
||||
PLUGIN_DATA,
|
||||
PLUGIN_DATA_PROVIDERS,
|
||||
PostgresPluginDataBackend,
|
||||
SqlitePluginDataBackend,
|
||||
} from '../../../src/nest/plugins/host/plugin-data.backend';
|
||||
import { PluginDataDb } from '../../../src/nest/plugins/host/plugin-data.service';
|
||||
import type { MikroORM } from '@mikro-orm/core';
|
||||
import type { FactoryProvider } from '@nestjs/common';
|
||||
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
|
||||
let tmp: string;
|
||||
beforeAll(() => {
|
||||
tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-plugin-data-port-'));
|
||||
process.env.TREK_PLUGINS_DATA_DIR = tmp;
|
||||
});
|
||||
afterAll(() => {
|
||||
delete process.env.TREK_PLUGINS_DATA_DIR;
|
||||
fs.rmSync(tmp, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
/** An ORM whose Kysely client records the SQL it is handed. */
|
||||
function fakeOrm(executed: string[]): MikroORM {
|
||||
const client = {
|
||||
executeQuery: async (query: { sql: string }) => {
|
||||
executed.push(query.sql);
|
||||
return { rows: [] };
|
||||
},
|
||||
};
|
||||
return { em: { getConnection: () => ({ getClient: () => client }) } } as unknown as MikroORM;
|
||||
}
|
||||
|
||||
const POSTGRES = { engine: 'postgres', url: 'postgres://trek@db/trek', poolMax: 4 } as unknown as DbSettings;
|
||||
const SQLITE = { engine: 'sqlite' } as unknown as DbSettings;
|
||||
|
||||
describe('plugin data port', () => {
|
||||
it('PLUGDATA-001: PLUGIN_DATA is the backend of the engine TREK runs on', () => {
|
||||
const binding = PLUGIN_DATA_PROVIDERS.find(
|
||||
(p): p is FactoryProvider => typeof p === 'object' && 'provide' in p && p.provide === PLUGIN_DATA,
|
||||
);
|
||||
expect(binding).toBeDefined();
|
||||
const sqlite = new SqlitePluginDataBackend();
|
||||
const postgres = new PostgresPluginDataBackend(fakeOrm([]), POSTGRES);
|
||||
const pick = (id: 'sqlite' | 'postgres') => binding?.useFactory({ id } as DatabaseEngine, sqlite, postgres);
|
||||
expect(pick('sqlite')).toBe(sqlite);
|
||||
expect(pick('postgres')).toBe(postgres);
|
||||
expect(sqlite.engine).toBe('sqlite');
|
||||
expect(postgres.engine).toBe('postgres');
|
||||
});
|
||||
|
||||
it("PLUGDATA-002: on SQLite a plugin's data is a file of its own, and erasing removes its directory", async () => {
|
||||
const backend = new SqlitePluginDataBackend();
|
||||
const store = backend.open('sqlite-one');
|
||||
expect(store).toBeInstanceOf(PluginDataDb);
|
||||
expect(store.engine).toBe('sqlite');
|
||||
store.close();
|
||||
expect(fs.existsSync(path.join(tmp, 'sqlite-one', 'plugin.db'))).toBe(true);
|
||||
await backend.erase('sqlite-one');
|
||||
expect(fs.existsSync(path.join(tmp, 'sqlite-one'))).toBe(false);
|
||||
});
|
||||
|
||||
it('PLUGDATA-003: on Postgres a plugin gets a schema store and its data directory, but no SQLite file', async () => {
|
||||
const executed: string[] = [];
|
||||
const backend = new PostgresPluginDataBackend(fakeOrm(executed), POSTGRES);
|
||||
const store = backend.open('pg-one');
|
||||
expect(store).toBeInstanceOf(PostgresPluginDataStore);
|
||||
expect(store.engine).toBe('postgres');
|
||||
expect(fs.readdirSync(path.join(tmp, 'pg-one'))).toEqual([]);
|
||||
expect(executed).toEqual([]);
|
||||
|
||||
await backend.erase('pg-one');
|
||||
expect(executed).toEqual(['drop schema if exists "trek_plugin_pg_one" cascade']);
|
||||
expect(fs.existsSync(path.join(tmp, 'pg-one'))).toBe(false);
|
||||
});
|
||||
|
||||
it('PLUGDATA-004: the Postgres backend refuses to open a store without a Postgres database', () => {
|
||||
const backend = new PostgresPluginDataBackend(fakeOrm([]), SQLITE);
|
||||
expect(() => backend.open('pg-two')).toThrow(/without TREK_DB_URL/);
|
||||
});
|
||||
|
||||
it('PLUGDATA-005: a closed Postgres store refuses new calls, so the host opens a fresh one', async () => {
|
||||
const backend = new PostgresPluginDataBackend(fakeOrm([]), POSTGRES);
|
||||
const store = backend.open('pg-three');
|
||||
expect(store.isOpen()).toBe(true);
|
||||
store.close();
|
||||
expect(store.isOpen()).toBe(false);
|
||||
await expect(store.query('SELECT 1')).rejects.toThrow('The database connection is not open');
|
||||
await expect(store.query('SELECT * FROM public.users')).rejects.toThrow('The database connection is not open');
|
||||
});
|
||||
});
|
||||
@@ -46,13 +46,13 @@ describe('plugin RPC coverage ledger', () => {
|
||||
expect(fromRegistry.size).toBe(KNOWN_METHODS.length + UNCONDITIONAL_METHODS.length);
|
||||
});
|
||||
|
||||
it('RPCLEDGER-005 the surface is the 114 methods the protocol declares', () => {
|
||||
it('RPCLEDGER-005 the surface is the 115 methods the protocol declares', () => {
|
||||
// Pinned rather than derived: a method DELETED from KNOWN_METHODS together with
|
||||
// its handler would keep every test above green, and this is the line that makes
|
||||
// that show up in the diff.
|
||||
expect(KNOWN_METHODS.length).toBe(111);
|
||||
expect(KNOWN_METHODS.length).toBe(112);
|
||||
expect(UNCONDITIONAL_METHODS.length).toBe(3);
|
||||
expect(fromRegistry.size).toBe(114);
|
||||
expect(fromRegistry.size).toBe(115);
|
||||
});
|
||||
|
||||
it('RPCLEDGER-006 boot-time total coverage is armed in production', () => {
|
||||
|
||||
Reference in New Issue
Block a user