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.
Files changed (180) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +18 -0
  3. package/dist/PeerState.d.ts +4 -1
  4. package/dist/PeerState.d.ts.map +1 -1
  5. package/dist/PeerState.js +16 -36
  6. package/dist/PeerState.js.map +1 -1
  7. package/dist/SyncStateManager.d.ts.map +1 -1
  8. package/dist/SyncStateManager.js +2 -3
  9. package/dist/SyncStateManager.js.map +1 -1
  10. package/dist/coValue.d.ts +4 -4
  11. package/dist/coValue.d.ts.map +1 -1
  12. package/dist/coValue.js +4 -4
  13. package/dist/coValue.js.map +1 -1
  14. package/dist/coValueCore/coValueCore.d.ts +143 -0
  15. package/dist/coValueCore/coValueCore.d.ts.map +1 -0
  16. package/dist/{coValueCore.js → coValueCore/coValueCore.js} +325 -253
  17. package/dist/coValueCore/coValueCore.js.map +1 -0
  18. package/dist/coValueCore/verifiedState.d.ts +65 -0
  19. package/dist/coValueCore/verifiedState.d.ts.map +1 -0
  20. package/dist/coValueCore/verifiedState.js +210 -0
  21. package/dist/coValueCore/verifiedState.js.map +1 -0
  22. package/dist/coValues/account.d.ts +8 -10
  23. package/dist/coValues/account.d.ts.map +1 -1
  24. package/dist/coValues/account.js +12 -13
  25. package/dist/coValues/account.js.map +1 -1
  26. package/dist/coValues/coList.d.ts +3 -3
  27. package/dist/coValues/coList.d.ts.map +1 -1
  28. package/dist/coValues/coList.js +6 -3
  29. package/dist/coValues/coList.js.map +1 -1
  30. package/dist/coValues/coMap.d.ts +3 -3
  31. package/dist/coValues/coMap.d.ts.map +1 -1
  32. package/dist/coValues/coMap.js +3 -3
  33. package/dist/coValues/coMap.js.map +1 -1
  34. package/dist/coValues/coPlainText.d.ts +2 -2
  35. package/dist/coValues/coPlainText.d.ts.map +1 -1
  36. package/dist/coValues/coPlainText.js +4 -4
  37. package/dist/coValues/coPlainText.js.map +1 -1
  38. package/dist/coValues/coStream.d.ts +3 -3
  39. package/dist/coValues/coStream.d.ts.map +1 -1
  40. package/dist/coValues/coStream.js +3 -3
  41. package/dist/coValues/coStream.js.map +1 -1
  42. package/dist/coValues/group.d.ts +7 -2
  43. package/dist/coValues/group.d.ts.map +1 -1
  44. package/dist/coValues/group.js +29 -26
  45. package/dist/coValues/group.js.map +1 -1
  46. package/dist/coreToCoValue.d.ts +2 -2
  47. package/dist/coreToCoValue.d.ts.map +1 -1
  48. package/dist/coreToCoValue.js +10 -14
  49. package/dist/coreToCoValue.js.map +1 -1
  50. package/dist/exports.d.ts +6 -5
  51. package/dist/exports.d.ts.map +1 -1
  52. package/dist/exports.js +3 -4
  53. package/dist/exports.js.map +1 -1
  54. package/dist/localNode.d.ts +30 -24
  55. package/dist/localNode.d.ts.map +1 -1
  56. package/dist/localNode.js +147 -173
  57. package/dist/localNode.js.map +1 -1
  58. package/dist/permissions.d.ts +2 -1
  59. package/dist/permissions.d.ts.map +1 -1
  60. package/dist/permissions.js +15 -11
  61. package/dist/permissions.js.map +1 -1
  62. package/dist/priority.d.ts +1 -1
  63. package/dist/priority.d.ts.map +1 -1
  64. package/dist/streamUtils.d.ts +5 -5
  65. package/dist/streamUtils.d.ts.map +1 -1
  66. package/dist/streamUtils.js +5 -20
  67. package/dist/streamUtils.js.map +1 -1
  68. package/dist/sync.d.ts +8 -6
  69. package/dist/sync.d.ts.map +1 -1
  70. package/dist/sync.js +121 -74
  71. package/dist/sync.js.map +1 -1
  72. package/dist/tests/PeerState.test.js +0 -31
  73. package/dist/tests/PeerState.test.js.map +1 -1
  74. package/dist/tests/SyncStateManager.test.js +41 -6
  75. package/dist/tests/SyncStateManager.test.js.map +1 -1
  76. package/dist/tests/account.test.js +16 -0
  77. package/dist/tests/account.test.js.map +1 -1
  78. package/dist/tests/coList.test.js +19 -16
  79. package/dist/tests/coList.test.js.map +1 -1
  80. package/dist/tests/coMap.test.js +12 -13
  81. package/dist/tests/coMap.test.js.map +1 -1
  82. package/dist/tests/coPlainText.test.js +9 -10
  83. package/dist/tests/coPlainText.test.js.map +1 -1
  84. package/dist/tests/coStream.test.js +22 -17
  85. package/dist/tests/coStream.test.js.map +1 -1
  86. package/dist/tests/coValueCore.test.js +22 -28
  87. package/dist/tests/coValueCore.test.js.map +1 -1
  88. package/dist/tests/coValueCoreLoadingState.test.d.ts +2 -0
  89. package/dist/tests/coValueCoreLoadingState.test.d.ts.map +1 -0
  90. package/dist/tests/{coValueState.test.js → coValueCoreLoadingState.test.js} +62 -46
  91. package/dist/tests/coValueCoreLoadingState.test.js.map +1 -0
  92. package/dist/tests/group.test.js +42 -43
  93. package/dist/tests/group.test.js.map +1 -1
  94. package/dist/tests/messagesTestUtils.d.ts +2 -2
  95. package/dist/tests/messagesTestUtils.d.ts.map +1 -1
  96. package/dist/tests/messagesTestUtils.js +1 -1
  97. package/dist/tests/messagesTestUtils.js.map +1 -1
  98. package/dist/tests/permissions.test.js +224 -292
  99. package/dist/tests/permissions.test.js.map +1 -1
  100. package/dist/tests/priority.test.js +13 -14
  101. package/dist/tests/priority.test.js.map +1 -1
  102. package/dist/tests/sync.auth.test.d.ts +2 -0
  103. package/dist/tests/sync.auth.test.d.ts.map +1 -0
  104. package/dist/tests/sync.auth.test.js +190 -0
  105. package/dist/tests/sync.auth.test.js.map +1 -0
  106. package/dist/tests/sync.load.test.js +6 -6
  107. package/dist/tests/sync.load.test.js.map +1 -1
  108. package/dist/tests/sync.mesh.test.js +25 -12
  109. package/dist/tests/sync.mesh.test.js.map +1 -1
  110. package/dist/tests/sync.peerReconciliation.test.js +19 -19
  111. package/dist/tests/sync.peerReconciliation.test.js.map +1 -1
  112. package/dist/tests/sync.storage.test.js +20 -13
  113. package/dist/tests/sync.storage.test.js.map +1 -1
  114. package/dist/tests/sync.test.js +32 -39
  115. package/dist/tests/sync.test.js.map +1 -1
  116. package/dist/tests/sync.upload.test.js +126 -37
  117. package/dist/tests/sync.upload.test.js.map +1 -1
  118. package/dist/tests/testUtils.d.ts +35 -17
  119. package/dist/tests/testUtils.d.ts.map +1 -1
  120. package/dist/tests/testUtils.js +103 -79
  121. package/dist/tests/testUtils.js.map +1 -1
  122. package/dist/typeUtils/expectGroup.js +1 -1
  123. package/dist/typeUtils/expectGroup.js.map +1 -1
  124. package/package.json +1 -1
  125. package/src/PeerState.ts +19 -40
  126. package/src/SyncStateManager.ts +2 -3
  127. package/src/coValue.ts +11 -8
  128. package/src/{coValueCore.ts → coValueCore/coValueCore.ts} +478 -422
  129. package/src/coValueCore/verifiedState.ts +376 -0
  130. package/src/coValues/account.ts +20 -25
  131. package/src/coValues/coList.ts +12 -6
  132. package/src/coValues/coMap.ts +9 -6
  133. package/src/coValues/coPlainText.ts +9 -6
  134. package/src/coValues/coStream.ts +9 -6
  135. package/src/coValues/group.ts +50 -28
  136. package/src/coreToCoValue.ts +14 -15
  137. package/src/exports.ts +9 -7
  138. package/src/localNode.ts +236 -275
  139. package/src/permissions.ts +18 -12
  140. package/src/priority.ts +1 -1
  141. package/src/streamUtils.ts +7 -34
  142. package/src/sync.ts +146 -84
  143. package/src/tests/PeerState.test.ts +0 -37
  144. package/src/tests/SyncStateManager.test.ts +56 -6
  145. package/src/tests/account.test.ts +24 -0
  146. package/src/tests/coList.test.ts +21 -15
  147. package/src/tests/coMap.test.ts +12 -13
  148. package/src/tests/coPlainText.test.ts +12 -9
  149. package/src/tests/coStream.test.ts +25 -16
  150. package/src/tests/coValueCore.test.ts +30 -27
  151. package/src/tests/{coValueState.test.ts → coValueCoreLoadingState.test.ts} +67 -57
  152. package/src/tests/group.test.ts +44 -69
  153. package/src/tests/messagesTestUtils.ts +3 -8
  154. package/src/tests/permissions.test.ts +283 -449
  155. package/src/tests/priority.test.ts +17 -13
  156. package/src/tests/sync.auth.test.ts +246 -0
  157. package/src/tests/sync.load.test.ts +7 -6
  158. package/src/tests/sync.mesh.test.ts +25 -12
  159. package/src/tests/sync.peerReconciliation.test.ts +25 -25
  160. package/src/tests/sync.storage.test.ts +20 -13
  161. package/src/tests/sync.test.ts +43 -43
  162. package/src/tests/sync.upload.test.ts +157 -37
  163. package/src/tests/testUtils.ts +143 -96
  164. package/src/typeUtils/expectGroup.ts +1 -1
  165. package/dist/CoValuesStore.d.ts +0 -14
  166. package/dist/CoValuesStore.d.ts.map +0 -1
  167. package/dist/CoValuesStore.js +0 -32
  168. package/dist/CoValuesStore.js.map +0 -1
  169. package/dist/coValueCore.d.ts +0 -142
  170. package/dist/coValueCore.d.ts.map +0 -1
  171. package/dist/coValueCore.js.map +0 -1
  172. package/dist/coValueState.d.ts +0 -34
  173. package/dist/coValueState.d.ts.map +0 -1
  174. package/dist/coValueState.js +0 -190
  175. package/dist/coValueState.js.map +0 -1
  176. package/dist/tests/coValueState.test.d.ts +0 -2
  177. package/dist/tests/coValueState.test.d.ts.map +0 -1
  178. package/dist/tests/coValueState.test.js.map +0 -1
  179. package/src/CoValuesStore.ts +0 -41
  180. package/src/coValueState.ts +0 -245
