1- import { beforeEach , expect , test } from 'vitest' ;
1+ import { afterEach , beforeEach , expect , test , vi } from 'vitest' ;
22import {
33 makeIosSession ,
44 authoringPublication ,
55} from '../../../__tests__/support/session-factories.ts' ;
66import { makeSessionStore } from '../../../__tests__/support/store-factory.ts' ;
77import { daemonPlatformServicesFixture } from '../../../__tests__/support/platform-services-fixture.ts' ;
88import { touchCreateCommandSurface } from '../../../__tests__/support/touch-command-surface-fake.ts' ;
9+ import {
10+ registerRequestAbort ,
11+ markRequestCanceled ,
12+ clearRequestCanceled ,
13+ } from '@agent-device/host-kit/request' ;
914import { handleInteractionCommands } from '../index.ts' ;
1015import { gestureRuntimeBindingsFixture } from './__tests__/gesture-runtime-bindings.fixtures.ts' ;
1116
1217let gestures = gestureRuntimeBindingsFixture ( ) ;
18+ const requestId = 'tap-series-cancel' ;
19+ const perform = vi . fn ( async ( ) => ( {
20+ kind : 'tap' as const ,
21+ pointerCount : 2 as const ,
22+ durationMs : 40 ,
23+ from : { x : 140 , y : 375 } ,
24+ to : { x : 140 , y : 375 } ,
25+ message : 'Tapped 2 points' ,
26+ } ) ) ;
1327
1428beforeEach ( ( ) => {
1529 gestures = gestureRuntimeBindingsFixture ( ) ;
30+ perform . mockClear ( ) ;
31+ registerRequestAbort ( requestId ) ;
32+ } ) ;
33+
34+ afterEach ( ( ) => {
35+ clearRequestCanceled ( requestId ) ;
36+ vi . useRealTimers ( ) ;
1637} ) ;
1738
18- async function runGesture ( input : Record < string , unknown > ) {
39+ async function runGesture (
40+ input : Record < string , unknown > ,
41+ command : 'gesture' | 'swipe' = 'gesture' ,
42+ ) {
1943 const sessionStore = makeSessionStore ( ) ;
2044 const session = makeIosSession ( 'tap' , {
2145 appBundleId : 'com.example.tap' ,
2246 scriptPublication : authoringPublication ( 'armed' ) ,
2347 } ) ;
2448 sessionStore . publish ( 'tap' , session ) ;
2549 const response = await handleInteractionCommands ( {
26- createCommandSurface : touchCreateCommandSurface ,
50+ createCommandSurface : async ( config ) => {
51+ const surface = await touchCreateCommandSurface ( config ) ;
52+ return { ...surface , interactions : { ...surface . interactions , gesture : perform } } ;
53+ } ,
2754 platformServices : daemonPlatformServicesFixture ( ) ,
2855 req : {
2956 token : 't' ,
3057 session : 'tap' ,
31- command : 'gesture' ,
58+ command,
59+ meta : { requestId } ,
3260 positionals : [ ] ,
3361 flags : { } ,
3462 input,
@@ -40,7 +68,7 @@ async function runGesture(input: Record<string, unknown>) {
4068 inspectFacts : gestures . inspectFacts ,
4169 bindDevice : gestures . bindDevice ,
4270 } ) ;
43- return { response, action : session . actions [ 0 ] } ;
71+ return { response, action : session . actions [ 0 ] , actions : session . actions } ;
4472}
4573
4674test ( 'a structured tap series records its contacts and series flags for .ad replay' , async ( ) => {
@@ -55,7 +83,38 @@ test('a structured tap series records its contacts and series flags for .ad repl
5583 pauseMs : 10 ,
5684 } ) ;
5785
58- expect ( response ?. ok ) . toBe ( true ) ;
86+ expect ( response ) . toMatchObject ( {
87+ ok : true ,
88+ data : {
89+ kind : 'tap' ,
90+ points : [
91+ { x : 80 , y : 300 } ,
92+ { x : 200 , y : 450 } ,
93+ ] ,
94+ holdMs : 40 ,
95+ count : 2 ,
96+ pauseMs : 10 ,
97+ message : 'Tapped 2 points 2 times' ,
98+ } ,
99+ } ) ;
100+ expect ( response && 'data' in response ? response . data : undefined ) . not . toHaveProperty (
101+ 'completed' ,
102+ ) ;
103+ expect ( perform ) . toHaveBeenCalledTimes ( 2 ) ;
104+ expect ( perform ) . toHaveBeenNthCalledWith (
105+ 2 ,
106+ expect . objectContaining ( {
107+ gesture : {
108+ intent : 'tap' ,
109+ points : [
110+ { x : 80 , y : 300 } ,
111+ { x : 200 , y : 450 } ,
112+ ] ,
113+ holdMs : 40 ,
114+ } ,
115+ } ) ,
116+ ) ;
117+ expect ( gestures . bindDevice ) . toHaveBeenCalledTimes ( 1 ) ;
59118 expect ( action ) . toMatchObject ( {
60119 command : 'gesture' ,
61120 positionals : [ 'tap' , '80' , '300' , '200' , '450' ] ,
@@ -76,3 +135,69 @@ test('a tap without series inputs records no series flags', async () => {
76135 expect ( action ?. flags ) . not . toHaveProperty ( 'holdMs' ) ;
77136 expect ( action ?. flags ) . not . toHaveProperty ( 'pauseMs' ) ;
78137} ) ;
138+
139+ for ( const command of [ 'gesture' , 'swipe' ] as const ) {
140+ test ( `${ command } cancellation interrupts a pause with its typed reason and records no action` , async ( ) => {
141+ vi . useFakeTimers ( ) ;
142+ const input =
143+ command === 'gesture'
144+ ? {
145+ kind : 'tap' ,
146+ points : [
147+ { x : 80 , y : 300 } ,
148+ { x : 200 , y : 450 } ,
149+ ] ,
150+ }
151+ : { from : { x : 80 , y : 300 } , to : { x : 200 , y : 450 } } ;
152+ let outcome : Awaited < ReturnType < typeof runGesture > > | undefined ;
153+ const pending = runGesture ( { ...input , count : 3 , pauseMs : 1000 } , command ) . then ( ( result ) => {
154+ outcome = result ;
155+ } ) ;
156+ await vi . advanceTimersByTimeAsync ( 0 ) ;
157+ expect ( perform ) . toHaveBeenCalledTimes ( 1 ) ;
158+ expect ( outcome ) . toBeUndefined ( ) ;
159+ markRequestCanceled ( requestId ) ;
160+ await vi . advanceTimersByTimeAsync ( 0 ) ;
161+ try {
162+ expect ( outcome ?. response ) . toMatchObject ( {
163+ ok : false ,
164+ error : { code : 'COMMAND_FAILED' , details : { reason : 'request_canceled' } } ,
165+ } ) ;
166+ expect ( outcome ?. actions ) . toEqual ( [ ] ) ;
167+ expect ( perform ) . toHaveBeenCalledTimes ( 1 ) ;
168+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
169+ } finally {
170+ await vi . runAllTimersAsync ( ) ;
171+ await pending ;
172+ }
173+ } ) ;
174+ }
175+
176+ test ( 'cancellation during the last tap records no completed action' , async ( ) => {
177+ let performed = 0 ;
178+ perform . mockImplementation ( async ( ) => {
179+ if ( ++ performed === 3 ) markRequestCanceled ( requestId ) ;
180+ return {
181+ kind : 'tap' ,
182+ pointerCount : 2 ,
183+ durationMs : 40 ,
184+ from : { x : 140 , y : 375 } ,
185+ to : { x : 140 , y : 375 } ,
186+ message : 'Tapped 2 points' ,
187+ } ;
188+ } ) ;
189+ const { response, actions } = await runGesture ( {
190+ kind : 'tap' ,
191+ points : [
192+ { x : 80 , y : 300 } ,
193+ { x : 200 , y : 450 } ,
194+ ] ,
195+ count : 3 ,
196+ } ) ;
197+ expect ( response ) . toMatchObject ( {
198+ ok : false ,
199+ error : { code : 'COMMAND_FAILED' , details : { reason : 'request_canceled' } } ,
200+ } ) ;
201+ expect ( actions ) . toEqual ( [ ] ) ;
202+ expect ( performed ) . toBe ( 3 ) ;
203+ } ) ;
0 commit comments