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

test("Focused native audio controls keep Space and m out of the composer", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("audio-demo");
  const thread = await t3.thread({ project, title: "Audio keyboard controls", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByText("first fixture turn complete", { exact: false }).waitFor();
  // Supply a synthetic 8-second WAV to the app's actual attachment file input.
  await browser.evaluate(() => {
    const rate = 8000;
    const bytes = new ArrayBuffer(44 + rate * 8 * 2);
    const view = new DataView(bytes);
    const text = (offset: number, value: string) => [...value].forEach((c, i) => view.setUint8(offset + i, c.charCodeAt(0)));
    text(0, "RIFF"); view.setUint32(4, bytes.byteLength - 8, true); text(8, "WAVEfmt ");
    view.setUint32(16, 16, true); view.setUint16(20, 1, true); view.setUint16(22, 1, true);
    view.setUint32(24, rate, true); view.setUint32(28, rate * 2, true); view.setUint16(32, 2, true); view.setUint16(34, 16, true);
    text(36, "data"); view.setUint32(40, bytes.byteLength - 44, true);
    for (let i = 0; i < rate * 8; i++) view.setInt16(44 + i * 2, Math.sin(i * 2 * Math.PI * 440 / rate) * 3000, true);
    const data = new DataTransfer();
    data.items.add(new File([bytes], "tone.wav", { type: "audio/wav" }));
    const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
    input.files = data.files;
    input.dispatchEvent(new Event("change", { bubbles: true }));
    return null;
  });
  await screen.getByRole("button", "File attachment, tone.wav", { exact: false }).waitFor();
  await screen.getByRole("button", "Submit message", { exact: true }).tap();
  await browser.locator('[data-message-role="user"] button[aria-label="Preview tone.wav"]').tap();
  const audio = browser.locator("audio[controls]");
  await audio.waitFor();
  // Click the native player's time label, away from its play button.
  await audio.tap({ position: { x: 75, y: 27 } });
  await browser.evaluate(() => {
    const audio = document.querySelector("audio[controls]") as HTMLAudioElement;
    if (document.activeElement !== audio || !audio.paused) throw new Error("Time-label click must focus the paused native audio player");
    return { focused: document.activeElement.tagName, paused: audio.paused, duration: audio.duration };
  });
  await app.screenshot("audio-focused-before-keys");
  await browser.keyboard.press("Space");
  await browser.keyboard.press("m");
  await app.screenshot("audio-after-space-and-m");
  const state = await browser.evaluate(() => ({
    focused: document.activeElement?.tagName,
    composer: document.querySelector('[role="textbox"]')?.textContent ?? "",
  }));
  await expect(state).toEqual({ focused: "AUDIO", composer: "" });
  // The ordinary type-to-focus path must still work outside the player.
  await screen.getByText("first fixture turn complete", { exact: false }).tap();
  await browser.keyboard.press("x");
  await app.screenshot("typing-elsewhere-still-reaches-composer");
  await expect(screen.getByRole("textbox")).toHaveText("x");
});
