@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,215 @@
1
+ import type { ILogger } from "document-model";
2
+ import { describe, expect, it } from "vitest";
3
+ import { switchboardConfigFromEnv } from "../src/config/from-env.js";
4
+ import {
5
+ callerOverEnv,
6
+ mergeSwitchboardConfig,
7
+ switchboardConfigSchema,
8
+ type SwitchboardConfig,
9
+ type SwitchboardConfigInput,
10
+ } from "../src/config/schema.js";
11
+
12
+ type Env = Record<string, string | undefined>;
13
+
14
+ const silent = { warn: () => undefined } as unknown as ILogger;
15
+
16
+ function resolved(env: Env, caller: SwitchboardConfigInput): SwitchboardConfig {
17
+ return switchboardConfigSchema.parse(
18
+ callerOverEnv(switchboardConfigFromEnv(env, { logger: silent }), caller),
19
+ );
20
+ }
21
+
22
+ describe("mergeSwitchboardConfig", () => {
23
+ it("lets a later layer win per key and keeps the rest", () => {
24
+ expect(
25
+ mergeSwitchboardConfig(
26
+ { server: { port: 1, basePath: "/a" }, logLevel: "debug" },
27
+ { server: { port: 2 } },
28
+ { server: { port: undefined } },
29
+ ),
30
+ ).toEqual({ server: { port: 2, basePath: "/a" }, logLevel: "debug" });
31
+ });
32
+
33
+ it("replaces arrays, maps and the attachment storage whole", () => {
34
+ const s3 = {
35
+ kind: "s3" as const,
36
+ s3: {
37
+ endpoint: "e",
38
+ region: "r",
39
+ bucket: "b",
40
+ accessKeyId: "a",
41
+ secretAccessKey: "s",
42
+ prefix: "p",
43
+ forcePathStyle: false,
44
+ uploadTtlSeconds: 1,
45
+ downloadTtlSeconds: 1,
46
+ },
47
+ };
48
+ const processorConfig = new Map([["b", 2]]);
49
+ expect(
50
+ mergeSwitchboardConfig(
51
+ {
52
+ auth: { admins: ["0xa", "0xb"] },
53
+ attachments: { storage: s3 },
54
+ processorConfig: new Map([["a", 1]]),
55
+ },
56
+ {
57
+ auth: { admins: ["0xc"] },
58
+ attachments: { storage: { kind: "filesystem" } },
59
+ processorConfig,
60
+ },
61
+ ),
62
+ ).toEqual({
63
+ auth: { admins: ["0xc"] },
64
+ attachments: { storage: { kind: "filesystem" } },
65
+ processorConfig,
66
+ });
67
+ });
68
+ });
69
+
70
+ describe("callerOverEnv keeps each key's precedence", () => {
71
+ it("lets the caller win an ordinary key", () => {
72
+ const config = resolved(
73
+ {
74
+ PH_REGISTRY_URL: "https://env",
75
+ PH_PGLITE_WORKERS: "true",
76
+ PH_WORKFLOWS_ENABLED: "1",
77
+ DATABASE_URL: "/env/read",
78
+ PH_REACTOR_DATABASE_URL: "/env/reactor",
79
+ },
80
+ {
81
+ packages: { registryUrl: "https://caller" },
82
+ storage: {
83
+ pglite: { workers: false },
84
+ readModel: { url: "/caller" },
85
+ reactor: { url: "/caller" },
86
+ },
87
+ features: { workflows: false },
88
+ },
89
+ );
90
+ expect(config.packages.registryUrl).toBe("https://caller");
91
+ expect(config.storage.pglite.workers).toBe(false);
92
+ expect(config.features.workflows).toBe(false);
93
+ expect(config.storage.readModel.url).toBe("/caller");
94
+ expect(config.storage.reactor.url).toBe("/caller");
95
+ });
96
+
97
+ it("falls back to the env where the caller says nothing", () => {
98
+ const config = resolved(
99
+ { PH_PGLITE_WORKERS: "false", PH_WORKFLOWS_ENABLED: "0" },
100
+ {},
101
+ );
102
+ expect(config.storage.pglite.workers).toBe(false);
103
+ expect(config.features.workflows).toBe(false);
104
+ });
105
+
106
+ it("lets DOCUMENT_MODEL_SUBGRAPHS_ENABLED win over the caller", () => {
107
+ expect(
108
+ resolved(
109
+ { DOCUMENT_MODEL_SUBGRAPHS_ENABLED: "false" },
110
+ { features: { documentModelSubgraphs: true } },
111
+ ).features.documentModelSubgraphs,
112
+ ).toBe(false);
113
+ expect(
114
+ resolved({}, { features: { documentModelSubgraphs: false } }).features
115
+ .documentModelSubgraphs,
116
+ ).toBe(false);
117
+ });
118
+
119
+ it("lets reactor-api's overriding auth env vars win over the caller", () => {
120
+ const { auth } = resolved(
121
+ {
122
+ AUTH_ENABLED: "false",
123
+ ADMINS: "0xEnv",
124
+ RESOLVE_CALLER_IDENTITY: "false",
125
+ REQUIRE_AUTHENTICATED_CALLER: "false",
126
+ REQUIRE_AUTHENTICATED_CALLER_EXEMPT_PATHS: "/env",
127
+ ATTACHMENT_READS_FOLLOW_DOCUMENT_POLICY: "false",
128
+ DEFAULT_PROTECTION: "false",
129
+ },
130
+ {
131
+ auth: {
132
+ enabled: true,
133
+ admins: ["0xcaller"],
134
+ resolveIdentity: true,
135
+ requireAuthenticatedCaller: true,
136
+ requireAuthenticatedCallerExemptPaths: ["/caller"],
137
+ attachmentReadsFollowDocumentPolicy: true,
138
+ defaultProtection: true,
139
+ },
140
+ },
141
+ );
142
+ expect(auth).toMatchObject({
143
+ enabled: false,
144
+ admins: ["0xenv"],
145
+ resolveIdentity: false,
146
+ requireAuthenticatedCaller: false,
147
+ requireAuthenticatedCallerExemptPaths: ["/env"],
148
+ attachmentReadsFollowDocumentPolicy: false,
149
+ // An ordinary key: the caller wins.
150
+ defaultProtection: true,
151
+ });
152
+ });
153
+
154
+ it.each([
155
+ [{}, undefined, true],
156
+ [{ MCP_ENABLED: "false" }, undefined, false],
157
+ [{ MCP_ENABLED: "false" }, true, false],
158
+ [{ MCP_ENABLED: "true" }, false, false],
159
+ [{ MCP_ENABLED: "yes" }, undefined, true],
160
+ [{}, false, false],
161
+ ] as const)(
162
+ "MCP: env %o with the caller's %s mounts it: %s",
163
+ (env, mcp, mounted) => {
164
+ expect(resolved(env, { features: { mcp } }).features.mcp ?? true).toBe(
165
+ mounted,
166
+ );
167
+ },
168
+ );
169
+
170
+ it.each([
171
+ [{}, undefined, true],
172
+ [{ INSPECTION_ENABLED: "false" }, undefined, false],
173
+ [{ INSPECTION_ENABLED: "false" }, true, false],
174
+ [{ INSPECTION_ENABLED: "true" }, false, false],
175
+ [{ INSPECTION_ENABLED: "0" }, undefined, true],
176
+ [{ INSPECTION_ENABLED: "true" }, undefined, true],
177
+ ] as const)(
178
+ "inspection: env %o with the caller's %s mounts it: %s",
179
+ (env, enabled, mounted) => {
180
+ expect(
181
+ resolved(env, { inspection: { enabled } }).inspection.enabled,
182
+ ).toBe(mounted);
183
+ },
184
+ );
185
+
186
+ it("ignores MCP_ENABLED and PH_WORKFLOWS_ENABLED under the plugin host", () => {
187
+ const env = {
188
+ FEATURE_PLUGIN_HOST_ENABLED: "1",
189
+ MCP_ENABLED: "false",
190
+ PH_WORKFLOWS_ENABLED: "true",
191
+ };
192
+ const host = resolved(env, {});
193
+ expect(host.features.pluginHost).toBe(true);
194
+ expect(host.features.mcp).toBeUndefined();
195
+ expect(host.features.workflows).toBeUndefined();
196
+ const withCaller = resolved(env, {
197
+ features: { mcp: false, workflows: true },
198
+ });
199
+ expect(withCaller.features.mcp).toBe(false);
200
+ expect(withCaller.features.workflows).toBe(true);
201
+ // Not carved: classic on both paths.
202
+ expect(
203
+ resolved({ ...env, INSPECTION_ENABLED: "false" }, {}).inspection.enabled,
204
+ ).toBe(false);
205
+ });
206
+ });
207
+
208
+ describe("startSwitchboard", () => {
209
+ it("refuses the old options shape instead of booting on defaults", async () => {
210
+ const { startSwitchboard } = await import("../src/server.mjs");
211
+ await expect(
212
+ startSwitchboard({ port: 0, dbPath: "x" } as SwitchboardConfigInput),
213
+ ).rejects.toThrow("unknown config keys: port, dbPath");
214
+ });
215
+ });
@@ -0,0 +1,128 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { z } from "zod";
3
+ import {
4
+ switchboardConfigLayerSchema,
5
+ switchboardConfigSchema,
6
+ type SwitchboardConfigInput,
7
+ } from "../src/config/schema.js";
8
+
9
+ describe("switchboardConfigSchema", () => {
10
+ it("fills every default from an empty layer", () => {
11
+ const config = switchboardConfigSchema.parse({});
12
+ expect(config.logLevel).toBe("info");
13
+ expect(config.server).toEqual({
14
+ port: 4001,
15
+ strictPort: false,
16
+ basePath: "/",
17
+ gatewayAdapter: "apollo",
18
+ dev: false,
19
+ });
20
+ expect(config.storage).toEqual({
21
+ reactor: {},
22
+ readModel: {},
23
+ inMemory: false,
24
+ pglite: { fsync: true, workers: true, vacuumFull: { mode: "off" } },
25
+ migratePglite: false,
26
+ });
27
+ expect(config.protocol).toEqual({
28
+ featureFlags: {
29
+ documentDecisions: false,
30
+ authEnforcement: false,
31
+ authGroups: false,
32
+ authConditions: false,
33
+ },
34
+ createSignaturePolicy: "v2-required",
35
+ unsupportedStoredDocuments: "refuse",
36
+ legacyProcessorIds: true,
37
+ catchUpPriority: "dynamic",
38
+ });
39
+ expect(config.workers).toEqual({
40
+ executor: { count: 0, poolSize: 2, acquireTimeoutMs: 5000 },
41
+ hostPoolSize: 16,
42
+ projection: { enabled: false, poolSize: 8 },
43
+ });
44
+ expect(config.inspection).toEqual({ enabled: true, admin: false });
45
+ expect(config.features).toEqual({
46
+ documentModelSubgraphs: true,
47
+ pluginHost: false,
48
+ privacy: { enabled: false },
49
+ });
50
+ expect(config.attachments).toEqual({ storage: { kind: "filesystem" } });
51
+ expect(config.auth.documentPermissions).toEqual({ enabled: false });
52
+ });
53
+
54
+ it("keeps the defaults beside a nested value a layer sets", () => {
55
+ const layer: SwitchboardConfigInput = {
56
+ workers: { executor: { count: "auto" } },
57
+ storage: { pglite: { vacuumFull: { mode: "quiet" } } },
58
+ };
59
+ const config = switchboardConfigSchema.parse(layer);
60
+ expect(config.workers.executor).toEqual({
61
+ count: "auto",
62
+ poolSize: 2,
63
+ acquireTimeoutMs: 5000,
64
+ });
65
+ expect(config.workers.hostPoolSize).toBe(16);
66
+ expect(config.storage.pglite).toEqual({
67
+ fsync: true,
68
+ workers: true,
69
+ vacuumFull: { mode: "quiet" },
70
+ });
71
+ });
72
+
73
+ it.each<[string, SwitchboardConfigInput]>([
74
+ ["a port out of range", { server: { port: 70000 } }],
75
+ ["a fractional worker count", { workers: { executor: { count: 1.5 } } }],
76
+ ["a host pool of 0", { workers: { hostPoolSize: 0 } }],
77
+ ["a catch-up level above 7", { protocol: { catchUpPriority: 8 } }],
78
+ [
79
+ "an unknown signature policy",
80
+ { protocol: { createSignaturePolicy: "v3" as "legacy" } },
81
+ ],
82
+ ["a PG major it does not ship", { storage: { forcePgVersion: 15 as 16 } }],
83
+ ])("refuses %s", (_name, layer) => {
84
+ expect(switchboardConfigSchema.safeParse(layer).success).toBe(false);
85
+ });
86
+ });
87
+
88
+ describe("the config modules", () => {
89
+ it("load without the server", async () => {
90
+ vi.doMock("../src/server.mjs", () => {
91
+ throw new Error("the server was loaded");
92
+ });
93
+ await expect(import("../src/config/from-env.js")).resolves.toBeDefined();
94
+ vi.doUnmock("../src/server.mjs");
95
+ });
96
+ });
97
+
98
+ describe("switchboardConfigLayerSchema", () => {
99
+ const layer = switchboardConfigLayerSchema(["server.port", "drive"]);
100
+
101
+ it("keeps a layer as written, without defaults", () => {
102
+ expect(layer.parse({})).toEqual({});
103
+ expect(layer.parse({ auth: { enabled: true } })).toEqual({
104
+ auth: { enabled: true },
105
+ });
106
+ });
107
+
108
+ it("validates values and refuses unknown or omitted keys", () => {
109
+ expect(
110
+ layer.safeParse({ workers: { executor: { count: "auto" } } }),
111
+ ).toMatchObject({ success: true });
112
+ expect(
113
+ layer.safeParse({ workers: { executor: { count: -1 } } }).success,
114
+ ).toBe(false);
115
+ expect(layer.safeParse({ auth: { enabeld: true } }).success).toBe(false);
116
+ expect(layer.safeParse({ server: { port: 1 } }).success).toBe(false);
117
+ expect(layer.safeParse({ drive: {} }).success).toBe(false);
118
+ expect(layer.safeParse({ server: { basePath: "/x" } }).success).toBe(true);
119
+ });
120
+
121
+ it("emits a JSON Schema once the map key is omitted", () => {
122
+ const schema = z.toJSONSchema(
123
+ switchboardConfigLayerSchema(["processorConfig"]),
124
+ { io: "input" },
125
+ );
126
+ expect(schema).toMatchObject({ additionalProperties: false });
127
+ });
128
+ });
@@ -0,0 +1,78 @@
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
+ write: {
41
+ create: (document: PHDocument) => {
42
+ created.push(document);
43
+ return Promise.resolve({
44
+ readable: Promise.resolve({
45
+ documentId: document.header.id,
46
+ branch: "main",
47
+ }),
48
+ });
49
+ },
50
+ },
51
+ } as unknown as IReactorClient,
52
+ };
53
+ }
54
+
55
+ describe.each([
56
+ ["addDefaultDrive", addDefaultDrive],
57
+ ["addDefaultReactorDrive", addDefaultReactorDrive],
58
+ ])("%s", (_name, add) => {
59
+ it("heads the drive with the client's newDocument", async () => {
60
+ const { client: reactor, created } = client();
61
+
62
+ await add(reactor, { slug: "main", global: { name: "Main" } } as never, 1);
63
+
64
+ expect(created[0].header.protocolVersions).toEqual({
65
+ "base-reducer": 3,
66
+ signature: 2,
67
+ });
68
+ });
69
+
70
+ it("heads a drive with a configured id as legacy at the same versions", async () => {
71
+ const { client: reactor, created } = client();
72
+
73
+ await add(reactor, { id: "fixed", global: { name: "Main" } } as never, 1);
74
+
75
+ expect(created[0].header.id).toBe("fixed");
76
+ expect(created[0].header.protocolVersions).toEqual({ "base-reducer": 3 });
77
+ });
78
+ });
@@ -7,20 +7,30 @@ describe("resolvePgliteStorageDirs", () => {
7
7
  const phDir = path.join("/proj", ".ph");
8
8
 
9
9
  it("keeps the classic literals verbatim without a user-data root", () => {
10
- const saved = process.env.PH_USER_DATA_DIR;
11
- delete process.env.PH_USER_DATA_DIR;
12
- try {
13
- const dirs = resolvePgliteStorageDirs({
14
- dbPath: undefined,
15
- reactorDbUrl: undefined,
16
- callerReactor: false,
17
- });
18
- expect(dirs.reactorPgliteDir).toBe("./.ph/reactor-storage");
19
- expect(dirs.readModelPgliteDir).toBe(".ph/read-storage");
20
- expect(dirs.sharedPgliteDir).toBeNull();
21
- } finally {
22
- if (saved !== undefined) process.env.PH_USER_DATA_DIR = saved;
23
- }
10
+ const dirs = resolvePgliteStorageDirs({
11
+ dbPath: undefined,
12
+ reactorDbUrl: undefined,
13
+ callerReactor: false,
14
+ });
15
+ expect(dirs.reactorPgliteDir).toBe("./.ph/reactor-storage");
16
+ expect(dirs.readModelPgliteDir).toBe(".ph/read-storage");
17
+ expect(dirs.sharedPgliteDir).toBeNull();
18
+ });
19
+
20
+ it("moves the defaults under the user-data root", () => {
21
+ const userData = path.join("/home", "user");
22
+ const dirs = resolvePgliteStorageDirs({
23
+ dbPath: undefined,
24
+ reactorDbUrl: undefined,
25
+ callerReactor: false,
26
+ userDataDir: userData,
27
+ });
28
+ expect(dirs.reactorPgliteDir).toBe(
29
+ path.join(userData, ".ph", "reactor-storage"),
30
+ );
31
+ expect(dirs.readModelPgliteDir).toBe(
32
+ path.join(userData, ".ph", "read-storage"),
33
+ );
24
34
  });
25
35
 
26
36
  it("defaults to the classic split under the .ph tree", () => {
@@ -9,28 +9,15 @@ import { EventEmitter } from "node:events";
9
9
  import { mkdtemp, rm } from "node:fs/promises";
10
10
  import { tmpdir } from "node:os";
11
11
  import { join } from "node:path";
12
- import {
13
- afterEach,
14
- beforeAll,
15
- beforeEach,
16
- describe,
17
- expect,
18
- it,
19
- vi,
20
- } from "vitest";
21
- import { initFeatureFlags } from "../src/feature-flags.js";
12
+ import { afterEach, describe, expect, it, vi } from "vitest";
22
13
  import { installFatalErrorShutdown } from "../src/fatal-shutdown.mjs";
23
14
  import {
24
15
  createPgliteStoreFactory,
25
- PGLITE_WORKERS_DEFAULT,
26
- PH_PGLITE_WORKERS,
27
- resolvePgliteWorkersEnabled,
28
16
  routePgliteWorkerFailures,
29
17
  terminateOnPoisoned,
30
18
  type PgliteStoreOptions,
31
19
  vacuumFullTimingFromEnv,
32
20
  } from "../src/pglite-store.mjs";
33
- import type { BooleanFlagSource } from "../src/workflow-runtime.mjs";
34
21
 
35
22
  const BOOT = 90_000;
36
23
 
@@ -72,54 +59,6 @@ class FakeWorkerEvents {
72
59
  }
73
60
  }
74
61
 
75
- describe("resolvePgliteWorkersEnabled", () => {
76
- let featureFlags: BooleanFlagSource;
77
-
78
- const previous = process.env[PH_PGLITE_WORKERS];
79
-
80
- beforeAll(async () => {
81
- featureFlags = await initFeatureFlags();
82
- });
83
-
84
- beforeEach(() => {
85
- delete process.env[PH_PGLITE_WORKERS];
86
- });
87
-
88
- afterEach(() => {
89
- if (previous === undefined) delete process.env[PH_PGLITE_WORKERS];
90
- else process.env[PH_PGLITE_WORKERS] = previous;
91
- });
92
-
93
- it("falls back to the default constant", async () => {
94
- expect(PGLITE_WORKERS_DEFAULT).toBe(true);
95
- await expect(resolvePgliteWorkersEnabled({ featureFlags })).resolves.toBe(
96
- PGLITE_WORKERS_DEFAULT,
97
- );
98
- });
99
-
100
- it('is turned on by PH_PGLITE_WORKERS="true"', async () => {
101
- process.env[PH_PGLITE_WORKERS] = "true";
102
- await expect(resolvePgliteWorkersEnabled({ featureFlags })).resolves.toBe(
103
- true,
104
- );
105
- process.env[PH_PGLITE_WORKERS] = "false";
106
- await expect(resolvePgliteWorkersEnabled({ featureFlags })).resolves.toBe(
107
- false,
108
- );
109
- });
110
-
111
- it("lets the host option win either way", async () => {
112
- process.env[PH_PGLITE_WORKERS] = "true";
113
- await expect(
114
- resolvePgliteWorkersEnabled({ featureFlags, option: false }),
115
- ).resolves.toBe(false);
116
- process.env[PH_PGLITE_WORKERS] = "false";
117
- await expect(
118
- resolvePgliteWorkersEnabled({ featureFlags, option: true }),
119
- ).resolves.toBe(true);
120
- });
121
- });
122
-
123
62
  describe("routePgliteWorkerFailures", () => {
124
63
  it("sends an abort to the fatal shutdown", () => {
125
64
  const proc = fatalProcess();
@@ -296,16 +235,30 @@ describe("vacuumFullTimingFromEnv", () => {
296
235
  it("reads both timings, and leaves unset or invalid values to the defaults", () => {
297
236
  expect(
298
237
  vacuumFullTimingFromEnv({
238
+ PH_PGLITE_VACUUM_FULL: "quiet",
299
239
  PH_PGLITE_VACUUM_FULL_QUIET_MS: "0",
300
240
  PH_PGLITE_VACUUM_FULL_MAX_DEFER_MS: "600000",
301
241
  }),
302
242
  ).toEqual({ vacuumFullQuietMs: 0, vacuumFullMaxDeferMs: 600_000 });
303
- expect(vacuumFullTimingFromEnv({})).toEqual({});
243
+ expect(vacuumFullTimingFromEnv({})).toEqual({ vacuumFullAboveBytes: 0 });
304
244
  expect(
305
245
  vacuumFullTimingFromEnv({
246
+ PH_PGLITE_VACUUM_FULL: "quiet",
306
247
  PH_PGLITE_VACUUM_FULL_QUIET_MS: "soon",
307
248
  PH_PGLITE_VACUUM_FULL_MAX_DEFER_MS: "-5",
308
249
  }),
309
250
  ).toEqual({});
310
251
  });
252
+
253
+ it("turns the mode into the settings the stores open with", () => {
254
+ expect(vacuumFullTimingFromEnv({ PH_PGLITE_VACUUM_FULL: "off" })).toEqual({
255
+ vacuumFullAboveBytes: 0,
256
+ });
257
+ expect(
258
+ vacuumFullTimingFromEnv({ PH_PGLITE_VACUUM_FULL: "forced" }),
259
+ ).toEqual({ vacuumFullMaxDeferMs: 1_800_000 });
260
+ expect(() =>
261
+ vacuumFullTimingFromEnv({ PH_PGLITE_VACUUM_FULL: "sometimes" }),
262
+ ).toThrow(/PH_PGLITE_VACUUM_FULL/);
263
+ });
311
264
  });