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

test("EPUB packaged SVG references preserve their selected view", WEB, async ({ t3, app, screen, browser }) => {
  const views = ["0,0,100,100", "100,0,100,100", "50,0,100,100", "100 0 100 100", "0,0,50,50"];
  const ref = (i: number) => `panels.svg#svgView(viewBox(${views[i]}))`;
  const epub = await new JSZip()
    .file("mimetype", "application/epub+zip", { compression: "STORE" })
    .file("META-INF/container.xml", '<container><rootfiles><rootfile full-path="book.opf"/></rootfiles></container>')
    .file("book.opf", '<package><metadata><title>SVG views</title></metadata><manifest><item id="chapter" href="chapter.xhtml" media-type="application/xhtml+xml"/><item id="svg" href="panels.svg" media-type="image/svg+xml"/></manifest><spine><itemref idref="chapter"/></spine></package>')
    .file("panels.svg", '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 100"><path fill="red" d="M0 0h100v100H0z"/><path fill="blue" d="M100 0h100v100H100z"/></svg>')
    .file("chapter.xhtml", `<html xmlns="http://www.w3.org/1999/xhtml"><head><style>body{font-family:sans-serif;margin:8px}h1{font-size:20px}h3{font-size:12px;margin:2px 0}section{display:inline-block;vertical-align:top;margin:6px;width:100px}img,object,video{width:80px;height:80px}p.crop{width:80px;height:80px;background:url("${ref(3)}") center/contain no-repeat}</style></head><body><h1>Authored SVG views</h1><section><h3>Red only</h3><img src="${ref(0)}" alt="red"/></section><section><h3>Blue only</h3><img src="${ref(1)}" alt="blue"/></section><section><h3>Middle crop</h3><object type="image/svg+xml" data="${ref(2)}"></object></section><section><h3>Blue background</h3><p class="crop" style='background-image:url("${ref(3)}")'></p></section><section><h3>Red quarter poster</h3><video poster="${ref(4)}"></video></section></body></html>`)
    .generateAsync({ type: "uint8array" });
  const project = await t3.project("SVG views", { "panels.epub": epub });
  const seed = { project, title: "Inspect EPUB", messages: ["Start"] };
  let thread;
  try {
    thread = await t3.thread({ ...seed, model: "default" });
  } catch (error) {
    if (!String(error).includes("Model default is not advertised")) throw error;
    thread = await t3.thread({ ...seed, model: "multi-turn" });
  }
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").waitFor();
  await browser.keyboard.press("Control+p");
  await screen.getByPlaceholder("Search files…").fill("panels.epub");
  await screen.getByRole("option", "panels.epub", { exact: false }).tap();
  const frame = browser.frameLocator('iframe[title^="panels.epub"]');
  await frame.getByText("Authored SVG views").waitFor({ timeout: 120_000 });
  await app.screenshot("svg-selected-views");
  const urls = [
    await frame.locator("img").nth(0).getAttribute("src"),
    await frame.locator("img").nth(1).getAttribute("src"),
    await frame.locator("img").nth(2).getAttribute("src"),
    await frame.locator("p.crop").getAttribute("style"),
    await frame.locator("video").getAttribute("poster"),
  ];
  for (let i = 0; i < views.length; i++) expect(decodeURIComponent(urls[i] ?? "")).toContain(`#svgView(viewBox(${views[i]}))`);
});
