X Tutup
Skip to content

[test] Add tests for middleware.applyJqSchema, savePayload, and WrapToolHandler uncovered paths#1704

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
coverage/middleware-jqschema-uncovered-paths-c3db2040123830b5
Draft

[test] Add tests for middleware.applyJqSchema, savePayload, and WrapToolHandler uncovered paths#1704
github-actions[bot] wants to merge 1 commit intomainfrom
coverage/middleware-jqschema-uncovered-paths-c3db2040123830b5

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Mar 9, 2026

Test Coverage Improvement: internal/middleware package

Function Analyzed

  • Package: internal/middleware
  • Functions: applyJqSchema, savePayload, WrapToolHandler
  • Previous Coverage: 75.2%
  • New Coverage: 88.3%
  • Complexity: High (multiple error handling branches, jq execution paths, filesystem operations)

Why These Functions?

Coverage analysis identified applyJqSchema (70%), savePayload (77.3%), and WrapToolHandler (80.5%) as the most complex under-tested functions in the middleware package. These functions handle critical error paths—jq execution failures, filesystem errors, and tool call result handling—that were completely untested.

Tests Added

TestApplyJqSchema_UncoveredPaths

  • ✅ No results from jq iterator (jq empty program produces zero outputs)
  • ✅ HaltError with nil value (jq halt — clean halt with no message)
  • ✅ HaltError with non-nil value (jq halt_error — halt with error message)
  • ✅ Generic jq type error (invalid operation on wrong type)

Tests inject custom-compiled jq programs by saving/restoring jqSchemaCode to precisely trigger each error branch.

TestSavePayload_ErrorCases

  • ✅ Directory creation failure (read-only parent directory)
  • ✅ File write failure (read-only target directory after MkdirAll succeeds)

TestWrapToolHandler_NilAndErrorResultCases

  • ✅ Handler returns nil result (pass-through without modification)
  • ✅ Handler returns IsError=true with no Go error (pass-through of MCP error result)

TestWrapToolHandler_SavePayloadFailure

  • ✅ Graceful continuation when payload save fails (read-only base directory)

TestWrapToolHandler_SchemaExtractionFailure

  • ✅ Falls back to original response when jq schema extraction fails (simulated compilation error)

Coverage Report

Before: 75.2% coverage
After:  88.3% coverage
Improvement: +13.1%

Function-level changes:
  applyJqSchema:   70.0% → 100.0%  (+30.0%)
  savePayload:     77.3% →  95.5%  (+18.2%)
  WrapToolHandler: 80.5% →  90.9%  (+10.4%)
```

## Test Execution

All new tests pass:

```
=== RUN   TestApplyJqSchema_UncoveredPaths
    --- PASS: TestApplyJqSchema_UncoveredPaths/no_results_from_iterator (0.00s)
    --- PASS: TestApplyJqSchema_UncoveredPaths/HaltError_with_nil_value_(clean_halt) (0.00s)
    --- PASS: TestApplyJqSchema_UncoveredPaths/HaltError_with_non-nil_value_(error_halt) (0.00s)
    --- PASS: TestApplyJqSchema_UncoveredPaths/generic_jq_error_(type_error) (0.00s)
--- PASS: TestApplyJqSchema_UncoveredPaths (0.00s)
=== RUN   TestSavePayload_ErrorCases
    --- PASS: TestSavePayload_ErrorCases/directory_creation_failure_with_read-only_parent (0.00s)
    --- PASS: TestSavePayload_ErrorCases/file_write_failure_with_read-only_directory (0.00s)
--- PASS: TestSavePayload_ErrorCases (0.00s)
=== RUN   TestWrapToolHandler_NilAndErrorResultCases
    --- PASS: TestWrapToolHandler_NilAndErrorResultCases/handler_returns_nil_result (0.00s)
    --- PASS: TestWrapToolHandler_NilAndErrorResultCases/handler_returns_IsError=true_with_no_error (0.00s)
--- PASS: TestWrapToolHandler_NilAndErrorResultCases (0.00s)
=== RUN   TestWrapToolHandler_SavePayloadFailure
--- PASS: TestWrapToolHandler_SavePayloadFailure (0.00s)
=== RUN   TestWrapToolHandler_SchemaExtractionFailure
--- PASS: TestWrapToolHandler_SchemaExtractionFailure (0.00s)

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

Generated by Test Coverage Improver

Warning

⚠️ Firewall blocked 8 domains

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

  • go.yaml.in
  • golang.org
  • gopkg.in
  • goproxy.cn
  • goproxy.io
  • invalidhostthatdoesnotexist12345.com
  • proxy.golang.org
  • sum.golang.org

…ler uncovered paths

Improves test coverage of internal/middleware package from 75.2% to 88.3%.

Changes:
- applyJqSchema: 70% -> 100% (added tests for no-results, HaltError nil/non-nil, generic jq errors)
- savePayload: 77.3% -> 95.5% (added tests for dir creation failure and file write failure)
- WrapToolHandler: 80.5% -> 90.9% (added tests for nil result, IsError=true, save failure, schema failure)

New test functions:
- TestApplyJqSchema_UncoveredPaths: tests all error branches in applyJqSchema by injecting
  custom jq programs (empty, halt, halt_error, type errors)
- TestSavePayload_ErrorCases: tests filesystem error paths using read-only directories
- TestWrapToolHandler_NilAndErrorResultCases: tests nil result and IsError=true pass-through
- TestWrapToolHandler_SavePayloadFailure: tests graceful handling when payload save fails
- TestWrapToolHandler_SchemaExtractionFailure: tests fallback when jq schema extraction fails

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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

X Tutup