1
- import { Key , keyboard } from '@computer-use/nut-js' ;
1
+ import {
2
+ Button ,
3
+ Key ,
4
+ Point ,
5
+ Region ,
6
+ centerOf ,
7
+ keyboard ,
8
+ mouse ,
9
+ straightTo ,
10
+ } from '@computer-use/nut-js' ;
2
11
import { beforeEach , describe , expect , it , vi } from 'vitest' ;
3
12
4
13
import { ExecuteParams , execute } from './execute' ;
@@ -13,6 +22,7 @@ vi.mock('@computer-use/nut-js', async (importOriginal) => {
13
22
config : {
14
23
mouseSpeed : 1500 ,
15
24
} ,
25
+ drag : vi . fn ( ) ,
16
26
} ,
17
27
Key : actual . Key ,
18
28
keyboard : {
@@ -28,8 +38,11 @@ vi.mock('@computer-use/nut-js', async (importOriginal) => {
28
38
RIGHT : 'right' ,
29
39
MIDDLE : 'middle' ,
30
40
} ,
31
- Point : vi . fn ( ) ,
41
+ Point : actual . Point ,
42
+ Region : actual . Region ,
32
43
straightTo : vi . fn ( ( point ) => point ) ,
44
+ centerOf : vi . fn ( ( region ) => region ) ,
45
+ randomPointIn : vi . fn ( ( region ) => region ) ,
33
46
sleep : vi . fn ( ) ,
34
47
} ;
35
48
} ) ;
@@ -45,6 +58,56 @@ describe('execute', () => {
45
58
vi . clearAllMocks ( ) ;
46
59
} ) ;
47
60
61
+ it ( 'Click on the search bar at the top of the screen' , async ( ) => {
62
+ const executeParams : ExecuteParams = {
63
+ prediction : {
64
+ reflection : '' ,
65
+ thought : 'Click on the search bar at the top of the screen\n' ,
66
+ action_type : 'click' ,
67
+ action_inputs : {
68
+ start_box : '[0.072,0.646,0.072,0.646]' ,
69
+ } ,
70
+ } ,
71
+ screenWidth : 1920 ,
72
+ screenHeight : 1080 ,
73
+ logger : mockLogger ,
74
+ scaleFactor : 1 ,
75
+ } ;
76
+
77
+ await execute ( executeParams ) ;
78
+
79
+ expect ( mouse . move ) . toHaveBeenCalledWith (
80
+ straightTo ( new Point ( 138.24 , 697.68 ) ) ,
81
+ ) ;
82
+
83
+ expect ( mouse . click ) . toHaveBeenCalledWith ( Button . LEFT ) ;
84
+ } ) ;
85
+
86
+ it ( 'Click on the search bar at the top of the screen with scaleFactor' , async ( ) => {
87
+ const executeParams : ExecuteParams = {
88
+ prediction : {
89
+ reflection : '' ,
90
+ thought : 'Click on the search bar at the top of the screen\n' ,
91
+ action_type : 'click' ,
92
+ action_inputs : {
93
+ start_box : '[0.072,0.646,0.072,0.646]' ,
94
+ } ,
95
+ } ,
96
+ screenWidth : 1920 ,
97
+ screenHeight : 1080 ,
98
+ logger : mockLogger ,
99
+ scaleFactor : 1.5 ,
100
+ } ;
101
+
102
+ await execute ( executeParams ) ;
103
+
104
+ expect ( mouse . move ) . toHaveBeenCalledWith (
105
+ straightTo ( new Point ( 207.36 , 1046.52 ) ) ,
106
+ ) ;
107
+
108
+ expect ( mouse . click ) . toHaveBeenCalledWith ( Button . LEFT ) ;
109
+ } ) ;
110
+
48
111
it ( 'type doubao.com\n' , async ( ) => {
49
112
const executeParams : ExecuteParams = {
50
113
prediction : {
@@ -110,4 +173,56 @@ describe('execute', () => {
110
173
expect ( keyboard . type ) . toHaveBeenCalledWith ( 'Hello World\\nUI-TARS' ) ;
111
174
expect ( keyboard . pressKey ) . toHaveBeenCalledWith ( Key . Enter ) ;
112
175
} ) ;
176
+
177
+ it ( 'drag slider horizontally' , async ( ) => {
178
+ const executeParams : ExecuteParams = {
179
+ prediction : {
180
+ reflection : '' ,
181
+ thought :
182
+ 'To narrow down the search results to cat litters within the specified price range of $18 to $32, I need to adjust the price filter. The next logical step is to drag the left handle of the price slider to set the minimum price to $18, ensuring that only products within the desired range are displayed.\n' +
183
+ 'Drag the left handle of the price slider to set the minimum price to $18.' ,
184
+ action_type : 'drag' ,
185
+ action_inputs : {
186
+ start_box : '[0.072,0.646,0.072,0.646]' ,
187
+ end_box : '[0.175,0.647,0.175,0.647]' ,
188
+ } ,
189
+ } ,
190
+ screenWidth : 1920 ,
191
+ screenHeight : 1080 ,
192
+ logger : mockLogger ,
193
+ scaleFactor : 1 ,
194
+ } ;
195
+
196
+ await execute ( executeParams ) ;
197
+
198
+ expect ( mouse . drag ) . toHaveBeenCalledWith (
199
+ straightTo ( centerOf ( new Region ( 138.24 , 697.68 , 197.76 , 1.08 ) ) ) ,
200
+ ) ;
201
+ } ) ;
202
+
203
+ it ( 'drag slider vertically' , async ( ) => {
204
+ const executeParams : ExecuteParams = {
205
+ prediction : {
206
+ reflection : '' ,
207
+ thought :
208
+ 'To narrow down the search results to cat litters within the specified price range of $18 to $32, I need to adjust the price filter. The next logical step is to drag the left handle of the price slider to set the minimum price to $18, ensuring that only products within the desired range are displayed.\n' +
209
+ 'Drag the left handle of the price slider to set the minimum price to $18.' ,
210
+ action_type : 'drag' ,
211
+ action_inputs : {
212
+ start_box : '[0.072,0.646,0.072,0.646]' ,
213
+ end_box : '[0.072,0.546,0.072,0.546]' ,
214
+ } ,
215
+ } ,
216
+ screenWidth : 1920 ,
217
+ screenHeight : 1080 ,
218
+ logger : mockLogger ,
219
+ scaleFactor : 1 ,
220
+ } ;
221
+
222
+ await execute ( executeParams ) ;
223
+
224
+ expect ( mouse . drag ) . toHaveBeenCalledWith (
225
+ straightTo ( centerOf ( new Region ( 138.24 , 697.68 , 0 , - 108 ) ) ) ,
226
+ ) ;
227
+ } ) ;
113
228
} ) ;
0 commit comments