Skip to content

fix(cli): make login tests hermetic (kill the 10-min network-flake) - #10

Merged
ZacxDev merged 1 commit into
mainfrom
zach/fix-flaky-login-network-test
Jun 22, 2026
Merged

fix(cli): make login tests hermetic (kill the 10-min network-flake)#10
ZacxDev merged 1 commit into
mainfrom
zach/fix-flaky-login-network-test

Conversation

@ZacxDev

@ZacxDev ZacxDev commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Root cause

TestLoginEmptyTokenViaStdinFails (in internal/cmd/cmd_test.go) was flaky and could time out at Go's 10-min test ceiling in CI.

civitai login now defaults to the OAuth device flow: with no --token, RunE calls loginWithDeviceoc.StartDevice(ctx), a real network call to base_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 set XDG_CONFIG_HOME (not CIVITAI_BASE_URL), so base_url defaulted to the real https://civitai.com — the test made an actual production device-login attempt.

  • Egress openStartDevice succeeds → PollToken loops until the device-code expiry (~10 min) waiting for an approval that never comes → Go's 10-min timeout fires.
  • Egress blockedStartDevice fails 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):

  • Stands up an httptest.NewServer whose device-init endpoint (/api/auth/oauth/device) returns 403 {"error":"invalid_client"}.
  • Pins the CLI at it via t.Setenv("CIVITAI_BASE_URL", srv.URL) so it never touches civitai.com.
  • Runs login --no-browser and asserts: an error is returned, it reports the device-init failure, the poll endpoint is never reached (proves it fails at init, no ~10-min poll), 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 --token is passed, and RunE only calls loginWithToken when the trimmed --token is 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.go and internal/api/*_test.go. Every other HTTP-exercising test already stands up an httptest server and overrides CIVITAI_BASE_URL (whoami, app submit upload paths, all oauth/api tests). The no-token app submit path 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 is TestLoginDeviceFlowHappyPath's two 1s device-flow interval sleeps against its local httptest server — not the network.
  • Hermeticity proven by re-running both packages inside an unshare -rn network namespace with an empty route table (external TCP to a public IP returns connect: 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 PollToken is deadline-bounded by ExpiresIn; 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

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]>
@ZacxDev
ZacxDev merged commit 1452d4a into main Jun 22, 2026
2 checks passed
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.

1 participant