@powerhousedao/switchboard 6.2.3-dev.7 → 6.2.3-dev.71

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.
Files changed (195) hide show
  1. package/.tsbuild/test/attachments/byte-grants.test.d.ts +2 -0
  2. package/.tsbuild/test/attachments/byte-grants.test.d.ts.map +1 -0
  3. package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts +2 -0
  4. package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts.map +1 -0
  5. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts +2 -0
  6. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts.map +1 -0
  7. package/.tsbuild/test/attachments/url-signer.test.d.ts +2 -0
  8. package/.tsbuild/test/attachments/url-signer.test.d.ts.map +1 -0
  9. package/.tsbuild/test/boot-create-signature-policy.test.d.ts +2 -0
  10. package/.tsbuild/test/boot-create-signature-policy.test.d.ts.map +1 -0
  11. package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
  12. package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
  13. package/.tsbuild/test/boot-mcp-enabled.test.d.ts +2 -0
  14. package/.tsbuild/test/boot-mcp-enabled.test.d.ts.map +1 -0
  15. package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
  16. package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
  17. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts +2 -0
  18. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts.map +1 -0
  19. package/.tsbuild/test/boot-without-renown.test.d.ts +2 -0
  20. package/.tsbuild/test/boot-without-renown.test.d.ts.map +1 -0
  21. package/.tsbuild/test/boot-without-require-signatures.test.d.ts +2 -0
  22. package/.tsbuild/test/boot-without-require-signatures.test.d.ts.map +1 -0
  23. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts +2 -0
  24. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts.map +1 -0
  25. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
  26. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
  27. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
  28. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
  29. package/.tsbuild/test/builder-defaults.test.d.ts +2 -0
  30. package/.tsbuild/test/builder-defaults.test.d.ts.map +1 -0
  31. package/.tsbuild/test/create-signature-policy.test.d.ts +2 -0
  32. package/.tsbuild/test/create-signature-policy.test.d.ts.map +1 -0
  33. package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
  34. package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
  35. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
  36. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
  37. package/.tsbuild/test/fatal-shutdown.test.d.ts +2 -0
  38. package/.tsbuild/test/fatal-shutdown.test.d.ts.map +1 -0
  39. package/.tsbuild/test/inspection-flag.test.d.ts +2 -0
  40. package/.tsbuild/test/inspection-flag.test.d.ts.map +1 -0
  41. package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
  42. package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
  43. package/.tsbuild/test/mcp-flag.test.d.ts +2 -0
  44. package/.tsbuild/test/mcp-flag.test.d.ts.map +1 -0
  45. package/.tsbuild/test/observability-signals.test.d.ts +2 -0
  46. package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
  47. package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
  48. package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
  49. package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
  50. package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
  51. package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
  52. package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
  53. package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
  54. package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
  55. package/.tsbuild/test/port-probe.test.d.ts +2 -0
  56. package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
  57. package/.tsbuild/test/renown-trust-policy.test.d.ts +2 -0
  58. package/.tsbuild/test/renown-trust-policy.test.d.ts.map +1 -0
  59. package/.tsbuild/test/shutdown.test.d.ts +2 -0
  60. package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
  61. package/.tsbuild/test/snapshot-writer.d.ts +18 -0
  62. package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
  63. package/.tsbuild/test/unsupported-stored-documents.test.d.ts +2 -0
  64. package/.tsbuild/test/unsupported-stored-documents.test.d.ts.map +1 -0
  65. package/.tsbuild/test/workflow/harness.d.ts +56 -0
  66. package/.tsbuild/test/workflow/harness.d.ts.map +1 -0
  67. package/.tsbuild/test/workflow/oauth.test.d.ts +2 -0
  68. package/.tsbuild/test/workflow/oauth.test.d.ts.map +1 -0
  69. package/.tsbuild/test/workflow/resolvers.test.d.ts +2 -0
  70. package/.tsbuild/test/workflow/resolvers.test.d.ts.map +1 -0
  71. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts +2 -0
  72. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts.map +1 -0
  73. package/.tsbuild/test/workflow/webhook-signed.test.d.ts +2 -0
  74. package/.tsbuild/test/workflow/webhook-signed.test.d.ts.map +1 -0
  75. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts +2 -0
  76. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts.map +1 -0
  77. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts +2 -0
  78. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts.map +1 -0
  79. package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
  80. package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
  81. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
  82. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
  83. package/.tsbuild/test/workflow-read-gate.test.d.ts +2 -0
  84. package/.tsbuild/test/workflow-read-gate.test.d.ts.map +1 -0
  85. package/.tsbuild/test/workflow-runtime.test.d.ts +2 -0
  86. package/.tsbuild/test/workflow-runtime.test.d.ts.map +1 -0
  87. package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
  88. package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
  89. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  90. package/.tsbuild/tsdown.config.d.ts.map +1 -1
  91. package/.tsbuild/vitest.config.d.ts.map +1 -1
  92. package/Auth.md +144 -2
  93. package/CHANGELOG.md +869 -0
  94. package/README.md +47 -10
  95. package/dist/index.d.mts +2 -1
  96. package/dist/index.mjs +50 -50
  97. package/dist/index.mjs.map +1 -1
  98. package/dist/install-packages.d.mts +2 -1
  99. package/dist/install-packages.mjs +2 -2
  100. package/dist/install-packages.mjs.map +1 -1
  101. package/dist/mcp-SsBK14Jm.mjs +32 -0
  102. package/dist/mcp-SsBK14Jm.mjs.map +1 -0
  103. package/dist/migrate.d.mts +2 -1
  104. package/dist/migrate.mjs +4 -3
  105. package/dist/migrate.mjs.map +1 -1
  106. package/dist/plugin-host-CGO4XZzK.d.mts +38 -0
  107. package/dist/plugin-host-CGO4XZzK.d.mts.map +1 -0
  108. package/dist/plugins/attachments.d.mts +33 -0
  109. package/dist/plugins/attachments.d.mts.map +1 -0
  110. package/dist/plugins/attachments.mjs +4 -0
  111. package/dist/plugins/mcp.d.mts +31 -0
  112. package/dist/plugins/mcp.d.mts.map +1 -0
  113. package/dist/plugins/mcp.mjs +4 -0
  114. package/dist/plugins/privacy.d.mts +22 -0
  115. package/dist/plugins/privacy.d.mts.map +1 -0
  116. package/dist/plugins/privacy.mjs +4 -0
  117. package/dist/plugins/workflows.d.mts +79 -0
  118. package/dist/plugins/workflows.d.mts.map +1 -0
  119. package/dist/plugins/workflows.mjs +4 -0
  120. package/dist/privacy-BsFl7GzK.mjs +118 -0
  121. package/dist/privacy-BsFl7GzK.mjs.map +1 -0
  122. package/dist/{server-CiKkVE6g.mjs → server-Be1vmpxu.mjs} +1213 -244
  123. package/dist/server-Be1vmpxu.mjs.map +1 -0
  124. package/dist/server.d.mts +208 -45
  125. package/dist/server.d.mts.map +1 -1
  126. package/dist/server.mjs +4 -5
  127. package/dist/{utils-Baw7rThP.mjs → utils-Dh845exv.mjs} +48 -9
  128. package/dist/utils-Dh845exv.mjs.map +1 -0
  129. package/dist/utils.d.mts +10 -5
  130. package/dist/utils.d.mts.map +1 -1
  131. package/dist/utils.mjs +3 -3
  132. package/dist/workflow-runtime-7GaJ13c9.d.mts +30 -0
  133. package/dist/workflow-runtime-7GaJ13c9.d.mts.map +1 -0
  134. package/dist/workflows-fsBcElEi.mjs +1382 -0
  135. package/dist/workflows-fsBcElEi.mjs.map +1 -0
  136. package/package.json +50 -20
  137. package/scripts/install-packages.sh +0 -0
  138. package/test/attachment-reference-read-model.test.ts +6 -1
  139. package/test/attachments/auth.test.ts +85 -0
  140. package/test/attachments/byte-grants.test.ts +290 -0
  141. package/test/attachments/byte-routes-read-gate.integration.test.ts +314 -0
  142. package/test/attachments/download-target.test.ts +201 -32
  143. package/test/attachments/index.test.ts +47 -4
  144. package/test/attachments/reserve-metadata-parity.test.ts +96 -0
  145. package/test/attachments/routes-integration.test.ts +60 -4
  146. package/test/attachments/routes.test.ts +271 -24
  147. package/test/attachments/service-config.test.ts +13 -3
  148. package/test/attachments/url-signer.test.ts +105 -0
  149. package/test/boot-create-signature-policy.test.ts +108 -0
  150. package/test/boot-inspection-enabled.test.ts +150 -0
  151. package/test/boot-mcp-enabled.test.ts +130 -0
  152. package/test/boot-privacy.test.ts +243 -0
  153. package/test/boot-unsupported-stored-documents.test.ts +213 -0
  154. package/test/boot-without-renown.test.ts +81 -0
  155. package/test/boot-without-require-signatures.test.ts +74 -0
  156. package/test/boot-worker-trust-policy.test.ts +90 -0
  157. package/test/boot-workflow-singleton-conflict.test.ts +107 -0
  158. package/test/boot-workflow-singleton-retry.test.ts +133 -0
  159. package/test/builder-defaults.test.ts +80 -0
  160. package/test/create-signature-policy.test.ts +69 -0
  161. package/test/default-drive-slug.test.ts +97 -0
  162. package/test/durable-node-fs-pg16.test.ts +80 -0
  163. package/test/fatal-shutdown.test.ts +165 -0
  164. package/test/inspection-flag.test.ts +62 -0
  165. package/test/inspection-throttle.test.ts +23 -0
  166. package/test/mcp-flag.test.ts +56 -0
  167. package/test/observability-signals.test.ts +131 -0
  168. package/test/pglite-dialect.test.ts +217 -2
  169. package/test/pglite-preflight.test.ts +313 -0
  170. package/test/pglite-storage-dirs.test.ts +103 -0
  171. package/test/pglite-version.test.ts +9 -1
  172. package/test/plugin-config-schemas.test.ts +83 -0
  173. package/test/plugin-presence.test.ts +269 -0
  174. package/test/port-probe.test.ts +45 -0
  175. package/test/renown-trust-policy.test.ts +315 -0
  176. package/test/shutdown.test.ts +278 -0
  177. package/test/snapshot-writer.ts +94 -0
  178. package/test/unsupported-stored-documents.test.ts +72 -0
  179. package/test/worker-pool.test.ts +27 -1
  180. package/test/workflow/harness.ts +289 -0
  181. package/test/workflow/oauth.test.ts +214 -0
  182. package/test/workflow/resolvers.test.ts +357 -0
  183. package/test/workflow/webhook-dedupe.test.ts +295 -0
  184. package/test/workflow/webhook-signed.test.ts +509 -0
  185. package/test/workflow/webhook-trigger-test.test.ts +82 -0
  186. package/test/workflow/webhook-unsigned.test.ts +345 -0
  187. package/test/workflow-reactor-access.test.ts +384 -0
  188. package/test/workflow-reactor-enforcement.test.ts +808 -0
  189. package/test/workflow-read-gate.test.ts +213 -0
  190. package/test/workflow-runtime.test.ts +1122 -0
  191. package/test/workflows-plugin-config.test.ts +77 -0
  192. package/tsconfig.json +11 -2
  193. package/tsdown.config.ts +9 -1
  194. package/dist/server-CiKkVE6g.mjs.map +0 -1
  195. package/dist/utils-Baw7rThP.mjs.map +0 -1
