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

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 (180) 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/durable-node-fs-pg16.test.d.ts +2 -0
  34. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
  35. package/.tsbuild/test/fatal-shutdown.test.d.ts +2 -0
  36. package/.tsbuild/test/fatal-shutdown.test.d.ts.map +1 -0
  37. package/.tsbuild/test/inspection-flag.test.d.ts +2 -0
  38. package/.tsbuild/test/inspection-flag.test.d.ts.map +1 -0
  39. package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
  40. package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
  41. package/.tsbuild/test/mcp-flag.test.d.ts +2 -0
  42. package/.tsbuild/test/mcp-flag.test.d.ts.map +1 -0
  43. package/.tsbuild/test/observability-signals.test.d.ts +2 -0
  44. package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
  45. package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
  46. package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
  47. package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
  48. package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
  49. package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
  50. package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
  51. package/.tsbuild/test/renown-trust-policy.test.d.ts +2 -0
  52. package/.tsbuild/test/renown-trust-policy.test.d.ts.map +1 -0
  53. package/.tsbuild/test/snapshot-writer.d.ts +18 -0
  54. package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
  55. package/.tsbuild/test/unsupported-stored-documents.test.d.ts +2 -0
  56. package/.tsbuild/test/unsupported-stored-documents.test.d.ts.map +1 -0
  57. package/.tsbuild/test/workflow/harness.d.ts +56 -0
  58. package/.tsbuild/test/workflow/harness.d.ts.map +1 -0
  59. package/.tsbuild/test/workflow/oauth.test.d.ts +2 -0
  60. package/.tsbuild/test/workflow/oauth.test.d.ts.map +1 -0
  61. package/.tsbuild/test/workflow/resolvers.test.d.ts +2 -0
  62. package/.tsbuild/test/workflow/resolvers.test.d.ts.map +1 -0
  63. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts +2 -0
  64. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts.map +1 -0
  65. package/.tsbuild/test/workflow/webhook-signed.test.d.ts +2 -0
  66. package/.tsbuild/test/workflow/webhook-signed.test.d.ts.map +1 -0
  67. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts +2 -0
  68. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts.map +1 -0
  69. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts +2 -0
  70. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts.map +1 -0
  71. package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
  72. package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
  73. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
  74. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
  75. package/.tsbuild/test/workflow-read-gate.test.d.ts +2 -0
  76. package/.tsbuild/test/workflow-read-gate.test.d.ts.map +1 -0
  77. package/.tsbuild/test/workflow-runtime.test.d.ts +2 -0
  78. package/.tsbuild/test/workflow-runtime.test.d.ts.map +1 -0
  79. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  80. package/.tsbuild/tsdown.config.d.ts.map +1 -1
  81. package/.tsbuild/vitest.config.d.ts.map +1 -1
  82. package/Auth.md +144 -2
  83. package/CHANGELOG.md +842 -0
  84. package/README.md +47 -10
  85. package/dist/index.d.mts +2 -1
  86. package/dist/index.mjs +50 -50
  87. package/dist/index.mjs.map +1 -1
  88. package/dist/install-packages.d.mts +2 -1
  89. package/dist/install-packages.mjs +2 -2
  90. package/dist/install-packages.mjs.map +1 -1
  91. package/dist/mcp-cNWW82NH.mjs +27 -0
  92. package/dist/mcp-cNWW82NH.mjs.map +1 -0
  93. package/dist/migrate.d.mts +2 -1
  94. package/dist/migrate.mjs +4 -3
  95. package/dist/migrate.mjs.map +1 -1
  96. package/dist/plugin-host-CGO4XZzK.d.mts +38 -0
  97. package/dist/plugin-host-CGO4XZzK.d.mts.map +1 -0
  98. package/dist/plugins/attachments.d.mts +23 -0
  99. package/dist/plugins/attachments.d.mts.map +1 -0
  100. package/dist/plugins/attachments.mjs +4 -0
  101. package/dist/plugins/mcp.d.mts +21 -0
  102. package/dist/plugins/mcp.d.mts.map +1 -0
  103. package/dist/plugins/mcp.mjs +4 -0
  104. package/dist/plugins/privacy.d.mts +12 -0
  105. package/dist/plugins/privacy.d.mts.map +1 -0
  106. package/dist/plugins/privacy.mjs +4 -0
  107. package/dist/plugins/workflows.d.mts +36 -0
  108. package/dist/plugins/workflows.d.mts.map +1 -0
  109. package/dist/plugins/workflows.mjs +4 -0
  110. package/dist/privacy-C8KVimKF.mjs +113 -0
  111. package/dist/privacy-C8KVimKF.mjs.map +1 -0
  112. package/dist/{server-CiKkVE6g.mjs → server-CF_mjyJi.mjs} +1170 -228
  113. package/dist/server-CF_mjyJi.mjs.map +1 -0
  114. package/dist/server.d.mts +190 -30
  115. package/dist/server.d.mts.map +1 -1
  116. package/dist/server.mjs +4 -5
  117. package/dist/{utils-Baw7rThP.mjs → utils-uaBlzyjS.mjs} +15 -9
  118. package/dist/utils-uaBlzyjS.mjs.map +1 -0
  119. package/dist/utils.d.mts +4 -5
  120. package/dist/utils.d.mts.map +1 -1
  121. package/dist/utils.mjs +3 -3
  122. package/dist/workflow-runtime-7GaJ13c9.d.mts +30 -0
  123. package/dist/workflow-runtime-7GaJ13c9.d.mts.map +1 -0
  124. package/dist/workflows-qKtqZhn3.mjs +1338 -0
  125. package/dist/workflows-qKtqZhn3.mjs.map +1 -0
  126. package/package.json +49 -20
  127. package/scripts/install-packages.sh +0 -0
  128. package/test/attachment-reference-read-model.test.ts +6 -1
  129. package/test/attachments/auth.test.ts +85 -0
  130. package/test/attachments/byte-grants.test.ts +290 -0
  131. package/test/attachments/byte-routes-read-gate.integration.test.ts +314 -0
  132. package/test/attachments/download-target.test.ts +201 -32
  133. package/test/attachments/index.test.ts +47 -4
  134. package/test/attachments/reserve-metadata-parity.test.ts +96 -0
  135. package/test/attachments/routes-integration.test.ts +60 -4
  136. package/test/attachments/routes.test.ts +271 -24
  137. package/test/attachments/service-config.test.ts +13 -3
  138. package/test/attachments/url-signer.test.ts +105 -0
  139. package/test/boot-create-signature-policy.test.ts +108 -0
  140. package/test/boot-inspection-enabled.test.ts +150 -0
  141. package/test/boot-mcp-enabled.test.ts +130 -0
  142. package/test/boot-privacy.test.ts +243 -0
  143. package/test/boot-unsupported-stored-documents.test.ts +213 -0
  144. package/test/boot-without-renown.test.ts +81 -0
  145. package/test/boot-without-require-signatures.test.ts +74 -0
  146. package/test/boot-worker-trust-policy.test.ts +90 -0
  147. package/test/boot-workflow-singleton-conflict.test.ts +107 -0
  148. package/test/boot-workflow-singleton-retry.test.ts +133 -0
  149. package/test/builder-defaults.test.ts +80 -0
  150. package/test/create-signature-policy.test.ts +69 -0
  151. package/test/durable-node-fs-pg16.test.ts +80 -0
  152. package/test/fatal-shutdown.test.ts +165 -0
  153. package/test/inspection-flag.test.ts +62 -0
  154. package/test/inspection-throttle.test.ts +23 -0
  155. package/test/mcp-flag.test.ts +56 -0
  156. package/test/observability-signals.test.ts +131 -0
  157. package/test/pglite-dialect.test.ts +217 -2
  158. package/test/pglite-preflight.test.ts +313 -0
  159. package/test/pglite-storage-dirs.test.ts +103 -0
  160. package/test/pglite-version.test.ts +9 -1
  161. package/test/plugin-presence.test.ts +269 -0
  162. package/test/renown-trust-policy.test.ts +315 -0
  163. package/test/snapshot-writer.ts +94 -0
  164. package/test/unsupported-stored-documents.test.ts +72 -0
  165. package/test/worker-pool.test.ts +27 -1
  166. package/test/workflow/harness.ts +289 -0
  167. package/test/workflow/oauth.test.ts +214 -0
  168. package/test/workflow/resolvers.test.ts +357 -0
  169. package/test/workflow/webhook-dedupe.test.ts +295 -0
  170. package/test/workflow/webhook-signed.test.ts +509 -0
  171. package/test/workflow/webhook-trigger-test.test.ts +82 -0
  172. package/test/workflow/webhook-unsigned.test.ts +345 -0
  173. package/test/workflow-reactor-access.test.ts +384 -0
  174. package/test/workflow-reactor-enforcement.test.ts +808 -0
  175. package/test/workflow-read-gate.test.ts +213 -0
  176. package/test/workflow-runtime.test.ts +1122 -0
  177. package/tsconfig.json +11 -2
  178. package/tsdown.config.ts +9 -1
  179. package/dist/server-CiKkVE6g.mjs.map +0 -1
  180. package/dist/utils-Baw7rThP.mjs.map +0 -1
