|
| 1 | +import { afterEach, describe, expect, it } from "bun:test"; |
| 2 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { Config } from "./index"; |
| 6 | + |
| 7 | +const ENV_KEYS = [ |
| 8 | + "ALERT_DAILY_SPEND_USD", |
| 9 | + "ALERT_TOKENS_PER_HOUR", |
| 10 | + "ALERT_REQUEST_TOKENS", |
| 11 | + "ALERT_ANOMALY_ENABLED", |
| 12 | + "ALERT_ANOMALY_INTERVAL_MINUTES", |
| 13 | + "ALERT_COOLDOWN_MINUTES", |
| 14 | + "ALERT_WEBHOOK_URL", |
| 15 | +] as const; |
| 16 | + |
| 17 | +const ORIGINAL_ENV: Record<string, string | undefined> = {}; |
| 18 | +for (const key of ENV_KEYS) { |
| 19 | + ORIGINAL_ENV[key] = process.env[key]; |
| 20 | +} |
| 21 | + |
| 22 | +function makeConfig(): { config: Config; cleanup: () => void } { |
| 23 | + const dir = mkdtempSync(join(tmpdir(), "better-ccflare-config-")); |
| 24 | + return { |
| 25 | + config: new Config(join(dir, "config.json")), |
| 26 | + cleanup: () => rmSync(dir, { recursive: true, force: true }), |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +describe("alert config settings", () => { |
| 31 | + afterEach(() => { |
| 32 | + for (const key of ENV_KEYS) { |
| 33 | + const original = ORIGINAL_ENV[key]; |
| 34 | + if (original === undefined) { |
| 35 | + delete process.env[key]; |
| 36 | + } else { |
| 37 | + process.env[key] = original; |
| 38 | + } |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + it("returns defaults when nothing is configured", () => { |
| 43 | + for (const key of ENV_KEYS) { |
| 44 | + delete process.env[key]; |
| 45 | + } |
| 46 | + const { config, cleanup } = makeConfig(); |
| 47 | + |
| 48 | + try { |
| 49 | + expect(config.getAlertDailySpendUsd()).toBe(0); |
| 50 | + expect(config.getAlertTokensPerHour()).toBe(0); |
| 51 | + expect(config.getAlertRequestTokens()).toBe(0); |
| 52 | + expect(config.getAlertAnomalyEnabled()).toBe(false); |
| 53 | + expect(config.getAlertAnomalyIntervalMinutes()).toBe(15); |
| 54 | + expect(config.getAlertCooldownMinutes()).toBe(60); |
| 55 | + expect(config.getAlertWebhookUrl()).toBe(""); |
| 56 | + } finally { |
| 57 | + cleanup(); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it("honors environment variable overrides", () => { |
| 62 | + process.env.ALERT_DAILY_SPEND_USD = "25.5"; |
| 63 | + process.env.ALERT_TOKENS_PER_HOUR = "500000"; |
| 64 | + process.env.ALERT_REQUEST_TOKENS = "200000"; |
| 65 | + process.env.ALERT_ANOMALY_ENABLED = "true"; |
| 66 | + process.env.ALERT_ANOMALY_INTERVAL_MINUTES = "30"; |
| 67 | + process.env.ALERT_COOLDOWN_MINUTES = "120"; |
| 68 | + process.env.ALERT_WEBHOOK_URL = "https://example.com/hook"; |
| 69 | + const { config, cleanup } = makeConfig(); |
| 70 | + |
| 71 | + try { |
| 72 | + expect(config.getAlertDailySpendUsd()).toBe(25.5); |
| 73 | + expect(config.getAlertTokensPerHour()).toBe(500000); |
| 74 | + expect(config.getAlertRequestTokens()).toBe(200000); |
| 75 | + expect(config.getAlertAnomalyEnabled()).toBe(true); |
| 76 | + expect(config.getAlertAnomalyIntervalMinutes()).toBe(30); |
| 77 | + expect(config.getAlertCooldownMinutes()).toBe(120); |
| 78 | + expect(config.getAlertWebhookUrl()).toBe("https://example.com/hook"); |
| 79 | + } finally { |
| 80 | + cleanup(); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + it("treats non-true anomaly env values as disabled", () => { |
| 85 | + process.env.ALERT_ANOMALY_ENABLED = "disabled"; |
| 86 | + const { config, cleanup } = makeConfig(); |
| 87 | + |
| 88 | + try { |
| 89 | + expect(config.getAlertAnomalyEnabled()).toBe(false); |
| 90 | + } finally { |
| 91 | + cleanup(); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it("clamps out-of-range environment values", () => { |
| 96 | + process.env.ALERT_DAILY_SPEND_USD = "-5"; |
| 97 | + process.env.ALERT_TOKENS_PER_HOUR = "9999999999"; |
| 98 | + process.env.ALERT_REQUEST_TOKENS = "-100"; |
| 99 | + process.env.ALERT_ANOMALY_INTERVAL_MINUTES = "2"; |
| 100 | + process.env.ALERT_COOLDOWN_MINUTES = "0"; |
| 101 | + const { config, cleanup } = makeConfig(); |
| 102 | + |
| 103 | + try { |
| 104 | + expect(config.getAlertDailySpendUsd()).toBe(0); |
| 105 | + expect(config.getAlertTokensPerHour()).toBe(1_000_000_000); |
| 106 | + expect(config.getAlertRequestTokens()).toBe(0); |
| 107 | + expect(config.getAlertAnomalyIntervalMinutes()).toBe(5); |
| 108 | + expect(config.getAlertCooldownMinutes()).toBe(1); |
| 109 | + } finally { |
| 110 | + cleanup(); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + it("clamps out-of-range config-file values via setters", () => { |
| 115 | + for (const key of ENV_KEYS) { |
| 116 | + delete process.env[key]; |
| 117 | + } |
| 118 | + const { config, cleanup } = makeConfig(); |
| 119 | + |
| 120 | + try { |
| 121 | + config.setAlertDailySpendUsd(2_000_000); |
| 122 | + expect(config.getAlertDailySpendUsd()).toBe(1_000_000); |
| 123 | + |
| 124 | + config.setAlertAnomalyIntervalMinutes(3); |
| 125 | + expect(config.getAlertAnomalyIntervalMinutes()).toBe(5); |
| 126 | + |
| 127 | + config.setAlertAnomalyIntervalMinutes(99999); |
| 128 | + expect(config.getAlertAnomalyIntervalMinutes()).toBe(1440); |
| 129 | + |
| 130 | + config.setAlertCooldownMinutes(0); |
| 131 | + expect(config.getAlertCooldownMinutes()).toBe(1); |
| 132 | + } finally { |
| 133 | + cleanup(); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + it("persists setter values readable by getters", () => { |
| 138 | + for (const key of ENV_KEYS) { |
| 139 | + delete process.env[key]; |
| 140 | + } |
| 141 | + const { config, cleanup } = makeConfig(); |
| 142 | + |
| 143 | + try { |
| 144 | + config.setAlertDailySpendUsd(50); |
| 145 | + config.setAlertTokensPerHour(1_000_000); |
| 146 | + config.setAlertRequestTokens(300_000); |
| 147 | + config.setAlertAnomalyEnabled(true); |
| 148 | + config.setAlertCooldownMinutes(45); |
| 149 | + config.setAlertWebhookUrl("https://hooks.example.com/alert"); |
| 150 | + |
| 151 | + expect(config.getAlertDailySpendUsd()).toBe(50); |
| 152 | + expect(config.getAlertTokensPerHour()).toBe(1_000_000); |
| 153 | + expect(config.getAlertRequestTokens()).toBe(300_000); |
| 154 | + expect(config.getAlertAnomalyEnabled()).toBe(true); |
| 155 | + expect(config.getAlertCooldownMinutes()).toBe(45); |
| 156 | + expect(config.getAlertWebhookUrl()).toBe( |
| 157 | + "https://hooks.example.com/alert", |
| 158 | + ); |
| 159 | + } finally { |
| 160 | + cleanup(); |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + it("accepts an empty webhook URL (disabled) and rejects invalid ones", () => { |
| 165 | + for (const key of ENV_KEYS) { |
| 166 | + delete process.env[key]; |
| 167 | + } |
| 168 | + const { config, cleanup } = makeConfig(); |
| 169 | + |
| 170 | + try { |
| 171 | + expect(() => config.setAlertWebhookUrl("")).not.toThrow(); |
| 172 | + expect(config.getAlertWebhookUrl()).toBe(""); |
| 173 | + |
| 174 | + expect(() => config.setAlertWebhookUrl("not-a-url")).toThrow(); |
| 175 | + expect(() => config.setAlertWebhookUrl("ftp://example.com")).toThrow(); |
| 176 | + expect(() => |
| 177 | + config.setAlertWebhookUrl("http://example.com/hook"), |
| 178 | + ).not.toThrow(); |
| 179 | + } finally { |
| 180 | + cleanup(); |
| 181 | + } |
| 182 | + }); |
| 183 | + |
| 184 | + it("includes alert settings in getAllSettings()", () => { |
| 185 | + for (const key of ENV_KEYS) { |
| 186 | + delete process.env[key]; |
| 187 | + } |
| 188 | + const { config, cleanup } = makeConfig(); |
| 189 | + |
| 190 | + try { |
| 191 | + const settings = config.getAllSettings(); |
| 192 | + expect(settings.alert_daily_spend_usd).toBe(0); |
| 193 | + expect(settings.alert_tokens_per_hour).toBe(0); |
| 194 | + expect(settings.alert_request_tokens).toBe(0); |
| 195 | + expect(settings.alert_anomaly_enabled).toBe(false); |
| 196 | + expect(settings.alert_anomaly_interval_minutes).toBe(15); |
| 197 | + expect(settings.alert_cooldown_minutes).toBe(60); |
| 198 | + expect(settings.alert_webhook_url).toBe(""); |
| 199 | + } finally { |
| 200 | + cleanup(); |
| 201 | + } |
| 202 | + }); |
| 203 | +}); |
0 commit comments