-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_servers_test.go
More file actions
134 lines (118 loc) · 3.52 KB
/
Copy pathmcp_servers_test.go
File metadata and controls
134 lines (118 loc) · 3.52 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
133
134
package flashduty
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestCreateMCPServer_HappyPath(t *testing.T) {
t.Parallel()
var captured CreateMCPServerInput
var capturedMethod string
var capturedPath string
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedMethod = r.Method
capturedPath = r.URL.Path
if err := json.NewDecoder(r.Body).Decode(&captured); err != nil {
t.Fatalf("decode request body: %v", err)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
"server_id": "mcp_abc123",
"status": "enabled",
},
})
}))
defer ts.Close()
client, err := NewClient("test-key", WithBaseURL(ts.URL))
if err != nil {
t.Fatalf("NewClient: %v", err)
}
out, err := client.CreateMCPServer(context.Background(), &CreateMCPServerInput{
ServerName: "prom-prod",
Description: "Prometheus prod MCP",
Transport: "sse",
URL: "https://prom.example/mcp",
Headers: map[string]string{"Authorization": "Bearer secret"},
TeamID: 0,
})
if err != nil {
t.Fatalf("CreateMCPServer error: %v", err)
}
if capturedMethod != http.MethodPost {
t.Errorf("HTTP method = %q, want POST", capturedMethod)
}
if capturedPath != "/safari/mcp/server/create" {
t.Errorf("path = %q, want /safari/mcp/server/create", capturedPath)
}
if captured.ServerName != "prom-prod" {
t.Errorf("server_name = %q, want prom-prod", captured.ServerName)
}
if captured.Transport != "sse" {
t.Errorf("transport = %q, want sse", captured.Transport)
}
if captured.TeamID != 0 {
t.Errorf("team_id = %d, want 0", captured.TeamID)
}
if out.ServerID != "mcp_abc123" {
t.Errorf("server_id = %q, want mcp_abc123", out.ServerID)
}
if out.Status != "enabled" {
t.Errorf("status = %q, want enabled", out.Status)
}
}
func TestCreateMCPServer_APIError(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"error": map[string]any{
"code": "InvalidArgument",
"message": "transport must be one of stdio, sse, streamable-http",
},
})
}))
defer ts.Close()
client, err := NewClient("test-key", WithBaseURL(ts.URL))
if err != nil {
t.Fatalf("NewClient: %v", err)
}
_, err = client.CreateMCPServer(context.Background(), &CreateMCPServerInput{
ServerName: "bad",
Transport: "carrier-pigeon",
})
if err == nil {
t.Fatal("expected error, got nil")
}
// Assert SDK unwraps the response envelope rather than just wrapping the
// raw body — a generic fmt.Errorf would also pass a substring check.
var de *DutyError
if !errors.As(err, &de) {
t.Fatalf("want *DutyError, got %T: %v", err, err)
}
if de.Code != "InvalidArgument" {
t.Fatalf("want code InvalidArgument, got %q", de.Code)
}
}
func TestCreateMCPServer_HTTPError(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"error":{"code":"Internal","message":"boom"}}`))
}))
defer ts.Close()
client, err := NewClient("test-key", WithBaseURL(ts.URL))
if err != nil {
t.Fatalf("NewClient: %v", err)
}
_, err = client.CreateMCPServer(context.Background(), &CreateMCPServerInput{
ServerName: "x",
Transport: "sse",
})
if err == nil {
t.Fatal("expected error, got nil")
}
}