import { authenticator } from "otplib";
import { describe, expect, it } from "vitest";

import {
  buildOtpAuthUrl,
  generateTotpSecret,
  verifyTotpCode,
} from "./totp";

describe("totp", () => {
  it("generates a secret and verifies a valid code", () => {
    const secret = generateTotpSecret();
    const code = authenticator.generate(secret);

    expect(secret.length).toBeGreaterThan(0);
    expect(verifyTotpCode(secret, code)).toBe(true);
  });

  it("builds an otpauth URL", () => {
    const secret = generateTotpSecret();
    const url = buildOtpAuthUrl("HexaHost", "user@example.com", secret);

    expect(url).toContain("otpauth://totp/");
    expect(url).toContain("issuer=HexaHost");
    expect(url).toContain(encodeURIComponent("user@example.com"));
  });
});
