|
| 1 | +package rpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + opaqueMCPHandle = "opaque:mcp/01-do-not-parse" |
| 10 | + opaqueSkillHandle = "opaque:skill/02-do-not-parse" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCatalogSearchResultPreservesCandidateSemantics(t *testing.T) { |
| 14 | + result, err := unmarshalCatalogSearchResult([]byte(`{ |
| 15 | + "kind":"succeeded", |
| 16 | + "searchId":"search-01", |
| 17 | + "candidates":[ |
| 18 | + { |
| 19 | + "kind":"mcp-server", |
| 20 | + "handle":"opaque:mcp/01-do-not-parse", |
| 21 | + "handleExpiresAt":"2026-09-02T12:00:00Z", |
| 22 | + "mediaType":"application/mcp-server-card+json", |
| 23 | + "installability":"installable", |
| 24 | + "displayName":"Example MCP", |
| 25 | + "rawCard":{"secret":"must-not-survive"}, |
| 26 | + "source":{"kind":"url","url":"https://catalog.example/mcp.json"}, |
| 27 | + "provenance":{ |
| 28 | + "authority":"catalog.example", |
| 29 | + "observedAt":"2026-09-02T11:00:00Z", |
| 30 | + "mediaType":"application/mcp-server-card+json" |
| 31 | + } |
| 32 | + }, |
| 33 | + { |
| 34 | + "kind":"ai-skill", |
| 35 | + "handle":"opaque:skill/02-do-not-parse", |
| 36 | + "handleExpiresAt":"2026-09-02T12:00:00Z", |
| 37 | + "mediaType":"application/ai-skill", |
| 38 | + "installability":"not-installable-kind", |
| 39 | + "displayName":"Example skill", |
| 40 | + "rawCard":{"secret":"must-not-survive"}, |
| 41 | + "source":{"kind":"embedded"}, |
| 42 | + "provenance":{ |
| 43 | + "authority":"catalog.example", |
| 44 | + "observedAt":"2026-09-02T11:00:00Z", |
| 45 | + "mediaType":"application/ai-skill" |
| 46 | + } |
| 47 | + } |
| 48 | + ], |
| 49 | + "truncated":false, |
| 50 | + "negotiated":{ |
| 51 | + "runtimeProtocolVersion":1, |
| 52 | + "grantedCapabilities":["mcp-server-card","ai-skill-discovery"] |
| 53 | + } |
| 54 | + }`)) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("unmarshal catalogue success: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + success, ok := result.(*CatalogSearchSucceeded) |
| 60 | + if !ok { |
| 61 | + t.Fatalf("catalogue result = %T, want *CatalogSearchSucceeded", result) |
| 62 | + } |
| 63 | + mcp, ok := success.Candidates[0].(*CatalogMCPServerCandidate) |
| 64 | + if !ok { |
| 65 | + t.Fatalf("first candidate = %T, want *CatalogMCPServerCandidate", success.Candidates[0]) |
| 66 | + } |
| 67 | + skill, ok := success.Candidates[1].(*CatalogAiSkillCandidate) |
| 68 | + if !ok { |
| 69 | + t.Fatalf("second candidate = %T, want *CatalogAiSkillCandidate", success.Candidates[1]) |
| 70 | + } |
| 71 | + if mcp.Handle != opaqueMCPHandle || skill.Handle != opaqueSkillHandle { |
| 72 | + t.Fatalf("opaque handles changed: %q, %q", mcp.Handle, skill.Handle) |
| 73 | + } |
| 74 | + if _, ok := mcp.Source.(*CatalogCandidateSourceURL); !ok { |
| 75 | + t.Fatalf("MCP source = %T, want *CatalogCandidateSourceURL", mcp.Source) |
| 76 | + } |
| 77 | + if _, ok := skill.Source.(*CatalogCandidateSourceEmbedded); !ok { |
| 78 | + t.Fatalf("skill source = %T, want *CatalogCandidateSourceEmbedded", skill.Source) |
| 79 | + } |
| 80 | + |
| 81 | + encoded, err := json.Marshal(success) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("marshal catalogue success: %v", err) |
| 84 | + } |
| 85 | + var wire map[string]any |
| 86 | + if err := json.Unmarshal(encoded, &wire); err != nil { |
| 87 | + t.Fatalf("decode catalogue wire result: %v", err) |
| 88 | + } |
| 89 | + for _, candidate := range wire["candidates"].([]any) { |
| 90 | + fields := candidate.(map[string]any) |
| 91 | + for _, forbidden := range []string{"card", "cardData", "rawCard"} { |
| 92 | + if _, exists := fields[forbidden]; exists { |
| 93 | + t.Fatalf("candidate leaked %q: %s", forbidden, encoded) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestCatalogSearchResultPreservesRefusalsAndFailures(t *testing.T) { |
| 100 | + tests := []struct { |
| 101 | + name string |
| 102 | + payload string |
| 103 | + assert func(*testing.T, CatalogSearchResult) |
| 104 | + }{ |
| 105 | + { |
| 106 | + name: "authentication required", |
| 107 | + payload: `{"kind":"authentication-required","reason":"no-credential","message":"Sign in is required."}`, |
| 108 | + assert: func(t *testing.T, result CatalogSearchResult) { |
| 109 | + if _, ok := result.(*CatalogAuthenticationRequiredError); !ok { |
| 110 | + t.Fatalf("result = %T, want *CatalogAuthenticationRequiredError", result) |
| 111 | + } |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "network failure", |
| 116 | + payload: `{"kind":"network-failure","reason":"timeout","retryAfterSeconds":30,"message":"The catalogue timed out."}`, |
| 117 | + assert: func(t *testing.T, result CatalogSearchResult) { |
| 118 | + failure, ok := result.(*CatalogNetworkFailureError) |
| 119 | + if !ok { |
| 120 | + t.Fatalf("result = %T, want *CatalogNetworkFailureError", result) |
| 121 | + } |
| 122 | + if failure.RetryAfterSeconds == nil || *failure.RetryAfterSeconds != 30 { |
| 123 | + t.Fatalf("retryAfterSeconds = %v, want 30", failure.RetryAfterSeconds) |
| 124 | + } |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + for _, test := range tests { |
| 130 | + t.Run(test.name, func(t *testing.T) { |
| 131 | + result, err := unmarshalCatalogSearchResult([]byte(test.payload)) |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("unmarshal catalogue result: %v", err) |
| 134 | + } |
| 135 | + test.assert(t, result) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments