import { expect } from "e2e";
import JSZip from "jszip";
import { test, WEB } from "./t3.ts";

test("ODT lists continue across a plain paragraph and separate lists restart", WEB, async ({ t3, app, screen, browser }) => {
  const variants = [
    { file: "continued.odt", attrs: 'text:continue-numbering="true"', label: "continue-numbering", start: 3 },
    { file: "named.odt", attrs: 'text:continue-list="list1"', label: "continue-list", start: 3 },
    { file: "separate.odt", attrs: "", label: "separate-list", start: 1 },
  ];
  const files: Record<string, Uint8Array> = {};
  for (const v of variants) {
    files[v.file] = 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:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.3">
        <office:automatic-styles><text:list-style style:name="Numbering_20_123"><text:list-level-style-number text:level="1" style:num-format="1" style:num-suffix="."/></text:list-style></office:automatic-styles>
        <office:body><office:text>
          <text:list xml:id="list1" text:style-name="Numbering_20_123"><text:list-item><text:p>Review</text:p></text:list-item><text:list-item><text:p>Send</text:p></text:list-item></text:list>
          <text:p>Wait for a reply.</text:p>
          <text:list ${v.attrs} text:style-name="Numbering_20_123"><text:list-item><text:p>Merge</text:p></text:list-item><text:list-item><text:p>Deploy</text:p></text:list-item></text:list>
        </office:text></office:body></office:document-content>`)
      .generateAsync({ type: "uint8array" });
  }
  const project = await t3.project("ODT review", files);
  const thread = await t3.thread({ project, title: "Review continued lists", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor({ state: "visible" });
  const actual: number[] = [];
  for (const v of variants) {
    await browser.keyboard.press("Control+p");
    await screen.getByPlaceholder("Search files…").fill(v.file);
    await screen.getByRole("option", v.file, { exact: false }).tap();
    const preview = browser.frameLocator(`iframe[title^="${v.file}"]`);
    await preview.getByText("Deploy", { exact: true }).waitFor({ state: "visible", timeout: 120_000 });
    await app.screenshot(v.label);
    actual.push(Number((await preview.locator("ol").nth(1).getAttribute("start")) ?? 1));
  }
  expect(actual).toEqual(variants.map(v => v.start));
});
