Skip to content

[test] Add tests for server.resolveGuardPolicy and normalizeScopeKind#2092

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test/resolve-guard-policy-coverage-d0ec0d96863f0254
Draft

[test] Add tests for server.resolveGuardPolicy and normalizeScopeKind#2092
github-actions[bot] wants to merge 1 commit intomainfrom
test/resolve-guard-policy-coverage-d0ec0d96863f0254

Conversation

@github-actions
Copy link
Contributor

Test Coverage Improvement: resolveGuardPolicy and normalizeScopeKind

Functions Analyzed

  • Package: internal/server
  • Functions: resolveGuardPolicy, normalizeScopeKind, resolveWriteSinkPolicy
  • File: internal/server/unified.go
  • Complexity: High (resolveGuardPolicy has 11 distinct code paths; normalizeScopeKind is a pure normalizer with type-dispatch)

Why These Functions?

resolveGuardPolicy (42 lines, 11 code paths) had only 1 of 11 paths tested in guard_policy_parsing_test.go. It drives all DIFC guard policy resolution and is called on every tool invocation when DIFC is enabled. normalizeScopeKind had zero tests despite being called during guard session initialization.

Tests Added

New file: internal/server/resolve_guard_policy_test.go32 test cases

normalizeScopeKind (10 tests)

  • nil input returns nil
  • ✅ Empty map returns empty copy
  • ✅ Map with no scope_kind field — other fields preserved
  • scope_kind already lowercase — unchanged
  • scope_kind uppercase → lowercased
  • scope_kind with leading/trailing spaces → trimmed
  • scope_kind uppercase + spaces → trimmed and lowercased
  • ✅ Non-string scope_kind → preserved unchanged
  • ✅ Other fields preserved alongside scope_kind
  • ✅ Input map not mutated (returns new map)

resolveGuardPolicy (18 tests)

  • ✅ Nil cfg → returns ("legacy", nil, nil)
  • ✅ Global policy override with valid AllowOnly policy, default "override" source
  • ✅ Global policy override with custom source ("cli")
  • ✅ Global policy override with write-sink policy and "env" source
  • ✅ Global policy override with invalid policy (empty) → error
  • ✅ Server ID not found in cfg.Servers"legacy"
  • ✅ Nil server config entry → "legacy"
  • ✅ Valid server guard-policies"server" source
  • ✅ Invalid server guard-policies (missing min-integrity) → error
  • ✅ No guard-policies, empty Guard field → "legacy"
  • Guard set but not in cfg.Guards"legacy"
  • Guard set, cfg.Guards[name] is nil → "legacy"
  • Guard set, guard config has nil Policy"legacy"
  • Guard set, guard config has valid AllowOnly policy → "config" source
  • Guard set, guard config has valid WriteSink policy → "config" source
  • Guard set, guard config has invalid policy (empty) → error
  • ✅ Empty cfg.Servers map → "legacy"

resolveWriteSinkPolicy (4 tests)

  • ✅ No policy configured → nil
  • ✅ Global write-sink policy returns WriteSinkPolicy
  • ✅ Allow-only policy → nil (no write-sink)
  • ✅ Error from resolveGuardPolicy → nil

Coverage Improvement

Function Before After
normalizeScopeKind 0% ~100% (all branches)
resolveGuardPolicy ~9% (1 of 11 paths) ~100% (all 11 paths)
resolveWriteSinkPolicy minimal full

Generated by Test Coverage Improver

Generated by Test Coverage Improver ·

Warning

⚠️ Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • proxy.golang.org

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

network:
  allowed:
    - defaults
    - "proxy.golang.org"

See Network Configuration for more information.

Adds comprehensive test coverage for two under-tested functions in
internal/server/unified.go:

- normalizeScopeKind: 10 test cases covering nil input, empty map,
  missing scope_kind field, uppercase/mixed-case normalization, leading/
  trailing whitespace trimming, non-string scope_kind values, preservation
  of other fields, and immutability of the input map.

- resolveGuardPolicy: 20 test cases covering all code paths including
  nil config (legacy), global policy override (with default/custom source,
  and invalid policy), server not found, nil server config, valid/invalid
  server guard-policies, Guard field pointing to missing/nil/policy-less/
  valid/invalid guard configs.

- resolveWriteSinkPolicy: 4 test cases covering no policy, write-sink
  policy, allow-only policy (nil write-sink), error from resolveGuardPolicy,
  and nil config.

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