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

test("A linked word pasted inside inline code stays plain text", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Inline code paste", messages: ["first"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.tap();
  await browser.keyboard.type("`sumBy`");
  await browser.keyboard.press("End");
  for (let i = 0; i < 4; i++) await browser.keyboard.press("ArrowLeft");
  await browser.evaluate(async () => {
    await navigator.clipboard.write([new ClipboardItem({
      "text/plain": new Blob(["Guide"], { type: "text/plain" }),
      "text/html": new Blob(['<a href="https://example.com/guide">Guide</a>'], { type: "text/html" }),
    })]);
    return null;
  });
  await browser.keyboard.press("Control+V");
  await browser.keyboard.press("End");
  await browser.keyboard.type(" after");
  await app.screenshot("composer-after-linked-word-paste");
  await browser.keyboard.press("Enter");
  await screen.getByText("second fixture turn complete").waitFor();
  await app.screenshot("sent-inline-code");
  const sent = await browser.evaluate(() => {
    const body = [...document.querySelectorAll("[data-user-message-body]")].find(el => el.textContent?.includes("after"));
    return { code: body?.querySelector("code")?.textContent ?? null, text: body?.textContent ?? null };
  });
  expect(sent).toEqual({ code: "suGuidemBy", text: "suGuidemBy after" });
});
