kaikai_test/test/mobile-pwa-offline.test.js
2026-05-14 18:53:53 +08:00

51 lines
2.0 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
const html = await readFile(new URL("../public/mobile.html", import.meta.url), "utf8");
const js = await readFile(new URL("../public/mobile.js", import.meta.url), "utf8");
const css = await readFile(new URL("../public/mobile.css", import.meta.url), "utf8");
const manifest = await readFile(new URL("../public/manifest.webmanifest", import.meta.url), "utf8");
const sw = await readFile(new URL("../public/mobile-sw.js", import.meta.url), "utf8");
test("mobile page exposes offline availability status", () => {
assert.match(html, /id="offline-status"/);
assert.match(html, /id="install-hint"/);
assert.match(html, /id="install-status"/);
assert.match(html, /id="install-app-button"/);
assert.match(css, /\.offline-status/);
assert.match(css, /\.install-hint/);
});
test("mobile app registers its service worker", () => {
assert.match(js, /serviceWorker/);
assert.match(js, /mobile-sw\.js/);
assert.match(js, /updateOfflineStatus/);
});
test("mobile app can prompt installation when the browser supports PWA install", () => {
assert.match(js, /beforeinstallprompt/);
assert.match(js, /deferredInstallPrompt/);
assert.match(js, /installMobileApp/);
assert.match(js, /updateInstallPrompt/);
assert.match(js, /appinstalled/);
assert.match(js, /display-mode: standalone/);
assert.match(css, /\.install-hint\.install-ready/);
});
test("mobile service worker caches app shell for offline use", () => {
assert.match(sw, /CACHE_NAME/);
assert.match(sw, /\/mobile\.html/);
assert.match(sw, /\/mobile\.css/);
assert.match(sw, /\/mobile\.js/);
assert.match(sw, /\/manifest\.webmanifest/);
assert.match(sw, /caches\.open/);
assert.match(sw, /fetch/);
});
test("manifest starts at the mobile page in standalone display", () => {
const parsed = JSON.parse(manifest);
assert.equal(parsed.start_url, "/mobile.html");
assert.equal(parsed.display, "standalone");
});