import { LoroDoc, LoroMap } from "loro-crdt" import { describe, expect, it } from "vitest" import { change } from "./functional-helpers.js" import { loro } from "./loro.js" import { Shape } from "./shape.js" import { createTypedDoc } from "./typed-doc.js" describe("CRDT Operations", () => { describe("Text Operations", () => { it("should handle basic text insertion and deletion", () => { const schema = Shape.doc({ title: Shape.text(), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.title.insert(0, "Hello") draft.title.insert(5, " World") draft.title.delete(0, 5) // Delete "Hello" }).toJSON() expect(result.title).toBe(" World") }) it("should handle text update (replacement)", () => { const schema = Shape.doc({ content: Shape.text(), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.content.insert(0, "Initial content") draft.content.update("Replaced content") }).toJSON() expect(result.content).toBe("Replaced content") }) it("should handle text marking and unmarking", () => { const schema = Shape.doc({ richText: Shape.text(), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.richText.insert(0, "Bold text") draft.richText.mark({ start: 0, end: 4 }, "bold", true) draft.richText.unmark({ start: 0, end: 2 }, "bold") }).toJSON() expect(result.richText).toBe("Bold text") }) it("should handle delta operations", () => { const schema = Shape.doc({ deltaText: Shape.text(), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.deltaText.insert(0, "Hello World") const delta = draft.deltaText.toDelta() expect(delta).toBeDefined() // Apply a new delta draft.deltaText.applyDelta([{ insert: "New " }]) }) // Use toJSON() to get plain values for comparison const result = typedDoc.toJSON() expect(result.deltaText).toContain("New") }) it("should provide text length property", () => { const schema = Shape.doc({ measuredText: Shape.text(), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.measuredText.insert(0, "Hello") expect(draft.measuredText.length).toBe(5) draft.measuredText.insert(5, " World") expect(draft.measuredText.length).toBe(11) }) }) }) describe("Counter Operations", () => { it("should handle increment and decrement operations", () => { const schema = Shape.doc({ count: Shape.counter(), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.count.increment(5) draft.count.decrement(2) draft.count.increment(10) }).toJSON() expect(result.count).toBe(13) // 5 - 2 + 10 = 13 }) it("should provide counter value property", () => { const schema = Shape.doc({ counter: Shape.counter(), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.counter.increment(7) expect(draft.counter.value).toBe(7) draft.counter.decrement(3) expect(draft.counter.value).toBe(4) }) }) it("should handle negative increments and decrements", () => { const schema = Shape.doc({ negativeCounter: Shape.counter(), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.negativeCounter.increment(-5) // Negative increment draft.negativeCounter.decrement(-3) // Negative decrement (adds 3) }).toJSON() expect(result.negativeCounter).toBe(-2) // -5 + 3 = -2 }) }) describe("List Operations", () => { it("should handle push, insert, and delete operations", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.items.push("first") draft.items.insert(0, "zero") draft.items.push("second") draft.items.delete(1, 1) // Delete "first" }).toJSON() expect(result.items).toEqual(["zero", "second"]) }) it("should handle list with number items", () => { const schema = Shape.doc({ numbers: Shape.list(Shape.plain.number()), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.numbers.push(1) draft.numbers.push(2) draft.numbers.insert(1, 1.5) }).toJSON() expect(result.numbers).toEqual([1, 1.5, 2]) }) it("should handle list with boolean items", () => { const schema = Shape.doc({ flags: Shape.list(Shape.plain.boolean()), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.flags.push(true) draft.flags.push(false) draft.flags.insert(1, true) }).toJSON() expect(result.flags).toEqual([true, true, false]) }) it("should provide list length and array conversion", () => { const schema = Shape.doc({ testList: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.testList.push("a") draft.testList.push("b") expect(draft.testList.length).toBe(2) expect(draft.testList.toArray()).toEqual(["a", "b"]) expect(draft.testList.get(0)).toBe("a") expect(draft.testList.get(1)).toBe("b") }) }) it("should handle container insertion", () => { const schema = Shape.doc({ containerList: Shape.list(Shape.text()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { // Note: pushContainer and insertContainer expect actual container instances // For testing purposes, we'll just verify the list exists expect(draft.containerList.length).toBe(0) }) const result = typedDoc.toJSON() expect(result.containerList).toHaveLength(0) // No containers were actually added }) it("should handle move operations on lists", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) // Add initial items change(typedDoc, draft => { draft.items.push("first") draft.items.push("second") draft.items.push("third") }) // Test move operation: move first item to the end const result = change(typedDoc, draft => { const valueToMove = draft.items.get(0) if (valueToMove !== undefined) { draft.items.delete(0, 1) draft.items.insert(2, valueToMove) } }).toJSON() expect(result.items).toEqual(["second", "third", "first"]) }) }) describe("Movable List Operations", () => { it("should handle push, insert, delete, and move operations", () => { const schema = Shape.doc({ tasks: Shape.movableList( Shape.plain.struct({ id: Shape.plain.string(), title: Shape.plain.string(), }), ), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.tasks.push({ id: "1", title: "Task 1" }) draft.tasks.push({ id: "2", title: "Task 2" }) draft.tasks.push({ id: "3", title: "Task 3" }) draft.tasks.move(0, 2) // Move first task to position 2 draft.tasks.delete(1, 1) // Delete middle task }).toJSON() expect(result.tasks).toHaveLength(2) expect(result.tasks[0]).toEqual({ id: "2", title: "Task 2" }) expect(result.tasks[1]).toEqual({ id: "1", title: "Task 1" }) }) it("should handle set operation", () => { const schema = Shape.doc({ editableList: Shape.movableList(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.editableList.push("original") draft.editableList.set(0, "modified") }).toJSON() expect(result.editableList).toEqual(["modified"]) }) it("should provide movable list properties and methods", () => { const schema = Shape.doc({ movableItems: Shape.movableList(Shape.plain.number()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.movableItems.push(10) draft.movableItems.push(20) expect(draft.movableItems.length).toBe(2) expect(draft.movableItems.get(0)).toBe(10) expect(draft.movableItems.toArray()).toEqual([10, 20]) }) }) }) describe("Map Operations", () => { it("should handle property assignment operations", () => { const schema = Shape.doc({ metadata: Shape.struct({ title: Shape.plain.string(), count: Shape.plain.number().placeholder(1), enabled: Shape.plain.boolean(), }), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.metadata.title = "Test Title" draft.metadata.count = 42 draft.metadata.enabled = true }).toJSON() expect(result.metadata.title).toBe("Test Title") expect(result.metadata.count).toBe(42) expect(result.metadata.enabled).toBe(true) }) it("should handle array values in maps", () => { const schema = Shape.doc({ config: Shape.struct({ tags: Shape.plain.array(Shape.plain.string()), numbers: Shape.plain.array(Shape.plain.number()), }), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.config.tags = ["tag1", "tag2", "tag3"] draft.config.numbers = [1, 2, 3] }).toJSON() expect(result.config.tags).toEqual(["tag1", "tag2", "tag3"]) expect(result.config.numbers).toEqual([1, 2, 3]) }) it("should provide JavaScript-native object methods", () => { const schema = Shape.doc({ testMap: Shape.struct({ key1: Shape.plain.string(), key2: Shape.plain.number(), }), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.testMap.key1 = "value1" draft.testMap.key2 = 123 expect(draft.testMap.key1).toBe("value1") expect("key1" in draft.testMap).toBe(true) // Use Object.keys() instead of .keys() expect(Object.keys(draft.testMap)).toContain("key1") expect(Object.keys(draft.testMap)).toContain("key2") expect(Object.values(draft.testMap)).toContain("value1") expect(Object.values(draft.testMap)).toContain(123) }) }) it("should handle container insertion in maps", () => { const schema = Shape.doc({ containerMap: Shape.struct({ textField: Shape.text(), }), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { // Note: setContainer expects actual container instances // For testing purposes, we'll just verify the map exists expect(draft.containerMap).toBeDefined() }) const rawValue = loro(typedDoc).rawValue as any // Since no container was actually set, containerMap might be undefined expect(rawValue.containerMap).toBeUndefined() }) }) describe("Tree Operations", () => { it("should handle basic tree operations", () => { const schema = Shape.doc({ tree: Shape.tree(Shape.struct({ name: Shape.text() })), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { const root = draft.tree.createNode() expect(root).toBeDefined() // Note: Tree operations have complex type requirements // For testing purposes, we'll just verify basic creation works expect(root.id).toBeDefined() }) }) it("should handle tree node movement and deletion", () => { const schema = Shape.doc({ hierarchy: Shape.tree(Shape.struct({ name: Shape.text() })), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { const parent1 = draft.hierarchy.createNode() const parent2 = draft.hierarchy.createNode() // Note: Tree operations have complex type requirements // For testing purposes, we'll just verify basic creation works expect(parent1.id).toBeDefined() expect(parent2.id).toBeDefined() }) }) it("should handle tree node lookup by ID", () => { const schema = Shape.doc({ searchableTree: Shape.tree(Shape.struct({ name: Shape.text() })), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { const node = draft.searchableTree.createNode() // Note: getNodeByID might not be available in all versions // For testing purposes, we'll just verify basic creation works expect(node.id).toBeDefined() }) }) }) }) describe("Nested Operations", () => { describe("Nested Maps", () => { it("should handle deeply nested map structures", () => { const schema = Shape.doc({ article: Shape.struct({ title: Shape.text(), metadata: Shape.struct({ views: Shape.counter(), author: Shape.struct({ name: Shape.plain.string(), email: Shape.plain.string(), }), }), }), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.article.title.insert(0, "Nested Article") draft.article.metadata.views.increment(10) draft.article.metadata.author.name = "John Doe" draft.article.metadata.author.email = "john@example.com" }).toJSON() expect(result.article.title).toBe("Nested Article") expect(result.article.metadata.views).toBe(10) expect(result.article.metadata.author.name).toBe("John Doe") expect(result.article.metadata.author.email).toBe("john@example.com") }) it("should handle maps with mixed Zod and Loro schemas", () => { const schema = Shape.doc({ mixed: Shape.struct({ plainString: Shape.plain.string(), plainArray: Shape.plain.array(Shape.plain.number()), loroText: Shape.text(), loroCounter: Shape.counter(), }), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.mixed.plainString = "Hello" draft.mixed.plainArray = [1, 2, 3] draft.mixed.loroText.insert(0, "Loro Text") draft.mixed.loroCounter.increment(5) }).toJSON() expect(result.mixed.plainString).toBe("Hello") expect(result.mixed.plainArray).toEqual([1, 2, 3]) expect(result.mixed.loroText).toBe("Loro Text") expect(result.mixed.loroCounter).toBe(5) }) }) describe("Lists with Complex Items", () => { it("should handle lists of maps with nested structures", () => { const schema = Shape.doc({ articles: Shape.list( Shape.struct({ title: Shape.text(), tags: Shape.list(Shape.plain.string()), metadata: Shape.struct({ views: Shape.counter(), published: Shape.plain.boolean(), }), }), ), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.articles.push({ title: "First Article", tags: ["tech", "programming"], metadata: { views: 100, published: true, }, }) draft.articles.push({ title: "Second Article", tags: ["design"], metadata: { views: 50, published: false, }, }) }).toJSON() expect(result.articles).toHaveLength(2) expect(result.articles[0].title).toBe("First Article") expect(result.articles[0].tags).toEqual(["tech", "programming"]) expect(result.articles[0].metadata.views).toBe(100) expect(result.articles[0].metadata.published).toBe(true) expect(result.articles[1].title).toBe("Second Article") }) it("should handle nested plain value maps", () => { const schema = Shape.doc({ articles: Shape.struct({ metadata: Shape.plain.struct({ views: Shape.plain.struct({ page: Shape.plain.number(), }), }), }), }) const typedDoc = createTypedDoc(schema) const result1 = change(typedDoc, draft => { // natural object access & assignment for Value nodes draft.articles.metadata.views.page = 1 }).toJSON() expect(result1).toEqual({ articles: { metadata: { views: { page: 1 } } }, }) const result2 = change(typedDoc, draft => { // natural object access & assignment for Value nodes draft.articles.metadata = { views: { page: 2 } } }).toJSON() expect(result2).toEqual({ articles: { metadata: { views: { page: 2 } } }, }) expect(loro(typedDoc).rawValue).toEqual({ articles: { metadata: { views: { page: 2 } } }, }) }) it("should handle lists of lists", () => { const schema = Shape.doc({ matrix: Shape.list(Shape.list(Shape.plain.number())), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.matrix.push([1, 2, 3]) draft.matrix.push([4, 5, 6]) }).toJSON() const correctResult = { matrix: [ [1, 2, 3], [4, 5, 6], ], } expect(result).toEqual(correctResult) expect(loro(typedDoc).rawValue).toEqual(correctResult) }) }) describe("Maps with List Values", () => { it("should handle maps containing lists", () => { const schema = Shape.doc({ categories: Shape.struct({ tech: Shape.list(Shape.plain.string()), design: Shape.list(Shape.plain.string()), }), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.categories.tech.push("JavaScript") draft.categories.tech.push("TypeScript") draft.categories.design.push("UI/UX") }).toJSON() expect(result.categories.tech).toEqual(["JavaScript", "TypeScript"]) expect(result.categories.design).toEqual(["UI/UX"]) }) }) }) describe("TypedLoroDoc", () => { describe("Empty State Overlay", () => { it("should return empty state when document is empty", () => { const schema = Shape.doc({ title: Shape.text().placeholder("Default Title"), count: Shape.counter(), items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) expect(typedDoc.toJSON()).toEqual({ title: "Default Title", count: 0, items: [], }) }) it("should overlay CRDT values over empty state", () => { const schema = Shape.doc({ title: Shape.text().placeholder("Default Title"), count: Shape.counter(), items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.title.insert(0, "Hello World") draft.count.increment(5) }).toJSON() expect(result.title).toBe("Hello World") expect(result.count).toBe(5) expect(result.items).toEqual([]) // Empty state preserved }) it("should handle nested empty state structures", () => { const schema = Shape.doc({ article: Shape.struct({ title: Shape.text().placeholder("Default Title"), metadata: Shape.struct({ views: Shape.counter(), tags: Shape.plain.array(Shape.plain.string()), author: Shape.plain.string().placeholder("Anonymous"), }), }), }) const expectedPlaceholder = { article: { title: "Default Title", metadata: { views: 0, tags: [], author: "Anonymous", }, }, } const typedDoc = createTypedDoc(schema) expect(typedDoc.toJSON()).toEqual(expectedPlaceholder) const result = change(typedDoc, draft => { draft.article.title.insert(0, "New Title") draft.article.metadata.views.increment(10) draft.article.metadata.author = "John Doe" }).toJSON() expect(result.article.title).toBe("New Title") expect(result.article.metadata.views).toBe(10) expect(result.article.metadata.tags).toEqual([]) // Preserved expect(result.article.metadata.author).toBe("John Doe") }) it("should handle empty state with optional fields", () => { const schema = Shape.doc({ profile: Shape.struct({ name: Shape.plain.string().placeholder("Anonymous"), email: Shape.plain .union([Shape.plain.null(), Shape.plain.string()]) .placeholder(null), age: Shape.plain .union([Shape.plain.null(), Shape.plain.number()]) .placeholder(null), }), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.profile.name = "John Doe" draft.profile.email = "john@example.com" }).toJSON() expect(result.profile.name).toBe("John Doe") expect(result.profile.email).toBe("john@example.com") expect(result.profile.age).toBeNull() }) describe("Nullable Builder", () => { describe("Basic nullable types", () => { it("should handle nullable string with null placeholder", () => { const schema = Shape.doc({ profile: Shape.struct({ email: Shape.plain.string().nullable(), }), }) const typedDoc = createTypedDoc(schema) // Should start with null placeholder expect(typedDoc.toJSON().profile.email).toBeNull() // Should accept string value const result = change(typedDoc, draft => { draft.profile.email = "test@example.com" }).toJSON() expect(result.profile.email).toBe("test@example.com") // Should accept null value const result2 = change(typedDoc, draft => { draft.profile.email = null }).toJSON() expect(result2.profile.email).toBeNull() }) it("should handle nullable number with null placeholder", () => { const schema = Shape.doc({ stats: Shape.struct({ age: Shape.plain.number().nullable(), }), }) const typedDoc = createTypedDoc(schema) expect(typedDoc.toJSON().stats.age).toBeNull() const result = change(typedDoc, draft => { draft.stats.age = 25 }).toJSON() expect(result.stats.age).toBe(25) }) it("should handle nullable boolean with null placeholder", () => { const schema = Shape.doc({ settings: Shape.struct({ enabled: Shape.plain.boolean().nullable(), }), }) const typedDoc = createTypedDoc(schema) expect(typedDoc.toJSON().settings.enabled).toBeNull() const result = change(typedDoc, draft => { draft.settings.enabled = true }).toJSON() expect(result.settings.enabled).toBe(true) }) it("should handle nullable record with null placeholder", () => { const schema = Shape.doc({ data: Shape.struct({ candidates: Shape.plain.record(Shape.plain.string()).nullable(), }), }) const typedDoc = createTypedDoc(schema) expect(typedDoc.toJSON().data.candidates).toBeNull() const result = change(typedDoc, draft => { draft.data.candidates = { a: "Alice", b: "Bob" } }).toJSON() expect(result.data.candidates).toEqual({ a: "Alice", b: "Bob" }) // Should accept null value const result2 = change(typedDoc, draft => { draft.data.candidates = null }).toJSON() expect(result2.data.candidates).toBeNull() }) it("should handle nullable array with null placeholder", () => { const schema = Shape.doc({ data: Shape.struct({ tags: Shape.plain.array(Shape.plain.string()).nullable(), }), }) const typedDoc = createTypedDoc(schema) expect(typedDoc.toJSON().data.tags).toBeNull() const result = change(typedDoc, draft => { draft.data.tags = ["a", "b", "c"] }).toJSON() expect(result.data.tags).toEqual(["a", "b", "c"]) // Should accept null value const result2 = change(typedDoc, draft => { draft.data.tags = null }).toJSON() expect(result2.data.tags).toBeNull() }) it("should handle nullable struct with null placeholder", () => { const schema = Shape.doc({ data: Shape.struct({ point: Shape.plain .struct({ x: Shape.plain.number(), y: Shape.plain.number(), }) .nullable(), }), }) const typedDoc = createTypedDoc(schema) expect(typedDoc.toJSON().data.point).toBeNull() const result = change(typedDoc, draft => { draft.data.point = { x: 10, y: 20 } }).toJSON() expect(result.data.point).toEqual({ x: 10, y: 20 }) // Should accept null value const result2 = change(typedDoc, draft => { draft.data.point = null }).toJSON() expect(result2.data.point).toBeNull() }) }) describe("Nullable with custom placeholder", () => { it("should allow custom placeholder after nullable()", () => { const schema = Shape.doc({ profile: Shape.struct({ name: Shape.plain.string().nullable().placeholder("Anonymous"), }), }) const typedDoc = createTypedDoc(schema) // Should use custom placeholder, not null expect(typedDoc.toJSON().profile.name).toBe("Anonymous") // Should still accept null const result = change(typedDoc, draft => { draft.profile.name = null }).toJSON() expect(result.profile.name).toBeNull() }) it("should allow number placeholder after nullable()", () => { const schema = Shape.doc({ stats: Shape.struct({ score: Shape.plain.number().nullable().placeholder(0), }), }) const typedDoc = createTypedDoc(schema) expect(typedDoc.toJSON().stats.score).toBe(0) }) }) describe("Type inference", () => { it("should infer correct types for nullable fields", () => { const schema = Shape.doc({ data: Shape.struct({ nullableString: Shape.plain.string().nullable(), nullableNumber: Shape.plain.number().nullable(), nullableBoolean: Shape.plain.boolean().nullable(), }), }) const typedDoc = createTypedDoc(schema) // TypeScript should allow these assignments change(typedDoc, draft => { draft.data.nullableString = "hello" draft.data.nullableString = null draft.data.nullableNumber = 42 draft.data.nullableNumber = null draft.data.nullableBoolean = true draft.data.nullableBoolean = null }) // Verify the types work correctly const json = typedDoc.toJSON() const str: string | null = json.data.nullableString const num: number | null = json.data.nullableNumber const bool: boolean | null = json.data.nullableBoolean expect(str).toBeNull() expect(num).toBeNull() expect(bool).toBeNull() }) }) describe("Equivalence to union pattern", () => { it("should behave identically to manual union pattern", () => { // Using nullable() const schema1 = Shape.doc({ profile: Shape.struct({ email: Shape.plain.string().nullable(), }), }) // Using manual union const schema2 = Shape.doc({ profile: Shape.struct({ email: Shape.plain .union([Shape.plain.null(), Shape.plain.string()]) .placeholder(null), }), }) const doc1 = createTypedDoc(schema1) const doc2 = createTypedDoc(schema2) // Both should have same initial state expect(doc1.toJSON()).toEqual(doc2.toJSON()) // Both should accept same operations change(doc1, draft => { draft.profile.email = "test@example.com" }) change(doc2, draft => { draft.profile.email = "test@example.com" }) expect(doc1.toJSON()).toEqual(doc2.toJSON()) }) }) }) }) describe("Raw vs Overlaid Values", () => { it("should distinguish between raw CRDT and overlaid values", () => { const schema = Shape.doc({ title: Shape.text(), metadata: Shape.struct({ optional: Shape.plain.string().placeholder("default-optional"), }), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.title.insert(0, "Hello") }) // Raw value should only contain what was actually set in CRDT const rawValue = loro(typedDoc).rawValue as any expect(rawValue.title).toBe("Hello") expect(rawValue.metadata).toBeUndefined() // Overlaid value should include empty state defaults // Use toJSON() to get plain values for comparison const overlaidValue = typedDoc.toJSON() expect(overlaidValue.title).toBe("Hello") expect(overlaidValue.metadata.optional).toBe("default-optional") }) }) describe("Validation", () => { it("should validate empty state against schema", () => { const schema = Shape.doc({ title: Shape.text(), count: Shape.counter(), }) expect(() => { createTypedDoc(schema) }).not.toThrow() }) it("should derive valid placeholder from schema", () => { const schema = Shape.doc({ title: Shape.text(), count: Shape.counter(), }) // With the new API, placeholder is always derived from schema // so it's always valid - this test verifies that expect(() => { createTypedDoc(schema) }).not.toThrow() }) it("should handle null values in placeholder correctly", () => { const schema = Shape.doc({ interjection: Shape.struct({ currentPrediction: Shape.plain .union([Shape.plain.null(), Shape.plain.string()]) .placeholder(null), }), }) const typedDoc = createTypedDoc(schema) // This should not throw "placeholder required" expect(() => { change(typedDoc, draft => { // Accessing the property triggers getOrCreateNode const current = draft.interjection.currentPrediction expect(current).toBeNull() // Verify we can update it draft.interjection.currentPrediction = "new value" }) }).not.toThrow() expect(typedDoc.toJSON().interjection.currentPrediction).toBe("new value") }) }) describe("Multiple Changes", () => { it("should persist state across multiple change calls", () => { const schema = Shape.doc({ title: Shape.text(), count: Shape.counter(), items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) // First change let result = change(typedDoc, draft => { draft.title.insert(0, "Hello") draft.count.increment(5) draft.items.push("first") }).toJSON() expect(result.title).toBe("Hello") expect(result.count).toBe(5) expect(result.items).toEqual(["first"]) // Second change - should build on previous state result = change(typedDoc, draft => { draft.title.insert(5, " World") draft.count.increment(3) draft.items.push("second") }).toJSON() expect(result.title).toBe("Hello World") expect(result.count).toBe(8) // 5 + 3 expect(result.items).toEqual(["first", "second"]) }) }) describe("Schema-Aware Input Conversion", () => { it("should convert plain objects to map containers in lists", () => { const schema = Shape.doc({ articles: Shape.list( Shape.struct({ title: Shape.text(), tags: Shape.list(Shape.plain.string()), }), ), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.articles.push({ title: "Hello World", tags: ["hello", "world"], }) }).toJSON() expect(result.articles).toHaveLength(1) expect(result.articles[0].title).toBe("Hello World") expect(result.articles[0].tags).toEqual(["hello", "world"]) }) it("should handle nested conversion in movable lists", () => { const schema = Shape.doc({ tasks: Shape.movableList( Shape.struct({ title: Shape.text(), completed: Shape.plain.boolean(), subtasks: Shape.list(Shape.plain.string()), }), ), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.tasks.push({ title: "Main Task", completed: false, subtasks: ["subtask1", "subtask2"], }) }).toJSON() expect(result.tasks).toHaveLength(1) expect(result.tasks[0].title).toBe("Main Task") expect(result.tasks[0].completed).toBe(false) expect(result.tasks[0].subtasks).toEqual(["subtask1", "subtask2"]) }) it("should handle deeply nested conversion", () => { const schema = Shape.doc({ posts: Shape.list( Shape.struct({ title: Shape.text(), metadata: Shape.struct({ views: Shape.counter(), tags: Shape.plain.array(Shape.plain.string()), }), }), ), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.posts.push({ title: "Complex Post", metadata: { views: 42, tags: ["complex", "nested"], }, }) }).toJSON() expect(result.posts).toHaveLength(1) expect(result.posts[0].title).toBe("Complex Post") expect(result.posts[0].metadata.views).toBe(42) expect(result.posts[0].metadata.tags).toEqual(["complex", "nested"]) }) }) }) describe("Edge Cases and Error Handling", () => { describe("Type Safety", () => { it("should maintain type safety with complex schemas", () => { const schema = Shape.doc({ title: Shape.text(), metadata: Shape.struct({ author: Shape.plain.string().placeholder("Anonymous"), publishedAt: Shape.plain.string().placeholder("2025-01-01"), }), }) const typedDoc = createTypedDoc(schema) // Multiple changes change(typedDoc, draft => { draft.title.insert(0, "First Title") draft.metadata.author = "John Doe" }) // Use toJSON() to get plain values for comparison let result = typedDoc.toJSON() expect(result.title).toBe("First Title") expect(result.metadata.author).toBe("John Doe") expect(result.metadata.publishedAt).toBe("2025-01-01") // More changes change(typedDoc, draft => { draft.title.update("Updated Title") draft.metadata.publishedAt = "2025-12-01" }) result = typedDoc.toJSON() expect(result.title).toBe("Updated Title") expect(result.metadata.author).toBe("John Doe") // Preserved from previous change expect(result.metadata.publishedAt).toBe("2025-12-01") }) it("should handle empty containers gracefully", () => { const schema = Shape.doc({ todos: Shape.list( Shape.struct({ text: Shape.text(), completed: Shape.plain.boolean(), }), ), }) const typedDoc = createTypedDoc(schema) // Add a todo item with minimal data const result = change(typedDoc, draft => { draft.todos.push({ text: "Test Todo", completed: false, }) }).toJSON() expect(result.todos).toHaveLength(1) expect(result.todos[0].text).toBe("Test Todo") expect(result.todos[0].completed).toBe(false) }) }) describe("Performance and Memory", () => { it("should handle large numbers of operations efficiently", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), counter: Shape.counter(), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { // Add many items for (let i = 0; i < 100; i++) { draft.items.push(`item-${i}`) draft.counter.increment(1) } }).toJSON() expect(result.items).toHaveLength(100) expect(result.counter).toBe(100) expect(result.items[0]).toBe("item-0") expect(result.items[99]).toBe("item-99") }) }) describe("Boundary Conditions", () => { it("should handle empty strings and zero values", () => { const schema = Shape.doc({ text: Shape.text(), count: Shape.counter(), items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.text.insert(0, "") draft.count.increment(0) draft.items.push("") }).toJSON() expect(result.text).toBe("") expect(result.count).toBe(0) expect(result.items).toEqual([""]) }) it("should handle special characters and unicode", () => { const schema = Shape.doc({ unicode: Shape.text(), emoji: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { draft.unicode.insert(0, "Hello δΈ–η•Œ 🌍") draft.emoji.push("πŸš€") draft.emoji.push("⭐") }).toJSON() expect(result.unicode).toBe("Hello δΈ–η•Œ 🌍") expect(result.emoji).toEqual(["πŸš€", "⭐"]) }) }) describe("Array-like Methods for Lists", () => { describe("Basic Array Methods", () => { it("should support find() method on lists", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push("apple") draft.items.push("banana") draft.items.push("cherry") // Test find method const found = draft.items.find(item => item.startsWith("b")) expect(found).toBe("banana") const notFound = draft.items.find(item => item.startsWith("z")) expect(notFound).toBeUndefined() }) }) it("should support findIndex() method on lists", () => { const schema = Shape.doc({ numbers: Shape.list(Shape.plain.number()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.numbers.push(10) draft.numbers.push(20) draft.numbers.push(30) // Test findIndex method const foundIndex = draft.numbers.findIndex(num => num > 15) expect(foundIndex).toBe(1) // Should find 20 at index 1 const notFoundIndex = draft.numbers.findIndex(num => num > 100) expect(notFoundIndex).toBe(-1) }) }) it("should support map() method on lists", () => { const schema = Shape.doc({ words: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.words.push("hello") draft.words.push("world") // Test map method const uppercased = draft.words.map(word => word.toUpperCase()) expect(uppercased).toEqual(["HELLO", "WORLD"]) const lengths = draft.words.map((word, index) => ({ word, index, length: word.length, })) expect(lengths).toEqual([ { word: "hello", index: 0, length: 5 }, { word: "world", index: 1, length: 5 }, ]) }) }) it("should support filter() method on lists", () => { const schema = Shape.doc({ numbers: Shape.list(Shape.plain.number()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.numbers.push(1) draft.numbers.push(2) draft.numbers.push(3) draft.numbers.push(4) draft.numbers.push(5) // Test filter method const evens = draft.numbers.filter(num => num % 2 === 0) expect(evens).toEqual([2, 4]) const withIndex = draft.numbers.filter((_num, index) => index > 2) expect(withIndex).toEqual([4, 5]) }) }) it("should support forEach() method on lists", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push("a") draft.items.push("b") draft.items.push("c") // Test forEach method const collected: Array<{ item: string; index: number }> = [] draft.items.forEach((item, index) => { collected.push({ item, index }) }) expect(collected).toEqual([ { item: "a", index: 0 }, { item: "b", index: 1 }, { item: "c", index: 2 }, ]) }) }) it("should support some() method on lists", () => { const schema = Shape.doc({ numbers: Shape.list(Shape.plain.number()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.numbers.push(1) draft.numbers.push(3) draft.numbers.push(5) // Test some method const hasEven = draft.numbers.some(num => num % 2 === 0) expect(hasEven).toBe(false) const hasOdd = draft.numbers.some(num => num % 2 === 1) expect(hasOdd).toBe(true) const hasLargeNumber = draft.numbers.some( (num, index) => num > index * 2, ) expect(hasLargeNumber).toBe(true) }) }) it("should support every() method on lists", () => { const schema = Shape.doc({ numbers: Shape.list(Shape.plain.number()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.numbers.push(2) draft.numbers.push(4) draft.numbers.push(6) // Test every method const allEven = draft.numbers.every(num => num % 2 === 0) expect(allEven).toBe(true) const allOdd = draft.numbers.every(num => num % 2 === 1) expect(allOdd).toBe(false) const allPositive = draft.numbers.every((num, _index) => num > 0) expect(allPositive).toBe(true) }) }) }) describe("Array Methods with Complex Objects", () => { it("should work with lists of plain objects", () => { const schema = Shape.doc({ todos: Shape.list( Shape.plain.struct({ id: Shape.plain.string(), text: Shape.plain.string(), completed: Shape.plain.boolean(), }), ), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.todos.push({ id: "1", text: "Buy milk", completed: false }) draft.todos.push({ id: "2", text: "Walk dog", completed: true }) draft.todos.push({ id: "3", text: "Write code", completed: false }) // Test find with objects const foundTodo = draft.todos.find(todo => todo.id === "2") expect(foundTodo).toEqual({ id: "2", text: "Walk dog", completed: true, }) // Test findIndex with objects const completedIndex = draft.todos.findIndex(todo => todo.completed) expect(completedIndex).toBe(1) // Test filter with objects const incompleteTodos = draft.todos.filter(todo => !todo.completed) expect(incompleteTodos).toHaveLength(2) expect(incompleteTodos[0].text).toBe("Buy milk") expect(incompleteTodos[1].text).toBe("Write code") // Test map with objects const todoTexts = draft.todos.map(todo => todo.text) expect(todoTexts).toEqual(["Buy milk", "Walk dog", "Write code"]) // Test some with objects const hasCompleted = draft.todos.some(todo => todo.completed) expect(hasCompleted).toBe(true) // Test every with objects const allCompleted = draft.todos.every(todo => todo.completed) expect(allCompleted).toBe(false) }) }) it("should work with lists of maps (nested containers)", () => { const schema = Shape.doc({ articles: Shape.list( Shape.struct({ title: Shape.text(), published: Shape.plain.boolean(), }), ), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.articles.push({ title: "First Article", published: true, }) draft.articles.push({ title: "Second Article", published: false, }) // Test find with nested containers const publishedArticle = draft.articles.find( article => article.published, ) expect(publishedArticle?.published).toBe(true) // Test map with nested containers const titles = draft.articles.map(article => article.title) expect(titles).toEqual(["First Article", "Second Article"]) // Test filter with nested containers const unpublished = draft.articles.filter( article => !article.published, ) expect(unpublished).toHaveLength(1) }) }) }) describe("Array Methods with MovableList", () => { it("should support all array methods on movable lists", () => { const schema = Shape.doc({ tasks: Shape.movableList( Shape.plain.struct({ id: Shape.plain.string(), priority: Shape.plain.number(), }), ), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.tasks.push({ id: "1", priority: 1 }) draft.tasks.push({ id: "2", priority: 3 }) draft.tasks.push({ id: "3", priority: 2 }) // Test find const highPriorityTask = draft.tasks.find(task => task.priority === 3) expect(highPriorityTask?.id).toBe("2") // Test findIndex const mediumPriorityIndex = draft.tasks.findIndex( task => task.priority === 2, ) expect(mediumPriorityIndex).toBe(2) // Test filter const lowPriorityTasks = draft.tasks.filter( task => task.priority <= 2, ) expect(lowPriorityTasks).toHaveLength(2) // Test map const priorities = draft.tasks.map(task => task.priority) expect(priorities).toEqual([1, 3, 2]) // Test some const hasHighPriority = draft.tasks.some(task => task.priority > 2) expect(hasHighPriority).toBe(true) // Test every const allHavePriority = draft.tasks.every(task => task.priority > 0) expect(allHavePriority).toBe(true) }) }) }) describe("Edge Cases", () => { it("should handle empty lists correctly", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { // Test all methods on empty list expect(draft.items.find(_item => true)).toBeUndefined() expect(draft.items.findIndex(_item => true)).toBe(-1) expect(draft.items.map(item => item)).toEqual([]) expect(draft.items.filter(_item => true)).toEqual([]) expect(draft.items.some(_item => true)).toBe(false) expect(draft.items.every(_item => true)).toBe(true) // vacuous truth let forEachCalled = false draft.items.forEach(() => { forEachCalled = true }) expect(forEachCalled).toBe(false) }) }) it("should handle single item lists correctly", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.number()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push(42) // Test all methods on single item list expect(draft.items.find(item => item === 42)).toBe(42) expect(draft.items.find(item => item === 99)).toBeUndefined() expect(draft.items.map(item => item * 2)).toEqual([84]) expect(draft.items.filter(item => item > 0)).toEqual([42]) expect(draft.items.filter(item => item < 0)).toEqual([]) expect(draft.items.some(item => item === 42)).toBe(true) expect(draft.items.some(item => item === 99)).toBe(false) expect(draft.items.every(item => item === 42)).toBe(true) expect(draft.items.every(item => item > 0)).toBe(true) expect(draft.items.every(item => item < 0)).toBe(false) const collected: number[] = [] // biome-ignore lint/suspicious/useIterableCallbackReturn: draft does not have iterable draft.items.forEach(item => collected.push(item)) expect(collected).toEqual([42]) }) }) it("should provide correct index parameter in callbacks", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push("a") draft.items.push("b") draft.items.push("c") // Test that index parameter is correct in all methods const findResult = draft.items.find((_item, index) => index === 1) expect(findResult).toBe("b") const findIndexResult = draft.items.findIndex( (_item, index) => index === 2, ) expect(findIndexResult).toBe(2) const mapResult = draft.items.map((item, index) => `${index}:${item}`) expect(mapResult).toEqual(["0:a", "1:b", "2:c"]) const filterResult = draft.items.filter( (_item, index) => index % 2 === 0, ) expect(filterResult).toEqual(["a", "c"]) const someResult = draft.items.some( (item, index) => index === 1 && item === "b", ) expect(someResult).toBe(true) const everyResult = draft.items.every((_item, index) => index < 3) expect(everyResult).toBe(true) const forEachResults: Array<{ item: string; index: number }> = [] draft.items.forEach((item, index) => { forEachResults.push({ item, index }) }) expect(forEachResults).toEqual([ { item: "a", index: 0 }, { item: "b", index: 1 }, { item: "c", index: 2 }, ]) }) }) describe("Find-and-Mutate Patterns", () => { it("should allow mutation of items found via array methods", () => { const schema = Shape.doc({ todos: Shape.list( Shape.plain.struct({ id: Shape.plain.string(), text: Shape.plain.string(), completed: Shape.plain.boolean(), }), ), }) const typedDoc = createTypedDoc(schema) // Add initial todos change(typedDoc, draft => { draft.todos.push({ id: "1", text: "Buy milk", completed: false }) draft.todos.push({ id: "2", text: "Walk dog", completed: false }) draft.todos.push({ id: "3", text: "Write code", completed: true }) }) // Test the key developer expectation: find + mutate const result = change(typedDoc, draft => { // Find a todo and toggle its completion status const todo = draft.todos.find(t => t.id === "2") if (todo) { todo.completed = !todo.completed // This should work and persist! } // Find another todo and change its text const codeTodo = draft.todos.find(t => t.text === "Write code") if (codeTodo) { codeTodo.text = "Write better code" } }).toJSON() // Verify the mutations persisted to the document state expect(result.todos[0]).toEqual({ id: "1", text: "Buy milk", completed: false, }) expect(result.todos[1]).toEqual({ id: "2", text: "Walk dog", completed: true, }) // Should be toggled expect(result.todos[2]).toEqual({ id: "3", text: "Write better code", completed: true, }) // Text should be changed // Also verify via typedDoc.toJSON() const finalState = typedDoc.toJSON() expect(finalState.todos[1].completed).toBe(true) expect(finalState.todos[2].text).toBe("Write better code") }) it("should allow mutation of nested container items found via array methods", () => { const schema = Shape.doc({ articles: Shape.list( Shape.struct({ title: Shape.text(), viewCount: Shape.counter(), metadata: Shape.plain.struct({ author: Shape.plain.string(), published: Shape.plain.boolean(), }), }), ), }) const typedDoc = createTypedDoc(schema) // Add initial articles change(typedDoc, draft => { draft.articles.push({ title: "First Article", viewCount: 0, metadata: { author: "Alice", published: false }, }) draft.articles.push({ title: "Second Article", viewCount: 5, metadata: { author: "Bob", published: true }, }) }) // Test mutation of nested containers found via array methods const result = change(typedDoc, draft => { // Find article by author and modify its nested properties const aliceArticle = draft.articles.find( article => article.metadata.author === "Alice", ) if (aliceArticle) { // Mutate text container aliceArticle.title.insert(0, "πŸ“ ") // Mutate counter container aliceArticle.viewCount.increment(10) // Mutate plain object property aliceArticle.metadata.published = true } // Find article by publication status and modify it const publishedArticle = draft.articles.find( article => article.metadata.published === true && article.metadata.author === "Bob", ) if (publishedArticle) { publishedArticle.title.update("Updated Second Article") publishedArticle.viewCount.increment(3) } }).toJSON() // Verify all mutations persisted correctly expect(result.articles[0].title).toBe("πŸ“ First Article") expect(result.articles[0].viewCount).toBe(10) expect(result.articles[0].metadata.published).toBe(true) expect(result.articles[1].title).toBe("Updated Second Article") expect(result.articles[1].viewCount).toBe(8) // 5 + 3 // Verify via typedDoc.toJSON() as well (use toJSON for plain values) const finalState = typedDoc.toJSON() expect(finalState.articles[0].title).toBe("πŸ“ First Article") expect(finalState.articles[0].viewCount).toBe(10) expect(finalState.articles[1].viewCount).toBe(8) }) it("should support common developer patterns with array methods", () => { const schema = Shape.doc({ users: Shape.list( Shape.plain.struct({ id: Shape.plain.string(), name: Shape.plain.string(), active: Shape.plain.boolean(), score: Shape.plain.number(), }), ), }) const typedDoc = createTypedDoc(schema) // Add initial users change(typedDoc, draft => { draft.users.push({ id: "1", name: "Alice", active: true, score: 100, }) draft.users.push({ id: "2", name: "Bob", active: false, score: 85 }) draft.users.push({ id: "3", name: "Charlie", active: true, score: 120, }) }) const result = change(typedDoc, draft => { // Pattern 1: Find and toggle boolean const inactiveUser = draft.users.find(user => !user.active) if (inactiveUser) { inactiveUser.active = true } // Pattern 2: Find by condition and update multiple properties const highScorer = draft.users.find(user => user.score > 110) if (highScorer) { highScorer.name = `${highScorer.name} (VIP)` highScorer.score += 50 } // Pattern 3: Filter and modify multiple items const activeUsers = draft.users.filter(user => user.active) activeUsers.forEach(user => { user.score += 10 // Bonus points for active users }) // Pattern 4: Find by index-based condition const firstUser = draft.users.find((_user, index) => index === 0) if (firstUser) { firstUser.name = `πŸ‘‘ ${firstUser.name}` } }).toJSON() // Verify all patterns worked expect(result.users[0].name).toBe("πŸ‘‘ Alice") expect(result.users[0].score).toBe(110) // 100 + 10 bonus expect(result.users[1].active).toBe(true) // Was toggled from false expect(result.users[1].score).toBe(95) // 85 + 10 bonus expect(result.users[2].name).toBe("Charlie (VIP)") expect(result.users[2].score).toBe(180) // 120 + 50 VIP + 10 bonus // Verify persistence const finalState = typedDoc.toJSON() expect(finalState.users.every(user => user.active)).toBe(true) expect(finalState.users[2].name).toContain("VIP") }) it("should handle edge cases in find-and-mutate patterns", () => { const schema = Shape.doc({ items: Shape.list( Shape.plain.struct({ id: Shape.plain.string(), value: Shape.plain.number(), }), ), }) const typedDoc = createTypedDoc(schema) const result = change(typedDoc, draft => { // Add some items draft.items.push({ id: "1", value: 10 }) draft.items.push({ id: "2", value: 20 }) // Try to find non-existent item - should not crash const nonExistent = draft.items.find(item => item.id === "999") if (nonExistent) { nonExistent.value = 999 // This shouldn't execute } // Find existing item and mutate const existing = draft.items.find(item => item.id === "1") if (existing) { existing.value *= 2 } // Use findIndex to locate and mutate // Note: After the first mutation, item with id "1" now has value 20, // so findIndex will find that item (index 0), not the original item with id "2" const index = draft.items.findIndex(item => item.value === 20) if (index !== -1) { const item = draft.items.get(index) if (item) { item.value += 5 } } }).toJSON() // Verify mutations worked correctly expect(result.items).toHaveLength(2) expect(result.items[0].value).toBe(25) // 10 * 2 + 5 (found by findIndex) expect(result.items[1].value).toBe(20) // 20 (unchanged) // Verify no phantom items were created expect(result.items.find(item => item.id === "999")).toBeUndefined() }) }) describe("slice method", () => { it("should return a slice of the list with start and end", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push("a") draft.items.push("b") draft.items.push("c") draft.items.push("d") draft.items.push("e") // slice(1, 3) returns items at indices 1 and 2 const sliced = draft.items.slice(1, 3) expect(sliced).toEqual(["b", "c"]) }) }) it("should handle negative indices", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push("a") draft.items.push("b") draft.items.push("c") draft.items.push("d") draft.items.push("e") // slice(-2) returns last 2 items const lastTwo = draft.items.slice(-2) expect(lastTwo).toEqual(["d", "e"]) // slice(1, -1) returns items from index 1 to second-to-last const middle = draft.items.slice(1, -1) expect(middle).toEqual(["b", "c", "d"]) // slice(-3, -1) returns items from third-to-last to second-to-last const negativeRange = draft.items.slice(-3, -1) expect(negativeRange).toEqual(["c", "d"]) }) }) it("should handle missing end parameter", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push("a") draft.items.push("b") draft.items.push("c") draft.items.push("d") // slice(2) returns items from index 2 to end const fromTwo = draft.items.slice(2) expect(fromTwo).toEqual(["c", "d"]) }) }) it("should handle no parameters", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push("a") draft.items.push("b") draft.items.push("c") // slice() returns all items (shallow copy) const all = draft.items.slice() expect(all).toEqual(["a", "b", "c"]) }) }) it("should handle out-of-bounds indices", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push("a") draft.items.push("b") draft.items.push("c") // slice(0, 100) on 3-item list returns all 3 items const overEnd = draft.items.slice(0, 100) expect(overEnd).toEqual(["a", "b", "c"]) // slice(100) returns empty array const overStart = draft.items.slice(100) expect(overStart).toEqual([]) // slice(-100) returns all items (clamped to 0) const underStart = draft.items.slice(-100) expect(underStart).toEqual(["a", "b", "c"]) }) }) it("should return empty array for empty list", () => { const schema = Shape.doc({ items: Shape.list(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { // slice() on empty list returns [] expect(draft.items.slice()).toEqual([]) expect(draft.items.slice(0, 10)).toEqual([]) expect(draft.items.slice(-5)).toEqual([]) }) }) it("should allow mutations to persist", () => { const schema = Shape.doc({ items: Shape.list( Shape.plain.struct({ id: Shape.plain.string(), value: Shape.plain.number(), }), ), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.items.push({ id: "1", value: 10 }) draft.items.push({ id: "2", value: 20 }) draft.items.push({ id: "3", value: 30 }) draft.items.push({ id: "4", value: 40 }) }) // Modify items from slice and verify changes persist const result = change(typedDoc, draft => { const middleItems = draft.items.slice(1, 3) // Mutate the sliced items middleItems[0].value = 200 middleItems[1].value = 300 }).toJSON() // Verify mutations persisted to the original list expect(result.items[0].value).toBe(10) // unchanged expect(result.items[1].value).toBe(200) // mutated via slice expect(result.items[2].value).toBe(300) // mutated via slice expect(result.items[3].value).toBe(40) // unchanged }) it("should work with MovableListRef", () => { const schema = Shape.doc({ tasks: Shape.movableList(Shape.plain.string()), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.tasks.push("task1") draft.tasks.push("task2") draft.tasks.push("task3") draft.tasks.push("task4") // Test slice on movable list const sliced = draft.tasks.slice(1, 3) expect(sliced).toEqual(["task2", "task3"]) const lastTwo = draft.tasks.slice(-2) expect(lastTwo).toEqual(["task3", "task4"]) }) }) it("should work with nested container items", () => { const schema = Shape.doc({ articles: Shape.list( Shape.struct({ title: Shape.text(), views: Shape.counter(), }), ), }) const typedDoc = createTypedDoc(schema) change(typedDoc, draft => { draft.articles.push({ title: "Article 1", views: 10 }) draft.articles.push({ title: "Article 2", views: 20 }) draft.articles.push({ title: "Article 3", views: 30 }) }) const result = change(typedDoc, draft => { const sliced = draft.articles.slice(0, 2) // Mutate nested containers in sliced items sliced[0].title.update("Updated Article 1") sliced[0].views.increment(5) sliced[1].views.increment(10) }).toJSON() // Verify mutations persisted expect(result.articles[0].title).toBe("Updated Article 1") expect(result.articles[0].views).toBe(15) // 10 + 5 expect(result.articles[1].views).toBe(30) // 20 + 10 expect(result.articles[2].views).toBe(30) // unchanged }) }) }) }) describe("Record with nested Map containers", () => { /** * Regression test for "placeholder required" error when calling toJSON() * on a document with Records containing Maps where the CRDT has partial data. * * The bug: When a Record contains Map entries that exist in the CRDT but not * in the placeholder (which is always {} for Records), the nested MapRef was * created with placeholder: undefined. When MapRef.toJSON() tried to access * value properties that don't exist in the CRDT, it threw "placeholder required". * * The fix: RecordRef.getTypedRefParams() now derives a placeholder from the * schema's shape when the Record's placeholder doesn't have an entry for that key. */ it("should call toJSON() without error when Record has entries with partial CRDT data", () => { // Schema with a Record containing Maps (similar to user's tomState schema) const StudentStateSchema = Shape.struct({ peerId: Shape.plain.string(), authorName: Shape.plain.string(), authorColor: Shape.plain.string(), history: Shape.list( Shape.struct({ timestamp: Shape.plain.number(), value: Shape.plain.string(), }), ), }) const DocSchema = Shape.doc({ students: Shape.record(StudentStateSchema), }) // Simulate loading an existing document that has Record entries with partial data const loroDoc = new LoroDoc() // Add an entry to the students record with only some fields populated const studentsMap = loroDoc.getMap("students") const studentMap = studentsMap.setContainer("peer-123", new LoroMap()) // Set some but not all properties - this simulates partial data from CRDT sync studentMap.set("peerId", "peer-123") studentMap.set("authorName", "Alice") // Note: authorColor is NOT set - this should fall back to placeholder default // Wrap with TypedDoc const typedDoc = createTypedDoc(DocSchema, { doc: loroDoc }) // This should not throw "placeholder required" expect(() => { // Use typedDoc.toJSON() to get plain values const json = typedDoc.toJSON() // Verify the result has placeholder defaults for missing fields expect(json.students["peer-123"].peerId).toBe("peer-123") expect(json.students["peer-123"].authorName).toBe("Alice") expect(json.students["peer-123"].authorColor).toBe("") // placeholder default expect(json.students["peer-123"].history).toEqual([]) // placeholder default }).not.toThrow() }) }) })