Repository navigation
Conversation
Tests cover the thv side of LLM gateway configuration: - setup patches claude-code and gemini-cli settings files with correct keys - teardown removes those keys cleanly - idempotent: second setup overwrites without duplicating keys - preserves pre-existing keys in settings files - multi-tool: single setup/teardown call handles both tools - edge cases: unconfigured gateway, no tools detected, teardown no-op Tools are simulated by creating their config directories in a temp HOME, matching how DetectedLLMGatewayClients() detects installation. Real-tool routing verification is out of scope for automated CI. Closes #5074 Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
|
Closing after deeper analysis of the existing test suite.
The e2e test added here exercises the CLI wrapper around those well-tested functions and provides no meaningful additional coverage. Dropping it rather than carrying dead weight. |
There was a problem hiding this comment.
Pull request overview
Adds end-to-end CLI coverage for thv llm setup and thv llm teardown, validating that tool settings are patched/reverted correctly for detected LLM-capable clients.
Changes:
- Introduces a new Ginkgo E2E test suite covering setup/teardown for Claude Code and Gemini CLI using an isolated
HOME/XDG_CONFIG_HOME. - Adds assertions for key patching, cleanup, idempotency, preservation of unrelated settings, multi-tool behavior, and basic edge cases.
- Simulates tool “installation” by creating the expected config directories in the temp home directory.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // readSettingsJSON reads and parses the JSON (or JSONC) file at path. | ||
| func readSettingsJSON(path string) map[string]any { | ||
| data, err := os.ReadFile(path) //nolint:gosec // test reads a known temp-dir path | ||
| Expect(err).ToNot(HaveOccurred(), "settings file should exist at %s", path) | ||
| var out map[string]any | ||
| Expect(json.Unmarshal(data, &out)).To(Succeed()) | ||
| return out |
There was a problem hiding this comment.
The helper comment says it reads JSON or JSONC, but it uses encoding/json.Unmarshal which will fail on JSONC (comments/trailing commas). This is especially likely here because the LLM gateway patching preserves JSONC formatting via hujson when a user already has comments. Either update the comment to say JSON only, or (better) standardize via hujson (or another JSONC-capable parser) before unmarshalling so the tests can read both JSON and JSONC reliably.
| It("is idempotent: a second setup overwrites the first without duplicating keys", func() { | ||
| By("Running setup twice") | ||
| env.thv("llm", "setup").ExpectSuccess() | ||
| env.thv("llm", "setup").ExpectSuccess() | ||
|
|
||
| By("Verifying only one value exists for each key") | ||
| settings := readSettingsJSON(settingsPath) | ||
| Expect(settings["apiKeyHelper"]).To(BeAssignableToTypeOf(""), | ||
| "apiKeyHelper should be a single string, not a list") | ||
| }) |
There was a problem hiding this comment.
This idempotency assertion can't detect duplicated JSON keys: json.Unmarshal into a map will silently keep only the last occurrence, so the test would still pass even if the file ended up with multiple "apiKeyHelper" keys. To actually verify idempotency, inspect the raw file contents and assert the key appears exactly once (similar to the unit test that counts substrings), or parse in a way that rejects duplicate keys.
| envMap, _ := settings["env"].(map[string]any) | ||
| Expect(envMap).ToNot(HaveKey("ANTHROPIC_BASE_URL"), | ||
| "ANTHROPIC_BASE_URL should be removed after teardown") |
There was a problem hiding this comment.
In this teardown assertion the type assertion result is ignored: if "env" is missing or accidentally becomes a non-object type, envMap will be nil and the HaveKey check will still pass, potentially masking regressions. Consider asserting either that "env" is absent, or that it is an object and does not contain ANTHROPIC_BASE_URL.
| envMap, _ := settings["env"].(map[string]any) | |
| Expect(envMap).ToNot(HaveKey("ANTHROPIC_BASE_URL"), | |
| "ANTHROPIC_BASE_URL should be removed after teardown") | |
| if envValue, exists := settings["env"]; exists { | |
| envMap, ok := envValue.(map[string]any) | |
| Expect(ok).To(BeTrue(), "env should be an object when present") | |
| Expect(envMap).ToNot(HaveKey("ANTHROPIC_BASE_URL"), | |
| "ANTHROPIC_BASE_URL should be removed after teardown") | |
| } |
| authMap, _ := settings["auth"].(map[string]any) | ||
| Expect(authMap).ToNot(HaveKey("tokenCommand"), | ||
| "auth.tokenCommand should be removed after teardown") |
There was a problem hiding this comment.
Same pattern here: the type assertion on settings["auth"] is ignored, so authMap can be nil (missing/wrong type) and the HaveKey expectation will still pass. To avoid false positives, assert that "auth" is either absent or an object and does not contain tokenCommand.
| authMap, _ := settings["auth"].(map[string]any) | |
| Expect(authMap).ToNot(HaveKey("tokenCommand"), | |
| "auth.tokenCommand should be removed after teardown") | |
| authVal, hasAuth := settings["auth"] | |
| if hasAuth { | |
| authMap, ok := authVal.(map[string]any) | |
| Expect(ok).To(BeTrue(), "auth should be absent or an object") | |
| Expect(authMap).ToNot(HaveKey("tokenCommand"), | |
| "auth.tokenCommand should be removed after teardown") | |
| } |
Summary
thv llm setupandthv llm teardownhad no automated test coverage beyond unit tests for the JSON patch logic. This left the full CLI path — config persistence, detection, rollback on partial failure, teardown — untested.test/e2e/cli_llm_setup_test.gowith 12 tests covering claude-code and gemini-cliHOME, matching howDetectedLLMGatewayClients()detects installation — no real tool binaries requiredpkg/client/llm_gateway_test.goagainstsupportedClientIntegrationsReal-tool routing verification (does Claude Code actually route traffic through the gateway?) is intentionally out of scope — it would require installing multi-hundred-MB tool binaries and a mock LLM+OIDC stack, making CI too slow.
Closes #5074
Type of change
Test plan
task test)task test-e2e)task lint-fix)API Compatibility
v1beta1API, OR theapi-break-allowedlabel is applied and the migration guidance is described above.Does this introduce a user-facing change?
No.
Special notes for reviewers
Tests follow the same pattern as
test/e2e/cli_llm_config_test.go— isolatedHOME/XDG_CONFIG_HOME, environment secrets provider, realthvbinary viaTHV_BINARY. They will pass in CI with a container runtime present.