mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-27 23:30:13 +00:00
fix: 账号徽章与用量格按实时档位展示,避免账单滞后误判
订阅失效刷新后凭证已是 free,但残留的 monthly_limit / usage_percent 仍会把用量格锁在付费 7d/30d,徽章也优先信滞后的 billing.plan。 - 凭证 subscription_tier 优先于账单/配额快照 - 用量格先看实时档位,再回退账单指标;Lite 仍走付费条 - 徽章补齐 SuperGrok Lite / Plus / X Basic
This commit is contained in:
1 parent
a04ce49016
commit
69648476d4
6 files changed
+111
-15
No files matched your search
@@ -1148,19 +1148,19 @@ const grokPlanLabelIsPaid = (value: string) => {
|
|||||||
const grokIsFree = computed(() => {
|
const grokIsFree = computed(() => {
|
||||||
if (props.account.platform !== 'grok' || props.account.type !== 'oauth') return false
|
if (props.account.platform !== 'grok' || props.account.type !== 'oauth') return false
|
||||||
const billing = grokBilling.value
|
const billing = grokBilling.value
|
||||||
|
const plan = (billing?.plan || '').trim().toLowerCase()
|
||||||
|
const tier = (usageInfo.value?.subscription_tier || '').trim().toLowerCase()
|
||||||
|
const entitlement = (usageInfo.value?.grok_entitlement_status || '').toLowerCase()
|
||||||
|
if (grokPlanLabelIsFree(tier)) return true
|
||||||
|
if (grokPlanLabelIsPaid(tier)) return false
|
||||||
if (
|
if (
|
||||||
billing?.usage_percent != null ||
|
billing?.usage_percent != null ||
|
||||||
billing?.used_percent != null ||
|
billing?.used_percent != null ||
|
||||||
(billing?.monthly_limit_cents != null && billing.monthly_limit_cents > 0)
|
(billing?.monthly_limit_cents != null && billing.monthly_limit_cents > 0)
|
||||||
) return false
|
) return false
|
||||||
|
if (grokPlanLabelIsPaid(plan)) return false
|
||||||
const plan = (billing?.plan || '').trim().toLowerCase()
|
|
||||||
const tier = (usageInfo.value?.subscription_tier || '').trim().toLowerCase()
|
|
||||||
const entitlement = (usageInfo.value?.grok_entitlement_status || '').toLowerCase()
|
|
||||||
if (grokPlanLabelIsPaid(plan) || grokPlanLabelIsPaid(tier)) return false
|
|
||||||
if (
|
if (
|
||||||
grokPlanLabelIsFree(plan) ||
|
grokPlanLabelIsFree(plan) ||
|
||||||
grokPlanLabelIsFree(tier) ||
|
|
||||||
grokPlanLabelIsFree(entitlement)
|
grokPlanLabelIsFree(entitlement)
|
||||||
) return true
|
) return true
|
||||||
return billing != null
|
return billing != null
|
||||||
|
|||||||
@@ -903,6 +903,81 @@ describe('AccountUsageCell', () => {
|
|||||||
expect(wrapper.text()).not.toContain('250.0K')
|
expect(wrapper.text()).not.toContain('250.0K')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Grok JWT free tier shows 24h bar even when leftover Heavy billing metrics remain', async () => {
|
||||||
|
getUsage.mockResolvedValue({
|
||||||
|
grok_free_token_limit: 500_000,
|
||||||
|
subscription_tier: 'free',
|
||||||
|
grok_billing: {
|
||||||
|
plan: 'SuperGrok Heavy',
|
||||||
|
monthly_limit_cents: 150_000,
|
||||||
|
usage_percent: 10,
|
||||||
|
used_percent: 5
|
||||||
|
},
|
||||||
|
grok_local_usage_24h: {
|
||||||
|
requests: 2,
|
||||||
|
tokens: 250_000,
|
||||||
|
cost: 0,
|
||||||
|
standard_cost: 0
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const wrapper = mount(AccountUsageCell, {
|
||||||
|
props: {
|
||||||
|
account: makeAccount({ id: 4404, platform: 'grok', type: 'oauth', extra: {} })
|
||||||
|
},
|
||||||
|
global: {
|
||||||
|
stubs: {
|
||||||
|
UsageProgressBar: {
|
||||||
|
props: ['label', 'utilization'],
|
||||||
|
template: '<div class="usage-bar">{{ label }}|{{ utilization }}</div>'
|
||||||
|
},
|
||||||
|
AccountQuotaInfo: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await flushPromises()
|
||||||
|
expect(wrapper.text()).toContain('24h|')
|
||||||
|
expect(wrapper.text()).not.toContain('7d|')
|
||||||
|
expect(wrapper.text()).not.toContain('30d|')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Grok SuperGrok Lite stays on paid 7d bar, not free 24h', async () => {
|
||||||
|
getUsage.mockResolvedValue({
|
||||||
|
subscription_tier: 'supergrok_lite',
|
||||||
|
grok_billing: {
|
||||||
|
period_type: 'weekly',
|
||||||
|
plan: 'SuperGrok',
|
||||||
|
usage_percent: 20
|
||||||
|
},
|
||||||
|
grok_local_usage_24h: {
|
||||||
|
requests: 1,
|
||||||
|
tokens: 100,
|
||||||
|
cost: 0,
|
||||||
|
standard_cost: 0
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const wrapper = mount(AccountUsageCell, {
|
||||||
|
props: {
|
||||||
|
account: makeAccount({ id: 4405, platform: 'grok', type: 'oauth', extra: {} })
|
||||||
|
},
|
||||||
|
global: {
|
||||||
|
stubs: {
|
||||||
|
UsageProgressBar: {
|
||||||
|
props: ['label', 'utilization'],
|
||||||
|
template: '<div class="usage-bar">{{ label }}|{{ utilization }}</div>'
|
||||||
|
},
|
||||||
|
AccountQuotaInfo: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await flushPromises()
|
||||||
|
expect(wrapper.text()).toContain('7d|')
|
||||||
|
expect(wrapper.text()).not.toContain('24h|')
|
||||||
|
})
|
||||||
|
|
||||||
it('Grok credential Free tier keeps the 1M fallback when billing is unavailable', async () => {
|
it('Grok credential Free tier keeps the 1M fallback when billing is unavailable', async () => {
|
||||||
getUsage.mockResolvedValue({
|
getUsage.mockResolvedValue({
|
||||||
grok_free_token_limit: 1_000_000,
|
grok_free_token_limit: 1_000_000,
|
||||||
|
|||||||
@@ -137,10 +137,16 @@ const planLabel = computed(() => {
|
|||||||
return props.platform === 'grok' ? 'Grok Free' : 'Free'
|
return props.platform === 'grok' ? 'Grok Free' : 'Free'
|
||||||
case 'supergrok':
|
case 'supergrok':
|
||||||
return 'SuperGrok'
|
return 'SuperGrok'
|
||||||
|
case 'supergroklite':
|
||||||
|
return 'SuperGrok Lite'
|
||||||
|
case 'supergrokplus':
|
||||||
|
return 'SuperGrok Plus'
|
||||||
case 'supergrokheavy':
|
case 'supergrokheavy':
|
||||||
return 'SuperGrok Heavy'
|
return 'SuperGrok Heavy'
|
||||||
case 'heavy':
|
case 'heavy':
|
||||||
return 'Heavy'
|
return 'Heavy'
|
||||||
|
case 'xbasic':
|
||||||
|
return 'X Basic'
|
||||||
case 'abnormal':
|
case 'abnormal':
|
||||||
return t('admin.accounts.subscriptionAbnormal')
|
return t('admin.accounts.subscriptionAbnormal')
|
||||||
default:
|
default:
|
||||||
@@ -150,7 +156,9 @@ const planLabel = computed(() => {
|
|||||||
|
|
||||||
const isGrokFreePlan = computed(() =>
|
const isGrokFreePlan = computed(() =>
|
||||||
props.platform === 'grok' &&
|
props.platform === 'grok' &&
|
||||||
(normalizedPlanType.value === 'free' || normalizedPlanType.value === 'basic')
|
(normalizedPlanType.value === 'free' ||
|
||||||
|
normalizedPlanType.value === 'basic' ||
|
||||||
|
normalizedPlanType.value === 'xbasic')
|
||||||
)
|
)
|
||||||
|
|
||||||
const planIconName = computed<'bolt' | null>(() => {
|
const planIconName = computed<'bolt' | null>(() => {
|
||||||
@@ -204,7 +212,11 @@ const planBadgeClass = computed(() => {
|
|||||||
return 'bg-red-100 text-red-600 dark:bg-red-900/30 dark:text-red-400'
|
return 'bg-red-100 text-red-600 dark:bg-red-900/30 dark:text-red-400'
|
||||||
}
|
}
|
||||||
// Free stays muted gray; paid Grok tiers get distinct colors.
|
// Free stays muted gray; paid Grok tiers get distinct colors.
|
||||||
if (normalizedPlanType.value === 'free' || normalizedPlanType.value === 'basic') {
|
if (
|
||||||
|
normalizedPlanType.value === 'free' ||
|
||||||
|
normalizedPlanType.value === 'basic' ||
|
||||||
|
normalizedPlanType.value === 'xbasic'
|
||||||
|
) {
|
||||||
return 'bg-gray-100 text-gray-600 dark:bg-gray-700 dark:text-gray-300'
|
return 'bg-gray-100 text-gray-600 dark:bg-gray-700 dark:text-gray-300'
|
||||||
}
|
}
|
||||||
if (props.platform === 'grok' && normalizedPlanType.value) {
|
if (props.platform === 'grok' && normalizedPlanType.value) {
|
||||||
@@ -235,7 +247,11 @@ const planBadgeClass = computed(() => {
|
|||||||
// Subscription expiration label (non-free only)
|
// Subscription expiration label (non-free only)
|
||||||
const expiresLabel = computed(() => {
|
const expiresLabel = computed(() => {
|
||||||
if (!props.subscriptionExpiresAt || !props.planType) return ''
|
if (!props.subscriptionExpiresAt || !props.planType) return ''
|
||||||
if (normalizedPlanType.value === 'free' || normalizedPlanType.value === 'basic') return ''
|
if (
|
||||||
|
normalizedPlanType.value === 'free' ||
|
||||||
|
normalizedPlanType.value === 'basic' ||
|
||||||
|
normalizedPlanType.value === 'xbasic'
|
||||||
|
) return ''
|
||||||
try {
|
try {
|
||||||
const d = new Date(props.subscriptionExpiresAt)
|
const d = new Date(props.subscriptionExpiresAt)
|
||||||
if (isNaN(d.getTime())) return ''
|
if (isNaN(d.getTime())) return ''
|
||||||
|
|||||||
@@ -76,6 +76,12 @@ describe('PlatformTypeBadge Grok plans', () => {
|
|||||||
expect(heavy.text()).toContain('Heavy')
|
expect(heavy.text()).toContain('Heavy')
|
||||||
expect(heavy.html()).toContain('bg-purple-100')
|
expect(heavy.html()).toContain('bg-purple-100')
|
||||||
expect(heavy.find('[data-testid="grok-plan-icon"]').exists()).toBe(true)
|
expect(heavy.find('[data-testid="grok-plan-icon"]').exists()).toBe(true)
|
||||||
|
|
||||||
|
const lite = mount(PlatformTypeBadge, {
|
||||||
|
props: { platform: 'grok', type: 'oauth', planType: 'supergrok_lite' },
|
||||||
|
})
|
||||||
|
expect(lite.text()).toContain('SuperGrok Lite')
|
||||||
|
expect(lite.html()).toContain('bg-cyan-100')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('uses a dedicated 12px currentColor Grok mark with a Free sparkle', () => {
|
it('uses a dedicated 12px currentColor Grok mark with a Free sparkle', () => {
|
||||||
|
|||||||
@@ -1481,8 +1481,7 @@ const { pause: pauseAutoRefresh, resume: resumeAutoRefresh } = useIntervalFn(
|
|||||||
{ immediate: false }
|
{ immediate: false }
|
||||||
)
|
)
|
||||||
|
|
||||||
// Fresh billing/quota snapshots are authoritative. Imported credential tiers
|
// Persisted credentials.subscription_tier outranks lagging billing/quota snapshots.
|
||||||
// can be stale, so they remain fallbacks together with legacy plan_type fields.
|
|
||||||
function getAccountPlanType(row: any): string | undefined {
|
function getAccountPlanType(row: any): string | undefined {
|
||||||
if (!row) return undefined
|
if (!row) return undefined
|
||||||
if (row.platform === 'grok') {
|
if (row.platform === 'grok') {
|
||||||
@@ -1490,9 +1489,9 @@ function getAccountPlanType(row: any): string | undefined {
|
|||||||
const billing = extra.grok_billing_snapshot as Record<string, any> | undefined
|
const billing = extra.grok_billing_snapshot as Record<string, any> | undefined
|
||||||
const quota = extra.grok_quota_snapshot as Record<string, any> | undefined
|
const quota = extra.grok_quota_snapshot as Record<string, any> | undefined
|
||||||
return (
|
return (
|
||||||
|
row.credentials?.subscription_tier ||
|
||||||
billing?.plan ||
|
billing?.plan ||
|
||||||
quota?.subscription_tier ||
|
quota?.subscription_tier ||
|
||||||
row.credentials?.subscription_tier ||
|
|
||||||
extra.subscription_tier ||
|
extra.subscription_tier ||
|
||||||
row.credentials?.plan_type ||
|
row.credentials?.plan_type ||
|
||||||
row.parent_plan_type ||
|
row.parent_plan_type ||
|
||||||
|
|||||||
@@ -346,7 +346,7 @@ describe('admin AccountsView — 账号行展示', () => {
|
|||||||
wrapper.unmount()
|
wrapper.unmount()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('passes fresh Grok billing and quota snapshots before stale credential fallbacks', async () => {
|
it('prefers persisted Grok JWT tier over lagging billing/quota snapshots', async () => {
|
||||||
const grokAccounts = [
|
const grokAccounts = [
|
||||||
{
|
{
|
||||||
id: 201,
|
id: 201,
|
||||||
@@ -411,9 +411,9 @@ describe('admin AccountsView — 账号行展示', () => {
|
|||||||
|
|
||||||
const badges = wrapper.findAllComponents(PlatformTypeBadge)
|
const badges = wrapper.findAllComponents(PlatformTypeBadge)
|
||||||
expect(badges.map((badge) => badge.props('planType'))).toEqual([
|
expect(badges.map((badge) => badge.props('planType'))).toEqual([
|
||||||
'SuperGrok',
|
'FREE',
|
||||||
'SuperGrok Heavy',
|
'SuperGrok Heavy',
|
||||||
'SuperGrok',
|
'FREE',
|
||||||
'BASIC',
|
'BASIC',
|
||||||
'SuperGrok',
|
'SuperGrok',
|
||||||
])
|
])
|
||||||
|
|||||||
Reference in new issue
Block a user