fix(review): address Copilot PR feedback
- Add compile-time interface assertion for sessionWindowMockRepo - Fix flaky fallback test by capturing time.Now() before calling UpdateSessionWindow - Replace stale hardcoded timestamps with dynamic future values - Add millisecond detection and bounds validation for reset header timestamp - Use pause/resume pattern for interval in UsageProgressBar to avoid idle timers on large lists - Fix gofmt comment alignment Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,8 @@ type sessionWindowMockRepo struct {
|
||||
clearRateLimitIDs []int64
|
||||
}
|
||||
|
||||
var _ AccountRepository = (*sessionWindowMockRepo)(nil)
|
||||
|
||||
type swCall struct {
|
||||
ID int64
|
||||
Start *time.Time
|
||||
@@ -160,7 +162,7 @@ func newRateLimitServiceForTest(repo AccountRepository) *RateLimitService {
|
||||
func TestUpdateSessionWindow_UsesResetHeader(t *testing.T) {
|
||||
// The reset header provides the real window end as a Unix timestamp.
|
||||
// UpdateSessionWindow should use it instead of the hour-truncated prediction.
|
||||
resetUnix := int64(1771020000) // 2026-02-14T10:00:00Z
|
||||
resetUnix := time.Now().Add(3 * time.Hour).Unix()
|
||||
wantEnd := time.Unix(resetUnix, 0)
|
||||
wantStart := wantEnd.Add(-5 * time.Hour)
|
||||
|
||||
@@ -203,6 +205,11 @@ func TestUpdateSessionWindow_FallbackPredictionWhenNoResetHeader(t *testing.T) {
|
||||
headers.Set("anthropic-ratelimit-unified-5h-status", "allowed_warning")
|
||||
// No anthropic-ratelimit-unified-5h-reset header
|
||||
|
||||
// Capture now before the call to avoid hour-boundary races
|
||||
now := time.Now()
|
||||
expectedStart := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
|
||||
expectedEnd := expectedStart.Add(5 * time.Hour)
|
||||
|
||||
svc.UpdateSessionWindow(context.Background(), account, headers)
|
||||
|
||||
if len(repo.sessionWindowCalls) != 1 {
|
||||
@@ -214,9 +221,6 @@ func TestUpdateSessionWindow_FallbackPredictionWhenNoResetHeader(t *testing.T) {
|
||||
t.Fatal("expected window end to be set (fallback prediction)")
|
||||
}
|
||||
// Fallback: start = current hour truncated, end = start + 5h
|
||||
now := time.Now()
|
||||
expectedStart := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
|
||||
expectedEnd := expectedStart.Add(5 * time.Hour)
|
||||
|
||||
if !call.End.Equal(expectedEnd) {
|
||||
t.Errorf("expected fallback end %v, got %v", expectedEnd, *call.End)
|
||||
@@ -229,7 +233,7 @@ func TestUpdateSessionWindow_FallbackPredictionWhenNoResetHeader(t *testing.T) {
|
||||
func TestUpdateSessionWindow_CorrectsStalePrediction(t *testing.T) {
|
||||
// When the stored SessionWindowEnd is wrong (from a previous prediction),
|
||||
// and the reset header provides the real time, it should update the window.
|
||||
staleEnd := time.Now().Add(2 * time.Hour) // existing prediction: 2h from now
|
||||
staleEnd := time.Now().Add(2 * time.Hour) // existing prediction: 2h from now
|
||||
realResetUnix := time.Now().Add(4 * time.Hour).Unix() // real reset: 4h from now
|
||||
wantEnd := time.Unix(realResetUnix, 0)
|
||||
|
||||
@@ -291,7 +295,7 @@ func TestUpdateSessionWindow_NoUpdateWhenHeaderMatchesStored(t *testing.T) {
|
||||
|
||||
func TestUpdateSessionWindow_ClearsUtilizationOnWindowReset(t *testing.T) {
|
||||
// When needInitWindow=true and window is set, utilization should be cleared.
|
||||
resetUnix := int64(1771020000)
|
||||
resetUnix := time.Now().Add(3 * time.Hour).Unix()
|
||||
|
||||
repo := &sessionWindowMockRepo{}
|
||||
svc := newRateLimitServiceForTest(repo)
|
||||
|
||||
Reference in New Issue
Block a user