Files
sub2api/backend/internal/service/openai_account_model_transient_test.go
T

131 lines
4.6 KiB
Go

package service
import (
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOpenAIModelTransient_FirstFailureDoesNotCreateLongBlock(t *testing.T) {
state := newOpenAIAccountModelTransientState(128)
now := time.Date(2026, 7, 10, 10, 0, 0, 0, time.UTC)
decision := state.recordFailure(35, "gpt-5.5", now)
assert.Equal(t, 1, decision.FailureStreak)
assert.Zero(t, decision.Cooldown)
assert.False(t, state.isBlocked(35, "gpt-5.5", now))
}
func TestOpenAIModelTransient_SecondFailureCreatesShortModelBlock(t *testing.T) {
state := newOpenAIAccountModelTransientState(128)
now := time.Date(2026, 7, 10, 10, 0, 0, 0, time.UTC)
state.recordFailure(35, "gpt-5.5", now)
decision := state.recordFailure(35, "gpt-5.5", now.Add(time.Second))
assert.Equal(t, 2, decision.FailureStreak)
assert.Equal(t, openAIModelTransientShortCooldown, decision.Cooldown)
assert.True(t, state.isBlocked(35, "gpt-5.5", now.Add(2*time.Second)))
assert.False(t, state.isBlocked(35, "gpt-5.5", now.Add(openAIModelTransientShortCooldown+2*time.Second)))
}
func TestOpenAIModelTransient_ThirdFailureCreatesFortyFiveSecondModelBlock(t *testing.T) {
state := newOpenAIAccountModelTransientState(128)
now := time.Date(2026, 7, 10, 10, 0, 0, 0, time.UTC)
state.recordFailure(35, "gpt-5.5", now)
state.recordFailure(35, "gpt-5.5", now.Add(time.Second))
decision := state.recordFailure(35, "gpt-5.5", now.Add(2*time.Second))
assert.Equal(t, 3, decision.FailureStreak)
assert.Equal(t, 45*time.Second, decision.Cooldown)
assert.True(t, state.isBlocked(35, "gpt-5.5", now.Add(40*time.Second)))
assert.False(t, state.isBlocked(35, "gpt-5.5", now.Add(48*time.Second)))
}
func TestOpenAIModelTransient_BlockIsIsolatedByModel(t *testing.T) {
state := newOpenAIAccountModelTransientState(128)
now := time.Date(2026, 7, 10, 10, 0, 0, 0, time.UTC)
state.recordFailure(35, "gpt-5.6-terra", now)
state.recordFailure(35, "GPT-5.6-TERRA", now.Add(time.Second))
assert.True(t, state.isBlocked(35, "gpt-5.6-terra", now.Add(2*time.Second)))
assert.False(t, state.isBlocked(35, "gpt-5.5", now.Add(2*time.Second)))
assert.False(t, state.isBlocked(47, "gpt-5.6-terra", now.Add(2*time.Second)))
}
func TestOpenAIModelTransient_SuccessClearsStreakAndBlock(t *testing.T) {
state := newOpenAIAccountModelTransientState(128)
now := time.Date(2026, 7, 10, 10, 0, 0, 0, time.UTC)
state.recordFailure(35, "gpt-5.5", now)
state.recordFailure(35, "gpt-5.5", now.Add(time.Second))
require.True(t, state.isBlocked(35, "gpt-5.5", now.Add(2*time.Second)))
state.recordSuccess(35, "gpt-5.5")
assert.False(t, state.isBlocked(35, "gpt-5.5", now.Add(2*time.Second)))
decision := state.recordFailure(35, "gpt-5.5", now.Add(3*time.Second))
assert.Equal(t, 1, decision.FailureStreak)
assert.Zero(t, decision.Cooldown)
}
func TestOpenAIModelTransient_StaleStreakExpires(t *testing.T) {
state := newOpenAIAccountModelTransientState(128)
now := time.Date(2026, 7, 10, 10, 0, 0, 0, time.UTC)
state.recordFailure(35, "gpt-5.5", now)
decision := state.recordFailure(35, "gpt-5.5", now.Add(openAIModelTransientFailureWindow+time.Second))
assert.Equal(t, 1, decision.FailureStreak)
assert.Zero(t, decision.Cooldown)
}
func TestOpenAIModelTransient_IgnoresInvalidKeys(t *testing.T) {
state := newOpenAIAccountModelTransientState(128)
now := time.Date(2026, 7, 10, 10, 0, 0, 0, time.UTC)
assert.Zero(t, state.recordFailure(0, "gpt-5.5", now).FailureStreak)
assert.Zero(t, state.recordFailure(35, " ", now).FailureStreak)
assert.False(t, state.isBlocked(0, "gpt-5.5", now))
assert.False(t, state.isBlocked(35, "", now))
assert.Equal(t, 0, state.size())
}
func TestOpenAIModelTransient_IgnoresOversizedModelKey(t *testing.T) {
state := newOpenAIAccountModelTransientState(128)
now := time.Date(2026, 7, 10, 10, 0, 0, 0, time.UTC)
model := strings.Repeat("m", openAIModelTransientMaxModelBytes+1)
decision := state.recordFailure(35, model, now)
assert.Zero(t, decision.FailureStreak)
assert.False(t, state.isBlocked(35, model, now))
assert.Equal(t, 0, state.size())
}
func TestOpenAIModelTransient_StateIsBoundedAndConcurrencySafe(t *testing.T) {
const maxEntries = 16
state := newOpenAIAccountModelTransientState(maxEntries)
now := time.Date(2026, 7, 10, 10, 0, 0, 0, time.UTC)
var wg sync.WaitGroup
for i := 0; i < 128; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
model := fmt.Sprintf("gpt-test-%d", i)
state.recordFailure(int64(i+1), model, now.Add(time.Duration(i)*time.Millisecond))
_ = state.isBlocked(int64(i+1), model, now.Add(time.Second))
}(i)
}
wg.Wait()
assert.LessOrEqual(t, state.size(), maxEntries)
}