Files
traefik-llmgateway/webui/src/lib/access-matrix.spec.ts
T
lukaszraczylo cf308624af feat(webui): make the dashboard responsive and add loading and empty states
Every tab row scrolls sideways instead of being clipped by its card.
On phones this hid tabs, such as the Config change helper's last forms
at 360-390px. The login button has a larger touch target, and long
names are truncated with a tooltip.

Loading and empty states:
- Skeletons show only while a selection has no data yet, never over
  data already on screen during a refresh.
- Empty states name the next step to take.
- Error states offer a Retry button that shows progress, and each card
  keeps its own error.
- Toasts confirm copies and key generation, and report background
  failures once per streak through polite and assertive live regions.

Other changes:
- The access matrix switches between Models, MCP servers and Agents,
  and says "none configured" only once its data has loaded.
- Home ranks the top 5 users by cost, requests or tokens. It requests
  only the needed metrics and warns before a range exceeds the
  server's key cap.
- MCP and agent deep links accept a bare target name.

admin_assets_gen.go is regenerated.

Claude-Session: https://claude.ai/code/session_01V5jYJNxpDFdJkyyExpADrv
2026-09-23 23:02:22 +01:00

165 lines
6.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
buildAccessMatrix,
MATRIX_KIND_LABEL,
MATRIX_KINDS,
matrixColumnsState,
parseMatrixKind,
providerAccessCell,
targetAccessCell,
} from './access-matrix'
import type { AdminConsumerGroup, AdminTargetView } from '@/types/api'
function group(overrides: Partial<AdminConsumerGroup> = {}): AdminConsumerGroup {
return {
name: 'friends',
allowedProviders: [],
modelAccess: {},
memberCount: 3,
...overrides,
}
}
function target(overrides: Partial<AdminTargetView> = {}): AdminTargetView {
return {
name: 'fetch',
url: 'http://mcp-fetch/mcp',
counters: { requestsPerMinute: 0, requestsPerDay: 0, requestsPerMonth: 0 },
health: { state: 'unknown', consecutiveFailures: 0 },
...overrides,
}
}
describe('providerAccessCell', () => {
it('not allowed when the provider is absent from allowedProviders', () => {
const g = group({ allowedProviders: ['minimax'] })
expect(providerAccessCell(g, 'uni')).toEqual({ allowed: false })
})
it('allowed with a "n/m models" detail when modelAccess has an entry', () => {
const g = group({ allowedProviders: ['minimax'], modelAccess: { minimax: { allowed: 2, total: 5 } } })
expect(providerAccessCell(g, 'minimax')).toEqual({ allowed: true, detail: '2/5 models' })
})
it('allowed with no detail when modelAccess has no entry for this provider', () => {
const g = group({ allowedProviders: ['minimax'], modelAccess: {} })
expect(providerAccessCell(g, 'minimax')).toEqual({ allowed: true, detail: undefined })
})
// P3 item 18: allowed but 0 of the provider's models are reachable
// (a `models` glob matching none of it) must NOT render identically to
// an ordinary, genuinely-unrestricted allowed cell.
it('sets zero: true when the provider is allowed but 0 of its models are reachable', () => {
const g = group({ allowedProviders: ['minimax'], modelAccess: { minimax: { allowed: 0, total: 5 } } })
expect(providerAccessCell(g, 'minimax')).toEqual({ allowed: true, detail: '0/5 models', zero: true })
})
it('does NOT set zero when the provider itself has 0 total models (unrelated to the grant)', () => {
const g = group({ allowedProviders: ['minimax'], modelAccess: { minimax: { allowed: 0, total: 0 } } })
expect(providerAccessCell(g, 'minimax')).toEqual({ allowed: true, detail: '0/0 models' })
})
})
describe('targetAccessCell', () => {
it('allowed for every group when access is undefined (unrestricted)', () => {
expect(targetAccessCell(group({ name: 'friends' }), target({ access: undefined }))).toEqual({ allowed: true })
})
it('allowed for every group when access is null (unrestricted)', () => {
expect(targetAccessCell(group({ name: 'friends' }), target({ access: null }))).toEqual({ allowed: true })
})
it('not allowed for any group when access is an empty array', () => {
expect(targetAccessCell(group({ name: 'friends' }), target({ access: [] }))).toEqual({ allowed: false })
})
it('allowed only for a named group', () => {
expect(targetAccessCell(group({ name: 'friends' }), target({ access: ['friends'] }))).toEqual({ allowed: true })
expect(targetAccessCell(group({ name: 'home' }), target({ access: ['friends'] }))).toEqual({ allowed: false })
})
})
describe('buildAccessMatrix', () => {
it('builds one row per group, keyed by column name', () => {
const groups = [
group({ name: 'home', allowedProviders: ['anthropic', 'openai'], modelAccess: { anthropic: { allowed: 5, total: 5 } } }),
group({ name: 'friends', allowedProviders: ['minimax'] }),
]
const mcp = [target({ name: 'fetch', access: undefined }), target({ name: 'weather', access: ['home'] })]
const agents = [target({ name: 'agentkit', access: [] })]
const rows = buildAccessMatrix(groups, ['anthropic', 'openai', 'minimax'], mcp, agents)
expect(rows).toHaveLength(2)
expect(rows[0].group).toBe('home')
expect(rows[0].providers.anthropic).toEqual({ allowed: true, detail: '5/5 models' })
expect(rows[0].providers.openai).toEqual({ allowed: true, detail: undefined })
expect(rows[0].providers.minimax).toEqual({ allowed: false })
expect(rows[0].mcpServers.fetch).toEqual({ allowed: true })
expect(rows[0].mcpServers.weather).toEqual({ allowed: true })
expect(rows[0].agents.agentkit).toEqual({ allowed: false })
expect(rows[1].group).toBe('friends')
expect(rows[1].mcpServers.weather).toEqual({ allowed: false })
})
it('returns an empty array for no groups', () => {
expect(buildAccessMatrix([], ['anthropic'], [], [])).toEqual([])
})
})
describe('MATRIX_KINDS', () => {
it('orders the three kinds models, mcp, agents', () => {
expect(MATRIX_KINDS).toEqual(['models', 'mcp', 'agents'])
})
it('labels every kind', () => {
expect(MATRIX_KIND_LABEL).toEqual({ models: 'Models', mcp: 'MCP servers', agents: 'Agents' })
})
})
describe('parseMatrixKind', () => {
it('parses each valid kind', () => {
expect(parseMatrixKind('models')).toBe('models')
expect(parseMatrixKind('mcp')).toBe('mcp')
expect(parseMatrixKind('agents')).toBe('agents')
})
it('falls back to models for an unknown value', () => {
expect(parseMatrixKind('bogus')).toBe('models')
})
it('falls back to models when undefined', () => {
expect(parseMatrixKind(undefined)).toBe('models')
})
})
describe('matrixColumnsState', () => {
it('is "loading" when the source has not loaded yet and there is no dashboard error', () => {
expect(matrixColumnsState(false, 0, '')).toBe('loading')
})
it('is "error" when the source has not loaded and dashboard.error is set', () => {
expect(matrixColumnsState(false, 0, 'overview: fetch failed')).toBe('error')
})
// Bug this helper fixes: a not-yet-loaded source maps to 0 columns via
// `?? []`, indistinguishable from a genuinely empty, loaded source by
// column count alone — so columnCount must never override sourceLoaded.
it('is "loading", not "empty", when the source has not loaded even though columnCount is 0', () => {
expect(matrixColumnsState(false, 0, '')).not.toBe('empty')
})
it('is "empty" when the source has loaded with zero columns', () => {
expect(matrixColumnsState(true, 0, '')).toBe('empty')
})
it('is "empty" when the source has loaded with zero columns even if a stale dashboard.error lingers', () => {
expect(matrixColumnsState(true, 0, 'usage: fetch failed')).toBe('empty')
})
it('is "ready" when the source has loaded with at least one column', () => {
expect(matrixColumnsState(true, 3, '')).toBe('ready')
})
})