@powerhousedao/switchboard 6.2.3-dev.79 → 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.
Files changed (38) hide show
  1. package/.tsbuild/test/boot-helpers.d.ts +2 -2
  2. package/.tsbuild/test/boot-helpers.d.ts.map +1 -1
  3. package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
  4. package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
  5. package/.tsbuild/test/served-schemas.test.d.ts +2 -0
  6. package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
  7. package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
  8. package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
  9. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  10. package/CHANGELOG.md +27 -0
  11. package/dist/from-env-BO0n9R02.mjs +2 -2
  12. package/dist/from-env-BO0n9R02.mjs.map +1 -1
  13. package/dist/index.mjs +1 -1
  14. package/dist/plugins/attachments.mjs +3 -3
  15. package/dist/plugins/workflows.mjs +3 -3
  16. package/dist/{server-CtGBZlTg.mjs → server-eNNf230W.mjs} +58 -9
  17. package/dist/server-eNNf230W.mjs.map +1 -0
  18. package/dist/server.d.mts +10 -0
  19. package/dist/server.d.mts.map +1 -1
  20. package/dist/server.mjs +3 -3
  21. package/dist/workflow-runtime-7GaJ13c9.d.mts.map +1 -1
  22. package/dist/{workflows-QY9Y2eoD.mjs → workflows-5Chwj98o.mjs} +72 -3
  23. package/dist/workflows-5Chwj98o.mjs.map +1 -0
  24. package/package.json +16 -16
  25. package/test/boot-helpers.ts +3 -2
  26. package/test/boot-mcp-auth.test.ts +449 -0
  27. package/test/goldens/packages.graphql +797 -0
  28. package/test/goldens/privacy.graphql +853 -0
  29. package/test/goldens/reactor-drive-nodes.graphql +827 -0
  30. package/test/goldens/resolvers.json +321 -0
  31. package/test/goldens/supergraph.graphql +5636 -0
  32. package/test/goldens/workflow-runtime.graphql +1403 -0
  33. package/test/served-schemas.test.ts +134 -0
  34. package/test/workflow/runtime-host.test.ts +20 -0
  35. package/test/workflow-runtime.test.ts +70 -0
  36. package/vitest.config.ts +14 -4
  37. package/dist/server-CtGBZlTg.mjs.map +0 -1
  38. package/dist/workflows-QY9Y2eoD.mjs.map +0 -1
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Regenerate after an intended change, from the repository root. Removing the
3
+ * goldens first drops those of endpoints no longer served:
4
+ *
5
+ * rm -rf packages/reactor-api/test/graphql/goldens apps/switchboard/test/goldens && \
6
+ * UPDATE_GOLDENS=1 pnpm --workspace-concurrency=1 \
7
+ * --filter @powerhousedao/reactor-api --filter @powerhousedao/switchboard \
8
+ * exec vitest run served-schemas && pnpm exec oxfmt \
9
+ * packages/reactor-api/test/graphql/goldens apps/switchboard/test/goldens
10
+ */
11
+ import {
12
+ expectSchemaGolden,
13
+ jsonGolden,
14
+ recordServedSchemas,
15
+ type ServedEndpoints,
16
+ } from "@powerhousedao/reactor-api/test";
17
+ import type { ILogger } from "document-model";
18
+ import { mkdtemp, rm, writeFile } from "node:fs/promises";
19
+ import { tmpdir } from "node:os";
20
+ import { join } from "node:path";
21
+ import { fileURLToPath } from "node:url";
22
+ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
23
+ import type { startSwitchboard } from "../src/server.mjs";
24
+ import { bootSwitchboard } from "./boot-helpers.js";
25
+
26
+ const goldens = (file: string) =>
27
+ fileURLToPath(new URL(`./goldens/${file}`, import.meta.url));
28
+
29
+ const SUBGRAPHS = [
30
+ "packages",
31
+ "privacy",
32
+ "reactor-drive-nodes",
33
+ "workflow-runtime",
34
+ ];
35
+
36
+ const ADMIN = "0xadad00000000000000000000000000000000adad";
37
+ const SECRET = "switchboard-served-schemas-deployment-secret";
38
+
39
+ function stubLogger(): ILogger {
40
+ const logger = {
41
+ verbose: vi.fn(),
42
+ debug: vi.fn(),
43
+ info: vi.fn(),
44
+ warn: vi.fn(),
45
+ error: vi.fn(),
46
+ child: vi.fn(),
47
+ };
48
+ logger.child.mockReturnValue(logger);
49
+ return logger as unknown as ILogger;
50
+ }
51
+
52
+ const realFetch = globalThis.fetch;
53
+ let tempRoot: string;
54
+ let recorder: ReturnType<typeof recordServedSchemas> | undefined;
55
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
56
+ let served: ServedEndpoints;
57
+
58
+ beforeAll(async () => {
59
+ tempRoot = await mkdtemp(join(tmpdir(), "switchboard-served-schemas-"));
60
+ const configFile = join(tempRoot, "powerhouse.config.json");
61
+ await writeFile(configFile, "{}");
62
+ // The workflow runtime reads a hard-coded cloud piece catalog at boot.
63
+ vi.stubGlobal(
64
+ "fetch",
65
+ (input: string | URL | Request, init?: RequestInit) => {
66
+ const url = new URL(input instanceof Request ? input.url : String(input));
67
+ return url.hostname === "127.0.0.1"
68
+ ? realFetch(input, init)
69
+ : Promise.reject(new Error(`${url.host} is off limits to this test`));
70
+ },
71
+ );
72
+ recorder = recordServedSchemas();
73
+ // Every switchboard subgraph on, from this config alone: no inherited env,
74
+ // config file or user data. The registry URL only turns the packages
75
+ // subgraph on, and the Renown URL keeps discovery off the network.
76
+ switchboard = await bootSwitchboard(
77
+ {
78
+ server: { port: 0 },
79
+ storage: {
80
+ reactor: { url: join(tempRoot, "reactor") },
81
+ readModel: { url: join(tempRoot, "read-model") },
82
+ userDataDir: tempRoot,
83
+ },
84
+ auth: {
85
+ enabled: true,
86
+ admins: [ADMIN],
87
+ renown: { url: "http://127.0.0.1:9" },
88
+ },
89
+ features: {
90
+ workflows: true,
91
+ mcp: false,
92
+ privacy: { enabled: true, deploymentSecret: SECRET },
93
+ },
94
+ packages: { disableLocal: true, registryUrl: "http://127.0.0.1:9" },
95
+ identity: { keypairPath: join(tempRoot, "identity.json") },
96
+ configFile,
97
+ },
98
+ { logger: stubLogger() },
99
+ {},
100
+ );
101
+ served = await recorder.read(
102
+ (request) => realFetch(request),
103
+ `http://127.0.0.1:${switchboard.port}`,
104
+ SUBGRAPHS,
105
+ );
106
+ }, 120_000);
107
+
108
+ afterAll(async () => {
109
+ try {
110
+ await switchboard?.shutdown();
111
+ } finally {
112
+ recorder?.restore();
113
+ vi.unstubAllGlobals();
114
+ await rm(tempRoot, { recursive: true, force: true });
115
+ }
116
+ });
117
+
118
+ describe("switchboard served schemas", () => {
119
+ it.each(SUBGRAPHS)("serves /graphql/%s as its golden", async (name) => {
120
+ const schema = served.subgraphs.get(name);
121
+ expect(schema, `/graphql/${name} is not served`).toBeDefined();
122
+ await expectSchemaGolden(schema!, goldens(`${name}.graphql`));
123
+ });
124
+
125
+ it("serves /graphql as supergraph.graphql", async () => {
126
+ await expectSchemaGolden(served.supergraph, goldens("supergraph.graphql"));
127
+ });
128
+
129
+ it("binds the resolvers in resolvers.json", async () => {
130
+ expect(served.resolvers).toEqual(
131
+ await jsonGolden(goldens("resolvers.json"), served.resolvers),
132
+ );
133
+ });
134
+ });
@@ -0,0 +1,20 @@
1
+ import type { IAuthorizationService } from "@powerhousedao/reactor-api";
2
+ import type { WorkflowRuntimeService } from "@powerhousedao/reactor-workflow";
3
+ import { hostname } from "node:os";
4
+ import { describe, expect, it } from "vitest";
5
+ import { getResolvers } from "../../src/workflow/resolvers.js";
6
+
7
+ type Resolver = (...args: unknown[]) => unknown;
8
+
9
+ describe("the runtime's host", () => {
10
+ it("is the switchboard on this machine, the id apps record as runner", () => {
11
+ const resolvers = getResolvers(
12
+ {} as WorkflowRuntimeService,
13
+ {} as IAuthorizationService,
14
+ ) as Record<string, Record<string, Resolver>>;
15
+ expect(resolvers.WorkflowRuntimeQueries.host()).toEqual({
16
+ id: `switchboard@${hostname()}`,
17
+ label: hostname(),
18
+ });
19
+ });
20
+ });
@@ -38,6 +38,7 @@ import {
38
38
  composeWorkflowRuntime,
39
39
  assertWorkflowPackageLoadable,
40
40
  hostPrincipalOf,
41
+ indexQueryFor,
41
42
  isWorkflowSingletonConflict,
42
43
  retryWorkflowSingleton,
43
44
  workflowSingletonLossHandler,
@@ -916,6 +917,75 @@ describe("the host's reactor access", () => {
916
917
  });
917
918
  });
