|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +// Package integration_test contains integration tests that require a real LLM. |
| 4 | +// Build with -tags integration to include them. |
| 5 | +package integration_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "flag" |
| 11 | + "fmt" |
| 12 | + "strings" |
| 13 | + "sync" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/nanobot-ai/nanobot/pkg/agents" |
| 17 | + "github.com/nanobot-ai/nanobot/pkg/config" |
| 18 | + "github.com/nanobot-ai/nanobot/pkg/llm" |
| 19 | + "github.com/nanobot-ai/nanobot/pkg/mcp" |
| 20 | + "github.com/nanobot-ai/nanobot/pkg/runtime" |
| 21 | + "github.com/nanobot-ai/nanobot/pkg/servers/system" |
| 22 | + "github.com/nanobot-ai/nanobot/pkg/types" |
| 23 | +) |
| 24 | + |
| 25 | +var numRuns = flag.Int("runs", 5, "number of times to run each prompt") |
| 26 | + |
| 27 | +// ToolHandler is a function that handles a mocked tool call and returns a result. |
| 28 | +type ToolHandler func(mcp.CallToolRequest) *mcp.CallToolResult |
| 29 | + |
| 30 | +// toolCallRecorder wraps an MCP handler and records every tools/call invocation. |
| 31 | +// By default, "config" and "getSkill" pass through to the real server; all other |
| 32 | +// tools return an error unless a custom ToolHandler is registered. |
| 33 | +type toolCallRecorder struct { |
| 34 | + mu sync.Mutex |
| 35 | + calls []mcp.CallToolRequest |
| 36 | + handlers map[string]ToolHandler |
| 37 | +} |
| 38 | + |
| 39 | +func newRecorder(handlers map[string]ToolHandler) *toolCallRecorder { |
| 40 | + return &toolCallRecorder{handlers: handlers} |
| 41 | +} |
| 42 | + |
| 43 | +func (r *toolCallRecorder) wrap(inner mcp.MessageHandler) mcp.MessageHandler { |
| 44 | + return mcp.MessageHandlerFunc(func(ctx context.Context, msg mcp.Message) { |
| 45 | + if msg.Method != "tools/call" { |
| 46 | + inner.OnMessage(ctx, msg) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + var call mcp.CallToolRequest |
| 51 | + if err := json.Unmarshal(msg.Params, &call); err != nil { |
| 52 | + msg.SendError(ctx, mcp.ErrRPCInvalidParams.WithMessage("unmarshal tools/call params: %v", err)) |
| 53 | + return |
| 54 | + } |
| 55 | + r.mu.Lock() |
| 56 | + r.calls = append(r.calls, call) |
| 57 | + r.mu.Unlock() |
| 58 | + |
| 59 | + switch call.Name { |
| 60 | + case "config", "getSkill": |
| 61 | + // Always pass through: config drives agent setup, getSkill loads skills. |
| 62 | + inner.OnMessage(ctx, msg) |
| 63 | + default: |
| 64 | + if handler, ok := r.handlers[call.Name]; ok { |
| 65 | + mcp.Invoke(ctx, msg, func(_ context.Context, _ mcp.Message, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 66 | + return handler(req), nil |
| 67 | + }) |
| 68 | + } else { |
| 69 | + mcp.Invoke(ctx, msg, func(_ context.Context, _ mcp.Message, _ mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 70 | + return &mcp.CallToolResult{ |
| 71 | + IsError: true, |
| 72 | + Content: []mcp.Content{{Type: "text", Text: fmt.Sprintf("tool %q is not available in the test environment", call.Name)}}, |
| 73 | + }, nil |
| 74 | + }) |
| 75 | + } |
| 76 | + } |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func (r *toolCallRecorder) find(name string) *mcp.CallToolRequest { |
| 81 | + r.mu.Lock() |
| 82 | + defer r.mu.Unlock() |
| 83 | + for i := range r.calls { |
| 84 | + if r.calls[i].Name == name { |
| 85 | + c := r.calls[i] |
| 86 | + return &c |
| 87 | + } |
| 88 | + } |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// summary returns a multi-line description of all recorded tool calls with their |
| 93 | +// arguments, for use in test failure output. |
| 94 | +func (r *toolCallRecorder) summary() string { |
| 95 | + r.mu.Lock() |
| 96 | + defer r.mu.Unlock() |
| 97 | + if len(r.calls) == 0 { |
| 98 | + return " (no tool calls recorded)" |
| 99 | + } |
| 100 | + var sb strings.Builder |
| 101 | + for i, c := range r.calls { |
| 102 | + args, _ := json.MarshalIndent(c.Arguments, " ", " ") |
| 103 | + fmt.Fprintf(&sb, " [%d] %s\n args: %s\n", i+1, c.Name, args) |
| 104 | + } |
| 105 | + return sb.String() |
| 106 | +} |
| 107 | + |
| 108 | +// stubMCPHandler returns a minimal, no-tool MCP server used as a placeholder for |
| 109 | +// servers that aren't needed in the test but are referenced by the config hook. |
| 110 | +func stubMCPHandler() mcp.MessageHandler { |
| 111 | + stubTools := mcp.NewServerTools() |
| 112 | + return mcp.MessageHandlerFunc(func(ctx context.Context, msg mcp.Message) { |
| 113 | + switch msg.Method { |
| 114 | + case "initialize": |
| 115 | + mcp.Invoke(ctx, msg, func(ctx context.Context, _ mcp.Message, params mcp.InitializeRequest) (*mcp.InitializeResult, error) { |
| 116 | + return &mcp.InitializeResult{ |
| 117 | + ProtocolVersion: params.ProtocolVersion, |
| 118 | + Capabilities: mcp.ServerCapabilities{Tools: &mcp.ToolsServerCapability{}}, |
| 119 | + ServerInfo: mcp.ServerInfo{Name: "stub", Version: "0"}, |
| 120 | + }, nil |
| 121 | + }) |
| 122 | + case "notifications/initialized", "notifications/cancelled": |
| 123 | + case "tools/list": |
| 124 | + mcp.Invoke(ctx, msg, stubTools.List) |
| 125 | + case "tools/call": |
| 126 | + mcp.Invoke(ctx, msg, stubTools.Call) |
| 127 | + default: |
| 128 | + msg.SendError(ctx, mcp.ErrRPCMethodNotFound.WithMessage("%s", msg.Method)) |
| 129 | + } |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +// newTestRuntime creates a Runtime and installs a toolCallRecorder on nanobot.system. |
| 134 | +// The agent has no pre-loaded instructions; it discovers the workflows skill naturally |
| 135 | +// via the getSkill tool that the config hook injects. |
| 136 | +func newTestRuntime(t *testing.T, completer types.Completer, recorder *toolCallRecorder) (context.Context, *agents.Agents) { |
| 137 | + t.Helper() |
| 138 | + |
| 139 | + rt, err := runtime.NewRuntime(context.Background(), llm.Config{}) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("NewRuntime failed: %v", err) |
| 142 | + } |
| 143 | + |
| 144 | + // Wrap the real system server so we can record and intercept tool calls. |
| 145 | + rt.AddServer("nanobot.system", func(string) mcp.MessageHandler { |
| 146 | + return recorder.wrap(system.NewServer("", "")) |
| 147 | + }) |
| 148 | + // nanobot.tasks is only registered when LoopbackURL+Store are set; stub it so |
| 149 | + // the config hook doesn't fail when it adds the server to MCPServers. |
| 150 | + rt.AddServer("nanobot.tasks", func(string) mcp.MessageHandler { return stubMCPHandler() }) |
| 151 | + |
| 152 | + // Load the production config (builtin nanobot agent) from the standard location. |
| 153 | + // config.Load handles a missing .nanobot/ directory gracefully. |
| 154 | + cfg, _, err := config.Load(context.Background(), ".nanobot/", true) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("config.Load failed: %v", err) |
| 157 | + } |
| 158 | + |
| 159 | + agentSvc := agents.New(completer, rt.Service) |
| 160 | + ctx := rt.WithTempSession(context.Background(), cfg) |
| 161 | + return ctx, agentSvc |
| 162 | +} |
| 163 | + |
| 164 | +func runAgent(ctx context.Context, svc *agents.Agents, prompt string) error { |
| 165 | + _, err := svc.Complete(ctx, types.CompletionRequest{ |
| 166 | + Agent: "nanobot", |
| 167 | + Input: []types.Message{{ |
| 168 | + Role: "user", |
| 169 | + Items: []types.CompletionItem{{ |
| 170 | + Content: &mcp.Content{Type: "text", Text: prompt}, |
| 171 | + }}, |
| 172 | + }}, |
| 173 | + }) |
| 174 | + return err |
| 175 | +} |
0 commit comments