Files
magpie/internal/gui/search_row_archive_test.go
T
yetone 73c98ebc20 gateway: a search API's or plan service's ask is routed and archived (axelburks)
axelburks on Discord: when magpie searches for a model through a search
API, the Routing page doesn't say which API answered, and Usage →
Requests lists the row but has no request archive, so the request and
response sent to the API can't be seen.

A model's search (searchWith) was already routed and archived. A search
API's ask (askSearchAPIKeys, a plugin's search hook, a signed-in search
API) and a plan's own search service (Kimi Code, GLM Coding Plan,
WorkBuddy, Codex's searchBy, and searchBy's Kimi branch, which wrote no
row at all before) only wrote a usage row.

Now every ask that writes a row goes through keepSearch
(search_archive.go): a done web_search route, agent magpie, For the
agent searched for, the API's name as model and its host as provider,
seated on the masked key or account; a Recent call with ring-cut
bodies; and with the request archive on, archiveOwn of the exchange as
sent. The row gets RouteID and Archive, which the Requests tab already
opens. Each ask has a searchWire in its context, filled where it is sent
(tapSearch tees the answer as read; the last exchange stands, so GLM's
MCP tools/call is the one kept). A plugin's search is noted as what
magpie handed the hook (key masked) and what came back (PLUGIN method).
The key is replaced by MaskSearchKey in path, query, headers and both
bodies before anything is kept; the archive's own scrub runs on top.
Bodies are kept up to RequestArchiveMaxMB (longer marked cut); archive
off keeps nothing on disk, the route and Recent call stay.

Run (mote's disk filled mid-work, see below; the rest on WSL
magpie-desktop via wsl-run):
- mote-run: go test -run 'Search|Archive' ./internal/gateway ok; GUI
  usage-ledger-search, routing-side-calls, request-archive-usage in
  chromium+webkit, every language and width: 38 pass.
- wsl-run: go test -run 'Search|Archive' ./internal/gateway ok; go vet
  ./internal/gateway ok; GOOS=windows go vet ./internal/gateway
  ./internal/gui ok; GOOS=darwin CGO_ENABLED=0 go vet ./internal/gateway
  ok; GUI usage-ledger-search, routing-side-calls, request-archive-usage,
  search-instead, search-mcp in both engines: 68 pass, 0 fail.
- gofmt -l clean.

Broken by hand: keepSearch's call commented out turns all five new
gateway tests red; taken()'s key masking turned off turns
TestSearchAPIAskIsRoutedAndArchived and TestSearchAPIKeyInQueryNotArchived
red. TestSearchRowKeepsRouteAndArchive (internal/gui) is a guard that the
row's RouteID and Archive reach /api/usage/requests; it passed on the
old code too.

Not tried: real Tavily/Brave/Kimi/GLM/WorkBuddy/Codex accounts (httptest
stubs only); Linux go vet of internal/gui (WSL lacks gtk4/webkitgtk);
web_fetch (search_fetch.go) and a vendor's own search (search_native.go),
which magpie doesn't send, are unchanged. Full suite runs after the push.
No GUI asset, API call or i18n string changed.
2026-11-05 12:40:35 +08:00

35 lines
1.4 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/yetone/magpie/internal/usage"
)
// A search magpie asked a search API for (axelburks on Discord: 用量-请求
// 页面 能看到请求记录 但是无请求存档) is a row of the Requests page with the
// route the Routing page lists it under and the archive that keeps its
// request and answer, which the row's detail opens (View routing, the
// archive panel), though it rides the probes' lane.
func TestSearchRowKeepsRouteAndArchive(t *testing.T) {
sandboxHome(t)
mux := http.NewServeMux()
usageRoutes(mux, folderOnly{})
usage.Append(usage.Record{Time: time.Now(), Agent: "claude", Kind: "web_search", Source: usage.SourceSearch, Requested: "Tavily",
Host: "api.tavily.com", ProviderKeyName: "tvly…ab12", Status: 200, Millis: 800, RouteID: 1793851649094, Archive: "2026-11-05/040654-a02c8d68dfe256d8"})
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/usage/requests?period=all", nil))
var ledger ledgerJSON
if err := json.Unmarshal(w.Body.Bytes(), &ledger); err != nil || w.Code != 200 {
t.Fatalf("%d %v %s", w.Code, err, w.Body)
}
if len(ledger.Rows) != 1 || ledger.Rows[0].Source != usage.SourceSearch || ledger.Rows[0].RouteID != 1793851649094 ||
ledger.Rows[0].Archive != "2026-11-05/040654-a02c8d68dfe256d8" {
t.Fatalf("rows %s", w.Body)
}
}