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

test("ODT preview omits author-hidden sections", WEB, async ({ t3, app, screen, browser }) => {
  const odt = await new JSZip()
    .file("mimetype", "application/vnd.oasis.opendocument.text", { compression: "STORE" })
    .file("content.xml", `<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.3"><office:body><office:text><text:p>Visible intro.</text:p><text:section text:name="Section1" text:display="none"><text:p>Internal margin 42%.</text:p></text:section><text:p>Visible end.</text:p></office:text></office:body></office:document-content>`)
    .generateAsync({ type: "uint8array" });
  const project = await t3.project("demo", { "hidden.odt": odt });
  const thread = await t3.thread({ project, title: "Review document", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor({ state: "visible" });
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Files", { exact: true }).tap();
  const openFile = () => browser.evaluate(() => {
    for (const host of document.querySelectorAll("*")) {
      const row = host.shadowRoot?.querySelector<HTMLElement>('[data-item-path="hidden.odt"]');
      if (row) { row.click(); return true; }
    }
    return false;
  });
  // Wait for the file tree before opening the same file on each commit.
  await expect.poll(openFile).toBe(true);
  const preview = browser.frameLocator('iframe[title^="hidden.odt"]');
  await preview.getByText("Visible end.", { exact: true }).waitFor({ state: "visible", timeout: 120_000 });
  await app.screenshot("hidden-section-preview");
  await expect(preview.locator("body")).not.toContainText("Internal margin 42%.");
  await expect(preview.getByText("Visible intro.", { exact: true })).toBeVisible();
  await expect(preview.getByText("Visible end.", { exact: true })).toBeVisible();
});
