import nrfdlModule, { Device, SerialPort, // @ts-ignore nrfdlModule Is not a module } from '../../../index'; import { validateSchema } from './common/helpers'; import { MIN_DEVICE_CONNECTED, PROGRAMMER_APP_FILTERS } from './common/config'; require('../../../jasmine_shared.js'); /** * Test cases are based on nRF Connect Programmer 3.0.0 Template * https://projecttools.nordicsemi.no/confluence/display/WYLND/nRF+Connect+Programmer+3.0.0+Template * * Test API: * - enumerate(): with various filters * - createContext() * - releaseContext() * * Test schemas (TS definition equality to the object returned): * - Device (for all possible devices) * Nested types (check if all present in the list): * * USB * * USBDevice * * USBDeviceDescriptor * * USBConfiguration * * USBConfigurationDescriptor * * USBInterface * * USBInterfaceDescriptor * * USBEndpoint * * USBEndpointDescriptor * * ... todo: see if other nested properties are covered as well */ describe('Test Device Selector', () => { let context = 0; const mcuDevices: Device[] = []; const jLinkDevices: Device[] = []; const serialPortsDevices: Device[] = []; jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; beforeAll(async () => { context = nrfdlModule.createContext({ plugins_dir: './Release' }); const mcu: Device[] = await nrfdlModule.enumerate(context, { mcuBoot: true, }); mcuDevices.push(...mcu); const jlink: Device[] = await nrfdlModule.enumerate(context, { jlink: true, }); jLinkDevices.push(...jlink); const serialPorts: Device[] = await nrfdlModule.enumerate(context, { serialPorts: true, }); serialPortsDevices.push( ...serialPorts // remove non-Nordic devices .filter( (device: Device) => device.traits.nordicUsb || device.traits.seggerUsb || device.traits.nordicDfu ) ); }); afterAll(() => { nrfdlModule.releaseContext(context); }); // can be expanded to handling Pascal's case describe('JLink/USB Serial/MCUboot devices are detected', () => { it('Detect MCUBoot devices', async () => { expect(mcuDevices.length).toBeGreaterThan(0); }); it('Detect JLink devices', async () => { expect(jLinkDevices.length).toBeGreaterThan(0); jLinkDevices.forEach(jLinkDevice => { // previously had typo seriaNlumber expect(jLinkDevice.jlink.serialNumber).not.toBe(''); expect(jLinkDevice.jlink.boardVersion).not.toBe(''); // skip for external jLink if (jLinkDevice.jlink.boardVersion) { expect(jLinkDevice.jlink.boardVersion).toContain('PCA'); } }); }); it('Detect USB Serial devices', async () => { expect(serialPortsDevices.length).toBeGreaterThan(0); // todo: boardVersion assertion serialPortsDevices.forEach(spDevice => { expect(spDevice.serialNumber).not.toBe(''); expect(spDevice.serialPorts.length).toBeGreaterThan(0); spDevice.serialPorts.forEach(async (sp: SerialPort) => { expect(sp.comName).toContain('COM'); const schemaIsValid = await validateSchema('#/definitions/SerialPort', sp); expect(schemaIsValid).toBe(true); }); }); }); }); // uses specific filter that is used in programmer app it('JLink, USB Serial and MCUboot devices are detected with filters as in programmer app', async () => { const allDevices: Device[] = await nrfdlModule.enumerate(context, PROGRAMMER_APP_FILTERS); expect(allDevices.length).toBeGreaterThanOrEqual(MIN_DEVICE_CONNECTED); // test devices schemas allDevices.forEach(async device => { const schemaIsValid = await validateSchema('#/definitions/Device', device, true); expect(schemaIsValid).toBe(true); }); }); xit('Enumerates external jLink device', async () => { const externalJLinks = jLinkDevices // todo: find a way for better detection .filter(d => d.usb.device.descriptor.idVendor === 4966 && !d.traits.nordicUsb) .filter(d => !d.jlink.deviceFamily || !d.jlink.deviceFamily.toLowerCase().includes('nrf')); expect(externalJLinks.length).toBeGreaterThan(0); expect(typeof externalJLinks[0].jlink === 'object').toBe(true); const schemaIsValid = await validateSchema('#/definitions/Device', externalJLinks[0], true); expect(schemaIsValid).toBe(true); }); });