Skip to content

AI media harness for planning, generating, and editing locally

TypeScript
97.1 KB
Raw Blame
// @vitest-environment jsdom
import { installH3TestDependencies } from "../../lib/h3TestUtils";
import { completeSceneGeneration, sceneInput } from "../../lib/sceneHistory";

import "@testing-library/jest-dom/vitest";
import { act, cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import { useState } from "react";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { createDraftGenerationJob, createProjectConfig, parseProjectConfig, sceneShots, type ProjectConfig } from "../../lib/project";
import { cancelSlopfabGeneration, getEngineStatus, type SlopfabStatus } from "../../lib/runtime";
import { askNative } from "../../lib/nativeShell";
import { GeneratorView, templateSceneBlocker } from "./GeneratorView";
import { ReferencesView } from "./ReferencesView";
import { choose, chooseOption, comboValue, optionNames } from "./comboTestUtils";
import { chooseReference, changePromptChip, insertPromptReference, placePromptCaret, typePrompt } from "./promptTestUtils";
import { saveLoras } from "../../lib/loras";
import { createGeneratorTemplate, mossImageTemplate, saveGeneratorTemplateSettings, minimaxOriginalTemplate, viggleAnimateTemplate, saveDebugOptionsEnabled } from "../../lib/settings";
import { saveActiveGeneratorId, loadActiveGeneratorId } from "../../lib/settings";

vi.mock("../../lib/nativeShell", async (importOriginal) => ({
  ...await importOriginal<typeof import("../../lib/nativeShell")>(),
  askNative: vi.fn().mockResolvedValue(true),
}));

vi.mock("../../lib/runtime", async (importOriginal) => ({
  ...await importOriginal<typeof import("../../lib/runtime")>(),
  cancelSlopfabGeneration: vi.fn().mockResolvedValue(true),
  getEngineStatus: vi.fn(),
}));

beforeAll(() => vi.spyOn(HTMLMediaElement.prototype, "load").mockImplementation(() => undefined));
beforeEach(() => {
  localStorage.clear(); installH3TestDependencies();
  vi.clearAllMocks();
});
afterEach(cleanup);

it("offers upscale actions above Generate and dismisses them without submitting", () => {
  const state = setup();
  const open = () => fireEvent.click(screen.getByRole("button", { name: "Scene generation actions" }));
  open();
  const dialog = screen.getByRole("dialog", { name: "Scene generation actions" });
  for (const scale of [1.5, 2, 4]) expect(within(dialog).getByRole("button", { name: `Upscale ${scale}x` })).toBeDisabled();
  fireEvent.keyDown(dialog, { key: "Escape" });
  expect(screen.queryByRole("dialog", { name: "Scene generation actions" })).toBeNull();
  open();
  fireEvent.pointerDown(document.body);
  expect(screen.queryByRole("dialog", { name: "Scene generation actions" })).toBeNull();
  expect(state.latest().generationJobs.every(job => job.status === "draft")).toBe(true);
});

it("submits the selected Take to upscale and closes the action popup", () => {
  Object.assign(window, { __TAURI_INTERNALS__: {} });
  try {
    let config = project();
    const job = config.generationJobs[0];
    config = completeSceneGeneration(config, job.id, { id: "take", scene: sceneInput(job), createdAt: config.createdAt, latentRelativePath: "latents/take.safetensors",
      media: { id: `asset-${job.id}`, kind: "generated", mimeType: "video/mp4", name: job.title, relativePath: "media/take.mp4", createdAt: config.createdAt } });
    const generate = vi.fn();
    render(<GeneratorView config={config} folderPath="C:/project" onChange={() => {}} onOpenTimeline={() => {}} onGenerate={generate} />);
    fireEvent.click(screen.getByRole("button", { name: "Scene generation actions" }));
    fireEvent.click(screen.getByRole("button", { name: "Upscale 2x" }));
    expect(generate).toHaveBeenCalledWith([expect.objectContaining({ upscaleSource: expect.objectContaining({ id: "take" }), request: expect.objectContaining({ latentUpscaleSource: { path: "latents/take.safetensors", scale: 2 } }) })]);
    expect(screen.queryByRole("dialog", { name: "Scene generation actions" })).toBeNull();
  } finally { delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__; }
});

it("excludes MOSS from the video picker even when it is the saved default", () => {
  const moss = { ...mossImageTemplate(), sources: {}, paths: { transformer: "moss", textEncoder: "qwen", videoVae: "flux", tokenizer: "tokenizer.json", audioVae: "" } };
  const video = createGeneratorTemplate("Video model");
  saveGeneratorTemplateSettings({ templates: [moss, video], defaultTemplateId: moss.id, catalogVersion: 17 });
  setup();
  const picker = screen.getByRole("combobox", { name: /Video generator template/ });
  expect(picker).toHaveTextContent("Video model");
  fireEvent.click(picker);
  expect(screen.queryByRole("option", { name: "MOSS Image" })).toBeNull();
  expect(templateSceneBlocker("prompt", moss)).toMatch(/image generation only/);
});

it.each(["scene", "shot"])("generates only the selected scene from the %s inspector", (view) => {
  const state = setup();
  fireEvent.click(screen.getByRole("button", { name: view === "shot" ? "Shot 1 of Second scene" : "Select scene Second scene" }));
  const panel = within(screen.getByRole("complementary", { name: view === "shot" ? "Shot 1 of Second scene" : "Scene: Second scene" }));
  fireEvent.click(panel.getByRole("button", { name: "Generate" }));
  expect(state.latest().generationJobs.map((job) => job.status)).toEqual(["draft", "queued"]);
  expect(panel.getByRole("button", { name: "Cancel" })).toBeEnabled();
});

it("uses the active video generator for every submitted scene instead of their saved generators", () => {
  const saved = createGeneratorTemplate("Saved generator"), active = createGeneratorTemplate("Active generator");
  saveGeneratorTemplateSettings({ templates: [saved, active], defaultTemplateId: saved.id });
  saveActiveGeneratorId("video", active.id);
  const config = project();
  config.generationJobs = config.generationJobs.map((job) => ({ ...job, generatorTemplateId: saved.id }));
  const generate = vi.fn();
  render(<GeneratorView config={config} folderPath="C:/project" runtime={readyRuntime} onChange={() => {}} onOpenTimeline={() => {}} onGenerate={generate} />);
  fireEvent.click(screen.getByRole("button", { name: "Select scene Second scene" }));
  expect(screen.getByRole("combobox", { name: /Video generator template/ })).toHaveTextContent(active.name);
  fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
  expect(generate).toHaveBeenCalledOnce();
  expect(generate.mock.calls[0][0]).toHaveLength(config.generationJobs.length);
  expect(generate.mock.calls[0][0].every((submission: { template: { id: string } }) => submission.template.id === active.id)).toBe(true);
});

it.each([
  ["queued", "scene"], ["generating", "scene"], ["queued", "shot"], ["generating", "shot"],
] as const)("cancels only the %s scene from the %s inspector", (status, view) => {
  const initial = project();
  initial.generationJobs.forEach((job) => { job.status = status; });
  setup(initial);
  fireEvent.click(screen.getByRole("button", { name: view === "shot" ? "Shot 1 of Second scene" : "Select scene Second scene" }));
  const panel = within(screen.getByRole("complementary", { name: view === "shot" ? "Shot 1 of Second scene" : "Scene: Second scene" }));
  fireEvent.click(panel.getByRole("button", { name: "Cancel" }));
  expect(cancelSlopfabGeneration).toHaveBeenCalledExactlyOnceWith("scene-second");
  expect(panel.getByRole("button", { name: "Cancelling…" })).toBeDisabled();
});

it("disables inspector generation while the scene is being saved", () => {
  const initial = project();
  initial.generationJobs[0].status = "ready";
  setup(initial);
  const panel = within(screen.getByRole("complementary", { name: "Scene: First scene" }));
  expect(panel.getByRole("button", { name: "Generate" })).toBeDisabled();
});

it("defaults to Prompt and switches frame conditioning off and back on without losing selections", () => {
  const initial = project();
  initial.references = [{ id: "still", kind: "image", name: "Still", description: "", intendedUse: [], sourcePath: "C:/still.png", createdAt: initial.createdAt }];
  const state = setup(initial);
  expect(comboValue(screen.getByRole("combobox", { name: "Scene type" }))).toBe("prompt");
  expect(screen.queryByLabelText("Start frame for this scene")).toBeNull();
  expect(screen.queryByLabelText("Last frame for this scene")).toBeNull();
  choose("Scene type", "First & last frame");
  chooseReference("Start frame for this scene", "Still");
  chooseReference("Last frame for this scene", "Still");
  choose("Scene type", "Prompt");
  expect(screen.queryByLabelText("Start frame for this scene")).toBeNull();
  expect(screen.queryByLabelText("Last frame for this scene")).toBeNull();
  fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
  const snapshot = JSON.parse(state.latest().generationJobs[0].generationSnapshot!);
  expect(snapshot.referencePaths).toEqual([]);
  expect(snapshot.prompt).not.toContain("first frame");
  choose("Scene type", "First & last frame");
  expect(state.latest().generationJobs[0]).toMatchObject({ startFrameReferenceId: "still", endFrameReferenceId: "still" });
  expect(screen.getByLabelText("Start frame for this scene")).toBeInTheDocument();
  fireEvent.click(screen.getByRole("button", { name: "Add scene" }));
  expect(state.latest().generationJobs.at(-1)?.sceneType).toBe("prompt");
  expect(comboValue(screen.getByRole("combobox", { name: "Scene type" }))).toBe("prompt");
});

it("queues Long Shot anchors before their intervening bridges", () => {
  const initial = project();
  initial.generationJobs = Array.from({ length: 7 }, (_, i) => createDraftGenerationJob("Keep walking", { id: String(i + 1), sceneType: "long-shot" }));
  const submitted = vi.fn();
  render(<GeneratorView config={initial} folderPath="C:/project" runtime={readyRuntime}
    onChange={vi.fn()} onGenerate={submitted} onOpenTimeline={() => undefined} />);
  fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
  expect(submitted.mock.calls[0][0].map((item: { job: { id: string } }) => item.job.id)).toEqual(["1", "3", "2", "5", "4", "7", "6"]);
  expect(submitted.mock.calls[0][0][2].request.latentBridge).toMatchObject({ leftSceneId: "1", rightSceneId: "3", leftMarginFrames: 17, rightMarginFrames: 17 });
});

it("keeps Single Shot prompts in the shot panel and the ending prompt and previous video in the scene panel", () => {
  const initial = project();
  initial.references = [{ id: "hero", kind: "image", name: "Hero", description: "", intendedUse: [], relativePath: "references/hero.png", createdAt: initial.createdAt }];
  initial.generationJobs[1].sceneType = "single-shot";
  initial.generationJobs.push(createDraftGenerationJob("A regular scene", { id: "ordinary", title: "Ordinary" }));
  const state = setup(initial);
  choose("Scene type", "Single Shot");
  expect(screen.queryByRole("spinbutton", { name: "Continuation" })).not.toBeInTheDocument();
  expect(screen.queryByLabelText("Describe shot 1")).not.toBeInTheDocument();
  const ending = screen.getByLabelText("Ending prompt");
  typePrompt(ending, "Wave goodbye.");
  placePromptCaret(ending);
  fireEvent.focus(ending);
  fireEvent.click(within(screen.getByRole("toolbar", { name: "Ending prompt tools" })).getByRole("button", { name: "Reference" }));
  fireEvent.click(screen.getByRole("button", { name: /^Hero\b/ }));
  expect(state.latest().generationJobs[0].endingPrompt).toContain("@[ref:hero]");
  expect(screen.queryByRole("button", { name: "Add a shot to First scene" })).not.toBeInTheDocument();
  const source = screen.getByRole("combobox", { name: "Single Shot previous video" });
  expect(optionNames(source)).toEqual(["None", "Second scene", "Ordinary"]);
  chooseOption(source, "Second scene");
  expect(state.latest().generationJobs[0].continuationSceneId).toBe("scene-second");
  const context = screen.getByRole("combobox", { name: "Single Shot context video" });
  chooseOption(context, "Second scene");
  expect(state.latest().generationJobs[0].contextSceneId).toBe("scene-second");
  expect(screen.queryByLabelText("Ending prompt")).not.toBeInTheDocument();
  chooseOption(context, "None");
  expect(screen.getByLabelText("Ending prompt")).toBeInTheDocument();
  expect(screen.queryByRole("spinbutton", { name: "Continuation" })).not.toBeInTheDocument();
  expect(screen.queryByText(/middle starts from fresh noise/)).not.toBeInTheDocument();
  fireEvent.click(screen.getByRole("button", { name: "Shot 1 of First scene" }));
  expect(screen.queryByRole("spinbutton", { name: "Continuation" })).not.toBeInTheDocument();
  expect(screen.queryByLabelText("Ending prompt")).not.toBeInTheDocument();
  const main = screen.getByLabelText("Describe shot 1");
  typePrompt(main, "Walk forward.");
  placePromptCaret(main);
  fireEvent.focus(main);
  fireEvent.click(within(screen.getByRole("toolbar", { name: "Describe shot 1 tools" })).getByRole("button", { name: "Reference" }));
  fireEvent.click(screen.getByRole("button", { name: /^Hero\b/ }));
  expect(sceneShots(state.latest().generationJobs[0])[0].action).toContain("@[ref:hero]");
  fireEvent.click(screen.getByRole("button", { name: "Select scene First scene" }));
  expect(screen.getByLabelText("Ending prompt")).toBeInTheDocument();
  expect(screen.queryByLabelText("Describe shot 1")).not.toBeInTheDocument();
  expect(state.latest().generationJobs[0].endingPrompt).toContain("@[ref:hero]");
  fireEvent.click(screen.getByRole("button", { name: "Add scene" }));
  expect(state.latest().generationJobs.at(-1)?.sceneType).toBe("single-shot");
});

it.each(["prompt", "single-shot", "context"] as const)("queues a starting %s scene before Single Shot dependents even when board order is reversed", (type) => {
  const initial = project();
  initial.generationJobs = [
    { ...createDraftGenerationJob("Next", { id: "next", sceneType: "single-shot" }), endingPrompt: "Finish next", continuationSceneId: "start", contextSceneId: type === "context" ? "start" : null },
    createDraftGenerationJob("Start", { id: "start", sceneType: type === "context" ? "prompt" : type }),
  ];
  const submitted = vi.fn();
  render(<GeneratorView config={initial} folderPath="C:/project" runtime={readyRuntime} onChange={vi.fn()} onGenerate={submitted} onOpenTimeline={() => {}} />);
  fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
  const submissions = submitted.mock.calls[0][0];
  expect(submissions.map((item: { job: { id: string } }) => item.job.id)).toEqual(["start", "next"]);
  if (type === "single-shot") expect(submissions[0].request.singleShot.endingPrompt).toBe(submissions[0].request.prompt);
  else expect(submissions[0].request.singleShot).toBeUndefined();
  if (type === "context") {
    expect(submissions[1].request.contextSceneId).toBe("start");
    expect(submissions[1].request.singleShot.endingPrompt).toBe("");
  } else expect(submissions[1].request.singleShot.endingPrompt).toContain("Finish next");
  expect(submissions[1].request.previousSceneId).toBe("start");
});

it.each(["queued", "generating", "ready", "draft", "failed"] as const)("allows Single Shot to wait for a source that is %s", (status) => {
  const initial = project();
  initial.generationJobs = [
    { ...createDraftGenerationJob("Start", { id: "start" }), status },
    { ...createDraftGenerationJob("Next", { id: "next", sceneType: "single-shot" }), continuationSceneId: "start" },
  ];
  const submitted = vi.fn();
  render(<GeneratorView config={initial} folderPath="C:/project" runtime={readyRuntime} selectedJobId="next" onChange={vi.fn()} onGenerate={submitted} onOpenTimeline={() => {}} />);
  const generate = within(screen.getByRole("complementary", { name: "Scene: Next" })).getByRole("button", { name: "Generate" });
  if (["draft", "failed"].includes(status)) { expect(generate).toBeDisabled(); return; }
  expect(generate).toBeEnabled(); fireEvent.click(generate);
  expect(submitted.mock.calls[0][0]).toHaveLength(1);
  expect(submitted.mock.calls[0][0][0].request.previousSceneId).toBe("start");
});

it("preserves saved Long Shot controls but offers Bridge and creates new Prompt scenes", () => {
  const initial = project();
  initial.generationJobs.push(createDraftGenerationJob("Keep walking", { id: "third" }));
  initial.generationJobs.forEach((job) => { job.sceneType = "long-shot"; });
  const state = setup(initial);
  fireEvent.click(screen.getByRole("button", { name: "Select scene Second scene" }));
  const picker = screen.getByRole("combobox", { name: "Scene type" });
  expect(picker).toHaveTextContent("Long Shot (legacy)");
  const options = optionNames(picker);
  expect(options).not.toContain("Long Shot");
  expect(options).toContain("Bridge");
  choose("Edit previous ending", "34 frames (1.42 s)");
  choose("Edit following beginning", "0 frames (0.00 s)");
  expect(state.latest().generationJobs[1]).toMatchObject({ sceneType: "long-shot", bridgeLeftMargin: 34, bridgeRightMargin: 0 });
  expect(screen.queryByLabelText("Start frame for this scene")).toBeNull();
  fireEvent.click(screen.getByRole("button", { name: "Add scene" }));
  expect(state.latest().generationJobs.at(-1)?.sceneType).toBe("prompt");
});

it.each([2, 4, 6])("queues every scene in a %s-scene Long Shot, ending with a continuation", (count) => {
  const initial = project();
  initial.generationJobs = Array.from({ length: count }, (_, i) => createDraftGenerationJob("Keep walking", { id: String(i + 1), sceneType: "long-shot" }));
  const submitted = vi.fn();
  render(<GeneratorView config={initial} folderPath="C:/project" runtime={readyRuntime}
    onChange={vi.fn()} onGenerate={submitted} onOpenTimeline={() => undefined} />);
  fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
  const items = submitted.mock.calls[0][0];
  expect(items.map((item: { job: { id: string } }) => item.job.id)).toEqual(
    count === 2 ? ["1", "2"] : count === 4 ? ["1", "3", "2", "4"] : ["1", "3", "2", "5", "4", "6"]);
  expect(items.at(-1).request).toMatchObject({ previousSceneId: String(count - 1), continuationFrom: "end", continuationOverlapFrames: 22 });
  expect(items.at(-1).request.latentBridge).toBeUndefined();
});

it("hides unfinished templates from the generator picker", () => {
  setup();
  fireEvent.click(screen.getByRole("combobox", { name: /Video generator template/ }));
  expect(screen.getByRole("option", { name: "Default" })).toBeInTheDocument();
  expect(screen.queryByRole("option", { name: "First/Last Frame" })).toBeNull();
});

it("generates the selected backdrop with the scene action and restores it on reopen", () => {
  const state = setup();
  fireEvent.click(screen.getByRole("button", { name: "Select scene First scene" }));
  choose("Scene type", "Backdrop");
  choose("Scene backdrop color", "White");
  expect(state.latest().generationJobs[0]).toMatchObject({ sceneType: "backdrop", backdropColor: "white" });
  expect(screen.queryByLabelText("Start frame for this scene")).toBeNull();
  fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
  const saved = parseProjectConfig(JSON.parse(JSON.stringify(state.latest())));
  const snapshot = JSON.parse(saved.generationJobs[0].generationSnapshot!);
  expect(snapshot.prompt).toContain("solid white (#ffffff)");
  expect(saved.generationJobs[0].backdropColor).toBe("white");
});

it("unbinds a reference after its last prompt chip is removed and excludes it from generation and Used by", () => {
  const initial = project();
  initial.references = ["hero", "opening"].map((id) => ({ id, kind: "image", name: id, description: "", intendedUse: [],
    relativePath: `references/${id}.png`, createdAt: initial.createdAt }));
  initial.generationJobs[0].referenceIds = ["hero"];
  initial.generationJobs[0].startFrameReferenceId = "opening";
  initial.generationJobs[0].shots!.forEach((shot) => { shot.action = "@[ref:hero] walks."; });
  const state = setup(initial);
  fireEvent.click(screen.getByRole("button", { name: "Shot 1 of First scene" }));
  changePromptChip(screen.getByLabelText("Describe shot 1"), 0, null);
  expect(state.latest().generationJobs[0].referenceIds).toEqual(["hero"]);
  fireEvent.click(screen.getByRole("button", { name: "Shot 2 of First scene" }));
  typePrompt(screen.getByLabelText("Describe shot 2"), "A walk through the forest.");
  expect(state.latest().generationJobs[0].referenceIds).toEqual([]);
  fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
  const saved = parseProjectConfig(JSON.parse(JSON.stringify(state.latest())));
  const snapshot = JSON.parse(saved.generationJobs[0].generationSnapshot!);
  expect(snapshot.referencePaths).toHaveLength(1);
  expect(snapshot.referencePaths[0]).toContain("opening.png");
  expect(snapshot.prompt).not.toContain("<Subject");
  cleanup();
  const { container } = render(<ReferencesView config={saved} folderPath="C:/project" onChange={vi.fn()} />);
  expect(container.querySelector(".reference-used-by")).toHaveTextContent("No scenes yet.");
  fireEvent.click(screen.getByRole("option", { name: /opening/ }));
  expect(within(container.querySelector(".reference-used-by")! as HTMLElement).getByRole("button", { name: "First scene" })).toBeInTheDocument();
});

it("replaces a chip's binding and removes bindings when its shot is deleted", async () => {
  const initial = project();
  initial.references = ["Hero", "Forest"].map((name) => ({ id: name.toLowerCase(), kind: "image", name, description: "", intendedUse: [],
    relativePath: `references/${name}.png`, createdAt: initial.createdAt }));
  initial.generationJobs[0].referenceIds = ["hero"];
  initial.generationJobs[0].shots![1].action = "@[ref:hero] walks.";
  const state = setup(initial);
  fireEvent.click(screen.getByRole("button", { name: "Shot 2 of First scene" }));
  changePromptChip(screen.getByLabelText("Describe shot 2"), 0, "Forest");
  expect(state.latest().generationJobs[0].referenceIds).toEqual(["forest"]);
  fireEvent.click(screen.getByRole("button", { name: "Delete shot 2" }));
  await waitFor(() => expect(state.latest().generationJobs[0].referenceIds).toEqual([]));
});

it("has no selected generator when all templates still need downloads", () => {
  localStorage.setItem("slopus.generator-templates.v1", JSON.stringify({ templates: [minimaxOriginalTemplate()], defaultTemplateId: "minimax-h3-original", catalogVersion: 1 }));
  setup();
  expect(screen.getByRole("combobox", { name: "Video generator template: No downloaded generators" })).toBeDisabled();
});

it("animates a blank scene only with a video and repainted frame", () => {
  const template = viggleAnimateTemplate();
  template.additionalSafetensors![0].downloadedPath = "conditioning.safetensors";
  template.paths = { transformer: "viggle.safetensors", textEncoder: "encoder", videoVae: "video", audioVae: "audio", tokenizer: "" };
  localStorage.setItem("slopus.loras.v1", JSON.stringify([{ id: template.loras![0].loraId, name: "Viggle", path: "distillation.safetensors", stepOverride: 4 }]));
  localStorage.setItem("slopus.generator-templates.v1", JSON.stringify({ templates: [template], defaultTemplateId: template.id, catalogVersion: 7 }));
  const initial = project();
  initial.generationJobs = [createDraftGenerationJob("", { id: "blank", title: "Blank scene", sceneType: "animate" })];
  initial.references = [{ id: "motion", kind: "video", name: "Motion", description: "", intendedUse: [],
    sourcePath: "C:/motion.mp4", createdAt: "2026-09-15T00:00:00.000Z",
    video: { startSeconds: 1, durationSeconds: 3, includeAudio: false } },
    { id: "repainted", kind: "image", name: "Repainted frame", description: "", intendedUse: [],
      relativePath: "references/repainted.png", createdAt: "2026-09-15T00:00:00.000Z" }];
  const submitted = vi.fn();
  function Harness() {
    const [config, setConfig] = useState(initial);
    return <GeneratorView config={config} folderPath="C:/project" runtime={readyRuntime}
      onChange={setConfig} onGenerate={submitted} onOpenTimeline={() => undefined} />;
  }
  render(<Harness />);
  const generate = within(screen.getByRole("region", { name: "Blank scene" })).getByRole("button", { name: "Generate" });
  expect(generate).toBeDisabled();
  fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
  expect(submitted.mock.calls.flatMap(([items]) => items)).toHaveLength(0);
  expect(screen.queryByLabelText("The sound of this scene")).toBeNull();
  choose("Reference video for this scene", "Motion");
  expect(generate).toBeDisabled();
  chooseReference("Start frame for this scene", "Repainted frame");
  expect(generate).toBeEnabled();
  fireEvent.click(generate);
  expect(submitted).toHaveBeenLastCalledWith([expect.objectContaining({ request: expect.objectContaining({
    prompt: "", referenceVideos: [{ name: "Motion", sourcePath: "C:/motion.mp4", startSeconds: 1, durationSeconds: 3, includeAudio: false }],
    referencePaths: ["C:/project/references/repainted.png"],
  }) })]);
  chooseReference("Start frame for this scene", "None");
  expect(generate).toBeDisabled();
  chooseReference("Start frame for this scene", "Repainted frame");
  expect(generate).toBeEnabled();
  choose("Reference video for this scene", "None");
  expect(generate).toBeDisabled();
});

const readyRuntime: SlopfabStatus = {
  state: "ready",
  dllPath: "test",
  version: "test",
  platform: "CUDA 13",
  detail: "Ready",
  models: [],
};

it("saves scene types and submits Character Replace with the exact selected references", () => {
  const initial = project();
  initial.generationJobs = [createDraftGenerationJob("", { id: "replace", title: "Replace scene" })];
  initial.references = [
    { id: "motion", kind: "video", name: "Motion", description: "", intendedUse: [], sourcePath: "C:/motion.mp4",
      video: { startSeconds: 1, durationSeconds: 3, includeAudio: false }, createdAt: "2026-09-21T00:00:00.000Z" },
    { id: "hero", kind: "image", name: "New hero", description: "", intendedUse: [], relativePath: "references/hero.png", createdAt: "2026-09-21T00:00:00.000Z" },
  ];
  let latest = initial;
  const submitted = vi.fn();
  function Harness() {
    const [config, setConfig] = useState(initial);
    latest = config;
    return <GeneratorView config={config} folderPath="C:/project" runtime={readyRuntime}
      onChange={setConfig} onGenerate={submitted} onOpenTimeline={() => undefined} />;
  }
  render(<Harness />);
  expect(comboValue(screen.getByRole("combobox", { name: "Scene type" }))).toBe("prompt");
  choose("Scene type", "Character replace");
  expect(latest.generationJobs[0].sceneType).toBe("character-replace");
  expect(screen.queryByLabelText("Start frame for this scene")).toBeNull();
  const generate = within(screen.getByRole("region", { name: "Replace scene" })).getByRole("button", { name: "Generate" });
  expect(generate).toBeDisabled();
  fireEvent.click(screen.getByRole("button", { name: "Shot 1 of Replace scene" }));
  expect(screen.queryByLabelText("Describe shot 1")).toBeNull();
  choose("Video reference for this shot", "Motion");
  expect(generate).toBeDisabled();
  choose("New character reference for this shot", "New hero");
  fireEvent.change(screen.getByLabelText("Character to replace in this shot"), { target: { value: "the person on the left" } });
  expect(generate).toBeEnabled();
  fireEvent.click(generate);
  expect(submitted).toHaveBeenCalledWith([expect.objectContaining({ request: expect.objectContaining({
    prompt: expect.stringContaining("replace the person on the left with <Subject 1>"),
    referencePaths: ["C:/project/references/hero.png"],
    referenceVideos: [{ name: "Motion", sourcePath: "C:/motion.mp4", startSeconds: 1, durationSeconds: 3, includeAudio: false }],
  }) })]);
  expect(parseProjectConfig(JSON.parse(JSON.stringify(latest))).generationJobs[0].shots![0]).toMatchObject({ videoReferenceId: "motion", characterReferenceId: "hero" });
  fireEvent.click(screen.getByRole("button", { name: "Select scene Replace scene" }));
  choose("Scene type", "First & last frame");
  expect(screen.getByLabelText("Start frame for this scene")).toBeInTheDocument();
  choose("Scene type", "Character replace");
  expect(generate).toBeEnabled();
  fireEvent.click(screen.getByRole("button", { name: "Shot 1 of Replace scene" }));
  expect(comboValue(screen.getByRole("combobox", { name: "New character reference for this shot" }))).toBe("hero");
  choose("New character reference for this shot", "None");
  expect(generate).toBeDisabled();
});

it("submits Pose from the scene panel with its video and optional prompt references", () => {
  const initial = project();
  initial.generationJobs = [createDraftGenerationJob("", { id: "pose", title: "Pose scene" })];
  initial.references = [
    { id: "motion", kind: "video", name: "Motion", description: "", intendedUse: [], sourcePath: "C:/motion.mp4",
      video: { startSeconds: 1, durationSeconds: 3, includeAudio: true }, createdAt: "2026-09-22T00:00:00.000Z" },
    { id: "hero", kind: "image", name: "Hero", description: "", intendedUse: [], relativePath: "references/hero.png", createdAt: "2026-09-22T00:00:00.000Z" },
  ];
  let latest = initial;
  const submitted = vi.fn();
  function Harness() {
    const [config, setConfig] = useState(initial);
    latest = config;
    return <GeneratorView config={config} folderPath="C:/project" runtime={readyRuntime}
      onChange={setConfig} onGenerate={submitted} onOpenTimeline={() => undefined} />;
  }
  render(<Harness />);
  choose("Scene type", "Pose");
  expect(screen.queryByLabelText("Start frame for this scene")).toBeNull();
  const generate = within(screen.getByRole("region", { name: "Pose scene" })).getByRole("button", { name: "Generate" });
  expect(generate).toBeDisabled();
  choose("Pose video reference for this scene", "Motion");
  expect(generate).toBeDisabled();
  typePrompt(screen.getByLabelText("Describe shot 1"), "A dancer on a rainy street.");
  expect(generate).toBeEnabled();
  fireEvent.click(generate);
  expect(submitted.mock.calls[0][0][0].request).toMatchObject({
    prompt: expect.stringContaining("Apply the body pose, movement and timing from <Video 1>"),
    referencePaths: [],
    referenceVideos: [{ name: "Motion", sourcePath: "C:/motion.mp4", startSeconds: 1, durationSeconds: 3, includeAudio: false }],
  });
  placePromptCaret(screen.getByLabelText("Describe shot 1"));
  insertPromptReference("Hero");
  expect(latest.generationJobs[0].shots![0].action).toContain("@[ref:hero]");
  fireEvent.click(generate);
  expect(submitted.mock.calls.at(-1)![0][0].request.referencePaths).toEqual(["C:/project/references/hero.png"]);
  expect(parseProjectConfig(JSON.parse(JSON.stringify(latest))).generationJobs[0]).toMatchObject({ sceneType: "pose", poseVideoReferenceId: "motion" });
  choose("Scene type", "First & last frame");
  choose("Scene type", "Pose");
  expect(comboValue(screen.getByRole("combobox", { name: "Pose video reference for this scene" }))).toBe("motion");
  choose("Pose video reference for this scene", "None");
  expect(generate).toBeDisabled();
  expect(templateSceneBlocker("pose", minimaxOriginalTemplate())).toContain("References or Singularity");
  expect(templateSceneBlocker("pose", viggleAnimateTemplate())).toContain("MiniMax prompt");
});

it("keeps an explicitly selected scene type independent of the global generator", () => {
  const initial = project();
  initial.generationJobs[0].sceneType = "animate";
  const state = setup(initial);
  expect(comboValue(screen.getByRole("combobox", { name: "Scene type" }))).toBe("animate");
  expect(screen.getByLabelText("Reference video for this scene")).toBeInTheDocument();
  const first = within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" });
  expect(first).toHaveAttribute("data-tooltip", "Select an Animate generator for this scene.");
  fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
  expect(state.latest().generationJobs[0].status).toBe("draft");
  expect(state.latest().generationJobs[1].status).toBe("queued");
});

it("refuses known first/last-frame weights for Character Replace", () => {
  expect(templateSceneBlocker("character-replace", minimaxOriginalTemplate())).toContain("References or Singularity");
  expect(templateSceneBlocker("character-replace", viggleAnimateTemplate())).toContain("MiniMax prompt");
});

it.each(["extend", "bridge"] as const)("submits %s with ordered video anchors and the shot prompt", (type) => {
  const initial = project();
  initial.generationJobs = [createDraftGenerationJob("The dancer turns toward the door.", { id: "transition", title: "Transition" })];
  initial.references = ["end", "start"].map((id) => ({ id, kind: "video" as const, name: id, description: "", intendedUse: [],
    sourcePath: `C:/${id}.mp4`, video: { startSeconds: 1, durationSeconds: 3, includeAudio: true }, createdAt: "2026-09-21T00:00:00.000Z" }));
  let latest = initial;
  const submitted = vi.fn();
  function Harness() {
    const [config, setConfig] = useState(initial);
    latest = config;
    return <GeneratorView config={config} folderPath="C:/project" runtime={readyRuntime}
      onChange={setConfig} onGenerate={submitted} onOpenTimeline={() => undefined} />;
  }
  render(<Harness />);
  choose("Scene type", type === "extend" ? "Extend" : "Bridge");
  expect(screen.queryByLabelText("Start frame for this scene")).toBeNull();
  const generate = within(screen.getByRole("region", { name: "Transition" })).getByRole("button", { name: "Generate" });
  expect(generate).toBeDisabled();
  choose("Start video reference for this scene", "start");
  if (type === "bridge") {
    expect(generate).toBeDisabled();
    choose("End video reference for this scene", "end");
  } else expect(screen.queryByLabelText("End video reference for this scene")).toBeNull();
  expect(generate).toBeEnabled();
  fireEvent.click(generate);
  const request = submitted.mock.calls[0][0][0].request;
  expect(request.videoTransition).toBe(type);
  expect(request.referenceVideos.map((video: { sourcePath: string }) => video.sourcePath)).toEqual(type === "extend" ? ["C:/start.mp4"] : ["C:/start.mp4", "C:/end.mp4"]);
  expect(request.referenceVideos.every((video: { includeAudio: boolean }) => !video.includeAudio)).toBe(true);
  expect(request.referencePaths).toEqual([]);
  expect(request).not.toHaveProperty("previousSceneId");
  expect(request.prompt).toContain("The dancer turns toward the door.");
  expect(parseProjectConfig(JSON.parse(JSON.stringify(latest))).generationJobs[0].startVideoReferenceId).toBe("start");
  fireEvent.click(screen.getByRole("button", { name: "Shot 1 of Transition" }));
  typePrompt(screen.getByLabelText("Describe shot 1"), "");
  expect(generate).toBeDisabled();
});

const project = (): ProjectConfig => {
  const base = createProjectConfig({
    name: "Drag test",
    prompt: "Opening action",
    aspectRatio: "16:9",
    resolution: "1080p",
    targetDurationSeconds: 20,
  });
  const now = "2026-08-22T00:00:00.000Z";
  const first = createDraftGenerationJob("Opening action", {
    id: "scene-first",
    title: "First scene",
    now,
    durationSeconds: 6,
    shots: [
      { id: "shot-first-a", startSeconds: 0, action: "Opening action" },
      { id: "shot-first-b", startSeconds: 3, action: "Second action" },
    ],
  });
  const second = createDraftGenerationJob("Another scene", {
    id: "scene-second",
    title: "Second scene",
    now,
    durationSeconds: 8,
    shots: [{ id: "shot-second-a", startSeconds: 0, action: "Another scene" }],
  });
  return { ...base, generationJobs: [first, second] };
};

const transfer = (): DataTransfer => {
  const data = new Map<string, string>();
  const value = {
    effectAllowed: "none",
    get types() { return [...data.keys()]; },
    setData(type: string, content: string) { data.set(type, content); },
    getData(type: string) { return data.get(type) ?? ""; },
  };
  return value as unknown as DataTransfer;
};

function setup(initial = project()) {
  let latest = initial;
  let replace: (next: ProjectConfig) => void = () => undefined;
  function Harness() {
    const [config, setConfig] = useState(latest);
    const [cancellingIds, setCancellingIds] = useState<ReadonlySet<string>>(() => new Set());
    latest = config;
    replace = (next) => setConfig(next);
    return <GeneratorView
      config={config}
      folderPath="C:\\project"
      runtime={readyRuntime}
      onChange={setConfig}
      onGenerate={(submissions) => setConfig((current) => ({ ...current, generationJobs: current.generationJobs.map((job) => {
        const submission = submissions.find((item) => item.job.id === job.id);
        return submission ? { ...job, status: "queued", stage: "queued", progress: 0, error: null, generationSnapshot: submission.snapshot } : job;
      }) }))}
      cancellingJobIds={cancellingIds}
      onCancelGeneration={async (ids) => {
        const next = ids.filter((id) => !cancellingIds.has(id));
        setCancellingIds((current) => new Set([...current, ...next]));
        await Promise.all(next.map((id) => cancelSlopfabGeneration(id)));
      }}
      onOpenTimeline={() => undefined}
    />;
  }
  render(<Harness />);
  return {
    latest: () => latest,
    replace: (next: ProjectConfig) => act(() => replace(next)),
  };
}

async function finishFirst(state: ReturnType<typeof setup>) {
  fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
  await waitFor(() => expect(state.latest().generationJobs[0].generationSnapshot).toEqual(expect.any(String)));
  state.replace({
    ...state.latest(),
    generationJobs: state.latest().generationJobs.map((job, index) => index === 0 ? {
      ...job,
      status: "completed",
      stage: "completed",
      progress: 1,
    } : job),
  });
}

describe("Generator scene controls", () => {
  it("uses Video defaults in the inspector, debug prompt and generation request", async () => {
    const initial = project();
    initial.settings.defaultLook = "claymation";
    initial.videos = [{ id: "film", title: "Film", sceneIds: initial.generationJobs.map((job) => job.id), prompt: "", createdAt: initial.createdAt,
      defaultLook: "watercolor", aspectRatio: "9:16", resolution: "544p" }];
    const state = setup(initial);
    expect(screen.getByText("Using Video Look: Watercolor.")).toBeInTheDocument();
    expect(screen.getByRole("combobox", { name: "The look of this scene" })).toHaveTextContent("Watercolor");
    act(() => saveDebugOptionsEnabled(true));
    fireEvent.click(screen.getByRole("button", { name: "Debug prompt" }));
    expect(screen.getByRole("dialog", { name: "Debug prompt" })).toHaveTextContent("Watercolor");
    fireEvent.keyDown(screen.getByRole("dialog", { name: "Debug prompt" }), { key: "Escape" });
    fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
    await waitFor(() => expect(state.latest().generationJobs[0].generationSnapshot).toEqual(expect.any(String)));
    expect(JSON.parse(state.latest().generationJobs[0].generationSnapshot!)).toMatchObject({ canvasWidth: 544, canvasHeight: 960, prompt: expect.stringContaining("Watercolor") });
    expect(state.latest().settings).toEqual(initial.settings);
  });
  it.each([["", "Watercolor"], ["claymation", "Claymation"]])("compiles scene Look %s with the project's default", async (sceneLook, expected) => {
    const initial = project();
    initial.settings.defaultLook = "watercolor";
    const state = setup(initial);
    const selector = screen.getByRole("combobox", { name: "The look of this scene" });
    expect(comboValue(selector)).toBe("watercolor");
    expect(screen.getByText("Using project Look: Watercolor.")).toBeInTheDocument();
    if (sceneLook) chooseOption(selector, expected);
    fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
    await waitFor(() => expect(state.latest().generationJobs[0].generationSnapshot).toEqual(expect.any(String)));
    expect(JSON.parse(state.latest().generationJobs[0].generationSnapshot!).prompt).toContain(expected);
    if (!sceneLook) expect(sceneShots(state.latest().generationJobs[0])[0].settings?.visualStyle).toBeUndefined();
  });

  it("only shows debug prompts when enabled, in a dialog outside the inspector", () => {
    setup();
    expect(screen.queryByRole("button", { name: "Debug prompt" })).not.toBeInTheDocument();
    act(() => saveDebugOptionsEnabled(true));
    const button = screen.getByRole("button", { name: "Debug prompt" });
    button.focus();
    fireEvent.click(button);
    const dialog = screen.getByRole("dialog", { name: "Debug prompt" });
    expect(dialog.closest("aside")).toBeNull();
    const prompt = within(dialog).getByLabelText("The compiled MiniMax H3 prompt");
    expect(prompt).toHaveTextContent("Opening action");
    expect(prompt).toHaveFocus();
    fireEvent.keyDown(prompt, { key: "a", ctrlKey: true });
    expect(window.getSelection()?.toString()).toBe(prompt.textContent);
    fireEvent.keyDown(prompt, { key: "Escape" });
    expect(screen.queryByRole("dialog", { name: "Debug prompt" })).not.toBeInTheDocument();
    fireEvent.click(button);
    fireEvent.click(within(screen.getByRole("dialog", { name: "Debug prompt" })).getByRole("button", { name: "Close" }));
    expect(screen.queryByRole("dialog", { name: "Debug prompt" })).not.toBeInTheDocument();
    fireEvent.click(button);
    act(() => saveDebugOptionsEnabled(false));
    expect(screen.queryByRole("button", { name: "Debug prompt" })).not.toBeInTheDocument();
    expect(screen.queryByRole("dialog", { name: "Debug prompt" })).not.toBeInTheDocument();
  });

  it("sends a cited video with its trim and soundtrack choice in the generation snapshot", async () => {
    const initial = project();
    initial.references = [{ id: "motion", kind: "video", name: "Motion", description: "", sourcePath: "C:/motion.mp4", intendedUse: [], createdAt: initial.createdAt,
      video: { startSeconds: 2, durationSeconds: 3, includeAudio: false } }];
    initial.generationJobs[0].referenceIds = ["motion"];
    initial.generationJobs[0].shots![0].action = "Follow @[ref:motion].";
    const state = setup(parseProjectConfig(initial));
    fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
    await waitFor(() => expect(state.latest().generationJobs[0].generationSnapshot).toEqual(expect.any(String)));
    const snapshot = JSON.parse(state.latest().generationJobs[0].generationSnapshot!);
    expect(snapshot.referenceVideos).toEqual([{ name: "Motion", sourcePath: "C:/motion.mp4", startSeconds: 2, durationSeconds: 3, includeAudio: false }]);
    expect(snapshot.referencePaths).toEqual([]);
    expect(snapshot.prompt).toContain("<Video 1>");
  });

  it("offers no inferred Look and sends a selected image as the scene's first frame", async () => {
    const initial = project();
    initial.references = [{
      id: "ref-opening",
      kind: "image",
      name: "Opening still",
      description: "",
      relativePath: "references/opening.jpg",
      intendedUse: [],
      createdAt: initial.createdAt,
    }];
    const state = setup(parseProjectConfig(initial));

    expect(optionNames(screen.getByRole("combobox", { name: "The look of this scene" }))).toContain("None");
    choose("Scene type", "First & last frame");
    chooseReference("Start frame for this scene", "Opening still");
    expect(state.latest().generationJobs[0].startFrameReferenceId).toBe("ref-opening");

    fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
    await waitFor(() => expect(state.latest().generationJobs[0].generationSnapshot).toEqual(expect.any(String)));
    const snapshot = JSON.parse(state.latest().generationJobs[0].generationSnapshot!);
    expect(snapshot.referencePaths).toEqual(["C:\\\\project\\references\\opening.jpg"]);
    expect(snapshot.prompt).toContain("<Picture 1> (from [Shot 1]) is fully referenced.");
  });

  it("sends a last-frame image and clears it independently of the start frame", () => {
    const initial = project();
    initial.references = [{ id: "closing", kind: "image", name: "Closing still", description: "", relativePath: "references/closing.png", intendedUse: [], createdAt: initial.createdAt }];
    const state = setup(initial);
    choose("Scene type", "First & last frame");
    chooseReference("Last frame for this scene", "Closing still");
    expect(state.latest().generationJobs[0].endFrameReferenceId).toBe("closing");
    fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
    const snapshot = JSON.parse(state.latest().generationJobs[0].generationSnapshot!);
    expect(snapshot.prompt).toContain("<Picture 1> (from [Shot 2]) aligns with the 6.58-second mark of the target video.");
    expect(snapshot.referencePaths[0]).toContain("closing.png");
    chooseReference("Last frame for this scene", "None");
    expect(state.latest().generationJobs[0].endFrameReferenceId).toBeUndefined();
  });

  it.each([false, true])("continues a later scene with selectable overlap and edge, with Lock Overlap %s", (lockOverlap) => {
    const initial = project();
    initial.generationJobs[1] = { ...initial.generationJobs[1], status: "completed", latentRelativePath: "latents/later.safetensors" };
    initial.references = [{ id: "still", kind: "image", name: "Still", description: "", intendedUse: [], sourcePath: "C:/still.png", createdAt: initial.createdAt }];
    initial.generationJobs[0].startFrameReferenceId = "still";
    initial.generationJobs[0].endFrameReferenceId = "still";
    const state = setup(initial);
    choose("Scene type", "Continue");
    const generate = within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" });
    expect(generate).toBeDisabled();
    expect(screen.queryByLabelText("Start frame for this scene")).toBeNull();
    expect(screen.queryByLabelText("Last frame for this scene")).toBeNull();
    expect(comboValue(screen.getByLabelText("Take continuation latents from"))).toBe("end");
    expect(comboValue(screen.getByLabelText("Continuation overlap frames"))).toBe("22");
    expect(optionNames(screen.getByLabelText("Source scene for continuation"))).not.toContain("First scene");
    choose("Source scene for continuation", "Second scene");
    choose("Take continuation latents from", "Start");
    choose("Continuation overlap frames", "39");
    expect(screen.getByRole("switch", { name: "Lock Overlap" })).not.toBeChecked();
    fireEvent.click(screen.getByRole("switch", { name: "Lock Overlap" }));
    if (!lockOverlap) fireEvent.click(screen.getByRole("switch", { name: "Lock Overlap" }));
    expect(generate).toBeEnabled();
    fireEvent.click(generate);
    const saved = parseProjectConfig(JSON.parse(JSON.stringify(state.latest())));
    expect(saved.generationJobs[0]).toMatchObject({ sceneType: "continue", continuationSceneId: "scene-second", continuationFrom: "start", continuationOverlapFrames: 39, continuationLockOverlap: lockOverlap });
    expect(JSON.parse(saved.generationJobs[0].generationSnapshot!).continuationLockOverlap ?? false).toBe(lockOverlap);
    expect(JSON.parse(saved.generationJobs[0].generationSnapshot!)).toMatchObject({
      previousSceneId: "scene-second", continuationRelativePath: "latents/later.safetensors", continuationFrom: "start", continuationOverlapFrames: 39, referencePaths: [],
    });
  });

  it("queues continuation dependencies before their dependents, regardless of board order", () => {
    const initial = project();
    initial.generationJobs = [
      { ...initial.generationJobs[0], sceneType: "continue", continuationSceneId: "middle" },
      { ...initial.generationJobs[1], id: "middle", sceneType: "continue", continuationSceneId: "source" },
      createDraftGenerationJob("Original", { id: "source" }),
      createDraftGenerationJob("Cycle A", { id: "cycle-a", sceneType: "continue" }),
      createDraftGenerationJob("Cycle B", { id: "cycle-b", sceneType: "continue" }),
    ];
    initial.generationJobs[3].continuationSceneId = "cycle-b";
    initial.generationJobs[4].continuationSceneId = "cycle-a";
    const submitted = vi.fn();
    render(<GeneratorView config={initial} folderPath="C:/project" runtime={readyRuntime}
      onChange={vi.fn()} onGenerate={submitted} onOpenTimeline={() => undefined} />);
    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    expect(submitted).toHaveBeenCalledOnce();
    expect(submitted.mock.calls[0][0].map((item: { job: { id: string } }) => item.job.id)).toEqual(["source", "middle", "scene-first"]);
    expect(submitted.mock.calls[0][0][2].request).toMatchObject({ previousSceneId: "middle", continuationFrom: "end", continuationOverlapFrames: 22 });
  });

  it("marks a linked scene yellow when its source regenerates and clears it after regeneration", () => {
    const initial = project();
    initial.generationJobs[0] = { ...initial.generationJobs[0], status: "completed", outputRelativePath: "media/generated/work-original.mp4", latentRelativePath: "latents/work-original.safetensors" };
    const state = setup(initial);
    choose("Scene type", "First & last frame");
    fireEvent.click(screen.getByRole("button", { name: "Start frame for this scene" }));
    expect(screen.queryByRole("option", { name: "Previous scene" })).toBeNull();
    fireEvent.click(screen.getByRole("button", { name: "Start frame for this scene" }));
    fireEvent.click(screen.getByRole("button", { name: "Select scene Second scene" }));
    choose("Scene type", "Continue");
    choose("Source scene for continuation", "First scene");
    expect(state.latest().generationJobs[1].continuationSceneId).toBe("scene-first");
    const generateSecond = () => fireEvent.click(within(screen.getByRole("region", { name: "Second scene" })).getByRole("button", { name: "Generate" }));
    const finishSecond = () => state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job, index) => index === 1 ? { ...job, status: "completed", outputRelativePath: "media/generated/work-second.mp4" } : job) });
    generateSecond();
    const snapshot = JSON.parse(state.latest().generationJobs[1].generationSnapshot!);
    expect(snapshot.previousSceneId).toBe("scene-first");
    expect(snapshot.continuationRelativePath).toBe("latents/work-original.safetensors");
    finishSecond();
    const second = () => screen.getByRole("region", { name: "Second scene" });
    expect(within(second()).getByText("Finished")).toBeInTheDocument();
    state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job, index) => index === 0 ? { ...job, status: "generating" } : job) });
    expect(within(second()).getByText("Changed")).toBeInTheDocument();
    expect(second().querySelector(".scene-rule__status--changed")).not.toBeNull();
    state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job, index) => index === 0 ? { ...job, status: "completed", outputRelativePath: "media/generated/work-new.mp4", latentRelativePath: "latents/work-new.safetensors" } : job) });
    expect(within(second()).getByText("Changed")).toBeInTheDocument();
    // Opening a saved project retains the dependency's recorded renderer inputs.
    state.replace(parseProjectConfig(JSON.parse(JSON.stringify(state.latest()))));
    expect(within(second()).getByText("Changed")).toBeInTheDocument();
    generateSecond();
    expect(JSON.parse(state.latest().generationJobs[1].generationSnapshot!).continuationRelativePath).toBe("latents/work-new.safetensors");
    finishSecond();
    expect(within(second()).getByText("Finished")).toBeInTheDocument();
  });

  it("includes fixed-seed linked scenes when Generate All regenerates their source", () => {
    const initial = project();
    initial.generationJobs = initial.generationJobs.map((job, index) => ({ ...job, seed: 42, outputRelativePath: `media/generated/work-${index}.mp4`, latentRelativePath: `latents/work-${index}.safetensors`, usePreviousSceneLastFrame: index === 1 }));
    initial.assets.push({ id: "asset-scene-first", kind: "generated", mimeType: "video/mp4", name: "First scene", relativePath: "media/generated/work-0.mp4", durationMs: 6000, createdAt: initial.createdAt });
    const state = setup(initial);
    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job) => ({ ...job, status: "completed" })) });
    expect(screen.getByRole("button", { name: "Generate all" })).toHaveAttribute("data-tooltip", "Every fixed-seed scene is already up to date.");
    // Suffix-only renders from older versions must regenerate even with an
    // unchanged seed and prompt, so the source side of the join is recovered.
    const legacy = JSON.parse(state.latest().generationJobs[1].generationSnapshot!);
    delete legacy.continuationOutput;
    state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job, index) => index === 1 ? { ...job, generationSnapshot: JSON.stringify(legacy) } : job) });
    expect(within(screen.getByRole("region", { name: "Second scene" })).getByText("Changed")).toBeInTheDocument();
    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    expect(state.latest().generationJobs.map((job) => job.status)).toEqual(["completed", "queued"]);
    state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job) => ({ ...job, status: "completed" })) });
    fireEvent.change(screen.getByRole("textbox", { name: "The sound of this scene" }), { target: { value: "Rain" } });
    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    expect(state.latest().generationJobs.map((job) => job.status)).toEqual(["queued", "queued"]);
  });

  it("regenerates a completed source without latents when Generate All continues it", () => {
    const initial = project();
    initial.generationJobs = initial.generationJobs.map((job, index) => ({ ...job, seed: 42,
      outputRelativePath: `media/generated/work-${index}.mp4`, usePreviousSceneLastFrame: index === 1 }));
    const state = setup(initial);
    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    // Simulate an older completed project: matching snapshots, but no archives.
    state.replace({ ...state.latest(), generationJobs: state.latest().generationJobs.map((job) => ({ ...job, status: "completed" })) });
    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    expect(state.latest().generationJobs.map((job) => job.status)).toEqual(["queued", "queued"]);
  });

  it.each([[false, false], [false, true], [true, false], [true, true]])("saves audio controls %s without applying legacy latent upscale %s", async (separateAudio, latentUpscale) => {
    const initial = project();
    initial.settings = { ...initial.settings, resolution: "416p", frameRate: 60 };
    initial.generationJobs[0].latentUpscale = latentUpscale;
    const state = setup(initial);
    const generation = screen.getByRole("region", { name: "Generation" });
    expect(within(generation).queryByRole("switch", { name: "Latent upscale" })).toBeNull();
    expect(within(generation).getByRole("spinbutton", { name: "Generation step count" })).toHaveValue(20);
    expect(within(generation).getByRole("spinbutton", { name: "Generation seed" })).toHaveValue(-1);

    fireEvent.change(within(generation).getByRole("spinbutton", { name: "Generation step count" }), { target: { value: "28" } });
    fireEvent.change(within(generation).getByRole("spinbutton", { name: "Generation seed" }), { target: { value: "9173" } });
    expect(within(generation).getByRole("switch", { name: "Advanced options" })).not.toBeChecked();
    expect(within(generation).queryByRole("spinbutton", { name: "Audio step count" })).toBeNull();
    expect(within(generation).queryByRole("spinbutton", { name: "Video sigma shift" })).toBeNull();
    expect(within(generation).queryByRole("spinbutton", { name: "Audio sigma shift" })).toBeNull();
    expect(within(generation).getAllByRole("switch").at(-1)).toHaveAccessibleName("Advanced options");
    fireEvent.click(within(generation).getByRole("switch", { name: "Advanced options" }));
    expect(within(generation).getByRole("spinbutton", { name: "Video sigma shift" })).toHaveValue(12);
    expect(within(generation).getByRole("spinbutton", { name: "Audio sigma shift" })).toHaveValue(3);
    fireEvent.change(within(generation).getByRole("spinbutton", { name: "Video sigma shift" }), { target: { value: "8.5" } });
    fireEvent.change(within(generation).getByRole("spinbutton", { name: "Audio sigma shift" }), { target: { value: "2.5" } });
    expect(parseProjectConfig(JSON.parse(JSON.stringify(state.latest()))).generationJobs[0]).toMatchObject({ videoSigmaShift: 8.5, audioSigmaShift: 2.5 });
    expect(within(generation).getByRole("spinbutton", { name: "Audio step count" })).toHaveValue(28);
    fireEvent.change(within(generation).getByRole("spinbutton", { name: "Audio step count" }), { target: { value: "17" } });
    if (!separateAudio) fireEvent.click(within(generation).getByRole("switch", { name: "Advanced options" }));
    expect(parseProjectConfig(JSON.parse(JSON.stringify(state.latest()))).generationJobs[0].audioSteps).toBe(separateAudio ? 17 : undefined);
    expect(state.latest().generationJobs[0]).toEqual(expect.objectContaining({ steps: 28, seed: 9173 }));

    fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
    await waitFor(() => expect(state.latest().generationJobs[0].generationSnapshot).toEqual(expect.any(String)));
    expect(JSON.parse(state.latest().generationJobs[0].generationSnapshot!).audioSteps).toBe(separateAudio ? 17 : undefined);
    expect(JSON.parse(state.latest().generationJobs[0].generationSnapshot!).latentUpscale).toBeUndefined();
    expect(JSON.parse(state.latest().generationJobs[0].generationSnapshot!)).toEqual(expect.objectContaining({
      steps: 28,
      videoSigmaShift: 8.5,
      audioSigmaShift: 2.5,
      seed: 9173,
      frames: 144,
      canvasWidth: 736,
      canvasHeight: 416,
    }));
  });

  it("edits shot speech and sends it with the selected language in dialogue tags", async () => {
    const state = setup();
    fireEvent.click(screen.getByRole("button", { name: "Shot 1 of First scene" }));

    const speech = screen.getByRole("textbox", { name: "Speech for shot 1" });
    const language = screen.getByRole("combobox", { name: "Speech language for shot 1" });
    expect(speech).toHaveTextContent("");
    expect(comboValue(language)).toBe("English");

    fireEvent.keyDown(language, { key: "ArrowDown" });
    fireEvent.click(screen.getByRole("option", { name: "Korean" }));
    typePrompt(speech, "문을 열어 주세요.");
    expect(sceneShots(state.latest().generationJobs[0])[0]).toEqual(expect.objectContaining({
      speech: "문을 열어 주세요.",
      speechLanguage: "Korean",
    }));

    fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
    await waitFor(() => expect(state.latest().generationJobs[0].generationSnapshot).toEqual(expect.any(String)));
    const snapshot = JSON.parse(state.latest().generationJobs[0].generationSnapshot!);
    expect(snapshot.prompt).toContain("<d>[Korean] 문을 열어 주세요.</d>");
  });

  it("keeps language presets and allows typing a custom speech language", () => {
    const initial = project();
    initial.generationJobs[0].shots![0].speechLanguage = "Klingon";
    const state = setup(parseProjectConfig(initial));
    fireEvent.click(screen.getByRole("button", { name: "Shot 1 of First scene" }));

    const language = screen.getByRole("combobox", { name: "Speech language for shot 1" });
    expect(comboValue(language)).toBe("Klingon");
    fireEvent.keyDown(language, { key: "ArrowDown" });
    expect(screen.getAllByRole("option").map(option => option.textContent)).toEqual(expect.arrayContaining(["English", "Korean", "Klingon"]));
    fireEvent.keyDown(language, { key: "Escape" });
    fireEvent.change(language, { target: { value: "  Finnish  " } });
    fireEvent.keyDown(language, { key: "Enter" });
    expect(language).toHaveValue("Finnish");
    expect(parseProjectConfig(state.latest()).generationJobs[0].shots![0].speechLanguage).toBe("Finnish");
    fireEvent.change(language, { target: { value: "Swedish" } });
    fireEvent.blur(language);
    expect(state.latest().generationJobs[0].shots![0].speechLanguage).toBe("Swedish");
    fireEvent.change(language, { target: { value: "   " } });
    fireEvent.blur(language);
    expect(language).toHaveValue("Swedish");
  });

  it("inserts a voice chip into Speech and removes its generation input when the chip is removed", () => {
    const initial = project();
    initial.references = [
      { id: "voice", kind: "audio", name: "Narrator voice", description: "", intendedUse: [], sourcePath: "C:/voice.wav",
        audio: { startSeconds: 2, durationSeconds: 4 }, createdAt: initial.createdAt },
      { id: "hero", kind: "image", name: "Hero", description: "", intendedUse: [], sourcePath: "C:/hero.png", createdAt: initial.createdAt },
    ];
    initial.generationJobs[0].startFrameReferenceId = "hero";
    let latest = initial;
    const submitted = vi.fn();
    function Harness() {
      const [config, setConfig] = useState(initial);
      latest = config;
      return <GeneratorView config={config} folderPath="C:/project" runtime={readyRuntime}
        onChange={setConfig} onGenerate={submitted} onOpenTimeline={() => undefined} />;
    }
    render(<Harness />);
    fireEvent.click(screen.getByRole("button", { name: "Shot 1 of First scene" }));
    const speech = screen.getByRole("textbox", { name: "Speech for shot 1" });
    typePrompt(speech, "Welcome home.");
    placePromptCaret(speech);
    fireEvent.click(screen.getByRole("button", { name: "Voice reference" }));
    expect(within(screen.getByRole("dialog")).queryByRole("button", { name: /^Hero/ })).toBeNull();
    fireEvent.click(screen.getByRole("button", { name: /^Narrator voice/ }));
    expect(speech.querySelector(".prompt-chip")).toHaveTextContent("Narrator voice");
    expect(latest.generationJobs[0].referenceIds).toEqual(["voice"]);
    expect(parseProjectConfig(JSON.parse(JSON.stringify(latest))).generationJobs[0].shots![0].speech).toContain("@[ref:voice]");
    const generate = within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" });
    fireEvent.click(generate);
    expect(submitted.mock.calls.at(-1)![0][0].request).toMatchObject({
      prompt: expect.stringContaining("<d>[English] Welcome home.</d>"),
      referenceAudios: [{ name: "Narrator voice", sourcePath: "C:/voice.wav", startSeconds: 2, durationSeconds: 4 }],
    });
    changePromptChip(speech, 0, null);
    expect(latest.generationJobs[0].referenceIds).toEqual([]);
    fireEvent.click(generate);
    expect(submitted.mock.calls.at(-1)![0][0].request.referenceAudios).toEqual([]);
    expect(submitted.mock.calls.at(-1)![0][0].request.prompt).not.toContain("<Audio");
  });

  it("reorders shots inside a scene while keeping its cut slots", () => {
    const state = setup();
    const shotTransfer = transfer();
    fireEvent.dragStart(screen.getByRole("button", { name: "Shot 1 of First scene" }), { dataTransfer: shotTransfer });
    fireEvent.drop(screen.getByRole("list", { name: "Shots in First scene" }), { dataTransfer: shotTransfer });

    const first = state.latest().generationJobs.find((job) => job.id === "scene-first")!;
    expect(sceneShots(first).map((shot) => shot.id)).toEqual(["shot-first-b", "shot-first-a"]);
    expect(sceneShots(first).map((shot) => shot.startSeconds)).toEqual([0, 3]);
  });

  it("reorders scenes and moves shots between scenes by drag and drop", () => {
    const state = setup();

    const sceneTransfer = transfer();
    fireEvent.dragStart(screen.getByRole("button", { name: "Drag Second scene to reorder scenes" }), { dataTransfer: sceneTransfer });
    fireEvent.drop(screen.getByRole("region", { name: "First scene" }), { dataTransfer: sceneTransfer });
    expect(state.latest().generationJobs.map((job) => job.id)).toEqual(["scene-second", "scene-first"]);

    const shotTransfer = transfer();
    fireEvent.dragStart(screen.getByRole("button", { name: "Shot 2 of First scene" }), { dataTransfer: shotTransfer });
    fireEvent.drop(screen.getByRole("button", { name: "Shot 1 of Second scene" }).parentElement!, { dataTransfer: shotTransfer });

    const source = state.latest().generationJobs.find((job) => job.id === "scene-first")!;
    const target = state.latest().generationJobs.find((job) => job.id === "scene-second")!;
    expect(sceneShots(source).map((shot) => shot.id)).toEqual(["shot-first-a"]);
    expect(sceneShots(target).map((shot) => shot.id)).toEqual(["shot-first-b", "shot-second-a"]);
    expect(sceneShots(target).map((shot) => shot.startSeconds)).toEqual([0, 4]);
  });

  it("selects a scene from its header and keeps confirmed removal in the inspector", async () => {
    const state = setup();
    expect(screen.getByRole("button", { name: "Draft of First scene" })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Draft of Second scene" })).toBeInTheDocument();

    fireEvent.change(screen.getByRole("slider", { name: "First scene length in seconds" }), { target: { value: "10" } });
    expect(state.latest().generationJobs[0].durationSeconds).toBe(10);
    expect(screen.queryByText("Changed")).not.toBeInTheDocument();

    const scene = screen.getByRole("region", { name: "First scene" });
    expect(within(scene).queryByRole("button", { name: "Delete scene First scene" })).not.toBeInTheDocument();
    expect(within(scene).queryByRole("button", { name: "Settings for First scene" })).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole("button", { name: "Shot 1 of First scene" }));
    fireEvent.click(scene.querySelector(".scene-rule")!);
    expect(screen.getByRole("textbox", { name: "Rename First scene" })).toBeInTheDocument();

    vi.mocked(askNative).mockResolvedValueOnce(false);
    fireEvent.click(within(screen.getByRole("complementary", { name: "Scene: First scene" })).getByRole("button", { name: "Delete scene First scene" }));
    await waitFor(() => expect(askNative).toHaveBeenCalledWith(expect.objectContaining({ title: "Delete scene", okLabel: "Delete" })));
    expect(state.latest().generationJobs.map((job) => job.id)).toEqual(["scene-first", "scene-second"]);
    vi.mocked(askNative).mockResolvedValueOnce(true);
    fireEvent.click(screen.getByRole("button", { name: "Delete scene First scene" }));
    await waitFor(() => expect(state.latest().generationJobs.map((job) => job.id)).toEqual(["scene-second"]));
  });

  it("adds new scenes at the bottom", () => {
    const state = setup();
    fireEvent.click(screen.getByRole("button", { name: "Add scene" }));
    expect(state.latest().generationJobs.slice(0, 2).map((job) => job.id)).toEqual(["scene-first", "scene-second"]);
    expect(state.latest().generationJobs.at(-1)?.title).toBe("Untitled scene");
  });

  it("uses the default generator template's steps for a new scene", () => {
    localStorage.setItem("slopus.generator-templates.v1", JSON.stringify({
      defaultTemplateId: "draft",
      templates: [{
        id: "draft",
        name: "Draft",
        defaultSteps: 9,
        paths: { transformer: "", textEncoder: "text", tokenizer: "", videoVae: "video", audioVae: "audio" },
      }],
    }));
    const state = setup();
    fireEvent.click(screen.getByRole("button", { name: "Add scene" }));
    expect(state.latest().generationJobs.at(-1)?.steps).toBe(9);
  });

  it("switches the active generator from the ready status control and probes its model paths", async () => {
    localStorage.setItem("slopus.generator-templates.v1", JSON.stringify({
      defaultTemplateId: "quality",
      templates: [
        {
          id: "quality",
          name: "Quality",
          defaultSteps: 20,
          paths: { transformer: "quality.safetensors", textEncoder: "text", tokenizer: "", videoVae: "video", audioVae: "audio" },
        },
        {
          id: "draft",
          name: "Fast draft",
          defaultSteps: 8,
          motionCache: true,
          paths: { transformer: "draft.safetensors", textEncoder: "text", tokenizer: "", videoVae: "video", audioVae: "audio" },
        },
      ],
    }));
    vi.mocked(getEngineStatus).mockResolvedValueOnce({
      state: "modelsMissing",
      dllPath: "test",
      version: "test",
      platform: "CUDA 13",
      detail: "Draft weights are missing.",
      models: [],
    });
    setup();

    expect(document.querySelector(".generator-runtime--ready > i")).not.toBeNull();
    expect(screen.getByRole("img", { name: "Video generator ready" })).toBeInTheDocument();
    const template = screen.getByRole("combobox", { name: /Video generator template/ });
    expect(template).toHaveTextContent("Quality");
    fireEvent.click(template);
    fireEvent.click(screen.getByRole("option", { name: "Fast draft" }));

    await waitFor(() => expect(document.querySelector(".generator-runtime--modelsMissing > i")).not.toBeNull());
    expect(getEngineStatus).toHaveBeenCalledWith(expect.objectContaining({ transformer: "draft.safetensors" }), "sage2", [], "prompt", [], true, expect.anything(), "minimax-h3");
    expect(loadActiveGeneratorId("video")).toBe("draft");
    expect(JSON.parse(localStorage.getItem("slopus.generator-templates.v1")!).defaultTemplateId).toBe("quality");
    expect(within(screen.getByRole("region", { name: "Generator status" })).getByText("Video model files missing")).toBeInTheDocument();
    expect(template.closest(".generator-runtime--modelsMissing")).not.toBeNull();
  });

  it("marks a finished scene yellow when scene settings change", async () => {
    const state = setup();
    await finishFirst(state);
    const scene = screen.getByRole("region", { name: "First scene" });
    expect(within(scene).getByText("Finished")).toBeInTheDocument();
    expect(scene.querySelector(".scene-rule__status--completed")).not.toBeNull();

    fireEvent.change(screen.getByRole("textbox", { name: "The sound of this scene" }), { target: { value: "Soft rain." } });
    expect(within(scene).getByText("Changed")).toBeInTheDocument();
    expect(scene.querySelector(".scene-rule__status--changed")).not.toBeNull();
  });

  it("keeps scene and shot settings editable during generation and marks the finished scene changed", async () => {
    const state = setup();
    const scene = screen.getByRole("region", { name: "First scene" });
    fireEvent.click(within(scene).getByRole("button", { name: "Generate" }));
    await waitFor(() => expect(state.latest().generationJobs[0].status).toBe("queued"));
    const sent = state.latest().generationJobs[0].generationSnapshot;
    state.replace({
      ...state.latest(),
      generationJobs: state.latest().generationJobs.map((job, index) => index === 0
        ? { ...job, status: "generating", stage: "generating", progress: 0.4 }
        : job),
    });

    const sound = screen.getByRole("textbox", { name: "The sound of this scene" });
    expect(sound).toBeEnabled();
    fireEvent.change(sound, { target: { value: "Rain added while this run is rendering." } });

    fireEvent.click(screen.getByRole("button", { name: "Shot 1 of First scene" }));
    const addSetting = screen.getByRole("combobox", { name: "Add a setting to shot 1" });
    expect(addSetting).toBeEnabled();
    chooseOption(addSetting, "Shot size");
    choose("Value for Shot size on shot 1", "Close-up");
    expect(state.latest().generationJobs[0].generationSnapshot).toBe(sent);

    state.replace({
      ...state.latest(),
      generationJobs: state.latest().generationJobs.map((job, index) => index === 0
        ? { ...job, status: "completed", stage: "completed", progress: 1 }
        : job),
    });
    const finished = screen.getByRole("region", { name: "First scene" });
    expect(within(finished).getByText("Changed")).toBeInTheDocument();
    expect(finished.querySelector(".scene-rule__status--changed")).not.toBeNull();
  });

  it("marks a finished scene changed when shot settings or order change", async () => {
    const state = setup();
    await finishFirst(state);
    const scene = screen.getByRole("region", { name: "First scene" });

    fireEvent.click(screen.getByRole("button", { name: "Shot 1 of First scene" }));
    expect(screen.getByText("Description")).toBeInTheDocument();
    choose("Add a setting to shot 1", "Shot size");
    choose("Value for Shot size on shot 1", "Close-up");
    expect(within(scene).getByText("Changed")).toBeInTheDocument();

    fireEvent.click(within(scene).getByRole("button", { name: "Generate" }));
    await waitFor(() => expect(state.latest().generationJobs[0].status).toBe("queued"));
    const regenerated = state.latest().generationJobs[0];
    state.replace({
      ...state.latest(),
      generationJobs: state.latest().generationJobs.map((job, index) => index === 0
        ? { ...regenerated, status: "completed", stage: "completed", progress: 1 }
        : job),
    });
    expect(within(scene).getByText("Finished")).toBeInTheDocument();

    const shotTransfer = transfer();
    fireEvent.dragStart(screen.getByRole("button", { name: "Shot 1 of First scene" }), { dataTransfer: shotTransfer });
    fireEvent.drop(screen.getByRole("list", { name: "Shots in First scene" }), { dataTransfer: shotTransfer });
    expect(within(scene).getByText("Changed")).toBeInTheDocument();
  });

  it("renames a shot and confirms before removing it", async () => {
    const state = setup();
    fireEvent.click(screen.getByRole("button", { name: "Shot 2 of First scene" }));
    fireEvent.change(screen.getByRole("textbox", { name: "Rename shot 2" }), { target: { value: "Doorway reveal" } });
    const shotHeader = screen.getByRole("textbox", { name: "Rename shot 2" }).closest<HTMLElement>(".ui-item-header");
    const removeButton = within(shotHeader!).getByRole("button", { name: "Delete shot 2" });
    expect(removeButton).toHaveClass("icon-button");
    expect(removeButton).toHaveTextContent("");
    expect(sceneShots(state.latest().generationJobs[0])[1].name).toBe("Doorway reveal");
    expect(parseProjectConfig(state.latest()).generationJobs[0].shots?.[1].name).toBe("Doorway reveal");
    expect(screen.getByRole("button", { name: "Doorway reveal of First scene" })).toBeInTheDocument();

    vi.mocked(askNative).mockResolvedValueOnce(false);
    fireEvent.click(removeButton);
    await waitFor(() => expect(askNative).toHaveBeenCalledWith(expect.objectContaining({ title: "Delete shot", message: "Delete “Doorway reveal” from “First scene”?" })));
    expect(sceneShots(state.latest().generationJobs[0])).toHaveLength(2);

    vi.mocked(askNative).mockResolvedValueOnce(true);
    fireEvent.click(screen.getByRole("button", { name: "Delete shot 2" }));
    await waitFor(() => expect(sceneShots(state.latest().generationJobs[0])).toHaveLength(1));
  });

  it("queues every valid draft from Generate All", async () => {
    const state = setup();
    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    await waitFor(() => expect(state.latest().generationJobs.map((job) => job.status)).toEqual(["queued", "queued"]));
    expect(state.latest().generationJobs.every((job) => Boolean(job.generationSnapshot))).toBe(true);
    expect(parseProjectConfig(state.latest())).toBeTruthy();
  });

  it("keeps Generate All available and skips unchanged completed fixed-seed scenes", async () => {
    const initial = project();
    initial.generationJobs = initial.generationJobs.map((job, index) => ({ ...job, seed: 100 + index }));
    const state = setup(initial);

    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    await waitFor(() => expect(state.latest().generationJobs.every((job) => job.status === "queued")).toBe(true));
    state.replace({
      ...state.latest(),
      generationJobs: state.latest().generationJobs.map((job) => ({
        ...job,
        status: "completed",
        stage: "completed",
        progress: 1,
        outputRelativePath: `media/generated/${job.id}.mp4`,
      })),
    });

    const generateAll = screen.getByRole("button", { name: "Generate all" });
    expect(generateAll).toBeEnabled();
    fireEvent.click(generateAll);
    expect(state.latest().generationJobs.map((job) => job.status)).toEqual(["completed", "completed"]);

    const changed = state.latest().generationJobs[0];
    state.replace({
      ...state.latest(),
      generationJobs: state.latest().generationJobs.map((job) => job.id === changed.id
        ? { ...job, shots: sceneShots(job).map((shot, index) => index === 0 ? { ...shot, action: `${shot.action} Changed.` } : shot) }
        : job),
    });
    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    await waitFor(() => expect(state.latest().generationJobs.map((job) => job.status)).toEqual(["queued", "completed"]));
  });

  it("regenerates an unchanged completed random-seed scene while skipping fixed-seed output", async () => {
    const initial = project();
    initial.generationJobs[1].seed = 417;
    const state = setup(initial);

    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    await waitFor(() => expect(state.latest().generationJobs.every((job) => job.status === "queued")).toBe(true));
    state.replace({
      ...state.latest(),
      generationJobs: state.latest().generationJobs.map((job) => ({
        ...job,
        status: "completed",
        stage: "completed",
        progress: 1,
        outputRelativePath: `media/generated/${job.id}.mp4`,
      })),
    });

    fireEvent.click(screen.getByRole("button", { name: "Generate all" }));
    await waitFor(() => expect(state.latest().generationJobs.map((job) => job.status)).toEqual(["queued", "completed"]));
  });

  it("turns active generation actions into per-scene and batch cancellation", async () => {
    const initial = project();
    initial.generationJobs = initial.generationJobs.map((job, index) => ({
      ...job,
      status: index === 0 ? "generating" : "queued",
      stage: index === 0 ? "generating" : "queued",
      progress: index === 0 ? 0.42 : 0,
    }));
    setup(initial);

    const first = screen.getByRole("region", { name: "First scene" });
    const second = screen.getByRole("region", { name: "Second scene" });
    expect(within(first).getByRole("button", { name: "Cancel" })).toBeEnabled();
    expect(within(second).getByRole("button", { name: "Cancel" })).toBeEnabled();
    expect(screen.getByRole("button", { name: "Cancel all" })).toBeEnabled();
    expect(within(first).getAllByText("Rendering 42%")).toHaveLength(2);
    const progressBars = first.querySelectorAll(".shot-thumb__progress > i");
    expect(progressBars).toHaveLength(2);
    expect([...progressBars].every((bar) => (bar as HTMLElement).style.width === "42%")).toBe(true);
    expect(within(first).queryByText("Rendering")).not.toBeInTheDocument();

    fireEvent.click(within(first).getByRole("button", { name: "Cancel" }));
    await waitFor(() => expect(cancelSlopfabGeneration).toHaveBeenCalledWith("scene-first"));
    const cancelling = within(first).getAllByRole("img", { name: /Cancelling\.\.\.$/ });
    expect(cancelling).toHaveLength(2);
    expect(cancelling.every((thumbnail) => thumbnail.querySelector(".spin"))).toBe(true);
    expect(within(first).queryByText("Rendering 42%")).not.toBeInTheDocument();

    vi.mocked(cancelSlopfabGeneration).mockClear();
    fireEvent.click(screen.getByRole("button", { name: "Cancel all" }));
    await waitFor(() => {
      expect(cancelSlopfabGeneration).toHaveBeenCalledTimes(1);
      expect(cancelSlopfabGeneration).toHaveBeenCalledWith("scene-second");
    });
  });
});

