import { writeFile } from "node:fs/promises";
import { expect } from "e2e";
import { run } from "./shell.ts";
import { runPromise, test, WEB } from "./t3.ts";

test("Git action dropdown hover fills the expanded commit success row", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Hover proof", { "README.md": "# Sample repository\n" });
  const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
  await git("config", "user.name", "Scenario");
  await git("config", "user.email", "scenario@example.invalid");
  const thread = await t3.thread({ project, title: "Commit hover proof", messages: ["Show the sample repository"] });
  await writeFile(`${project}/README.md`, "# Sample repository\n\nA change note.\n");
  await writeFile(`${project}/notes.txt`, "Synthetic change notes.\n");
  await t3.open(thread);
  // An explicit message avoids depending on a paid provider's message generation.
  await screen.getByRole("button", "Git action options").tap();
  await browser.locator('[role="menuitem"]:not([data-disabled]):not([aria-disabled="true"])').filter({ hasText: "Commit" }).tap();
  await screen.getByPlaceholder("Leave empty to auto-generate").fill("Add a README note");
  await screen.getByRole("button", "Commit", { exact: true }).tap();
  await browser.evaluate(() => new Promise<void>((resolve) => {
    const ready = () => document.querySelector('[role="group"][aria-label="Git actions"]')?.textContent?.includes("Add a README note");
    if (ready()) return resolve();
    const observer = new MutationObserver(() => {
      if (ready()) { observer.disconnect(); resolve(); }
    });
    observer.observe(document.body, { childList: true, subtree: true, characterData: true });
  }));
  const dropdown = screen.getByRole("button", "Git action options");
  await dropdown.hover();
  await app.screenshot("expanded-success-hover");
  const row = await screen.getByRole("group", "Git actions", { exact: true }).boundingBox();
  const chevron = await dropdown.boundingBox();
  // Let the real success timeout return the row to its normal state.
  await screen.getByRole("button", "Publish repository", { exact: true }).hover();
  await dropdown.hover();
  await app.screenshot("normal-row-hover");
  expect((await dropdown.boundingBox())?.height).toBe(32);
  expect(chevron?.height).toBe(row?.height);
  expect(chevron?.y).toBe(row?.y);
});
