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

test("Selected commits retain Git's whole subject", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("widget-demo");
  await runPromise(Effect.gen(function* () {
    const fs = yield* FileSystem.FileSystem;
    const git = (args: string[]) => run("git", args, { cwd: project });
    const author = ["-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid"];
    yield* git(["checkout", "-b", "feature/widget"]);
    yield* fs.writeFileString(`${project}/widget.txt`, "Widget implementation\n");
    yield* git(["add", "."]);
    yield* git([...author, "commit", "--cleanup=verbatim", "-m", "\n\nAdd widget\n\nBody"]);
    yield* fs.writeFileString(`${project}/wrap.txt`, "Wrapped subject example\n");
    yield* git(["add", "."]);
    yield* git([...author, "commit", "-m", "Line one\nline two\n\nBody"]);
  }));
  const thread = await t3.thread({ project, title: "Review widget commits", messages: ["first"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Diff", { exact: true }).tap();
  const labels: string[] = [];
  for (const [subject, file, label] of [["Add widget", "widget.txt", "leading-blank-lines"], ["Line one line two", "wrap.txt", "wrapped-subject"]]) {
    await screen.getByRole("button", /^Diff scope:/).tap();
    await screen.getByText("Commits", { exact: true }).tap();
    await screen.getByRole("menuitemradio", new RegExp(subject)).tap();
    await screen.getByRole("button", new RegExp(`(Collapse|Expand) ${file}`)).waitFor();
    await app.screenshot(label);
    labels.push(await browser.evaluate(() => document.querySelector('button[aria-label^="Diff scope:"]')?.getAttribute("aria-label") ?? ""));
  }
  expect(labels).toEqual(["Diff scope: Add widget", "Diff scope: Line one line two"]);
});