describe("Generator board as a native list", () => {
  it("summarises the board in the status bar and switches density", () => {
    const initial = project();
    initial.generationJobs[0] = { ...initial.generationJobs[0], status: "generating", stage: "generating", progress: 0.1 };
    setup(initial);
    expect(within(screen.getByRole("region", { name: "Generator status" })).getByRole("status")).toHaveTextContent("2 scenes · 1 rendering");
    expect(screen.queryByRole("heading", { name: "Generator" })).toBeNull();
    fireEvent.click(screen.getByRole("button", { name: "List" }));
    expect(screen.getByRole("button", { name: "List" })).toHaveAttribute("aria-pressed", "true");
    expect(document.querySelector(".scene-board--list")).not.toBeNull();
    expect(localStorage.getItem("slopus.generator.density")).toBe("list");
  });

  it("collapses a scene group from its header", () => {
    setup();
    fireEvent.click(screen.getByRole("button", { name: "Collapse First scene" }));
    expect(screen.queryByRole("list", { name: "Shots in First scene" })).toBeNull();
    fireEvent.click(screen.getByRole("button", { name: "Expand First scene" }));
    expect(screen.getByRole("list", { name: "Shots in First scene" })).toBeInTheDocument();
  });

  it("opens scene and shot menus by right-click and Shift+F10 and runs their commands", () => {
    const state = setup();
    fireEvent.contextMenu(screen.getByRole("region", { name: "Second scene" }).querySelector(".scene-rule")!);
    const menu = screen.getByRole("menu", { name: "Second scene actions" });
    expect(within(menu).getByRole("menuitem", { name: /Move down/ })).toBeDisabled();
    fireEvent.click(within(menu).getByRole("menuitem", { name: /Move up/ }));
    expect(state.latest().generationJobs.map((job) => job.id)).toEqual(["scene-second", "scene-first"]);

    const shot = screen.getByRole("button", { name: "Shot 2 of First scene" });
    fireEvent.keyDown(shot, { key: "F10", shiftKey: true });
    const shotMenu = screen.getByRole("menu", { name: "Shot 2 actions" });
    // One "Move to" item with the other scenes in a cascade, not one item per scene.
    const moveTo = within(shotMenu).getByRole("menuitem", { name: "Move to" });
    expect(moveTo).toHaveAttribute("aria-haspopup", "menu");
    moveTo.focus();
    fireEvent.keyDown(moveTo, { key: "ArrowRight" });
    fireEvent.click(within(screen.getByRole("menu", { name: "Move to" })).getByRole("menuitem", { name: "Second scene" }));
    expect(screen.queryByRole("menu")).toBeNull();
    expect(sceneShots(state.latest().generationJobs.find((job) => job.id === "scene-second")!).map((item) => item.id)).toEqual(["shot-second-a", "shot-first-b"]);
  });

  it("renames with F2 and deletes with Delete after a native confirmation", async () => {
    const state = setup();
    const shot = screen.getByRole("button", { name: "Shot 2 of First scene" });
    fireEvent.click(shot);
    shot.focus();
    fireEvent.keyDown(shot, { key: "F2" });
    await waitFor(() => expect(screen.getByRole("textbox", { name: "Rename shot 2" })).toHaveFocus());

    shot.focus();
    fireEvent.keyDown(shot, { key: "Delete" });
    await waitFor(() => expect(sceneShots(state.latest().generationJobs[0])).toHaveLength(1));
    expect(askNative).toHaveBeenCalledWith(expect.objectContaining({ title: "Delete shot" }));
  });

  it("moves between shot tiles with the arrow keys as one tab stop", () => {
    setup();
    const first = screen.getByRole("button", { name: "Shot 1 of First scene" });
    const second = screen.getByRole("button", { name: "Shot 2 of First scene" });
    expect(first).toHaveAttribute("tabindex", "0");
    expect(second).toHaveAttribute("tabindex", "-1");
    first.focus();
    fireEvent.keyDown(first, { key: "ArrowRight" });
    expect(second).toHaveFocus();
    expect(second).toHaveAttribute("aria-pressed", "true");
  });

  it("shows where a dragged shot will land and dims the source", () => {
    setup();
    const shotTransfer = transfer();
    const source = screen.getByRole("button", { name: "Shot 1 of First scene" });
    fireEvent.dragStart(source, { dataTransfer: shotTransfer });
    expect(source.closest(".shot-card")).toHaveClass("shot-card--dragging");
    const target = screen.getByRole("button", { name: "Shot 1 of Second scene" }).closest("li")!;
    fireEvent.dragOver(target, { dataTransfer: shotTransfer });
    expect(target).toHaveClass("shot-slot--drop-before");
    fireEvent.dragEnd(source, { dataTransfer: shotTransfer });
    expect(target).not.toHaveClass("shot-slot--drop-before");
    expect(source.closest(".shot-card")).not.toHaveClass("shot-card--dragging");
  });
});

