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

test("Copy code omits the fence closing newline", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("copy-demo");
  const thread = await t3.thread({ project, title: "Code copy demonstration", model: "showcase", messages: ["go"] });
  await t3.open(thread);
  await browser.evaluate(() => {
    const original = navigator.clipboard.writeText.bind(navigator.clipboard);
    navigator.clipboard.writeText = async (text) => {
      await original(text);
      document.body.dataset.copiedCode = text;
      const panel = document.createElement("section");
      panel.style.cssText = "position:fixed;bottom:24px;left:330px;right:24px;z-index:99999;padding:24px;background:#101827;color:#fff;border:2px solid #94a3b8;border-radius:12px;font:18px monospace;white-space:pre-wrap";
      panel.textContent = `COPY BUTTON — clipboard writeText argument\n\n${JSON.stringify(text)}\n\nTrailing newline: ${text.endsWith("\n") ? "YES — extra empty line when pasted" : "NO — no extra empty line"}\nBuffer lines: ${text.split("\n").length}`;
      document.body.append(panel);
    };
    return null;
  });
  await screen.getByRole("button", "Copy code", { exact: true }).tap();
  await app.screenshot("copied-code");
  const copied = await browser.evaluate(() => document.body.dataset.copiedCode ?? "");
  expect(copied).toBe("clamp(12, 0, 10); // 10\nclamp(-3, 0, 10); // 0");
});
