import { renderHook } from "@testing-library/react";
import { beforeEach, expect, test, vi } from "vitest";
import { PERMISSIONS } from "@/app/permissions";

const mocks = vi.hoisted(() => ({ granted: new Set() }));

vi.mock("@/app/auth/AuthProvider.jsx", () => ({
    useAuth: () => ({ can: (permission) => mocks.granted.has(permission) }),
}));

import { useExternalAdminUserAbilities } from "./permissions.js";

beforeEach(() => {
    mocks.granted = new Set();
});

test("requires both account creation and permission configuration grants to create", () => {
    mocks.granted.add(PERMISSIONS.externalAdminUserCreate);
    const { result, rerender } = renderHook(() => useExternalAdminUserAbilities());

    expect(result.current.canCreateAccount).toBe(true);
    expect(result.current.canManagePermissions).toBe(false);
    expect(result.current.canCreate).toBe(false);

    mocks.granted.add(PERMISSIONS.externalAdminUserPermissions);
    rerender();
    expect(result.current.canCreate).toBe(true);
});