@@ -0,0 +1,313 @@
1
+ import { createDurableNodeFs } from "@powerhousedao/reactor-api/pglite-node";
2
+ import type { ILogger } from "document-model";
3
+ import { promises as fs } from "node:fs";
4
+ import os from "node:os";
5
+ import path from "node:path";
6
+ import {
7
+ afterAll,
8
+ afterEach,
9
+ beforeAll,
10
+ beforeEach,
11
+ describe,
12
+ expect,
13
+ it,
14
+ vi,
15
+ } from "vitest";
16
+ import { runPglitePreflight } from "../src/pglite-preflight.js";
17
+ import { loadNodeFsClass, loadPGliteModule } from "../src/pglite-version.js";
18
+ import { writeSnapshotFromDir } from "./snapshot-writer.js";
19
+
20
+ // PGlite initdb and pg_dump are several times slower on the Windows runner.
21
+ const BOOT_TIMEOUT = 120_000;
22
+ const RM = { recursive: true, force: true, maxRetries: 10 } as const;
23
+ const ROWS = 10;
24
+ const CONVERTED_LINE =
25
+ /^Converted PGlite snapshot at (.+): snapshot (\d+) bytes -> (\d+) bytes on disk, (\d+) entries, (\d+) ms$/;
26
+
27
+ type StubLogger = ILogger & {
28
+ debug: ReturnType<typeof vi.fn>;
29
+ info: ReturnType<typeof vi.fn>;
30
+ warn: ReturnType<typeof vi.fn>;
31
+ };
32
+
33
+ function stubLogger(): StubLogger {
34
+ const logger = {
35
+ verbose: vi.fn(),
36
+ debug: vi.fn(),
37
+ info: vi.fn(),
38
+ warn: vi.fn(),
39
+ error: vi.fn(),
40
+ child: vi.fn(),
41
+ };
42
+ logger.child.mockReturnValue(logger);
43
+ return logger as unknown as StubLogger;
44
+ }
45
+
46
+ async function exists(p: string): Promise<boolean> {
47
+ try {
48
+ await fs.lstat(p);
49
+ return true;
50
+ } catch {
51
+ return false;
52
+ }
53
+ }
54
+
55
+ async function pgVersion(dir: string): Promise<string> {
56
+ return (await fs.readFile(path.join(dir, "PG_VERSION"), "utf8")).trim();
57
+ }
58
+
59
+ /** Opens `dir` the way the server does and counts the seeded rows. */
60
+ async function countRows(dir: string, major: 16 | 17): Promise<number> {
61
+ const [{ PGlite: Pg }, Base] = await Promise.all([
62
+ loadPGliteModule(major),
63
+ loadNodeFsClass(major),
64
+ ]);
65
+ const pg = new Pg({
66
+ fs: createDurableNodeFs(Base, dir, {
67
+ fsync: false,
68
+ maintenanceIntervalMs: 0,
69
+ }),
70
+ });
71
+ try {
72
+ await pg.waitReady;
73
+ const { rows } = await pg.query<{ n: number }>(
74
+ 'SELECT count(*)::int AS n FROM "Rows"',
75
+ );
76
+ return rows[0].n;
77
+ } finally {
78
+ await pg.close();
79
+ }
80
+ }
81
+
82
+ function convertedLines(logger: StubLogger): string[] {
83
+ return logger.info.mock.calls
84
+ .map((call) => String(call[0]))
85
+ .filter((line) => CONVERTED_LINE.test(line));
86
+ }
87
+
88
+ describe("runPglitePreflight", () => {
89
+ // A pure-snapshot PG16 store: snapshot.bin and nothing else, the layout
90
+ // AtomicNodeFs left for a store it created itself.
91
+ let template: string;
92
+ let root: string;
93
+ let logger: StubLogger;
94
+
95
+ beforeAll(async () => {
96
+ template = await fs.mkdtemp(path.join(os.tmpdir(), "preflight-template-"));
97
+ const loose = path.join(template, "loose");
98
+ const { PGlite: LegacyPGlite } = await loadPGliteModule(16);
99
+ const LegacyNodeFS = await loadNodeFsClass(16);
100
+ const pg = new LegacyPGlite({ fs: new LegacyNodeFS(loose) });
101
+ await pg.waitReady;
102
+ await pg.exec('CREATE TABLE "Rows" (id int PRIMARY KEY, payload text)');
103
+ await pg.exec(
104
+ `INSERT INTO "Rows" SELECT g, repeat('x', 100) FROM generate_series(1, ${ROWS}) g`,
105
+ );
106
+ await pg.close();
107
+ await fs.rm(path.join(loose, "postmaster.pid"), { force: true });
108
+ expect(await pgVersion(loose)).toBe("16");
109
+
110
+ const store = path.join(template, "store");
111
+ await fs.mkdir(store);
112
+ await writeSnapshotFromDir(loose, path.join(store, "snapshot.bin"));
113
+ await fs.rm(loose, RM);
114
+ }, BOOT_TIMEOUT);
115
+
116
+ afterAll(async () => {
117
+ await fs.rm(template, RM);
118
+ }, BOOT_TIMEOUT);
119
+
120
+ beforeEach(async () => {
121
+ root = await fs.mkdtemp(path.join(os.tmpdir(), "preflight-"));
122
+ logger = stubLogger();
123
+ });
124
+
125
+ afterEach(async () => {
126
+ await fs.rm(root, RM);
127
+ }, BOOT_TIMEOUT);
128
+
129
+ async function snapshotDir(name: string): Promise<string> {
130
+ const dir = path.join(root, name);
131
+ await fs.cp(path.join(template, "store"), dir, { recursive: true });
132
+ return dir;
133
+ }
134
+
135
+ async function looseDir(name: string, major = 17): Promise<string> {
136
+ const dir = path.join(root, name);
137
+ await fs.mkdir(path.join(dir, "pg_wal"), { recursive: true });
138
+ await fs.writeFile(path.join(dir, "PG_VERSION"), `${major}\n`);
139
+ return dir;
140
+ }
141
+
142
+ it(
143
+ "converts before detecting the major, so a PG16 snapshot is migrated",
144
+ async () => {
145
+ const dir = await snapshotDir("reactor-storage");
146
+
147
+ const detected = await runPglitePreflight({
148
+ dirs: [dir],
149
+ migratePglite: true,
150
+ inMemory: false,
151
+ logger,
152
+ });
153
+
154
+ expect(detected.get(dir)).toBe(17);
155
+ expect(await pgVersion(dir)).toBe("17");
156
+ expect(await exists(path.join(dir, "snapshot.bin"))).toBe(false);
157
+ expect(await exists(`${dir}.converting`)).toBe(false);
158
+ expect(await exists(`${dir}.old`)).toBe(false);
159
+ expect(convertedLines(logger)).toHaveLength(1);
160
+ expect(logger.info).toHaveBeenCalledWith(
161
+ expect.stringContaining("Migrating"),
162
+ );
163
+ expect(await countRows(dir, 17)).toBe(ROWS);
164
+ },
165
+ BOOT_TIMEOUT,
166
+ );
167
+
168
+ // Pins the order alone: before this change a pure-snapshot dir had no loose
169
+ // PG_VERSION and the migration logged "No PG_VERSION; skipping". The case
170
+ // above also needs the legacy pg_dump to work.
171
+ it(
172
+ "attempts the migration of a PG16 snapshot instead of skipping it",
173
+ async () => {
174
+ const dir = await snapshotDir("reactor-storage");
175
+
176
+ await runPglitePreflight({
177
+ dirs: [dir],
178
+ migratePglite: true,
179
+ inMemory: false,
180
+ logger,
181
+ }).catch(() => undefined);
182
+
183
+ expect(convertedLines(logger)).toHaveLength(1);
184
+ expect(logger.info).not.toHaveBeenCalledWith(
185
+ expect.stringContaining("No PG_VERSION"),
186
+ );
187
+ expect(logger.info).toHaveBeenCalledWith(
188
+ expect.stringContaining(`Migrating ${dir} from PG16 to PG17`),
189
+ );
190
+ },
191
+ BOOT_TIMEOUT,
192
+ );
193
+
194
+ it(
195
+ "boots a converted dir on the second start without converting",
196
+ async () => {
197
+ const dir = await snapshotDir("reactor-storage");
198
+ const first = { dirs: [dir], inMemory: false, logger };
199
+ await runPglitePreflight(first);
200
+ expect(convertedLines(logger)).toHaveLength(1);
201
+ expect(await pgVersion(dir)).toBe("16");
202
+
203
+ const second = stubLogger();
204
+ const detected = await runPglitePreflight({ ...first, logger: second });
205
+
206
+ expect(detected.get(dir)).toBe(16);
207
+ expect(convertedLines(second)).toHaveLength(0);
208
+ expect(await exists(`${dir}.converting`)).toBe(false);
209
+ expect(await exists(`${dir}.old`)).toBe(false);
210
+ expect(await countRows(dir, 16)).toBe(ROWS);
211
+ },
212
+ BOOT_TIMEOUT,
213
+ );
214
+
215
+ it("removes pg_wal/xlogtemp.*", async () => {
216
+ const dir = await looseDir("read-storage");
217
+ const segment = path.join(dir, "pg_wal", "000000010000000000000001");
218
+ await fs.writeFile(segment, "wal");
219
+ await fs.writeFile(path.join(dir, "pg_wal", "xlogtemp.42"), "");
220
+ await fs.writeFile(path.join(dir, "pg_wal", "xlogtemp.7"), "");
221
+
222
+ const detected = await runPglitePreflight({
223
+ dirs: [dir],
224
+ inMemory: false,
225
+ logger,
226
+ });
227
+
228
+ expect(detected.get(dir)).toBe(17);
229
+ expect(await fs.readdir(path.join(dir, "pg_wal"))).toEqual([
230
+ "000000010000000000000001",
231
+ ]);
232
+ expect(await exists(segment)).toBe(true);
233
+ });
234
+
235
+ it("removes postmaster.pid without a warn call", async () => {
236
+ const dir = await looseDir("read-storage");
237
+ const lockfile = path.join(dir, "postmaster.pid");
238
+ await fs.writeFile(lockfile, "42\n");
239
+
240
+ await runPglitePreflight({ dirs: [dir], inMemory: false, logger });
241
+
242
+ expect(await exists(lockfile)).toBe(false);
243
+ expect(logger.warn).not.toHaveBeenCalled();
244
+ expect(logger.debug).toHaveBeenCalledWith(
245
+ expect.stringContaining(lockfile),
246
+ );
247
+ });
248
+
249
+ it(
250
+ "logs one info line per converted dir with sizes and duration",
251
+ async () => {
252
+ const reactor = await snapshotDir("reactor-storage");
253
+ const readModel = await snapshotDir("read-storage");
254
+
255
+ await runPglitePreflight({
256
+ dirs: [reactor, readModel],
257
+ inMemory: false,
258
+ logger,
259
+ });
260
+
261
+ const lines = convertedLines(logger);
262
+ expect(lines).toHaveLength(2);
263
+ const byDir = new Map(
264
+ lines.map((line) => {
265
+ const match = CONVERTED_LINE.exec(line)!;
266
+ return [match[1], match.slice(2).map(Number)] as const;
267
+ }),
268
+ );
269
+ for (const dir of [reactor, readModel]) {
270
+ const [snapshotBytes, convertedBytes, entries] = byDir.get(dir)!;
271
+ expect(snapshotBytes).toBeGreaterThan(0);
272
+ expect(convertedBytes).toBeGreaterThan(0);
273
+ expect(entries).toBeGreaterThan(0);
274
+ }
275
+ },
276
+ BOOT_TIMEOUT,
277
+ );
278
+
279
+ it("touches nothing in memory", async () => {
280
+ const dir = await snapshotDir("reactor-storage");
281
+
282
+ const detected = await runPglitePreflight({
283
+ dirs: [dir],
284
+ inMemory: true,
285
+ logger,
286
+ });
287
+
288
+ expect(detected.size).toBe(0);
289
+ expect(await exists(path.join(dir, "snapshot.bin"))).toBe(true);
290
+ });
291
+
292
+ it("wipes the dirs and their conversion siblings under forcePgVersion", async () => {
293
+ const dir = await snapshotDir("reactor-storage");
294
+ await fs.mkdir(`${dir}.old`);
295
+ await fs.mkdir(`${dir}.converting`);
296
+
297
+ const detected = await runPglitePreflight({
298
+ dirs: [dir],
299
+ forcePgVersion: 17,
300
+ migratePglite: true,
301
+ inMemory: false,
302
+ logger,
303
+ });
304
+
305
+ expect(detected.size).toBe(0);
306
+ expect(await exists(dir)).toBe(false);
307
+ expect(await exists(`${dir}.old`)).toBe(false);
308
+ expect(await exists(`${dir}.converting`)).toBe(false);
309
+ expect(logger.warn).toHaveBeenCalledWith(
310
+ expect.stringContaining("ignoring --migrate-pglite"),
311
+ );
312
+ });
313
+ });
@@ -0,0 +1,103 @@
1
+ import path from "node:path";
2
+ import { describe, expect, it } from "vitest";
3
+ import { resolvePgliteStorageDirs } from "../src/server.mjs";
4
+
5
+ /** The classic layout, and the one-dir report (uniform-runners legacy parity). */
6
+ describe("resolvePgliteStorageDirs", () => {
7
+ const phDir = path.join("/proj", ".ph");
8
+
9
+ it("keeps the classic literals verbatim without a user-data root", () => {
10
+ const saved = process.env.PH_USER_DATA_DIR;
11
+ delete process.env.PH_USER_DATA_DIR;
12
+ try {
13
+ const dirs = resolvePgliteStorageDirs({
14
+ dbPath: undefined,
15
+ reactorDbUrl: undefined,
16
+ callerReactor: false,
17
+ });
18
+ expect(dirs.reactorPgliteDir).toBe("./.ph/reactor-storage");
19
+ expect(dirs.readModelPgliteDir).toBe(".ph/read-storage");
20
+ expect(dirs.sharedPgliteDir).toBeNull();
21
+ } finally {
22
+ if (saved !== undefined) process.env.PH_USER_DATA_DIR = saved;
23
+ }
24
+ });
25
+
26
+ it("defaults to the classic split under the .ph tree", () => {
27
+ const dirs = resolvePgliteStorageDirs({
28
+ dbPath: undefined,
29
+ reactorDbUrl: undefined,
30
+ callerReactor: false,
31
+ phDir,
32
+ });
33
+ expect(dirs.reactorPgliteDir).toBe(path.join(phDir, "reactor-storage"));
34
+ expect(dirs.readModelPgliteDir).toBe(path.join(phDir, "read-storage"));
35
+ expect(dirs.pgliteDirs).toEqual([
36
+ path.join(phDir, "reactor-storage"),
37
+ path.join(phDir, "read-storage"),
38
+ ]);
39
+ });
40
+
41
+ it("reports — but keeps — the classic dbPath-only layout: both stores in one dir", () => {
42
+ const store = path.join("/proj", ".ph", "read-model.db");
43
+ const dirs = resolvePgliteStorageDirs({
44
+ dbPath: store,
45
+ reactorDbUrl: store,
46
+ callerReactor: false,
47
+ phDir,
48
+ });
49
+ expect(dirs.reactorPgliteDir).toBe(store);
50
+ expect(dirs.readModelPgliteDir).toBe(store);
51
+ // pre-flight list unchanged from the classic code (not deduplicated)
52
+ expect(dirs.pgliteDirs).toEqual([store, store]);
53
+ expect(dirs.sharedPgliteDir).toBe(path.resolve(store));
54
+ });
55
+
56
+ it("detects the same dir spelled differently", () => {
57
+ const dirs = resolvePgliteStorageDirs({
58
+ dbPath: "/proj/.ph/store/",
59
+ reactorDbUrl: "/proj/.ph/../.ph/store",
60
+ callerReactor: false,
61
+ phDir,
62
+ });
63
+ expect(dirs.sharedPgliteDir).toBe(path.resolve("/proj/.ph/store"));
64
+ });
65
+
66
+ it("allows one Postgres database for both stores", () => {
67
+ const dirs = resolvePgliteStorageDirs({
68
+ dbPath: "postgres://db/ph",
69
+ reactorDbUrl: "postgres://db/ph",
70
+ callerReactor: false,
71
+ phDir,
72
+ });
73
+ expect(dirs.reactorPgliteDir).toBeNull();
74
+ expect(dirs.readModelPgliteDir).toBeNull();
75
+ expect(dirs.pgliteDirs).toEqual([]);
76
+ expect(dirs.sharedPgliteDir).toBeNull();
77
+ });
78
+
79
+ it("allows dbPath to name the read model only when the caller owns the reactor", () => {
80
+ const store = path.join("/proj", ".ph", "read-model.db");
81
+ const dirs = resolvePgliteStorageDirs({
82
+ dbPath: store,
83
+ reactorDbUrl: store,
84
+ callerReactor: true,
85
+ phDir,
86
+ });
87
+ expect(dirs.reactorPgliteDir).toBeNull();
88
+ expect(dirs.readModelPgliteDir).toBe(store);
89
+ expect(dirs.pgliteDirs).toEqual([store]);
90
+ expect(dirs.sharedPgliteDir).toBeNull();
91
+ });
92
+
93
+ it("keeps a distinct reactor dir from PH_REACTOR_DATABASE_URL beside a dbPath", () => {
94
+ const dirs = resolvePgliteStorageDirs({
95
+ dbPath: path.join(phDir, "read-storage"),
96
+ reactorDbUrl: path.join(phDir, "reactor-storage"),
97
+ callerReactor: false,
98
+ phDir,
99
+ });
100
+ expect(dirs.pgliteDirs).toHaveLength(2);
101
+ expect(dirs.sharedPgliteDir).toBeNull();
102
+ });
103
+ });
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from "vitest";
2
- import { parseForcePgVersion } from "../src/pglite-version.js";
2
+ import { openForVerify, parseForcePgVersion } from "../src/pglite-version.js";
3
3
 
