Files
archie/internal/tools/provider/mcp/provider_test.go
T
sam d2504d8023 chore(plugin): delete the unwired capability-host health surface (#882)
* chore(plugin): delete the unwired capability-host health surface

plugin.Host.Health, plugin.Host.Manifests, ModuleStatus and their private
safeHealth helper were implemented, exhaustively tested, and called from nowhere
but their own tests. AGENTS.md forbids speculative surfaces, and no settled
design places plugin module status anywhere that exists:

- docs/prds/status-health-surface.md Phase 2 is self-described as "a proposed
  resolution, awaiting Sam's sign-off -- not yet implemented", and does not
  mention plugin status even in that unapproved list.
- The readiness subsystem enumerates exactly five probes -- state_db, config,
  disk, model, gateway (internal/app/archied/readiness.go) -- with no plugin
  probe.
- docs/architecture/plugins-and-extensions.md names no operator-visible plugin
  status surface.
- The only bead naming it is closed.

plugin.Host itself stays: it is the daemon's live capability host, constructed
and Start/Stop-ed from internal/app/archied. Only the unconsumed observability
methods go.

host_contract_test.go encoded the defect -- it asserted plugin.Host *defines*
"Health" and "Manifests" -- so those two names are removed from its list, which
is now exactly the shipped lifecycle: Register, Start, Stop. The three tests
covering the deleted surface are removed with it.

The Yaegi symbol table (internal/plugin/pluginextract) was regenerated rather
than hand-edited, so ModuleStatus leaves the reflected type set with it.

Reachability evidence: Host.Health and Host.Manifests appear only in their own
tests; plugin.Host is otherwise reached from bootstrap.go and gateway.go for
Register/Start/Stop. Note that "task reachability" does NOT flag these -- the
address-taken constructor is a known blind spot of the whole-program RTA.

* chore(plugin): delete the dead Module.Health chain wired to nothing

Extends the capability-host observability deletion: plugin.Module.Health
was a required interface method with real implementations in
tools/provider/mcp and tools/provider/builtin, aggregated by
tools/provider.Registry.Health via a provider-package-local safeHealth,
and backed by per-module LifecycleState bookkeeping in plugin.Host
(registeredModule.state, setModuleState). None of it had a caller before
this change -- Host.Health was the only thing that would ever have read
a registered module's Health(), and Host.Health was already deleted as
unreachable. registeredModule.state was write-only: nothing ever read it.

Deleted:
- plugin.Module.Health, plugin.Health/HealthStatus (+ consts),
  plugin.LifecycleState (+ consts), legacyModule.Health,
  registeredModule.state, Host.setModuleState and its call sites.
- tools/provider.Registry.Health, tools/provider.safeHealth.
- tools/provider/mcp.Provider.Health, tools/provider/builtin.Provider.Health.
- Tests exclusively covering the above, and fakes' now-unnecessary Health
  methods (plugin/host_test.go, pluginextract/reachability_test.go,
  tools/provider/{provider,optional}_test.go, tools/provider/mcp/provider_test.go,
  app/agentworker/mcp_providers_test.go).

internal/plugin/pluginextract's generated file was regenerated via
`go generate ./internal/plugin/...` and the yaegi wrapper nil-guards
(dropped by fresh generation, as documented in reachability_test.go)
were reapplied by hand to WManifest/WStart/WStop/WName/WVersion,
following this repo's established post-regen pattern.
2026-09-19 14:10:39 +10:00

407 lines
11 KiB
Go

package mcp
import (
"context"
"encoding/json"
"errors"
"os"
"strings"
"sync"
"testing"
"github.com/samcharles93/archie-core/internal/tools"
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
)
func TestProviderInitializesDiscoversAndCallsOriginalMCPTool(t *testing.T) {
transport := newFakeTransport()
transport.listed = []protocol.ToolSchema{{
Name: "Search Repos!",
Description: "Search repositories",
InputSchema: tools.JSONSchema{"type": "object"},
}}
provider := New("Git Hub/Prod", transport)
manifest := provider.Manifest()
if manifest.ID != "mcp.git-hub-prod" {
t.Fatalf("Manifest().ID = %q, want mcp.git-hub-prod", manifest.ID)
}
if err := manifest.Validate(); err != nil {
t.Fatalf("Manifest().Validate() = %v", err)
}
if err := provider.Start(context.Background()); err != nil {
t.Fatal(err)
}
if got := transport.notifications(); len(got) != 1 || got[0] != "notifications/initialized" {
t.Fatalf("notifications = %v, want initialized", got)
}
entries, err := provider.Discover(context.Background())
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Fatalf("Discover() count = %d, want 1", len(entries))
}
entry := entries[0]
if entry.Name != "mcp.git_hub_prod.search_repos" {
t.Fatalf("tool name = %q, want sanitized mapped name", entry.Name)
}
if entry.Toolset != "mcp" || entry.Description != transport.listed[0].Description {
t.Fatalf("tool metadata = %+v", entry)
}
got, err := entry.Handler(context.Background(), map[string]any{"query": "archie"})
if err != nil {
t.Fatal(err)
}
if got != "firstsecond" {
t.Fatalf("handler result = %v, want concatenated MCP text", got)
}
name, args := transport.lastCall()
if name != "Search Repos!" || args["query"] != "archie" {
t.Fatalf("MCP call = %q %#v, want original name and args", name, args)
}
if err := provider.Stop(context.Background()); err != nil {
t.Fatal(err)
}
if transport.State() != protocol.StateStopped {
t.Fatalf("transport state = %s, want stopped", transport.State())
}
}
func TestProviderSurfacesMCPToolLevelErrors(t *testing.T) {
transport := newFakeTransport()
transport.listed = []protocol.ToolSchema{{Name: "fails"}}
transport.callResult = protocol.CallToolResult{
Content: []protocol.ContentBlock{{Type: "text", Text: "denied"}},
IsError: true,
}
provider := New("errors", transport)
if err := provider.Start(context.Background()); err != nil {
t.Fatal(err)
}
entries, err := provider.Discover(context.Background())
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Fatalf("Discover() count = %d, want 1", len(entries))
}
if _, err := entries[0].Handler(context.Background(), nil); err == nil ||
!strings.Contains(err.Error(), "denied") {
t.Fatalf("handler error = %v, want MCP tool failure content", err)
}
}
func TestProviderPreservesResourceTextAndMultimodalResults(t *testing.T) {
transport := newFakeTransport()
transport.listed = []protocol.ToolSchema{{Name: "capture"}}
transport.callResult = protocol.CallToolResult{Content: []protocol.ContentBlock{
{Type: "text", Text: "captured"},
{Type: "resource", Resource: &protocol.ResourceContent{Text: " resource context"}},
{Type: "image", Data: "aW1hZ2U=", MimeType: "image/png"},
}}
provider := New("media", transport)
if err := provider.Start(context.Background()); err != nil {
t.Fatal(err)
}
entries, err := provider.Discover(context.Background())
if err != nil {
t.Fatal(err)
}
output, err := entries[0].Handler(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
result, ok := output.(tools.MultimodalResult)
if !ok {
t.Fatalf("handler result type = %T, want tools.MultimodalResult", output)
}
if !result.IsMultimodal || result.Summary != "captured resource context" {
t.Fatalf("multimodal result = %+v", result)
}
}
func TestProviderWritesBinaryContentToDeliverableMediaRefs(t *testing.T) {
transport := newFakeTransport()
transport.listed = []protocol.ToolSchema{{Name: "media"}}
transport.callResult = protocol.CallToolResult{Content: []protocol.ContentBlock{
{Type: "text", Text: "summary "},
{Type: "image", Data: "aW1hZ2U=", MimeType: "image/png"},
{Type: "audio", Data: "YXVkaW8=", MimeType: "audio/wav"},
{Type: "resource", Resource: &protocol.ResourceContent{MimeType: "application/pdf", Blob: "cGRm"}},
}}
provider := New("media", transport)
if err := provider.Start(context.Background()); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = provider.Stop(context.Background()) })
entries, err := provider.Discover(context.Background())
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 {
t.Fatalf("Discover() count = %d, want 1", len(entries))
}
output, err := entries[0].Handler(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
result, ok := output.(tools.MultimodalResult)
if !ok {
t.Fatalf("handler result type = %T, want tools.MultimodalResult", output)
}
if !result.IsMultimodal {
t.Error("IsMultimodal = false, want true")
}
if result.Summary != "summary " {
t.Errorf("Summary = %q, want %q", result.Summary, "summary ")
}
if result.SubdirHint == "" {
t.Fatal("SubdirHint is empty, want the written media directory")
}
if len(result.Files) != 3 {
t.Fatalf("Files = %v, want 3 written files", result.Files)
}
if len(result.URLs) != 3 {
t.Fatalf("URLs = %v, want 3 deliverable media refs", result.URLs)
}
want := []struct {
bytes []byte
typ string
}{
{[]byte("image"), "image"},
{[]byte("audio"), "audio"},
{[]byte("pdf"), "document"},
}
for i, ref := range result.URLs {
if ref.Path == "" {
t.Fatalf("URLs[%d].Path is empty, want a local file for delivery", i)
}
if ref.Type != want[i].typ {
t.Errorf("URLs[%d].Type = %q, want %q", i, ref.Type, want[i].typ)
}
got, err := os.ReadFile(ref.Path)
if err != nil {
t.Fatalf("URLs[%d].Path not readable: %v", i, err)
}
if string(got) != string(want[i].bytes) {
t.Errorf("URLs[%d] bytes = %q, want %q", i, got, want[i].bytes)
}
}
}
func TestProviderStartFailureStopsTransport(t *testing.T) {
tests := []struct {
name string
startErr error
sendErr error
wantStops int
}{
{name: "transport start", startErr: errors.New("start failed")},
{name: "initialize", sendErr: errors.New("initialize failed"), wantStops: 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
transport := newFakeTransport()
transport.startErr = tt.startErr
transport.sendErr = tt.sendErr
provider := New("failure", transport)
if err := provider.Start(context.Background()); err == nil {
t.Fatal("Start() succeeded")
}
if transport.stopCount != tt.wantStops {
t.Fatalf("transport Stop() calls = %d, want %d", transport.stopCount, tt.wantStops)
}
})
}
}
func TestProviderRejectsInvalidConfiguration(t *testing.T) {
tests := []struct {
name string
server string
transport LifecycleTransport
}{
{name: "empty name", transport: newFakeTransport()},
{name: "nil transport", server: "server"},
{name: "typed nil transport", server: "server", transport: (*fakeTransport)(nil)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
provider := New(tt.server, tt.transport)
if err := provider.Start(context.Background()); err == nil {
t.Fatal("Start() succeeded")
}
})
}
}
func TestProviderSatisfiesEngine(t *testing.T) {
var _ toolprovider.Engine = New("compile", newFakeTransport())
}
// Compile-time checks: HTTP and SSE transports satisfy LifecycleTransport.
func TestTransportsSatisfyLifecycleTransport(t *testing.T) {
var _ LifecycleTransport = (*protocol.HTTPTransport)(nil)
var _ LifecycleTransport = (*protocol.SSETransport)(nil)
// Also check StdioTransport, which already satisfied it.
var _ LifecycleTransport = (*protocol.StdioTransport)(nil)
}
func TestSanitizeSegmentsAreDeterministicASCII(t *testing.T) {
tests := []struct {
name string
input string
manifest string
tool string
}{
{name: "spaces symbols and unicode", input: " 42 Répo/搜索 ", manifest: "x-42-r-po", tool: "x_42_r_po"},
{name: "separator runs", input: "A...B", manifest: "a-b", tool: "a_b"},
{name: "no identifier content", input: "---", manifest: "", tool: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sanitizeManifestSegment(tt.input); got != tt.manifest {
t.Fatalf("sanitizeManifestSegment(%q) = %q, want %q", tt.input, got, tt.manifest)
}
if got := sanitizeToolSegment(tt.input); got != tt.tool {
t.Fatalf("sanitizeToolSegment(%q) = %q, want %q", tt.input, got, tt.tool)
}
})
}
}
type fakeTransport struct {
mu sync.Mutex
state protocol.TransportState
startErr error
sendErr error
stopErr error
stopCount int
listed []protocol.ToolSchema
callResult protocol.CallToolResult
notifyMethods []string
calledName string
calledArgs map[string]any
}
func newFakeTransport() *fakeTransport {
return &fakeTransport{
state: protocol.StateStopped,
callResult: protocol.CallToolResult{
Content: []protocol.ContentBlock{
{Type: "text", Text: "first"},
{Type: "text", Text: "second"},
},
},
}
}
func (t *fakeTransport) Start(context.Context) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.startErr != nil {
return t.startErr
}
t.state = protocol.StateRunning
return nil
}
func (t *fakeTransport) Stop(context.Context) error {
t.mu.Lock()
defer t.mu.Unlock()
t.stopCount++
t.state = protocol.StateStopped
return t.stopErr
}
func (t *fakeTransport) State() protocol.TransportState {
t.mu.Lock()
defer t.mu.Unlock()
return t.state
}
func (t *fakeTransport) Send(_ context.Context, body []byte) ([]byte, error) {
t.mu.Lock()
defer t.mu.Unlock()
if t.sendErr != nil {
return nil, t.sendErr
}
var request struct {
ID json.RawMessage `json:"id"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
}
if err := json.Unmarshal(body, &request); err != nil {
return nil, err
}
var result any
switch request.Method {
case "initialize":
result = protocol.InitializeResult{
ProtocolVersion: "2024-11-05",
ServerInfo: protocol.ServerInfo{Name: "fake", Version: "1.0.0"},
Capabilities: protocol.Capabilities{Tools: json.RawMessage(`{}`)},
}
case "tools/list":
result = map[string]any{"tools": t.listed}
case "tools/call":
var params struct {
Name string `json:"name"`
Arguments map[string]any `json:"arguments"`
}
if err := json.Unmarshal(request.Params, &params); err != nil {
return nil, err
}
t.calledName = params.Name
t.calledArgs = params.Arguments
result = t.callResult
default:
return nil, errors.New("unexpected method: " + request.Method)
}
resultJSON, err := json.Marshal(result)
if err != nil {
return nil, err
}
return json.Marshal(protocol.Message{
JSONRPC: "2.0",
ID: request.ID,
Result: resultJSON,
})
}
func (t *fakeTransport) Notify(_ context.Context, body []byte) error {
t.mu.Lock()
defer t.mu.Unlock()
var notification struct {
Method string `json:"method"`
}
if err := json.Unmarshal(body, &notification); err != nil {
return err
}
t.notifyMethods = append(t.notifyMethods, notification.Method)
return nil
}
func (t *fakeTransport) notifications() []string {
t.mu.Lock()
defer t.mu.Unlock()
return append([]string(nil), t.notifyMethods...)
}
func (t *fakeTransport) lastCall() (string, map[string]any) {
t.mu.Lock()
defer t.mu.Unlock()
return t.calledName, t.calledArgs
}