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

test("Agent tasks are opt-in, expandable, compact, and configurable in Appearance", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Task progress");
  const thread = await t3.thread({ project, title: "Inspect project files", model: "todo-list", messages: ["Track three steps: inspect package.json, inspect tsconfig.json, report completion."] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open(thread);
  const details = browser.locator('[data-thread-details-card]');
  await details.waitFor();
  await screen.getByText("todo list fixture complete", { exact: true }).waitFor();
  await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("default-off");
  await expect(details.getByRole("region", "Tasks", { exact: true })).not.toBeVisible();
  const openMenu = async () => {
    await browser.evaluate(() => {
      const card = document.querySelector('[data-thread-details-card]')!;
      const rect = card.getBoundingClientRect();
      card.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true, cancelable: true, clientX: rect.left + 30, clientY: rect.top + 30 }));
      return null;
    });
    await screen.getByText("Show in thread details", { exact: true }).waitFor();
  };
  await openMenu();
  await app.screenshot("customization-menu");
  const option = screen.getByRole("menuitemcheckbox", "Agent tasks", { exact: true });
  await expect(option).toBeVisible();
  await expect(option).toHaveAttribute("aria-checked", "false");
  await option.tap();
  await browser.keyboard.press("Escape");
  await screen.getByRole("button", "Expand tasks, 3 of 3 completed").waitFor();
  await app.screenshot("collapsed-tasks");
  await screen.getByRole("button", "Expand tasks, 3 of 3 completed").tap();
  await expect(details.getByText("Inspect package.json", { exact: false })).toBeVisible();
  await expect(details.getByText("Inspect tsconfig.json", { exact: false })).toBeVisible();
  await expect(details.getByText("Report completion", { exact: false })).toBeVisible();
  await app.screenshot("expanded-tasks");
  await browser.setViewport({ width: 1440, height: 360 });
  await expect(details.getByRole("region", "Tasks", { exact: true })).toBeVisible();
  await expect(screen.getByRole("button", "Collapse tasks, 3 of 3 completed")).not.toBeVisible();
  await expect(details.getByText("All done", { exact: true })).toBeVisible();
  await app.screenshot("short-window");
  await openMenu();
  await screen.getByRole("menuitemcheckbox", "Agent tasks", { exact: true }).tap();
  await browser.keyboard.press("Escape");
  await expect(details.getByRole("region", "Tasks", { exact: true })).not.toBeVisible();
  await browser.mouse.move(600, 250);
  await screen.getByRole("textbox").tap();
  await screen.getByRole("tooltip").waitFor({ state: "hidden" });
  await app.screenshot("disabled-tasks");
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open("/settings/appearance");
  await screen.getByRole("button", /^Thread details:/).tap();
  await expect(option).toHaveAttribute("aria-checked", "false");
  await app.screenshot("appearance-menu");
  await option.tap();
  await browser.keyboard.press("Escape");
  await t3.open(thread);
  await expect(details.getByRole("region", "Tasks", { exact: true })).toBeVisible();
  await screen.getByText("todo list fixture complete", { exact: true }).waitFor();
  await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("enabled-from-appearance");
  await screen.getByRole("textbox").fill("Update the same three-step task list while inspecting the files again.");
  await screen.getByRole("textbox").press("Enter");
  await screen.getByRole("button", "Expand tasks, 0 of 3 completed").waitFor();
  await expect(details.getByText("Running: Inspect package.json", { exact: false })).toBeVisible();
  await app.screenshot("current-running-task");
  await screen.getByRole("button", "Expand tasks, 3 of 3 completed").waitFor();
  await screen.getByRole("button", "Stop generation", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("updated-completion-count");
});
