返回 DeepSeek-Reasonix
cache_tag_test.go
根目录 / internal / cli / cache_tag_test.go
1 package cli
2
3 import (
4 "strings"
5 "testing"
6
7 "reasonix/internal/control"
8 "reasonix/internal/provider"
9 )
10
11 // cacheTagCtrl stubs the two SessionAPI reads cacheTag performs; every other
12 // method panics via the embedded nil interface, which is exactly what we want
13 // from a focused status-line test.
14 type cacheTagCtrl struct {
15 control.SessionAPI
16 last *provider.Usage
17 hit, miss int
18 }
19
20 func (s cacheTagCtrl) LastUsage() *provider.Usage { return s.last }
21 func (s cacheTagCtrl) SessionCache() (int, int) { return s.hit, s.miss }
22
23 func TestCacheTagHiddenWhenProviderReportsNoCacheFields(t *testing.T) {
24 // A provider without prompt-cache support reports prompt tokens but no
25 // cache hit/miss fields. Falling back to PromptTokens as the denominator
26 // used to paint a bogus "turn hit 0.00%"; the tag must stay empty instead.
27 m := chatTUI{ctrl: cacheTagCtrl{last: &provider.Usage{PromptTokens: 1000}}}
28 if got := m.cacheTag(); got != "" {
29 t.Fatalf("cacheTag with no cache fields = %q, want empty", got)
30 }
31 }
32
33 func TestCacheTagShowsRealZeroHit(t *testing.T) {
34 // A genuine full miss (provider reports the fields, hit is zero) is
35 // informative and must still render.
36 m := chatTUI{ctrl: cacheTagCtrl{last: &provider.Usage{PromptTokens: 1000, CacheMissTokens: 1000}}}
37 if got := m.cacheTag(); !strings.Contains(got, "0.00%") {
38 t.Fatalf("cacheTag with a real full miss = %q, want 0.00%% rendered", got)
39 }
40 }
41
42 func TestCacheTagRendersHitRateAndSessionAverage(t *testing.T) {
43 m := chatTUI{ctrl: cacheTagCtrl{
44 last: &provider.Usage{CacheHitTokens: 80, CacheMissTokens: 20},
45 hit: 700, miss: 300,
46 }}
47 got := m.cacheTag()
48 if !strings.Contains(got, "80.00%") {
49 t.Fatalf("cacheTag = %q, want turn rate 80.00%%", got)
50 }
51 if !strings.Contains(got, "70.00%") {
52 t.Fatalf("cacheTag = %q, want session average 70.00%%", got)
53 }
54 }
55
55 lines GO