package archiegateway import ( "context" "encoding/json" "os" "path/filepath" "strings" "testing" "github.com/modelcontextprotocol/go-sdk/mcp" "github.com/samcharles93/archie-core/internal/tools" ) func TestTaskToolResults(t *testing.T) { for _, tt := range []struct { name, input string output any limit int badPath bool wantErr bool }{ {name: "text limit", input: `{"value":"valid"}`, output: strings.Repeat("a", 200), limit: 40}, {name: "schema enforced", input: `{"value":true}`, wantErr: true}, {name: "media", input: `{"value":"valid"}`}, {name: "media cannot escape", input: `{"value":"valid"}`, badPath: true, wantErr: true}, } { t.Run(tt.name, func(t *testing.T) { root := t.TempDir() file := filepath.Join(root, "image.png") if tt.badPath { file = filepath.Join(t.TempDir(), "outside.png") } if err := os.WriteFile(file, []byte("image-data"), 0o600); err != nil { t.Fatal(err) } output := tt.output if output == nil { output = tools.MultimodalResult{Summary: "image", SubdirHint: root, Files: []string{file}} } called := false entry := tools.ToolEntry{Name: "tool", Schema: tools.JSONSchema{"type": "object", "properties": map[string]any{"value": map[string]any{"type": "string"}}, "required": []string{"value"}}, Handler: func(context.Context, map[string]any) (any, error) { called = true; return output, nil }} result, err := executeTaskTool(t.Context(), entry, json.RawMessage(tt.input), tt.limit) if (err != nil) != tt.wantErr { t.Fatalf("error %v, want error %v", err, tt.wantErr) } if tt.name == "schema enforced" && called { t.Fatal("invalid arguments executed") } if err != nil { return } raw, err := json.Marshal(result) if err != nil { t.Fatal(err) } if strings.Contains(string(raw), root) { t.Fatal("host media path escaped") } if tt.name == "media" { image, ok := result.Content[1].(*mcp.ImageContent) if !ok || string(image.Data) != "image-data" { t.Fatalf("media result %s", raw) } } text, ok := result.Content[0].(*mcp.TextContent) if !ok { t.Fatal("missing text content") } if tt.limit > 0 && strings.Contains(text.Text, strings.Repeat("a", 200)) { t.Fatal("tool result limit was ignored") } }) } }