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

test("Composer prompt links share image labels without private context references", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("prompt-link-demo");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project }));
  const thread = await t3.thread({ project, title: "Prompt link demo", messages: ["first"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.fill("Why does this look wrong? ");
  await browser.evaluate(async () => {
    const canvas = new OffscreenCanvas(40, 30);
    const context = canvas.getContext("2d")!;
    context.fillStyle = "#3879dd";
    context.fillRect(0, 0, 40, 30);
    const png = await canvas.convertToBlob({ type: "image/png" });
    const clipboardData = new DataTransfer();
    clipboardData.items.add(new File([png], "screenshot.png", { type: "image/png" }));
    document.activeElement!.dispatchEvent(new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }));
    return null;
  });
  await composer.getByRole("button", "Image attachment, screenshot.png", { exact: false }).waitFor();
  await app.screenshot("source-image-chip");
  const prompts: string[] = [];
  const links: string[] = [];
  for (const command of ["Copy prompt link", "Copy prompt link with repo and model"]) {
    await browser.keyboard.press("ControlOrMeta+K");
    await screen.getByRole("combobox").fill("prompt link");
    await screen.getByRole("option", command, { exact: true }).tap();
    // Read the real clipboard by pasting into a temporary offscreen textarea.
    await browser.evaluate(() => {
      const output = document.createElement("textarea");
      output.id = "clipboard-probe";
      output.style.position = "fixed";
      output.style.left = "-10000px";
      document.body.append(output);
      output.focus();
      return null;
    });
    await browser.keyboard.press("ControlOrMeta+V");
    const copied = await browser.evaluate(() => {
      const output = document.querySelector<HTMLTextAreaElement>("#clipboard-probe")!;
      const value = output.value;
      output.remove();
      return value;
    });
    links.push(copied);
    const link = new URL(copied);
    prompts.push(link.searchParams.get("prompt") ?? "");
  }
  for (let index = 0; index < links.length; index++) {
    await browser.evaluate(async (link) => { await navigator.clipboard.writeText(link); return null; }, links[index]!);
    await composer.fill("");
    await composer.tap();
    await browser.keyboard.press("ControlOrMeta+V");
    await app.screenshot(index === 0 ? "plain-copied-link" : "repo-model-copied-link");
  }
  const recipient = new URL(links[1]!);
  await t3.open(`${recipient.pathname}${recipient.search}`);
  await screen.getByRole("textbox").waitFor();
  await screen.getByRole("textbox").getByText("Why does this look wrong?", { exact: false }).waitFor();
  await screen.getByText("Prompt added from a link", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("recipient-draft");
  expect(prompts, "Both composer copy commands replace private context Markdown with the image label").toEqual([
    "Why does this look wrong? screenshot.png",
    "Why does this look wrong? screenshot.png",
  ]);
});
