refactor: extract formatCompactNumber util and add last_used_at to refresh key

- Add formatCompactNumber() for consistent large-number formatting (K/M/B)
- Include last_used_at in OpenAI usage refresh key for better change detection
- Add .gitattributes eol=lf rules for frontend source files
This commit is contained in:
Ethan0x0000
2026-03-16 16:22:51 +08:00
parent fa782e70a4
commit 8640a62319
5 changed files with 75 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ describe('buildOpenAIUsageRefreshKey', () => {
platform: 'openai',
type: 'oauth',
updated_at: '2026-03-07T10:00:00Z',
last_used_at: '2026-03-07T09:59:00Z',
extra: {
codex_usage_updated_at: '2026-03-07T10:00:00Z',
codex_5h_used_percent: 0,
@@ -27,12 +28,35 @@ describe('buildOpenAIUsageRefreshKey', () => {
expect(buildOpenAIUsageRefreshKey(base)).not.toBe(buildOpenAIUsageRefreshKey(next))
})
it('会在 last_used_at 变化时生成不同 key', () => {
const base = {
id: 3,
platform: 'openai',
type: 'oauth',
updated_at: '2026-03-07T10:00:00Z',
last_used_at: '2026-03-07T10:00:00Z',
extra: {
codex_usage_updated_at: '2026-03-07T10:00:00Z',
codex_5h_used_percent: 12,
codex_7d_used_percent: 24
}
} as any
const next = {
...base,
last_used_at: '2026-03-07T10:02:00Z'
}
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',
last_used_at: '2026-03-07T10:00:00Z',
extra: {}
} as any)).toBe('')
})

View File

@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest'
import { formatCompactNumber } from '../format'
describe('formatCompactNumber', () => {
it('formats boundary values with K/M/B', () => {
expect(formatCompactNumber(0)).toBe('0')
expect(formatCompactNumber(999)).toBe('999')
expect(formatCompactNumber(1000)).toBe('1.0K')
expect(formatCompactNumber(999999)).toBe('1000.0K')
expect(formatCompactNumber(1000000)).toBe('1.0M')
expect(formatCompactNumber(1000000000)).toBe('1.0B')
})
it('supports disabling billion unit (requests style)', () => {
expect(formatCompactNumber(1000000000, { allowBillions: false })).toBe('1000.0M')
})
it('returns 0 for nullish input', () => {
expect(formatCompactNumber(null)).toBe('0')
expect(formatCompactNumber(undefined)).toBe('0')
})
})