it("shows template shifts and locks active LoRA overrides without replacing saved scene values", () => {
  const template = { ...createGeneratorTemplate("Shifts"), videoSigmaShift: 9, audioSigmaShift: 3.5,
    loras: [{ loraId: "shift", enabled: true, strength: 1 }],
    paths: { transformer: "model", textEncoder: "text", tokenizer: "", videoVae: "video", audioVae: "audio" } };
  saveLoras([{ id: "shift", name: "Shift", path: "D:/shift.safetensors", videoSigmaShiftOverride: 6 }]);
  saveGeneratorTemplateSettings({ templates: [template], defaultTemplateId: template.id, catalogVersion: 17 });
  const initial = project();
  initial.generationJobs[0].videoSigmaShift = 8;
  const state = setup(initial);
  expect(screen.getByRole("switch", { name: "Advanced options" })).not.toBeChecked();
  fireEvent.click(screen.getByRole("switch", { name: "Advanced options" }));
  expect(screen.getByLabelText("Video sigma shift")).toHaveValue(6);
  expect(screen.getByLabelText("Video sigma shift")).toBeDisabled();
  expect(screen.getByLabelText("Audio sigma shift")).toHaveValue(3.5);
  expect(screen.getByLabelText("Audio sigma shift")).toBeEnabled();
  expect(screen.getByRole("switch", { name: "Advanced options" })).toBeChecked();
  fireEvent.click(within(screen.getByRole("region", { name: "First scene" })).getByRole("button", { name: "Generate" }));
  expect(JSON.parse(state.latest().generationJobs[0].generationSnapshot!)).toMatchObject({ videoSigmaShift: 6, audioSigmaShift: 3.5 });
  expect(state.latest().generationJobs[0].videoSigmaShift).toBe(8);
});


