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

test("Long selected commit leaves file counts clear of toolbar controls", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("PR toolbar proof");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project }));
  await browser.setViewport({ width: 1056, height: 800 });
  await t3.open(`/pull-requests?${new URLSearchParams({ state: "merged", q: "41751", selectedUrl: "https://github.com/pingdotgg/t3code/pull/41751" })}`);
  await screen.getByRole("searchbox", "Search pull requests", { exact: true }).fill("41751");
  await screen.getByRole("button", /^Merged #41751 /).tap();
  await screen.getByRole("button", "Timeline", { exact: true }).tap();
  await screen.getByRole("button", /^View commit 3caa0a6/).tap();
  await screen.getByRole("button", /^Diff scope: fix\(server\)/).waitFor();
  await screen.getByText("0 / 5", { exact: true }).waitFor({ timeout: 60000 });
  await app.screenshot("long-commit-narrow-pane");
  const layout = await browser.evaluate(() => {
    const picker = document.querySelector('[aria-label^="Diff scope:"]')!;
    const left = picker.parentElement!;
    const toolbar = left.parentElement!;
    const controls = left.nextElementSibling!;
    const count = [...left.querySelectorAll('span')].find(e => e.textContent === '5 files')!;
    const viewed = [...left.querySelectorAll('span')].find(e => e.textContent === '0 / 5')!;
    return { width: toolbar.getBoundingClientRect().width, pickerRight: picker.getBoundingClientRect().right, countRight: count.getBoundingClientRect().right, viewedRight: viewed.getBoundingClientRect().right, controlsLeft: controls.getBoundingClientRect().left };
  });
  console.log(JSON.stringify(layout));
  expect(layout.pickerRight).toBeLessThanOrEqual(layout.controlsLeft);
  expect(layout.countRight).toBeLessThanOrEqual(layout.controlsLeft);
  expect(layout.viewedRight).toBeLessThanOrEqual(layout.controlsLeft);
});
