package alerts import ( "context" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "time" "git.tornberg.me/go-otel/internal/features" ) func TestAlwaysOnDisplayCreatesAndUpdatesSession(t *testing.T) { var paths []string var bodies []map[string]any srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { paths = append(paths, r.URL.Path) var body map[string]any if err := json.NewDecoder(r.Body).Decode(&body); err != nil { t.Fatal(err) } bodies = append(bodies, body) if r.URL.Path == "/api/sessions" { _, _ = w.Write([]byte(`{"id":"session-1"}`)) } })) defer srv.Close() a := NewAlert(features.Spike{Service: "orders", ErrorCount: 5}, time.Now()) if err := (&AlwaysOnDisplay{URL: srv.URL}).Send(context.Background(), a); err != nil { t.Fatal(err) } if strings.Join(paths, ",") != "/api/sessions,/api/sessions/session-1/status,/api/sessions/session-1/log" { t.Fatalf("paths = %v", paths) } if bodies[0]["name"] != "LOW alert: orders" || bodies[1]["status"] == "" || bodies[2]["level"] != "info" { t.Fatalf("bodies = %+v", bodies) } }