@@ -1,5 +1,6 @@
1
1
  import { assert, afterEach, beforeEach, expect, test, vi } from "vitest";
2
- import { CoValueCore, Transaction } from "../coValueCore.js";
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
- randomAnonymousAccountAndSessionID,
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 [account, sessionID] = randomAnonymousAccountAndSessionID();
31
- const node = new LocalNode(account, sessionID, Crypto);
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(account.currentSignerSecret(), expectedNewHash),
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 [agentSecret, sessionID] = randomAnonymousAccountAndSessionID();
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 [account, sessionID] = randomAnonymousAccountAndSessionID();
107
- const node = new LocalNode(account, sessionID, Crypto);
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(account.currentSignerSecret(), expectedNewHash),
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 [account, sessionID] = randomAnonymousAccountAndSessionID();
154
- const node = new LocalNode(account, sessionID, Crypto);
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: account.id,
182
+ key: agent.id,
179
183
  value: "revoked",
180
- } satisfies MapOpPayload<typeof account.id, Role>,
184
+ } satisfies MapOpPayload<typeof agent.id, Role>,
181
185
  ]),
182
186
  } satisfies Transaction;
183
187
 
184
- const { expectedNewHash } = group.core.expectedNewHashAfter(sessionID, [
185
- resignationThatWeJustLearnedAbout,
186
- ]);
188
+ const { expectedNewHash } = group.core.verified.expectedNewHashAfter(
189
+ sessionID,
190
+ [resignationThatWeJustLearnedAbout],
191
+ );
187
192
 
