/** @jest-environment node */ import { canManageMaterial } from '@/lib/material-authorization' import { isSuperAdmin, isSystemAdmin, } from '@/lib/department-permission' jest.mock('@/lib/department-permission', () => ({ isSuperAdmin: jest.fn(), isSystemAdmin: jest.fn(), })) describe('material authorization', () => { beforeEach(() => { jest.clearAllMocks() ;(isSystemAdmin as jest.Mock).mockReturnValue(false) ;(isSuperAdmin as jest.Mock).mockResolvedValue(false) }) test('素材所有者可以管理自己的素材', async () => { await expect( canManageMaterial( { id: 'user-1', role: 'USER' }, { userId: 'user-1' }, ), ).resolves.toBe(true) expect(isSuperAdmin).not.toHaveBeenCalled() }) test('系统管理员可以管理其他用户素材', async () => { ;(isSystemAdmin as jest.Mock).mockReturnValue(true) await expect( canManageMaterial( { id: 'admin-1', role: 'SYSTEM_ADMIN' }, { userId: 'user-1' }, ), ).resolves.toBe(true) expect(isSuperAdmin).not.toHaveBeenCalled() }) test('数据库中的超级管理员可以管理其他用户素材', async () => { ;(isSuperAdmin as jest.Mock).mockResolvedValue(true) await expect( canManageMaterial( { id: 'super-1', role: 'USER' }, { userId: 'user-1' }, ), ).resolves.toBe(true) expect(isSuperAdmin).toHaveBeenCalledWith('super-1') }) test('普通非所有者不得管理素材', async () => { await expect( canManageMaterial( { id: 'user-2', role: 'USER' }, { userId: 'user-1' }, ), ).resolves.toBe(false) }) })