-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathui_capability_test.go
More file actions
132 lines (114 loc) · 3.7 KB
/
Copy pathui_capability_test.go
File metadata and controls
132 lines (114 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package github
import (
"context"
"testing"
ghcontext "github.com/github/github-mcp-server/pkg/context"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func createMCPRequestWithCapabilities(t *testing.T, caps *mcp.ClientCapabilities) mcp.CallToolRequest {
t.Helper()
srv := mcp.NewServer(&mcp.Implementation{Name: "test"}, nil)
st, _ := mcp.NewInMemoryTransports()
session, err := srv.Connect(context.Background(), st, &mcp.ServerSessionOptions{
State: &mcp.ServerSessionState{
InitializeParams: &mcp.InitializeParams{
ClientInfo: &mcp.Implementation{Name: "test-client"},
Capabilities: caps,
},
},
})
require.NoError(t, err)
t.Cleanup(func() { _ = session.Close() })
return mcp.CallToolRequest{Session: session}
}
func Test_clientSupportsUI(t *testing.T) {
t.Parallel()
ctx := context.Background()
t.Run("client with UI extension", func(t *testing.T) {
caps := &mcp.ClientCapabilities{}
caps.AddExtension("io.modelcontextprotocol/ui", map[string]any{
"mimeTypes": []string{"text/html;profile=mcp-app"},
})
req := createMCPRequestWithCapabilities(t, caps)
assert.True(t, clientSupportsUI(ctx, &req))
})
t.Run("client without UI extension", func(t *testing.T) {
req := createMCPRequestWithCapabilities(t, &mcp.ClientCapabilities{})
assert.False(t, clientSupportsUI(ctx, &req))
})
t.Run("client with nil capabilities", func(t *testing.T) {
req := createMCPRequestWithCapabilities(t, nil)
assert.False(t, clientSupportsUI(ctx, &req))
})
t.Run("nil request", func(t *testing.T) {
assert.False(t, clientSupportsUI(ctx, nil))
})
t.Run("nil session", func(t *testing.T) {
req := createMCPRequest(nil)
assert.False(t, clientSupportsUI(ctx, &req))
})
}
func Test_clientSupportsUI_fromContext(t *testing.T) {
t.Parallel()
t.Run("UI supported in context", func(t *testing.T) {
ctx := ghcontext.WithUISupport(context.Background(), true)
assert.True(t, clientSupportsUI(ctx, nil))
})
t.Run("UI not supported in context", func(t *testing.T) {
ctx := ghcontext.WithUISupport(context.Background(), false)
assert.False(t, clientSupportsUI(ctx, nil))
})
t.Run("context takes precedence over session", func(t *testing.T) {
ctx := ghcontext.WithUISupport(context.Background(), false)
caps := &mcp.ClientCapabilities{}
caps.AddExtension("io.modelcontextprotocol/ui", map[string]any{})
req := createMCPRequestWithCapabilities(t, caps)
assert.False(t, clientSupportsUI(ctx, &req))
})
t.Run("no context or session", func(t *testing.T) {
assert.False(t, clientSupportsUI(context.Background(), nil))
})
}
func Test_shouldDeferToForm_featureFlags(t *testing.T) {
t.Parallel()
ctx := ghcontext.WithUISupport(context.Background(), true)
args := map[string]any{"owner": "octocat"}
formParams := map[string]struct{}{"owner": {}}
tests := []struct {
name string
enabledFlags []string
want bool
}{
{
name: "MCP Apps enabled defers to form",
enabledFlags: []string{MCPAppsFeatureFlag},
want: true,
},
{
name: "form deferral disabled executes directly",
enabledFlags: []string{
MCPAppsFeatureFlag,
MCPAppsDisableFormDeferralFeatureFlag,
},
want: false,
},
{
name: "form deferral opt-out does not enable MCP Apps",
enabledFlags: []string{MCPAppsDisableFormDeferralFeatureFlag},
want: false,
},
{
name: "MCP Apps disabled executes directly",
want: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
deps := BaseDeps{featureChecker: featureCheckerFor(tc.enabledFlags...)}
assert.Equal(t, tc.want, shouldDeferToForm(ctx, deps, nil, args, formParams))
})
}
}