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

test("An unchosen voice destination is distinct from saved Off", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Voice settings demo");
  const thread = await t3.thread({ project, title: "Voice destination", messages: ["Hello"] });
  await t3.open(thread);
  await browser.evaluate(() => { localStorage.removeItem("t3code:voice-audio-grant:v1"); return null; });
  await t3.open(thread);
  await screen.getByRole("button", "Start voice input", { exact: true }).waitFor();
  await screen.getByRole("textbox").waitFor();
  await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "detached" });
  await app.screenshot("initial-composer-mic");
  await t3.open("/settings/voice");
  const destination = screen.getByRole("combobox", "Transcribe with");
  await destination.waitFor();
  const unsetLabel = await browser.evaluate(() => document.querySelector('[role="combobox"][aria-label="Transcribe with"]')?.textContent?.trim());
  await app.screenshot("unchosen-destination");
  await destination.tap();
  await screen.getByRole("option", "Off", { exact: true }).waitFor();
  await app.screenshot("unchosen-destination-options");
  await screen.getByRole("option", "Off", { exact: true }).tap();
  await app.screenshot("saved-off");
  const saved = await browser.evaluate(() => localStorage.getItem("t3code:voice-audio-grant:v1"));
  await t3.open(thread);
  await screen.getByRole("textbox").waitFor();
  await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "detached" });
  await screen.getByRole("button", "Start voice input", { exact: true }).waitFor({ state: "detached" });
  await app.screenshot("saved-off-composer");
  expect(saved).toBe('"off"');
  expect(unsetLabel).not.toBe("Off");
});