918
919
 
920
+ describe("a piece's index query through the host", () => {
921
+ const signer = {
922
+ app: { key: "did:key:zHost" },
923
+ user: { address: "0xhost", chainId: 5, networkId: "eip155" },
924
+ };
925
+ const request = {
926
+ subgraph: "key-index",
927
+ query: "{ keyIndex { find { key } } }",
928
+ variables: { k: ["a"] },
929
+ };
930
+ const manager = () => ({
931
+ executeSubgraph: vi.fn().mockResolvedValue({ data: { keyIndex: {} } }),
932
+ });
933
+ const sentUser = (m: ReturnType<typeof manager>) =>
934
+ (m.executeSubgraph.mock.calls[0] as [string, { user?: unknown }])[1].user;
935
+
936
+ it("asks as the run user on the host's chain, with the user's key", async () => {
937
+ const m = manager();
938
+ const query = indexQueryFor(m, {
939
+ signer,
940
+ } as unknown as InProcessReactorClientModule);
941
+ const result = await query(request, {
942
+ address: "0xrun",
943
+ subject: { address: "0xrun", key: "did:key:zRun" },
944
+ });
945
+ expect(result).toEqual({ data: { keyIndex: {} } });
946
+ expect(m.executeSubgraph).toHaveBeenCalledWith("key-index", {
947
+ query: request.query,
948
+ variables: request.variables,
949
+ user: {
950
+ address: "0xrun",
951
+ chainId: 5,
952
+ networkId: "eip155",
953
+ appKey: "did:key:zRun",
954
+ },
955
+ });
956
+ });
957
+
958
+ it("asks as the host itself for a run with no user, and anonymously without an identity", async () => {
959
+ const m = manager();
960
+ await indexQueryFor(m, {
961
+ signer,
962
+ } as unknown as InProcessReactorClientModule)(request, null);
963
+ expect(sentUser(m)).toEqual({
964
+ address: "0xhost",
965
+ chainId: 5,
966
+ networkId: "eip155",
967
+ appKey: "did:key:zHost",
968
+ });
969
+
970
+ const anonymous = manager();
971
+ await indexQueryFor(anonymous, {
972
+ signer: { app: { key: "did:key:zHost" }, user: {} },
973
+ } as unknown as InProcessReactorClientModule)(request, null);
974
+ expect(sentUser(anonymous)).toBeUndefined();
975
+ });
976
+
977
+ it("hands the subgraph's errors back as messages", async () => {
978
+ const m = {
979
+ executeSubgraph: vi.fn().mockResolvedValue({
980
+ data: null,
981
+ errors: [{ message: "nope", extensions: { code: "X" } }],
982
+ }),
983
+ };
984
+ const result = await indexQueryFor(m, undefined)(request, null);
985
+ expect(result).toEqual({ data: null, errors: [{ message: "nope" }] });
986
+ });
987
+ });
988
+
919
989
  // The GraphQL manager rides on the boot result without being on its public
