import { render, screen, fireEvent, within } from '@testing-library/react'; import { beforeEach, describe, it, expect, vi } from 'vitest'; import api from '../services/api'; import OrchestrationCommandCenter from './OrchestrationCommandCenter'; vi.mock('../services/api', () => ({ default: {get:vi.fn(),post:vi.fn(),put:vi.fn()} })); const task={id:'t1',key:'bounded-1',title:'Bounded SEO audit',department:'SEO',state:'GATE1_PENDING',version:4,operationMode:'PREPARE',budgetLimitCents:500,availableActions:[{id:'g1',label:'Approve bounded work',gate:1,decision:'APPROVED'}],runs:[],metadata:{scope:'Only staging',acceptanceCriteria:['Audit schema']}}; describe('command center', () => { beforeEach(() => { vi.resetAllMocks(); vi.mocked(api.get).mockImplementation(async url => { if(url.endsWith('/overview'))return {data:{items:[],measuredAt:'2026-10-22T00:00:00Z',capabilities:{mutations:true,killSwitch:true,chat:true,runtime:false}}}; if(url.endsWith('/tasks')) return {data:[task]}; if(url.endsWith('/tasks/t1')) return {data:task}; throw new Error('Unavailable: '+url); }); }); it('loads server tasks, immutable bounds and only server actions; unavailable data is never fabricated', async () => { render(); fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'})); const detail=await screen.findByRole('region',{name:'جزئیات وظیفه'}); expect(within(detail).getByText(/Only staging/)).toBeInTheDocument(); expect(screen.getByRole('button',{name:'تأیید در Gate1'})).toBeInTheDocument(); expect(screen.queryByRole('button',{name:'RUNNING'})).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('button',{name:'عامل‌ها و دپارتمان‌ها'})); expect(screen.getByText(/این بخش در نسخه فعلی در دسترس نیست/)).toBeInTheDocument(); expect(vi.mocked(api.get).mock.calls.some(([url])=>url.endsWith('/agents'))).toBe(false); }); it('labels deliberately omitted scope metadata unavailable, not immutable scope',async()=>{ const {metadata:_metadata,...safeTask}=task; vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/tasks')?[safeTask]:safeTask})); render(); fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'})); expect(await screen.findByText('محدوده و معیارهای پذیرش در دسترس نیست؛ سرور این اطلاعات را در نمای امن ارسال نکرده است.')).toBeInTheDocument(); expect(screen.queryByText('Immutable scope / acceptance criteria')).not.toBeInTheDocument(); }); it('requires explicit confirmation and expiry; retains idempotency across failed retry', async () => { vi.mocked(api.post).mockRejectedValue(new Error('network failure')); render(); fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'})); fireEvent.click(await screen.findByRole('button',{name:'تأیید در Gate1'})); const dialog=screen.getByRole('dialog'); expect(within(dialog).getByRole('button',{name:'تأیید فرمان'})).toBeDisabled(); fireEvent.change(within(dialog).getByLabelText('زمان انقضا'),{target:{value:'2099-01-01T12:00'}}); fireEvent.change(within(dialog).getByLabelText('دلیل'),{target:{value:'محدوده checked'}}); fireEvent.click(within(dialog).getByLabelText('محدوده و اتصال تغییرناپذیر را بررسی کردم')); fireEvent.click(within(dialog).getByRole('button',{name:'تأیید فرمان'})); expect(await within(dialog).findByRole('alert')).toHaveTextContent('network failure'); expect(within(dialog).getByLabelText('دلیل')).toBeDisabled(); expect(within(dialog).getByLabelText('زمان انقضا')).toBeDisabled(); fireEvent.click(within(dialog).getByRole('button',{name:'تأیید فرمان'})); await within(dialog).findByRole('alert'); expect(api.post).toHaveBeenCalledTimes(2); const first=vi.mocked(api.post).mock.calls[0][1]; expect(first).toEqual(expect.objectContaining({expectedVersion:4,decision:'APPROVED'})); expect(vi.mocked(api.post).mock.calls[1][1]).toEqual(first); }); it('keeps legacy Gate2 release decisions disabled without sending a command',async()=>{ const release={...task,state:'GATE2_PENDING',availableActions:[{id:'g2',label:'Approve release',gate:2,decision:'APPROVED'}]}; vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'now',capabilities:{mutations:true}}:url.endsWith('/tasks')?[release]:release})); render(); fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'})); expect(await screen.findByRole('button',{name:'تأیید در Gate2'})).toBeDisabled(); expect(api.post).not.toHaveBeenCalled(); }); it('requires global impact confirmation for each kill level and reports backend rejection',async()=>{ vi.mocked(api.put).mockRejectedValue(new Error('Forbidden: role')); render();await screen.findByRole('button',{name:'Bounded SEO audit'}); fireEvent.click(screen.getByRole('button',{name:'توقف اضطراری'})); expect(screen.getAllByRole('option')).toHaveLength(4); expect(screen.getByRole('button',{name:'اعمال توقف سراسری'})).toBeDisabled(); fireEvent.change(screen.getByLabelText('سطح توقف'),{target:{value:'EMERGENCY_HALT'}}); fireEvent.change(screen.getByLabelText('دلیل توقف'),{target:{value:'Incident'}}); fireEvent.click(screen.getByLabelText('پیامد عملیاتی سراسری را تأیید می‌کنم')); fireEvent.click(screen.getByRole('button',{name:'اعمال توقف سراسری'})); expect(await screen.findByRole('alert')).toHaveTextContent('Forbidden: role'); expect(api.put).toHaveBeenCalledWith('/admin/orchestration/kill-switch',{level:'EMERGENCY_HALT',reason:'Incident'}); }); it('sends advisory chat via typed bridge and renders text, not HTML',async()=>{ vi.mocked(api.post).mockResolvedValue({data:{id:'reply',role:'assistant',content:''}}); render();await screen.findByRole('button',{name:'Bounded SEO audit'}); fireEvent.click(screen.getByRole('button',{name:'گفت‌وگو'})); fireEvent.change(screen.getByLabelText('شناسه نشست'),{target:{value:'session-1'}}); fireEvent.change(screen.getByLabelText('پیام'),{target:{value:'Explain bounded work'}}); fireEvent.click(screen.getByRole('button',{name:'ارسال پیام مشورتی'})); expect(await screen.findByText('assistant: ')).toBeInTheDocument(); expect(document.querySelector('script')).toBeNull(); expect(api.post).toHaveBeenCalledWith('/admin/orchestration/chat/messages',expect.objectContaining({sessionId:'session-1',message:'Explain bounded work'})); }); it('fails closed for creation, kill and chat when overview cannot authorize capabilities',async()=>{ vi.mocked(api.get).mockImplementation(async url=>{if(url.endsWith('/tasks'))return {data:[task]};throw new Error('Unavailable');}); render();await screen.findByRole('button',{name:'Bounded SEO audit'}); fireEvent.click(screen.getByRole('button',{name:'وظیفه‌ها و تأییدها'})); expect(screen.queryByText('ایجاد کار محدود')).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('button',{name:'توقف اضطراری'})); expect(screen.queryByRole('button',{name:'اعمال توقف سراسری'})).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('button',{name:'گفت‌وگو'})); expect(screen.queryByRole('button',{name:'ارسال پیام مشورتی'})).not.toBeInTheDocument(); expect(api.post).not.toHaveBeenCalled();expect(api.put).not.toHaveBeenCalled(); }); }); it('requires explicit kill capability rather than generic mutations',async()=>{ vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'now',capabilities:{mutations:true}}:url.endsWith('/tasks')?[task]:task})); render();await screen.findByRole('button',{name:'Bounded SEO audit'}); fireEvent.click(screen.getByRole('button',{name:'توقف اضطراری'})); expect(screen.queryByRole('button',{name:'اعمال توقف سراسری'})).not.toBeInTheDocument();expect(api.put).not.toHaveBeenCalled(); }); it.each([{}, {scope:'STAGE_PREPARE'}, {scope:'Human scope',acceptanceCriteria:[]}])('does not claim incomplete or legacy metadata is scope evidence: %j',async metadata=>{ vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/tasks')?[{...task,metadata}]:{...task,metadata}})); render();fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'})); expect(await screen.findByText(/محدوده و معیارهای پذیرش در دسترس نیست/)).toBeInTheDocument(); expect(screen.getByText('محدوده گردش کار سرور: در نمای امن در دسترس نیست.')).toBeInTheDocument(); }); it('separates human evidence from server workflow scope without inventing a marker',async()=>{ vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/tasks')?[task]:task})); render();fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'})); expect(await screen.findByText('محدوده و معیارهای پذیرش گزارش‌شده از سرور')).toBeInTheDocument(); expect(screen.getByText('محدوده گردش کار سرور: در نمای امن در دسترس نیست.')).toBeInTheDocument(); });