Commit f9b6274
authored
[test] Add tests for config.formatErrorContext, formatValidationErrorRecursive, formatSchemaError (#1435)
## Test Coverage Improvement: Schema Error Formatting Functions
### Functions Analyzed
- **Package**: `internal/config`
- **Functions**:
- `formatErrorContext` — provides human-friendly diagnostic context for
JSON schema validation errors based on message content (7 if-branches)
- `formatValidationErrorRecursive` — recursively formats schema errors
with depth-based indentation
- `formatSchemaError` — entry point that dispatches to the above and
appends version + documentation footer
- **Previous Coverage**: ~0% direct coverage (only reached indirectly
via `TestEnhancedErrorMessages` which requires network-fetched schemas)
- **Complexity**: High — `formatErrorContext` has 7 independent
if-branches plus a keyword-location check;
`formatValidationErrorRecursive` is recursive with depth tracking
### Why These Functions?
`formatErrorContext` has the highest branch density in
`validation_schema.go` with 7 independent conditional blocks, none
directly tested. The existing `TestEnhancedErrorMessages` calls
`validateJSONSchema` which fetches a schema from GitHub over the
network, making those tests environment-dependent and leaving the
formatting functions without reliable direct coverage.
### Tests Added
- ✅ **`TestFormatErrorContext`** (20 table-driven cases):
- All 7 branch conditions: `additionalProperties`, type mismatch
(`expected`+`but got`/`type`), enum (`value must be one of` / `must
be`), missing required (`missing properties` / `required`), pattern
(`does not match pattern` / `pattern`), min/max (`must be >=` / `must be
<=` / `minimum` / `maximum`), oneOf (`doesn't validate with any of` /
`oneOf`)
- Keyword location shown when different from instance location
- Keyword location suppressed when same as instance location or empty
- Prefix string prepended correctly to all output lines
- Unrecognized messages return empty string
- ✅ **`TestFormatValidationErrorRecursive`** (7 sub-tests):
- Root-level error formatting with location and message
- Empty `InstanceLocation` falls back to `(root)` placeholder
- Depth 0 appends trailing newline; depth > 0 does not
- Recursive processing of `Causes` slice
- Nested causes get 2× indentation per depth level
- Multiple root-level causes are all included
- Context details from `formatErrorContext` are included in output
- ✅ **`TestFormatSchemaError`** (6 sub-tests):
- `nil` input returns `nil`
- `*jsonschema.ValidationError` gets full formatted message with
version, location, and documentation footer
- Non-schema `errors.New` gets compact one-line format with version
- `fmt.Errorf` wrapped errors get compact format
- `additionalProperties` errors include context details in full path
- Recursive causes are included in output
### Test Execution
Tests are designed to pass in CI where Go module dependencies are
available. They use `jsonschema.ValidationError` struct directly (no
network required), `version.Set()` for deterministic version assertions,
and follow the project's table-driven test conventions with
`testify/assert`.
---
*Generated by Test Coverage Improver*
> Generated by [Test Coverage
Improver](https://github.com/github/gh-aw-mcpg/actions/runs/22451674798)
> [!WARNING]
> <details>
> <summary>1 file changed
Lines changed: 462 additions & 0 deletions
0 commit comments