cojson 0.8.12 → 0.8.16

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 (158) hide show
  1. package/CHANGELOG.md +89 -83
  2. package/dist/native/PeerKnownStates.js +1 -1
  3. package/dist/native/PeerKnownStates.js.map +1 -1
  4. package/dist/native/PeerState.js +1 -1
  5. package/dist/native/PeerState.js.map +1 -1
  6. package/dist/native/PriorityBasedMessageQueue.js +1 -10
  7. package/dist/native/PriorityBasedMessageQueue.js.map +1 -1
  8. package/dist/native/base64url.js.map +1 -1
  9. package/dist/native/base64url.test.js +1 -1
  10. package/dist/native/base64url.test.js.map +1 -1
  11. package/dist/native/coValue.js.map +1 -1
  12. package/dist/native/coValueCore.js +141 -149
  13. package/dist/native/coValueCore.js.map +1 -1
  14. package/dist/native/coValueState.js.map +1 -1
  15. package/dist/native/coValues/account.js +6 -6
  16. package/dist/native/coValues/account.js.map +1 -1
  17. package/dist/native/coValues/coList.js +2 -3
  18. package/dist/native/coValues/coList.js.map +1 -1
  19. package/dist/native/coValues/coMap.js +1 -1
  20. package/dist/native/coValues/coMap.js.map +1 -1
  21. package/dist/native/coValues/coStream.js +3 -5
  22. package/dist/native/coValues/coStream.js.map +1 -1
  23. package/dist/native/coValues/group.js +11 -11
  24. package/dist/native/coValues/group.js.map +1 -1
  25. package/dist/native/coreToCoValue.js +2 -2
  26. package/dist/native/coreToCoValue.js.map +1 -1
  27. package/dist/native/crypto/PureJSCrypto.js +4 -4
  28. package/dist/native/crypto/PureJSCrypto.js.map +1 -1
  29. package/dist/native/crypto/crypto.js.map +1 -1
  30. package/dist/native/exports.js +12 -12
  31. package/dist/native/exports.js.map +1 -1
  32. package/dist/native/ids.js.map +1 -1
  33. package/dist/native/jsonStringify.js.map +1 -1
  34. package/dist/native/localNode.js +5 -7
  35. package/dist/native/localNode.js.map +1 -1
  36. package/dist/native/permissions.js +4 -7
  37. package/dist/native/permissions.js.map +1 -1
  38. package/dist/native/priority.js.map +1 -1
  39. package/dist/native/storage/FileSystem.js.map +1 -1
  40. package/dist/native/storage/chunksAndKnownStates.js +2 -4
  41. package/dist/native/storage/chunksAndKnownStates.js.map +1 -1
  42. package/dist/native/storage/index.js +6 -15
  43. package/dist/native/storage/index.js.map +1 -1
  44. package/dist/native/streamUtils.js.map +1 -1
  45. package/dist/native/sync.js +2 -4
  46. package/dist/native/sync.js.map +1 -1
  47. package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  48. package/dist/native/typeUtils/expectGroup.js.map +1 -1
  49. package/dist/native/typeUtils/isAccountID.js.map +1 -1
  50. package/dist/native/typeUtils/isCoValue.js +1 -1
  51. package/dist/native/typeUtils/isCoValue.js.map +1 -1
  52. package/dist/web/PeerKnownStates.js +1 -1
  53. package/dist/web/PeerKnownStates.js.map +1 -1
  54. package/dist/web/PeerState.js +1 -1
  55. package/dist/web/PeerState.js.map +1 -1
  56. package/dist/web/PriorityBasedMessageQueue.js +1 -10
  57. package/dist/web/PriorityBasedMessageQueue.js.map +1 -1
  58. package/dist/web/base64url.js.map +1 -1
  59. package/dist/web/base64url.test.js +1 -1
  60. package/dist/web/base64url.test.js.map +1 -1
  61. package/dist/web/coValue.js.map +1 -1
  62. package/dist/web/coValueCore.js +141 -149
  63. package/dist/web/coValueCore.js.map +1 -1
  64. package/dist/web/coValueState.js.map +1 -1
  65. package/dist/web/coValues/account.js +6 -6
  66. package/dist/web/coValues/account.js.map +1 -1
  67. package/dist/web/coValues/coList.js +2 -3
  68. package/dist/web/coValues/coList.js.map +1 -1
  69. package/dist/web/coValues/coMap.js +1 -1
  70. package/dist/web/coValues/coMap.js.map +1 -1
  71. package/dist/web/coValues/coStream.js +3 -5
  72. package/dist/web/coValues/coStream.js.map +1 -1
  73. package/dist/web/coValues/group.js +11 -11
  74. package/dist/web/coValues/group.js.map +1 -1
  75. package/dist/web/coreToCoValue.js +2 -2
  76. package/dist/web/coreToCoValue.js.map +1 -1
  77. package/dist/web/crypto/PureJSCrypto.js +4 -4
  78. package/dist/web/crypto/PureJSCrypto.js.map +1 -1
  79. package/dist/web/crypto/WasmCrypto.js +5 -5
  80. package/dist/web/crypto/WasmCrypto.js.map +1 -1
  81. package/dist/web/crypto/crypto.js.map +1 -1
  82. package/dist/web/exports.js +12 -12
  83. package/dist/web/exports.js.map +1 -1
  84. package/dist/web/ids.js.map +1 -1
  85. package/dist/web/jsonStringify.js.map +1 -1
  86. package/dist/web/localNode.js +5 -7
  87. package/dist/web/localNode.js.map +1 -1
  88. package/dist/web/permissions.js +4 -7
  89. package/dist/web/permissions.js.map +1 -1
  90. package/dist/web/priority.js.map +1 -1
  91. package/dist/web/storage/FileSystem.js.map +1 -1
  92. package/dist/web/storage/chunksAndKnownStates.js +2 -4
  93. package/dist/web/storage/chunksAndKnownStates.js.map +1 -1
  94. package/dist/web/storage/index.js +6 -15
  95. package/dist/web/storage/index.js.map +1 -1
  96. package/dist/web/streamUtils.js.map +1 -1
  97. package/dist/web/sync.js +2 -4
  98. package/dist/web/sync.js.map +1 -1
  99. package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  100. package/dist/web/typeUtils/expectGroup.js.map +1 -1
  101. package/dist/web/typeUtils/isAccountID.js.map +1 -1
  102. package/dist/web/typeUtils/isCoValue.js +1 -1
  103. package/dist/web/typeUtils/isCoValue.js.map +1 -1
  104. package/package.json +4 -14
  105. package/src/PeerKnownStates.ts +91 -89
  106. package/src/PeerState.ts +72 -73
  107. package/src/PriorityBasedMessageQueue.ts +42 -49
  108. package/src/base64url.test.ts +24 -24
  109. package/src/base64url.ts +44 -45
  110. package/src/coValue.ts +45 -45
  111. package/src/coValueCore.ts +746 -785
  112. package/src/coValueState.ts +82 -72
  113. package/src/coValues/account.ts +143 -150
  114. package/src/coValues/coList.ts +520 -522
  115. package/src/coValues/coMap.ts +283 -285
  116. package/src/coValues/coStream.ts +320 -324
  117. package/src/coValues/group.ts +306 -305
  118. package/src/coreToCoValue.ts +28 -31
  119. package/src/crypto/PureJSCrypto.ts +188 -194
  120. package/src/crypto/WasmCrypto.ts +236 -254
  121. package/src/crypto/crypto.ts +302 -309
  122. package/src/exports.ts +116 -116
  123. package/src/ids.ts +9 -9
  124. package/src/jsonStringify.ts +46 -46
  125. package/src/jsonValue.ts +24 -10
  126. package/src/localNode.ts +635 -660
  127. package/src/media.ts +3 -3
  128. package/src/permissions.ts +272 -278
  129. package/src/priority.ts +21 -19
  130. package/src/storage/FileSystem.ts +91 -99
  131. package/src/storage/chunksAndKnownStates.ts +110 -115
  132. package/src/storage/index.ts +466 -497
  133. package/src/streamUtils.ts +60 -60
  134. package/src/sync.ts +593 -615
  135. package/src/tests/PeerKnownStates.test.ts +38 -34
  136. package/src/tests/PeerState.test.ts +101 -64
  137. package/src/tests/PriorityBasedMessageQueue.test.ts +91 -91
  138. package/src/tests/account.test.ts +59 -59
  139. package/src/tests/coList.test.ts +65 -65
  140. package/src/tests/coMap.test.ts +137 -137
  141. package/src/tests/coStream.test.ts +254 -257
  142. package/src/tests/coValueCore.test.ts +153 -156
  143. package/src/tests/crypto.test.ts +136 -144
  144. package/src/tests/cryptoImpl.test.ts +205 -197
  145. package/src/tests/group.test.ts +24 -24
  146. package/src/tests/permissions.test.ts +1306 -1371
  147. package/src/tests/priority.test.ts +65 -82
  148. package/src/tests/sync.test.ts +1300 -1291
  149. package/src/tests/testUtils.ts +52 -53
  150. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +4 -4
  151. package/src/typeUtils/expectGroup.ts +9 -9
  152. package/src/typeUtils/isAccountID.ts +1 -1
  153. package/src/typeUtils/isCoValue.ts +9 -9
  154. package/tsconfig.json +4 -6
  155. package/tsconfig.native.json +9 -11
  156. package/tsconfig.web.json +4 -10
  157. package/.eslintrc.cjs +0 -25
  158. package/.prettierrc.js +0 -9
