@@ -8,8 +8,7 @@ describe('getDeviceId', function () {
88
99 const deviceId = await getDeviceId ( {
1010 getMachineId,
11- isNodeMachineId : false ,
12- } ) . value ;
11+ } ) ;
1312
1413 expect ( deviceId ) . to . be . a ( 'string' ) ;
1514 expect ( deviceId ) . to . have . lengthOf ( 64 ) ; // SHA-256 hex digest length
@@ -22,48 +21,46 @@ describe('getDeviceId', function () {
2221
2322 const resultA = await getDeviceId ( {
2423 getMachineId,
25- isNodeMachineId : true ,
26- } ) . value ;
24+ } ) ;
2725
2826 const resultB = await getDeviceId ( {
2927 getMachineId : ( ) => Promise . resolve ( mockMachineId . toUpperCase ( ) ) ,
30- isNodeMachineId : true ,
31- } ) . value ;
28+ } ) ;
3229
3330 expect ( resultA ) . to . equal ( resultB ) ;
3431 } ) ;
3532
3633 it ( 'returns "unknown" when machine id is not found' , async function ( ) {
3734 const getMachineId = ( ) => Promise . resolve ( undefined ) ;
38- let capturedError : Error | undefined ;
35+ let capturedError : [ string , Error ] | undefined ;
3936
4037 const deviceId = await getDeviceId ( {
4138 getMachineId,
42- isNodeMachineId : false ,
43- onError : ( error ) => {
44- capturedError = error ;
39+ onError : ( reason , error ) => {
40+ capturedError = [ reason , error ] ;
4541 } ,
46- } ) . value ;
42+ } ) ;
4743
4844 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' ) ;
5047 } ) ;
5148
5249 it ( 'returns "unknown" and calls onError when getMachineId throws' , async function ( ) {
5350 const error = new Error ( 'Something went wrong' ) ;
5451 const getMachineId = ( ) => Promise . reject ( error ) ;
55- let capturedError : Error | undefined ;
52+ let capturedError : [ string , Error ] | undefined ;
5653
5754 const result = await getDeviceId ( {
5855 getMachineId,
59- isNodeMachineId : false ,
60- onError : ( err ) => {
61- capturedError = err ;
56+ onError : ( reason , err ) => {
57+ capturedError = [ reason , err ] ;
6258 } ,
63- } ) . value ;
59+ } ) ;
6460
6561 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 ) ;
6764 } ) ;
6865
6966 it ( 'produces consistent hash for the same machine id' , async function ( ) {
@@ -72,86 +69,84 @@ describe('getDeviceId', function () {
7269
7370 const resultA = await getDeviceId ( {
7471 getMachineId,
75- isNodeMachineId : false ,
76- } ) . value ;
72+ } ) ;
7773
7874 const resultB = await getDeviceId ( {
7975 getMachineId,
80- isNodeMachineId : false ,
81- } ) . value ;
76+ } ) ;
8277
8378 expect ( resultA ) . to . equal ( resultB ) ;
8479 } ) ;
8580
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+ ) ;
91115 } ) ;
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+ }
130117 } ) ;
131118
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' ) ;
137149 } ) ;
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 ) ;
155150 }
156151 } ) ;
157152} ) ;
0 commit comments