Files
suaveplan/packages/foundation/data/pwa/src/push-notifications/vapid.test.ts
2026-05-27 23:47:53 +01:00

37 lines
1.3 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import { urlBase64ToUint8Array } from "./vapid";
describe("urlBase64ToUint8Array", () => {
it("decodes padded and unpadded VAPID keys identically", {
timeout: 5000,
}, () => {
const unpadded =
"BFa-rtw9CKqL_BdcVqj7m0vYBzcsLkpsZBXmEdYxYsAj42SOPaCQq4qqkKZ9d5R6cy_xpDk0jUVx9b6CgPCu2wo";
const padded = `${unpadded}=`;
const a = urlBase64ToUint8Array(unpadded);
const b = urlBase64ToUint8Array(padded);
expect(a.length).toBe(65);
expect(b.length).toBe(65);
expect(Array.from(a)).toEqual(Array.from(b));
});
it("round-trips simple known bytes", { timeout: 5000 }, () => {
// "Aa" in base64 (URL-safe) is "QWE="
const out = urlBase64ToUint8Array("QWE");
expect(out.length).toBe(2);
expect(out[0]).toBe(65);
expect(out[1]).toBe(97);
});
it("handles URL-safe replacements", { timeout: 5000 }, () => {
// bytes [251, 255, 191] → standard base64 +/+/ = "+/+/" → URL-safe "-_-_"
const standard = "-_-_";
const out = urlBase64ToUint8Array(standard);
expect(out.length).toBeGreaterThan(0);
});
it("throws on invalid base64", { timeout: 5000 }, () => {
expect(() => urlBase64ToUint8Array("not%base64!")).toThrow();
});
});