// @vitest-environment happy-dom import React, { act } from "react"; import { afterEach, describe, expect, it, vi } from "vitest"; import { useTimelineAssetDropOps } from "./useTimelineAssetDropOps"; import { mountReactHarness } from "./domSelectionTestHarness"; import { usePlayerStore } from "../player/store/playerStore"; import type { TimelineElement } from "../player"; import type { TimelineDropPlacement } from "../player/components/timelineCallbacks"; (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; afterEach(() => { document.body.innerHTML = ""; vi.unstubAllGlobals(); usePlayerStore.getState().reset(); }); type DropFn = ReturnType["handleTimelineAssetDrop"]; type FileDropFn = ReturnType["handleTimelineFileDrop"]; type CompositionDropFn = ReturnType< typeof useTimelineAssetDropOps >["handleTimelineCompositionDrop"]; function renderDropHook( sourceContent: string, writeProjectFile: (path: string, content: string, expectedContent?: string) => Promise, timelineElements: TimelineElement[] = [], checkEditable?: (targets: readonly TimelineElement[]) => boolean, uploadProjectFiles: (files: Iterable, dir?: string) => Promise = vi .fn() .mockResolvedValue([]), ) { vi.stubGlobal( "fetch", vi.fn().mockResolvedValue({ ok: true, json: async () => ({ content: sourceContent }) }), ); let drop: DropFn | null = null; let fileDrop: FileDropFn | null = null; let compositionDrop: CompositionDropFn | null = null; function Harness() { const handlers = useTimelineAssetDropOps({ projectIdRef: { current: "project" }, activeCompPath: "index.html", timelineElements, showToast: vi.fn(), writeProjectFile, recordEdit: vi.fn().mockResolvedValue(undefined), reloadPreview: vi.fn(), uploadProjectFiles, checkEditable, }); drop = handlers.handleTimelineAssetDrop; fileDrop = handlers.handleTimelineFileDrop; compositionDrop = handlers.handleTimelineCompositionDrop; return null; } mountReactHarness(); return Object.assign(() => drop!, { file: () => fileDrop!, composition: () => compositionDrop!, }); } describe("useTimelineAssetDropOps handleTimelineAssetDrop", () => { it("grows the root duration when the drop lands past the current end", async () => { const source = '
'; const writeProjectFile = vi.fn().mockResolvedValue(undefined); const getDrop = renderDropHook(source, writeProjectFile); await act(async () => { await getDrop()("clip.mp4", { start: 8, track: 0 }, 5); }); const [, written] = writeProjectFile.mock.calls[0] as [string, string]; expect(written).toContain('data-duration="13"'); }); it("leaves the root duration alone when the drop lands inside the current end", async () => { const source = '
'; const writeProjectFile = vi.fn().mockResolvedValue(undefined); const getDrop = renderDropHook(source, writeProjectFile); await act(async () => { await getDrop()("clip.mp4", { start: 1, track: 0 }, 2); }); const [, written] = writeProjectFile.mock.calls[0] as [string, string]; expect(written).toContain('data-duration="10"'); }); it("selects and reveals the newly dropped clip", async () => { const source = '
'; const writeProjectFile = vi.fn().mockResolvedValue(undefined); const getDrop = renderDropHook(source, writeProjectFile); await act(async () => { await getDrop()("clip.mp4", { start: 1, track: 0 }, 2); }); expect(usePlayerStore.getState().selectedElementId).toBe("index.html#clip"); }); it("measures the insert row against every manifest timeline row", async () => { const clip = (id: string, track: number): TimelineElement => ({ id, key: id, tag: "div", start: 0, duration: 2, track, authoredTrack: track, hfId: `hf-${id}`, domId: id, }); const source = [ '
', ...["a:0", "hidden:1", "b:2"].map((t) => { const [id, track] = t.split(":"); return `
`; }), "
", ].join("\n"); const writeProjectFile = vi.fn().mockResolvedValue(undefined); const getDrop = renderDropHook(source, writeProjectFile, [ clip("a", 0), clip("hidden", 1), clip("b", 2), ]); await act(async () => { await getDrop()("clip.mp4", { start: 1, track: 1, insertRow: 1, trackOrder: [0, 2] }, 2); }); const [, written] = writeProjectFile.mock.calls[0] as [string, string]; expect(written).toContain('id="b" data-start="0" data-track-index="3"'); expect(written).toContain('id="hidden" data-start="0" data-track-index="2"'); expect(written).toMatch(/