mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-03 06:52:13 +08:00
fix: 修复 OpenAI WS 用量刷新遗漏场景
This commit is contained in:
@@ -412,14 +412,24 @@ const isActiveOpenAIRateLimited = computed(() => {
|
||||
})
|
||||
|
||||
const preferFetchedOpenAIUsage = computed(() => {
|
||||
return isActiveOpenAIRateLimited.value && hasOpenAIUsageFallback.value
|
||||
return (isActiveOpenAIRateLimited.value || isOpenAICodexSnapshotStale.value) && hasOpenAIUsageFallback.value
|
||||
})
|
||||
|
||||
const openAIUsageRefreshKey = computed(() => buildOpenAIUsageRefreshKey(props.account))
|
||||
|
||||
const isOpenAICodexSnapshotStale = computed(() => {
|
||||
if (props.account.platform !== 'openai' || props.account.type !== 'oauth') return false
|
||||
const extra = props.account.extra as Record<string, unknown> | undefined
|
||||
const updatedAtRaw = extra?.codex_usage_updated_at
|
||||
if (!updatedAtRaw) return true
|
||||
const updatedAt = Date.parse(String(updatedAtRaw))
|
||||
if (Number.isNaN(updatedAt)) return true
|
||||
return Date.now() - updatedAt >= 10 * 60 * 1000
|
||||
})
|
||||
|
||||
const shouldAutoLoadUsageOnMount = computed(() => {
|
||||
if (props.account.platform === 'openai' && props.account.type === 'oauth') {
|
||||
return isActiveOpenAIRateLimited.value || !hasCodexUsage.value
|
||||
return isActiveOpenAIRateLimited.value || !hasCodexUsage.value || isOpenAICodexSnapshotStale.value
|
||||
}
|
||||
return shouldFetchUsage.value
|
||||
})
|
||||
@@ -807,7 +817,7 @@ onMounted(() => {
|
||||
watch(openAIUsageRefreshKey, (nextKey, prevKey) => {
|
||||
if (!prevKey || nextKey === prevKey) return
|
||||
if (props.account.platform !== 'openai' || props.account.type !== 'oauth') return
|
||||
if (!isActiveOpenAIRateLimited.value && hasCodexUsage.value) return
|
||||
if (!isActiveOpenAIRateLimited.value && hasCodexUsage.value && !isOpenAICodexSnapshotStale.value) return
|
||||
|
||||
loadUsage().catch((e) => {
|
||||
console.error('Failed to refresh OpenAI usage:', e)
|
||||
|
||||
@@ -69,6 +69,67 @@ describe('AccountUsageCell', () => {
|
||||
})
|
||||
|
||||
|
||||
it('OpenAI OAuth 快照已过期时首屏会重新请求 usage', async () => {
|
||||
getUsage.mockResolvedValue({
|
||||
five_hour: {
|
||||
utilization: 15,
|
||||
resets_at: '2026-03-08T12:00:00Z',
|
||||
remaining_seconds: 3600,
|
||||
window_stats: {
|
||||
requests: 3,
|
||||
tokens: 300,
|
||||
cost: 0.03,
|
||||
standard_cost: 0.03,
|
||||
user_cost: 0.03
|
||||
}
|
||||
},
|
||||
seven_day: {
|
||||
utilization: 77,
|
||||
resets_at: '2026-03-13T12:00:00Z',
|
||||
remaining_seconds: 3600,
|
||||
window_stats: {
|
||||
requests: 3,
|
||||
tokens: 300,
|
||||
cost: 0.03,
|
||||
standard_cost: 0.03,
|
||||
user_cost: 0.03
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const wrapper = mount(AccountUsageCell, {
|
||||
props: {
|
||||
account: {
|
||||
id: 2000,
|
||||
platform: 'openai',
|
||||
type: 'oauth',
|
||||
extra: {
|
||||
codex_usage_updated_at: '2026-03-07T00:00:00Z',
|
||||
codex_5h_used_percent: 12,
|
||||
codex_5h_reset_at: '2026-03-08T12:00:00Z',
|
||||
codex_7d_used_percent: 34,
|
||||
codex_7d_reset_at: '2026-03-13T12:00:00Z'
|
||||
}
|
||||
} as any
|
||||
},
|
||||
global: {
|
||||
stubs: {
|
||||
UsageProgressBar: {
|
||||
props: ['label', 'utilization', 'resetsAt', 'windowStats', 'color'],
|
||||
template: '<div class="usage-bar">{{ label }}|{{ utilization }}|{{ windowStats?.tokens }}</div>'
|
||||
},
|
||||
AccountQuotaInfo: true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
await flushPromises()
|
||||
|
||||
expect(getUsage).toHaveBeenCalledWith(2000)
|
||||
expect(wrapper.text()).toContain('5h|15|300')
|
||||
expect(wrapper.text()).toContain('7d|77|300')
|
||||
})
|
||||
|
||||
it('OpenAI OAuth 有现成快照且未限额时不会首屏请求 usage', async () => {
|
||||
const wrapper = mount(AccountUsageCell, {
|
||||
props: {
|
||||
@@ -77,6 +138,7 @@ describe('AccountUsageCell', () => {
|
||||
platform: 'openai',
|
||||
type: 'oauth',
|
||||
extra: {
|
||||
codex_usage_updated_at: '2099-03-07T10:00:00Z',
|
||||
codex_5h_used_percent: 12,
|
||||
codex_5h_reset_at: '2099-03-07T12:00:00Z',
|
||||
codex_7d_used_percent: 34,
|
||||
|
||||
Reference in New Issue
Block a user