feat(backend): main wiring, graceful shutdown, local smoke green

This commit is contained in:
2026-09-06 19:52:42 +08:00
parent 8f18072843
commit 04095b5438
2 changed files with 99 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
package config
import (
"path/filepath"
"testing"
"time"
)
func TestLoad(t *testing.T) {
// 屏蔽外部 env,保证默认值断言自洽(Load 将空串视为未设置)
t.Setenv("BOOKS_DIR", "")
t.Setenv("CACHE_DIR", "")
t.Setenv("ADDR", "")
t.Setenv("JWT_SECRET", "")
if _, err := Load(); err == nil {
t.Fatal("missing JWT_SECRET must fail")
}
t.Setenv("JWT_SECRET", "x")
t.Setenv("SCAN_INTERVAL_SEC", "abc")
if _, err := Load(); err == nil {
t.Fatal("bad interval must fail")
}
t.Setenv("SCAN_INTERVAL_SEC", "30")
t.Setenv("DATABASE_URL", "postgres://x")
c, err := Load()
if err != nil {
t.Fatal(err)
}
// Load 会 EvalSymlinks 解析 BooksDir(任务10 裁定),这里做同样的期望值解析
wantBooks := "/data/books"
if r, e := filepath.EvalSymlinks(wantBooks); e == nil {
wantBooks = r
}
if c.ScanInterval != 30*time.Second || c.BooksDir != wantBooks || c.Addr != ":8080" {
t.Fatalf("%+v", c)
}
}