Files
magpie/internal/gui/test_stop_test.go
T
yetone b5aa56e42d providers: a model test fails fast on an unreachable host and can be stopped (RIck)
RIck (Discord #feedback): after pressing 测试模型, the error took too long
to show, and the test couldn't be cancelled.

Fail fast. Each probe now has 10s (connectWait) to get a connection,
traced through httptrace's GotConn. If it doesn't get one, the error says
"couldn't connect to <host> in 10s". In the past a refused, unresolved or
silent host waited out the whole 20s probe, or TLS's own timeout, once for
every model. TestModels keeps the hosts it couldn't reach for the run
(withUnreached). Once one model on a host fails to connect, the rest of
that host's models fail at once with the same error, so they don't each
wait 10s in turn, four at a time. The 20s per-probe bound and the GUI's
3m/25s bounds are unchanged. A stream that breaks off before the model
answers is now a failure ("the stream broke off before the model
answered"), not a long wait. Plugin and account transports don't get the
connect bound, since they dial for themselves.

Stop. While a provider has a test running, its Test models button (the
model editor), its endpoints' Test, and a chip's own right-click test all
read Stop (停止 / 停止 / 停止 / Stoppen). Pressing Stop aborts the fetch
and posts provider/teststop with the run's token. The handler keeps each
run's cancel in testRuns, so the vendor's request ends on the server too.
This matters on WebView2, where a fetch abort never reaches the request's
context. The status line then reads "Test stopped". Every entry point goes
through one helper (provTest, testButton), so they behave the same.

app.js fits its budget with no raise. The helpers are compact, and three
comments in the editor's Fetch models were cut down.

Tests:
- TestTestModelsUnreachedHostFailsOnce: a listener that accepts and never
  speaks TLS, connectWait set to 200ms and 8 models. Every model's error
  must be "couldn't connect", and the listener may get at most 4
  connections.
- TestStreamAnswerBrokenOff.
- TestProviderTestStops (models and endpoints): a fake vendor holds the
  request until its context ends. The test waits for the request to
  arrive, posts teststop, and waits for the reply to say canceled and for
  the vendor's request to end. It waits on that state, not on a duration.
- model-test-stop.test.cjs: all three buttons read Stop, teststop is
  posted with the run, the labels come back, the status reads Test
  stopped, there is no left-border accent, and nothing overflows at
  560px. It runs in Chromium and WebKit, in en, zh, zh-TW, ja and de.

Broken by hand (each one went red, then the fix was restored):
- the GUI test against origin/main's app.js;
- teststop's post removed ("Stop posted no teststop");
- teststop not cancelling ("the test went on after Stop");
- dead.Store removed (8 connections);
- the connecting wrap removed (TLS handshake timeout after 10s).

Run:
- At the pushed head:
  - mote: TestCoreScriptBudget, TestProviderTestStops, the i18n Go tests,
    TestTestModelsUnreachedHostFailsOnce and TestStreamAnswerBrokenOff:
    ok.
  - wsl-run (load 179): model-test-stop 10/10 and i18n-keys 4/4.
  - model-test-one: webkit zh was red, "model-c moved on its right-click",
    445.37 vs 444.89. origin/main's app.js was red the same way 3 of 6
    times, in en and zh. That file is on #4218.
  - provider-typed: 4 red (locator.fill 5s); see #4461.
- On the change before its last rebase:
  - gofmt clean. GOOS=windows go vet and GOOS=linux -tags nogui go vet:
    ok.
  - Full Go suite on mote (-timeout 45m, tee'd): 79 packages ok.
    internal/gui failed only on the ten AppKit window tests #4125 names.
  - Whole GUI suite on Linux WSL (both engines, every language): 15,767
    tests, 502 red across about 125 files, at load 160 to 175. The 8
    provider-editor files gave the same pass and fail set at fresh
    origin/main ccbefe2a2. The reds are on the WSL issues; the 12 files
    no issue named are filed as #4461.

Not tried:
- Stop in the real Windows app (WebView2).
- A real vendor, or a real unreachable host outside the tests.
- Plugin and account transports (no connect bound for them).
- The Fetch models step inside the endpoints' test.
- The CLI's test path and Detect, which get the connect bound through
  probeOnce but weren't exercised.
- The macOS GUI suite, since mote's WebKit is broken (#4125).
2026-10-31 00:08:29 +08:00

82 lines
2.7 KiB
Go

package gui
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// RIck on Discord: a test couldn't be stopped. The window's Stop ends a
// test by its run name (teststop), and the request to the vendor with it,
// though the test's own request isn't ended: WebView2 doesn't end it when
// the page aborts its fetch, and httptest's request here never ends.
func TestProviderTestStops(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
arrived, ended := make(chan struct{}, 8), make(chan struct{}, 8)
up := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { // the endpoint test's Fetch
_, _ = rw.Write([]byte(`{"data":[{"id":"m1"}]}`))
return
}
// read whole, or the server never notices the client went
// away and the context below never ends
_, _ = io.Copy(io.Discard, r.Body)
arrived <- struct{}{}
<-r.Context().Done() // a vendor that never answers
ended <- struct{}{}
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Chat: up.URL + "/v1", Key: "sk-slow", KeyProtocol: provider.Chat, Icon: "generic"}); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(action, body string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/"+action, strings.NewReader(body)))
return w
}
for _, c := range []struct{ name, body string }{
{"models", `{"id":"slow","test":["m1"],"run":"r-models"}`},
{"endpoints", `{"id":"slow","run":"r-endpoints"}`},
} {
t.Run(c.name, func(t *testing.T) {
got := make(chan *httptest.ResponseRecorder, 1)
go func() { got <- post("test", c.body) }()
select {
case <-arrived:
case w := <-got:
t.Fatalf("the vendor was never asked: %d %s", w.Code, w.Body)
}
var run struct{ Run string }
_ = json.Unmarshal([]byte(c.body), &run)
if w := post("teststop", `{"run":"`+run.Run+`"}`); w.Code != http.StatusNoContent {
t.Fatalf("teststop: %d %s", w.Code, w.Body)
}
select {
case w := <-got:
if w.Code != 200 || !strings.Contains(w.Body.String(), "canceled") {
t.Errorf("the stopped test answered %d %s", w.Code, w.Body)
}
case <-time.After(15 * time.Second): // the probe's own wait is 20s
t.Fatal("the test went on after Stop")
}
select { // the request to the vendor ended with the test
case <-ended:
case <-time.After(15 * time.Second):
t.Fatal("the vendor's request went on after Stop")
}
})
}
}