Files
magpie/internal/gui/usage_deleted_test.go
T
yetone 396e0e4638 usage: a deleted provider's requests merged into another, or named, from the Requests filter
CyberCaius on Discord: a deleted provider's requests sat under its old id
("<date> 已删除"), and the only way to put them under a provider they
have was renaming that provider onto the old id. Now, with a deleted
provider picked in Usage › Requests' Provider filter, a "…" beside it
offers Merge into… (an app menu of the providers there are now) and
Rename… (the filter's button turns into a name field; Enter or leaving
it saves, Escape keeps it). With the merged-into provider picked, the
same "…" lists Separate <name> for each, the undo.

Nothing in the log is rewritten. Both live on the deletion's own record
in providers.json (Deleted.Into/MergedAt, Deleted.Label), so a provider
added later under the old id inherits neither: it is a different
provider, and its record of its own is what it has. Renaming the target
carries the merge (Rename rewrites Into); deleting the target ends it.

Renames.Both(id, time) gives the id a call is shown under and the one it
was made to (Own). Listing, filtering, the summary and vias go by the
shown id; pricing (Record.priceID, block_price's as), window holds and
the gateway's loggedAnswers go by the own one, so a merge moves no cost
or token, only whose name they sit under. The target's face lists what
was merged into it (Named.Merged). Routes: GET /api/usage/deleted/into,
POST /api/usage/deleted {id, into?, name?}; a field not sent stays.

Run (mote, at this tree on d50f84c9f):
- go test -tags nogui -timeout 45m ./... : every package ok, exit 0.
- GOOS=linux and GOOS=windows go vet -tags nogui on usage, usageapi,
  provider, gui, gateway: ok. gofmt -l clean.
- New: TestDeletedProviderMergedIntoAnother (usage: cost 306 before and
  after the merge, 3 calls under main, coster/pricer price a relay call
  at its own price, a new "relay" keeps only its own call, rename and
  delete of the target, undo), TestDeletedProviderNamedAndMerged
  (usageapi), TestUsageDeletedProviderRoutes (gui).
- GUI: usage-ledger-deleted-merge.test.cjs, chromium and webkit, en zh
  zh-TW ja de at 1400 and 760px: ok. All 63 usage-*/ledger-* files and
  i18n-keys, both engines: 923 of 954 ok. The reds are red the same on
  origin/main c3b0484a9 run alone: usage-agents-hide (#3414),
  usage-fold (#3415), usage-ledger's "newest live route needs no
  return-to-live button" (#3104), and i18n-keys' "a plugin's words are
  its own" on library's "Runs as" (#3897).

After rebasing on origin/main 3e96197da (i18n.js merged with c3b0484a9's
balance keys): go build ./..., go test of internal/usage/...,
usageapi/..., provider/..., gui/... ok; the new GUI file in both
engines and every language ok; i18n-keys only #3897's red.

Broken by hand:
- Both returning (to, to): TestDeletedProviderMergedIntoAnother red,
  "cost 900, before 306".
- renamesOf not setting into: both Go tests red (relay still listed).
- ledProviderMore returning at once: the GUI test red (timeout waiting
  for the …), 5 of 5 subtests in chromium.
- On the old code the new tests don't compile (no MergeDeleted).

Not tried: the real app on a real providers.json with a deleted
provider; Linux/Windows GUI. gateway_sessions still prices by the raw
id, and a deleted provider's per-provider price stops applying once its
id is reused (both before this change, unchanged by it).
2026-10-28 11:15:57 +08:00

63 lines
2.2 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// The Usage page's "…" beside a deleted provider (CyberCaius on Discord):
// the providers it can be merged into, and a POST that merges, names and
// parts it, the request log left as it was.
func TestUsageDeletedProviderRoutes(t *testing.T) {
sandboxHome(t)
for _, p := range []provider.Provider{
{ID: "relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-9"}},
{ID: "main", Name: "Main", Key: "k2", Chat: "http://127.0.0.1:2/v1", Models: []string{"gpt-9"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
usage.Append(usage.Record{Time: time.Now().Add(-time.Hour), Agent: "codex", Provider: "relay", Model: "gpt-9", Input: 10, Status: 200})
if err := provider.Delete("relay"); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
usageRoutes(mux, folderOnly{})
do := func(method, path, body string, code int) string {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest(method, path, strings.NewReader(body)))
if w.Code != code {
t.Fatalf("%s %s: %d %s", method, path, w.Code, w.Body)
}
return w.Body.String()
}
var into []struct{ ID, Name string }
if err := json.Unmarshal([]byte(do("GET", "/api/usage/deleted/into", "", 200)), &into); err != nil {
t.Fatal(err)
}
if len(into) != 1 || into[0].ID != "main" || into[0].Name != "Main" {
t.Fatalf("into: %+v", into)
}
do("POST", "/api/usage/deleted", `{"id":"relay","name":"Old relay","into":"main"}`, 200)
gone, _ := provider.UsageRenames().Gone("relay")
if gone.Label != "Old relay" || gone.Into != "main" {
t.Fatalf("after the POST: %+v", gone)
}
// a field not sent stays as it is
do("POST", "/api/usage/deleted", `{"id":"relay","into":""}`, 200)
if gone, _ := provider.UsageRenames().Gone("relay"); gone.Label != "Old relay" || gone.Into != "" {
t.Fatalf("parted: %+v", gone)
}
do("POST", "/api/usage/deleted", `{"id":"relay","into":"nobody"}`, http.StatusBadRequest)
do("POST", "/api/usage/deleted", `{"id":"main","name":"x"}`, http.StatusBadRequest)
}