[test] Add tests for schema compilation and guard policy error paths#7112
Merged
lpcox merged 4 commits intoJun 6, 2026
Merged
Conversation
Improve test coverage for the most complex under-tested functions in
internal/config by adding comprehensive tests for error branches that
were previously unreachable via existing test paths.
Coverage improvements:
- getOrCompileSchema: 56% → 92% (+36 pp)
- fixSchemaBytes failure path
- schema without $id uses fallback embeddedSchemaID
- AddResource failure (invalid percent-encoded URL)
- Compile failure (duplicate canonical URI in definitions)
- validateJSONSchema: 89.5% → 100% (+10.5 pp)
- getOrCompileSchema error propagation
- invalid JSON config data
- fixSchemaBytes: 92.8% → 98.6% (+5.8 pp)
- type field with no pattern key
- type field with non-string pattern value
- type field with pattern that has no negative lookahead
- typeValue that is not a map[string]interface{}
- ValidateWriteSinkPolicy: → 100%
- empty/whitespace accept entry
- NormalizeGuardPolicy: → 100%
- []string Repos case error path
- normalizeToolCallLimits: 90.9% → 100% (+9.1 pp)
- empty/whitespace tool name key
- validateAgainstCustomSchema: 82.8% → 86.2% (+3.4 pp)
- cache hit with wrong (non-schema) type
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Collaborator
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves test coverage in internal/config by adding/expanding unit tests that exercise previously-uncovered error/edge branches in JSON schema compilation/validation and guard policy normalization/validation logic.
Changes:
- Added
schema_error_paths_test.goto coverfixSchemaBytes,getOrCompileSchema, andvalidateJSONSchemaerror/defensive branches. - Added
guard_policy_coverage_test.goto cover empty-input and error-propagation branches in guard policy validation/normalization. - Extended
validate_against_custom_schema_test.goto cover the “cache hit but wrong cached type” branch.
Show a summary per file
| File | Description |
|---|---|
| internal/config/validate_against_custom_schema_test.go | Adds coverage for unexpected (non-schema) values stored in the custom schema cache. |
| internal/config/schema_error_paths_test.go | New tests for schema fixing/compilation/validation error paths to raise coverage. |
| internal/config/guard_policy_coverage_test.go | New tests for guard policy validation/normalization edge cases. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 3/3 changed files
- Comments generated: 3
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Contributor
Fixed on the branch in 122e405, 293b461, and 8016e31. I rechecked the current tip locally with |
This was referenced Jun 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Coverage Improvement
Functions Analyzed
getOrCompileSchemainternal/configvalidateJSONSchemainternal/configfixSchemaBytesinternal/configValidateWriteSinkPolicyinternal/configNormalizeGuardPolicyinternal/confignormalizeToolCallLimitsinternal/configvalidateAgainstCustomSchemainternal/configWhy These Functions?
getOrCompileSchemahad the lowest coverage (56%) of all non-trivial functions in the testable packages. It usessync.Onceto cache schema compilation on first use, making its error branches (inside theDo()callback) unreachable from normal test paths. The function is critical — a bug here would silently break all JSON Schema validation on startup.fixSchemaByteshad four uncovered log/skip branches in thecustomServerConfig.typefixing logic, andvalidateJSONSchemahad two unreached defensive-error branches.The guard policy functions (
ValidateWriteSinkPolicy,NormalizeGuardPolicy,normalizeToolCallLimits) had small gaps in empty-input and error-propagation paths.Tests Added
schema_error_paths_test.go(new file):fixSchemaBytes— type field with no pattern keyfixSchemaBytes— type field pattern is non-stringfixSchemaBytes— type field pattern has no negative lookaheadfixSchemaBytes— type field value is not a map objectgetOrCompileSchema—fixSchemaBytesfailure (invalid JSON in embedded schema)getOrCompileSchema— schema without$iduses fallbackembeddedSchemaIDgetOrCompileSchema—AddResourcefailure (malformed percent-encoded URL)getOrCompileSchema—Compilefailure (duplicate canonical URIs in definitions)validateJSONSchema— invalid JSON config inputvalidateJSONSchema— schema compilation error propagationguard_policy_coverage_test.go(new file):ValidateWriteSinkPolicy— empty/whitespace accept entryNormalizeGuardPolicy—[]stringRepos case with invalid scopesnormalizeToolCallLimits— empty/whitespace-only tool name keysvalidate_against_custom_schema_test.go(extended):validateAgainstCustomSchema— cache hit with unexpected non-schema typeImplementation Notes
Tests for
getOrCompileSchemaerror paths reset thesync.Onceand package-level cache variables witht.Cleanuprestoration. This is safe because Go runs tests within a package sequentially by default (not.Parallel()is used).The remaining uncovered lines are dead code (e.g.,
json.Marshalon amap[string]interface{}cannot fail;json.UnmarshalafterfixSchemaBytesalways succeeds;io.ReadAllfailures over in-memory readers are impossible).Test Execution
All tests pass. No production code was modified.
Generated by Test Coverage Improver
Warning
Firewall blocked 8 domains
The following domains were blocked by the firewall during workflow execution:
go.opentelemetry.iogo.yaml.ingolang.orggoogle.golang.orggopkg.inproxy.golang.orgreleaseassets.githubusercontent.comsum.golang.orgSee Network Configuration for more information.