import { expect, test } from "vitest";
import { assetReloadStorageKey, isRecoverableAssetLoadError } from "./ErrorBoundary.jsx";

test("detects Vite CSS preload failures as recoverable asset errors", () => {
  expect(isRecoverableAssetLoadError(new Error("Unable to preload CSS for /assets/HostOrgTable-DWK6sgCN.css"))).toBe(
    true,
  );
  expect(isRecoverableAssetLoadError(new Error("Failed to fetch dynamically imported module"))).toBe(true);
});

test("keeps ordinary runtime errors visible", () => {
  expect(isRecoverableAssetLoadError(new Error("Cannot read properties of undefined"))).toBe(false);
});

test("scopes asset reload attempts by route and asset", () => {
  const key = assetReloadStorageKey(new Error("Unable to preload CSS for /assets/HostOrgTable-DWK6sgCN.css"), {
    pathname: "/host/managers",
  });

  expect(key).toContain("/host/managers:/assets/HostOrgTable-DWK6sgCN.css");
});
