|
| 1 | +/** |
| 2 | + * Session-level behavior: history coalescing invariants and T3 override replay. |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect } from "vitest"; |
| 6 | +import { openComposition } from "./session.js"; |
| 7 | + |
| 8 | +const BASE_HTML = ` |
| 9 | +<div data-hf-id="hf-stage" data-hf-root style="width: 1280px; height: 720px" data-duration="5"> |
| 10 | + <h1 data-hf-id="hf-title" data-start="0" data-end="3" style="color: #fff; font-size: 64px">Hello World</h1> |
| 11 | + <p data-hf-id="hf-sub" style="opacity: 0.5">subtitle</p> |
| 12 | + <img data-hf-id="hf-logo" src="/logo.png" alt="Logo" /> |
| 13 | +</div> |
| 14 | +`.trim(); |
| 15 | + |
| 16 | +// ─── History coalescing ─────────────────────────────────────────────────────── |
| 17 | + |
| 18 | +describe("history coalescing", () => { |
| 19 | + it("rapid edits to the SAME property coalesce into one undo entry", async () => { |
| 20 | + const comp = await openComposition(BASE_HTML); |
| 21 | + comp.setStyle("hf-title", { color: "#111" }); |
| 22 | + comp.setStyle("hf-title", { color: "#222" }); |
| 23 | + comp.setStyle("hf-title", { color: "#333" }); |
| 24 | + |
| 25 | + comp.undo(); |
| 26 | + const el = comp.getElement("hf-title"); |
| 27 | + expect(el?.inlineStyles["color"]).toBe("#fff"); // back to original in ONE step |
| 28 | + }); |
| 29 | + |
| 30 | + it("rapid edits to DIFFERENT elements do NOT coalesce — undo reverts only the last edit", async () => { |
| 31 | + const comp = await openComposition(BASE_HTML); |
| 32 | + comp.setStyle("hf-title", { color: "#111" }); |
| 33 | + comp.setStyle("hf-sub", { opacity: "1" }); |
| 34 | + |
| 35 | + comp.undo(); |
| 36 | + expect(comp.getElement("hf-sub")?.inlineStyles["opacity"]).toBe("0.5"); // last edit reverted |
| 37 | + expect(comp.getElement("hf-title")?.inlineStyles["color"]).toBe("#111"); // first edit intact |
| 38 | + |
| 39 | + comp.undo(); |
| 40 | + expect(comp.getElement("hf-title")?.inlineStyles["color"]).toBe("#fff"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("rapid edits to different properties of the same element do not coalesce", async () => { |
| 44 | + const comp = await openComposition(BASE_HTML); |
| 45 | + comp.setStyle("hf-title", { color: "#111" }); |
| 46 | + comp.setStyle("hf-title", { fontSize: "96px" }); |
| 47 | + |
| 48 | + comp.undo(); |
| 49 | + expect(comp.getElement("hf-title")?.inlineStyles["fontSize"]).toBe("64px"); |
| 50 | + expect(comp.getElement("hf-title")?.inlineStyles["color"]).toBe("#111"); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +// ─── T3 override replay ─────────────────────────────────────────────────────── |
| 55 | + |
| 56 | +describe("override-set replay on open", () => { |
| 57 | + it("applies style, text, and attribute overrides to the base document", async () => { |
| 58 | + const comp = await openComposition(BASE_HTML, { |
| 59 | + overrides: { |
| 60 | + "hf-title.style.color": "#e63946", |
| 61 | + "hf-title.text": "Edited headline", |
| 62 | + "hf-logo.attr.src": "/new-logo.png", |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + const title = comp.getElement("hf-title"); |
| 67 | + expect(title?.inlineStyles["color"]).toBe("#e63946"); |
| 68 | + expect(title?.text).toBe("Edited headline"); |
| 69 | + expect(comp.getElement("hf-logo")?.attributes["src"]).toBe("/new-logo.png"); |
| 70 | + |
| 71 | + const html = comp.serialize(); |
| 72 | + expect(html).toContain("Edited headline"); |
| 73 | + expect(html).toContain("/new-logo.png"); |
| 74 | + expect(html).toContain("#e63946"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("applies timing overrides (computed absolute end)", async () => { |
| 78 | + const comp = await openComposition(BASE_HTML, { |
| 79 | + overrides: { "hf-title.timing.end": 4.5 }, |
| 80 | + }); |
| 81 | + expect(comp.serialize()).toContain('data-end="4.5"'); |
| 82 | + }); |
| 83 | + |
| 84 | + it("removes elements marked with the null removal marker", async () => { |
| 85 | + const comp = await openComposition(BASE_HTML, { |
| 86 | + overrides: { "hf-sub": null }, |
| 87 | + }); |
| 88 | + expect(comp.getElement("hf-sub")).toBeNull(); |
| 89 | + expect(comp.serialize()).not.toContain("subtitle"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("treats property-level null as restore-base (no-op on fresh base)", async () => { |
| 93 | + const comp = await openComposition(BASE_HTML, { |
| 94 | + overrides: { "hf-title.style.color": null }, |
| 95 | + }); |
| 96 | + expect(comp.getElement("hf-title")?.inlineStyles["color"]).toBe("#fff"); |
| 97 | + }); |
| 98 | + |
| 99 | + it("getOverrides returns the set the session was opened with", async () => { |
| 100 | + const overrides = { "hf-title.style.color": "#e63946" }; |
| 101 | + const comp = await openComposition(BASE_HTML, { overrides }); |
| 102 | + expect(comp.getOverrides()).toEqual(overrides); |
| 103 | + }); |
| 104 | +}); |
0 commit comments