@powerhousedao/switchboard 6.2.3-dev.8 → 6.2.3-dev.81

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 (272) 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-auth-config.test.d.ts +2 -0
  10. package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
  11. package/.tsbuild/test/boot-create-signature-policy.test.d.ts +2 -0
  12. package/.tsbuild/test/boot-create-signature-policy.test.d.ts.map +1 -0
  13. package/.tsbuild/test/boot-env-isolation.test.d.ts +2 -0
  14. package/.tsbuild/test/boot-env-isolation.test.d.ts.map +1 -0
  15. package/.tsbuild/test/boot-helpers.d.ts +5 -0
  16. package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
  17. package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
  18. package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
  19. package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
  20. package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
  21. package/.tsbuild/test/boot-mcp-enabled.test.d.ts +2 -0
  22. package/.tsbuild/test/boot-mcp-enabled.test.d.ts.map +1 -0
  23. package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
  24. package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
  25. package/.tsbuild/test/boot-preconditions.test.d.ts +2 -0
  26. package/.tsbuild/test/boot-preconditions.test.d.ts.map +1 -0
  27. package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
  28. package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
  29. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts +2 -0
  30. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts.map +1 -0
  31. package/.tsbuild/test/boot-without-renown.test.d.ts +2 -0
  32. package/.tsbuild/test/boot-without-renown.test.d.ts.map +1 -0
  33. package/.tsbuild/test/boot-without-require-signatures.test.d.ts +2 -0
  34. package/.tsbuild/test/boot-without-require-signatures.test.d.ts.map +1 -0
  35. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts +2 -0
  36. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts.map +1 -0
  37. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
  38. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
  39. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
  40. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
  41. package/.tsbuild/test/builder-defaults.test.d.ts +2 -0
  42. package/.tsbuild/test/builder-defaults.test.d.ts.map +1 -0
  43. package/.tsbuild/test/config-from-env.test.d.ts +2 -0
  44. package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
  45. package/.tsbuild/test/config-merge.test.d.ts +2 -0
  46. package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
  47. package/.tsbuild/test/config-schema.test.d.ts +2 -0
  48. package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
  49. package/.tsbuild/test/create-signature-policy.test.d.ts +2 -0
  50. package/.tsbuild/test/create-signature-policy.test.d.ts.map +1 -0
  51. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
  52. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
  53. package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
  54. package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
  55. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
  56. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
  57. package/.tsbuild/test/fatal-shutdown.test.d.ts +2 -0
  58. package/.tsbuild/test/fatal-shutdown.test.d.ts.map +1 -0
  59. package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
  60. package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
  61. package/.tsbuild/test/observability-signals.test.d.ts +2 -0
  62. package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
  63. package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
  64. package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
  65. package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
  66. package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
  67. package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
  68. package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
  69. package/.tsbuild/test/pglite-store.test.d.ts +2 -0
  70. package/.tsbuild/test/pglite-store.test.d.ts.map +1 -0
  71. package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
  72. package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
  73. package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
  74. package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
  75. package/.tsbuild/test/port-probe.test.d.ts +2 -0
  76. package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
  77. package/.tsbuild/test/renown-storage-path.test.d.ts +2 -0
  78. package/.tsbuild/test/renown-storage-path.test.d.ts.map +1 -0
  79. package/.tsbuild/test/renown-trust-policy.test.d.ts +2 -0
  80. package/.tsbuild/test/renown-trust-policy.test.d.ts.map +1 -0
  81. package/.tsbuild/test/served-schemas.test.d.ts +2 -0
  82. package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
  83. package/.tsbuild/test/shutdown.test.d.ts +2 -0
  84. package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
  85. package/.tsbuild/test/snapshot-writer.d.ts +18 -0
  86. package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
  87. package/.tsbuild/test/store-lock.test.d.ts +2 -0
  88. package/.tsbuild/test/store-lock.test.d.ts.map +1 -0
  89. package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
  90. package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
  91. package/.tsbuild/test/unsupported-stored-documents.test.d.ts +2 -0
  92. package/.tsbuild/test/unsupported-stored-documents.test.d.ts.map +1 -0
  93. package/.tsbuild/test/workflow/harness.d.ts +56 -0
  94. package/.tsbuild/test/workflow/harness.d.ts.map +1 -0
  95. package/.tsbuild/test/workflow/oauth.test.d.ts +2 -0
  96. package/.tsbuild/test/workflow/oauth.test.d.ts.map +1 -0
  97. package/.tsbuild/test/workflow/resolvers.test.d.ts +2 -0
  98. package/.tsbuild/test/workflow/resolvers.test.d.ts.map +1 -0
  99. package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
  100. package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
  101. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts +2 -0
  102. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts.map +1 -0
  103. package/.tsbuild/test/workflow/webhook-signed.test.d.ts +2 -0
  104. package/.tsbuild/test/workflow/webhook-signed.test.d.ts.map +1 -0
  105. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts +2 -0
  106. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts.map +1 -0
  107. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts +2 -0
  108. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts.map +1 -0
  109. package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
  110. package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
  111. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
  112. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
  113. package/.tsbuild/test/workflow-read-gate.test.d.ts +2 -0
  114. package/.tsbuild/test/workflow-read-gate.test.d.ts.map +1 -0
  115. package/.tsbuild/test/workflow-runtime.test.d.ts +2 -0
  116. package/.tsbuild/test/workflow-runtime.test.d.ts.map +1 -0
  117. package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
  118. package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
  119. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  120. package/.tsbuild/tsdown.config.d.ts.map +1 -1
  121. package/.tsbuild/vitest.config.d.ts.map +1 -1
  122. package/Auth.md +144 -2
  123. package/CHANGELOG.md +1074 -0
  124. package/README.md +55 -14
  125. package/dist/boot.d.mts +46 -0
  126. package/dist/boot.d.mts.map +1 -0
  127. package/dist/boot.mjs +67 -0
  128. package/dist/boot.mjs.map +1 -0
  129. package/dist/config/from-env.d.mts +15 -0
  130. package/dist/config/from-env.d.mts.map +1 -0
  131. package/dist/config/from-env.mjs +4 -0
  132. package/dist/config/schema.d.mts +3 -0
  133. package/dist/config/schema.mjs +4 -0
  134. package/dist/from-env-BO0n9R02.mjs +401 -0
  135. package/dist/from-env-BO0n9R02.mjs.map +1 -0
  136. package/dist/index.d.mts +2 -1
  137. package/dist/index.mjs +76 -79
  138. package/dist/index.mjs.map +1 -1
  139. package/dist/install-packages.d.mts +2 -1
  140. package/dist/install-packages.mjs +2 -2
  141. package/dist/install-packages.mjs.map +1 -1
  142. package/dist/mcp-SsBK14Jm.mjs +32 -0
  143. package/dist/mcp-SsBK14Jm.mjs.map +1 -0
  144. package/dist/migrate.d.mts +2 -1
  145. package/dist/migrate.mjs +7 -5
  146. package/dist/migrate.mjs.map +1 -1
  147. package/dist/plugin-host-C1sYEm8g.d.mts +46 -0
  148. package/dist/plugin-host-C1sYEm8g.d.mts.map +1 -0
  149. package/dist/plugins/attachments.d.mts +33 -0
  150. package/dist/plugins/attachments.d.mts.map +1 -0
  151. package/dist/plugins/attachments.mjs +4 -0
  152. package/dist/plugins/mcp.d.mts +31 -0
  153. package/dist/plugins/mcp.d.mts.map +1 -0
  154. package/dist/plugins/mcp.mjs +4 -0
  155. package/dist/plugins/privacy.d.mts +22 -0
  156. package/dist/plugins/privacy.d.mts.map +1 -0
  157. package/dist/plugins/privacy.mjs +4 -0
  158. package/dist/plugins/workflows.d.mts +79 -0
  159. package/dist/plugins/workflows.d.mts.map +1 -0
  160. package/dist/plugins/workflows.mjs +4 -0
  161. package/dist/postgres-url-DTDhBjXX.mjs +11 -0
  162. package/dist/postgres-url-DTDhBjXX.mjs.map +1 -0
  163. package/dist/privacy-BO8Q25xI.mjs +44 -0
  164. package/dist/privacy-BO8Q25xI.mjs.map +1 -0
  165. package/dist/privacy-DxIJGH4P.mjs +91 -0
  166. package/dist/privacy-DxIJGH4P.mjs.map +1 -0
  167. package/dist/renown-layers-COZsrGQT.mjs +40 -0
  168. package/dist/renown-layers-COZsrGQT.mjs.map +1 -0
  169. package/dist/schema-0g_QdOWn.mjs +267 -0
  170. package/dist/schema-0g_QdOWn.mjs.map +1 -0
  171. package/dist/schema-ivUWPQOn.d.mts +202 -0
  172. package/dist/schema-ivUWPQOn.d.mts.map +1 -0
  173. package/dist/server-cE1BWLQu.mjs +2518 -0
  174. package/dist/server-cE1BWLQu.mjs.map +1 -0
  175. package/dist/server.d.mts +270 -48
  176. package/dist/server.d.mts.map +1 -1
  177. package/dist/server.mjs +5 -5
  178. package/dist/utils-C6AIF38Z.mjs +133 -0
  179. package/dist/utils-C6AIF38Z.mjs.map +1 -0
  180. package/dist/utils.d.mts +12 -5
  181. package/dist/utils.d.mts.map +1 -1
  182. package/dist/utils.mjs +4 -3
  183. package/dist/worker-pool-BWHBLBJd.mjs +304 -0
  184. package/dist/worker-pool-BWHBLBJd.mjs.map +1 -0
  185. package/dist/workflow-runtime-7GaJ13c9.d.mts +30 -0
  186. package/dist/workflow-runtime-7GaJ13c9.d.mts.map +1 -0
  187. package/dist/workflows-COM3MKfD.mjs +1437 -0
  188. package/dist/workflows-COM3MKfD.mjs.map +1 -0
  189. package/package.json +68 -23
  190. package/scripts/install-packages.sh +0 -0
  191. package/test/attachment-reference-read-model.test.ts +28 -20
  192. package/test/attachments/auth.test.ts +85 -0
  193. package/test/attachments/byte-grants.test.ts +290 -0
  194. package/test/attachments/byte-routes-read-gate.integration.test.ts +314 -0
  195. package/test/attachments/download-target.test.ts +211 -32
  196. package/test/attachments/index.test.ts +47 -4
  197. package/test/attachments/reserve-metadata-parity.test.ts +96 -0
  198. package/test/attachments/routes-integration.test.ts +60 -4
  199. package/test/attachments/routes.test.ts +297 -35
  200. package/test/attachments/service-config.test.ts +41 -19
  201. package/test/attachments/url-signer.test.ts +105 -0
  202. package/test/boot-auth-config.test.ts +109 -0
  203. package/test/boot-create-signature-policy.test.ts +110 -0
  204. package/test/boot-env-isolation.test.ts +26 -0
  205. package/test/boot-helpers.ts +19 -0
  206. package/test/boot-inspection-enabled.test.ts +140 -0
  207. package/test/boot-mcp-auth.test.ts +449 -0
  208. package/test/boot-mcp-enabled.test.ts +119 -0
  209. package/test/boot-port-zero.test.ts +101 -0
  210. package/test/boot-preconditions.test.ts +200 -0
  211. package/test/boot-privacy.test.ts +232 -0
  212. package/test/boot-unsupported-stored-documents.test.ts +214 -0
  213. package/test/boot-without-renown.test.ts +83 -0
  214. package/test/boot-without-require-signatures.test.ts +76 -0
  215. package/test/boot-worker-trust-policy.test.ts +91 -0
  216. package/test/boot-workflow-singleton-conflict.test.ts +109 -0
  217. package/test/boot-workflow-singleton-retry.test.ts +122 -0
  218. package/test/builder-defaults.test.ts +80 -0
  219. package/test/config-from-env.test.ts +758 -0
  220. package/test/config-merge.test.ts +215 -0
  221. package/test/config-schema.test.ts +128 -0
  222. package/test/create-signature-policy.test.ts +69 -0
  223. package/test/default-drive-protocol-versions.test.ts +102 -0
  224. package/test/default-drive-slug.test.ts +97 -0
  225. package/test/durable-node-fs-pg16.test.ts +80 -0
  226. package/test/fatal-shutdown.test.ts +165 -0
  227. package/test/goldens/packages.graphql +797 -0
  228. package/test/goldens/privacy.graphql +853 -0
  229. package/test/goldens/reactor-drive-nodes.graphql +827 -0
  230. package/test/goldens/resolvers.json +322 -0
  231. package/test/goldens/supergraph.graphql +5652 -0
  232. package/test/goldens/workflow-runtime.graphql +1419 -0
  233. package/test/inspection-throttle.test.ts +23 -0
  234. package/test/observability-signals.test.ts +131 -0
  235. package/test/permissions-backfill.test.ts +486 -0
  236. package/test/pglite-dialect.test.ts +237 -2
  237. package/test/pglite-preflight.test.ts +313 -0
  238. package/test/pglite-storage-dirs.test.ts +113 -0
  239. package/test/pglite-store.test.ts +264 -0
  240. package/test/pglite-version.test.ts +9 -1
  241. package/test/plugin-config-schemas.test.ts +83 -0
  242. package/test/plugin-presence.test.ts +262 -0
  243. package/test/port-probe.test.ts +45 -0
  244. package/test/renown-storage-path.test.ts +40 -0
  245. package/test/renown-trust-policy.test.ts +313 -0
  246. package/test/served-schemas.test.ts +134 -0
  247. package/test/shutdown.test.ts +278 -0
  248. package/test/snapshot-writer.ts +94 -0
  249. package/test/store-lock.test.ts +65 -0
  250. package/test/two-switchboards.test.ts +146 -0
  251. package/test/unsupported-stored-documents.test.ts +72 -0
  252. package/test/worker-pool.test.ts +27 -1
  253. package/test/workflow/harness.ts +289 -0
  254. package/test/workflow/oauth.test.ts +205 -0
  255. package/test/workflow/resolvers.test.ts +369 -0
  256. package/test/workflow/runtime-host.test.ts +19 -0
  257. package/test/workflow/webhook-dedupe.test.ts +295 -0
  258. package/test/workflow/webhook-signed.test.ts +509 -0
  259. package/test/workflow/webhook-trigger-test.test.ts +82 -0
  260. package/test/workflow/webhook-unsigned.test.ts +345 -0
  261. package/test/workflow-reactor-access.test.ts +384 -0
  262. package/test/workflow-reactor-enforcement.test.ts +808 -0
  263. package/test/workflow-read-gate.test.ts +213 -0
  264. package/test/workflow-runtime.test.ts +1135 -0
  265. package/test/workflows-plugin-config.test.ts +77 -0
  266. package/tsconfig.json +11 -2
  267. package/tsdown.config.ts +12 -1
  268. package/vitest.config.ts +65 -0
  269. package/dist/server-CiKkVE6g.mjs +0 -1555
  270. package/dist/server-CiKkVE6g.mjs.map +0 -1
  271. package/dist/utils-Baw7rThP.mjs +0 -80
  272. package/dist/utils-Baw7rThP.mjs.map +0 -1
