cojson 0.16.2 → 0.16.4
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 +9 -0
- package/dist/coValue.d.ts +1 -1
- package/dist/coValueContentMessage.d.ts +10 -0
- package/dist/coValueContentMessage.d.ts.map +1 -0
- package/dist/coValueContentMessage.js +46 -0
- package/dist/coValueContentMessage.js.map +1 -0
- package/dist/coValueCore/coValueCore.d.ts.map +1 -1
- package/dist/coValueCore/coValueCore.js +5 -3
- package/dist/coValueCore/coValueCore.js.map +1 -1
- package/dist/coValueCore/verifiedState.d.ts +1 -0
- package/dist/coValueCore/verifiedState.d.ts.map +1 -1
- package/dist/coValueCore/verifiedState.js +14 -27
- package/dist/coValueCore/verifiedState.js.map +1 -1
- package/dist/coValues/group.d.ts.map +1 -1
- package/dist/coValues/group.js +16 -8
- package/dist/coValues/group.js.map +1 -1
- package/dist/localNode.d.ts +6 -1
- package/dist/localNode.d.ts.map +1 -1
- package/dist/localNode.js +7 -2
- package/dist/localNode.js.map +1 -1
- package/dist/queue/LocalTransactionsSyncQueue.d.ts +24 -0
- package/dist/queue/LocalTransactionsSyncQueue.d.ts.map +1 -0
- package/dist/queue/LocalTransactionsSyncQueue.js +55 -0
- package/dist/queue/LocalTransactionsSyncQueue.js.map +1 -0
- package/dist/queue/StoreQueue.d.ts +9 -6
- package/dist/queue/StoreQueue.d.ts.map +1 -1
- package/dist/queue/StoreQueue.js +10 -2
- package/dist/queue/StoreQueue.js.map +1 -1
- package/dist/storage/storageAsync.d.ts +11 -3
- package/dist/storage/storageAsync.d.ts.map +1 -1
- package/dist/storage/storageAsync.js +59 -46
- package/dist/storage/storageAsync.js.map +1 -1
- package/dist/storage/storageSync.d.ts +9 -3
- package/dist/storage/storageSync.d.ts.map +1 -1
- package/dist/storage/storageSync.js +48 -35
- package/dist/storage/storageSync.js.map +1 -1
- package/dist/storage/syncUtils.d.ts +2 -1
- package/dist/storage/syncUtils.d.ts.map +1 -1
- package/dist/storage/syncUtils.js +4 -0
- package/dist/storage/syncUtils.js.map +1 -1
- package/dist/storage/types.d.ts +3 -2
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/sync.d.ts +6 -6
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +33 -56
- package/dist/sync.js.map +1 -1
- package/dist/tests/StorageApiAsync.test.d.ts +2 -0
- package/dist/tests/StorageApiAsync.test.d.ts.map +1 -0
- package/dist/tests/StorageApiAsync.test.js +574 -0
- package/dist/tests/StorageApiAsync.test.js.map +1 -0
- package/dist/tests/StorageApiSync.test.d.ts +2 -0
- package/dist/tests/StorageApiSync.test.d.ts.map +1 -0
- package/dist/tests/StorageApiSync.test.js +426 -0
- package/dist/tests/StorageApiSync.test.js.map +1 -0
- package/dist/tests/StoreQueue.test.js +9 -21
- package/dist/tests/StoreQueue.test.js.map +1 -1
- package/dist/tests/SyncStateManager.test.js +18 -8
- package/dist/tests/SyncStateManager.test.js.map +1 -1
- package/dist/tests/group.inheritance.test.js +79 -2
- package/dist/tests/group.inheritance.test.js.map +1 -1
- package/dist/tests/sync.auth.test.js +22 -10
- package/dist/tests/sync.auth.test.js.map +1 -1
- package/dist/tests/sync.load.test.js +25 -23
- package/dist/tests/sync.load.test.js.map +1 -1
- package/dist/tests/sync.mesh.test.js +12 -6
- package/dist/tests/sync.mesh.test.js.map +1 -1
- package/dist/tests/sync.peerReconciliation.test.js +6 -4
- package/dist/tests/sync.peerReconciliation.test.js.map +1 -1
- package/dist/tests/sync.storage.test.js +8 -14
- package/dist/tests/sync.storage.test.js.map +1 -1
- package/dist/tests/sync.storageAsync.test.js +31 -14
- package/dist/tests/sync.storageAsync.test.js.map +1 -1
- package/dist/tests/sync.test.js +5 -9
- package/dist/tests/sync.test.js.map +1 -1
- package/dist/tests/sync.upload.test.js +31 -1
- package/dist/tests/sync.upload.test.js.map +1 -1
- package/dist/tests/testStorage.d.ts +2 -3
- package/dist/tests/testStorage.d.ts.map +1 -1
- package/dist/tests/testStorage.js +16 -8
- package/dist/tests/testStorage.js.map +1 -1
- package/dist/tests/testUtils.d.ts +3 -0
- package/dist/tests/testUtils.d.ts.map +1 -1
- package/dist/tests/testUtils.js +17 -4
- package/dist/tests/testUtils.js.map +1 -1
- package/package.json +1 -1
- package/src/coValueContentMessage.ts +73 -0
- package/src/coValueCore/coValueCore.ts +14 -5
- package/src/coValueCore/verifiedState.ts +28 -35
- package/src/coValues/group.ts +20 -9
- package/src/localNode.ts +8 -3
- package/src/queue/LocalTransactionsSyncQueue.ts +96 -0
- package/src/queue/StoreQueue.ts +22 -12
- package/src/storage/storageAsync.ts +78 -56
- package/src/storage/storageSync.ts +66 -45
- package/src/storage/syncUtils.ts +9 -1
- package/src/storage/types.ts +6 -5
- package/src/sync.ts +47 -67
- package/src/tests/StorageApiAsync.test.ts +829 -0
- package/src/tests/StorageApiSync.test.ts +628 -0
- package/src/tests/StoreQueue.test.ts +10 -24
- package/src/tests/SyncStateManager.test.ts +22 -21
- package/src/tests/group.inheritance.test.ts +136 -1
- package/src/tests/sync.auth.test.ts +22 -10
- package/src/tests/sync.load.test.ts +27 -24
- package/src/tests/sync.mesh.test.ts +12 -6
- package/src/tests/sync.peerReconciliation.test.ts +6 -4
- package/src/tests/sync.storage.test.ts +8 -14
- package/src/tests/sync.storageAsync.test.ts +39 -14
- package/src/tests/sync.test.ts +6 -14
- package/src/tests/sync.upload.test.ts +38 -1
- package/src/tests/testStorage.ts +19 -13
- package/src/tests/testUtils.ts +24 -5
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { unlinkSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { describe, expect, onTestFinished, test, vi } from "vitest";
|
|
6
|
+
import { WasmCrypto } from "../crypto/WasmCrypto.js";
|
|
7
|
+
import { CoID, LocalNode, RawCoID, RawCoMap, logger } from "../exports.js";
|
|
8
|
+
import { CoValueCore } from "../exports.js";
|
|
9
|
+
import {
|
|
10
|
+
CoValueKnownState,
|
|
11
|
+
NewContentMessage,
|
|
12
|
+
emptyKnownState,
|
|
13
|
+
} from "../sync.js";
|
|
14
|
+
import { createSyncStorage } from "./testStorage.js";
|
|
15
|
+
import { loadCoValueOrFail, randomAgentAndSessionID } from "./testUtils.js";
|
|
16
|
+
|
|
17
|
+
const crypto = await WasmCrypto.create();
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Helper function that gets new content since a known state, throwing if:
|
|
21
|
+
* - The coValue is not verified
|
|
22
|
+
* - There is no new content
|
|
23
|
+
*/
|
|
24
|
+
function getNewContentSince(
|
|
25
|
+
coValue: CoValueCore,
|
|
26
|
+
knownState: CoValueKnownState,
|
|
27
|
+
): NewContentMessage {
|
|
28
|
+
if (!coValue.verified) {
|
|
29
|
+
throw new Error(`CoValue ${coValue.id} is not verified`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const contentMessage = coValue.verified.newContentSince(knownState)?.[0];
|
|
33
|
+
|
|
34
|
+
if (!contentMessage) {
|
|
35
|
+
throw new Error(`No new content available for coValue ${coValue.id}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return contentMessage;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function createFixturesNode(customDbPath?: string) {
|
|
42
|
+
const [admin, session] = randomAgentAndSessionID();
|
|
43
|
+
const node = new LocalNode(admin.agentSecret, session, crypto);
|
|
44
|
+
|
|
45
|
+
// Create a unique database file for each test
|
|
46
|
+
const dbPath = customDbPath ?? join(tmpdir(), `test-${randomUUID()}.db`);
|
|
47
|
+
const storage = createSyncStorage({
|
|
48
|
+
filename: dbPath,
|
|
49
|
+
nodeName: "test",
|
|
50
|
+
storageName: "test-storage",
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
onTestFinished(() => {
|
|
54
|
+
try {
|
|
55
|
+
unlinkSync(dbPath);
|
|
56
|
+
} catch {}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
node.setStorage(storage);
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
fixturesNode: node,
|
|
63
|
+
dbPath,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function createTestNode(dbPath?: string) {
|
|
68
|
+
const [admin, session] = randomAgentAndSessionID();
|
|
69
|
+
const node = new LocalNode(admin.agentSecret, session, crypto);
|
|
70
|
+
|
|
71
|
+
const storage = createSyncStorage({
|
|
72
|
+
filename: dbPath,
|
|
73
|
+
nodeName: "test",
|
|
74
|
+
storageName: "test-storage",
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
node,
|
|
79
|
+
storage,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
describe("StorageApiSync", () => {
|
|
84
|
+
describe("getKnownState", () => {
|
|
85
|
+
test("should return empty known state for new coValue ID and cache the result", async () => {
|
|
86
|
+
const { fixturesNode } = await createFixturesNode();
|
|
87
|
+
const { storage } = await createTestNode();
|
|
88
|
+
|
|
89
|
+
const id = fixturesNode.createGroup().id;
|
|
90
|
+
const knownState = storage.getKnownState(id);
|
|
91
|
+
|
|
92
|
+
expect(knownState).toEqual(emptyKnownState(id));
|
|
93
|
+
expect(storage.getKnownState(id)).toBe(knownState); // Should return same instance
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("should return separate known state instances for different coValue IDs", async () => {
|
|
97
|
+
const { storage } = await createTestNode();
|
|
98
|
+
const id1 = "test-id-1";
|
|
99
|
+
const id2 = "test-id-2";
|
|
100
|
+
|
|
101
|
+
const knownState1 = storage.getKnownState(id1);
|
|
102
|
+
const knownState2 = storage.getKnownState(id2);
|
|
103
|
+
|
|
104
|
+
expect(knownState1).not.toBe(knownState2);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("load", () => {
|
|
109
|
+
test("should fail gracefully when loading non-existent coValue and preserve known state", async () => {
|
|
110
|
+
const { storage } = await createTestNode();
|
|
111
|
+
const id = "non-existent-id";
|
|
112
|
+
const callback = vi.fn();
|
|
113
|
+
const done = vi.fn();
|
|
114
|
+
|
|
115
|
+
// Get initial known state
|
|
116
|
+
const initialKnownState = storage.getKnownState(id);
|
|
117
|
+
expect(initialKnownState).toEqual(emptyKnownState(id as `co_z${string}`));
|
|
118
|
+
|
|
119
|
+
await storage.load(id, callback, done);
|
|
120
|
+
|
|
121
|
+
expect(done).toHaveBeenCalledWith(false);
|
|
122
|
+
expect(callback).not.toHaveBeenCalled();
|
|
123
|
+
|
|
124
|
+
// Verify that storage known state is NOT updated when load fails
|
|
125
|
+
const afterLoadKnownState = storage.getKnownState(id);
|
|
126
|
+
expect(afterLoadKnownState).toEqual(initialKnownState);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("should successfully load coValue with header and update known state", async () => {
|
|
130
|
+
const { fixturesNode, dbPath } = await createFixturesNode();
|
|
131
|
+
const { node, storage } = await createTestNode(dbPath);
|
|
132
|
+
const callback = vi.fn((content) =>
|
|
133
|
+
node.syncManager.handleNewContent(content, "storage"),
|
|
134
|
+
);
|
|
135
|
+
const done = vi.fn();
|
|
136
|
+
|
|
137
|
+
// Create a real group and get its content message
|
|
138
|
+
const group = fixturesNode.createGroup();
|
|
139
|
+
await group.core.waitForSync();
|
|
140
|
+
|
|
141
|
+
// Get initial known state
|
|
142
|
+
const initialKnownState = storage.getKnownState(group.id);
|
|
143
|
+
expect(initialKnownState).toEqual(emptyKnownState(group.id));
|
|
144
|
+
|
|
145
|
+
await storage.load(group.id, callback, done);
|
|
146
|
+
|
|
147
|
+
expect(callback).toHaveBeenCalledWith(
|
|
148
|
+
expect.objectContaining({
|
|
149
|
+
id: group.id,
|
|
150
|
+
header: group.core.verified.header,
|
|
151
|
+
new: expect.any(Object),
|
|
152
|
+
}),
|
|
153
|
+
);
|
|
154
|
+
expect(done).toHaveBeenCalledWith(true);
|
|
155
|
+
|
|
156
|
+
// Verify that storage known state is updated after load
|
|
157
|
+
const updatedKnownState = storage.getKnownState(group.id);
|
|
158
|
+
expect(updatedKnownState).toEqual(group.core.verified.knownState());
|
|
159
|
+
|
|
160
|
+
const groupOnNode = await loadCoValueOrFail(node, group.id);
|
|
161
|
+
|
|
162
|
+
expect(groupOnNode.core.verified.header).toEqual(
|
|
163
|
+
group.core.verified.header,
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("should successfully load coValue with transactions and update known state", async () => {
|
|
168
|
+
const { fixturesNode, dbPath } = await createFixturesNode();
|
|
169
|
+
const { node, storage } = await createTestNode(dbPath);
|
|
170
|
+
const callback = vi.fn((content) =>
|
|
171
|
+
node.syncManager.handleNewContent(content, "storage"),
|
|
172
|
+
);
|
|
173
|
+
const done = vi.fn();
|
|
174
|
+
|
|
175
|
+
// Create a real group and add a member to create transactions
|
|
176
|
+
const group = fixturesNode.createGroup();
|
|
177
|
+
group.addMember("everyone", "reader");
|
|
178
|
+
await group.core.waitForSync();
|
|
179
|
+
|
|
180
|
+
// Get initial known state
|
|
181
|
+
const initialKnownState = storage.getKnownState(group.id);
|
|
182
|
+
expect(initialKnownState).toEqual(emptyKnownState(group.id));
|
|
183
|
+
|
|
184
|
+
await storage.load(group.id, callback, done);
|
|
185
|
+
|
|
186
|
+
expect(callback).toHaveBeenCalledWith(
|
|
187
|
+
expect.objectContaining({
|
|
188
|
+
id: group.id,
|
|
189
|
+
header: group.core.verified.header,
|
|
190
|
+
new: expect.objectContaining({
|
|
191
|
+
[fixturesNode.currentSessionID]: expect.any(Object),
|
|
192
|
+
}),
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
expect(done).toHaveBeenCalledWith(true);
|
|
196
|
+
|
|
197
|
+
// Verify that storage known state is updated after load
|
|
198
|
+
const updatedKnownState = storage.getKnownState(group.id);
|
|
199
|
+
expect(updatedKnownState).toEqual(group.core.verified.knownState());
|
|
200
|
+
|
|
201
|
+
const groupOnNode = await loadCoValueOrFail(node, group.id);
|
|
202
|
+
expect(groupOnNode.get("everyone")).toEqual("reader");
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe("store", () => {
|
|
207
|
+
test("should successfully store new coValue with header and update known state", async () => {
|
|
208
|
+
const { fixturesNode } = await createFixturesNode();
|
|
209
|
+
const { node, storage } = await createTestNode();
|
|
210
|
+
// Create a real group and get its content message
|
|
211
|
+
const group = fixturesNode.createGroup();
|
|
212
|
+
const contentMessage = getNewContentSince(
|
|
213
|
+
group.core,
|
|
214
|
+
emptyKnownState(group.id),
|
|
215
|
+
);
|
|
216
|
+
const correctionCallback = vi.fn();
|
|
217
|
+
|
|
218
|
+
// Get initial known state
|
|
219
|
+
const initialKnownState = storage.getKnownState(group.id);
|
|
220
|
+
expect(initialKnownState).toEqual(emptyKnownState(group.id));
|
|
221
|
+
|
|
222
|
+
storage.store(contentMessage, correctionCallback);
|
|
223
|
+
|
|
224
|
+
// Verify that storage known state is updated after store
|
|
225
|
+
const updatedKnownState = storage.getKnownState(group.id);
|
|
226
|
+
expect(updatedKnownState).toEqual(group.core.verified.knownState());
|
|
227
|
+
|
|
228
|
+
node.setStorage(storage);
|
|
229
|
+
|
|
230
|
+
const groupOnNode = await loadCoValueOrFail(node, group.id);
|
|
231
|
+
|
|
232
|
+
expect(groupOnNode.core.verified.header).toEqual(
|
|
233
|
+
group.core.verified.header,
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test("should successfully store coValue with transactions and update known state", async () => {
|
|
238
|
+
const { fixturesNode } = await createFixturesNode();
|
|
239
|
+
const { node, storage } = await createTestNode();
|
|
240
|
+
|
|
241
|
+
// Create a real group and add a member to create transactions
|
|
242
|
+
const group = fixturesNode.createGroup();
|
|
243
|
+
const knownState = group.core.verified.knownState();
|
|
244
|
+
|
|
245
|
+
group.addMember("everyone", "reader");
|
|
246
|
+
|
|
247
|
+
const contentMessage = getNewContentSince(
|
|
248
|
+
group.core,
|
|
249
|
+
emptyKnownState(group.id),
|
|
250
|
+
);
|
|
251
|
+
const correctionCallback = vi.fn();
|
|
252
|
+
|
|
253
|
+
// Get initial known state
|
|
254
|
+
const initialKnownState = storage.getKnownState(group.id);
|
|
255
|
+
expect(initialKnownState).toEqual(emptyKnownState(group.id));
|
|
256
|
+
|
|
257
|
+
storage.store(contentMessage, correctionCallback);
|
|
258
|
+
|
|
259
|
+
// Verify that storage known state is updated after store
|
|
260
|
+
const updatedKnownState = storage.getKnownState(group.id);
|
|
261
|
+
expect(updatedKnownState).toEqual(group.core.verified.knownState());
|
|
262
|
+
|
|
263
|
+
node.setStorage(storage);
|
|
264
|
+
|
|
265
|
+
const groupOnNode = await loadCoValueOrFail(node, group.id);
|
|
266
|
+
expect(groupOnNode.get("everyone")).toEqual("reader");
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test("should handle correction when header assumption is invalid", async () => {
|
|
270
|
+
const { fixturesNode } = await createFixturesNode();
|
|
271
|
+
const { node, storage } = await createTestNode();
|
|
272
|
+
|
|
273
|
+
const group = fixturesNode.createGroup();
|
|
274
|
+
const knownState = group.core.verified.knownState();
|
|
275
|
+
|
|
276
|
+
group.addMember("everyone", "reader");
|
|
277
|
+
|
|
278
|
+
const contentMessage = getNewContentSince(group.core, knownState);
|
|
279
|
+
const correctionCallback = vi.fn((known) => {
|
|
280
|
+
expect(known).toEqual(emptyKnownState(group.id));
|
|
281
|
+
return group.core.verified.newContentSince(known);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// Get initial known state
|
|
285
|
+
const initialKnownState = storage.getKnownState(group.id);
|
|
286
|
+
expect(initialKnownState).toEqual(emptyKnownState(group.id));
|
|
287
|
+
|
|
288
|
+
const result = storage.store(contentMessage, correctionCallback);
|
|
289
|
+
|
|
290
|
+
expect(correctionCallback).toHaveBeenCalledTimes(1);
|
|
291
|
+
expect(result).toBe(true);
|
|
292
|
+
|
|
293
|
+
// Verify that storage known state is updated after store with correction
|
|
294
|
+
const updatedKnownState = storage.getKnownState(group.id);
|
|
295
|
+
expect(updatedKnownState).toEqual(group.core.verified.knownState());
|
|
296
|
+
|
|
297
|
+
node.setStorage(storage);
|
|
298
|
+
const groupOnNode = await loadCoValueOrFail(node, group.id);
|
|
299
|
+
|
|
300
|
+
expect(groupOnNode.get("everyone")).toEqual("reader");
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
test("should handle correction when new content assumption is invalid", async () => {
|
|
304
|
+
const { fixturesNode } = await createFixturesNode();
|
|
305
|
+
const { node, storage } = await createTestNode();
|
|
306
|
+
|
|
307
|
+
const group = fixturesNode.createGroup();
|
|
308
|
+
|
|
309
|
+
const initialContent = getNewContentSince(
|
|
310
|
+
group.core,
|
|
311
|
+
emptyKnownState(group.id),
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const initialKnownState = group.core.knownState();
|
|
315
|
+
|
|
316
|
+
group.addMember("everyone", "reader");
|
|
317
|
+
|
|
318
|
+
const knownState = group.core.knownState();
|
|
319
|
+
|
|
320
|
+
group.addMember("everyone", "writer");
|
|
321
|
+
|
|
322
|
+
const contentMessage = getNewContentSince(group.core, knownState);
|
|
323
|
+
const correctionCallback = vi.fn((known) => {
|
|
324
|
+
expect(known).toEqual(initialKnownState);
|
|
325
|
+
return group.core.verified.newContentSince(known);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
// Get initial storage known state
|
|
329
|
+
const initialStorageKnownState = storage.getKnownState(group.id);
|
|
330
|
+
expect(initialStorageKnownState).toEqual(emptyKnownState(group.id));
|
|
331
|
+
|
|
332
|
+
storage.store(initialContent, correctionCallback);
|
|
333
|
+
|
|
334
|
+
// Verify storage known state after first store
|
|
335
|
+
const afterFirstStore = storage.getKnownState(group.id);
|
|
336
|
+
expect(afterFirstStore).toEqual(initialKnownState);
|
|
337
|
+
|
|
338
|
+
const result = storage.store(contentMessage, correctionCallback);
|
|
339
|
+
expect(correctionCallback).toHaveBeenCalledTimes(1);
|
|
340
|
+
|
|
341
|
+
expect(result).toBe(true);
|
|
342
|
+
|
|
343
|
+
// Verify that storage known state is updated after store with correction
|
|
344
|
+
const finalKnownState = storage.getKnownState(group.id);
|
|
345
|
+
expect(finalKnownState).toEqual(group.core.verified.knownState());
|
|
346
|
+
|
|
347
|
+
node.setStorage(storage);
|
|
348
|
+
const groupOnNode = await loadCoValueOrFail(node, group.id);
|
|
349
|
+
|
|
350
|
+
expect(groupOnNode.get("everyone")).toEqual("writer");
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
test("should log error and fail when correction callback returns undefined", async () => {
|
|
354
|
+
const { fixturesNode } = await createFixturesNode();
|
|
355
|
+
const { storage } = await createTestNode();
|
|
356
|
+
|
|
357
|
+
const group = fixturesNode.createGroup();
|
|
358
|
+
|
|
359
|
+
const knownState = group.core.knownState();
|
|
360
|
+
group.addMember("everyone", "writer");
|
|
361
|
+
|
|
362
|
+
const contentMessage = getNewContentSince(group.core, knownState);
|
|
363
|
+
const correctionCallback = vi.fn((known) => {
|
|
364
|
+
return undefined;
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
// Get initial known state
|
|
368
|
+
const initialKnownState = storage.getKnownState(group.id);
|
|
369
|
+
expect(initialKnownState).toEqual(emptyKnownState(group.id));
|
|
370
|
+
|
|
371
|
+
const errorSpy = vi.spyOn(logger, "error").mockImplementation(() => {});
|
|
372
|
+
const result = storage.store(contentMessage, correctionCallback);
|
|
373
|
+
expect(correctionCallback).toHaveBeenCalledTimes(1);
|
|
374
|
+
|
|
375
|
+
expect(result).toBe(false);
|
|
376
|
+
|
|
377
|
+
// Verify that storage known state is NOT updated when store fails
|
|
378
|
+
const afterStoreKnownState = storage.getKnownState(group.id);
|
|
379
|
+
expect(afterStoreKnownState).toEqual(initialKnownState);
|
|
380
|
+
|
|
381
|
+
expect(errorSpy).toHaveBeenCalledWith(
|
|
382
|
+
"Correction callback returned undefined",
|
|
383
|
+
{
|
|
384
|
+
knownState: expect.any(Object),
|
|
385
|
+
correction: null,
|
|
386
|
+
},
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
errorSpy.mockClear();
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
test("should log error and fail when correction callback returns invalid content message", async () => {
|
|
393
|
+
const { fixturesNode } = await createFixturesNode();
|
|
394
|
+
const { storage } = await createTestNode();
|
|
395
|
+
|
|
396
|
+
const group = fixturesNode.createGroup();
|
|
397
|
+
|
|
398
|
+
const knownState = group.core.knownState();
|
|
399
|
+
group.addMember("everyone", "writer");
|
|
400
|
+
|
|
401
|
+
const contentMessage = getNewContentSince(group.core, knownState);
|
|
402
|
+
const correctionCallback = vi.fn(() => {
|
|
403
|
+
return [contentMessage];
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
const errorSpy = vi.spyOn(logger, "error").mockImplementation(() => {});
|
|
407
|
+
const result = storage.store(contentMessage, correctionCallback);
|
|
408
|
+
expect(correctionCallback).toHaveBeenCalledTimes(1);
|
|
409
|
+
|
|
410
|
+
expect(result).toBe(false);
|
|
411
|
+
|
|
412
|
+
expect(errorSpy).toHaveBeenCalledWith(
|
|
413
|
+
"Correction callback returned undefined",
|
|
414
|
+
{
|
|
415
|
+
knownState: expect.any(Object),
|
|
416
|
+
correction: null,
|
|
417
|
+
},
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
expect(errorSpy).toHaveBeenCalledWith("Double correction requested", {
|
|
421
|
+
knownState: expect.any(Object),
|
|
422
|
+
msg: expect.any(Object),
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
errorSpy.mockClear();
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
test("should successfully store coValue with multiple sessions", async () => {
|
|
429
|
+
const { fixturesNode, dbPath } = await createFixturesNode();
|
|
430
|
+
const { fixturesNode: fixtureNode2 } = await createFixturesNode(dbPath);
|
|
431
|
+
const { node, storage } = await createTestNode();
|
|
432
|
+
|
|
433
|
+
const coValue = fixturesNode.createCoValue({
|
|
434
|
+
type: "comap",
|
|
435
|
+
ruleset: { type: "unsafeAllowAll" },
|
|
436
|
+
meta: null,
|
|
437
|
+
...crypto.createdNowUnique(),
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
coValue.makeTransaction(
|
|
441
|
+
[
|
|
442
|
+
{
|
|
443
|
+
count: 1,
|
|
444
|
+
},
|
|
445
|
+
],
|
|
446
|
+
"trusting",
|
|
447
|
+
);
|
|
448
|
+
|
|
449
|
+
await coValue.waitForSync();
|
|
450
|
+
|
|
451
|
+
const mapOnNode2 = await loadCoValueOrFail(
|
|
452
|
+
fixtureNode2,
|
|
453
|
+
coValue.id as CoID<RawCoMap>,
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
coValue.makeTransaction(
|
|
457
|
+
[
|
|
458
|
+
{
|
|
459
|
+
count: 2,
|
|
460
|
+
},
|
|
461
|
+
],
|
|
462
|
+
"trusting",
|
|
463
|
+
);
|
|
464
|
+
|
|
465
|
+
const knownState = mapOnNode2.core.knownState();
|
|
466
|
+
|
|
467
|
+
const contentMessage = getNewContentSince(
|
|
468
|
+
mapOnNode2.core,
|
|
469
|
+
emptyKnownState(mapOnNode2.id),
|
|
470
|
+
);
|
|
471
|
+
const correctionCallback = vi.fn();
|
|
472
|
+
|
|
473
|
+
storage.store(contentMessage, correctionCallback);
|
|
474
|
+
|
|
475
|
+
node.setStorage(storage);
|
|
476
|
+
|
|
477
|
+
const finalMap = await loadCoValueOrFail(node, mapOnNode2.id);
|
|
478
|
+
expect(finalMap.core.knownState()).toEqual(knownState);
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
describe("dependencies", () => {
|
|
483
|
+
test("should load dependencies before dependent coValues and update all known states", async () => {
|
|
484
|
+
const { fixturesNode, dbPath } = await createFixturesNode();
|
|
485
|
+
const { node, storage } = await createTestNode(dbPath);
|
|
486
|
+
|
|
487
|
+
// Create a group and a map owned by that group to create dependencies
|
|
488
|
+
const group = fixturesNode.createGroup();
|
|
489
|
+
group.addMember("everyone", "reader");
|
|
490
|
+
const map = group.createMap({ test: "value" });
|
|
491
|
+
await group.core.waitForSync();
|
|
492
|
+
await map.core.waitForSync();
|
|
493
|
+
|
|
494
|
+
const callback = vi.fn((content) =>
|
|
495
|
+
node.syncManager.handleNewContent(content, "storage"),
|
|
496
|
+
);
|
|
497
|
+
const done = vi.fn();
|
|
498
|
+
|
|
499
|
+
// Get initial known states
|
|
500
|
+
const initialGroupKnownState = storage.getKnownState(group.id);
|
|
501
|
+
const initialMapKnownState = storage.getKnownState(map.id);
|
|
502
|
+
expect(initialGroupKnownState).toEqual(emptyKnownState(group.id));
|
|
503
|
+
expect(initialMapKnownState).toEqual(emptyKnownState(map.id));
|
|
504
|
+
|
|
505
|
+
// Load the map, which should also load the group dependency first
|
|
506
|
+
await storage.load(map.id, callback, done);
|
|
507
|
+
|
|
508
|
+
expect(callback).toHaveBeenCalledTimes(2); // Group first, then map
|
|
509
|
+
expect(callback).toHaveBeenNthCalledWith(
|
|
510
|
+
1,
|
|
511
|
+
expect.objectContaining({
|
|
512
|
+
id: group.id,
|
|
513
|
+
}),
|
|
514
|
+
);
|
|
515
|
+
expect(callback).toHaveBeenNthCalledWith(
|
|
516
|
+
2,
|
|
517
|
+
expect.objectContaining({
|
|
518
|
+
id: map.id,
|
|
519
|
+
}),
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
expect(done).toHaveBeenCalledWith(true);
|
|
523
|
+
|
|
524
|
+
// Verify that storage known states are updated after load
|
|
525
|
+
const updatedGroupKnownState = storage.getKnownState(group.id);
|
|
526
|
+
const updatedMapKnownState = storage.getKnownState(map.id);
|
|
527
|
+
expect(updatedGroupKnownState).toEqual(group.core.verified.knownState());
|
|
528
|
+
expect(updatedMapKnownState).toEqual(map.core.verified.knownState());
|
|
529
|
+
|
|
530
|
+
node.setStorage(storage);
|
|
531
|
+
const mapOnNode = await loadCoValueOrFail(node, map.id);
|
|
532
|
+
expect(mapOnNode.get("test")).toEqual("value");
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
test("should skip loading already loaded dependencies", async () => {
|
|
536
|
+
const { fixturesNode, dbPath } = await createFixturesNode();
|
|
537
|
+
const { node, storage } = await createTestNode(dbPath);
|
|
538
|
+
|
|
539
|
+
// Create a group and a map owned by that group
|
|
540
|
+
const group = fixturesNode.createGroup();
|
|
541
|
+
group.addMember("everyone", "reader");
|
|
542
|
+
const map = group.createMap({ test: "value" });
|
|
543
|
+
await group.core.waitForSync();
|
|
544
|
+
await map.core.waitForSync();
|
|
545
|
+
|
|
546
|
+
const callback = vi.fn((content) =>
|
|
547
|
+
node.syncManager.handleNewContent(content, "storage"),
|
|
548
|
+
);
|
|
549
|
+
const done = vi.fn();
|
|
550
|
+
|
|
551
|
+
// Get initial known states
|
|
552
|
+
const initialGroupKnownState = storage.getKnownState(group.id);
|
|
553
|
+
const initialMapKnownState = storage.getKnownState(map.id);
|
|
554
|
+
expect(initialGroupKnownState).toEqual(emptyKnownState(group.id));
|
|
555
|
+
expect(initialMapKnownState).toEqual(emptyKnownState(map.id));
|
|
556
|
+
|
|
557
|
+
// First load the group
|
|
558
|
+
await storage.load(group.id, callback, done);
|
|
559
|
+
callback.mockClear();
|
|
560
|
+
done.mockClear();
|
|
561
|
+
|
|
562
|
+
// Verify group known state is updated after first load
|
|
563
|
+
const afterGroupLoad = storage.getKnownState(group.id);
|
|
564
|
+
expect(afterGroupLoad).toEqual(group.core.verified.knownState());
|
|
565
|
+
|
|
566
|
+
// Then load the map - the group dependency should already be loaded
|
|
567
|
+
await storage.load(map.id, callback, done);
|
|
568
|
+
|
|
569
|
+
// Should only call callback once for the map since group is already loaded
|
|
570
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
571
|
+
expect(callback).toHaveBeenCalledWith(
|
|
572
|
+
expect.objectContaining({
|
|
573
|
+
id: map.id,
|
|
574
|
+
}),
|
|
575
|
+
);
|
|
576
|
+
|
|
577
|
+
expect(done).toHaveBeenCalledWith(true);
|
|
578
|
+
|
|
579
|
+
// Verify map known state is updated after second load
|
|
580
|
+
const finalMapKnownState = storage.getKnownState(map.id);
|
|
581
|
+
expect(finalMapKnownState).toEqual(map.core.verified.knownState());
|
|
582
|
+
|
|
583
|
+
node.setStorage(storage);
|
|
584
|
+
const mapOnNode = await loadCoValueOrFail(node, map.id);
|
|
585
|
+
expect(mapOnNode.get("test")).toEqual("value");
|
|
586
|
+
});
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
describe("waitForSync", () => {
|
|
590
|
+
test("should resolve immediately when coValue is already synced", async () => {
|
|
591
|
+
const { fixturesNode, dbPath } = await createFixturesNode();
|
|
592
|
+
const { node, storage } = await createTestNode(dbPath);
|
|
593
|
+
|
|
594
|
+
// Create a group and add a member
|
|
595
|
+
const group = fixturesNode.createGroup();
|
|
596
|
+
group.addMember("everyone", "reader");
|
|
597
|
+
await group.core.waitForSync();
|
|
598
|
+
|
|
599
|
+
// Store the group in storage
|
|
600
|
+
const contentMessage = getNewContentSince(
|
|
601
|
+
group.core,
|
|
602
|
+
emptyKnownState(group.id),
|
|
603
|
+
);
|
|
604
|
+
const correctionCallback = vi.fn();
|
|
605
|
+
storage.store(contentMessage, correctionCallback);
|
|
606
|
+
|
|
607
|
+
node.setStorage(storage);
|
|
608
|
+
|
|
609
|
+
// Load the group on the new node
|
|
610
|
+
const groupOnNode = await loadCoValueOrFail(node, group.id);
|
|
611
|
+
|
|
612
|
+
// Wait for sync should resolve immediately since the coValue is already synced
|
|
613
|
+
await expect(
|
|
614
|
+
storage.waitForSync(group.id, groupOnNode.core),
|
|
615
|
+
).resolves.toBeUndefined();
|
|
616
|
+
|
|
617
|
+
expect(groupOnNode.get("everyone")).toEqual("reader");
|
|
618
|
+
});
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
describe("close", () => {
|
|
622
|
+
test("should close storage without throwing errors", async () => {
|
|
623
|
+
const { storage } = await createTestNode();
|
|
624
|
+
|
|
625
|
+
expect(() => storage.close()).not.toThrow();
|
|
626
|
+
});
|
|
627
|
+
});
|
|
628
|
+
});
|
|
@@ -2,15 +2,13 @@ import { beforeEach, describe, expect, test, vi } from "vitest";
|
|
|
2
2
|
import { StoreQueue } from "../queue/StoreQueue.js";
|
|
3
3
|
import type { CoValueKnownState, NewContentMessage } from "../sync.js";
|
|
4
4
|
|
|
5
|
-
function createMockNewContentMessage(id: string): NewContentMessage
|
|
6
|
-
return
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
},
|
|
13
|
-
];
|
|
5
|
+
function createMockNewContentMessage(id: string): NewContentMessage {
|
|
6
|
+
return {
|
|
7
|
+
action: "content",
|
|
8
|
+
id: id as any,
|
|
9
|
+
priority: 0,
|
|
10
|
+
new: {},
|
|
11
|
+
};
|
|
14
12
|
}
|
|
15
13
|
|
|
16
14
|
function setup() {
|
|
@@ -154,14 +152,14 @@ describe("StoreQueue", () => {
|
|
|
154
152
|
storeQueue.push(data1, mockCorrectionCallback);
|
|
155
153
|
storeQueue.push(data2, mockCorrectionCallback);
|
|
156
154
|
|
|
157
|
-
storeQueue.
|
|
155
|
+
storeQueue.close();
|
|
158
156
|
|
|
159
157
|
expect(storeQueue.pull()).toBeUndefined();
|
|
160
158
|
});
|
|
161
159
|
|
|
162
160
|
test("should handle empty queue", () => {
|
|
163
161
|
const { storeQueue } = setup();
|
|
164
|
-
expect(() => storeQueue.
|
|
162
|
+
expect(() => storeQueue.close()).not.toThrow();
|
|
165
163
|
expect(storeQueue.pull()).toBeUndefined();
|
|
166
164
|
});
|
|
167
165
|
});
|
|
@@ -240,23 +238,11 @@ describe("StoreQueue", () => {
|
|
|
240
238
|
});
|
|
241
239
|
|
|
242
240
|
describe("edge cases", () => {
|
|
243
|
-
test("should handle undefined data", () => {
|
|
244
|
-
const { storeQueue, mockCorrectionCallback } = setup();
|
|
245
|
-
const data: NewContentMessage[] = [];
|
|
246
|
-
storeQueue.push(data, mockCorrectionCallback);
|
|
247
|
-
|
|
248
|
-
const entry = storeQueue.pull();
|
|
249
|
-
expect(entry).toEqual({
|
|
250
|
-
data,
|
|
251
|
-
correctionCallback: mockCorrectionCallback,
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
|
|
255
241
|
test("should handle null correction callback", () => {
|
|
256
242
|
const { storeQueue } = setup();
|
|
257
243
|
const data = createMockNewContentMessage("co1");
|
|
258
244
|
|
|
259
|
-
const nullCallback = () =>
|
|
245
|
+
const nullCallback = () => undefined;
|
|
260
246
|
storeQueue.push(data, nullCallback);
|
|
261
247
|
|
|
262
248
|
const entry = storeQueue.pull();
|