@@ -0,0 +1,105 @@
1
+ import type { AttachmentHash } from "@powerhousedao/reactor";
2
+ import { describe, expect, it } from "vitest";
3
+ import {
4
+ ATTACHMENT_URL_SIGNING_SECRET_ENV,
5
+ AttachmentUrlSigner,
6
+ resolveAttachmentUrlSigning,
7
+ } from "../../src/attachments/url-signer.js";
8
+
9
+ const HASH = "a".repeat(64) as AttachmentHash;
10
+ const OTHER_HASH = "b".repeat(64) as AttachmentHash;
11
+ const SECRET = "k".repeat(32);
12
+
13
+ function signed(now: number, ttl = 300) {
14
+ const signer = new AttachmentUrlSigner(SECRET, () => now);
15
+ const { query, expiresAtUtc } = signer.sign(HASH, "doc-1", ttl);
16
+ const params = new URLSearchParams(query);
17
+ return {
18
+ signer,
19
+ expiresAtUtc,
20
+ documentId: params.get("documentId")!,
21
+ expires: params.get("expires")!,
22
+ signature: params.get("signature")!,
23
+ };
24
+ }
25
+
26
+ describe("AttachmentUrlSigner", () => {
27
+ const NOW = Date.parse("2026-09-24T00:00:00.000Z");
28
+
29
+ it("verifies what it signed until the expiry", () => {
30
+ const { signer, expiresAtUtc, documentId, expires, signature } =
31
+ signed(NOW);
32
+
33
+ expect(documentId).toBe("doc-1");
34
+ expect(expiresAtUtc).toBe(new Date(NOW + 300_000).toISOString());
35
+ expect(signer.verify(HASH, documentId, expires, signature)).toBe(true);
36
+ });
37
+
38
+ it("refuses at and after the expiry", () => {
39
+ const { expires, signature } = signed(NOW);
40
+ const at = new AttachmentUrlSigner(SECRET, () => NOW + 300_000);
41
+
42
+ expect(at.verify(HASH, "doc-1", expires, signature)).toBe(false);
43
+ });
44
+
45
+ it("refuses a different hash, document, expiry or secret", () => {
46
+ const { signer, expires, signature } = signed(NOW);
47
+
48
+ expect(signer.verify(OTHER_HASH, "doc-1", expires, signature)).toBe(false);
49
+ expect(signer.verify(HASH, "doc-2", expires, signature)).toBe(false);
50
+ expect(
51
+ signer.verify(HASH, "doc-1", String(Number(expires) + 1), signature),
52
+ ).toBe(false);
53
+ expect(
54
+ new AttachmentUrlSigner("x".repeat(32), () => NOW).verify(
55
+ HASH,
56
+ "doc-1",
57
+ expires,
58
+ signature,
59
+ ),
60
+ ).toBe(false);
61
+ });
62
+
63
+ it("refuses malformed expiry and signature values", () => {
64
+ const { signer, expires, signature } = signed(NOW);
65
+
66
+ expect(signer.verify(HASH, "doc-1", `${expires}.0`, signature)).toBe(false);
67
+ expect(signer.verify(HASH, "doc-1", expires, `${signature}=`)).toBe(false);
68
+ expect(signer.verify(HASH, "doc-1", expires, "")).toBe(false);
69
+ });
70
+ });
71
+
72
+ describe("resolveAttachmentUrlSigning", () => {
73
+ it("uses a configured secret", () => {
74
+ const signing = resolveAttachmentUrlSigning({
75
+ [ATTACHMENT_URL_SIGNING_SECRET_ENV]: SECRET,
76
+ NODE_ENV: "production",
77
+ });
78
+ expect(signing.status).toBe("configured");
79
+ });
80
+
81
+ it("fails closed in production when unset", () => {
82
+ expect(resolveAttachmentUrlSigning({ NODE_ENV: "production" })).toEqual({
83
+ status: "unconfigured",
84
+ });
85
+ });
86
+
87
+ it.each([undefined, "development", "test"])(
88
+ "uses a per-process secret when unset and NODE_ENV is %s",
89
+ (nodeEnv) => {
90
+ const signing = resolveAttachmentUrlSigning({ NODE_ENV: nodeEnv });
91
+ expect(signing.status).toBe("ephemeral");
92
+ },
93
+ );
94
+
95
+ it.each(["short", ` ${"k".repeat(32)}`, ""])(
96
+ "refuses to boot on an unusable secret %j",
97
+ (secret) => {
98
+ expect(() =>
99
+ resolveAttachmentUrlSigning({
100
+ [ATTACHMENT_URL_SIGNING_SECRET_ENV]: secret,
101
+ }),
102
+ ).toThrow(ATTACHMENT_URL_SIGNING_SECRET_ENV);
103
+ },
104
+ );
105
+ });
@@ -0,0 +1,108 @@
1
+ import {
2
+ hasDerivedDocumentId,
3
+ isDerivedDocumentId,
4
+ signaturePolicyOf,
5
+ } from "@powerhousedao/shared/document-model";
6
+ import { mkdtemp, rm } from "node:fs/promises";
7
+ import { tmpdir } from "node:os";
8
+ import { join } from "node:path";
9
+ import type { ILogger } from "document-model";
10
+ import { describe, expect, it, vi } from "vitest";
11
+ import { startSwitchboard } from "../src/server.mjs";
12
+
13
+ function stubLogger(): ILogger {
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;
24
+ }
25
+
26
+ async function withSwitchboard(
27
+ env: Record<string, string>,
28
+ run: (
29
+ switchboard: Awaited<ReturnType<typeof startSwitchboard>>,
30
+ ) => Promise<void>,
31
+ ): Promise<void> {
32
+ const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-create-policy-"));
33
+ const names = ["PH_REACTOR_DATABASE_URL", ...Object.keys(env)];
34
+ const previous = names.map((name) => [name, process.env[name]] as const);
35
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
36
+ Object.assign(process.env, env);
37
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
38
+
39
+ try {
40
+ switchboard = await startSwitchboard({
41
+ workflows: { enabled: false },
42
+ dbPath: join(tempRoot, "read-model"),
43
+ port: 0,
44
+ mcp: false,
45
+ disableLocalPackages: true,
46
+ identity: { keypairPath: join(tempRoot, "identity.json") },
47
+ drive: {
48
+ slug: "default-drive",
49
+ global: { name: "Default Drive" },
50
+ },
51
+ logger: stubLogger(),
52
+ });
53
+ await run(switchboard);
54
+ await switchboard.shutdown();
55
+ switchboard = undefined;
56
+ } finally {
57
+ await switchboard?.shutdown();
58
+ for (const [name, value] of previous) {
59
+ if (value === undefined) {
60
+ delete process.env[name];
61
+ } else {
62
+ process.env[name] = value;
63
+ }
64
+ }
65
+ await rm(tempRoot, { recursive: true, force: true });
66
+ }
67
+ }
68
+
69
+ describe("booting Switchboard", () => {
70
+ it("creates v2-required documents and drives by default", async () => {
71
+ await withSwitchboard({}, async ({ reactor }) => {
72
+ const document = await reactor.createEmpty("powerhouse/document-model");
73
+ expect(signaturePolicyOf(document.header)).toBe("v2-required");
74
+ expect(hasDerivedDocumentId(document.header)).toBe(true);
75
+
76
+ const drive = await reactor.drives.create({ global: { name: "Drive" } });
77
+ expect(signaturePolicyOf(drive.header)).toBe("v2-required");
78
+
79
+ const defaultDrive = await reactor.get("default-drive");
80
+ expect(signaturePolicyOf(defaultDrive.header)).toBe("v2-required");
81
+ });
82
+ }, 60_000);
83
+
84
+ it("creates legacy documents and drives under CREATE_SIGNATURE_POLICY=legacy", async () => {
85
+ await withSwitchboard(
86
+ { CREATE_SIGNATURE_POLICY: "legacy" },
87
+ async ({ reactor }) => {
88
+ const document = await reactor.createEmpty("powerhouse/document-model");
89
+ expect(signaturePolicyOf(document.header)).toBe("legacy");
90
+ expect(isDerivedDocumentId(document.header.id)).toBe(false);
91
+
92
+ const drive = await reactor.drives.create({
93
+ global: { name: "Drive" },
94
+ });
95
+ expect(signaturePolicyOf(drive.header)).toBe("legacy");
96
+ expect(isDerivedDocumentId(drive.header.id)).toBe(false);
97
+
98
+ const defaultDrive = await reactor.get("default-drive");
99
+ expect(signaturePolicyOf(defaultDrive.header)).toBe("legacy");
100
+
101
+ const asked = await reactor.createEmpty("powerhouse/document-model", {
102
+ signaturePolicy: "v2-required",
103
+ });
104
+ expect(hasDerivedDocumentId(asked.header)).toBe(true);
105
+ },
106
+ );
107
+ }, 60_000);
108
+ });
@@ -0,0 +1,150 @@
1
+ import { mkdtemp, rm } from "node:fs/promises";
2
+ import { createServer } from "node:net";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import type { ILogger } from "document-model";
6
+ import { describe, expect, it, vi } from "vitest";
7
+ import { startSwitchboard } from "../src/server.mjs";
8
+
9
+ // `port: 0` would bind a random port the switchboard does not report back, so
10
+ // the test picks a free one and pins it.
11
+ async function freePort(): Promise<number> {
12
+ const server = createServer();
13
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
14
+ const address = server.address();
15
+ await new Promise<void>((resolve) => server.close(() => resolve()));
16
+ if (!address || typeof address === "string") throw new Error("no port");
17
+ return address.port;
18
+ }
19
+
20
+ function stubLogger(): ILogger {
21
+ const logger = {
22
+ verbose: vi.fn(),
23
+ debug: vi.fn(),
24
+ info: vi.fn(),
25
+ warn: vi.fn(),
26
+ error: vi.fn(),
27
+ child: vi.fn(),
28
+ };
29
+ logger.child.mockReturnValue(logger);
30
+ return logger as unknown as ILogger;
31
+ }
32
+
33
+ async function withSwitchboard(
34
+ env: Record<string, string>,
35
+ inspection: boolean | undefined,
36
+ run: (
37
+ switchboard: Awaited<ReturnType<typeof startSwitchboard>>,
38
+ ) => Promise<void>,
39
+ ): Promise<void> {
40
+ const tempRoot = await mkdtemp(
41
+ join(tmpdir(), "switchboard-inspection-enabled-"),
42
+ );
43
+ const names = [
44
+ "PH_REACTOR_DATABASE_URL",
45
+ "INSPECTION_ENABLED",
46
+ ...Object.keys(env),
47
+ ];
48
+ const previous = names.map((name) => [name, process.env[name]] as const);
49
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
50
+ delete process.env.INSPECTION_ENABLED;
51
+ Object.assign(process.env, env);
52
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
53
+
54
+ try {
55
+ switchboard = await startSwitchboard({
56
+ workflows: { enabled: false },
57
+ dbPath: join(tempRoot, "read-model"),
58
+ port: await freePort(),
59
+ strictPort: true,
60
+ inspection,
61
+ disableLocalPackages: true,
62
+ identity: { keypairPath: join(tempRoot, "identity.json") },
63
+ drive: {
64
+ slug: "default-drive",
65
+ global: { name: "Default Drive" },
66
+ },
67
+ logger: stubLogger(),
68
+ });
69
+ await run(switchboard);
70
+ await switchboard.shutdown();
71
+ switchboard = undefined;
72
+ } finally {
73
+ await switchboard?.shutdown();
74
+ for (const [name, value] of previous) {
75
+ if (value === undefined) {
76
+ delete process.env[name];
77
+ } else {
78
+ process.env[name] = value;
79
+ }
80
+ }
81
+ await rm(tempRoot, { recursive: true, force: true });
82
+ }
83
+ }
84
+
85
+ const query = `{ inspection { info { workflows } } }`;
86
+
87
+ async function postQuery(
88
+ port: number,
89
+ path: string,
90
+ ): Promise<{ status: number; body: unknown }> {
91
+ const response = await fetch(`http://127.0.0.1:${port}${path}`, {
92
+ method: "POST",
93
+ headers: { "content-type": "application/json" },
94
+ body: JSON.stringify({ query }),
95
+ });
96
+ const text = await response.text();
97
+ let body: unknown = text;
98
+ try {
99
+ body = JSON.parse(text);
100
+ } catch {
101
+ // Not JSON; keep the raw text.
102
+ }
103
+ return { status: response.status, body };
104
+ }
105
+
106
+ describe("booting Switchboard", () => {
107
+ it("mounts /graphql/inspection by default", async () => {
108
+ await withSwitchboard(
109
+ {},
110
+ undefined,
111
+ async ({ inspectionEnabled, port }) => {
112
+ expect(inspectionEnabled).toBe(true);
113
+ const { status, body } = await postQuery(port, "/graphql/inspection");
114
+ expect(status).toBe(200);
115
+ expect(body).toMatchObject({
116
+ data: { inspection: { info: { workflows: false } } },
117
+ });
118
+ },
119
+ );
120
+ }, 60_000);
121
+
122
+ it('leaves /graphql/inspection unmounted under INSPECTION_ENABLED="false"', async () => {
123
+ await withSwitchboard(
124
+ { INSPECTION_ENABLED: "false" },
125
+ undefined,
126
+ async ({ inspectionEnabled, port }) => {
127
+ expect(inspectionEnabled).toBe(false);
128
+ expect((await postQuery(port, "/graphql/inspection")).status).toBe(404);
129
+ const supergraph = await postQuery(port, "/graphql");
130
+ const { errors } = supergraph.body as {
131
+ errors?: { message: string }[];
132
+ };
133
+ expect(errors?.[0]?.message).toContain(
134
+ 'Cannot query field "inspection"',
135
+ );
136
+ },
137
+ );
138
+ }, 60_000);
139
+
140
+ it("keeps a host's inspection: false over INSPECTION_ENABLED=true", async () => {
141
+ await withSwitchboard(
142
+ { INSPECTION_ENABLED: "true" },
143
+ false,
144
+ async ({ inspectionEnabled, port }) => {
145
+ expect(inspectionEnabled).toBe(false);
146
+ expect((await postQuery(port, "/graphql/inspection")).status).toBe(404);
147
+ },
148
+ );
149
+ }, 60_000);
150
+ });
@@ -0,0 +1,130 @@
1
+ import { mkdtemp, rm } from "node:fs/promises";
2
+ import { createServer } from "node:net";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import type { ILogger } from "document-model";
6
+ import { describe, expect, it, vi } from "vitest";
7
+ import { startSwitchboard } from "../src/server.mjs";
8
+
9
+ // `port: 0` would bind a random port the switchboard does not report back, so
10
+ // the test picks a free one and pins it.
11
+ async function freePort(): Promise<number> {
12
+ const server = createServer();
13
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
14
+ const address = server.address();
15
+ await new Promise<void>((resolve) => server.close(() => resolve()));
16
+ if (!address || typeof address === "string") throw new Error("no port");
17
+ return address.port;
18
+ }
19
+
20
+ function stubLogger(): ILogger {
21
+ const logger = {
22
+ verbose: vi.fn(),
23
+ debug: vi.fn(),
24
+ info: vi.fn(),
25
+ warn: vi.fn(),
26
+ error: vi.fn(),
27
+ child: vi.fn(),
28
+ };
29
+ logger.child.mockReturnValue(logger);
30
+ return logger as unknown as ILogger;
31
+ }
32
+
33
+ async function withSwitchboard(
34
+ env: Record<string, string>,
35
+ mcp: boolean | undefined,
36
+ run: (
37
+ switchboard: Awaited<ReturnType<typeof startSwitchboard>>,
38
+ ) => Promise<void>,
39
+ ): Promise<void> {
40
+ const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-mcp-enabled-"));
41
+ const names = [
42
+ "PH_REACTOR_DATABASE_URL",
43
+ "MCP_ENABLED",
44
+ // The env-flag semantics under test are the CLASSIC path's: flag-on, the
45
+ // mcp plugin owns options.mcp and MCP_ENABLED is deliberately inert
46
+ // (presence in the composition is the switch — rewire plan R2.2).
47
+ "FEATURE_PLUGIN_HOST_ENABLED",
48
+ ...Object.keys(env),
49
+ ];
50
+ const previous = names.map((name) => [name, process.env[name]] as const);
51
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
52
+ delete process.env.MCP_ENABLED;
53
+ delete process.env.FEATURE_PLUGIN_HOST_ENABLED;
54
+ Object.assign(process.env, env);
55
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
56
+
57
+ try {
58
+ switchboard = await startSwitchboard({
59
+ workflows: { enabled: false },
60
+ dbPath: join(tempRoot, "read-model"),
61
+ port: await freePort(),
62
+ strictPort: true,
63
+ mcp,
64
+ disableLocalPackages: true,
65
+ identity: { keypairPath: join(tempRoot, "identity.json") },
66
+ drive: {
67
+ slug: "default-drive",
68
+ global: { name: "Default Drive" },
69
+ },
70
+ logger: stubLogger(),
71
+ });
72
+ await run(switchboard);
73
+ await switchboard.shutdown();
74
+ switchboard = undefined;
75
+ } finally {
76
+ await switchboard?.shutdown();
77
+ for (const [name, value] of previous) {
78
+ if (value === undefined) {
79
+ delete process.env[name];
80
+ } else {
81
+ process.env[name] = value;
82
+ }
83
+ }
84
+ await rm(tempRoot, { recursive: true, force: true });
85
+ }
86
+ }
87
+
88
+ async function postMcp(port: number): Promise<number> {
89
+ const response = await fetch(`http://127.0.0.1:${port}/mcp`, {
90
+ method: "POST",
91
+ headers: {
92
+ "content-type": "application/json",
93
+ accept: "application/json, text/event-stream",
94
+ },
95
+ body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "ping" }),
96
+ });
97
+ await response.body?.cancel();
98
+ return response.status;
99
+ }
100
+
101
+ describe("booting Switchboard", () => {
102
+ it("mounts /mcp by default", async () => {
103
+ await withSwitchboard({}, undefined, async ({ mcpEnabled, port }) => {
104
+ expect(mcpEnabled).toBe(true);
105
+ expect(await postMcp(port)).not.toBe(404);
106
+ });
107
+ }, 60_000);
108
+
109
+ it('leaves /mcp unmounted under MCP_ENABLED="false"', async () => {
110
+ await withSwitchboard(
111
+ { MCP_ENABLED: "false" },
112
+ undefined,
113
+ async ({ mcpEnabled, port }) => {
114
+ expect(mcpEnabled).toBe(false);
115
+ expect(await postMcp(port)).toBe(404);
116
+ },
117
+ );
118
+ }, 60_000);
119
+
120
+ it("keeps a host's mcp: false over MCP_ENABLED=true", async () => {
121
+ await withSwitchboard(
122
+ { MCP_ENABLED: "true" },
123
+ false,
124
+ async ({ mcpEnabled, port }) => {
125
+ expect(mcpEnabled).toBe(false);
126
+ expect(await postMcp(port)).toBe(404);
127
+ },
128
+ );
129
+ }, 60_000);
130
+ });