Skip to content

Commit 0dc0eff

Browse files
committed
Restore test helper functions to fix test compilation
1 parent 6bb96fa commit 0dc0eff

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

pkg/flashduty/members_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package flashduty
22

33
import (
44
"context"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httptest"
58
"testing"
69

10+
"github.com/flashcatcloud/flashduty-mcp-server/pkg/translations"
711
"github.com/mark3labs/mcp-go/mcp"
812
"github.com/stretchr/testify/assert"
913
"github.com/stretchr/testify/require"
@@ -33,6 +37,65 @@ func getTextResult(t *testing.T, result *mcp.CallToolResult) mcp.TextContent {
3337
return textContent
3438
}
3539

40+
// testServer creates a mock Flashduty API server for testing
41+
func testServer(t *testing.T, responses map[string]interface{}) *httptest.Server {
42+
t.Helper()
43+
44+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
45+
// Check if a response is configured for the given path
46+
if response, exists := responses[r.URL.Path]; exists {
47+
w.Header().Set("Content-Type", "application/json")
48+
w.WriteHeader(http.StatusOK)
49+
err := json.NewEncoder(w).Encode(response)
50+
if err != nil {
51+
t.Fatalf("Failed to write mock response: %v", err)
52+
}
53+
return
54+
}
55+
56+
// Check if an error is configured
57+
if errorResponse, exists := responses["error:"+r.URL.Path]; exists {
58+
w.Header().Set("Content-Type", "application/json")
59+
w.WriteHeader(http.StatusInternalServerError)
60+
err := json.NewEncoder(w).Encode(errorResponse)
61+
if err != nil {
62+
t.Fatalf("Failed to write mock error response: %v", err)
63+
}
64+
return
65+
}
66+
67+
// Default to not found
68+
http.NotFound(w, r)
69+
}))
70+
}
71+
72+
// testTranslationHelper is a simple translation helper for testing
73+
func testTranslationHelper(key, defaultValue string) string {
74+
return defaultValue
75+
}
76+
77+
// testSetup provides common test setup for Flashduty tests
78+
func testSetup(t *testing.T, responses map[string]interface{}) (GetFlashdutyClientFn, translations.TranslationHelperFunc) {
79+
t.Helper()
80+
81+
// Create a test server
82+
server := testServer(t, responses)
83+
t.Cleanup(server.Close) // Ensure the server is closed after the test
84+
85+
// Create a client that points to the test server
86+
getClient := func(ctx context.Context) (context.Context, *Client, error) {
87+
client, err := NewClient("test-app-key", server.URL, "test-agent")
88+
if err != nil {
89+
return nil, nil, err
90+
}
91+
return ctx, client, nil
92+
}
93+
94+
translator := testTranslationHelper
95+
96+
return getClient, translator
97+
}
98+
3699
func TestMemberInfos(t *testing.T) {
37100
responses := map[string]interface{}{
38101
"/person/infos": map[string]interface{}{

0 commit comments

Comments
 (0)