it.each(["mouse", "keyboard"])("removes the Draft through its %s context menu and restores the primary Take", method => {
  let config = project();
  const job = config.generationJobs[0];
  config = completeSceneGeneration(config, job.id, { id: "one", scene: sceneInput(job), createdAt: config.createdAt,
    media: { id: `asset-${job.id}`, kind: "generated", name: job.title, mimeType: "video/mp4", relativePath: "media/one.mp4", createdAt: config.createdAt } });
  const state = setup(config);
  fireEvent.change(screen.getByLabelText("The music of this scene"), { target: { value: "Discard this draft" } });
  const draft = screen.getByRole("button", { name: "Draft of First scene" });
  if (method === "mouse") fireEvent.contextMenu(draft);
  else fireEvent.keyDown(draft, { key: "F10", shiftKey: true });
  fireEvent.click(screen.getByRole("menuitem", { name: "Remove draft" }));
  expect(screen.queryByRole("button", { name: "Draft of First scene" })).toBeNull();
  expect(state.latest().generationJobs[0].music).toBe(job.music);
  expect(state.latest().generationJobs[0]).toMatchObject({ outputRelativePath: "media/one.mp4",
    generationHistory: { selectedId: "one", primaryId: "one", draft: null } });
  expect(screen.getByRole("button", { name: "Generation 1 (Primary) of First scene" })).toHaveAttribute("aria-pressed", "true");
});

