mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-07 18:07:47 +08:00
fix: 修正用量失败状态判断与 Codex 会话头
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { isUsageRecordFailed, isUsageRecordSuccessful } from '../status'
|
||||
import type { UsageRecord } from '../../types'
|
||||
|
||||
function buildUsageRecord(overrides: Partial<UsageRecord> = {}): UsageRecord {
|
||||
return {
|
||||
id: 'usage-1',
|
||||
model: 'gpt-5',
|
||||
input_tokens: 10,
|
||||
output_tokens: 20,
|
||||
total_tokens: 30,
|
||||
cost: 0,
|
||||
is_stream: false,
|
||||
created_at: '2026-04-10T00:00:00Z',
|
||||
status: 'completed',
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
describe('usage status helpers', () => {
|
||||
it('treats explicit completed status as authoritative over stale legacy failure fields', () => {
|
||||
const record = buildUsageRecord({
|
||||
status: 'completed',
|
||||
status_code: 429,
|
||||
error_message: 'rate limited on first attempt'
|
||||
})
|
||||
|
||||
expect(isUsageRecordFailed(record)).toBe(false)
|
||||
expect(isUsageRecordSuccessful(record)).toBe(true)
|
||||
})
|
||||
|
||||
it('falls back to legacy failure signals when status is missing', () => {
|
||||
const record = buildUsageRecord({
|
||||
status: undefined,
|
||||
status_code: 429,
|
||||
error_message: 'rate limited'
|
||||
})
|
||||
|
||||
expect(isUsageRecordFailed(record)).toBe(true)
|
||||
expect(isUsageRecordSuccessful(record)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,28 @@
|
||||
import type { UsageRecord } from '../types'
|
||||
|
||||
function hasLegacyFailureSignal(
|
||||
record: Pick<UsageRecord, 'status_code' | 'error_message'>
|
||||
): boolean {
|
||||
return (typeof record.status_code === 'number' && record.status_code >= 400) ||
|
||||
(typeof record.error_message === 'string' && record.error_message.trim().length > 0)
|
||||
}
|
||||
|
||||
export function isUsageRecordFailed(
|
||||
record: Pick<UsageRecord, 'status' | 'status_code' | 'error_message'>
|
||||
): boolean {
|
||||
const status = typeof record.status === 'string' ? record.status.trim().toLowerCase() : ''
|
||||
if (status) {
|
||||
return status === 'failed'
|
||||
}
|
||||
return hasLegacyFailureSignal(record)
|
||||
}
|
||||
|
||||
export function isUsageRecordSuccessful(
|
||||
record: Pick<UsageRecord, 'status' | 'status_code' | 'error_message'>
|
||||
): boolean {
|
||||
const status = typeof record.status === 'string' ? record.status.trim().toLowerCase() : ''
|
||||
if (status) {
|
||||
return status === 'completed'
|
||||
}
|
||||
return !hasLegacyFailureSignal(record)
|
||||
}
|
||||
Reference in New Issue
Block a user