@powerhousedao/switchboard 6.2.3 → 6.3.0-dev.1

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 (271) hide show
  1. package/.tsbuild/test/ai-gateway.test.d.ts +2 -0
  2. package/.tsbuild/test/ai-gateway.test.d.ts.map +1 -0
  3. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts +2 -0
  4. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts.map +1 -0
  5. package/.tsbuild/test/boot-auth-config.test.d.ts +2 -0
  6. package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
  7. package/.tsbuild/test/boot-desktop-auth.test.d.ts +2 -0
  8. package/.tsbuild/test/boot-desktop-auth.test.d.ts.map +1 -0
  9. package/.tsbuild/test/boot-env-isolation.test.d.ts +2 -0
  10. package/.tsbuild/test/boot-env-isolation.test.d.ts.map +1 -0
  11. package/.tsbuild/test/boot-helpers.d.ts +5 -0
  12. package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
  13. package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
  14. package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
  15. package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
  16. package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
  17. package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
  18. package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
  19. package/.tsbuild/test/boot-preconditions.test.d.ts +2 -0
  20. package/.tsbuild/test/boot-preconditions.test.d.ts.map +1 -0
  21. package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
  22. package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
  23. package/.tsbuild/test/boot-ready-drives.test.d.ts +2 -0
  24. package/.tsbuild/test/boot-ready-drives.test.d.ts.map +1 -0
  25. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts +2 -0
  26. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts.map +1 -0
  27. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
  28. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
  29. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
  30. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
  31. package/.tsbuild/test/config-from-env.test.d.ts +2 -0
  32. package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
  33. package/.tsbuild/test/config-merge.test.d.ts +2 -0
  34. package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
  35. package/.tsbuild/test/config-schema.test.d.ts +2 -0
  36. package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
  37. package/.tsbuild/test/default-composition.test.d.ts +2 -0
  38. package/.tsbuild/test/default-composition.test.d.ts.map +1 -0
  39. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
  40. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
  41. package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
  42. package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
  43. package/.tsbuild/test/delegated-publish.test.d.ts +2 -0
  44. package/.tsbuild/test/delegated-publish.test.d.ts.map +1 -0
  45. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
  46. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
  47. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/greetings/index.d.ts +13 -0
  48. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/greetings/index.d.ts.map +1 -0
  49. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/index.d.ts +3 -0
  50. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/index.d.ts.map +1 -0
  51. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/tally/index.d.ts +7 -0
  52. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/tally/index.d.ts.map +1 -0
  53. package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
  54. package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
  55. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts +2 -0
  56. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts.map +1 -0
  57. package/.tsbuild/test/observability-signals.test.d.ts +2 -0
  58. package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
  59. package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
  60. package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
  61. package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
  62. package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
  63. package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
  64. package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
  65. package/.tsbuild/test/pglite-store.test.d.ts +2 -0
  66. package/.tsbuild/test/pglite-store.test.d.ts.map +1 -0
  67. package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
  68. package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
  69. package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
  70. package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
  71. package/.tsbuild/test/port-probe.test.d.ts +2 -0
  72. package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
  73. package/.tsbuild/test/processor-timing-log.test.d.ts +2 -0
  74. package/.tsbuild/test/processor-timing-log.test.d.ts.map +1 -0
  75. package/.tsbuild/test/renown-storage-path.test.d.ts +2 -0
  76. package/.tsbuild/test/renown-storage-path.test.d.ts.map +1 -0
  77. package/.tsbuild/test/served-schemas-mixed-package.test.d.ts +2 -0
  78. package/.tsbuild/test/served-schemas-mixed-package.test.d.ts.map +1 -0
  79. package/.tsbuild/test/served-schemas.test.d.ts +2 -0
  80. package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
  81. package/.tsbuild/test/shutdown.test.d.ts +2 -0
  82. package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
  83. package/.tsbuild/test/snapshot-writer.d.ts +18 -0
  84. package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
  85. package/.tsbuild/test/store-lock.test.d.ts +2 -0
  86. package/.tsbuild/test/store-lock.test.d.ts.map +1 -0
  87. package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
  88. package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
  89. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts +2 -0
  90. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts.map +1 -0
  91. package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
  92. package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
  93. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts +2 -0
  94. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts.map +1 -0
  95. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts +2 -0
  96. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts.map +1 -0
  97. package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
  98. package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
  99. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
  100. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
  101. package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
  102. package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
  103. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  104. package/Auth.md +108 -2
  105. package/CHANGELOG.md +1062 -2
  106. package/README.md +69 -24
  107. package/dist/boot.d.mts +46 -0
  108. package/dist/boot.d.mts.map +1 -0
  109. package/dist/boot.mjs +67 -0
  110. package/dist/boot.mjs.map +1 -0
  111. package/dist/config/from-env.d.mts +15 -0
  112. package/dist/config/from-env.d.mts.map +1 -0
  113. package/dist/config/from-env.mjs +4 -0
  114. package/dist/config/schema.d.mts +3 -0
  115. package/dist/config/schema.mjs +4 -0
  116. package/dist/from-env-DKK_wPrO.mjs +497 -0
  117. package/dist/from-env-DKK_wPrO.mjs.map +1 -0
  118. package/dist/index.d.mts +1 -2
  119. package/dist/index.mjs +40 -219
  120. package/dist/index.mjs.map +1 -1
  121. package/dist/mcp-VW52B8KD.mjs +32 -0
  122. package/dist/mcp-VW52B8KD.mjs.map +1 -0
  123. package/dist/migrate.mjs +5 -4
  124. package/dist/migrate.mjs.map +1 -1
  125. package/dist/observability-CldagyT_.mjs +224 -0
  126. package/dist/observability-CldagyT_.mjs.map +1 -0
  127. package/dist/observability.d.mts +30 -0
  128. package/dist/observability.d.mts.map +1 -0
  129. package/dist/observability.mjs +4 -0
  130. package/dist/plugin-host-tTjDTNz0.d.mts +46 -0
  131. package/dist/plugin-host-tTjDTNz0.d.mts.map +1 -0
  132. package/dist/plugins/ai.d.mts +37 -0
  133. package/dist/plugins/ai.d.mts.map +1 -0
  134. package/dist/plugins/ai.mjs +400 -0
  135. package/dist/plugins/ai.mjs.map +1 -0
  136. package/dist/plugins/attachments.d.mts +33 -0
  137. package/dist/plugins/attachments.d.mts.map +1 -0
  138. package/dist/plugins/attachments.mjs +4 -0
  139. package/dist/plugins/mcp.d.mts +31 -0
  140. package/dist/plugins/mcp.d.mts.map +1 -0
  141. package/dist/plugins/mcp.mjs +4 -0
  142. package/dist/plugins/privacy.d.mts +22 -0
  143. package/dist/plugins/privacy.d.mts.map +1 -0
  144. package/dist/plugins/privacy.mjs +4 -0
  145. package/dist/plugins/workflows.d.mts +80 -0
  146. package/dist/plugins/workflows.d.mts.map +1 -0
  147. package/dist/plugins/workflows.mjs +4 -0
  148. package/dist/postgres-url-DTDhBjXX.mjs +11 -0
  149. package/dist/postgres-url-DTDhBjXX.mjs.map +1 -0
  150. package/dist/privacy-HQnhqf2H.mjs +91 -0
  151. package/dist/privacy-HQnhqf2H.mjs.map +1 -0
  152. package/dist/privacy-UodgY0i_.mjs +44 -0
  153. package/dist/privacy-UodgY0i_.mjs.map +1 -0
  154. package/dist/renown-layers-DmbWLt46.mjs +42 -0
  155. package/dist/renown-layers-DmbWLt46.mjs.map +1 -0
  156. package/dist/schema-BJ-OFAox.mjs +359 -0
  157. package/dist/schema-BJ-OFAox.mjs.map +1 -0
  158. package/dist/schema-w1lShimw.d.mts +258 -0
  159. package/dist/schema-w1lShimw.d.mts.map +1 -0
  160. package/dist/server-BS-PFzoK.mjs +2752 -0
  161. package/dist/server-BS-PFzoK.mjs.map +1 -0
  162. package/dist/server.d.mts +94 -228
  163. package/dist/server.d.mts.map +1 -1
  164. package/dist/server.mjs +5 -4
  165. package/dist/types-Bk4T4D9F.d.mts +318 -0
  166. package/dist/types-Bk4T4D9F.d.mts.map +1 -0
  167. package/dist/utils-Cm0TE6dP.mjs +148 -0
  168. package/dist/utils-Cm0TE6dP.mjs.map +1 -0
  169. package/dist/utils.d.mts +29 -3
  170. package/dist/utils.d.mts.map +1 -1
  171. package/dist/utils.mjs +5 -4
  172. package/dist/worker-pool-gyxJnCxA.mjs +304 -0
  173. package/dist/worker-pool-gyxJnCxA.mjs.map +1 -0
  174. package/dist/workflow-runtime-CF5pc8mt.d.mts +33 -0
  175. package/dist/workflow-runtime-CF5pc8mt.d.mts.map +1 -0
  176. package/dist/workflows-mdYUh3Us.mjs +1947 -0
  177. package/dist/workflows-mdYUh3Us.mjs.map +1 -0
  178. package/package.json +86 -24
  179. package/scripts/install-packages.sh +0 -0
  180. package/test/ai-gateway.test.ts +612 -0
  181. package/test/attachment-reference-read-model.test.ts +28 -20
  182. package/test/attachments/byte-grants.test.ts +4 -1
  183. package/test/attachments/download-target.test.ts +36 -20
  184. package/test/attachments/index.test.ts +13 -1
  185. package/test/attachments/reserve-metadata-parity.test.ts +96 -0
  186. package/test/attachments/routes-integration.test.ts +1 -0
  187. package/test/attachments/routes.test.ts +132 -12
  188. package/test/attachments/service-config.test.ts +28 -16
  189. package/test/boot-auth-config.test.ts +146 -0
  190. package/test/boot-create-signature-policy.test.ts +15 -13
  191. package/test/boot-desktop-auth.test.ts +278 -0
  192. package/test/boot-env-isolation.test.ts +26 -0
  193. package/test/boot-helpers.ts +19 -0
  194. package/test/boot-inspection-enabled.test.ts +140 -0
  195. package/test/boot-mcp-auth.test.ts +449 -0
  196. package/test/boot-mcp-enabled.test.ts +27 -27
  197. package/test/boot-port-zero.test.ts +101 -0
  198. package/test/boot-preconditions.test.ts +213 -0
  199. package/test/boot-privacy.test.ts +232 -0
  200. package/test/boot-ready-drives.test.ts +141 -0
  201. package/test/boot-undo-v3-flag.test.ts +120 -0
  202. package/test/boot-unsupported-stored-documents.test.ts +23 -19
  203. package/test/boot-without-renown.test.ts +11 -9
  204. package/test/boot-without-require-signatures.test.ts +12 -10
  205. package/test/boot-worker-trust-policy.test.ts +11 -10
  206. package/test/boot-workflow-singleton-conflict.test.ts +109 -0
  207. package/test/boot-workflow-singleton-retry.test.ts +122 -0
  208. package/test/config-from-env.test.ts +968 -0
  209. package/test/config-merge.test.ts +351 -0
  210. package/test/config-schema.test.ts +183 -0
  211. package/test/default-composition.test.ts +290 -0
  212. package/test/default-drive-protocol-versions.test.ts +79 -0
  213. package/test/default-drive-slug.test.ts +265 -0
  214. package/test/delegated-publish.test.ts +435 -0
  215. package/test/durable-node-fs-pg16.test.ts +80 -0
  216. package/test/fatal-shutdown.test.ts +104 -1
  217. package/test/fixtures/mixed-subgraphs/subgraphs/greetings/index.ts +16 -0
  218. package/test/fixtures/mixed-subgraphs/subgraphs/index.ts +3 -0
  219. package/test/fixtures/mixed-subgraphs/subgraphs/tally/index.ts +73 -0
  220. package/test/goldens/packages.graphql +797 -0
  221. package/test/goldens/privacy.graphql +853 -0
  222. package/test/goldens/reactor-drive-nodes.graphql +827 -0
  223. package/test/goldens/resolvers.json +327 -0
  224. package/test/goldens/supergraph.graphql +5918 -0
  225. package/test/goldens/workflow-runtime.graphql +1548 -0
  226. package/test/inspection-throttle.test.ts +23 -0
  227. package/test/keyframe-reclaim-store.test.ts +156 -0
  228. package/test/observability-signals.test.ts +131 -0
  229. package/test/permissions-backfill.test.ts +486 -0
  230. package/test/pglite-dialect.test.ts +237 -2
  231. package/test/pglite-preflight.test.ts +313 -0
  232. package/test/pglite-storage-dirs.test.ts +113 -0
  233. package/test/pglite-store.test.ts +441 -0
  234. package/test/pglite-version.test.ts +9 -1
  235. package/test/plugin-config-schemas.test.ts +83 -0
  236. package/test/plugin-presence.test.ts +262 -0
  237. package/test/port-probe.test.ts +45 -0
  238. package/test/processor-timing-log.test.ts +109 -0
  239. package/test/reactor-feature-flags.test.ts +20 -0
  240. package/test/redact-webhook-path.test.ts +9 -0
  241. package/test/renown-storage-path.test.ts +40 -0
  242. package/test/renown-trust-policy.test.ts +7 -9
  243. package/test/served-schemas-mixed-package.test.ts +251 -0
  244. package/test/served-schemas.test.ts +134 -0
  245. package/test/shutdown.test.ts +278 -0
  246. package/test/snapshot-writer.ts +94 -0
  247. package/test/store-lock.test.ts +65 -0
  248. package/test/two-switchboards.test.ts +146 -0
  249. package/test/worker-pool.test.ts +27 -1
  250. package/test/workflow/oauth-apps-config.test.ts +66 -0
  251. package/test/workflow/oauth.test.ts +82 -10
  252. package/test/workflow/resolvers.test.ts +230 -38
  253. package/test/workflow/runtime-host.test.ts +19 -0
  254. package/test/workflow/waitpoint-routes.test.ts +306 -0
  255. package/test/workflow/waitpoint-runtime.test.ts +204 -0
  256. package/test/workflow/webhook-trigger-test.test.ts +8 -0
  257. package/test/workflow-reactor-access.test.ts +384 -0
  258. package/test/workflow-reactor-enforcement.test.ts +808 -0
  259. package/test/workflow-read-gate.test.ts +128 -60
  260. package/test/workflow-runtime.test.ts +1005 -237
  261. package/test/workflows-plugin-config.test.ts +77 -0
  262. package/tsconfig.json +5 -2
  263. package/tsdown.config.ts +12 -0
  264. package/vitest.config.ts +66 -1
  265. package/.tsbuild/test/mcp-flag.test.d.ts +0 -2
  266. package/.tsbuild/test/mcp-flag.test.d.ts.map +0 -1
  267. package/dist/server-sQoWp21G.mjs +0 -2939
  268. package/dist/server-sQoWp21G.mjs.map +0 -1
  269. package/dist/utils-uaBlzyjS.mjs +0 -86
  270. package/dist/utils-uaBlzyjS.mjs.map +0 -1
  271. package/test/mcp-flag.test.ts +0 -56
