package runner import ( "encoding/json" "math" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) func usageEvent(turn, usage string) provider.RecordedEvent { return provider.RecordedEvent{Kind: "turn_completed", ServerID: "server", AgentID: "agent", ProviderSessionID: "session", TurnID: turn, Payload: json.RawMessage(`{"event":{"type":"turn_completed","provider":"claude","turnId":"` + turn + `","usage":` + usage + `}}`)} } func TestUsageCumulativeAndMissing(t *testing.T) { a := NewUsageAccumulator(UsageProfile{Version: 1, Provider: "claude", PaseoVersion: "0.14.0", TokenSemantics: "turn", InputSemantics: "exclusive", CostSemantics: "cumulative", Subscription: true}) first := usageEvent("one", `{"inputTokens":18,"cachedInputTokens":29591,"outputTokens":404,"totalCostUsd":0.0673131}`) u := a.ObservePaseo(first) if u == nil || u.InputTokens != 29609 || u.CachedInputTokens != 29591 || u.CostMicros != 0 || !u.CostUnavailable { t.Fatal("normalization", u) } next := usageEvent("two", `{"inputTokens":2,"cachedInputTokens":3,"outputTokens":4,"totalCostUsd":0.0758928}`) if a.ObservePaseo(next) == nil { t.Fatal("second completion unknown") } if math.Abs(a.EstimatedCostUSD()-0.0758928) > 1e-10 { t.Fatal("cumulative cost summed", a.EstimatedCostUSD()) } if a.ObservePaseo(usageEvent("missing", `null`)) != nil { t.Fatal("missing usage became zero") } for _, kind := range []string{"agent_update", "queued", "idle", "timeline"} { e := first e.Kind = kind e.Payload = json.RawMessage(`{"agent":{"lastUsage":{"inputTokens":0,"cachedInputTokens":0,"outputTokens":0}}}`) if a.ObservePaseo(e) != nil { t.Fatal("status proved completion", kind) } } if got := a.ObservePaseo(first); got == nil || *got != *u { t.Fatal("lastUsage erased completion", got) } unknown := NewUsageAccumulator(UsageProfile{Version: 1, Provider: "claude", PaseoVersion: "0.14.0", TokenSemantics: "unverified", InputSemantics: "exclusive", Subscription: true}) if unknown.ObservePaseo(first) != nil { t.Fatal("unverified cumulative semantics settled") } if got := a.ObservePaseo(usageEvent("zero", `{"inputTokens":0,"cachedInputTokens":0,"outputTokens":0}`)); got == nil || got.InputTokens != 0 { t.Fatal("known zero lost") } for _, raw := range []string{`{"inputTokens":1,"outputTokens":2}`, `{"inputTokens":9223372036854775807,"cachedInputTokens":1,"outputTokens":1}`, `{"inputTokens":-1,"cachedInputTokens":0,"outputTokens":1}`} { if a.ObservePaseo(usageEvent("invalid", raw)) != nil { t.Fatal("partial or invalid usage settled") } } inclusive := NewUsageAccumulator(UsageProfile{Version: 1, Provider: "claude", PaseoVersion: "0.14.0", TokenSemantics: "turn", InputSemantics: "inclusive", Subscription: true}) if v := inclusive.ObservePaseo(first); v != nil { t.Fatal("cache exceeds inclusive input") } got := inclusive.ObservePaseo(usageEvent("inclusive", `{"inputTokens":20,"cachedInputTokens":5,"outputTokens":2}`)) if got == nil || got.InputTokens != 20 || got.CachedInputTokens != 5 { t.Fatal("inclusive cache counted twice", got) } tokens, err := NormalizeTokens(ProviderTokens{Input: 10, Cached: 3, CacheWrite: 4, Output: 2, InputKnown: true, CachedKnown: true, CacheWriteKnown: true, OutputKnown: true}, "exclusive", false) mustJournal(t, err) if tokens.UncachedInput != 14 || tokens.CachedInput != 3 { t.Fatal("cache write normalization", tokens) } } func TestUsageCumulativeCompletionGuards(t *testing.T) { p := UsageProfile{Version: 1, Provider: "claude", PaseoVersion: "0.14.0", TokenSemantics: "turn", InputSemantics: "exclusive", CostSemantics: "cumulative", Subscription: true} e := usageEvent("turn", `{"inputTokens":5,"cachedInputTokens":3,"outputTokens":2,"totalCostUsd":0.0673131}`) for _, kind := range []string{"idle", "queued", "timeline", "agent_update"} { bad := e bad.Kind = kind if NewUsageAccumulator(p).ObservePaseo(bad) != nil { t.Fatal("noncompletion kind settled", kind) } } for _, field := range []string{"version", "paseo", "provider", "turn", "session", "input_mode"} { bad := e profile := p switch field { case "version": profile.Version = 2 case "paseo": profile.PaseoVersion = "unknown" case "provider": profile.Provider = "codex" case "turn": bad.TurnID = "other" case "session": bad.ProviderSessionID = "" case "input_mode": profile.InputSemantics = "unknown" } if NewUsageAccumulator(profile).ObservePaseo(bad) != nil { t.Fatal("unverified usage settled", field) } } a := NewUsageAccumulator(p) u := a.ObservePaseo(e) if u == nil { t.Fatal("completion missing") } zero := usageEvent("turn", `{"inputTokens":0,"cachedInputTokens":0,"outputTokens":0}`) if got := a.ObservePaseo(zero); got == nil || *got != *u { t.Fatal("zero erased completed evidence") } p.Subscription = false if NewUsageAccumulator(p).ObservePaseo(e) != nil { t.Fatal("unverified cumulative currency settled") } p.CostSemantics = "turn" if got := NewUsageAccumulator(p).ObservePaseo(e); got == nil || got.CostMicros != 67314 || got.CostUnavailable { t.Fatal("metered per-turn normalization", got) } partial, err := NormalizeTokens(ProviderTokens{Input: 5, InputKnown: true, OutputKnown: true}, "inclusive", true) mustJournal(t, err) if partial.UncachedKnown || partial.CachedKnown || !partial.OutputKnown { t.Fatal("missing cache treated as zero", partial) } _, err = NormalizeTokens(ProviderTokens{Input: math.MaxInt64, InputKnown: true, CacheWrite: 1, CacheWriteKnown: true}, "exclusive", false) if err == nil { t.Fatal("cache-write overflow accepted") } }