test: add handler and config tests (auth, tasks, habits, savings, profile, interest, freeze)
Some checks failed
CI / lint-test (push) Failing after 1s

This commit is contained in:
Cosmo
2026-03-26 19:03:12 +00:00
parent 999f9911a9
commit 3c8dd575c3
9 changed files with 2509 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
package config
import (
"os"
"testing"
)
func TestLoad_Defaults(t *testing.T) {
// Clear env to test defaults
envKeys := []string{"DATABASE_URL", "JWT_SECRET", "PORT", "RESEND_API_KEY", "FROM_EMAIL", "FROM_NAME", "APP_URL", "TELEGRAM_BOT_TOKEN"}
originals := make(map[string]string)
for _, k := range envKeys {
originals[k] = os.Getenv(k)
os.Unsetenv(k)
}
defer func() {
for k, v := range originals {
if v != "" {
os.Setenv(k, v)
}
}
}()
cfg := Load()
if cfg.Port != "8080" {
t.Errorf("expected default port 8080, got %s", cfg.Port)
}
if cfg.JWTSecret != "change-me-in-production" {
t.Errorf("expected default JWT secret, got %s", cfg.JWTSecret)
}
if cfg.FromEmail != "noreply@digital-home.site" {
t.Errorf("expected default from email, got %s", cfg.FromEmail)
}
}
func TestLoad_FromEnv(t *testing.T) {
os.Setenv("PORT", "9090")
os.Setenv("JWT_SECRET", "my-secret")
defer func() {
os.Unsetenv("PORT")
os.Unsetenv("JWT_SECRET")
}()
cfg := Load()
if cfg.Port != "9090" {
t.Errorf("expected port 9090, got %s", cfg.Port)
}
if cfg.JWTSecret != "my-secret" {
t.Errorf("expected JWT secret my-secret, got %s", cfg.JWTSecret)
}
}
func TestGetEnv(t *testing.T) {
tests := []struct {
name string
key string
envValue string
def string
want string
}{
{"returns env value", "TEST_KEY_1", "env_val", "default", "env_val"},
{"returns default when empty", "TEST_KEY_2", "", "default", "default"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
os.Setenv(tt.key, tt.envValue)
defer os.Unsetenv(tt.key)
}
got := getEnv(tt.key, tt.def)
if got != tt.want {
t.Errorf("getEnv(%s) = %s, want %s", tt.key, got, tt.want)
}
})
}
}