test(tools): add unit tests for error handling functions

* Implement tests for error functions like errRequiredField, errInvalidField, and errEntityNotFound.
* Ensure proper metadata is returned for various error scenarios.
* Validate error handling in CRM, Files, and other tools.
* Introduce tests for parsing stored file IDs and UUIDs.
* Enhance coverage for helper functions related to project resolution and session management.
This commit is contained in:
Hein
2026-03-31 15:10:07 +02:00
parent acd780ac9c
commit f41c512f36
54 changed files with 1937 additions and 365 deletions

View File

@@ -1,6 +1,9 @@
package mcpserver
import (
"context"
"encoding/json"
"strings"
"testing"
"github.com/google/jsonschema-go/jsonschema"
@@ -40,3 +43,59 @@ func TestSetToolSchemasUsesStringUUIDsInListOutput(t *testing.T) {
t.Fatalf("id schema format = %q, want %q", idSchema.Format, "uuid")
}
}
func TestTruncateArgsRedactsSensitiveFields(t *testing.T) {
args := json.RawMessage(`{
"query": "todo from yesterday",
"content": "private thought body",
"content_base64": "c2VjcmV0LWJpbmFyeQ==",
"email": "user@example.com",
"nested": {
"notes": "private note",
"phone": "+1-555-0100",
"keep": "visible"
}
}`)
got := truncateArgs(args)
for _, secret := range []string{
"private thought body",
"c2VjcmV0LWJpbmFyeQ==",
"user@example.com",
"private note",
"+1-555-0100",
} {
if strings.Contains(got, secret) {
t.Fatalf("truncateArgs leaked sensitive value %q in %s", secret, got)
}
}
for _, expected := range []string{
`"query":"todo from yesterday"`,
`"keep":"visible"`,
`"content":"\u003credacted\u003e"`,
`"content_base64":"\u003credacted\u003e"`,
`"email":"\u003credacted\u003e"`,
`"notes":"\u003credacted\u003e"`,
`"phone":"\u003credacted\u003e"`,
} {
if !strings.Contains(got, expected) {
t.Fatalf("truncateArgs(%s) missing %q", got, expected)
}
}
}
func TestAddToolReturnsSchemaErrorInsteadOfPanicking(t *testing.T) {
server := mcp.NewServer(&mcp.Implementation{Name: "test", Version: "0.0.1"}, nil)
err := addTool(server, nil, &mcp.Tool{Name: "broken"}, func(_ context.Context, _ *mcp.CallToolRequest, _ struct{}) (*mcp.CallToolResult, chan int, error) {
return nil, nil, nil
})
if err == nil {
t.Fatal("addTool() error = nil, want schema inference error")
}
if !strings.Contains(err.Error(), `configure MCP tool "broken" schemas`) {
t.Fatalf("addTool() error = %q, want tool context", err.Error())
}
}