import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import { T3_HOME } from "../e2e.config.ts";
import { run } from "./shell.ts";
import { makeState, runPromise, test, WEB } from "./t3.ts";

test("Complete Forgejo lists have no false continuation indicators", WEB, async ({t3,app,browser,screen}) => {
  const fixture = process.env.FORGEJO_PROOF_FIXTURE!;
  await runPromise(Effect.gen(function* () {
    const fs = yield* FileSystem.FileSystem;
    const path = yield* Path.Path;
    yield* fs.copyFile(path.join(fixture,"fj"),path.join(T3_HOME,"bin/fj"));
    yield* fs.chmod(path.join(T3_HOME,"bin/fj"),0o755);
  }));
  const workspace = await t3.project("Fixture");
  const primary = makeState(T3_HOME);
  const add = async (name: string) => {
    const target = T3_HOME + "/" + name;
    await runPromise(run("git",["clone","--quiet",workspace,target]));
    await runPromise(run("git",["remote","set-url","origin",`http://localhost:3177/alice/${name}.git`],{cwd:target}));
    await runPromise(primary.cli(["project","add",target,"--title",name]));
    return target;
  };
  const small = await add("small");
  await t3.open("/issues?who=all&status=open");
  await screen.getByText("Only open issue",{exact:true}).waitFor();
  await app.screenshot("single-page-issues");
  const falseMore = await screen.getByRole("button","Load more",{exact:true}).count();
  await screen.getByRole("button","Filters",{exact:false}).tap();
  await screen.getByRole("menuitem",/^Labels/).tap();
  await screen.getByRole("menuitemcheckbox","feature",{exact:true}).waitFor();
  await app.screenshot("complete-label-picker");
  const labelWarning = await screen.getByText("More labels exist than are listed here.",{exact:true}).count();
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Escape");
  await screen.getByRole("button","Filters",{exact:false}).tap();
  await screen.getByRole("menuitem",/^Assignee/).tap();
  await screen.getByRole("menuitemradio",/^alice/).waitFor();
  await app.screenshot("complete-assignee-picker");
  const peopleWarning = await screen.getByText("More people exist than are listed here.",{exact:true}).count();
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Escape");
  await runPromise(primary.cli(["project","remove",small,"--force"]));
  const big = await add("big");
  await t3.open("/issues?who=all&status=open");
  await screen.getByText("Pagination issue 52",{exact:true}).waitFor();
  await browser.evaluate(() => { for (const el of document.querySelectorAll("*")) { if (el.scrollHeight > el.clientHeight && /auto|scroll/.test(getComputedStyle(el).overflowY)) el.scrollTop = el.scrollHeight; } return null; });
  await app.screenshot("real-first-page");
  await screen.getByRole("button","Load more",{exact:true}).tap();
  await screen.getByText("Pagination issue 01",{exact:true}).waitFor();
  await browser.evaluate(() => { for (const el of document.querySelectorAll("*")) { if (el.scrollHeight > el.clientHeight && /auto|scroll/.test(getComputedStyle(el).overflowY)) el.scrollTop = el.scrollHeight; } return null; });
  await app.screenshot("short-final-page");
  const finalMore = await screen.getByRole("button","Load more",{exact:true}).count();
  await runPromise(primary.cli(["project","remove",big,"--force"]));
  expect({falseMore,labelWarning,peopleWarning,finalMore}).toEqual({falseMore:0,labelWarning:0,peopleWarning:0,finalMore:0});
});
