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

test("Sent file cards keep three significant figures across rounding boundaries", WEB, async ({ t3, app, browser, screen }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("file-size-demo");
  const thread = await t3.thread({ project, title: "File size rounding", messages: ["Start"], model: "command-output" });
  await t3.open(thread);
  await screen.getByRole("textbox").waitFor();
  await browser.evaluate(() => {
    const input = document.querySelector<HTMLInputElement>('input[type="file"]');
    if (!input) throw new Error("No composer file input");
    const files = new DataTransfer();
    for (const size of [10235, 102350, 10480518]) {
      files.items.add(new File(["x".repeat(size)], `size-${size}.txt`, { type: "text/plain" }));
    }
    input.files = files.files;
    input.dispatchEvent(new Event("change", { bubbles: true }));
    return null;
  });
  await screen.getByRole("button", "Submit message", { exact: true }).tap();
  await screen.getByText("TXT ·", { exact: false }).first().waitFor();
  await screen.getByRole("button", "Submit message", { exact: true }).waitFor();
  await app.screenshot("sent-file-size-boundaries");
  const sizes = await browser.evaluate(() => Array.from(document.querySelectorAll("span"), el => el.textContent).filter(text => text?.startsWith("TXT ·")));
  expect(sizes).toEqual(["TXT · 10.0 KB", "TXT · 100 KB", "TXT · 10.0 MB"]);
});
