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

test("Every repeated hosted thread link paste becomes a thread card", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Paste demonstration");
  const source = await t3.thread({ project, title: "Source thread", messages: ["Source context"] });
  const destination = await t3.thread({ project, title: "Destination thread", messages: ["Destination context"] });
  await t3.open(source);
  const environmentId = await browser.evaluate(async () => (await (await fetch("/.well-known/t3/environment")).json()).environmentId);
  const url = `https://app.t3.codes/threads/${environmentId}/${encodeURIComponent(source.threadId)}`;
  await t3.open(destination);
  const editor = browser.locator('[contenteditable="true"]').first();
  await editor.tap();
  const paste = () => browser.evaluate((text: string) => {
    const clipboardData = new DataTransfer();
    clipboardData.setData("text/plain", text);
    document.querySelector('[contenteditable="true"]')!.dispatchEvent(new ClipboardEvent("paste", { bubbles: true, cancelable: true, clipboardData }));
  }, url);
  await paste();
  await screen.getByRole("link", "Thread, Source thread").first().waitFor({ state: "visible" });
  await app.screenshot("first-paste");
  await editor.tap();
  await paste();
  await app.screenshot("repeated-paste");
  await expect(editor).not.toContainText("https://app.t3.codes/threads/");
  await expect(screen.getByRole("link", "Thread, Source thread")).toHaveCount(2);
});
