package gui
import (
"slices"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// trayCards are three cards as the Usage page has them: Claude and Codex
// with a 5-hour and a weekly window, and a plan with a balance.
func trayCards() []provider.SubscriptionQuota {
week, five := 7*24*time.Hour, 5*time.Hour
return []provider.SubscriptionQuota{
{Provider: "claude", Name: "Claude Code", Icon: "claude-color", User: "a@b.c", Windows: []provider.QuotaWindow{
{Name: "Weekly", Used: 17.6, Span: week}, {Name: "5-hour", Used: 42.2, Span: five}}},
{Provider: "codex", Name: "Codex", Icon: "codex-color", User: "x@y.z", Windows: []provider.QuotaWindow{
{Name: "5-hour", Used: 8, Span: five}, {Name: "Weekly", Used: 100, Span: week}}},
{Provider: "copilot", Name: "Copilot", Icon: "githubcopilot", Windows: []provider.QuotaWindow{
{Name: "Premium", Used: 63}}},
{Provider: "deepseek", Name: "DeepSeek", Icon: "deepseek-color", Balance: "¥12.30"},
}
}
func TestTrayUsageCells(t *testing.T) {
now := time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC)
cards := trayCards()
// the ticked ones in the order ticked; one no longer there is left out
picked := trayPick(cards, []string{"codex|x@y.z", "gone|who", "claude|a@b.c"})
if len(picked) != 2 || picked[0].Provider != "codex" || picked[1].Provider != "claude" {
t.Fatalf("picked %+v", picked)
}
if got := trayPick(cards, nil); got != nil {
t.Fatalf("none ticked: %+v", got)
}
if got := trayPick(cards, []string{"claude"}); got != nil {
t.Fatalf("the vendor without the account: %+v", got)
}
for _, tc := range []struct {
ids []string
rows [][]string
label string
}{
{nil, nil, ""},
{[]string{"claude|a@b.c"}, [][]string{{"42%", "18%"}}, "42% · 18%"},
{[]string{"claude|a@b.c", "codex|x@y.z"}, [][]string{{"42%", "18%"}, {"8%", "100%"}}, "42% · 18% | 8% · 100%"},
{[]string{"claude|a@b.c", "codex|x@y.z", "copilot", "deepseek"},
[][]string{{"42%", "18%"}, {"8%", "100%"}, {"63%"}, {"¥12.30"}}, "42% · 18% | 8% · 100% | 63% | ¥12.30"},
} {
cells, label, tip := trayUsageView(trayPick(cards, tc.ids), now, false)
if label != tc.label {
t.Errorf("%v: label %q, want %q", tc.ids, label, tc.label)
}
if len(cells) != len(tc.rows) {
t.Fatalf("%v: %d cells, want %d", tc.ids, len(cells), len(tc.rows))
}
for i, c := range cells {
if !slices.Equal(c.Rows, tc.rows[i]) {
t.Errorf("%v: cell %d rows %q, want %q", tc.ids, i, c.Rows, tc.rows[i])
}
}
if n := strings.Count(tip, "\n\n"); len(tc.ids) > 0 && n != len(tc.ids)-1 {
t.Errorf("%v: tip has %d cards apart:\n%s", tc.ids, n+1, tip)
}
}
// each cell with its logo as the page draws it: its own colours, or a
// black glyph to tint; the name's letter always there to fall back on
cells, _, _ := trayUsageView(cards, now, false)
for i, want := range []struct {
mono bool
letter string
svg bool
}{{false, "C", true}, {false, "C", true}, {true, "C", true}, {false, "D", true}} {
c := cells[i]
if len(c.Icon) == 0 || c.Mono != want.mono || c.Letter != want.letter || strings.Contains(string(c.Icon[:min(len(c.Icon), 200)]), "`: ``,
``: ``,
// no arc: as it was
``: ``,
// what isn't a path's data is left alone
``: ``,
} {
if got := string(svgArcFlags([]byte(in))); got != want {
t.Errorf("%s\n got %s\nwant %s", in, got, want)
}
}
// as Codex's logo is handed to the menu bar
b, _ := trayIconFile("codex-color")
if !strings.Contains(string(b), "A 4.496 4.496 0 0 0 0 4.496") {
t.Errorf("codex's logo not spaced out: %.300s", b)
}
}