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

const source = Array.from({ length: 250 }, (_, i) => `export const setting${String(i + 1).padStart(3, "0")} = "value${i + 1}";`).join("\n") + "\n";

test("Select All in a read-only source preview copies the entire file", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo", { "../source.ts": source });
  await symlink("../source.ts", `${project}/shared-config.ts`);
  const thread = await t3.thread({ project, title: "Review configuration", messages: ["Read [shared-config.ts](shared-config.ts)"] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open(thread);
  await screen.getByText("first fixture turn complete", { exact: true }).waitFor();
  await screen.getByRole("button", /^Agent Read/).tap();
  await screen.getByRole("link", "shared-config.ts", { exact: true }).tap();
  await browser.locator('diffs-container [data-column-number="4"]').waitFor();
  await browser.locator('diffs-container [data-column-number="4"]').tap();
  await browser.keyboard.press("Control+a");
  await app.screenshot("gutter-select-all");
  await browser.keyboard.press("Control+c");
  const composer = screen.getByRole("textbox");
  await composer.tap();
  await browser.evaluate(() => {
    document.addEventListener("paste", (event) => {
      document.documentElement.dataset.proofPaste = event.clipboardData?.getData("text/plain") ?? "";
    }, { capture: true, once: true });
    return null;
  });
  await browser.keyboard.press("Control+v");
  const pasted = await browser.evaluate(() => document.documentElement.dataset.proofPaste ?? "");
  await app.screenshot("copied-file-pasted-in-composer");
  expect(pasted).toBe(source);
});
