cojson 0.7.0-alpha.29 → 0.7.0-alpha.36

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.
@@ -0,0 +1,167 @@
1
+ import { expect, test, beforeEach } from "vitest";
2
+ import { expectList, expectMap, expectStream } from "../coValue.js";
3
+ import { RawBinaryCoStream } from "../coValues/coStream.js";
4
+ import { createdNowUnique } from "../crypto.js";
5
+ import { MAX_RECOMMENDED_TX_SIZE, cojsonReady } from "../index.js";
6
+ import { LocalNode } from "../localNode.js";
7
+ import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfromSessionID.js";
8
+ import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
9
+
10
+ import { webcrypto } from "node:crypto";
11
+ if (!("crypto" in globalThis)) {
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ (globalThis as any).crypto = webcrypto;
14
+ }
15
+
16
+ beforeEach(async () => {
17
+ await cojsonReady;
18
+ });
19
+
20
+
21
+ test("Empty CoMap works", () => {
22
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID());
23
+
24
+ const coValue = node.createCoValue({
25
+ type: "comap",
26
+ ruleset: { type: "unsafeAllowAll" },
27
+ meta: null,
28
+ ...createdNowUnique(),
29
+ });
30
+
31
+ const content = expectMap(coValue.getCurrentContent());
32
+
33
+ expect(content.type).toEqual("comap");
34
+ expect([...content.keys()]).toEqual([]);
35
+ expect(content.toJSON()).toEqual({});
36
+ });
37
+
38
+ test("Can insert and delete CoMap entries in edit()", () => {
39
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID());
40
+
41
+ const coValue = node.createCoValue({
42
+ type: "comap",
43
+ ruleset: { type: "unsafeAllowAll" },
44
+ meta: null,
45
+ ...createdNowUnique(),
46
+ });
47
+
48
+ const content = expectMap(coValue.getCurrentContent());
49
+
50
+ expect(content.type).toEqual("comap");
51
+
52
+ content.set("hello", "world", "trusting");
53
+ expect(content.get("hello")).toEqual("world");
54
+ content.set("foo", "bar", "trusting");
55
+ expect(content.get("foo")).toEqual("bar");
56
+ expect([...content.keys()]).toEqual(["hello", "foo"]);
57
+ content.delete("foo", "trusting");
58
+ expect(content.get("foo")).toEqual(undefined);
59
+ expect(content.keys()).toEqual(["hello"]);
60
+ });
61
+
62
+ test("Can get CoMap entry values at different points in time", () => {
63
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID());
64
+
65
+ const coValue = node.createCoValue({
66
+ type: "comap",
67
+ ruleset: { type: "unsafeAllowAll" },
68
+ meta: null,
69
+ ...createdNowUnique(),
70
+ });
71
+
72
+ const content = expectMap(coValue.getCurrentContent());
73
+
74
+ expect(content.type).toEqual("comap");
75
+
76
+ const beforeA = Date.now();
77
+ while (Date.now() < beforeA + 10) {}
78
+ content.set("hello", "A", "trusting");
79
+ const beforeB = Date.now();
80
+ while (Date.now() < beforeB + 10) {}
81
+ content.set("hello", "B", "trusting");
82
+ const beforeC = Date.now();
83
+ while (Date.now() < beforeC + 10) {}
84
+ content.set("hello", "C", "trusting");
85
+ expect(content.get("hello")).toEqual("C");
86
+ expect(content.atTime(Date.now()).get("hello")).toEqual("C");
87
+ expect(content.atTime(beforeA).get("hello")).toEqual(undefined);
88
+ expect(content.atTime(beforeB).get("hello")).toEqual("A");
89
+ expect(content.atTime(beforeC).get("hello")).toEqual("B");
90
+ });
91
+
92
+ test("Can get all historic values of key in CoMap", () => {
93
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID());
94
+
95
+ const coValue = node.createCoValue({
96
+ type: "comap",
97
+ ruleset: { type: "unsafeAllowAll" },
98
+ meta: null,
99
+ ...createdNowUnique(),
100
+ });
101
+
102
+ const content = expectMap(coValue.getCurrentContent());
103
+
104
+ expect(content.type).toEqual("comap");
105
+
106
+ content.set("hello", "A", "trusting");
107
+ const editA = content.lastEditAt("hello");
108
+ content.set("hello", "B", "trusting");
109
+ const editB = content.lastEditAt("hello");
110
+ content.delete("hello", "trusting");
111
+ const editDel = content.lastEditAt("hello");
112
+ content.set("hello", "C", "trusting");
113
+ const editC = content.lastEditAt("hello");
114
+ expect([...content.editsAt("hello")]).toEqual([
115
+ {
116
+ tx: editA!.tx,
117
+ by: node.account.id,
118
+ value: "A",
119
+ at: editA?.at,
120
+ },
121
+ {
122
+ tx: editB!.tx,
123
+ by: node.account.id,
124
+ value: "B",
125
+ at: editB?.at,
126
+ },
127
+ {
128
+ tx: editDel!.tx,
129
+ by: node.account.id,
130
+ value: undefined,
131
+ at: editDel?.at,
132
+ },
133
+ {
134
+ tx: editC!.tx,
135
+ by: node.account.id,
136
+ value: "C",
137
+ at: editC?.at,
138
+ },
139
+ ]);
140
+ });
141
+
142
+ test("Can get last tx ID for a key in CoMap", () => {
143
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID());
144
+
145
+ const coValue = node.createCoValue({
146
+ type: "comap",
147
+ ruleset: { type: "unsafeAllowAll" },
148
+ meta: null,
149
+ ...createdNowUnique(),
150
+ });
151
+
152
+ const content = expectMap(coValue.getCurrentContent());
153
+
154
+ expect(content.type).toEqual("comap");
155
+
156
+ expect(content.lastEditAt("hello")).toEqual(undefined);
157
+ content.set("hello", "A", "trusting");
158
+ const sessionID = content.lastEditAt("hello")?.tx.sessionID;
159
+ expect(sessionID && accountOrAgentIDfromSessionID(sessionID)).toEqual(
160
+ node.account.id
161
+ );
162
+ expect(content.lastEditAt("hello")?.tx.txIndex).toEqual(0);
163
+ content.set("hello", "B", "trusting");
164
+ expect(content.lastEditAt("hello")?.tx.txIndex).toEqual(1);
165
+ content.set("hello", "C", "trusting");
166
+ expect(content.lastEditAt("hello")?.tx.txIndex).toEqual(2);
167
+ });
@@ -17,236 +17,6 @@ beforeEach(async () => {
17
17
  await cojsonReady;
18
18
  });
