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

// Run with web engine timezoneId America/Nuuk and locale en-US in e2e.config.ts.
// Python zipfile fixture: DOS timestamps 2026-03-28 23:30 and 2026-03-28 12:00.
const ZIP = "UEsDBBQAAAAAAMC7fFxbn4MUHwAAAB8AAAAQAAAAZHN0LW1pZG5pZ2h0LnR4dEFyY2hpdmVkIGRhdGU6IE1hcmNoIDI4LCAyMDI2LgpQSwMEFAAAAAAAAGB8XFufgxQfAAAAHwAAAAgAAABub29uLnR4dEFyY2hpdmVkIGRhdGU6IE1hcmNoIDI4LCAyMDI2LgpQSwECFAMUAAAAAADAu3xcW5+DFB8AAAAfAAAAEAAAAAAAAAAAAAAAgAEAAAAAZHN0LW1pZG5pZ2h0LnR4dFBLAQIUAxQAAAAAAABgfFxbn4MUHwAAAB8AAAAIAAAAAAAAAAAAAACAAU0AAABub29uLnR4dFBLBQYAAAAAAgACAHQAAACSAAAAAAA=";

test("ZIP preview keeps the archived date across Nuuk's midnight DST gap", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("proj");
  const thread = await t3.thread({ project, title: "ZIP archived dates", messages: ["Inspect the archive dates."] });
  await t3.open(thread);
  await screen.getByRole("button", "Attach files", { exact: true }).waitFor();
  // Populate the real attachment input, equivalent to selecting dst.zip in the file picker.
  await browser.evaluate((payload) => {
    const input = document.querySelector('form[data-chat-composer-form] input[type="file"]') as HTMLInputElement;
    if (!input) throw new Error("Attachment input not found");
    const data = new DataTransfer();
    data.items.add(new File([Uint8Array.from(atob(payload), c => c.charCodeAt(0))], "dst.zip", { type: "application/zip" }));
    input.files = data.files;
    input.dispatchEvent(new Event("change", { bubbles: true }));
  }, ZIP);
  await screen.getByRole("button", "File attachment, dst.zip", { exact: false }).click();
  await screen.getByText("Archive contents", { exact: true }).waitFor();
  await screen.getByText("dst-midnight.txt", { exact: true }).waitFor();
  await app.screenshot("zip-archived-date-nuuk");
  const modified = await browser.evaluate(() => {
    const row = [...document.querySelectorAll("tbody tr")].find(row => row.textContent?.includes("dst-midnight.txt"));
    return row?.querySelector("td:last-child")?.textContent?.trim() ?? null;
  });
  await expect(modified).toBe("3/28/2026");
});
