| new file mode 100644 |
| index 0000000..68c749d |
| --- /dev/null |
| +++ b/js/aviso-sw.test.mjs |
| @@ -0,0 +1,148 @@ |
| +import { test } from "node:test"; |
| +import assert from "node:assert/strict"; |
| +import { generateKeyPairSync } from "node:crypto"; |
| + |
| +globalThis.self = globalThis; |
| +globalThis.location = { origin: "https://app.example" }; |
| +await import("./aviso-sw.js"); |
| +const { AvisoSW } = globalThis; |
| + |
| +// A real P-256 point: a browser's subscribe() rejects anything else. |
| +const { publicKey: pk } = generateKeyPairSync("ec", { namedCurve: "prime256v1" }); |
| +const jwk = pk.export({ format: "jwk" }); |
| +const KEY_BYTES = Uint8Array.from([4, ...Buffer.from(jwk.x, "base64url"), ...Buffer.from(jwk.y, "base64url")]); |
| +const KEY = Buffer.from(KEY_BYTES).toString("base64url"); |
| + |
| +function rig() { |
| + const shown = [], opened = [], subscribed = []; |
| + globalThis.registration = { |
| + async showNotification(title, options) { shown.push({ title, options }); }, |
| + pushManager: { |
| + async subscribe(opts) { |
| + assert.equal(opts.userVisibleOnly, true); |
| + assert.deepEqual(Array.from(opts.applicationServerKey), Array.from(KEY_BYTES)); |
| + const s = { endpoint: "https://push.example/renewed", toJSON() { return { endpoint: this.endpoint, expirationTime: null, keys: { p256dh: "P", auth: "A" } }; } }; |
| + subscribed.push(s); |
| + return s; |
| + }, |
| + }, |
| + }; |
| + globalThis.clients = { |
| + windows: [], |
| + async matchAll() { return this.windows; }, |
| + async openWindow(u) { opened.push(u); }, |
| + }; |
| + return { shown, opened, subscribed }; |
| +} |
| + |
| +function pushEvent(payload) { |
| + return { data: payload === undefined ? null : { json() { return JSON.parse(payload); } } }; |
| +} |
| + |
| +test("validateURL admits only root-relative same-origin paths", () => { |
| + const o = "https://app.example"; |
| + assert.equal(AvisoSW.validateURL("/inbox?x=1", o), "https://app.example/inbox?x=1"); |
| + assert.equal(AvisoSW.validateURL("https://app.example/inbox", o), "https://app.example/inbox"); |
| + for (const bad of ["", "inbox", "//evil.example/x", "https://evil.example/x", "/a\\b", "javascript:alert(1)", "https://app.example.evil/x", 42, null]) { |
| + assert.equal(AvisoSW.validateURL(bad, o), null, String(bad)); |
| + } |
| +}); |
| + |
| +test("handlePush shows the default payload and validates url", async () => { |
| + const r = rig(); |
| + await AvisoSW.handlePush(pushEvent('{"title":"Hi","body":"b","url":"/inbox","tag":"t"}'), { fallback: () => ({ title: "fb" }) }); |
| + assert.equal(r.shown.length, 1); |
| + assert.equal(r.shown[0].title, "Hi"); |
| + assert.equal(r.shown[0].options.body, "b"); |
| + assert.equal(r.shown[0].options.tag, "t"); |
| + assert.equal(r.shown[0].options.data.url, "https://app.example/inbox"); |
| +}); |
| + |
| +test("handlePush drops an off-origin url but still shows", async () => { |
| + const r = rig(); |
| + await AvisoSW.handlePush(pushEvent('{"title":"Hi","url":"https://evil.example/"}'), { fallback: () => ({ title: "fb" }) }); |
| + assert.equal(r.shown[0].title, "Hi"); |
| + assert.equal("url" in r.shown[0].options.data, false); |
| +}); |
| + |
| +test("handlePush falls back on malformed or missing payload", async () => { |
| + const r = rig(); |
| + await AvisoSW.handlePush(pushEvent('{"nope":1}'), { fallback: () => ({ title: "fb", options: { data: { url: "/" } } }) }); |
| + await AvisoSW.handlePush(pushEvent(undefined), { fallback: () => ({ title: "fb2" }) }); |
| + await AvisoSW.handlePush({ data: { json() { throw new Error("not json"); } } }, { fallback: () => ({ title: "fb3" }) }); |
| + assert.deepEqual(r.shown.map((s) => s.title), ["fb", "fb2", "fb3"]); |
| + assert.equal(r.shown[0].options.data.url, "https://app.example/"); |
| +}); |
| + |
| +test("handlePush requires a fallback", async () => { |
| + rig(); |
| + await assert.rejects(AvisoSW.handlePush(pushEvent('{"x":1}'), {}), /fallback is required/); |
| +}); |
| + |
| +test("handlePush uses a custom decoder and validates its url; a throwing decoder falls back", async () => { |
| + const r = rig(); |
| + await AvisoSW.handlePush(pushEvent('{"blob":"..."}'), { |
| + decode: async () => ({ title: "Custom", options: { data: { url: "//evil.example/" } } }), |
| + fallback: () => ({ title: "fb" }), |
| + }); |
| + assert.equal(r.shown[0].title, "Custom"); |
| + assert.equal("url" in r.shown[0].options.data, false); |
| + await AvisoSW.handlePush(pushEvent('{"blob":"..."}'), { |
| + decode: async () => { throw new Error("cannot decrypt"); }, |
| + fallback: () => ({ title: "fb" }), |
| + }); |
| + assert.equal(r.shown[1].title, "fb"); |
| +}); |
| + |
| +test("handleClick focuses a matching window, else opens, else fallback, never off-origin", async () => { |
| + const r = rig(); |
| + let focusedURL = null; |
| + globalThis.clients.windows = [{ url: "https://app.example/inbox", async focus() { focusedURL = this.url; } }]; |
| + let closed = 0; |
| + const ev = (url) => ({ notification: { close() { closed++; }, data: url === undefined ? {} : { url } } }); |
| + await AvisoSW.handleClick(ev("https://app.example/inbox"), {}); |
| + assert.equal(focusedURL, "https://app.example/inbox"); |
| + await AvisoSW.handleClick(ev("/other"), {}); |
| + assert.deepEqual(r.opened, ["https://app.example/other"]); |
| + await AvisoSW.handleClick(ev(undefined), { fallbackURL: "/" }); |
| + assert.deepEqual(r.opened, ["https://app.example/other", "https://app.example/"]); |
| + await AvisoSW.handleClick(ev("https://evil.example/"), {}); |
| + await AvisoSW.handleClick(ev("https://evil.example/"), { fallbackURL: "//evil.example/" }); |
| + assert.equal(r.opened.length, 2); |
| + assert.equal(closed, 5); |
| +}); |
| + |
| +test("handleSubscriptionChange saves a new subscription with a fetched key", async () => { |
| + rig(); |
| + const saved = []; |
| + const sub = { endpoint: "https://push.example/new", toJSON() { return { endpoint: this.endpoint, expirationTime: 1, keys: { p256dh: "P", auth: "A" } }; } }; |
| + const ok = await AvisoSW.handleSubscriptionChange( |
| + { newSubscription: sub, oldSubscription: { endpoint: "https://push.example/old" } }, |
| + { publicKey: async () => KEY, save: async (b) => { saved.push(b); return { ok: true }; } }, |
| + ); |
| + assert.equal(ok, true); |
| + assert.deepEqual(saved[0], { |
| + subscription: { endpoint: "https://push.example/new", keys: { p256dh: "P", auth: "A" } }, |
| + publicKey: KEY, |
| + previousEndpoint: "https://push.example/old", |
| + }); |
| +}); |
| + |
| +test("handleSubscriptionChange renews itself when the event carries no subscription", async () => { |
| + const r = rig(); |
| + const saved = []; |
| + const ok = await AvisoSW.handleSubscriptionChange({}, { publicKey: async () => KEY, save: async (b) => { saved.push(b); } }); |
| + assert.equal(ok, true); |
| + assert.equal(r.subscribed.length, 1); |
| + assert.equal(saved[0].subscription.endpoint, "https://push.example/renewed"); |
| + assert.equal("previousEndpoint" in saved[0], false); |
| +}); |
| + |
| +test("handleSubscriptionChange fails silently on an expired session, a failed key fetch, or a throw", async () => { |
| + rig(); |
| + const sub = { endpoint: "e", toJSON() { return { endpoint: "e", keys: {} }; } }; |
| + assert.equal(await AvisoSW.handleSubscriptionChange({ newSubscription: sub }, { publicKey: async () => KEY, save: async () => ({ ok: false, status: 401 }) }), false); |
| + assert.equal(await AvisoSW.handleSubscriptionChange({ newSubscription: sub }, { publicKey: async () => { throw new Error("offline"); }, save: async () => ({ ok: true }) }), false); |
| + assert.equal(await AvisoSW.handleSubscriptionChange({ newSubscription: sub }, { publicKey: async () => KEY, save: async () => { throw new Error("net"); } }), false); |
| + await assert.rejects(AvisoSW.handleSubscriptionChange({ newSubscription: sub }, {}), /publicKey\(\) and save\(\) are required/); |
| +}); |