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

test("ZIP XML size limit applies to every suffix case, including encrypted entries", WEB, async ({t3, app, screen, browser}) => {
  const names = ["lower.zip", "upper.zip", "mixed.zip", "lower-enc.zip", "upper-enc.zip"];
  const files = Object.fromEntries(names.map(name => [name, readFileSync(`${process.env.ZIP_FIXTURE_DIR}/${name}`)]));
  const project = await t3.project("zip-xml-demo", files);
  const thread = await t3.thread({project, title: "ZIP XML preview limits", messages: ["Start"]});
  await browser.setViewport({width:1440,height:1080});
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("button", "Maximize panel", {exact:true}).tap();
  const results: Record<string, boolean> = {};
  for (const name of names) {
    await screen.getByRole("treeitem", name, {exact:true}).tap();
    if(name.includes("-enc")) {
      await screen.getByRole("textbox", "File password").fill("open me");
      await screen.getByRole("button", "Unlock", {exact:true}).tap();
    }
    await browser.locator('[data-right-panel-surface-content]').waitFor();
    await browser.locator('text=/The expanded archive exceeds|Archive contents/').waitFor({timeout:60000});
    results[name] = await browser.evaluate(() => document.querySelector('[data-right-panel-surface-content]')?.textContent?.includes("The expanded archive exceeds the preview size or compression ratio limit.") ?? false);
    await app.screenshot(name.replace(".zip", ""));
  }
  expect(results).toEqual(Object.fromEntries(names.map(name => [name, true])));
});
