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

test("PR and issue section choices survive right-panel tab switches", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("section-state-demo");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project }));
  const start = (model: string) => t3.thread({ project, title: "Review section state", model, messages: ["Review https://github.com/pingdotgg/t3code/pull/47941"] });
  const thread = await start("multi-turn").catch((error) => {
    if (!String(error).includes("Advertised models: default")) throw error;
    return start("default").catch((retryError) => {
      if (!String(retryError).includes("Model default is not advertised")) throw retryError;
      return start("multi-turn");
    });
  });
  await browser.setViewport({ width: 1440, height: 900 });
  await t3.open(thread);
  await screen.getByRole("button", /^Agent Review/).tap();
  await screen.getByRole("link", /47941/).tap();
  const section = (name: string) => browser.locator('[data-right-panel-surface-content]').getByRole('button', { name: name === 'Activity' ? /^Activity(?: \(\d+\))?$/ : name, exact: true });
  await section("Description").click();
  await section("Checks").click();
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().map(a => a.finished.catch(() => {}))); });
  await app.screenshot("pr-chosen-sections");
  await screen.getByRole("button", "Add panel surface").tap();
  await screen.getByRole("menuitem", /^Files/).tap();
  await browser.locator('[data-right-panel-tab-list]').getByText(/47941/).click();
  await section("Description").waitFor();
  const prState = await browser.evaluate(() => Object.fromEntries(Array.from(document.querySelectorAll('[data-right-panel-surface-content] button[aria-expanded]')).map(b => [b.textContent?.trim().replace(/ \(\d+\)$/, ''), b.getAttribute('aria-expanded')])));
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().map(a => a.finished.catch(() => {}))); });
  await app.screenshot("pr-after-files-round-trip");
  // Reopen Description only if necessary so the real linked issue can be opened.
  if (await section("Description").getAttribute("aria-expanded") === "false") await section("Description").click();
  await browser.locator('[data-right-panel-surface-content]').getByRole('link', { name: 'pingdotgg/t3code#47935', exact: true }).click();
  await browser.locator('[data-right-panel-tab-list]').getByText(/47935/).waitFor();
  await section("Description").click();
  await section("Activity").click();
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().map(a => a.finished.catch(() => {}))); });
  await app.screenshot("issue-chosen-sections");
  await browser.locator('[data-right-panel-tab-list]').getByText('Files', { exact: true }).click();
  await browser.locator('[data-right-panel-tab-list]').getByText(/47935/).click();
  await section("Activity").waitFor();
  const issueState = await browser.evaluate(() => Object.fromEntries(Array.from(document.querySelectorAll('[data-right-panel-surface-content] button[aria-expanded]')).map(b => [b.textContent?.trim().replace(/ \(\d+\)$/, ''), b.getAttribute('aria-expanded')])));
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().map(a => a.finished.catch(() => {}))); });
  await app.screenshot("issue-after-files-round-trip");
  expect({ prState, issueState }).toEqual({ prState: expect.objectContaining({ Description: "false", Checks: "true" }), issueState: expect.objectContaining({ Description: "false", Activity: "false" }) });
});
