diff options
Diffstat (limited to 'tests/js')
-rw-r--r-- | tests/js/hero.mjs | 114 |
1 files changed, 113 insertions, 1 deletions
diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs index 1e1668d..51fa84a 100644 --- a/tests/js/hero.mjs +++ b/tests/js/hero.mjs @@ -26,6 +26,8 @@ import { defaultInterceptors, chainInterceptors, wrapHandler, + actionsFn, + lineHandlerFn, buildRoutes, promisifyServer, buildServer, @@ -758,9 +760,10 @@ const test_makeLogger = t => { const log = makeLogger(writerFn); log.debug({ x: "ignored" }); - process.env.DEBUG = 1; + process.env.DEBUG = "1"; log.debug({ x: "seen" }); delete process.env.DEBUG; + // call the function that toggles log.debug({ x: "ignored" }); assert.deepEqual(contents.map(JSON.parse), [{ @@ -1106,6 +1109,113 @@ const test_wrapHandler = t => { }); }; +const test_actionsFn = t => { + { + t.start(`actionsFn()["toggle-debug-env"()]`); + + t.test("we can toggle back and forth", () => { + const contents = []; + const logger = { info: x => contents.push(x) }; + const actions = actionsFn({logger}); + + assert.equal(process.env.DEBUG, undefined); + actions["toggle-debug-env"]("action-text-1"); + assert.equal(process.env.DEBUG, "1"); + actions["toggle-debug-env"]("action-text-2"); + assert.equal(process.env.DEBUG, undefined); + + assert.deepEqual(contents, [ + { + type: "action-response", + action: "action-text-1", + message: "toggle process.env.DEBUG", + before: null, + after: "1", + }, + { + type: "action-response", + action: "action-text-2", + message: "toggle process.env.DEBUG", + before: "1", + after: null, + }, + ]); + }); + } + + { + t.start(`actionsFn()["config-dump"]()`); + + t.test("we just dump data as a log entry", () => { + const contents = []; + const logger = { info: x => contents.push(x) }; + const actions = actionsFn({logger}); + + configLogger({}); + actions["config-dump"]("first-call"); + configLogger({ some: "thing", }); + actions["config-dump"]("second-call"); + configLogger({}); + + assert.deepEqual(contents, [ + { + type: "action-response", + action: "first-call", + data: { + pid: process.pid, + ppid: process.ppid, + tool: "hero", + }, + }, + { + type: "action-response", + action: "second-call", + data: { + pid: process.pid, + ppid: process.ppid, + tool: "hero", + some: "thing", + }, + }, + + ]); + }); + } +}; + +const test_lineHandlerFn = t => { + t.start("lineHandlerFn()"); + + t.test("empty values", () => { + const contents = []; + const logger = { info: x => contents.push(x) }; + const lineHandler = lineHandlerFn({logger, actionsMap: {}}); + + lineHandler(""); + lineHandler("{}"); + lineHandler(`{"action": "this-action-does-not-exist"}`); + + assert.deepEqual(contents.map(x => x.type), [ + "invalid-cmd-input", + "missing-key-action", + "unsupported-action", + ]); + }); + + t.test("calling an action", () => { + const contents = []; + const logger = { info: x => contents.push(x) }; + const lineHandler = lineHandlerFn({ logger: null, actionsMap: { + "an-action": (arg1, arg2, arg3) => [arg1, arg2, arg3], + }}); + + const ret1 = lineHandler(`{"action": "an-action"}`); + const ret2 = lineHandler(`{"action": "an-action", "args": [1, "text", 2]}`); + assert.deepEqual(ret1, ["an-action", undefined, undefined]); + assert.deepEqual(ret2, ["an-action", 1, "text"]); + }); +}; + const test_buildRoutes = t => { t.start("buildRoutes()"); @@ -1334,6 +1444,8 @@ await runner.runTests([ test_interceptorsFn, test_chainInterceptors, test_wrapHandler, + test_actionsFn, + test_lineHandlerFn, test_buildRoutes, test_promisifyServer, test_buildServer, |