Skip to content

[test] Add tests for guard.WasmGuard.LabelResponse#8383

Merged
lpcox merged 3 commits into
mainfrom
test-coverage-wasm-label-response-bf796c1725c26348
Jul 1, 2026
Merged

[test] Add tests for guard.WasmGuard.LabelResponse#8383
lpcox merged 3 commits into
mainfrom
test-coverage-wasm-label-response-bf796c1725c26348

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Test Coverage Improvement: WasmGuard.LabelResponse

Function Analyzed

  • Package: internal/guard
  • Function: WasmGuard.LabelResponse (wasm.go:610)
  • Previous Coverage: 59.1%
  • New Coverage: 100.0%
  • Guard package overall: 92.5% → 93.6%
  • Complexity: High (multiple nested branches, WASM-invocation, JSON response parsing)

Why This Function?

WasmGuard.LabelResponse had the lowest coverage (59.1%) of any non-trivial function in the testable guard package. It contains three groups of uncovered branches: (1) context request-state extraction, (2) capabilities injection, and (3) three distinct JSON-response-parsing paths that each route to a different code path or final return.

Tests Added

  • Context state with tool_args — exercises lines 619-621 (state map extraction)
  • Non-map context state ignored — exercises line 619 false branch
  • Non-nil *difc.Capabilities — exercises the caps != nil branch (line 626)
  • Both state and capabilities together — combined input-preparation path
  • labeled_paths JSON response — exercises parsePathLabeledResponse path (lines 647-649)
  • items JSON response — exercises parseCollectionLabeledData path (lines 652-654), verifies returned *CollectionLabeledData
  • Empty-object JSON response — exercises the final return nil, nil (line 657)

WASM Fixtures

Three new WASM binary fixtures write specific JSON payloads to the output buffer at runtime using i32.store instructions (4-byte little-endian stores, consistent with existing test WASM patterns):

Fixture JSON Written Purpose
labelResponseWritesEmptyObjectWasm {} No-labeling final return
labelResponseWritesItemsWasm {"items":[{"data":"x"}]} Items-based labeling path
labelResponseWritesLabeledPathsWasm {"labeled_paths":[],"items_path":"/items"} Path-based labeling path

Coverage Report

Before: WasmGuard.LabelResponse  59.1%
After:  WasmGuard.LabelResponse  100.0%
Improvement: +40.9%

Guard package: 92.5% → 93.6%

Test Execution

All 7 new tests pass:

--- PASS: TestLabelResponse_ContextStateWithToolArgs
--- PASS: TestLabelResponse_ContextStateNonMapIsIgnored
--- PASS: TestLabelResponse_WithCapabilities
--- PASS: TestLabelResponse_NoLabelingResponse
--- PASS: TestLabelResponse_ItemsResponse
--- PASS: TestLabelResponse_LabeledPathsResponse
--- PASS: TestLabelResponse_ContextStateAndCapabilities
PASS
ok  github.com/github/gh-aw-mcpg/internal/guard  0.015s  coverage: 93.6% of statements

Generated by Test Coverage Improver
Next run will target the next most complex under-tested function

Warning

Firewall blocked 11 domains

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

  • athens.azurefd.net
  • go.opentelemetry.io
  • go.yaml.in
  • golang.org
  • google.golang.org
  • gopkg.in
  • goproxy.cn
  • goproxy.io
  • mirrors.aliyun.com
  • proxy.golang.org
  • releaseassets.githubusercontent.com

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

network:
  allowed:
    - defaults
    - "athens.azurefd.net"
    - "go.opentelemetry.io"
    - "go.yaml.in"
    - "golang.org"
    - "google.golang.org"
    - "gopkg.in"
    - "goproxy.cn"
    - "goproxy.io"
    - "mirrors.aliyun.com"
    - "proxy.golang.org"
    - "releaseassets.githubusercontent.com"

See Network Configuration for more information.

Generated by Test Coverage Improver · 707.6 AIC · ⊞ 7.1K ·

Cover previously untested branches in WasmGuard.LabelResponse (wasm.go:610):

