refactor: replace test-mutated globals with dependency injection#3326
Merged
Conversation
docker-agent
left a comment
Contributor
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
The refactor cleanly converts five package-level globals to dependency injection across pkg/gateway, pkg/toolinstall, pkg/hooks/builtins, pkg/tui/components/reasoningblock, and pkg/modelsdev. Production behaviour is preserved — every call site wires the original implementation as the default.
What was reviewed:
- Nil-safety of new zero-value code paths (
Store.fetcher(),Loader.load,loaderFrom) - Concurrency correctness of
sync.Oncememoization in the newgateway.Loader - Singleflight cross-test contamination via the residual package-level
installGroup - Data-race exposure on
Model.nowinreasoningblock - Default wiring correctness in all five refactored packages
Findings: All hypotheses examined were either non-issues (code paths that can never be reached) or correctly scoped (e.g., TestResolve_ConcurrentPanic_DoesNotCrash does not call t.Parallel(), so the shared installGroup cannot cause cross-test contamination in a standard go test ./... run). No CONFIRMED or LIKELY bugs were found in the introduced code.
Sayt-0
approved these changes
Jun 30, 2026
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.
What
Replaces several package-level globals that tests mutated at runtime with proper dependency injection. Tests no longer swap shared globals, so they can run with
t.Parallel()safely and there is no hidden cross-test coupling.Why
We had a recurring pattern where production code reached for a package-level singleton (an HTTP client, a fetch function, a clock, a catalog loader) and tests overrode it via
var x = ...; x = stub; t.Cleanup(...)or a dedicatedSetXForTesthelper. That pattern forces sequential tests, is racy under-raceif a goroutine ever reads the global concurrently, and exercises a wiring that only exists in tests. Injecting the dependency removes the global entirely.Changes
Five globals converted (production behavior unchanged — the default still wires the original implementation):
pkg/modelsdev— removedvar fetchCatalog; added aStore.fetchfield +WithFetcher(...)option.loadDatabasetakes afetcherparameter. Tests inject a stub fetcher.pkg/toolinstall— removedvar doInstallFn;resolve/safeInstalltake aninstallFunc, withEnsureCommandsupplying the realdoInstall.pkg/hooks/builtins— removedvar httpPostClientandSetHTTPPostClientUnsafeForTest;Registernow takes functionalOptions (WithHTTPPostClient(...)), andhttp_postis a closure over anhttpDoer. The SSRF-safe client remains the production default; only tests inject the unsafe client. Deletedexport_test.goandmain_test.go.pkg/tui/components/reasoningblock— replaced package-levelvar nowFuncwith a per-instanceModel.nowclock field defaulting totime.Now.pkg/gateway— introduced aLoadertype (memoization viasync.Once+ injectable fetch),NewLoader()/NewStaticLoader(), and context plumbingWithLoader/loaderFromwith a package-leveldefaultLoader. Removed thecatalogOnce/catalogCached/catalogOverrideglobals and deletedtesting.go. Production call sites use the default loader via context transparently.A follow-up commit hardens the new seams: nil-safe
WithLoader/loaderFrom, a zero-value-safeLoader.loadandStore.fetcher(), and a corrected godoc placement — all defensive, no behavior change.Validation
task lint— golangci-lint, custom cops, andgo mod tidyall cleantask test— full suite passesgo test -raceon the affected packages passes; the converted tests now runt.Parallel()