Files
canina/frontend/admin-panel/src/pages/editorialRecovery.test.tsx
T
2026-10-24 10:58:18 +00:00

64 lines
6.3 KiB
TypeScript

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 OrchestrationCommandCenter from './OrchestrationCommandCenter';
vi.mock('../services/api',()=>({default:{get:vi.fn(),post:vi.fn(),put:vi.fn()}}));
const run={id:'r-expired',status:'RUNNING',environment:'staging',commitSha:'a'.repeat(40),artifacts:[]};
const task={id:'editorial',key:'editorial-v1:'+ 'a'.repeat(64),title:'Editorial research: Test',department:'CONTENT',operationMode:'PREPARE',state:'RUNNING',version:6,budgetLimitCents:20,metadata:{commitSha:run.commitSha},availableActions:[],runs:[run],editorialRecovery:{runId:run.id}};
beforeEach(()=>{vi.resetAllMocks();vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'now',capabilities:{mutations:true}}:url.endsWith('/tasks')?[task]:task}));});
it('posts only the exact expired recovery payload through authenticated API',async()=>{
vi.mocked(api.post).mockResolvedValue({data:{}});
await orchestration.editorialFailure(task,{runId:run.id,expectedVersion:6,idempotencyKey:'stable',reason:'Process ended'});
expect(api.post).toHaveBeenCalledWith('/admin/orchestration/tasks/editorial/editorial-failure',{runId:run.id,expectedVersion:6,idempotencyKey:'stable',reason:'Process ended'});
});
it.each([{...task,editorialRecovery:undefined},{...task,runs:[]},{...task,runs:[{...run,status:'SUCCEEDED'}]},{...task,runs:[{...run,commitSha:'b'.repeat(40)}]}])('rejects missing or mismatched server recovery binding',async invalid=>{
await expect(orchestration.editorialFailure(invalid,{runId:run.id,expectedVersion:6,idempotencyKey:'k',reason:'Process ended'})).rejects.toThrow();expect(api.post).not.toHaveBeenCalled();
});
it.each(['',' ','x'.repeat(501)])('bounds required recovery reason',async reason=>{
await expect(orchestration.editorialFailure(task,{runId:run.id,expectedVersion:6,idempotencyKey:'k',reason})).rejects.toThrow();expect(api.post).not.toHaveBeenCalled();
});
it('uses fresh detail before modal; freezes retry bytes; blocks concurrency rejection until refresh',async()=>{
let details=0;
vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'now',capabilities:{mutations:true}}:url.endsWith('/tasks')?[task]:{...task,version:++details===1?6:7}}));
vi.mocked(api.post).mockRejectedValueOnce(new Error('offline')).mockRejectedValueOnce({response:{status:409}});
render(<OrchestrationCommandCenter/>);
fireEvent.click(await screen.findByRole('button',{name:task.title}));
fireEvent.click(await screen.findByRole('button',{name:'ثبت شکست اجرای متوقف‌شده'}));
const dialog=await screen.findByRole('dialog',{name:'ثبت شکست اجرای متوقف‌شده'});
expect(details).toBe(2);expect(api.post).not.toHaveBeenCalled();
expect(within(dialog).getByText(/۲۰ سنت.*نامشخص/)).toBeInTheDocument();
expect(within(dialog).getByText(/QA.*تأیید خودکار/)).toBeInTheDocument();
const submit=within(dialog).getByRole('button',{name:'ثبت شکست'});
expect(submit).toBeDisabled();
fireEvent.change(within(dialog).getByLabelText('دلیل شکست'),{target:{value:'Process ended'}});
fireEvent.click(within(dialog).getByLabelText('ثبت شکست بدون تأیید کیفیت یا آزادسازی بودجه را تأیید می‌کنم'));
fireEvent.click(submit);await within(dialog).findByRole('alert');
expect(within(dialog).getByLabelText('دلیل شکست')).toBeDisabled();
fireEvent.click(submit);await waitFor(()=>expect(submit).toBeDisabled());
expect(api.post).toHaveBeenCalledTimes(2);
const payload=vi.mocked(api.post).mock.calls[0][1];expect(payload).toEqual(expect.objectContaining({runId:run.id,expectedVersion:7,reason:'Process ended'}));expect(vi.mocked(api.post).mock.calls[1][1]).toEqual(payload);
fireEvent.click(within(dialog).getByRole('button',{name:'بازخوانی و بستن'}));await waitFor(()=>expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
});
it('refreshes authoritative tasks after success without approval or budget commands',async()=>{
vi.mocked(api.post).mockResolvedValue({data:{state:'FAILED'}});
render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:task.title}));fireEvent.click(await screen.findByRole('button',{name:'ثبت شکست اجرای متوقف‌شده'}));
const dialog=await screen.findByRole('dialog');
fireEvent.change(within(dialog).getByLabelText('دلیل شکست'),{target:{value:'Process ended'}});
fireEvent.click(within(dialog).getByLabelText('ثبت شکست بدون تأیید کیفیت یا آزادسازی بودجه را تأیید می‌کنم'));
fireEvent.click(within(dialog).getByRole('button',{name:'ثبت شکست'}));
await waitFor(()=>expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
expect(api.post).toHaveBeenCalledTimes(1);expect(api.put).not.toHaveBeenCalled();
expect(vi.mocked(api.get).mock.calls.filter(([url])=>url.endsWith('/tasks'))).toHaveLength(2);
});
it('does not infer eligibility from RUNNING or guessed age',async()=>{
vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/tasks')?[task]:{...task,editorialRecovery:undefined}}));
render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:task.title}));await screen.findByRole('region',{name:'جزئیات وظیفه'});
expect(screen.queryByRole('button',{name:'ثبت شکست اجرای متوقف‌شده'})).not.toBeInTheDocument();
});
it('rejects eligibility removed during fresh open without a mutation',async()=>{
let details=0;vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'now',capabilities:{mutations:true}}:url.endsWith('/tasks')?[task]:{...task,editorialRecovery:++details===1?task.editorialRecovery:undefined}}));
render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:task.title}));fireEvent.click(await screen.findByRole('button',{name:'ثبت شکست اجرای متوقف‌شده'}));
expect(await screen.findByRole('alert')).toHaveTextContent(/بازخوانی/);expect(screen.queryByRole('dialog')).not.toBeInTheDocument();expect(api.post).not.toHaveBeenCalled();
});