Files
forge-toolkit/result_probe_test.go
T
Maksim Totmin c398045664
ci / test (push) Failing after 5s
ci / lint (push) Failing after 4s
chore: initial release v0.1.0
forge-toolkit: хелперы для разработки MCP-серверов на Go.

- toolkit: args/schema/result/env/probe/server
- toolkit/validate: валидаторы синтаксиса
- configreload: live-reload конфига по контент-хэшу
- docs: ARCHITECTURE, quickstart, mcp-contract
- template/ и examples/hello-tool/
- CI (Gitea Actions), лицензия Apache-2.0
2026-09-14 00:23:08 +07:00

76 lines
1.9 KiB
Go

package toolkit
import (
"context"
"testing"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func TestTextAndError(t *testing.T) {
txt := Text("hello")
if txt.IsError {
t.Error("Text must not be an error")
}
if len(txt.Content) != 1 {
t.Fatalf("content = %v", txt.Content)
}
errRes := Error("boom")
if !errRes.IsError {
t.Error("Error must set IsError")
}
}
func TestIsProbe(t *testing.T) {
probe := &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{
Meta: mcp.Meta{metaProbeKey: true},
}}
if !IsProbe(probe) {
t.Error("IsProbe = false, want true")
}
if IsProbe(nil) {
t.Error("nil request must not be probe")
}
if IsProbe(&mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{}}) {
t.Error("no meta must not be probe")
}
}
func TestWrapProbeInterceptsAndDelegates(t *testing.T) {
handlerCalled := false
h := func(context.Context, *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
handlerCalled = true
return Text("real"), nil
}
fn := func(args map[string]any) ([]string, []string) {
return []string{"run:cat *"}, []string{"cat *"}
}
wrapped := WrapProbe(h, fn)
// Probe: действие не выполняется, возвращаются паттерны в _meta.
probe := &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{
Meta: mcp.Meta{metaProbeKey: true},
Arguments: []byte(`{"command":"cat /etc/hosts"}`),
}}
res, err := wrapped(context.Background(), probe)
if err != nil {
t.Fatalf("probe: %v", err)
}
if handlerCalled {
t.Error("handler must not run on probe")
}
meta := res.GetMeta()
if meta[metaPatternsKey] == nil || meta[metaAlwaysKey] == nil {
t.Errorf("probe meta = %v", meta)
}
// Обычный вызов: делегируется handler.
normal := &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{}}
if _, err := wrapped(context.Background(), normal); err != nil {
t.Fatalf("normal: %v", err)
}
if !handlerCalled {
t.Error("handler must run on normal call")
}
}