102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
"watch-tool/models"
|
|
)
|
|
|
|
type MockStorage struct {
|
|
}
|
|
|
|
func (m *MockStorage) Query(ctx context.Context, q StorageQuery) ([]models.LogMessage, error) {
|
|
return []models.LogMessage{
|
|
{
|
|
Service: "tixstream",
|
|
Fields: map[string]any{
|
|
"transfer_id": "uuid-1234",
|
|
"direction": "incoming",
|
|
},
|
|
},
|
|
{
|
|
Service: "tixstream",
|
|
Fields: map[string]any{
|
|
"transfer_id": "uuid-1234",
|
|
"direction": "incoming",
|
|
},
|
|
},
|
|
{
|
|
Service: "tixstream",
|
|
Fields: map[string]any{
|
|
"transfer_id": "uuid-5678",
|
|
"direction": "outgoing",
|
|
},
|
|
},
|
|
{
|
|
Service: "tjm",
|
|
Fields: map[string]any{
|
|
"transfer_name_raw": "20250927-ABC-test-in",
|
|
"correlation_id": "corr-9999",
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (m *MockStorage) Store(ctx context.Context, entries *models.LogMessage) error { return nil }
|
|
func (m *MockStorage) Close() error { return nil }
|
|
func (m *MockStorage) GetUnexportedEntries(ctx context.Context, limit int) ([]models.LogMessage, error) {
|
|
return nil, nil
|
|
}
|
|
func (m *MockStorage) MarkAsExported(ctx context.Context, entries []models.LogMessage) error {
|
|
return nil
|
|
}
|
|
func (m *MockStorage) DeleteOldEntries(ctx context.Context, cutoff time.Time) (int64, error) {
|
|
return 0, nil
|
|
}
|
|
func (m *MockStorage) GetStats(ctx context.Context) (map[string]any, error) { return nil, nil }
|
|
func (m *MockStorage) StoreBatch(ctx context.Context, entries []models.LogMessage) error { return nil }
|
|
|
|
func TestWebService_HandleServiceStats(t *testing.T) {
|
|
mockStorage := &MockStorage{}
|
|
cfg := &Config{WebService: WebConfig{Enabled: true}}
|
|
ws := NewWebService(cfg, mockStorage)
|
|
|
|
req, err := http.NewRequest("GET", "/api/service/tixstream/stats", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
req.SetPathValue("service", "tixstream")
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
ws.handleServiceStats(rr, req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK)
|
|
}
|
|
|
|
var response map[string]any
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
counts := response["transfer_counts"].(map[string]any)
|
|
total := int(counts["total"].(float64))
|
|
incoming := int(counts["incoming"].(float64))
|
|
outgoing := int(counts["outgoing"].(float64))
|
|
|
|
if total != 3 {
|
|
t.Errorf("Expected 3 total transfers, got %d", total)
|
|
}
|
|
if incoming != 2 {
|
|
t.Errorf("Expected 2 incoming transfers, got %d", incoming)
|
|
}
|
|
if outgoing != 1 {
|
|
t.Errorf("Expected 1 outgoing transfer, got %d", outgoing)
|
|
}
|
|
}
|