1
+ import {
2
+ findMatchingDevice ,
3
+ getDeviceVersions ,
4
+ resolveVersion ,
5
+ validateArgs ,
6
+ } from '../../src/tools/appautomate-utils/appautomate' ;
7
+
8
+ // Mock only the external dependencies
9
+ jest . mock ( '../../src/config' , ( ) => ( {
10
+ __esModule : true ,
11
+ default : {
12
+ browserstackUsername : 'fake-user' ,
13
+ browserstackAccessKey : 'fake-key' ,
14
+ } ,
15
+ } ) ) ;
16
+
17
+ jest . mock ( 'fs' ) ;
18
+ jest . mock ( '../../src/logger' , ( ) => ( {
19
+ error : jest . fn ( ) ,
20
+ info : jest . fn ( ) ,
21
+ } ) ) ;
22
+
23
+ jest . mock ( '../../src/lib/instrumentation' , ( ) => ( {
24
+ trackMCP : jest . fn ( ) ,
25
+ } ) ) ;
26
+
27
+ describe ( 'appautomate utils' , ( ) => {
28
+ const validAndroidArgs = {
29
+ desiredPlatform : 'android' ,
30
+ desiredPlatformVersion : '12.0' ,
31
+ appPath : '/path/to/app.apk' ,
32
+ desiredPhone : 'Samsung Galaxy S20' ,
33
+ } ;
34
+
35
+ const validIOSArgs = {
36
+ desiredPlatform : 'ios' ,
37
+ desiredPlatformVersion : '16.0' ,
38
+ appPath : '/path/to/app.ipa' ,
39
+ desiredPhone : 'iPhone 12 Pro' ,
40
+ } ;
41
+
42
+ beforeEach ( ( ) => {
43
+ jest . clearAllMocks ( ) ;
44
+ } ) ;
45
+
46
+ describe ( 'validateArgs' , ( ) => {
47
+ it ( 'should validate Android args successfully' , ( ) => {
48
+ expect ( ( ) => validateArgs ( validAndroidArgs ) ) . not . toThrow ( ) ;
49
+ } ) ;
50
+
51
+ it ( 'should validate iOS args successfully' , ( ) => {
52
+ expect ( ( ) => validateArgs ( validIOSArgs ) ) . not . toThrow ( ) ;
53
+ } ) ;
54
+
55
+ it ( 'should fail if platform is not provided' , ( ) => {
56
+ const args = { ...validAndroidArgs , desiredPlatform : '' } ;
57
+ expect ( ( ) => validateArgs ( args ) ) . toThrow ( 'Missing required arguments' ) ;
58
+ } ) ;
59
+
60
+ it ( 'should fail if app path is not provided' , ( ) => {
61
+ const args = { ...validAndroidArgs , appPath : '' } ;
62
+ expect ( ( ) => validateArgs ( args ) ) . toThrow ( 'You must provide an appPath' ) ;
63
+ } ) ;
64
+
65
+ it ( 'should fail if phone is not provided' , ( ) => {
66
+ const args = { ...validAndroidArgs , desiredPhone : '' } ;
67
+ expect ( ( ) => validateArgs ( args ) ) . toThrow ( 'Missing required arguments' ) ;
68
+ } ) ;
69
+
70
+ it ( 'should fail if Android app path does not end with .apk' , ( ) => {
71
+ const args = { ...validAndroidArgs , appPath : '/path/to/app.ipa' } ;
72
+ expect ( ( ) => validateArgs ( args ) ) . toThrow ( 'You must provide a valid Android app path' ) ;
73
+ } ) ;
74
+
75
+ it ( 'should fail if iOS app path does not end with .ipa' , ( ) => {
76
+ const args = { ...validIOSArgs , appPath : '/path/to/app.apk' } ;
77
+ expect ( ( ) => validateArgs ( args ) ) . toThrow ( 'You must provide a valid iOS app path' ) ;
78
+ } ) ;
79
+ } ) ;
80
+
81
+ describe ( 'findMatchingDevice' , ( ) => {
82
+ const devices = [
83
+ { device : 'Samsung Galaxy S20' , display_name : 'Samsung Galaxy S20' , os_version : '12.0' , real_mobile : true } ,
84
+ { device : 'iPhone 12 Pro' , display_name : 'iPhone 12 Pro' , os_version : '16.0' , real_mobile : true } ,
85
+ { device : 'Samsung Galaxy S21' , display_name : 'Samsung Galaxy S21' , os_version : '12.0' , real_mobile : true } ,
86
+ ] ;
87
+
88
+ it ( 'should find exact matching device' , ( ) => {
89
+ const result = findMatchingDevice ( devices , 'Samsung Galaxy S20' ) ;
90
+ expect ( result ) . toHaveLength ( 1 ) ;
91
+ expect ( result [ 0 ] . display_name ) . toBe ( 'Samsung Galaxy S20' ) ;
92
+ } ) ;
93
+
94
+ it ( 'should throw error if no device found' , ( ) => {
95
+ expect ( ( ) => findMatchingDevice ( devices , 'Invalid Device' ) ) . toThrow ( 'No devices found' ) ;
96
+ } ) ;
97
+
98
+ it ( 'should throw error with suggestions for similar devices' , ( ) => {
99
+ expect ( ( ) => findMatchingDevice ( devices , 'Galaxy' ) ) . toThrow ( 'Alternative devices found' ) ;
100
+ } ) ;
101
+ } ) ;
102
+
103
+ describe ( 'getDeviceVersions' , ( ) => {
104
+ const devices = [
105
+ { device : 'Device1' , display_name : 'Device1' , os_version : '11.0' , real_mobile : true } ,
106
+ { device : 'Device2' , display_name : 'Device2' , os_version : '12.0' , real_mobile : true } ,
107
+ { device : 'Device3' , display_name : 'Device3' , os_version : '11.0' , real_mobile : true } ,
108
+ { device : 'Device4' , display_name : 'Device4' , os_version : '13.0' , real_mobile : true } ,
109
+ ] ;
110
+
111
+ it ( 'should return unique sorted versions' , ( ) => {
112
+ const versions = getDeviceVersions ( devices ) ;
113
+ expect ( versions ) . toEqual ( [ '11.0' , '12.0' , '13.0' ] ) ;
114
+ } ) ;
115
+ } ) ;
116
+
117
+ describe ( 'resolveVersion' , ( ) => {
118
+ const versions = [ '11.0' , '12.0' , '13.0' ] ;
119
+
120
+ it ( 'should resolve latest version' , ( ) => {
121
+ expect ( resolveVersion ( versions , 'latest' ) ) . toBe ( '13.0' ) ;
122
+ } ) ;
123
+
124
+ it ( 'should resolve oldest version' , ( ) => {
125
+ expect ( resolveVersion ( versions , 'oldest' ) ) . toBe ( '11.0' ) ;
126
+ } ) ;
127
+
128
+ it ( 'should resolve specific version' , ( ) => {
129
+ expect ( resolveVersion ( versions , '12.0' ) ) . toBe ( '12.0' ) ;
130
+ } ) ;
131
+
132
+ it ( 'should throw error for invalid version' , ( ) => {
133
+ expect ( ( ) => resolveVersion ( versions , '10.0' ) ) . toThrow ( 'Version "10.0" not found' ) ;
134
+ } ) ;
135
+ } ) ;
136
+ } ) ;
0 commit comments