scooter  ·  2026-04-02

compat_test.js

 1import * as Y from "yjs";
 2import fs from "node:fs";
 3
 4const writeTestData = (fname, f) => {
 5  const name = "testdata/" + fname.replace(/\s/g, "-") + ".yjs";
 6  console.log(`writing test ${name}`);
 7
 8  const doc = f();
 9  const content = Y.encodeStateAsUpdate(doc);
10  doc.destroy();
11
12  fs.writeFileSync(name, content);
13};
14
15writeTestData("shared YText", () => {
16  const doc = new Y.Doc();
17  const yt = doc.getText("test");
18  yt.insert(0, "test text");
19  return doc;
20});
21
22writeTestData("map value types", () => {
23  const doc = new Y.Doc();
24  const ym = doc.getMap("test");
25  ym.set("string", "value");
26  ym.set("bool", true);
27  ym.set("number", 1);
28  ym.set("object", { foo: "bar", num: 1, test: true });
29  return doc;
30});
31
32writeTestData("shared YArray", () => {
33  const doc = new Y.Doc();
34  const ya = doc.getArray("test");
35  ya.push(["first", "second", "third"]);
36  ya.insert(1, ["middle"]);
37  return doc;
38});
39
40writeTestData("nested XmlElement with attrs", () => {
41  const doc = new Y.Doc();
42  const frag = doc.getXmlFragment("test");
43  
44  const div = new Y.XmlElement("div");
45  div.setAttribute("class", "container");
46  div.setAttribute("data-id", "main");
47  
48  const p = new Y.XmlElement("p");
49  p.setAttribute("style", "color:red");
50  p.insert(0, [new Y.XmlText("Hello")]);
51  
52  div.insert(0, [p]);
53  div.insert(1, [new Y.XmlText("World")]);
54  
55  frag.insert(0, [div]);
56  return doc;
57});
58
59writeTestData("mixed content array", () => {
60  const doc = new Y.Doc();
61  const ya = doc.getArray("test");
62  ya.push(["string", 42, true, null, { nested: "object" }]);
63  return doc;
64});
65
66writeTestData("nested YMap in YMap", () => {
67  const doc = new Y.Doc();
68  const parent = doc.getMap("test");
69  const child = new Y.Map();
70  child.set("key1", "value1");
71  child.set("key2", 42);
72  parent.set("child", child);
73  return doc;
74});
75
76writeTestData("nested YArray in YMap", () => {
77  const doc = new Y.Doc();
78  const parent = doc.getMap("test");
79  const child = new Y.Array();
80  child.push(["a", "b", "c"]);
81  parent.set("arr", child);
82  return doc;
83});
84
85writeTestData("nested YMap in YArray", () => {
86  const doc = new Y.Doc();
87  const arr = doc.getArray("test");
88  const item1 = new Y.Map();
89  item1.set("name", "first");
90  const item2 = new Y.Map();
91  item2.set("name", "second");
92  arr.push([item1, item2]);
93  return doc;
94});
95
96console.log("All test files written successfully");