@powerhousedao/switchboard 6.2.3-dev.77 → 6.2.3-dev.79

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 (110) hide show
  1. package/.tsbuild/test/boot-auth-config.test.d.ts +2 -0
  2. package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
  3. package/.tsbuild/test/boot-helpers.d.ts +5 -0
  4. package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
  5. package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
  6. package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
  7. package/.tsbuild/test/config-from-env.test.d.ts +2 -0
  8. package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
  9. package/.tsbuild/test/config-merge.test.d.ts +2 -0
  10. package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
  11. package/.tsbuild/test/config-schema.test.d.ts +2 -0
  12. package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
  13. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
  14. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
  15. package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
  16. package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
  17. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  18. package/CHANGELOG.md +56 -0
  19. package/README.md +9 -5
  20. package/dist/boot.d.mts +8 -0
  21. package/dist/boot.d.mts.map +1 -1
  22. package/dist/boot.mjs +22 -16
  23. package/dist/boot.mjs.map +1 -1
  24. package/dist/config/from-env.d.mts +15 -0
  25. package/dist/config/from-env.d.mts.map +1 -0
  26. package/dist/config/from-env.mjs +4 -0
  27. package/dist/config/schema.d.mts +3 -0
  28. package/dist/config/schema.mjs +4 -0
  29. package/dist/from-env-BO0n9R02.mjs +401 -0
  30. package/dist/from-env-BO0n9R02.mjs.map +1 -0
  31. package/dist/index.mjs +34 -38
  32. package/dist/index.mjs.map +1 -1
  33. package/dist/migrate.mjs +2 -2
  34. package/dist/{plugin-host-BBqojyyY.d.mts → plugin-host-C1sYEm8g.d.mts} +4 -1
  35. package/dist/plugin-host-C1sYEm8g.d.mts.map +1 -0
  36. package/dist/plugins/attachments.d.mts +3 -3
  37. package/dist/plugins/attachments.mjs +3 -3
  38. package/dist/plugins/mcp.d.mts +1 -1
  39. package/dist/plugins/privacy.d.mts +1 -1
  40. package/dist/plugins/privacy.mjs +3 -3
  41. package/dist/plugins/workflows.d.mts +1 -1
  42. package/dist/plugins/workflows.mjs +3 -3
  43. package/dist/privacy-BO8Q25xI.mjs +44 -0
  44. package/dist/privacy-BO8Q25xI.mjs.map +1 -0
  45. package/dist/{privacy-BsFl7GzK.mjs → privacy-DxIJGH4P.mjs} +14 -41
  46. package/dist/privacy-DxIJGH4P.mjs.map +1 -0
  47. package/dist/renown-layers-COZsrGQT.mjs +40 -0
  48. package/dist/renown-layers-COZsrGQT.mjs.map +1 -0
  49. package/dist/schema-0g_QdOWn.mjs +267 -0
  50. package/dist/schema-0g_QdOWn.mjs.map +1 -0
  51. package/dist/schema-ivUWPQOn.d.mts +202 -0
  52. package/dist/schema-ivUWPQOn.d.mts.map +1 -0
  53. package/dist/{server-BkL_NyGG.mjs → server-CtGBZlTg.mjs} +239 -399
  54. package/dist/server-CtGBZlTg.mjs.map +1 -0
  55. package/dist/server.d.mts +54 -13
  56. package/dist/server.d.mts.map +1 -1
  57. package/dist/server.mjs +5 -4
  58. package/dist/{utils-CTtD7Oti.mjs → utils-BbV_nodR.mjs} +23 -20
  59. package/dist/utils-BbV_nodR.mjs.map +1 -0
  60. package/dist/utils.d.mts.map +1 -1
  61. package/dist/utils.mjs +3 -3
  62. package/dist/{reactor-feature-flags-BtWVQDt6.mjs → worker-pool-BWHBLBJd.mjs} +12 -32
  63. package/dist/worker-pool-BWHBLBJd.mjs.map +1 -0
  64. package/dist/workflow-runtime-7GaJ13c9.d.mts.map +1 -1
  65. package/dist/{workflows-fsBcElEi.mjs → workflows-QY9Y2eoD.mjs} +5 -16
  66. package/dist/workflows-QY9Y2eoD.mjs.map +1 -0
  67. package/package.json +29 -19
  68. package/test/attachment-reference-read-model.test.ts +22 -19
  69. package/test/attachments/download-target.test.ts +29 -19
  70. package/test/attachments/service-config.test.ts +28 -16
  71. package/test/boot-auth-config.test.ts +109 -0
  72. package/test/boot-create-signature-policy.test.ts +15 -13
  73. package/test/boot-env-isolation.test.ts +2 -2
  74. package/test/boot-helpers.ts +18 -0
  75. package/test/boot-inspection-enabled.test.ts +16 -26
  76. package/test/boot-mcp-enabled.test.ts +15 -26
  77. package/test/boot-port-zero.test.ts +101 -0
  78. package/test/boot-preconditions.test.ts +51 -0
  79. package/test/boot-privacy.test.ts +12 -23
  80. package/test/boot-unsupported-stored-documents.test.ts +10 -9
  81. package/test/boot-without-renown.test.ts +11 -9
  82. package/test/boot-without-require-signatures.test.ts +12 -10
  83. package/test/boot-worker-trust-policy.test.ts +11 -10
  84. package/test/boot-workflow-singleton-conflict.test.ts +11 -9
  85. package/test/boot-workflow-singleton-retry.test.ts +12 -23
  86. package/test/config-from-env.test.ts +758 -0
  87. package/test/config-merge.test.ts +215 -0
  88. package/test/config-schema.test.ts +128 -0
  89. package/test/default-drive-protocol-versions.test.ts +78 -0
  90. package/test/pglite-storage-dirs.test.ts +24 -14
  91. package/test/pglite-store.test.ts +16 -63
  92. package/test/plugin-presence.test.ts +27 -34
  93. package/test/renown-storage-path.test.ts +9 -8
  94. package/test/shutdown.test.ts +22 -22
  95. package/test/two-switchboards.test.ts +146 -0
  96. package/test/workflow-runtime.test.ts +12 -69
  97. package/tsdown.config.ts +2 -0
  98. package/vitest.config.ts +2 -0
  99. package/.tsbuild/test/inspection-flag.test.d.ts +0 -2
  100. package/.tsbuild/test/inspection-flag.test.d.ts.map +0 -1
  101. package/.tsbuild/test/mcp-flag.test.d.ts +0 -2
  102. package/.tsbuild/test/mcp-flag.test.d.ts.map +0 -1
  103. package/dist/plugin-host-BBqojyyY.d.mts.map +0 -1
  104. package/dist/privacy-BsFl7GzK.mjs.map +0 -1
  105. package/dist/reactor-feature-flags-BtWVQDt6.mjs.map +0 -1
  106. package/dist/server-BkL_NyGG.mjs.map +0 -1
  107. package/dist/utils-CTtD7Oti.mjs.map +0 -1
  108. package/dist/workflows-fsBcElEi.mjs.map +0 -1
  109. package/test/inspection-flag.test.ts +0 -62
  110. package/test/mcp-flag.test.ts +0 -56
