| 1 | package tool |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestMCPAppSanitizedDropsOversizedNestedImageAndPreservesResultFields(t *testing.T) { |
| 10 | raw := json.RawMessage(`{ |
| 11 | "content":[ |
| 12 | {"type":"image","data":"` + strings.Repeat("A", 5000) + `","mimeType":"image/png"}, |
| 13 | {"type":"resource","resource":{"blob":"` + strings.Repeat("B", 5000) + `"}}, |
| 14 | {"type":"text","text":"kept"} |
| 15 | ], |
| 16 | "structuredContent":{"answer":42}, |
| 17 | "isError":false, |
| 18 | "_meta":{"trace":"local"} |
| 19 | }`) |
| 20 | got := (&MCPAppResult{Server: "srv", Tool: "show", RawResult: raw}).Sanitized() |
| 21 | if got == nil { |
| 22 | t.Fatal("sanitized result unexpectedly nil") |
| 23 | } |
| 24 | text := string(got.RawResult) |
| 25 | for _, unwanted := range []string{strings.Repeat("A", 100), strings.Repeat("B", 100)} { |
| 26 | if strings.Contains(text, unwanted) { |
| 27 | t.Fatal("oversized inline data survived sanitization") |
| 28 | } |
| 29 | } |
| 30 | for _, wanted := range []string{`"text":"kept"`, `"structuredContent":{"answer":42}`, `"isError":false`, `"trace":"local"`} { |
| 31 | if !strings.Contains(text, wanted) { |
| 32 | t.Fatalf("result field %s was lost: %s", wanted, text) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestMCPAppSanitizedEnforcesOneAggregateBudget(t *testing.T) { |
| 38 | rawPayload, err := json.Marshal(map[string]any{ |
| 39 | "content": []any{map[string]any{"type": "text", "text": strings.Repeat("r", 400<<10)}}, |
| 40 | }) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | structured, err := json.Marshal(map[string]any{"value": strings.Repeat("s", 400<<10)}) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | got := (&MCPAppResult{ |
| 49 | Server: "srv", Tool: "show", ResourceURI: "ui://app/main.html", |
| 50 | RawResult: rawPayload, Structured: structured, |
| 51 | CSP: map[string][]string{"connect-src": {"https://api.example.test"}}, |
| 52 | }).Sanitized() |
| 53 | if got == nil { |
| 54 | t.Fatal("bounded result unexpectedly nil") |
| 55 | } |
| 56 | if size := mcpAppPersistedSize(got); size > maxMCPAppBytes { |
| 57 | t.Fatalf("persisted presentation = %d bytes, limit = %d", size, maxMCPAppBytes) |
| 58 | } |
| 59 | if len(got.RawResult) == 0 || len(got.Structured) != 0 { |
| 60 | t.Fatalf("expected raw result to win over duplicate structured copy: raw=%d structured=%d", len(got.RawResult), len(got.Structured)) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestMCPAppSanitizedKeepsStructuredFallbackWhenRawIsTooLarge(t *testing.T) { |
| 65 | rawPayload, err := json.Marshal(map[string]any{ |
| 66 | "content": []any{map[string]any{"type": "text", "text": strings.Repeat("r", 600<<10)}}, |
| 67 | }) |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | structured := json.RawMessage(`{"answer":42}`) |
| 72 | got := (&MCPAppResult{Server: "srv", Tool: "show", RawResult: rawPayload, Structured: structured}).Sanitized() |
| 73 | if got == nil || len(got.RawResult) != 0 || string(got.Structured) != string(structured) { |
| 74 | t.Fatalf("structured fallback not retained: %+v", got) |
| 75 | } |
| 76 | } |
| 77 |