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

test("GitLab HTML rename notes show the complete new title", WEB, async ({ t3, app, browser, screen }) => {
  const workspace = await t3.project("demo");
  const repository = `${T3_HOME}/rename-proof`;
  await runPromise(run("git", ["clone", "--quiet", workspace, repository]));
  await runPromise(run("git", ["remote", "set-url", "origin", "https://gitlab.com/julius54/rename-activity-proof-44390.git"], { cwd: repository }));
  const added = await runPromise(makeState(T3_HOME).cli(["project", "add", repository, "--title", "Login issues"]));
  const projectId = /Added project ([^ ]+)/.exec(added.stdout)![1];
  const titles = ["Fix login on Linux", "Fix login", 'Fix <login> & "session" on Linux'];
  const observed: string[] = [];
  for (const [index, title] of titles.entries()) {
    await t3.open(`/issues?selectedProjectId=${projectId}&tracker=gitlab&scope=julius54%2Frename-activity-proof-44390&key=${index + 1}`);
    await screen.getByRole("button", "Refresh issue", { exact: true }).waitFor({ timeout: 60_000 });
    await screen.getByRole("heading", title, { exact: true }).waitFor({ timeout: 60_000 });
    await browser.locator('section[aria-label^="Activity"]').waitFor();
    await app.screenshot(`rename-${index + 1}`);
    observed.push(await browser.evaluate(() => document.querySelector('section[aria-label^="Activity"]')?.textContent ?? ""));
  }
  expect(observed.map((text, index) => text.includes(`renamed this to “${titles[index]}”`))).toEqual([true, true, true]);
});
