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

test("Pasting a reference definition preserves its thread destination", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Link review");
  const target = await t3.thread({ project, title: "Target thread for links", messages: ["Start"] });
  const source = await t3.thread({ project, title: "Review thread links", messages: ["Start"] });
  await t3.open(source);
  const composer = screen.getByRole("textbox");
  await composer.tap();
  const href = await browser.evaluate((threadId: string) => {
    const environmentId = decodeURIComponent(window.location.pathname.split("/")[1]!);
    const href = `https://app.t3.codes/threads/${environmentId}/${encodeURIComponent(threadId)}`;
    const clipboardData = new DataTransfer();
    clipboardData.setData("text/plain", `[Review][thread]\n\n[thread]: ${href}\n\nOrdinary thread link: ${href}`);
    const editor = document.activeElement;
    if (!editor?.matches('[contenteditable="true"]')) throw new Error("Composer is not focused");
    editor.dispatchEvent(new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }));
    return href;
  }, target.threadId);
  await composer.getByText("Ordinary thread link:", { exact: false }).waitFor();
  await app.screenshot("reference-destination-in-composer");
  await expect(composer).toContainText(`[thread]: ${href}`);
});
