mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-10 18:14:48 +08:00
fix: 修复 OpenAI WS 用量窗口刷新与限额纠偏
This commit is contained in:
39
frontend/src/utils/__tests__/accountUsageRefresh.spec.ts
Normal file
39
frontend/src/utils/__tests__/accountUsageRefresh.spec.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { buildOpenAIUsageRefreshKey } from '../accountUsageRefresh'
|
||||
|
||||
describe('buildOpenAIUsageRefreshKey', () => {
|
||||
it('会在 codex 快照变化时生成不同 key', () => {
|
||||
const base = {
|
||||
id: 1,
|
||||
platform: 'openai',
|
||||
type: 'oauth',
|
||||
updated_at: '2026-03-07T10:00:00Z',
|
||||
extra: {
|
||||
codex_usage_updated_at: '2026-03-07T10:00:00Z',
|
||||
codex_5h_used_percent: 0,
|
||||
codex_7d_used_percent: 0
|
||||
}
|
||||
} as any
|
||||
|
||||
const next = {
|
||||
...base,
|
||||
extra: {
|
||||
...base.extra,
|
||||
codex_usage_updated_at: '2026-03-07T10:01:00Z',
|
||||
codex_5h_used_percent: 100
|
||||
}
|
||||
}
|
||||
|
||||
expect(buildOpenAIUsageRefreshKey(base)).not.toBe(buildOpenAIUsageRefreshKey(next))
|
||||
})
|
||||
|
||||
it('非 OpenAI OAuth 账号返回空 key', () => {
|
||||
expect(buildOpenAIUsageRefreshKey({
|
||||
id: 2,
|
||||
platform: 'anthropic',
|
||||
type: 'oauth',
|
||||
updated_at: '2026-03-07T10:00:00Z',
|
||||
extra: {}
|
||||
} as any)).toBe('')
|
||||
})
|
||||
})
|
||||
28
frontend/src/utils/accountUsageRefresh.ts
Normal file
28
frontend/src/utils/accountUsageRefresh.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { Account } from '@/types'
|
||||
|
||||
const normalizeUsageRefreshValue = (value: unknown): string => {
|
||||
if (value == null) return ''
|
||||
return String(value)
|
||||
}
|
||||
|
||||
export const buildOpenAIUsageRefreshKey = (account: Pick<Account, 'id' | 'platform' | 'type' | 'updated_at' | 'rate_limit_reset_at' | 'extra'>): string => {
|
||||
if (account.platform !== 'openai' || account.type !== 'oauth') {
|
||||
return ''
|
||||
}
|
||||
|
||||
const extra = account.extra ?? {}
|
||||
return [
|
||||
account.id,
|
||||
account.updated_at,
|
||||
account.rate_limit_reset_at,
|
||||
extra.codex_usage_updated_at,
|
||||
extra.codex_5h_used_percent,
|
||||
extra.codex_5h_reset_at,
|
||||
extra.codex_5h_reset_after_seconds,
|
||||
extra.codex_5h_window_minutes,
|
||||
extra.codex_7d_used_percent,
|
||||
extra.codex_7d_reset_at,
|
||||
extra.codex_7d_reset_after_seconds,
|
||||
extra.codex_7d_window_minutes
|
||||
].map(normalizeUsageRefreshValue).join('|')
|
||||
}
|
||||
Reference in New Issue
Block a user