From 130676bda10d2131f8af919f73ef87b3b7f5886a Mon Sep 17 00:00:00 2001 From: adcondev <38170282+adcondev@users.noreply.github.com> Date: Thu, 19 Feb 2026 20:25:33 +0000 Subject: [PATCH] test(config): add tests for GetEnvironment and LogPath This commit adds table-driven tests for the `GetEnvironment` function in `internal/config`, covering valid environments ("local", "remote") and fallback behavior for unknown/empty environments (defaulting to "local"). It also adds a basic test for `Environment.LogPath`. The tests confirm the existing behavior where unknown environments default to "local", contrary to the outdated documentation/prompt description. Fixes #TestingGap Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- internal/config/config_test.go | 98 ++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 internal/config/config_test.go diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..f44ae73 --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,98 @@ +package config + +import ( + "path/filepath" + "testing" +) + +func TestGetEnvironment(t *testing.T) { + // Table-driven test cases + tests := []struct { + name string + inputEnv string + expectedName string + expectedAddr string + expectedQCap int + expectDefault bool // If true, we expect the fallback (local) config + }{ + { + name: "Get local environment", + inputEnv: "local", + expectedName: "LOCAL", + expectedAddr: "localhost:" + ServerPort, + expectedQCap: 50, + }, + { + name: "Get remote environment", + inputEnv: "remote", + expectedName: "REMOTO", + expectedAddr: "0.0.0.0:" + ServerPort, + expectedQCap: 50, + }, + { + name: "Get unknown environment (defaults to local)", + inputEnv: "unknown_env", + expectedName: "LOCAL", + expectedAddr: "localhost:" + ServerPort, + expectedQCap: 50, + expectDefault: true, + }, + { + name: "Get empty environment (defaults to local)", + inputEnv: "", + expectedName: "LOCAL", + expectedAddr: "localhost:" + ServerPort, + expectedQCap: 50, + expectDefault: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetEnvironment(tt.inputEnv) + + // Verify key fields + if got.Name != tt.expectedName { + t.Errorf("GetEnvironment(%q).Name = %q; want %q", tt.inputEnv, got.Name, tt.expectedName) + } + if got.ListenAddr != tt.expectedAddr { + t.Errorf("GetEnvironment(%q).ListenAddr = %q; want %q", tt.inputEnv, got.ListenAddr, tt.expectedAddr) + } + if got.QueueCapacity != tt.expectedQCap { + t.Errorf("GetEnvironment(%q).QueueCapacity = %d; want %d", tt.inputEnv, got.QueueCapacity, tt.expectedQCap) + } + + // Verify timeout settings are reasonable (not zero) + if got.ReadTimeout == 0 { + t.Errorf("GetEnvironment(%q).ReadTimeout is 0; expected non-zero duration", tt.inputEnv) + } + if got.WriteTimeout == 0 { + t.Errorf("GetEnvironment(%q).WriteTimeout is 0; expected non-zero duration", tt.inputEnv) + } + + // Specific check for local environment details if expected + if tt.expectDefault { + // Verify it matches the 'local' config exactly + localCfg := environments["local"] + if got.Name != localCfg.Name { + t.Errorf("GetEnvironment(%q) did not return local config as default", tt.inputEnv) + } + } + }) + } +} + +func TestEnvironment_LogPath(t *testing.T) { + // Quick test for the LogPath method as well + env := Environment{ + ServiceName: "TestService", + } + programData := "/var/lib" + expected := filepath.Join(programData, "TestService", "TestService.log") + + got := env.LogPath(programData) + + if got != expected { + t.Errorf("LogPath(%q) = %q; want %q", programData, got, expected) + } +}