Deploy Canina / deploy (push) Successful in 2m1s
Preserve bounded command safety and snapshot provenance; render structured Persian records without translating human evidence. Validated app/node TypeScript, Vite build and 394 tests across 64 files in private Node 22 runner.
127 lines
11 KiB
TypeScript
127 lines
11 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import api from './api';
|
|
import { orchestration } from './orchestration';
|
|
import type { Task } from './orchestration';
|
|
vi.mock('./api', () => ({ default: { get: vi.fn(), post: vi.fn(), put: vi.fn() } }));
|
|
const task = {id:'t/1',version:7,state:'GATE2_PENDING',availableActions:[{id:'approve',label:'Approve',gate:2,decision:'APPROVED'}]} as Task;
|
|
describe('orchestration adapter', () => {
|
|
beforeEach(() => vi.resetAllMocks());
|
|
it('loads bare task lists through existing authenticated client and fails closed without actions', async () => {
|
|
vi.mocked(api.get).mockResolvedValue({data:[{id:'t1',title:'Bounded work',version:2,state:'READY'}]});
|
|
const tasks = await orchestration.tasks();
|
|
expect(api.get).toHaveBeenCalledWith('/admin/orchestration/tasks');
|
|
expect(tasks[0].availableActions).toEqual([]);
|
|
});
|
|
it('rejects unadvertised transitions and incomplete Gate2 binding without a request', async () => {
|
|
await expect(orchestration.command(task, {id:'invented',label:'Run',toState:'RUNNING'}, {idempotencyKey:'k',reason:'reviewed'})).rejects.toThrow(/اعلام نشده/);
|
|
await expect(orchestration.command(task, task.availableActions[0], {idempotencyKey:'k',expiresAt:'2099-01-01T00:00:00Z'})).rejects.toThrow(/Gate2.*غیرفعال/);
|
|
expect(api.put).not.toHaveBeenCalled(); expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
it('rejects even fully registered Gate2 evidence because release decisions are disabled', async () => {
|
|
const run={id:'r',commitSha:'abc',environment:'staging',status:'SUCCEEDED',artifacts:[{id:'a',sha256:'a'.repeat(64)}]};
|
|
await expect(orchestration.command({...task,runs:[run]},task.availableActions[0],{idempotencyKey:'same-key',expiresAt:'2099-01-01T00:00:00Z',runId:'r',artifactHash:'a'.repeat(64)})).rejects.toThrow(/Gate2.*غیرفعال/);
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
it('refuses expired approvals and unknown artifacts', async () => {
|
|
await expect(orchestration.command({...task,availableActions:[{id:'g1',label:'Gate1',gate:1,decision:'APPROVED'}]},{id:'g1',label:'Gate1',gate:1,decision:'APPROVED'},{idempotencyKey:'k',expiresAt:'2000-01-01T00:00:00Z'})).rejects.toThrow(/انقضا/);
|
|
const bad={...task,runs:[{id:'r',commitSha:'abc',environment:'staging',status:'DONE',artifacts:[]}]};
|
|
await expect(orchestration.command(bad,task.availableActions[0],{idempotencyKey:'k',expiresAt:'2099-01-01T00:00:00Z',runId:'r',artifactHash:'bad'})).rejects.toThrow(/Gate2.*غیرفعال/);
|
|
});
|
|
it('loads immutable detail and rejects malformed optional resources', async () => {
|
|
vi.mocked(api.get).mockResolvedValueOnce({data:{success:true,data:{...task,runs:[]}}}).mockResolvedValueOnce({data:{nonsense:true}});
|
|
expect((await orchestration.task('t/1')).version).toBe(7);
|
|
await expect(orchestration.resource('agents')).rejects.toThrow(/نامعتبر/);
|
|
});
|
|
it('requires kill reason and uses only typed message bridge', async () => {
|
|
await expect(orchestration.kill('EMERGENCY_HALT','')).rejects.toThrow(/دلیل/);
|
|
await expect(orchestration.chat('session',' ','key')).rejects.toThrow(/پیام/);
|
|
vi.mocked(api.put).mockResolvedValue({data:{active:true}});
|
|
await orchestration.kill('STOP_AGENTS','Incident review');
|
|
expect(api.put).toHaveBeenCalledWith('/admin/orchestration/kill-switch',{level:'STOP_AGENTS',reason:'Incident review'});
|
|
vi.mocked(api.post).mockResolvedValue({data:{id:'reply',role:'assistant',content:'Review complete'}});
|
|
expect(await orchestration.chat('session','Review task','key')).toEqual({id:'reply',role:'assistant',content:'Review complete'});
|
|
expect(api.post).toHaveBeenCalledWith('/admin/orchestration/chat/messages',{sessionId:'session',message:'Review task',idempotencyKey:'key'});
|
|
});
|
|
it('creates bounded work without permitting invalid caps or client-supplied state',async()=>{
|
|
await expect(orchestration.create({key:'t',title:'Audit',department:'SEO',operationMode:'PREPARE',budgetLimitCents:-1,metadata:{commitSha:'a'.repeat(40),scope:'Audit staging only',acceptanceCriteria:['Schema checked']}})).rejects.toThrow(/budget/);
|
|
vi.mocked(api.post).mockResolvedValue({data:{id:'new-task'}});
|
|
await orchestration.create({key:'t',title:'Audit',department:'SEO',operationMode:'PREPARE',budgetLimitCents:20,metadata:{commitSha:'a'.repeat(40),scope:'Audit staging only',acceptanceCriteria:['Schema checked']}});
|
|
expect(api.post).toHaveBeenCalledWith('/admin/orchestration/tasks',{key:'t',title:'Audit',department:'SEO',operationMode:'PREPARE',budgetLimitCents:20,metadata:{commitSha:'a'.repeat(40),scope:'Audit staging only',acceptanceCriteria:['Schema checked']}});
|
|
});
|
|
});
|
|
|
|
describe('actual PREPARE backend contract',()=>{
|
|
beforeEach(()=>vi.resetAllMocks());
|
|
it('normalizes strings by fixed mapping, drops unknown and tampered objects, never infers from state',async()=>{
|
|
vi.mocked(api.get).mockResolvedValue({data:{...task,state:'DRAFT',availableActions:['SUBMIT_GATE1','APPROVE_GATE1','REJECT_GATE1','QUEUE_PREPARE','RELEASE_AUTHORIZE',{id:'SUBMIT_GATE1',label:'bad',toState:'RELEASING'},null]}});
|
|
const detail=await orchestration.task('t/1');
|
|
expect(detail.availableActions.map(a=>a.id)).toEqual(['SUBMIT_GATE1','APPROVE_GATE1','REJECT_GATE1','QUEUE_PREPARE']);
|
|
vi.mocked(api.put).mockResolvedValue({data:{}});
|
|
await orchestration.command(detail,detail.availableActions[0],{idempotencyKey:'submit'});
|
|
expect(api.put).toHaveBeenCalledWith('/admin/orchestration/tasks/t%2F1/transition',{toState:'GATE1_PENDING',expectedVersion:7,idempotencyKey:'submit',reason:undefined});
|
|
vi.mocked(api.get).mockResolvedValue({data:[{...task,state:'DRAFT',availableActions:[]}]});
|
|
expect((await orchestration.tasks())[0].availableActions).toEqual([]);
|
|
});
|
|
it('rejects arbitrary object transitions and conflicting known mappings',async()=>{
|
|
vi.mocked(api.get).mockResolvedValue({data:{...task,availableActions:[{id:'evil',label:'go',toState:'RUNNING'},{id:'SUBMIT_GATE1',label:'go',toState:'READY'}]}});
|
|
expect((await orchestration.task('t/1')).availableActions).toEqual([]);
|
|
});
|
|
it.each([0,19,10001,20.5])('rejects invalid budget %s',async budget=>{
|
|
await expect(orchestration.create({key:'k',title:'t',department:'SEO',operationMode:'PREPARE',budgetLimitCents:budget,metadata:{commitSha:'a'.repeat(40),scope:'Audit staging only',acceptanceCriteria:['Schema checked']}})).rejects.toThrow(/budget/i);
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
it.each(['abc','A'.repeat(40),'a'.repeat(64),''])('rejects invalid commit %s',async commitSha=>{
|
|
await expect(orchestration.create({key:'k',title:'t',department:'SEO',operationMode:'PREPARE',budgetLimitCents:20,metadata:{commitSha,scope:'Audit staging only',acceptanceCriteria:['Schema checked']}})).rejects.toThrow(/commit/i);
|
|
});
|
|
it.each([20,10000])('posts exact DTO at boundary %s',async budget=>{
|
|
vi.mocked(api.post).mockResolvedValue({data:{}});
|
|
const dto={key:'k',title:'t',department:'SEO',operationMode:'PREPARE' as const,budgetLimitCents:budget,metadata:{commitSha:'a'.repeat(40),scope:'Audit staging only',acceptanceCriteria:['Schema checked']}};
|
|
await orchestration.create(dto);expect(api.post).toHaveBeenCalledWith('/admin/orchestration/tasks',dto);
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
['APPROVE_GATE1','post','/gates/1/decide',{decision:'APPROVED'}],
|
|
['REJECT_GATE1','post','/gates/1/decide',{decision:'REJECTED'}],
|
|
['QUEUE_PREPARE','put','/transition',{toState:'READY'}],
|
|
])('routes actual advertised %s regardless of state',async(id,method,path,payload)=>{
|
|
vi.resetAllMocks();vi.mocked(api.get).mockResolvedValue({data:{...task,state:'DRAFT',availableActions:[id]}});
|
|
vi.mocked(api.post).mockResolvedValue({data:{}});vi.mocked(api.put).mockResolvedValue({data:{}});
|
|
const detail=await orchestration.task('t/1');
|
|
await orchestration.command(detail,detail.availableActions[0],{idempotencyKey:'k',expiresAt:new Date(Date.now()+3600000).toISOString()});
|
|
expect(method==='post'?api.post:api.put).toHaveBeenCalledWith('/admin/orchestration/tasks/t%2F1'+path,expect.objectContaining({...payload,expectedVersion:7,idempotencyKey:'k'}));
|
|
});
|
|
it('rejects canonical Gate1 decisions beyond backend 24 hour limit',async()=>{
|
|
vi.resetAllMocks();vi.mocked(api.get).mockResolvedValue({data:{...task,availableActions:['APPROVE_GATE1']}});
|
|
const detail=await orchestration.task('t/1');
|
|
await expect(orchestration.command(detail,detail.availableActions[0],{idempotencyKey:'k',expiresAt:'2099-01-01T00:00:00Z'})).rejects.toThrow(/۲۴ ساعت/);
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
const validProposal={key:'scope-1',title:'Review staging',department:'SEO',operationMode:'PREPARE' as const,budgetLimitCents:20,metadata:{commitSha:'a'.repeat(40),scope:' Human scope ',acceptanceCriteria:[' Evidence reviewed ']}};
|
|
it('preserves immutable human metadata exactly and strips server-owned fields from exact DTO',async()=>{
|
|
vi.resetAllMocks();
|
|
const projected={id:'new',...validProposal,state:'DRAFT',version:1,metadata:{...validProposal.metadata,environment:'staging'},availableActions:['SUBMIT_GATE1']};
|
|
vi.mocked(api.post).mockResolvedValue({data:projected});
|
|
await expect(orchestration.create({...validProposal,metadata:{...validProposal.metadata,workflowScope:'PRODUCTION',createdBy:'forged'}} as unknown as import('./orchestration').CreateTaskInput)).resolves.toEqual(projected);
|
|
expect(api.post).toHaveBeenCalledWith('/admin/orchestration/tasks',validProposal);
|
|
vi.mocked(api.get).mockResolvedValue({data:projected});
|
|
expect((await orchestration.task('new')).metadata).toEqual(projected.metadata);
|
|
vi.mocked(api.get).mockResolvedValue({data:[projected]});
|
|
expect((await orchestration.tasks())[0].metadata).toEqual(projected.metadata);
|
|
});
|
|
it.each(['', ' ', 'x'.repeat(2001),undefined,42])('rejects invalid human scope %s before HTTP',async scope=>{
|
|
vi.resetAllMocks();await expect(orchestration.create({...validProposal,metadata:{...validProposal.metadata,scope}} as unknown as import('./orchestration').CreateTaskInput)).rejects.toThrow(/scope/i);expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
it.each([[],Array(21).fill('x'),[''],[' '],['x'.repeat(501)],undefined,'criterion',[42]])('rejects invalid acceptance criteria %s before HTTP',async acceptanceCriteria=>{
|
|
vi.resetAllMocks();await expect(orchestration.create({...validProposal,metadata:{...validProposal.metadata,acceptanceCriteria}} as unknown as import('./orchestration').CreateTaskInput)).rejects.toThrow(/criteria/i);expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
it('rejects array-coerced commit hashes',async()=>{
|
|
vi.resetAllMocks();await expect(orchestration.create({...validProposal,metadata:{...validProposal.metadata,commitSha:['a'.repeat(40)]}} as unknown as import('./orchestration').CreateTaskInput)).rejects.toThrow(/commit/i);expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
it('accepts scope and criteria at exact backend limits',async()=>{
|
|
vi.resetAllMocks();vi.mocked(api.post).mockResolvedValue({data:{}});
|
|
const dto={...validProposal,metadata:{...validProposal.metadata,scope:'x'.repeat(2000),acceptanceCriteria:Array(20).fill('x'.repeat(500))}};
|
|
await orchestration.create(dto);expect(api.post).toHaveBeenCalledWith('/admin/orchestration/tasks',dto);
|
|
});
|