fix: 修复PR #458中的totalCostLimit功能问题

主要修复:
- 移除重复的totalUsageLimit字段,统一使用totalCostLimit
- 删除auth.js中重复的总费用限制检查逻辑
- 删除admin.js中重复的totalCostLimit验证代码
- 更新所有前端组件,移除totalUsageLimit引用

功能改进:
- 确保totalCostLimit作为永久累计费用限制正常工作
- 与dailyCostLimit(每日重置)功能互补
- 适用于预付费、一次性API Key场景

测试:
- 删除有逻辑错误的test-total-usage-limit.js
- 创建新的test-total-cost-limit.js验证功能正确性
- 所有测试通过,功能正常工作

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
shaw
2025-09-20 17:37:20 +08:00
parent 398f00c4cb
commit 08c2b7a444
10 changed files with 47 additions and 137 deletions

View File

@@ -21,13 +21,13 @@ function createMockReq(apiKey) {
return {
headers: {
'x-api-key': apiKey,
'user-agent': 'total-usage-limit-test'
'user-agent': 'total-cost-limit-test'
},
query: {},
body: {},
ip: '127.0.0.1',
connection: {},
originalUrl: '/test-total-usage-limit',
originalUrl: '/test-total-cost-limit',
once: () => {},
on: () => {},
get(header) {
@@ -102,18 +102,18 @@ async function cleanupKey(keyId) {
async function main() {
await redis.connect()
const testName = `TotalUsageLimitTest-${Date.now()}`
const totalLimit = 1.0
const testName = `TotalCostLimitTest-${Date.now()}`
const totalCostLimit = 1.0
const newKey = await apiKeyService.generateApiKey({
name: testName,
permissions: 'all',
totalUsageLimit: totalLimit
totalCostLimit: totalCostLimit
})
const keyId = newKey.id
const { apiKey } = newKey
console.log(` Created test API key ${keyId} with total usage limit $${totalLimit}`)
console.log(` Created test API key ${keyId} with total cost limit $${totalCostLimit}`)
let authResult = await runAuth(apiKey)
if (authResult.status !== 200) {
@@ -121,32 +121,37 @@ async function main() {
}
console.log('✅ Authentication succeeds before consuming quota')
await redis.incrementDailyCost(keyId, 0.6)
// 增加总费用
const client = redis.getClient()
await client.set(`usage:cost:total:${keyId}`, '0.6')
authResult = await runAuth(apiKey)
if (authResult.status !== 200) {
throw new Error(`Expected success under quota, got status ${authResult.status}`)
}
console.log('✅ Authentication succeeds while still under quota ($0.60)')
await redis.incrementDailyCost(keyId, 0.5)
// 继续增加总费用超过限制
await client.set(`usage:cost:total:${keyId}`, '1.1')
authResult = await runAuth(apiKey)
if (authResult.status !== 429) {
throw new Error(`Expected 429 after exceeding quota, got status ${authResult.status}`)
}
console.log('✅ Authentication returns 429 after exceeding total usage limit ($1.10)')
console.log('✅ Authentication returns 429 after exceeding total cost limit ($1.10)')
await cleanupKey(keyId)
await redis.disconnect()
console.log('🎉 Total usage limit test completed successfully')
console.log('🎉 Total cost limit test completed successfully')
}
main().catch(async (error) => {
console.error('❌ Total usage limit test failed:', error)
console.error('❌ Total cost limit test failed:', error)
try {
await redis.disconnect()
} catch (_) {
// Ignore disconnect errors during cleanup
}
process.exitCode = 1
})
})