11import { describe , expect , it , vi , beforeEach , afterEach } from "vitest" ;
2- import { DeviceIdService } from "../../../src/helpers/deviceId.js" ;
2+ import { DeviceId } from "../../../src/helpers/deviceId.js" ;
33import { CompositeLogger } from "../../../src/common/logger.js" ;
44import nodeMachineId from "node-machine-id" ;
55
@@ -12,15 +12,13 @@ describe("Device ID", () => {
1212 } ) ;
1313
1414 afterEach ( ( ) => {
15- if ( DeviceIdService . isInitialized ( ) ) {
16- DeviceIdService . getInstance ( ) . close ( ) ;
17- }
15+ DeviceId . create ( testLogger ) . close ( ) ;
1816 } ) ;
1917
2018 describe ( "when resolving device ID" , ( ) => {
2119 it ( "should successfully resolve device ID in real environment" , async ( ) => {
22- const deviceId = DeviceIdService . init ( testLogger ) ;
23- const result = await deviceId . getDeviceId ( ) ;
20+ const deviceId = DeviceId . create ( testLogger ) ;
21+ const result = await deviceId . get ( ) ;
2422
2523 expect ( result ) . not . toBe ( "unknown" ) ;
2624 expect ( result ) . toBeTruthy ( ) ;
@@ -31,23 +29,23 @@ describe("Device ID", () => {
3129 it ( "should cache device ID after first resolution" , async ( ) => {
3230 // spy on machineId
3331 const machineIdSpy = vi . spyOn ( nodeMachineId , "machineId" ) ;
34- const deviceId = DeviceIdService . init ( testLogger ) ;
32+ const deviceId = DeviceId . create ( testLogger ) ;
3533
3634 // First call
37- const result1 = await deviceId . getDeviceId ( ) ;
35+ const result1 = await deviceId . get ( ) ;
3836 expect ( result1 ) . not . toBe ( "unknown" ) ;
3937
4038 // Second call should be cached
41- const result2 = await deviceId . getDeviceId ( ) ;
39+ const result2 = await deviceId . get ( ) ;
4240 expect ( result2 ) . toBe ( result1 ) ;
4341 // check that machineId was called only once
4442 expect ( machineIdSpy ) . toHaveBeenCalledOnce ( ) ;
4543 } ) ;
4644
4745 it ( "should handle concurrent device ID requests correctly" , async ( ) => {
48- const deviceId = DeviceIdService . init ( testLogger ) ;
46+ const deviceId = DeviceId . create ( testLogger ) ;
4947
50- const promises = Array . from ( { length : 5 } , ( ) => deviceId . getDeviceId ( ) ) ;
48+ const promises = Array . from ( { length : 5 } , ( ) => deviceId . get ( ) ) ;
5149
5250 // All should resolve to the same value
5351 const results = await Promise . all ( promises ) ;
@@ -81,10 +79,10 @@ describe("Device ID", () => {
8179 reject ( new Error ( "Machine ID failed" ) ) ;
8280 } ) ;
8381 } ) ;
84- const deviceId = DeviceIdService . init ( testLogger ) ;
85- const handleDeviceIdErrorSpy = vi . spyOn ( deviceId , "handleDeviceIdError" as keyof DeviceIdService ) ;
82+ const deviceId = DeviceId . create ( testLogger ) ;
83+ const handleDeviceIdErrorSpy = vi . spyOn ( deviceId , "handleDeviceIdError" as keyof DeviceId ) ;
8684
87- const result = await deviceId . getDeviceId ( ) ;
85+ const result = await deviceId . get ( ) ;
8886
8987 expect ( result ) . toBe ( "unknown" ) ;
9088 expect ( handleDeviceIdErrorSpy ) . toHaveBeenCalledWith (
@@ -101,38 +99,13 @@ describe("Device ID", () => {
10199 } ) ;
102100 } ) ;
103101
104- const deviceId = DeviceIdService . init ( testLogger ) ;
105- const handleDeviceIdErrorSpy = vi . spyOn ( deviceId , "handleDeviceIdError" as keyof DeviceIdService ) ;
102+ const deviceId = DeviceId . create ( testLogger , 100 ) ; // Short timeout
103+ const handleDeviceIdErrorSpy = vi . spyOn ( deviceId , "handleDeviceIdError" as keyof DeviceId ) ;
106104
107- deviceId . close ( ) ;
108-
109- // expect the deviceId service to throw an error
110- await expect ( deviceId . getDeviceId ( ) ) . rejects . toThrow ( Error ) ;
111- // test that the private function handleDeviceIdError was called with reason "abort"
112- expect ( handleDeviceIdErrorSpy ) . toHaveBeenCalledWith (
113- "abort" ,
114- expect . stringContaining ( "Aborted by abort signal" )
115- ) ;
116-
117- // check that the deviceId service is not initialized anymore
118- expect ( ( ) => DeviceIdService . getInstance ( ) ) . toThrow ( Error ) ;
119- } ) ;
120-
121- it ( "should handle timeout scenarios gracefully" , async ( ) => {
122- nodeMachineId . machineId = vi . fn ( ) . mockImplementation ( ( ) => {
123- return new Promise < string > ( ( resolve ) => {
124- setTimeout ( ( ) => resolve ( "delayed-id" ) , 200 ) ;
125- } ) ;
126- } ) ;
127-
128- // override the timeout to 100ms
129- const deviceId = DeviceIdService . init ( testLogger , 100 ) ;
130- const handleDeviceIdErrorSpy = vi . spyOn ( deviceId , "handleDeviceIdError" as keyof DeviceIdService ) ;
131-
132- const result = await deviceId . getDeviceId ( ) ;
105+ const result = await deviceId . get ( ) ;
133106
134107 expect ( result ) . toBe ( "unknown" ) ;
135- expect ( handleDeviceIdErrorSpy ) . toHaveBeenCalledWith ( "timeout" , expect . stringContaining ( "Timeout" ) ) ;
136- } , 5000 ) ;
108+ expect ( handleDeviceIdErrorSpy ) . toHaveBeenCalledWith ( "timeout" , expect . any ( String ) ) ;
109+ } ) ;
137110 } ) ;
138111} ) ;
0 commit comments