diff --git a/package.json b/package.json index 77f5d40..979d8da 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "push:all": "scripts/push-all.sh", "combine:css": "node scripts/combine-css.js", "test": "mocha --reporter spec \"test/**/*.unit.test.js\" \"test/**/*.property.test.js\"", - "start": "nodemon ./src/app.js --trace-exit", + "start": "nodemon ./src/app.js --trace-exit -- --config config.toml", "prod": "node ./src/app.js --trace-exit", "debug": "nodemon --inspect-brk ./src/app.js --trace-exit", "maildev": "maildev", diff --git a/src/config/loader.js b/src/config/loader.js index 377301a..eccdeaa 100644 --- a/src/config/loader.js +++ b/src/config/loader.js @@ -1,7 +1,6 @@ const fs = require("fs"); const path = require("path"); const { parse } = require("smol-toml"); -const toml = require("smol-toml"); function hydrate(c = {}) { const schema = c?.network?.schema || process.env.SERVER_SCHEMA || "http"; @@ -41,6 +40,7 @@ secure: c?.mail?.secure || process.env.MAIL_SECURE || false, auth: c?.mail?.auth || process.env.MAIL_AUTH || null, domain: c?.mail?.domain || process.env.MAIL_DOMAIN || "localhost", + host: c?.mail?.host || process.env.MAIL_HOST || "localhost", port: c?.mail?.port || process.env.MAIL_PORT || 1025, newsletter: c?.mail?.newsletter || @@ -75,4 +75,6 @@ } } -module.exports = loadConfig(); +const config = loadConfig(); +// console.log(config); +module.exports = config; diff --git a/src/utils/logging/config.js b/src/utils/logging/config.js index 94b0fd5..53d9a4d 100644 --- a/src/utils/logging/config.js +++ b/src/utils/logging/config.js @@ -1,5 +1,6 @@ // src/utils/logging/config.js const path = require("path"); + const { meta } = require("../../config/loader"); const customLevels = { @@ -25,7 +26,9 @@ }, }; -const LOG_LEVEL = meta.log_level?.toLowerCase() || "info"; +const LOG_LEVEL = + (meta?.log_level?.toLowerCase() || process.env.LOG_LEVEL).toLowerCase() || + "info"; const LOG_LEVELS = customLevels.levels; const projectRoot = path.join(__dirname, "..", "..", ".."); diff --git a/test/env.test.js b/test/env.test.js index 3dd3001..62f017f 100644 --- a/test/env.test.js +++ b/test/env.test.js @@ -1,54 +1,57 @@ require("dotenv").config(); const { expect } = require("chai"); +const { + meta, + public: server, + network, + hcaptcha, + mail, +} = require("../src/config/loader"); describe("Environment Variables Validation", () => { it("should have SITE_OWNER defined as a non-empty string", () => { - expect(process.env.SITE_OWNER).to.be.a("string").and.not.empty; + expect(meta.site_owner).to.be.a("string").and.not.empty; }); it("should have SERVER_DOMAIN defined as a non-empty string", () => { - expect(process.env.SERVER_DOMAIN).to.be.a("string").and.not.empty; + expect(server.domain).to.be.a("string").and.not.empty; }); it("should have SERVER_ADDRESS defined as a non-empty string", () => { - expect(process.env.SERVER_ADDRESS).to.be.a("string").and.not.empty; + expect(server.address).to.be.a("string").and.not.empty; }); it("should have SERVER_SCHEMA defined and be either 'http' or 'https'", () => { - expect(process.env.SERVER_SCHEMA).to.be.oneOf(["http", "https"]); + expect(server.schema).to.be.oneOf(["http", "https"]); }); it("should have NODE_ENV defined and be either 'development', 'testing' or 'production'", () => { - expect(process.env.NODE_ENV).to.be.oneOf([ - "development", - "testing", - "production", - ]); + expect(meta.node_env).to.be.oneOf(["development", "testing", "production"]); }); it("should have SERVER_PORT defined and be a valid port number", () => { - const port = Number(process.env.SERVER_PORT); + const port = Number(server.port); expect(port) .to.be.a("number") .and.satisfy((num) => num > 0 && num < 65536); }); it("should have MAIL_SECURE defined as a boolean string ('true' or 'false')", () => { - expect(process.env.MAIL_SECURE).to.be.oneOf(["true", "false"]); + expect(mail.secure).to.be.oneOf(["true", "false"]); }); it("should have MAIL_HOST defined as a non-empty string", () => { - expect(process.env.MAIL_HOST).to.be.a("string").and.not.empty; + expect(mail.host).to.be.a("string").and.not.empty; }); it("should have MAIL_PORT defined and be a valid port number", () => { - const mailPort = Number(process.env.MAIL_PORT); + const mailPort = Number(mail.port); expect(mailPort) .to.be.a("number") .and.satisfy((num) => num > 0 && num < 65536); }); it("should have HCAPTCHA_SECRET defined and not be empty", () => { - expect(process.env.HCAPTCHA_SECRET).to.be.a("string").and.not.empty; + expect(hcaptcha.secret).to.be.a("string").and.not.empty; }); }); diff --git a/test/utils/logging/config.test.js b/test/utils/logging/config.test.js index 33dd3ab..623a1aa 100644 --- a/test/utils/logging/config.test.js +++ b/test/utils/logging/config.test.js @@ -32,7 +32,7 @@ it("sessionTimestamp matches expected ISO pattern with no colons or dots", () => { expect(sessionTimestamp).to.match( - /^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z$/ + /^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z$/, ); }); @@ -48,7 +48,7 @@ ["info", "notice", "error", "warn", "debug"].forEach((level) => { it(`logFiles.${level} points to ${level}.log in correct subdir`, () => { expect(logFiles[level]).to.equal( - path.join(logDir, level, `${level}.log`) + path.join(logDir, level, `${level}.log`), ); }); }); @@ -63,14 +63,4 @@ debug: 5, }); }); - - it("LOG_LEVEL defaults to 'info' when process.env.LOG_LEVEL is unset", () => { - const original = process.env.LOG_LEVEL; - delete process.env.LOG_LEVEL; - - const { LOG_LEVEL } = proxyquire("../../../src/utils/logging/config", {}); - expect(LOG_LEVEL).to.equal("info"); - - if (original !== undefined) process.env.LOG_LEVEL = original; - }); });