import { describe, expect, test } from "vitest";
import {
    externalAdminUserCreateFormSchema,
    externalAdminUserLookupSchema,
    externalAdminUserPasswordFormSchema,
    externalAdminUserPermissionsFormSchema,
} from "./schema";

describe("external admin user schemas", () => {
    test("accepts Unicode pretty IDs and trims login identifiers", () => {
        expect(externalAdminUserLookupSchema.parse({ displayUserId: "  靓号-一号  " })).toEqual({
            displayUserId: "靓号-一号",
        });

        expect(
            externalAdminUserCreateFormSchema.parse({
                confirmPassword: " StrongPass123! ",
                displayUserId: "  VIP-1001 ",
                password: " StrongPass123! ",
                username: "  ops.fami  ",
            }),
        ).toEqual({
            displayUserId: "VIP-1001",
            password: " StrongPass123! ",
            username: "ops.fami",
        });
    });

    test("keeps password bytes unchanged and rejects mismatched confirmation", () => {
        expect(
            externalAdminUserPasswordFormSchema.parse({
                confirmPassword: " password-with-spaces ",
                password: " password-with-spaces ",
            }),
        ).toEqual({ password: " password-with-spaces " });

        expect(() =>
            externalAdminUserPasswordFormSchema.parse({
                confirmPassword: "DifferentPass123!",
                password: "StrongPass123!",
            }),
        ).toThrow("两次输入的密码不一致");
    });

    test("rejects all-whitespace credentials that cannot complete first-login rotation", () => {
        const result = externalAdminUserCreateFormSchema.safeParse({
            confirmPassword: "        ",
            displayUserId: "163000",
            password: "        ",
            username: "fami-operator",
        });

        expect(result.success).toBe(false);
    });

    test("enforces the login account character set from the API contract", () => {
        const result = externalAdminUserCreateFormSchema.safeParse({
            confirmPassword: "StrongPass123!",
            displayUserId: "1001",
            password: "StrongPass123!",
            username: "运营账号",
        });

        expect(result.success).toBe(false);
    });

    test("preserves an explicit zero-permission snapshot and removes duplicate codes", () => {
        expect(
            externalAdminUserPermissionsFormSchema.parse({ expectedRevision: 4, permissions: [] }),
        ).toEqual({ expectedRevision: 4, permissions: [] });
        expect(
            externalAdminUserPermissionsFormSchema.parse({
                expectedRevision: 4,
                permissions: ["user:list", "user:list", "room:list"],
            }),
        ).toEqual({ expectedRevision: 4, permissions: ["user:list", "room:list"] });
    });
});
