From cb791b39e095e9ace553ca636a0fd8ac3d4978b8 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Tue, 19 Sep 2023 16:21:27 +0800 Subject: [PATCH] feat: map interoperability for yjs --- y-octo-node/package.json | 2 - y-octo-node/tests/doc.spec.mts | 1 - y-octo-node/tests/map.spec.mts | 94 +++++++++++++++++++++++++++++++++- yarn.lock | 24 ++------- 4 files changed, 95 insertions(+), 26 deletions(-) diff --git a/y-octo-node/package.json b/y-octo-node/package.json index 738a35d..eac80cf 100644 --- a/y-octo-node/package.json +++ b/y-octo-node/package.json @@ -21,12 +21,10 @@ "@napi-rs/cli": "^2.16.2", "@types/node": "^18.17.5", "@types/prompts": "^2.4.4", - "@types/uuid": "^9.0.2", "c8": "^8.0.1", "prompts": "^2.4.2", "ts-node": "^10.9.1", "typescript": "^5.1.6", - "uuid": "^9.0.0", "yjs": "^13.6.8" }, "engines": { diff --git a/y-octo-node/tests/doc.spec.mts b/y-octo-node/tests/doc.spec.mts index 2319998..800cf28 100644 --- a/y-octo-node/tests/doc.spec.mts +++ b/y-octo-node/tests/doc.spec.mts @@ -85,7 +85,6 @@ test("doc test", { concurrency: false }, async (t) => { let text = doc.getOrCreateText("text"); equal(array.length, 4); - console.log(array.toJson(), array.get(0), array.get(1), array.get(2), array.get(3) ,array.get(4)); equal(array.get(0), true); equal(array.get(1), false); equal(array.get(2), 1); diff --git a/y-octo-node/tests/map.spec.mts b/y-octo-node/tests/map.spec.mts index a0febe3..23bb5bb 100644 --- a/y-octo-node/tests/map.spec.mts +++ b/y-octo-node/tests/map.spec.mts @@ -1,7 +1,8 @@ import assert, { equal, deepEqual } from "node:assert"; import { test } from "node:test"; -import { Doc, YMap } from "../index"; +import * as Y from "yjs"; +import { Doc, YArray, YMap, YText } from "../index"; test("map test", { concurrency: false }, async (t) => { let client_id: number; @@ -40,7 +41,7 @@ test("map test", { concurrency: false }, async (t) => { equal(map.get("d"), "hello world"); }); - await t.test("sub map should can edit", () => { + await t.test("map should can be nested", () => { let map = doc.getOrCreateMap("map"); let sub = doc.createMap(); map.setMap("sub", sub); @@ -59,4 +60,93 @@ test("map test", { concurrency: false }, async (t) => { equal(sub2.get("d"), "hello world"); equal(sub2.length, 4); }); + + await t.test("y-octo to yjs compatibility test with nested type", () => { + let map = doc.getOrCreateMap("map"); + let sub_array = doc.createArray(); + let sub_map = doc.createMap(); + let sub_text = doc.createText(); + + map.setArray("array", sub_array); + map.setMap("map", sub_map); + map.setText("text", sub_text); + + sub_array.insert(0, true); + sub_array.insert(1, false); + sub_array.insert(2, 1); + sub_array.insert(3, "hello world"); + sub_map.set("a", true); + sub_map.set("b", false); + sub_map.set("c", 1); + sub_map.set("d", "hello world"); + sub_text.insert(0, "a"); + sub_text.insert(1, "b"); + sub_text.insert(2, "c"); + + let doc2 = new Y.Doc(); + Y.applyUpdate(doc2, doc.encodeUpdateV1()); + + let map2 = doc2.getMap("map"); + let sub_array2 = map2.get("array") as Y.Array; + let sub_map2 = map2.get("map") as Y.Map; + let sub_text2 = map2.get("text") as Y.Text; + + assert(sub_array2); + equal(sub_array2.length, 4); + equal(sub_array2.get(0), true); + equal(sub_array2.get(1), false); + equal(sub_array2.get(2), 1); + equal(sub_array2.get(3), "hello world"); + assert(sub_map2); + equal(sub_map2.get("a"), true); + equal(sub_map2.get("b"), false); + equal(sub_map2.get("c"), 1); + equal(sub_map2.get("d"), "hello world"); + assert(sub_text2); + equal(sub_text2.toString(), "abc"); + }); + + await t.test("yjs to y-octo compatibility test with nested type", () => { + let doc2 = new Y.Doc(); + let map2 = doc2.getMap("map"); + let sub_array2 = new Y.Array(); + let sub_map2 = new Y.Map(); + let sub_text2 = new Y.Text(); + map2.set("array", sub_array2); + map2.set("map", sub_map2); + map2.set("text", sub_text2); + + sub_array2.insert(0, [true]); + sub_array2.insert(1, [false]); + sub_array2.insert(2, [1]); + sub_array2.insert(3, ["hello world"]); + sub_map2.set("a", true); + sub_map2.set("b", false); + sub_map2.set("c", 1); + sub_map2.set("d", "hello world"); + sub_text2.insert(0, "a"); + sub_text2.insert(1, "b"); + sub_text2.insert(2, "c"); + + doc.applyUpdate(Buffer.from(Y.encodeStateAsUpdate(doc2))); + + let map = doc.getOrCreateMap("map"); + let sub_array = map.get("array"); + let sub_map = map.get("map"); + let sub_text = map.get("text"); + + assert(sub_array); + equal(sub_array.length, 4); + equal(sub_array.get(0), true); + equal(sub_array.get(1), false); + equal(sub_array.get(2), 1); + equal(sub_array.get(3), "hello world"); + assert(sub_map); + equal(sub_map.get("a"), true); + equal(sub_map.get("b"), false); + equal(sub_map.get("c"), 1); + equal(sub_map.get("d"), "hello world"); + assert(sub_text); + equal(sub_text.toString(), "abc"); + }); }); diff --git a/yarn.lock b/yarn.lock index 3fd685d..9f27f91 100644 --- a/yarn.lock +++ b/yarn.lock @@ -123,9 +123,9 @@ __metadata: linkType: hard "@types/node@npm:*": - version: 20.5.1 - resolution: "@types/node@npm:20.5.1" - checksum: 3dbe611cd67afa987102c8558ee70f848949c5dcfee5f60abc073e55c0d7b048e391bf06bb1e0dc052cb7210ca97136ac496cbaf6e89123c989de6bd125fde82 + version: 20.6.2 + resolution: "@types/node@npm:20.6.2" + checksum: 96fe5303872640a173f3fd43e289a451776ed5b8f0090094447c6790b43f23fb607eea8268af0829cef4d132e5afa0bfa4cd871aa7412e9042a414a698e9e971 languageName: node linkType: hard @@ -146,13 +146,6 @@ __metadata: languageName: node linkType: hard -"@types/uuid@npm:^9.0.2": - version: 9.0.2 - resolution: "@types/uuid@npm:9.0.2" - checksum: 1754bcf3444e1e3aeadd6e774fc328eb53bc956665e2e8fb6ec127aa8e1f43d9a224c3d22a9a6233dca8dd81a12dc7fed4d84b8876dd5ec82d40f574f7ff8b68 - languageName: node - linkType: hard - "@y-octo/cli@workspace:.": version: 0.0.0-use.local resolution: "@y-octo/cli@workspace:." @@ -172,12 +165,10 @@ __metadata: "@napi-rs/cli": ^2.16.2 "@types/node": ^18.17.5 "@types/prompts": ^2.4.4 - "@types/uuid": ^9.0.2 c8: ^8.0.1 prompts: ^2.4.2 ts-node: ^10.9.1 typescript: ^5.1.6 - uuid: ^9.0.0 yjs: ^13.6.8 languageName: unknown linkType: soft @@ -1514,15 +1505,6 @@ __metadata: languageName: node linkType: hard -"uuid@npm:^9.0.0": - version: 9.0.0 - resolution: "uuid@npm:9.0.0" - bin: - uuid: dist/bin/uuid - checksum: 8dd2c83c43ddc7e1c71e36b60aea40030a6505139af6bee0f382ebcd1a56f6cd3028f7f06ffb07f8cf6ced320b76aea275284b224b002b289f89fe89c389b028 - languageName: node - linkType: hard - "v8-compile-cache-lib@npm:^3.0.1": version: 3.0.1 resolution: "v8-compile-cache-lib@npm:3.0.1"