Files
magpie/internal/provider/account_settings_test.go
T
yetone 8042d1bf3b provider: a signed-in account's settings go through one list, so Save keeps what withPicks reads (#4738)
infinitex233 (#4738, Discord): `magpie provider set codex
additional-tools=on` said "saved" and changed nothing. Save rebuilt a
signed-in account's entry from one field list and withPicks read it back
from another; AdditionalTools was on neither.

The two lists had drifted in both directions, so the same class hit
other settings on every subscription account (Codex, Copilot, ...):
- written, never read back: QuotaRest, QuotaResetAt, RestCap. The gateway
  never saw them on an account, and the next Save of anything cleared
  them, since Save starts from Find.
- read back, never written: ExtraBody and ReplyHeaders. A Codex
  account's extraBody (magpie-releases#18), written by hand, was wiped
  by the next editor Save.
- on neither: AdditionalTools, LimitRestOff, Patience.

Fix: accountSettings is the one list. withPicks is it, and Save's account
branch builds {ID, Key, Hidden, Quiet, Tucked} and runs it, so a field
added to one is in both.

Run:
- New TestAccountSettingsKept (codex and copilot accounts): sets
  AdditionalTools, ExtraBody, ReplyHeaders, QuotaRest, RestCap,
  LimitRestOff and Patience, Save, Find, then clears them. Red on the
  old provider.go (every field lost on both accounts), green now.
  TestAccountPriceRateKept still green.
- Real CLI path in a sandbox HOME on mote: `magpie provider set codex
  additional-tools=on` wrote "additionalTools": true to providers.json
  and the running gateway used it (see the next commit for the backend).
- Suite record for this and the next commit together is in the next
  commit's body.
2026-11-01 14:50:45 +08:00

66 lines
2.2 KiB
Go

package provider
import (
"encoding/json"
"maps"
"slices"
"testing"
)
// TestAccountSettingsKept: what the user sets on a signed-in account's
// provider is written by Save and read back by Find, every setting alike.
// Save and withPicks kept two lists, and a setting on one alone was lost
// (#4738: `magpie provider set codex additional-tools=on` was on neither,
// so the setting seemed taken and nothing changed; an account's extraBody
// and reply headers were read back but never written; its rests were
// written but never read back, so the next Save cleared them).
func TestAccountSettingsKept(t *testing.T) {
signIn(t)
for _, id := range []string{"codex", "copilot"} {
p, err := Find(id)
if err != nil {
t.Fatal(err)
}
q := *p
q.AdditionalTools = true
q.ExtraBody = ExtraBody{"service_tier": json.RawMessage(`"priority"`)}
q.ReplyHeaders = []string{"X-Trace-Id"}
q.QuotaRest = 90
q.RestCap = 20
q.LimitRestOff = true
q.Patience = 30
if err := Save(q); err != nil {
t.Fatal(err)
}
got, err := Find(id)
if err != nil {
t.Fatal(err)
}
if got.Account == nil {
t.Fatalf("%s: no longer the signed-in account: %+v", id, got)
}
if !got.AdditionalTools {
t.Errorf("%s: additional-tools not kept", id)
}
if !maps.EqualFunc(got.ExtraBody, q.ExtraBody, func(a, b json.RawMessage) bool { return string(a) == string(b) }) {
t.Errorf("%s: extraBody %v, want %v", id, got.ExtraBody, q.ExtraBody)
}
if !slices.Equal(got.ReplyHeaders, q.ReplyHeaders) {
t.Errorf("%s: reply headers %v, want %v", id, got.ReplyHeaders, q.ReplyHeaders)
}
if got.QuotaRest != 90 || got.RestCap != 20 || !got.LimitRestOff || got.Patience != 30 {
t.Errorf("%s: rests %d %d %v, patience %d", id, got.QuotaRest, got.RestCap, got.LimitRestOff, got.Patience)
}
// set back, each is the default again
r := *got
r.AdditionalTools, r.ExtraBody, r.ReplyHeaders = false, nil, nil
r.QuotaRest, r.RestCap, r.LimitRestOff, r.Patience = 0, 0, false, 0
if err := Save(r); err != nil {
t.Fatal(err)
}
if got, _ := Find(id); got.AdditionalTools || len(got.ExtraBody) > 0 || len(got.ReplyHeaders) > 0 || got.QuotaRest != 0 || got.RestCap != 0 || got.LimitRestOff || got.Patience != 0 {
t.Errorf("%s: left after clearing: %+v", id, got)
}
}
}