package controller import ( perfmetrics "github.com/QuantumNous/new-api/pkg/perf_metrics" "github.com/QuantumNous/new-api/service" "github.com/stretchr/testify/assert" "testing" ) func TestCurrentGroupStatus(t *testing.T) { const now int64 = 10000 for _, tc := range []struct { name string success int64 probes []service.ProbeHistoryPoint disabled bool want int source string at int64 }{ {"fresh user overrides older failed probe", now, []service.ProbeHistoryPoint{{At: now - 10, Status: 0}}, false, 1, "requests", now}, {"newer failed probe cannot erase recent user success", now - 10, []service.ProbeHistoryPoint{{At: now, Status: 0}}, false, 1, "requests", now - 10}, {"working primary with failing backup stays normal", now - 60, []service.ProbeHistoryPoint{{At: now, Status: 2}}, false, 1, "requests", now - 60}, {"success remains fresh for full fifteen minutes", now - 900, nil, false, 1, "requests", now - 900}, {"expired user success uses latest partial probe", now - 901, []service.ProbeHistoryPoint{{At: now - 10, Status: 2}}, false, 2, "detection", now - 10}, {"healthy probe overrides historical hourly error", 0, []service.ProbeHistoryPoint{{At: now - 100, Status: 1}}, false, 1, "detection", now - 100}, {"latest probe determines availability", 0, []service.ProbeHistoryPoint{{At: now - 100, Status: 0}, {At: now - 200, Status: 1}}, false, 0, "detection", now - 100}, {"one missed probe remains fresh", 0, []service.ProbeHistoryPoint{{At: now - 1800, Status: 1}}, false, 1, "detection", now - 1800}, {"stale probe is unknown", 0, []service.ProbeHistoryPoint{{At: now - 1801, Status: 0}}, false, -1, "detection", 0}, {"quality failures alone do not imply an outage", 0, nil, false, -1, "detection", 0}, {"disabled remains disabled", now, []service.ProbeHistoryPoint{{At: now, Status: 1}}, true, 0, "disabled", 0}, {"future evidence is ignored", now + 1, []service.ProbeHistoryPoint{{At: now + 1, Status: 1}}, false, -1, "detection", 0}, } { t.Run(tc.name, func(t *testing.T) { m := Monitor{Status: 0, StatusSource: "requests", StatusCheckedAt: now - 100, ChannelDisabled: tc.disabled, ProbeHistory: tc.probes, Uptime: 0.58, Fingerprint: &service.AstraSamplingSummary{Errors: 25}, Benchmark: &service.BenchmarkSummary{Failed: 33}} applyCurrentStatus(&m, perfmetrics.UserRequestResult{At: tc.success * 1000, Success: true}, now) assert.Equal(t, tc.want, m.Status) assert.Equal(t, tc.source, m.StatusSource) assert.Equal(t, tc.at, m.StatusCheckedAt) assert.Equal(t, 25, m.Fingerprint.Errors) assert.Equal(t, 33, m.Benchmark.Failed) assert.Equal(t, 0.58, m.Uptime) }) } } func TestFailedUserRequestChangesGroupStatus(t *testing.T) { const now int64 = 10000 for _, tc := range []struct { name string at int64 want int source string }{ {"user failure overrides a newer healthy probe", now - 10, 0, "requests"}, {"user failure expires after fifteen minutes", now - 901, 1, "detection"}, } { t.Run(tc.name, func(t *testing.T) { m := Monitor{Status: 1, ProbeHistory: []service.ProbeHistoryPoint{{At: now, Status: 1}}} applyCurrentStatus(&m, perfmetrics.UserRequestResult{At: tc.at * 1000, Success: false}, now) assert.Equal(t, tc.want, m.Status) assert.Equal(t, tc.source, m.StatusSource) }) } }