| 1 | // Service-worker event tests require no push subscription or external service. |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { readFile } from 'node:fs/promises'; |
| 4 | import vm from 'node:vm'; |
| 5 | const handlers = {}, shown = [], opened = [], navigated = []; |
| 6 | const scope = 'https://example.test/microchess/'; |
| 7 | let windows = [], focused = false; |
| 8 | const self = { |
| 9 | addEventListener: (name, handler) => { handlers[name] = handler; }, |
| 10 | skipWaiting: async () => {}, |
| 11 | registration: { scope, showNotification: async (title, options) => { shown.push({ title, ...options }); } }, |
| 12 | clients: { claim: async () => {}, matchAll: async () => windows, openWindow: async url => { opened.push(url); } } |
| 13 | }; |
| 14 | vm.runInNewContext(await readFile(new URL('../sw.js', import.meta.url), 'utf8'), { self, URL }); |
| 15 | async function dispatch (type, fields = {}) { |
| 16 | let finished; |
| 17 | handlers[type]({ ...fields, waitUntil: promise => { finished = promise; } }); |
| 18 | await finished; |
| 19 | } |
| 20 | await dispatch('push', { data: { json: () => ({ game: 42, body: 'Your turn against Tim.' }) } }); |
| 21 | assert.equal(shown[0].tag, 'game-42'); |
| 22 | assert.equal(shown[0].icon, scope + 'assets/app-icon-192.png'); |
| 23 | assert.equal(shown[0].data.game, 42); |
| 24 | windows = [{ url: scope, visibilityState: 'visible' }]; |
| 25 | await dispatch('push', { data: { json: () => ({ game: 99, body: 'Should stay silent.' }) } }); |
| 26 | assert.equal(shown.length, 1, 'Visible Microchess tab suppresses OS notification'); |
| 27 | windows = [{ url: scope, visibilityState: 'hidden' }]; |
| 28 | await dispatch('push', { data: { json: () => ({ game: 100, body: 'Background tab still notifies.' }) } }); |
| 29 | assert.equal(shown.length, 2, 'Hidden tab still shows a notification'); |
| 30 | assert.equal(shown[1].tag, 'game-100'); |
| 31 | windows = []; |
| 32 | let closed = false; |
| 33 | const notification = { data: { game: 42 }, close: () => { closed = true; } }; |
| 34 | await dispatch('notificationclick', { notification }); |
| 35 | assert(closed); |
| 36 | assert.equal(opened[0], scope + '?game=42', 'Notification opens main screen within deployment subdirectory'); |
| 37 | windows = [{ url: scope + '?replay=abc', navigate: async url => { navigated.push(url); return { focus: async () => { focused = true; } }; } }]; |
| 38 | await dispatch('notificationclick', { notification }); |
| 39 | assert.equal(navigated[0], scope + '?game=42'); |
| 40 | assert(focused, 'Existing window is focused'); |
| 41 | assert.equal(opened.length, 1, 'Existing app window is reused'); |
| 42 | windows = [{ url: 'https://example.test/other-app/', navigate: async () => { throw Error('Wrong scope'); } }]; |
| 43 | await dispatch('notificationclick', { notification }); |
| 44 | assert.equal(opened.length, 2, 'Another application is not navigated'); |
| 45 | await dispatch('push', { data: { json: () => { throw Error('Malformed payload'); } } }); |
| 46 | assert.equal(shown[2].body, 'A game is waiting for you.'); |
| 47 | await dispatch('notificationclick', { notification: { data: { game: 'https://evil.test/' }, close: () => {} } }); |
| 48 | assert.equal(opened.at(-1), scope, 'Notification data cannot redirect outside app'); |
| 49 | console.log('PWA: notification display, visible-tab suppression, subdirectory links, window reuse and malformed payload checks passed.'); |
projects-timfalken-com microchess