(t *testing.T)
| 7 | ) |
| 8 | |
| 9 | func TestPromptCacheTrackerComputeAndUpdate(t *testing.T) { |
| 10 | tracker := newPromptCacheTracker(time.Hour) |
| 11 | longSystem := strings.Repeat("You are a helpful coding assistant with deep knowledge of Go, Rust, Python, and TypeScript. ", 80) |
| 12 | req := &ClaudeRequest{ |
| 13 | Model: "claude-sonnet-4.5", |
| 14 | System: []interface{}{ |
| 15 | map[string]interface{}{ |
| 16 | "type": "text", |
| 17 | "text": longSystem, |
| 18 | "cache_control": map[string]interface{}{ |
| 19 | "type": "ephemeral", |
| 20 | }, |
| 21 | }, |
| 22 | }, |
| 23 | Messages: []ClaudeMessage{{Role: "user", Content: "hello world"}}, |
| 24 | } |
| 25 | |
| 26 | profile := tracker.BuildClaudeProfile(req, 120) |
| 27 | if profile == nil { |
| 28 | t.Fatalf("expected cache profile to be built") |
| 29 | } |
| 30 | |
| 31 | first := tracker.Compute("acct-1", profile) |
| 32 | if first.CacheCreationInputTokens <= 0 { |
| 33 | t.Fatalf("expected first request to create cache tokens, got %+v", first) |
| 34 | } |
| 35 | if first.CacheReadInputTokens != 0 { |
| 36 | t.Fatalf("expected first request to have zero cache reads, got %+v", first) |
| 37 | } |
| 38 | |
| 39 | tracker.Update("acct-1", profile) |
| 40 | second := tracker.Compute("acct-1", profile) |
| 41 | if second.CacheReadInputTokens <= 0 { |
| 42 | t.Fatalf("expected repeated request to read cache tokens, got %+v", second) |
| 43 | } |
| 44 | if second.CacheCreationInputTokens != 0 { |
| 45 | t.Fatalf("expected repeated request to avoid cache creation, got %+v", second) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestBuildClaudeUsageMapIncludesCacheFields(t *testing.T) { |
| 50 | usage := promptCacheUsage{ |
nothing calls this directly
no test coverage detected