service-core/test/util.test.mjs
2022-01-28 11:47:55 +00:00

305 lines
12 KiB
JavaScript

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.test('should fail to find if https is defined but not a boolean', function() {
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, https: null } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, https: false } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, https: true } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, https: 'asdf' } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, https: '1234' } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, https: 0 } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, https: [] } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, https: {} } }), [])
})
t.test('should fail to find if updateEvery is defined but not a valid number', function() {
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, updateEvery: null } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, updateEvery: 5 } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, updateEvery: 1000 } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, updateEvery: 'asdf' } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, updateEvery: '1234' } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, updateEvery: 0 } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, updateEvery: -5 } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, updateEvery: [] } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, updateEvery: {} } }), [])
})
t.test('should fail to find if waitUntilFail is defined but not a valid number', function() {
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: null } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: 5 } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: 15 } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: 1000 } }), ['app'])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: 'asdf' } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: '1234' } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: 0 } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: -5 } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: [] } }), [])
assert.deepStrictEqual(util.getAppNames({ app: { provider: 'asdf', port: 1234, waitUntilFail: {} } }), [])
})
})
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
})
})
})
t.describe('#extractFile()', function() {
var util = new Util(import.meta.url)
t.afterEach(function() {
return Promise.all([
fs.rm('./test/testapp/example.tar', { force: true }),
fs.rm('./test/testapp/file1.txt', { force: true }),
fs.rm('./test/testapp/file2.txt', { force: true }),
])
})
t.test('should support extracting 7z file', async function() {
await Promise.all([
assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/file1.txt'))),
assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/file2.txt'))),
])
let log = ''
try {
await util.extractFile(util.getPathFromRoot('./testapp/example.7z'), function(msg) {
log += msg + '\n'
})
} catch (err) {
console.log(log)
console.log(err)
throw err
}
let stats = await Promise.all([
fs.stat(util.getPathFromRoot('./testapp/file1.txt')),
fs.stat(util.getPathFromRoot('./testapp/file2.txt')),
])
assert.strictEqual(stats[0].size, 5)
assert.strictEqual(stats[1].size, 5)
})
t.test('should support extracting .tar.gz file', async function() {
await Promise.all([
assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/file1.txt'))),
assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/file2.txt'))),
])
let log = ''
try {
await util.extractFile(util.getPathFromRoot('./testapp/example.tar.gz'), function(msg) {
log += msg + '\n'
})
} catch (err) {
console.log(log)
console.log(err)
throw err
}
let stats = await Promise.all([
fs.stat(util.getPathFromRoot('./testapp/file1.txt')),
fs.stat(util.getPathFromRoot('./testapp/file2.txt')),
])
assert.strictEqual(stats[0].size, 5)
assert.strictEqual(stats[1].size, 5)
await assert.isRejected(fs.stat(util.getPathFromRoot('./testapp/example.tar')))
})
t.test('should stream the process of extracting', async function() {
let output = ''
try {
await util.extractFile(util.getPathFromRoot('./testapp/example.tar.gz'), function(msg) { output += msg + '\n' })
} catch (err) {
console.log(output)
console.log(err)
throw err
}
assert.match(output, /Extracting archive\:.+example.tar.gz/)
assert.match(output, /1 file, 123 bytes/)
assert.strictEqual(output.indexOf('\r\n'), -1)
})
})