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

test("Unsupported file hints distinguish extensions from filenames", WEB, async ({ t3, app, screen, browser }) => {
  const names = ["firmware", "firmware.", ".cache", "blob.foo"];
  const project = await t3.project("preview-demo", Object.fromEntries(names.map(name => [name, new Uint8Array([0, 1, 2, 255, 0, 128])])));
  const thread = await t3.thread({ project, title: "Unsupported file previews", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor();
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Files", { exact: true }).tap();
  const hints: string[] = [];
  for (const name of names) {
    await expect.poll(() => browser.evaluate((name) => {
      const find = () => Array.from(document.querySelectorAll("*")).flatMap(host => Array.from(host.shadowRoot?.querySelectorAll<HTMLElement>("[data-item-path]") ?? [])).find(row => row.dataset.itemPath === name);
      const row = find();
      if (!row) return false;
      row.click();
      return true;
    }, name)).toBe(true);
    await screen.getByRole("region", `Workspace panel: ${name}`, { exact: true }).waitFor();
    await screen.getByText("No preview for this file", { exact: true }).waitFor();
    await app.screenshot(name === ".cache" ? "dotfile" : name === "firmware." ? "trailing-dot" : name);
    hints.push(await browser.evaluate(() => Array.from(document.querySelectorAll("p")).map(p => p.textContent ?? "").find(text => text.startsWith("Save it to open in an app")) ?? ""));
  }
  expect(hints).toEqual([
    "Save it to open in an app that supports this format.",
    "Save it to open in an app that supports this format.",
    "Save it to open in an app that supports this format.",
    "Save it to open in an app that supports foo files.",
  ]);
});
