|
| 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 | +}); |
0 commit comments