From 67606b9b3b10d75216a5fadd481148c9eec70f10 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Thu, 10 Mar 2022 13:08:02 +0000 Subject: [PATCH] Fixed lib and finished implementing it --- core/application.mjs | 2 +- core/lib.mjs | 7 ++++- index.mjs | 2 +- test/lib.test.mjs | 64 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 test/lib.test.mjs diff --git a/core/application.mjs b/core/application.mjs index f3ddbe2..153ee34 100644 --- a/core/application.mjs +++ b/core/application.mjs @@ -379,7 +379,7 @@ export default class Application extends EventEmitter { rej(errTimeout) }, this.config.startWaitUntilFail) - let startRes = this.module.start(this.http, this.config.port, this.ctx) + let startRes = this.module.start(this.http, this.config.port || null, this.ctx) if (startRes && startRes.then) { return startRes.then(res, rej) } diff --git a/core/lib.mjs b/core/lib.mjs index f1a5094..1f85acb 100644 --- a/core/lib.mjs +++ b/core/lib.mjs @@ -1,6 +1,7 @@ import Util from './util.mjs' import getLog from './log.mjs' import GetDB from './db.mjs' +import Application from './application.mjs' import StaticProvider from './providers/static.mjs' import Core from './core.mjs' @@ -11,7 +12,7 @@ export default class ServiceCore { } this._root_import_meta_url = root_import_meta_url this.util = new Util(this._root_import_meta_url) - this.dbfilename = dbname + this.dbfilename = dbfilename this.log = getLog(name) this.name = name this.config = {} @@ -20,6 +21,10 @@ export default class ServiceCore { this.app = null } + setConfig(config) { + this.config[this.name] = config + } + async init(module = null) { this.db = await GetDB(this.config, this.log, this.dbfilename) this.core = new Core(this.db, this.util, this.log) diff --git a/index.mjs b/index.mjs index 4643cc3..1ad1c94 100644 --- a/index.mjs +++ b/index.mjs @@ -1,6 +1,6 @@ import bunyan from 'bunyan-lite' import { runner } from './core/runner.mjs' -import { ServiceCore } from './core/lib.mjs' +import ServiceCore from './core/lib.mjs' import Core from './core/core.mjs' import Application from './core/application.mjs' import Util from './core/util.mjs' diff --git a/test/lib.test.mjs b/test/lib.test.mjs new file mode 100644 index 0000000..752bb32 --- /dev/null +++ b/test/lib.test.mjs @@ -0,0 +1,64 @@ +import { Eltro as t, assert, stub } from 'eltro' +import * as sc from '../index.mjs' + +t.describe('', function() { + const module = { + start: stub() + } + + t.beforeEach(function() { + module.start.reset() + }) + + t.test('should have ServiceCore defined', function() { + assert.ok(sc.ServiceCore) + }) + + t.test('constructor should work', function() { + const assertAppName = 'Gondola' + let core = new sc.ServiceCore(assertAppName, import.meta.url) + assert.strictEqual(core.util._root_import_meta_url, import.meta.url) + assert.strictEqual(core.name, assertAppName) + }) + + t.test('should support proper init', async function() { + const assertAppName = 'Hero Combat' + let core = new sc.ServiceCore(assertAppName, import.meta.url) + await core.init(module) + + assert.strictEqual(core.app.name, assertAppName) + assert.strictEqual(core.app.module, module) + }) + + t.test('should call module start', async function() { + const assertError = new Error('Inbo') + module.start.rejects(assertError) + + let core = new sc.ServiceCore('testapp', import.meta.url) + await core.init(module) + let err = await assert.isRejected(core.run()) + assert.strictEqual(err, assertError) + assert.strictEqual(module.start.firstCall[0], core.app.http) + assert.strictEqual(module.start.firstCall[1], null) + assert.strictEqual(module.start.firstCall[2], core.app.ctx) + }) + + t.test('should support overwriting config', async function() { + const assertError = new Error('Inbo') + const assertPort = 9382 + module.start.rejects(assertError) + + let core = new sc.ServiceCore('testapp', import.meta.url) + + core.setConfig({ + port: assertPort + }) + await core.init(module) + + let err = await assert.isRejected(core.run()) + assert.strictEqual(err, assertError) + assert.strictEqual(module.start.firstCall[0], core.app.http) + assert.strictEqual(module.start.firstCall[1], assertPort) + assert.strictEqual(module.start.firstCall[2], core.app.ctx) + }) +})