Skip to content

[test] Add tests for server.ensureGuardInitialized and normalizeScopeKind#2037

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test/ensure-guard-initialized-coverage-f23f96946df355e5
Draft

[test] Add tests for server.ensureGuardInitialized and normalizeScopeKind#2037
github-actions[bot] wants to merge 1 commit intomainfrom
test/ensure-guard-initialized-coverage-f23f96946df355e5

Conversation

@github-actions
Copy link
Contributor

Test Coverage Improvement: ensureGuardInitialized + normalizeScopeKind

Functions Analyzed

Detail
Package internal/server
File unified.go
Functions ensureGuardInitialized (~100 LOC, high complexity) + normalizeScopeKind
Previous Coverage 0% direct coverage (only indirect via integration-style callBackendTool tests)
New Coverage All branches covered
Complexity High – ensureGuardInitialized has 10+ distinct branches including caching, error propagation, session creation, label merging, and mode parsing

Why These Functions?

ensureGuardInitialized is the core DIFC session-initialization routine. It:

  • Resolves and validates the guard policy
  • Implements a policy-hash-keyed cache to avoid redundant LabelAgent calls
  • Parses DIFCMode strings from guard results
  • Merges secrecy/integrity tags into the agent registry
  • Creates session state on first use

Despite being central to DIFC enforcement, it had zero direct test coverage – every branch was reachable only via expensive end-to-end callBackendTool integration tests that require a live HTTP backend.

normalizeScopeKind is a pure helper with no tests at all.

Tests Added

normalizeScopeKind (7 tests):

  • nil input → nil output
  • ✅ Empty map → empty map copy
  • ✅ Map without scope_kind field → copy unchanged
  • scope_kind already lowercase → unchanged
  • scope_kind UPPERCASE → lowercased
  • scope_kind mixed-case + whitespace → trimmed and lowercased
  • ✅ Non-string scope_kind → type assertion skipped, value preserved
  • ✅ Input map not mutated (returns copy)

ensureGuardInitialized (12 tests):

  • ✅ Policy nil → evaluator default mode returned, no error
  • resolveGuardPolicy error → wrapped error propagated
  • ✅ Cache hit (same session/server/policy hash) → LabelAgent not called again
  • LabelAgent returns error → error propagated
  • LabelAgent returns nil result → error propagated
  • DIFCMode empty → falls back to evaluator default
  • DIFCMode valid non-empty → overrides evaluator default
  • DIFCMode invalid string → error propagated
  • ✅ New session created when none exists; GuardInit state written correctly
  • ✅ Labels from LabelAgent merged into agentRegistry
  • ✅ Stale policy hash → cache invalidated, LabelAgent re-called
  • ✅ Existing session with nil GuardInit map → map created before writing
  • ✅ Normalized policy stored with scope_kind lowercased via normalizeScopeKind
  • ✅ Multiple serverIDs tracked independently within the same session
  • ✅ Union-semantics: tags from multiple guards are additive, not overwriting

Implementation Notes

Tests use a lightweight newMinimalUnifiedServer helper that constructs a UnifiedServer directly (without calling NewUnified or starting backend servers), making them fast pure-unit tests with no network or process dependencies.

Three new test doubles are introduced:

  • configurableGuard – configurable LabelAgent return values per test
  • countGuard – wraps configurableGuard to count LabelAgent invocations
  • noopBackendCaller – no-op guard.BackendCaller

All names are distinct from existing test types in the package (mockGuard, writeSinkTestGuard, labelAgentTestGuard).


Generated by Test Coverage Improver
Next run candidates: resolveGuardPolicy Guards-config path, mcp.initializeHTTPSession error branches, config.validateStandardServerConfig HTTP+mounts path

Generated by Test Coverage Improver ·

Warning

⚠️ Firewall blocked 3 domains

The following domains were blocked by the firewall during workflow execution:

  • goproxy.io
  • proxy.golang.org
  • sum.golang.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "goproxy.io"
    - "proxy.golang.org"
    - "sum.golang.org"

See Network Configuration for more information.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants