Files
reddit-reader/internal/config/config_test.go

122 lines
2.7 KiB
Go

package config_test
import (
"os"
"path/filepath"
"testing"
"somegit.dev/vikingowl/reddit-reader/internal/config"
)
func TestLoadFromFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
err := os.WriteFile(path, []byte(`
[reddit]
client_id = "test_id"
client_secret = "test_secret"
username = "test_user"
password = "test_pass"
[llm]
backend = "ollama"
endpoint = "localhost:11434"
model = "mistral-small"
relevance_threshold = 0.7
[interests]
description = "Go programming, Linux"
[monitor]
poll_interval = "5m"
max_posts_per_poll = 10
[grpc]
socket = "/tmp/test.sock"
`), 0o644)
if err != nil {
t.Fatal(err)
}
cfg, err := config.LoadFromFile(path)
if err != nil {
t.Fatalf("LoadFromFile: %v", err)
}
if cfg.Reddit.ClientID != "test_id" {
t.Errorf("ClientID = %q, want %q", cfg.Reddit.ClientID, "test_id")
}
if cfg.LLM.Backend != "ollama" {
t.Errorf("Backend = %q, want %q", cfg.LLM.Backend, "ollama")
}
if cfg.LLM.RelevanceThreshold != 0.7 {
t.Errorf("RelevanceThreshold = %f, want 0.7", cfg.LLM.RelevanceThreshold)
}
if cfg.Interests.Description != "Go programming, Linux" {
t.Errorf("Description = %q, want %q", cfg.Interests.Description, "Go programming, Linux")
}
if cfg.Monitor.PollInterval.String() != "5m0s" {
t.Errorf("PollInterval = %v, want 5m", cfg.Monitor.PollInterval)
}
if cfg.Monitor.MaxPostsPerPoll != 10 {
t.Errorf("MaxPostsPerPoll = %d, want 10", cfg.Monitor.MaxPostsPerPoll)
}
if cfg.GRPC.Socket != "/tmp/test.sock" {
t.Errorf("Socket = %q, want %q", cfg.GRPC.Socket, "/tmp/test.sock")
}
}
func TestEnvVarOverride(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
err := os.WriteFile(path, []byte(`
[reddit]
client_id = "file_id"
client_secret = "file_secret"
username = "file_user"
password = "file_pass"
[llm]
backend = "ollama"
endpoint = "localhost:11434"
model = "mistral-small"
relevance_threshold = 0.6
[interests]
description = ""
[monitor]
poll_interval = "2m"
max_posts_per_poll = 25
[grpc]
socket = "/tmp/test.sock"
`), 0o644)
if err != nil {
t.Fatal(err)
}
t.Setenv("REDDIT_READER_REDDIT_CLIENT_ID", "env_id")
t.Setenv("REDDIT_READER_LLM_API_KEY", "env_key")
cfg, err := config.LoadFromFile(path)
if err != nil {
t.Fatalf("LoadFromFile: %v", err)
}
cfg.ApplyEnvOverrides()
if cfg.Reddit.ClientID != "env_id" {
t.Errorf("ClientID = %q, want %q (env override)", cfg.Reddit.ClientID, "env_id")
}
if cfg.LLM.APIKey != "env_key" {
t.Errorf("APIKey = %q, want %q (env override)", cfg.LLM.APIKey, "env_key")
}
}
func TestDefaultConfigPath(t *testing.T) {
path := config.DefaultPath()
if path == "" {
t.Error("DefaultPath returned empty string")
}
}