summaryrefslogtreecommitdiff
path: root/internal/usage/observer_test.go
blob: 3104acb157cc1cda97fe37666de2642c1ace6313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package usage

import (
	"testing"

	"aigw/internal/domain"
)

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)
	}
}

func TestObserverCombinesAnthropicSSEUsage(t *testing.T) {
	observer := NewObserver(domain.ProtocolAnthropic, true)
	_, _ = 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")
	}
}