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

test("Outdated review thread cards follow the selected German UI language", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("review-language");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project }));
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open("/settings/general");
  await screen.getByLabel("Language", { exact: true }).tap();
  await screen.getByText("Deutsch", { exact: true }).tap();
  await screen.getByLabel("Sprache", { exact: true }).waitFor();
  await t3.open("/pull-requests?state=all");
  await screen.getByRole("searchbox", "Pull Requests durchsuchen", { exact: true }).fill("43697");
  await screen.getByRole("button", /Gemergt #43697/).tap();
  await screen.getByRole("button", "Code", { exact: true }).tap();
  await screen.getByRole("button", /Unterhaltungen, die nicht im aktuellen Diff liegen/).tap();
  await screen.getByRole("button", "Offen · 1 Kommentar", { exact: true }).waitFor();
  // Wait for the real height animation, including its starting style, to finish before capture.
  await browser.evaluate(async () => {
    const card = Array.from(document.querySelectorAll("button")).find(el => el.textContent?.trim() === "Offen · 1 Kommentar");
    const panel = card?.closest('[data-slot="collapsible-panel"]');
    if (!panel) throw new Error("Review conversations panel missing");
    let previous = -1;
    let stable = 0;
    for (let frame = 0; frame < 240; frame++) {
      await new Promise<void>(resolve => requestAnimationFrame(() => resolve()));
      const height = panel.getBoundingClientRect().height;
      stable = height === previous && !panel.hasAttribute("data-starting-style") && panel.getAnimations().every(animation => animation.playState === "finished") ? stable + 1 : 0;
      previous = height;
      if (stable >= 35) return;
    }
    throw new Error("Review conversations panel did not settle");
  });
  await app.screenshot("outdated-review-german");
  await expect(screen.getByText("Veraltet", { exact: true })).toBeVisible();
});
