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

async function epub(root: string, entry: string, href: string, chapter: string, text: string) {
  return new JSZip()
    .file("mimetype", "application/epub+zip", { compression: "STORE" })
    .file("META-INF/container.xml", `<?xml version="1.0"?><container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="${root}" media-type="application/oebps-package+xml"/></rootfiles></container>`)
    .file(entry, `<?xml version="1.0"?><package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="id"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:identifier id="id">fixture</dc:identifier><dc:title>Harbor stories</dc:title><dc:language>en</dc:language></metadata><manifest><item id="chapter" href="${href}" media-type="application/xhtml+xml"/></manifest><spine><itemref idref="chapter"/></spine></package>`)
    .file(chapter, `<?xml version="1.0"?><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Chapter 1</title></head><body><h1>Chapter 1</h1><p>${text}</p></body></html>`)
    .generateAsync({ type: "uint8array" });
}

test("EPUB package paths resolve percent-encoded spaces while preserving literal percent names", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("epubproj", {
    "plain.epub": await epub("MyBook/book.opf", "MyBook/book.opf", "chapter1.xhtml", "MyBook/chapter1.xhtml", "Control: plain path renders."),
    "encoded-rootfile.epub": await epub("My%20Book/book%201.opf", "My Book/book 1.opf", "chapter%201.xhtml", "My Book/chapter 1.xhtml", "The lighthouse keeper lit the harbor lamp."),
    "literal-percent.epub": await epub("100%20Book/book.opf", "100%20Book/book.opf", "chapter.xhtml", "100%20Book/chapter.xhtml", "Literal percent package renders."),
    "malformed-percent.epub": await epub("100%Book/book.opf", "100%Book/book.opf", "chapter.xhtml", "100%Book/chapter.xhtml", "Malformed percent package renders."),
  });
  const thread = await t3.thread({ project, title: "Read EPUB fixtures", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  for (const [name, text] of [
    ["plain.epub", "Control: plain path renders."],
    ["literal-percent.epub", "Literal percent package renders."],
    ["malformed-percent.epub", "Malformed percent package renders."],
    ["encoded-rootfile.epub", "The lighthouse keeper lit the harbor lamp."],
  ]) {
    await screen.getByRole("treeitem", name).tap();
    await browser.locator('iframe[title^="' + name + '"], [role="alert"]').waitFor({ state: "visible", timeout: 120_000 });
    await app.screenshot(name.replace(".epub", ""));
    await expect(browser.locator('iframe[title^="' + name + '"]')).toHaveCount(1);
    await expect(browser.frameLocator('iframe[title^="' + name + '"]').locator("body")).toContainText(text);
  }
});
