Remove obsolete article and scrape-job screens and APIs, preserve reports, and clear legacy API caches from the service worker. Refs #8 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
45 lines
2.1 KiB
TypeScript
45 lines
2.1 KiB
TypeScript
import { existsSync, readFileSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
import { runInNewContext } from 'node:vm'
|
|
import { expect, it, vi } from 'vitest'
|
|
|
|
const workerPath = resolve(process.cwd(), 'public/clear-legacy-api-cache.js')
|
|
const config = readFileSync(resolve(process.cwd(), 'vite.config.ts'), 'utf8')
|
|
|
|
it('exécute le vrai handler activate et supprime uniquement api-cache de CacheStorage', async () => {
|
|
expect(existsSync(workerPath), `service worker import attendu: ${workerPath}`).toBe(true)
|
|
const source = readFileSync(workerPath, 'utf8')
|
|
const cacheNames = new Set(['api-cache', 'workbox-precache', 'user-data-cache'])
|
|
const cacheStorage = {
|
|
delete: vi.fn(async (name: string) => cacheNames.delete(name)),
|
|
}
|
|
let activateHandler: ((event: { waitUntil: (promise: Promise<unknown>) => void }) => void) | undefined
|
|
const self = {
|
|
addEventListener: vi.fn((name: string, handler: typeof activateHandler) => {
|
|
if (name === 'activate') activateHandler = handler
|
|
}),
|
|
}
|
|
|
|
runInNewContext(source, { self, caches: cacheStorage })
|
|
expect(activateHandler, 'le script doit enregistrer son handler activate').toBeTypeOf('function')
|
|
const pending: Promise<unknown>[] = []
|
|
activateHandler!({ waitUntil: promise => pending.push(promise) })
|
|
expect(pending, 'la purge doit être attachée à event.waitUntil').toHaveLength(1)
|
|
await Promise.all(pending)
|
|
|
|
expect(cacheStorage.delete).toHaveBeenCalledOnce()
|
|
expect(cacheStorage.delete).toHaveBeenCalledWith('api-cache')
|
|
expect([...cacheNames].sort()).toEqual(['user-data-cache', 'workbox-precache'])
|
|
})
|
|
|
|
it('garde GenerateSW, le precache, autoUpdate et importe le nettoyage legacy', () => {
|
|
expect(config).toContain('VitePWA(')
|
|
expect(config).toContain("registerType: 'autoUpdate'")
|
|
expect(config).toContain("globPatterns: ['**/*.{js,css,html,ico,png,svg}']")
|
|
expect(config).toMatch(/importScripts:\s*\[\s*['"]clear-legacy-api-cache\.js['"]\s*\]/)
|
|
})
|
|
|
|
it('ne configure aucun runtime cache pour les réponses /api/', () => {
|
|
expect(config).not.toMatch(/urlPattern:\s*\/\^\\\/api\\\//)
|
|
})
|