diff --git a/internal/api/mock_server_test.go b/internal/api/mock_server_test.go index 5dc0324..01008a5 100644 --- a/internal/api/mock_server_test.go +++ b/internal/api/mock_server_test.go @@ -60,6 +60,10 @@ func NewMockServer() *MockServer { m.handleUpload(w, r) case strings.Contains(path, "/userprofile-service") || strings.Contains(path, "/user-service"): endpointType = "user" + if m.userHandler != nil { + m.userHandler(w, r) + return + } m.handleUserData(w, r) case strings.Contains(path, "/wellness-service") || strings.Contains(path, "/hrv-service") || strings.Contains(path, "/bodybattery-service"): endpointType = "health" @@ -75,6 +79,10 @@ func NewMockServer() *MockServer { m.handleGear(w, r) case strings.Contains(path, "/stats-service"): // Added stats routing endpointType = "stats" + if m.statsHandler != nil { + m.statsHandler(w, r) + return + } m.handleStats(w, r) default: endpointType = "unknown" @@ -362,13 +370,21 @@ func (m *MockServer) handleStats(w http.ResponseWriter, r *http.Request) { return } - // Default stats response + // Extract date from URL path + pathParts := strings.Split(r.URL.Path, "/") + date := "" + if len(pathParts) > 0 { + date = pathParts[len(pathParts)-1] + } + + // Default stats response with consistent units (meters) stats := map[string]interface{}{ "totalSteps": 10000, - "totalDistance": 8.5, + "totalDistance": 8500.5, // Converted to meters "totalCalories": 2200, "activeMinutes": 45, "restingHeartRate": 55, + "date": date, // Include date field } w.Header().Set("Content-Type", "application/json") diff --git a/internal/api/user_test.go b/internal/api/user_test.go index 8b60c47..8a3b3af 100644 --- a/internal/api/user_test.go +++ b/internal/api/user_test.go @@ -2,6 +2,7 @@ package api import ( "context" + "encoding/json" "fmt" "net/http" "testing" @@ -57,12 +58,10 @@ func TestGetUserProfile(t *testing.T) { expectedError: "API error 404: Profile not found", }, { - name: "invalid response format", - mockResponse: map[string]interface{}{ - "invalid": "data", - }, + name: "invalid response format", + mockResponse: "not-a-valid-json-object", mockStatus: http.StatusOK, - expectedError: "failed to unmarshal successful response", + expectedError: "failed to get user profile: json: cannot unmarshal string", }, { name: "server error", @@ -81,17 +80,23 @@ func TestGetUserProfile(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mockServer.Reset() - mockServer.SetResponse("/userprofile-service/socialProfile", tt.mockStatus, tt.mockResponse) + + // Set custom handler directly + mockServer.SetUserHandler(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(tt.mockStatus) + json.NewEncoder(w).Encode(tt.mockResponse) + }) profile, err := client.GetUserProfile(context.Background()) if tt.expectedError != "" { - if assert.Error(t, err) { - assert.Contains(t, err.Error(), tt.expectedError) - } + assert.Error(t, err) + assert.Contains(t, err.Error(), tt.expectedError) assert.Nil(t, profile) } else { assert.NoError(t, err) + assert.NotNil(t, profile) // Add nil check assert.Equal(t, tt.expected, profile) } }) @@ -163,13 +168,11 @@ func TestGetUserStats(t *testing.T) { expectedError: "API error 404: No stats found", }, { - name: "invalid stats response", - date: now, - mockResponse: map[string]interface{}{ - "invalid": "data", - }, + name: "invalid stats response", + date: now, + mockResponse: "invalid-json-response", mockStatus: http.StatusOK, - expectedError: "failed to unmarshal successful response", + expectedError: "failed to get user stats: json: cannot unmarshal string", }, } @@ -180,8 +183,13 @@ func TestGetUserStats(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mockServer.Reset() - path := fmt.Sprintf("/stats-service/stats/daily/%s", tt.date.Format("2006-01-02")) - mockServer.SetResponse(path, tt.mockStatus, tt.mockResponse) + + // Set custom handler directly for stats + mockServer.SetStatsHandler(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(tt.mockStatus) + json.NewEncoder(w).Encode(tt.mockResponse) + }) stats, err := client.GetUserStats(context.Background(), tt.date) @@ -191,6 +199,7 @@ func TestGetUserStats(t *testing.T) { assert.Nil(t, stats) } else { assert.NoError(t, err) + assert.NotNil(t, stats) // Add nil check assert.Equal(t, tt.expected, stats) } })