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} 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, "