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

test("Starting without a project retains keyboard focus on the new picker", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Welcome", messages: ["Hello"] });
  await t3.open(thread);
  await screen.getByRole("button", "New thread", { exact: true }).tap();
  await browser.locator('[data-testid="composer-editor"]').waitFor();
  await browser.evaluate(async () => { await new Promise(resolve => setTimeout(resolve, 1000)); return null; });
  await browser.keyboard.press("Shift+Tab");
  await app.screenshot("keyboard-on-start-without-project");
  await browser.keyboard.press("Enter");
  await screen.getByText("What should we work on?", { exact: true }).waitFor();
  // Observe after the composer autofocus has had time to run, matching the reported reproduction.
  await browser.evaluate(async () => { await new Promise(resolve => setTimeout(resolve, 2500)); return null; });
  await app.screenshot("no-project-picker-after-keyboard-activation");
  await expect(browser.locator('[data-draft-project-trigger]:focus-visible')).toHaveText("No project");
});
