diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-08 22:18:05 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-08 22:18:05 +0200 |
| commit | 9952f53408c8c688f97afbde93cfd9d77fbe8974 (patch) | |
| tree | 61631688ba195afcaeb6239cca3e0993182ffdff /internal/appconfig | |
| parent | 03cbe41dfa124a78116609cdfa49014ad4d09e8c (diff) | |
Add unit tests to improve coverage above 80% target
Implement comprehensive unit tests for critical internal packages
to increase overall test coverage from 80.9% to 81.8%, providing
a buffer above the 80% threshold specified in project guidelines.
Changes:
- Add config parsing tests (parseTemperatureValue, decodeModelEntry, resolvedModel, parseSurfaceEntries)
- Add HumanBytes utility tests with edge cases and boundary values
- Fix HumanBytes bug: corrected suffix array indexing (off-by-one error)
- Fix HumanBytes to handle negative values correctly
- Add comprehensive shellJoin and isSafeBare tests for shell escaping
- Update comments to reflect actual behavior and implementation details
Coverage impact:
- internal/appconfig: Improved config parsing function coverage
- internal/textutil: HumanBytes now at 100% coverage (fixed bug in process)
- internal/tmux: shellJoin and isSafeBare now at 100% coverage
- Overall project: 80.9% → 81.8% (+0.9%)
All tests pass. Code formatted with gofumpt.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'internal/appconfig')
| -rw-r--r-- | internal/appconfig/config_test.go | 371 |
1 files changed, 371 insertions, 0 deletions
diff --git a/internal/appconfig/config_test.go b/internal/appconfig/config_test.go index 6b8ee5b..4a1403f 100644 --- a/internal/appconfig/config_test.go +++ b/internal/appconfig/config_test.go @@ -991,3 +991,374 @@ display_name = "Empty" t.Errorf("got %d agents, want 0 (empty name should be skipped)", len(cfg.TmuxEditAgents)) } } + +// --- Phase 1: Config Parsing Tests --- + +func TestParseTemperatureValue(t *testing.T) { + tests := []struct { + name string + input any + wantValue *float64 + wantOK bool + }{ + {"float64 zero", float64(0.0), floatPtr(0.0), true}, + {"float64 half", float64(0.5), floatPtr(0.5), true}, + {"float64 one", float64(1.0), floatPtr(1.0), true}, + {"float64 two", float64(2.0), floatPtr(2.0), true}, + {"int64 zero", int64(0), floatPtr(0.0), true}, + {"int64 one", int64(1), floatPtr(1.0), true}, + {"int64 two", int64(2), floatPtr(2.0), true}, + {"string zero", "0", floatPtr(0.0), true}, + {"string one", "1", floatPtr(1.0), true}, + {"string two", "2", floatPtr(2.0), true}, + {"string float", "0.75", floatPtr(0.75), true}, + {"string empty", "", nil, true}, + {"string whitespace", " ", nil, true}, + {"string invalid", "invalid", nil, false}, + {"string negative", "-0.5", floatPtr(-0.5), true}, + {"string very small", "0.0001", floatPtr(0.0001), true}, + {"string high precision", "1.123456789", floatPtr(1.123456789), true}, + {"nil value", nil, nil, false}, + {"bool value", true, nil, false}, + {"map value", map[string]any{}, nil, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := parseTemperatureValue(tt.input, "test", newLogger()) + if ok != tt.wantOK { + t.Errorf("parseTemperatureValue() ok = %v, want %v", ok, tt.wantOK) + } + if !ok { + return + } + if (got == nil) != (tt.wantValue == nil) { + t.Errorf("parseTemperatureValue() = %v, want %v", got, tt.wantValue) + return + } + if got != nil && tt.wantValue != nil && *got != *tt.wantValue { + t.Errorf("parseTemperatureValue() = %v, want %v", *got, *tt.wantValue) + } + }) + } +} + +func TestDecodeModelEntry(t *testing.T) { + tests := []struct { + name string + input any + wantCfg *SurfaceConfig + wantOK bool + }{ + { + name: "simple string model", + input: "gpt-4", + wantCfg: &SurfaceConfig{Model: "gpt-4"}, + wantOK: true, + }, + { + name: "empty string", + input: "", + wantCfg: nil, + wantOK: false, + }, + { + name: "whitespace string", + input: " ", + wantCfg: nil, + wantOK: false, + }, + { + name: "object with all fields", + input: map[string]any{ + "model": "claude-3", + "provider": "anthropic", + "temperature": float64(0.7), + }, + wantCfg: &SurfaceConfig{ + Model: "claude-3", + Provider: "anthropic", + Temperature: floatPtr(0.7), + }, + wantOK: true, + }, + { + name: "object with model only", + input: map[string]any{ + "model": "gpt-4o", + }, + wantCfg: &SurfaceConfig{Model: "gpt-4o"}, + wantOK: true, + }, + { + name: "object with provider only", + input: map[string]any{ + "provider": "openai", + }, + wantCfg: &SurfaceConfig{Provider: "openai"}, + wantOK: true, + }, + { + name: "object with temperature only", + input: map[string]any{ + "temperature": float64(0.5), + }, + wantCfg: &SurfaceConfig{Temperature: floatPtr(0.5)}, + wantOK: true, + }, + { + name: "object with empty fields", + input: map[string]any{ + "model": "", + "provider": "", + }, + wantCfg: nil, + wantOK: false, + }, + { + name: "object with invalid model type", + input: map[string]any{ + "model": 123, + }, + wantCfg: nil, + wantOK: false, + }, + { + name: "object with invalid provider type", + input: map[string]any{ + "provider": 456, + }, + wantCfg: nil, + wantOK: false, + }, + { + name: "object with invalid temperature", + input: map[string]any{ + "model": "gpt-4", + "temperature": "not a number", + }, + wantCfg: nil, + wantOK: false, + }, + { + name: "nil input", + input: nil, + wantCfg: nil, + wantOK: false, + }, + { + name: "invalid type (int)", + input: 123, + wantCfg: nil, + wantOK: false, + }, + { + name: "invalid type (slice)", + input: []string{"gpt-4"}, + wantCfg: nil, + wantOK: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := decodeModelEntry(tt.input, "test", newLogger()) + if ok != tt.wantOK { + t.Errorf("decodeModelEntry() ok = %v, want %v", ok, tt.wantOK) + } + if !ok { + return + } + if (got == nil) != (tt.wantCfg == nil) { + t.Errorf("decodeModelEntry() = %v, want %v", got, tt.wantCfg) + return + } + if got == nil { + return + } + if got.Model != tt.wantCfg.Model { + t.Errorf("Model = %q, want %q", got.Model, tt.wantCfg.Model) + } + if got.Provider != tt.wantCfg.Provider { + t.Errorf("Provider = %q, want %q", got.Provider, tt.wantCfg.Provider) + } + if (got.Temperature == nil) != (tt.wantCfg.Temperature == nil) { + t.Errorf("Temperature nil mismatch: got %v, want %v", got.Temperature, tt.wantCfg.Temperature) + } else if got.Temperature != nil && *got.Temperature != *tt.wantCfg.Temperature { + t.Errorf("Temperature = %v, want %v", *got.Temperature, *tt.wantCfg.Temperature) + } + }) + } +} + +func TestResolvedModel(t *testing.T) { + tests := []struct { + name string + section sectionOpenAI + want string + }{ + { + name: "explicit model no presets", + section: sectionOpenAI{Model: "gpt-4"}, + want: "gpt-4", + }, + { + name: "empty model", + section: sectionOpenAI{Model: ""}, + want: "", + }, + { + name: "whitespace model", + section: sectionOpenAI{Model: " "}, + want: "", + }, + { + name: "preset match exact case", + section: sectionOpenAI{ + Model: "fast", + Presets: map[string]string{"fast": "gpt-3.5-turbo"}, + }, + want: "gpt-3.5-turbo", + }, + { + name: "preset match case insensitive", + section: sectionOpenAI{ + Model: "FAST", + Presets: map[string]string{"fast": "gpt-3.5-turbo"}, + }, + want: "gpt-3.5-turbo", + }, + { + name: "no preset match returns original", + section: sectionOpenAI{ + Model: "custom-model", + Presets: map[string]string{"fast": "gpt-3.5-turbo"}, + }, + want: "custom-model", + }, + { + name: "preset empty value returns original", + section: sectionOpenAI{ + Model: "fast", + Presets: map[string]string{"fast": ""}, + }, + want: "fast", + }, + { + name: "preset whitespace value returns original", + section: sectionOpenAI{ + Model: "fast", + Presets: map[string]string{"fast": " "}, + }, + want: "fast", + }, + { + name: "multiple presets uses correct one", + section: sectionOpenAI{ + Model: "smart", + Presets: map[string]string{ + "fast": "gpt-3.5-turbo", + "smart": "gpt-4", + "mini": "gpt-3.5-mini", + }, + }, + want: "gpt-4", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := tt.section.resolvedModel() + if got != tt.want { + t.Errorf("resolvedModel() = %q, want %q", got, tt.want) + } + }) + } +} + +func TestParseSurfaceEntries(t *testing.T) { + tests := []struct { + name string + input any + wantLen int + wantOK bool + }{ + { + name: "nil input", + input: nil, + wantLen: 0, + wantOK: false, + }, + { + name: "single string", + input: "gpt-4", + wantLen: 1, + wantOK: true, + }, + { + name: "single map", + input: map[string]any{ + "model": "claude-3", + "provider": "anthropic", + }, + wantLen: 1, + wantOK: true, + }, + { + name: "array of strings", + input: []any{ + "gpt-4", + "claude-3", + }, + wantLen: 2, + wantOK: true, + }, + { + name: "array of maps", + input: []any{ + map[string]any{"model": "gpt-4", "provider": "openai"}, + map[string]any{"model": "claude-3", "provider": "anthropic"}, + }, + wantLen: 2, + wantOK: true, + }, + { + name: "array with invalid entries", + input: []any{ + "gpt-4", + 123, + "claude-3", + }, + wantLen: 2, + wantOK: true, + }, + { + name: "array with all invalid entries", + input: []any{ + 123, + true, + nil, + }, + wantLen: 0, + wantOK: false, + }, + { + name: "empty array", + input: []any{}, + wantLen: 0, + wantOK: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := parseSurfaceEntries(tt.input, "test", newLogger()) + if ok != tt.wantOK { + t.Errorf("parseSurfaceEntries() ok = %v, want %v", ok, tt.wantOK) + } + if len(got) != tt.wantLen { + t.Errorf("parseSurfaceEntries() len = %d, want %d", len(got), tt.wantLen) + } + }) + } +} |
