1
+ // Mock node-machine-id to simulate machine ID resolution
2
+ jest . mock ( "node-machine-id" , ( ) => ( {
3
+ machineId : jest . fn ( ) ,
4
+ } ) ) ;
5
+
1
6
import { ApiClient } from "../../src/common/atlas/apiClient.js" ;
2
7
import { Session } from "../../src/session.js" ;
3
8
import { DEVICE_ID_TIMEOUT , Telemetry } from "../../src/telemetry/telemetry.js" ;
@@ -6,6 +11,9 @@ import { EventCache } from "../../src/telemetry/eventCache.js";
6
11
import { config } from "../../src/config.js" ;
7
12
import { MACHINE_METADATA } from "../../src/telemetry/constants.js" ;
8
13
import { jest } from "@jest/globals" ;
14
+ import { createHmac } from "crypto" ;
15
+ import logger , { LogId } from "../../src/logger.js" ;
16
+ import nodeMachineId from "node-machine-id" ;
9
17
10
18
// Mock the ApiClient to avoid real API calls
11
19
jest . mock ( "../../src/common/atlas/apiClient.js" ) ;
@@ -15,14 +23,7 @@ const MockApiClient = ApiClient as jest.MockedClass<typeof ApiClient>;
15
23
jest . mock ( "../../src/telemetry/eventCache.js" ) ;
16
24
const MockEventCache = EventCache as jest . MockedClass < typeof EventCache > ;
17
25
18
- // Mock node-machine-id to simulate machine ID resolution
19
- jest . mock ( "node-machine-id" , ( ) => ( {
20
- machineId : jest . fn ( ) ,
21
- } ) ) ;
22
-
23
- import * as nodeMachineId from "node-machine-id" ;
24
- import { createHmac } from "crypto" ;
25
- import logger , { LogId } from "../../src/logger.js" ;
26
+ const mockMachineId = jest . spyOn ( nodeMachineId , "machineId" ) ;
26
27
27
28
describe ( "Telemetry" , ( ) => {
28
29
let mockApiClient : jest . Mocked < ApiClient > ;
@@ -145,7 +146,9 @@ describe("Telemetry", () => {
145
146
146
147
describe ( "sending events" , ( ) => {
147
148
beforeEach ( ( ) => {
148
- ( nodeMachineId . machineId as jest . Mock ) . mockResolvedValue ( "test-machine-id" ) ;
149
+ // @ts -expect-error This is a workaround
150
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
151
+ mockMachineId . mockResolvedValue ( "test-machine-id" ) ;
149
152
} ) ;
150
153
151
154
describe ( "when telemetry is enabled" , ( ) => {
@@ -265,8 +268,9 @@ describe("Telemetry", () => {
265
268
} ) ;
266
269
267
270
it ( "should successfully resolve the machine ID" , async ( ) => {
268
- ( nodeMachineId . machineId as jest . Mock ) . mockResolvedValue ( machineId ) ;
269
- telemetry = Telemetry . create ( session ) ;
271
+ // @ts -expect-error This is a workaround
272
+ mockMachineId . mockResolvedValue ( machineId ) ;
273
+ telemetry = Telemetry . create ( session , config ) ;
270
274
271
275
expect ( telemetry [ "isBufferingEvents" ] ) . toBe ( true ) ;
272
276
expect ( telemetry . getCommonProperties ( ) . device_id ) . toBe ( undefined ) ;
@@ -280,9 +284,11 @@ describe("Telemetry", () => {
280
284
it ( "should handle machine ID resolution failure" , async ( ) => {
281
285
const loggerSpy = jest . spyOn ( logger , "debug" ) ;
282
286
283
- ( nodeMachineId . machineId as jest . Mock ) . mockRejectedValue ( new Error ( "Failed to get device ID" ) ) ;
287
+ // @ts -expect-error This is a workaround
288
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
289
+ mockMachineId . mockRejectedValue ( new Error ( "Failed to get device ID" ) ) ;
284
290
285
- telemetry = Telemetry . create ( session ) ;
291
+ telemetry = Telemetry . create ( session , config ) ;
286
292
287
293
expect ( telemetry [ "isBufferingEvents" ] ) . toBe ( true ) ;
288
294
expect ( telemetry . getCommonProperties ( ) . device_id ) . toBe ( undefined ) ;
@@ -302,11 +308,12 @@ describe("Telemetry", () => {
302
308
it ( "should timeout if machine ID resolution takes too long" , async ( ) => {
303
309
const loggerSpy = jest . spyOn ( logger , "debug" ) ;
304
310
305
- ( nodeMachineId . machineId as jest . Mock ) . mockImplementation ( ( ) => {
311
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
312
+ mockMachineId . mockImplementation ( ( ) => {
306
313
return new Promise ( ( ) => { } ) ;
307
314
} ) ;
308
315
309
- telemetry = Telemetry . create ( session ) ;
316
+ telemetry = Telemetry . create ( session , config ) ;
310
317
311
318
expect ( telemetry [ "isBufferingEvents" ] ) . toBe ( true ) ;
312
319
expect ( telemetry . getCommonProperties ( ) . device_id ) . toBe ( undefined ) ;
0 commit comments