import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Typing outside the read-only composer during dictation preserves the draft", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("Dictation demo");
  const thread = await t3.thread({project, title:"Dictation draft", messages:["Ready"]});
  await t3.open("/settings/voice");
  await screen.getByRole("combobox", "Transcribe with").tap();
  await screen.getByRole("option", /OpenAI via/).tap();
  await screen.getByRole("textbox", "OpenAI API key").fill("sk-dictation-proof-dummy-key");
  await screen.getByRole("button", "Save and enable").tap();
  await screen.getByRole("button", "Save and enable").waitFor({state:"hidden"});
  await t3.open(thread);
  const composer = browser.locator('[role="textbox"][contenteditable]');
  await composer.fill("Draft");
  await screen.getByRole("button", "Start voice input").tap();
  await composer.waitFor({state:"visible"});
  await screen.getByRole("status", "Recording").waitFor({state:"visible"});
  await screen.getByRole("region", "Conversation").tap({position:{x:450,y:80}});
  await browser.keyboard.type("abc");
  await browser.keyboard.press("Backspace");
  await browser.keyboard.press("Backspace");
  await browser.keyboard.press("ArrowLeft");
  await app.screenshot("dictation-after-typing");
  await expect(composer).toHaveText("Draft");
});
