@@ -82,4 +82,72 @@ describe('getDeviceId', function () {
82
82
83
83
expect ( resultA ) . to . equal ( resultB ) ;
84
84
} ) ;
85
+
86
+ it ( 'handles timeout when getting machine id' , async ( ) => {
87
+ let timeoutId : NodeJS . Timeout ;
88
+ const getMachineId = ( ) =>
89
+ new Promise < string > ( ( resolve ) => {
90
+ timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
91
+ } ) ;
92
+
93
+ let errorCalled = false ;
94
+ const result = await getDeviceId ( {
95
+ getMachineId,
96
+ isNodeMachineId : false ,
97
+ onError : ( ) => {
98
+ errorCalled = true ;
99
+ } ,
100
+ timeout : 1 ,
101
+ } ) . value ;
102
+
103
+ clearTimeout ( timeoutId ! ) ;
104
+ expect ( result ) . to . equal ( 'unknown' ) ;
105
+ } ) ;
106
+
107
+ it ( 'handles external promise resolution' , async ( ) => {
108
+ let timeoutId : NodeJS . Timeout ;
109
+ const getMachineId = ( ) =>
110
+ new Promise < string > ( ( resolve ) => {
111
+ timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
112
+ } ) ;
113
+
114
+ const { resolve, value } = getDeviceId ( {
115
+ getMachineId,
116
+ isNodeMachineId : false ,
117
+ } ) ;
118
+
119
+ resolve ( 'external-id' ) ;
120
+
121
+ const result = await value ;
122
+
123
+ clearTimeout ( timeoutId ! ) ;
124
+ expect ( result ) . to . be . a ( 'string' ) ;
125
+ expect ( result ) . to . equal ( 'external-id' ) ;
126
+ expect ( result ) . to . not . equal ( 'unknown' ) ;
127
+ } ) ;
128
+
129
+ it ( 'handles external promise rejection' , async ( ) => {
130
+ let timeoutId : NodeJS . Timeout ;
131
+ const getMachineId = ( ) =>
132
+ new Promise < string > ( ( resolve ) => {
133
+ timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
134
+ } ) ;
135
+
136
+ const error = new Error ( 'External rejection' ) ;
137
+
138
+ const { reject, value } = getDeviceId ( {
139
+ getMachineId,
140
+ isNodeMachineId : false ,
141
+ } ) ;
142
+
143
+ reject ( error ) ;
144
+
145
+ clearTimeout ( timeoutId ! ) ;
146
+ try {
147
+ await value ;
148
+ expect . fail ( 'Expected promise to be rejected' ) ;
149
+ } catch ( e ) {
150
+ expect ( e ) . to . equal ( error ) ;
151
+ }
152
+ } ) ;
85
153
} ) ;
0 commit comments