thermograph/backend/daemon/internal/config/config_test.go
emi 2d3f37c474
All checks were successful
Sync infra to hosts / sync-beta (push) Successful in 8s
Sync infra to hosts / sync-prod (push) Successful in 7s
secrets-guard / encrypted (push) Successful in 8s
shell-lint / shellcheck (push) Successful in 10s
Build + push backend image (Forgejo registry) / build-push (push) Successful in 1m14s
Deploy backend to beta VPS / deploy (push) Successful in 2m4s
daemon: move the Discord gateway and scheduler out of the web process into Go (#21)
The gateway bot and APScheduler were long-lived stateful I/O loops running
inside the async web app under a leader election. They move into a single Go
binary that owns ONLY that I/O -- websocket, RESUME, heartbeat, backoff, timers.

It owns no grading logic. Anything needing data calls back over a new
internal-only surface (/internal/discord/grade, /internal/jobs/*). Grading
depends on polars and the parquet cache; reimplementing it in Go would let the
bot's grades drift from the API's. The grade route returns gateway-ready JSON
and Go relays the bytes verbatim.

The binary ships in the backend image and runs as a second compose service off
the same tag, so the two ends of the /internal/* contract can never skew.
deploy.sh rolls daemon alongside backend -- without that the service would never
be created, since a single-service deploy uses --no-deps. It also probes the
image first and skips the daemon when rolling a tag that predates the binary:
infra tracks main while image tags are env-staged, so a host can legitimately be
asked to roll an older backend image, and creating the service anyway would
leave a container crash-looping on a missing binary.

replicas: 1 with order: stop-first replaces the leader election -- Discord
permits one gateway connection per bot token.

THERMOGRAPH_INTERNAL_TOKEN is optional: both ends derive it from
THERMOGRAPH_AUTH_SECRET via HMAC under a domain-separation label, so this needs
no new vault entry. The derivation is pinned to a shared cross-language test
vector asserted on both sides, so drift fails CI instead of 401ing every call.
Fail closed when neither secret is set.

Improvements over the Python: a close intended for RESUME uses 4000 rather than
1000 (Discord invalidates a session closed 1000, so the old default defeated its
own resume); MESSAGE_CREATE runs on a bounded worker pool; and a malformed HELLO
returns an error rather than a clean reconnect, which would otherwise reset
backoff and hot-loop against the gateway.

365 Python tests pass; Go build/vet/test -race clean; shellcheck 0 findings.
2026-07-23 22:49:54 +00:00

96 lines
2.9 KiB
Go

package config
import (
"testing"
"time"
)
// env builds a getenv func over a map, defaulting the two required vars so
// each test only states what it cares about.
func env(overrides map[string]string) func(string) string {
base := map[string]string{
"THERMOGRAPH_INTERNAL_TOKEN": "sekrit",
"THERMOGRAPH_API_BASE_INTERNAL": "http://web:8137",
}
for k, v := range overrides {
base[k] = v
}
return func(k string) string { return base[k] }
}
func TestRequiredVarsRefuseToStart(t *testing.T) {
for _, missing := range []string{"THERMOGRAPH_INTERNAL_TOKEN", "THERMOGRAPH_API_BASE_INTERNAL"} {
if _, err := load(env(map[string]string{missing: ""})); err == nil {
t.Errorf("expected error when %s is unset, got nil", missing)
}
}
if cfg, err := load(env(nil)); err != nil || cfg == nil {
t.Fatalf("both required vars set: unexpected error %v", err)
}
}
func TestTruthyParsing(t *testing.T) {
cases := []struct {
flag, token string
want bool
}{
// Must match the Python _TRUTHY set exactly.
{"1", "tok", true},
{"true", "tok", true},
{"TRUE", "tok", true},
{"yes", "tok", true},
{"on", "tok", true},
{" on ", "tok", true}, // whitespace tolerated, like .strip() in Python
{"0", "tok", false},
{"false", "tok", false},
{"", "tok", false},
{"enabled", "tok", false}, // not in the truthy set
// Flag alone is not enough: no token means no gateway.
{"1", "", false},
}
for _, c := range cases {
cfg, err := load(env(map[string]string{
"THERMOGRAPH_DISCORD_BOT": c.flag,
"THERMOGRAPH_DISCORD_BOT_TOKEN": c.token,
}))
if err != nil {
t.Fatalf("flag=%q token=%q: unexpected error %v", c.flag, c.token, err)
}
if cfg.DiscordEnabled != c.want {
t.Errorf("flag=%q token=%q: DiscordEnabled=%v, want %v", c.flag, c.token, cfg.DiscordEnabled, c.want)
}
}
}
func TestIntervalDefaultsAndFallbacks(t *testing.T) {
cases := []struct {
name string
warm, indexnow string
wantWarm time.Duration
wantIndexNow time.Duration
}{
{"unset uses defaults", "", "", 24 * time.Hour, 6 * time.Hour},
{"explicit values win", "12", "3", 12 * time.Hour, 3 * time.Hour},
// Malformed values degrade to the default instead of erroring, like
// the Python int(... or 24) they replace.
{"garbage falls back", "soon", "1.5", 24 * time.Hour, 6 * time.Hour},
{"non-positive falls back", "0", "-2", 24 * time.Hour, 6 * time.Hour},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
cfg, err := load(env(map[string]string{
"THERMOGRAPH_WARM_CITIES_INTERVAL_HOURS": c.warm,
"THERMOGRAPH_INDEXNOW_INTERVAL_HOURS": c.indexnow,
}))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.WarmCitiesInterval != c.wantWarm {
t.Errorf("WarmCitiesInterval=%v, want %v", cfg.WarmCitiesInterval, c.wantWarm)
}
if cfg.IndexNowInterval != c.wantIndexNow {
t.Errorf("IndexNowInterval=%v, want %v", cfg.IndexNowInterval, c.wantIndexNow)
}
})
}
}