summaryrefslogtreecommitdiff
path: root/internal/usage/observer_test.go
blob: 4a4eba6166815cb8729e30f4f3e5a8224ed9fdc4 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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,"prompt_tokens_details":{"cached_tokens":3,"cache_write_tokens":2},"completion_tokens":7,"total_tokens":18}}`))
	got := observer.Usage()
	if !observer.Reported() {
		t.Fatal("expected usage to be marked as reported")
	}
	if got.InputTokens != 6 || got.CacheReadInputTokens != 3 || got.CacheCreationInputTokens != 2 || 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,\"cache_read_input_tokens\":5,\"cache_creation_input_tokens\":2}}}\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 || got.CacheReadInputTokens != 5 || got.CacheCreationInputTokens != 2 {
		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")
	}
}

func TestObserverReadsResponsesUsage(t *testing.T) {
	nonStream := NewObserver(domain.ProtocolOpenAIResponses, false)
	_, _ = nonStream.Write([]byte(`{"object":"response","usage":{"input_tokens":11,"input_tokens_details":{"cached_tokens":3,"cache_write_tokens":2},"output_tokens":7,"total_tokens":18}}`))
	if got := nonStream.Usage(); got.InputTokens != 6 || got.OutputTokens != 7 || got.TotalTokens != 18 || got.CacheReadInputTokens != 3 || got.CacheCreationInputTokens != 2 || !nonStream.Reported() {
		t.Fatalf("unexpected non-stream Responses usage: %+v reported=%v", got, nonStream.Reported())
	}

	stream := NewObserver(domain.ProtocolOpenAIResponses, true)
	_, _ = stream.Write([]byte("event: response.completed\ndata: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":13,\"output_tokens\":5,\"total_tokens\":18}}}\n\n"))
	if got := stream.Usage(); got.InputTokens != 13 || got.OutputTokens != 5 || got.TotalTokens != 18 || !stream.Reported() {
		t.Fatalf("unexpected streaming Responses usage: %+v reported=%v", got, stream.Reported())
	}
}