import { expect } from "e2e";
import { copyFileSync } from "node:fs";
import { execFileSync } from "node:child_process";
import { join } from "node:path";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Forgejo capped pages keep Load more until all 60 issues are listed", WEB, async ({ t3, app, browser, screen }) => {
  const source = process.env.FORGEJO_PROOF_FJ!;
  copyFileSync(source, join(T3_HOME, "bin", "fj"));
  const workspace = await t3.project("web");
  execFileSync("git", ["remote", "add", "origin", "http://127.0.0.1:23171/alice/web.git"], { cwd: workspace });
  await t3.open("/issues?who=all&status=open");
  const rows = screen.getByRole("button", /Pagination issue \d+/);
  await screen.getByText("Pagination issue 60", { exact: true }).waitFor();
  const more = screen.getByRole("button", "Load more", { exact: true });
  await more.click();
  await screen.getByText("Pagination issue 21", { exact: true }).waitFor();
  await screen.getByRole("button", /Pagination issue 21/).scrollIntoView();
  await app.screenshot("forty-issues-loaded");
  await expect(rows).toHaveCount(40);
  await expect(more).toBeVisible();
  await more.click();
  await screen.getByText("Pagination issue 01", { exact: true }).waitFor();
  await screen.getByRole("button", /Pagination issue 01/).scrollIntoView();
  await app.screenshot("all-sixty-issues-loaded");
  await expect(rows).toHaveCount(60);
  await expect(more).toBeHidden();
});
