Skip to content

Commit 680e1e1

Browse files
committed
update tests
1 parent 026b91a commit 680e1e1

File tree

2 files changed

+197
-5
lines changed

2 files changed

+197
-5
lines changed

tests/unit/helpers/deviceId.test.ts

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/unbound-method */
2+
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
3+
import { getDeviceIdForConnection } from "../../../src/helpers/deviceId.js";
4+
import { getDeviceId } from "@mongodb-js/device-id";
5+
import nodeMachineId from "node-machine-id";
6+
import logger, { LogId } from "../../../src/common/logger.js";
7+
8+
// Mock the dependencies
9+
vi.mock("@mongodb-js/device-id");
10+
vi.mock("node-machine-id");
11+
vi.mock("../../../src/common/logger.js");
12+
13+
const MockGetDeviceId = vi.mocked(getDeviceId);
14+
const MockNodeMachineId = vi.mocked(nodeMachineId);
15+
const MockLogger = vi.mocked(logger);
16+
17+
describe("Device ID Helper", () => {
18+
beforeEach(() => {
19+
vi.clearAllMocks();
20+
MockLogger.debug = vi.fn();
21+
});
22+
23+
afterEach(() => {
24+
vi.restoreAllMocks();
25+
});
26+
27+
describe("getDeviceIdForConnection", () => {
28+
it("should successfully retrieve device ID", async () => {
29+
const mockDeviceId = "test-device-id-123";
30+
const mockMachineId = "machine-id-456";
31+
32+
MockNodeMachineId.machineId.mockResolvedValue(mockMachineId);
33+
MockGetDeviceId.mockResolvedValue(mockDeviceId);
34+
35+
const result = await getDeviceIdForConnection();
36+
37+
expect(result).toBe(mockDeviceId);
38+
expect(MockGetDeviceId).toHaveBeenCalledWith({
39+
getMachineId: expect.any(Function),
40+
onError: expect.any(Function),
41+
abortSignal: expect.any(AbortSignal),
42+
});
43+
44+
// Verify the getMachineId function works
45+
const callArgs = MockGetDeviceId.mock.calls[0]?.[0];
46+
if (callArgs?.getMachineId) {
47+
const getMachineIdFn = callArgs.getMachineId;
48+
expect(await getMachineIdFn()).toBe(mockMachineId);
49+
}
50+
});
51+
52+
it("should return 'unknown' when getDeviceId throws an error", async () => {
53+
MockNodeMachineId.machineId.mockResolvedValue("machine-id");
54+
MockGetDeviceId.mockRejectedValue(new Error("Device ID resolution failed"));
55+
56+
const result = await getDeviceIdForConnection();
57+
58+
expect(result).toBe("unknown");
59+
expect(MockLogger.debug).toHaveBeenCalledWith(
60+
LogId.telemetryDeviceIdFailure,
61+
"deviceId",
62+
"Failed to get device ID: Error: Device ID resolution failed"
63+
);
64+
});
65+
66+
it("should handle resolution error callback", async () => {
67+
const mockMachineId = "machine-id";
68+
MockNodeMachineId.machineId.mockResolvedValue(mockMachineId);
69+
MockGetDeviceId.mockImplementation((options) => {
70+
// Simulate a resolution error
71+
if (options.onError) {
72+
options.onError("resolutionError", new Error("Resolution failed"));
73+
}
74+
return Promise.resolve("device-id");
75+
});
76+
77+
const result = await getDeviceIdForConnection();
78+
79+
expect(result).toBe("device-id");
80+
expect(MockLogger.debug).toHaveBeenCalledWith(
81+
LogId.telemetryDeviceIdFailure,
82+
"deviceId",
83+
"Error: Resolution failed"
84+
);
85+
});
86+
87+
it("should handle timeout error callback", async () => {
88+
const mockMachineId = "machine-id";
89+
MockNodeMachineId.machineId.mockResolvedValue(mockMachineId);
90+
MockGetDeviceId.mockImplementation((options) => {
91+
// Simulate a timeout error
92+
if (options.onError) {
93+
options.onError("timeout", new Error("Timeout"));
94+
}
95+
return Promise.resolve("device-id");
96+
});
97+
98+
const result = await getDeviceIdForConnection();
99+
100+
expect(result).toBe("device-id");
101+
expect(MockLogger.debug).toHaveBeenCalledWith(
102+
LogId.telemetryDeviceIdTimeout,
103+
"deviceId",
104+
"Device ID retrieval timed out"
105+
);
106+
});
107+
108+
it("should handle abort error callback without logging", async () => {
109+
const mockMachineId = "machine-id";
110+
MockNodeMachineId.machineId.mockResolvedValue(mockMachineId);
111+
MockGetDeviceId.mockImplementation((options) => {
112+
// Simulate an abort error
113+
if (options.onError) {
114+
options.onError("abort", new Error("Aborted"));
115+
}
116+
return Promise.resolve("device-id");
117+
});
118+
119+
const result = await getDeviceIdForConnection();
120+
121+
expect(result).toBe("device-id");
122+
// Should not log abort errors
123+
expect(MockLogger.debug).not.toHaveBeenCalledWith(
124+
LogId.telemetryDeviceIdFailure,
125+
"deviceId",
126+
expect.stringContaining("Aborted")
127+
);
128+
});
129+
130+
it("should handle machine ID generation failure", async () => {
131+
MockNodeMachineId.machineId.mockImplementation(() => {
132+
throw new Error("Machine ID generation failed");
133+
});
134+
// Also mock getDeviceId to throw to ensure we get the fallback
135+
MockGetDeviceId.mockRejectedValue(new Error("Device ID failed"));
136+
137+
const result = await getDeviceIdForConnection();
138+
139+
expect(result).toBe("unknown");
140+
expect(MockLogger.debug).toHaveBeenCalledWith(
141+
LogId.telemetryDeviceIdFailure,
142+
"deviceId",
143+
"Failed to get device ID: Error: Device ID failed"
144+
);
145+
});
146+
147+
it("should use AbortController signal", async () => {
148+
MockNodeMachineId.machineId.mockResolvedValue("machine-id");
149+
MockGetDeviceId.mockResolvedValue("device-id");
150+
151+
await getDeviceIdForConnection();
152+
153+
const callArgs = MockGetDeviceId.mock.calls[0]?.[0];
154+
if (callArgs) {
155+
expect(callArgs.abortSignal).toBeInstanceOf(AbortSignal);
156+
}
157+
});
158+
159+
it("should handle non-Error exceptions", async () => {
160+
MockNodeMachineId.machineId.mockResolvedValue("machine-id");
161+
MockGetDeviceId.mockRejectedValue("String error");
162+
163+
const result = await getDeviceIdForConnection();
164+
165+
expect(result).toBe("unknown");
166+
expect(MockLogger.debug).toHaveBeenCalledWith(
167+
LogId.telemetryDeviceIdFailure,
168+
"deviceId",
169+
"Failed to get device ID: String error"
170+
);
171+
});
172+
});
173+
});

