[test] Add tests for server.ensureGuardInitialized and normalizeScopeKind#2037
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
[test] Add tests for server.ensureGuardInitialized and normalizeScopeKind#2037github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
Adds internal/server/ensure_guard_initialized_test.go covering all previously untested branches of two functions in unified.go: normalizeScopeKind (7 tests): - nil input, empty map, map without scope_kind field - scope_kind already lowercase, UPPERCASE, mixed-case+whitespace - non-string scope_kind (type assertion skipped) - immutability guarantee (input map is not modified) ensureGuardInitialized (12 tests): - policy nil → evaluator default mode returned - resolveGuardPolicy error propagation - cache hit → LabelAgent not called a second time - LabelAgent error propagation - LabelAgent nil result error - DIFCMode empty → falls back to evaluator default - DIFCMode valid → overrides evaluator default - DIFCMode invalid → error propagated - new session created when none exists - labels from LabelAgent merged into agentRegistry - stale policy hash → cache invalidation triggers re-init - existing session with nil GuardInit initialised correctly - NormalizedPolicy stored with scope_kind lowercased - multiple serverIDs in same session tracked independently - union semantics: tags from multiple guards additive Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Test Coverage Improvement:
ensureGuardInitialized+normalizeScopeKindFunctions Analyzed
internal/serverunified.goensureGuardInitialized(~100 LOC, high complexity) +normalizeScopeKindcallBackendTooltests)ensureGuardInitializedhas 10+ distinct branches including caching, error propagation, session creation, label merging, and mode parsingWhy These Functions?
ensureGuardInitializedis the core DIFC session-initialization routine. It:LabelAgentcallsDIFCModestrings from guard resultsDespite being central to DIFC enforcement, it had zero direct test coverage – every branch was reachable only via expensive end-to-end
callBackendToolintegration tests that require a live HTTP backend.normalizeScopeKindis a pure helper with no tests at all.Tests Added
normalizeScopeKind(7 tests):nilinput →niloutputscope_kindfield → copy unchangedscope_kindalready lowercase → unchangedscope_kindUPPERCASE→ lowercasedscope_kindmixed-case + whitespace → trimmed and lowercasedscope_kind→ type assertion skipped, value preservedensureGuardInitialized(12 tests):nil→ evaluator default mode returned, no errorresolveGuardPolicyerror → wrapped error propagatedLabelAgentnot called againLabelAgentreturns error → error propagatedLabelAgentreturnsnilresult → error propagatedDIFCModeempty → falls back to evaluator defaultDIFCModevalid non-empty → overrides evaluator defaultDIFCModeinvalid string → error propagatedGuardInitstate written correctlyLabelAgentmerged intoagentRegistryLabelAgentre-callednilGuardInitmap → map created before writingscope_kindlowercased vianormalizeScopeKindserverIDs tracked independently within the same sessionImplementation Notes
Tests use a lightweight
newMinimalUnifiedServerhelper that constructs aUnifiedServerdirectly (without callingNewUnifiedor starting backend servers), making them fast pure-unit tests with no network or process dependencies.Three new test doubles are introduced:
configurableGuard– configurableLabelAgentreturn values per testcountGuard– wrapsconfigurableGuardto countLabelAgentinvocationsnoopBackendCaller– no-opguard.BackendCallerAll names are distinct from existing test types in the package (
mockGuard,writeSinkTestGuard,labelAgentTestGuard).Generated by Test Coverage Improver
Next run candidates:
resolveGuardPolicyGuards-config path,mcp.initializeHTTPSessionerror branches,config.validateStandardServerConfigHTTP+mounts pathWarning
The following domains were blocked by the firewall during workflow execution:
goproxy.ioproxy.golang.orgsum.golang.orgTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.