@@ -0,0 +1,200 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ reactorDbUrlOf,
4
+ switchboardBootFactsFromEnv,
5
+ switchboardBootFactsOf,
6
+ switchboardBootIssues,
7
+ type SwitchboardBootFacts,
8
+ } from "../src/boot.mjs";
9
+
10
+ const POSTGRES = "postgres://u:p@localhost:5432/reactor";
11
+
12
+ const clean: SwitchboardBootFacts = {
13
+ dev: false,
14
+ workers: 0,
15
+ projectionWorker: false,
16
+ reactorDbUrl: undefined,
17
+ authEnforcement: false,
18
+ renownSource: "remote",
19
+ };
20
+
21
+ describe("switchboardBootIssues", () => {
22
+ it("reports nothing with the pools off", () => {
23
+ expect(switchboardBootIssues({ ...clean, dev: true })).toEqual([]);
24
+ });
25
+
26
+ it("reports nothing for workers on Postgres", () => {
27
+ expect(
28
+ switchboardBootIssues({ ...clean, workers: 2, reactorDbUrl: POSTGRES }),
29
+ ).toEqual([]);
30
+ });
31
+
32
+ it("reports every worker-pool issue, in the server's order", () => {
33
+ const issues = switchboardBootIssues({
34
+ ...clean,
35
+ dev: true,
36
+ workers: 2,
37
+ authEnforcement: true,
38
+ renownSource: "self",
39
+ });
40
+ expect(issues).toHaveLength(3);
41
+ expect(issues[0]).toMatch(/REACTOR_WORKERS\) is not supported in dev mode/);
42
+ expect(issues[1]).toMatch(/cannot verify signers/);
43
+ expect(issues[2]).toMatch(
44
+ /REACTOR_WORKERS\) requires a Postgres reactor database/,
45
+ );
46
+ });
47
+
48
+ it("treats a PGlite path as no Postgres", () => {
49
+ expect(
50
+ switchboardBootIssues({
51
+ ...clean,
52
+ workers: 1,
53
+ reactorDbUrl: "./.ph/reactor-storage",
54
+ }),
55
+ ).toEqual([expect.stringMatching(/requires a Postgres reactor database/)]);
56
+ });
57
+
58
+ it("reports the projection worker's issues when it is enabled", () => {
59
+ expect(
60
+ switchboardBootIssues({ ...clean, dev: true, projectionWorker: true }),
61
+ ).toEqual([
62
+ expect.stringMatching(/REACTOR_PROJECTION_WORKER\) is not supported/),
63
+ expect.stringMatching(
64
+ /REACTOR_PROJECTION_WORKER\) requires a Postgres reactor database/,
65
+ ),
66
+ ]);
67
+ });
68
+ });
69
+
70
+ describe("switchboardBootFactsOf", () => {
71
+ it("reads the facts from a config", async () => {
72
+ expect(
73
+ await switchboardBootFactsOf(
74
+ {
75
+ storage: {
76
+ reactor: { url: POSTGRES },
77
+ readModel: { url: "./read" },
78
+ },
79
+ workers: { executor: { count: 3 }, projection: { enabled: true } },
80
+ protocol: { featureFlags: { authEnforcement: true } },
81
+ auth: { renown: { source: "self" } },
82
+ },
83
+ { dev: false },
84
+ ),
85
+ ).toEqual({
86
+ dev: false,
87
+ workers: 3,
88
+ projectionWorker: true,
89
+ reactorDbUrl: POSTGRES,
90
+ authEnforcement: true,
91
+ renownSource: "self",
92
+ });
93
+ });
94
+
95
+ it("never takes the reactor database from the read model", async () => {
96
+ const facts = await switchboardBootFactsOf(
97
+ { storage: { readModel: { url: POSTGRES } } },
98
+ { dev: false },
99
+ );
100
+ expect(facts.reactorDbUrl).toBeUndefined();
101
+ });
102
+
103
+ it("lets the config's renown source win over the configured one", async () => {
104
+ const configuredRenown = { source: "self" as const };
105
+ expect(
106
+ (await switchboardBootFactsOf({}, { dev: false, configuredRenown }))
107
+ .renownSource,
108
+ ).toBe("self");
109
+ expect(
110
+ (
111
+ await switchboardBootFactsOf(
112
+ { auth: { renown: { source: "remote" } } },
113
+ { dev: false, configuredRenown },
114
+ )
115
+ ).renownSource,
116
+ ).toBe("remote");
117
+ });
118
+ });
119
+
120
+ describe("switchboardBootFactsFromEnv", () => {
121
+ it("reads the facts with the server's parsers", async () => {
122
+ const { facts, issues } = await switchboardBootFactsFromEnv(
123
+ {
124
+ REACTOR_WORKERS: "3",
125
+ REACTOR_PROJECTION_WORKER: "on",
126
+ REACTOR_AUTH_ENFORCEMENT: "true",
127
+ RENOWN_SOURCE: "self",
128
+ PH_SWITCHBOARD_DATABASE_URL: POSTGRES,
129
+ },
130
+ { dev: false },
131
+ );
132
+ expect(issues).toEqual([]);
133
+ expect(facts).toEqual({
134
+ dev: false,
135
+ workers: 3,
136
+ projectionWorker: true,
137
+ reactorDbUrl: POSTGRES,
138
+ authEnforcement: true,
139
+ renownSource: "self",
140
+ });
141
+ });
142
+
143
+ it("prefers dbPath, then PH_REACTOR_DATABASE_URL", async () => {
144
+ const env = {
145
+ PH_REACTOR_DATABASE_URL: "./reactor",
146
+ PH_SWITCHBOARD_DATABASE_URL: POSTGRES,
147
+ };
148
+ expect(
149
+ (await switchboardBootFactsFromEnv(env, { dev: false })).facts
150
+ .reactorDbUrl,
151
+ ).toBe("./reactor");
152
+ expect(
153
+ (await switchboardBootFactsFromEnv(env, { dev: false, dbPath: POSTGRES }))
154
+ .facts.reactorDbUrl,
155
+ ).toBe(POSTGRES);
156
+ });
157
+
158
+ it("lets RENOWN_SOURCE win over the configured source", async () => {
159
+ const configuredRenown = { source: "self" as const };
160
+ expect(
161
+ (await switchboardBootFactsFromEnv({}, { dev: false, configuredRenown }))
162
+ .facts.renownSource,
163
+ ).toBe("self");
164
+ expect(
165
+ (
166
+ await switchboardBootFactsFromEnv(
167
+ { RENOWN_SOURCE: "remote" },
168
+ { dev: false, configuredRenown },
169
+ )
170
+ ).facts.renownSource,
171
+ ).toBe("remote");
172
+ });
173
+
174
+ it("turns parser errors into issues", async () => {
175
+ const { facts, issues } = await switchboardBootFactsFromEnv(
176
+ { REACTOR_WORKERS: "lots", REACTOR_PROJECTION_WORKER: "maybe" },
177
+ { dev: false },
178
+ );
179
+ expect(issues).toEqual([
180
+ expect.stringMatching(/REACTOR_WORKERS/),
181
+ expect.stringMatching(/REACTOR_PROJECTION_WORKER must be one of/),
182
+ ]);
183
+ expect(facts.workers).toBe(0);
184
+ expect(facts.projectionWorker).toBe(false);
185
+ });
186
+ });
187
+
188
+ describe("reactorDbUrlOf", () => {
189
+ it("prefers dbPath, then PH_REACTOR_DATABASE_URL, then PH_SWITCHBOARD_DATABASE_URL", () => {
190
+ const env = {
191
+ PH_REACTOR_DATABASE_URL: "./r",
192
+ PH_SWITCHBOARD_DATABASE_URL: POSTGRES,
193
+ };
194
+ expect(reactorDbUrlOf("./d", env)).toBe("./d");
195
+ expect(reactorDbUrlOf(undefined, env)).toBe("./r");
196
+ expect(
197
+ reactorDbUrlOf(undefined, { PH_SWITCHBOARD_DATABASE_URL: POSTGRES }),
198
+ ).toBe(POSTGRES);
199
+ });
200
+ });
@@ -0,0 +1,232 @@
1
+ import { mkdtemp, rm } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import type { ILogger } from "document-model";
5
+ import pg from "pg";
6
+ import { describe, expect, it, vi } from "vitest";
7
+ import type { startSwitchboard } from "../src/server.mjs";
8
+ import { bootSwitchboard } from "./boot-helpers.js";
9
+
10
+ const ADMIN = "0xadad00000000000000000000000000000000adad";
11
+ const SECRET = "switchboard-privacy-test-deployment-secret-0123";
12
+
13
+ function stubLogger(): ILogger & { error: ReturnType<typeof vi.fn> } {
14
+ const logger = {
15
+ verbose: vi.fn(),
16
+ debug: vi.fn(),
17
+ info: vi.fn(),
18
+ warn: vi.fn(),
19
+ error: vi.fn(),
20
+ child: vi.fn(),
21
+ };
22
+ logger.child.mockReturnValue(logger);
23
+ return logger as unknown as ILogger & { error: ReturnType<typeof vi.fn> };
24
+ }
25
+
26
+ type Switchboard = Awaited<ReturnType<typeof startSwitchboard>>;
27
+
28
+ async function withEnv<T>(
29
+ env: Record<string, string>,
30
+ run: (tempRoot: string) => Promise<T>,
31
+ ): Promise<T> {
32
+ const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-privacy-"));
33
+ const names = [
34
+ "PH_REACTOR_DATABASE_URL",
35
+ "AUTH_ENABLED",
36
+ "ADMINS",
37
+ "PH_PRIVACY_ENABLED",
38
+ "PH_PRIVACY_DEPLOYMENT_SECRET",
39
+ ...Object.keys(env),
40
+ ];
41
+ const previous = names.map((name) => [name, process.env[name]] as const);
42
+ for (const name of names) delete process.env[name];
43
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
44
+ Object.assign(process.env, env);
45
+ try {
46
+ return await run(tempRoot);
47
+ } finally {
48
+ for (const [name, value] of previous) {
49
+ if (value === undefined) {
50
+ delete process.env[name];
51
+ } else {
52
+ process.env[name] = value;
53
+ }
54
+ }
55
+ await rm(tempRoot, { recursive: true, force: true });
56
+ }
57
+ }
58
+
59
+ async function boot(
60
+ tempRoot: string,
61
+ logger: ILogger = stubLogger(),
62
+ ): Promise<Switchboard> {
63
+ return bootSwitchboard(
64
+ {
65
+ server: { port: 0 },
66
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
67
+ features: { workflows: false, mcp: false },
68
+ packages: { disableLocal: true },
69
+ identity: { keypairPath: join(tempRoot, "identity.json") },
70
+ },
71
+ { logger },
72
+ );
73
+ }
74
+
75
+ async function queryErasure(switchboard: Switchboard) {
76
+ const response = await fetch(`http://127.0.0.1:${switchboard.port}/graphql`, {
77
+ method: "POST",
78
+ headers: { "content-type": "application/json" },
79
+ body: JSON.stringify({
80
+ query: `{ erasureRequest(requestId: "none") { requestId } }`,
81
+ }),
82
+ });
83
+ return (await response.json()) as {
84
+ errors?: { message: string; extensions?: { code?: string } }[];
85
+ };
86
+ }
87
+
88
+ describe("booting Switchboard with the privacy add-on", () => {
89
+ it("mounts nothing when PH_PRIVACY_ENABLED is off", async () => {
90
+ await withEnv({}, async (tempRoot) => {
91
+ const switchboard = await boot(tempRoot);
92
+ try {
93
+ expect(switchboard.privacy).toBeUndefined();
94
+ const result = await queryErasure(switchboard);
95
+ expect(result.errors?.[0]?.message).toMatch(/erasureRequest/);
96
+ expect(result.errors?.[0]?.extensions?.code).not.toBe("FORBIDDEN");
97
+ } finally {
98
+ await switchboard.shutdown();
99
+ }
100
+ });
101
+ }, 60_000);
102
+
103
+ it("serves erasure to admins only when it is on under authentication", async () => {
104
+ await withEnv(
105
+ {
106
+ AUTH_ENABLED: "true",
107
+ ADMINS: ADMIN,
108
+ PH_PRIVACY_ENABLED: "true",
109
+ PH_PRIVACY_DEPLOYMENT_SECRET: SECRET,
110
+ PH_PRIVACY_INTERVAL_MS: "50",
111
+ },
112
+ async (tempRoot) => {
113
+ const logger = stubLogger();
114
+ const switchboard = await boot(tempRoot, logger);
115
+ try {
116
+ expect(switchboard.privacy).toBeDefined();
117
+ expect(await switchboard.privacy!.erasure.plan([])).toEqual({
118
+ maxPurgeOperations: expect.any(Number) as number,
119
+ items: [],
120
+ });
121
+ await vi.waitUntil(
122
+ async () =>
123
+ (await queryErasure(switchboard)).errors?.[0]?.extensions
124
+ ?.code === "FORBIDDEN",
125
+ { timeout: 20_000, interval: 100 },
126
+ );
127
+ const document = await switchboard.reactor.createEmpty(
128
+ "powerhouse/document-model",
129
+ );
130
+ const id = document.header.id;
131
+ await switchboard.reactor.deleteDocument(id);
132
+ const erasure = switchboard.privacy!.erasure;
133
+ const { requestId } = await erasure.request([id], {
134
+ requestedBy: ADMIN,
135
+ });
136
+ await vi.waitUntil(
137
+ async () => (await erasure.status(requestId)).status === "complete",
138
+ { timeout: 30_000, interval: 100 },
139
+ );
140
+ expect(
141
+ logger.error.mock.calls.filter(([message]) =>
142
+ String(message).startsWith("Erasure"),
143
+ ),
144
+ ).toEqual([]);
145
+ } finally {
146
+ await switchboard.shutdown();
147
+ }
148
+ },
149
+ );
150
+ }, 60_000);
151
+
152
+ it("refuses to boot under the OPEN policy", async () => {
153
+ await withEnv(
154
+ {
155
+ PH_PRIVACY_ENABLED: "true",
156
+ PH_PRIVACY_DEPLOYMENT_SECRET: SECRET,
157
+ },
158
+ async (tempRoot) => {
159
+ await expect(boot(tempRoot)).rejects.toThrow(/OPEN/);
160
+ },
161
+ );
162
+ }, 60_000);
163
+
164
+ it("refuses to boot without a deployment secret", async () => {
165
+ await withEnv(
166
+ { AUTH_ENABLED: "true", ADMINS: ADMIN, PH_PRIVACY_ENABLED: "true" },
167
+ async (tempRoot) => {
168
+ await expect(boot(tempRoot)).rejects.toThrow(
169
+ /PH_PRIVACY_DEPLOYMENT_SECRET/,
170
+ );
171
+ },
172
+ );
173
+ }, 60_000);
174
+ });
175
+
176
+ const PG_URL = process.env.REACTOR_TEST_PG_URL;
177
+
178
+ describe.skipIf(!PG_URL)(
179
+ "the privacy add-on on a Postgres reactor store",
180
+ () => {
181
+ it("erases a document end to end", async () => {
182
+ const name = `sb_privacy_${process.pid}`;
183
+ const admin = new pg.Pool({ connectionString: PG_URL });
184
+ await admin.query(`DROP DATABASE IF EXISTS "${name}" WITH (FORCE)`);
185
+ await admin.query(`CREATE DATABASE "${name}"`);
186
+ const url = new URL(PG_URL!);
187
+ url.pathname = `/${name}`;
188
+ try {
189
+ await withEnv(
190
+ {
191
+ AUTH_ENABLED: "true",
192
+ ADMINS: ADMIN,
193
+ PH_PRIVACY_ENABLED: "true",
194
+ PH_PRIVACY_DEPLOYMENT_SECRET: SECRET,
195
+ PH_PRIVACY_INTERVAL_MS: "50",
196
+ },
197
+ async (tempRoot) => {
198
+ process.env.PH_REACTOR_DATABASE_URL = url.toString();
199
+ const logger = stubLogger();
200
+ const switchboard = await boot(tempRoot, logger);
201
+ try {
202
+ const document = await switchboard.reactor.createEmpty(
203
+ "powerhouse/document-model",
204
+ );
205
+ const id = document.header.id;
206
+ await switchboard.reactor.deleteDocument(id);
207
+ const erasure = switchboard.privacy!.erasure;
208
+ const { requestId } = await erasure.request([id], {
209
+ requestedBy: ADMIN,
210
+ });
211
+ await vi.waitUntil(
212
+ async () =>
213
+ (await erasure.status(requestId)).status === "complete",
214
+ { timeout: 30_000, interval: 100 },
215
+ );
216
+ expect(
217
+ logger.error.mock.calls.filter(([message]) =>
218
+ String(message).startsWith("Erasure"),
219
+ ),
220
+ ).toEqual([]);
221
+ } finally {
222
+ await switchboard.shutdown();
223
+ }
224
+ },
225
+ );
226
+ } finally {
227
+ await admin.query(`DROP DATABASE IF EXISTS "${name}" WITH (FORCE)`);
228
+ await admin.end();
229
+ }
230
+ }, 90_000);
231
+ },
232
+ );
@@ -0,0 +1,214 @@
1
+ import { PGlite } from "@electric-sql/pglite";
2
+ import { NodeFS } from "@electric-sql/pglite/nodefs";
3
+ import {
4
+ JobStatus,
5
+ ReactorBuilder,
6
+ type Database,
7
+ } from "@powerhousedao/reactor";
8
+ import { driveDocumentModelModule } from "@powerhousedao/shared/document-drive";
9
+ import {
10
+ withSignaturePolicy,
11
+ type PeerCapability,
12
+ } from "@powerhousedao/shared/document-model";
13
+ import type { ILogger } from "document-model";
14
+ import { Kysely } from "kysely";
15
+ import { cp, mkdtemp, rm } from "node:fs/promises";
16
+ import { tmpdir } from "node:os";
17
+ import { join } from "node:path";
18
+ import {
19
+ afterAll,
20
+ afterEach,
21
+ beforeAll,
22
+ beforeEach,
23
+ describe,
24
+ expect,
25
+ it,
26
+ vi,
27
+ } from "vitest";
28
+ import { ClosablePGliteDialect } from "../src/pglite-dialect.js";
29
+ import { bootSwitchboard } from "./boot-helpers.js";
30
+ import { StoredDocumentsRefusedError } from "../src/unsupported-stored-documents.mjs";
31
+
32
+ const ENV_KEYS = [
33
+ "PH_REACTOR_DATABASE_URL",
34
+ "DATABASE_URL",
35
+ "REACTOR_UNSUPPORTED_STORED_DOCUMENTS",
36
+ ] as const;
37
+
38
+ const BASE_REDUCER_7: PeerCapability = {
39
+ kind: "protocol",
40
+ name: "base-reducer",
41
+ baseline: [1, 2],
42
+ supported: () => [1, 2, 7],
43
+ preferred: () => 2,
44
+ optional: false,
45
+ };
46
+
47
+ type StubLogger = ILogger & {
48
+ warn: ReturnType<typeof vi.fn>;
49
+ error: ReturnType<typeof vi.fn>;
50
+ };
51
+
52
+ function stubLogger(): StubLogger {
53
+ const logger = {
54
+ verbose: vi.fn(),
55
+ debug: vi.fn(),
56
+ info: vi.fn(),
57
+ warn: vi.fn(),
58
+ error: vi.fn(),
59
+ child: vi.fn(),
60
+ };
61
+ logger.child.mockReturnValue(logger);
62
+ return logger as unknown as StubLogger;
63
+ }
64
+
65
+ /** A reactor store holding one drive created at base-reducer 7. */
66
+ async function seedStore(dir: string): Promise<void> {
67
+ const db = new Kysely<Database>({
68
+ dialect: new ClosablePGliteDialect(new PGlite({ fs: new NodeFS(dir) })),
69
+ });
70
+ const module = await new ReactorBuilder()
71
+ .withKysely(db)
72
+ .withDocumentModelSources([driveDocumentModelModule as never])
73
+ .withPeerCapabilities([BASE_REDUCER_7])
74
+ .buildModule();
75
+ const created = await module.reactor.create(
76
+ withSignaturePolicy(
77
+ driveDocumentModelModule.utils.createDocument(),
78
+ "legacy",
79
+ {
80
+ id: "br7-stored",
81
+ protocolVersions: { "base-reducer": 7 },
82
+ },
83
+ ),
84
+ );
85
+ await vi.waitUntil(
86
+ async () => {
87
+ const { status } = await module.reactor.getJobStatus(created.id);
88
+ return status === JobStatus.READ_READY || status === JobStatus.FAILED;
89
+ },
90
+ // PGlite is several times slower on the Windows runner.
91
+ { timeout: BOOT_TIMEOUT, interval: 5 },
92
+ );
93
+ expect((await module.reactor.getJobStatus(created.id)).status).toBe(
94
+ JobStatus.READ_READY,
95
+ );
96
+ await module.reactor.kill().completed;
97
+ await db.destroy();
98
+ // PGlite leaves its lockfile behind after a clean close.
99
+ await rm(join(dir, "postmaster.pid"), { force: true });
100
+ }
101
+
102
+ // PGlite boots are several times slower on Windows runners.
103
+ const BOOT_TIMEOUT = 120_000;
104
+ // Windows can briefly keep just-closed store files locked (EBUSY/EPERM).
105
+ const RM_OPTIONS = { recursive: true, force: true, maxRetries: 10 };
106
+
107
+ describe("booting over documents this build does not run", () => {
108
+ let seeded: string;
109
+ let tempRoot: string;
110
+ let previous: Partial<Record<(typeof ENV_KEYS)[number], string>>;
111
+
112
+ beforeAll(async () => {
113
+ seeded = await mkdtemp(join(tmpdir(), "switchboard-stored-protocol-seed-"));
114
+ await seedStore(join(seeded, "reactor-storage"));
115
+ }, BOOT_TIMEOUT);
116
+
117
+ afterAll(async () => {
118
+ await rm(seeded, RM_OPTIONS);
119
+ }, BOOT_TIMEOUT);
120
+
121
+ beforeEach(async () => {
122
+ tempRoot = await mkdtemp(join(tmpdir(), "switchboard-stored-protocol-"));
123
+ previous = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
124
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
125
+ process.env.DATABASE_URL = join(tempRoot, "read-model");
126
+ delete process.env.REACTOR_UNSUPPORTED_STORED_DOCUMENTS;
127
+ await cp(
128
+ join(seeded, "reactor-storage"),
129
+ process.env.PH_REACTOR_DATABASE_URL,
130
+ { recursive: true },
131
+ );
132
+ }, BOOT_TIMEOUT);
133
+
134
+ afterEach(async () => {
135
+ for (const key of ENV_KEYS) {
136
+ if (previous[key] === undefined) {
137
+ delete process.env[key];
138
+ } else {
139
+ process.env[key] = previous[key];
140
+ }
141
+ }
142
+ await rm(tempRoot, RM_OPTIONS);
143
+ }, BOOT_TIMEOUT);
144
+
145
+ function boot(logger: ILogger) {
146
+ return bootSwitchboard(
147
+ {
148
+ server: { port: 0 },
149
+ features: { workflows: false, mcp: false },
150
+ packages: { disableLocal: true },
151
+ identity: { keypairPath: join(tempRoot, "identity.json") },
152
+ },
153
+ { logger },
154
+ );
155
+ }
156
+
157
+ it(
158
+ "refuses by default and tells the operator both ways forward",
159
+ async () => {
160
+ const logger = stubLogger();
161
+
162
+ const booted = boot(logger);
163
+
164
+ await expect(booted).rejects.toSatisfy(
165
+ (error) =>
166
+ StoredDocumentsRefusedError.isError(error) &&
167
+ error.documents === 1 &&
168
+ error.versions.some(
169
+ ({ protocol, version }) =>
170
+ protocol === "base-reducer" && version === 7,
171
+ ),
172
+ );
173
+ const message = (await booted.catch((error: Error) => error)) as Error;
174
+ expect(message.message).toMatch(
175
+ /1 stored document\(s\) require base-reducer 7/,
176
+ );
177
+ expect(message.message).toMatch(
178
+ /switchboard build that runs base-reducer 7/,
179
+ );
180
+ expect(message.message).toMatch(
181
+ /REACTOR_UNSUPPORTED_STORED_DOCUMENTS=read-only/,
182
+ );
183
+ expect(logger.error).toHaveBeenCalledWith(message.message);
184
+ expect(logger.error).not.toHaveBeenCalledWith(
185
+ "App crashed: @error",
186
+ expect.anything(),
187
+ );
188
+ },
189
+ BOOT_TIMEOUT,
190
+ );
191
+
192
+ it(
193
+ "starts read-only when REACTOR_UNSUPPORTED_STORED_DOCUMENTS says so",
194
+ async () => {
195
+ process.env.REACTOR_UNSUPPORTED_STORED_DOCUMENTS = "read-only";
196
+ const logger = stubLogger();
197
+
198
+ const switchboard = await boot(logger);
199
+ try {
200
+ expect(logger.warn).toHaveBeenCalledWith(
201
+ expect.stringContaining("read-only"),
202
+ expect.stringContaining(
203
+ "1 stored document(s) require base-reducer 7",
204
+ ),
205
+ );
206
+ const stored = await switchboard.reactor.get("br7-stored");
207
+ expect(stored.header.id).toBe("br7-stored");
208
+ } finally {
209
+ await switchboard.shutdown();
210
+ }
211
+ },
212
+ BOOT_TIMEOUT,
213
+ );
214
+ });