package thintest import ( "database/sql" "io" "log/slog" "strconv" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" "amadan.net/rastrillo/rastrillo/db" ) func tokens(n int64) *int64 { return &n } // TestPagesTaskSpend: the task page says quietly what the task used, // from its usage rows (Claude's cached input counted beside its input, // Codex's inside it), and the specialist panel has one line per step // that used any. A task with no rows says nothing, and a teammate who // can read the task sees the same. func TestPagesTaskSpend(t *testing.T) { r := newToolRig(t) owner := r.inst.Cookie() base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id := taskID(t, r.call("s1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Fix the README typo", "brief": "teh panes"})) if page := r.get(owner, "/tasks/"+id, 200); strings.Contains(page, "data-spend") { t.Fatalf("a task with no usage shows spend: %s", page) } r.call("s2", wire.ToolStartTask, map[string]string{"task": id, "brief": "Fix it."}) _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) r.step("s-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, Family: "claude", Account: "claude-a"}) eng := []wire.Usage{{Provider: "claude", Connection: "claude-a", Model: "claude-opus-5-5", Attempt: 1, Outcome: wire.OutcomeReplied, Complete: true, InputTokens: tokens(2000), CachedInputTokens: tokens(60000), CacheWriteTokens: tokens(1000), OutputTokens: tokens(1000), Turns: tokens(12)}} r.step("s-done", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Pushed: true, Termination: true, Family: "claude", Account: "claude-a", Usage: eng}) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) r.checks("s-c", c, wire.ChecksPassed, exit(0), "", "ok\n") rv := r.dispatched(wire.SpecialistReview, base, cand, 0) review := []wire.Usage{{Provider: "codex", Connection: "codex-b", Model: "gpt-6-astra", Attempt: 1, Outcome: wire.OutcomeReplied, Complete: true, InputTokens: tokens(19000), CachedInputTokens: tokens(1000), OutputTokens: tokens(1000), Turns: tokens(3)}} r.step("s-r", wire.StepReport{TaskID: id, StepID: rv.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Termination: true, Family: "gpt", Account: "codex-b", Verdict: wire.VerdictChanges, ReviewLabel: wire.ReviewOtherFamily, Findings: []wire.Finding{}, Summary: "One more.", Usage: review}) r.wait("fix round", func(c *Conversation) bool { return c.Tasks[id].FixRounds == 1 }) for _, who := range []string{"owner", "member"} { cookie := owner if who == "member" { cookie = r.memberCookie() } page := r.get(cookie, "/tasks/"+id, 200) for _, want := range []string{ `
Used 84k tokens (61k cached)
`, `Used at least 87k tokens (62k cached)
`, `Used 10k tokens (4k cached)
Earlier usage not shown
`) || strings.Contains(page, "Questions:") { t.Fatalf("task page after the upgrade:\n%s", page) } } // A Claude answer's row from a runner before ProtocolSpend overcounts // (it read the session's running total): the task page leaves it out // and says so, and so does today's usage on Connections. From a runner // at ProtocolSpend it counts. func TestPagesSpendLeavesOutOvercount(t *testing.T) { for _, proto := range []int{wire.Protocol1, wire.Protocol2, wire.ProtocolSpend} { t.Run(strconv.Itoa(proto), func(t *testing.T) { r := newToolRig(t) if proto > wire.Protocol1 { gen := r.runner.Welcome.Generation r.runner.Close() h := primaryHello(gen) h.Protocol = proto p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, h) if err != nil { t.Fatal(err) } t.Cleanup(p.Close) r.runner = p } base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id, _, _ := r.askableTask("o", base, cand) q, err := r.client.Ask(r.ctx, id, "What did you change?", "", "k1") if err != nil { t.Fatal(err) } r.expect(wire.FrameAsk) used := []wire.Usage{{Provider: "claude", Connection: "claude-a", Model: "claude-opus-5-5", Attempt: 1, Outcome: wire.OutcomeReplied, Complete: true, InputTokens: tokens(500), CachedInputTokens: tokens(9000), CacheWriteTokens: tokens(100), OutputTokens: tokens(400)}} r.answer("o-q", wire.AnswerReport{TaskID: id, QuestionID: q.ID, Attempt: 1, State: wire.QuestionAnswered, Text: "I fixed the typo.", Usage: used}) page := r.get(r.inst.Cookie(), "/tasks/"+id, 200) today, err := r.inst.Store.Chat().UsageToday(r.ctx, Owner) if err != nil { t.Fatal(err) } if proto < wire.ProtocolSpend { if !strings.Contains(page, `Earlier usage not shown
`) || strings.Contains(page, "data-spend>") || strings.Contains(page, "data-spend-step=") || today["claude-a"] != 0 { t.Fatalf("an older runner's answer counted (today %v):\n%s", today, page) } return } if !strings.Contains(page, `Used 10k tokens (9k cached)
`) || !strings.Contains(page, `