gateway_cache_reasoning_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package repository
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/Wei-Shaw/sub2api/internal/service"
  7. "github.com/alicebob/miniredis/v2"
  8. "github.com/redis/go-redis/v9"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestGatewayCacheReasoningContent(t *testing.T) {
  12. mr := miniredis.RunT(t)
  13. client := redis.NewClient(&redis.Options{Addr: mr.Addr()})
  14. cache := NewGatewayCache(client)
  15. ctx := context.Background()
  16. // 未命中返回哨兵错误,区别于真实读取失败。
  17. _, err := cache.GetReasoningContent(ctx, "item_missing")
  18. require.ErrorIs(t, err, service.ErrReasoningContentNotFound)
  19. // 写入后可读回。
  20. require.NoError(t, cache.SetReasoningContent(ctx, "item_abc", "think hard", time.Minute))
  21. got, err := cache.GetReasoningContent(ctx, "item_abc")
  22. require.NoError(t, err)
  23. require.Equal(t, "think hard", got)
  24. // ttl<=0 时兜底为默认 7 天。
  25. require.NoError(t, cache.SetReasoningContent(ctx, "item_ttl", "x", 0))
  26. ttl := mr.TTL(reasoningContentPrefix + "item_ttl")
  27. require.Greater(t, ttl, 6*24*time.Hour)
  28. require.LessOrEqual(t, ttl, reasoningContentDefaultTTL)
  29. // 空 itemID / 空 content 是 no-op(无可缓存内容不算错误)。
  30. require.NoError(t, cache.SetReasoningContent(ctx, "", "x", time.Minute))
  31. require.NoError(t, cache.SetReasoningContent(ctx, "item_empty", "", time.Minute))
  32. _, err = cache.GetReasoningContent(ctx, "item_empty")
  33. require.ErrorIs(t, err, service.ErrReasoningContentNotFound)
  34. }