Files
magpie/internal/gui/webpage_error_test.go
T
yetone 059d995d56 gateway keys: the list reads the ledger once for all keys, and a proxy's web page is said by its title (aiandcloud on X)
aiandcloud (2.98B tokens, 12,654 requests a day, server mode behind a
proxy) saw Gateway keys draw a Cloudflare 524 page as raw HTML.

Slow list: every poll of /api/caller-keys read up to a month of ledger
rows once per window per key. budget.AllOf now reads each distinct window
once for all keys, and keeps the settled blocks' sums with a cursor
(usage.VisitFrom), so a poll only reads what was appended since; the
unfinished tail block is always read again. A rewritten or shorter log
resets the cursor. All(k) is AllOf of one key.
Numbers (BenchmarkAll vs BenchmarkReadEach, 8 keys x day/week/month,
31 days x 12000 rows/day = 372k rows, warm): 211-228 ms before, 23.5 ms
after.

Raw HTML: fail() now turns an error string holding a web page (a remote
magpie's or a vendor's 524/502 page inside the JSON error) into a short
said/what pair, "A proxy in front of magpie answered, not magpie ({what})"
with the page's title, translated in all 5 languages (key already in
i18n.js). The browser already handled non-JSON bodies (notJSON, #2032).

Run (Windows box, mote ssh was down earlier): go test budget, usage, gui
(AllOf, Budget, FailSays, I18n, Keys); gateway-keys-proxy-page and
gateway-key-limit GUI files in chromium and webkit, 5 languages: 24/24;
gofmt; vet windows, linux nogui, darwin (mote, only a clang deprecation
warning). Broken by hand: with the old fail() TestFailSaysAProxyPageByItsTitle
is red; with VisitFrom's reset() removed TestAllOfIsWhatEachWindowRead is red.
Not tried: a user-scale cold ledger, a server behind a real Cloudflare,
the full suite before the tag (started after).
2026-11-11 13:00:54 +08:00

41 lines
1.3 KiB
Go

package gui
import (
"encoding/json"
"errors"
"net/http/httptest"
"strings"
"testing"
)
const cf524 = `<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<head><title>magpie.example.com | 524: A timeout occurred</title></head>
<body><h1>A timeout occurred</h1></body></html>`
// An error with a proxy's page in it reaches the page as the page's title,
// said by a key the GUI translates, and not as markup.
func TestFailSaysAProxyPageByItsTitle(t *testing.T) {
w := httptest.NewRecorder()
fail(w, errors.New("remote magpie answered 524: "+cf524))
var got map[string]string
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
what := "remote magpie answered 524: magpie.example.com | 524: A timeout occurred"
if got["what"] != what || got["said"] != "A proxy in front of magpie answered, not magpie ({what})" {
t.Fatalf("got %v", got)
}
if strings.Contains(got["error"], "<") {
t.Fatalf("markup in the error: %q", got["error"])
}
// no page: the text stays as it was
w = httptest.NewRecorder()
fail(w, errors.New("a < b and <b>bold</b>"))
got = nil
_ = json.Unmarshal(w.Body.Bytes(), &got)
if got["error"] != "a < b and <b>bold</b>" || got["said"] != "" {
t.Fatalf("plain text changed: %v", got)
}
}