-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e.test.ts
76 lines (74 loc) · 2.02 KB
/
e2e.test.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
import {
assert,
assertEquals,
} from "https://deno.land/std@0.179.0/testing/asserts.ts";
import {
connect,
generatePrivateKey,
getPublicKey,
} from "./client/src/index.ts";
const alicePayload = { text: "hello Bob! I've sent you this json." };
const bobPayload = "hello Alice, here's a string message.";
Deno.test("e2e", async () => {
const alice = generatePrivateKey();
const bob = generatePrivateKey();
const events: string[] = [];
await new Promise<void>((resolve) => {
let closedCount = 0;
const markClosed = () => {
closedCount++;
assert(closedCount <= 2);
if (closedCount === 2) resolve();
};
Promise.all([
connect({
privateKey: alice,
onMessage: ({ from, payload }) =>
new Promise((resolve) => {
events.push("bob->alice");
assertEquals(from, getPublicKey(bob));
assertEquals(payload, bobPayload);
resolve();
}),
onClose: () => {
markClosed();
},
}),
connect({
privateKey: bob,
onMessage: ({ from, payload }) =>
new Promise((resolve) => {
events.push("alice->bob");
assertEquals(from, getPublicKey(alice));
assertEquals(payload, alicePayload);
resolve();
}),
onClose: () => {},
}),
])
.then(([aliceFn, bobFn]) =>
Promise.all([
aliceFn
.send({ to: getPublicKey(bob), payload: alicePayload })
.then(() => {
events.push("bob-acked");
}),
bobFn
.send({ to: getPublicKey(alice), payload: bobPayload })
.then(() => {
events.push("alice-acked");
}),
]).then(() => () => {
aliceFn.close();
bobFn.close();
})
)
.then((close) => {
assertEquals(
new Set(events),
new Set(["alice->bob", "bob->alice", "bob-acked", "alice-acked"]),
);
close();
});
});
});