|
1 | 1 | /* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/unbound-method */
|
2 | 2 | import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
|
3 |
| -import { getDeviceIdForConnection } from "../../../src/helpers/deviceId.js"; |
| 3 | +import { getDeviceIdForConnection, DEVICE_ID_TIMEOUT } from "../../../src/helpers/deviceId.js"; |
4 | 4 | import { getDeviceId } from "@mongodb-js/device-id";
|
5 | 5 | import nodeMachineId from "node-machine-id";
|
6 | 6 | import logger, { LogId } from "../../../src/common/logger.js";
|
@@ -105,6 +105,31 @@ describe("Device ID Helper", () => {
|
105 | 105 | );
|
106 | 106 | });
|
107 | 107 |
|
| 108 | + it("should handle timeout with timer advancement", async () => { |
| 109 | + vi.useFakeTimers(); |
| 110 | + |
| 111 | + const mockMachineId = "machine-id"; |
| 112 | + MockNodeMachineId.machineId.mockResolvedValue(mockMachineId); |
| 113 | + MockGetDeviceId.mockImplementation((options) => { |
| 114 | + vi.advanceTimersByTime(DEVICE_ID_TIMEOUT / 2); |
| 115 | + if (options.onError) { |
| 116 | + options.onError("timeout", new Error("Timeout")); |
| 117 | + } |
| 118 | + return Promise.resolve("device-id"); |
| 119 | + }); |
| 120 | + |
| 121 | + const result = await getDeviceIdForConnection(); |
| 122 | + |
| 123 | + expect(result).toBe("device-id"); |
| 124 | + expect(MockLogger.debug).toHaveBeenCalledWith( |
| 125 | + LogId.telemetryDeviceIdTimeout, |
| 126 | + "deviceId", |
| 127 | + "Device ID retrieval timed out" |
| 128 | + ); |
| 129 | + |
| 130 | + vi.useRealTimers(); |
| 131 | + }); |
| 132 | + |
108 | 133 | it("should handle abort error callback without logging", async () => {
|
109 | 134 | const mockMachineId = "machine-id";
|
110 | 135 | MockNodeMachineId.machineId.mockResolvedValue(mockMachineId);
|
|
0 commit comments