@@ -0,0 +1,290 @@
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 { describe, expect, it, vi } from "vitest";
6
+ import { binSwitchboardConfig } from "../src/config/bin.js";
7
+ import { switchboardConfigFromEnv } from "../src/config/from-env.js";
8
+ import {
9
+ callerOverEnv,
10
+ switchboardConfigSchema,
11
+ type SwitchboardConfig,
12
+ type SwitchboardConfigInput,
13
+ } from "../src/config/schema.js";
14
+ import {
15
+ builtinSwitchboardPlugins,
16
+ defaultSwitchboardPlugins,
17
+ } from "../src/plugin-host.mjs";
18
+ import type { startSwitchboard } from "../src/server.mjs";
19
+ import { bootSwitchboard } from "./boot-helpers.js";
20
+
21
+ type Env = Record<string, string | undefined>;
22
+
23
+ const MCP = "switchboard-mcp";
24
+ const ATTACHMENTS = "switchboard-attachments";
25
+ const PRIVACY = "switchboard-privacy";
26
+ const WORKFLOWS = "switchboard-workflows";
27
+
28
+ const silent = { warn: () => undefined } as unknown as ILogger;
29
+
30
+ function stubLogger(): ILogger {
31
+ const logger = {
32
+ verbose: vi.fn(),
33
+ debug: vi.fn(),
34
+ info: vi.fn(),
35
+ warn: vi.fn(),
36
+ error: vi.fn(),
37
+ child: vi.fn(),
38
+ };
39
+ logger.child.mockReturnValue(logger);
40
+ return logger as unknown as ILogger;
41
+ }
42
+
43
+ function resolved(env: Env, caller: SwitchboardConfigInput): SwitchboardConfig {
44
+ return switchboardConfigSchema.parse(
45
+ callerOverEnv(switchboardConfigFromEnv(env, { logger: silent }), caller),
46
+ );
47
+ }
48
+
49
+ function names(switches: { mcp: boolean; workflows: boolean }): string[] {
50
+ return defaultSwitchboardPlugins(switches).map((r) => r.plugin.name);
51
+ }
52
+
53
+ describe("defaultSwitchboardPlugins", () => {
54
+ it.each([
55
+ [true, true, [MCP, ATTACHMENTS, PRIVACY, WORKFLOWS]],
56
+ [true, false, [MCP, ATTACHMENTS, PRIVACY]],
57
+ [false, true, [ATTACHMENTS, PRIVACY, WORKFLOWS]],
58
+ [false, false, [ATTACHMENTS, PRIVACY]],
59
+ ])("mcp %s, workflows %s", (mcp, workflows, expected) => {
60
+ expect(names({ mcp, workflows })).toEqual(expected);
61
+ });
62
+
63
+ it("is the built-in list, filtered: same entries, same order", () => {
64
+ const builtin = builtinSwitchboardPlugins();
65
+ expect(defaultSwitchboardPlugins({ mcp: true, workflows: true })).toEqual(
66
+ builtin,
67
+ );
68
+ const neither = defaultSwitchboardPlugins({ mcp: false, workflows: false });
69
+ expect(neither).toEqual(
70
+ builtin.filter(
71
+ (r) => r.plugin.name === ATTACHMENTS || r.plugin.name === PRIVACY,
72
+ ),
73
+ );
74
+ });
75
+ });
76
+
77
+ describe("features.pluginHost is tri-state", () => {
78
+ it("stays unset in the schema, and keeps an explicit true or false", () => {
79
+ expect(switchboardConfigSchema.parse({}).features.pluginHost).toBe(
80
+ undefined,
81
+ );
82
+ for (const pluginHost of [true, false]) {
83
+ expect(
84
+ switchboardConfigSchema.parse({ features: { pluginHost } }).features
85
+ .pluginHost,
86
+ ).toBe(pluginHost);
87
+ }
88
+ });
89
+
90
+ it.each([
91
+ [undefined, undefined],
92
+ ["", undefined],
93
+ [" ", undefined],
94
+ ["1", true],
95
+ ["true", true],
96
+ ["false", false],
97
+ ["0", false],
98
+ ["off", false],
99
+ ["no", false],
100
+ // Neither a yes nor a no: the default composition, as when unset.
101
+ ["on", undefined],
102
+ ["yes", undefined],
103
+ ])("FEATURE_PLUGIN_HOST_ENABLED=%j maps to %s", (value, expected) => {
104
+ expect(
105
+ resolved({ FEATURE_PLUGIN_HOST_ENABLED: value }, {}).features.pluginHost,
106
+ ).toBe(expected);
107
+ });
108
+
109
+ it("a caller's explicit false selects the classic boot over the env", () => {
110
+ expect(
111
+ resolved(
112
+ { FEATURE_PLUGIN_HOST_ENABLED: "1" },
113
+ { features: { pluginHost: false } },
114
+ ).features.pluginHost,
115
+ ).toBe(false);
116
+ });
117
+ });
118
+
119
+ describe("the switches without a declared composition", () => {
120
+ it("keeps an env true for MCP_ENABLED and PH_WORKFLOWS_ENABLED", () => {
121
+ const config = resolved(
122
+ { MCP_ENABLED: "true", PH_WORKFLOWS_ENABLED: "true" },
123
+ {},
124
+ );
125
+ expect(config.features.pluginHost).toBeUndefined();
126
+ expect(config.features.mcp).toBe(true);
127
+ expect(config.features.workflows).toBe(true);
128
+ });
129
+
130
+ it("lets an env false win over the caller's true", () => {
131
+ const config = resolved(
132
+ { MCP_ENABLED: "false", PH_WORKFLOWS_ENABLED: "false" },
133
+ { features: { mcp: true, workflows: true } },
134
+ );
135
+ expect(config.features.mcp).toBe(false);
136
+ expect(config.features.workflows).toBe(false);
137
+ });
138
+
139
+ it("MCP_ENABLED=false beats the bin's own features.mcp", () => {
140
+ const config = switchboardConfigSchema.parse(
141
+ binSwitchboardConfig(
142
+ { MCP_ENABLED: "false" },
143
+ { migratePglite: false, logger: silent },
144
+ ),
145
+ );
146
+ expect(config.features.pluginHost).toBeUndefined();
147
+ expect(config.features.mcp).toBe(false);
148
+ });
149
+
150
+ it("the bin mounts MCP when the env says nothing", () => {
151
+ const config = switchboardConfigSchema.parse(
152
+ binSwitchboardConfig({}, { migratePglite: false, logger: silent }),
153
+ );
154
+ expect(config.features.pluginHost).toBeUndefined();
155
+ expect(config.features.mcp).toBe(true);
156
+ });
157
+ });
158
+
159
+ const PLUGIN_LOG = "[switchboard:plugins]";
160
+
161
+ /** Boots with the env sandboxed like plugin-presence.test.ts, and hands back
162
+ * the plugin host's console lines: none means the classic inline boot ran. */
163
+ async function withSwitchboard(
164
+ env: Env,
165
+ features: NonNullable<SwitchboardConfigInput["features"]>,
166
+ run: (
167
+ switchboard: Awaited<ReturnType<typeof startSwitchboard>>,
168
+ pluginLines: string[],
169
+ ) => Promise<void>,
170
+ ): Promise<void> {
171
+ const tempRoot = await mkdtemp(
172
+ join(tmpdir(), "switchboard-default-composition-"),
173
+ );
174
+ const touched = [
175
+ "PH_REACTOR_DATABASE_URL",
176
+ "MCP_ENABLED",
177
+ "PH_WORKFLOWS_ENABLED",
178
+ "FEATURE_PLUGIN_HOST_ENABLED",
179
+ ];
180
+ const previous = touched.map((name) => [name, process.env[name]] as const);
181
+ for (const name of touched) delete process.env[name];
182
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
183
+ for (const [name, value] of Object.entries(env)) {
184
+ if (value !== undefined) process.env[name] = value;
185
+ }
186
+ const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
187
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
188
+
189
+ try {
190
+ switchboard = await bootSwitchboard(
191
+ {
192
+ server: { port: 0 },
193
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
194
+ features: { workflows: false, ...features },
195
+ packages: { disableLocal: true },
196
+ identity: { keypairPath: join(tempRoot, "identity.json") },
197
+ drive: {
198
+ slug: "default-drive",
199
+ global: { name: "Default Drive" },
200
+ },
201
+ },
202
+ { logger: stubLogger() },
203
+ );
204
+ const pluginLines = log.mock.calls
205
+ .map((call) => String(call[0]))
206
+ .filter((line) => line.startsWith(PLUGIN_LOG));
207
+ await run(switchboard, pluginLines);
208
+ await switchboard.shutdown();
209
+ switchboard = undefined;
210
+ } finally {
211
+ await switchboard?.shutdown();
212
+ log.mockRestore();
213
+ for (const [name, value] of previous) {
214
+ if (value === undefined) {
215
+ delete process.env[name];
216
+ } else {
217
+ process.env[name] = value;
218
+ }
219
+ }
220
+ await rm(tempRoot, { recursive: true, force: true });
221
+ }
222
+ }
223
+
224
+ async function postMcp(port: number): Promise<number> {
225
+ const response = await fetch(`http://127.0.0.1:${port}/mcp`, {
226
+ method: "POST",
227
+ headers: {
228
+ "content-type": "application/json",
229
+ accept: "application/json, text/event-stream",
230
+ },
231
+ body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "ping" }),
232
+ });
233
+ await response.body?.cancel();
234
+ return response.status;
235
+ }
236
+
237
+ describe("a boot without a declared composition", () => {
238
+ it("runs the default composition through the plugin host", async () => {
239
+ await withSwitchboard({}, {}, async (switchboard, pluginLines) => {
240
+ expect(pluginLines[0]).toContain(
241
+ `composition: 3 plugin(s) in order — ${MCP}, ${ATTACHMENTS}, ${PRIVACY}`,
242
+ );
243
+ expect(switchboard.mcpEnabled).toBe(true);
244
+ expect(await postMcp(switchboard.port)).not.toBe(404);
245
+ expect(switchboard.attachmentService).toBeDefined();
246
+ expect(switchboard.workflowsEnabled).toBe(false);
247
+ });
248
+ }, 60_000);
249
+
250
+ it("MCP_ENABLED=false leaves the mcp plugin out", async () => {
251
+ await withSwitchboard(
252
+ { MCP_ENABLED: "false" },
253
+ { mcp: true },
254
+ async (switchboard, pluginLines) => {
255
+ expect(pluginLines[0]).toContain(
256
+ `composition: 2 plugin(s) in order — ${ATTACHMENTS}, ${PRIVACY}`,
257
+ );
258
+ expect(switchboard.mcpEnabled).toBe(false);
259
+ expect(await postMcp(switchboard.port)).toBe(404);
260
+ expect(switchboard.attachmentService).toBeDefined();
261
+ },
262
+ );
263
+ }, 60_000);
264
+
265
+ it("an explicit features.pluginHost: false still boots the classic path", async () => {
266
+ await withSwitchboard(
267
+ {},
268
+ { pluginHost: false },
269
+ async (switchboard, pluginLines) => {
270
+ expect(pluginLines).toEqual([]);
271
+ expect(switchboard.mcpEnabled).toBe(true);
272
+ expect(await postMcp(switchboard.port)).not.toBe(404);
273
+ expect(switchboard.attachmentService).toBeDefined();
274
+ },
275
+ );
276
+ }, 60_000);
277
+
278
+ it("FEATURE_PLUGIN_HOST_ENABLED=false still boots the classic path", async () => {
279
+ await withSwitchboard(
280
+ { FEATURE_PLUGIN_HOST_ENABLED: "false", MCP_ENABLED: "false" },
281
+ {},
282
+ async (switchboard, pluginLines) => {
283
+ expect(pluginLines).toEqual([]);
284
+ expect(switchboard.mcpEnabled).toBe(false);
285
+ expect(await postMcp(switchboard.port)).toBe(404);
286
+ expect(switchboard.attachmentService).toBeDefined();
287
+ },
288
+ );
289
+ }, 60_000);
290
+ });
@@ -0,0 +1,79 @@
1
+ import type { IReactorClient } from "@powerhousedao/reactor";
2
+ import { reactorDriveDocumentModelModule } from "@powerhousedao/reactor-drive";
3
+ import { driveDocumentModelModule } from "@powerhousedao/shared/document-drive";
4
+ import {
5
+ headNewDocument,
6
+ selectDocumentModelModule,
7
+ type DocumentModelModule,
8
+ type NewDocumentHeaderOptions,
9
+ type PHDocument,
10
+ } from "@powerhousedao/shared/document-model";
11
+
12
+ const modules = [
13
+ driveDocumentModelModule,
14
+ reactorDriveDocumentModelModule,
15
+ ] as unknown as DocumentModelModule[];
16
+ import { describe, expect, it } from "vitest";
17
+ import { addDefaultDrive, addDefaultReactorDrive } from "../src/utils.mjs";
18
+
19
+ function client(): { client: IReactorClient; created: PHDocument[] } {
20
+ const created: PHDocument[] = [];
21
+ return {
22
+ created,
23
+ client: {
24
+ get: () => Promise.reject(new Error("not found")),
25
+ resolveIdOrSlug: () =>
26
+ Promise.reject(
27
+ Object.assign(new Error("not found"), {
28
+ name: "DocumentNotFoundError",
29
+ }),
30
+ ),
31
+ newDocument: (type: string, options?: NewDocumentHeaderOptions) =>
32
+ Promise.resolve(
33
+ headNewDocument(
34
+ selectDocumentModelModule(modules, type),
35
+ options,
36
+ "v2-required",
37
+ { "base-reducer": 3 },
38
+ ),
39
+ ),
40
+ isDocumentIdTaken: () => Promise.resolve(false),
41
+ write: {
42
+ create: (document: PHDocument) => {
43
+ created.push(document);
44
+ return Promise.resolve({
45
+ readable: Promise.resolve({
46
+ documentId: document.header.id,
47
+ branch: "main",
48
+ }),
49
+ });
50
+ },
51
+ },
52
+ } as unknown as IReactorClient,
53
+ };
54
+ }
55
+
56
+ describe.each([
57
+ ["addDefaultDrive", addDefaultDrive],
58
+ ["addDefaultReactorDrive", addDefaultReactorDrive],
59
+ ])("%s", (_name, add) => {
60
+ it("heads the drive with the client's newDocument", async () => {
61
+ const { client: reactor, created } = client();
62
+
63
+ await add(reactor, { slug: "main", global: { name: "Main" } } as never, 1);
64
+
65
+ expect(created[0].header.protocolVersions).toEqual({
66
+ "base-reducer": 3,
67
+ signature: 2,
68
+ });
69
+ });
70
+
71
+ it("heads a drive with a configured id as legacy at the same versions", async () => {
72
+ const { client: reactor, created } = client();
73
+
74
+ await add(reactor, { id: "fixed", global: { name: "Main" } } as never, 1);
75
+
76
+ expect(created[0].header.id).toBe("fixed");
77
+ expect(created[0].header.protocolVersions).toEqual({ "base-reducer": 3 });
78
+ });
79
+ });
@@ -0,0 +1,265 @@
1
+ import {
2
+ ReactorBuilder,
3
+ ReactorClientBuilder,
4
+ type InProcessReactorClientModule,
5
+ } from "@powerhousedao/reactor";
6
+ import {
7
+ driveDocumentModelModule,
8
+ setDriveName,
9
+ } from "@powerhousedao/shared/document-drive";
10
+ import type { DriveInput } from "@powerhousedao/shared/document-drive";
11
+ import {
12
+ withSignaturePolicy,
13
+ type DocumentModelModule,
14
+ } from "@powerhousedao/shared/document-model";
15
+ import { deleteDocumentAction } from "@powerhousedao/reactor";
16
+ import { documentModelDocumentModelModule } from "document-model";
17
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
18
+ import { addDefaultDrive, ensureDrive } from "../src/utils.mjs";
19
+
20
+ const PORT = 4001;
21
+
22
+ const workflows = (id: string): DriveInput =>
23
+ ({
24
+ id,
25
+ slug: "workflows",
26
+ global: { name: "Workflows" },
27
+ local: {
28
+ availableOffline: true,
29
+ listeners: [],
30
+ sharingType: "public",
31
+ triggers: [],
32
+ },
33
+ }) as DriveInput;
34
+
35
+ describe("addDefaultDrive with a slug another document holds", () => {
36
+ let module: InProcessReactorClientModule;
37
+ const logger = { error: vi.fn() };
38
+
39
+ beforeEach(async () => {
40
+ logger.error.mockClear();
41
+ module = await new ReactorClientBuilder()
42
+ .withReactorBuilder(
43
+ new ReactorBuilder().withDocumentModelSources([
44
+ driveDocumentModelModule as unknown as DocumentModelModule,
45
+ documentModelDocumentModelModule as unknown as DocumentModelModule,
46
+ ]),
47
+ )
48
+ .buildModule();
49
+ });
50
+
51
+ afterEach(() => {
52
+ module.reactor.kill();
53
+ });
54
+
55
+ const isCreated = (id: string) =>
56
+ module.client.get(id).then(
57
+ () => true,
58
+ () => false,
59
+ );
60
+
61
+ it("creates the drive when the slug is free", async () => {
62
+ await expect(
63
+ addDefaultDrive(module.client, workflows("workflows-a"), PORT, logger),
64
+ ).resolves.toBe(`http://localhost:${PORT}/d/workflows-a`);
65
+ await expect(module.client.resolveIdOrSlug("workflows")).resolves.toBe(
66
+ "workflows-a",
67
+ );
68
+ });
69
+
70
+ // A project moved to another folder hashes its drive ids anew.
71
+ it("serves the drive that holds the slug instead of creating a twin", async () => {
72
+ await addDefaultDrive(
73
+ module.client,
74
+ workflows("workflows-a"),
75
+ PORT,
76
+ logger,
77
+ );
78
+ await expect(
79
+ addDefaultDrive(module.client, workflows("workflows-b"), PORT, logger),
80
+ ).resolves.toBe(`http://localhost:${PORT}/d/workflows-a`);
81
+ expect(await isCreated("workflows-b")).toBe(false);
82
+ expect(logger.error).not.toHaveBeenCalled();
83
+ });
84
+
85
+ it("logs an error and creates nothing when another kind of document holds it", async () => {
86
+ const other = withSignaturePolicy(
87
+ documentModelDocumentModelModule.utils.createDocument(),
88
+ "legacy",
89
+ );
90
+ other.header.slug = "workflows";
91
+ await module.client.create(other);
92
+
93
+ await expect(
94
+ addDefaultDrive(module.client, workflows("workflows-c"), PORT, logger),
95
+ ).resolves.toBeUndefined();
96
+ expect(await isCreated("workflows-c")).toBe(false);
97
+ expect(logger.error).toHaveBeenCalledWith(
98
+ `The default drive "workflows" was not created: the slug belongs to powerhouse/document-model document ${other.header.id}.`,
99
+ );
100
+ });
101
+ });
102
+
103
+ describe.each([
104
+ ["without document decisions", false],
105
+ ["with document decisions", true],
106
+ ])("ensureDrive %s", (_name, documentDecisions) => {
107
+ let module: InProcessReactorClientModule;
108
+ const logger = { error: vi.fn() };
109
+ const mail = { slug: "mail", global: { name: "Mail" } };
110
+
111
+ const deleteDrive = async (id: string) => {
112
+ await (
113
+ await module.client.write.deleteDocument(id)
114
+ ).readable;
115
+ };
116
+
117
+ beforeEach(async () => {
118
+ logger.error.mockClear();
119
+ module = await new ReactorClientBuilder()
120
+ .withCreateSignaturePolicy("legacy")
121
+ .withReactorBuilder(
122
+ new ReactorBuilder()
123
+ .withDocumentModelSources([
124
+ driveDocumentModelModule as unknown as DocumentModelModule,
125
+ documentModelDocumentModelModule as unknown as DocumentModelModule,
126
+ ])
127
+ .withExecutorConfig({ featureFlags: { documentDecisions } }),
128
+ )
129
+ .buildModule();
130
+ });
131
+
132
+ afterEach(() => {
133
+ module.reactor.kill();
134
+ });
135
+
136
+ it("generates the id of a drive declared by its slug alone", async () => {
137
+ await ensureDrive(module.client, mail, PORT, logger);
138
+
139
+ const id = await module.client.resolveIdOrSlug("mail");
140
+ expect(id).not.toBe("mail");
141
+ const drive = await module.client.get(id);
142
+ expect(drive.header.slug).toBe("mail");
143
+ expect(drive.header.documentType).toBe("powerhouse/document-drive");
144
+ });
145
+
146
+ // A random id can't clash, so a second boot must find the drive by its slug.
147
+ it("finds that drive again on the next boot instead of making a twin", async () => {
148
+ await ensureDrive(module.client, mail, PORT, logger);
149
+ const first = await module.client.resolveIdOrSlug("mail");
150
+
151
+ await ensureDrive(module.client, mail, PORT, logger);
152
+
153
+ const { results } = await module.client.find({
154
+ type: "powerhouse/document-drive",
155
+ });
156
+ expect(results.map((drive) => drive.header.id)).toEqual([first]);
157
+ });
158
+
159
+ it("leaves a drive of another type under the slug as it is", async () => {
160
+ await ensureDrive(module.client, mail, PORT, logger);
161
+ const id = await module.client.resolveIdOrSlug("mail");
162
+
163
+ await expect(
164
+ ensureDrive(
165
+ module.client,
166
+ { ...mail, documentType: "powerhouse/reactor-drive" },
167
+ PORT,
168
+ logger,
169
+ ),
170
+ ).resolves.toBe(`http://localhost:${PORT}/d/${id}`);
171
+ expect(logger.error).not.toHaveBeenCalled();
172
+ });
173
+
174
+ it("finds a drive whose id equals its slug, as older stores hold them", async () => {
175
+ await ensureDrive(module.client, { ...mail, id: "mail" }, PORT, logger);
176
+
177
+ await expect(ensureDrive(module.client, mail, PORT, logger)).resolves.toBe(
178
+ `http://localhost:${PORT}/d/mail`,
179
+ );
180
+ await expect(module.client.resolveIdOrSlug("mail")).resolves.toBe("mail");
181
+ });
182
+
183
+ it("makes a new empty drive after the drive under the slug was deleted", async () => {
184
+ await ensureDrive(module.client, mail, PORT, logger);
185
+ const first = await module.client.resolveIdOrSlug("mail");
186
+ await deleteDrive(first);
187
+
188
+ await ensureDrive(module.client, mail, PORT, logger);
189
+
190
+ const second = await module.client.resolveIdOrSlug("mail");
191
+ expect(second).not.toBe(first);
192
+ expect(logger.error).not.toHaveBeenCalled();
193
+ });
194
+
195
+ // Stores from before generated ids: the deleted drive's id is the slug.
196
+ it("points the slug at the new drive when the deleted drive's id was that slug", async () => {
197
+ await ensureDrive(module.client, { ...mail, id: "mail" }, PORT, logger);
198
+ await deleteDrive("mail");
199
+
200
+ await ensureDrive(module.client, mail, PORT, logger);
201
+
202
+ const id = await module.client.resolveIdOrSlug("mail");
203
+ expect(id).not.toBe("mail");
204
+ await expect(module.client.get("mail")).resolves.toMatchObject({
205
+ header: { id },
206
+ });
207
+ // Writes resolve through the identity table, reads through the view
208
+ const renamed = await module.client.execute("mail", "main", [
209
+ setDriveName({ name: "Renamed" }),
210
+ ]);
211
+ expect(renamed.header.id).toBe(id);
212
+ });
213
+
214
+ // A project-hashed drive declares an id, and that id outlives the drive.
215
+ it.each([
216
+ ["a different slug", "mail", "mail-1234abcd"],
217
+ ["the same slug", "mail-1234abcd", "mail-1234abcd"],
218
+ ])(
219
+ "makes a new drive when a deleted drive holds the declared id, under %s",
220
+ async (_case, slug, id) => {
221
+ const declared = { ...mail, slug, id };
222
+ await ensureDrive(module.client, declared, PORT, logger);
223
+ await deleteDrive(id);
224
+
225
+ await expect(
226
+ ensureDrive(module.client, declared, PORT, logger),
227
+ ).resolves.toBe(`http://localhost:${PORT}/d/${slug}`);
228
+
229
+ const created = await module.client.resolveIdOrSlug(slug);
230
+ expect(created).not.toBe(id);
231
+
232
+ await ensureDrive(module.client, declared, PORT, logger);
233
+ await expect(module.client.resolveIdOrSlug(slug)).resolves.toBe(created);
234
+ expect(logger.error).not.toHaveBeenCalled();
235
+ },
236
+ );
237
+
238
+ it("leaves a drive deleted when the delete kept its slug", async () => {
239
+ await ensureDrive(module.client, mail, PORT, logger);
240
+ const id = await module.client.resolveIdOrSlug("mail");
241
+ await (
242
+ await module.client.write.execute(id, "main", [
243
+ deleteDocumentAction(id, { keepSlug: true }),
244
+ ])
245
+ ).readable;
246
+
247
+ await expect(
248
+ ensureDrive(module.client, mail, PORT, logger),
249
+ ).resolves.toBeUndefined();
250
+
251
+ const { results } = await module.client.find({
252
+ type: "powerhouse/document-drive",
253
+ });
254
+ expect(results).toEqual([]);
255
+ expect(logger.error).toHaveBeenCalledWith(
256
+ `The default drive "mail" was not created: it was deleted, and its slug kept.`,
257
+ );
258
+ });
259
+
260
+ it("refuses a drive with neither a slug nor an id", async () => {
261
+ await expect(
262
+ ensureDrive(module.client, { global: { name: "x" } }, PORT, logger),
263
+ ).rejects.toThrow("A drive needs a slug");
264
+ });
265
+ });