188
193
  const signature = Crypto.sign(
189
- node.account.currentSignerSecret(),
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 [account, sessionID] = randomAnonymousAccountAndSessionID();
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 [account, sessionID] = randomAnonymousAccountAndSessionID();
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 [account, sessionID] = randomAnonymousAccountAndSessionID();
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 { CoValueState } from "../coValueState";
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
- describe("CoValueState", () => {
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 = new CoValueState(mockCoValueId);
26
+ const state = CoValueCore.fromID(mockCoValueId, mockNode);
33
27
 
34
28
  expect(state.id).toBe(mockCoValueId);
35
- expect(state.highLevelState).toBe("unknown");
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 = new CoValueState(mockCoValueId);
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.highLevelState).toBe("loading");
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 mockCoValue = createMockCoValueCore(mockCoValueId);
61
- const state = new CoValueState(mockCoValueId);
62
- state.internalMarkMagicallyAvailable(mockCoValue);
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.highLevelState).toBe("available");
66
- expect(state.core).toBe(mockCoValue);
67
- await expect(state.getCoValue()).resolves.toEqual(mockCoValue);
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 mockCoValue = createMockCoValueCore(mockCoValueId);
77
- const state = new CoValueState(mockCoValueId);
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.getCoValue();
90
+ const stateValuePromise = state.waitForAvailableOrUnavailable();
95
91
 
96
- state.internalMarkMagicallyAvailable(mockCoValue);
92
+ state.internalMarkMagicallyAvailable(mockVerified);
97
93
 
98
- const result = await state.getCoValue();
99
- expect(result).toBe(mockCoValue);
100
- await expect(stateValuePromise).resolves.toBe(mockCoValue);
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 = new CoValueState(mockCoValueId);
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.highLevelState).toBe("unavailable");
148
- await expect(state.getCoValue()).resolves.toBe("unavailable");
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 = new CoValueState(mockCoValueId);
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(createMockCoValueCore(mockCoValueId));
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.highLevelState).toBe("available");
179
- await expect(state.getCoValue()).resolves.toEqual({ id: mockCoValueId });
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 mockCoValue = createMockCoValueCore(mockCoValueId);
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(mockCoValue, "peer1");
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 = new CoValueState(mockCoValueId);
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
- ...mockCoValue.knownState(),
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 mockCoValue = createMockCoValueCore(mockCoValueId);
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(mockCoValue, "peer2");
251
+ state.markAvailable({} as CoValueHeader, "peer2");
247
252
  },
248
253
  );
249
254
 
250
255
  peer1.closed = true;
251
256
 
252
- const state = new CoValueState(mockCoValueId);
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.highLevelState).toBe("available");
262
- await expect(state.getCoValue()).resolves.toEqual({ id: mockCoValueId });
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 = new CoValueState(mockCoValueId);
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.highLevelState).toBe("unavailable");
287
- await expect(state.getCoValue()).resolves.toEqual("unavailable");
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 createMockCoValueCore(mockCoValueId: string) {
323
+ function createMockCoValueVerified(mockCoValueId: string) {
315
324
  // Setting the knownState as part of the prototype to simplify
316
325
  // the equality checks
317
- const mockCoValue = Object.create({
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
- mockCoValue.id = mockCoValueId;
326
- return mockCoValue as unknown as CoValueCore;
336
+ return mockCoValueVerified as unknown as VerifiedState;
327
337
  }