library
Prebuilt pebble components: drop-in custom elements, no compiler needed
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runAucun
0Étoiles
0Forks
2Issues
| 1 | // <pebble-sequence>'s pure model: parsing and serializing the value, the tree |
| 2 | // edits (insert in any slot, remove with its successor taking the place, |
| 3 | // update), structural sharing, and the editing reducer. |
| 4 | import { describe, expect, test } from "bun:test"; |
| 5 | import { |
| 6 | childAt, |
| 7 | discarded, |
| 8 | find, |
| 9 | freshId, |
| 10 | idsOf, |
| 11 | insert, |
| 12 | parse, |
| 13 | remove, |
| 14 | ROOT, |
| 15 | runOf, |
| 16 | serialize, |
| 17 | size, |
| 18 | slotOf, |
| 19 | start, |
| 20 | step, |
| 21 | update, |
| 22 | type Sequence, |
| 23 | type Step, |
| 24 | } from "../packages/sequence/model"; |
| 25 | |
| 26 | const linear = (id: string, kind = "invite", next: Step | null = null): Step => ({ id, kind, fields: {}, next }); |
| 27 | const branching = (id: string, branches: (Step | null)[], kind = "connected"): Step => ({ id, kind, fields: {}, branches }); |
| 28 | |
| 29 | // a -> b -> c?{ yes: d -> e, no: f } |
| 30 | const sample = (): Sequence => ({ |
| 31 | root: linear("a", "visit", linear("b", "invite", branching("c", [linear("d", "message", linear("e", "wait")), linear("f", "wait")]))), |
| 32 | }); |
| 33 | |
| 34 | describe("parse / serialize", () => { |
| 35 | test("an empty field is an empty sequence", () => { |
| 36 | expect(parse("")).toEqual({ root: null }); |
| 37 | expect(parse(" \n")).toEqual({ root: null }); |
| 38 | expect(parse('{"root":null}')).toEqual({ root: null }); |
| 39 | }); |
| 40 | |
| 41 | test("round-trips, with keys in a fixed order", () => { |
| 42 | const text = serialize(sample()); |
| 43 | expect(parse(text)).toEqual(sample()); |
| 44 | expect(text.startsWith('{"root":{"id":"a","kind":"visit","fields":{},"next":{"id":"b"')).toBe(true); |
| 45 | expect(serialize(parse(text))).toBe(text); |
| 46 | }); |
| 47 | |
| 48 | test("fills a missing id and missing fields, and pads a known kind's branches", () => { |
| 49 | const parsed = parse('{"root":{"kind":"connected","branches":[{"id":"x","kind":"wait","fields":{"days":3}}]}}', new Map([["connected", 2]])); |
| 50 | expect(parsed.root!.id).toMatch(/^s[0-9a-z]{6}$/); |
| 51 | expect(parsed.root!.fields).toEqual({}); |
| 52 | expect(parsed.root!.branches).toHaveLength(2); |
| 53 | expect(parsed.root!.branches![0]).toEqual({ id: "x", kind: "wait", fields: { days: 3 }, next: null }); |
| 54 | expect(parsed.root!.branches![1]).toBeNull(); |
| 55 | }); |
| 56 | |
| 57 | test("never truncates branches the data has", () => { |
| 58 | const parsed = parse('{"root":{"id":"c","kind":"connected","branches":[null,null,{"id":"z","kind":"wait"}]}}', new Map([["connected", 2]])); |
| 59 | expect(parsed.root!.branches).toHaveLength(3); |
| 60 | expect(parsed.root!.branches![2]!.id).toBe("z"); |
| 61 | }); |
| 62 | |
| 63 | test("names the malformed step", () => { |
| 64 | expect(() => parse("[]")).toThrow('"root"'); |
| 65 | expect(() => parse('{"root":{"id":"a"}}')).toThrow('root: a step needs a "kind"'); |
| 66 | expect(() => parse('{"root":{"id":"a","kind":"x","next":{"id":"a","kind":"y"}}}')).toThrow('root.next: the id "a" is used twice'); |
| 67 | expect(() => parse('{"root":{"kind":"x","fields":{"a":[1]}}}')).toThrow('"fields"'); |
| 68 | expect(() => parse("{nope")).toThrow(); |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | describe("queries", () => { |
| 73 | test("find, slotOf, childAt, size, ids", () => { |
| 74 | const s = sample(); |
| 75 | expect(find(s, "e")!.kind).toBe("wait"); |
| 76 | expect(find(s, "zz")).toBeNull(); |
| 77 | expect(slotOf(s, "a")).toEqual(ROOT); |
| 78 | expect(slotOf(s, "b")).toEqual({ parent: "a", branch: null }); |
| 79 | expect(slotOf(s, "f")).toEqual({ parent: "c", branch: 1 }); |
| 80 | expect(childAt(s, { parent: "c", branch: 0 })!.id).toBe("d"); |
| 81 | expect(size(s.root)).toBe(6); |
| 82 | expect([...idsOf(s)].sort()).toEqual(["a", "b", "c", "d", "e", "f"]); |
| 83 | }); |
| 84 | |
| 85 | test("a run goes through next and stops at the first branching step", () => { |
| 86 | expect(runOf(sample().root).map(s => s.id)).toEqual(["a", "b", "c"]); |
| 87 | expect(runOf(null)).toEqual([]); |
| 88 | }); |
| 89 | |
| 90 | test("a run survives a cycle in corrupt data", () => { |
| 91 | const a = linear("a"); |
| 92 | a.next = a; |
| 93 | expect(runOf(a).map(s => s.id)).toEqual(["a"]); |
| 94 | }); |
| 95 | |
| 96 | test("fresh ids avoid the taken ones", () => { |
| 97 | let calls = 0; |
| 98 | const random = () => (calls++ === 0 ? 0 : 0.5); |
| 99 | const taken = new Set(["s000000"]); |
| 100 | const id = freshId(taken, random); |
| 101 | expect(id).not.toBe("s000000"); |
| 102 | expect(id).toMatch(/^s[0-9a-z]{6}$/); |
| 103 | }); |
| 104 | }); |
| 105 | |
| 106 | describe("edits", () => { |
| 107 | test("insert at the root moves the old root after it", () => { |
| 108 | const s = insert(sample(), ROOT, linear("n", "wait")); |
| 109 | expect(runOf(s.root).map(x => x.id)).toEqual(["n", "a", "b", "c"]); |
| 110 | }); |
| 111 | |
| 112 | test("insert mid-run moves the occupant after it, sharing the rest", () => { |
| 113 | const before = sample(); |
| 114 | const s = insert(before, { parent: "a", branch: null }, linear("n", "wait")); |
| 115 | expect(runOf(s.root).map(x => x.id)).toEqual(["a", "n", "b", "c"]); |
| 116 | expect(find(s, "b")).toBe(find(before, "b")); // the moved subtree is the same object |
| 117 | }); |
| 118 | |
| 119 | test("insert into an empty branch and at a run's end", () => { |
| 120 | let s = insert(sample(), { parent: "c", branch: 1 }, linear("n")); |
| 121 | expect(childAt(s, { parent: "c", branch: 1 })!.id).toBe("n"); |
| 122 | expect(find(s, "n")!.next!.id).toBe("f"); |
| 123 | s = insert(s, { parent: "e", branch: null }, linear("z")); |
| 124 | expect(find(s, "e")!.next!.id).toBe("z"); |
| 125 | expect(find(s, "z")!.next).toBeNull(); |
| 126 | }); |
| 127 | |
| 128 | test("a branching step inserted mid-run takes the occupant into its first branch", () => { |
| 129 | const s = insert(sample(), { parent: "a", branch: null }, branching("q", [null, null])); |
| 130 | const q = find(s, "q")!; |
| 131 | expect(q.branches![0]!.id).toBe("b"); |
| 132 | expect(q.branches![1]).toBeNull(); |
| 133 | }); |
| 134 | |
| 135 | test("update replaces fields along the path only", () => { |
| 136 | const before = sample(); |
| 137 | const s = update(before, "d", { text: "Hi" }); |
| 138 | expect(find(s, "d")!.fields).toEqual({ text: "Hi" }); |
| 139 | expect(find(s, "f")).toBe(find(before, "f")); |
| 140 | expect(s.root).not.toBe(before.root); |
| 141 | expect(update(before, "nope", {})).toBe(before); |
| 142 | }); |
| 143 | |
| 144 | test("remove a linear step: its next takes its place", () => { |
| 145 | const s = remove(sample(), "b"); |
| 146 | expect(runOf(s.root).map(x => x.id)).toEqual(["a", "c"]); |
| 147 | expect(remove(sample(), "a").root!.id).toBe("b"); |
| 148 | expect(remove({ root: linear("only") }, "only")).toEqual({ root: null }); |
| 149 | }); |
| 150 | |
| 151 | test("remove a branching step: its first non-empty branch takes its place", () => { |
| 152 | const s = remove(sample(), "c"); |
| 153 | expect(runOf(s.root).map(x => x.id)).toEqual(["a", "b", "d", "e"]); |
| 154 | expect(find(s, "f")).toBeNull(); |
| 155 | const firstEmpty: Sequence = { root: branching("c", [null, linear("f")]) }; |
| 156 | expect(remove(firstEmpty, "c").root!.id).toBe("f"); |
| 157 | }); |
| 158 | |
| 159 | test("discarded counts what a removal drops besides the step", () => { |
| 160 | const s = sample(); |
| 161 | expect(discarded(s, "b")).toBe(0); |
| 162 | expect(discarded(s, "c")).toBe(1); // f |
| 163 | expect(discarded(s, "nope")).toBe(0); |
| 164 | }); |
| 165 | }); |
| 166 | |
| 167 | describe("the reducer", () => { |
| 168 | const fresh = linear("n", "wait"); |
| 169 | |
| 170 | test("a menu opens on one slot at a time", () => { |
| 171 | let m = step(start(sample()), { type: "menu", slot: ROOT }); |
| 172 | expect(m.adding).toBe("/next"); |
| 173 | m = step(m, { type: "menu", slot: { parent: "c", branch: 1 } }); |
| 174 | expect(m.adding).toBe("c/1"); |
| 175 | expect(step(m, { type: "menu", slot: null }).adding).toBeNull(); |
| 176 | }); |
| 177 | |
| 178 | test("inserting with an editor marks it fresh; canceling a fresh edit takes the step back out", () => { |
| 179 | let m = step(start(sample()), { type: "insert", slot: ROOT, step: fresh, edit: true }); |
| 180 | expect(m.editing).toBe("n"); |
| 181 | expect(m.fresh).toBe(true); |
| 182 | m = step(m, { type: "cancel" }); |
| 183 | expect(m.editing).toBeNull(); |
| 184 | expect(serialize(m.value)).toBe(serialize(sample())); |
| 185 | }); |
| 186 | |
| 187 | test("committing keeps the step; canceling a later edit keeps it unchanged", () => { |
| 188 | let m = step(start(sample()), { type: "insert", slot: ROOT, step: fresh, edit: true }); |
| 189 | m = step(m, { type: "commit", id: "n", fields: { days: 2 } }); |
| 190 | expect(find(m.value, "n")!.fields).toEqual({ days: 2 }); |
| 191 | m = step(m, { type: "edit", id: "n" }); |
| 192 | expect(m.fresh).toBe(false); |
| 193 | m = step(m, { type: "cancel" }); |
| 194 | expect(find(m.value, "n")!.fields).toEqual({ days: 2 }); |
| 195 | }); |
| 196 | |
| 197 | test("removing what would discard others asks first", () => { |
| 198 | let m = step(start(sample()), { type: "remove", id: "c" }); |
| 199 | expect(m.confirming).toBe("c"); |
| 200 | expect(find(m.value, "c")).not.toBeNull(); |
| 201 | m = step(m, { type: "remove", id: "c" }); |
| 202 | expect(m.confirming).toBeNull(); |
| 203 | expect(find(m.value, "c")).toBeNull(); |
| 204 | }); |
| 205 | |
| 206 | test("any other action withdraws the question", () => { |
| 207 | let m = step(start(sample()), { type: "remove", id: "c" }); |
| 208 | m = step(m, { type: "menu", slot: ROOT }); |
| 209 | expect(m.confirming).toBeNull(); |
| 210 | m = step(m, { type: "remove", id: "c" }); |
| 211 | expect(m.confirming).toBe("c"); |
| 212 | }); |
| 213 | |
| 214 | test("editing an unknown id is ignored; load starts over", () => { |
| 215 | const m = start(sample()); |
| 216 | expect(step(m, { type: "edit", id: "nope" })).toBe(m); |
| 217 | const loaded = step({ ...m, editing: "a" }, { type: "load", value: { root: null } }); |
| 218 | expect(loaded).toEqual(start({ root: null })); |
| 219 | }); |
| 220 | }); |