Skip to content

[test-improver] Improve tests for server requireGuardPolicyIfGuardEnabled#2096

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-improve-require-guard-policy-03d9702015ae936f
Draft

[test-improver] Improve tests for server requireGuardPolicyIfGuardEnabled#2096
github-actions[bot] wants to merge 1 commit intomainfrom
test-improve-require-guard-policy-03d9702015ae936f

Conversation

@github-actions
Copy link
Contributor

File Analyzed

  • Test File: internal/server/require_guard_policy_test.go
  • Package: internal/server
  • Lines of Code: 133 → 241 (+108 lines)

Improvements Made

1. Increased Coverage

requireGuardPolicyIfGuardEnabled in unified.go has five distinct code paths. The original three tests only exercised two of them (the policy == nil branches). Three paths had zero direct coverage:

Code Path Triggered by
Early return for nil guard g == nil at line 681
Early return for noop guard g.Name() == "noop" at line 681
Non-nil policy path (guard is kept) return g, nil at line 704
Error propagation return nil, err at line 687
Unknown server ID falls back to noop server not in cfg.Servers

2. New Tests Added

  • TestRequireGuardPolicyIfGuardEnabled_NilGuard — verifies that a nil guard is returned immediately without any policy lookup (early-return g == nil branch).

  • TestRequireGuardPolicyIfGuardEnabled_NoopGuard — verifies that a noop guard passes through unchanged (early-return g.Name() == "noop" branch), using guard.NewNoopGuard().

  • TestRequireGuardPolicyIfGuardEnabled_WithValidGlobalPolicy — verifies that when cfg.GuardPolicy is a valid allow-only policy the non-noop guard is kept (return g, nil at line 704).

  • TestRequireGuardPolicyIfGuardEnabled_WithInvalidGlobalPolicy — verifies that a GuardPolicy with an invalid min-integrity level causes resolveGuardPolicy to return an error, which is propagated as (nil, err).

  • TestRequireGuardPolicyIfGuardEnabled_UnknownServerID — verifies that calling with a server ID not present in cfg.Servers falls back to noop (nil policy + no guard-policies entry).

3. Test Patterns Consistent with Existing File

All new tests follow the established patterns in the file:

  • Use require for fatal assertions (error presence / guard non-nil)
  • Use assert for value comparisons
  • Reuse the existing mockGuard helper struct
  • No test helpers modified; guard.NewNoopGuard() used directly (already imported)

Why These Changes?

requireGuardPolicyIfGuardEnabled is the gating function that decides whether an active (non-noop) guard is allowed to enforce policy for a given backend server. Its five branches each have distinct runtime consequences for DIFC enforcement. The three previously untested paths include the only path that returns an error, the only path where a nil guard is a legitimate input, and the primary "happy path" where a configured global policy keeps the guard active. Covering these paths ensures regressions in the guard-policy resolution logic are caught by tests rather than surfacing at runtime.


Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests

Generated by Test Improver ·

Add five new test cases covering previously untested code paths in
requireGuardPolicyIfGuardEnabled:

- NilGuard: verifies the nil-guard early return (g == nil)
- NoopGuard: verifies the noop-guard early return (g.Name() == "noop")
- WithValidGlobalPolicy: verifies the non-nil policy path (guard is kept)
- WithInvalidGlobalPolicy: verifies error propagation from resolveGuardPolicy
- UnknownServerID: verifies fallback to noop for an unrecognised server ID

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
lpcox added a commit that referenced this pull request Mar 19, 2026
…sue (#2158)

## Summary

Adds a **Guard Filtering Summary** section to the repo-assist Monthly
Activity Issue so maintainers can see what objects the guard policy
blocked during each run.

## What it looks like

When the guard filters objects, the monthly activity issue will include:

```markdown
## Guard Filtering Summary

| Type | Count | Resources |
|------|-------|-----------|
| Issues | 7 | #1711, #2049, #2086, #2087, #2089, #2093, #2100 |
| PRs | 7 | #2037, #2042, #2061, #2063, #2064, #2092, #2096 |
| Other | 2 | actions_list, get_repository_tree |

**Policy**: `repos: [github/*], min-integrity: merged`
**Total filtered**: 54 items across 17 tool calls
```

When no filtering occurs, it states "No objects were filtered by the
guard policy."

## How it works

1. **New section in issue template** — "Guard Filtering Summary" sits
between "Future Work" and "Run History"
2. **New step 6** — Agent reads `/tmp/gh-aw/mcp-logs/rpc-messages.jsonl`
via bash, parses `DIFC_FILTERED` entries, groups by type
(issues/PRs/other), deduplicates across tool calls
3. **Python one-liner** — Extracts resource descriptions, groups into a
JSON summary the agent uses to populate the template

## Motivation

From [run
23274488766](https://github.com/github/gh-aw-mcpg/actions/runs/23274488766),
54 objects were silently filtered with `min-integrity: merged`. The
agent reported "GitHub API access to private repo issues unavailable"
without understanding why. This change gives both the agent and
maintainers explicit visibility into guard policy impact.
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