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

test("Copying a two-line chat message preserves one line break", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Copy two lines", messages: [] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.fill("first line");
  await browser.keyboard.press("Shift+Enter");
  await browser.keyboard.type("second line");
  await browser.keyboard.press("Enter");
  await screen.getByText("first line", { exact: false }).waitFor();
  await browser.evaluate(() => {
    const paragraph = document.querySelector("[data-user-message-body] p");
    if (!paragraph) throw new Error("No rendered two-line user message");
    const range = document.createRange();
    range.selectNodeContents(paragraph);
    const selection = window.getSelection();
    selection?.removeAllRanges();
    selection?.addRange(range);
    return null;
  });
  await browser.keyboard.press("ControlOrMeta+C");
  await composer.tap();
  await browser.keyboard.press("ControlOrMeta+V");
  await app.screenshot("pasted-two-lines");
  const pasted = await browser.evaluate(() => [...document.querySelectorAll('[role="textbox"] p')].map(p => p.textContent).join('\n'));
  expect(pasted, "Pasting the selected message keeps exactly one line break").toBe("first line\nsecond line");
});
