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

test("Choice-only question retains keyboard focus and Tab reaches its first answer", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Keyboard focus demo");
  const thread = await t3.thread({ project, title: "Keyboard starting point", messages: ["Start"], model: "multi-turn" });
  await t3.open(thread);
  const editor = browser.locator('[data-testid="composer-editor"][contenteditable="true"]');
  await editor.tap();
  await browser.keyboard.type("x");
  await browser.keyboard.press("Backspace");
  await browser.evaluate(async () => { await (window as any).t3dev.replay("question"); });
  await screen.getByText("Should this fixture prefer strict schemas or UI flexibility?", { exact: false }).waitFor();
  await browser.locator('[data-testid="composer-editor"][contenteditable="false"]').waitFor();
  const headerFocused = await browser.evaluate(() => document.activeElement?.matches('[data-pending-user-input-toggle]') ?? false);
  await app.screenshot("question-header-focus");
  await browser.keyboard.press("Tab");
  const firstAnswerFocused = await browser.evaluate(() => document.activeElement?.textContent?.includes("Strict schemas") ?? false);
  await app.screenshot("first-tab-focus");
  expect(headerFocused).toBe(true);
  expect(firstAnswerFocused).toBe(true);
});
