summaryrefslogtreecommitdiff
path: root/internal/usage/observer_test.go
diff options
context:
space:
mode:
authorChia <Chia@93.nz>2026-08-05 22:01:29 +1200
committerChia <Chia@93.nz>2026-08-05 22:07:50 +1200
commiteadb2ffe85c43cf6fc741c9823cd28eedb4a844c (patch)
tree1aba2536d57360da403aa35c9ced58b615c7064e /internal/usage/observer_test.go
parentcd0dd91ab93653631904f2ea0e574ccde6d60339 (diff)
feat: harden prepaid billing and commercial operations
Diffstat (limited to 'internal/usage/observer_test.go')
-rw-r--r--internal/usage/observer_test.go19
1 files changed, 19 insertions, 0 deletions
diff --git a/internal/usage/observer_test.go b/internal/usage/observer_test.go
index 8fcf408..3104acb 100644
--- a/internal/usage/observer_test.go
+++ b/internal/usage/observer_test.go
@@ -10,6 +10,9 @@ func TestObserverReadsOpenAIJSONUsage(t *testing.T) {
observer := NewObserver(domain.ProtocolOpenAI, false)
_, _ = observer.Write([]byte(`{"choices":[],"usage":{"prompt_tokens":11,"completion_tokens":7,"total_tokens":18}}`))
got := observer.Usage()
+ if !observer.Reported() {
+ t.Fatal("expected usage to be marked as reported")
+ }
if got.InputTokens != 11 || got.OutputTokens != 7 || got.TotalTokens != 18 {
t.Fatalf("unexpected usage: %+v", got)
}
@@ -20,7 +23,23 @@ func TestObserverCombinesAnthropicSSEUsage(t *testing.T) {
_, _ = observer.Write([]byte("event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"usage\":{\"input_tokens\":12,\"output_tokens\":1}}}\n\n"))
_, _ = observer.Write([]byte("event: message_delta\ndata: {\"type\":\"message_delta\",\"usage\":{\"output_tokens\":8}}\n\n"))
got := observer.Usage()
+ if !observer.Reported() {
+ t.Fatal("expected streaming usage to be marked as reported")
+ }
if got.InputTokens != 12 || got.OutputTokens != 8 || got.TotalTokens != 20 {
t.Fatalf("unexpected usage: %+v", got)
}
}
+
+func TestObserverDistinguishesMissingUsageFromReportedZero(t *testing.T) {
+ missing := NewObserver(domain.ProtocolOpenAI, false)
+ _, _ = missing.Write([]byte(`{"choices":[]}`))
+ if missing.Reported() {
+ t.Fatal("response without usage must not be reported")
+ }
+ reported := NewObserver(domain.ProtocolOpenAI, false)
+ _, _ = reported.Write([]byte(`{"choices":[],"usage":{"prompt_tokens":0,"completion_tokens":0,"total_tokens":0}}`))
+ if !reported.Reported() {
+ t.Fatal("explicit zero usage must be distinguished from a missing usage object")
+ }
+}