@@ -8,8 +8,7 @@ describe('getDeviceId', function () {
8
8
9
9
const deviceId = await getDeviceId ( {
10
10
getMachineId,
11
- isNodeMachineId : false ,
12
- } ) . value ;
11
+ } ) ;
13
12
14
13
expect ( deviceId ) . to . be . a ( 'string' ) ;
15
14
expect ( deviceId ) . to . have . lengthOf ( 64 ) ; // SHA-256 hex digest length
@@ -22,48 +21,46 @@ describe('getDeviceId', function () {
22
21
23
22
const resultA = await getDeviceId ( {
24
23
getMachineId,
25
- isNodeMachineId : true ,
26
- } ) . value ;
24
+ } ) ;
27
25
28
26
const resultB = await getDeviceId ( {
29
27
getMachineId : ( ) => Promise . resolve ( mockMachineId . toUpperCase ( ) ) ,
30
- isNodeMachineId : true ,
31
- } ) . value ;
28
+ } ) ;
32
29
33
30
expect ( resultA ) . to . equal ( resultB ) ;
34
31
} ) ;
35
32
36
33
it ( 'returns "unknown" when machine id is not found' , async function ( ) {
37
34
const getMachineId = ( ) => Promise . resolve ( undefined ) ;
38
- let capturedError : Error | undefined ;
35
+ let capturedError : [ string , Error ] | undefined ;
39
36
40
37
const deviceId = await getDeviceId ( {
41
38
getMachineId,
42
- isNodeMachineId : false ,
43
- onError : ( error ) => {
44
- capturedError = error ;
39
+ onError : ( reason , error ) => {
40
+ capturedError = [ reason , error ] ;
45
41
} ,
46
- } ) . value ;
42
+ } ) ;
47
43
48
44
expect ( deviceId ) . to . equal ( 'unknown' ) ;
49
- expect ( capturedError ?. message ) . to . equal ( 'Failed to resolve machine ID' ) ;
45
+ expect ( capturedError ?. [ 0 ] ) . to . equal ( 'resolutionError' ) ;
46
+ expect ( capturedError ?. [ 1 ] . message ) . to . equal ( 'Failed to resolve machine ID' ) ;
50
47
} ) ;
51
48
52
49
it ( 'returns "unknown" and calls onError when getMachineId throws' , async function ( ) {
53
50
const error = new Error ( 'Something went wrong' ) ;
54
51
const getMachineId = ( ) => Promise . reject ( error ) ;
55
- let capturedError : Error | undefined ;
52
+ let capturedError : [ string , Error ] | undefined ;
56
53
57
54
const result = await getDeviceId ( {
58
55
getMachineId,
59
- isNodeMachineId : false ,
60
- onError : ( err ) => {
61
- capturedError = err ;
56
+ onError : ( reason , err ) => {
57
+ capturedError = [ reason , err ] ;
62
58
} ,
63
- } ) . value ;
59
+ } ) ;
64
60
65
61
expect ( result ) . to . equal ( 'unknown' ) ;
66
- expect ( capturedError ) . to . equal ( error ) ;
62
+ expect ( capturedError ?. [ 0 ] ) . to . equal ( 'resolutionError' ) ;
63
+ expect ( capturedError ?. [ 1 ] ) . to . equal ( error ) ;
67
64
} ) ;
68
65
69
66
it ( 'produces consistent hash for the same machine id' , async function ( ) {
@@ -72,86 +69,84 @@ describe('getDeviceId', function () {
72
69
73
70
const resultA = await getDeviceId ( {
74
71
getMachineId,
75
- isNodeMachineId : false ,
76
- } ) . value ;
72
+ } ) ;
77
73
78
74
const resultB = await getDeviceId ( {
79
75
getMachineId,
80
- isNodeMachineId : false ,
81
- } ) . value ;
76
+ } ) ;
82
77
83
78
expect ( resultA ) . to . equal ( resultB ) ;
84
79
} ) ;
85
80
86
- it ( 'handles timeout when getting machine id' , async function ( ) {
87
- let timeoutId : NodeJS . Timeout ;
88
- const getMachineId = ( ) =>
89
- new Promise < string > ( ( resolve ) => {
90
- timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
81
+ const fallbackTestCases : {
82
+ fallbackValue ?: string ;
83
+ expectedResult : string ;
84
+ } [ ] = [
85
+ { expectedResult : 'unknown' } ,
86
+ { fallbackValue : 'fallback-id' , expectedResult : 'fallback-id' } ,
87
+ ] ;
88
+
89
+ describe ( 'when timed out' , function ( ) {
90
+ for ( const testCase of fallbackTestCases ) {
91
+ it ( `resolves with ${ testCase . expectedResult } when fallback value is ${ testCase . fallbackValue ?? 'undefined' } ` , async function ( ) {
92
+ let timeoutId : NodeJS . Timeout ;
93
+ const getMachineId = ( ) =>
94
+ new Promise < string > ( ( resolve ) => {
95
+ timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
96
+ } ) ;
97
+
98
+ let capturedError : [ string , Error ] | undefined ;
99
+ const result = await getDeviceId ( {
100
+ getMachineId,
101
+ onError : ( reason , error ) => {
102
+ capturedError = [ reason , error ] ;
103
+ } ,
104
+ timeout : 5 ,
105
+ fallbackValue : testCase . fallbackValue ,
106
+ } ) ;
107
+
108
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
109
+ clearTimeout ( timeoutId ! ) ;
110
+ expect ( result ) . to . equal ( testCase . expectedResult ) ;
111
+ expect ( capturedError ?. [ 0 ] ) . to . equal ( 'timeout' ) ;
112
+ expect ( capturedError ?. [ 1 ] . message ) . to . equal (
113
+ 'Timeout reached after 5 ms' ,
114
+ ) ;
91
115
} ) ;
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
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
104
- clearTimeout ( timeoutId ! ) ;
105
- expect ( result ) . to . equal ( 'unknown' ) ;
106
- expect ( errorCalled ) . to . equal ( false ) ;
107
- } ) ;
108
-
109
- it ( 'handles external promise resolution' , async function ( ) {
110
- let timeoutId : NodeJS . Timeout ;
111
- const getMachineId = ( ) =>
112
- new Promise < string > ( ( resolve ) => {
113
- timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
114
- } ) ;
115
-
116
- const { resolve, value } = getDeviceId ( {
117
- getMachineId,
118
- isNodeMachineId : false ,
119
- } ) ;
120
-
121
- resolve ( 'external-id' ) ;
122
-
123
- const result = await value ;
124
-
125
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
126
- clearTimeout ( timeoutId ! ) ;
127
- expect ( result ) . to . be . a ( 'string' ) ;
128
- expect ( result ) . to . equal ( 'external-id' ) ;
129
- expect ( result ) . to . not . equal ( 'unknown' ) ;
116
+ }
130
117
} ) ;
131
118
132
- it ( 'handles external promise rejection' , async function ( ) {
133
- let timeoutId : NodeJS . Timeout ;
134
- const getMachineId = ( ) =>
135
- new Promise < string > ( ( resolve ) => {
136
- timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
119
+ describe ( 'when aborted' , function ( ) {
120
+ for ( const testCase of fallbackTestCases ) {
121
+ it ( `resolves with ${ testCase . expectedResult } when fallback value is ${ testCase . fallbackValue ?? 'undefined' } ` , async function ( ) {
122
+ let timeoutId : NodeJS . Timeout ;
123
+ const getMachineId = ( ) =>
124
+ new Promise < string > ( ( resolve ) => {
125
+ timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
126
+ } ) ;
127
+
128
+ let capturedError : [ string , Error ] | undefined ;
129
+ const abortController = new AbortController ( ) ;
130
+ const value = getDeviceId ( {
131
+ getMachineId,
132
+ abortSignal : abortController . signal ,
133
+ onError : ( reason , error ) => {
134
+ capturedError = [ reason , error ] ;
135
+ } ,
136
+ fallbackValue : testCase . fallbackValue ,
137
+ } ) ;
138
+
139
+ abortController . abort ( ) ;
140
+
141
+ const result = await value ;
142
+
143
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
144
+ clearTimeout ( timeoutId ! ) ;
145
+ expect ( result ) . to . be . a ( 'string' ) ;
146
+ expect ( result ) . to . equal ( testCase . expectedResult ) ;
147
+ expect ( capturedError ?. [ 0 ] ) . to . equal ( 'abort' ) ;
148
+ expect ( capturedError ?. [ 1 ] . message ) . to . equal ( 'Aborted by abort signal' ) ;
137
149
} ) ;
138
-
139
- const error = new Error ( 'External rejection' ) ;
140
-
141
- const { reject, value } = getDeviceId ( {
142
- getMachineId,
143
- isNodeMachineId : false ,
144
- } ) ;
145
-
146
- reject ( error ) ;
147
-
148
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
149
- clearTimeout ( timeoutId ! ) ;
150
- try {
151
- await value ;
152
- expect . fail ( 'Expected promise to be rejected' ) ;
153
- } catch ( e ) {
154
- expect ( e ) . to . equal ( error ) ;
155
150
}
156
151
} ) ;
157
152
} ) ;
0 commit comments