[test] Add tests for guard.WasmGuard.LabelResponse#8383
Conversation
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>
There was a problem hiding this comment.
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
LabelResponsecoverage-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
LabelResponseparsing 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
| // 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. |
| // 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") |
|
@copilot address review feedback |
…e input-preparation test assertions
Both review points have been addressed:
|
Test Coverage Improvement:
WasmGuard.LabelResponseFunction Analyzed
internal/guardWasmGuard.LabelResponse(wasm.go:610)Why This Function?
WasmGuard.LabelResponsehad 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
tool_args— exercises lines 619-621 (state map extraction)*difc.Capabilities— exercises thecaps != nilbranch (line 626)labeled_pathsJSON response — exercisesparsePathLabeledResponsepath (lines 647-649)itemsJSON response — exercisesparseCollectionLabeledDatapath (lines 652-654), verifies returned*CollectionLabeledDatareturn nil, nil(line 657)WASM Fixtures
Three new WASM binary fixtures write specific JSON payloads to the output buffer at runtime using
i32.storeinstructions (4-byte little-endian stores, consistent with existing test WASM patterns):labelResponseWritesEmptyObjectWasm{}labelResponseWritesItemsWasm{"items":[{"data":"x"}]}labelResponseWritesLabeledPathsWasm{"labeled_paths":[],"items_path":"/items"}Coverage Report
Test Execution
All 7 new tests pass:
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.netgo.opentelemetry.iogo.yaml.ingolang.orggoogle.golang.orggopkg.ingoproxy.cngoproxy.iomirrors.aliyun.comproxy.golang.orgreleaseassets.githubusercontent.comSee Network Configuration for more information.