// Service-worker event tests require no push subscription or external service. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import vm from 'node:vm'; const handlers = {}, shown = [], opened = [], navigated = []; const scope = 'https://example.test/microchess/'; let windows = [], focused = false; const self = { addEventListener: (name, handler) => { handlers[name] = handler; }, skipWaiting: async () => {}, registration: { scope, showNotification: async (title, options) => { shown.push({ title, ...options }); } }, clients: { claim: async () => {}, matchAll: async () => windows, openWindow: async url => { opened.push(url); } } }; vm.runInNewContext(await readFile(new URL('../sw.js', import.meta.url), 'utf8'), { self, URL }); async function dispatch (type, fields = {}) { let finished; handlers[type]({ ...fields, waitUntil: promise => { finished = promise; } }); await finished; } await dispatch('push', { data: { json: () => ({ game: 42, body: 'Your turn against Tim.' }) } }); assert.equal(shown[0].tag, 'game-42'); assert.equal(shown[0].icon, scope + 'assets/app-icon-192.png'); assert.equal(shown[0].data.game, 42); windows = [{ url: scope, visibilityState: 'visible' }]; await dispatch('push', { data: { json: () => ({ game: 99, body: 'Should stay silent.' }) } }); assert.equal(shown.length, 1, 'Visible Microchess tab suppresses OS notification'); windows = [{ url: scope, visibilityState: 'hidden' }]; await dispatch('push', { data: { json: () => ({ game: 100, body: 'Background tab still notifies.' }) } }); assert.equal(shown.length, 2, 'Hidden tab still shows a notification'); assert.equal(shown[1].tag, 'game-100'); windows = []; let closed = false; const notification = { data: { game: 42 }, close: () => { closed = true; } }; await dispatch('notificationclick', { notification }); assert(closed); assert.equal(opened[0], scope + '?game=42', 'Notification opens main screen within deployment subdirectory'); windows = [{ url: scope + '?replay=abc', navigate: async url => { navigated.push(url); return { focus: async () => { focused = true; } }; } }]; await dispatch('notificationclick', { notification }); assert.equal(navigated[0], scope + '?game=42'); assert(focused, 'Existing window is focused'); assert.equal(opened.length, 1, 'Existing app window is reused'); windows = [{ url: 'https://example.test/other-app/', navigate: async () => { throw Error('Wrong scope'); } }]; await dispatch('notificationclick', { notification }); assert.equal(opened.length, 2, 'Another application is not navigated'); await dispatch('push', { data: { json: () => { throw Error('Malformed payload'); } } }); assert.equal(shown[2].body, 'A game is waiting for you.'); await dispatch('notificationclick', { notification: { data: { game: 'https://evil.test/' }, close: () => {} } }); assert.equal(opened.at(-1), scope, 'Notification data cannot redirect outside app'); console.log('PWA: notification display, visible-tab suppression, subdirectory links, window reuse and malformed payload checks passed.');