import { expect } from "e2e";
import { test, IOS } from "./t3-mobile.ts";

test("Closed math fences keep mentions literal on iOS", IOS, async ({ t3, app, device, screen }) => {
  const project = await t3.project("Math fence demo", { note: "Synthetic mention target" });
  await t3.thread({ project, title: "Math fence clipboard cases", messages: ["Hello"] });
  await t3.open();
  const thread = screen.getByText("Math fence clipboard cases", { exact: false }).first();
  await expect(thread).toBeVisible({ timeout: 120_000 });
  await thread.tap();
  const composer = screen.getByRole("textbox", "Ask anything");
  await expect(composer).toBeVisible();
  const cases = [
    ["CR closed", "```math\r@note \r```"],
    ["LF closed", "```math\n@note \n```"],
    ["CRLF closed", "```math\r\n@note \r\n```"],
    ["CR unclosed", "```math\r@note "],
    ["CR shorter closer", "````math\r@note \r```"],
    ["CR mismatched closer", "```math\r@note \r~~~"],
    ["CR blockquote", "> ```math\r> @note \r> ```"],
    ["CR indented closer", "```math\r@note \r    ```"],
    ["LF indented closer", "```math\n@note \n    ```"],
  ];
  const counts: number[] = [];
  for (const [label, payload] of cases) {
    await composer.fill("");
    await device.setClipboard(payload!);
    const readback = await device.clipboard();
    if (readback !== payload) throw new Error("Clipboard changed line endings");
    await composer.longPress();
    await screen.getByText("Paste", { exact: true }).tap();
    await app.screenshot(label!);
    counts.push(await screen.getByRole("button", "note", { exact: true }).count());
  }
  expect(counts).toEqual([0, 0, 0, 1, 1, 1, 0, 1, 1]);
});
