From 90079b2f1ca4e674e8a48861578d6c5666e3974b Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Fri, 10 Nov 2023 20:13:01 +0800 Subject: [PATCH] test: add more examples --- deno.lock | 7 +++- loro-js/src/index.ts | 37 ++++++++-------- loro-js/tests/basic.test.ts | 77 ++++++++++++++++++++++++++++++++++ loro-js/tests/checkout.test.ts | 16 +++---- 4 files changed, 108 insertions(+), 29 deletions(-) diff --git a/deno.lock b/deno.lock index b1756d65..a81388aa 100644 --- a/deno.lock +++ b/deno.lock @@ -1,5 +1,10 @@ { - "version": "2", + "version": "3", + "redirects": { + "https://deno.land/std/fmt/printf.ts": "https://deno.land/std@0.105.0/fmt/printf.ts", + "https://deno.land/std/path/mod.ts": "https://deno.land/std@0.105.0/path/mod.ts", + "https://deno.land/std/testing/asserts.ts": "https://deno.land/std@0.105.0/testing/asserts.ts" + }, "remote": { "https://deno.land/std@0.105.0/_util/assert.ts": "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58", "https://deno.land/std@0.105.0/_util/os.ts": "dfb186cc4e968c770ab6cc3288bd65f4871be03b93beecae57d657232ecffcac", diff --git a/loro-js/src/index.ts b/loro-js/src/index.ts index 49af68bb..1138dca1 100644 --- a/loro-js/src/index.ts +++ b/loro-js/src/index.ts @@ -44,24 +44,6 @@ export type Value = export type Prelim = PrelimList | PrelimMap | PrelimText; -export type Path = (number | string)[]; - -export type ListDiff = { - type: "list"; - diff: Delta[]; -}; - -export type TextDiff = { - type: "text"; - diff: Delta[]; -}; - -export type MapDiff = { - type: "map"; - updated: Record; -}; - -export type Diff = ListDiff | TextDiff | MapDiff; export interface LoroEvent { /** @@ -82,6 +64,25 @@ export interface LoroEvent { path: Path; } +export type Path = (number | string)[]; + +export type Diff = ListDiff | TextDiff | MapDiff; + +export type ListDiff = { + type: "list"; + diff: Delta[]; +}; + +export type TextDiff = { + type: "text"; + diff: Delta[]; +}; + +export type MapDiff = { + type: "map"; + updated: Record; +}; + interface Listener { (event: LoroEvent): void; } diff --git a/loro-js/tests/basic.test.ts b/loro-js/tests/basic.test.ts index f5360f3a..e9ae6a15 100644 --- a/loro-js/tests/basic.test.ts +++ b/loro-js/tests/basic.test.ts @@ -2,11 +2,88 @@ import { describe, expect, it } from "vitest"; import { ContainerID, Loro, + LoroList, + LoroMap, setPanicHook, } from "../src"; setPanicHook(); +it("basic example", () => { + const doc = new Loro(); + const list: LoroList = doc.getList("list"); + list.insert(0, "A"); + list.insert(1, "B"); + list.insert(2, "C"); + + const map: LoroMap = doc.getMap("map"); + // map can only has string key + map.set("key", "value"); + expect(doc.toJson()).toStrictEqual({ + list: ["A", "B", "C"], + map: { key: "value" } + }); + + // delete 1 element at index 0 + list.delete(0, 2) + expect(doc.toJson()).toStrictEqual({ + list: ["C"], + map: { key: "value" } + }); + + // Insert a text container to the list + const text = list.insertContainer(0, "Text"); + text.insert(0, "Hello"); + text.insert(0, "Hi! ") + + // delete 1 element at index 0 + expect(doc.toJson()).toStrictEqual({ + list: ["Hi! Hello", "C"], + map: { key: "value" } + }); + + // Insert a list container to the map + const list2 = map.setContainer("test", "List"); + list2.insert(0, 1); + expect(doc.toJson()).toStrictEqual({ + list: ["Hi! Hello", "C"], + map: { key: "value", test: [1] } + }); +}) + +it("basic sync example", () => { + const docA = new Loro(); + const docB = new Loro(); + const listA: LoroList = docA.getList("list"); + listA.insert(0, "A"); + listA.insert(1, "B"); + listA.insert(2, "C"); + // B import the ops from A + docB.import(docA.exportFrom()); + expect(docB.toJson()).toStrictEqual({ + list: ["A", "B", "C"] + }) + + const listB: LoroList = docB.getList("list"); + // delete 1 element at index 1 + listB.delete(1, 1); + // A import the ops from B + docA.import(docB.exportFrom(docA.version())) + // list at A is now ["A", "C"], with the same state as B + expect(docA.toJson()).toStrictEqual({ + list: ["A", "C"] + }); + expect(docA.toJson()).toStrictEqual(docB.toJson()); +}) + +it("basic events", () => { + const doc = new Loro(); + doc.subscribe(event => { + + }); + const list = doc.getList("list"); +}) + describe("list", () => { it("insert containers", () => { const doc = new Loro(); diff --git a/loro-js/tests/checkout.test.ts b/loro-js/tests/checkout.test.ts index c4215b64..f0bd690e 100644 --- a/loro-js/tests/checkout.test.ts +++ b/loro-js/tests/checkout.test.ts @@ -8,23 +8,19 @@ setPanicHook(); describe("Checkout", () => { it("simple checkout", () => { const doc = new Loro(); + doc.setPeerId(0n); const text = doc.getText("text"); - text.insert(0, "hello world"); + text.insert(0, "H"); doc.commit(); const v = doc.frontiers(); - text.insert(0, "000"); + text.insert(1, "i"); expect(doc.toJson()).toStrictEqual({ - text: "000hello world" + text: "Hi" }); - doc.checkout(v); + doc.checkout([{ peer: 0n, counter: 0 }]); expect(doc.toJson()).toStrictEqual({ - text: "hello world" - }); - v[0].counter -= 1; - doc.checkout(v); - expect(doc.toJson()).toStrictEqual({ - text: "hello worl" + text: "H" }); });