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

@@ -31,10 +31,11 @@ type ServerConfig struct {
}
type MCPConfig struct {
Path string `yaml:"path"`
ServerName string `yaml:"server_name"`
Version string `yaml:"version"`
Transport string `yaml:"transport"`
Path string `yaml:"path"`
ServerName string `yaml:"server_name"`
Version string `yaml:"version"`
Transport string `yaml:"transport"`
SessionTimeout time.Duration `yaml:"session_timeout"`
}
type AuthConfig struct {

View File

@@ -7,6 +7,7 @@ import (
"strings"
"time"
"git.warky.dev/wdevs/amcs/internal/buildinfo"
"gopkg.in/yaml.v3"
)
@@ -46,6 +47,7 @@ func ResolvePath(explicitPath string) string {
}
func defaultConfig() Config {
info := buildinfo.Current()
return Config{
Server: ServerConfig{
Host: "0.0.0.0",
@@ -55,10 +57,11 @@ func defaultConfig() Config {
IdleTimeout: 60 * time.Second,
},
MCP: MCPConfig{
Path: "/mcp",
ServerName: "amcs",
Version: "0.1.0",
Transport: "streamable_http",
Path: "/mcp",
ServerName: "amcs",
Version: info.Version,
Transport: "streamable_http",
SessionTimeout: 10 * time.Minute,
},
Auth: AuthConfig{
HeaderName: "x-brain-key",

View File

@@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"testing"
"time"
)
func TestResolvePathPrecedence(t *testing.T) {
@@ -37,6 +38,7 @@ server:
port: 8080
mcp:
path: "/mcp"
session_timeout: "30m"
auth:
keys:
- id: "test"
@@ -80,6 +82,9 @@ logging:
if cfg.Server.Port != 9090 {
t.Fatalf("server port = %d, want 9090", cfg.Server.Port)
}
if cfg.MCP.SessionTimeout != 30*time.Minute {
t.Fatalf("mcp session timeout = %v, want 30m", cfg.MCP.SessionTimeout)
}
}
func TestLoadAppliesOllamaEnvOverrides(t *testing.T) {

View File

@@ -33,6 +33,9 @@ func (c Config) Validate() error {
if strings.TrimSpace(c.MCP.Path) == "" {
return fmt.Errorf("invalid config: mcp.path is required")
}
if c.MCP.SessionTimeout <= 0 {
return fmt.Errorf("invalid config: mcp.session_timeout must be greater than zero")
}
switch c.AI.Provider {
case "litellm", "ollama", "openrouter":

View File

@@ -1,11 +1,14 @@
package config
import "testing"
import (
"testing"
"time"
)
func validConfig() Config {
return Config{
Server: ServerConfig{Port: 8080},
MCP: MCPConfig{Path: "/mcp"},
MCP: MCPConfig{Path: "/mcp", SessionTimeout: 10 * time.Minute},
Auth: AuthConfig{
Keys: []APIKey{{ID: "test", Value: "secret"}},
},
@@ -121,3 +124,12 @@ func TestValidateRejectsInvalidMetadataRetryConfig(t *testing.T) {
t.Fatal("Validate() error = nil, want error for invalid metadata retry config")
}
}
func TestValidateRejectsInvalidMCPSessionTimeout(t *testing.T) {
cfg := validConfig()
cfg.MCP.SessionTimeout = 0
if err := cfg.Validate(); err == nil {
t.Fatal("Validate() error = nil, want error for invalid mcp session timeout")
}
}