const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const source = fs.readFileSync(path.join(__dirname, '..', 'service-worker.js'), 'utf8'); function harness({ network, cached }) { const handlers = {}; const writes = []; const context = { URL, location: { origin: 'https://localhost' }, self: { addEventListener: (type, handler) => { handlers[type] = handler; } }, caches: { match: async request => cached.get(typeof request === 'string' ? request : request.url), open: async () => ({ put: async (request, response) => writes.push([request.url, response]) }) }, fetch: network }; vm.runInNewContext(source, context); return { writes, async request(request) { let response; const pending = []; handlers.fetch({ request, respondWith(value) { response = value; }, waitUntil(value) { pending.push(value); } }); if (!response) return { intercepted: false }; const result = await response; await Promise.all(pending); return result; } }; } test('new Android app assets replace old cached scripts', async () => { const request = { method: 'GET', url: 'https://localhost/mail-ui.js', mode: 'same-origin' }; const fresh = { ok: true, clone() { return this; } }; const old = { old: true }; const ui = harness({ network: async () => fresh, cached: new Map([[request.url, old]]) }); assert.equal(await ui.request(request), fresh); assert.deepEqual(ui.writes, [[request.url, fresh]]); assert.notEqual(source.match(/const CACHE\s*=\s*['"]([^'"]+)/)[1], 'ep-mail-v19'); }); test('offline requests use cached assets and missing assets fail explicitly', async () => { const request = { method: 'GET', url: 'https://localhost/mail-ui.js', mode: 'same-origin' }; const old = { old: true }; const offline = async () => { throw new Error('offline'); }; const ui = harness({ network: offline, cached: new Map([[request.url, old]]) }); assert.equal(await ui.request(request), old); const missing = harness({ network: offline, cached: new Map() }); await assert.rejects(missing.request(request), /offline/); }); test('network failures for navigation use the cached app shell', async () => { const request = { method: 'GET', url: 'https://localhost/', mode: 'navigate' }; const shell = { shell: true }; const ui = harness({ network: async () => { throw new Error('offline'); }, cached: new Map([['./index.html', shell]]) }); assert.equal(await ui.request(request), shell); }); test('HTTP error responses are not stored or replaced with cached content', async () => { const request = { method: 'GET', url: 'https://localhost/mail-ui.js', mode: 'same-origin' }; const failed = { ok: false, status: 404 }; const ui = harness({ network: async () => failed, cached: new Map([[request.url, { old: true }]]) }); assert.equal(await ui.request(request), failed); assert.deepEqual(ui.writes, []); }); test('cross-origin and non-GET requests are not intercepted', async () => { const ui = harness({ network: async () => { throw new Error('unexpected fetch'); }, cached: new Map() }); assert.deepEqual(await ui.request({ method: 'GET', url: 'https://other.example/image.png' }), { intercepted: false }); assert.deepEqual(await ui.request({ method: 'POST', url: 'https://localhost/mail-ui.js' }), { intercepted: false }); });