920
990
  // type, and the subgraph registers late: poll rather than race it.
921
991
  async function pollWorkflowSubgraph(
package/vitest.config.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { readFileSync } from "node:fs";
2
+ import { createRequire } from "node:module";
2
3
  import { dirname, resolve } from "node:path";
3
4
  import { defineConfig } from "vitest/config";
4
5
 
@@ -31,10 +32,19 @@ const escape = (text: string) => text.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
31
32
 
32
33
  export default defineConfig({
33
34
  resolve: {
34
- alias: Object.entries(SOURCE_ALIASES).map(([name, path]) => ({
35
- find: new RegExp(`^${escape(name)}$`),
36
- replacement: resolve(packages, path),
37
- })),
35
+ alias: [
36
+ ...Object.entries(SOURCE_ALIASES).map(([name, path]) => ({
37
+ find: new RegExp(`^${escape(name)}$`),
38
+ replacement: resolve(packages, path),
39
+ })),
40
+ // Node resolves `graphql` to its CommonJS entry, the copy
41
+ // @apollo/subgraph requires. Vite would pick the ESM entry, a second
42
+ // copy, so tests would see errors production never sends.
43
+ {
44
+ find: /^graphql$/,
45
+ replacement: createRequire(import.meta.url).resolve("graphql"),
46
+ },
47
+ ],
38
48
  },
39
49
  plugins: [
40
50
  {