Files
hyperframes/packages/studio/src/components/EditorShell.readOnlyHostMount.test.tsx
T
Miguel Ángel 84e6531668 feat(studio): a host can make the preview read-only (#4200)
* feat(studio): a host can make the preview read-only

Gates the shared geometry commit funnel (offset/size/rotation), so the
inspector's manual X/Y/W/H/rotation fields refuse the same as canvas
drag/resize/rotate. The flat inspector's fields render disabled with a
host-supplied reason. Fixes a hooks-order crash on deselect found in review.

* refactor(studio): derive the read-only preview flag from context, not a synced store

EditorShell synced its readOnlyPreview prop into a zustand store via a raw
useEffect behind an eslint-disable of no-restricted-syntax. Replace it with
a PreviewReadOnlyProvider context computed fresh from the prop every render:
Tier-1 consumers (genuine EditorShell descendants) read it via useContext;
Tier-2 consumers (useAppHotkeys, useGestureCommit, the
useDomEditSession/useDomEditCommits/useDomGeometryCommits chain), which run
in App.tsx above EditorShell, take it as an explicit parameter threaded from
StudioApp's own prop instead. Adds a host-mount test proving EditorShell
gates its descendants from its own prop with no StudioApp/App.tsx involved.

* fix(studio): keep preview hotkeys context explicit

* fix(studio): close read-only interaction escapes

* fix(studio): preserve timeline ownership in read-only preview

* fix(studio): rollback read-only preview work

* fix(studio): keep hotkey ownership on the active surface

* fix(studio): format preview overlay and strengthen split test

* fix(studio): clear preview gate findings

* refactor(studio): share preview test fixtures
2026-09-20 10:10:57 -04:00

117 lines
4.0 KiB
TypeScript

// @vitest-environment happy-dom
// A host that mounts EditorShell directly — no StudioApp, no App.tsx — must
// still get its descendants gated purely from the readOnlyPreview prop.
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import "./EditorShellTestMocks";
import { EditorShell } from "./EditorShell";
import { usePreviewBlockDrop } from "./nle/usePreviewBlockDrop";
import { TIMELINE_BLOCK_MIME } from "../utils/timelineAssetDrop";
vi.mock("../hooks/useTimelineSelectionPreviewSync", () => ({
useTimelineSelectionPreviewSync: vi.fn(),
}));
vi.mock("./nle/PreviewOverlays", () => ({ PreviewOverlays: () => null }));
// The real hook — this is the actual Tier-1 consumer a host relies on to gate
// a drop onto the preview. Only PreviewPane's own DOM/player chrome is mocked.
const stage = document.createElement("div");
stage.getBoundingClientRect = () => ({ left: 0, top: 0, width: 100, height: 100 }) as DOMRect;
const dropHandlers: { current: ReturnType<typeof usePreviewBlockDrop> | null } = { current: null };
vi.mock("./nle/PreviewPane", () => ({
PreviewPane: (props: { onPreviewBlockDrop?: (name: string, pos: unknown) => void }) => {
dropHandlers.current = usePreviewBlockDrop({
stageRef: { current: stage },
compositionSize: { width: 1000, height: 1000 },
onBlockDrop: props.onPreviewBlockDrop,
});
return null;
},
}));
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let root: ReturnType<typeof createRoot> | null = null;
afterEach(() => {
if (root) act(() => root!.unmount());
root = null;
document.body.innerHTML = "";
});
function mountShellAlone(
readOnlyPreview: boolean,
onPreviewBlockDrop: (...args: unknown[]) => void,
) {
const host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
act(() =>
root!.render(
<EditorShell
panels={null}
timelineToolbar={null}
renderClipContent={() => null}
handleTimelineElementDelete={vi.fn()}
handleTimelineAssetDrop={vi.fn()}
handleTimelineFileDrop={vi.fn()}
handleTimelineElementMove={vi.fn()}
handleTimelineElementsMove={vi.fn()}
handleTimelineElementResize={vi.fn()}
handleTimelineGroupResize={vi.fn()}
handleToggleTrackHidden={vi.fn()}
setAudioGroupAttribute={{ setLive: vi.fn(), setQuiet: vi.fn() }}
handleBlockedTimelineEdit={vi.fn()}
handleTimelineElementSplit={vi.fn()}
handleRazorSplit={vi.fn()}
handleRazorSplitAll={vi.fn()}
onCopyClip={vi.fn(() => false)}
onPasteClip={vi.fn(async () => {})}
onDuplicateClip={vi.fn(async () => false)}
canPasteClip={vi.fn(() => false)}
setCompIdToSrc={vi.fn()}
setCompositionLoading={vi.fn()}
shouldShowMotionPath={false}
shouldShowSelectedDomBounds={false}
handlePreviewBlockDrop={onPreviewBlockDrop}
readOnlyPreview={readOnlyPreview}
/>,
),
);
return { host, root };
}
function dropEvent() {
return {
clientX: 50,
clientY: 50,
preventDefault: vi.fn(),
dataTransfer: {
types: [TIMELINE_BLOCK_MIME],
getData: () => JSON.stringify({ name: "lower-third" }),
},
};
}
describe("a host mounting EditorShell alone (no StudioApp, no App.tsx)", () => {
it("gates a Tier-1 descendant's drop handling purely from the prop", () => {
const onBlockDrop = vi.fn();
mountShellAlone(true, onBlockDrop);
const event = dropEvent();
act(() => dropHandlers.current!.handleDrop(event as never));
expect(onBlockDrop).not.toHaveBeenCalled();
expect(event.preventDefault).not.toHaveBeenCalled();
});
it("control: with the flag off, the same descendant still places a drop", () => {
const onBlockDrop = vi.fn();
mountShellAlone(false, onBlockDrop);
const event = dropEvent();
act(() => dropHandlers.current!.handleDrop(event as never));
expect(onBlockDrop).toHaveBeenCalledWith("lower-third", { left: 500, top: 500 });
});
});