import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const serviceWorker = await readFile(resolve(root, 'dist/sw.js'), 'utf8')
const shellMatch = serviceWorker.match(/const SHELL = (\[[^\n]+\])/)
if (!shellMatch) throw new Error('Built PWA Service Worker shell is missing.')
const shell = JSON.parse(shellMatch[1])
const assetCount = shell.filter((entry) => entry.startsWith('/assets/')).length
if (!shell.includes('/index.html') || !shell.includes('/manifest.webmanifest') || assetCount < 5 || !shell.some((entry) => entry.endsWith('.wasm'))) throw new Error(`Built PWA precache is incomplete: ${assetCount} assets.`)
console.log(JSON.stringify({ status: 'built-pwa-assets-pass', shellEntries: shell.length, assetCount, wasmPrecached: shell.some((entry) => entry.endsWith('.wasm')) }, null, 2))
