|
| 1 | +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { DeviceIdService } from "../../../src/helpers/deviceId.js"; |
| 3 | +import { CompositeLogger } from "../../../src/common/logger.js"; |
| 4 | +import nodeMachineId from "node-machine-id"; |
| 5 | + |
| 6 | +describe("Device ID", () => { |
| 7 | + let testLogger: CompositeLogger; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + testLogger = new CompositeLogger(); |
| 11 | + testLogger.debug = vi.fn(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + if (DeviceIdService.isInitialized()) { |
| 16 | + DeviceIdService.getInstance().close(); |
| 17 | + } |
| 18 | + }); |
| 19 | + |
| 20 | + describe("when resolving device ID", () => { |
| 21 | + it("should successfully resolve device ID in real environment", async () => { |
| 22 | + const deviceId = DeviceIdService.init(testLogger); |
| 23 | + const result = await deviceId.getDeviceId(); |
| 24 | + |
| 25 | + expect(result).not.toBe("unknown"); |
| 26 | + expect(result).toBeTruthy(); |
| 27 | + expect(typeof result).toBe("string"); |
| 28 | + expect(result.length).toBeGreaterThan(0); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should cache device ID after first resolution", async () => { |
| 32 | + // spy on machineId |
| 33 | + const machineIdSpy = vi.spyOn(nodeMachineId, "machineId"); |
| 34 | + const deviceId = DeviceIdService.init(testLogger); |
| 35 | + |
| 36 | + // First call |
| 37 | + const result1 = await deviceId.getDeviceId(); |
| 38 | + expect(result1).not.toBe("unknown"); |
| 39 | + |
| 40 | + // Second call should be cached |
| 41 | + const result2 = await deviceId.getDeviceId(); |
| 42 | + expect(result2).toBe(result1); |
| 43 | + // check that machineId was called only once |
| 44 | + expect(machineIdSpy).toHaveBeenCalledOnce(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should handle concurrent device ID requests correctly", async () => { |
| 48 | + const deviceId = DeviceIdService.init(testLogger); |
| 49 | + |
| 50 | + const promises = Array.from({ length: 5 }, () => deviceId.getDeviceId()); |
| 51 | + |
| 52 | + // All should resolve to the same value |
| 53 | + const results = await Promise.all(promises); |
| 54 | + const firstResult = results[0]; |
| 55 | + expect(firstResult).not.toBe("unknown"); |
| 56 | + |
| 57 | + // All results should be identical |
| 58 | + results.forEach((result) => { |
| 59 | + expect(result).toBe(firstResult); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe("when resolving device ID fails", () => { |
| 65 | + const originalMachineId: typeof nodeMachineId.machineId = nodeMachineId.machineId; |
| 66 | + |
| 67 | + beforeEach(() => { |
| 68 | + // mock the machineId function to throw an abort error |
| 69 | + nodeMachineId.machineId = vi.fn(); |
| 70 | + }); |
| 71 | + |
| 72 | + afterEach(() => { |
| 73 | + // Restore original implementation |
| 74 | + nodeMachineId.machineId = originalMachineId; |
| 75 | + }); |
| 76 | + |
| 77 | + it("should handle resolution errors gracefully", async () => { |
| 78 | + // mock the machineId function to throw a resolution error |
| 79 | + nodeMachineId.machineId = vi.fn().mockImplementation(() => { |
| 80 | + return new Promise<string>((resolve, reject) => { |
| 81 | + reject(new Error("Machine ID failed")); |
| 82 | + }); |
| 83 | + }); |
| 84 | + const deviceId = DeviceIdService.init(testLogger); |
| 85 | + const handleDeviceIdErrorSpy = vi.spyOn(deviceId, "handleDeviceIdError" as keyof DeviceIdService); |
| 86 | + |
| 87 | + const result = await deviceId.getDeviceId(); |
| 88 | + |
| 89 | + expect(result).toBe("unknown"); |
| 90 | + expect(handleDeviceIdErrorSpy).toHaveBeenCalledWith( |
| 91 | + "resolutionError", |
| 92 | + expect.stringContaining("Machine ID failed") |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should handle abort signal scenarios gracefully", async () => { |
| 97 | + // slow down the machineId function to give time to send abort signal |
| 98 | + nodeMachineId.machineId = vi.fn().mockImplementation(() => { |
| 99 | + return new Promise<string>((resolve) => { |
| 100 | + setTimeout(() => resolve("delayed-id"), 1000); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + const deviceId = DeviceIdService.init(testLogger); |
| 105 | + const handleDeviceIdErrorSpy = vi.spyOn(deviceId, "handleDeviceIdError" as keyof DeviceIdService); |
| 106 | + |
| 107 | + deviceId.close(); |
| 108 | + |
| 109 | + // expect the deviceId service to throw an error |
| 110 | + await expect(deviceId.getDeviceId()).rejects.toThrow(Error); |
| 111 | + // test that the private function handleDeviceIdError was called with reason "abort" |
| 112 | + expect(handleDeviceIdErrorSpy).toHaveBeenCalledWith( |
| 113 | + "abort", |
| 114 | + expect.stringContaining("Aborted by abort signal") |
| 115 | + ); |
| 116 | + |
| 117 | + // check that the deviceId service is not initialized anymore |
| 118 | + expect(() => DeviceIdService.getInstance()).toThrow(Error); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should handle timeout scenarios gracefully", async () => { |
| 122 | + nodeMachineId.machineId = vi.fn().mockImplementation(() => { |
| 123 | + return new Promise<string>((resolve) => { |
| 124 | + setTimeout(() => resolve("delayed-id"), 200); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + // override the timeout to 100ms |
| 129 | + const deviceId = DeviceIdService.init(testLogger, 100); |
| 130 | + const handleDeviceIdErrorSpy = vi.spyOn(deviceId, "handleDeviceIdError" as keyof DeviceIdService); |
| 131 | + |
| 132 | + const result = await deviceId.getDeviceId(); |
| 133 | + |
| 134 | + expect(result).toBe("unknown"); |
| 135 | + expect(handleDeviceIdErrorSpy).toHaveBeenCalledWith("timeout", expect.stringContaining("Timeout")); |
| 136 | + }, 5000); |
| 137 | + }); |
| 138 | +}); |
0 commit comments