// tests/mobile-admin/pwa-install.spec.ts
import { test, expect } from "@playwright/test";

test.use({ viewport: { width: 390, height: 844 } }); // iPhone 13

test("PWA manifest is valid and all icons resolve", async ({ page, request }) => {
  await page.goto("/admin/login");
  const manifestLink = await page.locator('link[rel="manifest"]').getAttribute("href");
  expect(manifestLink).toBe("/manifest.json");

  const manifestRes = await request.get("/manifest.json");
  expect(manifestRes.status()).toBe(200);
  const manifest = await manifestRes.json();
  expect(manifest.name).toBe("Route Commerce");
  expect(manifest.theme_color).toBe("#166534");

  for (const icon of manifest.icons) {
    const r = await request.get(icon.src);
    expect(r.status(), `icon ${icon.src} should resolve`).toBe(200);
  }
});

test("apple-touch-icon is present in head", async ({ page }) => {
  await page.goto("/admin/login");
  const appleTouch = await page.locator('link[rel="apple-touch-icon"]').count();
  expect(appleTouch).toBeGreaterThan(0);
});

test("service worker registers successfully", async ({ page }) => {
  await page.goto("/admin/login");
  const swReady = await page.evaluate(async () => {
    if (!("serviceWorker" in navigator)) return false;
    const reg = await navigator.serviceWorker.ready;
    return !!reg.active;
  });
  expect(swReady).toBe(true);
});
