279 lines
9.6 KiB
JavaScript
279 lines
9.6 KiB
JavaScript
import { Eltro as t, assert, stub } from 'eltro'
|
|
import fs from 'fs/promises'
|
|
import Application from '../core/application.mjs'
|
|
import Util from '../core/util.mjs'
|
|
import lowdb from '../core/db.mjs'
|
|
import StaticProvider from '../core/providers/static.mjs'
|
|
import { createFakeContext } from './helpers.mjs'
|
|
|
|
const util = new Util(import.meta.url)
|
|
|
|
t.describe('#runVersion("static")', function() {
|
|
const assertPort = 22345
|
|
let ctx
|
|
let app
|
|
|
|
const defaultHandler = function(orgHandler) {
|
|
let handler = orgHandler || function (req, res) {
|
|
res.writeHead(204); res.end(JSON.stringify({ a: 1 }))
|
|
}
|
|
return function(http, port, ctx) {
|
|
const server = http.createServer(handler)
|
|
|
|
return server.listenAsync(port)
|
|
}
|
|
}
|
|
|
|
t.before(function() {
|
|
return fs.mkdir(util.getPathFromRoot('./testnoexisting'), { recursive: true })
|
|
})
|
|
|
|
t.after(function() {
|
|
return fs.rm(util.getPathFromRoot('./testnoexisting'), { force: true, recursive: true })
|
|
})
|
|
|
|
t.beforeEach(function() {
|
|
return createFakeContext()
|
|
.then(function(res) {
|
|
ctx = res
|
|
let provider = new StaticProvider()
|
|
app = new Application(ctx, provider, 'testapp')
|
|
app.config.port = assertPort
|
|
app.registerModule(defaultHandler())
|
|
})
|
|
})
|
|
|
|
t.afterEach(function() {
|
|
return app.http.closeServer()
|
|
})
|
|
|
|
t.test('should throw if http is not called', async function() {
|
|
app.registerModule(function(checkHttp, checkPort, checkCtx) {
|
|
assert.strictEqual(checkHttp, app.http)
|
|
assert.strictEqual(checkPort, assertPort)
|
|
assert.strictEqual(checkCtx.db, ctx.db)
|
|
assert.strictEqual(checkCtx.log, ctx.log)
|
|
assert.strictEqual(checkCtx.app, app)
|
|
})
|
|
|
|
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)
|
|
assert.strictEqual(ctx.db.data.core.testapp.active, 'static')
|
|
})
|
|
|
|
t.test('should throw if it timeouts waiting for promise to succeed', async function() {
|
|
app.config.startWaitUntilFail = 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))
|
|
assert.strictEqual(ctx.db.data.core.testapp.active, 'static')
|
|
})
|
|
|
|
t.test('should otherwise succeed if it finished within the time limit', async function() {
|
|
const handler = defaultHandler()
|
|
app.config.startWaitUntilFail = 250
|
|
app.registerModule(function(http, port, ctx) {
|
|
return new Promise(function(res) {
|
|
setTimeout(res, 25)
|
|
}).then(function() {
|
|
return handler(http, port, ctx)
|
|
})
|
|
})
|
|
|
|
await app.runVersion('static')
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.active, 'static')
|
|
})
|
|
|
|
t.test('should fail if run succeeds but heartbeat errors', async function() {
|
|
let called = 0
|
|
const handler = function(req, res) {
|
|
called++
|
|
res.statusCode = 400
|
|
res.end(JSON.stringify({ a: 1 }))
|
|
}
|
|
app.config.heartbeatAttempts = 3
|
|
app.registerModule(defaultHandler(handler))
|
|
|
|
let err = await assert.isRejected(app.runVersion('static'))
|
|
assert.match(err.message, /app/i)
|
|
assert.match(err.message, /failed/i)
|
|
assert.match(err.message, /static/i)
|
|
assert.match(err.message, /testapp/i)
|
|
assert.match(err.message, /400/i)
|
|
assert.strictEqual(called, 3)
|
|
assert.strictEqual(ctx.db.data.core.testapp.active, 'static')
|
|
})
|
|
|
|
t.test('should fail if run succeeds but heartbeat times out', async function() {
|
|
let called = 0
|
|
const handler = function(req, res) {
|
|
called++
|
|
}
|
|
app.config.heartbeatAttempts = 2
|
|
app.config.heartbeatAttemptsWait = 30
|
|
app.registerModule(defaultHandler(handler))
|
|
|
|
let start = performance.now()
|
|
let err = await assert.isRejected(app.runVersion('static'))
|
|
let end = performance.now()
|
|
assert.match(err.message, /app/i)
|
|
assert.match(err.message, /failed/i)
|
|
assert.match(err.message, /static/i)
|
|
assert.match(err.message, /testapp/i)
|
|
assert.match(err.message, /time/i)
|
|
assert.match(err.message, /out/i)
|
|
assert.match(err.message, /30ms/i)
|
|
assert.ok(end - start > app.config.heartbeatAttempts * app.config.heartbeatAttemptsWait)
|
|
assert.strictEqual(called, 2)
|
|
assert.strictEqual(ctx.db.data.core.testapp.active, 'static')
|
|
})
|
|
|
|
t.test('should call with correct path', async function() {
|
|
const assertPath = '/test/something'
|
|
const handler = function(req, res) {
|
|
if (req.url === assertPath) {
|
|
res.writeHead(204); res.end(JSON.stringify({ a: 1 }))
|
|
} else {
|
|
res.statusCode = 400
|
|
res.end(JSON.stringify({ a: 1 }))
|
|
}
|
|
}
|
|
app.config.heartbeatAttempts = 3
|
|
app.registerModule(defaultHandler(handler))
|
|
|
|
let err = await assert.isRejected(app.runVersion('static'))
|
|
assert.match(err.message, /app/i)
|
|
assert.match(err.message, /failed/i)
|
|
assert.match(err.message, /static/i)
|
|
assert.match(err.message, /testapp/i)
|
|
assert.match(err.message, /400/i)
|
|
|
|
await app.http.closeServer()
|
|
app.registerModule(defaultHandler(handler))
|
|
|
|
app.config.heartbeatPath = assertPath
|
|
await app.runVersion('static')
|
|
|
|
assert.strictEqual(ctx.db.data.core.testapp.active, 'static')
|
|
})
|
|
})
|
|
|
|
t.skip().describe('#runVersion("version")', function() {
|
|
const assertConfig = util.getPathFromRoot('./db_test_applicationrun.json')
|
|
const assertPort = 22345
|
|
let ctx
|
|
let app
|
|
|
|
t.before(function() {
|
|
return fs.rm(util.getPathFromRoot('./testnoexisting'), { force: true, recursive: true })
|
|
.then(function() {
|
|
return fs.mkdir(util.getPathFromRoot('./testnoexisting'), { recursive: true })
|
|
})
|
|
})
|
|
|
|
t.after(function() {
|
|
return fs.rm(util.getPathFromRoot('./testnoexisting'), { force: true, recursive: true })
|
|
})
|
|
|
|
t.beforeEach(function() {
|
|
return createFakeContext({ }, util, assertConfig)
|
|
.then(function(res) {
|
|
ctx = res
|
|
let provider = new StaticProvider()
|
|
app = new Application(ctx, provider, 'testnoexisting')
|
|
app.config.port = assertPort
|
|
return app.ctx.db.write()
|
|
})
|
|
})
|
|
|
|
t.afterEach(function() {
|
|
return Promise.all([
|
|
fs.rm(assertConfig),
|
|
app.http.closeServer(),
|
|
])
|
|
})
|
|
|
|
t.test('when version is specified, should check if index.mjs exists', async function() {
|
|
const assertNotError = new Error('AI DO')
|
|
const assertTarget = util.getPathFromRoot('./testnoexisting/v100/index.mjs')
|
|
let stubFsStat = stub()
|
|
let provider = new StaticProvider()
|
|
app = new Application(ctx, provider, 'testnoexisting', {
|
|
fs: { stat: stubFsStat }
|
|
})
|
|
app.config.port = assertPort
|
|
stubFsStat.rejects(assertNotError)
|
|
|
|
let err = await assert.isRejected(app.runVersion('v100'))
|
|
assert.notStrictEqual(err, assertNotError)
|
|
assert.match(err.message, new RegExp(assertNotError.message))
|
|
assert.match(err.message, /index\.mjs/i)
|
|
assert.match(err.message, /testnoexisting/i)
|
|
assert.match(err.message, /v100/i)
|
|
assert.match(err.message, /missing/i)
|
|
assert.strictEqual(stubFsStat.firstCall[0], assertTarget)
|
|
|
|
assert.strictEqual(app.ctx.db.data.core.testnoexisting.active, 'v100')
|
|
let checkDb = await lowdb({}, ctx.log, assertConfig)
|
|
assert.strictEqual(checkDb.data.core.testnoexisting.active, 'v100')
|
|
})
|
|
|
|
t.test('when version is specified and file exists, should attempt to load module', async function() {
|
|
const assertError = new Error('Parallel Days')
|
|
await fs.mkdir(util.getPathFromRoot('./testnoexisting/v99'), { recursive: true })
|
|
await fs.writeFile(util.getPathFromRoot('./testnoexisting/v99/index.mjs'), `throw new Error('${assertError.message}')`)
|
|
|
|
let err = await assert.isRejected(app.runVersion('v99'))
|
|
assert.notStrictEqual(err, assertError)
|
|
assert.match(err.message, new RegExp(assertError.message))
|
|
assert.match(err.message, /testnoexisting/i)
|
|
assert.match(err.message, /v99/i)
|
|
assert.match(err.message, /index\.mjs/i)
|
|
|
|
assert.strictEqual(app.ctx.db.data.core.testnoexisting.active, 'v99')
|
|
let checkDb = await lowdb({}, ctx.log, assertConfig)
|
|
assert.strictEqual(checkDb.data.core.testnoexisting.active, 'v99')
|
|
})
|
|
|
|
t.test('when version is specified and file exists, should check if it has start', async function() {
|
|
await fs.mkdir(util.getPathFromRoot('./testnoexisting/v98'), { recursive: true })
|
|
await fs.writeFile(util.getPathFromRoot('./testnoexisting/v98/index.mjs'), ``)
|
|
|
|
let err = await assert.isRejected(app.runVersion('v98'))
|
|
assert.match(err.message, /testnoexisting/i)
|
|
assert.match(err.message, /v98/i)
|
|
assert.match(err.message, /start/i)
|
|
|
|
assert.strictEqual(app.ctx.db.data.core.testnoexisting.active, 'v98')
|
|
let checkDb = await lowdb({}, ctx.log, assertConfig)
|
|
assert.strictEqual(checkDb.data.core.testnoexisting.active, 'v98')
|
|
})
|
|
|
|
t.test('when version is specified and file exists and everything is okay, should work normally', async function() {
|
|
await fs.mkdir(util.getPathFromRoot('./testnoexisting/v97'), { recursive: true })
|
|
await fs.copyFile(
|
|
util.getPathFromRoot('./exampleindex.mjs'),
|
|
util.getPathFromRoot('./testnoexisting/v97/index.mjs')
|
|
)
|
|
|
|
app.ctx.log.info.reset()
|
|
app.ctx.log.event.info.reset()
|
|
|
|
await app.runVersion('v97')
|
|
|
|
assert.ok(app.ctx.log.info.called)
|
|
assert.ok(app.ctx.log.event.info.called)
|
|
})
|
|
})
|