@powerhousedao/switchboard 6.2.3-dev.89 → 6.2.3-dev.90
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/boot-desktop-auth.test.d.ts +2 -0
- package/.tsbuild/test/boot-desktop-auth.test.d.ts.map +1 -0
- package/.tsbuild/test/delegated-publish.test.d.ts +2 -0
- package/.tsbuild/test/delegated-publish.test.d.ts.map +1 -0
- package/.tsbuild/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +35 -0
- package/dist/boot.d.mts +2 -2
- package/dist/boot.mjs +2 -2
- package/dist/config/from-env.d.mts +1 -1
- package/dist/config/from-env.mjs +3 -3
- package/dist/config/schema.d.mts +1 -1
- package/dist/config/schema.mjs +3 -3
- package/dist/{from-env-DuUwngOC.mjs → from-env-DJ4iQ3QZ.mjs} +5 -5
- package/dist/from-env-DJ4iQ3QZ.mjs.map +1 -0
- package/dist/index.mjs +3 -3
- package/dist/{plugin-host-C0bSz1Oe.d.mts → plugin-host-BHEUSn9G.d.mts} +2 -2
- package/dist/{plugin-host-C0bSz1Oe.d.mts.map → plugin-host-BHEUSn9G.d.mts.map} +1 -1
- package/dist/plugins/ai.d.mts +1 -1
- package/dist/plugins/ai.d.mts.map +1 -1
- package/dist/plugins/ai.mjs +68 -20
- package/dist/plugins/ai.mjs.map +1 -1
- package/dist/plugins/attachments.d.mts +1 -1
- package/dist/plugins/attachments.mjs +3 -3
- package/dist/plugins/mcp.d.mts +1 -1
- package/dist/plugins/privacy.d.mts +1 -1
- package/dist/plugins/workflows.d.mts +3 -3
- package/dist/plugins/workflows.mjs +3 -3
- package/dist/{schema-XEZrdmag.mjs → schema-1yj85yQD.mjs} +15 -5
- package/dist/schema-1yj85yQD.mjs.map +1 -0
- package/dist/{schema-DhempFjb.d.mts → schema-ugrJupVH.d.mts} +5 -1
- package/dist/schema-ugrJupVH.d.mts.map +1 -0
- package/dist/{server-BJk9-9n4.mjs → server-CgDvrUqE.mjs} +129 -30
- package/dist/server-CgDvrUqE.mjs.map +1 -0
- package/dist/server.d.mts +8 -306
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +4 -4
- package/dist/types-DzO3_320.d.mts +308 -0
- package/dist/types-DzO3_320.d.mts.map +1 -0
- package/dist/utils-Cm0TE6dP.mjs +148 -0
- package/dist/utils-Cm0TE6dP.mjs.map +1 -0
- package/dist/utils.d.mts +21 -3
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +4 -4
- package/dist/{worker-pool-BWHBLBJd.mjs → worker-pool-gyxJnCxA.mjs} +4 -4
- package/dist/worker-pool-gyxJnCxA.mjs.map +1 -0
- package/dist/workflow-runtime-CF5pc8mt.d.mts.map +1 -1
- package/dist/{workflows-krankcQT.mjs → workflows-C9BQlyT0.mjs} +31 -7
- package/dist/workflows-C9BQlyT0.mjs.map +1 -0
- package/package.json +29 -19
- package/test/ai-gateway.test.ts +58 -1
- package/test/boot-desktop-auth.test.ts +278 -0
- package/test/boot-preconditions.test.ts +13 -0
- package/test/config-merge.test.ts +11 -0
- package/test/default-drive-protocol-versions.test.ts +1 -24
- package/test/default-drive-slug.test.ts +170 -2
- package/test/delegated-publish.test.ts +435 -0
- package/test/fatal-shutdown.test.ts +40 -0
- package/test/pglite-store.test.ts +179 -2
- package/test/workflow-runtime.test.ts +119 -0
- package/dist/from-env-DuUwngOC.mjs.map +0 -1
- package/dist/schema-DhempFjb.d.mts.map +0 -1
- package/dist/schema-XEZrdmag.mjs.map +0 -1
- package/dist/server-BJk9-9n4.mjs.map +0 -1
- package/dist/utils-C6AIF38Z.mjs +0 -133
- package/dist/utils-C6AIF38Z.mjs.map +0 -1
- package/dist/worker-pool-BWHBLBJd.mjs.map +0 -1
- package/dist/workflows-krankcQT.mjs.map +0 -1
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// A switchboard shaped like the desktop host's: sign-in enforced, the first
|
|
2
|
+
// person the owner, bound to loopback, and working with Renown unreachable once
|
|
3
|
+
// that person has signed in (ADR 0008 §11).
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
6
|
+
import { createServer, type Server } from "node:http";
|
|
7
|
+
import { createRequire } from "node:module";
|
|
8
|
+
import { networkInterfaces, tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import type { GraphQLManager } from "@powerhousedao/reactor-api";
|
|
11
|
+
import {
|
|
12
|
+
buildAndSignCredential,
|
|
13
|
+
MemoryKeyStorage,
|
|
14
|
+
RenownCryptoBuilder,
|
|
15
|
+
type IRenownCrypto,
|
|
16
|
+
type PowerhouseVerifiableCredential,
|
|
17
|
+
type SignCredentialTypedData,
|
|
18
|
+
} from "@renown/sdk/node";
|
|
19
|
+
import type { ILogger } from "document-model";
|
|
20
|
+
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
21
|
+
import type { SwitchboardConfigInput } from "../src/config/schema.js";
|
|
22
|
+
import type { startSwitchboard } from "../src/server.mjs";
|
|
23
|
+
import { bootSwitchboard } from "./boot-helpers.js";
|
|
24
|
+
|
|
25
|
+
// viem is the SDK's own dependency; the switchboard does not declare it.
|
|
26
|
+
type Account = {
|
|
27
|
+
address: `0x${string}`;
|
|
28
|
+
signTypedData(args: unknown): Promise<`0x${string}`>;
|
|
29
|
+
};
|
|
30
|
+
const { privateKeyToAccount } = createRequire(
|
|
31
|
+
import.meta.resolve("@renown/sdk/node"),
|
|
32
|
+
)("viem/accounts") as {
|
|
33
|
+
privateKeyToAccount: (key: `0x${string}`) => Account;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Well-known Anvil dev keys; never used for anything real.
|
|
37
|
+
const PERSON = privateKeyToAccount(
|
|
38
|
+
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
|
|
39
|
+
);
|
|
40
|
+
const VISITOR = privateKeyToAccount(
|
|
41
|
+
"0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d",
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const ENV_KEYS = [
|
|
45
|
+
"PH_REACTOR_DATABASE_URL",
|
|
46
|
+
"AUTH_ENABLED",
|
|
47
|
+
"ADMINS",
|
|
48
|
+
"RESOLVE_CALLER_IDENTITY",
|
|
49
|
+
"REQUIRE_AUTHENTICATED_CALLER",
|
|
50
|
+
"RENOWN_URL",
|
|
51
|
+
"SWITCHBOARD_URL",
|
|
52
|
+
] as const;
|
|
53
|
+
|
|
54
|
+
const logger = {
|
|
55
|
+
verbose: vi.fn(),
|
|
56
|
+
debug: vi.fn(),
|
|
57
|
+
info: vi.fn(),
|
|
58
|
+
warn: vi.fn(),
|
|
59
|
+
error: vi.fn(),
|
|
60
|
+
child: () => logger,
|
|
61
|
+
} as unknown as ILogger;
|
|
62
|
+
|
|
63
|
+
type Switchboard = Awaited<ReturnType<typeof startSwitchboard>>;
|
|
64
|
+
|
|
65
|
+
const newKey = (): Promise<IRenownCrypto> =>
|
|
66
|
+
new RenownCryptoBuilder().withKeyPairStorage(new MemoryKeyStorage()).build();
|
|
67
|
+
|
|
68
|
+
const credentialFor = (
|
|
69
|
+
account: Account,
|
|
70
|
+
key: string,
|
|
71
|
+
expiresInDays = 7,
|
|
72
|
+
): Promise<PowerhouseVerifiableCredential> =>
|
|
73
|
+
buildAndSignCredential({
|
|
74
|
+
signTypedData: ((args) =>
|
|
75
|
+
account.signTypedData(args)) as SignCredentialTypedData,
|
|
76
|
+
address: account.address,
|
|
77
|
+
chainId: 1,
|
|
78
|
+
app: "connect",
|
|
79
|
+
appId: key,
|
|
80
|
+
expiresInDays,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
/** Renown's REST lookup and nothing else. */
|
|
84
|
+
function fakeRenown(
|
|
85
|
+
credentials: Map<string, PowerhouseVerifiableCredential>,
|
|
86
|
+
): Promise<{ server: Server; url: string }> {
|
|
87
|
+
const server = createServer((request, response) => {
|
|
88
|
+
const url = new URL(request.url ?? "/", "http://renown.test");
|
|
89
|
+
const credential =
|
|
90
|
+
url.pathname === "/api/auth/credential"
|
|
91
|
+
? credentials.get(url.searchParams.get("appId") ?? "")
|
|
92
|
+
: undefined;
|
|
93
|
+
response.writeHead(credential ? 200 : 404, {
|
|
94
|
+
"content-type": "application/json",
|
|
95
|
+
});
|
|
96
|
+
response.end(JSON.stringify(credential ? { credential } : {}));
|
|
97
|
+
});
|
|
98
|
+
return new Promise((resolve) => {
|
|
99
|
+
server.listen(0, "127.0.0.1", () => {
|
|
100
|
+
const address = server.address();
|
|
101
|
+
const port = typeof address === "object" ? address?.port : undefined;
|
|
102
|
+
resolve({ server, url: `http://127.0.0.1:${String(port)}` });
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const stopped = (server: Server) =>
|
|
108
|
+
new Promise<void>((resolve) => {
|
|
109
|
+
server.close(() => resolve());
|
|
110
|
+
server.closeAllConnections();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// A booted server under a full suite's load: the default 5s is for a unit test.
|
|
114
|
+
describe(
|
|
115
|
+
"a switchboard on a person's own computer",
|
|
116
|
+
{ timeout: 60_000 },
|
|
117
|
+
() => {
|
|
118
|
+
let root = "";
|
|
119
|
+
let previous: [string, string | undefined][] = [];
|
|
120
|
+
let switchboard: Switchboard | undefined;
|
|
121
|
+
let person: IRenownCrypto;
|
|
122
|
+
let visitor: IRenownCrypto;
|
|
123
|
+
let renown: { server: Server; url: string };
|
|
124
|
+
|
|
125
|
+
const config = (renownUrl: string): SwitchboardConfigInput => ({
|
|
126
|
+
server: { port: 0, host: "127.0.0.1" },
|
|
127
|
+
storage: {
|
|
128
|
+
userDataDir: root,
|
|
129
|
+
readModel: { url: join(root, "read-model") },
|
|
130
|
+
},
|
|
131
|
+
features: { workflows: false, mcp: false },
|
|
132
|
+
packages: { disableLocal: true },
|
|
133
|
+
auth: {
|
|
134
|
+
enabled: true,
|
|
135
|
+
requireAuthenticatedCaller: true,
|
|
136
|
+
resolveIdentity: true,
|
|
137
|
+
ownerClaim: true,
|
|
138
|
+
offlineCredentials: true,
|
|
139
|
+
renown: { source: "remote", url: renownUrl },
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const boot = async (renownUrl: string) => {
|
|
144
|
+
switchboard = await bootSwitchboard(config(renownUrl), { logger });
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const stop = async () => {
|
|
148
|
+
await switchboard?.shutdown();
|
|
149
|
+
switchboard = undefined;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const ask = async (
|
|
153
|
+
key?: IRenownCrypto,
|
|
154
|
+
address?: string,
|
|
155
|
+
): Promise<number> => {
|
|
156
|
+
const bearer = key ? await key.getBearerToken(address) : undefined;
|
|
157
|
+
const response = await fetch(
|
|
158
|
+
`http://127.0.0.1:${String(switchboard!.port)}/graphql`,
|
|
159
|
+
{
|
|
160
|
+
method: "POST",
|
|
161
|
+
headers: {
|
|
162
|
+
"content-type": "application/json",
|
|
163
|
+
...(bearer ? { authorization: `Bearer ${bearer}` } : {}),
|
|
164
|
+
},
|
|
165
|
+
body: JSON.stringify({ query: "{ __typename }" }),
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
return response.status;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const isAdmin = (address: string): boolean =>
|
|
172
|
+
(
|
|
173
|
+
switchboard as unknown as { api: { graphqlManager: GraphQLManager } }
|
|
174
|
+
).api.graphqlManager
|
|
175
|
+
.getAuthorizationService()
|
|
176
|
+
.isSupremeAdmin(address);
|
|
177
|
+
|
|
178
|
+
beforeAll(async () => {
|
|
179
|
+
root = await mkdtemp(join(tmpdir(), "switchboard-desktop-auth-"));
|
|
180
|
+
previous = ENV_KEYS.map((key) => [key, process.env[key]]);
|
|
181
|
+
for (const key of ENV_KEYS) delete process.env[key];
|
|
182
|
+
process.env.PH_REACTOR_DATABASE_URL = join(root, "reactor-storage");
|
|
183
|
+
|
|
184
|
+
person = await newKey();
|
|
185
|
+
visitor = await newKey();
|
|
186
|
+
renown = await fakeRenown(
|
|
187
|
+
new Map([
|
|
188
|
+
// Past its date: it is the proof that counts while Renown is away.
|
|
189
|
+
[person.did, await credentialFor(PERSON, person.did, -1)],
|
|
190
|
+
[visitor.did, await credentialFor(VISITOR, visitor.did)],
|
|
191
|
+
]),
|
|
192
|
+
);
|
|
193
|
+
await boot(renown.url);
|
|
194
|
+
}, 120_000);
|
|
195
|
+
|
|
196
|
+
afterAll(async () => {
|
|
197
|
+
await stop();
|
|
198
|
+
await stopped(renown.server);
|
|
199
|
+
for (const [key, value] of previous) {
|
|
200
|
+
if (value === undefined) delete process.env[key];
|
|
201
|
+
else process.env[key] = value;
|
|
202
|
+
}
|
|
203
|
+
await rm(root, { recursive: true, force: true });
|
|
204
|
+
}, 60_000);
|
|
205
|
+
|
|
206
|
+
it("has its own key on first run, apart from the person's", () => {
|
|
207
|
+
expect(existsSync(join(root, ".ph", ".keypair.json"))).toBe(true);
|
|
208
|
+
const hostKey = switchboard!.renown?.did;
|
|
209
|
+
expect(hostKey).toMatch(/^did:key:/);
|
|
210
|
+
expect(hostKey).not.toBe(person.did);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("refuses anyone not signed in", async () => {
|
|
214
|
+
expect(await ask()).toBe(401);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("makes the first person to sign in its owner, with no env file", async () => {
|
|
218
|
+
expect(isAdmin(PERSON.address)).toBe(false);
|
|
219
|
+
expect(await ask(person, PERSON.address)).toBe(200);
|
|
220
|
+
expect(isAdmin(PERSON.address)).toBe(true);
|
|
221
|
+
const owner = JSON.parse(
|
|
222
|
+
await readFile(join(root, ".ph", ".owner.json"), "utf8"),
|
|
223
|
+
) as { owner: string };
|
|
224
|
+
expect(owner.owner).toBe(PERSON.address.toLowerCase());
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("gives a second person no ownership", async () => {
|
|
228
|
+
expect(await ask(visitor, VISITOR.address)).toBe(200);
|
|
229
|
+
expect(isAdmin(VISITOR.address)).toBe(false);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("answers on loopback only", async () => {
|
|
233
|
+
const lan = Object.values(networkInterfaces())
|
|
234
|
+
.flat()
|
|
235
|
+
.find((entry) => entry?.family === "IPv4" && !entry.internal);
|
|
236
|
+
if (!lan) return;
|
|
237
|
+
await expect(
|
|
238
|
+
fetch(`http://${lan.address}:${String(switchboard!.port)}/graphql`, {
|
|
239
|
+
signal: AbortSignal.timeout(3_000),
|
|
240
|
+
}),
|
|
241
|
+
).rejects.toThrow();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("refuses an owner claim on an address others can reach", async () => {
|
|
245
|
+
const exposed = config(renown.url);
|
|
246
|
+
await expect(
|
|
247
|
+
bootSwitchboard(
|
|
248
|
+
{
|
|
249
|
+
...exposed,
|
|
250
|
+
server: { port: 0, host: "0.0.0.0" },
|
|
251
|
+
storage: {
|
|
252
|
+
userDataDir: join(root, "exposed"),
|
|
253
|
+
readModel: { url: join(root, "exposed", "read-model") },
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
{ logger },
|
|
257
|
+
),
|
|
258
|
+
).rejects.toThrow(/loopback/);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("boots and verifies its owner with Renown unreachable", async () => {
|
|
262
|
+
expect(existsSync(join(root, ".ph", ".credentials.json"))).toBe(true);
|
|
263
|
+
await stop();
|
|
264
|
+
await stopped(renown.server);
|
|
265
|
+
|
|
266
|
+
await boot(renown.url);
|
|
267
|
+
|
|
268
|
+
expect(await ask(person, PERSON.address)).toBe(200);
|
|
269
|
+
expect(isAdmin(PERSON.address)).toBe(true);
|
|
270
|
+
}, 120_000);
|
|
271
|
+
|
|
272
|
+
it("verifies nobody it has not seen while Renown is unreachable", async () => {
|
|
273
|
+
const stranger = await newKey();
|
|
274
|
+
expect(await ask(stranger, VISITOR.address)).toBe(401);
|
|
275
|
+
expect(await ask()).toBe(401);
|
|
276
|
+
});
|
|
277
|
+
},
|
|
278
|
+
);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { workerTrustPolicyIssue } from "../src/boot-preconditions.mjs";
|
|
2
3
|
import {
|
|
3
4
|
reactorDbUrlOf,
|
|
4
5
|
switchboardBootFactsFromEnv,
|
|
@@ -45,6 +46,18 @@ describe("switchboardBootIssues", () => {
|
|
|
45
46
|
);
|
|
46
47
|
});
|
|
47
48
|
|
|
49
|
+
it("keeps workers away from credentials only this switchboard holds", () => {
|
|
50
|
+
const facts = { workers: 2, authEnforcement: true };
|
|
51
|
+
for (const renownSource of ["self", "kept"] as const) {
|
|
52
|
+
expect(workerTrustPolicyIssue({ ...facts, renownSource })).toMatch(
|
|
53
|
+
/cannot verify signers/,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
expect(
|
|
57
|
+
workerTrustPolicyIssue({ ...facts, renownSource: "remote" }),
|
|
58
|
+
).toBeUndefined();
|
|
59
|
+
});
|
|
60
|
+
|
|
48
61
|
it("treats a PGlite path as no Postgres", () => {
|
|
49
62
|
expect(
|
|
50
63
|
switchboardBootIssues({
|
|
@@ -183,6 +183,17 @@ describe("callerOverEnv keeps each key's precedence", () => {
|
|
|
183
183
|
},
|
|
184
184
|
);
|
|
185
185
|
|
|
186
|
+
it("lets PH_WORKFLOWS_ENABLED switch workflows off under the plugin host", () => {
|
|
187
|
+
const env = {
|
|
188
|
+
FEATURE_PLUGIN_HOST_ENABLED: "1",
|
|
189
|
+
PH_WORKFLOWS_ENABLED: "false",
|
|
190
|
+
};
|
|
191
|
+
expect(resolved(env, {}).features.workflows).toBe(false);
|
|
192
|
+
expect(
|
|
193
|
+
resolved(env, { features: { workflows: true } }).features.workflows,
|
|
194
|
+
).toBe(false);
|
|
195
|
+
});
|
|
196
|
+
|
|
186
197
|
it("ignores MCP_ENABLED and PH_WORKFLOWS_ENABLED under the plugin host", () => {
|
|
187
198
|
const env = {
|
|
188
199
|
FEATURE_PLUGIN_HOST_ENABLED: "1",
|
|
@@ -37,6 +37,7 @@ function client(): { client: IReactorClient; created: PHDocument[] } {
|
|
|
37
37
|
{ "base-reducer": 3 },
|
|
38
38
|
),
|
|
39
39
|
),
|
|
40
|
+
isDocumentIdTaken: () => Promise.resolve(false),
|
|
40
41
|
write: {
|
|
41
42
|
create: (document: PHDocument) => {
|
|
42
43
|
created.push(document);
|
|
@@ -75,28 +76,4 @@ describe.each([
|
|
|
75
76
|
expect(created[0].header.id).toBe("fixed");
|
|
76
77
|
expect(created[0].header.protocolVersions).toEqual({ "base-reducer": 3 });
|
|
77
78
|
});
|
|
78
|
-
|
|
79
|
-
it("makes a drive anew under its slug when its pinned id is one only a v2 create derives", async () => {
|
|
80
|
-
const { client: reactor, created } = client();
|
|
81
|
-
const errors: string[] = [];
|
|
82
|
-
// The id of a drive created as v2-required in another store
|
|
83
|
-
const pinned = "HRsIHuMMTdmjgCdy9QGWyx96zOFByPo787oBQcEt9f4";
|
|
84
|
-
|
|
85
|
-
const url = await add(
|
|
86
|
-
reactor,
|
|
87
|
-
{ id: pinned, slug: "mail", global: { name: "Mail" } } as never,
|
|
88
|
-
1,
|
|
89
|
-
{ error: (message: string) => errors.push(message) },
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
// Created v2-required under a derived id, not as a legacy one claiming it
|
|
93
|
-
expect(created[0].header.id).not.toBe(pinned);
|
|
94
|
-
expect(created[0].header.protocolVersions).toEqual({
|
|
95
|
-
"base-reducer": 3,
|
|
96
|
-
signature: 2,
|
|
97
|
-
});
|
|
98
|
-
expect(created[0].header.slug).toBe("mail");
|
|
99
|
-
expect(url).toBe("http://localhost:1/d/mail");
|
|
100
|
-
expect(errors[0]).toContain(`pins ${pinned}`);
|
|
101
|
-
});
|
|
102
79
|
});
|
|
@@ -3,15 +3,19 @@ import {
|
|
|
3
3
|
ReactorClientBuilder,
|
|
4
4
|
type InProcessReactorClientModule,
|
|
5
5
|
} from "@powerhousedao/reactor";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
driveDocumentModelModule,
|
|
8
|
+
setDriveName,
|
|
9
|
+
} from "@powerhousedao/shared/document-drive";
|
|
7
10
|
import type { DriveInput } from "@powerhousedao/shared/document-drive";
|
|
8
11
|
import {
|
|
9
12
|
withSignaturePolicy,
|
|
10
13
|
type DocumentModelModule,
|
|
11
14
|
} from "@powerhousedao/shared/document-model";
|
|
15
|
+
import { deleteDocumentAction } from "@powerhousedao/reactor";
|
|
12
16
|
import { documentModelDocumentModelModule } from "document-model";
|
|
13
17
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
14
|
-
import { addDefaultDrive } from "../src/utils.mjs";
|
|
18
|
+
import { addDefaultDrive, ensureDrive } from "../src/utils.mjs";
|
|
15
19
|
|
|
16
20
|
const PORT = 4001;
|
|
17
21
|
|
|
@@ -95,3 +99,167 @@ describe("addDefaultDrive with a slug another document holds", () => {
|
|
|
95
99
|
);
|
|
96
100
|
});
|
|
97
101
|
});
|
|
102
|
+
|
|
103
|
+
describe.each([
|
|
104
|
+
["without document decisions", false],
|
|
105
|
+
["with document decisions", true],
|
|
106
|
+
])("ensureDrive %s", (_name, documentDecisions) => {
|
|
107
|
+
let module: InProcessReactorClientModule;
|
|
108
|
+
const logger = { error: vi.fn() };
|
|
109
|
+
const mail = { slug: "mail", global: { name: "Mail" } };
|
|
110
|
+
|
|
111
|
+
const deleteDrive = async (id: string) => {
|
|
112
|
+
await (
|
|
113
|
+
await module.client.write.deleteDocument(id)
|
|
114
|
+
).readable;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
beforeEach(async () => {
|
|
118
|
+
logger.error.mockClear();
|
|
119
|
+
module = await new ReactorClientBuilder()
|
|
120
|
+
.withCreateSignaturePolicy("legacy")
|
|
121
|
+
.withReactorBuilder(
|
|
122
|
+
new ReactorBuilder()
|
|
123
|
+
.withDocumentModelSources([
|
|
124
|
+
driveDocumentModelModule as unknown as DocumentModelModule,
|
|
125
|
+
documentModelDocumentModelModule as unknown as DocumentModelModule,
|
|
126
|
+
])
|
|
127
|
+
.withExecutorConfig({ featureFlags: { documentDecisions } }),
|
|
128
|
+
)
|
|
129
|
+
.buildModule();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
afterEach(() => {
|
|
133
|
+
module.reactor.kill();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("generates the id of a drive declared by its slug alone", async () => {
|
|
137
|
+
await ensureDrive(module.client, mail, PORT, logger);
|
|
138
|
+
|
|
139
|
+
const id = await module.client.resolveIdOrSlug("mail");
|
|
140
|
+
expect(id).not.toBe("mail");
|
|
141
|
+
const drive = await module.client.get(id);
|
|
142
|
+
expect(drive.header.slug).toBe("mail");
|
|
143
|
+
expect(drive.header.documentType).toBe("powerhouse/document-drive");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// A random id can't clash, so a second boot must find the drive by its slug.
|
|
147
|
+
it("finds that drive again on the next boot instead of making a twin", async () => {
|
|
148
|
+
await ensureDrive(module.client, mail, PORT, logger);
|
|
149
|
+
const first = await module.client.resolveIdOrSlug("mail");
|
|
150
|
+
|
|
151
|
+
await ensureDrive(module.client, mail, PORT, logger);
|
|
152
|
+
|
|
153
|
+
const { results } = await module.client.find({
|
|
154
|
+
type: "powerhouse/document-drive",
|
|
155
|
+
});
|
|
156
|
+
expect(results.map((drive) => drive.header.id)).toEqual([first]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("leaves a drive of another type under the slug as it is", async () => {
|
|
160
|
+
await ensureDrive(module.client, mail, PORT, logger);
|
|
161
|
+
const id = await module.client.resolveIdOrSlug("mail");
|
|
162
|
+
|
|
163
|
+
await expect(
|
|
164
|
+
ensureDrive(
|
|
165
|
+
module.client,
|
|
166
|
+
{ ...mail, documentType: "powerhouse/reactor-drive" },
|
|
167
|
+
PORT,
|
|
168
|
+
logger,
|
|
169
|
+
),
|
|
170
|
+
).resolves.toBe(`http://localhost:${PORT}/d/${id}`);
|
|
171
|
+
expect(logger.error).not.toHaveBeenCalled();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("finds a drive whose id equals its slug, as older stores hold them", async () => {
|
|
175
|
+
await ensureDrive(module.client, { ...mail, id: "mail" }, PORT, logger);
|
|
176
|
+
|
|
177
|
+
await expect(ensureDrive(module.client, mail, PORT, logger)).resolves.toBe(
|
|
178
|
+
`http://localhost:${PORT}/d/mail`,
|
|
179
|
+
);
|
|
180
|
+
await expect(module.client.resolveIdOrSlug("mail")).resolves.toBe("mail");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("makes a new empty drive after the drive under the slug was deleted", async () => {
|
|
184
|
+
await ensureDrive(module.client, mail, PORT, logger);
|
|
185
|
+
const first = await module.client.resolveIdOrSlug("mail");
|
|
186
|
+
await deleteDrive(first);
|
|
187
|
+
|
|
188
|
+
await ensureDrive(module.client, mail, PORT, logger);
|
|
189
|
+
|
|
190
|
+
const second = await module.client.resolveIdOrSlug("mail");
|
|
191
|
+
expect(second).not.toBe(first);
|
|
192
|
+
expect(logger.error).not.toHaveBeenCalled();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// Stores from before generated ids: the deleted drive's id is the slug.
|
|
196
|
+
it("points the slug at the new drive when the deleted drive's id was that slug", async () => {
|
|
197
|
+
await ensureDrive(module.client, { ...mail, id: "mail" }, PORT, logger);
|
|
198
|
+
await deleteDrive("mail");
|
|
199
|
+
|
|
200
|
+
await ensureDrive(module.client, mail, PORT, logger);
|
|
201
|
+
|
|
202
|
+
const id = await module.client.resolveIdOrSlug("mail");
|
|
203
|
+
expect(id).not.toBe("mail");
|
|
204
|
+
await expect(module.client.get("mail")).resolves.toMatchObject({
|
|
205
|
+
header: { id },
|
|
206
|
+
});
|
|
207
|
+
// Writes resolve through the identity table, reads through the view
|
|
208
|
+
const renamed = await module.client.execute("mail", "main", [
|
|
209
|
+
setDriveName({ name: "Renamed" }),
|
|
210
|
+
]);
|
|
211
|
+
expect(renamed.header.id).toBe(id);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
// A project-hashed drive declares an id, and that id outlives the drive.
|
|
215
|
+
it.each([
|
|
216
|
+
["a different slug", "mail", "mail-1234abcd"],
|
|
217
|
+
["the same slug", "mail-1234abcd", "mail-1234abcd"],
|
|
218
|
+
])(
|
|
219
|
+
"makes a new drive when a deleted drive holds the declared id, under %s",
|
|
220
|
+
async (_case, slug, id) => {
|
|
221
|
+
const declared = { ...mail, slug, id };
|
|
222
|
+
await ensureDrive(module.client, declared, PORT, logger);
|
|
223
|
+
await deleteDrive(id);
|
|
224
|
+
|
|
225
|
+
await expect(
|
|
226
|
+
ensureDrive(module.client, declared, PORT, logger),
|
|
227
|
+
).resolves.toBe(`http://localhost:${PORT}/d/${slug}`);
|
|
228
|
+
|
|
229
|
+
const created = await module.client.resolveIdOrSlug(slug);
|
|
230
|
+
expect(created).not.toBe(id);
|
|
231
|
+
|
|
232
|
+
await ensureDrive(module.client, declared, PORT, logger);
|
|
233
|
+
await expect(module.client.resolveIdOrSlug(slug)).resolves.toBe(created);
|
|
234
|
+
expect(logger.error).not.toHaveBeenCalled();
|
|
235
|
+
},
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
it("leaves a drive deleted when the delete kept its slug", async () => {
|
|
239
|
+
await ensureDrive(module.client, mail, PORT, logger);
|
|
240
|
+
const id = await module.client.resolveIdOrSlug("mail");
|
|
241
|
+
await (
|
|
242
|
+
await module.client.write.execute(id, "main", [
|
|
243
|
+
deleteDocumentAction(id, { keepSlug: true }),
|
|
244
|
+
])
|
|
245
|
+
).readable;
|
|
246
|
+
|
|
247
|
+
await expect(
|
|
248
|
+
ensureDrive(module.client, mail, PORT, logger),
|
|
249
|
+
).resolves.toBeUndefined();
|
|
250
|
+
|
|
251
|
+
const { results } = await module.client.find({
|
|
252
|
+
type: "powerhouse/document-drive",
|
|
253
|
+
});
|
|
254
|
+
expect(results).toEqual([]);
|
|
255
|
+
expect(logger.error).toHaveBeenCalledWith(
|
|
256
|
+
`The default drive "mail" was not created: it was deleted, and its slug kept.`,
|
|
257
|
+
);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("refuses a drive with neither a slug nor an id", async () => {
|
|
261
|
+
await expect(
|
|
262
|
+
ensureDrive(module.client, { global: { name: "x" } }, PORT, logger),
|
|
263
|
+
).rejects.toThrow("A drive needs a slug");
|
|
264
|
+
});
|
|
265
|
+
});
|