@@ -0,0 +1,213 @@
1+package mcp
2+
3+import (
4+ "context"
5+ "encoding/json"
6+ "testing"
7+)
8+
9+// mockCredentialStore is a mock implementation for testing.
10+type mockCredentialStore struct {
11+ credentials map[string]struct{ username, password string }
12+}
13+
14+func newMockCredentialStore() *mockCredentialStore {
15+ return &mockCredentialStore{
16+ credentials: make(map[string]struct{ username, password string }),
17+ }
18+}
19+
20+func (m *mockCredentialStore) SaveCredentials(sessionID, username, password string) error {
21+ m.credentials[sessionID] = struct{ username, password string }{username, password}
22+ return nil
23+}
24+
25+func (m *mockCredentialStore) GetCredentials(sessionID string) (string, string, error) {
26+ cred, exists := m.credentials[sessionID]
27+ if !exists {
28+ return "", "", nil
29+ }
30+ return cred.username, cred.password, nil
31+}
32+
33+func TestSaveCredentialsTool(t *testing.T) {
34+ store := newMockCredentialStore()
35+ sessionID := "test-session-123"
36+ handler := MakeSaveCredentialsHandler(store, sessionID)
37+
38+ tests := []struct {
39+ name string
40+ args SaveCredentialsArgs
41+ wantError bool
42+ wantText string
43+ }{
44+ {
45+ name: "valid credentials",
46+ args: SaveCredentialsArgs{Username: "player1", Password: "secret123"},
47+ wantError: false,
48+ wantText: "Credentials saved successfully for user 'player1'",
49+ },
50+ {
51+ name: "empty username",
52+ args: SaveCredentialsArgs{Username: "", Password: "secret123"},
53+ wantError: true,
54+ wantText: "Username cannot be empty",
55+ },
56+ {
57+ name: "empty password",
58+ args: SaveCredentialsArgs{Username: "player1", Password: ""},
59+ wantError: true,
60+ wantText: "Password cannot be empty",
61+ },
62+ }
63+
64+ for _, tt := range tests {
65+ t.Run(tt.name, func(t *testing.T) {
66+ argsJSON, _ := json.Marshal(tt.args)
67+ result, err := handler(context.Background(), argsJSON)
68+
69+ if err != nil {
70+ t.Fatalf("unexpected error: %v", err)
71+ }
72+
73+ if result.IsError != tt.wantError {
74+ t.Errorf("IsError = %v, want %v", result.IsError, tt.wantError)
75+ }
76+
77+ if len(result.Content) == 0 {
78+ t.Fatal("expected content blocks")
79+ }
80+
81+ if result.Content[0].Text != tt.wantText {
82+ t.Errorf("Text = %q, want %q", result.Content[0].Text, tt.wantText)
83+ }
84+ })
85+ }
86+}
87+
88+func TestGetCredentialsTool(t *testing.T) {
89+ store := newMockCredentialStore()
90+ sessionID := "test-session-456"
91+ handler := MakeGetCredentialsHandler(store, sessionID)
92+
93+ t.Run("no credentials saved", func(t *testing.T) {
94+ result, err := handler(context.Background(), json.RawMessage("{}"))
95+ if err != nil {
96+ t.Fatalf("unexpected error: %v", err)
97+ }
98+
99+ if result.IsError {
100+ t.Error("expected no error")
101+ }
102+
103+ if result.Content[0].Text != "No credentials saved for this session" {
104+ t.Errorf("unexpected text: %s", result.Content[0].Text)
105+ }
106+ })
107+
108+ t.Run("credentials exist", func(t *testing.T) {
109+ // Save credentials first
110+ store.SaveCredentials(sessionID, "player2", "pass456")
111+
112+ result, err := handler(context.Background(), json.RawMessage("{}"))
113+ if err != nil {
114+ t.Fatalf("unexpected error: %v", err)
115+ }
116+
117+ if result.IsError {
118+ t.Error("expected no error")
119+ }
120+
121+ var creds GetCredentialsResult
122+ if err := json.Unmarshal([]byte(result.Content[0].Text), &creds); err != nil {
123+ t.Fatalf("failed to parse result: %v", err)
124+ }
125+
126+ if creds.Username != "player2" {
127+ t.Errorf("Username = %q, want %q", creds.Username, "player2")
128+ }
129+
130+ if creds.Password != "pass456" {
131+ t.Errorf("Password = %q, want %q", creds.Password, "pass456")
132+ }
133+ })
134+}
135+
136+func TestCredentialToolDefinitions(t *testing.T) {
137+ t.Run("save_credentials tool", func(t *testing.T) {
138+ tool := NewSaveCredentialsTool()
139+
140+ if tool.Name != "save_credentials" {
141+ t.Errorf("Name = %q, want %q", tool.Name, "save_credentials")
142+ }
143+
144+ if tool.Description == "" {
145+ t.Error("Description should not be empty")
146+ }
147+
148+ if len(tool.InputSchema) == 0 {
149+ t.Error("InputSchema should not be empty")
150+ }
151+
152+ var schema map[string]interface{}
153+ if err := json.Unmarshal(tool.InputSchema, &schema); err != nil {
154+ t.Fatalf("failed to parse schema: %v", err)
155+ }
156+
157+ if schema["type"] != "object" {
158+ t.Error("schema type should be object")
159+ }
160+ })
161+
162+ t.Run("get_credentials tool", func(t *testing.T) {
163+ tool := NewGetCredentialsTool()
164+
165+ if tool.Name != "get_credentials" {
166+ t.Errorf("Name = %q, want %q", tool.Name, "get_credentials")
167+ }
168+
169+ if tool.Description == "" {
170+ t.Error("Description should not be empty")
171+ }
172+
173+ if len(tool.InputSchema) == 0 {
174+ t.Error("InputSchema should not be empty")
175+ }
176+ })
177+}
178+
179+func TestSessionIsolation(t *testing.T) {
180+ store := newMockCredentialStore()
181+
182+ // Session 1
183+ session1 := "session-1"
184+ handler1 := MakeSaveCredentialsHandler(store, session1)
185+ args1, _ := json.Marshal(SaveCredentialsArgs{Username: "user1", Password: "pass1"})
186+ handler1(context.Background(), args1)
187+
188+ // Session 2
189+ session2 := "session-2"
190+ handler2 := MakeSaveCredentialsHandler(store, session2)
191+ args2, _ := json.Marshal(SaveCredentialsArgs{Username: "user2", Password: "pass2"})
192+ handler2(context.Background(), args2)
193+
194+ // Verify session 1 credentials
195+ getHandler1 := MakeGetCredentialsHandler(store, session1)
196+ result1, _ := getHandler1(context.Background(), json.RawMessage("{}"))
197+ var creds1 GetCredentialsResult
198+ json.Unmarshal([]byte(result1.Content[0].Text), &creds1)
199+
200+ if creds1.Username != "user1" {
201+ t.Errorf("Session 1: Username = %q, want %q", creds1.Username, "user1")
202+ }
203+
204+ // Verify session 2 credentials
205+ getHandler2 := MakeGetCredentialsHandler(store, session2)
206+ result2, _ := getHandler2(context.Background(), json.RawMessage("{}"))
207+ var creds2 GetCredentialsResult
208+ json.Unmarshal([]byte(result2.Content[0].Text), &creds2)
209+
210+ if creds2.Username != "user2" {
211+ t.Errorf("Session 2: Username = %q, want %q", creds2.Username, "user2")
212+ }
213+}