@@ -16,10 +16,6 @@ vi.mock('../../../core/dispatch.ts', async (importOriginal) => {
1616 } ;
1717} ) ;
1818
19- vi . mock ( '../../selector-runtime.ts' , ( ) => ( {
20- dispatchFindReadOnlyViaRuntime : vi . fn ( async ( ) => null ) ,
21- } ) ) ;
22-
2319import { dispatchCommand } from '../../../core/dispatch.ts' ;
2420
2521const mockDispatch = vi . mocked ( dispatchCommand ) ;
@@ -164,7 +160,7 @@ test('handleFindCommands click prefers on-screen duplicate text matches', async
164160 expect ( invokeCalls [ 0 ] ! . positionals ?. [ 0 ] ) . toBe ( '@e3' ) ;
165161} ) ;
166162
167- test ( 'handleFindCommands click fails without daemon retry when quality verdict is sparse' , async ( ) => {
163+ test ( 'handleFindCommands click tries query-scoped full retry before failing sparse verdict ' , async ( ) => {
168164 const session = makeSession ( 'default' ) ;
169165 session . snapshot = {
170166 nodes : [
@@ -223,17 +219,21 @@ test('handleFindCommands click fails without daemon retry when quality verdict i
223219 } ,
224220 } ) ;
225221 const snapshotCalls = mockDispatch . mock . calls . filter ( ( call ) => call [ 1 ] === 'snapshot' ) ;
226- expect ( snapshotCalls ) . toHaveLength ( 1 ) ;
222+ expect ( snapshotCalls ) . toHaveLength ( 2 ) ;
227223 expect ( snapshotCalls [ 0 ] ! [ 4 ] ) . toMatchObject ( {
228224 snapshotInteractiveOnly : true ,
229225 snapshotCompact : true ,
230226 } ) ;
227+ expect ( snapshotCalls [ 1 ] ! [ 4 ] ) . toMatchObject ( {
228+ snapshotInteractiveOnly : false ,
229+ snapshotCompact : false ,
230+ snapshotScope : 'Search' ,
231+ } ) ;
231232} ) ;
232233
233- test ( 'handleFindCommands wait preserves sparse verdict through snapshot cache hits' , async ( ) => {
234- mockDispatch . mockImplementation ( async ( _device , command ) => {
235- if ( command !== 'snapshot' ) return { } ;
236- return {
234+ test ( 'handleFindCommands click uses query-scoped full retry when sparse verdict recovers' , async ( ) => {
235+ const snapshotResponses = [
236+ {
237237 backend : 'xctest' ,
238238 quality : {
239239 state : 'sparse' ,
@@ -248,23 +248,50 @@ test('handleFindCommands wait preserves sparse verdict through snapshot cache hi
248248 rect : { x : 0 , y : 0 , width : 0 , height : 0 } ,
249249 } ,
250250 ] ,
251- } ;
251+ } ,
252+ {
253+ backend : 'xctest' ,
254+ quality : {
255+ state : 'healthy' ,
256+ backend : 'tree' ,
257+ } ,
258+ nodes : [
259+ {
260+ index : 0 ,
261+ type : 'Application' ,
262+ hittable : false ,
263+ rect : { x : 0 , y : 0 , width : 390 , height : 844 } ,
264+ } ,
265+ {
266+ index : 1 ,
267+ type : 'Button' ,
268+ label : 'Search' ,
269+ hittable : true ,
270+ rect : { x : 80 , y : 792 , width : 78 , height : 48 } ,
271+ parentIndex : 0 ,
272+ } ,
273+ ] ,
274+ } ,
275+ ] ;
276+ mockDispatch . mockImplementation ( async ( _device , command ) => {
277+ if ( command !== 'snapshot' ) return { } ;
278+ return snapshotResponses . shift ( ) ?? { nodes : [ ] } ;
252279 } ) ;
253280
254- const { response } = await runFindClickScenario ( {
255- positionals : [ 'text ' , 'Never appears' , 'wait' , '350 '] ,
281+ const { response, invokeCalls } = await runFindClickScenario ( {
282+ positionals : [ 'Search ' , 'click ' ] ,
256283 } ) ;
257284
258- expect ( response . ok ) . toBe ( false ) ;
259- expect ( ! response . ok && response . error ) . toMatchObject ( {
260- code : 'COMMAND_FAILED' ,
261- message : 'find could not read the current accessibility tree' ,
262- details : {
263- reason : 'sparse tree' ,
264- hint : expect . stringContaining ( 'snapshot quality verdict is sparse' ) ,
265- } ,
285+ expect ( response . ok ) . toBe ( true ) ;
286+ expect ( invokeCalls [ 0 ] ! . positionals ?. [ 0 ] ) . toBe ( '@e1' ) ;
287+ expect ( response . ok ? response . data : undefined ) . toMatchObject ( { x : 119 , y : 816 } ) ;
288+ const snapshotCalls = mockDispatch . mock . calls . filter ( ( call ) => call [ 1 ] === 'snapshot' ) ;
289+ expect ( snapshotCalls ) . toHaveLength ( 2 ) ;
290+ expect ( snapshotCalls [ 1 ] ! [ 4 ] ) . toMatchObject ( {
291+ snapshotInteractiveOnly : false ,
292+ snapshotCompact : false ,
293+ snapshotScope : 'Search' ,
266294 } ) ;
267- expect ( mockDispatch ) . toHaveBeenCalledTimes ( 1 ) ;
268295} ) ;
269296
270297test ( 'handleFindCommands click retries full snapshot for legacy iOS sparse shape without verdict' , async ( ) => {
@@ -614,7 +641,59 @@ test('handleFindCommands wait bypasses snapshot cache while Android freshness re
614641 expect ( mockDispatch ) . toHaveBeenCalledTimes ( 2 ) ;
615642} ) ;
616643
617- test ( 'handleFindCommands wait reuses the snapshot cache while polling' , async ( ) => {
644+ test ( 'handleFindCommands wait reports sparse verdict through selector runtime route' , async ( ) => {
645+ const session = makeSession ( 'default' ) ;
646+ session . snapshot = {
647+ nodes : [
648+ {
649+ index : 0 ,
650+ ref : 'e1' ,
651+ type : 'Button' ,
652+ label : 'Previous screen action' ,
653+ rect : { x : 24 , y : 600 , width : 180 , height : 52 } ,
654+ } ,
655+ ] ,
656+ createdAt : Date . now ( ) ,
657+ backend : 'xctest' ,
658+ } ;
659+ const previousSnapshot = session . snapshot ;
660+ mockDispatch . mockImplementation ( async ( _device , command ) => {
661+ if ( command !== 'snapshot' ) return { } ;
662+ return {
663+ backend : 'xctest' ,
664+ quality : {
665+ state : 'sparse' ,
666+ backend : 'private-ax' ,
667+ reason : 'sparse tree' ,
668+ reasonCode : 'sparse-tree' ,
669+ } ,
670+ nodes : [
671+ {
672+ index : 0 ,
673+ type : 'Application' ,
674+ } ,
675+ ] ,
676+ } ;
677+ } ) ;
678+
679+ const { response } = await runFindClickScenario ( {
680+ positionals : [ 'text' , 'Never appears' , 'wait' , '350' ] ,
681+ session,
682+ } ) ;
683+
684+ expect ( response . ok ) . toBe ( false ) ;
685+ expect ( session . snapshot ) . toBe ( previousSnapshot ) ;
686+ expect ( ! response . ok && response . error ) . toMatchObject ( {
687+ code : 'COMMAND_FAILED' ,
688+ message : 'find could not read the current accessibility tree' ,
689+ details : {
690+ reason : 'sparse tree' ,
691+ hint : expect . stringContaining ( 'snapshot quality verdict is sparse' ) ,
692+ } ,
693+ } ) ;
694+ } ) ;
695+
696+ test ( 'handleFindCommands wait captures fresh snapshots while polling' , async ( ) => {
618697 const { response } = await runFindClickScenario ( {
619698 positionals : [ 'text' , 'Never appears' , 'wait' , '350' ] ,
620699 nodes : [ { index : 0 , depth : 0 , type : 'StaticText' , label : 'Other text' } ] ,
@@ -624,5 +703,5 @@ test('handleFindCommands wait reuses the snapshot cache while polling', async ()
624703 if ( ! response . ok ) {
625704 expect ( response . error . message ) . toContain ( 'find wait timed out' ) ;
626705 }
627- expect ( mockDispatch ) . toHaveBeenCalledTimes ( 1 ) ;
706+ expect ( mockDispatch ) . toHaveBeenCalledTimes ( 2 ) ;
628707} ) ;
0 commit comments