/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Escape closes an enlarged image before cancelling dictation", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("image-notes");
  const thread = await t3.thread({project, title: "Image and dictation", messages: ["first"]});
  await t3.open("/settings/voice");
  await screen.getByRole("combobox", "Transcribe with").tap();
  await screen.getByRole("option", /OpenAI/).tap();
  await screen.getByRole("textbox", "OpenAI API key", {exact:true}).fill("sk-dummy-for-local-recording-only");
  await screen.getByRole("button", "Save and enable", {exact:true}).tap();
  await screen.getByRole("combobox", "Transcribe with").filter({hasText: "OpenAI"}).waitFor();
  await t3.open(thread);
  await screen.getByText("first fixture turn complete", {exact:false}).waitFor();
  await screen.getByRole("textbox").fill("Describe this sample image.");
  await browser.evaluate(async () => {
    const canvas = document.createElement("canvas"); canvas.width=640; canvas.height=360;
    const ctx=canvas.getContext("2d")!; ctx.fillStyle="#e0f2fe";ctx.fillRect(0,0,640,360);
    ctx.fillStyle="#0369a1";ctx.fillRect(50,50,540,260);ctx.fillStyle="white";ctx.font="36px sans-serif";ctx.fillText("Sample image",195,190);
    const blob = await new Promise<Blob>(resolve=>canvas.toBlob(b=>resolve(b!),"image/png"));
    const data=new DataTransfer();data.items.add(new File([blob],"sample.png",{type:"image/png"}));
    document.querySelector('[contenteditable="true"]')!.dispatchEvent(new ClipboardEvent("paste",{clipboardData:data,bubbles:true,cancelable:true}));
    return null;
  });
  await screen.getByRole("button", "Image attachment", {exact:false}).waitFor();
  await browser.keyboard.press("Enter");
  await screen.getByRole("button", "Preview sample.png", {exact:true}).waitFor();
  // Hold the interaction states long enough to inspect them in the proof video.
  const holdForVideo = () => browser.evaluate(() => new Promise<null>(resolve => setTimeout(() => resolve(null), 1800)));
  await holdForVideo();
  await screen.getByRole("button", "Start voice input", {exact:true}).tap();
  await screen.getByRole("button", "Cancel voice input", {exact:true}).waitFor();
  await screen.getByRole("button", "Finish voice input", {exact:true}).waitFor();
  await holdForVideo();
  await screen.getByRole("button", "Preview sample.png", {exact:true}).tap();
  await screen.getByRole("dialog", "Expanded image preview", {exact:true}).waitFor();
  await holdForVideo();
  await app.screenshot("recording-with-image-expanded");
  await browser.keyboard.press("Escape");
  await holdForVideo();
  await app.screenshot("after-first-escape");
  const firstEscape = await browser.evaluate(() => ({
    expanded: document.querySelector('[role="dialog"]') !== null,
    recording: document.querySelector('button[aria-label="Cancel voice input"]') !== null,
  }));
  await browser.keyboard.press("Escape");
  await holdForVideo();
  await app.screenshot("after-second-escape");
  expect(firstEscape, "First Escape closes the image and keeps dictation recording").toEqual({expanded: false, recording: true});
  await expect(screen.getByRole("button", "Start voice input", {exact:true})).toBeVisible();
});
