neiko and gys619 on Discord: a provider's Test or Detect APIs left no
trace in Usage > Requests, so a failing test couldn't be read back.
The provider package marks the context of the editor's Test, Detect APIs
(and per-model detection) and a model list's refresh with a kind
(internal/probelog). Each request sent under the mark is written once:
by the provider's Do, or by probelog.Transport under http.DefaultClient
(netproxy.Install), whichever sends it first; the Claude Code run and
the gateway's translated test write theirs from their result. A row
keeps time, provider, account, model, host, path (no query, no
user:password), method, status, ms and the first 500 bytes of the error.
The row is usage.SourceProbe, agent "magpie", Kind provider_test /
api_detect / model_list ("Test", "Detect APIs", "Model list" in Called
for; purposes.js regenerated). The log index keeps probes apart
(logSnapshot.probes), so Visit, Load, Summarize, budgets, Vias, heatmap,
shared days, LastSeen and observers never meet one. The request page
lists them (probeChunk) and counts none: Row.IsProbe, since a packed row
keeps the row's Source and not its record's. The page shows a muted
badge "magpie's own · not counted in any total" (en, zh, zh-TW, ja, de);
no new control, no layout change.
Tests, each red on the old code:
- usage TestProbeRowsListedNotCounted: listed, clipped, and absent from
Sum, Through, every By group, Summarize, Load, LastSeen, OwnDays and
the observer; a provider filter lists it and sums 0.
- provider TestTestWritesItsRequestsDown / TestDetectWritesItsRequestsDown:
every request the fake server saw is exactly one row, with the right
kind, model and 401 text; an unmarked Do writes nothing.
- probelog TestNoteKeepsNoSecret, TestTransportOnce.
- GUI usage-ledger-probe: badge per language, both engines, 1180/420,
light and dark, no left border, no sideways scroll.
- gui TestProviderDetectDraft now expects the log line (source probe,
kind api_detect, no key) and leaves usage.jsonl out of the unchanged
config comparison.
Broken by hand, each went red: request_page's uncounted check,
log_index's probe skip, Append's observer skip, Do's Taken (6 rows for
4 requests), Test's and Detect's marks, probelog's query strip,
page.js's badge (11 GUI fails).
Run at this head (on origin/main 22a7f86c9): gofmt -l clean; go vet
./..., GOOS=linux -tags nogui and GOOS=windows clean; go test -count=1
usage, probelog, netproxy, usageapi, provider and gui packages ok (the
rebase over 22a7f86c9 applied cleanly; upstream's 8b460833e chunk
cache leaves probeChunk, built per query, alone).
Run one rebase earlier (on origin/main 0b72ccbef; request_page.go
merged by hand onto 7d67910f7/66b6d6ade's per-row Totals, with
uncounted rows skipping the caller and account groups too):
- go test -tags nogui -timeout 45m ./... in a sandbox HOME, log tee'd:
every package ok (gateway 640s, agent 514s, provider 278s, usage 77s)
but internal/gui's TestRestartHandsTheGatewayOver (7 of 8 requests
answered during the restart), 3 of 3 green alone after (#3451).
- gofmt -l clean; go vet ./..., GOOS=linux -tags nogui and GOOS=windows
go vet ./... clean.
- GUI usage-ledger-probe, usage-ledger-reported, i18n-keys,
purpose-filter, quota-pool-model: 72/72. usage-ledger's "newest live
route needs no return-to-live button" is red on origin/main 0b72ccbef
alone too (#3104).
- Whole GUI suite, both engines, at the pre-rebase base 761904b5a:
1313 tests; reds, each also red on origin/main 7b1faf99f alone:
account-discovery "agents' sign-ins read" (#3497), builtins
"Plugins > Built-in" (Masking page in the list, #3518 filed), and
context-map "two tiles overlap" chromium ja 420 (#3488; 1 of 2 red on
main alone, 3 of 3 green on this branch alone). Earlier package run:
TestRestartHandsTheGatewayOver red under load, passes alone (#3451).
- No size budget raised.
Not tried: no real vendor account (fake servers only); a plugin's model
list, fetched in the JS plugin host, isn't written down; the real GUI
app wasn't launched (Playwright only).
214 lines
7.0 KiB
Go
214 lines
7.0 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// #1545 (thedavidweng): Detect APIs and Each picked model ask with the
|
|
// form as it stands, not saved: a new provider's typed key and the models
|
|
// typed in its Models field, a saved one's picks not saved yet with its
|
|
// saved key when none is typed, and a copy not saved yet with the key of
|
|
// the one it copies. None of it writes anything: providers.json and the
|
|
// rest of the config folder are as they were, and no provider is added.
|
|
func TestProviderDetectDraft(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())
|
|
var mu sync.Mutex
|
|
var asked []string
|
|
// a relay with no model list (its /v1/models is a 404) that chats only
|
|
// for the model typed in the form, "draft-m", and the keys it knows
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
var body struct{ Model string }
|
|
json.Unmarshal(b, &body)
|
|
auth := r.Header.Get("Authorization")
|
|
mu.Lock()
|
|
asked = append(asked, r.Method+" "+r.URL.Path+" "+body.Model+" "+auth)
|
|
mu.Unlock()
|
|
if r.URL.Path != "/v1/chat/completions" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if auth != "Bearer sk-typed" && auth != "Bearer sk-saved" {
|
|
http.Error(w, `{"error":{"message":"invalid api key"}}`, http.StatusUnauthorized)
|
|
return
|
|
}
|
|
if body.Model != "draft-m" {
|
|
http.Error(w, `{"error":{"message":"model not found"}}`, http.StatusNotFound)
|
|
return
|
|
}
|
|
io.WriteString(w, `{"choices":[{"message":{"content":"hi"}}]}`)
|
|
}))
|
|
defer up.Close()
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
post := func(h http.Handler, action, body string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/"+action, strings.NewReader(body)))
|
|
return w
|
|
}
|
|
type answer struct {
|
|
Results []provider.Detection
|
|
Models []provider.ModelDetection
|
|
}
|
|
detect := func(body string) answer {
|
|
t.Helper()
|
|
w := post(mux, "detect", body)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d %s", body, w.Code, w.Body)
|
|
}
|
|
var got answer
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return got
|
|
}
|
|
chat := func(rs []provider.Detection) provider.Detection {
|
|
for _, r := range rs {
|
|
if r.Protocol == provider.Chat {
|
|
return r
|
|
}
|
|
}
|
|
t.Fatalf("no chat in %+v", rs)
|
|
return provider.Detection{}
|
|
}
|
|
keys := func() []string {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
var out []string
|
|
for _, a := range asked {
|
|
if strings.Contains(a, "/chat/completions") {
|
|
out = append(out, a[strings.LastIndex(a, " ")+1:])
|
|
}
|
|
}
|
|
asked = nil
|
|
return out
|
|
}
|
|
base := up.URL + "/v1"
|
|
|
|
// a new provider: the key just typed and the models typed in its form
|
|
if r := chat(detect(`{"typed":true,"key":"sk-typed","base":"` + base + `","model":"","picked":["draft-m","other"]}`).Results); !r.OK || r.Model != "draft-m" {
|
|
t.Fatalf("new, models typed: %+v", r)
|
|
}
|
|
if got := keys(); len(got) != 1 || got[0] != "sk-typed" {
|
|
t.Fatalf("new asked with %v", got)
|
|
}
|
|
got := detect(`{"typed":true,"key":"sk-typed","base":"` + base + `","detectModels":["draft-m","gone"]}`)
|
|
if len(got.Models) != 2 || !chat(got.Models[0].Results).OK || chat(got.Models[1].Results).OK {
|
|
t.Fatalf("new, each model: %+v", got.Models)
|
|
}
|
|
keys()
|
|
if ps := provider.All(); len(ps) != 0 {
|
|
t.Fatalf("a detection added %v", ps)
|
|
}
|
|
|
|
// a saved provider, its key shown masked and not typed again, with a
|
|
// model picked in the form and not saved
|
|
if w := post(mux, "save", `{"id":"relay","name":"Relay","key":"sk-saved","chat":"`+base+`","models":["saved-m"],"new":true}`); w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
keys()
|
|
before := draftFiles(t, appdir.Config())
|
|
if _, ok := before["providers.json"]; !ok {
|
|
t.Fatalf("no providers.json in %v", before)
|
|
}
|
|
if r := chat(detect(`{"id":"relay","typed":true,"key":"","base":"` + base + `","model":"","picked":["draft-m"]}`).Results); !r.OK || r.Model != "draft-m" {
|
|
t.Fatalf("saved, a pick not saved: %+v", r)
|
|
}
|
|
if got := keys(); len(got) != 1 || got[0] != "sk-saved" {
|
|
t.Fatalf("saved asked with %v", got)
|
|
}
|
|
// a copy of it, not saved yet, with no key typed: the copied one's key
|
|
if r := chat(detect(`{"new":true,"copyOf":"relay","typed":true,"key":"","base":"` + base + `","model":"","picked":["draft-m"]}`).Results); !r.OK {
|
|
t.Fatalf("copy: %+v", r)
|
|
}
|
|
if got := keys(); len(got) != 1 || got[0] != "sk-saved" {
|
|
t.Fatalf("copy asked with %v", got)
|
|
}
|
|
// a key typed over the saved one is the one asked with
|
|
if r := chat(detect(`{"new":true,"copyOf":"relay","typed":true,"key":"sk-typed","base":"` + base + `","model":"draft-m"}`).Results); !r.OK {
|
|
t.Fatalf("copy, key typed: %+v", r)
|
|
}
|
|
if got := keys(); len(got) != 1 || got[0] != "sk-typed" {
|
|
t.Fatalf("copy with a key typed asked with %v", got)
|
|
}
|
|
// the request log is the one file a detection writes: each request it
|
|
// sent, as magpie's own (usage.SourceProbe), and nothing else changes
|
|
after := draftFiles(t, appdir.Config())
|
|
logBefore, logAfter := before["usage.jsonl"], after["usage.jsonl"]
|
|
delete(before, "usage.jsonl")
|
|
delete(after, "usage.jsonl")
|
|
if !draftSame(before, after) {
|
|
t.Fatalf("a detection wrote to the config folder:\nbefore %v\nafter %v", draftNames(before), draftNames(after))
|
|
}
|
|
if added := strings.TrimPrefix(logAfter, logBefore); added == logAfter || !strings.Contains(added, `"source":"probe"`) ||
|
|
!strings.Contains(added, `"kind":"api_detect"`) || strings.Contains(added, "sk-") {
|
|
t.Fatalf("the request log has no detection of its own:\n%s", added)
|
|
}
|
|
if ps := provider.All(); len(ps) != 1 || ps[0].ID != "relay" || len(ps[0].Models) != 1 {
|
|
t.Fatalf("providers now %+v", ps)
|
|
}
|
|
|
|
// magpie web: without its key a draft probe is turned away, and the
|
|
// vendor is never asked
|
|
guarded := webHandler("3999", "0123456789abcdef-1545", time.Hour, mux)
|
|
if w := post(guarded, "detect", `{"typed":true,"key":"sk-typed","base":"`+base+`","picked":["draft-m"]}`); w.Code != http.StatusUnauthorized {
|
|
t.Fatalf("no web key: %d %s", w.Code, w.Body)
|
|
}
|
|
if got := keys(); len(got) != 0 {
|
|
t.Fatalf("asked without the web key: %v", got)
|
|
}
|
|
}
|
|
|
|
// draftFiles is every file under dir, by its path, with its bytes.
|
|
func draftFiles(t *testing.T, dir string) map[string]string {
|
|
t.Helper()
|
|
out := map[string]string{}
|
|
filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil || d.IsDir() {
|
|
return nil
|
|
}
|
|
b, _ := os.ReadFile(p)
|
|
rel, _ := filepath.Rel(dir, p)
|
|
out[filepath.ToSlash(rel)] = string(b)
|
|
return nil
|
|
})
|
|
return out
|
|
}
|
|
|
|
func draftSame(a, b map[string]string) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for k, v := range a {
|
|
if w, ok := b[k]; !ok || w != v {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func draftNames(m map[string]string) []string {
|
|
var out []string
|
|
for k := range m {
|
|
out = append(out, k)
|
|
}
|
|
return out
|
|
}
|