@@ -0,0 +1,109 @@
1
+ import { mkdtemp, rm, writeFile } 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 type { SwitchboardConfigInput } from "../src/config/schema.js";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
8
+
9
+ type Info = ReturnType<typeof vi.fn>;
10
+
11
+ function stubLogger(): ILogger & { info: Info } {
12
+ const logger = {
13
+ verbose: vi.fn(),
14
+ debug: vi.fn(),
15
+ info: vi.fn(),
16
+ warn: vi.fn(),
17
+ error: vi.fn(),
18
+ child: vi.fn(),
19
+ };
20
+ logger.child.mockReturnValue(logger);
21
+ return logger as unknown as ILogger & { info: Info };
22
+ }
23
+
24
+ const AUTH_VARS = ["AUTH_ENABLED", "ADMINS", "RESOLVE_CALLER_IDENTITY"];
25
+
26
+ /** Whether the switchboard enforces auth, from reactor-api's setup log. */
27
+ async function bootsWithAuth(options: {
28
+ file: Record<string, unknown>;
29
+ config?: SwitchboardConfigInput["auth"];
30
+ env?: Record<string, string>;
31
+ }): Promise<boolean> {
32
+ const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-auth-config-"));
33
+ const names = ["PH_REACTOR_DATABASE_URL", ...AUTH_VARS];
34
+ const previous = names.map((name) => [name, process.env[name]] as const);
35
+ for (const name of AUTH_VARS) delete process.env[name];
36
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
37
+ Object.assign(process.env, options.env);
38
+ const configFile = join(tempRoot, "powerhouse.config.json");
39
+ await writeFile(configFile, JSON.stringify(options.file));
40
+ const logger = stubLogger();
41
+ const switchboard = await bootSwitchboard(
42
+ {
43
+ configFile,
44
+ auth: options.config,
45
+ server: { port: 0 },
46
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
47
+ features: { workflows: false, mcp: false },
48
+ packages: { disableLocal: true },
49
+ identity: { keypairPath: join(tempRoot, "identity.json") },
50
+ },
51
+ { logger },
52
+ ).finally(() => {
53
+ for (const [name, value] of previous) {
54
+ if (value === undefined) delete process.env[name];
55
+ else process.env[name] = value;
56
+ }
57
+ });
58
+ try {
59
+ const setup = logger.info.mock.calls.find(
60
+ ([message]) =>
61
+ typeof message === "string" &&
62
+ message.startsWith("Setting up Auth middleware"),
63
+ );
64
+ return setup?.[1] === true;
65
+ } finally {
66
+ await switchboard.shutdown();
67
+ await rm(tempRoot, { recursive: true, force: true });
68
+ }
69
+ }
70
+
71
+ const ADMIN = "0x1111111111111111111111111111111111111111";
72
+
73
+ describe("booting Switchboard with auth", () => {
74
+ it("enforces auth the config turns on", async () => {
75
+ expect(
76
+ await bootsWithAuth({
77
+ file: {},
78
+ config: { enabled: true, admins: [ADMIN] },
79
+ }),
80
+ ).toBe(true);
81
+ }, 60_000);
82
+
83
+ it("still takes auth from the config file when the config leaves it unset", async () => {
84
+ expect(
85
+ await bootsWithAuth({
86
+ file: { auth: { enabled: true, admins: [ADMIN] } },
87
+ }),
88
+ ).toBe(true);
89
+ }, 60_000);
90
+
91
+ it("lets AUTH_ENABLED override the config file, as before", async () => {
92
+ expect(
93
+ await bootsWithAuth({
94
+ file: { auth: { enabled: true, admins: [ADMIN] } },
95
+ env: { AUTH_ENABLED: "false" },
96
+ }),
97
+ ).toBe(false);
98
+ }, 60_000);
99
+
100
+ it("lets AUTH_ENABLED override the caller's config, as it overrode options", async () => {
101
+ expect(
102
+ await bootsWithAuth({
103
+ file: {},
104
+ config: { enabled: false },
105
+ env: { AUTH_ENABLED: "true", ADMINS: ADMIN },
106
+ }),
107
+ ).toBe(true);
108
+ }, 60_000);
109
+ });
@@ -8,7 +8,8 @@ import { tmpdir } from "node:os";
8
8
  import { join } from "node:path";
