@powerhousedao/switchboard 6.2.3-dev.76 → 6.2.3-dev.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
- package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
- package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
- package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
- package/.tsbuild/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +33 -0
- package/dist/index.mjs +1 -1
- package/dist/{plugin-host-CGO4XZzK.d.mts → plugin-host-BBqojyyY.d.mts} +6 -1
- package/dist/plugin-host-BBqojyyY.d.mts.map +1 -0
- package/dist/plugins/attachments.d.mts +1 -1
- package/dist/plugins/attachments.mjs +3 -3
- package/dist/plugins/mcp.d.mts +1 -1
- package/dist/plugins/privacy.d.mts +1 -1
- package/dist/plugins/workflows.d.mts +1 -1
- package/dist/{server-CJjK7WpR.mjs → server-Bdq8xquD.mjs} +188 -30
- package/dist/server-Bdq8xquD.mjs.map +1 -0
- package/dist/server.d.mts +8 -2
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +3 -3
- package/package.json +16 -16
- package/test/boot-inspection-enabled.test.ts +1 -14
- package/test/boot-mcp-enabled.test.ts +1 -14
- package/test/boot-port-zero.test.ts +99 -0
- package/test/boot-privacy.test.ts +1 -14
- package/test/boot-workflow-singleton-retry.test.ts +1 -14
- package/test/permissions-backfill.test.ts +486 -0
- package/test/pglite-store.test.ts +15 -1
- package/test/plugin-presence.test.ts +1 -14
- package/test/shutdown.test.ts +1 -2
- package/vitest.config.ts +53 -0
- package/dist/plugin-host-CGO4XZzK.d.mts.map +0 -1
- package/dist/server-CJjK7WpR.mjs.map +0 -1
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
2
|
+
import { getDbClient } from "@powerhousedao/reactor-api";
|
|
3
|
+
import type { ILogger } from "document-model";
|
|
4
|
+
import { Kysely, PostgresDialect, sql } from "kysely";
|
|
5
|
+
import { PGliteDialect } from "kysely-pglite-dialect";
|
|
6
|
+
import pg from "pg";
|
|
7
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
8
|
+
import {
|
|
9
|
+
backfillPermissionTables,
|
|
10
|
+
MIGRATED_TABLE_SUFFIX,
|
|
11
|
+
} from "../src/permissions-backfill.mjs";
|
|
12
|
+
|
|
13
|
+
/** What reactor-api's migrations 001 to 003 leave on the read-model store. */
|
|
14
|
+
const LEGACY_DDL = `
|
|
15
|
+
CREATE TABLE "DocumentPermission" (
|
|
16
|
+
"id" SERIAL PRIMARY KEY,
|
|
17
|
+
"documentId" VARCHAR(255) NOT NULL,
|
|
18
|
+
"userAddress" VARCHAR(255) NOT NULL,
|
|
19
|
+
"permission" VARCHAR(20) NOT NULL CHECK ("permission" IN ('READ', 'WRITE', 'ADMIN')),
|
|
20
|
+
"grantedBy" VARCHAR(255) NOT NULL,
|
|
21
|
+
"createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
22
|
+
"updatedAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
23
|
+
UNIQUE ("documentId", "userAddress")
|
|
24
|
+
);
|
|
25
|
+
CREATE TABLE "OperationUserPermission" (
|
|
26
|
+
"id" SERIAL PRIMARY KEY,
|
|
27
|
+
"documentId" VARCHAR(255) NOT NULL,
|
|
28
|
+
"operationType" VARCHAR(255) NOT NULL,
|
|
29
|
+
"userAddress" VARCHAR(255) NOT NULL,
|
|
30
|
+
"grantedBy" VARCHAR(255) NOT NULL,
|
|
31
|
+
"createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
32
|
+
UNIQUE ("documentId", "operationType", "userAddress")
|
|
33
|
+
);
|
|
34
|
+
CREATE TABLE "DocumentProtection" (
|
|
35
|
+
"documentId" VARCHAR(255) PRIMARY KEY,
|
|
36
|
+
"protected" BOOLEAN NOT NULL DEFAULT false,
|
|
37
|
+
"ownerAddress" VARCHAR(255),
|
|
38
|
+
"createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
39
|
+
"updatedAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
40
|
+
);
|
|
41
|
+
`;
|
|
42
|
+
|
|
43
|
+
/** What reactor migration 029 creates in the reactor schema. */
|
|
44
|
+
const REACTOR_DDL = `
|
|
45
|
+
CREATE SCHEMA reactor;
|
|
46
|
+
CREATE TABLE reactor."DocumentPermission" (
|
|
47
|
+
"id" SERIAL PRIMARY KEY,
|
|
48
|
+
"documentId" VARCHAR(255) NOT NULL,
|
|
49
|
+
"userAddress" VARCHAR(255) NOT NULL,
|
|
50
|
+
"permission" VARCHAR(20) NOT NULL CHECK ("permission" IN ('READ', 'WRITE', 'ADMIN')),
|
|
51
|
+
"grantedBy" VARCHAR(255) NOT NULL,
|
|
52
|
+
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
53
|
+
"updatedAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
54
|
+
UNIQUE ("documentId", "userAddress")
|
|
55
|
+
);
|
|
56
|
+
CREATE TABLE reactor."OperationUserPermission" (
|
|
57
|
+
"id" SERIAL PRIMARY KEY,
|
|
58
|
+
"documentId" VARCHAR(255) NOT NULL,
|
|
59
|
+
"operationType" VARCHAR(255) NOT NULL,
|
|
60
|
+
"userAddress" VARCHAR(255) NOT NULL,
|
|
61
|
+
"grantedBy" VARCHAR(255) NOT NULL,
|
|
62
|
+
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
63
|
+
UNIQUE ("documentId", "operationType", "userAddress")
|
|
64
|
+
);
|
|
65
|
+
CREATE TABLE reactor."DocumentProtection" (
|
|
66
|
+
"documentId" VARCHAR(255) PRIMARY KEY,
|
|
67
|
+
"protected" BOOLEAN NOT NULL DEFAULT false,
|
|
68
|
+
"ownerAddress" VARCHAR(255),
|
|
69
|
+
"createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
70
|
+
"updatedAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
71
|
+
);
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
const CREATED = new Date("2026-01-02T03:04:05.000Z");
|
|
75
|
+
|
|
76
|
+
function createLogger(): ILogger {
|
|
77
|
+
return {
|
|
78
|
+
info: vi.fn(),
|
|
79
|
+
warn: vi.fn(),
|
|
80
|
+
debug: vi.fn(),
|
|
81
|
+
error: vi.fn(),
|
|
82
|
+
} as unknown as ILogger;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The two stores a test runs against; `destroy` releases both. */
|
|
86
|
+
type BackfillStores = {
|
|
87
|
+
readModelDb: Kysely<any>;
|
|
88
|
+
reactorBase: Kysely<any>;
|
|
89
|
+
execRead: (statements: string) => Promise<void>;
|
|
90
|
+
execReactor: (statements: string) => Promise<void>;
|
|
91
|
+
destroy: () => Promise<void>;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
function openPgliteStores(): Promise<BackfillStores> {
|
|
95
|
+
const readPg = new PGlite();
|
|
96
|
+
const reactorPg = new PGlite();
|
|
97
|
+
const readModelDb = new Kysely<any>({ dialect: new PGliteDialect(readPg) });
|
|
98
|
+
const reactorBase = new Kysely<any>({
|
|
99
|
+
dialect: new PGliteDialect(reactorPg),
|
|
100
|
+
});
|
|
101
|
+
return Promise.resolve({
|
|
102
|
+
readModelDb,
|
|
103
|
+
reactorBase,
|
|
104
|
+
execRead: async (statements) => {
|
|
105
|
+
await readPg.exec(statements);
|
|
106
|
+
},
|
|
107
|
+
execReactor: async (statements) => {
|
|
108
|
+
await reactorPg.exec(statements);
|
|
109
|
+
},
|
|
110
|
+
destroy: async () => {
|
|
111
|
+
await readModelDb.destroy();
|
|
112
|
+
await reactorBase.destroy();
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The read-model store opened the way reactor-api opens it (knex over PGlite,
|
|
119
|
+
* UTC timestamp parsers), which is the handle the switchboard receives.
|
|
120
|
+
*/
|
|
121
|
+
function openReactorApiReadStore(): Promise<BackfillStores> {
|
|
122
|
+
const read = getDbClient();
|
|
123
|
+
const reactorPg = new PGlite();
|
|
124
|
+
const reactorBase = new Kysely<any>({
|
|
125
|
+
dialect: new PGliteDialect(reactorPg),
|
|
126
|
+
});
|
|
127
|
+
return Promise.resolve({
|
|
128
|
+
readModelDb: read.db,
|
|
129
|
+
reactorBase,
|
|
130
|
+
execRead: async (statements) => {
|
|
131
|
+
await read.pglite!.exec(statements);
|
|
132
|
+
},
|
|
133
|
+
execReactor: async (statements) => {
|
|
134
|
+
await reactorPg.exec(statements);
|
|
135
|
+
},
|
|
136
|
+
destroy: async () => {
|
|
137
|
+
await read.knex.destroy();
|
|
138
|
+
if (!read.pglite!.closed) await read.pglite!.close();
|
|
139
|
+
await reactorBase.destroy();
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Both stores in one Postgres database, the read model in `public` and the
|
|
146
|
+
* reactor in its schema: what a bare `dbPath` gives a Postgres deployment.
|
|
147
|
+
*/
|
|
148
|
+
async function openPostgresStores(url: string): Promise<BackfillStores> {
|
|
149
|
+
const name = `sb_permissions_backfill_${process.pid}`;
|
|
150
|
+
const admin = new pg.Pool({ connectionString: url });
|
|
151
|
+
await admin.query(`DROP DATABASE IF EXISTS "${name}" WITH (FORCE)`);
|
|
152
|
+
await admin.query(`CREATE DATABASE "${name}"`);
|
|
153
|
+
const target = new URL(url);
|
|
154
|
+
target.pathname = `/${name}`;
|
|
155
|
+
const readPool = new pg.Pool({ connectionString: target.toString() });
|
|
156
|
+
const reactorPool = new pg.Pool({ connectionString: target.toString() });
|
|
157
|
+
for (const pool of [readPool, reactorPool]) {
|
|
158
|
+
// Dropping the database terminates whatever is still connected to it.
|
|
159
|
+
pool.on("error", (error: Error & { code?: string }) => {
|
|
160
|
+
if (error.code !== "57P01") throw error;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
const readModelDb = new Kysely<any>({
|
|
164
|
+
dialect: new PostgresDialect({ pool: readPool }),
|
|
165
|
+
});
|
|
166
|
+
const reactorBase = new Kysely<any>({
|
|
167
|
+
dialect: new PostgresDialect({ pool: reactorPool }),
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
readModelDb,
|
|
171
|
+
reactorBase,
|
|
172
|
+
execRead: async (statements) => {
|
|
173
|
+
await readPool.query(statements);
|
|
174
|
+
},
|
|
175
|
+
execReactor: async (statements) => {
|
|
176
|
+
await reactorPool.query(statements);
|
|
177
|
+
},
|
|
178
|
+
destroy: async () => {
|
|
179
|
+
try {
|
|
180
|
+
await readModelDb.destroy();
|
|
181
|
+
await reactorBase.destroy();
|
|
182
|
+
} finally {
|
|
183
|
+
await admin.query(`DROP DATABASE IF EXISTS "${name}" WITH (FORCE)`);
|
|
184
|
+
await admin.end();
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const PG_URL = process.env.REACTOR_TEST_PG_URL;
|
|
191
|
+
|
|
192
|
+
defineBackfillCases("PGlite", openPgliteStores, false);
|
|
193
|
+
defineBackfillCases("reactor-api read store", openReactorApiReadStore, false);
|
|
194
|
+
defineBackfillCases("Postgres", () => openPostgresStores(PG_URL!), !PG_URL);
|
|
195
|
+
|
|
196
|
+
function defineBackfillCases(
|
|
197
|
+
backend: string,
|
|
198
|
+
open: () => Promise<BackfillStores>,
|
|
199
|
+
skip: boolean,
|
|
200
|
+
): void {
|
|
201
|
+
describe.skipIf(skip)(`backfillPermissionTables [${backend}]`, () => {
|
|
202
|
+
let stores: BackfillStores;
|
|
203
|
+
let readModelDb: Kysely<any>;
|
|
204
|
+
let reactorDb: Kysely<any>;
|
|
205
|
+
let logger: ILogger;
|
|
206
|
+
|
|
207
|
+
const seedLegacy = async () => {
|
|
208
|
+
await stores.execRead(LEGACY_DDL);
|
|
209
|
+
await readModelDb
|
|
210
|
+
.insertInto("DocumentPermission")
|
|
211
|
+
.values([
|
|
212
|
+
{
|
|
213
|
+
documentId: "doc-1",
|
|
214
|
+
userAddress: "0xreader",
|
|
215
|
+
permission: "READ",
|
|
216
|
+
grantedBy: "0xowner",
|
|
217
|
+
createdAt: CREATED,
|
|
218
|
+
updatedAt: CREATED,
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
documentId: "doc-1",
|
|
222
|
+
userAddress: "0xowner",
|
|
223
|
+
permission: "ADMIN",
|
|
224
|
+
grantedBy: "0xowner",
|
|
225
|
+
createdAt: CREATED,
|
|
226
|
+
updatedAt: CREATED,
|
|
227
|
+
},
|
|
228
|
+
])
|
|
229
|
+
.execute();
|
|
230
|
+
await readModelDb
|
|
231
|
+
.insertInto("OperationUserPermission")
|
|
232
|
+
.values({
|
|
233
|
+
documentId: "doc-1",
|
|
234
|
+
operationType: "SET_NAME",
|
|
235
|
+
userAddress: "0xreader",
|
|
236
|
+
grantedBy: "0xowner",
|
|
237
|
+
createdAt: CREATED,
|
|
238
|
+
})
|
|
239
|
+
.execute();
|
|
240
|
+
await readModelDb
|
|
241
|
+
.insertInto("DocumentProtection")
|
|
242
|
+
.values({
|
|
243
|
+
documentId: "doc-1",
|
|
244
|
+
protected: true,
|
|
245
|
+
ownerAddress: "0xowner",
|
|
246
|
+
createdAt: CREATED,
|
|
247
|
+
updatedAt: CREATED,
|
|
248
|
+
})
|
|
249
|
+
.execute();
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const readModelTables = async (): Promise<string[]> => {
|
|
253
|
+
const result = await sql<{ table_name: string }>`
|
|
254
|
+
select table_name from information_schema.tables
|
|
255
|
+
where table_schema = 'public' order by table_name
|
|
256
|
+
`.execute(readModelDb);
|
|
257
|
+
return result.rows.map((row) => row.table_name);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
beforeEach(async () => {
|
|
261
|
+
stores = await open();
|
|
262
|
+
readModelDb = stores.readModelDb;
|
|
263
|
+
await stores.execReactor(REACTOR_DDL);
|
|
264
|
+
reactorDb = stores.reactorBase.withSchema("reactor");
|
|
265
|
+
logger = createLogger();
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
afterEach(async () => {
|
|
269
|
+
await stores.destroy();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("copies every row into the reactor store and renames the read-model tables", async () => {
|
|
273
|
+
await seedLegacy();
|
|
274
|
+
|
|
275
|
+
const outcome = await backfillPermissionTables(
|
|
276
|
+
readModelDb,
|
|
277
|
+
reactorDb,
|
|
278
|
+
logger,
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
expect(outcome).toEqual({
|
|
282
|
+
status: "copied",
|
|
283
|
+
counts: {
|
|
284
|
+
DocumentPermission: 2,
|
|
285
|
+
OperationUserPermission: 1,
|
|
286
|
+
DocumentProtection: 1,
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
expect(
|
|
290
|
+
await reactorDb
|
|
291
|
+
.selectFrom("DocumentPermission")
|
|
292
|
+
.select(["documentId", "userAddress", "permission", "grantedBy"])
|
|
293
|
+
.orderBy("id")
|
|
294
|
+
.execute(),
|
|
295
|
+
).toEqual([
|
|
296
|
+
{
|
|
297
|
+
documentId: "doc-1",
|
|
298
|
+
userAddress: "0xreader",
|
|
299
|
+
permission: "READ",
|
|
300
|
+
grantedBy: "0xowner",
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
documentId: "doc-1",
|
|
304
|
+
userAddress: "0xowner",
|
|
305
|
+
permission: "ADMIN",
|
|
306
|
+
grantedBy: "0xowner",
|
|
307
|
+
},
|
|
308
|
+
]);
|
|
309
|
+
expect(
|
|
310
|
+
await reactorDb
|
|
311
|
+
.selectFrom("OperationUserPermission")
|
|
312
|
+
.select(["documentId", "operationType", "userAddress", "grantedBy"])
|
|
313
|
+
.execute(),
|
|
314
|
+
).toEqual([
|
|
315
|
+
{
|
|
316
|
+
documentId: "doc-1",
|
|
317
|
+
operationType: "SET_NAME",
|
|
318
|
+
userAddress: "0xreader",
|
|
319
|
+
grantedBy: "0xowner",
|
|
320
|
+
},
|
|
321
|
+
]);
|
|
322
|
+
const protection = await reactorDb
|
|
323
|
+
.selectFrom("DocumentProtection")
|
|
324
|
+
.selectAll()
|
|
325
|
+
.executeTakeFirstOrThrow();
|
|
326
|
+
expect(protection).toMatchObject({
|
|
327
|
+
documentId: "doc-1",
|
|
328
|
+
protected: true,
|
|
329
|
+
ownerAddress: "0xowner",
|
|
330
|
+
});
|
|
331
|
+
const legacyCreated = await readModelDb
|
|
332
|
+
.selectFrom(`DocumentProtection${MIGRATED_TABLE_SUFFIX}`)
|
|
333
|
+
.select("createdAt")
|
|
334
|
+
.executeTakeFirstOrThrow();
|
|
335
|
+
expect((protection.createdAt as Date).getTime()).toBe(
|
|
336
|
+
(legacyCreated.createdAt as Date).getTime(),
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
expect(await readModelTables()).toEqual([
|
|
340
|
+
`DocumentPermission${MIGRATED_TABLE_SUFFIX}`,
|
|
341
|
+
`DocumentProtection${MIGRATED_TABLE_SUFFIX}`,
|
|
342
|
+
`OperationUserPermission${MIGRATED_TABLE_SUFFIX}`,
|
|
343
|
+
]);
|
|
344
|
+
expect(logger.info).toHaveBeenCalledWith(
|
|
345
|
+
`Permission tables backfill: copied DocumentPermission=2, OperationUserPermission=1, DocumentProtection=1 from the read-model store to the reactor store; renamed the read-model tables to *${MIGRATED_TABLE_SUFFIX}`,
|
|
346
|
+
);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("does nothing on a second run", async () => {
|
|
350
|
+
await seedLegacy();
|
|
351
|
+
await backfillPermissionTables(readModelDb, reactorDb, logger);
|
|
352
|
+
|
|
353
|
+
const outcome = await backfillPermissionTables(
|
|
354
|
+
readModelDb,
|
|
355
|
+
reactorDb,
|
|
356
|
+
logger,
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
expect(outcome).toEqual({ status: "already-backfilled" });
|
|
360
|
+
const count = await reactorDb
|
|
361
|
+
.selectFrom("DocumentPermission")
|
|
362
|
+
.select((eb) => eb.fn.countAll().as("count"))
|
|
363
|
+
.executeTakeFirstOrThrow();
|
|
364
|
+
expect(Number(count.count)).toBe(2);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("does nothing on a fresh install", async () => {
|
|
368
|
+
const outcome = await backfillPermissionTables(
|
|
369
|
+
readModelDb,
|
|
370
|
+
reactorDb,
|
|
371
|
+
logger,
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
expect(outcome).toEqual({ status: "fresh-install" });
|
|
375
|
+
expect(await readModelTables()).toEqual([]);
|
|
376
|
+
expect(logger.info).not.toHaveBeenCalled();
|
|
377
|
+
expect(logger.warn).not.toHaveBeenCalled();
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("renames empty legacy tables without copying", async () => {
|
|
381
|
+
await stores.execRead(LEGACY_DDL);
|
|
382
|
+
|
|
383
|
+
const outcome = await backfillPermissionTables(
|
|
384
|
+
readModelDb,
|
|
385
|
+
reactorDb,
|
|
386
|
+
logger,
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
expect(outcome).toEqual({
|
|
390
|
+
status: "copied",
|
|
391
|
+
counts: {
|
|
392
|
+
DocumentPermission: 0,
|
|
393
|
+
OperationUserPermission: 0,
|
|
394
|
+
DocumentProtection: 0,
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
expect(await readModelTables()).toHaveLength(3);
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it("only renames when an earlier boot copied the rows but stopped before renaming", async () => {
|
|
401
|
+
await seedLegacy();
|
|
402
|
+
await backfillPermissionTables(readModelDb, reactorDb, logger);
|
|
403
|
+
for (const table of [
|
|
404
|
+
"DocumentPermission",
|
|
405
|
+
"OperationUserPermission",
|
|
406
|
+
"DocumentProtection",
|
|
407
|
+
]) {
|
|
408
|
+
await sql`ALTER TABLE ${sql.id(`${table}${MIGRATED_TABLE_SUFFIX}`)} RENAME TO ${sql.id(table)}`.execute(
|
|
409
|
+
readModelDb,
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const outcome = await backfillPermissionTables(
|
|
414
|
+
readModelDb,
|
|
415
|
+
reactorDb,
|
|
416
|
+
logger,
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
expect(outcome.status).toBe("rename-completed");
|
|
420
|
+
const count = await reactorDb
|
|
421
|
+
.selectFrom("DocumentPermission")
|
|
422
|
+
.select((eb) => eb.fn.countAll().as("count"))
|
|
423
|
+
.executeTakeFirstOrThrow();
|
|
424
|
+
expect(Number(count.count)).toBe(2);
|
|
425
|
+
expect(await readModelTables()).toEqual([
|
|
426
|
+
`DocumentPermission${MIGRATED_TABLE_SUFFIX}`,
|
|
427
|
+
`DocumentProtection${MIGRATED_TABLE_SUFFIX}`,
|
|
428
|
+
`OperationUserPermission${MIGRATED_TABLE_SUFFIX}`,
|
|
429
|
+
]);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it("refuses when the reactor store already holds different rows", async () => {
|
|
433
|
+
await seedLegacy();
|
|
434
|
+
await reactorDb
|
|
435
|
+
.insertInto("DocumentPermission")
|
|
436
|
+
.values({
|
|
437
|
+
documentId: "doc-9",
|
|
438
|
+
userAddress: "0xsomeone",
|
|
439
|
+
permission: "WRITE",
|
|
440
|
+
grantedBy: "0xowner",
|
|
441
|
+
})
|
|
442
|
+
.execute();
|
|
443
|
+
|
|
444
|
+
const outcome = await backfillPermissionTables(
|
|
445
|
+
readModelDb,
|
|
446
|
+
reactorDb,
|
|
447
|
+
logger,
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
expect(outcome.status).toBe("refused");
|
|
451
|
+
expect(logger.warn).toHaveBeenCalledTimes(1);
|
|
452
|
+
expect(await readModelTables()).toEqual([
|
|
453
|
+
"DocumentPermission",
|
|
454
|
+
"DocumentProtection",
|
|
455
|
+
"OperationUserPermission",
|
|
456
|
+
]);
|
|
457
|
+
const count = await reactorDb
|
|
458
|
+
.selectFrom("DocumentPermission")
|
|
459
|
+
.select((eb) => eb.fn.countAll().as("count"))
|
|
460
|
+
.executeTakeFirstOrThrow();
|
|
461
|
+
expect(Number(count.count)).toBe(1);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it("rolls the copy back when a row does not fit the reactor table", async () => {
|
|
465
|
+
await seedLegacy();
|
|
466
|
+
await stores.execReactor(
|
|
467
|
+
`ALTER TABLE reactor."OperationUserPermission" ADD CONSTRAINT no_set_name CHECK ("operationType" <> 'SET_NAME')`,
|
|
468
|
+
);
|
|
469
|
+
|
|
470
|
+
await expect(
|
|
471
|
+
backfillPermissionTables(readModelDb, reactorDb, logger),
|
|
472
|
+
).rejects.toThrow();
|
|
473
|
+
|
|
474
|
+
const count = await reactorDb
|
|
475
|
+
.selectFrom("DocumentPermission")
|
|
476
|
+
.select((eb) => eb.fn.countAll().as("count"))
|
|
477
|
+
.executeTakeFirstOrThrow();
|
|
478
|
+
expect(Number(count.count)).toBe(0);
|
|
479
|
+
expect(await readModelTables()).toEqual([
|
|
480
|
+
"DocumentPermission",
|
|
481
|
+
"DocumentProtection",
|
|
482
|
+
"OperationUserPermission",
|
|
483
|
+
]);
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
}
|
|
@@ -296,16 +296,30 @@ describe("vacuumFullTimingFromEnv", () => {
|
|
|
296
296
|
it("reads both timings, and leaves unset or invalid values to the defaults", () => {
|
|
297
297
|
expect(
|
|
298
298
|
vacuumFullTimingFromEnv({
|
|
299
|
+
PH_PGLITE_VACUUM_FULL: "quiet",
|
|
299
300
|
PH_PGLITE_VACUUM_FULL_QUIET_MS: "0",
|
|
300
301
|
PH_PGLITE_VACUUM_FULL_MAX_DEFER_MS: "600000",
|
|
301
302
|
}),
|
|
302
303
|
).toEqual({ vacuumFullQuietMs: 0, vacuumFullMaxDeferMs: 600_000 });
|
|
303
|
-
expect(vacuumFullTimingFromEnv({})).toEqual({});
|
|
304
|
+
expect(vacuumFullTimingFromEnv({})).toEqual({ vacuumFullAboveBytes: 0 });
|
|
304
305
|
expect(
|
|
305
306
|
vacuumFullTimingFromEnv({
|
|
307
|
+
PH_PGLITE_VACUUM_FULL: "quiet",
|
|
306
308
|
PH_PGLITE_VACUUM_FULL_QUIET_MS: "soon",
|
|
307
309
|
PH_PGLITE_VACUUM_FULL_MAX_DEFER_MS: "-5",
|
|
308
310
|
}),
|
|
309
311
|
).toEqual({});
|
|
310
312
|
});
|
|
313
|
+
|
|
314
|
+
it("turns the mode into the settings the stores open with", () => {
|
|
315
|
+
expect(vacuumFullTimingFromEnv({ PH_PGLITE_VACUUM_FULL: "off" })).toEqual({
|
|
316
|
+
vacuumFullAboveBytes: 0,
|
|
317
|
+
});
|
|
318
|
+
expect(
|
|
319
|
+
vacuumFullTimingFromEnv({ PH_PGLITE_VACUUM_FULL: "forced" }),
|
|
320
|
+
).toEqual({ vacuumFullMaxDeferMs: 1_800_000 });
|
|
321
|
+
expect(() =>
|
|
322
|
+
vacuumFullTimingFromEnv({ PH_PGLITE_VACUUM_FULL: "sometimes" }),
|
|
323
|
+
).toThrow(/PH_PGLITE_VACUUM_FULL/);
|
|
324
|
+
});
|
|
311
325
|
});
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { mkdtemp, rm } from "node:fs/promises";
|
|
2
|
-
import { createServer } from "node:net";
|
|
3
2
|
import { tmpdir } from "node:os";
|
|
4
3
|
import { join } from "node:path";
|
|
5
4
|
import type { HostPlugin, ResolvedPlugin } from "@powerhousedao/host-core";
|
|
@@ -11,17 +10,6 @@ import {
|
|
|
11
10
|
} from "../src/plugin-host.mjs";
|
|
12
11
|
import { startSwitchboard, type StartServerOptions } from "../src/server.mjs";
|
|
13
12
|
|
|
14
|
-
// `port: 0` would bind a random port the switchboard does not report back, so
|
|
15
|
-
// the test picks a free one and pins it.
|
|
16
|
-
async function freePort(): Promise<number> {
|
|
17
|
-
const server = createServer();
|
|
18
|
-
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
19
|
-
const address = server.address();
|
|
20
|
-
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
21
|
-
if (!address || typeof address === "string") throw new Error("no port");
|
|
22
|
-
return address.port;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
13
|
function stubLogger(): ILogger {
|
|
26
14
|
const logger = {
|
|
27
15
|
verbose: vi.fn(),
|
|
@@ -66,8 +54,7 @@ async function withPluginHostSwitchboard(
|
|
|
66
54
|
switchboard = await startSwitchboard({
|
|
67
55
|
workflows: { enabled: false },
|
|
68
56
|
dbPath: join(tempRoot, "read-model"),
|
|
69
|
-
port:
|
|
70
|
-
strictPort: true,
|
|
57
|
+
port: 0,
|
|
71
58
|
disableLocalPackages: true,
|
|
72
59
|
identity: { keypairPath: join(tempRoot, "identity.json") },
|
|
73
60
|
drive: {
|
package/test/shutdown.test.ts
CHANGED
|
@@ -120,8 +120,7 @@ describe("SwitchboardReactor.shutdown", () => {
|
|
|
120
120
|
reactor,
|
|
121
121
|
workflows: { enabled: false },
|
|
122
122
|
dbPath: join(tempRoot, "read-model"),
|
|
123
|
-
port:
|
|
124
|
-
strictPort: true,
|
|
123
|
+
port: 0,
|
|
125
124
|
mcp: false,
|
|
126
125
|
disableLocalPackages: true,
|
|
127
126
|
identity: { keypairPath: join(tempRoot, "identity.json") },
|
package/vitest.config.ts
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
1
3
|
import { defineConfig } from "vitest/config";
|
|
2
4
|
|
|
5
|
+
const packages = resolve(import.meta.dirname, "../../packages");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The reactor, reactor-api and reactor-drive are read from source, so a test
|
|
9
|
+
* never runs the switchboard's wiring against a stale dist of the packages it
|
|
10
|
+
* wires (the client halves live in their source first). Only these: the
|
|
11
|
+
* "source" condition for every package breaks those whose source leans on
|
|
12
|
+
* their own build.
|
|
13
|
+
*/
|
|
14
|
+
const SOURCE_ALIASES: Record<string, string> = {
|
|
15
|
+
"@powerhousedao/reactor": "reactor/index.ts",
|
|
16
|
+
"@powerhousedao/reactor/rpc": "reactor/src/rpc/index.ts",
|
|
17
|
+
"@powerhousedao/reactor-api": "reactor-api/index.mts",
|
|
18
|
+
"@powerhousedao/reactor-api/vite": "reactor-api/src/packages/vite-loader.mts",
|
|
19
|
+
"@powerhousedao/reactor-api/https-hooks":
|
|
20
|
+
"reactor-api/src/packages/https-hooks.mts",
|
|
21
|
+
"@powerhousedao/reactor-api/pglite-node":
|
|
22
|
+
"reactor-api/src/pglite/pglite-node.ts",
|
|
23
|
+
"@powerhousedao/reactor-api/subgraph-types":
|
|
24
|
+
"reactor-api/src/graphql/subgraph-types.ts",
|
|
25
|
+
"@powerhousedao/reactor-drive": "reactor-drive/index.ts",
|
|
26
|
+
"@powerhousedao/reactor-drive/subgraph":
|
|
27
|
+
"reactor-drive/src/subgraph/index.ts",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const escape = (text: string) => text.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
|
|
31
|
+
|
|
3
32
|
export default defineConfig({
|
|
33
|
+
resolve: {
|
|
34
|
+
alias: Object.entries(SOURCE_ALIASES).map(([name, path]) => ({
|
|
35
|
+
find: new RegExp(`^${escape(name)}$`),
|
|
36
|
+
replacement: resolve(packages, path),
|
|
37
|
+
})),
|
|
38
|
+
},
|
|
39
|
+
plugins: [
|
|
40
|
+
{
|
|
41
|
+
// reactor-api's source imports its GraphQL schema files as strings.
|
|
42
|
+
name: "graphql-source-loader",
|
|
43
|
+
resolveId(source, importer) {
|
|
44
|
+
if (source.endsWith(".graphql")) {
|
|
45
|
+
return resolve(dirname(importer || ""), source);
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
},
|
|
49
|
+
load(id) {
|
|
50
|
+
if (id.endsWith(".graphql")) {
|
|
51
|
+
return `export default ${JSON.stringify(readFileSync(id, "utf-8"))}`;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
],
|
|
4
57
|
test: {
|
|
5
58
|
pool: "forks",
|
|
6
59
|
environment: "node",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin-host-CGO4XZzK.d.mts","names":[],"sources":["../src/plugin-host.mts"],"mappings":";;;;;;;UAgDiB;;;EAGf;EACA,SAAS;EACT;EACA;;;EAGA;;;EAGA;EACA;;;EAGA;EACA;EACA;EACA;EACA;EACA;EACA;;EAEA,gBAAgB;EAChB,eAAe,eAAe"}
|