11import { describe , expect , it } from "vitest" ;
2+ import { createQaBusState } from "./bus-state.js" ;
23import { readQaScenarioById , readQaScenarioExecutionConfig } from "./scenario-catalog.js" ;
34import { readFlowAssertExpression , requireFlowScenario } from "./scenario-catalog.test-utils.js" ;
5+ import { runLoadedScenarioFlow } from "./scenario-flow-runner.test-support.js" ;
46
57describe ( "qa scenario catalog causality" , ( ) => {
68 it ( "loads live gateway sentinel scenarios for harness self-health" , ( ) => {
@@ -144,24 +146,30 @@ describe("qa scenario catalog causality", () => {
144146 "thread-memory-isolation" ,
145147 "poll" ,
146148 "finalRequest.toolOutputCallId === searchResultRequest.plannedToolCallId" ,
149+ null ,
147150 ] ,
148151 [
149152 "memory-tools-channel-context" ,
150153 "poll" ,
151154 "finalRequest.toolOutputCallId === searchResultRequest.plannedToolCallId" ,
155+ "durableChannelLifecycle" ,
152156 ] ,
153157 [
154158 "agent-tool-consumption" ,
155159 "immediate" ,
156160 "getResultRequest.toolOutputCallId === searchResultRequest.plannedToolCallId" ,
161+ null ,
157162 ] ,
158163 ] as const ) (
159164 "asserts the complete memory tool chain before %s delivery" ,
160- ( scenarioId , requestCollectionMode , finalLinkNeedle ) => {
165+ ( scenarioId , requestCollectionMode , finalLinkNeedle , durableWaitSaveAs ) => {
161166 const scenario = requireFlowScenario ( readQaScenarioById ( scenarioId ) ) ;
162167 const actions = scenario . execution . flow ?. steps [ 0 ] ?. actions ?? [ ] ;
163- const outboundIndex = actions . findIndex (
164- ( action ) => ( action as { call ?: string } ) . call === "waitForOutboundMessage" ,
168+ const outboundIndex = actions . findIndex ( ( action ) =>
169+ durableWaitSaveAs
170+ ? ( action as { call ?: string } ) . call === "waitForCondition" &&
171+ ( action as { saveAs ?: string } ) . saveAs === durableWaitSaveAs
172+ : ( action as { call ?: string } ) . call === "waitForOutboundMessage" ,
165173 ) ;
166174 const requestCollectionIndex = actions . findIndex ( ( action ) =>
167175 requestCollectionMode === "poll"
@@ -193,6 +201,15 @@ describe("qa scenario catalog causality", () => {
193201 expect ( finalRequestAssertIndex , scenarioId ) . toBeGreaterThan ( searchResultAssertIndex ) ;
194202 expect ( outboundIndex , scenarioId ) . toBeGreaterThan ( finalRequestAssertIndex ) ;
195203
204+ if ( durableWaitSaveAs ) {
205+ const durableWait = actions [ outboundIndex ] as
206+ | { args ?: Array < { lambda ?: { expr ?: string } } > }
207+ | undefined ;
208+ const durableExpr = durableWait ?. args ?. [ 0 ] ?. lambda ?. expr ?? "" ;
209+ expect ( durableExpr , scenarioId ) . toContain ( "event.cursor < finalSent.cursor" ) ;
210+ expect ( durableExpr , scenarioId ) . toContain ( "event.cursor < previewRetired.cursor" ) ;
211+ }
212+
196213 if ( requestCollectionMode === "poll" ) {
197214 const requestPoll = actions [ requestCollectionIndex ] as
198215 | { args ?: Array < { lambda ?: { expr ?: string } } > }
@@ -212,4 +229,108 @@ describe("qa scenario catalog causality", () => {
212229 }
213230 } ,
214231 ) ;
232+
233+ it . each ( [
234+ [ "memory-tools-channel-context" , "durableChannelLifecycle" , 30000 ] ,
235+ [ "agent-progress-evidence" , "durableCompletionLifecycle" , 60000 ] ,
236+ ] as const ) ( "keeps the policy-aware durable delivery budget for %s" , ( scenarioId , saveAs , ms ) => {
237+ const scenario = requireFlowScenario ( readQaScenarioById ( scenarioId ) ) ;
238+ const actions = scenario . execution . flow ?. steps [ 0 ] ?. actions ?? [ ] ;
239+ const durableWait = actions . find (
240+ ( action ) =>
241+ ( action as { call ?: string } ) . call === "waitForCondition" &&
242+ ( action as { saveAs ?: string } ) . saveAs === saveAs ,
243+ ) ;
244+
245+ expect ( durableWait , scenarioId ) . toMatchObject ( {
246+ args : [ expect . any ( Object ) , { expr : `liveTurnTimeoutMs(env, ${ ms } )` } ] ,
247+ } ) ;
248+ } ) ;
249+
250+ it . each ( [
251+ {
252+ scenarioId : "memory-tools-channel-context" ,
253+ saveAs : "durableChannelLifecycle" ,
254+ cursorName : "busCursorBeforeInbound" ,
255+ conversationKey : "channelId" ,
256+ markerKey : "expectedNeedle" ,
257+ targetPrefix : "channel" ,
258+ } ,
259+ {
260+ scenarioId : "agent-progress-evidence" ,
261+ saveAs : "durableCompletionLifecycle" ,
262+ cursorName : "busCursorBefore" ,
263+ conversationKey : "conversationId" ,
264+ markerKey : "completionText" ,
265+ targetPrefix : "dm" ,
266+ } ,
267+ ] as const ) ( "isolates $scenarioId durable lifecycle evidence by account" , async ( fixture ) => {
268+ const scenario = requireFlowScenario ( readQaScenarioById ( fixture . scenarioId ) ) ;
269+ const actions = scenario . execution . flow ?. steps [ 0 ] ?. actions ?? [ ] ;
270+ const durableWaitIndex = actions . findIndex (
271+ ( action ) =>
272+ ( action as { call ?: string } ) . call === "waitForCondition" &&
273+ ( action as { saveAs ?: string } ) . saveAs === fixture . saveAs ,
274+ ) ;
275+ const cardinalityAssertIndex = actions . findIndex ( ( action ) =>
276+ readFlowAssertExpression ( action ) . includes (
277+ fixture . scenarioId === "memory-tools-channel-context"
278+ ? "visibleChannelOutbounds.length === 1"
279+ : "completionMessages.length === 1" ,
280+ ) ,
281+ ) ;
282+ expect ( durableWaitIndex , fixture . scenarioId ) . toBeGreaterThanOrEqual ( 0 ) ;
283+ expect ( cardinalityAssertIndex , fixture . scenarioId ) . toBeGreaterThan ( durableWaitIndex ) ;
284+ if ( durableWaitIndex < 0 || cardinalityAssertIndex <= durableWaitIndex ) {
285+ throw new Error ( `missing durable lifecycle assertion path for ${ fixture . scenarioId } ` ) ;
286+ }
287+ const postWaitAssertionPath = actions . slice ( durableWaitIndex , cardinalityAssertIndex + 1 ) ;
288+
289+ const config = scenario . execution . config ?? { } ;
290+ const conversationId = String ( config [ fixture . conversationKey ] ) ;
291+ const marker = String ( config [ fixture . markerKey ] ) ;
292+ const target = `${ fixture . targetPrefix } :${ conversationId } ` ;
293+ const state = createQaBusState ( ) ;
294+ for ( const accountId of [ "foreign" , "qa-channel" ] ) {
295+ const preview = state . addOutboundMessage ( { accountId, to : target , text : marker } ) ;
296+ state . deleteMessage ( { accountId, messageId : preview . id } ) ;
297+ state . addOutboundMessage ( { accountId, to : target , text : marker } ) ;
298+ }
299+ const foreignKind = fixture . targetPrefix === "dm" ? "channel" : "dm" ;
300+ const foreignKindTarget = `${ foreignKind } :${ conversationId } ` ;
301+ const foreignKindPreview = state . addOutboundMessage ( {
302+ accountId : "qa-channel" ,
303+ to : foreignKindTarget ,
304+ text : marker ,
305+ } ) ;
306+ state . deleteMessage ( { accountId : "qa-channel" , messageId : foreignKindPreview . id } ) ;
307+ state . addOutboundMessage ( {
308+ accountId : "qa-channel" ,
309+ to : foreignKindTarget ,
310+ text : marker ,
311+ } ) ;
312+
313+ await expect (
314+ runLoadedScenarioFlow ( fixture . scenarioId , {
315+ state,
316+ flow : {
317+ steps : [
318+ {
319+ name : "keeps foreign account lifecycle evidence isolated" ,
320+ actions : [
321+ { set : "outboundStartIndex" , value : { expr : "0" } } ,
322+ { set : fixture . cursorName , value : { expr : "0" } } ,
323+ ...postWaitAssertionPath ,
324+ {
325+ assert : {
326+ expr : `${ fixture . saveAs } .message.accountId === transport.accountId` ,
327+ } ,
328+ } ,
329+ ] ,
330+ } ,
331+ ] ,
332+ } ,
333+ } ) ,
334+ ) . resolves . toMatchObject ( { status : "pass" } ) ;
335+ } ) ;
215336} ) ;
0 commit comments