|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import React, { act } from "react"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import type { BlockParam } from "@hyperframes/core/registry"; |
| 7 | +import { FileManagerProvider } from "../../contexts/FileManagerContext"; |
| 8 | +import type { useFileManager } from "../../hooks/useFileManager"; |
| 9 | +import { StudioPlaybackProvider, type StudioPlaybackValue } from "../../contexts/StudioContext"; |
| 10 | +import { BlockParamsPanel } from "./BlockParamsPanel"; |
| 11 | + |
| 12 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 13 | + |
| 14 | +vi.useFakeTimers(); |
| 15 | + |
| 16 | +const PARAMS: BlockParam[] = [ |
| 17 | + { key: "--bg-color", label: "Background", type: "color", default: "#0c0c0c" }, |
| 18 | + { key: "--text-color", label: "Text color", type: "color", default: "#fafafa" }, |
| 19 | +]; |
| 20 | + |
| 21 | +const playbackValue: StudioPlaybackValue = { |
| 22 | + captionEditMode: false, |
| 23 | + compositionLoading: false, |
| 24 | + refreshKey: 0, |
| 25 | + setRefreshKey: vi.fn(), |
| 26 | + timelineElements: [], |
| 27 | + isPlaying: false, |
| 28 | + refreshPreviewDocumentVersion: vi.fn(), |
| 29 | +}; |
| 30 | + |
| 31 | +function makeFileManager(content: { value: string }) { |
| 32 | + const readProjectFile = vi.fn(async () => content.value); |
| 33 | + const writeProjectFile = vi.fn(async (_path: string, next: string) => { |
| 34 | + content.value = next; |
| 35 | + }); |
| 36 | + // The panel only touches read/writeProjectFile; the rest of the context |
| 37 | + // surface is irrelevant to these tests. |
| 38 | + const value = { readProjectFile, writeProjectFile } as unknown as ReturnType< |
| 39 | + typeof useFileManager |
| 40 | + >; |
| 41 | + return { value, readProjectFile, writeProjectFile }; |
| 42 | +} |
| 43 | + |
| 44 | +let root: Root | null = null; |
| 45 | + |
| 46 | +afterEach(() => { |
| 47 | + act(() => root?.unmount()); |
| 48 | + root = null; |
| 49 | + document.body.innerHTML = ""; |
| 50 | + vi.clearAllTimers(); |
| 51 | +}); |
| 52 | + |
| 53 | +function renderPanel(fileManager: ReturnType<typeof makeFileManager>["value"]) { |
| 54 | + const host = document.createElement("div"); |
| 55 | + document.body.append(host); |
| 56 | + root = createRoot(host); |
| 57 | + act(() => { |
| 58 | + root?.render( |
| 59 | + <StudioPlaybackProvider value={playbackValue}> |
| 60 | + <FileManagerProvider value={fileManager}> |
| 61 | + <BlockParamsPanel |
| 62 | + blockName="vfx-demo" |
| 63 | + blockTitle="VFX Demo" |
| 64 | + params={PARAMS} |
| 65 | + compositionPath="compositions/vfx-demo.html" |
| 66 | + onClose={vi.fn()} |
| 67 | + /> |
| 68 | + </FileManagerProvider> |
| 69 | + </StudioPlaybackProvider>, |
| 70 | + ); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +function changeParam(label: string, next: string) { |
| 75 | + const input = document.querySelector<HTMLInputElement>(`input[aria-label="${label} value"]`); |
| 76 | + if (!input) throw new Error(`no input for ${label}`); |
| 77 | + act(() => { |
| 78 | + const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set; |
| 79 | + setter?.call(input, next); |
| 80 | + input.dispatchEvent(new Event("input", { bubbles: true })); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +async function flushCommit() { |
| 85 | + await act(async () => { |
| 86 | + vi.advanceTimersByTime(350); |
| 87 | + // Drain the read → guard → write promise chain. |
| 88 | + await Promise.resolve(); |
| 89 | + await Promise.resolve(); |
| 90 | + await Promise.resolve(); |
| 91 | + await Promise.resolve(); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +describe("BlockParamsPanel commit safety", () => { |
| 96 | + it("refuses a multi-occurrence value and never mutates the unrelated sibling", async () => { |
| 97 | + // The param's default (#0c0c0c) also appears on an UNRELATED element. The |
| 98 | + // panel has no way to tell which occurrence belongs to the param, so it |
| 99 | + // must refuse rather than risk rewriting `.unrelated`. Nothing is written. |
| 100 | + const content = { |
| 101 | + value: ".block { background: #0c0c0c; } .unrelated { border-color: #0c0c0c; }", |
| 102 | + }; |
| 103 | + const before = content.value; |
| 104 | + const fm = makeFileManager(content); |
| 105 | + renderPanel(fm.value); |
| 106 | + |
| 107 | + changeParam("Background", "#123456"); |
| 108 | + await flushCommit(); |
| 109 | + |
| 110 | + expect(fm.writeProjectFile).not.toHaveBeenCalled(); |
| 111 | + expect(content.value).toBe(before); // both occurrences untouched |
| 112 | + expect(content.value).toContain(".unrelated { border-color: #0c0c0c; }"); |
| 113 | + expect(document.body.textContent).toContain("appears 2×"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("writes a unique occurrence with token boundaries (no substring corruption)", async () => { |
| 117 | + // #0c0c0c is unique here — the 8-digit #0c0c0cff must NOT match (token |
| 118 | + // boundary), so exactly one occurrence rewrites and the hex8 is preserved. |
| 119 | + const content = { |
| 120 | + value: ".a { background: #0c0c0c; } .c { color: #0c0c0cff; }", |
| 121 | + }; |
| 122 | + const fm = makeFileManager(content); |
| 123 | + renderPanel(fm.value); |
| 124 | + |
| 125 | + changeParam("Background", "#123456"); |
| 126 | + await flushCommit(); |
| 127 | + |
| 128 | + expect(fm.writeProjectFile).toHaveBeenCalledTimes(1); |
| 129 | + expect(content.value).toBe(".a { background: #123456; } .c { color: #0c0c0cff; }"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("refuses once a previously-unique value later collides with unrelated content", async () => { |
| 133 | + const content = { |
| 134 | + value: ".a { background: #0c0c0c; } .b { color: #fafafa; }", |
| 135 | + }; |
| 136 | + const fm = makeFileManager(content); |
| 137 | + renderPanel(fm.value); |
| 138 | + |
| 139 | + // #0c0c0c is unique → first commit writes; now #fafafa appears 2×. |
| 140 | + changeParam("Background", "#fafafa"); |
| 141 | + await flushCommit(); |
| 142 | + expect(content.value).toBe(".a { background: #fafafa; } .b { color: #fafafa; }"); |
| 143 | + |
| 144 | + // Next edit's current value (#fafafa) now matches 2× → refuse. |
| 145 | + changeParam("Background", "#ff0000"); |
| 146 | + await flushCommit(); |
| 147 | + |
| 148 | + expect(fm.writeProjectFile).toHaveBeenCalledTimes(1); // no second write |
| 149 | + expect(content.value).toBe(".a { background: #fafafa; } .b { color: #fafafa; }"); |
| 150 | + expect(document.body.textContent).toContain("appears 2×"); |
| 151 | + }); |
| 152 | + |
| 153 | + it("keeps a pending commit for one param when another param is edited", async () => { |
| 154 | + const content = { |
| 155 | + value: ".a { background: #0c0c0c; } .b { color: #fafafa; }", |
| 156 | + }; |
| 157 | + const fm = makeFileManager(content); |
| 158 | + renderPanel(fm.value); |
| 159 | + |
| 160 | + // Edit both params back-to-back within the 300ms debounce window: the |
| 161 | + // second edit must not cancel the first param's pending commit. |
| 162 | + changeParam("Background", "#111111"); |
| 163 | + changeParam("Text color", "#222222"); |
| 164 | + await flushCommit(); |
| 165 | + await flushCommit(); |
| 166 | + |
| 167 | + expect(content.value).toContain("#111111"); |
| 168 | + expect(content.value).toContain("#222222"); |
| 169 | + }); |
| 170 | +}); |
0 commit comments