import { describe, expect, test } from "vitest";
import { FormValidationError, parseForm } from "@/shared/forms/validation";
import { userCreateFormSchema, userUpdateFormSchema } from "./schema";

describe("user form schema", () => {
  test("normalizes create payload", () => {
    const payload = parseForm(userCreateFormSchema, {
      mfaEnabled: true,
      name: "  张三  ",
      password: "",
      roleIds: ["1", 2],
      status: "active",
      team: " 平台组 ",
      teamId: "2",
      username: " admin "
    });

    expect(payload).toEqual({
      mfaEnabled: true,
      name: "张三",
      password: "",
      roleIds: [1, 2],
      status: "active",
      team: "平台组",
      teamId: 2,
      username: "admin"
    });
  });

  test("rejects missing required fields", () => {
    expect(() =>
      parseForm(userUpdateFormSchema, {
        mfaEnabled: false,
        name: "",
        roleIds: [],
        status: "active",
        team: ""
      })
    ).toThrow(FormValidationError);
  });
});
