Files
magpie/internal/settings/settings_test.go
T
yetone 6aa99ef1c0 usage: costs show in RMB at a live rate, when Settings picks it (#212 L1cardo: 添加费用用人名币结算的选项,最好是能根据实时汇率来计算) — the Usage page, the tray panel, the TUI and the CLI all price a call in USD, magpie's list prices being dollars; Settings now has a Currency row (after Usage shows) offering $ USD, kept by default, and ¥ CNY, which converts every one of those at display time only — usage.Totals.Cost and the JSONL records it comes from stay in USD always, and a vendor's own balance (already in its own currency, a relay's ¥) is never touched.
The rate is internal/fx: a free, no-key endpoint (open.er-api.com/v6/latest/USD), cached in memory and in a small file beside magpie's other caches (XDG_CACHE_HOME/magpie/fxrate.json) for 12h, refetched only once that's stale, and falling back to the last rate it ever had — or a fixed 7.2 — while offline; the currency row's tooltip names the rate and when it was learned. usage.FormatCost carries the $/¥ formatting (2 decimals above $1, 3 below, whole numbers above 100) that usage_cli.go and internal/tui/pages.go used to each have their own copy of; both now read Settings' currency once per command/redraw rather than per row. internal/gui/api.go exposes the rate on /api/settings always and on /api/state only when cny is actually chosen, so usd users' hot path never touches the network for a rate nothing shows with.

Tested with new internal/fx/fx_test.go (a fake server: fetches and caches, answers from memory then from the cache file with no further calls, refetches once stale, falls back to a stale cache or Fallback when offline, ignores a reply with no CNY rate), TestFXInState in internal/gui/fx_test.go, TestFmtCostShowsChosenCurrency in internal/tui/usage_test.go, and a new internal/gui/tests/currency.test.cjs in Chromium and WebKit, English and Chinese (the Usage page's $ by default and ¥12.34×7.2 once cny is picked, the currency row's labels/state/tooltip, a real wheel scroll deep into Settings left alone by picking a currency, the choice surviving a reload). go build -tags nogui, GOOS=linux and GOOS=windows builds, and go test -tags nogui -count=1 ./... all pass clean.

A fetch that failed is not tried again for 10 minutes (the stale or fixed rate shown at once meanwhile), so offline each state() does not wait up to 4s on the network: TestGetDoesNotRetryAFailedFetchAtOnce.
2026-09-29 19:41:29 +08:00

157 lines
5.3 KiB
Go

package settings
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/redact"
)
func TestRoundTrip(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if s := Load(); s.Theme != "system" || s.Lang != "system" {
t.Fatalf("defaults: %+v", s)
}
if err := Save(Settings{Theme: "dark", Lang: "zh"}); err != nil {
t.Fatal(err)
}
if s := Load(); s.Theme != "dark" || s.Lang != "zh" {
t.Fatalf("saved: %+v", s)
}
if err := Save(Settings{Theme: "sepia"}); err == nil {
t.Fatal("bad theme accepted")
}
if Save(Settings{}) != nil || Load().Theme != "system" {
t.Fatal("empty means system")
}
// costs show in dollars unless cny is chosen
if s := Load(); s.Currency != "usd" {
t.Fatalf("default currency: %+v", s)
}
if err := Save(Settings{Currency: "cny"}); err != nil {
t.Fatal(err)
}
if s := Load(); s.Currency != "cny" {
t.Fatalf("saved currency: %+v", s)
}
if Save(Settings{Currency: "eur"}) == nil {
t.Fatal("bad currency accepted")
}
if filepath.Base(Path()) != "settings.json" {
t.Fatal(Path())
}
if Save(Settings{CodexWarmup: "week"}) != nil || Load().CodexWarmup != "week" {
t.Fatal("codex warm-up not kept")
}
if Save(Settings{CodexWarmup: "off"}) != nil || Load().CodexWarmup != "" {
t.Fatal("off is off")
}
if Save(Settings{CodexWarmup: "daily"}) == nil {
t.Fatal("bad codex warm-up accepted")
}
if Save(Settings{ClaudeWarmup: "all"}) != nil || Load().ClaudeWarmup != "all" {
t.Fatal("claude warm-up not kept")
}
if Save(Settings{ClaudeWarmup: "hourly"}) == nil {
t.Fatal("bad claude warm-up accepted")
}
// a time of day to start the 5-hour windows at, as 06:00 however given
if Save(Settings{CodexWarmAt: "6:00", ClaudeWarmAt: "21:30:00"}) != nil || Load().CodexWarmAt != "06:00" || Load().ClaudeWarmAt != "21:30" {
t.Fatalf("warm-up times not kept: %+v", Load())
}
for _, bad := range []string{"25:00", "6am", "06:60"} {
if Save(Settings{CodexWarmAt: bad}) == nil {
t.Fatalf("bad time %q accepted", bad)
}
}
if Save(Settings{}) != nil || Load().CodexWarmAt != "" {
t.Fatal("no time is off")
}
// the menu bar's usage: every 3 minutes unless told, left or used
if s := Load(); s.TrayUsageEvery != 3 || s.QuotaLeft {
t.Fatalf("tray defaults: %+v", s)
}
if Save(Settings{TrayUsageEvery: 10, QuotaLeft: true}) != nil || Load().TrayUsageEvery != 10 || !Load().QuotaLeft {
t.Fatal("tray refresh or left not kept")
}
if Save(Settings{TrayUsageEvery: 7}) == nil {
t.Fatal("bad tray refresh accepted")
}
// the user's masking rules: kept tidied, and one that doesn't compile
// said, not saved and left out (#195)
if err := Save(Settings{RedactRules: []redact.Rule{{Kind: "gw key", Prefix: " oc_sk_ "}, {Kind: "ns", Regex: `ns-[0-9a-f]{12}`}}}); err != nil {
t.Fatal(err)
}
if r := Load().RedactRules; len(r) != 2 || r[0] != (redact.Rule{Kind: "GW_KEY", Prefix: "oc_sk_"}) || r[1].Kind != "NS" {
t.Fatalf("rules not kept: %+v", r)
}
if err := Save(Settings{RedactRules: []redact.Rule{{Kind: "bad", Regex: `ns-[0-9a-f`}}}); err == nil || !strings.Contains(err.Error(), "BAD") {
t.Fatalf("bad regex: %v", err)
}
if len(Load().RedactRules) != 2 {
t.Fatal("a failed save changed the rules")
}
}
func TestMigrate(t *testing.T) {
cfg, cache := t.TempDir(), t.TempDir()
t.Setenv("XDG_CONFIG_HOME", cfg)
t.Setenv("XDG_CACHE_HOME", cache)
old := filepath.Join(cfg, "dial")
if err := os.MkdirAll(filepath.Join(old, "sub"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(old, "providers.json"), []byte("{}"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(old, "sub", "x"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
Migrate()
fi, err := os.Stat(filepath.Join(cfg, "magpie", "providers.json"))
if err != nil || fi.Mode().Perm() != 0o600 {
t.Fatalf("providers.json not copied with its mode: %v %v", fi, err)
}
if _, err := os.Stat(filepath.Join(cfg, "magpie", "sub", "x")); err != nil {
t.Fatal("nested file not copied")
}
if _, err := os.Stat(old); err != nil {
t.Fatal("the old folder must stay")
}
if _, err := os.Stat(filepath.Join(cache, "magpie")); err == nil {
t.Fatal("no old cache, so no new one")
}
// a second run must not touch an existing folder
if err := os.WriteFile(filepath.Join(cfg, "magpie", "providers.json"), []byte("new"), 0o600); err != nil {
t.Fatal(err)
}
Migrate()
if b, _ := os.ReadFile(filepath.Join(cfg, "magpie", "providers.json")); string(b) != "new" {
t.Fatal("existing folder overwritten")
}
}
func TestArrange(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
s := Settings{AgentOrder: []string{"codex", "gone", "claude", "codex"}, AgentsHidden: []string{"crush"}}
if err := Save(s); err != nil {
t.Fatal(err)
}
s = Load()
if strings.Join(s.AgentOrder, ",") != "codex,gone,claude" {
t.Fatalf("order kept as %v", s.AgentOrder)
}
// an agent the order doesn't name follows, in the order it came
shown, hidden := Arrange(s, []string{"claude", "gemini", "crush", "codex", "pi"}, func(x string) string { return x })
if strings.Join(shown, ",") != "codex,claude,gemini,pi" || strings.Join(hidden, ",") != "crush" {
t.Fatalf("arranged %v, hidden %v", shown, hidden)
}
// the rest of the settings leave it as it is
s.Theme = "dark"
if Save(s) != nil || len(Load().AgentsHidden) != 1 {
t.Fatal("arrangement lost")
}
}