import fs from 'fs/promises'; // @ts-ignore nrfdlModule Is not a module import nrfdlModule from '../../../index'; import { SPECIAL_CHARACTERS } from './common/helpers'; require('../../../jasmine_shared.js'); /** * Test cases: * - create context with special characters * - create multiple contexts * * Test API: * - createContext() * * Test Schema: * - */ // TODO: add test case for failure of the context creation. ER: get a message error https://github.com/NordicPlayground/nrf-device-lib-js/pull/142/files describe('[API] createContext() tests: ', () => { const RELEASE_DIR = './Release'; // includes some chinese and japanese symbols const RELEASE_DIR_COPY = './temp_Release_dir_copy'; const RELEASE_DIR_COPY_SPEC_CHARS = `./temp_Release_dir_spec_chars_${SPECIAL_CHARACTERS}_text`; let localContext = 0; const cleanup = async () => { if (localContext) { nrfdlModule.releaseContext(localContext); localContext = 0; } // cleanup try { await fs.access(RELEASE_DIR_COPY); await fs.rm(RELEASE_DIR_COPY, { recursive: true }); } catch (e) { console.log('API] createContext() tests: nothing to cleanup in path: ', RELEASE_DIR_COPY); } try { await fs.access(RELEASE_DIR_COPY_SPEC_CHARS); await fs.rm(RELEASE_DIR_COPY_SPEC_CHARS, { recursive: true }); } catch (e) { // console.log('API] createContext() tests: nothing to cleanup in path: ', RELEASE_DIR_COPY_SPEC_CHARS); } }; afterEach(cleanup); afterAll(cleanup); // error is expected with current version it('with special characters', async () => { await fs.mkdir(RELEASE_DIR_COPY_SPEC_CHARS); const files = await fs.readdir(RELEASE_DIR); await Promise.all( files.map(file => fs.copyFile(`${RELEASE_DIR}/${file}`, `${RELEASE_DIR_COPY_SPEC_CHARS}/${file}`)) ); localContext = nrfdlModule.createContext({ plugins_dir: RELEASE_DIR_COPY_SPEC_CHARS, }); expect(localContext).toBeGreaterThan(0); }); it('Creates multiple contexts with single plugins_dir', () => { // nrfdlModule - singleton const firstContext = nrfdlModule.createContext({ plugins_dir: RELEASE_DIR, }); const secondContext = nrfdlModule.createContext({ plugins_dir: RELEASE_DIR, }); expect(firstContext).toBeGreaterThan(0); expect(secondContext).toBeGreaterThan(0); expect(firstContext).not.toEqual(secondContext); // cleanup nrfdlModule.releaseContext(firstContext); nrfdlModule.releaseContext(secondContext); }); it('Creates multiple contexts with multiple plugins_dir', async () => { await new Promise(r => setTimeout(r, 2000)); await fs.mkdir(RELEASE_DIR_COPY); const files = await fs.readdir(RELEASE_DIR); await Promise.all(files.map(file => fs.copyFile(`${RELEASE_DIR}/${file}`, `${RELEASE_DIR_COPY}/${file}`))); // nrfdlModule are same // plugins_dir are different const secondContext = nrfdlModule.createContext({ plugins_dir: RELEASE_DIR_COPY, }); const firstContext = nrfdlModule.createContext({ plugins_dir: RELEASE_DIR, }); expect(firstContext).toBeGreaterThan(0); expect(secondContext).toBeGreaterThan(0); // cleanup nrfdlModule.releaseContext(firstContext); nrfdlModule.releaseContext(secondContext); }); });