Skip to content

[test-improver] Improve tests for guard Registry.HasNonNoopGuard#3158

Merged
lpcox merged 1 commit intomainfrom
test-improver/guard-has-non-noop-guard-3e1ab3a737a10cdf
Apr 5, 2026
Merged

[test-improver] Improve tests for guard Registry.HasNonNoopGuard#3158
lpcox merged 1 commit intomainfrom
test-improver/guard-has-non-noop-guard-3e1ab3a737a10cdf

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 4, 2026

Test Improvements: internal/guard/guard_test.go

File Analyzed

  • Test File: internal/guard/guard_test.go
  • Package: internal/guard
  • Lines of Code: 802 → 847 (+45 lines)

Improvements Made

1. Increased Coverage — Direct Tests for Registry.HasNonNoopGuard

Problem: The HasNonNoopGuard method in internal/guard/registry.go was called from internal/server/unified.go to determine whether DIFC should be auto-enabled:

// server/unified.go line 170
if !us.enableDIFC && (us.guardRegistry.HasNonNoopGuard() || ...) {

Despite being a meaningful predicate used in server startup logic, HasNonNoopGuard had no dedicated unit tests in guard_test.go. All other Registry methods (Register, Get, Has, List, Remove, GetGuardInfo) had explicit subtests — HasNonNoopGuard was the only gap.

2. New Test: TestGuardRegistry_HasNonNoopGuard

Added 6 subtests covering all branches of the HasNonNoopGuard method:

  • empty registry returns false — verifies base case
  • only noop guards returns false — verifies multiple noop guards don't trigger true
  • one non-noop guard returns true — verifies detection of a single non-noop guard
  • mix of noop and non-noop returns true — verifies any non-noop triggers true
  • removing non-noop guard makes it return false — verifies dynamic removal
  • replacing non-noop with noop returns false — verifies guard replacement

The tests reuse the existing mockGuard helper and follow the established subtest patterns throughout the file (consistent with TestGuardRegistry).

3. Cleaner & More Stable Tests

  • ✅ Each subtest uses its own fresh NewRegistry() — fully isolated, no shared state
  • ✅ Uses assert.True/assert.False for clear, readable assertions
  • ✅ Tests the state before and after mutations (remove/replace) to validate dynamic behaviour
  • ✅ Follows the existing file style exactly — no new imports needed

Why These Changes?

HasNonNoopGuard is the sentinel used by the server to auto-activate DIFC when a non-trivial guard is registered. A regression in this predicate (e.g., always returning false) would silently disable DIFC enforcement at startup — a security-relevant failure. Despite the rest of the Registry API being well-tested, this method was the only gap.


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

Generated by Test Improver ·

Add TestGuardRegistry_HasNonNoopGuard covering all branches of the
HasNonNoopGuard method which was used in server/unified.go but had
no dedicated unit tests:

- empty registry returns false
- only noop guards returns false
- one non-noop guard returns true
- mix of noop and non-noop returns true
- removing the non-noop guard reverts to false
- replacing non-noop with noop reverts to false

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review April 5, 2026 22:07
Copilot AI review requested due to automatic review settings April 5, 2026 22:07
@lpcox lpcox merged commit 1699231 into main Apr 5, 2026
3 checks passed
@lpcox lpcox deleted the test-improver/guard-has-non-noop-guard-3e1ab3a737a10cdf branch April 5, 2026 22:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds dedicated unit tests for Registry.HasNonNoopGuard in internal/guard, improving confidence in the predicate used by server startup logic to detect whether any “real” (non-noop) guards are registered.

Changes:

  • Introduces TestGuardRegistry_HasNonNoopGuard with subtests covering empty, noop-only, non-noop, mixed, removal, and replacement scenarios.
  • Ensures each subtest uses a fresh registry instance for isolation.
Show a summary per file
File Description
internal/guard/guard_test.go Adds new unit tests covering all main behavioral branches of Registry.HasNonNoopGuard.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

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.

2 participants