19
19
 
20
- test("Empty CoMap works", () => {
21
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
22
-
23
- const coValue = node.createCoValue({
24
- type: "comap",
25
- ruleset: { type: "unsafeAllowAll" },
26
- meta: null,
27
- ...createdNowUnique(),
28
- });
29
-
30
- const content = expectMap(coValue.getCurrentContent());
31
-
32
- expect(content.type).toEqual("comap");
33
- expect([...content.keys()]).toEqual([]);
34
- expect(content.toJSON()).toEqual({});
35
- });
36
-
37
- test("Can insert and delete CoMap entries in edit()", () => {
38
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
39
-
40
- const coValue = node.createCoValue({
41
- type: "comap",
42
- ruleset: { type: "unsafeAllowAll" },
43
- meta: null,
44
- ...createdNowUnique(),
45
- });
46
-
47
- const content = expectMap(coValue.getCurrentContent());
48
-
49
- expect(content.type).toEqual("comap");
50
-
51
- content.set("hello", "world", "trusting");
52
- expect(content.get("hello")).toEqual("world");
53
- content.set("foo", "bar", "trusting");
54
- expect(content.get("foo")).toEqual("bar");
55
- expect([...content.keys()]).toEqual(["hello", "foo"]);
56
- content.delete("foo", "trusting");
57
- expect(content.get("foo")).toEqual(undefined);
58
- expect(content.keys()).toEqual(["hello"]);
59
- });
60
-
61
- test("Can get CoMap entry values at different points in time", () => {
62
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
63
-
64
- const coValue = node.createCoValue({
65
- type: "comap",
66
- ruleset: { type: "unsafeAllowAll" },
67
- meta: null,
68
- ...createdNowUnique(),
69
- });
70
-
71
- const content = expectMap(coValue.getCurrentContent());
72
-
73
- expect(content.type).toEqual("comap");
74
-
75
- const beforeA = Date.now();
76
- while (Date.now() < beforeA + 10) {}
77
- content.set("hello", "A", "trusting");
78
- const beforeB = Date.now();
79
- while (Date.now() < beforeB + 10) {}
80
- content.set("hello", "B", "trusting");
81
- const beforeC = Date.now();
82
- while (Date.now() < beforeC + 10) {}
83
- content.set("hello", "C", "trusting");
84
- expect(content.get("hello")).toEqual("C");
85
- expect(content.atTime(Date.now()).get("hello")).toEqual("C");
86
- expect(content.atTime(beforeA).get("hello")).toEqual(undefined);
87
- expect(content.atTime(beforeB).get("hello")).toEqual("A");
88
- expect(content.atTime(beforeC).get("hello")).toEqual("B");
89
- });
90
-
91
- test("Can get all historic values of key in CoMap", () => {
92
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
93
-
94
- const coValue = node.createCoValue({
95
- type: "comap",
96
- ruleset: { type: "unsafeAllowAll" },
97
- meta: null,
98
- ...createdNowUnique(),
99
- });
100
-
101
- const content = expectMap(coValue.getCurrentContent());
102
-
103
- expect(content.type).toEqual("comap");
104
-
105
- content.set("hello", "A", "trusting");
106
- const editA = content.lastEditAt("hello");
107
- content.set("hello", "B", "trusting");
108
- const editB = content.lastEditAt("hello");
109
- content.delete("hello", "trusting");
110
- const editDel = content.lastEditAt("hello");
111
- content.set("hello", "C", "trusting");
112
- const editC = content.lastEditAt("hello");
113
- expect([...content.editsAt("hello")]).toEqual([
114
- {
115
- tx: editA!.tx,
116
- by: node.account.id,
117
- value: "A",
118
- at: editA?.at,
119
- },
120
- {
121
- tx: editB!.tx,
122
- by: node.account.id,
123
- value: "B",
124
- at: editB?.at,
125
- },
126
- {
127
- tx: editDel!.tx,
128
- by: node.account.id,
129
- value: undefined,
130
- at: editDel?.at,
131
- },
132
- {
133
- tx: editC!.tx,
134
- by: node.account.id,
135
- value: "C",
136
- at: editC?.at,
137
- },
138
- ]);
139
- });
140
-
141
- test("Can get last tx ID for a key in CoMap", () => {
142
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
143
-
144
- const coValue = node.createCoValue({
145
- type: "comap",
146
- ruleset: { type: "unsafeAllowAll" },
147
- meta: null,
148
- ...createdNowUnique(),
149
- });
150
-
151
- const content = expectMap(coValue.getCurrentContent());
152
-
153
- expect(content.type).toEqual("comap");
154
-
155
- expect(content.lastEditAt("hello")).toEqual(undefined);
156
- content.set("hello", "A", "trusting");
157
- const sessionID = content.lastEditAt("hello")?.tx.sessionID;
158
- expect(sessionID && accountOrAgentIDfromSessionID(sessionID)).toEqual(
159
- node.account.id
160
- );
161
- expect(content.lastEditAt("hello")?.tx.txIndex).toEqual(0);
162
- content.set("hello", "B", "trusting");
163
- expect(content.lastEditAt("hello")?.tx.txIndex).toEqual(1);
164
- content.set("hello", "C", "trusting");
165
- expect(content.lastEditAt("hello")?.tx.txIndex).toEqual(2);
166
- });
167
-
168
- test("Empty CoList works", () => {
169
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
170
-
171
- const coValue = node.createCoValue({
172
- type: "colist",
173
- ruleset: { type: "unsafeAllowAll" },
174
- meta: null,
175
- ...createdNowUnique(),
176
- });
177
-
178
- const content = expectList(coValue.getCurrentContent());
179
-
180
- expect(content.type).toEqual("colist");
181
- expect(content.toJSON()).toEqual([]);
182
- });
183
-
184
- test("Can append, prepend, delete and replace items in CoList", () => {
185
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
186
-
187
- const coValue = node.createCoValue({
188
- type: "colist",
189
- ruleset: { type: "unsafeAllowAll" },
190
- meta: null,
191
- ...createdNowUnique(),
192
- });
193
-
194
- const content = expectList(coValue.getCurrentContent());
195
-
196
- content.append("hello", 0, "trusting");
197
- expect(content.toJSON()).toEqual(["hello"]);
198
- content.append("world", 0, "trusting");
199
- expect(content.toJSON()).toEqual(["hello", "world"]);
200
- content.prepend("beautiful", 1, "trusting");
201
- expect(content.toJSON()).toEqual(["hello", "beautiful", "world"]);
202
- content.prepend("hooray", 3, "trusting");
203
- expect(content.toJSON()).toEqual(["hello", "beautiful", "world", "hooray"]);
204
- content.replace(2, "universe", "trusting");
205
- expect(content.toJSON()).toEqual(["hello", "beautiful", "universe", "hooray"]);
206
- content.delete(2, "trusting");
207
- expect(content.toJSON()).toEqual(["hello", "beautiful", "hooray"]);
208
- });
209
-
210
- test("Push is equivalent to append after last item", () => {
211
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
212
-
213
- const coValue = node.createCoValue({
214
- type: "colist",
215
- ruleset: { type: "unsafeAllowAll" },
216
- meta: null,
217
- ...createdNowUnique(),
218
- });
219
-
220
- const content = expectList(coValue.getCurrentContent());
221
-
222
- expect(content.type).toEqual("colist");
223
-
224
- content.append("hello", 0, "trusting");
225
- expect(content.toJSON()).toEqual(["hello"]);
226
- content.append("world", undefined, "trusting");
227
- expect(content.toJSON()).toEqual(["hello", "world"]);
228
- content.append("hooray", undefined, "trusting");
229
- expect(content.toJSON()).toEqual(["hello", "world", "hooray"]);
230
- });
231
-
232
- test("Can push into empty list", () => {
233
- const node = new LocalNode(...randomAnonymousAccountAndSessionID());
234
-
235
- const coValue = node.createCoValue({
236
- type: "colist",
237
- ruleset: { type: "unsafeAllowAll" },
238
- meta: null,
239
- ...createdNowUnique(),
240
- });
241
-
242
- const content = expectList(coValue.getCurrentContent());
243
-
244
- expect(content.type).toEqual("colist");
245
-
246
- content.append("hello", undefined, "trusting");
247
- expect(content.toJSON()).toEqual(["hello"]);
248
- });
249
-
250
20
  test("Empty CoStream works", () => {
251
21
  const node = new LocalNode(...randomAnonymousAccountAndSessionID());
252
22
 
@@ -483,4 +253,4 @@ test("When adding large transactions (bigger than MAX_RECOMMENDED_TX_SIZE), we s
483
253
  expect(newContent[4]!.new[node.currentSessionID]!.lastSignature).toEqual(
484
254
  sessionEntry.lastSignature
485
255
  );
486
- });
256
+ });