cojson 0.7.35 → 0.8.3
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/.turbo/turbo-build.log +2 -7
- package/.turbo/turbo-lint.log +4 -0
- package/CHANGELOG.md +13 -0
- package/dist/{PeerState.js → native/PeerState.js} +13 -5
- package/dist/native/PeerState.js.map +1 -0
- package/dist/native/PriorityBasedMessageQueue.js.map +1 -0
- package/dist/native/base64url.js.map +1 -0
- package/dist/native/base64url.test.js.map +1 -0
- package/dist/native/coValue.js.map +1 -0
- package/dist/{coValueCore.js → native/coValueCore.js} +0 -6
- package/dist/native/coValueCore.js.map +1 -0
- package/dist/{coValues → native/coValues}/account.js +2 -3
- package/dist/native/coValues/account.js.map +1 -0
- package/dist/native/coValues/coList.js.map +1 -0
- package/dist/native/coValues/coMap.js.map +1 -0
- package/dist/native/coValues/coStream.js.map +1 -0
- package/dist/{coValues → native/coValues}/group.js +8 -8
- package/dist/native/coValues/group.js.map +1 -0
- package/dist/native/coreToCoValue.js.map +1 -0
- package/dist/{crypto → native/crypto}/PureJSCrypto.js +4 -0
- package/dist/native/crypto/PureJSCrypto.js.map +1 -0
- package/dist/{crypto → native/crypto}/crypto.js +4 -1
- package/dist/native/crypto/crypto.js.map +1 -0
- package/dist/native/ids.js.map +1 -0
- package/dist/{index.js → native/index.native.js} +4 -6
- package/dist/native/index.native.js.map +1 -0
- package/dist/native/jsonStringify.js.map +1 -0
- package/dist/{jsonValue.js.map → native/jsonValue.js.map} +1 -1
- package/dist/{localNode.js → native/localNode.js} +44 -38
- package/dist/native/localNode.js.map +1 -0
- package/dist/native/media.js.map +1 -0
- package/dist/native/permissions.js.map +1 -0
- package/dist/native/priority.js.map +1 -0
- package/dist/native/storage/FileSystem.js.map +1 -0
- package/dist/{storage → native/storage}/chunksAndKnownStates.js +1 -1
- package/dist/native/storage/chunksAndKnownStates.js.map +1 -0
- package/dist/native/storage/index.js.map +1 -0
- package/dist/native/streamUtils.js.map +1 -0
- package/dist/native/sync.js.map +1 -0
- package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -0
- package/dist/native/typeUtils/expectGroup.js.map +1 -0
- package/dist/native/typeUtils/isAccountID.js.map +1 -0
- package/dist/native/typeUtils/isCoValue.js.map +1 -0
- package/dist/web/PeerState.js +66 -0
- package/dist/web/PeerState.js.map +1 -0
- package/dist/web/PriorityBasedMessageQueue.js +51 -0
- package/dist/web/PriorityBasedMessageQueue.js.map +1 -0
- package/dist/web/base64url.js +59 -0
- package/dist/web/base64url.js.map +1 -0
- package/dist/web/base64url.test.js +25 -0
- package/dist/web/base64url.test.js.map +1 -0
- package/dist/web/coValue.js +19 -0
- package/dist/web/coValue.js.map +1 -0
- package/dist/web/coValueCore.js +693 -0
- package/dist/web/coValueCore.js.map +1 -0
- package/dist/web/coValues/account.js +96 -0
- package/dist/web/coValues/account.js.map +1 -0
- package/dist/web/coValues/coList.js +393 -0
- package/dist/web/coValues/coList.js.map +1 -0
- package/dist/web/coValues/coMap.js +197 -0
- package/dist/web/coValues/coMap.js.map +1 -0
- package/dist/web/coValues/coStream.js +220 -0
- package/dist/web/coValues/coStream.js.map +1 -0
- package/dist/web/coValues/group.js +250 -0
- package/dist/web/coValues/group.js.map +1 -0
- package/dist/web/coreToCoValue.js +42 -0
- package/dist/web/coreToCoValue.js.map +1 -0
- package/dist/web/crypto/PureJSCrypto.js +93 -0
- package/dist/web/crypto/PureJSCrypto.js.map +1 -0
- package/dist/{crypto → web/crypto}/WasmCrypto.js +3 -0
- package/dist/web/crypto/WasmCrypto.js.map +1 -0
- package/dist/web/crypto/crypto.js +154 -0
- package/dist/web/crypto/crypto.js.map +1 -0
- package/dist/web/ids.js +17 -0
- package/dist/web/ids.js.map +1 -0
- package/dist/web/index.web.js +40 -0
- package/dist/web/index.web.js.map +1 -0
- package/dist/web/jsonStringify.js +57 -0
- package/dist/web/jsonStringify.js.map +1 -0
- package/dist/web/jsonValue.js +2 -0
- package/dist/web/jsonValue.js.map +1 -0
- package/dist/web/localNode.js +442 -0
- package/dist/web/localNode.js.map +1 -0
- package/dist/web/media.js +2 -0
- package/dist/web/media.js.map +1 -0
- package/dist/web/permissions.js +206 -0
- package/dist/web/permissions.js.map +1 -0
- package/dist/web/priority.js +31 -0
- package/dist/web/priority.js.map +1 -0
- package/dist/web/storage/FileSystem.js +58 -0
- package/dist/web/storage/FileSystem.js.map +1 -0
- package/dist/web/storage/chunksAndKnownStates.js +100 -0
- package/dist/web/storage/chunksAndKnownStates.js.map +1 -0
- package/dist/web/storage/index.js +348 -0
- package/dist/web/storage/index.js.map +1 -0
- package/dist/web/streamUtils.js +41 -0
- package/dist/web/streamUtils.js.map +1 -0
- package/dist/web/sync.js +504 -0
- package/dist/web/sync.js.map +1 -0
- package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js +5 -0
- package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -0
- package/dist/web/typeUtils/expectGroup.js +13 -0
- package/dist/web/typeUtils/expectGroup.js.map +1 -0
- package/dist/web/typeUtils/isAccountID.js +4 -0
- package/dist/web/typeUtils/isAccountID.js.map +1 -0
- package/dist/web/typeUtils/isCoValue.js +11 -0
- package/dist/web/typeUtils/isCoValue.js.map +1 -0
- package/package.json +25 -8
- package/src/PeerState.ts +18 -7
- package/src/coValue.ts +1 -1
- package/src/coValueCore.ts +9 -14
- package/src/coValues/account.ts +12 -7
- package/src/coValues/coList.ts +4 -4
- package/src/coValues/coMap.ts +3 -3
- package/src/coValues/coStream.ts +8 -8
- package/src/coValues/group.ts +15 -11
- package/src/crypto/PureJSCrypto.ts +5 -0
- package/src/crypto/WasmCrypto.ts +4 -0
- package/src/crypto/crypto.ts +11 -1
- package/src/ids.ts +2 -2
- package/src/{index.ts → index.native.ts} +7 -8
- package/src/index.web.ts +152 -0
- package/src/localNode.ts +75 -67
- package/src/permissions.ts +5 -5
- package/src/storage/chunksAndKnownStates.ts +1 -1
- package/src/storage/index.ts +1 -1
- package/src/tests/account.test.ts +1 -2
- package/src/tests/coList.test.ts +1 -1
- package/src/tests/coMap.test.ts +1 -1
- package/src/tests/coStream.test.ts +2 -1
- package/src/tests/cryptoImpl.test.ts +24 -2
- package/src/tests/group.test.ts +6 -8
- package/src/tests/permissions.test.ts +90 -43
- package/src/tests/priority.test.ts +44 -15
- package/src/tests/sync.test.ts +8 -8
- package/src/tests/testUtils.ts +1 -2
- package/src/typeUtils/accountOrAgentIDfromSessionID.ts +3 -3
- package/src/typeUtils/isAccountID.ts +2 -2
- package/tsconfig.json +5 -4
- package/tsconfig.native.json +12 -0
- package/tsconfig.web.json +11 -0
- package/dist/PeerState.js.map +0 -1
- package/dist/PriorityBasedMessageQueue.js.map +0 -1
- package/dist/base64url.js.map +0 -1
- package/dist/base64url.test.js.map +0 -1
- package/dist/coValue.js.map +0 -1
- package/dist/coValueCore.js.map +0 -1
- package/dist/coValues/account.js.map +0 -1
- package/dist/coValues/coList.js.map +0 -1
- package/dist/coValues/coMap.js.map +0 -1
- package/dist/coValues/coStream.js.map +0 -1
- package/dist/coValues/group.js.map +0 -1
- package/dist/coreToCoValue.js.map +0 -1
- package/dist/crypto/PureJSCrypto.js.map +0 -1
- package/dist/crypto/WasmCrypto.js.map +0 -1
- package/dist/crypto/crypto.js.map +0 -1
- package/dist/ids.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/jsonStringify.js.map +0 -1
- package/dist/localNode.js.map +0 -1
- package/dist/media.js.map +0 -1
- package/dist/permissions.js.map +0 -1
- package/dist/priority.js.map +0 -1
- package/dist/storage/FileSystem.js.map +0 -1
- package/dist/storage/chunksAndKnownStates.js.map +0 -1
- package/dist/storage/index.js.map +0 -1
- package/dist/streamUtils.js.map +0 -1
- package/dist/sync.js.map +0 -1
- package/dist/tests/PeerState.test.js +0 -80
- package/dist/tests/PeerState.test.js.map +0 -1
- package/dist/tests/PriorityBasedMessageQueue.test.js +0 -97
- package/dist/tests/PriorityBasedMessageQueue.test.js.map +0 -1
- package/dist/tests/account.test.js +0 -59
- package/dist/tests/account.test.js.map +0 -1
- package/dist/tests/coList.test.js +0 -76
- package/dist/tests/coList.test.js.map +0 -1
- package/dist/tests/coMap.test.js +0 -136
- package/dist/tests/coMap.test.js.map +0 -1
- package/dist/tests/coStream.test.js +0 -205
- package/dist/tests/coStream.test.js.map +0 -1
- package/dist/tests/coValueCore.test.js +0 -124
- package/dist/tests/coValueCore.test.js.map +0 -1
- package/dist/tests/crypto.test.js +0 -118
- package/dist/tests/crypto.test.js.map +0 -1
- package/dist/tests/cryptoImpl.test.js +0 -113
- package/dist/tests/cryptoImpl.test.js.map +0 -1
- package/dist/tests/group.test.js +0 -34
- package/dist/tests/group.test.js.map +0 -1
- package/dist/tests/permissions.test.js +0 -1060
- package/dist/tests/permissions.test.js.map +0 -1
- package/dist/tests/priority.test.js +0 -61
- package/dist/tests/priority.test.js.map +0 -1
- package/dist/tests/sync.test.js +0 -1187
- package/dist/tests/sync.test.js.map +0 -1
- package/dist/tests/testUtils.js +0 -60
- package/dist/tests/testUtils.js.map +0 -1
- package/dist/typeUtils/accountOrAgentIDfromSessionID.js.map +0 -1
- package/dist/typeUtils/expectGroup.js.map +0 -1
- package/dist/typeUtils/isAccountID.js.map +0 -1
- package/dist/typeUtils/isCoValue.js.map +0 -1
- /package/dist/{PriorityBasedMessageQueue.js → native/PriorityBasedMessageQueue.js} +0 -0
- /package/dist/{base64url.js → native/base64url.js} +0 -0
- /package/dist/{base64url.test.js → native/base64url.test.js} +0 -0
- /package/dist/{coValue.js → native/coValue.js} +0 -0
- /package/dist/{coValues → native/coValues}/coList.js +0 -0
- /package/dist/{coValues → native/coValues}/coMap.js +0 -0
- /package/dist/{coValues → native/coValues}/coStream.js +0 -0
- /package/dist/{coreToCoValue.js → native/coreToCoValue.js} +0 -0
- /package/dist/{ids.js → native/ids.js} +0 -0
- /package/dist/{jsonStringify.js → native/jsonStringify.js} +0 -0
- /package/dist/{jsonValue.js → native/jsonValue.js} +0 -0
- /package/dist/{media.js → native/media.js} +0 -0
- /package/dist/{permissions.js → native/permissions.js} +0 -0
- /package/dist/{priority.js → native/priority.js} +0 -0
- /package/dist/{storage → native/storage}/FileSystem.js +0 -0
- /package/dist/{storage → native/storage}/index.js +0 -0
- /package/dist/{streamUtils.js → native/streamUtils.js} +0 -0
- /package/dist/{sync.js → native/sync.js} +0 -0
- /package/dist/{typeUtils → native/typeUtils}/accountOrAgentIDfromSessionID.js +0 -0
- /package/dist/{typeUtils → native/typeUtils}/expectGroup.js +0 -0
- /package/dist/{typeUtils → native/typeUtils}/isAccountID.js +0 -0
- /package/dist/{typeUtils → native/typeUtils}/isCoValue.js +0 -0
package/dist/tests/sync.test.js
DELETED
|
@@ -1,1187 +0,0 @@
|
|
|
1
|
-
import { expect, test, describe } from "vitest";
|
|
2
|
-
import { LocalNode } from "../localNode.js";
|
|
3
|
-
import { RawCoMap } from "../coValues/coMap.js";
|
|
4
|
-
import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
|
|
5
|
-
import { connectedPeers, newQueuePair } from "../streamUtils.js";
|
|
6
|
-
import { stableStringify } from "../jsonStringify.js";
|
|
7
|
-
import { WasmCrypto } from "../crypto/WasmCrypto.js";
|
|
8
|
-
import { expectMap } from "../coValue.js";
|
|
9
|
-
import { newRandomSessionID } from "../coValueCore.js";
|
|
10
|
-
import { getPriorityFromHeader } from "../priority.js";
|
|
11
|
-
const Crypto = await WasmCrypto.create();
|
|
12
|
-
test("Node replies with initial tx and header to empty subscribe", async () => {
|
|
13
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
14
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
15
|
-
const group = node.createGroup();
|
|
16
|
-
const map = group.createMap();
|
|
17
|
-
map.set("hello", "world", "trusting");
|
|
18
|
-
const [inRx, inTx] = newQueuePair();
|
|
19
|
-
const [outRx, outTx] = newQueuePair();
|
|
20
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
21
|
-
node.syncManager.addPeer({
|
|
22
|
-
id: "test",
|
|
23
|
-
incoming: inRx,
|
|
24
|
-
outgoing: outTx,
|
|
25
|
-
role: "peer",
|
|
26
|
-
crashOnClose: true,
|
|
27
|
-
});
|
|
28
|
-
await inTx.push({
|
|
29
|
-
action: "load",
|
|
30
|
-
id: map.core.id,
|
|
31
|
-
header: false,
|
|
32
|
-
sessions: {},
|
|
33
|
-
});
|
|
34
|
-
// expect((await outRxQ.next()).value).toMatchObject(admStateEx(admin.id));
|
|
35
|
-
expect((await outRxQ.next()).value).toMatchObject(groupStateEx(group));
|
|
36
|
-
const mapTellKnownStateMsg = (await outRxQ.next()).value;
|
|
37
|
-
expect(mapTellKnownStateMsg).toEqual({
|
|
38
|
-
action: "known",
|
|
39
|
-
...map.core.knownState(),
|
|
40
|
-
});
|
|
41
|
-
// expect((await outRxQ.next()).value).toMatchObject(admContEx(admin.id));
|
|
42
|
-
expect((await outRxQ.next()).value).toMatchObject(groupContentEx(group));
|
|
43
|
-
const newContentMsg = (await outRxQ.next()).value;
|
|
44
|
-
const expectedHeader = {
|
|
45
|
-
type: "comap",
|
|
46
|
-
ruleset: { type: "ownedByGroup", group: group.id },
|
|
47
|
-
meta: null,
|
|
48
|
-
createdAt: map.core.header.createdAt,
|
|
49
|
-
uniqueness: map.core.header.uniqueness,
|
|
50
|
-
};
|
|
51
|
-
expect(newContentMsg).toEqual({
|
|
52
|
-
action: "content",
|
|
53
|
-
id: map.core.id,
|
|
54
|
-
header: expectedHeader,
|
|
55
|
-
new: {
|
|
56
|
-
[node.currentSessionID]: {
|
|
57
|
-
after: 0,
|
|
58
|
-
newTransactions: [
|
|
59
|
-
{
|
|
60
|
-
privacy: "trusting",
|
|
61
|
-
madeAt: map.core.sessionLogs.get(node.currentSessionID)
|
|
62
|
-
.transactions[0].madeAt,
|
|
63
|
-
changes: stableStringify([
|
|
64
|
-
{
|
|
65
|
-
op: "set",
|
|
66
|
-
key: "hello",
|
|
67
|
-
value: "world",
|
|
68
|
-
},
|
|
69
|
-
]),
|
|
70
|
-
},
|
|
71
|
-
],
|
|
72
|
-
lastSignature: map.core.sessionLogs.get(node.currentSessionID)
|
|
73
|
-
.lastSignature,
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
test("Node replies with only new tx to subscribe with some known state", async () => {
|
|
80
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
81
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
82
|
-
const group = node.createGroup();
|
|
83
|
-
const map = group.createMap();
|
|
84
|
-
map.set("hello", "world", "trusting");
|
|
85
|
-
map.set("goodbye", "world", "trusting");
|
|
86
|
-
const [inRx, inTx] = newQueuePair();
|
|
87
|
-
const [outRx, outTx] = newQueuePair();
|
|
88
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
89
|
-
node.syncManager.addPeer({
|
|
90
|
-
id: "test",
|
|
91
|
-
incoming: inRx,
|
|
92
|
-
outgoing: outTx,
|
|
93
|
-
role: "peer",
|
|
94
|
-
crashOnClose: true,
|
|
95
|
-
});
|
|
96
|
-
await inTx.push({
|
|
97
|
-
action: "load",
|
|
98
|
-
id: map.core.id,
|
|
99
|
-
header: true,
|
|
100
|
-
sessions: {
|
|
101
|
-
[node.currentSessionID]: 1,
|
|
102
|
-
},
|
|
103
|
-
});
|
|
104
|
-
// expect((await outRxQ.next()).value).toMatchObject(admStateEx(admin.id));
|
|
105
|
-
expect((await outRxQ.next()).value).toMatchObject(groupStateEx(group));
|
|
106
|
-
const mapTellKnownStateMsg = (await outRxQ.next()).value;
|
|
107
|
-
expect(mapTellKnownStateMsg).toEqual({
|
|
108
|
-
action: "known",
|
|
109
|
-
...map.core.knownState(),
|
|
110
|
-
});
|
|
111
|
-
// expect((await outRxQ.next()).value).toMatchObject(admContEx(admin.id));
|
|
112
|
-
expect((await outRxQ.next()).value).toMatchObject(groupContentEx(group));
|
|
113
|
-
const mapNewContentMsg = (await outRxQ.next()).value;
|
|
114
|
-
expect(mapNewContentMsg).toEqual({
|
|
115
|
-
action: "content",
|
|
116
|
-
id: map.core.id,
|
|
117
|
-
header: undefined,
|
|
118
|
-
new: {
|
|
119
|
-
[node.currentSessionID]: {
|
|
120
|
-
after: 1,
|
|
121
|
-
newTransactions: [
|
|
122
|
-
{
|
|
123
|
-
privacy: "trusting",
|
|
124
|
-
madeAt: map.core.sessionLogs.get(node.currentSessionID)
|
|
125
|
-
.transactions[1].madeAt,
|
|
126
|
-
changes: stableStringify([
|
|
127
|
-
{
|
|
128
|
-
op: "set",
|
|
129
|
-
key: "goodbye",
|
|
130
|
-
value: "world",
|
|
131
|
-
},
|
|
132
|
-
]),
|
|
133
|
-
},
|
|
134
|
-
],
|
|
135
|
-
lastSignature: map.core.sessionLogs.get(node.currentSessionID)
|
|
136
|
-
.lastSignature,
|
|
137
|
-
},
|
|
138
|
-
},
|
|
139
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
test.todo("TODO: node only replies with new tx to subscribe with some known state, even in the depended on coValues");
|
|
143
|
-
test("After subscribing, node sends own known state and new txs to peer", async () => {
|
|
144
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
145
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
146
|
-
const group = node.createGroup();
|
|
147
|
-
const map = group.createMap();
|
|
148
|
-
const [inRx, inTx] = newQueuePair();
|
|
149
|
-
const [outRx, outTx] = newQueuePair();
|
|
150
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
151
|
-
node.syncManager.addPeer({
|
|
152
|
-
id: "test",
|
|
153
|
-
incoming: inRx,
|
|
154
|
-
outgoing: outTx,
|
|
155
|
-
role: "peer",
|
|
156
|
-
crashOnClose: true,
|
|
157
|
-
});
|
|
158
|
-
await inTx.push({
|
|
159
|
-
action: "load",
|
|
160
|
-
id: map.core.id,
|
|
161
|
-
header: false,
|
|
162
|
-
sessions: {
|
|
163
|
-
[node.currentSessionID]: 0,
|
|
164
|
-
},
|
|
165
|
-
});
|
|
166
|
-
// expect((await outRxQ.next()).value).toMatchObject(admStateEx(admin.id));
|
|
167
|
-
expect((await outRxQ.next()).value).toMatchObject(groupStateEx(group));
|
|
168
|
-
const mapTellKnownStateMsg = (await outRxQ.next()).value;
|
|
169
|
-
expect(mapTellKnownStateMsg).toEqual({
|
|
170
|
-
action: "known",
|
|
171
|
-
...map.core.knownState(),
|
|
172
|
-
});
|
|
173
|
-
// expect((await outRxQ.next()).value).toMatchObject(admContEx(admin.id));
|
|
174
|
-
expect((await outRxQ.next()).value).toMatchObject(groupContentEx(group));
|
|
175
|
-
const mapNewContentHeaderOnlyMsg = (await outRxQ.next()).value;
|
|
176
|
-
expect(mapNewContentHeaderOnlyMsg).toEqual({
|
|
177
|
-
action: "content",
|
|
178
|
-
id: map.core.id,
|
|
179
|
-
header: map.core.header,
|
|
180
|
-
new: {},
|
|
181
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
182
|
-
});
|
|
183
|
-
map.set("hello", "world", "trusting");
|
|
184
|
-
const mapEditMsg1 = (await outRxQ.next()).value;
|
|
185
|
-
expect(mapEditMsg1).toEqual({
|
|
186
|
-
action: "content",
|
|
187
|
-
id: map.core.id,
|
|
188
|
-
new: {
|
|
189
|
-
[node.currentSessionID]: {
|
|
190
|
-
after: 0,
|
|
191
|
-
newTransactions: [
|
|
192
|
-
{
|
|
193
|
-
privacy: "trusting",
|
|
194
|
-
madeAt: map.core.sessionLogs.get(node.currentSessionID)
|
|
195
|
-
.transactions[0].madeAt,
|
|
196
|
-
changes: stableStringify([
|
|
197
|
-
{
|
|
198
|
-
op: "set",
|
|
199
|
-
key: "hello",
|
|
200
|
-
value: "world",
|
|
201
|
-
},
|
|
202
|
-
]),
|
|
203
|
-
},
|
|
204
|
-
],
|
|
205
|
-
lastSignature: map.core.sessionLogs.get(node.currentSessionID)
|
|
206
|
-
.lastSignature,
|
|
207
|
-
},
|
|
208
|
-
},
|
|
209
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
210
|
-
});
|
|
211
|
-
map.set("goodbye", "world", "trusting");
|
|
212
|
-
const mapEditMsg2 = (await outRxQ.next()).value;
|
|
213
|
-
expect(mapEditMsg2).toEqual({
|
|
214
|
-
action: "content",
|
|
215
|
-
id: map.core.id,
|
|
216
|
-
new: {
|
|
217
|
-
[node.currentSessionID]: {
|
|
218
|
-
after: 1,
|
|
219
|
-
newTransactions: [
|
|
220
|
-
{
|
|
221
|
-
privacy: "trusting",
|
|
222
|
-
madeAt: map.core.sessionLogs.get(node.currentSessionID)
|
|
223
|
-
.transactions[1].madeAt,
|
|
224
|
-
changes: stableStringify([
|
|
225
|
-
{
|
|
226
|
-
op: "set",
|
|
227
|
-
key: "goodbye",
|
|
228
|
-
value: "world",
|
|
229
|
-
},
|
|
230
|
-
]),
|
|
231
|
-
},
|
|
232
|
-
],
|
|
233
|
-
lastSignature: map.core.sessionLogs.get(node.currentSessionID)
|
|
234
|
-
.lastSignature,
|
|
235
|
-
},
|
|
236
|
-
},
|
|
237
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
238
|
-
});
|
|
239
|
-
});
|
|
240
|
-
test("Client replies with known new content to tellKnownState from server", async () => {
|
|
241
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
242
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
243
|
-
const group = node.createGroup();
|
|
244
|
-
const map = group.createMap();
|
|
245
|
-
map.set("hello", "world", "trusting");
|
|
246
|
-
const [inRx, inTx] = newQueuePair();
|
|
247
|
-
const [outRx, outTx] = newQueuePair();
|
|
248
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
249
|
-
node.syncManager.addPeer({
|
|
250
|
-
id: "test",
|
|
251
|
-
incoming: inRx,
|
|
252
|
-
outgoing: outTx,
|
|
253
|
-
role: "peer",
|
|
254
|
-
crashOnClose: true,
|
|
255
|
-
});
|
|
256
|
-
// expect((await outRxQ.next()).value).toMatchObject(groupStateEx(group));
|
|
257
|
-
await inTx.push({
|
|
258
|
-
action: "known",
|
|
259
|
-
id: map.core.id,
|
|
260
|
-
header: false,
|
|
261
|
-
sessions: {
|
|
262
|
-
[node.currentSessionID]: 0,
|
|
263
|
-
},
|
|
264
|
-
});
|
|
265
|
-
// expect((await outRxQ.next()).value).toMatchObject(admStateEx(admin.id));
|
|
266
|
-
expect((await outRxQ.next()).value).toMatchObject(groupStateEx(group));
|
|
267
|
-
const mapTellKnownStateMsg = (await outRxQ.next()).value;
|
|
268
|
-
expect(mapTellKnownStateMsg).toEqual({
|
|
269
|
-
action: "known",
|
|
270
|
-
...map.core.knownState(),
|
|
271
|
-
});
|
|
272
|
-
// expect((await outRxQ.next()).value).toMatchObject(admContEx(admin.id));
|
|
273
|
-
expect((await outRxQ.next()).value).toMatchObject(groupContentEx(group));
|
|
274
|
-
const mapNewContentMsg = (await outRxQ.next()).value;
|
|
275
|
-
expect(mapNewContentMsg).toEqual({
|
|
276
|
-
action: "content",
|
|
277
|
-
id: map.core.id,
|
|
278
|
-
header: map.core.header,
|
|
279
|
-
new: {
|
|
280
|
-
[node.currentSessionID]: {
|
|
281
|
-
after: 0,
|
|
282
|
-
newTransactions: [
|
|
283
|
-
{
|
|
284
|
-
privacy: "trusting",
|
|
285
|
-
madeAt: map.core.sessionLogs.get(node.currentSessionID)
|
|
286
|
-
.transactions[0].madeAt,
|
|
287
|
-
changes: stableStringify([
|
|
288
|
-
{
|
|
289
|
-
op: "set",
|
|
290
|
-
key: "hello",
|
|
291
|
-
value: "world",
|
|
292
|
-
},
|
|
293
|
-
]),
|
|
294
|
-
},
|
|
295
|
-
],
|
|
296
|
-
lastSignature: map.core.sessionLogs.get(node.currentSessionID)
|
|
297
|
-
.lastSignature,
|
|
298
|
-
},
|
|
299
|
-
},
|
|
300
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
test("No matter the optimistic known state, node respects invalid known state messages and resyncs", async () => {
|
|
304
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
305
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
306
|
-
const group = node.createGroup();
|
|
307
|
-
const map = group.createMap();
|
|
308
|
-
const [inRx, inTx] = newQueuePair();
|
|
309
|
-
const [outRx, outTx] = newQueuePair();
|
|
310
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
311
|
-
node.syncManager.addPeer({
|
|
312
|
-
id: "test",
|
|
313
|
-
incoming: inRx,
|
|
314
|
-
outgoing: outTx,
|
|
315
|
-
role: "peer",
|
|
316
|
-
crashOnClose: true,
|
|
317
|
-
});
|
|
318
|
-
await inTx.push({
|
|
319
|
-
action: "load",
|
|
320
|
-
id: map.core.id,
|
|
321
|
-
header: false,
|
|
322
|
-
sessions: {
|
|
323
|
-
[node.currentSessionID]: 0,
|
|
324
|
-
},
|
|
325
|
-
});
|
|
326
|
-
// expect((await outRxQ.next()).value).toMatchObject(admStateEx(admin.id));
|
|
327
|
-
expect((await outRxQ.next()).value).toMatchObject(groupStateEx(group));
|
|
328
|
-
const mapTellKnownStateMsg = (await outRxQ.next()).value;
|
|
329
|
-
expect(mapTellKnownStateMsg).toEqual({
|
|
330
|
-
action: "known",
|
|
331
|
-
...map.core.knownState(),
|
|
332
|
-
});
|
|
333
|
-
// expect((await outRxQ.next()).value).toMatchObject(admContEx(admin.id));
|
|
334
|
-
expect((await outRxQ.next()).value).toMatchObject(groupContentEx(group));
|
|
335
|
-
const mapNewContentHeaderOnlyMsg = (await outRxQ.next()).value;
|
|
336
|
-
expect(mapNewContentHeaderOnlyMsg).toEqual({
|
|
337
|
-
action: "content",
|
|
338
|
-
id: map.core.id,
|
|
339
|
-
header: map.core.header,
|
|
340
|
-
new: {},
|
|
341
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
342
|
-
});
|
|
343
|
-
map.set("hello", "world", "trusting");
|
|
344
|
-
map.set("goodbye", "world", "trusting");
|
|
345
|
-
const _mapEditMsgs = (await outRxQ.next()).value;
|
|
346
|
-
console.log("Sending correction");
|
|
347
|
-
await inTx.push({
|
|
348
|
-
action: "known",
|
|
349
|
-
isCorrection: true,
|
|
350
|
-
id: map.core.id,
|
|
351
|
-
header: true,
|
|
352
|
-
sessions: {
|
|
353
|
-
[node.currentSessionID]: 1,
|
|
354
|
-
},
|
|
355
|
-
});
|
|
356
|
-
const newContentAfterWrongAssumedState = (await outRxQ.next()).value;
|
|
357
|
-
expect(newContentAfterWrongAssumedState).toEqual({
|
|
358
|
-
action: "content",
|
|
359
|
-
id: map.core.id,
|
|
360
|
-
header: undefined,
|
|
361
|
-
new: {
|
|
362
|
-
[node.currentSessionID]: {
|
|
363
|
-
after: 1,
|
|
364
|
-
newTransactions: [
|
|
365
|
-
{
|
|
366
|
-
privacy: "trusting",
|
|
367
|
-
madeAt: map.core.sessionLogs.get(node.currentSessionID)
|
|
368
|
-
.transactions[1].madeAt,
|
|
369
|
-
changes: stableStringify([
|
|
370
|
-
{
|
|
371
|
-
op: "set",
|
|
372
|
-
key: "goodbye",
|
|
373
|
-
value: "world",
|
|
374
|
-
},
|
|
375
|
-
]),
|
|
376
|
-
},
|
|
377
|
-
],
|
|
378
|
-
lastSignature: map.core.sessionLogs.get(node.currentSessionID)
|
|
379
|
-
.lastSignature,
|
|
380
|
-
},
|
|
381
|
-
},
|
|
382
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
383
|
-
});
|
|
384
|
-
});
|
|
385
|
-
test("If we add a peer, but it never subscribes to a coValue, it won't get any messages", async () => {
|
|
386
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
387
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
388
|
-
const group = node.createGroup();
|
|
389
|
-
const map = group.createMap();
|
|
390
|
-
const [inRx, _inTx] = newQueuePair();
|
|
391
|
-
const [outRx, outTx] = newQueuePair();
|
|
392
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
393
|
-
node.syncManager.addPeer({
|
|
394
|
-
id: "test",
|
|
395
|
-
incoming: inRx,
|
|
396
|
-
outgoing: outTx,
|
|
397
|
-
role: "peer",
|
|
398
|
-
crashOnClose: true,
|
|
399
|
-
});
|
|
400
|
-
map.set("hello", "world", "trusting");
|
|
401
|
-
const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve("neverHappened"), 100));
|
|
402
|
-
const result = await Promise.race([
|
|
403
|
-
outRxQ.next().then((value) => value.value),
|
|
404
|
-
timeoutPromise,
|
|
405
|
-
]);
|
|
406
|
-
expect(result).toEqual("neverHappened");
|
|
407
|
-
});
|
|
408
|
-
test.todo("If we add a server peer, all updates to all coValues are sent to it, even if it doesn't subscribe", async () => {
|
|
409
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
410
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
411
|
-
const group = node.createGroup();
|
|
412
|
-
const map = group.createMap();
|
|
413
|
-
const [inRx, _inTx] = newQueuePair();
|
|
414
|
-
const [outRx, outTx] = newQueuePair();
|
|
415
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
416
|
-
node.syncManager.addPeer({
|
|
417
|
-
id: "test",
|
|
418
|
-
incoming: inRx,
|
|
419
|
-
outgoing: outTx,
|
|
420
|
-
role: "server",
|
|
421
|
-
crashOnClose: true,
|
|
422
|
-
});
|
|
423
|
-
// expect((await outRxQ.next()).value).toMatchObject({
|
|
424
|
-
// action: "load",
|
|
425
|
-
// id: adminID,
|
|
426
|
-
// });
|
|
427
|
-
expect((await outRxQ.next()).value).toMatchObject({
|
|
428
|
-
action: "load",
|
|
429
|
-
id: group.core.id,
|
|
430
|
-
});
|
|
431
|
-
const mapSubscribeMsg = (await outRxQ.next()).value;
|
|
432
|
-
expect(mapSubscribeMsg).toEqual({
|
|
433
|
-
action: "load",
|
|
434
|
-
id: map.core.id,
|
|
435
|
-
header: true,
|
|
436
|
-
sessions: {},
|
|
437
|
-
});
|
|
438
|
-
map.set("hello", "world", "trusting");
|
|
439
|
-
// expect((await outRxQ.next()).value).toMatchObject(admContEx(admin.id));
|
|
440
|
-
expect((await outRxQ.next()).value).toMatchObject(groupContentEx(group));
|
|
441
|
-
const mapNewContentMsg = (await outRxQ.next()).value;
|
|
442
|
-
expect(mapNewContentMsg).toEqual({
|
|
443
|
-
action: "content",
|
|
444
|
-
id: map.core.id,
|
|
445
|
-
header: map.core.header,
|
|
446
|
-
new: {
|
|
447
|
-
[node.currentSessionID]: {
|
|
448
|
-
after: 0,
|
|
449
|
-
newTransactions: [
|
|
450
|
-
{
|
|
451
|
-
privacy: "trusting",
|
|
452
|
-
madeAt: map.core.sessionLogs.get(node.currentSessionID).transactions[0].madeAt,
|
|
453
|
-
changes: stableStringify([
|
|
454
|
-
{
|
|
455
|
-
op: "set",
|
|
456
|
-
key: "hello",
|
|
457
|
-
value: "world",
|
|
458
|
-
},
|
|
459
|
-
]),
|
|
460
|
-
},
|
|
461
|
-
],
|
|
462
|
-
lastSignature: map.core.sessionLogs.get(node.currentSessionID).lastSignature,
|
|
463
|
-
},
|
|
464
|
-
},
|
|
465
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
466
|
-
});
|
|
467
|
-
});
|
|
468
|
-
test.skip("If we add a server peer, newly created coValues are auto-subscribed to", async () => {
|
|
469
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
470
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
471
|
-
const group = node.createGroup();
|
|
472
|
-
const [inRx, _inTx] = newQueuePair();
|
|
473
|
-
const [outRx, outTx] = newQueuePair();
|
|
474
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
475
|
-
node.syncManager.addPeer({
|
|
476
|
-
id: "test",
|
|
477
|
-
incoming: inRx,
|
|
478
|
-
outgoing: outTx,
|
|
479
|
-
role: "server",
|
|
480
|
-
crashOnClose: true,
|
|
481
|
-
});
|
|
482
|
-
// expect((await outRxQ.next()).value).toMatchObject({
|
|
483
|
-
// action: "load",
|
|
484
|
-
// id: admin.id,
|
|
485
|
-
// });
|
|
486
|
-
expect((await outRxQ.next()).value).toMatchObject({
|
|
487
|
-
action: "load",
|
|
488
|
-
id: group.core.id,
|
|
489
|
-
});
|
|
490
|
-
const map = group.createMap();
|
|
491
|
-
const mapSubscribeMsg = (await outRxQ.next()).value;
|
|
492
|
-
expect(mapSubscribeMsg).toEqual({
|
|
493
|
-
action: "load",
|
|
494
|
-
...map.core.knownState(),
|
|
495
|
-
});
|
|
496
|
-
// expect((await outRxQ.next()).value).toMatchObject(admContEx(adminID));
|
|
497
|
-
expect((await outRxQ.next()).value).toMatchObject(groupContentEx(group));
|
|
498
|
-
const mapContentMsg = (await outRxQ.next()).value;
|
|
499
|
-
expect(mapContentMsg).toEqual({
|
|
500
|
-
action: "content",
|
|
501
|
-
id: map.core.id,
|
|
502
|
-
header: map.core.header,
|
|
503
|
-
new: {},
|
|
504
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
505
|
-
});
|
|
506
|
-
});
|
|
507
|
-
test.todo("TODO: when receiving a subscribe response that is behind our optimistic state (due to already sent content), we ignore it");
|
|
508
|
-
test("When we connect a new server peer, we try to sync all existing coValues to it", async () => {
|
|
509
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
510
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
511
|
-
const group = node.createGroup();
|
|
512
|
-
const map = group.createMap();
|
|
513
|
-
const [inRx, _inTx] = newQueuePair();
|
|
514
|
-
const [outRx, outTx] = newQueuePair();
|
|
515
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
516
|
-
node.syncManager.addPeer({
|
|
517
|
-
id: "test",
|
|
518
|
-
incoming: inRx,
|
|
519
|
-
outgoing: outTx,
|
|
520
|
-
role: "server",
|
|
521
|
-
crashOnClose: true,
|
|
522
|
-
});
|
|
523
|
-
// const _adminSubscribeMessage = await outRxQ.next();
|
|
524
|
-
const groupSubscribeMessage = (await outRxQ.next()).value;
|
|
525
|
-
expect(groupSubscribeMessage).toEqual({
|
|
526
|
-
action: "load",
|
|
527
|
-
...group.core.knownState(),
|
|
528
|
-
});
|
|
529
|
-
const secondMessage = (await outRxQ.next()).value;
|
|
530
|
-
expect(secondMessage).toEqual({
|
|
531
|
-
action: "load",
|
|
532
|
-
...map.core.knownState(),
|
|
533
|
-
});
|
|
534
|
-
});
|
|
535
|
-
test("When receiving a subscribe with a known state that is ahead of our own, peers should respond with a corresponding subscribe response message", async () => {
|
|
536
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
537
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
538
|
-
const group = node.createGroup();
|
|
539
|
-
const map = group.createMap();
|
|
540
|
-
const [inRx, inTx] = newQueuePair();
|
|
541
|
-
const [outRx, outTx] = newQueuePair();
|
|
542
|
-
const outRxQ = outRx[Symbol.asyncIterator]();
|
|
543
|
-
node.syncManager.addPeer({
|
|
544
|
-
id: "test",
|
|
545
|
-
incoming: inRx,
|
|
546
|
-
outgoing: outTx,
|
|
547
|
-
role: "peer",
|
|
548
|
-
crashOnClose: true,
|
|
549
|
-
});
|
|
550
|
-
await inTx.push({
|
|
551
|
-
action: "load",
|
|
552
|
-
id: map.core.id,
|
|
553
|
-
header: true,
|
|
554
|
-
sessions: {
|
|
555
|
-
[node.currentSessionID]: 1,
|
|
556
|
-
},
|
|
557
|
-
});
|
|
558
|
-
// expect((await outRxQ.next()).value).toMatchObject(admStateEx(admin.id));
|
|
559
|
-
expect((await outRxQ.next()).value).toMatchObject(groupStateEx(group));
|
|
560
|
-
const mapTellKnownState = (await outRxQ.next()).value;
|
|
561
|
-
expect(mapTellKnownState).toEqual({
|
|
562
|
-
action: "known",
|
|
563
|
-
...map.core.knownState(),
|
|
564
|
-
});
|
|
565
|
-
});
|
|
566
|
-
test.skip("When replaying creation and transactions of a coValue as new content, the receiving peer integrates this information", async () => {
|
|
567
|
-
// TODO: this test is mostly correct but also slightly unrealistic, make sure we pass all messages back and forth as expected and then it should work
|
|
568
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
569
|
-
const node1 = new LocalNode(admin, session, Crypto);
|
|
570
|
-
const group = node1.createGroup();
|
|
571
|
-
const [inRx1, inTx1] = newQueuePair();
|
|
572
|
-
const [outRx1, outTx1] = newQueuePair();
|
|
573
|
-
const outRxQ1 = outRx1[Symbol.asyncIterator]();
|
|
574
|
-
node1.syncManager.addPeer({
|
|
575
|
-
id: "test2",
|
|
576
|
-
incoming: inRx1,
|
|
577
|
-
outgoing: outTx1,
|
|
578
|
-
role: "server",
|
|
579
|
-
crashOnClose: true,
|
|
580
|
-
});
|
|
581
|
-
const node2 = new LocalNode(admin, newRandomSessionID(admin.id), Crypto);
|
|
582
|
-
const [inRx2, inTx2] = newQueuePair();
|
|
583
|
-
const [outRx2, outTx2] = newQueuePair();
|
|
584
|
-
const outRxQ2 = outRx2[Symbol.asyncIterator]();
|
|
585
|
-
node2.syncManager.addPeer({
|
|
586
|
-
id: "test1",
|
|
587
|
-
incoming: inRx2,
|
|
588
|
-
outgoing: outTx2,
|
|
589
|
-
role: "client",
|
|
590
|
-
crashOnClose: true,
|
|
591
|
-
});
|
|
592
|
-
const adminSubscribeMessage = (await outRxQ1.next()).value;
|
|
593
|
-
expect(adminSubscribeMessage).toMatchObject({
|
|
594
|
-
action: "load",
|
|
595
|
-
id: admin.id,
|
|
596
|
-
});
|
|
597
|
-
const groupSubscribeMsg = (await outRxQ1.next()).value;
|
|
598
|
-
expect(groupSubscribeMsg).toMatchObject({
|
|
599
|
-
action: "load",
|
|
600
|
-
id: group.core.id,
|
|
601
|
-
});
|
|
602
|
-
await inTx2.push(adminSubscribeMessage);
|
|
603
|
-
await inTx2.push(groupSubscribeMsg);
|
|
604
|
-
// const adminTellKnownStateMsg = (await outRxQ2.next()).value;
|
|
605
|
-
// expect(adminTellKnownStateMsg).toMatchObject(admStateEx(admin.id));
|
|
606
|
-
const groupTellKnownStateMsg = (await outRxQ2.next()).value;
|
|
607
|
-
expect(groupTellKnownStateMsg).toMatchObject(groupStateEx(group));
|
|
608
|
-
expect(node2.syncManager.peers["test1"].optimisticKnownStates[group.core.id]).toBeDefined();
|
|
609
|
-
// await inTx1.push(adminTellKnownStateMsg);
|
|
610
|
-
await inTx1.push(groupTellKnownStateMsg);
|
|
611
|
-
// const adminContentMsg = (await outRxQ1.next()).value;
|
|
612
|
-
// expect(adminContentMsg).toMatchObject(admContEx(admin.id));
|
|
613
|
-
const groupContentMsg = (await outRxQ1.next()).value;
|
|
614
|
-
expect(groupContentMsg).toMatchObject(groupContentEx(group));
|
|
615
|
-
// await inTx2.push(adminContentMsg);
|
|
616
|
-
await inTx2.push(groupContentMsg);
|
|
617
|
-
const map = group.createMap();
|
|
618
|
-
const mapSubscriptionMsg = (await outRxQ1.next()).value;
|
|
619
|
-
expect(mapSubscriptionMsg).toMatchObject({
|
|
620
|
-
action: "load",
|
|
621
|
-
id: map.core.id,
|
|
622
|
-
});
|
|
623
|
-
const mapNewContentMsg = (await outRxQ1.next()).value;
|
|
624
|
-
expect(mapNewContentMsg).toEqual({
|
|
625
|
-
action: "content",
|
|
626
|
-
id: map.core.id,
|
|
627
|
-
header: map.core.header,
|
|
628
|
-
new: {},
|
|
629
|
-
priority: getPriorityFromHeader(map.core.header),
|
|
630
|
-
});
|
|
631
|
-
await inTx2.push(mapSubscriptionMsg);
|
|
632
|
-
const mapTellKnownStateMsg = (await outRxQ2.next()).value;
|
|
633
|
-
expect(mapTellKnownStateMsg).toEqual({
|
|
634
|
-
action: "known",
|
|
635
|
-
id: map.core.id,
|
|
636
|
-
header: false,
|
|
637
|
-
sessions: {},
|
|
638
|
-
});
|
|
639
|
-
expect(node2.coValues[map.core.id]?.state).toEqual("loading");
|
|
640
|
-
await inTx2.push(mapNewContentMsg);
|
|
641
|
-
map.set("hello", "world", "trusting");
|
|
642
|
-
const mapEditMsg = (await outRxQ1.next()).value;
|
|
643
|
-
await inTx2.push(mapEditMsg);
|
|
644
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
645
|
-
expect(expectMap(node2.expectCoValueLoaded(map.core.id).getCurrentContent()).get("hello")).toEqual("world");
|
|
646
|
-
});
|
|
647
|
-
test.skip("When loading a coValue on one node, the server node it is requested from replies with all the necessary depended on coValues to make it work", async () => {
|
|
648
|
-
/*
|
|
649
|
-
// TODO: this test is mostly correct but also slightly unrealistic, make sure we pass all messages back and forth as expected and then it should work
|
|
650
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
651
|
-
|
|
652
|
-
const node1 = new LocalNode(admin, session, Crypto);
|
|
653
|
-
|
|
654
|
-
const group = node1.createGroup();
|
|
655
|
-
|
|
656
|
-
const map = group.createMap();
|
|
657
|
-
map.set("hello", "world", "trusting");
|
|
658
|
-
|
|
659
|
-
const node2 = new LocalNode(admin, newRandomSessionID(admin.id), Crypto);
|
|
660
|
-
|
|
661
|
-
const [node1asPeer, node2asPeer] = connectedPeers("peer1", "peer2");
|
|
662
|
-
|
|
663
|
-
node1.syncManager.addPeer(node2asPeer);
|
|
664
|
-
node2.syncManager.addPeer(node1asPeer);
|
|
665
|
-
|
|
666
|
-
await node2.loadCoValueCore(map.core.id);
|
|
667
|
-
|
|
668
|
-
expect(
|
|
669
|
-
expectMap(
|
|
670
|
-
node2.expectCoValueLoaded(map.core.id).getCurrentContent(),
|
|
671
|
-
).get("hello"),
|
|
672
|
-
).toEqual("world");
|
|
673
|
-
*/
|
|
674
|
-
});
|
|
675
|
-
test("Can sync a coValue through a server to another client", async () => {
|
|
676
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
677
|
-
const client1 = new LocalNode(admin, session, Crypto);
|
|
678
|
-
const group = client1.createGroup();
|
|
679
|
-
const map = group.createMap();
|
|
680
|
-
map.set("hello", "world", "trusting");
|
|
681
|
-
const [serverUser, serverSession] = randomAnonymousAccountAndSessionID();
|
|
682
|
-
const server = new LocalNode(serverUser, serverSession, Crypto);
|
|
683
|
-
const [serverAsPeerForClient1, client1AsPeer] = connectedPeers("serverFor1", "client1", {
|
|
684
|
-
peer1role: "server",
|
|
685
|
-
peer2role: "client",
|
|
686
|
-
trace: true,
|
|
687
|
-
});
|
|
688
|
-
client1.syncManager.addPeer(serverAsPeerForClient1);
|
|
689
|
-
server.syncManager.addPeer(client1AsPeer);
|
|
690
|
-
const client2 = new LocalNode(admin, newRandomSessionID(admin.id), Crypto);
|
|
691
|
-
const [serverAsPeerForClient2, client2AsPeer] = connectedPeers("serverFor2", "client2", {
|
|
692
|
-
peer1role: "server",
|
|
693
|
-
peer2role: "client",
|
|
694
|
-
trace: true,
|
|
695
|
-
});
|
|
696
|
-
client2.syncManager.addPeer(serverAsPeerForClient2);
|
|
697
|
-
server.syncManager.addPeer(client2AsPeer);
|
|
698
|
-
const mapOnClient2 = await client2.loadCoValueCore(map.core.id);
|
|
699
|
-
if (mapOnClient2 === "unavailable") {
|
|
700
|
-
throw new Error("Map is unavailable");
|
|
701
|
-
}
|
|
702
|
-
expect(expectMap(mapOnClient2.getCurrentContent()).get("hello")).toEqual("world");
|
|
703
|
-
});
|
|
704
|
-
test("Can sync a coValue with private transactions through a server to another client", async () => {
|
|
705
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
706
|
-
const client1 = new LocalNode(admin, session, Crypto);
|
|
707
|
-
const group = client1.createGroup();
|
|
708
|
-
const map = group.createMap();
|
|
709
|
-
map.set("hello", "world", "private");
|
|
710
|
-
const [serverUser, serverSession] = randomAnonymousAccountAndSessionID();
|
|
711
|
-
const server = new LocalNode(serverUser, serverSession, Crypto);
|
|
712
|
-
const [serverAsPeer, client1AsPeer] = connectedPeers("server", "client1", {
|
|
713
|
-
trace: true,
|
|
714
|
-
peer1role: "server",
|
|
715
|
-
peer2role: "client",
|
|
716
|
-
});
|
|
717
|
-
client1.syncManager.addPeer(serverAsPeer);
|
|
718
|
-
server.syncManager.addPeer(client1AsPeer);
|
|
719
|
-
const client2 = new LocalNode(admin, newRandomSessionID(admin.id), Crypto);
|
|
720
|
-
const [serverAsOtherPeer, client2AsPeer] = connectedPeers("server", "client2", {
|
|
721
|
-
trace: true,
|
|
722
|
-
peer1role: "server",
|
|
723
|
-
peer2role: "client",
|
|
724
|
-
});
|
|
725
|
-
client2.syncManager.addPeer(serverAsOtherPeer);
|
|
726
|
-
server.syncManager.addPeer(client2AsPeer);
|
|
727
|
-
const mapOnClient2 = await client2.loadCoValueCore(map.core.id);
|
|
728
|
-
if (mapOnClient2 === "unavailable") {
|
|
729
|
-
throw new Error("Map is unavailable");
|
|
730
|
-
}
|
|
731
|
-
expect(expectMap(mapOnClient2.getCurrentContent()).get("hello")).toEqual("world");
|
|
732
|
-
});
|
|
733
|
-
test.skip("When a peer's incoming/readable stream closes, we remove the peer", async () => {
|
|
734
|
-
/*
|
|
735
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
736
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
737
|
-
|
|
738
|
-
const group = node.createGroup();
|
|
739
|
-
|
|
740
|
-
const [inRx, inTx] = await Effect.runPromise(newStreamPair());
|
|
741
|
-
const [outRx, outTx] = await Effect.runPromise(newStreamPair());
|
|
742
|
-
|
|
743
|
-
node.syncManager.addPeer({
|
|
744
|
-
id: "test",
|
|
745
|
-
incoming: inRx,
|
|
746
|
-
outgoing: outTx,
|
|
747
|
-
role: "server",
|
|
748
|
-
});
|
|
749
|
-
|
|
750
|
-
// expect(yield* Queue.take(outRxQ)).toMatchObject({
|
|
751
|
-
// action: "load",
|
|
752
|
-
// id: admin.id,
|
|
753
|
-
// });
|
|
754
|
-
expect(yield * Queue.take(outRxQ)).toMatchObject({
|
|
755
|
-
action: "load",
|
|
756
|
-
id: group.core.id,
|
|
757
|
-
});
|
|
758
|
-
|
|
759
|
-
const map = group.createMap();
|
|
760
|
-
|
|
761
|
-
const mapSubscribeMsg = await reader.read();
|
|
762
|
-
|
|
763
|
-
expect(mapSubscribeMsg.value).toEqual({
|
|
764
|
-
action: "load",
|
|
765
|
-
...map.core.knownState(),
|
|
766
|
-
} satisfies SyncMessage);
|
|
767
|
-
|
|
768
|
-
// expect(yield* Queue.take(outRxQ)).toMatchObject(admContEx(admin.id));
|
|
769
|
-
expect(yield * Queue.take(outRxQ)).toMatchObject(groupContentEx(group));
|
|
770
|
-
|
|
771
|
-
const mapContentMsg = await reader.read();
|
|
772
|
-
|
|
773
|
-
expect(mapContentMsg.value).toEqual({
|
|
774
|
-
action: "content",
|
|
775
|
-
id: map.core.id,
|
|
776
|
-
header: map.core.header,
|
|
777
|
-
new: {},
|
|
778
|
-
} satisfies SyncMessage);
|
|
779
|
-
|
|
780
|
-
await inTx.abort();
|
|
781
|
-
|
|
782
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
783
|
-
|
|
784
|
-
expect(node.syncManager.peers["test"]).toBeUndefined();
|
|
785
|
-
*/
|
|
786
|
-
});
|
|
787
|
-
test.skip("When a peer's outgoing/writable stream closes, we remove the peer", async () => {
|
|
788
|
-
/*
|
|
789
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
790
|
-
const node = new LocalNode(admin, session, Crypto);
|
|
791
|
-
|
|
792
|
-
const group = node.createGroup();
|
|
793
|
-
|
|
794
|
-
const [inRx] = await Effect.runPromise(newStreamPair());
|
|
795
|
-
const [outRx, outTx] = await Effect.runPromise(newStreamPair());
|
|
796
|
-
|
|
797
|
-
node.syncManager.addPeer({
|
|
798
|
-
id: "test",
|
|
799
|
-
incoming: inRx,
|
|
800
|
-
outgoing: outTx,
|
|
801
|
-
role: "server",
|
|
802
|
-
});
|
|
803
|
-
|
|
804
|
-
// expect(yield* Queue.take(outRxQ)).toMatchObject({
|
|
805
|
-
// action: "load",
|
|
806
|
-
// id: admin.id,
|
|
807
|
-
// });
|
|
808
|
-
expect(yield * Queue.take(outRxQ)).toMatchObject({
|
|
809
|
-
action: "load",
|
|
810
|
-
id: group.core.id,
|
|
811
|
-
});
|
|
812
|
-
|
|
813
|
-
const map = group.createMap();
|
|
814
|
-
|
|
815
|
-
const mapSubscribeMsg = await reader.read();
|
|
816
|
-
|
|
817
|
-
expect(mapSubscribeMsg.value).toEqual({
|
|
818
|
-
action: "load",
|
|
819
|
-
...map.core.knownState(),
|
|
820
|
-
} satisfies SyncMessage);
|
|
821
|
-
|
|
822
|
-
// expect(yield* Queue.take(outRxQ)).toMatchObject(admContEx(admin.id));
|
|
823
|
-
expect(yield * Queue.take(outRxQ)).toMatchObject(groupContentEx(group));
|
|
824
|
-
|
|
825
|
-
const mapContentMsg = await reader.read();
|
|
826
|
-
|
|
827
|
-
expect(mapContentMsg.value).toEqual({
|
|
828
|
-
action: "content",
|
|
829
|
-
id: map.core.id,
|
|
830
|
-
header: map.core.header,
|
|
831
|
-
new: {},
|
|
832
|
-
} satisfies SyncMessage);
|
|
833
|
-
|
|
834
|
-
reader.releaseLock();
|
|
835
|
-
await outRx.cancel();
|
|
836
|
-
|
|
837
|
-
map.set("hello", "world", "trusting");
|
|
838
|
-
|
|
839
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
840
|
-
|
|
841
|
-
expect(node.syncManager.peers["test"]).toBeUndefined();
|
|
842
|
-
*/
|
|
843
|
-
});
|
|
844
|
-
test("If we start loading a coValue before connecting to a peer that has it, it will load it once we connect", async () => {
|
|
845
|
-
const [admin, session] = randomAnonymousAccountAndSessionID();
|
|
846
|
-
const node1 = new LocalNode(admin, session, Crypto);
|
|
847
|
-
const group = node1.createGroup();
|
|
848
|
-
const map = group.createMap();
|
|
849
|
-
map.set("hello", "world", "trusting");
|
|
850
|
-
const node2 = new LocalNode(admin, newRandomSessionID(admin.id), Crypto);
|
|
851
|
-
const [node1asPeer, node2asPeer] = connectedPeers("peer1", "peer2", {
|
|
852
|
-
peer1role: "server",
|
|
853
|
-
peer2role: "client",
|
|
854
|
-
trace: true,
|
|
855
|
-
});
|
|
856
|
-
node1.syncManager.addPeer(node2asPeer);
|
|
857
|
-
const mapOnNode2Promise = node2.loadCoValueCore(map.core.id);
|
|
858
|
-
expect(node2.coValues[map.core.id]?.state).toEqual("loading");
|
|
859
|
-
node2.syncManager.addPeer(node1asPeer);
|
|
860
|
-
const mapOnNode2 = await mapOnNode2Promise;
|
|
861
|
-
if (mapOnNode2 === "unavailable") {
|
|
862
|
-
throw new Error("Map is unavailable");
|
|
863
|
-
}
|
|
864
|
-
expect(expectMap(mapOnNode2.getCurrentContent()).get("hello")).toEqual("world");
|
|
865
|
-
});
|
|
866
|
-
describe("sync - extra tests", () => {
|
|
867
|
-
test("Node handles disconnection and reconnection of a peer gracefully", async () => {
|
|
868
|
-
// Create two nodes
|
|
869
|
-
const [admin1, session1] = randomAnonymousAccountAndSessionID();
|
|
870
|
-
const node1 = new LocalNode(admin1, session1, Crypto);
|
|
871
|
-
const [admin2, session2] = randomAnonymousAccountAndSessionID();
|
|
872
|
-
const node2 = new LocalNode(admin2, session2, Crypto);
|
|
873
|
-
// Create a group and a map on node1
|
|
874
|
-
const group = node1.createGroup();
|
|
875
|
-
group.addMember("everyone", "writer");
|
|
876
|
-
const map = group.createMap();
|
|
877
|
-
map.set("key1", "value1", "trusting");
|
|
878
|
-
// Connect the nodes
|
|
879
|
-
const [node1AsPeer, node2AsPeer] = connectedPeers("node1", "node2", {
|
|
880
|
-
peer1role: "server",
|
|
881
|
-
peer2role: "client",
|
|
882
|
-
});
|
|
883
|
-
node1.syncManager.addPeer(node2AsPeer);
|
|
884
|
-
node2.syncManager.addPeer(node1AsPeer);
|
|
885
|
-
// Wait for initial sync
|
|
886
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
887
|
-
// Verify that node2 has received the map
|
|
888
|
-
const mapOnNode2 = await node2.loadCoValueCore(map.core.id);
|
|
889
|
-
if (mapOnNode2 === "unavailable") {
|
|
890
|
-
throw new Error("Map is unavailable on node2");
|
|
891
|
-
}
|
|
892
|
-
expect(expectMap(mapOnNode2.getCurrentContent()).get("key1")).toEqual("value1");
|
|
893
|
-
// Simulate disconnection
|
|
894
|
-
node1.syncManager.gracefulShutdown();
|
|
895
|
-
node2.syncManager.gracefulShutdown();
|
|
896
|
-
// Make changes on node1 while disconnected
|
|
897
|
-
map.set("key2", "value2", "trusting");
|
|
898
|
-
// Simulate reconnection
|
|
899
|
-
const [newNode1AsPeer, newNode2AsPeer] = connectedPeers("node11", "node22", {
|
|
900
|
-
peer1role: "server",
|
|
901
|
-
peer2role: "client",
|
|
902
|
-
// trace: true,
|
|
903
|
-
});
|
|
904
|
-
node1.syncManager.addPeer(newNode2AsPeer);
|
|
905
|
-
node2.syncManager.addPeer(newNode1AsPeer);
|
|
906
|
-
// Wait for re-sync
|
|
907
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
908
|
-
// Verify that node2 has received the changes made during disconnection
|
|
909
|
-
const updatedMapOnNode2 = await node2.loadCoValueCore(map.core.id);
|
|
910
|
-
if (updatedMapOnNode2 === "unavailable") {
|
|
911
|
-
throw new Error("Updated map is unavailable on node2");
|
|
912
|
-
}
|
|
913
|
-
expect(expectMap(updatedMapOnNode2.getCurrentContent()).get("key2")).toEqual("value2");
|
|
914
|
-
// Make a new change on node2 to verify two-way sync
|
|
915
|
-
const mapOnNode2ForEdit = await node2.loadCoValueCore(map.core.id);
|
|
916
|
-
if (mapOnNode2ForEdit === "unavailable") {
|
|
917
|
-
throw new Error("Updated map is unavailable on node2");
|
|
918
|
-
}
|
|
919
|
-
const success = mapOnNode2ForEdit.makeTransaction([
|
|
920
|
-
{
|
|
921
|
-
op: "set",
|
|
922
|
-
key: "key3",
|
|
923
|
-
value: "value3",
|
|
924
|
-
},
|
|
925
|
-
], "trusting");
|
|
926
|
-
if (!success) {
|
|
927
|
-
throw new Error("Failed to make transaction");
|
|
928
|
-
}
|
|
929
|
-
// Wait for sync back to node1
|
|
930
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
931
|
-
const mapOnNode1 = await node1.loadCoValueCore(map.core.id);
|
|
932
|
-
if (mapOnNode1 === "unavailable") {
|
|
933
|
-
throw new Error("Updated map is unavailable on node1");
|
|
934
|
-
}
|
|
935
|
-
// Verify that node1 has received the change from node2
|
|
936
|
-
expect(expectMap(mapOnNode1.getCurrentContent()).get("key3")).toEqual("value3");
|
|
937
|
-
});
|
|
938
|
-
test("Concurrent modifications on multiple nodes are resolved correctly", async () => {
|
|
939
|
-
// Create three nodes
|
|
940
|
-
const [admin1, session1] = randomAnonymousAccountAndSessionID();
|
|
941
|
-
const node1 = new LocalNode(admin1, session1, Crypto);
|
|
942
|
-
const [admin2, session2] = randomAnonymousAccountAndSessionID();
|
|
943
|
-
const node2 = new LocalNode(admin2, session2, Crypto);
|
|
944
|
-
const [admin3, session3] = randomAnonymousAccountAndSessionID();
|
|
945
|
-
const node3 = new LocalNode(admin3, session3, Crypto);
|
|
946
|
-
// Create a group and a map on node1
|
|
947
|
-
const group = node1.createGroup();
|
|
948
|
-
group.addMember("everyone", "writer");
|
|
949
|
-
const map = group.createMap();
|
|
950
|
-
// Connect the nodes in a triangle topology
|
|
951
|
-
const [node1AsPeerFor2, node2AsPeerFor1] = connectedPeers("node1", "node2", {
|
|
952
|
-
peer1role: "server",
|
|
953
|
-
peer2role: "client",
|
|
954
|
-
// trace: true,
|
|
955
|
-
});
|
|
956
|
-
const [node2AsPeerFor3, node3AsPeerFor2] = connectedPeers("node2", "node3", {
|
|
957
|
-
peer1role: "server",
|
|
958
|
-
peer2role: "client",
|
|
959
|
-
// trace: true,
|
|
960
|
-
});
|
|
961
|
-
const [node3AsPeerFor1, node1AsPeerFor3] = connectedPeers("node3", "node1", {
|
|
962
|
-
peer1role: "server",
|
|
963
|
-
peer2role: "client",
|
|
964
|
-
// trace: true,
|
|
965
|
-
});
|
|
966
|
-
node1.syncManager.addPeer(node2AsPeerFor1);
|
|
967
|
-
node1.syncManager.addPeer(node3AsPeerFor1);
|
|
968
|
-
node2.syncManager.addPeer(node1AsPeerFor2);
|
|
969
|
-
node2.syncManager.addPeer(node3AsPeerFor2);
|
|
970
|
-
node3.syncManager.addPeer(node1AsPeerFor3);
|
|
971
|
-
node3.syncManager.addPeer(node2AsPeerFor3);
|
|
972
|
-
// Wait for initial sync
|
|
973
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
974
|
-
// Verify that all nodes have the map
|
|
975
|
-
const mapOnNode1 = await node1.loadCoValueCore(map.core.id);
|
|
976
|
-
const mapOnNode2 = await node2.loadCoValueCore(map.core.id);
|
|
977
|
-
const mapOnNode3 = await node3.loadCoValueCore(map.core.id);
|
|
978
|
-
if (mapOnNode1 === "unavailable" ||
|
|
979
|
-
mapOnNode2 === "unavailable" ||
|
|
980
|
-
mapOnNode3 === "unavailable") {
|
|
981
|
-
throw new Error("Map is unavailable on node2 or node3");
|
|
982
|
-
}
|
|
983
|
-
// Perform concurrent modifications
|
|
984
|
-
map.set("key1", "value1", "trusting");
|
|
985
|
-
new RawCoMap(mapOnNode2).set("key2", "value2", "trusting");
|
|
986
|
-
new RawCoMap(mapOnNode3).set("key3", "value3", "trusting");
|
|
987
|
-
// Wait for sync to complete
|
|
988
|
-
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
989
|
-
// Verify that all nodes have the same final state
|
|
990
|
-
const finalStateNode1 = expectMap(mapOnNode1.getCurrentContent());
|
|
991
|
-
const finalStateNode2 = expectMap(mapOnNode2.getCurrentContent());
|
|
992
|
-
const finalStateNode3 = expectMap(mapOnNode3.getCurrentContent());
|
|
993
|
-
const expectedState = {
|
|
994
|
-
key1: "value1",
|
|
995
|
-
key2: "value2",
|
|
996
|
-
key3: "value3",
|
|
997
|
-
};
|
|
998
|
-
expect(finalStateNode1.toJSON()).toEqual(expectedState);
|
|
999
|
-
expect(finalStateNode2.toJSON()).toEqual(expectedState);
|
|
1000
|
-
expect(finalStateNode3.toJSON()).toEqual(expectedState);
|
|
1001
|
-
});
|
|
1002
|
-
test.skip("Large coValues are synced efficiently in chunks", async () => {
|
|
1003
|
-
// Create two nodes
|
|
1004
|
-
const [admin1, session1] = randomAnonymousAccountAndSessionID();
|
|
1005
|
-
const node1 = new LocalNode(admin1, session1, Crypto);
|
|
1006
|
-
const [admin2, session2] = randomAnonymousAccountAndSessionID();
|
|
1007
|
-
const node2 = new LocalNode(admin2, session2, Crypto);
|
|
1008
|
-
// Create a group and a large map on node1
|
|
1009
|
-
const group = node1.createGroup();
|
|
1010
|
-
group.addMember("everyone", "writer");
|
|
1011
|
-
const largeMap = group.createMap();
|
|
1012
|
-
// Generate a large amount of data (about 10MB)
|
|
1013
|
-
const dataSize = 1 * 1024 * 1024;
|
|
1014
|
-
const chunkSize = 1024; // 1KB chunks
|
|
1015
|
-
const chunks = dataSize / chunkSize;
|
|
1016
|
-
for (let i = 0; i < chunks; i++) {
|
|
1017
|
-
const key = `key${i}`;
|
|
1018
|
-
const value = Buffer.alloc(chunkSize, `value${i}`).toString("base64");
|
|
1019
|
-
largeMap.set(key, value, "trusting");
|
|
1020
|
-
}
|
|
1021
|
-
// Connect the nodes
|
|
1022
|
-
const [node1AsPeer, node2AsPeer] = connectedPeers("node1", "node2", {
|
|
1023
|
-
peer1role: "server",
|
|
1024
|
-
peer2role: "client",
|
|
1025
|
-
});
|
|
1026
|
-
node1.syncManager.addPeer(node2AsPeer);
|
|
1027
|
-
node2.syncManager.addPeer(node1AsPeer);
|
|
1028
|
-
await new Promise((resolve) => setTimeout(resolve, 4000));
|
|
1029
|
-
// Measure sync time
|
|
1030
|
-
const startSync = performance.now();
|
|
1031
|
-
// Load the large map on node2
|
|
1032
|
-
const largeMapOnNode2 = await node2.loadCoValueCore(largeMap.core.id);
|
|
1033
|
-
if (largeMapOnNode2 === "unavailable") {
|
|
1034
|
-
throw new Error("Large map is unavailable on node2");
|
|
1035
|
-
}
|
|
1036
|
-
const endSync = performance.now();
|
|
1037
|
-
const syncTime = endSync - startSync;
|
|
1038
|
-
// Verify that all data was synced correctly
|
|
1039
|
-
const syncedMap = new RawCoMap(largeMapOnNode2);
|
|
1040
|
-
expect(Object.keys(largeMapOnNode2.getCurrentContent().toJSON() || {})
|
|
1041
|
-
.length).toBe(chunks);
|
|
1042
|
-
for (let i = 0; i < chunks; i++) {
|
|
1043
|
-
const key = `key${i}`;
|
|
1044
|
-
const expectedValue = Buffer.alloc(chunkSize, `value${i}`).toString("base64");
|
|
1045
|
-
expect(syncedMap.get(key)).toBe(expectedValue);
|
|
1046
|
-
}
|
|
1047
|
-
// Check that sync time is reasonable (this threshold may need adjustment)
|
|
1048
|
-
const reasonableSyncTime = 10; // 30 seconds
|
|
1049
|
-
expect(syncTime).toBeLessThan(reasonableSyncTime);
|
|
1050
|
-
// Check memory usage (this threshold may need adjustment)
|
|
1051
|
-
const memoryUsage = process.memoryUsage().heapUsed / 1024 / 1024; // in MB
|
|
1052
|
-
const reasonableMemoryUsage = 1; // 500 MB
|
|
1053
|
-
expect(memoryUsage).toBeLessThan(reasonableMemoryUsage);
|
|
1054
|
-
});
|
|
1055
|
-
test("Node correctly handles and recovers from network partitions", async () => {
|
|
1056
|
-
// Create three nodes
|
|
1057
|
-
const [admin1, session1] = randomAnonymousAccountAndSessionID();
|
|
1058
|
-
const node1 = new LocalNode(admin1, session1, Crypto);
|
|
1059
|
-
const [admin2, session2] = randomAnonymousAccountAndSessionID();
|
|
1060
|
-
const node2 = new LocalNode(admin2, session2, Crypto);
|
|
1061
|
-
const [admin3, session3] = randomAnonymousAccountAndSessionID();
|
|
1062
|
-
const node3 = new LocalNode(admin3, session3, Crypto);
|
|
1063
|
-
// Create a group and a map on node1
|
|
1064
|
-
const group = node1.createGroup();
|
|
1065
|
-
group.addMember("everyone", "writer");
|
|
1066
|
-
const map = group.createMap();
|
|
1067
|
-
map.set("initial", "value", "trusting");
|
|
1068
|
-
// Connect all nodes
|
|
1069
|
-
const [node1AsPeerFor2, node2AsPeerFor1] = connectedPeers("node1", "node2", {
|
|
1070
|
-
peer1role: "server",
|
|
1071
|
-
peer2role: "client",
|
|
1072
|
-
// trace: true,
|
|
1073
|
-
});
|
|
1074
|
-
const [node2AsPeerFor3, node3AsPeerFor2] = connectedPeers("node2", "node3", {
|
|
1075
|
-
peer1role: "server",
|
|
1076
|
-
peer2role: "client",
|
|
1077
|
-
// trace: true,
|
|
1078
|
-
});
|
|
1079
|
-
const [node3AsPeerFor1, node1AsPeerFor3] = connectedPeers("node3", "node1", {
|
|
1080
|
-
peer1role: "server",
|
|
1081
|
-
peer2role: "client",
|
|
1082
|
-
// trace: true,
|
|
1083
|
-
});
|
|
1084
|
-
node1.syncManager.addPeer(node2AsPeerFor1);
|
|
1085
|
-
node1.syncManager.addPeer(node3AsPeerFor1);
|
|
1086
|
-
node2.syncManager.addPeer(node1AsPeerFor2);
|
|
1087
|
-
node2.syncManager.addPeer(node3AsPeerFor2);
|
|
1088
|
-
node3.syncManager.addPeer(node1AsPeerFor3);
|
|
1089
|
-
node3.syncManager.addPeer(node2AsPeerFor3);
|
|
1090
|
-
// Wait for initial sync
|
|
1091
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
1092
|
-
// Verify initial state
|
|
1093
|
-
const mapOnNode1Core = await node1.loadCoValueCore(map.core.id);
|
|
1094
|
-
const mapOnNode2Core = await node2.loadCoValueCore(map.core.id);
|
|
1095
|
-
const mapOnNode3Core = await node3.loadCoValueCore(map.core.id);
|
|
1096
|
-
if (mapOnNode1Core === "unavailable" ||
|
|
1097
|
-
mapOnNode2Core === "unavailable" ||
|
|
1098
|
-
mapOnNode3Core === "unavailable") {
|
|
1099
|
-
throw new Error("Map is unavailable on node2 or node3");
|
|
1100
|
-
}
|
|
1101
|
-
// const mapOnNode1 = new RawCoMap(mapOnNode1Core);
|
|
1102
|
-
const mapOnNode2 = new RawCoMap(mapOnNode2Core);
|
|
1103
|
-
const mapOnNode3 = new RawCoMap(mapOnNode3Core);
|
|
1104
|
-
expect(mapOnNode2.get("initial")).toBe("value");
|
|
1105
|
-
expect(mapOnNode3.get("initial")).toBe("value");
|
|
1106
|
-
// Simulate network partition: disconnect node3 from node1 and node2
|
|
1107
|
-
node1.syncManager.peers["node3"]?.gracefulShutdown();
|
|
1108
|
-
delete node1.syncManager.peers["node3"];
|
|
1109
|
-
node2.syncManager.peers["node3"]?.gracefulShutdown();
|
|
1110
|
-
delete node2.syncManager.peers["node3"];
|
|
1111
|
-
node3.syncManager.peers["node1"]?.gracefulShutdown();
|
|
1112
|
-
delete node3.syncManager.peers["node1"];
|
|
1113
|
-
node3.syncManager.peers["node2"]?.gracefulShutdown();
|
|
1114
|
-
delete node3.syncManager.peers["node2"];
|
|
1115
|
-
// Make changes on both sides of the partition
|
|
1116
|
-
map.set("node1", "partition", "trusting");
|
|
1117
|
-
mapOnNode2.set("node2", "partition", "trusting");
|
|
1118
|
-
mapOnNode3.set("node3", "partition", "trusting");
|
|
1119
|
-
// Wait for sync between node1 and node2
|
|
1120
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
1121
|
-
// Verify that node1 and node2 are in sync, but node3 is not
|
|
1122
|
-
expect(expectMap(mapOnNode1Core.getCurrentContent()).get("node1")).toBe("partition");
|
|
1123
|
-
expect(expectMap(mapOnNode1Core.getCurrentContent()).get("node2")).toBe("partition");
|
|
1124
|
-
expect(expectMap(mapOnNode1Core.getCurrentContent()).toJSON()?.node3).toBe(undefined);
|
|
1125
|
-
expect(expectMap(mapOnNode2Core.getCurrentContent()).get("node1")).toBe("partition");
|
|
1126
|
-
expect(expectMap(mapOnNode2Core.getCurrentContent()).get("node2")).toBe("partition");
|
|
1127
|
-
expect(expectMap(mapOnNode2Core.getCurrentContent()).toJSON()?.node3).toBe(undefined);
|
|
1128
|
-
expect(expectMap(mapOnNode3Core.getCurrentContent()).toJSON()?.node1).toBe(undefined);
|
|
1129
|
-
expect(expectMap(mapOnNode3Core.getCurrentContent()).toJSON()?.node2).toBe(undefined);
|
|
1130
|
-
expect(expectMap(mapOnNode3Core.getCurrentContent()).toJSON()?.node3).toBe("partition");
|
|
1131
|
-
// Restore connectivity
|
|
1132
|
-
const [newNode3AsPeerFor1, newNode1AsPeerFor3] = connectedPeers("node3", "node1", {
|
|
1133
|
-
peer1role: "server",
|
|
1134
|
-
peer2role: "client",
|
|
1135
|
-
trace: true,
|
|
1136
|
-
});
|
|
1137
|
-
const [newNode3AsPeerFor2, newNode2AsPeerFor3] = connectedPeers("node3", "node2", {
|
|
1138
|
-
peer1role: "server",
|
|
1139
|
-
peer2role: "client",
|
|
1140
|
-
trace: true,
|
|
1141
|
-
});
|
|
1142
|
-
node1.syncManager.addPeer(newNode3AsPeerFor1);
|
|
1143
|
-
node2.syncManager.addPeer(newNode3AsPeerFor2);
|
|
1144
|
-
node3.syncManager.addPeer(newNode1AsPeerFor3);
|
|
1145
|
-
node3.syncManager.addPeer(newNode2AsPeerFor3);
|
|
1146
|
-
// Wait for re-sync
|
|
1147
|
-
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
1148
|
-
// Verify final state: all nodes should have all changes
|
|
1149
|
-
const finalStateNode1 = expectMap(mapOnNode1Core.getCurrentContent()).toJSON();
|
|
1150
|
-
const finalStateNode2 = expectMap(mapOnNode2Core.getCurrentContent()).toJSON();
|
|
1151
|
-
const finalStateNode3 = expectMap(mapOnNode3Core.getCurrentContent()).toJSON();
|
|
1152
|
-
const expectedFinalState = {
|
|
1153
|
-
initial: "value",
|
|
1154
|
-
node1: "partition",
|
|
1155
|
-
node2: "partition",
|
|
1156
|
-
node3: "partition",
|
|
1157
|
-
};
|
|
1158
|
-
expect(finalStateNode1).toEqual(expectedFinalState);
|
|
1159
|
-
expect(finalStateNode2).toEqual(expectedFinalState);
|
|
1160
|
-
expect(finalStateNode3).toEqual(expectedFinalState);
|
|
1161
|
-
});
|
|
1162
|
-
});
|
|
1163
|
-
function groupContentEx(group) {
|
|
1164
|
-
return {
|
|
1165
|
-
action: "content",
|
|
1166
|
-
id: group.core.id,
|
|
1167
|
-
};
|
|
1168
|
-
}
|
|
1169
|
-
function _admContEx(adminID) {
|
|
1170
|
-
return {
|
|
1171
|
-
action: "content",
|
|
1172
|
-
id: adminID,
|
|
1173
|
-
};
|
|
1174
|
-
}
|
|
1175
|
-
function groupStateEx(group) {
|
|
1176
|
-
return {
|
|
1177
|
-
action: "known",
|
|
1178
|
-
id: group.core.id,
|
|
1179
|
-
};
|
|
1180
|
-
}
|
|
1181
|
-
function _admStateEx(adminID) {
|
|
1182
|
-
return {
|
|
1183
|
-
action: "known",
|
|
1184
|
-
id: adminID,
|
|
1185
|
-
};
|
|
1186
|
-
}
|
|
1187
|
-
//# sourceMappingURL=sync.test.js.map
|