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

test("Empty archive files and folders display zero bytes", WEB, async ({ t3, app, screen, browser }) => {
  const zip = new JSZip();
  zip.file("empty.txt", "", { date: new Date("2026-01-01T00:00:00Z") });
  zip.folder("empty-dir");
  zip.file("pkg/__init__.py", "", { createFolders: false, date: new Date("2026-01-01T00:00:00Z") });
  zip.file("readme.txt", "hello", { date: new Date("2026-01-01T00:00:00Z") });
  zip.forEach((_, file) => { file.date = new Date("2026-01-01T00:00:00Z"); });
  const project = await t3.project("archive-demo", { "sizes.zip": await zip.generateAsync({ type: "uint8array" }) });
  const thread = await t3.thread({ project, title: "Review archive sizes", messages: ["Start"] });
  await browser.setViewport({ width: 1440, height: 900 });
  await t3.open(thread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor();
  await browser.keyboard.press("Control+p");
  await screen.getByPlaceholder("Search files…").fill("sizes.zip");
  await screen.getByRole("option", "sizes.zip", { exact: false }).tap();
  await screen.getByRole("table", "Archive entries").waitFor({ timeout: 120_000 });
  await screen.getByRole("button", "Maximize panel", { exact: true }).tap();
  const readRows = () => browser.evaluate(() => Array.from(document.querySelectorAll('table[aria-label="Archive entries"] tbody tr')).map(row => Array.from(row.querySelectorAll("td")).slice(0, 2).map(cell => cell.textContent?.trim())));
  const rootRows = await readRows();
  await app.screenshot("archive-root-sizes");
  await screen.getByRole("button", "pkg", { exact: true }).tap();
  await screen.getByRole("button", "__init__.py", { exact: true }).waitFor();
  const nestedRows = await readRows();
  await app.screenshot("archive-pkg-sizes");
  expect({ rootRows, nestedRows }).toEqual({
    rootRows: [["empty-dir", "0 bytes"], ["pkg", "0 bytes"], ["empty.txt", "0 bytes"], ["readme.txt", "1 KB"]],
    nestedRows: [["__init__.py", "0 bytes"]],
  });
});
