1- import { noMaestroIncludeSources } from '@agent-device/replay-port/replay-script-source-fixtures' ;
21import { beforeEach , expect , test , vi } from 'vitest' ;
32import { mkdtempForTestSync } from '@agent-device/test-kit/tmp-dir' ;
43
@@ -13,23 +12,12 @@ vi.mock('../../../core/snapshot-interactor-capture.ts', () => ({
1312import fs from 'node:fs' ;
1413import path from 'node:path' ;
1514import { stringify } from 'yaml' ;
16- import {
17- executeMaestroFlow ,
18- inspectMaestroFlow ,
19- type MaestroFailedAction ,
20- type MaestroRuntimeCommand ,
21- type MaestroRuntimePort ,
22- } from '@agent-device/maestro' ;
23- import {
24- buildTypedMaestroFailureReportProjection ,
25- buildTypedMaestroFailureResponse ,
26- } from '@agent-device/replay-port/session-replay-maestro-failure' ;
15+ import type { MaestroRuntimeCommand } from '@agent-device/maestro' ;
2716import { runReplayForTest } from '../replay-runtime/replay-command-fixture.ts' ;
2817import { SessionStore } from '../../../core/session-store.ts' ;
2918import { captureSnapshotWithInteractor } from '../../../core/snapshot-interactor-capture.ts' ;
3019import { makeIosSession } from '../../../__tests__/support/session-factories.ts' ;
3120import { baseReplayRequest as baseReq } from '../replay-runtime/session-replay-runtime.fixtures.ts' ;
32- import { replaySessionForTest } from '../replay-divergence/replay-session-fixture.ts' ;
3321import type { SnapshotNode } from '@agent-device/kernel/snapshot' ;
3422import {
3523 captureSnapshotThroughLegacyDispatchFixture ,
@@ -47,30 +35,35 @@ beforeEach(() => {
4735 mockCaptureSnapshotWithInteractor . mockImplementation ( captureSnapshotThroughLegacyDispatchFixture ) ;
4836} ) ;
4937
38+ /**
39+ * Runs a one-step flow whose action fails, so the divergence report is built the way the daemon
40+ * builds it: `nodes` is the screen captured after the failure, `engineNodes` what the flow's own
41+ * target lookup sees before it.
42+ */
5043async function buildFailureScenario (
5144 command : MaestroRuntimeCommand ,
5245 nodes : SnapshotNode [ ] ,
53- options : { warnings ?: readonly string [ ] } = { } ,
54- ) : Promise < {
55- response : Extract < Awaited < ReturnType < typeof buildTypedMaestroFailureResponse > > , { ok : false } > ;
56- sessionStore : SessionStore ;
57- sessionName : string ;
58- } > {
46+ options : { engineNodes ?: SnapshotNode [ ] } = { } ,
47+ ) {
5948 const root = mkdtempForTestSync ( 'agent-device-maestro-suggestions-' ) ;
6049 const sessionName = 'default' ;
6150 const sessionStore = new SessionStore ( path . join ( root , 'sessions' ) ) ;
6251 sessionStore . publish ( sessionName , makeIosSession ( sessionName ) ) ;
63- const replaySession = replaySessionForTest ( sessionStore , sessionName ) ;
52+ const flowPath = path . join ( root , 'flow.yaml' ) ;
53+ fs . writeFileSync ( flowPath , maestroFlowForCommand ( command ) ) ;
6454 mockDispatchCommand . mockResolvedValue ( { nodes, truncated : false , backend : 'xctest' } ) ;
65- const failure = await captureMaestroFailure ( command , path . join ( root , 'flow.yaml' ) ) ;
66- const response = await buildTypedMaestroFailureResponse ( {
67- error : { code : 'COMMAND_FAILED' , message : 'typed Maestro action failed' } ,
68- failure : options . warnings ? { ...failure , warnings : options . warnings } : failure ,
69- replayPath : path . join ( root , 'flow.yaml' ) ,
70- req : baseReq ( { flags : { replayBackend : 'maestro' , platform : 'ios' } } ) ,
71- sessionStore : replaySession . store ,
72- observationStore : replaySession . observationStore ,
55+ const response = await runReplayForTest ( {
56+ req : baseReq ( {
57+ positionals : [ flowPath ] ,
58+ flags : { replayBackend : 'maestro' , platform : 'ios' } ,
59+ } ) ,
60+ sessionName,
7361 logPath : path . join ( root , 'daemon.log' ) ,
62+ sessionStore,
63+ invoke : async ( req ) =>
64+ req . command === 'snapshot'
65+ ? { ok : true , data : { nodes : options . engineNodes ?? nodes } }
66+ : { ok : false , error : { code : 'COMMAND_FAILED' , message : 'typed Maestro action failed' } } ,
7467 } ) ;
7568 if ( response . ok ) throw new Error ( 'expected typed Maestro failure response' ) ;
7669 return { response, sessionStore, sessionName } ;
@@ -79,85 +72,11 @@ async function buildFailureScenario(
7972async function buildFailureResponse (
8073 command : MaestroRuntimeCommand ,
8174 nodes : SnapshotNode [ ] ,
82- options : { warnings ?: readonly string [ ] } = { } ,
83- ) : Promise < Extract < Awaited < ReturnType < typeof buildTypedMaestroFailureResponse > > , { ok : false } > > {
75+ options : { engineNodes ?: SnapshotNode [ ] } = { } ,
76+ ) {
8477 return ( await buildFailureScenario ( command , nodes , options ) ) . response ;
8578}
8679
87- test ( 'typed Maestro failure response carries warnings accumulated before the failing step (#2560)' , async ( ) => {
88- const response = await buildFailureResponse (
89- {
90- kind : 'tapOn' as const ,
91- source : { path : '/flows/login.yaml' , line : 4 } ,
92- target : { space : 'target' as const , selector : { id : 'save' } } ,
93- } ,
94- [ ] ,
95- { warnings : [ 'Optional Maestro assertVisible skipped at line 2: no match' ] } ,
96- ) ;
97-
98- if ( ! response . error . details ) throw new Error ( 'expected error details on the failure response' ) ;
99- const details = response . error . details ;
100- expect ( details . warnings ) . toEqual ( [ 'Optional Maestro assertVisible skipped at line 2: no match' ] ) ;
101- expect ( ( details . divergence as { kind : string } ) . kind ) . toBe ( 'action-failure' ) ;
102- } ) ;
103-
104- test ( 'typed Maestro failure response omits an empty warnings channel' , async ( ) => {
105- const response = await buildFailureResponse (
106- {
107- kind : 'tapOn' as const ,
108- source : { path : '/flows/login.yaml' , line : 4 } ,
109- target : { space : 'target' as const , selector : { id : 'save' } } ,
110- } ,
111- [ ] ,
112- { warnings : [ ] } ,
113- ) ;
114-
115- expect ( 'warnings' in ( response . error . details ?? { } ) ) . toBe ( false ) ;
116- } ) ;
117-
118- test ( 'typed Maestro failure projection keeps action and source provenance' , async ( ) => {
119- const command = {
120- kind : 'tapOn' as const ,
121- source : { path : '/flows/login.yaml' , line : 4 } ,
122- target : { space : 'target' as const , selector : { id : 'save' } } ,
123- } ;
124- const request = baseReq ( { flags : { replayBackend : 'maestro' } } ) ;
125- const failure = await captureMaestroFailure ( command , command . source . path ) ;
126- const projection = buildTypedMaestroFailureReportProjection ( failure , request ) ;
127-
128- expect ( projection . command ) . toEqual ( { kind : 'tapOn' } ) ;
129- expect ( projection . source ) . toEqual ( { path : command . source . path , line : 3 } ) ;
130- expect ( projection . progress ) . toEqual ( { command : 'tapOn' , value : 'save' } ) ;
131- expect ( projection . action ) . toEqual ( {
132- command : 'click' ,
133- positionals : [ 'save' ] ,
134- flags : request . flags ,
135- } ) ;
136- expect ( Object . keys ( projection . action ) ) . toEqual ( [ 'command' , 'positionals' , 'flags' ] ) ;
137- } ) ;
138-
139- async function captureMaestroFailure (
140- command : MaestroRuntimeCommand ,
141- sourcePath : string ,
142- ) : Promise < MaestroFailedAction > {
143- const flow = inspectMaestroFlow ( maestroFlowForCommand ( command ) , sourcePath ) ;
144- const error = new Error ( 'typed Maestro action failed' ) ;
145- const port : MaestroRuntimePort = {
146- execute : async ( ) => {
147- throw error ;
148- } ,
149- observe : async ( ) => {
150- throw error ;
151- } ,
152- } ;
153- const outcome = await executeMaestroFlow ( flow , port , {
154- platform : 'ios' ,
155- readSource : noMaestroIncludeSources ,
156- } ) ;
157- if ( outcome . ok || ! outcome . failure ) throw new Error ( 'expected typed Maestro failure' ) ;
158- return outcome . failure ;
159- }
160-
16180function maestroFlowForCommand ( command : MaestroRuntimeCommand ) : string {
16281 const authored =
16382 command . kind === 'inputText'
@@ -378,7 +297,7 @@ test('typed Maestro failure publishes exactly the refs exposed by its divergence
378297 source : { path : '/flows/actions.yaml' , line : 4 } ,
379298 target : { space : 'target' as const , selector : { text : 'Missing' } } ,
380299 } satisfies Extract < MaestroRuntimeCommand , { kind : 'tapOn' } > ;
381- const scenario = await buildFailureScenario ( command , [
300+ const screen : SnapshotNode [ ] = [
382301 {
383302 ref : 'e1' ,
384303 index : 0 ,
@@ -394,7 +313,20 @@ test('typed Maestro failure publishes exactly the refs exposed by its divergence
394313 rect : { x : 16 , y : 40 , width : 140 , height : 44 } ,
395314 hittable : true ,
396315 } ,
397- ] ) ;
316+ ] ;
317+ const scenario = await buildFailureScenario ( command , screen , {
318+ engineNodes : [
319+ ...screen ,
320+ {
321+ index : 2 ,
322+ parentIndex : 0 ,
323+ label : 'Missing' ,
324+ type : 'Button' ,
325+ rect : { x : 16 , y : 96 , width : 140 , height : 44 } ,
326+ hittable : true ,
327+ } ,
328+ ] ,
329+ } ) ;
398330 const divergence = scenario . response . error . details ?. divergence as {
399331 screen : { state : string ; refs : Array < { ref : string } > } ;
400332 } ;
0 commit comments