import { describe, expect, it } from "vitest"; import { deriveSignupVerificationToken } from "./signupVerificationToken.server"; const CREATION_REQUEST_KEY = "d7077797-054b-497a-b1ce-8e4f36257a96"; const OTHER_CREATION_REQUEST_KEY = "b79f4743-b5ee-48df-9287-10f24b2aedbf"; describe("deriveSignupVerificationToken", () => { it("derives a stable opaque capability from normalized email, key and server secret", () => { const token = deriveSignupVerificationToken(" Ada@Example.com ", CREATION_REQUEST_KEY, "secret-a"); expect(token).toMatch(/^[a-f0-9]{40}$/u); expect(deriveSignupVerificationToken("ada@example.com", CREATION_REQUEST_KEY, "secret-a")).toBe(token); expect(token).not.toContain("ada@example.com"); expect(token).not.toContain(CREATION_REQUEST_KEY); }); it("changes when any bound input changes", () => { const token = deriveSignupVerificationToken("ada@example.com", CREATION_REQUEST_KEY, "secret-a"); expect(deriveSignupVerificationToken("grace@example.com", CREATION_REQUEST_KEY, "secret-a")).not.toBe(token); expect(deriveSignupVerificationToken("ada@example.com", OTHER_CREATION_REQUEST_KEY, "secret-a")).not.toBe(token); expect(deriveSignupVerificationToken("ada@example.com", CREATION_REQUEST_KEY, "secret-b")).not.toBe(token); }); it("fails closed without a secret or a valid UUIDv4 request key", () => { expect(() => deriveSignupVerificationToken("ada@example.com", CREATION_REQUEST_KEY, "")).toThrow("Signup verification token configuration is invalid"); expect(() => deriveSignupVerificationToken("ada@example.com", "not-a-uuid", "secret-a")).toThrow("Signup verification token configuration is invalid"); }); });