Files
notification-wakeup-service/test/env.test.ts
T

61 lines
1.9 KiB
TypeScript

import assert from "node:assert/strict";
import { afterEach, beforeEach, describe, it } from "node:test";
import { smsConfig } from "../src/env.js";
const KEYS = ["SMS_ENABLED", "SMS_REQUIRE_ACTION_CLAIM"] as const;
const saved: Record<string, string | undefined> = {};
beforeEach(() => {
for (const key of KEYS) {
saved[key] = process.env[key];
delete process.env[key];
}
});
afterEach(() => {
for (const key of KEYS) {
if (saved[key] === undefined) delete process.env[key];
else process.env[key] = saved[key];
}
});
describe("smsConfig boolean flags", () => {
it("uses the defaults when unset or empty", () => {
assert.equal(smsConfig().enabled, false);
assert.equal(smsConfig().requireActionClaim, true);
process.env.SMS_ENABLED = "";
process.env.SMS_REQUIRE_ACTION_CLAIM = "";
assert.equal(smsConfig().enabled, false);
assert.equal(smsConfig().requireActionClaim, true);
});
it("accepts every truthy spelling", () => {
for (const value of ["true", "TRUE", "1", "yes", "On"]) {
process.env.SMS_ENABLED = value;
assert.equal(smsConfig().enabled, true, value);
}
});
it("accepts every falsy spelling", () => {
for (const value of ["false", "FALSE", "0", "no", "Off"]) {
process.env.SMS_REQUIRE_ACTION_CLAIM = value;
assert.equal(smsConfig().requireActionClaim, false, value);
}
});
it("ignores surrounding whitespace, as docker --env-file leaves it", () => {
process.env.SMS_ENABLED = "true ";
assert.equal(smsConfig().enabled, true);
process.env.SMS_REQUIRE_ACTION_CLAIM = " false\t";
assert.equal(smsConfig().requireActionClaim, false);
});
it("falls back to the default on an unrecognized value", () => {
process.env.SMS_ENABLED = '"true"';
assert.equal(smsConfig().enabled, false);
process.env.SMS_REQUIRE_ACTION_CLAIM = "nope";
assert.equal(smsConfig().requireActionClaim, true);
});
});