import nrfdlModule, { FirmwareReadResult, Device, Error, Progress, // LogEvent, // @ts-ignore nrfdlModule Is not a module } from '../../../../index'; import { validateSchema } from '../common/helpers'; require('../../../../jasmine_shared.js'); const fs = require('fs'); /** * NOTE: Copy of the JS test with typescript and AJV added AND added progressCb */ describe('[Read firmware]', () => { let context = 0; // const logtask = 0; const hex_path = './temp_read_fw.hex'; const number_of_jlink_devices_program = 1; const minHexBytesSize = 100; jasmine.DEFAULT_TIMEOUT_INTERVAL = number_of_jlink_devices_program * 100000; beforeAll(() => { context = nrfdlModule.createContext(); fs.writeFileSync(hex_path, ''); // todo: run and delete // nrfdlModule.setLogLevel(context, 'NRFDL_LOG_DEBUG'); // logtask = nrfdlModule.startLogEvents( // context, // () => {}, // (event: LogEvent) => { // // console.log('Event:', event) // } // ); }); afterAll(() => { nrfdlModule.releaseContext(context); // todo: run and delete // nrfdlModule.stopLogEvents(logtask); }); it('Test read firmware to file', async () => { const result: Device[] = await nrfdlModule.enumerate(context); expect(result.length).toBeGreaterThan(0); let devices = result.filter(el => el.traits.jlink); devices = devices.slice(0, number_of_jlink_devices_program); devices = devices.map(async (device: Device) => { await nrfdlModule.deviceControlRecover(context, device.id); return device; }); devices = await Promise.all(devices); expect(devices.length).toBeGreaterThanOrEqual(number_of_jlink_devices_program); const devicePromises = devices.map( dev => new Promise((resolve, reject) => nrfdlModule.firmwareRead( context, dev.id, nrfdlModule.NRFDL_FW_FILE, nrfdlModule.NRFDL_FW_INTEL_HEX, (fwResult: Error | FirmwareReadResult) => { if ('error_code' in fwResult) { reject(fwResult); } // TODO: test schema for fwResult const stats = fs.statSync(fwResult.file); expect(stats.size).toBeGreaterThan(minHexBytesSize); resolve(fwResult); }, async (progress: Progress.CallbackParameters) => { const schemaValid = await validateSchema( '#/definitions/Progress.CallbackParameters', progress, true ); expect(schemaValid).toBe(true); }, hex_path ) ) ); await Promise.all(devicePromises); }); it('Test read firmware to buffer', async () => { const result: Device[] = await nrfdlModule.enumerate(context); expect(result.length).toBeGreaterThan(0); let devices = result.filter(el => el.traits.jlink); devices = devices.slice(0, number_of_jlink_devices_program); devices = devices.map(async (device: Device) => { await nrfdlModule.deviceControlRecover(context, device.id); return device; }); devices = await Promise.all(devices); expect(devices.length).toBeGreaterThanOrEqual(number_of_jlink_devices_program); const devicePromises = devices.map( dev => new Promise((resolve, reject) => nrfdlModule.firmwareRead( context, dev.id, nrfdlModule.NRFDL_FW_BUFFER, nrfdlModule.NRFDL_FW_INTEL_HEX, (fwResult: Error | FirmwareReadResult) => { if ('error_code' in fwResult) { reject(fwResult); } // TODO: test schema for fwResult // console.log( // 'fwResult.buffer typeof', // fwResult.buffer instanceof Buffer // ); // console.log( // 'fwResult.buffer typeof', // typeof fwResult.buffer // ); // todo: what is buffer? base64? // todo: check from the app code and decode buffer base64 and utf8 as done in the app expect(Buffer.byteLength(fwResult.buffer, 'utf8')).toBeGreaterThan(minHexBytesSize); resolve(fwResult); }, async (progress: Progress.CallbackParameters) => { const schemaValid = await validateSchema( '#/definitions/Progress.CallbackParameters', progress ); expect(schemaValid).toBe(true); }, hex_path ) ) ); await Promise.all(devicePromises); }); });