4
4
  describe("parseForcePgVersion", () => {
5
5
  it("returns null for undefined", () => {
@@ -35,3 +35,11 @@ describe("parseForcePgVersion", () => {
35
35
  expect(() => parseForcePgVersion("16.5")).toThrow(/PH_FORCE_PG_VERSION/);
36
36
  });
37
37
  });
38
+
39
+ describe("openForVerify", () => {
40
+ it("throws on an unsupported major before touching disk", async () => {
41
+ await expect(openForVerify(15, "/nonexistent/pglite-dir")).rejects.toThrow(
42
+ /PG_VERSION=15 is not supported/,
43
+ );
44
+ });
45
+ });
@@ -0,0 +1,83 @@
1
+ import type { PluginEntry } from "@powerhousedao/host-core";
2
+ import { describe, expect, it } from "vitest";
3
+ import { attachmentsPlugin } from "../src/plugins/attachments.mjs";
4
+ import { mcpPlugin } from "../src/plugins/mcp.mjs";
5
+ import { privacyPlugin } from "../src/plugins/privacy.mjs";
6
+ import { workflowsPlugin } from "../src/plugins/workflows.mjs";
7
+
8
+ // typed-host-configs 1.4 / T2 lane C: every built-in switchboard plugin
9
+ // declares a zod configSchema, and its module augments PHPluginConfigs so a
10
+ // host.js entry's config is typed by specifier.
11
+
12
+ /** The typed object form of a known specifier's entry. (The bare-string form
13
+ * and the untyped `{ module: string }` fallback do not narrow by specifier.) */
14
+ type EntryFor<M extends string> = Extract<PluginEntry, { module: M }>;
15
+
16
+ describe("switchboard plugin configSchemas", () => {
17
+ it.each([
18
+ ["mcp", mcpPlugin],
19
+ ["attachments", attachmentsPlugin],
20
+ ["privacy", privacyPlugin],
21
+ ])("%s takes no config: {} passes, any key is a typo", (_, plugin) => {
22
+ expect(plugin.configSchema?.safeParse({}).success).toBe(true);
23
+ expect(plugin.configSchema?.safeParse({ enabled: true }).success).toBe(
24
+ false,
25
+ );
26
+ });
27
+
28
+ it("workflows rejects a typo'd key and accepts drive: false", () => {
29
+ expect(
30
+ workflowsPlugin.configSchema?.safeParse({ drive: false }).success,
31
+ ).toBe(true);
32
+ expect(
33
+ workflowsPlugin.configSchema?.safeParse({ drives: false }).success,
34
+ ).toBe(false);
35
+ });
36
+ });
37
+
38
+ describe("PHPluginConfigs augmentation (type level)", () => {
39
+ it("types the switchboard workflows entry: drive is { slug, name, … } | false", () => {
40
+ const entries: PluginEntry[] = [
41
+ "@powerhousedao/switchboard/plugins/mcp",
42
+ {
43
+ module: "@powerhousedao/switchboard/plugins/workflows",
44
+ config: { drive: { slug: "workflows", name: "Workflows" } },
45
+ },
46
+ {
47
+ module: "@powerhousedao/switchboard/plugins/workflows",
48
+ config: { drive: false },
49
+ },
50
+ ];
51
+ // Positive cases pin that the entry type exists (not `never`), so the
52
+ // expect-errors below fail for the right reason.
53
+ const declared: EntryFor<"@powerhousedao/switchboard/plugins/workflows"> = {
54
+ module: "@powerhousedao/switchboard/plugins/workflows",
55
+ config: { drive: { slug: "flows", name: "Flows" } },
56
+ };
57
+ const off: PHPluginConfigs["@powerhousedao/switchboard/plugins/workflows"] =
58
+ { drive: false };
59
+ const urlDrive: EntryFor<"@powerhousedao/switchboard/plugins/workflows"> = {
60
+ module: "@powerhousedao/switchboard/plugins/workflows",
61
+ // @ts-expect-error -- the switchboard workflows drive is a declaration, not a URL (that is Connect's)
62
+ config: { drive: "http://localhost:4001/d/workflows" },
63
+ };
64
+ const typo: EntryFor<"@powerhousedao/switchboard/plugins/workflows"> = {
65
+ module: "@powerhousedao/switchboard/plugins/workflows",
66
+ // @ts-expect-error -- `drives` is not a workflows config key
67
+ config: { drives: false },
68
+ };
69
+ expect([entries, declared, off, urlDrive, typo]).toBeDefined();
70
+ });
71
+
72
+ it("types the no-config plugins as taking no keys", () => {
73
+ const mcp: PHPluginConfigs["@powerhousedao/switchboard/plugins/mcp"] = {
74
+ // @ts-expect-error -- mcp takes no config
75
+ enabled: true,
76
+ };
77
+ const none: PHPluginConfigs["@powerhousedao/switchboard/plugins/privacy"] =
78
+ {};
79
+ const attachments: EntryFor<"@powerhousedao/switchboard/plugins/attachments"> =
80
+ { module: "@powerhousedao/switchboard/plugins/attachments", config: {} };
81
+ expect([mcp, none, attachments]).toBeDefined();
82
+ });
83
+ });