@powerhousedao/switchboard 6.2.3-dev.8 → 6.2.3-dev.80
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.
- package/.tsbuild/test/attachments/byte-grants.test.d.ts +2 -0
- package/.tsbuild/test/attachments/byte-grants.test.d.ts.map +1 -0
- package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts +2 -0
- package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts.map +1 -0
- package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts +2 -0
- package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts.map +1 -0
- package/.tsbuild/test/attachments/url-signer.test.d.ts +2 -0
- package/.tsbuild/test/attachments/url-signer.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-auth-config.test.d.ts +2 -0
- package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-create-signature-policy.test.d.ts +2 -0
- package/.tsbuild/test/boot-create-signature-policy.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-env-isolation.test.d.ts +2 -0
- package/.tsbuild/test/boot-env-isolation.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-helpers.d.ts +5 -0
- package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
- package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
- package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
- package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-mcp-enabled.test.d.ts +2 -0
- package/.tsbuild/test/boot-mcp-enabled.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
- package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-preconditions.test.d.ts +2 -0
- package/.tsbuild/test/boot-preconditions.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
- package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts +2 -0
- package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-without-renown.test.d.ts +2 -0
- package/.tsbuild/test/boot-without-renown.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-without-require-signatures.test.d.ts +2 -0
- package/.tsbuild/test/boot-without-require-signatures.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-worker-trust-policy.test.d.ts +2 -0
- package/.tsbuild/test/boot-worker-trust-policy.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
- package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
- package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
- package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
- package/.tsbuild/test/builder-defaults.test.d.ts +2 -0
- package/.tsbuild/test/builder-defaults.test.d.ts.map +1 -0
- package/.tsbuild/test/config-from-env.test.d.ts +2 -0
- package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
- package/.tsbuild/test/config-merge.test.d.ts +2 -0
- package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
- package/.tsbuild/test/config-schema.test.d.ts +2 -0
- package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
- package/.tsbuild/test/create-signature-policy.test.d.ts +2 -0
- package/.tsbuild/test/create-signature-policy.test.d.ts.map +1 -0
- package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
- package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
- package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
- package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
- package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
- package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
- package/.tsbuild/test/fatal-shutdown.test.d.ts +2 -0
- package/.tsbuild/test/fatal-shutdown.test.d.ts.map +1 -0
- package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
- package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
- package/.tsbuild/test/observability-signals.test.d.ts +2 -0
- package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
- package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
- package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
- package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
- package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
- package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
- package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
- package/.tsbuild/test/pglite-store.test.d.ts +2 -0
- package/.tsbuild/test/pglite-store.test.d.ts.map +1 -0
- package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
- package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
- package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
- package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
- package/.tsbuild/test/port-probe.test.d.ts +2 -0
- package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
- package/.tsbuild/test/renown-storage-path.test.d.ts +2 -0
- package/.tsbuild/test/renown-storage-path.test.d.ts.map +1 -0
- package/.tsbuild/test/renown-trust-policy.test.d.ts +2 -0
- package/.tsbuild/test/renown-trust-policy.test.d.ts.map +1 -0
- package/.tsbuild/test/served-schemas.test.d.ts +2 -0
- package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
- package/.tsbuild/test/shutdown.test.d.ts +2 -0
- package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
- package/.tsbuild/test/snapshot-writer.d.ts +18 -0
- package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
- package/.tsbuild/test/store-lock.test.d.ts +2 -0
- package/.tsbuild/test/store-lock.test.d.ts.map +1 -0
- package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
- package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
- package/.tsbuild/test/unsupported-stored-documents.test.d.ts +2 -0
- package/.tsbuild/test/unsupported-stored-documents.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/harness.d.ts +56 -0
- package/.tsbuild/test/workflow/harness.d.ts.map +1 -0
- package/.tsbuild/test/workflow/oauth.test.d.ts +2 -0
- package/.tsbuild/test/workflow/oauth.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/resolvers.test.d.ts +2 -0
- package/.tsbuild/test/workflow/resolvers.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
- package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts +2 -0
- package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/webhook-signed.test.d.ts +2 -0
- package/.tsbuild/test/workflow/webhook-signed.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts +2 -0
- package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts +2 -0
- package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
- package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
- package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-read-gate.test.d.ts +2 -0
- package/.tsbuild/test/workflow-read-gate.test.d.ts.map +1 -0
- package/.tsbuild/test/workflow-runtime.test.d.ts +2 -0
- package/.tsbuild/test/workflow-runtime.test.d.ts.map +1 -0
- package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
- package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
- package/.tsbuild/tsconfig.tsbuildinfo +1 -1
- package/.tsbuild/tsdown.config.d.ts.map +1 -1
- package/.tsbuild/vitest.config.d.ts.map +1 -1
- package/Auth.md +144 -2
- package/CHANGELOG.md +1057 -0
- package/README.md +55 -14
- package/dist/boot.d.mts +46 -0
- package/dist/boot.d.mts.map +1 -0
- package/dist/boot.mjs +67 -0
- package/dist/boot.mjs.map +1 -0
- package/dist/config/from-env.d.mts +15 -0
- package/dist/config/from-env.d.mts.map +1 -0
- package/dist/config/from-env.mjs +4 -0
- package/dist/config/schema.d.mts +3 -0
- package/dist/config/schema.mjs +4 -0
- package/dist/from-env-BO0n9R02.mjs +401 -0
- package/dist/from-env-BO0n9R02.mjs.map +1 -0
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +76 -79
- package/dist/index.mjs.map +1 -1
- package/dist/install-packages.d.mts +2 -1
- package/dist/install-packages.mjs +2 -2
- package/dist/install-packages.mjs.map +1 -1
- package/dist/mcp-SsBK14Jm.mjs +32 -0
- package/dist/mcp-SsBK14Jm.mjs.map +1 -0
- package/dist/migrate.d.mts +2 -1
- package/dist/migrate.mjs +7 -5
- package/dist/migrate.mjs.map +1 -1
- package/dist/plugin-host-C1sYEm8g.d.mts +46 -0
- package/dist/plugin-host-C1sYEm8g.d.mts.map +1 -0
- package/dist/plugins/attachments.d.mts +33 -0
- package/dist/plugins/attachments.d.mts.map +1 -0
- package/dist/plugins/attachments.mjs +4 -0
- package/dist/plugins/mcp.d.mts +31 -0
- package/dist/plugins/mcp.d.mts.map +1 -0
- package/dist/plugins/mcp.mjs +4 -0
- package/dist/plugins/privacy.d.mts +22 -0
- package/dist/plugins/privacy.d.mts.map +1 -0
- package/dist/plugins/privacy.mjs +4 -0
- package/dist/plugins/workflows.d.mts +79 -0
- package/dist/plugins/workflows.d.mts.map +1 -0
- package/dist/plugins/workflows.mjs +4 -0
- package/dist/postgres-url-DTDhBjXX.mjs +11 -0
- package/dist/postgres-url-DTDhBjXX.mjs.map +1 -0
- package/dist/privacy-BO8Q25xI.mjs +44 -0
- package/dist/privacy-BO8Q25xI.mjs.map +1 -0
- package/dist/privacy-DxIJGH4P.mjs +91 -0
- package/dist/privacy-DxIJGH4P.mjs.map +1 -0
- package/dist/renown-layers-COZsrGQT.mjs +40 -0
- package/dist/renown-layers-COZsrGQT.mjs.map +1 -0
- package/dist/schema-0g_QdOWn.mjs +267 -0
- package/dist/schema-0g_QdOWn.mjs.map +1 -0
- package/dist/schema-ivUWPQOn.d.mts +202 -0
- package/dist/schema-ivUWPQOn.d.mts.map +1 -0
- package/dist/server-eNNf230W.mjs +2518 -0
- package/dist/server-eNNf230W.mjs.map +1 -0
- package/dist/server.d.mts +270 -48
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +5 -5
- package/dist/utils-BbV_nodR.mjs +119 -0
- package/dist/utils-BbV_nodR.mjs.map +1 -0
- package/dist/utils.d.mts +12 -5
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +4 -3
- package/dist/worker-pool-BWHBLBJd.mjs +304 -0
- package/dist/worker-pool-BWHBLBJd.mjs.map +1 -0
- package/dist/workflow-runtime-7GaJ13c9.d.mts +30 -0
- package/dist/workflow-runtime-7GaJ13c9.d.mts.map +1 -0
- package/dist/workflows-5Chwj98o.mjs +1440 -0
- package/dist/workflows-5Chwj98o.mjs.map +1 -0
- package/package.json +68 -23
- package/scripts/install-packages.sh +0 -0
- package/test/attachment-reference-read-model.test.ts +28 -20
- package/test/attachments/auth.test.ts +85 -0
- package/test/attachments/byte-grants.test.ts +290 -0
- package/test/attachments/byte-routes-read-gate.integration.test.ts +314 -0
- package/test/attachments/download-target.test.ts +211 -32
- package/test/attachments/index.test.ts +47 -4
- package/test/attachments/reserve-metadata-parity.test.ts +96 -0
- package/test/attachments/routes-integration.test.ts +60 -4
- package/test/attachments/routes.test.ts +297 -35
- package/test/attachments/service-config.test.ts +41 -19
- package/test/attachments/url-signer.test.ts +105 -0
- package/test/boot-auth-config.test.ts +109 -0
- package/test/boot-create-signature-policy.test.ts +110 -0
- package/test/boot-env-isolation.test.ts +26 -0
- package/test/boot-helpers.ts +19 -0
- package/test/boot-inspection-enabled.test.ts +140 -0
- package/test/boot-mcp-auth.test.ts +449 -0
- package/test/boot-mcp-enabled.test.ts +119 -0
- package/test/boot-port-zero.test.ts +101 -0
- package/test/boot-preconditions.test.ts +200 -0
- package/test/boot-privacy.test.ts +232 -0
- package/test/boot-unsupported-stored-documents.test.ts +214 -0
- package/test/boot-without-renown.test.ts +83 -0
- package/test/boot-without-require-signatures.test.ts +76 -0
- package/test/boot-worker-trust-policy.test.ts +91 -0
- package/test/boot-workflow-singleton-conflict.test.ts +109 -0
- package/test/boot-workflow-singleton-retry.test.ts +122 -0
- package/test/builder-defaults.test.ts +80 -0
- package/test/config-from-env.test.ts +758 -0
- package/test/config-merge.test.ts +215 -0
- package/test/config-schema.test.ts +128 -0
- package/test/create-signature-policy.test.ts +69 -0
- package/test/default-drive-protocol-versions.test.ts +78 -0
- package/test/default-drive-slug.test.ts +97 -0
- package/test/durable-node-fs-pg16.test.ts +80 -0
- package/test/fatal-shutdown.test.ts +165 -0
- package/test/goldens/packages.graphql +797 -0
- package/test/goldens/privacy.graphql +853 -0
- package/test/goldens/reactor-drive-nodes.graphql +827 -0
- package/test/goldens/resolvers.json +321 -0
- package/test/goldens/supergraph.graphql +5636 -0
- package/test/goldens/workflow-runtime.graphql +1403 -0
- package/test/inspection-throttle.test.ts +23 -0
- package/test/observability-signals.test.ts +131 -0
- package/test/permissions-backfill.test.ts +486 -0
- package/test/pglite-dialect.test.ts +237 -2
- package/test/pglite-preflight.test.ts +313 -0
- package/test/pglite-storage-dirs.test.ts +113 -0
- package/test/pglite-store.test.ts +264 -0
- package/test/pglite-version.test.ts +9 -1
- package/test/plugin-config-schemas.test.ts +83 -0
- package/test/plugin-presence.test.ts +262 -0
- package/test/port-probe.test.ts +45 -0
- package/test/renown-storage-path.test.ts +40 -0
- package/test/renown-trust-policy.test.ts +313 -0
- package/test/served-schemas.test.ts +134 -0
- package/test/shutdown.test.ts +278 -0
- package/test/snapshot-writer.ts +94 -0
- package/test/store-lock.test.ts +65 -0
- package/test/two-switchboards.test.ts +146 -0
- package/test/unsupported-stored-documents.test.ts +72 -0
- package/test/worker-pool.test.ts +27 -1
- package/test/workflow/harness.ts +289 -0
- package/test/workflow/oauth.test.ts +214 -0
- package/test/workflow/resolvers.test.ts +357 -0
- package/test/workflow/runtime-host.test.ts +20 -0
- package/test/workflow/webhook-dedupe.test.ts +295 -0
- package/test/workflow/webhook-signed.test.ts +509 -0
- package/test/workflow/webhook-trigger-test.test.ts +82 -0
- package/test/workflow/webhook-unsigned.test.ts +345 -0
- package/test/workflow-reactor-access.test.ts +384 -0
- package/test/workflow-reactor-enforcement.test.ts +808 -0
- package/test/workflow-read-gate.test.ts +213 -0
- package/test/workflow-runtime.test.ts +1135 -0
- package/test/workflows-plugin-config.test.ts +77 -0
- package/tsconfig.json +11 -2
- package/tsdown.config.ts +12 -1
- package/vitest.config.ts +65 -0
- package/dist/server-CiKkVE6g.mjs +0 -1555
- package/dist/server-CiKkVE6g.mjs.map +0 -1
- package/dist/utils-Baw7rThP.mjs +0 -80
- package/dist/utils-Baw7rThP.mjs.map +0 -1
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ThreadedPGlite,
|
|
3
|
+
type PGliteAbortListener,
|
|
4
|
+
type PGliteExitListener,
|
|
5
|
+
type PGliteLike,
|
|
6
|
+
} from "@powerhousedao/reactor-api/pglite-node";
|
|
7
|
+
import type { ILogger } from "document-model";
|
|
8
|
+
import { EventEmitter } from "node:events";
|
|
9
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
13
|
+
import { installFatalErrorShutdown } from "../src/fatal-shutdown.mjs";
|
|
14
|
+
import {
|
|
15
|
+
createPgliteStoreFactory,
|
|
16
|
+
routePgliteWorkerFailures,
|
|
17
|
+
terminateOnPoisoned,
|
|
18
|
+
type PgliteStoreOptions,
|
|
19
|
+
vacuumFullTimingFromEnv,
|
|
20
|
+
} from "../src/pglite-store.mjs";
|
|
21
|
+
|
|
22
|
+
const BOOT = 90_000;
|
|
23
|
+
|
|
24
|
+
function stubLogger() {
|
|
25
|
+
const logger = {
|
|
26
|
+
verbose: vi.fn(),
|
|
27
|
+
debug: vi.fn(),
|
|
28
|
+
info: vi.fn(),
|
|
29
|
+
warn: vi.fn(),
|
|
30
|
+
error: vi.fn(),
|
|
31
|
+
child: vi.fn(),
|
|
32
|
+
};
|
|
33
|
+
logger.child.mockReturnValue(logger);
|
|
34
|
+
return logger;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A process stand-in with the fatal shutdown installed; `kill` records it. */
|
|
38
|
+
function fatalProcess() {
|
|
39
|
+
const proc = Object.assign(new EventEmitter(), {
|
|
40
|
+
pid: 4321,
|
|
41
|
+
exitCode: undefined as number | undefined,
|
|
42
|
+
kill: vi.fn(() => true),
|
|
43
|
+
exit: vi.fn(),
|
|
44
|
+
});
|
|
45
|
+
installFatalErrorShutdown(stubLogger() as unknown as ILogger, proc as never);
|
|
46
|
+
return proc;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class FakeWorkerEvents {
|
|
50
|
+
abort: PGliteAbortListener = () => undefined;
|
|
51
|
+
exit: PGliteExitListener = () => undefined;
|
|
52
|
+
onAbort(listener: PGliteAbortListener): () => void {
|
|
53
|
+
this.abort = listener;
|
|
54
|
+
return () => undefined;
|
|
55
|
+
}
|
|
56
|
+
onExit(listener: PGliteExitListener): () => void {
|
|
57
|
+
this.exit = listener;
|
|
58
|
+
return () => undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
describe("routePgliteWorkerFailures", () => {
|
|
63
|
+
it("sends an abort to the fatal shutdown", () => {
|
|
64
|
+
const proc = fatalProcess();
|
|
65
|
+
const events = new FakeWorkerEvents();
|
|
66
|
+
routePgliteWorkerFailures(
|
|
67
|
+
events,
|
|
68
|
+
stubLogger() as unknown as ILogger,
|
|
69
|
+
proc as never,
|
|
70
|
+
);
|
|
71
|
+
events.abort("PGlite aborted: disk full");
|
|
72
|
+
expect(proc.kill).toHaveBeenCalledWith(4321, "SIGTERM");
|
|
73
|
+
expect(proc.exitCode).toBe(1);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("sends an unexpected exit to the fatal shutdown, but not a clean close", () => {
|
|
77
|
+
const proc = fatalProcess();
|
|
78
|
+
const events = new FakeWorkerEvents();
|
|
79
|
+
routePgliteWorkerFailures(
|
|
80
|
+
events,
|
|
81
|
+
stubLogger() as unknown as ILogger,
|
|
82
|
+
proc as never,
|
|
83
|
+
);
|
|
84
|
+
events.exit(null);
|
|
85
|
+
expect(proc.kill).not.toHaveBeenCalled();
|
|
86
|
+
events.exit(new Error("worker exited with code 1"));
|
|
87
|
+
expect(proc.kill).toHaveBeenCalledWith(4321, "SIGTERM");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("logs when no fatal shutdown is installed", () => {
|
|
91
|
+
const logger = stubLogger();
|
|
92
|
+
const events = new FakeWorkerEvents();
|
|
93
|
+
routePgliteWorkerFailures(events, logger as unknown as ILogger);
|
|
94
|
+
events.exit(new Error("gone"));
|
|
95
|
+
expect(logger.error).toHaveBeenCalledWith(
|
|
96
|
+
"PGlite worker exited: @error",
|
|
97
|
+
expect.any(Error),
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("terminateOnPoisoned", () => {
|
|
103
|
+
it("reports the poisoning, then stops the store's worker", () => {
|
|
104
|
+
const calls: string[] = [];
|
|
105
|
+
const options = terminateOnPoisoned(
|
|
106
|
+
{ onPoisoned: () => calls.push("reported"), statementTimeoutMs: 5 },
|
|
107
|
+
{
|
|
108
|
+
terminate: () => {
|
|
109
|
+
calls.push("terminated");
|
|
110
|
+
return Promise.resolve();
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
expect(options.statementTimeoutMs).toBe(5);
|
|
115
|
+
options.onPoisoned?.(new Error("hung"));
|
|
116
|
+
expect(calls).toEqual(["reported", "terminated"]);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("createPgliteStoreFactory", () => {
|
|
121
|
+
const open: PGliteLike[] = [];
|
|
122
|
+
let dataDir: string | undefined;
|
|
123
|
+
|
|
124
|
+
afterEach(async () => {
|
|
125
|
+
for (const store of open.splice(0)) {
|
|
126
|
+
if (store instanceof ThreadedPGlite) {
|
|
127
|
+
await store.terminate();
|
|
128
|
+
} else if (!store.closed) {
|
|
129
|
+
await store.close();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (dataDir) await rm(dataDir, { recursive: true, force: true });
|
|
133
|
+
dataDir = undefined;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
function options(
|
|
137
|
+
overrides: Partial<PgliteStoreOptions>,
|
|
138
|
+
): PgliteStoreOptions & { logger: ReturnType<typeof stubLogger> } {
|
|
139
|
+
const logger = stubLogger();
|
|
140
|
+
return {
|
|
141
|
+
label: "read-storage",
|
|
142
|
+
major: 17,
|
|
143
|
+
parsers: "utc-timestamps",
|
|
144
|
+
inMemory: true,
|
|
145
|
+
threaded: false,
|
|
146
|
+
...overrides,
|
|
147
|
+
logger: logger as unknown as ILogger,
|
|
148
|
+
} as PgliteStoreOptions & { logger: ReturnType<typeof stubLogger> };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function utcTimestamp(store: PGliteLike): Promise<string> {
|
|
152
|
+
const { rows } = await store.query<{ at: Date }>(
|
|
153
|
+
"select '2024-01-02 03:04:05'::timestamp as at",
|
|
154
|
+
);
|
|
155
|
+
return rows[0].at.toISOString();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
it(
|
|
159
|
+
"opens the store in the main thread when not threaded",
|
|
160
|
+
async () => {
|
|
161
|
+
const opts = options({ threaded: false });
|
|
162
|
+
const store = (await createPgliteStoreFactory(opts))("unused");
|
|
163
|
+
open.push(store);
|
|
164
|
+
expect(store).not.toBeInstanceOf(ThreadedPGlite);
|
|
165
|
+
expect(await utcTimestamp(store)).toBe("2024-01-02T03:04:05.000Z");
|
|
166
|
+
expect(opts.logger.info).toHaveBeenCalledWith(
|
|
167
|
+
"read-storage: PGlite (PG17) in the main thread, in memory [PH_PGLITE_IN_MEMORY=1]",
|
|
168
|
+
);
|
|
169
|
+
},
|
|
170
|
+
BOOT,
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
it(
|
|
174
|
+
"opens the store in a worker thread when threaded",
|
|
175
|
+
async () => {
|
|
176
|
+
const opts = options({ threaded: true });
|
|
177
|
+
const store = (await createPgliteStoreFactory(opts))("unused");
|
|
178
|
+
open.push(store);
|
|
179
|
+
expect(store).toBeInstanceOf(ThreadedPGlite);
|
|
180
|
+
expect(await utcTimestamp(store)).toBe("2024-01-02T03:04:05.000Z");
|
|
181
|
+
expect(opts.logger.info).toHaveBeenCalledWith(
|
|
182
|
+
"read-storage: PGlite (PG17) in a worker thread (memory limit 1024 MB), in memory [PH_PGLITE_IN_MEMORY=1]",
|
|
183
|
+
);
|
|
184
|
+
},
|
|
185
|
+
BOOT,
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
it(
|
|
189
|
+
"takes the fatal shutdown when a store's worker dies",
|
|
190
|
+
async () => {
|
|
191
|
+
const proc = fatalProcess();
|
|
192
|
+
const opts = options({ threaded: true, proc: proc as never });
|
|
193
|
+
const store = (await createPgliteStoreFactory(opts))("unused");
|
|
194
|
+
open.push(store);
|
|
195
|
+
await store.waitReady;
|
|
196
|
+
await (store as ThreadedPGlite).terminate();
|
|
197
|
+
await vi.waitFor(() =>
|
|
198
|
+
expect(proc.kill).toHaveBeenCalledWith(4321, "SIGTERM"),
|
|
199
|
+
);
|
|
200
|
+
},
|
|
201
|
+
BOOT,
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
it(
|
|
205
|
+
"writes a durable store in a worker that the main thread reads back",
|
|
206
|
+
async () => {
|
|
207
|
+
dataDir = await mkdtemp(join(tmpdir(), "sb-pglite-store-"));
|
|
208
|
+
const dir = join(dataDir, "read-storage");
|
|
209
|
+
const threaded = (
|
|
210
|
+
await createPgliteStoreFactory(
|
|
211
|
+
options({ threaded: true, inMemory: false, parsers: "none" }),
|
|
212
|
+
)
|
|
213
|
+
)(dir);
|
|
214
|
+
await threaded.exec("create table kept (v text)");
|
|
215
|
+
await threaded.query("insert into kept values ($1)", ["from-worker"]);
|
|
216
|
+
await threaded.close();
|
|
217
|
+
|
|
218
|
+
const mainThread = (
|
|
219
|
+
await createPgliteStoreFactory(
|
|
220
|
+
options({ threaded: false, inMemory: false, parsers: "none" }),
|
|
221
|
+
)
|
|
222
|
+
)(dir);
|
|
223
|
+
open.push(mainThread);
|
|
224
|
+
const { rows } = await mainThread.query<{ v: string }>(
|
|
225
|
+
"select v from kept",
|
|
226
|
+
);
|
|
227
|
+
expect(rows).toEqual([{ v: "from-worker" }]);
|
|
228
|
+
await mainThread.close();
|
|
229
|
+
},
|
|
230
|
+
BOOT * 2,
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe("vacuumFullTimingFromEnv", () => {
|
|
235
|
+
it("reads both timings, and leaves unset or invalid values to the defaults", () => {
|
|
236
|
+
expect(
|
|
237
|
+
vacuumFullTimingFromEnv({
|
|
238
|
+
PH_PGLITE_VACUUM_FULL: "quiet",
|
|
239
|
+
PH_PGLITE_VACUUM_FULL_QUIET_MS: "0",
|
|
240
|
+
PH_PGLITE_VACUUM_FULL_MAX_DEFER_MS: "600000",
|
|
241
|
+
}),
|
|
242
|
+
).toEqual({ vacuumFullQuietMs: 0, vacuumFullMaxDeferMs: 600_000 });
|
|
243
|
+
expect(vacuumFullTimingFromEnv({})).toEqual({ vacuumFullAboveBytes: 0 });
|
|
244
|
+
expect(
|
|
245
|
+
vacuumFullTimingFromEnv({
|
|
246
|
+
PH_PGLITE_VACUUM_FULL: "quiet",
|
|
247
|
+
PH_PGLITE_VACUUM_FULL_QUIET_MS: "soon",
|
|
248
|
+
PH_PGLITE_VACUUM_FULL_MAX_DEFER_MS: "-5",
|
|
249
|
+
}),
|
|
250
|
+
).toEqual({});
|
|
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
|
+
});
|
|
264
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { parseForcePgVersion } from "../src/pglite-version.js";
|
|
2
|
+
import { openForVerify, parseForcePgVersion } from "../src/pglite-version.js";
|
|
3
3
|
|
|
4
4
|
describe("parseForcePgVersion", () => {
|
|
5
5
|
it("returns null for undefined", () => {
|
|
@@ -35,3 +35,11 @@ describe("parseForcePgVersion", () => {
|
|
|
35
35
|
expect(() => parseForcePgVersion("16.5")).toThrow(/PH_FORCE_PG_VERSION/);
|
|
36
36
|
});
|
|
37
37
|
});
|
|
38
|
+
|
|
39
|
+
describe("openForVerify", () => {
|
|
40
|
+
it("throws on an unsupported major before touching disk", async () => {
|
|
41
|
+
await expect(openForVerify(15, "/nonexistent/pglite-dir")).rejects.toThrow(
|
|
42
|
+
/PG_VERSION=15 is not supported/,
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { PluginEntry } from "@powerhousedao/host-core";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { attachmentsPlugin } from "../src/plugins/attachments.mjs";
|
|
4
|
+
import { mcpPlugin } from "../src/plugins/mcp.mjs";
|
|
5
|
+
import { privacyPlugin } from "../src/plugins/privacy.mjs";
|
|
6
|
+
import { workflowsPlugin } from "../src/plugins/workflows.mjs";
|
|
7
|
+
|
|
8
|
+
// typed-host-configs 1.4 / T2 lane C: every built-in switchboard plugin
|
|
9
|
+
// declares a zod configSchema, and its module augments PHPluginConfigs so a
|
|
10
|
+
// host.js entry's config is typed by specifier.
|
|
11
|
+
|
|
12
|
+
/** The typed object form of a known specifier's entry. (The bare-string form
|
|
13
|
+
* and the untyped `{ module: string }` fallback do not narrow by specifier.) */
|
|
14
|
+
type EntryFor<M extends string> = Extract<PluginEntry, { module: M }>;
|
|
15
|
+
|
|
16
|
+
describe("switchboard plugin configSchemas", () => {
|
|
17
|
+
it.each([
|
|
18
|
+
["mcp", mcpPlugin],
|
|
19
|
+
["attachments", attachmentsPlugin],
|
|
20
|
+
["privacy", privacyPlugin],
|
|
21
|
+
])("%s takes no config: {} passes, any key is a typo", (_, plugin) => {
|
|
22
|
+
expect(plugin.configSchema?.safeParse({}).success).toBe(true);
|
|
23
|
+
expect(plugin.configSchema?.safeParse({ enabled: true }).success).toBe(
|
|
24
|
+
false,
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("workflows rejects a typo'd key and accepts drive: false", () => {
|
|
29
|
+
expect(
|
|
30
|
+
workflowsPlugin.configSchema?.safeParse({ drive: false }).success,
|
|
31
|
+
).toBe(true);
|
|
32
|
+
expect(
|
|
33
|
+
workflowsPlugin.configSchema?.safeParse({ drives: false }).success,
|
|
34
|
+
).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("PHPluginConfigs augmentation (type level)", () => {
|
|
39
|
+
it("types the switchboard workflows entry: drive is { slug, name, … } | false", () => {
|
|
40
|
+
const entries: PluginEntry[] = [
|
|
41
|
+
"@powerhousedao/switchboard/plugins/mcp",
|
|
42
|
+
{
|
|
43
|
+
module: "@powerhousedao/switchboard/plugins/workflows",
|
|
44
|
+
config: { drive: { slug: "workflows", name: "Workflows" } },
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
module: "@powerhousedao/switchboard/plugins/workflows",
|
|
48
|
+
config: { drive: false },
|
|
49
|
+
},
|
|
50
|
+
];
|
|
51
|
+
// Positive cases pin that the entry type exists (not `never`), so the
|
|
52
|
+
// expect-errors below fail for the right reason.
|
|
53
|
+
const declared: EntryFor<"@powerhousedao/switchboard/plugins/workflows"> = {
|
|
54
|
+
module: "@powerhousedao/switchboard/plugins/workflows",
|
|
55
|
+
config: { drive: { slug: "flows", name: "Flows" } },
|
|
56
|
+
};
|
|
57
|
+
const off: PHPluginConfigs["@powerhousedao/switchboard/plugins/workflows"] =
|
|
58
|
+
{ drive: false };
|
|
59
|
+
const urlDrive: EntryFor<"@powerhousedao/switchboard/plugins/workflows"> = {
|
|
60
|
+
module: "@powerhousedao/switchboard/plugins/workflows",
|
|
61
|
+
// @ts-expect-error -- the switchboard workflows drive is a declaration, not a URL (that is Connect's)
|
|
62
|
+
config: { drive: "http://localhost:4001/d/workflows" },
|
|
63
|
+
};
|
|
64
|
+
const typo: EntryFor<"@powerhousedao/switchboard/plugins/workflows"> = {
|
|
65
|
+
module: "@powerhousedao/switchboard/plugins/workflows",
|
|
66
|
+
// @ts-expect-error -- `drives` is not a workflows config key
|
|
67
|
+
config: { drives: false },
|
|
68
|
+
};
|
|
69
|
+
expect([entries, declared, off, urlDrive, typo]).toBeDefined();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("types the no-config plugins as taking no keys", () => {
|
|
73
|
+
const mcp: PHPluginConfigs["@powerhousedao/switchboard/plugins/mcp"] = {
|
|
74
|
+
// @ts-expect-error -- mcp takes no config
|
|
75
|
+
enabled: true,
|
|
76
|
+
};
|
|
77
|
+
const none: PHPluginConfigs["@powerhousedao/switchboard/plugins/privacy"] =
|
|
78
|
+
{};
|
|
79
|
+
const attachments: EntryFor<"@powerhousedao/switchboard/plugins/attachments"> =
|
|
80
|
+
{ module: "@powerhousedao/switchboard/plugins/attachments", config: {} };
|
|
81
|
+
expect([mcp, none, attachments]).toBeDefined();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import type { HostPlugin, ResolvedPlugin } from "@powerhousedao/host-core";
|
|
5
|
+
import type { ILogger } from "document-model";
|
|
6
|
+
import { describe, expect, it, vi } from "vitest";
|
|
7
|
+
import {
|
|
8
|
+
builtinSwitchboardPlugins,
|
|
9
|
+
createSwitchboardPluginHost,
|
|
10
|
+
} from "../src/plugin-host.mjs";
|
|
11
|
+
import type { SwitchboardConfigInput } from "../src/config/schema.js";
|
|
12
|
+
import type { startSwitchboard, SwitchboardDeps } from "../src/server.mjs";
|
|
13
|
+
import { bootSwitchboard } from "./boot-helpers.js";
|
|
14
|
+
|
|
15
|
+
function stubLogger(): ILogger {
|
|
16
|
+
const logger = {
|
|
17
|
+
verbose: vi.fn(),
|
|
18
|
+
debug: vi.fn(),
|
|
19
|
+
info: vi.fn(),
|
|
20
|
+
warn: vi.fn(),
|
|
21
|
+
error: vi.fn(),
|
|
22
|
+
child: vi.fn(),
|
|
23
|
+
};
|
|
24
|
+
logger.child.mockReturnValue(logger);
|
|
25
|
+
return logger as unknown as ILogger;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Boots with FEATURE_PLUGIN_HOST_ENABLED=1, sandboxing the env exactly like
|
|
30
|
+
* boot-mcp-enabled.test.ts: temp PH_REACTOR_DATABASE_URL, MCP_ENABLED cleared
|
|
31
|
+
* so it never leaks into the plugin-host composition under test, and every
|
|
32
|
+
* touched env var restored in `finally`. Workflows stay off by default here
|
|
33
|
+
* — case 5 below covers workflow presence on its own, lighter path.
|
|
34
|
+
*/
|
|
35
|
+
async function withPluginHostSwitchboard(
|
|
36
|
+
override: { config?: SwitchboardConfigInput; deps?: SwitchboardDeps },
|
|
37
|
+
run: (
|
|
38
|
+
switchboard: Awaited<ReturnType<typeof startSwitchboard>>,
|
|
39
|
+
) => Promise<void>,
|
|
40
|
+
): Promise<void> {
|
|
41
|
+
const tempRoot = await mkdtemp(
|
|
42
|
+
join(tmpdir(), "switchboard-plugin-presence-"),
|
|
43
|
+
);
|
|
44
|
+
const names = [
|
|
45
|
+
"PH_REACTOR_DATABASE_URL",
|
|
46
|
+
"MCP_ENABLED",
|
|
47
|
+
"FEATURE_PLUGIN_HOST_ENABLED",
|
|
48
|
+
];
|
|
49
|
+
const previous = names.map((name) => [name, process.env[name]] as const);
|
|
50
|
+
process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
|
|
51
|
+
delete process.env.MCP_ENABLED;
|
|
52
|
+
process.env.FEATURE_PLUGIN_HOST_ENABLED = "1";
|
|
53
|
+
let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
switchboard = await bootSwitchboard(
|
|
57
|
+
{
|
|
58
|
+
server: { port: 0 },
|
|
59
|
+
storage: { readModel: { url: join(tempRoot, "read-model") } },
|
|
60
|
+
features: { workflows: false, ...override.config?.features },
|
|
61
|
+
packages: { disableLocal: true },
|
|
62
|
+
identity: { keypairPath: join(tempRoot, "identity.json") },
|
|
63
|
+
drive: {
|
|
64
|
+
slug: "default-drive",
|
|
65
|
+
global: { name: "Default Drive" },
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
{ logger: stubLogger(), ...override.deps },
|
|
69
|
+
);
|
|
70
|
+
await run(switchboard);
|
|
71
|
+
await switchboard.shutdown();
|
|
72
|
+
switchboard = undefined;
|
|
73
|
+
} finally {
|
|
74
|
+
await switchboard?.shutdown();
|
|
75
|
+
for (const [name, value] of previous) {
|
|
76
|
+
if (value === undefined) {
|
|
77
|
+
delete process.env[name];
|
|
78
|
+
} else {
|
|
79
|
+
process.env[name] = value;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
await rm(tempRoot, { recursive: true, force: true });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function postMcp(port: number): Promise<number> {
|
|
87
|
+
const response = await fetch(`http://127.0.0.1:${port}/mcp`, {
|
|
88
|
+
method: "POST",
|
|
89
|
+
headers: {
|
|
90
|
+
"content-type": "application/json",
|
|
91
|
+
accept: "application/json, text/event-stream",
|
|
92
|
+
},
|
|
93
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "ping" }),
|
|
94
|
+
});
|
|
95
|
+
await response.body?.cancel();
|
|
96
|
+
return response.status;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function getReservation(port: number): Promise<number> {
|
|
100
|
+
const response = await fetch(
|
|
101
|
+
`http://127.0.0.1:${port}/attachments/reservations/does-not-matter`,
|
|
102
|
+
);
|
|
103
|
+
await response.body?.cancel();
|
|
104
|
+
return response.status;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
describe("plugin-host composition presence (FEATURE_PLUGIN_HOST_ENABLED)", () => {
|
|
108
|
+
it("DEFAULT composition: mcp mounted, attachments present", async () => {
|
|
109
|
+
await withPluginHostSwitchboard({}, async (switchboard) => {
|
|
110
|
+
expect(switchboard.mcpEnabled).toBe(true);
|
|
111
|
+
expect(await postMcp(switchboard.port)).not.toBe(404);
|
|
112
|
+
expect(switchboard.attachmentService).toBeDefined();
|
|
113
|
+
});
|
|
114
|
+
}, 60_000);
|
|
115
|
+
|
|
116
|
+
it("EMPTY composition ([]): attachments absent", async () => {
|
|
117
|
+
await withPluginHostSwitchboard(
|
|
118
|
+
{ deps: { pluginComposition: [] } },
|
|
119
|
+
async (switchboard) => {
|
|
120
|
+
expect(switchboard.attachmentService).toBeUndefined();
|
|
121
|
+
expect(await getReservation(switchboard.port)).toBe(404);
|
|
122
|
+
},
|
|
123
|
+
);
|
|
124
|
+
}, 60_000);
|
|
125
|
+
|
|
126
|
+
// Presence is activation (R2.6c fix): flag-on, startSwitchboard skips the
|
|
127
|
+
// classic mcp/workflows resolvers and initServer normalizes unset values to
|
|
128
|
+
// OFF after transformServerOptions — an absent plugin actually deactivates
|
|
129
|
+
// its subsystem.
|
|
130
|
+
it("EMPTY composition ([]): /mcp does not mount — presence is activation", async () => {
|
|
131
|
+
await withPluginHostSwitchboard(
|
|
132
|
+
{ deps: { pluginComposition: [] } },
|
|
133
|
+
async (switchboard) => {
|
|
134
|
+
expect(switchboard.mcpEnabled).toBe(false);
|
|
135
|
+
expect(await postMcp(switchboard.port)).toBe(404);
|
|
136
|
+
},
|
|
137
|
+
);
|
|
138
|
+
}, 60_000);
|
|
139
|
+
|
|
140
|
+
it("SUBSET composition (mcp only): mcp mounted, attachments absent", async () => {
|
|
141
|
+
const mcpOnly = builtinSwitchboardPlugins().filter((resolved) =>
|
|
142
|
+
resolved.entry.module.includes("/plugins/mcp"),
|
|
143
|
+
);
|
|
144
|
+
expect(mcpOnly).toHaveLength(1);
|
|
145
|
+
await withPluginHostSwitchboard(
|
|
146
|
+
{ deps: { pluginComposition: mcpOnly } },
|
|
147
|
+
async (switchboard) => {
|
|
148
|
+
expect(switchboard.mcpEnabled).toBe(true);
|
|
149
|
+
expect(await postMcp(switchboard.port)).not.toBe(404);
|
|
150
|
+
expect(switchboard.attachmentService).toBeUndefined();
|
|
151
|
+
expect(await getReservation(switchboard.port)).toBe(404);
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
}, 60_000);
|
|
155
|
+
|
|
156
|
+
it("an explicit mcp: false option still wins over the default composition", async () => {
|
|
157
|
+
await withPluginHostSwitchboard(
|
|
158
|
+
{ config: { features: { mcp: false } } },
|
|
159
|
+
async (switchboard) => {
|
|
160
|
+
expect(switchboard.mcpEnabled).toBe(false);
|
|
161
|
+
expect(await postMcp(switchboard.port)).toBe(404);
|
|
162
|
+
},
|
|
163
|
+
);
|
|
164
|
+
}, 60_000);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe("plugin-host workflows presence (transformServerOptions)", () => {
|
|
168
|
+
// A real boot's workflows.enabled is resolved classically (env/config,
|
|
169
|
+
// default OFF) in startSwitchboard BEFORE the plugin host is even
|
|
170
|
+
// constructed (server.mts resolveWorkflowsEnabled ~line 1545, pluginHost
|
|
171
|
+
// gate at ~line 407) — by the time the workflows plugin's
|
|
172
|
+
// transformServerOptions hook runs, options.workflows.enabled is already a
|
|
173
|
+
// definite boolean, so its "unless explicit false" short-circuit can never
|
|
174
|
+
// observe an "unset" value through that path. Exercising the hook directly
|
|
175
|
+
// against createSwitchboardPluginHost, as suggested as a fallback, is the
|
|
176
|
+
// only way to observe the plugin's own force-true behavior in isolation —
|
|
177
|
+
// see the suspected-bug note in the final report.
|
|
178
|
+
it("forces workflows.enabled true when the plugin is present, leaves it alone when absent", async () => {
|
|
179
|
+
const withWorkflowsPlugin = createSwitchboardPluginHost(
|
|
180
|
+
{ logger: stubLogger(), options: {}, renown: null },
|
|
181
|
+
builtinSwitchboardPlugins(),
|
|
182
|
+
);
|
|
183
|
+
const resolvedWith = await withWorkflowsPlugin.transformOptions<
|
|
184
|
+
Record<string, unknown>
|
|
185
|
+
>({});
|
|
186
|
+
expect(
|
|
187
|
+
(resolvedWith.workflows as { enabled?: boolean } | undefined)?.enabled,
|
|
188
|
+
).toBe(true);
|
|
189
|
+
|
|
190
|
+
const withoutAnyPlugin = createSwitchboardPluginHost(
|
|
191
|
+
{ logger: stubLogger(), options: {}, renown: null },
|
|
192
|
+
[],
|
|
193
|
+
);
|
|
194
|
+
const resolvedWithout = await withoutAnyPlugin.transformOptions<
|
|
195
|
+
Record<string, unknown>
|
|
196
|
+
>({});
|
|
197
|
+
expect(resolvedWithout.workflows).toBeUndefined();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("still honors an explicit workflows.enabled: false even with the plugin present", async () => {
|
|
201
|
+
const host = createSwitchboardPluginHost(
|
|
202
|
+
{ logger: stubLogger(), options: {}, renown: null },
|
|
203
|
+
builtinSwitchboardPlugins(),
|
|
204
|
+
);
|
|
205
|
+
const resolved = await host.transformOptions<Record<string, unknown>>({
|
|
206
|
+
workflows: { enabled: false },
|
|
207
|
+
});
|
|
208
|
+
expect(
|
|
209
|
+
(resolved.workflows as { enabled?: boolean } | undefined)?.enabled,
|
|
210
|
+
).toBe(false);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe("plugin-host onShutdown ordering (runObserveHookSequential)", () => {
|
|
215
|
+
it("runs onShutdown sequentially, in registration order — not by whichever finishes first", async () => {
|
|
216
|
+
const order: string[] = [];
|
|
217
|
+
|
|
218
|
+
function fakePlugin(name: string, delayMs: number): HostPlugin {
|
|
219
|
+
return {
|
|
220
|
+
name,
|
|
221
|
+
appliesTo: "switchboard",
|
|
222
|
+
hooks: {
|
|
223
|
+
onShutdown: async () => {
|
|
224
|
+
if (delayMs > 0) {
|
|
225
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
226
|
+
}
|
|
227
|
+
order.push(name);
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function resolvedFake(plugin: HostPlugin): ResolvedPlugin {
|
|
234
|
+
return {
|
|
235
|
+
plugin,
|
|
236
|
+
provenance: "local",
|
|
237
|
+
entry: {
|
|
238
|
+
module: `fake:${plugin.name}`,
|
|
239
|
+
enabled: true,
|
|
240
|
+
verbs: "*",
|
|
241
|
+
config: {},
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Registered first but slower: if onShutdown ran in parallel (the way
|
|
247
|
+
// the other hook moments do, via Promise.all in runObserveHook), "b"
|
|
248
|
+
// would resolve first and the recorded order would be ["b", "a"].
|
|
249
|
+
// Sequential execution must still record registration order, ["a", "b"].
|
|
250
|
+
const first = fakePlugin("a", 50);
|
|
251
|
+
const second = fakePlugin("b", 0);
|
|
252
|
+
|
|
253
|
+
const host = createSwitchboardPluginHost(
|
|
254
|
+
{ logger: stubLogger(), options: {}, renown: null },
|
|
255
|
+
[resolvedFake(first), resolvedFake(second)],
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
await host.observe("onShutdown");
|
|
259
|
+
|
|
260
|
+
expect(order).toEqual(["a", "b"]);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import net from "node:net";
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { isPortAvailable } from "../src/server.mjs";
|
|
4
|
+
|
|
5
|
+
// A host without IPv6, e.g. a container: binding `::` fails before the port is tried.
|
|
6
|
+
function withoutIpv6() {
|
|
7
|
+
const { listen } = net.Server.prototype as unknown as {
|
|
8
|
+
listen: (this: net.Server, ...args: unknown[]) => net.Server;
|
|
9
|
+
};
|
|
10
|
+
return vi.spyOn(net.Server.prototype, "listen").mockImplementation(function (
|
|
11
|
+
this: net.Server,
|
|
12
|
+
...args: unknown[]
|
|
13
|
+
) {
|
|
14
|
+
const [options] = args as [{ host?: string } | undefined];
|
|
15
|
+
if (options?.host === "::") {
|
|
16
|
+
const error = Object.assign(new Error("address family not supported"), {
|
|
17
|
+
code: "EAFNOSUPPORT",
|
|
18
|
+
});
|
|
19
|
+
queueMicrotask(() => this.emit("error", error));
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
return listen.apply(this, args);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
vi.restoreAllMocks();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("isPortAvailable", () => {
|
|
31
|
+
it("probes IPv4 on a host without IPv6", async () => {
|
|
32
|
+
withoutIpv6();
|
|
33
|
+
const busy = net.createServer();
|
|
34
|
+
await new Promise<void>((resolve) =>
|
|
35
|
+
busy.listen({ port: 0, host: "0.0.0.0" }, resolve),
|
|
36
|
+
);
|
|
37
|
+
const { port } = busy.address() as net.AddressInfo;
|
|
38
|
+
try {
|
|
39
|
+
await expect(isPortAvailable(port)).resolves.toBe(false);
|
|
40
|
+
} finally {
|
|
41
|
+
await new Promise((resolve) => busy.close(resolve));
|
|
42
|
+
}
|
|
43
|
+
await expect(isPortAvailable(port)).resolves.toBe(true);
|
|
44
|
+
});
|
|
45
|
+
});
|