@@ -1,47 +1,51 @@
1
- import { PeerKnownStates } from '../PeerKnownStates.js';
2
- import { CoValueKnownState, emptyKnownState } from '../sync.js';
3
- import { RawCoID, SessionID } from '../ids.js';
4
- import { vi, describe, test, expect } from 'vitest';
1
+ import { describe, expect, test, vi } from "vitest";
2
+ import { PeerKnownStates } from "../PeerKnownStates.js";
3
+ import { RawCoID, SessionID } from "../ids.js";
4
+ import { CoValueKnownState, emptyKnownState } from "../sync.js";
5
5
 
6
-
7
- describe('PeerKnownStates', () => {
8
- test('should set and get a known state', () => {
6
+ describe("PeerKnownStates", () => {
7
+ test("should set and get a known state", () => {
9
8
  const peerKnownStates = new PeerKnownStates();
10
- const id = 'test-id' as RawCoID;
9
+ const id = "test-id" as RawCoID;
11
10
  const knownState: CoValueKnownState = emptyKnownState(id);
12
11
 
13
- peerKnownStates.dispatch({ type: 'SET', id, value: knownState });
12
+ peerKnownStates.dispatch({ type: "SET", id, value: knownState });
14
13
 
15
14
  expect(peerKnownStates.get(id)).toEqual(knownState);
16
15
  expect(peerKnownStates.has(id)).toBe(true);
17
16
  });
18
17
 
19
- test('should update header', () => {
18
+ test("should update header", () => {
20
19
  const peerKnownStates = new PeerKnownStates();
21
- const id = 'test-id' as RawCoID;
20
+ const id = "test-id" as RawCoID;
22
21
 
23
- peerKnownStates.dispatch({ type: 'UPDATE_HEADER', id, header: true });
22
+ peerKnownStates.dispatch({ type: "UPDATE_HEADER", id, header: true });
24
23
 
25
24
  const result = peerKnownStates.get(id);
26
25
  expect(result?.header).toBe(true);
27
26
  });
28
27
 
29
- test('should update session counter', () => {
28
+ test("should update session counter", () => {
30
29
  const peerKnownStates = new PeerKnownStates();
31
- const id = 'test-id' as RawCoID;
32
- const sessionId = 'session-1' as SessionID;
30
+ const id = "test-id" as RawCoID;
31
+ const sessionId = "session-1" as SessionID;
33
32
 
34
- peerKnownStates.dispatch({ type: 'UPDATE_SESSION_COUNTER', id, sessionId, value: 5 });
33
+ peerKnownStates.dispatch({
34
+ type: "UPDATE_SESSION_COUNTER",
35
+ id,
36
+ sessionId,
37
+ value: 5,
38
+ });
35
39
 
36
40
  const result = peerKnownStates.get(id);
37
41
  expect(result?.sessions[sessionId]).toBe(5);
38
42
  });
39
43
 
40
- test('should combine with existing state', () => {
44
+ test("should combine with existing state", () => {
41
45
  const peerKnownStates = new PeerKnownStates();
42
- const id = 'test-id' as RawCoID;
43
- const session1 = 'session-1' as SessionID;
44
- const session2 = 'session-2' as SessionID;
46
+ const id = "test-id" as RawCoID;
47
+ const session1 = "session-1" as SessionID;
48
+ const session2 = "session-2" as SessionID;
45
49
  const initialState: CoValueKnownState = {
46
50
  ...emptyKnownState(id),
47
51
  sessions: { [session1]: 5 },
@@ -51,50 +55,50 @@ describe('PeerKnownStates', () => {
51
55
  sessions: { [session2]: 10 },
52
56
  };
53
57
 
54
- peerKnownStates.dispatch({ type: 'SET', id, value: initialState });
55
- peerKnownStates.dispatch({ type: 'COMBINE_WITH', id, value: combineState });
58
+ peerKnownStates.dispatch({ type: "SET", id, value: initialState });
59
+ peerKnownStates.dispatch({ type: "COMBINE_WITH", id, value: combineState });
56
60
 
57
61
  const result = peerKnownStates.get(id);
58
62
  expect(result?.sessions).toEqual({ [session1]: 5, [session2]: 10 });
59
63
  });
60
64
 
61
- test('should set as empty', () => {
65
+ test("should set as empty", () => {
62
66
  const peerKnownStates = new PeerKnownStates();
63
- const id = 'test-id' as RawCoID;
64
- const sessionId = 'session-1' as SessionID;
67
+ const id = "test-id" as RawCoID;
68
+ const sessionId = "session-1" as SessionID;
65
69
  const initialState: CoValueKnownState = {
66
70
  ...emptyKnownState(id),
67
71
  sessions: { [sessionId]: 5 },
68
72
  };
69
73
 
70
- peerKnownStates.dispatch({ type: 'SET', id, value: initialState });
71
- peerKnownStates.dispatch({ type: 'SET_AS_EMPTY', id });
74
+ peerKnownStates.dispatch({ type: "SET", id, value: initialState });
75
+ peerKnownStates.dispatch({ type: "SET_AS_EMPTY", id });
72
76
 
73
77
  const result = peerKnownStates.get(id);
74
78
  expect(result).toEqual(emptyKnownState(id));
75
79
  });
76
80
 
77
- test('should trigger listeners on dispatch', () => {
81
+ test("should trigger listeners on dispatch", () => {
78
82
  const peerKnownStates = new PeerKnownStates();
79
- const id = 'test-id' as RawCoID;
83
+ const id = "test-id" as RawCoID;
80
84
  const listener = vi.fn();
81
85
 
82
86
  peerKnownStates.subscribe(listener);
83
- peerKnownStates.dispatch({ type: 'SET_AS_EMPTY', id });
87
+ peerKnownStates.dispatch({ type: "SET_AS_EMPTY", id });
84
88
 
85
89
  expect(listener).toHaveBeenCalledWith(id, emptyKnownState(id));
86
90
  });
87
91
 
88
- test('should unsubscribe listener', () => {
92
+ test("should unsubscribe listener", () => {
89
93
  const peerKnownStates = new PeerKnownStates();
90
- const id = 'test-id' as RawCoID;
94
+ const id = "test-id" as RawCoID;
91
95
  const listener = vi.fn();
92
96
 
93
97
  const unsubscribe = peerKnownStates.subscribe(listener);
94
98
  unsubscribe();
95
99
 
96
- peerKnownStates.dispatch({ type: 'SET_AS_EMPTY', id });
100
+ peerKnownStates.dispatch({ type: "SET_AS_EMPTY", id });
97
101
 
98
102
  expect(listener).not.toHaveBeenCalled();
99
103
  });
100
- });
104
+ });
@@ -1,81 +1,118 @@
1
- import { describe, test, expect, vi } from "vitest";
1
+ import { describe, expect, test, vi } from "vitest";
2
2
  import { PeerState } from "../PeerState.js";
3
- import { Peer, SyncMessage } from "../sync.js";
4
3
  import { CO_VALUE_PRIORITY } from "../priority.js";
4
+ import { Peer, SyncMessage } from "../sync.js";
5
5
 
6
6
  function setup() {
7
- const mockPeer: Peer = {
8
- id: "test-peer",
9
- role: "peer",
10
- priority: 1,
11
- crashOnClose: false,
12
- incoming: (async function* () {})(),
13
- outgoing: {
14
- push: vi.fn().mockResolvedValue(undefined),
15
- close: vi.fn(),
16
- },
17
- };
18
- const peerState = new PeerState(mockPeer);
19
- return { mockPeer, peerState };
7
+ const mockPeer: Peer = {
8
+ id: "test-peer",
9
+ role: "peer",
10
+ priority: 1,
11
+ crashOnClose: false,
12
+ incoming: (async function* () {})(),
13
+ outgoing: {
14
+ push: vi.fn().mockResolvedValue(undefined),
15
+ close: vi.fn(),
16
+ },
17
+ };
18
+ const peerState = new PeerState(mockPeer);
19
+ return { mockPeer, peerState };
20
20
  }
21
21
 
22
22
  describe("PeerState", () => {
23
- test("should push outgoing message to peer", async () => {
24
- const { mockPeer, peerState } = setup();
25
- const message: SyncMessage = { action: "load", id: "co_ztest-id", header: false, sessions: {} };
26
- await peerState.pushOutgoingMessage(message);
27
- expect(mockPeer.outgoing.push).toHaveBeenCalledWith(message);
28
- });
23
+ test("should push outgoing message to peer", async () => {
24
+ const { mockPeer, peerState } = setup();
25
+ const message: SyncMessage = {
26
+ action: "load",
27
+ id: "co_ztest-id",
28
+ header: false,
29
+ sessions: {},
30
+ };
31
+ await peerState.pushOutgoingMessage(message);
32
+ expect(mockPeer.outgoing.push).toHaveBeenCalledWith(message);
33
+ });
29
34
 
30
- test("should return peer's incoming when not closed", () => {
31
- const { mockPeer, peerState } = setup();
32
- expect(peerState.incoming).toBe(mockPeer.incoming);
33
- });
35
+ test("should return peer's incoming when not closed", () => {
36
+ const { mockPeer, peerState } = setup();
37
+ expect(peerState.incoming).toBe(mockPeer.incoming);
38
+ });
34
39
 
35
- test("should return Disconnected when closed", async () => {
36
- const { peerState } = setup();
37
- peerState.gracefulShutdown();
38
- const incomingIterator = peerState.incoming[Symbol.asyncIterator]();
39
- const { value, done } = await incomingIterator.next();
40
- expect(value).toBe("Disconnected");
41
- expect(done).toBe(false);
42
- });
40
+ test("should return Disconnected when closed", async () => {
41
+ const { peerState } = setup();
42
+ peerState.gracefulShutdown();
43
+ const incomingIterator = peerState.incoming[Symbol.asyncIterator]();
44
+ const { value, done } = await incomingIterator.next();
45
+ expect(value).toBe("Disconnected");
46
+ expect(done).toBe(false);
47
+ });
43
48
 
44
- test("should perform graceful shutdown", () => {
45
- const { mockPeer, peerState } = setup();
46
- const consoleSpy = vi.spyOn(console, "debug").mockImplementation(() => {});
47
- peerState.gracefulShutdown();
48
- expect(mockPeer.outgoing.close).toHaveBeenCalled();
49
- expect(peerState.closed).toBe(true);
50
- expect(consoleSpy).toHaveBeenCalledWith("Gracefully closing", "test-peer");
51
- consoleSpy.mockRestore();
52
- });
49
+ test("should perform graceful shutdown", () => {
50
+ const { mockPeer, peerState } = setup();
51
+ const consoleSpy = vi.spyOn(console, "debug").mockImplementation(() => {});
52
+ peerState.gracefulShutdown();
53
+ expect(mockPeer.outgoing.close).toHaveBeenCalled();
54
+ expect(peerState.closed).toBe(true);
55
+ expect(consoleSpy).toHaveBeenCalledWith("Gracefully closing", "test-peer");
56
+ consoleSpy.mockRestore();
57
+ });
53
58
 
54
- test("should schedule outgoing messages based on their priority", async () => {
55
- const { peerState } = setup();
59
+ test("should schedule outgoing messages based on their priority", async () => {
60
+ const { peerState } = setup();
56
61
 
57
- const loadMessage: SyncMessage = { action: "load", id: "co_zhigh", header: false, sessions: {} };
58
- const contentMessageHigh: SyncMessage = { action: "content", id: "co_zhigh", new: {}, priority: CO_VALUE_PRIORITY.HIGH };
59
- const contentMessageMid: SyncMessage = { action: "content", id: "co_zmid", new: {}, priority: CO_VALUE_PRIORITY.MEDIUM };
60
- const contentMessageLow: SyncMessage = { action: "content", id: "co_zlow", new: {}, priority: CO_VALUE_PRIORITY.LOW };
62
+ const loadMessage: SyncMessage = {
63
+ action: "load",
64
+ id: "co_zhigh",
65
+ header: false,
66
+ sessions: {},
67
+ };
68
+ const contentMessageHigh: SyncMessage = {
69
+ action: "content",
70
+ id: "co_zhigh",
71
+ new: {},
72
+ priority: CO_VALUE_PRIORITY.HIGH,
73
+ };
74
+ const contentMessageMid: SyncMessage = {
75
+ action: "content",
76
+ id: "co_zmid",
77
+ new: {},
78
+ priority: CO_VALUE_PRIORITY.MEDIUM,
79
+ };
80
+ const contentMessageLow: SyncMessage = {
81
+ action: "content",
82
+ id: "co_zlow",
83
+ new: {},
84
+ priority: CO_VALUE_PRIORITY.LOW,
85
+ };
61
86
 
62
- const promises = [
63
- peerState.pushOutgoingMessage(contentMessageLow),
64
- peerState.pushOutgoingMessage(contentMessageMid),
65
- peerState.pushOutgoingMessage(contentMessageHigh),
66
- peerState.pushOutgoingMessage(loadMessage),
67
- ];
87
+ const promises = [
88
+ peerState.pushOutgoingMessage(contentMessageLow),
89
+ peerState.pushOutgoingMessage(contentMessageMid),
90
+ peerState.pushOutgoingMessage(contentMessageHigh),
91
+ peerState.pushOutgoingMessage(loadMessage),
92
+ ];
68
93
 
69
- await Promise.all(promises);
94
+ await Promise.all(promises);
70
95
 
71
- // The first message is pushed directly, the other three are queued because are waiting
72
- // for the first push to be completed.
73
- expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(1, contentMessageLow);
96
+ // The first message is pushed directly, the other three are queued because are waiting
97
+ // for the first push to be completed.
98
+ expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(
99
+ 1,
100
+ contentMessageLow,
101
+ );
74
102
 
75
- // Load message are managed as high priority messages and having the same priority as the content message
76
- // they follow the push order.
77
- expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(2, contentMessageHigh);
78
- expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(3, loadMessage);
79
- expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(4, contentMessageMid);
80
- });
103
+ // Load message are managed as high priority messages and having the same priority as the content message
104
+ // they follow the push order.
105
+ expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(
106
+ 2,
107
+ contentMessageHigh,
108
+ );
109
+ expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(
110
+ 3,
111
+ loadMessage,
112
+ );
113
+ expect(peerState["peer"].outgoing.push).toHaveBeenNthCalledWith(
114
+ 4,
115
+ contentMessageMid,
116
+ );
117
+ });
81
118
  });
@@ -1,111 +1,111 @@
1
- import { describe, test, expect } from "vitest";
1
+ import { describe, expect, test } from "vitest";
2
2
  import { PriorityBasedMessageQueue } from "../PriorityBasedMessageQueue.js";
3
- import { SyncMessage } from "../sync.js";
4
3
  import { CO_VALUE_PRIORITY } from "../priority.js";
4
+ import { SyncMessage } from "../sync.js";
5
5
 
6
6
  function setup() {
7
- const queue = new PriorityBasedMessageQueue(CO_VALUE_PRIORITY.MEDIUM);
8
- return { queue };
7
+ const queue = new PriorityBasedMessageQueue(CO_VALUE_PRIORITY.MEDIUM);
8
+ return { queue };
9
9
  }
10
10
 
11
11
  describe("PriorityBasedMessageQueue", () => {
12
- test("should initialize with correct properties", () => {
13
- const { queue } = setup();
14
- expect(queue["defaultPriority"]).toBe(CO_VALUE_PRIORITY.MEDIUM);
15
- expect(queue["queues"].length).toBe(8);
16
- expect(queue["queues"].every((q) => q.length === 0)).toBe(true);
17
- });
12
+ test("should initialize with correct properties", () => {
13
+ const { queue } = setup();
14
+ expect(queue["defaultPriority"]).toBe(CO_VALUE_PRIORITY.MEDIUM);
15
+ expect(queue["queues"].length).toBe(8);
16
+ expect(queue["queues"].every((q) => q.length === 0)).toBe(true);
17
+ });
18
18
 
19
- test("should push message with default priority", async () => {
20
- const { queue } = setup();
21
- const message: SyncMessage = {
22
- action: "load",
23
- id: "co_ztest-id",
24
- header: false,
25
- sessions: {},
26
- };
27
- void queue.push(message);
28
- const pulledEntry = queue.pull();
29
- expect(pulledEntry?.msg).toEqual(message);
30
- });
19
+ test("should push message with default priority", async () => {
20
+ const { queue } = setup();
21
+ const message: SyncMessage = {
22
+ action: "load",
23
+ id: "co_ztest-id",
24
+ header: false,
25
+ sessions: {},
26
+ };
27
+ void queue.push(message);
28
+ const pulledEntry = queue.pull();
29
+ expect(pulledEntry?.msg).toEqual(message);
30
+ });
31
31
 
32
- test("should push message with specified priority", async () => {
33
- const { queue } = setup();
34
- const message: SyncMessage = {
35
- action: "content",
36
- id: "co_zhigh",
37
- new: {},
38
- priority: CO_VALUE_PRIORITY.HIGH,
39
- };
40
- void queue.push(message);
41
- const pulledEntry = queue.pull();
42
- expect(pulledEntry?.msg).toEqual(message);
43
- });
32
+ test("should push message with specified priority", async () => {
33
+ const { queue } = setup();
34
+ const message: SyncMessage = {
35
+ action: "content",
36
+ id: "co_zhigh",
37
+ new: {},
38
+ priority: CO_VALUE_PRIORITY.HIGH,
39
+ };
40
+ void queue.push(message);
41
+ const pulledEntry = queue.pull();
42
+ expect(pulledEntry?.msg).toEqual(message);
43
+ });
44
44
 
45
- test("should pull messages in priority order", async () => {
46
- const { queue } = setup();
47
- const lowPriorityMsg: SyncMessage = {
48
- action: "content",
49
- id: "co_zlow",
50
- new: {},
51
- priority: CO_VALUE_PRIORITY.LOW,
52
- };
53
- const mediumPriorityMsg: SyncMessage = {
54
- action: "content",
55
- id: "co_zmedium",
56
- new: {},
57
- priority: CO_VALUE_PRIORITY.MEDIUM,
58
- };
59
- const highPriorityMsg: SyncMessage = {
60
- action: "content",
61
- id: "co_zhigh",
62
- new: {},
63
- priority: CO_VALUE_PRIORITY.HIGH,
64
- };
45
+ test("should pull messages in priority order", async () => {
46
+ const { queue } = setup();
47
+ const lowPriorityMsg: SyncMessage = {
48
+ action: "content",
49
+ id: "co_zlow",
50
+ new: {},
51
+ priority: CO_VALUE_PRIORITY.LOW,
52
+ };
53
+ const mediumPriorityMsg: SyncMessage = {
54
+ action: "content",
55
+ id: "co_zmedium",
56
+ new: {},
57
+ priority: CO_VALUE_PRIORITY.MEDIUM,
58
+ };
59
+ const highPriorityMsg: SyncMessage = {
60
+ action: "content",
61
+ id: "co_zhigh",
62
+ new: {},
63
+ priority: CO_VALUE_PRIORITY.HIGH,
64
+ };
65
65
 
66
- void queue.push(lowPriorityMsg);
67
- void queue.push(mediumPriorityMsg);
68
- void queue.push(highPriorityMsg);
66
+ void queue.push(lowPriorityMsg);
67
+ void queue.push(mediumPriorityMsg);
68
+ void queue.push(highPriorityMsg);
69
69
 
70
- expect(queue.pull()?.msg).toEqual(highPriorityMsg);
71
- expect(queue.pull()?.msg).toEqual(mediumPriorityMsg);
72
- expect(queue.pull()?.msg).toEqual(lowPriorityMsg);
73
- });
70
+ expect(queue.pull()?.msg).toEqual(highPriorityMsg);
71
+ expect(queue.pull()?.msg).toEqual(mediumPriorityMsg);
72
+ expect(queue.pull()?.msg).toEqual(lowPriorityMsg);
73
+ });
74
74
 
75
- test("should return undefined when pulling from empty queue", () => {
76
- const { queue } = setup();
77
- expect(queue.pull()).toBeUndefined();
78
- });
75
+ test("should return undefined when pulling from empty queue", () => {
76
+ const { queue } = setup();
77
+ expect(queue.pull()).toBeUndefined();
78
+ });
79
79
 
80
- test("should resolve promise when message is pulled", async () => {
81
- const { queue } = setup();
82
- const message: SyncMessage = {
83
- action: "load",
84
- id: "co_ztest-id",
85
- header: false,
86
- sessions: {},
87
- };
88
- const pushPromise = queue.push(message);
80
+ test("should resolve promise when message is pulled", async () => {
81
+ const { queue } = setup();
82
+ const message: SyncMessage = {
83
+ action: "load",
84
+ id: "co_ztest-id",
85
+ header: false,
86
+ sessions: {},
87
+ };
88
+ const pushPromise = queue.push(message);
89
89
 
90
- const pulledEntry = queue.pull();
91
- pulledEntry?.resolve();
90
+ const pulledEntry = queue.pull();
91
+ pulledEntry?.resolve();
92
92
 
93
- await expect(pushPromise).resolves.toBeUndefined();
94
- });
93
+ await expect(pushPromise).resolves.toBeUndefined();
94
+ });
95
95
 
96
- test("should reject promise when message is rejected", async () => {
97
- const { queue } = setup();
98
- const message: SyncMessage = {
99
- action: "load",
100
- id: "co_ztest-id",
101
- header: false,
102
- sessions: {},
103
- };
104
- const pushPromise = queue.push(message);
96
+ test("should reject promise when message is rejected", async () => {
97
+ const { queue } = setup();
98
+ const message: SyncMessage = {
99
+ action: "load",
100
+ id: "co_ztest-id",
101
+ header: false,
102
+ sessions: {},
103
+ };
104
+ const pushPromise = queue.push(message);
105
105
 
106
- const pulledEntry = queue.pull();
107
- pulledEntry?.reject(new Error("Test error"));
106
+ const pulledEntry = queue.pull();
107
+ pulledEntry?.reject(new Error("Test error"));
108
108
 
109
- await expect(pushPromise).rejects.toThrow("Test error");
110
- });
109
+ await expect(pushPromise).rejects.toThrow("Test error");
110
+ });
111
111
  });
@@ -1,76 +1,76 @@
1
1
  import { expect, test } from "vitest";
2
+ import { WasmCrypto } from "../crypto/WasmCrypto.js";
2
3
  import { LocalNode } from "../localNode.js";
3
4
  import { connectedPeers } from "../streamUtils.js";
4
- import { WasmCrypto } from "../crypto/WasmCrypto.js";
5
5
 
6
6
  const Crypto = await WasmCrypto.create();
7
7
 
8
8
  test("Can create a node while creating a new account with profile", async () => {
9
- const { node, accountID, accountSecret, sessionID } =
10
- await LocalNode.withNewlyCreatedAccount({
11
- creationProps: { name: "Hermes Puggington" },
12
- crypto: Crypto,
13
- });
14
-
15
- expect(node).not.toBeNull();
16
- expect(accountID).not.toBeNull();
17
- expect(accountSecret).not.toBeNull();
18
- expect(sessionID).not.toBeNull();
19
-
20
- expect(node.expectProfileLoaded(accountID).get("name")).toEqual(
21
- "Hermes Puggington",
22
- );
23
- });
24
-
25
- test("A node with an account can create groups and and objects within them", async () => {
26
- const { node, accountID } = await LocalNode.withNewlyCreatedAccount({
27
- creationProps: { name: "Hermes Puggington" },
28
- crypto: Crypto,
9
+ const { node, accountID, accountSecret, sessionID } =
10
+ await LocalNode.withNewlyCreatedAccount({
11
+ creationProps: { name: "Hermes Puggington" },
12
+ crypto: Crypto,
29
13
  });
30
14
 
31
- const group = await node.createGroup();
32
- expect(group).not.toBeNull();
15
+ expect(node).not.toBeNull();
16
+ expect(accountID).not.toBeNull();
17
+ expect(accountSecret).not.toBeNull();
18
+ expect(sessionID).not.toBeNull();
19
+
20
+ expect(node.expectProfileLoaded(accountID).get("name")).toEqual(
21
+ "Hermes Puggington",
22
+ );
23
+ });
33
24
 
34
- const map = group.createMap();
35
- map.set("foo", "bar", "private");
36
- expect(map.get("foo")).toEqual("bar");
37
- expect(map.lastEditAt("foo")?.by).toEqual(accountID);
25
+ test("A node with an account can create groups and and objects within them", async () => {
26
+ const { node, accountID } = await LocalNode.withNewlyCreatedAccount({
27
+ creationProps: { name: "Hermes Puggington" },
28
+ crypto: Crypto,
29
+ });
30
+
31
+ const group = await node.createGroup();
32
+ expect(group).not.toBeNull();
33
+
34
+ const map = group.createMap();
35
+ map.set("foo", "bar", "private");
36
+ expect(map.get("foo")).toEqual("bar");
37
+ expect(map.lastEditAt("foo")?.by).toEqual(accountID);
38
38
  });
39
39
 
40
40
  test("Can create account with one node, and then load it on another", async () => {
41
- const { node, accountID, accountSecret } =
42
- await LocalNode.withNewlyCreatedAccount({
43
- creationProps: { name: "Hermes Puggington" },
44
- crypto: Crypto,
45
- });
46
-
47
- const group = await node.createGroup();
48
- expect(group).not.toBeNull();
49
-
50
- const map = group.createMap();
51
- map.set("foo", "bar", "private");
52
- expect(map.get("foo")).toEqual("bar");
53
-
54
- const [node1asPeer, node2asPeer] = connectedPeers("node1", "node2", {
55
- trace: true,
56
- peer1role: "server",
57
- peer2role: "client",
58
- })
59
-
60
- console.log("After connected peers");
61
-
62
- node.syncManager.addPeer(node2asPeer);
63
-
64
- const node2 = await LocalNode.withLoadedAccount({
65
- accountID,
66
- accountSecret,
67
- sessionID: Crypto.newRandomSessionID(accountID),
68
- peersToLoadFrom: [node1asPeer],
69
- crypto: Crypto,
41
+ const { node, accountID, accountSecret } =
42
+ await LocalNode.withNewlyCreatedAccount({
43
+ creationProps: { name: "Hermes Puggington" },
44
+ crypto: Crypto,
70
45
  });
71
46
 
72
- const map2 = await node2.load(map.id);
73
- if (map2 === "unavailable") throw new Error("Map unavailable");
47
+ const group = await node.createGroup();
48
+ expect(group).not.toBeNull();
49
+
50
+ const map = group.createMap();
51
+ map.set("foo", "bar", "private");
52
+ expect(map.get("foo")).toEqual("bar");
53
+
54
+ const [node1asPeer, node2asPeer] = connectedPeers("node1", "node2", {
55
+ trace: true,
56
+ peer1role: "server",
57
+ peer2role: "client",
58
+ });
59
+
60
+ console.log("After connected peers");
61
+
62
+ node.syncManager.addPeer(node2asPeer);
63
+
64
+ const node2 = await LocalNode.withLoadedAccount({
65
+ accountID,
66
+ accountSecret,
67
+ sessionID: Crypto.newRandomSessionID(accountID),
68
+ peersToLoadFrom: [node1asPeer],
69
+ crypto: Crypto,
70
+ });
71
+
72
+ const map2 = await node2.load(map.id);
73
+ if (map2 === "unavailable") throw new Error("Map unavailable");
74
74
 
75
- expect(map2.get("foo")).toEqual("bar");
75
+ expect(map2.get("foo")).toEqual("bar");
76
76
  });