fix(cli): make login tests hermetic (kill the 10-min network-flake) - #10
Merged
Conversation
TestLoginEmptyTokenViaStdinFails was flaky and could time out at the 10-min Go test ceiling in CI. `civitai login` now DEFAULTS to the OAuth device flow: with no --token, RunE calls loginWithDevice -> StartDevice, a real network call to base_url. The test set stdin to "\n" expecting the OLD token-prompt "no token provided" error, but the default path ignores stdin. With no CIVITAI_BASE_URL override (only XDG_CONFIG_HOME was set), base_url defaulted to https://civitai.com and the test made a REAL production device-login attempt. When CI egress reached civitai.com, device-init succeeded and PollToken looped until the device-code expiry (~10 min) -> 10-min test timeout. When egress was blocked it failed in ~30s and passed. Hence the network-dependent flake. Replace it with TestLoginDeviceStartFailureReturnsError: stand up an httptest server whose device-init endpoint returns 4xx, pin the CLI at it via t.Setenv("CIVITAI_BASE_URL", srv.URL), run `login --no-browser`, and assert it errors fast (sub-second, no polling, never touching civitai.com). Asserts the device-init failure surfaces, nothing is persisted, and the poll endpoint is never reached. Audited the rest of internal/cmd and internal/api tests: all other HTTP-exercising tests already use httptest + CIVITAI_BASE_URL; this was the only test that could reach the real network. `go test ./...` no longer depends on outbound network. Proof: in an `unshare -rn` net namespace (empty route table, external TCP = "network is unreachable"), `go test ./internal/cmd/... ./internal/api/...` passes in ~2s (the 2s is two 1s device-flow interval sleeps against a LOCAL httptest server, not the network). No product code changed. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
TestLoginEmptyTokenViaStdinFails(ininternal/cmd/cmd_test.go) was flaky and could time out at Go's 10-min test ceiling in CI.civitai loginnow defaults to the OAuth device flow: with no--token,RunEcallsloginWithDevice→oc.StartDevice(ctx), a real network call tobase_url. The old test set stdin to"\n"expecting the old personal-key token-prompt error ("no token provided"), but the default path no longer reads stdin. The test only setXDG_CONFIG_HOME(notCIVITAI_BASE_URL), sobase_urldefaulted to the realhttps://civitai.com— the test made an actual production device-login attempt.StartDevicesucceeds →PollTokenloops until the device-code expiry (~10 min) waiting for an approval that never comes → Go's 10-min timeout fires.StartDevicefails in ~30s → test passes.Hence flaky (network-dependent), and a real production device-login attempt from CI.
The fix
Replaced the obsolete test with
TestLoginDeviceStartFailureReturnsError(hermetic, deterministic):httptest.NewServerwhose device-init endpoint (/api/auth/oauth/device) returns403 {"error":"invalid_client"}.t.Setenv("CIVITAI_BASE_URL", srv.URL)so it never touches civitai.com.login --no-browserand asserts: an error is returned, it reports the device-init failure, the poll endpoint is never reached (proves it fails at init, no~10-minpoll), nothing is persisted, and it completes sub-second (asserts< 5s).The old test's intent ("empty token via stdin fails") is obsolete:
loginWithToken's empty-prompt branch is only reachable when--tokenis passed, andRunEonly callsloginWithTokenwhen the trimmed--tokenis non-empty — so that branch is unreachable from the CLI. No point asserting a path that can't be triggered; the new test asserts the real, reachable default behavior (device login fails cleanly).Other network-touching tests
Audited all of
internal/cmd/*_test.goandinternal/api/*_test.go. Every other HTTP-exercising test already stands up anhttptestserver and overridesCIVITAI_BASE_URL(whoami, app submit upload paths, all oauth/api tests). The no-tokenapp submitpath falls back to writing a local zip — no network. This was the only test that could reach the real network.go test ./...no longer depends on outbound network.Proof — fast & deterministic, no real network
go build ./...green;go vet ./internal/cmd/...green.go test ./internal/cmd/... ./internal/api/...passes in ~2s (was a 10-min timeout when the flake tripped). The new test itself runs in 0.00s. The residual ~2s isTestLoginDeviceFlowHappyPath's two 1s device-flowintervalsleeps against its local httptest server — not the network.unshare -rnnetwork namespace with an empty route table (external TCP to a public IP returnsconnect: network is unreachable). Tests still pass in ~2s, proving no real egress is required.Product code
None changed — test-only. (The HTTP client already has a 30s timeout and
PollTokenis deadline-bounded byExpiresIn; the perceived "hang" was the device-code lifetime, which the user Ctrl-Cs. Not over-engineered here — test hermeticity is the actual fix.)🤖 Generated with Claude Code