import { Eltro as t, assert} from 'eltro' import fs from 'fs/promises' import Util from '../core/util.mjs' const isWindows = process.platform === 'win32' t.describe('#getPathFromRoot()', function() { t.test('should return file relative to root', async function() { var util = new Util(import.meta.url) let path = util.getPathFromRoot('') if (isWindows) { assert.ok(path.endsWith('\\test\\')) } else { assert.ok(path.endsWith('/test/')) } path = util.getPathFromRoot('../core/http.mjs') if (isWindows) { assert.ok(path.endsWith('\\core\\http.mjs')) } else { assert.ok(path.endsWith('/core/http.mjs')) } let stat = await fs.stat(util.getPathFromRoot('../manage/.gitkeep')) assert.strictEqual(stat.size, 0) }) }) t.describe('#getUrlFromRoot()', function() { t.test('should return an import compatible path', async function() { var util = new Util(import.meta.url) let data = await import(util.getUrlFromRoot('template.mjs')) assert.deepStrictEqual(data.default, { a: 1 }) }) }) t.describe('#get7zipExecutable()', function() { var util = new Util(import.meta.url) if (process.platform === 'win32') { console.log('Adding 7zip windows exe path test') t.test('should return windows executable path', function() { assert.ok(util.get7zipExecutable().endsWith('\\service-core\\bin\\7za.exe'), `${util.get7zipExecutable()} should end with 7za.exe`) }) } else { console.log('Adding 7zip linux exe path test') t.test('should return linux executable path', function() { assert.ok(util.get7zipExecutable().endsWith('/service-core/bin/7zas'), `${util.get7zipExecutable()} should end with 7zas`) }) } }) t.describe('#getExtension()', function() { var util = new Util(import.meta.url) t.test('should return correct extension on basic extension types', function() { assert.strictEqual(util.getExtension('file.7z'), '.7z') assert.strictEqual(util.getExtension('file.zip'), '.zip') assert.strictEqual(util.getExtension('file.tar'), '.tar') assert.strictEqual(util.getExtension('file.doc'), '.doc') assert.strictEqual(util.getExtension('file.rar'), '.rar') assert.strictEqual(util.getExtension('file.test'), '.test') }) t.test('should support that annoying tar extension', function() { assert.strictEqual(util.getExtension('file.tar.test'), '.tar.test') assert.strictEqual(util.getExtension('file.tar.gz'), '.tar.gz') assert.strictEqual(util.getExtension('file.tar.xz'), '.tar.xz') assert.strictEqual(util.getExtension('file.tar.bz2'), '.tar.bz2') }) }) t.describe('#getApplications()', function() { var util = new Util(import.meta.url) t.test('should fail to find if not a valid object', function() { assert.deepStrictEqual(util.getAppNames({ app: [] }), []) assert.deepStrictEqual(util.getAppNames({ app: 1234 }), []) assert.deepStrictEqual(util.getAppNames({ app: '124124' }), []) assert.deepStrictEqual(util.getAppNames({ app: '' }), []) assert.deepStrictEqual(util.getAppNames({ app: {} }), []) assert.deepStrictEqual(util.getAppNames({ app: null }), []) }) t.test('should return the name of the key if an object with port and provider is found', function() { assert.deepStrictEqual(util.getAppNames({ app: { port: 1234, provider: 'asdf' } }), ['app']) }) t.test('should fail to find if port is missing or port not a number', function() { assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: null } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 'asdf' } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: '1234' } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 0 } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: [] } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: {} } }), []) }) t.test('should fail to find if provider is missing or not a string', function() { assert.deepStrictEqual(util.getAppNames({ app: { port: 1234 } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: '', port: 1234 } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: null, port: 1234 } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: [], port: 1234 } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: {}, port: 1234 } }), []) assert.deepStrictEqual(util.getAppNames({ app: { provider: 1234, port: 1234 } }), []) }) }) t.describe('#verifyConfig()', function() { var util = new Util(import.meta.url) let input t.beforeEach(function() { input = { name: 'test', title: 'Test', description: 'Some description', app: { port: 1234, provider: 'asdf' }, } }) let testMissing = ['name'] testMissing.forEach(function(check) { t.test(`should fail on missing ${check}`, function() { delete input[check] assert.throws(function() { util.verifyConfig(input) }, function(err) { assert.match(err.message, new RegExp(check)) return true }) }) }) let testOptional = ['title', 'description'] testOptional.forEach(function(check) { t.test(`should succeed even if ${check} is missing`, function() { delete input[check] util.verifyConfig(input) }) }) t.test('should succeed if debug port is specified or null', function() { input.debugPort = 1234 util.verifyConfig(input) input.debugPort = null util.verifyConfig(input) }) t.test('should fail if debug port is invalid', function() { let checks = [ [], {}, 0, 'asdf', '1234' ] checks.forEach(function(check) { input.debugPort = check assert.throws(function() { util.verifyConfig(input) }, function(err) { assert.match(err.message, /debugPort/) assert.match(err.message, /number/) return true }) }) }) t.test('should fail if no objects in config', function() { delete input.app assert.throws(function() { util.verifyConfig(input) }, function(err) { assert.match(err.message, /no/) assert.match(err.message, /app/) return true }) }) })