-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #993 from lbrooks/find-device-tests
coverage: Refactor and add tests to findDevice.ts
- Loading branch information
Showing
12 changed files
with
182 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,45 @@ | ||
import { DygmaDeviceType } from "@Renderer/types/dygmaDefs"; | ||
import { DeviceTools } from "@Renderer/DeviceContext"; | ||
import log from "electron-log/renderer"; | ||
import { DeviceTools } from "@Renderer/DeviceContext"; | ||
import { DygmaDeviceType } from "@Renderer/types/dygmaDefs"; | ||
import { ExtendedPort } from "../comms/serial/SerialAPI"; | ||
import Hardware from "../hardware"; | ||
|
||
function getDeviceList(deviceType: "serial" | "nonSerial" | "bootloader", desiredDevice?: DygmaDeviceType): DygmaDeviceType[] { | ||
if (deviceType === "serial" && desiredDevice?.info.keyboardType !== undefined) { | ||
return [desiredDevice]; | ||
} | ||
return Hardware[deviceType]; | ||
} | ||
|
||
export const findDevice = async ( | ||
deviceType: "serial" | "nonSerial" | "bootloader", | ||
message: string, | ||
desiredDevice?: DygmaDeviceType, | ||
) => { | ||
log.info("Going to list devices"); | ||
let devices = Hardware[deviceType] as DygmaDeviceType[]; | ||
const bootloader = deviceType === "bootloader"; | ||
const list: ExtendedPort[] = (await DeviceTools.enumerateSerial(bootloader)).foundDevices as ExtendedPort[]; | ||
if (deviceType === "serial" && desiredDevice?.info.keyboardType !== undefined) devices = [desiredDevice]; | ||
log.verbose("List of Devices: ", list); | ||
const detected = list.find(dev => { | ||
log.info("Searching for Device"); | ||
let found = false; | ||
devices.forEach(device => { | ||
): Promise<ExtendedPort> => { | ||
log.info(`Going to list devices for type: ${deviceType}`); | ||
const lookupDevices: DygmaDeviceType[] = getDeviceList(deviceType, desiredDevice); | ||
|
||
const isBootloader = deviceType === "bootloader"; | ||
const hwDevices: ExtendedPort[] = (await DeviceTools.enumerateSerial(isBootloader)).foundDevices; | ||
log.verbose("List of Devices: ", hwDevices); | ||
|
||
const matchingDevice: ExtendedPort = hwDevices.find(hwDevice => | ||
lookupDevices.some(lookupDevice => { | ||
log.info( | ||
`Dev bootloader: ${dev.device.bootloader} & HW: ${bootloader}, Dev KBType: ${device.info.keyboardType} & HW: ${dev.device.info.keyboardType}`, | ||
`Bootloader - Lookup: ${isBootloader} & HW: ${hwDevice.device.bootloader} | KBType - Lookup: ${lookupDevice.info.keyboardType} & HW: ${hwDevice.device.info.keyboardType}`, | ||
); | ||
if ( | ||
found !== true && bootloader | ||
? dev.device.bootloader !== undefined && | ||
dev.device.bootloader === bootloader && | ||
device.usb.vendorId === dev.device.usb.vendorId && | ||
device.usb.productId === dev.device.usb.productId | ||
: device.usb.vendorId === dev.device.usb.vendorId && | ||
device.usb.productId === dev.device.usb.productId && | ||
device.info.keyboardType === dev.device.info.keyboardType | ||
) { | ||
found = true; | ||
|
||
if (lookupDevice.usb.vendorId !== hwDevice.device.usb.vendorId) { | ||
return false; | ||
} | ||
if (lookupDevice.usb.productId !== hwDevice.device.usb.productId) { | ||
return false; | ||
} | ||
if (isBootloader) { | ||
return hwDevice.device.bootloader === true; | ||
} | ||
}); | ||
return found; | ||
}); | ||
log.info(message, detected); | ||
return detected; | ||
return lookupDevice.info.keyboardType === hwDevice.device.info.keyboardType; | ||
}), | ||
); | ||
log.info(`${deviceType === "serial" ? "keyboard" : deviceType} detected`, matchingDevice); | ||
return matchingDevice; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
import log from "electron-log/renderer"; | ||
import { DeviceTools } from "@Renderer/DeviceContext"; | ||
import { findDevice } from "./findDevice"; | ||
import { DefyWired, DefyWiredBootloader } from "../hardware-dygma-defy-wired"; | ||
import { Raise2ANSI } from "../hardware-dygma-raise2-ansi"; | ||
|
||
const connectedDevices = [ | ||
{ | ||
manufacturer: "", | ||
serialNumber: "", | ||
pnpId: "", | ||
locationId: "", | ||
vendorId: "35ef", | ||
path: "Defy-Wired", | ||
productId: "0010", | ||
device: { ...DefyWired }, | ||
}, | ||
{ | ||
manufacturer: "", | ||
serialNumber: "", | ||
pnpId: "", | ||
locationId: "", | ||
vendorId: "35ef", | ||
path: "Defy-Wired-Bootloader", | ||
productId: "0011", | ||
device: { ...DefyWiredBootloader }, | ||
}, | ||
{ | ||
manufacturer: "", | ||
serialNumber: "", | ||
pnpId: "", | ||
locationId: "", | ||
vendorId: "35ef", | ||
path: "Dygma-Raise-2-ANSI", | ||
productId: "0021", | ||
device: { ...Raise2ANSI }, | ||
}, | ||
]; | ||
|
||
describe("findDevices", () => { | ||
beforeEach(() => { | ||
vi.mock("../../renderer/DeviceContext.tsx", async importOriginal => { | ||
const data = await importOriginal<typeof import("../../renderer/DeviceContext.tsx")>(); | ||
const val = { | ||
...data, | ||
}; | ||
val.DeviceTools.enumerateSerial = vi.fn(); | ||
return val; | ||
}); | ||
|
||
vi.mock("electron-log/renderer", () => ({ | ||
default: { | ||
error: vi.fn(), | ||
warn: vi.fn(), | ||
info: vi.fn(), | ||
verbose: vi.fn(), | ||
debug: vi.fn(), | ||
silly: vi.fn(), | ||
}, | ||
})); | ||
}); | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("should find no device when none connected", async () => { | ||
vi.mocked(DeviceTools).enumerateSerial.mockResolvedValue({ foundDevices: [], validDevices: [] }); | ||
|
||
expect(await findDevice("serial", undefined)).toBeUndefined(); | ||
|
||
expect(log.info).toHaveBeenCalledTimes(2); | ||
expect(log.info).toHaveBeenCalledWith("Going to list devices for type: serial"); | ||
expect(log.info).toHaveBeenCalledWith("keyboard detected", undefined); | ||
|
||
expect(log.verbose).toHaveBeenCalledTimes(1); | ||
expect(log.verbose).toHaveBeenCalledWith("List of Devices: ", []); | ||
}); | ||
|
||
it("should find a device", async () => { | ||
vi.mocked(DeviceTools).enumerateSerial.mockResolvedValue({ | ||
foundDevices: connectedDevices, | ||
validDevices: [], | ||
}); | ||
|
||
expect(await findDevice("serial", undefined)).toEqual(connectedDevices[0]); | ||
|
||
expect(log.info).toHaveBeenCalledTimes(5); | ||
expect(log.info).toHaveBeenCalledWith("Going to list devices for type: serial"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: false & HW: undefined | KBType - Lookup: ISO & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: false & HW: undefined | KBType - Lookup: ANSI & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: false & HW: undefined | KBType - Lookup: wired & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("keyboard detected", connectedDevices[0]); | ||
|
||
expect(log.verbose).toHaveBeenCalledTimes(1); | ||
expect(log.verbose).toHaveBeenCalledWith("List of Devices: ", connectedDevices); | ||
}); | ||
|
||
it("should find a wireless device", async () => { | ||
vi.mocked(DeviceTools).enumerateSerial.mockResolvedValue({ | ||
foundDevices: connectedDevices, | ||
validDevices: [], | ||
}); | ||
|
||
expect(await findDevice("serial", connectedDevices[2].device)).toEqual(connectedDevices[2]); | ||
|
||
expect(log.info).toHaveBeenCalledTimes(5); | ||
expect(log.info).toHaveBeenCalledWith("Going to list devices for type: serial"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: false & HW: undefined | KBType - Lookup: ANSI & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: false & HW: true | KBType - Lookup: ANSI & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: false & HW: false | KBType - Lookup: ANSI & HW: ANSI"); | ||
expect(log.info).toHaveBeenCalledWith("keyboard detected", connectedDevices[2]); | ||
|
||
expect(log.verbose).toHaveBeenCalledTimes(1); | ||
expect(log.verbose).toHaveBeenCalledWith("List of Devices: ", connectedDevices); | ||
}); | ||
|
||
it("should finds a bootloader", async () => { | ||
vi.mocked(DeviceTools).enumerateSerial.mockResolvedValue({ | ||
foundDevices: connectedDevices, | ||
validDevices: [], | ||
}); | ||
|
||
expect(await findDevice("bootloader", undefined)).toEqual(connectedDevices[1]); | ||
|
||
expect(log.info).toHaveBeenCalledTimes(11); | ||
expect(log.info).toHaveBeenCalledWith("Going to list devices for type: bootloader"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: true & HW: undefined | KBType - Lookup: ANSI & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: true & HW: undefined | KBType - Lookup: ISO & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: true & HW: undefined | KBType - Lookup: wired & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: true & HW: undefined | KBType - Lookup: wireless & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: true & HW: true | KBType - Lookup: ANSI & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: true & HW: true | KBType - Lookup: ISO & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("Bootloader - Lookup: true & HW: true | KBType - Lookup: wired & HW: wired"); | ||
expect(log.info).toHaveBeenCalledWith("bootloader detected", connectedDevices[1]); | ||
|
||
expect(log.verbose).toHaveBeenCalledTimes(1); | ||
expect(log.verbose).toHaveBeenCalledWith("List of Devices: ", connectedDevices); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters