Files
magpie/internal/upstream/upstream_test.go
T
yetone 2a990175e5 upstream: the vendors' own status pages say when their API is down (#971, emo172: an Anthropic or OpenAI outage looked like a sign-in or quota problem) — a provider that calls Anthropic's, OpenAI's or Moonshot's API (by its base URL's host: api.anthropic.com, which the Claude subscription calls too; api.openai.com and chatgpt.com, Codex's; api.moonshot.cn/.ai) carries "API outage" (red), "API degraded" or "API maintenance" beside its name on its Providers row and its Usage card while the vendor's page says so; its title names the parts and the open incidents, and a click opens the incident (or the page), not the row. internal/upstream reads each page's Statuspage-style summary.json (status.claude.com; status.openai.com, incident.io's; status.moonshot.cn), checked by hand on 2026-10-06, and only the API's own parts (Claude API and Claude Code, not claude.ai; Chat Completions, Responses, Codex, Codex CLI, not "Codex in ChatGPT Desktop", which was degraded that day); an incident naming no parts (OpenAI's) counts only while those parts are affected. Pages are read only when the Providers or Usage page asks (GET /api/upstream), at most once in five minutes, never on a request's way; a page that fails, isn't JSON or no longer lists the API's parts is unknown and marks nothing. A relay serving the same models is never marked. DeepSeek's page (Flashduty) has nothing readable and is only known for its link; OpenRouter, xAI, Mistral and z.ai have no page read. The gateway's failure messages, TUI, CLI and tray aren't changed. zh/ja/de added. upstream_test (fake pages from the real summaries: API down, claude.ai down isn't the API, Claude Code degraded, OpenAI's app isn't the API, 503/HTML/changed page unknown, read once in Fresh and Of never waits, hosts; -race -count=5) and gui's TestUpstreamRoute pass; upstream-status (Anthropic down: its provider and the Claude subscription marked, relay/OpenAI/unread Moonshot not, click opens the incident not the row, all well marks nothing; 900 and 440px, the badge whole, the name giving way) passes in Chromium and WebKit, en and zh, and fails 8/8 without the app.js change. i18n-keys, gui-ja, gui-de, click-scroll, account-return, account-models, usage-keys-fold, deprecated-subscriptions and add-sheet pass; go vet, go test ./internal/gui/, linux/windows/nogui builds pass.
2026-10-06 12:36:05 +08:00

238 lines
9.1 KiB
Go

package upstream
import (
"net/http"
"net/http/httptest"
"slices"
"sync/atomic"
"testing"
"time"
)
// claudePage is status.claude.com's summary.json as it was read on
// 2026-10-06, its components' states set by the test.
func claudePage(api, code, web string, incidents string) string {
return `{"page":{"id":"tymt9n04zgry","name":"Claude","url":"https://status.claude.com"},
"status":{"indicator":"none","description":"All Systems Operational"},
"components":[{"id":"rwppv331jlwc","name":"claude.ai","status":"` + web + `","group":false},
{"id":"0qbwn08sd68x","name":"Claude Console (platform.claude.com)","status":"operational","group":false},
{"id":"k8w3r06qmzrp","name":"Claude API (api.anthropic.com)","status":"` + api + `","group":false},
{"id":"yyzkbfz2thpt","name":"Claude Code","status":"` + code + `","group":false}],
"incidents":[` + incidents + `]}`
}
// openAIPage is status.openai.com's (incident.io): no groups, and
// incidents that name no components.
func openAIPage(responses, desktop string, incidents string) string {
return `{"page":{"id":"01JMDK9XYNY6RXSED6SDWW50WY","name":"OpenAI","url":"https://status.openai.com/"},
"status":{"description":"Partial System Degradation","indicator":"minor"},
"components":[{"id":"01JMXBRMFE6N2NNT7DG6XZQ6PW","name":"Chat Completions","status":"operational","group":null},
{"id":"01JP8CD9JR3HR6Y7G4Q75N4DVW","name":"Responses","status":"` + responses + `","group":null},
{"id":"01KMKFAMWKQ81YWSE1Z18R6VHR","name":"Codex in ChatGPT Desktop","status":"` + desktop + `","group":null},
{"id":"01JMXBNJXGGT5SR5DB9J7GYY48","name":"Voice mode","status":"major_outage","group":null}],
"incidents":[` + incidents + `]}`
}
// serve points the vendors at a fake page and counts what it is asked.
func serve(t *testing.T, id string, body *atomic.Value, code *atomic.Int32) *atomic.Int32 {
t.Helper()
var asked atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
asked.Add(1)
if c := code.Load(); c != 0 && c != 200 {
w.WriteHeader(int(c))
return
}
w.Write([]byte(body.Load().(string)))
}))
t.Cleanup(srv.Close)
was := slices.Clone(Vendors)
t.Cleanup(func() { Vendors = was; Forget() })
for i := range Vendors {
if Vendors[i].ID == id {
Vendors[i].Summary = srv.URL + "/api/v2/summary.json"
}
}
Forget()
return &asked
}
func one(t *testing.T, id string) Status {
t.Helper()
s := Wait(5*time.Second, id)
if len(s) != 1 {
t.Fatalf("%d statuses for %s", len(s), id)
}
return s[0]
}
func TestClaudeAPIDownIsSaid(t *testing.T) {
var body atomic.Value
var code atomic.Int32
body.Store(claudePage("partial_outage", "operational", "operational",
`{"name":"Elevated errors on Claude API","status":"investigating","impact":"major","shortlink":"https://stspg.io/x","components":[{"id":"k8w3r06qmzrp","name":"Claude API (api.anthropic.com)"}]},
{"name":"claude.ai login issues","status":"investigating","impact":"minor","shortlink":"https://stspg.io/y","components":[{"id":"rwppv331jlwc","name":"claude.ai"}]},
{"name":"Old one","status":"resolved","impact":"major","components":[{"id":"k8w3r06qmzrp"}]}`))
serve(t, "anthropic", &body, &code)
s := one(t, "anthropic")
if s.Level != "partial" || s.Error != "" {
t.Fatalf("level %q (error %q), want partial", s.Level, s.Error)
}
if len(s.Parts) != 1 || s.Parts[0].Name != "Claude API (api.anthropic.com)" {
t.Fatalf("parts %+v", s.Parts)
}
// the API's own incident, not claude.ai's, not a resolved one
if len(s.Incidents) != 1 || s.Incidents[0].Name != "Elevated errors on Claude API" || s.Incidents[0].URL != "https://stspg.io/x" {
t.Fatalf("incidents %+v", s.Incidents)
}
if s.Page != "https://status.claude.com" {
t.Fatalf("page %q", s.Page)
}
}
// claude.ai down says nothing of the API
func TestClaudeAppDownIsNotTheAPI(t *testing.T) {
var body atomic.Value
var code atomic.Int32
body.Store(claudePage("operational", "operational", "major_outage",
`{"name":"claude.ai down","status":"identified","impact":"critical","components":[{"id":"rwppv331jlwc","name":"claude.ai"}]}`))
serve(t, "anthropic", &body, &code)
if s := one(t, "anthropic"); s.Level != "ok" || len(s.Parts) != 0 || len(s.Incidents) != 0 {
t.Fatalf("got %+v, want ok with nothing listed", s)
}
}
// Claude Code is what a Claude subscription's requests run through
func TestClaudeCodeDegraded(t *testing.T) {
var body atomic.Value
var code atomic.Int32
body.Store(claudePage("operational", "degraded_performance", "operational", ""))
serve(t, "anthropic", &body, &code)
if s := one(t, "anthropic"); s.Level != "degraded" {
t.Fatalf("level %q, want degraded", s.Level)
}
}
// OpenAI's page as it was on 2026-10-06: its desktop app's Codex
// degraded and voice mode down, the API well — and an incident that
// names no parts is the API's only while the API's parts are affected.
func TestOpenAIAppIsNotTheAPI(t *testing.T) {
var body atomic.Value
var code atomic.Int32
inc := `{"name":"Elevated Work Mode errors","status":"monitoring","impact":"minor","shortlink":null,"components":null}`
body.Store(openAIPage("operational", "degraded_performance", inc))
serve(t, "openai", &body, &code)
if s := one(t, "openai"); s.Level != "ok" || len(s.Incidents) != 0 {
t.Fatalf("got %+v, want ok", s)
}
body.Store(openAIPage("major_outage", "operational", inc))
Forget()
s := one(t, "openai")
if s.Level != "major" || len(s.Incidents) != 1 || s.Incidents[0].Name != "Elevated Work Mode errors" {
t.Fatalf("got %+v, want major with its incident", s)
}
}
// a page that fails is unknown, never "all well", and it isn't kept
// saying the outage it said before either
func TestFailedPageIsUnknown(t *testing.T) {
var body atomic.Value
var code atomic.Int32
body.Store(claudePage("major_outage", "operational", "operational", ""))
serve(t, "anthropic", &body, &code)
if s := one(t, "anthropic"); s.Level != "major" {
t.Fatalf("level %q", s.Level)
}
code.Store(503)
defer func(n func() time.Time) { now = n }(now)
now = func() time.Time { return time.Now().Add(Fresh) }
s := one(t, "anthropic")
if s.Level != "" || s.Error == "" {
t.Fatalf("got %+v, want unknown with an error", s)
}
code.Store(0)
body.Store(`<html>Just a moment...</html>`)
now = func() time.Time { return time.Now().Add(3 * Fresh) }
if s := one(t, "anthropic"); s.Level != "" || s.Error == "" {
t.Fatalf("got %+v from a page that isn't JSON", s)
}
// a page that no longer lists the API's parts can't say anything of it
body.Store(`{"components":[{"id":"a","name":"Something else","status":"operational"}],"incidents":[]}`)
now = func() time.Time { return time.Now().Add(5 * Fresh) }
if s := one(t, "anthropic"); s.Level != "" || s.Error == "" {
t.Fatalf("got %+v from a page without the API's parts", s)
}
}
// a page is read once in Fresh however often it is asked for, and the
// asking never waits for it
func TestReadOnceInFresh(t *testing.T) {
var body atomic.Value
var code atomic.Int32
body.Store(claudePage("operational", "operational", "operational", ""))
slow := make(chan struct{})
asked := serve(t, "anthropic", &body, &code)
one(t, "anthropic")
for range 20 {
Of("anthropic")
}
if n := asked.Load(); n != 1 {
t.Fatalf("asked %d times within Fresh, want 1", n)
}
// stale: asked again, and Of answers at once with the last reading
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-slow
w.Write([]byte(claudePage("major_outage", "operational", "operational", "")))
}))
defer srv.Close()
for i := range Vendors {
if Vendors[i].ID == "anthropic" {
Vendors[i].Summary = srv.URL
}
}
defer func(n func() time.Time) { now = n }(now)
now = func() time.Time { return time.Now().Add(Fresh) }
start := time.Now()
s := Of("anthropic")
if took := time.Since(start); took > 100*time.Millisecond {
t.Fatalf("Of waited %v for the page", took)
}
if s[0].Level != "ok" {
t.Fatalf("level %q while the page is read again, want the last one", s[0].Level)
}
close(slow)
if s := one(t, "anthropic"); s.Level != "major" {
t.Fatalf("level %q once read again, want major", s.Level)
}
}
// DeepSeek's page has nothing to read: it is only linked, never asked
func TestLinkedOnly(t *testing.T) {
s := Of("deepseek")
if len(s) != 1 || s[0].Level != "" || s[0].Error != "" || s[0].Page != "https://status.deepseek.com" {
t.Fatalf("got %+v", s)
}
}
func TestVendorOf(t *testing.T) {
for _, c := range []struct {
bases []string
want string
}{
{[]string{"", "", "https://api.anthropic.com"}, "anthropic"},
{[]string{"https://api.openai.com/v1"}, "openai"},
{[]string{"", "https://chatgpt.com/backend-api/codex"}, "openai"},
{[]string{"https://api.deepseek.com/v1", "", "https://api.deepseek.com/anthropic"}, "deepseek"},
{[]string{"https://api.moonshot.cn/v1"}, "moonshot"},
{[]string{"https://API.Moonshot.ai:443/v1"}, "moonshot"},
// a relay that serves the same models is not the vendor
{[]string{"https://relay.example.com/v1", "", "https://relay.example.com"}, ""},
{[]string{"https://api.anthropic.com.evil.example/v1"}, ""},
{[]string{"http://127.0.0.1:11434/v1"}, ""},
} {
if got := VendorOf(c.bases...); got != c.want {
t.Errorf("VendorOf(%q) = %q, want %q", c.bases, got, c.want)
}
}
}