diff options
author | EuAndreh <eu@euandre.org> | 2024-02-25 06:46:52 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-02-25 06:46:52 -0300 |
commit | 1f0c90b9a34872c3a33ea12c6e5e329a1f98b213 (patch) | |
tree | b4522548f89a2abe28053d47748b0f9adee8c164 /tests/js/hero.mjs | |
parent | Explicit import from "node:process"; move log() to hero.mjs (diff) | |
download | papod-1f0c90b9a34872c3a33ea12c6e5e329a1f98b213.tar.gz papod-1f0c90b9a34872c3a33ea12c6e5e329a1f98b213.tar.xz |
src/hero.mjs: Promote log() to fancy logger object
Diffstat (limited to 'tests/js/hero.mjs')
-rw-r--r-- | tests/js/hero.mjs | 151 |
1 files changed, 139 insertions, 12 deletions
diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs index 5507375..1e1668d 100644 --- a/tests/js/hero.mjs +++ b/tests/js/hero.mjs @@ -1,5 +1,6 @@ -import assert from "node:assert/strict"; -import http from "node:http"; +import assert from "node:assert/strict"; +import http from "node:http"; +import procees from "node:process"; import * as runner from "../runner.mjs"; import * as u from "../../src/utils.mjs"; @@ -15,7 +16,11 @@ import { extractQueryParams, handleRequest, makeRequestListener, - log, + loggerDefaults, + loggerGlobals, + configLogger, + logit, + makeLogger, interceptorsFn, interceptors, defaultInterceptors, @@ -632,17 +637,137 @@ const test_makeRequestListener = t => { }); }; -const test_log = t => { - t.start("log()"); +const test_configLogger = t => { + t.start("configLogger()"); + + t.test("globals starts empty", () => { + assert.deepEqual(loggerGlobals, {}); + }); + + t.test("is gets becomes we assign it", () => { + const globals = { + app: "my-app", + color: "green", + version: "deadbeef", + }; + configLogger(globals); + assert.deepEqual(loggerGlobals, globals); + }); + + t.test("we can reset it", () => { + configLogger({}); + assert.deepEqual(loggerGlobals, {}); + }); +}; + +const test_logit = t => { + t.start("logit()"); t.test("we can log data", () => { - log({ a: 1, type: "log-test" }); + configLogger({ app: "hero-based app" }); + const contents = []; + const writerFn = x => contents.push(x); + + logit(writerFn, "my level", { a: 1, type: "log-test" }); + assert.deepEqual(contents.map(JSON.parse), [{ + ...loggerDefaults, + app: "hero-based app", + level: "my level", + a: 1, + type: "log-test", + }]); + + // reset the logger out of the values specific to this test + configLogger({}); + }); + + t.test("the object can change previous fallback values", () => { + const contents = []; + const writerFn = x => contents.push(x); + + configLogger({ + level: "unseen", + a: "unseen", + }); + logit(writerFn, "overwritten by level", { a: "overwritten by o" }) + + configLogger({ + pid: "overwritten by loggerGlobals", + }); + logit(writerFn, "unseen", { level: "overwritten by o" }); + + // reset the logger out of the values specific to this test + configLogger({}); + + assert.deepEqual(contents.map(JSON.parse), [ + { + ...loggerDefaults, + pid: process.pid, + level: "overwritten by level", + a: "overwritten by o", + }, + { + ...loggerDefaults, + pid: "overwritten by loggerGlobals", + level: "overwritten by o", + }, + ]); + }); t.test("we can't log unserializable things", () => { const obj = { self: null }; obj.self = obj; - assert.throws(() => log(obj), TypeError); + assert.throws(() => logit(obj), TypeError); + }); +}; + +const test_makeLogger = t => { + t.start("makeLogger()"); + + t.test("various log levels", () => { + const contents = []; + const writerFn = x => contents.push(x); + const log = makeLogger(writerFn); + + log.info ({ type: "expected" }); + log.warn ({ type: "worrysome" }); + log.error({ type: "bad" }); + assert.deepEqual(contents.map(JSON.parse), [ + { + ...loggerDefaults, + level: "INFO", + type: "expected", + }, + { + ...loggerDefaults, + level: "WARN", + type: "worrysome", + }, + { + ...loggerDefaults, + level: "ERROR", + type: "bad", + }, + ]); + }); + + t.test("debug only works when $DEBUG is set", () => { + const contents = []; + const writerFn = x => contents.push(x); + const log = makeLogger(writerFn); + + log.debug({ x: "ignored" }); + process.env.DEBUG = 1; + log.debug({ x: "seen" }); + delete process.env.DEBUG; + log.debug({ x: "ignored" }); + + assert.deepEqual(contents.map(JSON.parse), [{ + ...loggerDefaults, + level: "DEBUG", + x: "seen", + }]); }); }; @@ -686,7 +811,7 @@ const test_interceptorsFn = t => { t.test("we log before and after next()", async () => { const contents = []; - const logger = x => contents.push(x); + const logger = { info: x => contents.push(x) }; const status = 201; const req = { id: "an ID", @@ -810,7 +935,7 @@ const test_interceptorsFn = t => { t.test(`an error is thrown if "status" is missing`, async () => { const contents = []; - const logger = x => contents.push(x); + const logger = { error: x => contents.push(x) }; assert.deepEqual( await interceptorsFn({ logger }).serverError({ id: 123 }, next), { @@ -830,7 +955,7 @@ const test_interceptorsFn = t => { t.test("we turn a handler error into a 500 response", async () => { const contents = []; - const logger = x => contents.push(x); + const logger = { error: x => contents.push(x) }; assert.deepEqual( await interceptorsFn({ logger }).serverError( { id: "some ID" }, @@ -929,7 +1054,7 @@ const test_wrapHandler = t => { const uuidFn = () => `${i++}`; const contents = []; - const logger = x => contents.push(x); + const logger = { info: x => contents.push(x) }; const interceptors = interceptorsFn({uuidFn, logger}); @@ -1203,7 +1328,9 @@ await runner.runTests([ test_extractQueryParams, test_handleRequest, test_makeRequestListener, - test_log, + test_configLogger, + test_logit, + test_makeLogger, test_interceptorsFn, test_chainInterceptors, test_wrapHandler, |