// 's pure model: parsing and serializing the value, the tree // edits (insert in any slot, remove with its successor taking the place, // update), structural sharing, and the editing reducer. import { describe, expect, test } from "bun:test"; import { childAt, discarded, find, freshId, idsOf, insert, parse, remove, ROOT, runOf, serialize, size, slotOf, start, step, update, type Sequence, type Step, } from "../packages/sequence/model"; const linear = (id: string, kind = "invite", next: Step | null = null): Step => ({ id, kind, fields: {}, next }); const branching = (id: string, branches: (Step | null)[], kind = "connected"): Step => ({ id, kind, fields: {}, branches }); // a -> b -> c?{ yes: d -> e, no: f } const sample = (): Sequence => ({ root: linear("a", "visit", linear("b", "invite", branching("c", [linear("d", "message", linear("e", "wait")), linear("f", "wait")]))), }); describe("parse / serialize", () => { test("an empty field is an empty sequence", () => { expect(parse("")).toEqual({ root: null }); expect(parse(" \n")).toEqual({ root: null }); expect(parse('{"root":null}')).toEqual({ root: null }); }); test("round-trips, with keys in a fixed order", () => { const text = serialize(sample()); expect(parse(text)).toEqual(sample()); expect(text.startsWith('{"root":{"id":"a","kind":"visit","fields":{},"next":{"id":"b"')).toBe(true); expect(serialize(parse(text))).toBe(text); }); test("fills a missing id and missing fields, and pads a known kind's branches", () => { const parsed = parse('{"root":{"kind":"connected","branches":[{"id":"x","kind":"wait","fields":{"days":3}}]}}', new Map([["connected", 2]])); expect(parsed.root!.id).toMatch(/^s[0-9a-z]{6}$/); expect(parsed.root!.fields).toEqual({}); expect(parsed.root!.branches).toHaveLength(2); expect(parsed.root!.branches![0]).toEqual({ id: "x", kind: "wait", fields: { days: 3 }, next: null }); expect(parsed.root!.branches![1]).toBeNull(); }); test("never truncates branches the data has", () => { const parsed = parse('{"root":{"id":"c","kind":"connected","branches":[null,null,{"id":"z","kind":"wait"}]}}', new Map([["connected", 2]])); expect(parsed.root!.branches).toHaveLength(3); expect(parsed.root!.branches![2]!.id).toBe("z"); }); test("names the malformed step", () => { expect(() => parse("[]")).toThrow('"root"'); expect(() => parse('{"root":{"id":"a"}}')).toThrow('root: a step needs a "kind"'); expect(() => parse('{"root":{"id":"a","kind":"x","next":{"id":"a","kind":"y"}}}')).toThrow('root.next: the id "a" is used twice'); expect(() => parse('{"root":{"kind":"x","fields":{"a":[1]}}}')).toThrow('"fields"'); expect(() => parse("{nope")).toThrow(); }); }); describe("queries", () => { test("find, slotOf, childAt, size, ids", () => { const s = sample(); expect(find(s, "e")!.kind).toBe("wait"); expect(find(s, "zz")).toBeNull(); expect(slotOf(s, "a")).toEqual(ROOT); expect(slotOf(s, "b")).toEqual({ parent: "a", branch: null }); expect(slotOf(s, "f")).toEqual({ parent: "c", branch: 1 }); expect(childAt(s, { parent: "c", branch: 0 })!.id).toBe("d"); expect(size(s.root)).toBe(6); expect([...idsOf(s)].sort()).toEqual(["a", "b", "c", "d", "e", "f"]); }); test("a run goes through next and stops at the first branching step", () => { expect(runOf(sample().root).map(s => s.id)).toEqual(["a", "b", "c"]); expect(runOf(null)).toEqual([]); }); test("a run survives a cycle in corrupt data", () => { const a = linear("a"); a.next = a; expect(runOf(a).map(s => s.id)).toEqual(["a"]); }); test("fresh ids avoid the taken ones", () => { let calls = 0; const random = () => (calls++ === 0 ? 0 : 0.5); const taken = new Set(["s000000"]); const id = freshId(taken, random); expect(id).not.toBe("s000000"); expect(id).toMatch(/^s[0-9a-z]{6}$/); }); }); describe("edits", () => { test("insert at the root moves the old root after it", () => { const s = insert(sample(), ROOT, linear("n", "wait")); expect(runOf(s.root).map(x => x.id)).toEqual(["n", "a", "b", "c"]); }); test("insert mid-run moves the occupant after it, sharing the rest", () => { const before = sample(); const s = insert(before, { parent: "a", branch: null }, linear("n", "wait")); expect(runOf(s.root).map(x => x.id)).toEqual(["a", "n", "b", "c"]); expect(find(s, "b")).toBe(find(before, "b")); // the moved subtree is the same object }); test("insert into an empty branch and at a run's end", () => { let s = insert(sample(), { parent: "c", branch: 1 }, linear("n")); expect(childAt(s, { parent: "c", branch: 1 })!.id).toBe("n"); expect(find(s, "n")!.next!.id).toBe("f"); s = insert(s, { parent: "e", branch: null }, linear("z")); expect(find(s, "e")!.next!.id).toBe("z"); expect(find(s, "z")!.next).toBeNull(); }); test("a branching step inserted mid-run takes the occupant into its first branch", () => { const s = insert(sample(), { parent: "a", branch: null }, branching("q", [null, null])); const q = find(s, "q")!; expect(q.branches![0]!.id).toBe("b"); expect(q.branches![1]).toBeNull(); }); test("update replaces fields along the path only", () => { const before = sample(); const s = update(before, "d", { text: "Hi" }); expect(find(s, "d")!.fields).toEqual({ text: "Hi" }); expect(find(s, "f")).toBe(find(before, "f")); expect(s.root).not.toBe(before.root); expect(update(before, "nope", {})).toBe(before); }); test("remove a linear step: its next takes its place", () => { const s = remove(sample(), "b"); expect(runOf(s.root).map(x => x.id)).toEqual(["a", "c"]); expect(remove(sample(), "a").root!.id).toBe("b"); expect(remove({ root: linear("only") }, "only")).toEqual({ root: null }); }); test("remove a branching step: its first non-empty branch takes its place", () => { const s = remove(sample(), "c"); expect(runOf(s.root).map(x => x.id)).toEqual(["a", "b", "d", "e"]); expect(find(s, "f")).toBeNull(); const firstEmpty: Sequence = { root: branching("c", [null, linear("f")]) }; expect(remove(firstEmpty, "c").root!.id).toBe("f"); }); test("discarded counts what a removal drops besides the step", () => { const s = sample(); expect(discarded(s, "b")).toBe(0); expect(discarded(s, "c")).toBe(1); // f expect(discarded(s, "nope")).toBe(0); }); }); describe("the reducer", () => { const fresh = linear("n", "wait"); test("a menu opens on one slot at a time", () => { let m = step(start(sample()), { type: "menu", slot: ROOT }); expect(m.adding).toBe("/next"); m = step(m, { type: "menu", slot: { parent: "c", branch: 1 } }); expect(m.adding).toBe("c/1"); expect(step(m, { type: "menu", slot: null }).adding).toBeNull(); }); test("inserting with an editor marks it fresh; canceling a fresh edit takes the step back out", () => { let m = step(start(sample()), { type: "insert", slot: ROOT, step: fresh, edit: true }); expect(m.editing).toBe("n"); expect(m.fresh).toBe(true); m = step(m, { type: "cancel" }); expect(m.editing).toBeNull(); expect(serialize(m.value)).toBe(serialize(sample())); }); test("committing keeps the step; canceling a later edit keeps it unchanged", () => { let m = step(start(sample()), { type: "insert", slot: ROOT, step: fresh, edit: true }); m = step(m, { type: "commit", id: "n", fields: { days: 2 } }); expect(find(m.value, "n")!.fields).toEqual({ days: 2 }); m = step(m, { type: "edit", id: "n" }); expect(m.fresh).toBe(false); m = step(m, { type: "cancel" }); expect(find(m.value, "n")!.fields).toEqual({ days: 2 }); }); test("removing what would discard others asks first", () => { let m = step(start(sample()), { type: "remove", id: "c" }); expect(m.confirming).toBe("c"); expect(find(m.value, "c")).not.toBeNull(); m = step(m, { type: "remove", id: "c" }); expect(m.confirming).toBeNull(); expect(find(m.value, "c")).toBeNull(); }); test("any other action withdraws the question", () => { let m = step(start(sample()), { type: "remove", id: "c" }); m = step(m, { type: "menu", slot: ROOT }); expect(m.confirming).toBeNull(); m = step(m, { type: "remove", id: "c" }); expect(m.confirming).toBe("c"); }); test("editing an unknown id is ignored; load starts over", () => { const m = start(sample()); expect(step(m, { type: "edit", id: "nope" })).toBe(m); const loaded = step({ ...m, editing: "a" }, { type: "load", value: { root: null } }); expect(loaded).toEqual(start({ root: null })); }); });