package api_test import ( "context" "net/http" "strings" "testing" "time" "github.com/google/uuid" ) func TestTimeRangesAndSLA(t *testing.T) { e := newEnv(t) _, region := e.platformChecker(t, uniq("eu-")) owner := e.browser(t) owner.signup("o@"+uniq("r")+".test", uniq("rng-")) st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{ "name": "Shop", "type": "tcp", "target": "example.com:443", "interval_seconds": 3600, "regions": []string{region}, }) if st != http.StatusCreated { t.Fatalf("create monitor = %d %v", st, body) } id := uuid.MustParse(body["id"].(string)) org := uuid.MustParse(owner.orgID()) // Two days old, down for six minutes half an hour ago. ctx := context.Background() now := time.Now() conn, err := e.dbPool.Acquire(ctx) if err != nil { t.Fatal(err) } defer conn.Release() if _, err := conn.Exec(ctx, `SELECT set_config('app.scope', 'system', false)`); err != nil { t.Fatal(err) } if _, err := conn.Exec(ctx, `UPDATE monitors SET created_at = $2 WHERE id = $1`, id, now.Add(-48*time.Hour)); err != nil { t.Fatal(err) } if _, err := conn.Exec(ctx, `INSERT INTO incidents (org_id, monitor_id, started_at, resolved_at) VALUES ($1, $2, $3, $4)`, org, id, now.Add(-30*time.Minute), now.Add(-24*time.Minute)); err != nil { t.Fatal(err) } _, _ = conn.Exec(ctx, `SELECT set_config('app.scope', '', false)`) _, _, html := owner.page("/monitors/" + id.String() + "?range=1h") // The monitor is two days old, so 7d and longer cover only those two // days: 6 of 2880 minutes down. for _, want := range []string{">1h<", "90.0%", ">7d<", "99.79%", ">90d<"} { if !strings.Contains(html, want) { t.Errorf("monitor page lacks %q", want) } } bars := map[string]int{"1h": 60, "3h": 36, "12h": 48, "24h": 48, "7d": 42, "30d": 60, "90d": 45, "180d": 45, "1y": 52} for key, n := range bars { st, _, html := owner.page("/?range=" + key) if st != http.StatusOK { t.Errorf("GET /?range=%s = %d", key, st) continue } // The dashboard has one monitor; its chart holds one rect per bar. if got := strings.Count(html, "`+key+"<") if got != disabled { t.Errorf("range %s disabled = %t, want %t", key, got, disabled) } } if !strings.Contains(html, `href="/monitors/`+id.String()+`?range=3h"`) { t.Error("3h is not selectable with two hours of data") } if !strings.Contains(html, "By region") && !strings.Contains(html, "Nach Region") { t.Fatal("monitor page lacks the per-region section") } if !strings.Contains(html, ">100%<") || !strings.Contains(html, ">0.0%<") { t.Error("per-region availability does not show 100% for eu and 0.0% for us") } // The dashboard follows the monitor with the oldest data. if _, _, dash := owner.page("/?range=1h"); !strings.Contains(dash, `aria-disabled="true">12h<`) || strings.Contains(dash, `aria-disabled="true">3h<`) { t.Error("dashboard range availability does not follow the data") } }