cojson 0.9.0 β†’ 0.9.9

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,133 @@
1
+ import { afterEach, expect, test, vi } from "vitest";
2
+ import { expectPlainText } from "../coValue.js";
3
+ import { WasmCrypto } from "../crypto/WasmCrypto.js";
4
+ import { LocalNode } from "../localNode.js";
5
+ import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
6
+
7
+ const Crypto = await WasmCrypto.create();
8
+
9
+ afterEach(() => void vi.unstubAllGlobals());
10
+
11
+ test("should throw on creation if Intl.Segmenter is not available", () => {
12
+ vi.stubGlobal("Intl", {
13
+ Segmenter: undefined,
14
+ });
15
+
16
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
17
+ const group = node.createGroup();
18
+ expect(() => group.createPlainText()).toThrow(
19
+ "Intl.Segmenter is not supported. Use a polyfill to get coPlainText support in Jazz. (eg. https://formatjs.github.io/docs/polyfills/intl-segmenter/)",
20
+ );
21
+ });
22
+
23
+ test("Empty CoPlainText works", () => {
24
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
25
+
26
+ const coValue = node.createCoValue({
27
+ type: "coplaintext",
28
+ ruleset: { type: "unsafeAllowAll" },
29
+ meta: null,
30
+ ...Crypto.createdNowUnique(),
31
+ });
32
+
33
+ const content = expectPlainText(coValue.getCurrentContent());
34
+
35
+ expect(content.type).toEqual("coplaintext");
36
+ expect(content.toString()).toEqual("");
37
+ });
38
+
39
+ test("Can insert into empty CoPlainText", () => {
40
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
41
+
42
+ const coValue = node.createCoValue({
43
+ type: "coplaintext",
44
+ ruleset: { type: "unsafeAllowAll" },
45
+ meta: null,
46
+ ...Crypto.createdNowUnique(),
47
+ });
48
+
49
+ const content = expectPlainText(coValue.getCurrentContent());
50
+
51
+ expect(content.type).toEqual("coplaintext");
52
+
53
+ content.insertAfter(0, "a", "trusting");
54
+ expect(content.toString()).toEqual("a");
55
+ });
56
+
57
+ test("Can insert and delete in CoPlainText", () => {
58
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
59
+
60
+ const coValue = node.createCoValue({
61
+ type: "coplaintext",
62
+ ruleset: { type: "unsafeAllowAll" },
63
+ meta: null,
64
+ ...Crypto.createdNowUnique(),
65
+ });
66
+
67
+ const content = expectPlainText(coValue.getCurrentContent());
68
+
69
+ expect(content.type).toEqual("coplaintext");
70
+
71
+ content.insertAfter(0, "hello", "trusting");
72
+ expect(content.toString()).toEqual("hello");
73
+
74
+ content.insertAfter(5, " world", "trusting");
75
+ expect(content.toString()).toEqual("hello world");
76
+
77
+ content.insertAfter(0, "Hello, ", "trusting");
78
+ expect(content.toString()).toEqual("Hello, hello world");
79
+
80
+ console.log("first delete");
81
+ content.deleteRange({ from: 6, to: 12 }, "trusting");
82
+ expect(content.toString()).toEqual("Hello, world");
83
+
84
+ content.insertAfter(2, "😍", "trusting");
85
+ expect(content.toString()).toEqual("He😍llo, world");
86
+
87
+ console.log("second delete");
88
+ content.deleteRange({ from: 2, to: 4 }, "trusting");
89
+ expect(content.toString()).toEqual("Hello, world");
90
+ });
91
+
92
+ test("Multiple items appended after start appear in correct order", () => {
93
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
94
+
95
+ const coValue = node.createCoValue({
96
+ type: "coplaintext",
97
+ ruleset: { type: "unsafeAllowAll" },
98
+ meta: null,
99
+ ...Crypto.createdNowUnique(),
100
+ });
101
+
102
+ const content = expectPlainText(coValue.getCurrentContent());
103
+
104
+ // Add multiple items in a single transaction, all after start
105
+ content.insertAfter(0, "h", "trusting");
106
+ content.insertAfter(1, "e", "trusting");
107
+ content.insertAfter(2, "y", "trusting");
108
+
109
+ // They should appear in insertion order (hey), not reversed (yeh)
110
+ expect(content.toString()).toEqual("hey");
111
+ });
112
+
113
+ test("Items inserted at start appear with latest first", () => {
114
+ const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
115
+
116
+ const coValue = node.createCoValue({
117
+ type: "coplaintext",
118
+ ruleset: { type: "unsafeAllowAll" },
119
+ meta: null,
120
+ ...Crypto.createdNowUnique(),
121
+ });
122
+
123
+ const content = expectPlainText(coValue.getCurrentContent());
124
+
125
+ // Insert multiple items at the start
126
+ content.insertAfter(0, "first", "trusting");
127
+ content.insertAfter(0, "second", "trusting");
128
+ content.insertAfter(0, "third", "trusting");
129
+
130
+ // They should appear in reverse chronological order
131
+ // because newer items should appear before older items
132
+ expect(content.toString()).toEqual("thirdsecondfirst");
133
+ });