service-core/test/application.run.test.mjs

93 lines
2.6 KiB
JavaScript

import { Eltro as t, assert, stub } from 'eltro'
import fs from 'fs/promises'
import lowdb from '../core/db.mjs'
import Application from '../core/application.mjs'
import Util from '../core/util.mjs'
import StaticProvider from '../core/providers/static.mjs'
const util = new Util(import.meta.url)
const logger = {
info: stub(),
warn: stub(),
error: stub(),
}
function createProvider() {
return {
getLatestVersion: stub(),
downloadVersion: stub(),
}
}
t.timeout(250).describe('#runVersion()', function() {
const assertPort = 22345
let db
let app
const defaultHandler = function(db, log, http, port) {
const server = http.createServer(function (req, res) {
res.writeHead(204); res.end(JSON.stringify({ a: 1 }))
})
return new Promise(function(res, rej) {
server.listen(port, '0.0.0.0', function(err) {
if (err) return rej(err)
res()
})
})
}
t.beforeEach(function() {
return lowdb({ test: { } }, logger, null).then(function(res) {
db = res
let provider = new StaticProvider()
app = new Application(util, db, provider, 'testapp')
app.config.port = assertPort
app.registerModule(defaultHandler)
})
})
t.test('should throw if http is not called', async function() {
app.registerModule(function(checkDb, checkLog, checkHttp, checkPort) {
assert.strictEqual(checkDb, db)
assert.strictEqual(checkLog, db.log)
assert.strictEqual(checkHttp, app.http)
assert.strictEqual(checkPort, assertPort)
})
let err = await assert.isRejected(app.runVersion('static'))
assert.match(err.message, /http/i)
assert.match(err.message, /createServer/i)
assert.match(err.message, /static/)
assert.match(err.message, new RegExp(app.name))
assert.match(err.message, /call/i)
})
t.test('should throw if it timeouts waiting for promise to succeed', async function() {
app.config.waitUntilFail = 50
app.registerModule(function() { return new Promise(function() {}) })
let err = await assert.isRejected(app.runVersion('static'))
assert.match(err.message, /time/i)
assert.match(err.message, /out/i)
assert.match(err.message, /static/)
assert.match(err.message, /50ms/)
assert.match(err.message, new RegExp(app.name))
})
t.test('should otherwise succeed if it finished within the time limit', async function() {
app.config.waitUntilFail = 250
app.registerModule(function(db, log, http, port) {
return new Promise(function(res) {
setTimeout(res, 25)
}).then(function() {
return defaultHandler(db, log, http, port)
})
})
await app.runVersion('static')
})
})