-
Notifications
You must be signed in to change notification settings - Fork 13
/
scenario.ts
458 lines (396 loc) · 14.2 KB
/
scenario.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import {
ActionHash,
AppBundleSource,
AppSignal,
AppWebsocket,
EntryHash,
PreflightResponse,
Signal,
SignalCb,
SignalType,
} from "@holochain/client";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "tape-promise/tape.js";
import { Scenario, dhtSync, getZomeCaller, runScenario } from "../../src";
import { FIXTURE_HAPP_URL } from "../fixture";
const TEST_ZOME_NAME = "coordinator";
test("Local Scenario - runScenario - Install hApp bundle and access cells through role ids", async (t) => {
await runScenario(async (scenario: Scenario) => {
const alice = await scenario.addPlayerWithApp({
path: FIXTURE_HAPP_URL.pathname,
});
t.ok(alice.namedCells.get("test"));
});
});
test("Local Scenario - runScenario - Catch error when calling non-existent zome", async (t) => {
await runScenario(async (scenario: Scenario) => {
const alice = await scenario.addPlayerWithApp({
path: FIXTURE_HAPP_URL.pathname,
});
await t.rejects(
alice.cells[0].callZome<EntryHash>({
zome_name: "NOZOME",
fn_name: "create",
})
);
});
});
test("Local Scenario - runScenario - Catch error when attaching a protected port", async (t) => {
await runScenario(async (scenario: Scenario) => {
const alice = await scenario.addPlayerWithApp({
path: FIXTURE_HAPP_URL.pathname,
});
await t.rejects(
alice.conductor.attachAppInterface({ port: 300, allowed_origins: "*" })
);
});
});
test("Local Scenario - runScenario - Catch error when calling a zome of an undefined cell", async (t) => {
await runScenario(async (scenario: Scenario) => {
const alice = await scenario.addPlayerWithApp({
path: FIXTURE_HAPP_URL.pathname,
});
t.throws(() => alice.cells[2].callZome({ zome_name: "", fn_name: "" }));
});
});
test("Local Scenario - runScenario - Catch error that occurs in a signal handler", async (t) => {
await runScenario(async (scenario: Scenario) => {
let signalHandlerAlice: SignalCb | undefined;
const signalReceivedAlice = new Promise<Signal>((_, reject) => {
signalHandlerAlice = () => {
reject();
};
});
const alice = await scenario.addPlayerWithApp({
path: FIXTURE_HAPP_URL.pathname,
});
assert(signalHandlerAlice);
assert("on" in alice.appWs);
alice.appWs.on("signal", signalHandlerAlice);
const signalAlice = { value: "hello alice" };
alice.cells[0].callZome({
zome_name: TEST_ZOME_NAME,
fn_name: "signal_loopback",
payload: signalAlice,
});
await t.rejects(signalReceivedAlice);
});
});
test("Local Scenario - Install hApp bundle and access cell by role name", async (t) => {
const scenario = new Scenario();
const alice = await scenario.addPlayerWithApp({
path: FIXTURE_HAPP_URL.pathname,
});
t.ok(alice.namedCells.get("test"));
await scenario.cleanUp();
});
test("Local Scenario - Add players with hApp bundles", async (t) => {
const scenario = new Scenario();
t.ok(scenario.networkSeed);
const [alice, bob] = await scenario.addPlayersWithApps([
{ appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
{ appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
]);
t.ok(alice.namedCells.get("test"));
t.ok(bob.namedCells.get("test"));
await scenario.cleanUp();
});
// unstable-dpki
// test("Local Scenario - All players have DPKI enabled", async (t) => {
// const scenario = new Scenario();
// await scenario.addPlayersWithApps([
// { appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
// { appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
// ]);
// scenario.conductors.every((conductor) => {
// const tmpDirPath = conductor.getTmpDirectory();
// const conductorConfig = readFileSync(
// tmpDirPath + "/conductor-config.yaml"
// ).toString();
// t.assert(
// conductorConfig.includes("no_dpki: false"),
// "DPKI enabled in conductor config"
// );
// });
// await scenario.cleanUp();
// });
// test("Local Scenario - All players have DPKI disabled", async (t) => {
// const scenario = new Scenario();
// scenario.noDpki = true;
// await scenario.addPlayersWithApps([
// { appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
// { appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
// ]);
// scenario.conductors.every((conductor) => {
// const tmpDirPath = conductor.getTmpDirectory();
// const conductorConfig = readFileSync(
// tmpDirPath + "/conductor-config.yaml"
// ).toString();
// t.assert(
// conductorConfig.includes("no_dpki: true"),
// "DPKI disabled in conductor config"
// );
// });
// await scenario.cleanUp();
// });
// test("Local Scenario - All players have a custom DPKI network seed", async (t) => {
// const scenario = new Scenario();
// scenario.dpkiNetworkSeed = "tryorama-dpki-test";
// await scenario.addPlayersWithApps([
// { appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
// { appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
// ]);
// scenario.conductors.every((conductor) => {
// const tmpDirPath = conductor.getTmpDirectory();
// const conductorConfig = readFileSync(
// tmpDirPath + "/conductor-config.yaml"
// ).toString();
// t.assert(
// conductorConfig.includes(`network_seed: ${scenario.dpkiNetworkSeed}`),
// "default DPKI network seed set in conductor config"
// );
// });
// await scenario.cleanUp();
// });
// test("Local Scenario - All players have a random DPKI network seed", async (t) => {
// const scenario = new Scenario();
// await scenario.addPlayersWithApps([
// { appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
// { appBundleSource: { path: FIXTURE_HAPP_URL.pathname } },
// ]);
// scenario.conductors.every((conductor) => {
// const tmpDirPath = conductor.getTmpDirectory();
// const conductorConfig = readFileSync(
// tmpDirPath + "/conductor-config.yaml"
// ).toString();
// t.assert(
// conductorConfig.includes(`network_seed: ${scenario.dpkiNetworkSeed}`),
// "DPKI network seed set in conductor config"
// );
// });
// await scenario.cleanUp();
// });
test("Local Scenario - Create and read an entry, 2 conductors", async (t) => {
// The wrapper takes care of creating a scenario and shutting down or deleting
// all conductors involved in the test scenario.
await runScenario(async (scenario) => {
// Construct proper paths for a hApp file created by the `hc app pack` command.
const appBundleSource: AppBundleSource = {
path: FIXTURE_HAPP_URL.pathname,
};
// Add 2 players with the test hApp to the Scenario. The returned players
// can be destructured.
const [alice, bob] = await scenario.addPlayersWithApps([
{ appBundleSource },
{ appBundleSource },
]);
// Content to be passed to the zome function that create an entry,
const content = "Hello Tryorama";
// The cells of the installed hApp are returned in the same order as the DNAs
// in the app manifest.
const createEntryHash = await alice.cells[0].callZome<EntryHash>({
zome_name: TEST_ZOME_NAME,
fn_name: "create",
payload: content,
});
// Wait for the created entry to be propagated to the other player.
await dhtSync([alice, bob], alice.cells[0].cell_id[0]);
// Using the same cell and zome as before, the second player reads the
// created entry.
const readContent = await bob.cells[0].callZome<typeof content>({
zome_name: TEST_ZOME_NAME,
fn_name: "read",
payload: createEntryHash,
});
t.equal(readContent, content);
});
});
test("Local Scenario - Conductor maintains data after shutdown and restart", async (t) => {
const scenario = new Scenario();
const appBundleSource: AppBundleSource = { path: FIXTURE_HAPP_URL.pathname };
const [alice, bob] = await scenario.addPlayersWithApps([
{ appBundleSource },
{ appBundleSource },
]);
// Get shortcut functions to call a specific zome of a specific agent
const aliceCaller = getZomeCaller(alice.cells[0], TEST_ZOME_NAME);
const bobCaller = getZomeCaller(bob.cells[0], TEST_ZOME_NAME);
const content = "Before shutdown";
// Use the curried function to call alice's coordinator zome
const createEntryHash = await aliceCaller<EntryHash>("create", content);
await dhtSync([alice, bob], alice.cells[0].cell_id[0]);
const readContent = await bobCaller<typeof content>("read", createEntryHash);
t.equal(readContent, content);
await bob.conductor.shutDown();
t.throws(bob.conductor.adminWs);
await bob.conductor.startUp();
const [appInterfaceInfo] = await bob.conductor.adminWs().listAppInterfaces();
const issuedBob = await bob.conductor
.adminWs()
.issueAppAuthenticationToken({ installed_app_id: bob.appId });
bob.appWs = await bob.conductor.connectAppWs(
issuedBob.token,
appInterfaceInfo.port
);
const readContentAfterRestart: typeof content = await bob.appWs.callZome({
cell_id: bob.cells[0].cell_id,
zome_name: TEST_ZOME_NAME,
fn_name: "read",
payload: createEntryHash,
});
t.equal(readContentAfterRestart, content);
await scenario.cleanUp();
});
test("Local Scenario - Receive signals with 2 conductors", async (t) => {
const scenario = new Scenario();
let signalHandlerAlice: SignalCb | undefined;
const signalReceivedAlice = new Promise<AppSignal>((resolve) => {
signalHandlerAlice = (signal: Signal) => {
assert(SignalType.App in signal);
resolve(signal[SignalType.App]);
};
});
let signalHandlerBob: SignalCb | undefined;
const signalReceivedBob = new Promise<AppSignal>((resolve) => {
signalHandlerBob = (signal: Signal) => {
assert(SignalType.App in signal);
resolve(signal[SignalType.App]);
};
});
const appBundleSource: AppBundleSource = { path: FIXTURE_HAPP_URL.pathname };
const [alice, bob] = await scenario.addPlayersWithApps([
{ appBundleSource },
{ appBundleSource },
]);
assert(signalHandlerAlice);
assert("on" in alice.appWs);
alice.appWs.on("signal", signalHandlerAlice);
assert(signalHandlerBob);
assert("on" in bob.appWs);
bob.appWs.on("signal", signalHandlerBob);
const signalAlice = { value: "hello alice" };
alice.cells[0].callZome({
zome_name: TEST_ZOME_NAME,
fn_name: "signal_loopback",
payload: signalAlice,
});
const signalBob = { value: "hello bob" };
bob.cells[0].callZome({
zome_name: TEST_ZOME_NAME,
fn_name: "signal_loopback",
payload: signalBob,
});
const [actualSignalAlice, actualSignalBob] = await Promise.all([
signalReceivedAlice,
signalReceivedBob,
]);
t.deepEqual(actualSignalAlice.payload, signalAlice);
t.deepEqual(actualSignalBob.payload, signalBob);
await scenario.cleanUp();
});
test("Local Scenario - pauseUntilDhtEqual - Create multiple entries, read the last, 2 conductors", async (t) => {
const scenario = new Scenario();
const appBundleSource: AppBundleSource = { path: FIXTURE_HAPP_URL.pathname };
const [alice, bob] = await scenario.addPlayersWithApps([
{ appBundleSource },
{ appBundleSource },
]);
// Alice creates 10 entries
let lastCreatedHash;
let lastCreatedContent;
for (let i = 0; i < 10; i++) {
lastCreatedContent = `Hi dare ${i}`;
lastCreatedHash = await alice.cells[0].callZome<EntryHash>({
zome_name: TEST_ZOME_NAME,
fn_name: "create",
payload: lastCreatedContent,
});
}
await dhtSync([alice, bob], alice.cells[0].cell_id[0]);
// Bob gets the last created entry
const readContent = await bob.cells[0].callZome<string>({
zome_name: TEST_ZOME_NAME,
fn_name: "read",
payload: lastCreatedHash,
});
t.equal(readContent, lastCreatedContent);
await scenario.cleanUp();
});
test("Local Scenario - runScenario - call zome by role name", async (t) => {
await runScenario(async (scenario: Scenario) => {
const alice = await scenario.addPlayerWithApp({
path: FIXTURE_HAPP_URL.pathname,
});
const result = (await alice.appWs.callZome({
role_name: "test",
zome_name: "coordinator",
fn_name: "create",
payload: "hello",
})) as ActionHash;
t.ok(result);
});
});
// unstable-countersigning
// test("Local Scenario - countersigning", async (t) => {
// await runScenario(async (scenario: Scenario) => {
// const appBundleSource: AppBundleSource = {
// path: FIXTURE_HAPP_URL.pathname,
// };
// const [alice, bob] = await scenario.addPlayersWithApps([
// { appBundleSource },
// { appBundleSource },
// ]);
// const result = new Promise<Signal>((resolve, reject) => {
// const timeout = setTimeout(
// () => reject("timeout waiting for signal"),
// 60000
// );
// (alice.appWs as AppWebsocket).on("signal", (signal) => {
// clearTimeout(timeout);
// resolve(signal);
// });
// });
// // Make sure init has been called
// await alice.appWs.callZome({
// role_name: "test",
// zome_name: "coordinator",
// fn_name: "create",
// payload: "hello",
// });
// await bob.appWs.callZome({
// role_name: "test",
// zome_name: "coordinator",
// fn_name: "create",
// payload: "hello",
// });
// const response1: PreflightResponse = await alice.appWs.callZome({
// role_name: "test",
// zome_name: "coordinator",
// fn_name: "create_two_party_countersigning_session",
// payload: bob.agentPubKey,
// });
// const response2: PreflightResponse = await bob.appWs.callZome({
// role_name: "test",
// zome_name: "coordinator",
// fn_name: "accept_two_party",
// payload: response1.request,
// });
// await alice.appWs.callZome({
// role_name: "test",
// zome_name: "coordinator",
// fn_name: "commit_two_party",
// payload: [response1, response2],
// });
// await bob.appWs.callZome({
// role_name: "test",
// zome_name: "coordinator",
// fn_name: "commit_two_party",
// payload: [response1, response2],
// });
// const completionSignal = await result;
// assert(SignalType.System in completionSignal);
// const systemSignal = completionSignal[SignalType.System];
// t.assert("SuccessfulCountersigning" in systemSignal);
// });
// });