/// <reference lib="dom" />
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "e2e";
import { APP_DIR } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Copying a table as CSV separates text across a rendered line break", WEB, async ({ t3, app, browser, screen }) => {
  const fixture = join(APP_DIR, "apps/server/scripts/dev-replay/showcase_transcript.ndjson");
  const original = await readFile(fixture, "utf8");
  await writeFile(fixture, original.replace("| Test | `execute` | 12 passed |", "| Test | `execute` | 12 passed<br>0 failed |"));
  let thread;
  try {
    const project = await t3.project("csv-demo");
    thread = await t3.thread({ project, title: "CSV line break", model: "showcase", messages: ["go"] });
  } finally {
    await writeFile(fixture, original);
  }
  await t3.open(thread);
  await screen.getByRole("button", "Copy table", { exact: true }).waitFor();
  await app.screenshot("rendered-table");
  await screen.getByRole("button", "Copy table", { exact: true }).tap();
  await screen.getByRole("menuitem", "Copy as CSV", { exact: true }).tap();
  const composer = screen.getByRole("textbox");
  await composer.tap();
  await browser.keyboard.press("ControlOrMeta+V");
  await app.screenshot("pasted-csv");
  await expect(composer).toContainText("Test,execute,12 passed 0 failed");
});
