|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/persys-dev/persysctl/internal/config" |
| 10 | +) |
| 11 | + |
| 12 | +func TestMeterSummaryGatewayPath(t *testing.T) { |
| 13 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 14 | + if r.Method != http.MethodGet { |
| 15 | + t.Fatalf("unexpected method: %s", r.Method) |
| 16 | + } |
| 17 | + if r.URL.Path != "/meter/v1/workloads/wl-123/summary" { |
| 18 | + t.Fatalf("unexpected path: %s", r.URL.Path) |
| 19 | + } |
| 20 | + if got := r.URL.Query().Get("from"); got == "" { |
| 21 | + t.Fatal("missing from query") |
| 22 | + } |
| 23 | + w.Header().Set("Content-Type", "application/json") |
| 24 | + _, _ = w.Write([]byte(`{"workload_id":"wl-123","sample_count":7}`)) |
| 25 | + })) |
| 26 | + defer server.Close() |
| 27 | + |
| 28 | + cli := &Client{cfg: config.Config{Transport: "http", APIEndpoint: server.URL}, httpClient: server.Client()} |
| 29 | + from := time.Date(2026, 7, 1, 0, 0, 0, 0, time.UTC) |
| 30 | + to := time.Date(2026, 7, 2, 0, 0, 0, 0, time.UTC) |
| 31 | + |
| 32 | + resp, err := cli.MeterSummary("wl-123", from, to) |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("MeterSummary() error = %v", err) |
| 35 | + } |
| 36 | + if resp["workload_id"] != "wl-123" { |
| 37 | + t.Fatalf("MeterSummary() workload_id = %v", resp["workload_id"]) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestWatchGatewayEventsParsesSSE(t *testing.T) { |
| 42 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 43 | + if r.URL.Path != "/events/watch" { |
| 44 | + t.Fatalf("unexpected path: %s", r.URL.Path) |
| 45 | + } |
| 46 | + w.Header().Set("Content-Type", "text/event-stream") |
| 47 | + _, _ = w.Write([]byte("event: event\ndata: {\"type\":\"workload\",\"workload_id\":\"wl-9\"}\n\n")) |
| 48 | + })) |
| 49 | + defer server.Close() |
| 50 | + |
| 51 | + cli := &Client{cfg: config.Config{Transport: "http", APIEndpoint: server.URL}, httpClient: server.Client()} |
| 52 | + events, err := cli.WatchGatewayEvents("workload", "wl-9", "", 10) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("WatchGatewayEvents() error = %v", err) |
| 55 | + } |
| 56 | + if len(events) != 1 { |
| 57 | + t.Fatalf("WatchGatewayEvents() len = %d", len(events)) |
| 58 | + } |
| 59 | + if got := events[0]["workload_id"]; got != "wl-9" { |
| 60 | + t.Fatalf("WatchGatewayEvents() workload_id = %v", got) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestWatchGatewayEventsTimesOutOnIdleStream(t *testing.T) { |
| 65 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 66 | + w.Header().Set("Content-Type", "text/event-stream") |
| 67 | + if f, ok := w.(http.Flusher); ok { |
| 68 | + f.Flush() |
| 69 | + } |
| 70 | + <-r.Context().Done() |
| 71 | + })) |
| 72 | + defer server.Close() |
| 73 | + |
| 74 | + cli := &Client{cfg: config.Config{Transport: "http", APIEndpoint: server.URL, RPCTimeoutSeconds: 1}, httpClient: server.Client()} |
| 75 | + start := time.Now() |
| 76 | + events, err := cli.WatchGatewayEvents("", "", "", 20) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("WatchGatewayEvents() error = %v", err) |
| 79 | + } |
| 80 | + if len(events) != 0 { |
| 81 | + t.Fatalf("WatchGatewayEvents() idle stream returned %d events", len(events)) |
| 82 | + } |
| 83 | + if elapsed := time.Since(start); elapsed > 3*time.Second { |
| 84 | + t.Fatalf("WatchGatewayEvents() took too long: %s", elapsed) |
| 85 | + } |
| 86 | +} |
0 commit comments