|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { LoroDoc, LoroMap, LoroList } from "loro-crdt"; |
| 3 | +import { Mirror, schema, toNormalizedJson } from "../../src"; |
| 4 | + |
| 5 | +describe("setState consistency with null fields in LoroMap", () => { |
| 6 | + it("does not diverge when a loro-map field contains null and checkStateConsistency is enabled", async () => { |
| 7 | + const withSchema = schema({ |
| 8 | + m: schema.LoroMap({ |
| 9 | + nested: schema.LoroMap({}), |
| 10 | + }), |
| 11 | + }); |
| 12 | + |
| 13 | + const doc = new LoroDoc(); |
| 14 | + const m = doc.getMap("m"); |
| 15 | + // Pre-populate a null field inside a loro map |
| 16 | + m.set("nested", null); |
| 17 | + doc.commit(); |
| 18 | + await Promise.resolve(); |
| 19 | + |
| 20 | + const mirror = new Mirror({ |
| 21 | + schema: withSchema, |
| 22 | + doc, |
| 23 | + // rely on doc state; just enable consistency check |
| 24 | + checkStateConsistency: true, |
| 25 | + }); |
| 26 | + |
| 27 | + // Sanity: mirror picks up the null from doc |
| 28 | + expect(mirror.getState()).toEqual(toNormalizedJson(doc)); |
| 29 | + console.log(JSON.stringify(doc.toJSON(), null, 2)); |
| 30 | + |
| 31 | + // Update another field (unrelated) to force a diff run |
| 32 | + expect(() => { |
| 33 | + mirror.setState((s) => { |
| 34 | + // write a new primitive field alongside nested |
| 35 | + (s as any).m["other"] = 1; |
| 36 | + }); |
| 37 | + }).not.toThrow(); |
| 38 | + |
| 39 | + // State remains in sync with doc |
| 40 | + expect(mirror.getState()).toEqual(toNormalizedJson(doc)); |
| 41 | + // And the original null is preserved |
| 42 | + expect((mirror.getState() as any).m.nested).toBeNull(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("remains stable when the null lives deeper (list -> map) and we no-op setState", async () => { |
| 46 | + const withSchema = schema({ |
| 47 | + root: schema.LoroMap({ |
| 48 | + list: schema.LoroList( |
| 49 | + schema.LoroMap({ child: schema.LoroMap({}) }), |
| 50 | + ), |
| 51 | + }), |
| 52 | + }); |
| 53 | + |
| 54 | + const doc = new LoroDoc(); |
| 55 | + const root = doc.getMap("root"); |
| 56 | + // Attach a new list container under the root map, not the root-level list |
| 57 | + // to avoid introducing an unexpected top-level key for validation. |
| 58 | + const list = root.setContainer("list", new LoroList()); |
| 59 | + const item = list.pushContainer(new LoroMap()); |
| 60 | + // child is a loro-map by schema, but currently null in the doc JSON |
| 61 | + item.set("child", null); |
| 62 | + |
| 63 | + const mirror = new Mirror({ |
| 64 | + schema: withSchema, |
| 65 | + doc, |
| 66 | + checkStateConsistency: true, |
| 67 | + }); |
| 68 | + |
| 69 | + // No-op update should not throw or change representation |
| 70 | + expect(() => { |
| 71 | + mirror.setState((s) => s); |
| 72 | + }).not.toThrow(); |
| 73 | + expect(mirror.getState()).toEqual(toNormalizedJson(doc)); |
| 74 | + expect((mirror.getState() as any).root.list[0].child).toBeNull(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments