Files
traefik-llmgateway/webui/src/stores/usage-users.spec.ts
T
lukaszraczyloandGitHub 979d903c19 feat: admin redesign, decision assist, prompt caching and shared state (#11)
This change adds the redesigned admin panel and the smart router decision model assist.
It adds opt-in prompt-prefix caching per provider and cached-token pricing and metrics.
With Redis, the metrics route reports fleet-wide values. The redis.sharedState switch shares six coordination states across replicas.
PR CI grants actions read, which the shared go-pr workflow needs.
The pull request description has the full list and the verification.
2026-11-26 15:50:11 +00:00

60 lines
2.2 KiB
TypeScript

import { createPinia, setActivePinia } from 'pinia'
import { beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('@/lib/api', async () => {
const actual = await vi.importActual<typeof import('@/lib/api')>('@/lib/api')
return { ...actual, adminFetch: vi.fn() }
})
import { AdminApiError, adminFetch } from '@/lib/api'
import { useAuthStore } from '@/stores/auth'
import { useDashboardStore } from '@/stores/dashboard'
import { USERS_RANGE_TOO_LARGE, useUsageUsersStore } from '@/stores/usage-users'
const mocked = vi.mocked(adminFetch)
function route(url: string): unknown {
const u = new URL(url, 'http://x')
if (u.pathname.endsWith('/usage/totals')) {
return { rows: [{ id: 'bob', values: { cost: 3 } }, { id: 'alice', values: { cost: 9 } }, { id: 'idle', values: { cost: 0 } }] }
}
const scopes = u.searchParams.getAll('scope')
return { series: scopes.map((scope) => ({ scope, points: scope === 'total' ? [20, 20] : [4, 4] })) }
}
describe('useUsageUsersStore.refresh', () => {
beforeEach(() => {
setActivePinia(createPinia())
mocked.mockReset()
mocked.mockImplementation(async (url: string) => route(url) as never)
useAuthStore().submit('test-admin-key')
})
it('ranks active users by the metric and derives the other bucket from the total', async () => {
const s = useUsageUsersStore()
await s.refresh('cost')
expect(s.rows).toEqual([{ id: 'alice', value: 9 }, { id: 'bob', value: 3 }])
expect(s.series.map((x) => x.scope)).toEqual(['user:alice', 'user:bob'])
expect(s.otherPoints).toEqual([12, 12])
expect(s.loaded).toBe(true)
expect(s.error).toBe('')
})
it('refuses ranges whose user count would exceed the per-request key cap', async () => {
const dashboard = useDashboardStore()
dashboard.usage = { users: new Array(20000).fill({ id: 'u' }) } as never
const s = useUsageUsersStore()
await s.refresh('cost')
expect(s.error).toBe(USERS_RANGE_TOO_LARGE)
expect(mocked).not.toHaveBeenCalled()
})
it('reports a fetch failure and stays unloaded', async () => {
mocked.mockRejectedValue(new AdminApiError('boom', 503))
const s = useUsageUsersStore()
await s.refresh('cost')
expect(s.error).toBe('boom')
expect(s.loaded).toBe(false)
})
})