Skip to content

Commit eca40e1

Browse files
committed
add timeout test
1 parent 92aab61 commit eca40e1

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

tests/unit/helpers/deviceId.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/unbound-method */
22
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";
44
import { getDeviceId } from "@mongodb-js/device-id";
55
import nodeMachineId from "node-machine-id";
66
import logger, { LogId } from "../../../src/common/logger.js";
@@ -105,6 +105,31 @@ describe("Device ID Helper", () => {
105105
);
106106
});
107107

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+
108133
it("should handle abort error callback without logging", async () => {
109134
const mockMachineId = "machine-id";
110135
MockNodeMachineId.machineId.mockResolvedValue(mockMachineId);

0 commit comments

Comments
 (0)