fix(studio): sealed previews fill scene children, clip parents and first-frame counts (#8180)

* test(studio): a sealed scene's children and clip parents fill as an open preview's do

* fix(studio): a sealed preview's scene children and clip parents come from the editing port

* test(studio): a sealed preview's first-frame event carries its clips and length

* fix(studio): a sealed preview's first-frame event counts its clips from the document copy

* refactor(studio): drop two comments the names already carry
This commit is contained in:
Miguel Ángel
2026-11-18 13:35:14 +00:00
committed by GitHub
parent 67410727e6
commit 579feb02ee
5 changed files with 124 additions and 20 deletions
@@ -0,0 +1,43 @@
// @vitest-environment happy-dom
import { act, createElement } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, expect, it, vi } from "vitest";
import { sealedPreviewFrame } from "../lib/sealedPreviewFrame.test-helpers";
import { usePreviewFirstFrameTelemetry } from "./usePreviewFirstFrameTelemetry";
const firstFrames = vi.hoisted(() => [] as Array<Record<string, unknown>>);
vi.mock("../../telemetry/events", () => ({
trackPreviewFirstFrame: (props: Record<string, unknown>) => firstFrames.push(props),
}));
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
afterEach(() => {
document.body.innerHTML = "";
firstFrames.length = 0;
});
// fails without the fix: a sealed frame's document read as null, so the event said 0 clips and 0 seconds.
it("reports a sealed preview's clips and length on its first frame", async () => {
document.body.innerHTML = `<div data-composition-id="main" data-duration="6">
<div id="title" class="clip" data-start="0" data-duration="2"></div>
<video id="shot" class="clip" data-start="2" data-duration="4" src="shot.mp4"></video>
</div>`;
const slot = { gen: 1, role: "live" as const };
let report: ReturnType<typeof usePreviewFirstFrameTelemetry> | undefined;
function Harness() {
report = usePreviewFirstFrameTelemetry([slot]);
return null;
}
act(() => createRoot(document.createElement("div")).render(createElement(Harness)));
report!(slot, { iframe: sealedPreviewFrame().iframe, startedAt: performance.now(), loadId: 1 });
await vi.waitFor(() => expect(firstFrames).toHaveLength(1));
expect(firstFrames[0]).toMatchObject({
composition_seconds: 6,
clip_count: 2,
media_clip_count: 1,
});
});
@@ -4,6 +4,7 @@ import { readTimelineDurationFromDocument } from "../lib/timelineElementHelpers"
import type { PreviewIframeSlot } from "./useTimelineSyncCallbacks";
import { trackPreviewFirstFrame } from "../../telemetry/events";
import { getBrowserSystemMeta } from "../../telemetry/system";
import { previewDocumentCopy, previewFrameOpaque } from "../lib/previewChannel";
export function readPreviewComplexity(doc: Document | null | undefined): {
clip_count: number;
@@ -38,13 +39,16 @@ export function usePreviewFirstFrameTelemetry(previewSlots: PreviewIframeSlot[])
const reportKey = `${slot.role}:${slot.gen}:${loadId}`;
if (reportedPaintsRef.current.has(reportKey)) return;
reportedPaintsRef.current.add(reportKey);
const complexity = readPreviewComplexity(iframe.contentDocument);
trackPreviewFirstFrame({
duration_ms: Math.max(0, Math.round(performance.now() - startedAt)),
composition_seconds: readTimelineDurationFromDocument(iframe.contentDocument),
...complexity,
studio_version: getBrowserSystemMeta().studio_version,
});
const durationMs = Math.max(0, Math.round(performance.now() - startedAt));
const report = (doc: Document | null) =>
trackPreviewFirstFrame({
duration_ms: durationMs,
composition_seconds: readTimelineDurationFromDocument(doc),
...readPreviewComplexity(doc),
studio_version: getBrowserSystemMeta().studio_version,
});
if (previewFrameOpaque(iframe)) void previewDocumentCopy(iframe).then(report);
else report(iframe.contentDocument);
},
[],
);
@@ -2,8 +2,8 @@
import { act, createElement } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, expect, it, vi } from "vitest";
import type { TimelineElement } from "../store/playerStore";
import { afterEach, expect, it, onTestFinished, vi } from "vitest";
import { usePlayerStore, type TimelineElement } from "../store/playerStore";
import { useTimelineSyncCallbacks } from "./useTimelineSyncCallbacks";
import { sealedPreviewFrame } from "../lib/sealedPreviewFrame.test-helpers";
@@ -69,3 +69,38 @@ it("binds a sealed preview's timeline row to its element, as an open preview doe
audioGroup: "voice",
});
});
// fails without the fix: reading the frame's `__clipTree` threw, so a sealed scene's children and parents stayed empty.
it("fills a sealed scene's children and the runtime's clip parents", async () => {
document.body.innerHTML = `<div data-composition-id="main" data-composition-file="index.html">
<div id="scene" data-composition-id="scene" data-composition-file="scene.html" data-start="0" data-duration="2">
<h1 id="title">Title</h1>
</div>
</div>`;
(window as unknown as { __clipTree: unknown }).__clipTree = {
roots: [
{ id: "scene", parentId: null, children: [{ id: "cue", parentId: "scene", children: [] }] },
],
};
onTestFinished(() => delete (window as unknown as { __clipTree?: unknown }).__clipTree);
usePlayerStore.getState().setClipParentMap(new Map());
usePlayerStore.getState().setDomClipChildren([]);
const { process } = processWith(sealedPreviewFrame().iframe);
process({
clips: [
{
...boxClip,
id: "scene",
label: "scene",
kind: "composition",
tagName: "div",
compositionId: "scene",
},
],
durationInFrames: 60,
});
await vi.waitFor(() => expect(usePlayerStore.getState().clipParentMap.get("cue")).toBe("scene"));
expect(usePlayerStore.getState().domClipChildren.map((child) => child.id)).toContain("title");
});
@@ -15,7 +15,7 @@ import type { PlaybackAdapter, IframeWindow } from "../lib/playbackTypes";
import { readTimelineDurationFromDocument } from "../lib/timelineDOM";
import { buildMissingCompositionElements } from "../lib/timelineIframeHelpers";
import { acceptedRuntimeMessageFps } from "../lib/runtimeProtocol";
import { previewDocumentCopy, previewFrameOpaque } from "../lib/previewChannel";
import { previewClipParents, previewDocumentCopy, previewFrameOpaque } from "../lib/previewChannel";
import { useLinkedClipPreferences } from "../../utils/linkedClipPreferences";
import {
buildTimelineElementsFromClips,
@@ -153,6 +153,15 @@ export function resolveTimelineTotalDuration(input: {
);
}
/** A same-origin frame's clip tree, read in place; a frame that refuses the read has none. */
function openClipParents(iframe: HTMLIFrameElement | null): Map<string, string> {
try {
return clipTreeParentMap(iframe?.contentWindow ?? null);
} catch {
return new Map();
}
}
export function useTimelineSyncCallbacks({
iframeRef,
probeIntervalRef,
@@ -173,14 +182,13 @@ export function useTimelineSyncCallbacks({
}: UseTimelineSyncCallbacksParams) {
// Convert a runtime timeline message (from iframe postMessage) into TimelineElements
const applyTimelineMessage = useCallback(
(data: RuntimeTimelineMessage, iframeDoc: Document | null) => {
(data: RuntimeTimelineMessage, iframeDoc: Document | null, parentMap: Map<string, string>) => {
// Show root-level clips: no parentCompositionId, OR parent is a "phantom wrapper"
const clipCompositionIds = new Set(data.clips.map((c) => c.compositionId).filter(Boolean));
const filtered = data.clips.filter(
(clip) => !clip.parentCompositionId || !clipCompositionIds.has(clip.parentCompositionId),
);
try {
const parentMap = clipTreeParentMap(iframeRef.current?.contentWindow ?? null);
const domClipChildren = collectSubCompositionDomChildren(iframeDoc, data.clips, parentMap);
usePlayerStore.getState().setClipParentMap(parentMap);
usePlayerStore.getState().setDomClipChildren(domClipChildren);
@@ -188,7 +196,7 @@ export function useTimelineSyncCallbacks({
.getState()
.setSubCompositionHostState(collectSubCompositionHostState(iframeDoc, data.clips));
} catch {
// cross-origin or __clipTree not available — maps stay empty
// A document the walk cannot read leaves the maps empty.
}
const els = buildTimelineElementsFromClips(filtered, iframeDoc);
@@ -211,7 +219,7 @@ export function useTimelineSyncCallbacks({
syncTimelineElements,
);
},
[iframeRef, syncTimelineElements],
[syncTimelineElements],
);
const timelineReads = useRef(0);
@@ -222,14 +230,16 @@ export function useTimelineSyncCallbacks({
const iframe = iframeRef.current;
const read = ++timelineReads.current;
if (!iframe || !previewFrameOpaque(iframe)) {
applyTimelineMessage(data, safeContentDocument(iframe));
applyTimelineMessage(data, safeContentDocument(iframe), openClipParents(iframe));
return;
}
// A sealed frame's rows bind to a copy of its document from the editing port; the newest read wins.
void previewDocumentCopy(iframe).then((doc) => {
if (read === timelineReads.current && iframeRef.current === iframe)
applyTimelineMessage(data, doc);
});
// A sealed frame's rows bind to its document copy and clip tree from the editing port; the newest read wins.
void Promise.all([previewDocumentCopy(iframe), previewClipParents(iframe)]).then(
([doc, parents]) => {
if (read === timelineReads.current && iframeRef.current === iframe)
applyTimelineMessage(data, doc, parents);
},
);
},
[iframeRef, applyTimelineMessage],
);
@@ -106,6 +106,18 @@ export async function previewClipManifest(iframe: HTMLIFrameElement): Promise<Cl
return manifest as ClipManifest;
}
export async function previewClipParents(iframe: HTMLIFrameElement): Promise<Map<string, string>> {
const snapshot = await previewRequest(iframe, "runtime-snapshot", {});
const pairs: unknown = snapshot?.clipParents;
const parents = new Map<string, string>();
if (!Array.isArray(pairs)) return parents;
for (const pair of pairs) {
if (Array.isArray(pair) && typeof pair[0] === "string" && typeof pair[1] === "string")
parents.set(pair[0], pair[1]);
}
return parents;
}
export async function previewConsoleErrors(
iframe: HTMLIFrameElement,
from: number,