import {render,screen,fireEvent,within,waitFor} from '@testing-library/react'; import {beforeEach,it,expect,vi} from 'vitest'; import api from '../services/api'; import {orchestration} from '../services/orchestration'; import type {CreateTaskInput} from '../services/orchestration'; import OrchestrationCommandCenter from './OrchestrationCommandCenter'; vi.mock('../services/api',()=>({default:{get:vi.fn(),post:vi.fn(),put:vi.fn()}})); const action={id:'g1',label:'تأیید در Gate1',gate:1 as const,decision:'APPROVED' as const}; const task={id:'t',key:'t',title:'Safe proposal',department:'DEV',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',budgetLimitCents:0,availableActions:[action],runs:[]}; beforeEach(()=>{vi.resetAllMocks();vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'2026-10-23T00:00:00Z',capabilities:{mutations:true,chat:false,runtime:false}}:url.endsWith('/tasks')?[task]:task}));}); it('refuses Production creation even when called outside form',async()=>{ await expect(orchestration.create({key:'x',title:'x',department:'DEV',operationMode:'MUTATE_PRODUCTION',budgetLimitCents:0,metadata:{commitSha:'a'.repeat(40)}} as unknown as CreateTaskInput)).rejects.toThrow(/PREPARE|اعلام نشده/); expect(api.post).not.toHaveBeenCalled(); }); it('rejects ambiguous advertised action rather than selecting a mutation path',async()=>{ const ambiguous={...action,toState:'READY'}; await expect(orchestration.command({...task,availableActions:[ambiguous]},ambiguous,{idempotencyKey:'key',expiresAt:'2099-01-01T00:00:00Z'})).rejects.toThrow(/اعلام نشده/); expect(api.post).not.toHaveBeenCalled(); }); it('blocks release transitions even when advertised by server',async()=>{ const release={id:'release',label:'Release',toState:'RELEASING'}; await expect(orchestration.command({...task,availableActions:[release]},release,{idempotencyKey:'key'})).rejects.toThrow(/PREPARE|اعلام نشده/); expect(api.put).not.toHaveBeenCalled(); }); it('rejects Gate2 evidence from failed runs',async()=>{ const g2={...action,gate:2 as const}; await expect(orchestration.command({...task,availableActions:[g2],runs:[{id:'r',status:'FAILED',commitSha:'abc',environment:'stage',artifacts:[{id:'a',sha256:'a'.repeat(64)}]}]},g2,{idempotencyKey:'key',expiresAt:'2099-01-01T00:00:00Z',runId:'r',artifactHash:'a'.repeat(64)})).rejects.toThrow(/Gate2.*غیرفعال/); expect(api.post).not.toHaveBeenCalled(); }); async function dialog(){render();fireEvent.click(await screen.findByRole('button',{name:'Safe proposal'}));fireEvent.click(await screen.findByRole('button',{name:'تأیید در Gate1'}));const d=within(screen.getByRole('dialog'));fireEvent.change(d.getByLabelText('زمان انقضا'),{target:{value:'2099-01-01T12:00'}});fireEvent.change(d.getByLabelText('دلیل'),{target:{value:'Reviewed bounds'}});fireEvent.click(d.getByLabelText('محدوده و اتصال تغییرناپذیر را بررسی کردم'));return d;} it('locks stale 409 command until authoritative refresh instead of retrying stale payload',async()=>{ vi.mocked(api.post).mockRejectedValue({response:{status:409,data:{message:'Version conflict'}}}); const d=await dialog();fireEvent.click(d.getByRole('button',{name:'تأیید فرمان'})); await waitFor(()=>expect(d.getByRole('button',{name:'تأیید فرمان'})).toBeDisabled()); expect(await d.findByRole('alert')).toHaveTextContent(/نسخه قدیمی/); fireEvent.click(d.getByRole('button',{name:'تأیید فرمان'}));expect(api.post).toHaveBeenCalledTimes(1); fireEvent.click(screen.getByRole('button',{name:'بازخوانی وضعیت از سرور'}));expect(screen.queryByRole('dialog')).not.toBeInTheDocument();await waitFor(()=>expect(screen.queryByText('در حال دریافت وضعیت از سرور…')).not.toBeInTheDocument()); }); it('does not expose authorized task mutations when capabilities are unavailable',async()=>{ vi.mocked(api.get).mockImplementation(async url=>{if(url.endsWith('/overview'))throw new Error('offline');return {data:url.endsWith('/tasks')?[task]:task};}); render();fireEvent.click(await screen.findByRole('button',{name:'Safe proposal'}));await screen.findByRole('region',{name:'جزئیات وظیفه'}); expect(screen.getByRole('button',{name:'تأیید در Gate1'})).toBeDisabled(); }); it('prevents confirming an already expired approval',async()=>{ const d=await dialog();fireEvent.change(d.getByLabelText('زمان انقضا'),{target:{value:'2020-01-01T12:00'}}); expect(d.getByRole('button',{name:'تأیید فرمان'})).toBeDisabled(); });