Files
sub2api/backend/internal/service/account_usage_antigravity_force_test.go
T
William DrakeandClaude Opus 5.5 4ac1675b56 feat(antigravity): query button that refreshes usage past the cache
Antigravity accounts had no manual query button in the usage cell, unlike Claude
and OpenAI, and the server ignored force=true for them: getAntigravityUsage
always answered from its in-memory cache while fresh. After an upgrade or an
upstream reset the cell kept old figures (an account upgraded to Ultra still
showed its Claude week at 100%).

- getAntigravityUsage takes force; cachedAntigravityUsage skips the cache when
  forced. singleflight still merges concurrent fetches.
- The antigravity cell shows the same 查询/Query button (active, force=true) in
  every state, including errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 14:33:29 -07:00

40 lines
1.4 KiB
Go

//go:build unit
package service
import (
"testing"
"time"
)
// A manual refresh (force=true) must not be answered from the antigravity usage cache: an
// account upgraded or reset upstream otherwise keeps showing old figures for the cache TTL.
func TestCachedAntigravityUsage_ForceSkipsFreshCache(t *testing.T) {
s := &AccountUsageService{cache: NewUsageCache()}
resetsAt := time.Now().Add(2 * time.Hour)
stored := &UsageInfo{FiveHour: &UsageProgress{ResetsAt: &resetsAt}}
s.cache.antigravityCache.Store(int64(6), &antigravityUsageCache{usageInfo: stored, timestamp: time.Now()})
usage, ok := s.cachedAntigravityUsage(6, false)
if !ok || usage != stored {
t.Fatalf("fresh cache should answer a normal query, got ok=%v", ok)
}
if usage.FiveHour.RemainingSeconds <= 0 {
t.Fatalf("remaining seconds should be recalculated, got %d", usage.FiveHour.RemainingSeconds)
}
if usage, ok := s.cachedAntigravityUsage(6, true); ok || usage != nil {
t.Fatalf("a forced query must skip the cache")
}
}
func TestCachedAntigravityUsage_ExpiredOrMissing(t *testing.T) {
s := &AccountUsageService{cache: NewUsageCache()}
if _, ok := s.cachedAntigravityUsage(7, false); ok {
t.Fatalf("missing entry must miss")
}
s.cache.antigravityCache.Store(int64(7), &antigravityUsageCache{usageInfo: &UsageInfo{}, timestamp: time.Now().Add(-time.Hour)})
if _, ok := s.cachedAntigravityUsage(7, false); ok {
t.Fatalf("expired entry must miss")
}
}