Files
2026-10-27 17:02:39 -06:00

73 lines
4.0 KiB
JavaScript

// Runs sw.js in a stand-in service worker scope and checks what it shows and calls.
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import vm from 'node:vm';
function load({ visible = false, session = { server: 'http://s', token: 'tok', device: 'Pixel' } } = {}) {
const handlers = {}, shown = [], closed = [], fetches = [], opened = [], posted = [];
const win = { url: 'http://s/app/', visibilityState: visible ? 'visible' : 'hidden', focus: async () => {}, postMessage: (m) => posted.push(m) };
const self = {
location: { origin: 'http://s' },
addEventListener: (n, f) => { handlers[n] = f; },
skipWaiting: () => {},
clients: { claim: async () => {}, matchAll: async () => (visible ? [win] : []), openWindow: async (u) => opened.push(u) },
registration: {
scope: 'http://s/app/',
showNotification: async (title, o) => shown.push({ title, ...o }),
getNotifications: async ({ tag }) => [{ tag, close: () => closed.push(tag) }],
pushManager: {},
},
};
const idb = { open: () => { const r = {}; setTimeout(() => { r.result = { createObjectStore() {}, transaction: () => ({ objectStore: () => ({ get: () => { const g = {}; setTimeout(() => { g.result = session; g.onsuccess(); }); return g; } }) }) }; r.onsuccess(); }); return r; } };
const ctx = vm.createContext({ self, indexedDB: idb, fetch: async (u, i) => { fetches.push([u, i]); return { ok: true, status: 200, json: async () => ({ ok: true }) }; }, caches: {}, URL, console, setTimeout, Uint8Array, atob });
vm.runInContext(readFileSync(new URL('../public/sw.js', import.meta.url), 'utf8'), ctx);
const run = async (name, ev) => { let p; await handlers[name]({ ...ev, waitUntil: (x) => { p = x; } }); await p; };
return { run, shown, closed, fetches, opened, posted };
}
const push = (o) => ({ data: { json: () => o } });
test('a firing alert shows a notification with Acknowledge and Snooze', async () => {
const w = load();
await w.run('push', push({ id: 'm1', topic: 'alerts', title: 'Disk full', message: 'db1 98%', priority: 5, alert: 'a1', status: 'firing', time: 1 }));
assert.equal(w.shown.length, 1);
assert.equal(w.shown[0].title, 'Disk full');
assert.equal(w.shown[0].tag, 'a1');
assert.equal(w.shown[0].requireInteraction, true);
assert.equal(JSON.stringify(w.shown[0].actions.map((a) => a.action)), '["ack","snooze"]');
});
test('a plain message has no alert buttons, and an open app shows its own toast instead', async () => {
const w = load();
await w.run('push', push({ id: 'm2', topic: 'news', title: 'Hi', message: 'x', priority: 3 }));
assert.equal(w.shown[0].actions.length, 0);
const v = load({ visible: true });
await v.run('push', push({ id: 'm3', topic: 'news', title: 'Hi', message: 'x' }));
assert.equal(v.shown.length, 0);
assert.equal(v.posted[0].type, 'push');
});
test('a resolved alert closes its earlier notification', async () => {
const w = load();
await w.run('push', push({ id: 'm4', topic: 'alerts', title: 'Resolved', message: 'ok', alert: 'a1', status: 'resolved' }));
assert.equal(w.closed.join(), 'a1');
});
test('Acknowledge on the notification calls the API without opening the app', async () => {
const w = load();
await w.run('notificationclick', { action: 'ack', notification: { data: { alert: 'a1' }, close() {} } });
assert.equal(w.fetches.length, 1);
assert.equal(w.fetches[0][0], 'http://s/api/v1/alerts/a1/ack');
assert.equal(w.fetches[0][1].headers.Authorization, 'Bearer tok');
assert.equal(w.opened.length, 0);
});
test('Snooze sends 30 minutes, and tapping the body opens the alerts view', async () => {
const w = load();
await w.run('notificationclick', { action: 'snooze', notification: { data: { alert: 'a1' }, close() {} } });
assert.equal(w.fetches[0][1].body, '{"minutes":30}');
await w.run('notificationclick', { action: '', notification: { data: { alert: 'a1' }, close() {} } });
assert.equal(w.opened[0], 'http://s/app/#alerts');
});