@@ -8,7 +8,6 @@ describe('getDeviceId', function () {
88
99 const deviceId = await getDeviceId ( {
1010 getMachineId,
11- isNodeMachineId : false ,
1211 } ) . value ;
1312
1413 expect ( deviceId ) . to . be . a ( 'string' ) ;
@@ -22,12 +21,10 @@ describe('getDeviceId', function () {
2221
2322 const resultA = await getDeviceId ( {
2423 getMachineId,
25- isNodeMachineId : true ,
2624 } ) . value ;
2725
2826 const resultB = await getDeviceId ( {
2927 getMachineId : ( ) => Promise . resolve ( mockMachineId . toUpperCase ( ) ) ,
30- isNodeMachineId : true ,
3128 } ) . value ;
3229
3330 expect ( resultA ) . to . equal ( resultB ) ;
@@ -39,7 +36,6 @@ describe('getDeviceId', function () {
3936
4037 const deviceId = await getDeviceId ( {
4138 getMachineId,
42- isNodeMachineId : false ,
4339 onError : ( error ) => {
4440 capturedError = error ;
4541 } ,
@@ -56,7 +52,6 @@ describe('getDeviceId', function () {
5652
5753 const result = await getDeviceId ( {
5854 getMachineId,
59- isNodeMachineId : false ,
6055 onError : ( err ) => {
6156 capturedError = err ;
6257 } ,
@@ -72,18 +67,16 @@ describe('getDeviceId', function () {
7267
7368 const resultA = await getDeviceId ( {
7469 getMachineId,
75- isNodeMachineId : false ,
7670 } ) . value ;
7771
7872 const resultB = await getDeviceId ( {
7973 getMachineId,
80- isNodeMachineId : false ,
8174 } ) . value ;
8275
8376 expect ( resultA ) . to . equal ( resultB ) ;
8477 } ) ;
8578
86- it ( 'handles timeout when getting machine id ' , async function ( ) {
79+ it ( 'resolves timeout with "unknown" by default ' , async function ( ) {
8780 let timeoutId : NodeJS . Timeout ;
8881 const getMachineId = ( ) =>
8982 new Promise < string > ( ( resolve ) => {
@@ -93,7 +86,6 @@ describe('getDeviceId', function () {
9386 let errorCalled = false ;
9487 const result = await getDeviceId ( {
9588 getMachineId,
96- isNodeMachineId : false ,
9789 onError : ( ) => {
9890 errorCalled = true ;
9991 } ,
@@ -106,6 +98,57 @@ describe('getDeviceId', function () {
10698 expect ( errorCalled ) . to . equal ( false ) ;
10799 } ) ;
108100
101+ it ( 'resolves with result of onTimeout when successful' , async function ( ) {
102+ let timeoutId : NodeJS . Timeout ;
103+ const getMachineId = ( ) =>
104+ new Promise < string > ( ( resolve ) => {
105+ timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
106+ } ) ;
107+
108+ let errorCalled = false ;
109+ const result = await getDeviceId ( {
110+ getMachineId,
111+ onError : ( ) => {
112+ errorCalled = true ;
113+ } ,
114+ timeout : 1 ,
115+ onTimeout : ( ) => {
116+ return 'abc-123' ;
117+ } ,
118+ } ) . value ;
119+
120+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
121+ clearTimeout ( timeoutId ! ) ;
122+ expect ( result ) . to . equal ( 'abc-123' ) ;
123+ expect ( errorCalled ) . to . equal ( false ) ;
124+ } ) ;
125+
126+ it ( 'rejects with an error if onTimeout throws' , async function ( ) {
127+ let timeoutId : NodeJS . Timeout ;
128+ const getMachineId = ( ) =>
129+ new Promise < string > ( ( resolve ) => {
130+ timeoutId = setTimeout ( ( ) => resolve ( 'delayed-id' ) , 10_000 ) ;
131+ } ) ;
132+
133+ let errorCalled = false ;
134+ try {
135+ const result = await getDeviceId ( {
136+ getMachineId,
137+ onError : ( ) => {
138+ errorCalled = true ;
139+ } ,
140+ timeout : 1 ,
141+ onTimeout : ( ) => {
142+ throw new Error ( 'Operation timed out' ) ;
143+ } ,
144+ } ) . value ;
145+ expect . fail ( 'Expected promise to be rejected' ) ;
146+ } catch ( error ) {
147+ expect ( ( error as Error ) . message ) . to . equal ( 'Operation timed out' ) ;
148+ expect ( errorCalled ) . to . equal ( false ) ;
149+ }
150+ } ) ;
151+
109152 it ( 'handles external promise resolution' , async function ( ) {
110153 let timeoutId : NodeJS . Timeout ;
111154 const getMachineId = ( ) =>
@@ -115,7 +158,6 @@ describe('getDeviceId', function () {
115158
116159 const { resolve, value } = getDeviceId ( {
117160 getMachineId,
118- isNodeMachineId : false ,
119161 } ) ;
120162
121163 resolve ( 'external-id' ) ;
@@ -140,7 +182,6 @@ describe('getDeviceId', function () {
140182
141183 const { reject, value } = getDeviceId ( {
142184 getMachineId,
143- isNodeMachineId : false ,
144185 } ) ;
145186
146187 reject ( error ) ;
0 commit comments