Skip to content

Commit ef7c3df

Browse files
authored
[test-improver] Improve tests for validateSingleAgentPolicy error paths (#12193)
## Summary Improved `internal/config/agent_policy_test.go` by adding coverage for previously-untested error branches in `validateSingleAgentPolicy` (`internal/config/agent_policy.go`). ## File analyzed `internal/config/agent_policy_test.go` (tests `internal/config/agent_policy.go`) ## Improvements made Added two new test functions: - `TestValidateSingleAgentPolicy_ErrorPaths` — table-driven test covering: - empty server entry (`Servers: []string{""}`) - unknown server reference - duplicate server entry - tools map referencing a server not in the policy's `Servers` list - empty tool entry - duplicate tool entry - invalid `AllowOnly` sub-policy (propagated validation error) - `TestValidateSingleAgentPolicy_ValidPolicySucceeds` — a happy-path sanity check that a fully valid policy returns no error. All new tests use testify (`require.Error`/`assert.ErrorContains`/`assert.NotContains`/`assert.NoError`), consistent with the existing file's conventions, and assert that the raw agent ID is never leaked into error messages (matching the existing whitespace test's behavior). ## Coverage before/after - `validateSingleAgentPolicy`: 87.5% → **100%** - `internal/config` package total: 96.9% → 97.1% ## Test output ``` go test -run "TestValidateSingleAgentPolicy" -v ./internal/config/ --- PASS: TestValidateSingleAgentPolicy_RejectsSurroundingWhitespace (0.00s) --- PASS: TestValidateSingleAgentPolicy_ErrorPaths (0.00s) --- PASS: TestValidateSingleAgentPolicy_ValidPolicySucceeds (0.00s) PASS ok github.com/github/gh-aw-mcpg/internal/config 0.008s ``` Also verified: - `go test -count=3 ./internal/config/...` — all pass (stability check) - `go vet ./internal/config/` — clean - `gofmt -l internal/config/agent_policy_test.go` — no output (already formatted) No existing tests were modified or removed; all changes are additive. > Generated by [Test Improver](https://github.com/github/gh-aw-mcpg/actions/runs/33342548419) · copilot · auto · 133.9 AIC · ⊞ 8.6K · [◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+test-improver%22&type=pullrequests) <!-- gh-aw-agentic-workflow: Test Improver, engine: copilot, model: auto, id: 33342548419, workflow_id: test-improver, run: https://github.com/github/gh-aw-mcpg/actions/runs/33342548419 --> <!-- gh-aw-workflow-id: test-improver --> <!-- gh-aw-workflow-call-id: github/gh-aw-mcpg/test-improver -->
2 parents ccc66fd + 6e2ae79 commit ef7c3df

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

internal/config/agent_policy_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,90 @@ func TestValidateSingleAgentPolicy_RejectsSurroundingWhitespace(t *testing.T) {
8686
}
8787
}
8888

89+
// TestValidateSingleAgentPolicy_ErrorPaths covers the remaining validation
90+
// failure branches in validateSingleAgentPolicy that were previously
91+
// uncovered: empty server/tool entries and duplicate server/tool references.
92+
func TestValidateSingleAgentPolicy_ErrorPaths(t *testing.T) {
93+
servers := map[string]*ServerConfig{"github": {}, "fetch": {}}
94+
95+
tests := []struct {
96+
name string
97+
policy *AgentPolicy
98+
errContains string
99+
}{
100+
{
101+
name: "empty server entry",
102+
policy: &AgentPolicy{Servers: []string{""}},
103+
errContains: "must be non-empty strings",
104+
},
105+
{
106+
name: "unknown server reference",
107+
policy: &AgentPolicy{Servers: []string{"unknown-server"}},
108+
errContains: "references unknown server",
109+
},
110+
{
111+
name: "duplicate server entry",
112+
policy: &AgentPolicy{Servers: []string{"github", "github"}},
113+
errContains: "must not contain duplicate server",
114+
},
115+
{
116+
name: "tools reference server not in policy's servers list",
117+
policy: &AgentPolicy{
118+
Servers: []string{"github"},
119+
Tools: map[string][]string{"fetch": {"fetch_url"}},
120+
},
121+
errContains: "not in the policy's servers list",
122+
},
123+
{
124+
name: "empty tool entry",
125+
policy: &AgentPolicy{
126+
Servers: []string{"github"},
127+
Tools: map[string][]string{"github": {""}},
128+
},
129+
errContains: "must be non-empty strings",
130+
},
131+
{
132+
name: "duplicate tool entry",
133+
policy: &AgentPolicy{
134+
Servers: []string{"github"},
135+
Tools: map[string][]string{"github": {"search_code", "search_code"}},
136+
},
137+
errContains: "must not contain duplicate tool",
138+
},
139+
{
140+
name: "invalid allow-only policy",
141+
policy: &AgentPolicy{
142+
Servers: []string{"github"},
143+
AllowOnly: &AllowOnlyPolicy{MinIntegrity: "not-a-real-level"},
144+
},
145+
errContains: "allow-only is invalid",
146+
},
147+
}
148+
149+
for _, tt := range tests {
150+
t.Run(tt.name, func(t *testing.T) {
151+
err := validateSingleAgentPolicy("secret-agent-id", tt.policy, servers)
152+
require.Error(t, err)
153+
assert.ErrorContains(t, err, tt.errContains)
154+
assert.NotContains(t, err.Error(), "secret-agent-id")
155+
})
156+
}
157+
}
158+
159+
func TestValidateSingleAgentPolicy_ValidPolicySucceeds(t *testing.T) {
160+
servers := map[string]*ServerConfig{"github": {}, "fetch": {}}
161+
policy := &AgentPolicy{
162+
Servers: []string{"github", "fetch"},
163+
Tools: map[string][]string{
164+
"github": {"search_code", "get_file_contents"},
165+
"fetch": {"*"},
166+
},
167+
}
168+
169+
err := validateSingleAgentPolicy("agent-1", policy, servers)
170+
assert.NoError(t, err)
171+
}
172+
89173
// --- Config accessor tests ---
90174

91175
func TestConfig_AgentPolicyAccessors(t *testing.T) {

0 commit comments

Comments
 (0)