import { describe, expect, it } from "vitest";
import { base64UrlDecode, base64UrlEncode, parseShareRequestFromLocation } from "./secureShare";

describe("sikker deling", () => {
  it("leser export id fra /s?uuid og nøkkel fra fragment", () => {
    const parsed = parseShareRequestFromLocation({
      search: "?123e4567-e89b-12d3-a456-426614174000",
      hash: "#key=abc_def-123&confirm=confirm_456"
    } as Location);

    expect(parsed.exportId).toBe("123e4567-e89b-12d3-a456-426614174000");
    expect(parsed.key).toBe("abc_def-123");
    expect(parsed.confirmToken).toBe("confirm_456");
  });

  it("base64url roundtripper bytes uten pluss, slash eller padding", () => {
    const bytes = new Uint8Array([0, 1, 2, 250, 251, 252, 253, 254, 255]);
    const encoded = base64UrlEncode(bytes);

    expect(encoded).not.toMatch(/[+/=]/);
    expect([...base64UrlDecode(encoded)]).toEqual([...bytes]);
  });
});
