mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
feat(webui): config page
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { filterSections, splitConfigSections } from '@/lib/config-sections'
|
||||
|
||||
const YAML = ['admin:', ' enabled: true', 'agents:', ' a:', ' url: x', ' b:', ' url: y', '"odd/key":', ' v: 1', 'redis:', ' addr: r'].join('\n')
|
||||
|
||||
describe('splitConfigSections', () => {
|
||||
it('splits at each top-level key with 1-based start lines and line counts', () => {
|
||||
const s = splitConfigSections(YAML)
|
||||
expect(s.map((x) => [x.key, x.startLine, x.lineCount])).toEqual([
|
||||
['admin', 1, 2],
|
||||
['agents', 3, 5],
|
||||
['odd/key', 8, 2],
|
||||
['redis', 10, 2],
|
||||
])
|
||||
})
|
||||
|
||||
it('keeps each section text verbatim so the sections join back to the document', () => {
|
||||
expect(splitConfigSections(YAML).map((x) => x.text).join('\n')).toBe(YAML)
|
||||
})
|
||||
|
||||
it('returns no sections for empty text', () => {
|
||||
expect(splitConfigSections('')).toEqual([])
|
||||
})
|
||||
|
||||
it('does not treat list items or comments at column 0 as keys', () => {
|
||||
const s = splitConfigSections(['a:', '- x: 1', '# note', 'b: 2'].join('\n'))
|
||||
expect(s.map((x) => x.key)).toEqual(['a', 'b'])
|
||||
expect(s[0].lineCount).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe('filterSections', () => {
|
||||
const s = splitConfigSections(YAML)
|
||||
it('returns all sections for a blank query', () => {
|
||||
expect(filterSections(s, ' ')).toHaveLength(4)
|
||||
})
|
||||
it('matches keys case-insensitively by substring', () => {
|
||||
expect(filterSections(s, 'AG').map((x) => x.key)).toEqual(['agents'])
|
||||
})
|
||||
it('returns none when nothing matches', () => {
|
||||
expect(filterSections(s, 'zzz')).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,40 @@
|
||||
export interface ConfigSection {
|
||||
key: string
|
||||
/** 1-based line of the key in the full document. */
|
||||
startLine: number
|
||||
lineCount: number
|
||||
text: string
|
||||
}
|
||||
|
||||
const TOP_KEY = /^([^\s#-][^:]*|"[^"]*"|'[^']*'):/
|
||||
|
||||
function unquote(raw: string): string {
|
||||
const q = raw[0]
|
||||
return (q === '"' || q === "'") && raw.endsWith(q) ? raw.slice(1, -1) : raw
|
||||
}
|
||||
|
||||
/** Splits a block-style YAML document at its column-0 keys; joining the section texts with newlines gives the document back. */
|
||||
export function splitConfigSections(yaml: string): ConfigSection[] {
|
||||
if (yaml === '') return []
|
||||
const out: ConfigSection[] = []
|
||||
const lines = yaml.split('\n')
|
||||
let current: { key: string; startLine: number; lines: string[] } | null = null
|
||||
const flush = (): void => {
|
||||
if (current) out.push({ key: current.key, startLine: current.startLine, lineCount: current.lines.length, text: current.lines.join('\n') })
|
||||
}
|
||||
lines.forEach((line, i) => {
|
||||
const m = TOP_KEY.exec(line)
|
||||
if (m) {
|
||||
flush()
|
||||
current = { key: unquote(m[1]), startLine: i + 1, lines: [line] }
|
||||
} else current?.lines.push(line)
|
||||
})
|
||||
flush()
|
||||
return out
|
||||
}
|
||||
|
||||
/** Sections whose key contains the query, case-insensitively; a blank query keeps all. */
|
||||
export function filterSections(sections: ConfigSection[], query: string): ConfigSection[] {
|
||||
const q = query.trim().toLowerCase()
|
||||
return q === '' ? sections : sections.filter((s) => s.key.toLowerCase().includes(q))
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { HELPER_IDS, HELPER_LABEL, formatHelperSel, parseHelperSel } from '@/lib/helper-forms'
|
||||
|
||||
describe('helper selection', () => {
|
||||
it('round-trips every helper id', () => {
|
||||
for (const id of HELPER_IDS) expect(parseHelperSel(formatHelperSel(id))).toBe(id)
|
||||
})
|
||||
it('formats as helper:<id>', () => {
|
||||
expect(formatHelperSel('router')).toBe('helper:router')
|
||||
})
|
||||
it('rejects absent, unknown and non-helper values', () => {
|
||||
expect(parseHelperSel(undefined)).toBeNull()
|
||||
expect(parseHelperSel('helper:nope')).toBeNull()
|
||||
expect(parseHelperSel('helper:')).toBeNull()
|
||||
expect(parseHelperSel('user:router')).toBeNull()
|
||||
})
|
||||
it('opens the first form for a bare "helper" selection', () => {
|
||||
expect(parseHelperSel('helper')).toBe(HELPER_IDS[0])
|
||||
})
|
||||
it('has a label for every id', () => {
|
||||
for (const id of HELPER_IDS) expect(HELPER_LABEL[id]).toBeTruthy()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,27 @@
|
||||
/** The ChangeHelper forms, in picker order; the single source for the Config page's `sel=helper:<id>` hash and the form switch. */
|
||||
export const HELPER_IDS = ['limits', 'grant', 'pricing', 'model', 'router', 'user'] as const
|
||||
|
||||
export type HelperId = (typeof HELPER_IDS)[number]
|
||||
|
||||
export const HELPER_LABEL: Record<HelperId, string> = {
|
||||
limits: 'Limits',
|
||||
grant: 'Access grant',
|
||||
pricing: 'Pricing',
|
||||
model: 'Model metadata',
|
||||
router: 'Smart router',
|
||||
user: 'New user',
|
||||
}
|
||||
|
||||
const PREFIX = 'helper'
|
||||
|
||||
export function formatHelperSel(id: HelperId): string {
|
||||
return `${PREFIX}:${id}`
|
||||
}
|
||||
|
||||
/** A bare `helper` opens the first form; an unknown id or foreign kind is null. */
|
||||
export function parseHelperSel(v: string | undefined): HelperId | null {
|
||||
if (v === PREFIX) return HELPER_IDS[0]
|
||||
if (!v?.startsWith(`${PREFIX}:`)) return null
|
||||
const id = v.slice(PREFIX.length + 1)
|
||||
return (HELPER_IDS as readonly string[]).includes(id) ? (id as HelperId) : null
|
||||
}
|
||||
Reference in New Issue
Block a user