it("disables removing the scene's only Draft", () => {
  setup();
  fireEvent.contextMenu(screen.getByRole("button", { name: "Draft of First scene" }));
  expect(screen.getByRole("menuitem", { name: "Remove draft" })).toBeDisabled();
});

it.each(["mouse", "keyboard"])("removes a non-primary take during generation through its %s menu", method => {
  let config = project();
  const id = config.generationJobs[0].id;
  for (const takeId of ["one", "two"]) {
    const job = config.generationJobs[0];
    config = completeSceneGeneration(config, id, { id: takeId, scene: sceneInput(job), createdAt: config.createdAt,
      media: { id: `asset-${id}`, kind: "generated", name: job.title, mimeType: "video/mp4", relativePath: `media/${takeId}.mp4`, createdAt: config.createdAt } });
  }
  config.generationJobs[0].status = "generating";
  config.generationJobs[0].progress = 0.4;
  const state = setup(config);
  const older = screen.getByRole("button", { name: "Generation 1 of First scene" });
  expect(older).toBeEnabled();
  expect(screen.getByRole("button", { name: "Generation 2 (Primary) of First scene" })).toBeDisabled();
  fireEvent.click(older);
  expect(state.latest().generationJobs[0].generationHistory!.selectedId).toBe("two");
  if (method === "mouse") fireEvent.contextMenu(older);
  else fireEvent.keyDown(older, { key: "F10", shiftKey: true });
  expect(screen.getByRole("menuitem", { name: "Make Primary" })).toBeDisabled();
  expect(screen.getByRole("menuitem", { name: "Remove generation" })).toBeEnabled();
  fireEvent.click(screen.getByRole("menuitem", { name: "Remove generation" }));
  expect(state.latest().generationJobs[0]).toMatchObject({ status: "generating", progress: 0.4, outputRelativePath: "media/two.mp4",
    generationHistory: { primaryId: "two", selectedId: "two" } });
  expect(state.latest().generationJobs[0].generationHistory!.versions.map(version => version.id)).toEqual(["two"]);
  expect(screen.getByRole("button", { name: "Generation 1 (Primary) of First scene" })).toBeDisabled();
});

