From 51174031c8ff0aebd3c9accaac30b1c420e4b2d5 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 2 Jul 2023 15:50:24 +0200 Subject: [PATCH 1/4] add unit tests --- lib/time-tree.js | 8 ++- test/plugin-loaded-so-far.test.js | 108 ++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 test/plugin-loaded-so-far.test.js diff --git a/lib/time-tree.js b/lib/time-tree.js index 61e3ca3..96da672 100644 --- a/lib/time-tree.js +++ b/lib/time-tree.js @@ -72,8 +72,12 @@ class TimeTree { * @returns {TimeTreeNode} */ [kGetParent] (parent) { - const parentNode = this.tableLabel.get(parent) - return parentNode[parentNode.length - 1] + if (this.tableLabel.has(parent)) { + const parentNode = this.tableLabel.get(parent) + return parentNode[parentNode.length - 1] + } else { + return this.tableId.get('root') + } } /** diff --git a/test/plugin-loaded-so-far.test.js b/test/plugin-loaded-so-far.test.js new file mode 100644 index 0000000..0234602 --- /dev/null +++ b/test/plugin-loaded-so-far.test.js @@ -0,0 +1,108 @@ +'use strict' + +const { test } = require('tap') +const fastq = require('fastq') +const boot = require('..') +const { Plugin } = require('../lib/plugin') + +test('loadedSoFar resolves a Promise, if plugin.loaded is set to true', async (t) => { + t.plan(1) + const app = boot({}) + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { + done() + }, false, 0) + + plugin.loaded = true + + await t.resolves(plugin.loadedSoFar()) +}) + +test('loadedSoFar resolves a Promise, if plugin was loaded by avvio', async (t) => { + t.plan(2) + const app = boot({}) + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { + done() + }, false, 0) + + app._loadPlugin(plugin, function (err) { + t.equal(err, undefined) + }) + + await app.ready() + + await t.resolves(plugin.loadedSoFar()) +}) + +test('loadedSoFar resolves a Promise when plugin is loaded by avvio', t => { + t.plan(2) + const app = boot() + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { + setTimeout(done, 500) + }, false, 0) + + const loadStart = Date.now() + plugin.loadedSoFar().then(() => { + const thenTimestamp = Date.now() + t.ok((thenTimestamp - loadStart) > 500) + }).catch(err => t.error(err)) + + app._loadPlugin(plugin, function () { + const thenTimestamp = Date.now() + t.ok((thenTimestamp - loadStart) > 500) + }) + + plugin.loadedSoFar().then(() => { + t.ok() + }).catch(err => t.error(err)) +}) + +test('loadedSoFar resolves a Promise, if .after() has no error', async t => { + t.plan(1) + const app = boot() + + app.after = function (callback) { + callback(null, () => {}) + } + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { + done() + }, false, 0) + + app._loadPlugin(plugin, function () {}) + + await t.resolves(plugin.loadedSoFar()) +}) + +test('loadedSoFar rejects a Promise, if .after() has an error', async t => { + t.plan(1) + const app = boot() + + app.after = function (fn) { + fn(new Error('ArbitraryError'), () => {}) + } + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { + done() + }, false, 0) + + app._loadPlugin(plugin, function () {}) + + await t.rejects(plugin.loadedSoFar(), new Error('ArbitraryError')) +}) + +test('loadedSoFar resolves a Promise, if Plugin is attached to avvio after it the Plugin was instantiated', async t => { + t.plan(1) + + const plugin = new Plugin(fastq(null, null, 1), function (instance, opts, done) { + done() + }, false, 0) + + const promise = plugin.loadedSoFar() + + plugin.server = boot() + plugin.emit('start') + await t.resolves(promise) +}) From 577df40ffb0ca5fddeef1cd8bdbb1182e64e8e50 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 14 Aug 2023 11:40:09 +0200 Subject: [PATCH 2/4] improve failing test --- test/plugin-loaded-so-far.test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/plugin-loaded-so-far.test.js b/test/plugin-loaded-so-far.test.js index 0234602..9e30b67 100644 --- a/test/plugin-loaded-so-far.test.js +++ b/test/plugin-loaded-so-far.test.js @@ -35,7 +35,7 @@ test('loadedSoFar resolves a Promise, if plugin was loaded by avvio', async (t) await t.resolves(plugin.loadedSoFar()) }) -test('loadedSoFar resolves a Promise when plugin is loaded by avvio', t => { +test('loadedSoFar resolves a Promise when plugin is loaded by avvio', async t => { t.plan(2) const app = boot() @@ -46,17 +46,15 @@ test('loadedSoFar resolves a Promise when plugin is loaded by avvio', t => { const loadStart = Date.now() plugin.loadedSoFar().then(() => { const thenTimestamp = Date.now() - t.ok((thenTimestamp - loadStart) > 500) + t.ok((thenTimestamp - loadStart) > 500, thenTimestamp - loadStart) }).catch(err => t.error(err)) app._loadPlugin(plugin, function () { const thenTimestamp = Date.now() - t.ok((thenTimestamp - loadStart) > 500) + t.ok((thenTimestamp - loadStart) > 500, thenTimestamp - loadStart) }) - plugin.loadedSoFar().then(() => { - t.ok() - }).catch(err => t.error(err)) + await t.resolves(plugin.loadedSoFar()) }) test('loadedSoFar resolves a Promise, if .after() has no error', async t => { From 9548d992829dc823132cc32e0329cf7abd059213 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 15 Aug 2023 18:52:42 +0200 Subject: [PATCH 3/4] remove unclear test --- test/plugin-loaded-so-far.test.js | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/test/plugin-loaded-so-far.test.js b/test/plugin-loaded-so-far.test.js index 9e30b67..bdad133 100644 --- a/test/plugin-loaded-so-far.test.js +++ b/test/plugin-loaded-so-far.test.js @@ -35,28 +35,6 @@ test('loadedSoFar resolves a Promise, if plugin was loaded by avvio', async (t) await t.resolves(plugin.loadedSoFar()) }) -test('loadedSoFar resolves a Promise when plugin is loaded by avvio', async t => { - t.plan(2) - const app = boot() - - const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { - setTimeout(done, 500) - }, false, 0) - - const loadStart = Date.now() - plugin.loadedSoFar().then(() => { - const thenTimestamp = Date.now() - t.ok((thenTimestamp - loadStart) > 500, thenTimestamp - loadStart) - }).catch(err => t.error(err)) - - app._loadPlugin(plugin, function () { - const thenTimestamp = Date.now() - t.ok((thenTimestamp - loadStart) > 500, thenTimestamp - loadStart) - }) - - await t.resolves(plugin.loadedSoFar()) -}) - test('loadedSoFar resolves a Promise, if .after() has no error', async t => { t.plan(1) const app = boot() From de8bd443c283719edbf5564239085fe13e3a7073 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 15 Aug 2023 19:04:16 +0200 Subject: [PATCH 4/4] increase again coverage for time-tree.js --- lib/time-tree.js | 11 +++++++---- test/lib/time-tree.test.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/time-tree.js b/lib/time-tree.js index 96da672..5f02d68 100644 --- a/lib/time-tree.js +++ b/lib/time-tree.js @@ -72,11 +72,13 @@ class TimeTree { * @returns {TimeTreeNode} */ [kGetParent] (parent) { - if (this.tableLabel.has(parent)) { + if (parent === null) { + return null + } else if (this.tableLabel.has(parent)) { const parentNode = this.tableLabel.get(parent) return parentNode[parentNode.length - 1] } else { - return this.tableId.get('root') + return null } } @@ -96,7 +98,9 @@ class TimeTree { * @returns {TimeTreeNode["id"]} */ [kAddNode] (parent, label, start) { - const isRoot = parent === null + const parentNode = this[kGetParent](parent) + const isRoot = parentNode === null + if (isRoot) { this.root = { parent: null, @@ -111,7 +115,6 @@ class TimeTree { return this.root.id } - const parentNode = this[kGetParent](parent) const nodeId = `${label}-${Math.random()}` /** * @type {TimeTreeNode} diff --git a/test/lib/time-tree.test.js b/test/lib/time-tree.test.js index c0cb1a4..8bef526 100644 --- a/test/lib/time-tree.test.js +++ b/test/lib/time-tree.test.js @@ -114,6 +114,25 @@ test('TimeTree#start is adding a root element when parent is null', t => { t.type(rootNode.diff, 'number') }) +test('TimeTree#start is adding a root element when parent does not exist', t => { + t.plan(9) + + const tree = new TimeTree() + tree.start('invalid', 'root') + + const rootNode = tree.root + + t.type(rootNode, 'object') + t.equal(Object.keys(rootNode).length, 7) + t.equal(rootNode.parent, null) + t.equal(rootNode.id, 'root') + t.equal(rootNode.label, 'root') + t.ok(Array.isArray(rootNode.nodes)) + t.equal(rootNode.nodes.length, 0) + t.ok(Number.isInteger(rootNode.start)) + t.type(rootNode.diff, 'number') +}) + test('TimeTree#start parameter start can override automatically generated start time', t => { t.plan(1)