Files
canina/backend/src/orchestration/gate-validity.spec.ts
T
2026-10-24 09:53:07 +00:00

45 lines
3.5 KiB
TypeScript

import { PrepareWorkflowService } from './prepare-workflow.service';
describe('queued Gate1 expiry renewal',()=>{
const owner={id:'owner',role:'SUPER_ADMIN'};
let t:any,tx:any,s:PrepareWorkflowService,approval:any;
beforeEach(()=>{
jest.useFakeTimers().setSystemTime(new Date('2026-10-24T09:15:00Z'));
approval={id:'approval',gate:1,decision:'APPROVED',actorId:'owner',createdAt:new Date('2026-10-24T05:37:54.808Z'),expiresAt:new Date('2026-10-24T06:37:00Z')};
t={id:'t',state:'READY',version:4,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'pmo',commitSha:'a'.repeat(40)},decisions:[approval]};
tx={$queryRaw:jest.fn().mockResolvedValue([]),orchestrationTask:{findUnique:jest.fn(async()=>t),update:jest.fn(async({data}:any)=>(t={...t,state:data.state,version:t.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{findFirst:jest.fn(async()=>approval),create:jest.fn()},prepareWorkflowOutbox:{findUnique:jest.fn().mockResolvedValue({status:'PENDING',attempts:0,runId:null,notBefore:null}),create:jest.fn(),update:jest.fn()}};
s=new PrepareWorkflowService({$transaction:async(fn:any)=>fn(tx)},{workerIds:['pmo'],stageCiIds:[]});
});
afterEach(()=>jest.useRealTimers());
const dto={decision:'APPROVED',expectedVersion:4,idempotencyKey:'renew'};
it('shows blocked renewal rather than actionable READY at actual failure time',()=>{
expect(s.project(t,owner)).toMatchObject({displayState:'GATE1_RENEWAL_REQUIRED',availableActions:['APPROVE_GATE1']});
});
it('renews by ordinary independent approval without recreating outbox or changing immutable task binding',async()=>{
await expect(s.decide('t',1,dto,owner)).resolves.toMatchObject({state:'READY',version:5});
expect(tx.prepareWorkflowOutbox.create).not.toHaveBeenCalled();
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:'owner',expiresAt:new Date('2026-10-25T09:15:00Z')})});
expect(tx.orchestrationEvent.create).toHaveBeenCalledWith({data:expect.objectContaining({eventType:'PREPARE_GATE1_RENEWED',fromState:'READY',toState:'READY'})});
});
it('does not renew valid approvals, self review, active runs, or schedule changes',async()=>{
await expect(s.decide('t',1,dto,{id:'pmo',role:'SUPER_ADMIN'})).rejects.toMatchObject({status:403});
await expect(s.decide('t',1,{...dto,notBefore:'2026-10-25T10:00:00Z'},owner)).rejects.toMatchObject({status:400});
tx.prepareWorkflowOutbox.findUnique.mockResolvedValue({status:'PENDING',attempts:1,runId:'r'});
await expect(s.decide('t',1,dto,owner)).rejects.toMatchObject({status:409});
tx.prepareWorkflowOutbox.findUnique.mockResolvedValue({status:'PENDING',attempts:0,runId:null});
approval.expiresAt=new Date('2026-10-25T09:15:00Z');
await expect(s.decide('t',1,dto,owner)).rejects.toMatchObject({status:409});
});
it('replays identical command after expiry but denies changed payload and stale concurrent command',async()=>{
await s.decide('t',1,dto,owner);
const e=tx.orchestrationEvent.create.mock.calls[0][0].data;
tx.orchestrationEvent.findUnique.mockResolvedValue(e);
jest.setSystemTime(new Date('2026-10-26T09:15:00Z'));
await s.decide('t',1,dto,owner);
expect(tx.orchestrationTask.update).toHaveBeenCalledTimes(1);
await expect(s.decide('t',1,{...dto,notes:'changed'},owner)).rejects.toMatchObject({status:409});
tx.orchestrationEvent.findUnique.mockResolvedValue(null);
await expect(s.decide('t',1,{...dto,idempotencyKey:'competing'},owner)).rejects.toMatchObject({status:409});
});
});