it("restores older takes and drafts, makes a take primary, removes history and duplicates a scene", () => {
  let config = project();
  const id = config.generationJobs[0].id;
  const saveTake = (takeId: string) => {
    const job = config.generationJobs[0];
    config = completeSceneGeneration(config, id, { id: takeId, scene: sceneInput(job), createdAt: config.createdAt,
      media: { id: `asset-${id}`, kind: "generated", name: job.title, mimeType: "video/mp4", relativePath: `media/${takeId}.mp4`, createdAt: config.createdAt } });
  };
  config.generationJobs[0].music = "First music"; config.generationJobs[0].steps = 4; saveTake("one");
  config = { ...config, generationJobs: config.generationJobs.map((job, i) => i ? job : { ...job, music: "Second music", steps: 8 }) }; saveTake("two");
  const state = setup(config);
  fireEvent.change(screen.getByLabelText("The music of this scene"), { target: { value: "Draft music" } });
  fireEvent.click(screen.getByRole("button", { name: "Generation 1 of First scene" }));
  expect(state.latest().generationJobs[0]).toMatchObject({ music: "First music", steps: 4, outputRelativePath: "media/two.mp4" });
  fireEvent.click(screen.getByRole("button", { name: "Draft of First scene" }));
  expect(state.latest().generationJobs[0].music).toBe("Draft music");
  fireEvent.contextMenu(screen.getByRole("button", { name: "Generation 1 of First scene" }));
  fireEvent.click(screen.getByRole("menuitem", { name: "Make Primary" }));
  expect(state.latest().generationJobs[0].outputRelativePath).toBe("media/one.mp4");
  expect(state.latest().generationJobs[0].music).toBe("Draft music");
  fireEvent.contextMenu(screen.getByRole("region", { name: "First scene" }).querySelector(".scene-rule")!);
  fireEvent.click(screen.getByRole("menuitem", { name: "Duplicate" }));
  const copy = state.latest().generationJobs[1];
  expect(copy).toMatchObject({ title: "First scene copy", music: "Draft music", steps: 8, status: "draft" });
  expect(copy.generationHistory).toBeUndefined(); expect(copy.outputRelativePath).toBeFalsy();
  fireEvent.contextMenu(screen.getByRole("button", { name: "Generation 1 (Primary) of First scene" }));
  fireEvent.click(screen.getByRole("menuitem", { name: "Remove generation" }));
  expect(state.latest().generationJobs[0]).toMatchObject({ music: "Draft music", outputRelativePath: "media/two.mp4", generationHistory: { primaryId: "two", selectedId: null } });
  expect(state.latest().generationJobs[0].generationHistory!.versions.map(version => version.id)).toEqual(["two"]);
  fireEvent.keyDown(screen.getByRole("button", { name: "Generation 1 (Primary) of First scene" }), { key: "ContextMenu" });
  fireEvent.click(screen.getByRole("menuitem", { name: "Remove generation" }));
  expect(state.latest().generationJobs[0]).toMatchObject({ status: "draft", outputRelativePath: null, music: "Draft music" });
  expect(screen.queryByRole("button", { name: /Generation .* of First scene/ })).not.toBeInTheDocument();
  expect(screen.getByRole("button", { name: "Draft of First scene" })).toBeInTheDocument();
});


