BlueGit

projects-timfalken-com microchess

Files

microchess / web / tests / pwa.mjs

pwa.mjs

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