feat: add configurable persistent state file path and env tests
This commit is contained in:
@@ -10,8 +10,14 @@ function intFromEnv(name, fallback) {
|
|||||||
return Number.isInteger(parsed) ? parsed : fallback;
|
return Number.isInteger(parsed) ? parsed : fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function strFromEnv(name, fallback) {
|
||||||
|
const raw = process.env[name];
|
||||||
|
return raw && String(raw).trim() ? String(raw).trim() : fallback;
|
||||||
|
}
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
port: intFromEnv("PORT", 3000),
|
port: intFromEnv("PORT", 3000),
|
||||||
|
stateFilePath: strFromEnv("STATE_FILE_PATH", "./data/state.json"),
|
||||||
xWebhookSecret: process.env.X_WEBHOOK_SECRET || "dev-x-secret",
|
xWebhookSecret: process.env.X_WEBHOOK_SECRET || "dev-x-secret",
|
||||||
polarWebhookSecret: process.env.POLAR_WEBHOOK_SECRET || "dev-polar-secret",
|
polarWebhookSecret: process.env.POLAR_WEBHOOK_SECRET || "dev-polar-secret",
|
||||||
credit: {
|
credit: {
|
||||||
|
|||||||
60
test/config.test.js
Normal file
60
test/config.test.js
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
const test = require("node:test");
|
||||||
|
const assert = require("node:assert/strict");
|
||||||
|
|
||||||
|
test("config uses defaults when env is missing", () => {
|
||||||
|
const previous = {
|
||||||
|
PORT: process.env.PORT,
|
||||||
|
STATE_FILE_PATH: process.env.STATE_FILE_PATH,
|
||||||
|
};
|
||||||
|
|
||||||
|
delete process.env.PORT;
|
||||||
|
delete process.env.STATE_FILE_PATH;
|
||||||
|
|
||||||
|
delete require.cache[require.resolve("../src/config")];
|
||||||
|
const { config } = require("../src/config");
|
||||||
|
|
||||||
|
assert.equal(config.port, 3000);
|
||||||
|
assert.equal(config.stateFilePath, "./data/state.json");
|
||||||
|
|
||||||
|
if (previous.PORT === undefined) {
|
||||||
|
delete process.env.PORT;
|
||||||
|
} else {
|
||||||
|
process.env.PORT = previous.PORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (previous.STATE_FILE_PATH === undefined) {
|
||||||
|
delete process.env.STATE_FILE_PATH;
|
||||||
|
} else {
|
||||||
|
process.env.STATE_FILE_PATH = previous.STATE_FILE_PATH;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("config reads state path and numeric env overrides", () => {
|
||||||
|
const previous = {
|
||||||
|
PORT: process.env.PORT,
|
||||||
|
STATE_FILE_PATH: process.env.STATE_FILE_PATH,
|
||||||
|
};
|
||||||
|
|
||||||
|
process.env.PORT = "8080";
|
||||||
|
process.env.STATE_FILE_PATH = "/data/prod-state.json";
|
||||||
|
|
||||||
|
delete require.cache[require.resolve("../src/config")];
|
||||||
|
const { config } = require("../src/config");
|
||||||
|
|
||||||
|
assert.equal(config.port, 8080);
|
||||||
|
assert.equal(config.stateFilePath, "/data/prod-state.json");
|
||||||
|
|
||||||
|
if (previous.PORT === undefined) {
|
||||||
|
delete process.env.PORT;
|
||||||
|
} else {
|
||||||
|
process.env.PORT = previous.PORT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (previous.STATE_FILE_PATH === undefined) {
|
||||||
|
delete process.env.STATE_FILE_PATH;
|
||||||
|
} else {
|
||||||
|
process.env.STATE_FILE_PATH = previous.STATE_FILE_PATH;
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user