it("keeps the active generator when changing to an incompatible scene type", async () => {
  vi.mocked(getEngineStatus).mockResolvedValue(readyRuntime);
  const prompt = { ...minimaxOriginalTemplate(), id: "prompt-template", name: "Prompt template", sources: {}, paths: { transformer: "prompt-transformer", textEncoder: "text", videoVae: "vae", audioVae: "audio", tokenizer: "tokens" } };
  const animate = { ...viggleAnimateTemplate(), id: "animate-template", name: "Animate template", sources: {}, loras: [], additionalSafetensors: [], paths: { transformer: "animate-transformer", textEncoder: "text", videoVae: "vae", audioVae: "audio", tokenizer: "tokens" } };
  saveGeneratorTemplateSettings({ templates: [prompt, animate], defaultTemplateId: prompt.id, catalogVersion: 17 });
  const state = setup();
  choose("Scene type", "Animate");
  await waitFor(() => expect(state.latest().generationJobs[0]).toMatchObject({ sceneType: "animate" }));
  expect(screen.getByRole("combobox", { name: /Video generator template/ })).toHaveTextContent("Prompt template");
});

it("keeps the active video generator while browsing takes and restores the used generator explicitly", async () => {
  vi.mocked(getEngineStatus).mockResolvedValue(readyRuntime);
  const first = createGeneratorTemplate("Original generator"), second = createGeneratorTemplate("Current generator");
  saveGeneratorTemplateSettings({ templates: [first, second], defaultTemplateId: first.id });
  saveActiveGeneratorId("video", second.id);
  let config = project();
  const id = config.generationJobs[0].id;
  for (const [index, template] of [first, second].entries()) {
    const job = { ...config.generationJobs[0], seed: -1, generatorTemplateId: template.id };
    config = { ...config, generationJobs: config.generationJobs.map((current) => current.id === id ? job : current) };
    config = completeSceneGeneration(config, id, { id: `take-${index}`, scene: sceneInput(job),
      generatorName: template.name, generationSnapshot: JSON.stringify({ seed: 100 + index }), createdAt: config.createdAt,
      media: { id: `asset-${id}`, kind: "generated", name: job.title, mimeType: "video/mp4", relativePath: `media/take-${index}.mp4`, createdAt: config.createdAt } });
  }
  const state = setup(config);
  expect(screen.getByLabelText("Used seed")).toHaveValue("101");
  expect(screen.getByLabelText("Used generator")).toHaveValue(second.name);
  fireEvent.click(screen.getByRole("button", { name: "Generation 1 of First scene" }));
  expect(screen.getByLabelText("Used seed")).toHaveValue("100");
  expect(screen.getByLabelText("Used generator")).toHaveValue(first.name);
  expect(screen.getByRole("combobox", { name: /Video generator template/ })).toHaveTextContent(second.name);
  const before = state.latest();
  const writeText = vi.fn().mockResolvedValue(undefined);
  Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText } });
  fireEvent.click(screen.getByRole("button", { name: "Copy used seed" }));
  expect(writeText).toHaveBeenCalledWith("100");
  fireEvent.click(screen.getByRole("button", { name: "Use this generator" }));
  expect(loadActiveGeneratorId("video")).toBe(first.id);
  expect(screen.getByRole("combobox", { name: /Video generator template/ })).toHaveTextContent(first.name);
  expect(state.latest()).toEqual(before);
  cleanup();
  setup(config);
  expect(screen.getByRole("combobox", { name: /Video generator template/ })).toHaveTextContent(first.name);
});
it.each(["scene", "shot"])("offers Create Reference only after the %s has generated video", (target) => {
  const state = setup();
  const openMenu = () => fireEvent.contextMenu(target === "scene"
    ? screen.getByRole("region", { name: "First scene" }).querySelector(".scene-rule")!
    : screen.getByRole("button", { name: "Shot 2 of First scene" }));
  openMenu();
  expect(screen.queryByRole("menuitem", { name: "Create Reference" })).not.toBeInTheDocument();
  fireEvent.keyDown(screen.getByRole("menu"), { key: "Escape" });
  const config = state.latest(), job = config.generationJobs[0];
  state.replace(completeSceneGeneration(config, job.id, { id: "take", scene: sceneInput(job), createdAt: config.createdAt,
    media: { id: `asset-${job.id}`, kind: "generated", name: job.title, mimeType: "video/mp4", relativePath: "media/take.mp4", durationMs: 6000, hasAudio: true, createdAt: config.createdAt } }));
  openMenu();
  fireEvent.click(screen.getByRole("menuitem", { name: "Create Reference" }));
  const saved = parseProjectConfig(state.latest());
  expect(saved.references.at(-1)).toMatchObject({ kind: "video", relativePath: "media/take.mp4",
    name: target === "scene" ? "First scene" : "First scene — Shot 2",
    video: { mode: "video", startSeconds: target === "scene" ? 0 : 3, durationSeconds: target === "scene" ? 6 : 3, includeAudio: true } });
  expect(saved.generationJobs[0].outputRelativePath).toBe("media/take.mp4");
});

