import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, expect, test, vi } from "vitest";
import { ExternalAdminPermissionDrawer } from "./ExternalAdminPermissionDrawer.jsx";
import { ExternalAdminUserFormDialog } from "./ExternalAdminUserFormDialog.jsx";

afterEach(cleanup);

const abilities = { canCreate: true, canManagePermissions: true };

test("disables account creation when a successful permission catalog is empty", () => {
    render(
        <ExternalAdminUserFormDialog
            abilities={abilities}
            catalog={[]}
            catalogError=""
            catalogLoading={false}
            form={{
                confirmPassword: "StrongPass123!",
                displayUserId: "1001",
                password: "StrongPass123!",
                permissions: [],
                username: "ops.fami",
            }}
            loading={false}
            lookupLoading={false}
            open
            targetUser={{ displayUserId: "1001", userId: "user-1", username: "Fami user" }}
            onClose={vi.fn()}
            onDisplayUserIdChange={vi.fn()}
            onFormChange={vi.fn()}
            onLookup={vi.fn()}
            onReloadCatalog={vi.fn()}
            onSubmit={vi.fn()}
        />,
    );

    expect(screen.getByRole("button", { name: "创建" })).toBeDisabled();
});

test("disables permission updates when a successful catalog is empty", () => {
    render(
        <ExternalAdminPermissionDrawer
            abilities={abilities}
            catalog={[]}
            catalogError=""
            catalogLoading={false}
            error=""
            loading={false}
            permissions={[]}
            permissionsLoading={false}
            user={{ id: 71, username: "ops.fami" }}
            onChange={vi.fn()}
            onClose={vi.fn()}
            onReloadCatalog={vi.fn()}
            onRetry={vi.fn()}
            onSubmit={vi.fn()}
        />,
    );

    expect(screen.getByRole("button", { name: "保存权限" })).toBeDisabled();
});