- Context state extraction with tool_args (lines 618-624)
- Non-map context state ignored gracefully (line 619 false branch)
- Non-nil capabilities added to WASM input (lines 625-627)
- labeled_paths JSON response path via parsePathLabeledResponse (lines 647-649)
- items JSON response path via parseCollectionLabeledData (lines 652-654)
- Final no-labeling return nil,nil (line 657)

Three new WASM fixtures written using i32.store instructions write hardcoded
JSON payloads to the output buffer at runtime:
- labelResponseWritesEmptyObjectWasm      → writes '{}'
- labelResponseWritesItemsWasm            → writes '{"items":[{"data":"x"}]}'
- labelResponseWritesLabeledPathsWasm     → writes labeled_paths/items_path response

Coverage for WasmGuard.LabelResponse: 59.1% → 100.0%
Guard package overall: 92.5% → 93.6%

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review July 1, 2026 14:43
Copilot AI review requested due to automatic review settings July 1, 2026 14:43

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 targeted unit tests in internal/guard to raise coverage for WasmGuard.LabelResponse, exercising its early-return, JSON parsing, and path/collection labeling branches using minimal WASM bytecode fixtures.

Changes:

  • Introduces new LabelResponse coverage-focused tests for empty result, {} (no-labeling), "items" response parsing, and "labeled_paths" error propagation.
  • Adds three new embedded WASM fixtures that write specific JSON payloads to the output buffer to drive the different LabelResponse parsing paths.
  • Adds tests intended to cover context request-state extraction (tool_args) and capability injection paths.
Show a summary per file
File Description
internal/guard/wasm_label_response_coverage_test.go New tests + WASM fixtures to exercise WasmGuard.LabelResponse branches and improve guard package coverage.

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 on lines +13 to +19
// labelResponseWritesEmptyObjectWasm exports "label_response" and "memory".
// It writes the following JSON to the output buffer and returns its length:
//
// {}
//
// Used to exercise the 'no labeled_paths, no items → return nil, nil' path in LabelResponse.
// The function writes 4 bytes (little-endian i32) and returns 2 so only the '{' '}' bytes are read.
Comment on lines +71 to +75
// TestLabelResponse_ContextStateWithToolArgs verifies that when the context contains
// a request state map with a "tool_args" key, LabelResponse extracts it into the WASM
// input and still returns (nil, nil) when the WASM guard returns an empty result.
func TestLabelResponse_ContextStateWithToolArgs(t *testing.T) {
g, cleanup := setupRawWasmModule(t, labelResponseReturnsZeroWasm, "lresp-state-tool-args")
@lpcox

lpcox commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

@copilot address review feedback

Copilot AI commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

@copilot address review feedback

Both review points have been addressed:

  1. WAT source comments — Added // Compiled from: WAT blocks to all three existing fixtures (labelResponseWritesEmptyObjectWasm, labelResponseWritesItemsWasm, labelResponseWritesLabeledPathsWasm) matching the pattern used elsewhere in the package.

  2. Input-preparation tests now validate actual behavior — Added a new fixture labelResponseWritesItemsIfInputLargeWasm that produces {"items":[{"data":"x"}]} only when inLen > 50, otherwise {}. The baseline input ({"tool_name":"my_tool","tool_result":null}) is 42 bytes. Adding tool_args pushes it to 72 bytes, capabilities to 60 bytes, both to 97 bytes. The four input-preparation tests now assert require.NotNil on the result (input was injected → WASM returns items → non-nil) vs require.Nil for the non-map-state test (injection skipped → WASM returns {} → nil). These assertions will fail if the injection logic is removed.

Copilot finished work on behalf of lpcox July 1, 2026 15:42
Copilot AI requested a review from lpcox July 1, 2026 15:42
@lpcox lpcox merged commit 23049a8 into main Jul 1, 2026
17 checks passed
@lpcox lpcox deleted the test-coverage-wasm-label-response-bf796c1725c26348 branch July 1, 2026 15:50
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.

3 participants