it("creates an exact short shot reference from the selected older take through the keyboard menu", () => {
  let config = project();
  config.generationJobs[0].shots![1].startSeconds = 5.5;
  const id = config.generationJobs[0].id;
  for (const takeId of ["one", "two"]) {
    const job = config.generationJobs[0];
    config = completeSceneGeneration(config, id, { id: takeId, scene: sceneInput(job), createdAt: config.createdAt,
      media: { id: `asset-${id}`, kind: "generated", name: job.title, mimeType: "video/mp4", relativePath: `media/${takeId}.mp4`, durationMs: 6000, hasAudio: false, createdAt: config.createdAt } });
  }
  const state = setup(config);
  fireEvent.click(screen.getByRole("button", { name: "Generation 1 of First scene" }));
  fireEvent.keyDown(screen.getByRole("button", { name: "Shot 2 of First scene" }), { key: "F10", shiftKey: true });
  fireEvent.click(screen.getByRole("menuitem", { name: "Create Reference" }));
  expect(parseProjectConfig(state.latest()).references.at(-1)).toMatchObject({ relativePath: "media/one.mp4",
    video: { startSeconds: 5.5, durationSeconds: 0.5, includeAudio: false } });
  expect(state.latest().generationJobs[0].outputRelativePath).toBe("media/two.mp4");
});