cojson 0.13.17 → 0.13.20
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 +1 -1
- package/CHANGELOG.md +18 -0
- package/dist/PeerState.d.ts +4 -1
- package/dist/PeerState.d.ts.map +1 -1
- package/dist/PeerState.js +16 -36
- package/dist/PeerState.js.map +1 -1
- package/dist/SyncStateManager.d.ts.map +1 -1
- package/dist/SyncStateManager.js +2 -3
- package/dist/SyncStateManager.js.map +1 -1
- package/dist/coValue.d.ts +4 -4
- package/dist/coValue.d.ts.map +1 -1
- package/dist/coValue.js +4 -4
- package/dist/coValue.js.map +1 -1
- package/dist/coValueCore/coValueCore.d.ts +143 -0
- package/dist/coValueCore/coValueCore.d.ts.map +1 -0
- package/dist/{coValueCore.js → coValueCore/coValueCore.js} +325 -253
- package/dist/coValueCore/coValueCore.js.map +1 -0
- package/dist/coValueCore/verifiedState.d.ts +65 -0
- package/dist/coValueCore/verifiedState.d.ts.map +1 -0
- package/dist/coValueCore/verifiedState.js +210 -0
- package/dist/coValueCore/verifiedState.js.map +1 -0
- package/dist/coValues/account.d.ts +8 -10
- package/dist/coValues/account.d.ts.map +1 -1
- package/dist/coValues/account.js +12 -13
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/coList.d.ts +3 -3
- package/dist/coValues/coList.d.ts.map +1 -1
- package/dist/coValues/coList.js +6 -3
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/coMap.d.ts +3 -3
- package/dist/coValues/coMap.d.ts.map +1 -1
- package/dist/coValues/coMap.js +3 -3
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coPlainText.d.ts +2 -2
- package/dist/coValues/coPlainText.d.ts.map +1 -1
- package/dist/coValues/coPlainText.js +4 -4
- package/dist/coValues/coPlainText.js.map +1 -1
- package/dist/coValues/coStream.d.ts +3 -3
- package/dist/coValues/coStream.d.ts.map +1 -1
- package/dist/coValues/coStream.js +3 -3
- package/dist/coValues/coStream.js.map +1 -1
- package/dist/coValues/group.d.ts +7 -2
- package/dist/coValues/group.d.ts.map +1 -1
- package/dist/coValues/group.js +29 -26
- package/dist/coValues/group.js.map +1 -1
- package/dist/coreToCoValue.d.ts +2 -2
- package/dist/coreToCoValue.d.ts.map +1 -1
- package/dist/coreToCoValue.js +10 -14
- package/dist/coreToCoValue.js.map +1 -1
- package/dist/exports.d.ts +6 -5
- package/dist/exports.d.ts.map +1 -1
- package/dist/exports.js +3 -4
- package/dist/exports.js.map +1 -1
- package/dist/localNode.d.ts +30 -24
- package/dist/localNode.d.ts.map +1 -1
- package/dist/localNode.js +147 -173
- package/dist/localNode.js.map +1 -1
- package/dist/permissions.d.ts +2 -1
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +15 -11
- package/dist/permissions.js.map +1 -1
- package/dist/priority.d.ts +1 -1
- package/dist/priority.d.ts.map +1 -1
- package/dist/streamUtils.d.ts +5 -5
- package/dist/streamUtils.d.ts.map +1 -1
- package/dist/streamUtils.js +5 -20
- package/dist/streamUtils.js.map +1 -1
- package/dist/sync.d.ts +8 -6
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +121 -74
- package/dist/sync.js.map +1 -1
- package/dist/tests/PeerState.test.js +0 -31
- package/dist/tests/PeerState.test.js.map +1 -1
- package/dist/tests/SyncStateManager.test.js +41 -6
- package/dist/tests/SyncStateManager.test.js.map +1 -1
- package/dist/tests/account.test.js +16 -0
- package/dist/tests/account.test.js.map +1 -1
- package/dist/tests/coList.test.js +19 -16
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +12 -13
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coPlainText.test.js +9 -10
- package/dist/tests/coPlainText.test.js.map +1 -1
- package/dist/tests/coStream.test.js +22 -17
- package/dist/tests/coStream.test.js.map +1 -1
- package/dist/tests/coValueCore.test.js +22 -28
- package/dist/tests/coValueCore.test.js.map +1 -1
- package/dist/tests/coValueCoreLoadingState.test.d.ts +2 -0
- package/dist/tests/coValueCoreLoadingState.test.d.ts.map +1 -0
- package/dist/tests/{coValueState.test.js → coValueCoreLoadingState.test.js} +62 -46
- package/dist/tests/coValueCoreLoadingState.test.js.map +1 -0
- package/dist/tests/group.test.js +42 -43
- package/dist/tests/group.test.js.map +1 -1
- package/dist/tests/messagesTestUtils.d.ts +2 -2
- package/dist/tests/messagesTestUtils.d.ts.map +1 -1
- package/dist/tests/messagesTestUtils.js +1 -1
- package/dist/tests/messagesTestUtils.js.map +1 -1
- package/dist/tests/permissions.test.js +224 -292
- package/dist/tests/permissions.test.js.map +1 -1
- package/dist/tests/priority.test.js +13 -14
- package/dist/tests/priority.test.js.map +1 -1
- package/dist/tests/sync.auth.test.d.ts +2 -0
- package/dist/tests/sync.auth.test.d.ts.map +1 -0
- package/dist/tests/sync.auth.test.js +190 -0
- package/dist/tests/sync.auth.test.js.map +1 -0
- package/dist/tests/sync.load.test.js +6 -6
- package/dist/tests/sync.load.test.js.map +1 -1
- package/dist/tests/sync.mesh.test.js +25 -12
- package/dist/tests/sync.mesh.test.js.map +1 -1
- package/dist/tests/sync.peerReconciliation.test.js +19 -19
- package/dist/tests/sync.peerReconciliation.test.js.map +1 -1
- package/dist/tests/sync.storage.test.js +20 -13
- package/dist/tests/sync.storage.test.js.map +1 -1
- package/dist/tests/sync.test.js +32 -39
- package/dist/tests/sync.test.js.map +1 -1
- package/dist/tests/sync.upload.test.js +126 -37
- package/dist/tests/sync.upload.test.js.map +1 -1
- package/dist/tests/testUtils.d.ts +35 -17
- package/dist/tests/testUtils.d.ts.map +1 -1
- package/dist/tests/testUtils.js +103 -79
- package/dist/tests/testUtils.js.map +1 -1
- package/dist/typeUtils/expectGroup.js +1 -1
- package/dist/typeUtils/expectGroup.js.map +1 -1
- package/package.json +1 -1
- package/src/PeerState.ts +19 -40
- package/src/SyncStateManager.ts +2 -3
- package/src/coValue.ts +11 -8
- package/src/{coValueCore.ts → coValueCore/coValueCore.ts} +478 -422
- package/src/coValueCore/verifiedState.ts +376 -0
- package/src/coValues/account.ts +20 -25
- package/src/coValues/coList.ts +12 -6
- package/src/coValues/coMap.ts +9 -6
- package/src/coValues/coPlainText.ts +9 -6
- package/src/coValues/coStream.ts +9 -6
- package/src/coValues/group.ts +50 -28
- package/src/coreToCoValue.ts +14 -15
- package/src/exports.ts +9 -7
- package/src/localNode.ts +236 -275
- package/src/permissions.ts +18 -12
- package/src/priority.ts +1 -1
- package/src/streamUtils.ts +7 -34
- package/src/sync.ts +146 -84
- package/src/tests/PeerState.test.ts +0 -37
- package/src/tests/SyncStateManager.test.ts +56 -6
- package/src/tests/account.test.ts +24 -0
- package/src/tests/coList.test.ts +21 -15
- package/src/tests/coMap.test.ts +12 -13
- package/src/tests/coPlainText.test.ts +12 -9
- package/src/tests/coStream.test.ts +25 -16
- package/src/tests/coValueCore.test.ts +30 -27
- package/src/tests/{coValueState.test.ts → coValueCoreLoadingState.test.ts} +67 -57
- package/src/tests/group.test.ts +44 -69
- package/src/tests/messagesTestUtils.ts +3 -8
- package/src/tests/permissions.test.ts +283 -449
- package/src/tests/priority.test.ts +17 -13
- package/src/tests/sync.auth.test.ts +246 -0
- package/src/tests/sync.load.test.ts +7 -6
- package/src/tests/sync.mesh.test.ts +25 -12
- package/src/tests/sync.peerReconciliation.test.ts +25 -25
- package/src/tests/sync.storage.test.ts +20 -13
- package/src/tests/sync.test.ts +43 -43
- package/src/tests/sync.upload.test.ts +157 -37
- package/src/tests/testUtils.ts +143 -96
- package/src/typeUtils/expectGroup.ts +1 -1
- package/dist/CoValuesStore.d.ts +0 -14
- package/dist/CoValuesStore.d.ts.map +0 -1
- package/dist/CoValuesStore.js +0 -32
- package/dist/CoValuesStore.js.map +0 -1
- package/dist/coValueCore.d.ts +0 -142
- package/dist/coValueCore.d.ts.map +0 -1
- package/dist/coValueCore.js.map +0 -1
- package/dist/coValueState.d.ts +0 -34
- package/dist/coValueState.d.ts.map +0 -1
- package/dist/coValueState.js +0 -190
- package/dist/coValueState.js.map +0 -1
- package/dist/tests/coValueState.test.d.ts +0 -2
- package/dist/tests/coValueState.test.d.ts.map +0 -1
- package/dist/tests/coValueState.test.js.map +0 -1
- package/src/CoValuesStore.ts +0 -41
- package/src/coValueState.ts +0 -245
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { assert, afterEach, beforeEach, expect, test, vi } from "vitest";
|
|
2
|
-
import { CoValueCore
|
|
2
|
+
import { CoValueCore } from "../coValueCore/coValueCore.js";
|
|
3
|
+
import { Transaction } from "../coValueCore/verifiedState.js";
|
|
3
4
|
import { MapOpPayload } from "../coValues/coMap.js";
|
|
4
5
|
import { WasmCrypto } from "../crypto/WasmCrypto.js";
|
|
5
6
|
import { stableStringify } from "../jsonStringify.js";
|
|
@@ -10,7 +11,8 @@ import {
|
|
|
10
11
|
createTestNode,
|
|
11
12
|
createTwoConnectedNodes,
|
|
12
13
|
loadCoValueOrFail,
|
|
13
|
-
|
|
14
|
+
nodeWithRandomAgentAndSessionID,
|
|
15
|
+
randomAgentAndSessionID,
|
|
14
16
|
tearDownTestMetricReader,
|
|
15
17
|
} from "./testUtils.js";
|
|
16
18
|
|
|
@@ -27,8 +29,8 @@ afterEach(() => {
|
|
|
27
29
|
});
|
|
28
30
|
|
|
29
31
|
test("Can create coValue with new agent credentials and add transaction to it", () => {
|
|
30
|
-
const [
|
|
31
|
-
const node = new LocalNode(
|
|
32
|
+
const [agent, sessionID] = randomAgentAndSessionID();
|
|
33
|
+
const node = new LocalNode(agent.agentSecret, sessionID, Crypto);
|
|
32
34
|
|
|
33
35
|
const coValue = node.createCoValue({
|
|
34
36
|
type: "costream",
|
|
@@ -47,7 +49,7 @@ test("Can create coValue with new agent credentials and add transaction to it",
|
|
|
47
49
|
]),
|
|
48
50
|
};
|
|
49
51
|
|
|
50
|
-
const { expectedNewHash } = coValue.expectedNewHashAfter(
|
|
52
|
+
const { expectedNewHash } = coValue.verified.expectedNewHashAfter(
|
|
51
53
|
node.currentSessionID,
|
|
52
54
|
[transaction],
|
|
53
55
|
);
|
|
@@ -58,7 +60,8 @@ test("Can create coValue with new agent credentials and add transaction to it",
|
|
|
58
60
|
node.currentSessionID,
|
|
59
61
|
[transaction],
|
|
60
62
|
expectedNewHash,
|
|
61
|
-
Crypto.sign(
|
|
63
|
+
Crypto.sign(agent.currentSignerSecret(), expectedNewHash),
|
|
64
|
+
"immediate",
|
|
62
65
|
)
|
|
63
66
|
._unsafeUnwrap(),
|
|
64
67
|
).toBe(true);
|
|
@@ -66,8 +69,7 @@ test("Can create coValue with new agent credentials and add transaction to it",
|
|
|
66
69
|
|
|
67
70
|
test("transactions with wrong signature are rejected", () => {
|
|
68
71
|
const wrongAgent = Crypto.newRandomAgentSecret();
|
|
69
|
-
const
|
|
70
|
-
const node = new LocalNode(agentSecret, sessionID, Crypto);
|
|
72
|
+
const node = nodeWithRandomAgentAndSessionID();
|
|
71
73
|
|
|
72
74
|
const coValue = node.createCoValue({
|
|
73
75
|
type: "costream",
|
|
@@ -86,7 +88,7 @@ test("transactions with wrong signature are rejected", () => {
|
|
|
86
88
|
]),
|
|
87
89
|
};
|
|
88
90
|
|
|
89
|
-
const { expectedNewHash } = coValue.expectedNewHashAfter(
|
|
91
|
+
const { expectedNewHash } = coValue.verified.expectedNewHashAfter(
|
|
90
92
|
node.currentSessionID,
|
|
91
93
|
[transaction],
|
|
92
94
|
);
|
|
@@ -98,13 +100,14 @@ test("transactions with wrong signature are rejected", () => {
|
|
|
98
100
|
[transaction],
|
|
99
101
|
expectedNewHash,
|
|
100
102
|
Crypto.sign(Crypto.getAgentSignerSecret(wrongAgent), expectedNewHash),
|
|
103
|
+
"immediate",
|
|
101
104
|
)
|
|
102
105
|
._unsafeUnwrapErr({ withStackTrace: true });
|
|
103
106
|
});
|
|
104
107
|
|
|
105
108
|
test("transactions with correctly signed, but wrong hash are rejected", () => {
|
|
106
|
-
const [
|
|
107
|
-
const node = new LocalNode(
|
|
109
|
+
const [agent, sessionID] = randomAgentAndSessionID();
|
|
110
|
+
const node = new LocalNode(agent.agentSecret, sessionID, Crypto);
|
|
108
111
|
|
|
109
112
|
const coValue = node.createCoValue({
|
|
110
113
|
type: "costream",
|
|
@@ -123,7 +126,7 @@ test("transactions with correctly signed, but wrong hash are rejected", () => {
|
|
|
123
126
|
]),
|
|
124
127
|
};
|
|
125
128
|
|
|
126
|
-
const { expectedNewHash } = coValue.expectedNewHashAfter(
|
|
129
|
+
const { expectedNewHash } = coValue.verified.expectedNewHashAfter(
|
|
127
130
|
node.currentSessionID,
|
|
128
131
|
[
|
|
129
132
|
{
|
|
@@ -144,14 +147,15 @@ test("transactions with correctly signed, but wrong hash are rejected", () => {
|
|
|
144
147
|
node.currentSessionID,
|
|
145
148
|
[transaction],
|
|
146
149
|
expectedNewHash,
|
|
147
|
-
Crypto.sign(
|
|
150
|
+
Crypto.sign(agent.currentSignerSecret(), expectedNewHash),
|
|
151
|
+
"immediate",
|
|
148
152
|
)
|
|
149
153
|
._unsafeUnwrapErr({ withStackTrace: true });
|
|
150
154
|
});
|
|
151
155
|
|
|
152
156
|
test("New transactions in a group correctly update owned values, including subscriptions", async () => {
|
|
153
|
-
const [
|
|
154
|
-
const node = new LocalNode(
|
|
157
|
+
const [agent, sessionID] = randomAgentAndSessionID();
|
|
158
|
+
const node = new LocalNode(agent.agentSecret, sessionID, Crypto);
|
|
155
159
|
|
|
156
160
|
const group = node.createGroup();
|
|
157
161
|
|
|
@@ -175,18 +179,19 @@ test("New transactions in a group correctly update owned values, including subsc
|
|
|
175
179
|
changes: stableStringify([
|
|
176
180
|
{
|
|
177
181
|
op: "set",
|
|
178
|
-
key:
|
|
182
|
+
key: agent.id,
|
|
179
183
|
value: "revoked",
|
|
180
|
-
} satisfies MapOpPayload<typeof
|
|
184
|
+
} satisfies MapOpPayload<typeof agent.id, Role>,
|
|
181
185
|
]),
|
|
182
186
|
} satisfies Transaction;
|
|
183
187
|
|
|
184
|
-
const { expectedNewHash } = group.core.expectedNewHashAfter(
|
|
185
|
-
|
|
186
|
-
|
|
188
|
+
const { expectedNewHash } = group.core.verified.expectedNewHashAfter(
|
|
189
|
+
sessionID,
|
|
190
|
+
[resignationThatWeJustLearnedAbout],
|
|
191
|
+
);
|
|
187
192
|
|
|
188
193
|
const signature = Crypto.sign(
|
|
189
|
-
node.
|
|
194
|
+
node.getCurrentAgent().currentSignerSecret(),
|
|
190
195
|
expectedNewHash,
|
|
191
196
|
);
|
|
192
197
|
|
|
@@ -198,6 +203,7 @@ test("New transactions in a group correctly update owned values, including subsc
|
|
|
198
203
|
[resignationThatWeJustLearnedAbout],
|
|
199
204
|
expectedNewHash,
|
|
200
205
|
signature,
|
|
206
|
+
"immediate",
|
|
201
207
|
)
|
|
202
208
|
._unsafeUnwrap({ withStackTrace: true });
|
|
203
209
|
|
|
@@ -210,8 +216,7 @@ test("New transactions in a group correctly update owned values, including subsc
|
|
|
210
216
|
});
|
|
211
217
|
|
|
212
218
|
test("correctly records transactions", async () => {
|
|
213
|
-
const
|
|
214
|
-
const node = new LocalNode(account, sessionID, Crypto);
|
|
219
|
+
const node = nodeWithRandomAgentAndSessionID();
|
|
215
220
|
|
|
216
221
|
const changes1 = stableStringify([{ hello: "world" }]);
|
|
217
222
|
node.syncManager.recordTransactionsSize(
|
|
@@ -253,8 +258,7 @@ test("correctly records transactions", async () => {
|
|
|
253
258
|
});
|
|
254
259
|
|
|
255
260
|
test("(smoke test) records transactions from local node", async () => {
|
|
256
|
-
const
|
|
257
|
-
const node = new LocalNode(account, sessionID, Crypto);
|
|
261
|
+
const node = nodeWithRandomAgentAndSessionID();
|
|
258
262
|
|
|
259
263
|
node.createGroup();
|
|
260
264
|
|
|
@@ -294,8 +298,7 @@ test("creating a coValue with a group should't trigger automatically a content c
|
|
|
294
298
|
});
|
|
295
299
|
|
|
296
300
|
test("loading a coValue core without having the owner group available doesn't crash", () => {
|
|
297
|
-
const
|
|
298
|
-
const node = new LocalNode(account, sessionID, Crypto);
|
|
301
|
+
const node = nodeWithRandomAgentAndSessionID();
|
|
299
302
|
|
|
300
303
|
const otherNode = createTestNode();
|
|
301
304
|
|
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
assert,
|
|
3
|
-
afterEach,
|
|
4
|
-
beforeEach,
|
|
5
|
-
describe,
|
|
6
|
-
expect,
|
|
7
|
-
onTestFinished,
|
|
8
|
-
test,
|
|
9
|
-
vi,
|
|
10
|
-
} from "vitest";
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
11
2
|
import { PeerState } from "../PeerState";
|
|
12
|
-
import { CoValueCore } from "../coValueCore";
|
|
13
|
-
import {
|
|
3
|
+
import { CoValueCore } from "../coValueCore/coValueCore";
|
|
4
|
+
import { CoValueHeader, VerifiedState } from "../coValueCore/verifiedState";
|
|
14
5
|
import { RawCoID } from "../ids";
|
|
6
|
+
import { LocalNode } from "../localNode";
|
|
15
7
|
import { Peer } from "../sync";
|
|
16
8
|
import { createTestMetricReader, tearDownTestMetricReader } from "./testUtils";
|
|
17
9
|
|
|
@@ -25,14 +17,16 @@ afterEach(() => {
|
|
|
25
17
|
tearDownTestMetricReader();
|
|
26
18
|
});
|
|
27
19
|
|
|
28
|
-
|
|
20
|
+
const mockNode = {} as LocalNode;
|
|
21
|
+
|
|
22
|
+
describe("CoValueCore loading state", () => {
|
|
29
23
|
const mockCoValueId = "co_test123" as RawCoID;
|
|
30
24
|
|
|
31
25
|
test("should create unknown state", async () => {
|
|
32
|
-
const state =
|
|
26
|
+
const state = CoValueCore.fromID(mockCoValueId, mockNode);
|
|
33
27
|
|
|
34
28
|
expect(state.id).toBe(mockCoValueId);
|
|
35
|
-
expect(state.
|
|
29
|
+
expect(state.loadingState).toBe("unknown");
|
|
36
30
|
expect(
|
|
37
31
|
await metricReader.getMetricValue("jazz.covalues.loaded", {
|
|
38
32
|
state: "unknown",
|
|
@@ -41,14 +35,14 @@ describe("CoValueState", () => {
|
|
|
41
35
|
});
|
|
42
36
|
|
|
43
37
|
test("should create loading state", async () => {
|
|
44
|
-
const state =
|
|
38
|
+
const state = CoValueCore.fromID(mockCoValueId, mockNode);
|
|
45
39
|
state.loadFromPeers([
|
|
46
40
|
createMockPeerState({ id: "peer1", role: "server" }),
|
|
47
41
|
createMockPeerState({ id: "peer2", role: "server" }),
|
|
48
42
|
]);
|
|
49
43
|
|
|
50
44
|
expect(state.id).toBe(mockCoValueId);
|
|
51
|
-
expect(state.
|
|
45
|
+
expect(state.loadingState).toBe("loading");
|
|
52
46
|
expect(
|
|
53
47
|
await metricReader.getMetricValue("jazz.covalues.loaded", {
|
|
54
48
|
state: "loading",
|
|
@@ -57,14 +51,16 @@ describe("CoValueState", () => {
|
|
|
57
51
|
});
|
|
58
52
|
|
|
59
53
|
test("should create available state", async () => {
|
|
60
|
-
const
|
|
61
|
-
const state =
|
|
62
|
-
state.internalMarkMagicallyAvailable(
|
|
54
|
+
const mockVerified = createMockCoValueVerified(mockCoValueId);
|
|
55
|
+
const state = CoValueCore.fromID(mockCoValueId, mockNode);
|
|
56
|
+
state.internalMarkMagicallyAvailable(mockVerified);
|
|
63
57
|
|
|
64
58
|
expect(state.id).toBe(mockCoValueId);
|
|
65
|
-
expect(state.
|
|
66
|
-
expect(state.
|
|
67
|
-
await expect(state.
|
|
59
|
+
expect(state.loadingState).toBe("available");
|
|
60
|
+
expect(state.verified).toBe(mockVerified);
|
|
61
|
+
await expect(state.waitForAvailableOrUnavailable()).resolves.toMatchObject({
|
|
62
|
+
verified: mockVerified,
|
|
63
|
+
});
|
|
68
64
|
expect(
|
|
69
65
|
await metricReader.getMetricValue("jazz.covalues.loaded", {
|
|
70
66
|
state: "available",
|
|
@@ -73,8 +69,8 @@ describe("CoValueState", () => {
|
|
|
73
69
|
});
|
|
74
70
|
|
|
75
71
|
test("should handle found action", async () => {
|
|
76
|
-
const
|
|
77
|
-
const state =
|
|
72
|
+
const mockVerified = createMockCoValueVerified(mockCoValueId);
|
|
73
|
+
const state = CoValueCore.fromID(mockCoValueId, mockNode);
|
|
78
74
|
state.loadFromPeers([
|
|
79
75
|
createMockPeerState({ id: "peer1", role: "server" }),
|
|
80
76
|
createMockPeerState({ id: "peer2", role: "server" }),
|
|
@@ -91,13 +87,15 @@ describe("CoValueState", () => {
|
|
|
91
87
|
}),
|
|
92
88
|
).toBe(1);
|
|
93
89
|
|
|
94
|
-
const stateValuePromise = state.
|
|
90
|
+
const stateValuePromise = state.waitForAvailableOrUnavailable();
|
|
95
91
|
|
|
96
|
-
state.internalMarkMagicallyAvailable(
|
|
92
|
+
state.internalMarkMagicallyAvailable(mockVerified);
|
|
97
93
|
|
|
98
|
-
const result = await state.
|
|
99
|
-
expect(result).
|
|
100
|
-
await expect(stateValuePromise).resolves.
|
|
94
|
+
const result = await state.waitForAvailableOrUnavailable();
|
|
95
|
+
expect(result).toMatchObject({ verified: mockVerified });
|
|
96
|
+
await expect(stateValuePromise).resolves.toMatchObject({
|
|
97
|
+
verified: mockVerified,
|
|
98
|
+
});
|
|
101
99
|
|
|
102
100
|
expect(
|
|
103
101
|
await metricReader.getMetricValue("jazz.covalues.loaded", {
|
|
@@ -135,17 +133,18 @@ describe("CoValueState", () => {
|
|
|
135
133
|
|
|
136
134
|
const mockPeers = [peer1, peer2] as unknown as PeerState[];
|
|
137
135
|
|
|
138
|
-
const state =
|
|
136
|
+
const state = CoValueCore.fromID(mockCoValueId, mockNode);
|
|
139
137
|
const loadPromise = state.loadFromPeers(mockPeers);
|
|
140
138
|
|
|
141
139
|
await vi.runAllTimersAsync();
|
|
142
|
-
|
|
143
140
|
await loadPromise;
|
|
144
141
|
|
|
145
142
|
expect(peer1.pushOutgoingMessage).toHaveBeenCalledTimes(1);
|
|
146
143
|
expect(peer2.pushOutgoingMessage).toHaveBeenCalledTimes(1);
|
|
147
|
-
expect(state.
|
|
148
|
-
await expect(state.
|
|
144
|
+
expect(state.loadingState).toBe("unavailable");
|
|
145
|
+
await expect(state.waitForAvailableOrUnavailable()).resolves.toMatchObject({
|
|
146
|
+
verified: null,
|
|
147
|
+
});
|
|
149
148
|
|
|
150
149
|
vi.useRealTimers();
|
|
151
150
|
});
|
|
@@ -165,18 +164,22 @@ describe("CoValueState", () => {
|
|
|
165
164
|
|
|
166
165
|
const mockPeers = [peer1] as unknown as PeerState[];
|
|
167
166
|
|
|
168
|
-
const state =
|
|
167
|
+
const state = CoValueCore.fromID(mockCoValueId, mockNode);
|
|
169
168
|
const loadPromise = state.loadFromPeers(mockPeers);
|
|
170
169
|
|
|
171
170
|
await vi.runAllTimersAsync();
|
|
172
171
|
|
|
173
|
-
state.internalMarkMagicallyAvailable(
|
|
172
|
+
state.internalMarkMagicallyAvailable(
|
|
173
|
+
createMockCoValueVerified(mockCoValueId),
|
|
174
|
+
);
|
|
174
175
|
|
|
175
176
|
await loadPromise;
|
|
176
177
|
|
|
177
178
|
expect(peer1.pushOutgoingMessage).toHaveBeenCalledTimes(1);
|
|
178
|
-
expect(state.
|
|
179
|
-
await expect(state.
|
|
179
|
+
expect(state.loadingState).toBe("available");
|
|
180
|
+
await expect(state.waitForAvailableOrUnavailable()).resolves.toMatchObject({
|
|
181
|
+
_verified: expect.any(Object),
|
|
182
|
+
});
|
|
180
183
|
|
|
181
184
|
vi.useRealTimers();
|
|
182
185
|
});
|
|
@@ -184,7 +187,7 @@ describe("CoValueState", () => {
|
|
|
184
187
|
test("should start sending the known state to peers when available", async () => {
|
|
185
188
|
vi.useFakeTimers();
|
|
186
189
|
|
|
187
|
-
const
|
|
190
|
+
const mockVerified = createMockCoValueVerified(mockCoValueId);
|
|
188
191
|
|
|
189
192
|
const peer1 = createMockPeerState(
|
|
190
193
|
{
|
|
@@ -192,7 +195,7 @@ describe("CoValueState", () => {
|
|
|
192
195
|
role: "storage",
|
|
193
196
|
},
|
|
194
197
|
async () => {
|
|
195
|
-
state.markAvailable(
|
|
198
|
+
state.markAvailable({} as CoValueHeader, "peer1");
|
|
196
199
|
},
|
|
197
200
|
);
|
|
198
201
|
const peer2 = createMockPeerState(
|
|
@@ -205,7 +208,7 @@ describe("CoValueState", () => {
|
|
|
205
208
|
},
|
|
206
209
|
);
|
|
207
210
|
|
|
208
|
-
const state =
|
|
211
|
+
const state = CoValueCore.fromID(mockCoValueId, mockNode);
|
|
209
212
|
const loadPromise = state.loadFromPeers([peer1, peer2]);
|
|
210
213
|
|
|
211
214
|
await vi.runAllTimersAsync();
|
|
@@ -215,10 +218,12 @@ describe("CoValueState", () => {
|
|
|
215
218
|
expect(peer2.pushOutgoingMessage).toHaveBeenCalledTimes(1);
|
|
216
219
|
expect(peer2.pushOutgoingMessage).toHaveBeenCalledWith({
|
|
217
220
|
action: "load",
|
|
218
|
-
...
|
|
221
|
+
...mockVerified.knownState(),
|
|
222
|
+
});
|
|
223
|
+
expect(state.loadingState).toBe("available");
|
|
224
|
+
await expect(state.waitForAvailableOrUnavailable()).resolves.toMatchObject({
|
|
225
|
+
verified: mockVerified,
|
|
219
226
|
});
|
|
220
|
-
expect(state.highLevelState).toBe("available");
|
|
221
|
-
await expect(state.getCoValue()).resolves.toEqual({ id: mockCoValueId });
|
|
222
227
|
|
|
223
228
|
vi.useRealTimers();
|
|
224
229
|
});
|
|
@@ -226,7 +231,7 @@ describe("CoValueState", () => {
|
|
|
226
231
|
test("should skip closed peers", async () => {
|
|
227
232
|
vi.useFakeTimers();
|
|
228
233
|
|
|
229
|
-
const
|
|
234
|
+
const mockVerified = createMockCoValueVerified(mockCoValueId);
|
|
230
235
|
|
|
231
236
|
const peer1 = createMockPeerState(
|
|
232
237
|
{
|
|
@@ -243,13 +248,13 @@ describe("CoValueState", () => {
|
|
|
243
248
|
role: "server",
|
|
244
249
|
},
|
|
245
250
|
async () => {
|
|
246
|
-
state.markAvailable(
|
|
251
|
+
state.markAvailable({} as CoValueHeader, "peer2");
|
|
247
252
|
},
|
|
248
253
|
);
|
|
249
254
|
|
|
250
255
|
peer1.closed = true;
|
|
251
256
|
|
|
252
|
-
const state =
|
|
257
|
+
const state = CoValueCore.fromID(mockCoValueId, mockNode);
|
|
253
258
|
const loadPromise = state.loadFromPeers([peer1, peer2]);
|
|
254
259
|
|
|
255
260
|
await vi.runAllTimersAsync();
|
|
@@ -258,8 +263,10 @@ describe("CoValueState", () => {
|
|
|
258
263
|
expect(peer1.pushOutgoingMessage).toHaveBeenCalledTimes(0);
|
|
259
264
|
expect(peer2.pushOutgoingMessage).toHaveBeenCalledTimes(1);
|
|
260
265
|
|
|
261
|
-
expect(state.
|
|
262
|
-
await expect(state.
|
|
266
|
+
expect(state.loadingState).toBe("available");
|
|
267
|
+
await expect(state.waitForAvailableOrUnavailable()).resolves.toMatchObject({
|
|
268
|
+
verified: mockVerified,
|
|
269
|
+
});
|
|
263
270
|
|
|
264
271
|
vi.useRealTimers();
|
|
265
272
|
});
|
|
@@ -275,7 +282,7 @@ describe("CoValueState", () => {
|
|
|
275
282
|
async () => {},
|
|
276
283
|
);
|
|
277
284
|
|
|
278
|
-
const state =
|
|
285
|
+
const state = CoValueCore.fromID(mockCoValueId, mockNode);
|
|
279
286
|
const loadPromise = state.loadFromPeers([peer1]);
|
|
280
287
|
|
|
281
288
|
await vi.runAllTimersAsync();
|
|
@@ -283,8 +290,10 @@ describe("CoValueState", () => {
|
|
|
283
290
|
|
|
284
291
|
expect(peer1.pushOutgoingMessage).toHaveBeenCalledTimes(1);
|
|
285
292
|
|
|
286
|
-
expect(state.
|
|
287
|
-
await expect(state.
|
|
293
|
+
expect(state.loadingState).toBe("unavailable");
|
|
294
|
+
await expect(state.waitForAvailableOrUnavailable()).resolves.toMatchObject({
|
|
295
|
+
verified: null,
|
|
296
|
+
});
|
|
288
297
|
|
|
289
298
|
vi.useRealTimers();
|
|
290
299
|
});
|
|
@@ -311,17 +320,18 @@ function createMockPeerState(
|
|
|
311
320
|
return peerState;
|
|
312
321
|
}
|
|
313
322
|
|
|
314
|
-
function
|
|
323
|
+
function createMockCoValueVerified(mockCoValueId: string) {
|
|
315
324
|
// Setting the knownState as part of the prototype to simplify
|
|
316
325
|
// the equality checks
|
|
317
|
-
const
|
|
326
|
+
const mockCoValueVerified = Object.create({
|
|
327
|
+
id: mockCoValueId,
|
|
318
328
|
knownState: vi.fn().mockReturnValue({
|
|
319
329
|
id: mockCoValueId,
|
|
320
330
|
header: true,
|
|
321
331
|
sessions: {},
|
|
322
332
|
}),
|
|
323
|
-
|
|
333
|
+
clone: vi.fn().mockReturnThis(),
|
|
334
|
+
}) as unknown as VerifiedState;
|
|
324
335
|
|
|
325
|
-
|
|
326
|
-
return mockCoValue as unknown as CoValueCore;
|
|
336
|
+
return mockCoValueVerified as unknown as VerifiedState;
|
|
327
337
|
}
|