import nrfdlModule, { Error, LogEvent, Device, // @ts-ignore nrfdlModule Is not a module } from '../../../index'; import { validateSchema } from './common/helpers'; import { PROGRAMMER_APP_FILTERS } from './common/config'; require('../../../jasmine_shared.js'); /** * Test cases: * * * Test API: * - startLogEvents() * - stopLogEvents() * - setLogLevel() * - setLogPattern() * - setTimeoutConfig() * * Test Schema: * - LogLevel (not all possible values) * - LogEvent */ describe('Logger events: ', () => { jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; const LOG_PATTERN_FROM_APP = '[%n][%l](%T.%e) %v'; let context = 0; let logTaskID = 0; let logTraceCounter = 0; let logErrorCounter = 0; let jlinkDevices: Device[] = []; beforeAll(async () => { context = nrfdlModule.createContext({ plugins_dir: './Release' }); nrfdlModule.setTimeoutConfig(context, { enumerateMs: 3 * 60 * 1000 }); jlinkDevices = await nrfdlModule.enumerate(context, { jlink: true }); logTaskID = nrfdlModule.startLogEvents( context, (err: Error) => { if (err) { console.warn('Log events stopped with an error'); } }, async (event: LogEvent) => { const schemaIsValid = await validateSchema('#/definitions/LogEvent', event); // exit immediately after first error (or it will be hard to trace back error) if (!schemaIsValid) { nrfdlModule.stopLogEvents(logTaskID); } expect(schemaIsValid).toBe(true); if (event.level === 'NRFDL_LOG_TRACE') { logTraceCounter += 1; } if (event.level === 'NRFDL_LOG_ERROR') { logErrorCounter += 1; } } ); }); beforeEach(() => { logErrorCounter = 0; logTraceCounter = 0; }); afterAll(async () => { nrfdlModule.stopLogEvents(logTaskID); // cleanup after error tracing test const jLinkDevice = jlinkDevices[0]; const protectionStatus = await nrfdlModule.deviceControlGetProtectionStatus(context, jLinkDevice.id); if (protectionStatus.protection_status !== 'NRFDL_PROTECTION_STATUS_NONE') { await nrfdlModule.deviceControlRecover(context, jLinkDevice.id); } }); it('Test start of logging events', () => { // task ID is set expect(logTaskID).toBeGreaterThan(0); }); it('Test log tracing', async () => { nrfdlModule.setLogLevel(context, 'NRFDL_LOG_TRACE'); // will trigger events to trace await nrfdlModule.enumerate(context, PROGRAMMER_APP_FILTERS); expect(logTraceCounter).toBeGreaterThan(0); }); it('Test error log tracing', async () => { nrfdlModule.setLogLevel(context, 'NRFDL_LOG_ERROR'); expect(jlinkDevices.length).toBeGreaterThan(0); const jLinkDevice = jlinkDevices[0]; const protectionStatus = await nrfdlModule.deviceControlGetProtectionStatus(context, jLinkDevice.id); if (protectionStatus.protection_status !== 'NRFDL_PROTECTION_STATUS_ALL') { // get protected device -> guarantee error await nrfdlModule.deviceControlSetProtectionStatus(context, jLinkDevice.id, 'NRFDL_PROTECTION_STATUS_ALL'); } try { // fail is OK, error is expected await nrfdlModule.getDeviceCoreInfo(context, jLinkDevice.id); } catch (e) { // console.log('Expected error', e); } expect(logErrorCounter).toBeGreaterThan(0); expect(logTraceCounter).toBe(0); }); /** * Currently test checks if pattern do not break anything; * Can be tested with a regexp if pattern works as expected, but assume * it is covered in the source lib * https://github.com/gabime/spdlog/blob/v1.x/tests/test_pattern_formatter.cpp */ it('Test log pattern', async () => { nrfdlModule.setLogLevel(context, 'NRFDL_LOG_TRACE'); nrfdlModule.setLogPattern(context, LOG_PATTERN_FROM_APP); // will trigger events to trace await nrfdlModule.enumerate(context, PROGRAMMER_APP_FILTERS); expect(logTraceCounter).toBeGreaterThan(0); }); });