9
9
  import type { ILogger } from "document-model";
10
10
  import { describe, expect, it, vi } from "vitest";
11
- import { startSwitchboard } from "../src/server.mjs";
11
+ import type { startSwitchboard } from "../src/server.mjs";
12
+ import { bootSwitchboard } from "./boot-helpers.js";
12
13
 
13
14
  function stubLogger(): ILogger {
14
15
  const logger = {
@@ -37,19 +38,20 @@ async function withSwitchboard(
37
38
  let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
38
39
 
39
40
  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" },
41
+ switchboard = await bootSwitchboard(
42
+ {
43
+ server: { port: 0 },
44
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
45
+ features: { workflows: false, mcp: false },
46
+ packages: { disableLocal: true },
47
+ identity: { keypairPath: join(tempRoot, "identity.json") },
48
+ drive: {
49
+ slug: "default-drive",
50
+ global: { name: "Default Drive" },
51
+ },
50
52
  },
51
- logger: stubLogger(),
52
- });
53
+ { logger: stubLogger() },
54
+ );
53
55
  await run(switchboard);
54
56
  await switchboard.shutdown();
55
57
  switchboard = undefined;
@@ -4,7 +4,7 @@ import path from "node:path";
4
4
  import { afterEach, describe, expect, it } from "vitest";
5
5
  import { switchboardBootFactsFromEnv } from "../src/boot.mjs";
6
6
 
7
- // Its own file: reactor-api's root must not have been imported in this worker yet.
7
+ // Its own file: a .env loaded by an earlier import in this worker would mask a leak.
8
8
  describe("switchboardBootFactsFromEnv", () => {
9
9
  const cwd = process.cwd();
10
10
  let dir: string | undefined;
@@ -14,7 +14,7 @@ describe("switchboardBootFactsFromEnv", () => {
14
14
  delete process.env.PH_BOOT_ENV_SENTINEL;
15
15
  });
16
16
 
17
- it("leaves process.env as it found it, though reactor-api loads the cwd .env", async () => {
17
+ it("leaves process.env as it found it, with a .env in the cwd", async () => {
18
18
  dir = mkdtempSync(path.join(tmpdir(), "sb-boot-env-"));
19
19
  writeFileSync(path.join(dir, ".env"), "PH_BOOT_ENV_SENTINEL=leaked\n");
20
20
  process.chdir(dir);
@@ -0,0 +1,18 @@
1
+ import { switchboardConfigFromEnv } from "../src/config/from-env.js";
2
+ import { callerOverEnv } from "../src/config/merge.js";
3
+ import type { SwitchboardConfigInput } from "../src/config/schema.js";
4
+ import { startSwitchboard, type SwitchboardDeps } from "../src/server.mjs";
5
+
6
+ /** Boots the way an edge does: the caller's layer over process.env's. */
7
+ export function bootSwitchboard(
8
+ config: SwitchboardConfigInput,
9
+ deps: SwitchboardDeps = {},
10
+ ): ReturnType<typeof startSwitchboard> {
11
+ return startSwitchboard(
12
+ callerOverEnv(
13
+ switchboardConfigFromEnv(process.env, { logger: deps.logger }),
14
+ config,
15
+ ),
16
+ deps,
17
+ );
18
+ }
@@ -1,21 +1,10 @@
1
1
  import { mkdtemp, rm } from "node:fs/promises";
2
- import { createServer } from "node:net";
3
2
  import { tmpdir } from "node:os";
4
3
  import { join } from "node:path";
5
4
  import type { ILogger } from "document-model";
6
5
  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
- }
6
+ import type { startSwitchboard } from "../src/server.mjs";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
19
8
 
20
9
  function stubLogger(): ILogger {
21
10
  const logger = {
@@ -52,20 +41,21 @@ async function withSwitchboard(
52
41
  let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
53
42
 
54
43
  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" },
44
+ switchboard = await bootSwitchboard(
45
+ {
46
+ server: { port: 0 },
47
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
48
+ features: { workflows: false },
49
+ inspection: { enabled: inspection },
50
+ packages: { disableLocal: true },
51
+ identity: { keypairPath: join(tempRoot, "identity.json") },
52
+ drive: {
53
+ slug: "default-drive",
54
+ global: { name: "Default Drive" },
55
+ },
66
56
  },
67
- logger: stubLogger(),
68
- });
57
+ { logger: stubLogger() },
58
+ );
69
59
  await run(switchboard);
70
60
  await switchboard.shutdown();
71
61
  switchboard = undefined;
@@ -1,21 +1,10 @@
1
1
  import { mkdtemp, rm } from "node:fs/promises";
2
- import { createServer } from "node:net";
3
2
  import { tmpdir } from "node:os";
4
3
  import { join } from "node:path";
5
4
  import type { ILogger } from "document-model";
6
5
  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
- }
6
+ import type { startSwitchboard } from "../src/server.mjs";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
19
8
 
20
9
  function stubLogger(): ILogger {
21
10
  const logger = {
@@ -55,20 +44,20 @@ async function withSwitchboard(
55
44
  let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
56
45
 
57
46
  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" },
47
+ switchboard = await bootSwitchboard(
48
+ {
49
+ server: { port: 0 },
50
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
51
+ features: { workflows: false, mcp: mcp },
52
+ packages: { disableLocal: true },
53
+ identity: { keypairPath: join(tempRoot, "identity.json") },
54
+ drive: {
55
+ slug: "default-drive",
56
+ global: { name: "Default Drive" },
57
+ },
69
58
  },
70
- logger: stubLogger(),
71
- });
59
+ { logger: stubLogger() },
60
+ );
72
61
  await run(switchboard);
73
62
  await switchboard.shutdown();
74
63
  switchboard = undefined;
@@ -0,0 +1,101 @@
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 { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
+ import type { startSwitchboard } from "../src/server.mjs";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
8
+
9
+ type Switchboard = Awaited<ReturnType<typeof startSwitchboard>>;
10
+
11
+ function stubLogger(): ILogger {
12
+ const logger = {
13
+ verbose: vi.fn(),
14
+ debug: vi.fn(),
15
+ info: vi.fn(),
16
+ warn: vi.fn(),
17
+ error: vi.fn(),
18
+ child: vi.fn(),
19
+ };
20
+ logger.child.mockReturnValue(logger);
21
+ return logger as unknown as ILogger;
22
+ }
23
+
24
+ const ENV = ["PH_REACTOR_DATABASE_URL", "FEATURE_PLUGIN_HOST_ENABLED"];
25
+ let previousEnv: (readonly [string, string | undefined])[] = [];
26
+ let tempRoots: string[] = [];
27
+ let running: Switchboard[] = [];
28
+
29
+ beforeEach(() => {
30
+ previousEnv = ENV.map((name) => [name, process.env[name]] as const);
31
+ delete process.env.FEATURE_PLUGIN_HOST_ENABLED;
32
+ });
33
+
34
+ afterEach(async () => {
35
+ for (const switchboard of running.reverse()) await switchboard.shutdown();
36
+ running = [];
37
+ for (const [name, value] of previousEnv) {
38
+ if (value === undefined) delete process.env[name];
39
+ else process.env[name] = value;
40
+ }
41
+ for (const root of tempRoots) {
42
+ await rm(root, { recursive: true, force: true });
43
+ }
44
+ tempRoots = [];
45
+ });
46
+
47
+ // The reactor database comes from process env, so instances boot one at a time.
48
+ async function boot(): Promise<Switchboard> {
49
+ const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-port-zero-"));
50
+ tempRoots.push(tempRoot);
51
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
52
+ const switchboard = await bootSwitchboard(
53
+ {
54
+ server: { port: 0 },
55
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
56
+ features: { workflows: false, mcp: false },
57
+ packages: { disableLocal: true },
58
+ identity: { keypairPath: join(tempRoot, "identity.json") },
59
+ drive: {
60
+ slug: "default-drive",
61
+ global: { name: "Default Drive" },
62
+ },
63
+ },
64
+ { logger: stubLogger() },
65
+ );
66
+ running.push(switchboard);
67
+ return switchboard;
68
+ }
69
+
70
+ describe("booting Switchboard on port 0", () => {
71
+ it("reports the port the OS picked", async () => {
72
+ const { port, defaultDriveUrl } = await boot();
73
+
74
+ expect(port).toBeGreaterThan(0);
75
+ expect((await fetch(`http://localhost:${port}/health`)).status).toBe(200);
76
+ const graphql = await fetch(`http://localhost:${port}/graphql`, {
77
+ method: "POST",
78
+ headers: { "content-type": "application/json" },
79
+ // Routed to a subgraph, which the gateway fetches at its own port.
80
+ body: JSON.stringify({ query: "{ system { __typename } }" }),
81
+ });
82
+ expect(await graphql.json()).toEqual({
83
+ data: { system: { __typename: "SystemInfo" } },
84
+ });
85
+ expect(defaultDriveUrl).toBe(`http://localhost:${port}/d/default-drive`);
86
+ }, 60_000);
87
+
88
+ it("gives a second instance its own port while the first runs", async () => {
89
+ const first = await boot();
90
+ const second = await boot();
91
+
92
+ expect(second.port).toBeGreaterThan(0);
93
+ expect(second.port).not.toBe(first.port);
94
+ expect((await fetch(`http://localhost:${first.port}/health`)).status).toBe(
95
+ 200,
96
+ );
97
+ expect((await fetch(`http://localhost:${second.port}/health`)).status).toBe(
98
+ 200,
99
+ );
100
+ }, 120_000);
101
+ });
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
2
2
  import {
3
3
  reactorDbUrlOf,
4
4
  switchboardBootFactsFromEnv,
5
+ switchboardBootFactsOf,
5
6
  switchboardBootIssues,
6
7
  type SwitchboardBootFacts,
7
8
  } from "../src/boot.mjs";
@@ -66,6 +67,56 @@ describe("switchboardBootIssues", () => {
66
67
  });
67
68
  });
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
+
69
120
  describe("switchboardBootFactsFromEnv", () => {
70
121
  it("reads the facts with the server's parsers", async () => {
71
122
  const { facts, issues } = await switchboardBootFactsFromEnv(
@@ -1,11 +1,11 @@
1
1
  import { mkdtemp, rm } from "node:fs/promises";
2
- import { createServer } from "node:net";
3
2
  import { tmpdir } from "node:os";
4
3
  import { join } from "node:path";
5
4
  import type { ILogger } from "document-model";
6
5
  import pg from "pg";
7
6
  import { describe, expect, it, vi } from "vitest";
8
- import { startSwitchboard } from "../src/server.mjs";
7
+ import type { startSwitchboard } from "../src/server.mjs";
8
+ import { bootSwitchboard } from "./boot-helpers.js";
9
9
 
10
10
  const ADMIN = "0xadad00000000000000000000000000000000adad";
11
11
  const SECRET = "switchboard-privacy-test-deployment-secret-0123";
@@ -56,31 +56,20 @@ async function withEnv<T>(
56
56
  }
57
57
  }
58
58
 
59
- async function freePort(): Promise<number> {
60
- const server = createServer();
61
- await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
62
- const address = server.address();
63
- await new Promise((resolve) => server.close(resolve));
64
- if (address === null || typeof address === "string") {
65
- throw new Error("no port");
66
- }
67
- return address.port;
68
- }
69
-
70
59
  async function boot(
71
60
  tempRoot: string,
72
61
  logger: ILogger = stubLogger(),
73
62
  ): Promise<Switchboard> {
74
- return startSwitchboard({
75
- workflows: { enabled: false },
76
- dbPath: join(tempRoot, "read-model"),
77
- port: await freePort(),
78
- strictPort: true,
79
- mcp: false,
80
- disableLocalPackages: true,
81
- identity: { keypairPath: join(tempRoot, "identity.json") },
82
- logger,
83
- });
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
+ );
84
73
  }
85
74
 
86
75
  async function queryErasure(switchboard: Switchboard) {
@@ -26,7 +26,7 @@ import {
26
26
  vi,
27
27
  } from "vitest";
28
28
  import { ClosablePGliteDialect } from "../src/pglite-dialect.js";
29
- import { startSwitchboard } from "../src/server.mjs";
29
+ import { bootSwitchboard } from "./boot-helpers.js";
30
30
  import { StoredDocumentsRefusedError } from "../src/unsupported-stored-documents.mjs";
31
31
 
32
32
  const ENV_KEYS = [
@@ -143,14 +143,15 @@ describe("booting over documents this build does not run", () => {
143
143
  }, BOOT_TIMEOUT);
144
144
 
145
145
  function boot(logger: ILogger) {
146
- return startSwitchboard({
147
- workflows: { enabled: false },
148
- port: 0,
149
- mcp: false,
150
- disableLocalPackages: true,
151
- identity: { keypairPath: join(tempRoot, "identity.json") },
152
- logger,
153
- });
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
+ );
154
155
  }
155
156
 
156
157
  it(
@@ -8,7 +8,8 @@ import { join } from "node:path";
8
8
  import type { ILogger } from "document-model";
9
9
  import { describe, expect, it, vi } from "vitest";
10
10
  import type * as RenownModule from "../src/renown.js";
11
- import { startSwitchboard } from "../src/server.mjs";
11
+ import type { startSwitchboard } from "../src/server.mjs";
12
+ import { bootSwitchboard } from "./boot-helpers.js";
12
13
 
13
14
  vi.mock("../src/renown.js", async (importOriginal) => ({
14
15
  ...(await importOriginal<typeof RenownModule>()),
@@ -37,14 +38,15 @@ describe("booting Switchboard without Renown", () => {
37
38
  let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
38
39
 
39
40
  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
- logger,
47
- });
41
+ switchboard = await bootSwitchboard(
42
+ {
43
+ server: { port: 0 },
44
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
45
+ features: { workflows: false, mcp: false },
46
+ packages: { disableLocal: true },
47
+ },
48
+ { logger },
49
+ );
48
50
 
49
51
  expect(
50
52
  logger.warn.mock.calls.some(
@@ -3,7 +3,8 @@ import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import type { ILogger } from "document-model";
5
5
  import { describe, expect, it, vi } from "vitest";
6
- import { startSwitchboard } from "../src/server.mjs";
6
+ import type { startSwitchboard } from "../src/server.mjs";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
7
8
 
8
9
  function stubLogger(): ILogger & { info: ReturnType<typeof vi.fn> } {
9
10
  const logger = {
@@ -31,15 +32,16 @@ describe("booting Switchboard", () => {
31
32
  let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
32
33
 
33
34
  try {
34
- switchboard = await startSwitchboard({
35
- workflows: { enabled: false },
36
- dbPath: join(tempRoot, "read-model"),
37
- port: 0,
38
- mcp: false,
39
- disableLocalPackages: true,
40
- identity: { keypairPath: join(tempRoot, "identity.json") },
41
- logger,
42
- });
35
+ switchboard = await bootSwitchboard(
36
+ {
37
+ server: { port: 0 },
38
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
39
+ features: { workflows: false, mcp: false },
40
+ packages: { disableLocal: true },
41
+ identity: { keypairPath: join(tempRoot, "identity.json") },
42
+ },
43
+ { logger },
44
+ );
43
45
 
44
46
  const flagLog = logger.info.mock.calls.find(
45
47
  ([message]) => message === "Feature flags: @flags",
@@ -4,7 +4,7 @@ import { tmpdir } from "node:os";
4
4
  import { join } from "node:path";
5
5
  import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
6
  import type * as RenownModule from "../src/renown.js";
7
- import { startSwitchboard } from "../src/server.mjs";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
8
8
 
9
9
  vi.mock("../src/renown.js", async (importOriginal) => ({
10
10
  ...(await importOriginal<typeof RenownModule>()),
@@ -57,15 +57,16 @@ describe("booting with pooled workers under authEnforcement", () => {
57
57
  });
58
58
 
59
59
  function boot(numWorkers: number) {
60
- return startSwitchboard({
61
- workflows: { enabled: false },
62
- dbPath: join(tempRoot, "read-model"),
63
- port: 0,
64
- mcp: false,
65
- disableLocalPackages: true,
66
- logger: stubLogger(),
67
- workerPool: { numWorkers },
68
- });
60
+ return bootSwitchboard(
61
+ {
62
+ server: { port: 0 },
63
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
64
+ features: { workflows: false, mcp: false },
65
+ packages: { disableLocal: true },
66
+ workers: { executor: { count: numWorkers } },
67
+ },
68
+ { logger: stubLogger() },
69
+ );
69
70
  }
70
71
 
71
72
  it("fails with a self Renown source", async () => {