Skip to content
File

Blob: tests/shared/encoding.test.ts

typescript33 lines
1import { describe, expect, it } from "vitest";
2import { base64UrlDecode, base64UrlEncode } from "@/lib/encoding";
3 
4describe("base64UrlEncode", () => {
5 it("produces URL-safe alphabet with no padding", () => {
6 const bytes = new Uint8Array([0xfb, 0xff, 0x00, 0xfa]);
7 const encoded = base64UrlEncode(bytes);
8 expect(encoded).not.toMatch(/[+/=]/);
9 expect(encoded).toMatch(/^[A-Za-z0-9_-]+$/);
10 });
11 
12 it("round-trips arbitrary byte sequences", () => {
13 const bytes = crypto.getRandomValues(new Uint8Array(33));
14 const decoded = base64UrlDecode(base64UrlEncode(bytes));
15 expect(decoded).toEqual(bytes);
16 });
17 
18 it("round-trips empty input", () => {
19 const encoded = base64UrlEncode(new Uint8Array());
20 expect(encoded).toBe("");
21 expect(base64UrlDecode("")).toEqual(new Uint8Array());
22 });
23});
24 
25describe("base64UrlDecode", () => {
26 it("accepts inputs across every modulo-4 length", () => {
27 for (const len of [1, 2, 3, 4, 5]) {
28 const bytes = crypto.getRandomValues(new Uint8Array(len));
29 expect(base64UrlDecode(base64UrlEncode(bytes))).toEqual(bytes);
30 }
31 });
32});