/// <reference lib="dom" />
import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Comment hover preserves literal alert markers", WEB, async ({t3, app, browser, screen}) => {
  await browser.setViewport({width:1280,height:800});
  const project = await t3.project("alert-excerpt-proof");
  execFileSync("git", ["remote", "add", "origin", "https://gitlab.com/julius54/alert-excerpt-proof-44024.git"], {cwd:project});
  execFileSync("git", ["checkout", "-b", "excerpt-proof"], {cwd:project});
  const urls = ["https://gitlab.com/julius54/alert-excerpt-proof-44024/-/merge_requests/1#note_4337674595", "https://gitlab.com/julius54/alert-excerpt-proof-44024/-/merge_requests/1#note_4337674697", "https://gitlab.com/julius54/alert-excerpt-proof-44024/-/merge_requests/1#note_4337674735"];
  const thread = await t3.thread({project, title:"Alert marker hover excerpts", messages:[urls.join("\n\n")]});
  await t3.open(thread);
  await screen.getByRole("button", /^Agent https/).tap();
  const expected = ["Keep [!NOTE] in the file.", "Real alert.", "Keep [!WARNING] in prose."];
  const labels = ["inline-code-marker", "real-alert", "prose-marker"];
  const actual: string[] = [];
  const contents = [/^Keep .*in the file\.$/, /^Real alert\.$/, /^Keep .*in prose\.$/];
  for (let i=0; i<urls.length; i++) {
    await browser.locator(`a[href="${urls[i]}"]`).hover();
    const excerpt = browser.locator('[data-slot="preview-card-popup"]').getByText(contents[i]!);
    await excerpt.waitFor();
    await browser.evaluate(async () => { await Promise.all(document.getAnimations().map((animation) => animation.finished.catch(() => {}))); });
    await app.screenshot(labels[i]!);
    actual.push((await excerpt.textContent())!.trim());
    await screen.getByRole("textbox").hover();
    await browser.locator('[data-slot="preview-card-popup"]').waitFor({state:"hidden"});
  }
  expect(actual).toEqual(expected);
});
