diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-18 07:45:37 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-18 07:45:37 +0300 |
| commit | 4ffb22e7f69f1c9c79b095d4e60bad3d97aac55b (patch) | |
| tree | 2dc708cbb95975d34084eb5afd376871caa13e27 /internal/hexaicli/cache_test.go | |
| parent | ece7dcfd232b780f5650326c8ac2379ca70387d4 (diff) | |
ik0 replace test seams with dependency injection
Diffstat (limited to 'internal/hexaicli/cache_test.go')
| -rw-r--r-- | internal/hexaicli/cache_test.go | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/internal/hexaicli/cache_test.go b/internal/hexaicli/cache_test.go index c9b83c6..98dfb2d 100644 --- a/internal/hexaicli/cache_test.go +++ b/internal/hexaicli/cache_test.go @@ -47,12 +47,13 @@ func TestCLIResponseCacheFingerprintChanges(t *testing.T) { func TestLookupCLIResponseCacheExpiresEntries(t *testing.T) { t.Setenv("XDG_CACHE_HOME", t.TempDir()) - oldNow := nowCLIResponseCache - nowCLIResponseCache = func() time.Time { return time.Date(2026, 3, 15, 10, 0, 0, 0, time.UTC) } - defer func() { nowCLIResponseCache = oldNow }() + // Inject a fake clock via a responseCache value, demonstrating dependency + // injection rather than mutating package state. + now := time.Date(2026, 3, 15, 10, 0, 0, 0, time.UTC) + cache := responseCache{now: func() time.Time { return now }} key := newCLIResponseCacheKey("openai", "gpt-4.1", requestArgs{maxTokens: 10}, []llm.Message{{Role: "user", Content: "hello"}}) - storeCLIResponseCache(key, "cached") + cache.store(key, "cached") path, ok := cliResponseCachePath(key) if !ok { @@ -62,8 +63,9 @@ func TestLookupCLIResponseCacheExpiresEntries(t *testing.T) { t.Fatalf("expected cache file: %v", err) } - nowCLIResponseCache = func() time.Time { return time.Date(2026, 3, 16, 11, 0, 0, 0, time.UTC) } - if _, _, hit := lookupCLIResponseCache(key); hit { + // Advance the injected clock past the TTL so the entry expires. + now = time.Date(2026, 3, 16, 11, 0, 0, 0, time.UTC) + if _, _, hit := cache.lookup(key); hit { t.Fatal("expected expired cache miss") } if _, err := os.Stat(path); !os.IsNotExist(err) { @@ -175,9 +177,8 @@ func TestRun_ExpiredCacheFallsBackToProvider(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) t.Setenv("XDG_CACHE_HOME", t.TempDir()) - oldNow := nowCLIResponseCache - nowCLIResponseCache = func() time.Time { return time.Date(2026, 3, 15, 10, 0, 0, 0, time.UTC) } - defer func() { nowCLIResponseCache = oldNow }() + now := time.Date(2026, 3, 15, 10, 0, 0, 0, time.UTC) + ctx := withCLIResponseCacheNow(context.Background(), func() time.Time { return now }) oldNew := newClientFromApp defer func() { newClientFromApp = oldNew }() @@ -192,13 +193,13 @@ func TestRun_ExpiredCacheFallsBackToProvider(t *testing.T) { return &fakeClient{name: cfg.Provider, model: "gpt-4.1", resp: resp}, nil } - if err := Run(context.Background(), []string{"hello"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{}); err != nil { + if err := Run(ctx, []string{"hello"}, strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{}); err != nil { t.Fatalf("first Run: %v", err) } - nowCLIResponseCache = func() time.Time { return time.Date(2026, 3, 16, 11, 0, 0, 0, time.UTC) } + now = time.Date(2026, 3, 16, 11, 0, 0, 0, time.UTC) var out, errb bytes.Buffer - if err := Run(context.Background(), []string{"hello"}, strings.NewReader(""), &out, &errb); err != nil { + if err := Run(ctx, []string{"hello"}, strings.NewReader(""), &out, &errb); err != nil { t.Fatalf("second Run: %v", err) } if calls != 2 { |
