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

import {
  buildDefaultContactProtectionSettings,
  parseContactProtectionValue,
  toContactProtectionFormValues,
} from "../lib/contact-protection";

describe("contact protection helpers", () => {
  it("builds safe defaults", () => {
    expect(buildDefaultContactProtectionSettings()).toEqual({
      turnstile: {
        enabled: false,
        siteKey: "",
        secretKey: "",
      },
      rateLimit: {
        enabled: true,
        maxRequests: 5,
        windowMinutes: 10,
      },
    });
  });

  it("parses stored settings and trims keys", () => {
    const settings = parseContactProtectionValue(
      JSON.stringify({
        turnstile: {
          enabled: true,
          siteKey: " 0x4AAAAA ",
          secretKey: "secret",
        },
        rateLimit: {
          enabled: true,
          maxRequests: "8",
          windowMinutes: "15",
        },
      }),
    );

    expect(settings.turnstile.enabled).toBe(true);
    expect(settings.turnstile.siteKey).toBe("0x4AAAAA");
    expect(settings.turnstile.secretKey).toBe("secret");
    expect(settings.rateLimit.maxRequests).toBe(8);
    expect(settings.rateLimit.windowMinutes).toBe(15);
  });

  it("hides the stored secret in form values", () => {
    const values = toContactProtectionFormValues({
      turnstile: {
        enabled: true,
        siteKey: "site-key",
        secretKey: "secret-key",
      },
      rateLimit: {
        enabled: true,
        maxRequests: 5,
        windowMinutes: 10,
      },
    });

    expect(values.turnstile.secretKey).toBe("");
    expect(values.turnstile.hasSecretKey).toBe(true);
  });
});
