gongxiangp2/apps/detail-agent-pi/test/workflow-not-dispatched.test.mjs

117 lines
5.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { executeWorkflow } from '../src/workflow-execute.mjs';
import { BridgeError } from '../src/bridge-error.mjs';
const args = {version:1,intent:'generate_candidate',outputId:'detail',slotId:'hero',productVariantId:'walnut'};
function fixture(change = value => value) {
const calls = [], events = [];
const identity = {scope:'scope',project:'project'};
const host = {turnEpoch:1,currentWorkflow:{revisions:{canvas:17}},
canvas:{run:'run',documentId:7,identity:{accountId:'account',origin:'https://platform.test'},
async workflowExecute({operationId}) {
calls.push(operationId);
return change({operationId,phase:'not_dispatched',accepted:false,dispatched:false,
run:'run',documentId:7,baseRevision:17,accountId:'account',origin:'https://platform.test',
projectLocalId:'https://platform.test|account|project',
error:{code:'local_precondition_failed',message:'local precondition failed'}});
},
async workflowPoll() { assert.fail('a rejected operation must not be polled'); }},
store:{identity,async appendEvent(event) { events.push(event); }},
async readWorkflow() { assert.fail('a rejected operation must not refresh as completed'); }};
return {host,calls,events};
}
test('a known local rejection returns the host reason and lets the agent submit again',async()=>{
const x = fixture();
await assert.rejects(executeWorkflow(x.host,args),/未派发.*local precondition failed/);
assert.equal(x.calls.length,1);
assert.equal(x.host.canvasOperations.original,undefined);
assert.equal(x.host.canvasOperations.active,null);
assert.deepEqual(x.events.map(event=>event.type),['workflow_operation_not_dispatched']);
assert.equal(x.events[0].operationId,x.calls[0]);
await assert.rejects(executeWorkflow(x.host,args),/未派发.*local precondition failed/);
assert.equal(x.calls.length,2);
});
test('preserves a new layer lock as the reason a write was not dispatched',async()=>{
const x = fixture(receipt=>({...receipt,error:{...receipt.error,reason:'layer_newly_locked'}}));
await assert.rejects(executeWorkflow(x.host,args),{category:'layer_newly_locked',dispatched:false});
assert.equal(x.events[0].code,'local_precondition_failed');
assert.equal(x.events[0].reason,'layer_newly_locked');
});
test('a rejection for another operation cannot clear a possibly dispatched operation',async()=>{
const x = fixture(receipt=>({...receipt,operationId:'other'}));
await assert.rejects(executeWorkflow(x.host,args));
assert.equal(x.host.canvasOperations.original.operationId,x.calls[0]);
assert.equal(x.events.length,0);
});
test('a user edit after the workflow read leaves an authoritative rejection known not dispatched',async()=>{
const x = fixture(receipt=>({...receipt,baseRevision:18}));
await assert.rejects(executeWorkflow(x.host,args),/未派发/);
assert.equal(x.host.canvasOperations.original,undefined);
assert.equal(x.calls.length,1);
assert.equal(x.events[0].baseRevision,18);
});
test('a transport error keeps the original unknown identity even when its words say not sent',async()=>{
const x = fixture();
x.host.canvas.workflowExecute=async input=>{x.calls.push(input.operationId);throw Error('not_dispatched: 用户已接手,未执行操作');};
await assert.rejects(executeWorkflow(x.host,args));
assert.equal(x.host.canvasOperations.original.operationId,x.calls[0]);
assert.equal(x.host.canvasOperations.original.run,'run');
assert.equal(x.events.length,0);
});
test('a rejection after waiting for a save is still known not dispatched',async()=>{
const x = fixture(), reject = x.host.canvas.workflowExecute;
x.host.canvas.read=async()=>({revision:17,conversationActive:true,writeBlocked:false});
x.host.canvas.workflowExecute=async input=>x.calls.length===0
? (x.calls.push(input.operationId),{operationId:input.operationId,phase:'waiting_save',dispatched:false,revision:17})
: reject(input);
await assert.rejects(executeWorkflow(x.host,args,undefined,{pause:async()=>{}}),/未派发/);
assert.equal(new Set(x.calls).size,1);
assert.equal(x.calls.length,2);
assert.equal(x.host.canvasOperations.original,undefined);
});
test('a late authoritative rejection cannot stop or contaminate a new user turn',async()=>{
const x = fixture();
let release;
const rejected = x.host.canvas.workflowExecute;
x.host.canvas.workflowExecute=async input=>{
const receipt=await rejected(input);
return new Promise(resolve=>{release=()=>resolve(receipt);});
};
const task=executeWorkflow(x.host,args);
await new Promise(resolve=>setImmediate(resolve));
x.host.turnEpoch=2;x.host.canvas.run='new-run';
release();
await assert.rejects(task,/原回合已结束/);
assert.equal(x.host.canvasOperations.original,undefined);
assert.equal(x.events.length,0);
});
test('structured host rejection before dispatch does not hold the slot',async()=>{
const x=fixture();
const reject=x.host.canvas.workflowExecute;
x.host.canvas.workflowExecute=async input=>{
if(x.calls.length)return reject(input);
x.calls.push(input.operationId);
throw new BridgeError('目标图层已变化','revision_conflict',{dispatched:false,currentRevision:18});
};
await assert.rejects(executeWorkflow(x.host,args),{category:'revision_conflict',dispatched:false});
assert.deepEqual(x.host.canvasOperations.pending,[]);
await assert.rejects(executeWorkflow(x.host,args),/未派发/);
assert.equal(new Set(x.calls).size,2);
});
test('a matching receipt flagged as not dispatched is terminal even if its phase is unknown',async()=>{
const x=fixture(receipt=>({...receipt,phase:'unknown',error:{code:'scope_changed',message:'画布已切换'}}));
await assert.rejects(executeWorkflow(x.host,args),{category:'scope_changed',dispatched:false});
assert.deepEqual(x.host.canvasOperations.pending,[]);
assert.equal(x.events[0].type,'workflow_operation_not_dispatched');
});