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

test("Pasted titled links and multiline code remain literal while ordinary links become chips", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Markdown links");
  await t3.thread({ project, title: "Destination thread", messages: ["Create destination"] });
  await t3.thread({ project, title: "Paste verification", messages: ["Ready for pasted Markdown"] });
  await t3.open();
  await screen.getByRole("button", /^Destination thread,/).tap();
  await screen.getByRole("textbox", "Message Destination thread").tap();
  await browser.keyboard.press("Control+Shift+C");
  await screen.getByText("Thread copied", { exact: true }).waitFor();
  const copied = await browser.evaluate(() => navigator.clipboard.readText());
  const url = copied.match(/https:\/\/app\.t3\.codes\/threads\/[^\s)]+/)?.[0];
  if (!url) throw new Error("Copy thread did not supply its hosted link");
  await screen.getByRole("button", /^Paste verification,/).tap();
  const editor = screen.getByRole("textbox", "Message Paste verification");
  await editor.tap();
  const source = `[label](${url} "tooltip")\n\n\`opening text\n${url}\nclosing text\`\n\n${url}`;
  await browser.evaluate(async (text: string) => navigator.clipboard.writeText(text), source);
  await browser.keyboard.press("Control+V");
  await app.screenshot("pasted-markdown");
  await expect(editor).toContainText(url);
  await expect(editor).toContainText("tooltip");
  await expect(editor).toContainText("opening text");
  await expect(editor).toContainText("closing text");
  await expect(editor).toContainText("Destination thread");
  // Exactly one chip: the ordinary link after the blank line.
  await expect(browser.locator('[role="textbox"] [data-node-view-wrapper]').filter({ hasText: "Destination thread" })).toHaveCount(1);
});
