import assert from "node:assert/strict";
import test from "node:test";
import { readFile } from "node:fs/promises";

test("PWA manifest has complete install metadata", async () => {
  const manifest = JSON.parse(await readFile("public/manifest.webmanifest", "utf8"));
  assert.equal(manifest.name, "UX7");
  assert.equal(manifest.short_name, "UX7");
  assert.equal(manifest.display, "standalone");
  assert.equal(manifest.scope, "/");
  assert.ok(manifest.icons.some((icon: any) => icon.sizes === "192x192"));
  assert.ok(manifest.icons.some((icon: any) => icon.sizes === "512x512" && icon.purpose === "maskable"));
});

test("service worker excludes every private route class", async () => {
  const source = await readFile("public/sw.js", "utf8");
  assert.match(source, /\/api\//);
  assert.match(source, /request\.method !== "GET"/);
  assert.match(source, /cache: "no-store"/);
  assert.doesNotMatch(source.match(/const SHELL = \[[^;]+/s)?.[0] ?? "", /api|session|messages|projects|events/);
});

test("offline fallback includes the required message", async () => {
  const source = await readFile("public/offline.html", "utf8");
  assert.match(source, /UX7 is offline\. Reconnect to continue your development session\./);
});
