import { expect, test } from "bun:test";
import { base64UrlToBytes, bytesToBase64Url, randomDigits, randomToken } from "../src/crypto.ts";

test("base64url helpers round-trip canonical values and reject malformed input", () => {
  const bytes = new Uint8Array([0, 1, 2, 253, 254, 255]);
  const encoded = bytesToBase64Url(bytes);
  expect(base64UrlToBytes(encoded)).toEqual(bytes);
  expect(() => base64UrlToBytes("a===")).toThrow("Invalid base64url value");
  expect(() => base64UrlToBytes("a")).toThrow("Invalid base64url value");
});

test("random helpers reject broken random providers instead of looping forever", () => {
  expect(() => randomToken(() => new Uint8Array(1), 32)).toThrow("exactly 32 bytes");
  expect(() => randomDigits((length) => new Uint8Array(length).fill(255), 6)).toThrow(
    "WRN-AUTH-RANDOM-SOURCE-REJECTED",
  );
});
