From 849aa593d4ac56519e8b5fb98fb7031d3cbe0d43 Mon Sep 17 00:00:00 2001 From: r266-tech Date: Thu, 12 Nov 2026 09:14:56 -0800 Subject: [PATCH 1/2] fix(cn-quota): refresh usage cells with account snapshots --- .../account/CNProviderQuotaCell.vue | 34 ++++++-- .../__tests__/CNProviderQuotaCell.spec.ts | 82 +++++++++++++++++++ 2 files changed, 107 insertions(+), 9 deletions(-) diff --git a/frontend/src/components/account/CNProviderQuotaCell.vue b/frontend/src/components/account/CNProviderQuotaCell.vue index d814173abb..9cddb9f088 100644 --- a/frontend/src/components/account/CNProviderQuotaCell.vue +++ b/frontend/src/components/account/CNProviderQuotaCell.vue @@ -4,7 +4,7 @@ data-test="cn-provider-quota" class="min-w-[220px] space-y-1" > -
cnQuotaCellVisible(props.account)) const loading = ref(false) const error = ref(null) -const data = ref(null) +const probeData = ref(null) +let probeGeneration = 0 // 后端周期任务/手动探测写入的 extra 快照键(_ 前缀,与后端 // cnQuotaExtraUpdates 对齐)。页面加载即有数据,无需等待探测。 @@ -118,6 +119,18 @@ const snapshotData = computed(() => { return { success: true, tiers } as CNProviderQuotaProbeResult }) +const data = computed(() => probeData.value ?? snapshotData.value) + +// A list refresh replaces account.extra even when its quota snapshot is +// unchanged. Keep a manual probe until the quota values or snapshot version +// actually change, rather than reverting it on unrelated account updates. +watch( + [snapshotData, () => readExtraString(`${props.account.platform}_usage_updated_at`)], + (next, previous) => { + if (JSON.stringify(next) !== JSON.stringify(previous)) probeData.value = null + } +) + // 快照是否过期(无更新时间或超过 staleness 窗口)→ 挂载时需要自动探测。 const snapshotIsStale = computed(() => { const updatedAt = readExtraString(`${props.account.platform}_usage_updated_at`) @@ -130,7 +143,6 @@ const snapshotIsStale = computed(() => { // 避免静默失败导致单元格空白无提示)。 onMounted(() => { if (!visible.value) return - data.value = snapshotData.value if (!snapshotIsStale.value) return // 模块级去抖:列表页每行一个实例,翻页/筛选/刷新会重复挂载;同一账号 // 短时间内已自动探测过则跳过,避免对上游形成探测风暴。 @@ -168,27 +180,31 @@ const windowLabel = (window: string) => { const handleProbe = async () => { if (loading.value) return + const generation = ++probeGeneration + const accountID = props.account.id loading.value = true error.value = null try { - const result = await adminAPI.cnProviders.queryQuota(props.account.id) + const result = await adminAPI.cnProviders.queryQuota(accountID) + if (generation !== probeGeneration) return // 失败时保留已渲染的快照条形图(仅显示错误行),成功才覆盖。 if (result.success) { - data.value = result + probeData.value = result } else { error.value = result.error || t('common.error') } } catch (e) { - error.value = extractErrorMessage(e) + if (generation === probeGeneration) error.value = extractErrorMessage(e) } finally { - loading.value = false + if (generation === probeGeneration) loading.value = false } } watch( - () => props.account.id, + [() => props.account.id, () => props.account.platform], () => { - data.value = null + probeGeneration++ + probeData.value = null error.value = null loading.value = false } diff --git a/frontend/src/components/account/__tests__/CNProviderQuotaCell.spec.ts b/frontend/src/components/account/__tests__/CNProviderQuotaCell.spec.ts index e0384fe72c..55c250dbd1 100644 --- a/frontend/src/components/account/__tests__/CNProviderQuotaCell.spec.ts +++ b/frontend/src/components/account/__tests__/CNProviderQuotaCell.spec.ts @@ -1,5 +1,6 @@ import { flushPromises, mount } from '@vue/test-utils' import { beforeEach, describe, expect, it, vi } from 'vitest' +import { reactive } from 'vue' import CNProviderQuotaCell from '../CNProviderQuotaCell.vue' import UsageProgressBar from '../UsageProgressBar.vue' import type { Account } from '@/types' @@ -102,4 +103,85 @@ describe('CNProviderQuotaCell', () => { await flushPromises() expect(queryQuota).toHaveBeenCalledWith(account.id) }) + + it('renders replacement quota values and windows for the same account without probing', async () => { + const wrapper = mount(CNProviderQuotaCell, { props: { account } }) + await wrapper.setProps({ account: { + ...account, + extra: { + ...account.extra, + zhipu_5h_used_percent: 52, + zhipu_weekly_used_percent: null, + zhipu_monthly_used_percent: 81, + zhipu_monthly_reset_at: '2026-12-01T00:00:00Z' + } + } }) + const bars = wrapper.findAllComponents(UsageProgressBar) + expect(bars.map(bar => bar.props('utilization'))).toEqual([52, 81]) + expect(bars[1].props('label')).toBe('admin.accounts.cnProviders.windowMonthly') + expect(bars[1].props('resetsAt')).toBe('2026-12-01T00:00:00Z') + expect(queryQuota).not.toHaveBeenCalled() + }) + + it('reacts to quota changes inside the existing extra object', async () => { + const row = reactive({ ...account, extra: { ...account.extra } }) + const wrapper = mount(CNProviderQuotaCell, { props: { account: row } }) + row.extra.zhipu_5h_used_percent = 63 + await flushPromises() + expect(wrapper.findAllComponents(UsageProgressBar)[0].props('utilization')).toBe(63) + expect(queryQuota).not.toHaveBeenCalled() + }) + + it('removes bars when a refreshed snapshot clears all quota windows', async () => { + const wrapper = mount(CNProviderQuotaCell, { props: { account } }) + await wrapper.setProps({ account: { + ...account, + extra: { ...account.extra, zhipu_5h_used_percent: null, zhipu_weekly_used_percent: null } + } }) + expect(wrapper.findAllComponents(UsageProgressBar)).toHaveLength(0) + expect(queryQuota).not.toHaveBeenCalled() + }) + + it('keeps a manual result through unrelated refreshes, then adopts a changed snapshot', async () => { + queryQuota.mockResolvedValue({ success: true, tiers: [{ window: '5h', used_percent: 44 }] }) + const wrapper = mount(CNProviderQuotaCell, { props: { account } }) + await wrapper.get('[data-test="cn-provider-quota-probe"]').trigger('click') + await flushPromises() + await wrapper.setProps({ account: { + ...account, extra: { ...account.extra, unrelated_field: true } + } }) + expect(wrapper.findAllComponents(UsageProgressBar).map(bar => bar.props('utilization'))).toEqual([44]) + await wrapper.setProps({ account: { + ...account, extra: { ...account.extra, zhipu_5h_used_percent: 66 } + } }) + expect(wrapper.findAllComponents(UsageProgressBar).map(bar => bar.props('utilization'))).toEqual([66, 27]) + expect(queryQuota).toHaveBeenCalledTimes(1) + }) + + it('adopts a new snapshot version even if its quota values equal the old snapshot', async () => { + queryQuota.mockResolvedValue({ success: true, tiers: [{ window: '5h', used_percent: 44 }] }) + const wrapper = mount(CNProviderQuotaCell, { props: { account } }) + await wrapper.get('[data-test="cn-provider-quota-probe"]').trigger('click') + await flushPromises() + await wrapper.setProps({ account: { + ...account, + extra: { ...account.extra, zhipu_usage_updated_at: new Date(Date.now() + 1000).toISOString() } + } }) + expect(wrapper.findAllComponents(UsageProgressBar).map(bar => bar.props('utilization'))).toEqual([0, 27]) + }) + + it('keeps the new account snapshot when the previous account probe finishes late', async () => { + let resolveProbe!: (value: unknown) => void + queryQuota.mockReturnValue(new Promise(resolve => { resolveProbe = resolve })) + const wrapper = mount(CNProviderQuotaCell, { props: { account } }) + await wrapper.get('[data-test="cn-provider-quota-probe"]').trigger('click') + await wrapper.setProps({ account: { + ...account, id: 8, extra: { ...account.extra, zhipu_5h_used_percent: 91 } + } }) + expect(wrapper.findAllComponents(UsageProgressBar)[0].props('utilization')).toBe(91) + resolveProbe({ success: true, tiers: [{ window: '5h', used_percent: 44 }] }) + await flushPromises() + expect(wrapper.findAllComponents(UsageProgressBar)[0].props('utilization')).toBe(91) + expect(wrapper.get('[data-test="cn-provider-quota-probe"]').attributes('disabled')).toBeUndefined() + }) }) From e95e9eb748b9eb2c2abd90abc8f4b7c5d73f3b11 Mon Sep 17 00:00:00 2001 From: r266-tech Date: Thu, 12 Nov 2026 09:20:16 -0800 Subject: [PATCH 2/2] test(cn-quota): include snapshot regressions in critical CI --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 4a853cefdd..df5b97fb8b 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ FRONTEND_CRITICAL_VITEST := \ src/components/account/__tests__/OpenAIQuotaResetCell.spark_shadow.spec.ts \ src/constants/__tests__/platforms.spec.ts \ src/components/account/__tests__/credentialsBuilder.platformCatalog.spec.ts \ + src/components/account/__tests__/CNProviderQuotaCell.spec.ts \ src/components/account/__tests__/CreateAccountModal.spec.ts \ src/components/account/__tests__/EditAccountModal.spec.ts \ src/components/account/__tests__/credentialsBuilder.spec.ts \