/** @jest-environment node */ import { POST } from '@/app/api/restore-data/route' import { getServerSession } from 'next-auth' import { hasSystemAdminPermission } from '@/lib/department-permission' jest.mock('next-auth', () => ({ getServerSession: jest.fn(), })) jest.mock('@/lib/auth', () => ({ authOptions: {}, })) jest.mock('@/lib/department-permission', () => ({ hasSystemAdminPermission: jest.fn(), })) describe('/api/restore-data', () => { beforeEach(() => { jest.clearAllMocks() }) test('匿名访问仍被拒绝', async () => { ;(getServerSession as jest.Mock).mockResolvedValue(null) const response = await POST() expect(response.status).toBe(401) }) test('普通管理员不能进入历史恢复流程', async () => { ;(getServerSession as jest.Mock).mockResolvedValue({ user: { role: 'ADMIN', isSuperAdmin: false }, }) ;(hasSystemAdminPermission as jest.Mock).mockReturnValue(false) const response = await POST() expect(response.status).toBe(403) }) test('系统管理员也只能收到永久停用说明,不再触发任何写入', async () => { ;(getServerSession as jest.Mock).mockResolvedValue({ user: { role: 'SYSTEM_ADMIN', isSuperAdmin: false }, }) ;(hasSystemAdminPermission as jest.Mock).mockReturnValue(true) const response = await POST() const body = await response.json() expect(response.status).toBe(410) expect(body.error).toBe('在线历史恢复接口已停用') expect(body.action).toContain('dry-run') }) })