Files
canina/frontend/admin-panel/src/pages/OrchestrationCommandCenter.test.tsx
T

117 lines
8.9 KiB
TypeScript

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,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(<OrchestrationCommandCenter />);
fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'}));
expect(await screen.findByText(/Only staging/)).toBeInTheDocument();
expect(screen.getByRole('button',{name:'Approve bounded work'})).toBeInTheDocument();
expect(screen.queryByRole('button',{name:'RUNNING'})).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button',{name:'Agents & departments'}));
expect(await screen.findByRole('alert')).toHaveTextContent('Unavailable');
});
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(<OrchestrationCommandCenter/>);
fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'}));
expect(await screen.findByText('Scope / acceptance criteria unavailable — omitted by the safe backend projection.')).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(<OrchestrationCommandCenter />);
fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'}));
fireEvent.click(await screen.findByRole('button',{name:'Approve bounded work'}));
const dialog=screen.getByRole('dialog');
expect(within(dialog).getByRole('button',{name:'Confirm command'})).toBeDisabled();
fireEvent.change(within(dialog).getByLabelText('Expires at'),{target:{value:'2099-01-01T12:00'}});
fireEvent.change(within(dialog).getByLabelText('Reason'),{target:{value:'Scope checked'}});
fireEvent.click(within(dialog).getByLabelText('I reviewed the immutable scope and binding'));
fireEvent.click(within(dialog).getByRole('button',{name:'Confirm command'}));
expect(await within(dialog).findByRole('alert')).toHaveTextContent('network failure');
expect(within(dialog).getByLabelText('Reason')).toBeDisabled();
expect(within(dialog).getByLabelText('Expires at')).toBeDisabled();
fireEvent.click(within(dialog).getByRole('button',{name:'Confirm command'}));
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('shows measured readiness snapshot and unknown optional services without synthetic cards',async()=>{
vi.mocked(api.get).mockImplementation(async url=>{
if(url.endsWith('/tasks'))return {data:[]};
return {data:{measuredAt:'2026-10-22T12:00:00Z',items:[{id:'agent-1',title:'SEO worker',status:'BLOCKED',readiness:'Missing model policy',department:'SEO'}]}};
});
render(<OrchestrationCommandCenter/>);
fireEvent.click(screen.getByRole('button',{name:'Agents & departments'}));
expect(await screen.findByText('Missing model policy')).toBeInTheDocument();
expect(screen.getByText('BLOCKED')).toBeInTheDocument();
});
it('binds Gate2 using registered evidence, never editable commit or environment',async()=>{
const release={...task,state:'GATE2_PENDING',availableActions:[{id:'g2',label:'Approve release',gate:2,decision:'APPROVED'}],runs:[{id:'run-1',status:'SUCCEEDED',commitSha:'abc123',environment:'staging',artifacts:[{id:'artifact-1',sha256:'a'.repeat(64)}]}]};
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')?[release]:release}));
vi.mocked(api.post).mockResolvedValue({data:{id:'decision-2'}});
render(<OrchestrationCommandCenter/>);
fireEvent.click(await screen.findByRole('button',{name:'Bounded SEO audit'}));
fireEvent.click(await screen.findByRole('button',{name:'Approve release'}));
const dialog=screen.getByRole('dialog');
fireEvent.change(within(dialog).getByLabelText('Expires at'),{target:{value:'2099-01-01T12:00'}});
fireEvent.change(within(dialog).getByLabelText('Reason'),{target:{value:'Evidence reviewed'}});
fireEvent.click(within(dialog).getByLabelText('I reviewed the immutable scope and binding'));
expect(within(dialog).getByRole('button',{name:'Confirm command'})).toBeDisabled();
fireEvent.change(within(dialog).getByLabelText('Registered run / artifact binding'),{target:{value:'run-1|'+'a'.repeat(64)}});
fireEvent.click(within(dialog).getByLabelText('I reviewed the immutable scope and binding'));
fireEvent.click(within(dialog).getByRole('button',{name:'Confirm command'}));
await screen.findByText(/Command accepted/);
expect(api.post).toHaveBeenCalledWith('/admin/orchestration/tasks/t1/gates/2/decide',expect.objectContaining({runId:'run-1',artifactHash:'a'.repeat(64),commitSha:'abc123',environment:'staging',expectedVersion:4}));
});
it('requires global impact confirmation for each kill level and reports backend rejection',async()=>{
vi.mocked(api.put).mockRejectedValue(new Error('Forbidden: role'));
render(<OrchestrationCommandCenter/>);await screen.findByRole('button',{name:'Bounded SEO audit'});
fireEvent.click(screen.getByRole('button',{name:'Kill switch'}));
expect(screen.getAllByRole('option')).toHaveLength(4);
expect(screen.getByRole('button',{name:'Apply kill switch'})).toBeDisabled();
fireEvent.change(screen.getByLabelText('Kill level'),{target:{value:'EMERGENCY_HALT'}});
fireEvent.change(screen.getByLabelText('Kill reason'),{target:{value:'Incident'}});
fireEvent.click(screen.getByLabelText('I confirm global operational impact'));
fireEvent.click(screen.getByRole('button',{name:'Apply kill switch'}));
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:'<script>unsafe</script>'}});
render(<OrchestrationCommandCenter/>);await screen.findByRole('button',{name:'Bounded SEO audit'});
fireEvent.click(screen.getByRole('button',{name:'Chat'}));
fireEvent.change(screen.getByLabelText('Session ID'),{target:{value:'session-1'}});
fireEvent.change(screen.getByLabelText('Message'),{target:{value:'Explain bounded work'}});
fireEvent.click(screen.getByRole('button',{name:'Send advisory message'}));
expect(await screen.findByText('assistant: <script>unsafe</script>')).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(<OrchestrationCommandCenter/>);await screen.findByRole('button',{name:'Bounded SEO audit'});
fireEvent.click(screen.getByRole('button',{name:'Tasks & approvals'}));
expect(screen.queryByText('Create bounded work')).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button',{name:'Kill switch'}));
expect(screen.queryByRole('button',{name:'Apply kill switch'})).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button',{name:'Chat'}));
expect(screen.queryByRole('button',{name:'Send advisory message'})).not.toBeInTheDocument();
expect(api.post).not.toHaveBeenCalled();expect(api.put).not.toHaveBeenCalled();
});
});