mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-21 07:04:45 +08:00
feat(admin): add metric toggle to usage charts
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
|
||||
import ModelDistributionChart from '../ModelDistributionChart.vue'
|
||||
|
||||
const messages: Record<string, string> = {
|
||||
'admin.dashboard.modelDistribution': 'Model Distribution',
|
||||
'admin.dashboard.model': 'Model',
|
||||
'admin.dashboard.requests': 'Requests',
|
||||
'admin.dashboard.tokens': 'Tokens',
|
||||
'admin.dashboard.actual': 'Actual',
|
||||
'admin.dashboard.standard': 'Standard',
|
||||
'admin.dashboard.metricTokens': 'By Tokens',
|
||||
'admin.dashboard.metricActualCost': 'By Actual Cost',
|
||||
'admin.dashboard.noDataAvailable': 'No data available',
|
||||
}
|
||||
|
||||
vi.mock('vue-i18n', async () => {
|
||||
const actual = await vi.importActual<typeof import('vue-i18n')>('vue-i18n')
|
||||
return {
|
||||
...actual,
|
||||
useI18n: () => ({
|
||||
t: (key: string) => messages[key] ?? key,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('vue-chartjs', () => ({
|
||||
Doughnut: {
|
||||
props: ['data'],
|
||||
template: '<div class="chart-data">{{ JSON.stringify(data) }}</div>',
|
||||
},
|
||||
}))
|
||||
|
||||
describe('ModelDistributionChart', () => {
|
||||
const modelStats = [
|
||||
{
|
||||
model: 'model-a',
|
||||
requests: 8,
|
||||
input_tokens: 100,
|
||||
output_tokens: 50,
|
||||
cache_creation_tokens: 0,
|
||||
cache_read_tokens: 0,
|
||||
total_tokens: 1000,
|
||||
cost: 1.5,
|
||||
actual_cost: 0.2,
|
||||
},
|
||||
{
|
||||
model: 'model-b',
|
||||
requests: 3,
|
||||
input_tokens: 40,
|
||||
output_tokens: 20,
|
||||
cache_creation_tokens: 0,
|
||||
cache_read_tokens: 0,
|
||||
total_tokens: 500,
|
||||
cost: 0.5,
|
||||
actual_cost: 1.4,
|
||||
},
|
||||
]
|
||||
|
||||
it('uses total_tokens and token ordering by default', () => {
|
||||
const wrapper = mount(ModelDistributionChart, {
|
||||
props: {
|
||||
modelStats,
|
||||
},
|
||||
global: {
|
||||
stubs: {
|
||||
LoadingSpinner: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const chartData = JSON.parse(wrapper.find('.chart-data').text())
|
||||
expect(chartData.labels).toEqual(['model-a', 'model-b'])
|
||||
expect(chartData.datasets[0].data).toEqual([1000, 500])
|
||||
|
||||
const rows = wrapper.findAll('tbody tr')
|
||||
expect(rows[0].text()).toContain('model-a')
|
||||
expect(rows[1].text()).toContain('model-b')
|
||||
|
||||
const options = (wrapper.vm as any).$?.setupState.doughnutOptions
|
||||
const label = options.plugins.tooltip.callbacks.label({
|
||||
label: 'model-a',
|
||||
raw: 1000,
|
||||
dataset: { data: [1000, 500] },
|
||||
})
|
||||
expect(label).toBe('model-a: 1.00K (66.7%)')
|
||||
})
|
||||
|
||||
it('uses actual_cost and reorders rows in actual cost mode', () => {
|
||||
const wrapper = mount(ModelDistributionChart, {
|
||||
props: {
|
||||
modelStats,
|
||||
metric: 'actual_cost',
|
||||
},
|
||||
global: {
|
||||
stubs: {
|
||||
LoadingSpinner: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const chartData = JSON.parse(wrapper.find('.chart-data').text())
|
||||
expect(chartData.labels).toEqual(['model-b', 'model-a'])
|
||||
expect(chartData.datasets[0].data).toEqual([1.4, 0.2])
|
||||
|
||||
const rows = wrapper.findAll('tbody tr')
|
||||
expect(rows[0].text()).toContain('model-b')
|
||||
expect(rows[1].text()).toContain('model-a')
|
||||
|
||||
const options = (wrapper.vm as any).$?.setupState.doughnutOptions
|
||||
const label = options.plugins.tooltip.callbacks.label({
|
||||
label: 'model-b',
|
||||
raw: 1.4,
|
||||
dataset: { data: [1.4, 0.2] },
|
||||
})
|
||||
expect(label).toBe('model-b: $1.40 (87.5%)')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user