Skip to content

test(e2e): add CLI tests for thv llm setup/teardown - #5108

Closed
yrobla wants to merge 1 commit into
mainfrom
issue-5074
Closed

yrobla wants to merge 1 commit into
mainfrom
issue-5074

Conversation

@yrobla

@yrobla yrobla commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

thv llm setup and thv llm teardown had 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.

  • Adds test/e2e/cli_llm_setup_test.go with 12 tests covering claude-code and gemini-cli
  • Tests verify: settings file patching, teardown cleanup, idempotency, preservation of pre-existing keys, multi-tool setup/teardown, and error/edge cases
  • Tools are simulated by creating their config directories in a temp HOME, matching how DetectedLLMGatewayClients() detects installation — no real tool binaries required
  • Path correctness per tool is covered by unit tests in pkg/client/llm_gateway_test.go against supportedClientIntegrations

Real-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

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe): e2e test coverage

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

API Compatibility

  • This PR does not break the v1beta1 API, OR the api-break-allowed label 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 — isolated HOME/XDG_CONFIG_HOME, environment secrets provider, real thv binary via THV_BINARY. They will pass in CI with a container runtime present.

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]>
@yrobla

yrobla commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

Closing after deeper analysis of the existing test suite.

cmd/thv/app/llm_test.go and pkg/client/llm_gateway_test.go already cover the scenarios this PR aimed to add — rollback on config persistence failure, partial failures, all real tool configs with correct JSON paths, detection boundaries, idempotency, JSONC preservation, and all teardown edge cases.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +60 to +66
// 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

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +122 to +131
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")
})

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +117 to +119
envMap, _ := settings["env"].(map[string]any)
Expect(envMap).ToNot(HaveKey("ANTHROPIC_BASE_URL"),
"ANTHROPIC_BASE_URL should be removed after teardown")

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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")
}

Copilot uses AI. Check for mistakes.
Comment on lines +196 to +198
authMap, _ := settings["auth"].(map[string]any)
Expect(authMap).ToNot(HaveKey("tokenCommand"),
"auth.tokenCommand should be removed after teardown")

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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")
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

llm: e2e tests for thv llm setup/teardown against real tool installations

3 participants