From 9aac3b73f7f6d03ca40a151647f48a49757b41d8 Mon Sep 17 00:00:00 2001 From: xiaobo <467359395@qq.com> Date: Sun, 16 Aug 2026 21:33:53 -0700 Subject: [PATCH] add Ollama usage query action --- .../components/account/AccountUsageCell.vue | 5 ++ .../account/OllamaCloudUsageCell.vue | 54 +++++++++++++++++-- .../__tests__/AccountUsageCell.spec.ts | 11 +++- .../__tests__/OllamaCloudUsageCell.spec.ts | 53 +++++++++++++++--- 4 files changed, 111 insertions(+), 12 deletions(-) diff --git a/frontend/src/components/account/AccountUsageCell.vue b/frontend/src/components/account/AccountUsageCell.vue index 485b4b8701..5ad110087c 100644 --- a/frontend/src/components/account/AccountUsageCell.vue +++ b/frontend/src/components/account/AccountUsageCell.vue @@ -547,6 +547,7 @@
{ emit('account-updated', account) } +const handleOllamaCloudUsageUpdated = (state: NonNullable) => { + emit('account-updated', { ...props.account, ollama_cloud_usage: state }) +} + // ===== Key account today stats formatters ===== const formatKeyRequests = computed(() => { diff --git a/frontend/src/components/account/OllamaCloudUsageCell.vue b/frontend/src/components/account/OllamaCloudUsageCell.vue index 201e271fe5..17c09050c0 100644 --- a/frontend/src/components/account/OllamaCloudUsageCell.vue +++ b/frontend/src/components/account/OllamaCloudUsageCell.vue @@ -20,16 +20,64 @@ color="emerald" data-testid="ollama-cloud-seven-day" /> +
+ +
- diff --git a/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts b/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts index ff0e94d664..dc071b6de5 100644 --- a/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts +++ b/frontend/src/components/account/__tests__/AccountUsageCell.spec.ts @@ -72,7 +72,7 @@ describe('AccountUsageCell', () => { }) }) - it('renders eligible Ollama Cloud state inside the unified usage cell', () => { + it('renders eligible Ollama Cloud state and forwards query updates', async () => { const wrapper = mount(AccountUsageCell, { props: { account: makeAccount({ @@ -101,7 +101,8 @@ describe('AccountUsageCell', () => { stubs: { OllamaCloudUsageCell: { props: ['account'], - template: '
{{ account.ollama_cloud_usage.snapshot.data.five_hour.used_percent }}
' + emits: ['updated'], + template: '' }, UsageProgressBar: true, AccountQuotaInfo: true @@ -111,6 +112,12 @@ describe('AccountUsageCell', () => { expect(wrapper.get('[data-test="embedded-ollama"]').text()).toBe('12') expect(getUsage).not.toHaveBeenCalled() + + await wrapper.get('[data-test="embedded-ollama"]').trigger('click') + + const updatedAccount = wrapper.emitted('account-updated')?.[0]?.[0] + expect(updatedAccount?.id).toBe(9001) + expect(updatedAccount?.ollama_cloud_usage?.auto_refresh_enabled).toBe(false) }) it('Antigravity 图片用量会聚合新旧 image 模型', async () => { diff --git a/frontend/src/components/account/__tests__/OllamaCloudUsageCell.spec.ts b/frontend/src/components/account/__tests__/OllamaCloudUsageCell.spec.ts index 5f6dfee054..e38f3d3729 100644 --- a/frontend/src/components/account/__tests__/OllamaCloudUsageCell.spec.ts +++ b/frontend/src/components/account/__tests__/OllamaCloudUsageCell.spec.ts @@ -1,9 +1,21 @@ -import { mount } from '@vue/test-utils' -import { describe, expect, it, vi } from 'vitest' +import { flushPromises, mount } from '@vue/test-utils' +import { beforeEach, describe, expect, it, vi } from 'vitest' import OllamaCloudUsageCell from '../OllamaCloudUsageCell.vue' import UsageProgressBar from '../UsageProgressBar.vue' import type { Account, OllamaCloudUsageState } from '@/types' +const { refreshOllamaCloudUsage } = vi.hoisted(() => ({ + refreshOllamaCloudUsage: vi.fn() +})) + +vi.mock('@/api/admin', () => ({ + adminAPI: { + accounts: { + refreshOllamaCloudUsage + } + } +})) + vi.mock('vue-i18n', async () => { const actual = await vi.importActual('vue-i18n') return { @@ -64,7 +76,11 @@ const account = (state = usageState()): Account => ({ }) describe('OllamaCloudUsageCell', () => { - it('renders only native 5h and 7d windows in a shrinkable mobile-safe cell', () => { + beforeEach(() => { + refreshOllamaCloudUsage.mockReset() + }) + + it('renders native 5h and 7d windows with the configured-account query action', () => { const wrapper = mount(OllamaCloudUsageCell, { props: { account: account() } }) const cell = wrapper.get('[data-testid="ollama-cloud-usage-cell"]') expect(cell.classes()).toEqual(expect.arrayContaining(['min-w-0', 'max-w-full'])) @@ -84,14 +100,15 @@ describe('OllamaCloudUsageCell', () => { }) expect(wrapper.find('[data-testid="ollama-cloud-usage-details"]').exists()).toBe(false) - expect(wrapper.find('[data-testid="ollama-cloud-usage-refresh"]').exists()).toBe(false) - expect(wrapper.findAll('button')).toHaveLength(0) + const query = wrapper.get('[data-testid="ollama-cloud-usage-query"]') + expect(query.classes()).toEqual(expect.arrayContaining(['text-blue-600', 'hover:bg-blue-50'])) + expect(query.text()).toContain('admin.accounts.usageWindow.activeQuery') expect(wrapper.text()).not.toContain('max') expect(wrapper.text()).not.toContain('$0') expect(wrapper.text()).not.toContain('gpt-oss:120b-cloud') }) - it('reacts to an account snapshot update without a list-cell refresh action', async () => { + it('reacts to an account snapshot update', async () => { const wrapper = mount(OllamaCloudUsageCell, { props: { account: account() } }) const next = usageState() next.snapshot!.data!.five_hour!.used_percent = 43 @@ -99,6 +116,28 @@ describe('OllamaCloudUsageCell', () => { await wrapper.setProps({ account: account(next) }) expect(wrapper.findAllComponents(UsageProgressBar)[0].props('utilization')).toBe(43) - expect(wrapper.findAll('button')).toHaveLength(0) + }) + + it('queries through the edit-page refresh endpoint and emits the updated state', async () => { + const next = usageState() + next.snapshot!.data!.five_hour!.used_percent = 43 + refreshOllamaCloudUsage.mockResolvedValueOnce(next) + const wrapper = mount(OllamaCloudUsageCell, { props: { account: account() } }) + + await wrapper.get('[data-testid="ollama-cloud-usage-query"]').trigger('click') + await flushPromises() + + expect(refreshOllamaCloudUsage).toHaveBeenCalledWith(7) + expect(wrapper.findAllComponents(UsageProgressBar)[0].props('utilization')).toBe(43) + expect(wrapper.emitted('updated')?.[0]?.[0]).toEqual(next) + }) + + it('hides the query action until a browser session is configured', () => { + const state = usageState() + state.configured = false + + const wrapper = mount(OllamaCloudUsageCell, { props: { account: account(state) } }) + + expect(wrapper.find('[data-testid="ollama-cloud-usage-query"]').exists()).toBe(false) }) })