|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import React, { act } from "react"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import type { TimelineElement } from "../player"; |
| 7 | +import { useRazorSplit } from "./useRazorSplit"; |
| 8 | +import { createPersistentEditHistoryStore } from "./usePersistentEditHistory"; |
| 9 | +import { createMemoryEditHistoryStorage } from "../utils/editHistoryStorage"; |
| 10 | +import { createEmptyEditHistory } from "../utils/editHistory"; |
| 11 | + |
| 12 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 13 | + |
| 14 | +const ORIGINAL = `<div class="clip" id="clip1" data-start="0" data-duration="4">hi</div>`; |
| 15 | +const SPLIT = |
| 16 | + `<div class="clip" id="clip1" data-start="0" data-duration="2">hi</div>` + |
| 17 | + `<div class="clip" id="clip1-split" data-start="2" data-duration="2">hi</div>`; |
| 18 | + |
| 19 | +const element: TimelineElement = { |
| 20 | + id: "clip1", |
| 21 | + tag: "div", |
| 22 | + start: 0, |
| 23 | + duration: 4, |
| 24 | + track: 0, |
| 25 | + domId: "clip1", |
| 26 | + sourceFile: "index.html", |
| 27 | + timingSource: "authored", |
| 28 | +}; |
| 29 | + |
| 30 | +type Split = (element: TimelineElement, splitTime: number) => Promise<void>; |
| 31 | + |
| 32 | +interface Harness { |
| 33 | + disk: Record<string, string>; |
| 34 | + store: ReturnType<typeof createPersistentEditHistoryStore>; |
| 35 | + splitRef: { current: Split | undefined }; |
| 36 | + root: ReturnType<typeof createRoot>; |
| 37 | + expected: string; |
| 38 | +} |
| 39 | + |
| 40 | +const SPLIT_GSAP = SPLIT.replace( |
| 41 | + "</div>", |
| 42 | + "</div><script>window.__timelines={};const tl=gsap.timeline({paused:true});" + |
| 43 | + 'tl.set("#clip1-split",{x:0},2);window.__timelines["c"]=tl;</script>', |
| 44 | +); |
| 45 | + |
| 46 | +function mountRazorSplit(opts: { gsap?: boolean } = {}): Harness { |
| 47 | + const disk: Record<string, string> = { "index.html": ORIGINAL }; |
| 48 | + const finalContent = opts.gsap ? SPLIT_GSAP : SPLIT; |
| 49 | + |
| 50 | + const storage = createMemoryEditHistoryStorage(); |
| 51 | + const store = createPersistentEditHistoryStore({ |
| 52 | + projectId: "p1", |
| 53 | + storage, |
| 54 | + initialState: createEmptyEditHistory(), |
| 55 | + now: (() => { |
| 56 | + let t = 1000; |
| 57 | + return () => (t += 10); |
| 58 | + })(), |
| 59 | + onChange: () => {}, |
| 60 | + }); |
| 61 | + |
| 62 | + // Faithful stand-in for the studio-server file-mutation endpoints: the server |
| 63 | + // writes the split to disk itself, then returns the patched content. |
| 64 | + const fetchMock = vi.fn(async (url: string, init?: RequestInit) => { |
| 65 | + const u = String(url); |
| 66 | + if (u.includes("/gsap-mutations/")) { |
| 67 | + if (opts.gsap) { |
| 68 | + // Mirror the server: rewrites the GSAP script for the new id, writes to |
| 69 | + // disk, returns the final content. |
| 70 | + disk["index.html"] = SPLIT_GSAP; |
| 71 | + return new Response(JSON.stringify({ ok: true, after: SPLIT_GSAP }), { |
| 72 | + status: 200, |
| 73 | + headers: { "Content-Type": "application/json" }, |
| 74 | + }); |
| 75 | + } |
| 76 | + // The fixture has no GSAP script — mirror the server's 400 response. |
| 77 | + return new Response(JSON.stringify({ error: "no GSAP script found in file" }), { |
| 78 | + status: 400, |
| 79 | + headers: { "Content-Type": "application/json" }, |
| 80 | + }); |
| 81 | + } |
| 82 | + if (u.includes("/file-mutations/split-element/")) { |
| 83 | + disk["index.html"] = SPLIT; |
| 84 | + return new Response( |
| 85 | + JSON.stringify({ ok: true, changed: true, content: SPLIT, newId: "clip1-split" }), |
| 86 | + { status: 200, headers: { "Content-Type": "application/json" } }, |
| 87 | + ); |
| 88 | + } |
| 89 | + if (u.includes("/files/")) { |
| 90 | + return new Response(JSON.stringify({ content: disk["index.html"] }), { |
| 91 | + status: 200, |
| 92 | + headers: { "Content-Type": "application/json" }, |
| 93 | + }); |
| 94 | + } |
| 95 | + void init; |
| 96 | + throw new Error(`unexpected fetch: ${u}`); |
| 97 | + }); |
| 98 | + vi.stubGlobal("fetch", fetchMock); |
| 99 | + |
| 100 | + const splitRef: { current: Split | undefined } = { current: undefined }; |
| 101 | + |
| 102 | + function Component() { |
| 103 | + const { handleRazorSplit } = useRazorSplit({ |
| 104 | + projectId: "p1", |
| 105 | + activeCompPath: "index.html", |
| 106 | + showToast: () => {}, |
| 107 | + writeProjectFile: async (path, content) => { |
| 108 | + disk[path] = content; |
| 109 | + }, |
| 110 | + recordEdit: store.recordEdit, |
| 111 | + domEditSaveTimestampRef: { current: 0 }, |
| 112 | + reloadPreview: () => {}, |
| 113 | + }); |
| 114 | + splitRef.current = handleRazorSplit; |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + const host = document.createElement("div"); |
| 119 | + document.body.append(host); |
| 120 | + const root = createRoot(host); |
| 121 | + act(() => { |
| 122 | + root.render(<Component />); |
| 123 | + }); |
| 124 | + |
| 125 | + return { disk, store, splitRef, root, expected: finalContent }; |
| 126 | +} |
| 127 | + |
| 128 | +async function undoViaDisk(harness: Harness) { |
| 129 | + return harness.store.undo({ |
| 130 | + readFile: async (path) => harness.disk[path], |
| 131 | + writeFile: async (path, content) => { |
| 132 | + harness.disk[path] = content; |
| 133 | + }, |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +afterEach(() => { |
| 138 | + document.body.innerHTML = ""; |
| 139 | + vi.unstubAllGlobals(); |
| 140 | +}); |
| 141 | + |
| 142 | +describe("useRazorSplit — split is undoable via edit history", () => { |
| 143 | + for (const gsap of [false, true]) { |
| 144 | + describe(gsap ? "with GSAP rewrite" : "plain HTML split", () => { |
| 145 | + let harness: Harness; |
| 146 | + beforeEach(() => { |
| 147 | + harness = mountRazorSplit({ gsap }); |
| 148 | + }); |
| 149 | + afterEach(() => { |
| 150 | + act(() => harness.root.unmount()); |
| 151 | + }); |
| 152 | + |
| 153 | + it("records a single 'Split timeline clip' history entry that undo restores", async () => { |
| 154 | + await act(async () => { |
| 155 | + await harness.splitRef.current!(element, 2); |
| 156 | + }); |
| 157 | + |
| 158 | + // The split reached disk. |
| 159 | + expect(harness.disk["index.html"]).toBe(harness.expected); |
| 160 | + |
| 161 | + // The split must be the top of the undo stack — not a prior/other entry. |
| 162 | + expect(harness.store.snapshot().canUndo).toBe(true); |
| 163 | + expect(harness.store.snapshot().undoLabel).toBe("Split timeline clip"); |
| 164 | + |
| 165 | + // Undo restores the exact pre-split file. |
| 166 | + const result = await undoViaDisk(harness); |
| 167 | + expect(result.ok).toBe(true); |
| 168 | + expect(result.label).toBe("Split timeline clip"); |
| 169 | + expect(harness.disk["index.html"]).toBe(ORIGINAL); |
| 170 | + }); |
| 171 | + }); |
| 172 | + } |
| 173 | +}); |
0 commit comments