fix(orchestration): atomically queue approvals with optional not-before schedule
Deploy Canina / deploy (push) Successful in 4m56s

This commit is contained in:
devops committed 2026-10-24 06:16:20 +00:00
1 parent ab2f330eb8
commit 50d0df822c
15 files changed
+216 -65

No files matched your search

@@ -4,7 +4,7 @@ import {
Injectable,
NotFoundException,
} from '@nestjs/common';
import { IsObject, IsUUID } from 'class-validator';
import { IsInt, Min, IsNotEmpty, IsString, IsObject, IsUUID } from 'class-validator';
import { createHash } from 'crypto';
import {
PrepareWorkflowService,
@@ -16,6 +16,10 @@ import { EDITORIAL_PILOT_KIND } from './editorial-bridge';
export class EditorialClaimDto {
@IsUUID() taskId!: string;
}
export class EditorialQueueDto extends EditorialClaimDto {
@IsInt() @Min(1) expectedVersion!: number;
@IsString() @IsNotEmpty() idempotencyKey!: string;
}
export class EditorialResultDto extends EditorialClaimDto {
@IsUUID() runId!: string;
@IsUUID() leaseToken!: string;
@@ -266,6 +270,18 @@ export class EditorialExecutionService {
};
});
}
async queue(a: WorkflowIdentity, dto: EditorialQueueDto) {
this.pmo(a);
return this.db.$transaction(async (tx:any)=>{
await this.lock(tx,dto.taskId,true);
const core=new PrepareWorkflowService(new Proxy(tx, {get(target,key){
if(key==='$transaction') return (fn:any)=>fn(tx);
const value=target[key]; return typeof value==='function'?value.bind(target):value;
}}),{workerIds:['pmo'],stageCiIds:[]});
const t=await core.enqueueApproved(a,dto);
return {taskId:t.id,state:t.state,version:t.version};
});
}
async claim(a: WorkflowIdentity, dto: EditorialClaimDto) {
this.pmo(a);
return this.db.$transaction(async (tx: any) => {
@@ -33,7 +33,7 @@ import {
} from "../orchestration/prepare-workflow.service";
import { EditorialProposalBridge, EditorialProposalDto, isEditorialProposal } from "./editorial-bridge";
import { EditorialExecutionService, EditorialClaimDto, EditorialResultDto } from './editorial-execution';
import { EditorialExecutionService, EditorialClaimDto, EditorialQueueDto, EditorialResultDto } from './editorial-execution';
import { admittedTechTask, TECH_PILOT_KIND, techArtifact, validateTechSource } from "./tech-pilot-validator";
// This is reviewed static configuration, not a URL fetched by the transport.
@@ -324,6 +324,9 @@ class WorkerTransportController {
@Get('editorial-proposals/:taskId') editorialStatus(@Req() req: any, @Param('taskId') taskId: string) {
return this.execution.status(req.workerIdentity, taskId);
}
@Post('editorial-queue') editorialQueue(@Req() req: any, @Body() dto: EditorialQueueDto) {
return this.execution.queue(req.workerIdentity, dto);
}
@Post('editorial-claim') editorialClaim(@Req() req: any, @Body() dto: EditorialClaimDto) {
return this.execution.claim(req.workerIdentity, dto);
}
@@ -0,0 +1,59 @@
import { PrismaClient } from '@prisma/client';
import { PrepareWorkflowService } from './prepare-workflow.service';
const enabled=process.env.WORKFLOW_DB==='1';
(enabled?describe:describe.skip)('approval queue PostgreSQL atomicity',()=>{
const db=new PrismaClient();
const service=new PrepareWorkflowService(db,{workerIds:['pmo','worker'],stageCiIds:[]});
const owner={id:'pmo',role:'Admin'}, reviewer={id:'reviewer',role:'SUPER_ADMIN'},worker={id:'worker',role:'WORKFLOW_WORKER'};
async function pending(){const t=await service.create({key:`queue-${crypto.randomUUID()}`,title:'QA',department:'CONTENT',operationMode:'PREPARE',budgetLimitCents:20,metadata:{scope:'Isolated QA',acceptanceCriteria:['Test queue'],commitSha:'a'.repeat(40)}},owner);return service.transition(t.id,{toState:'GATE1_PENDING',expectedVersion:t.version,idempotencyKey:`submit-${t.id}`},owner);}
afterAll(()=>db.$disconnect());
it('racing approvals serialize once, replay once, and queue can immediately claim',async()=>{
const t=await pending();const dto={decision:'APPROVED',expectedVersion:t.version,idempotencyKey:`approve-${t.id}`};
const results=await Promise.all([service.decide(t.id,1,dto,reviewer),service.decide(t.id,1,dto,reviewer)]);
expect(results.every(t=>t.state==='READY')).toBe(true);
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(1);
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(1);
const claims=await Promise.all([service.claim(worker,t.id),service.claim(worker,t.id)]);
expect(claims.filter(Boolean)).toHaveLength(1);
});
it('scheduled work cannot claim early via targeted or polling claim; becomes claimable at due time',async()=>{
const t=await pending();const notBefore=new Date(Date.now()+3600000).toISOString();
await service.decide(t.id,1,{decision:'APPROVED',expectedVersion:t.version,idempotencyKey:`schedule-${t.id}`,notBefore},reviewer);
expect(await service.claim(worker,t.id)).toBeNull();
expect(await service.claim(worker)).toBeNull();
const o=await db.prepareWorkflowOutbox.findUniqueOrThrow({where:{taskId:t.id}});
expect(o.notBefore).toEqual(new Date(notBefore));
const approval=await db.orchestrationApprovalDecision.findFirstOrThrow({where:{taskId:t.id}});
expect(approval.expiresAt.getTime()).toBeGreaterThan(Date.parse(notBefore));
await db.prepareWorkflowOutbox.update({where:{taskId:t.id},data:{notBefore:new Date(0)}});
expect(await service.claim(worker,t.id)).toMatchObject({taskId:t.id,attempt:1});
});
it('hold and rejection never queue',async()=>{
const t=await pending();const held=await service.transition(t.id,{toState:'GATE1_HELD',expectedVersion:t.version,idempotencyKey:`hold-${t.id}`,reason:'Revise scope'},reviewer);
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(0);
await service.decide(t.id,1,{decision:'REJECTED',expectedVersion:held.version,idempotencyKey:`reject-${t.id}`},reviewer);
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(0);
});
it('expired approval cannot claim and immutable decision is not edited',async()=>{
const t=await pending();await service.decide(t.id,1,{decision:'APPROVED',expectedVersion:t.version,idempotencyKey:`expire-${t.id}`,expiresAt:new Date(Date.now()+300).toISOString()},reviewer);
await new Promise(r=>setTimeout(r,350));
await expect(service.claim(worker,t.id)).rejects.toThrow(/expired/i);
expect(await db.orchestrationRun.count({where:{taskId:t.id}})).toBe(0);
});
it('PMO recovery queues stored v3 approval without creating a decision, no impersonation',async()=>{
const t=await pending();
await db.orchestrationTask.update({where:{id:t.id},data:{state:'GATE1_APPROVED',version:3}});
await db.orchestrationApprovalDecision.create({data:{taskId:t.id,gate:1,decision:'APPROVED',actorId:'independent',expiresAt:new Date(Date.now()+60000),idempotencyKey:`legacy-${t.id}`}});
const dto={taskId:t.id,expectedVersion:3,idempotencyKey:`recover-${t.id}`};
const result=await Promise.all([service.enqueueApproved({id:'pmo',role:'WORKFLOW_WORKER'},dto),service.enqueueApproved({id:'pmo',role:'WORKFLOW_WORKER'},dto)]);
expect(result.every(t=>t.state==='READY'&&t.version===4)).toBe(true);
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(1);
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(1);
});
it('outbox failure rolls back immutable approval and READY together',async()=>{
const t=await pending();await db.prepareWorkflowOutbox.create({data:{taskId:t.id}});
await expect(service.decide(t.id,1,{decision:'APPROVED',expectedVersion:t.version,idempotencyKey:`fail-${t.id}`},reviewer)).rejects.toThrow();
expect((await db.orchestrationTask.findUniqueOrThrow({where:{id:t.id}})).state).toBe('GATE1_PENDING');
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(0);
});
});
@@ -0,0 +1,47 @@
import { PrepareWorkflowService } from './prepare-workflow.service';
describe('atomic approval queue contract', () => {
let t:any, tx:any, service:PrepareWorkflowService;
const actor={id:'reviewer',role:'SUPER_ADMIN'};
const command={decision:'APPROVED',expectedVersion:2,idempotencyKey:'approve'};
beforeEach(()=>{
t={id:'t',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'pmo'}};
tx={$queryRaw:jest.fn().mockResolvedValue([]),orchestrationTask:{findUnique:jest.fn(async()=>t),update:jest.fn(async({data}:any)=>(t={...t,...data,version:t.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()},prepareWorkflowOutbox:{create:jest.fn()}};
service=new PrepareWorkflowService({$transaction:async(fn:any)=>fn(tx)},{workerIds:['pmo'],stageCiIds:[]});
});
it('approval with no date enters READY and creates exactly one outbox atomically',async()=>{
await expect(service.decide('t',1,command,actor)).resolves.toMatchObject({state:'READY',version:3});
expect(tx.prepareWorkflowOutbox.create).toHaveBeenCalledTimes(1);
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:'reviewer',expiresAt:expect.any(Date),notes:expect.any(String)})});
tx.orchestrationEvent.findUnique.mockResolvedValue(tx.orchestrationEvent.create.mock.calls[0][0].data);
await service.decide('t',1,command,actor);
expect(tx.prepareWorkflowOutbox.create).toHaveBeenCalledTimes(1);
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledTimes(1);
await expect(service.decide('t',1,{...command,notes:'changed'},actor)).rejects.toMatchObject({status:409});
});
it('future schedule is durable and server expiry extends beyond it',async()=>{
const notBefore=new Date(Date.now()+7*86400000).toISOString();
await service.decide('t',1,{...command,notBefore},actor);
const data=tx.prepareWorkflowOutbox.create.mock.calls[0][0].data;
expect(data.notBefore).toEqual(new Date(notBefore));
expect(tx.orchestrationApprovalDecision.create.mock.calls[0][0].data.expiresAt.getTime()).toBeGreaterThan(Date.parse(notBefore));
});
it('rejected decision never queues',async()=>{
await expect(service.decide('t',1,{...command,decision:'REJECTED'},actor)).resolves.toMatchObject({state:'REJECTED'});
expect(tx.prepareWorkflowOutbox.create).not.toHaveBeenCalled();
});
it.each([new Date(0).toISOString(),new Date(Date.now()+31*86400000).toISOString(),'bad'])('denies invalid schedule %s',async notBefore=>{
await expect(service.decide('t',1,{...command,notBefore},actor)).rejects.toMatchObject({status:400});
expect(tx.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
});
it('legacy explicit expiry remains an expiry, never a schedule',async()=>{
const expiresAt=new Date(Date.now()+60000).toISOString();
await service.decide('t',1,{...command,expiresAt},actor);
expect(tx.orchestrationApprovalDecision.create.mock.calls[0][0].data.expiresAt).toEqual(new Date(expiresAt));
expect(tx.prepareWorkflowOutbox.create.mock.calls[0][0].data.notBefore).toBeNull();
});
it('expired approval is denied with no writes',async()=>{
await expect(service.decide('t',1,{...command,expiresAt:new Date(0).toISOString()},actor)).rejects.toMatchObject({status:400});
expect(tx.prepareWorkflowOutbox.create).not.toHaveBeenCalled();
});
});
@@ -30,7 +30,9 @@ export class GateDecisionDto {
@IsIn(['APPROVED', 'REJECTED']) decision!: 'APPROVED' | 'REJECTED';
@Type(() => Number) @IsInt() @Min(1) expectedVersion!: number;
@IsString() @IsNotEmpty() idempotencyKey!: string;
@IsISO8601() expiresAt!: string;
// Legacy explicit validity cap; scheduling uses notBefore instead.
@IsOptional() @IsISO8601() expiresAt?: string;
@IsOptional() @IsISO8601() notBefore?: string;
@IsOptional() @IsString() notes?: string;
@IsOptional() @IsString() runId?: string;
@IsOptional() @IsHash('sha256') artifactHash?: string;
@@ -200,6 +200,7 @@ export class OrchestrationService {
return prior;
}
if (
typeof dto.expiresAt !== 'string' ||
!Number.isFinite(Date.parse(dto.expiresAt)) ||
new Date(dto.expiresAt) <= new Date()
)
@@ -5,14 +5,14 @@ describe('owner Gate1 hold operation', () => {
let task:any, tx:any, service:PrepareWorkflowService;
beforeEach(()=>{
task={id:'t',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'proposer'}};
tx={$queryRaw:jest.fn().mockResolvedValue([]),orchestrationTask:{findUnique:jest.fn(async()=>task),update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
tx={prepareWorkflowOutbox:{create:jest.fn()},$queryRaw:jest.fn().mockResolvedValue([]),orchestrationTask:{findUnique:jest.fn(async()=>task),update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
service=new PrepareWorkflowService({$transaction:async(cb:any)=>cb(tx)}, {workerIds:[],stageCiIds:[],ownerReviewerIds:['12345678-1234-1234-1234-123456789012']} as any);
});
it('advertises and permits the pinned canonical Admin independent reviewer only',async()=>{
const owner={id:'12345678-1234-1234-1234-123456789012',role:'Admin'};
expect(service.project(task,owner).availableActions).toEqual(['APPROVE_GATE1','REJECT_GATE1','HOLD_GATE1']);
await expect(service.transition('t',hold,owner)).resolves.toMatchObject({state:'GATE1_HELD'});
await expect(service.decide('t',1,{decision:'APPROVED',expectedVersion:3,idempotencyKey:'owner-approve',expiresAt:new Date(Date.now()+60000).toISOString()},owner)).resolves.toMatchObject({state:'GATE1_APPROVED'});
await expect(service.decide('t',1,{decision:'APPROVED',expectedVersion:3,idempotencyKey:'owner-approve',expiresAt:new Date(Date.now()+60000).toISOString()},owner)).resolves.toMatchObject({state:'READY'});
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:owner.id})});
});
it.each(['Admin','SUPER_ADMIN'])('never permits proposer self-review even if allowlisted: %s',async role=>{
@@ -62,6 +62,6 @@ describe('owner Gate1 hold operation', () => {
});
it.each(['APPROVED','REJECTED'])('decides held proposal via existing Gate1 command: %s',async decision=>{
task.state='GATE1_HELD';
await expect(service.decide('t',1,{decision,expectedVersion:2,idempotencyKey:'decision',expiresAt:new Date(Date.now()+60000).toISOString()},reviewer)).resolves.toMatchObject({state:decision==='APPROVED'?'GATE1_APPROVED':'REJECTED'});
await expect(service.decide('t',1,{decision,expectedVersion:2,idempotencyKey:'decision',expiresAt:new Date(Date.now()+60000).toISOString()},reviewer)).resolves.toMatchObject({state:decision==='APPROVED'?'READY':'REJECTED'});
});
});
@@ -10,7 +10,7 @@ describe('configured owner real workflow HTTP authorization',()=>{
const owner='12345678-1234-1234-1234-123456789012';
let app:INestApplication,task:any,tx:any;
beforeAll(async()=>{
tx={$queryRaw:jest.fn(async()=>[]),orchestrationTask:{findUnique:async()=>task,update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
tx={prepareWorkflowOutbox:{create:jest.fn()},$queryRaw:jest.fn(async()=>[]),orchestrationTask:{findUnique:async()=>task,update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
const workflow=new PrepareWorkflowService({$transaction:async(cb:any)=>cb(tx)},{workerIds:[],stageCiIds:[],ownerReviewerIds:[owner]});
const m=await Test.createTestingModule({controllers:[OrchestrationController],providers:[{provide:OrchestrationService,useValue:{findAll:async()=>[task],findOne:async()=>task}},{provide:PrepareWorkflowService,useValue:workflow},RolesGuard]})
.overrideGuard(JwtAuthGuard).useValue({canActivate(c:any){const r=c.switchToHttp().getRequest();if(!r.headers.authorization)return false;r.user={sub:r.headers.authorization,role:'ADMIN'};return true;}}).compile();
@@ -249,15 +249,10 @@ export class PrepareWorkflowService {
throw new ForbiddenException(
'Gate2 decisions and release remain disabled',
);
if (
!['APPROVED', 'REJECTED'].includes(dto.decision) ||
!Number.isFinite(Date.parse(dto.expiresAt)) ||
Date.parse(dto.expiresAt) <= Date.now() ||
Date.parse(dto.expiresAt) > Date.now() + 86400000
)
throw new BadRequestException(
'Decision requires future expiry within 24 hours',
);
if (!['APPROVED', 'REJECTED'].includes(dto.decision))
throw new BadRequestException('Invalid decision');
// Time-sensitive validation happens after replay, so a successful command
// remains replayable after its original schedule or validity window passes.
return this.db.$transaction(async (tx: any) => {
await this.allowed(tx);
const t = await this.task(tx, id);
@@ -268,20 +263,37 @@ export class PrepareWorkflowService {
throw new ForbiddenException('Independent reviewer required');
if (!['GATE1_PENDING', 'GATE1_HELD'].includes(t.state))
throw new ConflictException('Gate1 pending required');
const now = Date.now();
const scheduled = dto.notBefore !== undefined;
const notBefore = scheduled ? new Date(dto.notBefore) : null;
if (scheduled && (dto.decision !== 'APPROVED' || typeof dto.notBefore !== 'string' || !Number.isFinite(notBefore!.getTime()) || notBefore!.getTime() <= now || notBefore!.getTime() > now + 30 * 86400000))
throw new BadRequestException('notBefore must be a future approval schedule within 30 days');
const policyExpiry = (notBefore?.getTime() ?? now) + 86400000;
// Legacy expiresAt is an explicit validity cap, never a scheduled start.
const expiresAt = dto.expiresAt === undefined ? new Date(policyExpiry) : new Date(dto.expiresAt);
if (!Number.isFinite(expiresAt.getTime()) || expiresAt.getTime() <= (notBefore?.getTime() ?? now) || expiresAt.getTime() > policyExpiry)
throw new BadRequestException('Approval expiry must cover schedule and remain within backend validity policy');
if (dto.notes !== undefined && (typeof dto.notes !== 'string' || dto.notes.length > 2000))
throw new BadRequestException('Audit notes must be at most 2000 characters');
if (dto.decision === 'APPROVED') {
await this.allowed(tx, true);
await tx.prepareWorkflowOutbox.create({data:{taskId:id,notBefore}});
}
await tx.orchestrationApprovalDecision.create({
data: {
taskId: id,
gate: 1,
decision: dto.decision,
actorId: a.id,
expiresAt: new Date(dto.expiresAt),
expiresAt,
notes: dto.notes?.trim() || `Owner ${dto.decision.toLowerCase()} Gate1`,
idempotencyKey: `decision:${dto.idempotencyKey}`,
},
});
return this.event(
tx,
t,
dto.decision === 'APPROVED' ? 'GATE1_APPROVED' : 'REJECTED',
dto.decision === 'APPROVED' ? 'READY' : 'REJECTED',
a,
dto.idempotencyKey,
'PREPARE_GATE1',
@@ -289,14 +301,32 @@ export class PrepareWorkflowService {
);
});
}
/** Recovery of a stored approval by its provisioned proposal worker, not a new decision. */
async enqueueApproved(a: WorkflowIdentity, dto: {taskId:string;expectedVersion:number;idempotencyKey:string}) {
this.worker(a);
return this.db.$transaction(async (tx:any)=>{
await this.allowed(tx,true);
const t=await this.task(tx,dto.taskId);
if(t.metadata.createdBy!==a.id) throw new ForbiddenException('Proposal worker required');
const fingerprint=JSON.stringify({dto,a});
if(await this.replay(tx,dto.idempotencyKey,fingerprint)) return t;
this.version(t,dto);
if(t.state!=='GATE1_APPROVED') throw new ConflictException('Stored Gate1 approval required');
const approval=await tx.orchestrationApprovalDecision.findFirst({where:{taskId:t.id,gate:1},orderBy:{createdAt:'desc'}});
if(!approval || approval.decision!=='APPROVED' || approval.actorId===a.id || approval.expiresAt<=new Date())
throw new ForbiddenException('Valid independent Gate1 required');
await tx.prepareWorkflowOutbox.create({data:{taskId:t.id}});
return this.event(tx,t,'READY',a,dto.idempotencyKey,'PREPARE_APPROVED_QUEUED',fingerprint);
});
}
async claim(a: WorkflowIdentity, id?: string): Promise<any> {
this.worker(a);
return this.db.$transaction(async (tx: any) => {
await this.allowed(tx, true);
// Lock order is task then outbox for every worker operation. SKIP LOCKED permits competing workers.
const rows: any[] = id
? await tx.$queryRaw`SELECT t.id FROM orchestration_tasks t JOIN prepare_workflow_outbox o ON o.task_id=t.id WHERE t.id=${id}::uuid AND o.status='PENDING' AND (o.lease_until IS NULL OR o.lease_until < now()) ORDER BY o.created_at FOR UPDATE OF t SKIP LOCKED LIMIT 1`
: await tx.$queryRaw`SELECT t.id FROM orchestration_tasks t JOIN prepare_workflow_outbox o ON o.task_id=t.id WHERE o.status='PENDING' AND (o.lease_until IS NULL OR o.lease_until < now()) ORDER BY o.created_at FOR UPDATE OF t SKIP LOCKED LIMIT 1`;
? await tx.$queryRaw`SELECT t.id FROM orchestration_tasks t JOIN prepare_workflow_outbox o ON o.task_id=t.id WHERE t.id=${id}::uuid AND t.state IN ('READY','RUNNING') AND o.status='PENDING' AND (o.not_before IS NULL OR o.not_before <= now()) AND (o.lease_until IS NULL OR o.lease_until < now()) ORDER BY o.created_at FOR UPDATE OF t SKIP LOCKED LIMIT 1`
: await tx.$queryRaw`SELECT t.id FROM orchestration_tasks t JOIN prepare_workflow_outbox o ON o.task_id=t.id WHERE t.state IN ('READY','RUNNING') AND o.status='PENDING' AND (o.not_before IS NULL OR o.not_before <= now()) AND (o.lease_until IS NULL OR o.lease_until < now()) ORDER BY o.created_at FOR UPDATE OF t SKIP LOCKED LIMIT 1`;
if (!rows.length) return null;
const t = await this.task(tx, rows[0].id);
const o = await tx.prepareWorkflowOutbox.findUnique({
+4 -33
View File
@@ -56,15 +56,7 @@ const enabled = process.env.WORKFLOW_DB === '1';
},
reviewer,
);
return service.transition(
t.id,
{
toState: WorkflowState.READY,
expectedVersion: t.version,
idempotencyKey: `ready-${t.id}`,
},
admin,
);
return t;
}
afterAll(async () => {
await db.$disconnect();
@@ -87,7 +79,7 @@ const enabled = process.env.WORKFLOW_DB === '1';
expect(JSON.stringify(events[0].payload)).toContain('Need source review');
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(0);
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(0);
await expect(service.transition(t.id, {...command,expectedVersion:held.version,reason:'changed'},reviewer)).rejects.toMatchObject({status:409});
await expect(service.transition(t.id, {...command,idempotencyKey:events[0].idempotencyKey,expectedVersion:held.version,reason:'changed'},reviewer)).rejects.toMatchObject({status:409});
const decision = await service.decide(t.id,1,{decision:'REJECTED',expectedVersion:held.version,idempotencyKey:`decline-held-${t.id}`,expiresAt:new Date(Date.now()+60000).toISOString()},reviewer);
expect(decision.state).toBe('REJECTED');
});
@@ -340,15 +332,7 @@ const enabled = process.env.WORKFLOW_DB === '1';
},
reviewer,
);
await service.transition(
t.id,
{
toState: 'READY',
expectedVersion: current.version,
idempotencyKey: `q-${t.id}`,
},
admin,
);
expect(current.state).toBe('READY');
const first = await service.claim(worker, t.id);
for (let i = 0; i < 2; i++) {
await db.prepareWorkflowOutbox.update({
@@ -462,20 +446,7 @@ const enabled = process.env.WORKFLOW_DB === '1';
},
reviewer,
);
const results = await Promise.allSettled(
['one', 'two'].map((k) =>
service.transition(
t.id,
{
toState: 'READY',
expectedVersion: t.version,
idempotencyKey: `${k}-${t.id}`,
},
admin,
),
),
);
expect(results.filter((r) => r.status === 'fulfilled')).toHaveLength(1);
expect(t.state).toBe('READY');
expect(
await db.prepareWorkflowOutbox.count({ where: { taskId: t.id } }),
).toBe(1);