Something went wrong. Try again.
A KDE Plasma panel widget showing how much Claude Max quota is left, at a glance.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213package state
import ( "encoding/json" "errors" "os" "path/filepath" "testing" "time"
"github.com/claudioquaglia/dipstick/internal/anthropic")
func fixtureUsage(t *testing.T) *anthropic.Usage { t.Helper() raw, err := os.ReadFile("../anthropic/testdata/usage.json") if err != nil { t.Fatal(err) } u, err := anthropic.ParseUsage(raw) if err != nil { t.Fatal(err) } return u}
func TestFromUsageRealShape(t *testing.T) { now := time.Date(2026, 8, 27, 12, 0, 0, 0, time.UTC) s := FromUsage(fixtureUsage(t), now)
if s.Schema != SchemaVersion || s.Quota.Status != StatusFresh { t.Errorf("schema=%d status=%q", s.Schema, s.Quota.Status) } if len(s.Windows) != 3 { t.Errorf("windows = %d, want 3 (scoped ones stay for the popup)", len(s.Windows)) } // Week at 30 against session at 12: the week goes in the panel. if s.Compact == nil || s.Compact.Primary.Kind != anthropic.KindWeeklyAll { t.Fatalf("compact.primary = %+v", s.Compact) } if s.Compact.Primary.Percent != 30 { t.Errorf("primary.percent = %d, want 30", s.Compact.Primary.Percent) } if s.Compact.Secondary == nil || s.Compact.Secondary.Percent != 12 { t.Errorf("secondary = %+v, want the session at 12", s.Compact.Secondary) }}
func TestPickCompactPrefersTheTighterWindow(t *testing.T) { cases := []struct { name string session, weekly int wantKind string wantPrimaryPct int }{ {"session higher", 80, 30, anthropic.KindSession, 80}, {"week higher", 10, 55, anthropic.KindWeeklyAll, 55}, // On a tie the week wins: the session refills within hours, the week // does not. {"tie", 40, 40, anthropic.KindWeeklyAll, 40}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { c := pickCompact([]Window{ {Kind: anthropic.KindSession, Percent: tc.session}, {Kind: anthropic.KindWeeklyAll, Percent: tc.weekly}, }) if c == nil || c.Primary.Kind != tc.wantKind || c.Primary.Percent != tc.wantPrimaryPct { t.Fatalf("primary = %+v, want %s@%d", c, tc.wantKind, tc.wantPrimaryPct) } if c.Secondary == nil { t.Error("secondary missing: the popup loses the other window") } }) }}
// Per-model windows must never end up in the panel: a high scoped window does// not block, you switch model.func TestPickCompactIgnoresScopedWindows(t *testing.T) { c := pickCompact([]Window{ {Kind: anthropic.KindWeeklyScoped, Percent: 99, Model: "Fable"}, {Kind: anthropic.KindSession, Percent: 10}, }) if c == nil || c.Primary.Kind != anthropic.KindSession { t.Fatalf("primary = %+v, want the session", c) } if c.Secondary != nil { t.Errorf("secondary = %+v, want nil without weekly_all", c.Secondary) }}
func TestPickCompactNoWindows(t *testing.T) { if c := pickCompact(nil); c != nil { t.Errorf("compact = %+v, want nil so a dash gets drawn", c) }}
func TestUnavailableCarriesTheReason(t *testing.T) { now := time.Now() s := Unavailable(anthropic.ErrNoAuth, true, now) if s.Quota.Status != StatusUnavailable || !s.Quota.NeedsAuth { t.Errorf("quota = %+v", s.Quota) } if s.Compact != nil { t.Error("compact must be nil: with no data we do not show a zero") } if s.Quota.Error == "" { t.Error("error lost: the plasmoid cannot explain what is wrong") }}
// The schema is the contract with the plasmoid: if the field names change,// QML stops reading. This test makes that a deliberate change.func TestJSONFieldNames(t *testing.T) { now := time.Date(2026, 8, 27, 12, 0, 0, 0, time.UTC) raw, err := FromUsage(fixtureUsage(t), now).Marshal() if err != nil { t.Fatal(err) } var m map[string]json.RawMessage if err := json.Unmarshal(raw, &m); err != nil { t.Fatal(err) } for _, k := range []string{"schema", "generated_at", "quota", "compact", "windows", "extra_usage_enabled"} { if _, ok := m[k]; !ok { t.Errorf("missing top-level field: %q", k) } } var c struct { Primary struct { Kind string `json:"kind"` Percent *int `json:"percent"` Severity string `json:"severity"` } `json:"primary"` Secondary *struct { Percent *int `json:"percent"` } `json:"secondary"` } if err := json.Unmarshal(m["compact"], &c); err != nil { t.Fatal(err) } if c.Primary.Kind == "" || c.Primary.Percent == nil || c.Secondary == nil { t.Errorf("incomplete compact: %+v", c) }}
// The state file is read by a process that does not coordinate with the// writer: it must never see a partial document, and it must be readable.func TestWriteIsAtomicAndReadable(t *testing.T) { dir := t.TempDir() t.Setenv("XDG_CONFIG_HOME", dir)
st := FromUsage(fixtureUsage(t), time.Now()) if err := st.Write(); err != nil { t.Fatal(err) } path, _ := Path()
info, err := os.Stat(path) if err != nil { t.Fatal(err) } if perm := info.Mode().Perm(); perm != 0o644 { t.Errorf("permissions = %o, want 644", perm) }
raw, err := os.ReadFile(path) if err != nil { t.Fatal(err) } var back State if err := json.Unmarshal(raw, &back); err != nil { t.Fatalf("what was written is not valid JSON: %v", err) } if back.Schema != SchemaVersion || back.Compact == nil { t.Errorf("round trip lost data: %+v", back) }
// No temporary file may survive: the plasmoid globs nothing, but a stray // half-written state.json look-alike is still litter in the user's config. entries, _ := os.ReadDir(filepath.Dir(path)) for _, e := range entries { if e.Name() != "state.json" { t.Errorf("leftover file after the write: %s", e.Name()) } }}
// Rewriting must leave exactly one file, whatever the previous content was.func TestWriteOverwritesCleanly(t *testing.T) { dir := t.TempDir() t.Setenv("XDG_CONFIG_HOME", dir) now := time.Now()
if err := FromUsage(fixtureUsage(t), now).Write(); err != nil { t.Fatal(err) } if err := Unavailable(errors.New("boom"), true, now).Write(); err != nil { t.Fatal(err) }
path, _ := Path() raw, _ := os.ReadFile(path) var back State if err := json.Unmarshal(raw, &back); err != nil { t.Fatal(err) } if back.Quota.Status != StatusUnavailable || back.Compact != nil { t.Errorf("the second write did not replace the first: %+v", back.Quota) }}