mirror of
https://github.com/kjanat/articulate-parser.git
synced 2026-01-16 11:02:10 +01:00
refactor(core)!: Add context, config, and structured logging
Introduces `context.Context` to the `FetchCourse` method and its call chain, allowing for cancellable network requests and timeouts. This improves application robustness when fetching remote course data. A new configuration package centralizes application settings, loading them from environment variables with sensible defaults for base URL, request timeout, and logging. Standard `log` and `fmt` calls are replaced with a structured logging system built on `slog`, supporting both JSON and human-readable text formats. This change also includes: - Extensive benchmarks and example tests. - Simplified Go doc comments across several packages. BREAKING CHANGE: The `NewArticulateParser` constructor signature has been updated to accept a logger, base URL, and timeout, which are now supplied via the new configuration system.
This commit is contained in:
118
internal/config/config_test.go
Normal file
118
internal/config/config_test.go
Normal file
@ -0,0 +1,118 @@
|
||||
// Package config_test provides tests for the config package.
|
||||
package config
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
// Clear environment
|
||||
os.Clearenv()
|
||||
|
||||
cfg := Load()
|
||||
|
||||
if cfg.BaseURL != DefaultBaseURL {
|
||||
t.Errorf("Expected BaseURL '%s', got '%s'", DefaultBaseURL, cfg.BaseURL)
|
||||
}
|
||||
|
||||
if cfg.RequestTimeout != DefaultRequestTimeout {
|
||||
t.Errorf("Expected timeout %v, got %v", DefaultRequestTimeout, cfg.RequestTimeout)
|
||||
}
|
||||
|
||||
if cfg.LogLevel != DefaultLogLevel {
|
||||
t.Errorf("Expected log level %v, got %v", DefaultLogLevel, cfg.LogLevel)
|
||||
}
|
||||
|
||||
if cfg.LogFormat != DefaultLogFormat {
|
||||
t.Errorf("Expected log format '%s', got '%s'", DefaultLogFormat, cfg.LogFormat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_WithEnvironmentVariables(t *testing.T) {
|
||||
// Set environment variables
|
||||
os.Setenv("ARTICULATE_BASE_URL", "https://test.example.com")
|
||||
os.Setenv("ARTICULATE_REQUEST_TIMEOUT", "60")
|
||||
os.Setenv("LOG_LEVEL", "debug")
|
||||
os.Setenv("LOG_FORMAT", "json")
|
||||
defer os.Clearenv()
|
||||
|
||||
cfg := Load()
|
||||
|
||||
if cfg.BaseURL != "https://test.example.com" {
|
||||
t.Errorf("Expected BaseURL 'https://test.example.com', got '%s'", cfg.BaseURL)
|
||||
}
|
||||
|
||||
if cfg.RequestTimeout != 60*time.Second {
|
||||
t.Errorf("Expected timeout 60s, got %v", cfg.RequestTimeout)
|
||||
}
|
||||
|
||||
if cfg.LogLevel != slog.LevelDebug {
|
||||
t.Errorf("Expected log level Debug, got %v", cfg.LogLevel)
|
||||
}
|
||||
|
||||
if cfg.LogFormat != "json" {
|
||||
t.Errorf("Expected log format 'json', got '%s'", cfg.LogFormat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetLogLevelEnv(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value string
|
||||
expected slog.Level
|
||||
}{
|
||||
{"debug lowercase", "debug", slog.LevelDebug},
|
||||
{"debug uppercase", "DEBUG", slog.LevelDebug},
|
||||
{"info lowercase", "info", slog.LevelInfo},
|
||||
{"info uppercase", "INFO", slog.LevelInfo},
|
||||
{"warn lowercase", "warn", slog.LevelWarn},
|
||||
{"warn uppercase", "WARN", slog.LevelWarn},
|
||||
{"warning lowercase", "warning", slog.LevelWarn},
|
||||
{"error lowercase", "error", slog.LevelError},
|
||||
{"error uppercase", "ERROR", slog.LevelError},
|
||||
{"invalid value", "invalid", slog.LevelInfo},
|
||||
{"empty value", "", slog.LevelInfo},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
os.Clearenv()
|
||||
if tt.value != "" {
|
||||
os.Setenv("TEST_LOG_LEVEL", tt.value)
|
||||
}
|
||||
result := getLogLevelEnv("TEST_LOG_LEVEL", slog.LevelInfo)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Expected %v, got %v", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDurationEnv(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value string
|
||||
expected time.Duration
|
||||
}{
|
||||
{"valid duration", "45", 45 * time.Second},
|
||||
{"zero duration", "0", 0},
|
||||
{"invalid duration", "invalid", 30 * time.Second},
|
||||
{"empty value", "", 30 * time.Second},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
os.Clearenv()
|
||||
if tt.value != "" {
|
||||
os.Setenv("TEST_DURATION", tt.value)
|
||||
}
|
||||
result := getDurationEnv("TEST_DURATION", 30*time.Second)
|
||||
if result != tt.expected {
|
||||
t.Errorf("Expected %v, got %v", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user