tests/unit/telemetry.test.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,11 @@ describe("Telemetry", () => {
230230
expect(telemetry.getCommonProperties().device_id).toBe("test-device-id");
231231
});
232232

233-
it("should handle device ID resolution failure", async () => {
234-
// The deviceId utility is already mocked to return "test-device-id"
235-
// We can't easily test the failure case without complex mocking
236-
// So we'll just verify that the deviceId is set correctly
233+
it("should handle device ID resolution failure gracefully", async () => {
234+
// Mock the deviceId utility to return "unknown" for this test
235+
const { getDeviceIdForConnection } = await import("../../src/helpers/deviceId.js");
236+
vi.mocked(getDeviceIdForConnection).mockResolvedValueOnce("unknown");
237+
237238
telemetry = Telemetry.create(session, config);
238239

239240
expect(telemetry["isBufferingEvents"]).toBe(true);
@@ -242,7 +243,25 @@ describe("Telemetry", () => {
242243
await telemetry.setupPromise;
243244

244245
expect(telemetry["isBufferingEvents"]).toBe(false);
245-
expect(telemetry.getCommonProperties().device_id).toBe("test-device-id");
246+
// Should use "unknown" as fallback when device ID resolution fails
247+
expect(telemetry.getCommonProperties().device_id).toBe("unknown");
248+
});
249+
250+
it("should handle device ID timeout gracefully", async () => {
251+
// Mock the deviceId utility to return "unknown" for this test
252+
const { getDeviceIdForConnection } = await import("../../src/helpers/deviceId.js");
253+
vi.mocked(getDeviceIdForConnection).mockResolvedValueOnce("unknown");
254+
255+
telemetry = Telemetry.create(session, config);
256+
257+
expect(telemetry["isBufferingEvents"]).toBe(true);
258+
expect(telemetry.getCommonProperties().device_id).toBe(undefined);
259+
260+
await telemetry.setupPromise;
261+
262+
expect(telemetry["isBufferingEvents"]).toBe(false);
263+
// Should use "unknown" as fallback when device ID times out
264+
expect(telemetry.getCommonProperties().device_id).toBe("unknown");
246265
});
247266
});
248267
});

0 commit comments

Comments
 (0)