import { readFileSync } from "node:fs"; import path from "node:path"; import { describe, expect, it } from "vitest"; const viteConfig = readFileSync(path.resolve(__dirname, "../../../../client/vite.config.ts"), "utf8"); function runtimeCacheRule(cacheName) { const marker = `cacheName: "${cacheName}"`; const at = viteConfig.indexOf(marker); expect(at, `${cacheName} rule must exist`).toBeGreaterThan(-1); const start = viteConfig.lastIndexOf("urlPattern:", at); const end = viteConfig.indexOf("urlPattern:", at); return viteConfig.slice(start, end === -1 ? undefined : end); } describe("client PWA API runtime cache", () => { it("falls back to cached API data only when the network fails, never on a slow response", () => { const rule = runtimeCacheRule("app-api-runtime"); expect(rule).toContain('handler: "NetworkFirst"'); // A timeout made the service worker answer a post-approval refetch with the // cached pre-approval task after 3s, so the page kept showing "待派发". expect(rule).not.toMatch(/networkTimeoutSeconds/); }); });