55 lines
4.7 KiB
TypeScript
55 lines
4.7 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(/advertised/);
|
|
await expect(orchestration.command(task, task.availableActions[0], {idempotencyKey:'k',expiresAt:'2099-01-01T00:00:00Z'})).rejects.toThrow(/binding/);
|
|
expect(api.put).not.toHaveBeenCalled(); expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
it('binds Gate2 to immutable registered run and sends version and retry-stable key', async () => {
|
|
const run={id:'r',commitSha:'abc',environment:'staging',status:'SUCCEEDED',artifacts:[{id:'a',sha256:'a'.repeat(64)}]};
|
|
const input={idempotencyKey:'same-key',expiresAt:'2099-01-01T00:00:00Z',runId:'r',artifactHash:'a'.repeat(64)};
|
|
vi.mocked(api.post).mockRejectedValueOnce(new Error('network')).mockResolvedValueOnce({data:{id:'decision'}});
|
|
await expect(orchestration.command({...task,runs:[run]},task.availableActions[0],input)).rejects.toThrow('network');
|
|
await orchestration.command({...task,runs:[run]},task.availableActions[0],input);
|
|
expect(api.post).toHaveBeenLastCalledWith('/admin/orchestration/tasks/t%2F1/gates/2/decide',{expectedVersion:7,idempotencyKey:'same-key',decision:'APPROVED',expiresAt:input.expiresAt,notes:undefined,runId:'r',artifactHash:'a'.repeat(64),commitSha:'abc',environment:'staging'});
|
|
});
|
|
it('refuses expired approvals and unknown artifacts', async () => {
|
|
await expect(orchestration.command(task,task.availableActions[0],{idempotencyKey:'k',expiresAt:'2000-01-01T00:00:00Z'})).rejects.toThrow(/expiry/);
|
|
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(/binding/);
|
|
});
|
|
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(/Invalid/);
|
|
});
|
|
it('requires kill reason and uses only typed message bridge', async () => {
|
|
await expect(orchestration.kill('EMERGENCY_HALT','')).rejects.toThrow(/reason/);
|
|
await expect(orchestration.chat('session',' ','key')).rejects.toThrow(/message/);
|
|
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:{scope:'staging',acceptanceCriteria:['report']}})).rejects.toThrow(/budget/);
|
|
vi.mocked(api.post).mockResolvedValue({data:{id:'new-task'}});
|
|
await orchestration.create({key:'t',title:'Audit',department:'SEO',operationMode:'PREPARE',budgetLimitCents:0,metadata:{scope:'staging',acceptanceCriteria:['report']}});
|
|
expect(api.post).toHaveBeenCalledWith('/admin/orchestration/tasks',{key:'t',title:'Audit',department:'SEO',operationMode:'PREPARE',budgetLimitCents:0,metadata:{scope:'staging',acceptanceCriteria:['report']}});
|
|
});
|
|
});
|