Skip to content

[test-improver] Improve tests for guard package (normalizeLabelListField)#8210

Merged
lpcox merged 3 commits into
mainfrom
test-improver/wasm-payload-normalize-9dc631260b42a35c
Jun 28, 2026
Merged

[test-improver] Improve tests for guard package (normalizeLabelListField)#8210
lpcox merged 3 commits into
mainfrom
test-improver/wasm-payload-normalize-9dc631260b42a35c

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

File Analyzed

  • Test File: internal/guard/wasm_payload_test.go
  • Package: internal/guard
  • Lines Added: +123

Improvements Made

1. Added TestNormalizeLabelListField

The normalizeLabelListField helper in wasm_payload.go had no dedicated unit tests and sat at 82.6% coverage (4 branches uncovered). Tests for this function were entirely indirect — exercised only through buildStrictLabelAgentPayload in wasm_test.go.

Added a comprehensive, table-driven TestNormalizeLabelListField directly targeting the function:

normalizeLabelListField: 82.6% → 100%
internal/guard package:  92.0% → 92.5%

2. Increased Coverage — Previously Uncovered Branches

Branch Input Reason Not Covered
return nil, fmt.Errorf("...expected array of strings or comma/newline-delimited string...") raw = 42 (integer) Never tested with non-string, non-array input
return nil, fmt.Errorf("...must include at least one non-empty label") (first) raw = "," Never tested with delimiter-only string
if trimmed == "" { continue } raw = "valid, ,other" Never tested with whitespace-only intermediate part
return nil, fmt.Errorf("...must include at least one non-empty label") (second) raw = " " Never tested with all-whitespace string

3. Test Cases Added (17 subtests)

Array path:

  • Valid array → trimmed labels returned
  • Empty array → empty slice
  • Array with empty string entry → error
  • Array with non-string entry → error

Non-array, non-string path (previously 0% covered):

  • Integer input → error
  • Boolean input → error

String path:

  • Comma-delimited string → split labels
  • Newline-delimited string → split labels
  • Mixed comma and newline delimiters
  • Leading/trailing whitespace trimmed per label
  • Single label string

String path error cases (previously 0% covered):

  • Empty string → error
  • Comma-only string → error
  • Newline-only string → error
  • Whitespace-only string → error (hits both continue and len(out)==0 branches)
  • Comma-delimited whitespace-only parts → error

Mixed case:

  • Whitespace-only part among valid parts is skipped (hits continue, result non-empty)

4. Better Test Structure

  • ✅ Used t.Parallel() at top level and in each subtest for faster execution
  • ✅ Table-driven pattern with clear name, raw, want, wantErr fields
  • ✅ Comments explaining which branches each case covers
  • ✅ Consistent use of require for fatal checks, assert for informational checks

Test Execution

=== RUN   TestNormalizeLabelListField
--- PASS: TestNormalizeLabelListField (0.00s)
    --- PASS: .../valid_array_returns_trimmed_labels (0.00s)
    --- PASS: .../empty_array_returns_empty_slice (0.00s)
    --- PASS: .../array_with_empty_string_entry_returns_error (0.00s)
    --- PASS: .../array_with_non-string_entry_returns_error (0.00s)
    --- PASS: .../integer_value_is_not_array_or_string (0.00s)
    --- PASS: .../bool_value_is_not_array_or_string (0.00s)
    --- PASS: .../comma-delimited_string_returns_split_labels (0.00s)
    --- PASS: .../newline-delimited_string_returns_split_labels (0.00s)
    --- PASS: .../mixed_comma_and_newline_delimiters (0.00s)
    --- PASS: .../leading_and_trailing_whitespace_trimmed_from_each_label (0.00s)
    --- PASS: .../single_label_string (0.00s)
    --- PASS: .../empty_string_returns_error (0.00s)
    --- PASS: .../string_with_only_a_comma_delimiter_returns_error (0.00s)
    --- PASS: .../string_with_only_newline_delimiter_returns_error (0.00s)
    --- PASS: .../whitespace-only_string_returns_error (0.00s)
    --- PASS: .../comma-delimited_whitespace-only_parts_returns_error (0.00s)
    --- PASS: .../whitespace-only_part_among_valid_parts_is_skipped (0.00s)

ok  github.com/github/gh-aw-mcpg/internal/guard  0.077s  coverage: 92.5%

All 26 package tests pass (including the 17 new subtests).

Why These Changes?

normalizeLabelListField is a security-relevant helper that validates and normalizes the refusal-labels field in guard policies used to control tool call behavior. Having comprehensive direct unit tests for this function:

  1. Makes the validation semantics explicit and self-documenting
  2. Ensures edge cases (whitespace, mixed delimiters, wrong types) behave correctly
  3. Improves confidence when modifying label parsing logic in the future

The existing indirect coverage through buildStrictLabelAgentPayload only exercised the happy paths, leaving 4 error branches entirely untested.


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

Warning

Firewall blocked 1 domain

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

  • index.crates.io

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

network:
  allowed:
    - defaults
    - "index.crates.io"

See Network Configuration for more information.

Generated by Test Improver · 1.2K AIC · ⊞ 29.6K ·

Add TestNormalizeLabelListField to wasm_payload_test.go covering all
previously-uncovered branches of the normalizeLabelListField helper:

- non-string, non-[]interface{} input (type error path)
- empty/delimiter-only string (len(parts)==0 error path)
- whitespace-only parts skipped via continue (empty trimmed part)
- all parts whitespace resulting in empty output (len(out)==0 error path)

Also adds positive test cases for comma-delimited strings, newline-
delimited strings, mixed delimiters, whitespace trimming, empty arrays,
and the array validation error paths.

Coverage: normalizeLabelListField 82.6% → 100%
Package:  internal/guard 92.0% → 92.5%

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds direct, table-driven unit coverage for the internal/guard helper normalizeLabelListField, making label parsing/validation behavior explicit and fully exercised (including previously uncovered error branches).

Changes:

  • Added TestNormalizeLabelListField with comprehensive table-driven coverage across array/string/invalid-type inputs.
  • Increased branch coverage for delimiter/whitespace edge cases and invalid inputs.
Show a summary per file
File Description
internal/guard/wasm_payload_test.go Adds dedicated unit tests for normalizeLabelListField, covering success paths and previously uncovered error/edge branches.

Review details

Tip

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

  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment thread internal/guard/wasm_payload_test.go
Comment thread internal/guard/wasm_payload_test.go
lpcox and others added 2 commits June 28, 2026 10:15
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
GitHub Advanced Security started work on behalf of lpcox June 28, 2026 17:15 View session
GitHub Advanced Security started work on behalf of lpcox June 28, 2026 17:15 View session
GitHub Advanced Security finished work on behalf of lpcox June 28, 2026 17:16
GitHub Advanced Security finished work on behalf of lpcox June 28, 2026 17:16
@lpcox lpcox merged commit a733e83 into main Jun 28, 2026
27 checks passed
@lpcox lpcox deleted the test-improver/wasm-payload-normalize-9dc631260b42a35c branch June 28, 2026 17:18
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