wwcraft_ on Discord asked for the WorkBuddy credits spent on the Usage page, by model, agent and session. WorkBuddy's chat stream ends with usage.credit, the per-request figure its own app shows as "Credits Consumed". The built-in and the moved plugin both hand that chunk to the gateway's chat decoder (the plugin's renamed()/kept() pass usage on as it came), so cUsage reads `credit` into Usage.Credits and the three record sites keep it on the row. A reply that says nothing has no credits, not 0. - usage: Record.Credits; Totals/Via sum Credits and count Credited; the packed row keeps it under its own flag bit (4096, in TestRowFlagsApart); the ledger CSV gains a trailing "credits" column. - GUI: the Usage ranking's line says "0.7 credits" (积分, 積分, クレジット, Credits) beside the cost, its title how many requests said it; the Sessions page's Routed line says it per route. TUI usage page and `magpie usage` tables say it muted on the same line. No new layout. Real replies on workbuddy-ai, 10-16: gemini-3.5-flash "credit":0.01; deepseek-v4.1-flash, minimax-m3, kimi-k2.6 "credit":0; glm-5.3-flash refused (14003). The tests use those bytes. Run (at b07b518e7 on origin/main63be47717, with #2511's commit): - gofmt -l clean; go vet ./... (darwin), GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean; go build: ok. - go test -timeout 45m ./... with pipefail, sandbox HOME: 63 packages ok, usage (200s), cli, tui, gui among them; agent, gateway and library timed out at load ~270 (see the #2511 commit); library ok alone, agent and gateway ok with -timeout 150m: agent ok (5529s); gateway red only in TestIntentClassifierGroup (#1813), TestMaxRPMWaitsForRoomInTheMinute (#1791) and TestMaxRPMGroupPrefersMemberWithRoom (#2520), known timing reds under load, each passing alone here -count=3 (19s). - New: TestPlanCreditsReachTheLedger (chat stream, whole reply, translated /v1/messages, free 0, none), TestPlanCreditsSummed, TestPlanCreditsPacked, TestFormatCredits, TestUsageSaysCredits, and plan-credits.test.cjs (en, zh, zh-TW, ja, de; Chromium and WebKit; 420px fit) 12/12. - Broken by hand: dropping Credits at the record site turns the gateway test red; skipping it in unpack turns the usage tests red; dropping the ranking's ledCredits turns plan-credits red in every language. - GUI reds elsewhere: see the #2511 commit (#2536, #2552, a provider-detect-draft load timeout that passes 3/3 alone). - Rebased onto origin/mainba132d68b(13 commits; chat.go, gateway.go, app.js, i18n.js among theirs): gofmt and go vet ./internal/... clean; Credits/DeepSeek/Balance/Ledger/RowFlags tests in usage, gateway, provider, cli, tui ok; plan-credits 12/12, balance-parts 4/4, i18n-keys 3/3 in both engines.
126 lines
4.4 KiB
Go
126 lines
4.4 KiB
Go
package usage
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"math"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// What a request said it cost in its plan's credits (#2512) is summed by
|
|
// model, agent and session, through the packed blocks a long history is
|
|
// kept in; a call that said nothing is not counted as credited.
|
|
func TestPlanCreditsSummed(t *testing.T) {
|
|
resetUsageTest(t)
|
|
at := Clock().Add(-time.Hour)
|
|
credit := func(v float64) *float64 { return &v }
|
|
// past a block's 64 rows, so the rows are read back from a packed one
|
|
for i := 0; i < 70; i++ {
|
|
Append(Record{Time: at.Add(time.Duration(i) * time.Second), Agent: "claude-code", Session: "s1", Provider: "workbuddy-ai", Model: "gemini-3.5-flash", Input: 9, Output: 12, Status: 200, Credits: credit(0.01)})
|
|
}
|
|
Append(Record{Time: at.Add(2 * time.Minute), Agent: "codex", Session: "s2", Provider: "workbuddy-ai", Model: "deepseek-v4.1-flash", Input: 9, Output: 1, Status: 200, Credits: credit(0)})
|
|
Append(Record{Time: at.Add(3 * time.Minute), Agent: "codex", Session: "s2", Provider: "p", Model: "m", Input: 9, Output: 1, Status: 200})
|
|
|
|
near := func(a, b float64) bool { return math.Abs(a-b) < 1e-9 }
|
|
page := QueryPage(All, Filter{}, 0, 50)
|
|
if s := page.Sum; !near(s.Credits, 0.7) || s.Credited != 71 {
|
|
t.Fatalf("sum: %v credits of %d calls", s.Credits, s.Credited)
|
|
}
|
|
byModel := map[string]Share{}
|
|
for _, s := range page.By["model"] {
|
|
byModel[s.ID] = s
|
|
}
|
|
if s := byModel["gemini-3.5-flash"]; !near(s.Credits, 0.7) || s.Credited != 70 {
|
|
t.Fatalf("gemini-3.5-flash: %v credits of %d calls", s.Credits, s.Credited)
|
|
}
|
|
if s := byModel["deepseek-v4.1-flash"]; s.Credits != 0 || s.Credited != 1 {
|
|
t.Fatalf("a free model: %v credits of %d calls", s.Credits, s.Credited)
|
|
}
|
|
if s := byModel["m"]; s.Credited != 0 {
|
|
t.Fatalf("a call that said none is credited: %+v", s)
|
|
}
|
|
for _, s := range page.By["agent"] {
|
|
if want := map[string]int{"claude-code": 70, "codex": 1}[s.ID]; s.Credited != want {
|
|
t.Fatalf("agent %s: %d credited, want %d", s.ID, s.Credited, want)
|
|
}
|
|
}
|
|
vias := Vias(time.Time{})
|
|
if v := vias["claude-code|s1"]; len(v) != 1 || !near(v[0].Credits, 0.7) || v[0].Credited != 70 {
|
|
t.Fatalf("session s1: %+v", v)
|
|
}
|
|
if v := vias["codex|s2"]; len(v) != 2 || v[0].Credited+v[1].Credited != 1 {
|
|
t.Fatalf("session s2: %+v", v)
|
|
}
|
|
if got := FormatCredits(page.Sum); got != "0.7 credits" {
|
|
t.Fatalf("FormatCredits %q", got)
|
|
}
|
|
// the CSV's credits column: the reply's figure, empty where it said none
|
|
rows, _, _ := Ledger(All, Filter{})
|
|
var b strings.Builder
|
|
if err := WriteCSV(&b, rows); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cells, err := csv.NewReader(strings.NewReader(b.String())).ReadAll()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
col := slices.Index(CSVHeader, "credits")
|
|
said := map[string]int{}
|
|
for _, c := range cells[1:] {
|
|
said[c[col]]++
|
|
}
|
|
if said["0.01"] != 70 || said["0"] != 1 || said[""] != 1 {
|
|
t.Fatalf("credits column: %v", said)
|
|
}
|
|
}
|
|
|
|
func TestFormatCredits(t *testing.T) {
|
|
for _, c := range []struct {
|
|
t Totals
|
|
want string
|
|
}{
|
|
{Totals{}, ""},
|
|
{Totals{Credited: 3}, "0 credits"},
|
|
{Totals{Credits: 0.004, Credited: 1}, "<0.01 credits"},
|
|
{Totals{Credits: 5.71, Credited: 1}, "5.71 credits"},
|
|
{Totals{Credits: 0.1 + 0.2, Credited: 2}, "0.3 credits"},
|
|
{Totals{Credits: 1234.567, Credited: 9}, "1234.57 credits"},
|
|
} {
|
|
if got := FormatCredits(c.t); got != c.want {
|
|
t.Errorf("FormatCredits(%v of %d) = %q, want %q", c.t.Credits, c.t.Credited, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A packed block, and one the block pricer rewrote, give each row's
|
|
// credits back, and only to the rows that had them.
|
|
func TestPlanCreditsPacked(t *testing.T) {
|
|
credit := 0.25
|
|
var chunk rowChunk
|
|
for i := 0; i < 80; i++ {
|
|
r := Record{Time: time.Now(), Provider: "workbuddy-ai", Model: "m", Input: 1, Output: 1, Status: 200}
|
|
if i%2 == 0 {
|
|
r.Credits = &credit
|
|
}
|
|
chunk.add(Row{Record: r}, "", int64(i), false)
|
|
}
|
|
b := newBlockPricer(nil, nil, func(Record) *catalog.Price { return nil })
|
|
defer b.release()
|
|
packed := chunk.pack()
|
|
if len(packed.Archive) == 0 {
|
|
t.Fatal("the block wasn't packed")
|
|
}
|
|
for name, c := range map[string]*rowChunk{"raw": &chunk, "unpacked": packed.unpack(), "priced": b.block(packed)} {
|
|
for i := 0; i < 80; i++ {
|
|
got := c.row(i).Credits
|
|
if i%2 == 0 && (got == nil || *got != credit) || i%2 == 1 && got != nil {
|
|
t.Fatalf("%s row %d: credits %v", name, i, got)
|
|
}
|
|
}
|
|
}
|
|
}
|