import { expect, test } from "bun:test";
import { PWA_CLIENT, PWA_DEV_CLEANUP_CLIENT, usesMobileRuntime } from "../src/runtime.ts";

test("PWA registration is valid JavaScript and Trusted Types compatible", () => {
  expect(() => new Bun.Transpiler({ loader: "js" }).transformSync(PWA_CLIENT)).not.toThrow();
  expect(PWA_CLIENT).toContain('createPolicy("wrnexus-pwa"');
  expect(PWA_CLIENT).toContain("createScriptURL(swUrl)");
  expect(PWA_CLIENT).toContain('register(swUrl, { updateViaCache: "none" })');
  expect(PWA_CLIENT).toContain("requestIdleCallback");
  expect(PWA_CLIENT).not.toContain("location.reload()");
});

test("development PWA cleanup removes stale WRNexus service workers and caches", () => {
  expect(() =>
    new Bun.Transpiler({ loader: "js" }).transformSync(PWA_DEV_CLEANUP_CLIENT),
  ).not.toThrow();
  expect(PWA_DEV_CLEANUP_CLIENT).toContain("registration.unregister()");
  expect(PWA_DEV_CLEANUP_CLIENT).toContain("wrnexus-pwa-");
});

test("mobile runtime is shipped only for pages using mobile or native directives", () => {
  expect(usesMobileRuntime('<main class="page">Docs</main>')).toBe(false);
  expect(usesMobileRuntime('<button data-native-mobile="share">Share</button>')).toBe(true);
  expect(usesMobileRuntime("<div data-mobile-only>App</div>")).toBe(true);
  expect(usesMobileRuntime('<button data-on-wrnexus-browser-click="open">Open</button>')).toBe(
    true,
  );
});
