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

test("Copy as CSV preserves merged-cell column positions", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("merged-table", {
    "spans.md": '# Spans\n\n<table><thead><tr><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr><td colspan="2">wide</td><td>c1</td></tr><tr><td rowspan="2">tall</td><td>b2</td><td>c2</td></tr><tr><td>b3</td><td>c3</td></tr></tbody></table>\n',
  });
  const thread = await t3.thread({ project, title: "Merged table CSV", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "spans.md").tap();
  await screen.getByRole("button", "Show rendered markdown").tap();
  await screen.getByRole("button", "Copy table").tap();
  await screen.getByRole("menuitem", "Copy as CSV").tap();
  await screen.getByRole("button", "Copied").waitFor();
  await app.screenshot("merged-table-copied");
  const csv = await browser.evaluate(() => navigator.clipboard.readText());
  await browser.keyboard.press("Escape");
  const composer = screen.getByRole("textbox");
  await composer.tap();
  await browser.keyboard.press("ControlOrMeta+v");
  await composer.getByText("c3", { exact: false }).waitFor();
  await app.screenshot("copied-csv-pasted");
  expect(csv).toBe("A,B,C\nwide,,c1\ntall,b2,c2\n,b3,c3");
});
