import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Clone error drops the whole boundary emoji", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Clone failure", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("combobox", "Projects", { exact: true }).tap();
  await screen.getByText("Add project", { exact: true }).tap();
  await screen.getByText("Git URL", { exact: true }).tap();
  const prefix = "/tmp/clone-emoji-proof/missing/";
  const segments = Array(4).fill("a".repeat(199)).join("/") + "/";
  const repository = prefix + segments + "a".repeat(979 - prefix.length - segments.length) + "🚀tail-beyond-the-limit";
  await screen.getByRole("combobox").fill(repository);
  await screen.getByRole("button", "Continue (Enter)", { exact: true }).tap();
  await screen.getByRole("combobox").fill("/tmp/factory-pr-41669-evidence-1/clones/dest");
  await screen.getByRole("button", /^(Create & )?Clone/).tap();
  await screen.getByText("Failed to clone dest", { exact: true }).first().waitFor();
  await screen.getByRole("button", "Show details: Failed to clone dest", { exact: true }).tap();
  const details = browser.locator('[aria-label="Notice details"]');
  await details.waitFor();
  await browser.evaluate(() => {
    for (const animation of document.getAnimations()) {
      if (Number.isFinite(animation.effect?.getComputedTiming().endTime)) animation.finish();
    }
    return null;
  });
  await app.screenshot("clone-error-details");
  const text = await browser.evaluate(() => document.querySelector('[aria-label="Notice details"]')!.textContent ?? "");
  expect(text.endsWith("a…"), "Error ends with a complete character and ellipsis").toBe(true);
  expect(text.length <= 1000, "Error fits the 1000 UTF-16 unit limit").toBe(true);
});
