import { ForbiddenException } from '@nestjs/common'; import { KillSwitchService } from './kill-switch.service'; describe('KillSwitchService', () => { const prisma = { orchestrationKillSwitch: { findUnique: jest.fn() } }; const service = new KillSwitchService(prisma as never); beforeEach(() => jest.resetAllMocks()); it.each([ ['FREEZE_NEW_WORK', 'CREATE_TASK'], ['STOP_AGENTS', 'RUN_AGENT'], ['DISABLE_MUTATIONS', 'MUTATE_PRODUCTION'], ['EMERGENCY_HALT', 'OBSERVE'], ])('blocks %s at its policy boundary', async (level, action) => { prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({ scope: 'GLOBAL', level, active: true, }); await expect(service.assertAllowed(action as never)).rejects.toThrow( ForbiddenException, ); }); it('freezes new claims while allowing an already running agent to finish', async () => { prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({scope:'GLOBAL',level:'FREEZE_NEW_WORK',active:true}); await expect(service.assertAllowed('CLAIM_TASK' as never)).rejects.toThrow(ForbiddenException); await expect(service.assertAllowed('RUN_AGENT')).resolves.toBeUndefined(); }); it('fails closed when an active persisted kill level is unknown', async () => { prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({scope:'GLOBAL',level:'CORRUPT_LEVEL',active:true}); await expect(service.assertAllowed('RUN_AGENT')).rejects.toThrow(ForbiddenException); }); it('allows observation while production mutations are disabled', async () => { prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({ scope: 'GLOBAL', level: 'DISABLE_MUTATIONS', active: true, }); await expect(service.assertAllowed('OBSERVE')).resolves.toBeUndefined(); }); });