11import { Button , ColorMode , Key , Point , Region , Size } from "@nut-tree/shared" ;
22import { WindowsHostClient } from "./host-client" ;
33import { createWindowsProviders } from "./providers" ;
4+ import { KeyboardClass } from "../../../../core/nut.js/lib/keyboard.class" ;
5+ import { NoopLogProvider } from "../../../../core/nut.js/lib/provider/log/noop-log-provider.class" ;
6+ import { ProviderRegistry } from "@nut-tree/provider-interfaces" ;
7+
8+ // sleep() uses the global registry only for logging. Do not load native defaults
9+ // when exercising the real KeyboardClass and its actual timer implementation.
10+ jest . mock ( "../../../../core/nut.js/lib/provider/provider-registry.class" , ( ) => ( {
11+ __esModule : true ,
12+ default : { getLogProvider : ( ) => ( { debug : jest . fn ( ) } ) }
13+ } ) ) ;
414
515describe ( "WSL Windows providers" , ( ) => {
616 function setup ( ) {
@@ -23,13 +33,13 @@ describe("WSL Windows providers", () => {
2333 await providers . keyboard . click ( ...keys ) ;
2434 expect ( keys ) . toEqual ( [ Key . LeftControl , Key . LeftShift , Key . A ] ) ;
2535 expect ( request ) . toHaveBeenCalledTimes ( 1 ) ;
26- expect ( request ) . toHaveBeenCalledWith ( "keyClick" , { keys : [ "control" , "shift" , "a" ] , delay : 10 } ) ;
36+ expect ( request ) . toHaveBeenCalledWith ( "keyClick" , { keys : [ "control" , "shift" , "a" ] , delay : 10 } , 30010 ) ;
2737 } ) ;
2838
2939 it ( "maps function, right modifier and keypad keys" , async ( ) => {
3040 const { request, providers } = setup ( ) ;
3141 await providers . keyboard . pressKey ( Key . RightSuper , Key . F24 , Key . NumPad9 ) ;
32- expect ( request ) . toHaveBeenCalledWith ( "keys" , { keys : [ "right_win" , "f24" , "numpad_9" ] , down : true , delay : 10 } ) ;
42+ expect ( request ) . toHaveBeenCalledWith ( "keys" , { keys : [ "right_win" , "f24" , "numpad_9" ] , down : true , delay : 10 } , 30010 ) ;
3343 } ) ;
3444
3545 it ( "rejects unsupported keys before sending input" , async ( ) => {
@@ -42,16 +52,16 @@ describe("WSL Windows providers", () => {
4252 const { request, providers } = setup ( ) ;
4353 await providers . mouse . scrollUp ( 120 ) ; await providers . mouse . scrollDown ( 120 ) ;
4454 await providers . mouse . scrollLeft ( 120 ) ; await providers . mouse . scrollRight ( 120 ) ;
45- expect ( request ) . toHaveBeenNthCalledWith ( 1 , "scroll" , { amount : 120 , horizontal : false , delay : 100 } ) ;
46- expect ( request ) . toHaveBeenNthCalledWith ( 2 , "scroll" , { amount : - 120 , horizontal : false , delay : 100 } ) ;
47- expect ( request ) . toHaveBeenNthCalledWith ( 3 , "scroll" , { amount : - 120 , horizontal : true , delay : 100 } ) ;
48- expect ( request ) . toHaveBeenNthCalledWith ( 4 , "scroll" , { amount : 120 , horizontal : true , delay : 100 } ) ;
55+ expect ( request ) . toHaveBeenNthCalledWith ( 1 , "scroll" , { amount : 120 , horizontal : false , delay : 100 } , 30100 ) ;
56+ expect ( request ) . toHaveBeenNthCalledWith ( 2 , "scroll" , { amount : - 120 , horizontal : false , delay : 100 } , 30100 ) ;
57+ expect ( request ) . toHaveBeenNthCalledWith ( 3 , "scroll" , { amount : - 120 , horizontal : true , delay : 100 } , 30100 ) ;
58+ expect ( request ) . toHaveBeenNthCalledWith ( 4 , "scroll" , { amount : 120 , horizontal : true , delay : 100 } , 30100 ) ;
4959 } ) ;
5060
5161 it ( "uses native desktop coordinates including negative monitor positions" , async ( ) => {
5262 const { request, providers } = setup ( ) ;
5363 await providers . mouse . setMousePosition ( new Point ( - 100 , 200 ) ) ;
54- expect ( request ) . toHaveBeenCalledWith ( "moveMouse" , { x : - 100 , y : 200 , delay : 100 } ) ;
64+ expect ( request ) . toHaveBeenCalledWith ( "moveMouse" , { x : - 100 , y : 200 , delay : 100 } , 30100 ) ;
5565 } ) ;
5666
5767 it ( "returns a nut.js Image directly from pixel data" , async ( ) => {
@@ -108,4 +118,61 @@ describe("WSL Windows providers", () => {
108118 await expect ( providers . mouse . setMousePosition ( new Point ( NaN , 0 ) ) ) . rejects . toThrow ( "x" ) ;
109119 expect ( request ) . not . toHaveBeenCalled ( ) ;
110120 } ) ;
121+
122+ it ( "includes the maximum configured delay in all input command deadlines" , async ( ) => {
123+ const { request, providers } = setup ( ) ;
124+ providers . keyboard . setKeyboardDelay ( 60000 ) ;
125+ providers . mouse . setMouseDelay ( 60000 ) ;
126+ await providers . keyboard . click ( Key . A ) ;
127+ await providers . keyboard . pressKey ( Key . A ) ;
128+ await providers . keyboard . releaseKey ( Key . A ) ;
129+ await providers . mouse . setMousePosition ( new Point ( 0 , 0 ) ) ;
130+ await providers . mouse . click ( Button . LEFT ) ;
131+ await providers . mouse . doubleClick ( Button . LEFT ) ;
132+ await providers . mouse . pressButton ( Button . LEFT ) ;
133+ await providers . mouse . releaseButton ( Button . LEFT ) ;
134+ await providers . mouse . scrollUp ( 120 ) ;
135+ for ( const [ , args , timeout ] of request . mock . calls ) {
136+ expect ( args ?. delay ) . toBe ( 60000 ) ;
137+ expect ( timeout ) . toBe ( 90000 ) ;
138+ }
139+ expect ( request ) . toHaveBeenCalledTimes ( 9 ) ;
140+ } ) ;
141+
142+ it ( "rejects overflowing highlight deadlines before sending the action" , async ( ) => {
143+ const { request, providers } = setup ( ) ;
144+ const region = new Region ( 0 , 0 , 1 , 1 ) ;
145+ await expect ( providers . screen . highlightScreenRegion ( region , 2147483647 - 30000 + 1 , 0.5 ) ) . rejects . toThrow ( "timeout" ) ;
146+ expect ( request ) . not . toHaveBeenCalled ( ) ;
147+ await providers . screen . highlightScreenRegion ( region , 2147483647 - 30000 , 0.5 ) ;
148+ expect ( request . mock . calls [ 0 ] [ 2 ] ) . toBe ( 2147483647 ) ;
149+ } ) ;
150+
151+ it ( "does not add provider delay to direct string typing" , async ( ) => {
152+ const { request, providers } = setup ( ) ;
153+ providers . keyboard . setKeyboardDelay ( 60000 ) ;
154+ await providers . keyboard . type ( "a" . repeat ( 40000 ) ) ;
155+ expect ( request ) . toHaveBeenCalledWith ( "type" , { text : "a" . repeat ( 40000 ) , delay : 0 } ) ;
156+ } ) ;
157+
158+ it . each ( [ 0 , 25 , 300 ] ) ( "uses the current public string delay once (%i ms)" , async milliseconds => {
159+ jest . useFakeTimers ( ) ;
160+ try {
161+ const { request, providers } = setup ( ) ;
162+ const registry = {
163+ hasKeyboard : ( ) => true , getKeyboard : ( ) => providers . keyboard ,
164+ getLogProvider : ( ) => new NoopLogProvider ( )
165+ } as unknown as ProviderRegistry ;
166+ const keyboard = new KeyboardClass ( registry ) ;
167+ keyboard . config . autoDelayMs = milliseconds ;
168+ const start = Date . now ( ) ;
169+ const typing = keyboard . type ( "ab" ) ;
170+ await jest . runAllTimersAsync ( ) ;
171+ await typing ;
172+ expect ( Date . now ( ) - start ) . toBe ( 2 * milliseconds ) ;
173+ expect ( request . mock . calls ) . toEqual ( [
174+ [ "type" , { text : "a" , delay : 0 } ] , [ "type" , { text : "b" , delay : 0 } ]
175+ ] ) ;
176+ } finally { jest . useRealTimers ( ) ; }
177+ } ) ;
111178} ) ;
0 commit comments