Skip to content

Commit

Permalink
Separate test files, pass internal names
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Sep 9, 2024
1 parent 213f50f commit 712b185
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 47 deletions.
9 changes: 8 additions & 1 deletion lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,11 @@ const parseMarkdown = (src) => {
return scenario;
};

module.exports = { parseMarkdown };
module.exports = {
Action,
Scenario,
Step,
parseLine,
parseStep,
parseMarkdown,
};
2 changes: 1 addition & 1 deletion lib/metabot.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,4 @@ class Metabot {
}
}

module.exports = { Chat, Metabot };
module.exports = { MENU, Chat, Metabot };
25 changes: 25 additions & 0 deletions test/markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const test = require('node:test');
const assert = require('node:assert');
const path = require('node:path');
const fsp = require('node:fs/promises');
const EventEmitter = require('node:events');
const { parseMarkdown } = require('../lib/markdown.js');

const PATH = 'test/Example';

test('Parse markdown to Scenario', async () => {
const scenarioFile = path.join(PATH, 'scenario.md');
const data = await fsp.readFile(scenarioFile, 'utf8');
const scenario = parseMarkdown(data);
assert.ok(scenario instanceof EventEmitter);
assert.ok(scenario.steps instanceof Map);
assert.strictEqual(scenario.steps.size, 3);
assert.ok(scenario.menu instanceof Array);
assert.strictEqual(scenario.menu.length, 3);
assert.ok(Object.getPrototypeOf(scenario.entry).constructor.name, 'Step');
assert.strictEqual(scenario.steps.get('1'), scenario.entry);
assert.strictEqual(typeof scenario.name, 'string');
assert.strictEqual(typeof scenario.description, 'string');
});
58 changes: 13 additions & 45 deletions test/metabot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,22 @@

const test = require('node:test');
const assert = require('node:assert');
const path = require('node:path');
const fsp = require('node:fs/promises');
const EventEmitter = require('node:events');
const { parseMarkdown, Stopwatch } = require('..');
const { MENU, Chat, Metabot } = require('../lib/metabot.js');

const PATH = 'test/Example';

test('Parse markdown to Scenario', async () => {
const scenarioFile = path.join(PATH, 'scenario.md');
const data = await fsp.readFile(scenarioFile, 'utf8');
const scenario = parseMarkdown(data);
assert.ok(scenario instanceof EventEmitter);
assert.ok(scenario.steps instanceof Map);
assert.strictEqual(scenario.steps.size, 3);
assert.ok(scenario.menu instanceof Array);
assert.strictEqual(scenario.menu.length, 3);
assert.ok(Object.getPrototypeOf(scenario.entry).constructor.name, 'Step');
assert.strictEqual(scenario.steps.get('1'), scenario.entry);
assert.strictEqual(typeof scenario.name, 'string');
assert.strictEqual(typeof scenario.description, 'string');
});

test('Stopwatch initial start', async () => {
const options = { timeout: 1000 };
const timer = new Stopwatch(() => {}, options);
assert.strictEqual(timer.state.timeout, 1000);
assert.strictEqual(typeof timer.state.start, 'number');
assert.strictEqual(timer.state.done, false);
test('Check MENU constant', async () => {
const menuKeys = Object.keys(MENU);
const allStrings = menuKeys.every((name) => typeof name === 'string');
assert.strictEqual(allStrings, true);
for (const name of menuKeys) {
const val = MENU[name];
assert.strictEqual(val.constructor.name, 'AsyncFunction');
}
});

test('Stopwatch initial start', async () => {
const options = { timeout: 1000 };
const timer = new Stopwatch(() => {}, options);
assert.strictEqual(typeof timer.start, 'number');
assert.strictEqual(typeof timer.callback, 'function');
assert.strictEqual(timer.state.timeout, 1000);
assert.strictEqual(typeof timer.state.start, 'number');
assert.strictEqual(timer.state.done, false);
assert.ok(Object.getPrototypeOf(timer.timer).constructor.name, 'Timer');
timer.stop();
test('Chat stub', () => {
assert.ok(Chat);
});

test('Stopwatch restore', async () => {
const options = { start: 1725824170365, timeout: 1000, done: false };
const timer = new Stopwatch(() => {}, options);
assert.strictEqual(timer.state.timeout, 1000);
assert.strictEqual(timer.state.start, 1725824170365);
assert.strictEqual(timer.state.done, false);
assert.strictEqual(timer.remaining, 0);
timer.stop();
test('Metabot stub', () => {
assert.ok(Metabot);
});
35 changes: 35 additions & 0 deletions test/stopwatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const test = require('node:test');
const assert = require('node:assert');
const { Stopwatch } = require('../lib/stopwatch.js');

test('Stopwatch initial start', async () => {
const options = { timeout: 1000 };
const timer = new Stopwatch(() => {}, options);
assert.strictEqual(timer.state.timeout, 1000);
assert.strictEqual(typeof timer.state.start, 'number');
assert.strictEqual(timer.state.done, false);
});

test('Stopwatch initial start', async () => {
const options = { timeout: 1000 };
const timer = new Stopwatch(() => {}, options);
assert.strictEqual(typeof timer.start, 'number');
assert.strictEqual(typeof timer.callback, 'function');
assert.strictEqual(timer.state.timeout, 1000);
assert.strictEqual(typeof timer.state.start, 'number');
assert.strictEqual(timer.state.done, false);
assert.ok(Object.getPrototypeOf(timer.timer).constructor.name, 'Timer');
timer.stop();
});

test('Stopwatch restore', async () => {
const options = { start: 1725824170365, timeout: 1000, done: false };
const timer = new Stopwatch(() => {}, options);
assert.strictEqual(timer.state.timeout, 1000);
assert.strictEqual(timer.state.start, 1725824170365);
assert.strictEqual(timer.state.done, false);
assert.strictEqual(timer.remaining, 0);
timer.stop();
});

0 comments on commit 712b185

Please sign in to comment.