import { expect, test } from "bun:test";
import {
  invitationAcceptSchema,
  loginSchema,
  mfaSchema,
  otpLoginRequestSchema,
  registerSchema,
  signUpSchema,
} from "../src/validation.ts";

test("authentication schemas reject malformed input", () => {
  expect(registerSchema.parse({ email: "bad", password: "short", displayName: "A" }).ok).toBe(
    false,
  );
  expect(loginSchema.parse({ identifier: "", password: "" }).ok).toBe(false);
  expect(
    otpLoginRequestSchema.parse({ identifier: "person@example.com", method: "voice" }).ok,
  ).toBe(false);
  expect(invitationAcceptSchema.parse({ token: "short" }).ok).toBe(false);
  expect(mfaSchema.parse({ mfaToken: "short", method: "unknown", code: "1" }).ok).toBe(false);
});

test("sign-up schema is shared by browser and server registration", () => {
  const invalid = signUpSchema.parse({
    displayName: "A",
    email: "bad",
    password: "weak",
    consent: false,
  });
  expect(invalid.ok).toBe(false);
  expect(invalid.errors).toMatchObject({
    displayName: "Enter your full name",
    email: "Enter a valid email address",
    consent: "Accept the terms and privacy policy to continue",
  });

  expect(
    signUpSchema.parse({
      displayName: "Ada Lovelace",
      email: "ada@example.com",
      password: "StrongPassword123",
      consent: true,
    }).ok,
  ).toBe(true);
});

test("built-in browser schema registry covers packaged auth forms", async () => {
  const { authBrowserSchemaDescriptors, authSchemas } = await import("../src/validation.ts");
  const descriptors = authBrowserSchemaDescriptors(authSchemas);
  expect(descriptors["auth-register"]).toBeDefined();
  expect(descriptors["auth-password-request"]).toBeDefined();
  expect(descriptors["auth-session-revoke"]).toBeDefined();
  expect(descriptors["auth-authenticator-setup"]).toBeDefined();
  expect(descriptors["auth-authenticator-confirm"]).toBeDefined();
  expect(descriptors["auth-authenticator-disable"]).toBeDefined();
  expect(descriptors["auth-recovery-codes"]).toBeDefined();
  expect(descriptors["auth-empty"]).toBeDefined();
});
