import { expect, it } from "vitest";
import { decrypt, encrypt, totp, totpOk } from "./crypto.js";

it("TOTP matches the RFC 6238 test vector", () => {
  const secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ";
  expect(totp(secret, 59_000)).toBe("287082");
  expect(totpOk(secret, "287082", 59_000 + 30_000)).toBe(true);
  expect(totpOk(secret, "287082", 59_000 + 120_000)).toBe(false);
});
it("encryption round-trips and detects changes", () => {
  const e = encrypt("smtp-pass");
  expect(decrypt(e)).toBe("smtp-pass");
  const b = Buffer.from(e, "base64"); b[b.length - 1]! ^= 1;
  expect(() => decrypt(b.toString("base64"))).toThrow();
});
