diff --git a/frontend/src/components/admin/channel/__tests__/types.spec.ts b/frontend/src/components/admin/channel/__tests__/types.spec.ts new file mode 100644 index 00000000..9fd8e066 --- /dev/null +++ b/frontend/src/components/admin/channel/__tests__/types.spec.ts @@ -0,0 +1,79 @@ +import { describe, expect, it } from 'vitest' +import { validateIntervals, type IntervalFormEntry } from '../types' + +function makeInterval(over: Partial): IntervalFormEntry { + return { + min_tokens: 0, + max_tokens: null, + tier_label: '', + input_price: null, + output_price: null, + cache_write_price: null, + cache_read_price: null, + per_request_price: null, + sort_order: 0, + ...over, + } +} + +describe('validateIntervals', () => { + describe('token mode', () => { + it('rejects unbounded interval that is not last', () => { + const intervals: IntervalFormEntry[] = [ + makeInterval({ min_tokens: 0, max_tokens: null, input_price: 1, output_price: 1 }), + makeInterval({ min_tokens: 200000, max_tokens: 500000, input_price: 2, output_price: 2 }), + ] + expect(validateIntervals(intervals, 'token')).toMatch(/无上限/) + }) + + it('accepts unbounded interval at the end', () => { + const intervals: IntervalFormEntry[] = [ + makeInterval({ min_tokens: 0, max_tokens: 200000, input_price: 1, output_price: 1 }), + makeInterval({ min_tokens: 200000, max_tokens: null, input_price: 2, output_price: 2 }), + ] + expect(validateIntervals(intervals, 'token')).toBeNull() + }) + + it('rejects overlapping intervals', () => { + const intervals: IntervalFormEntry[] = [ + makeInterval({ min_tokens: 0, max_tokens: 250000, input_price: 1, output_price: 1 }), + makeInterval({ min_tokens: 200000, max_tokens: 500000, input_price: 2, output_price: 2 }), + ] + expect(validateIntervals(intervals, 'token')).toMatch(/重叠/) + }) + + it('defaults mode to token when omitted', () => { + const intervals: IntervalFormEntry[] = [ + makeInterval({ min_tokens: 0, max_tokens: null, input_price: 1, output_price: 1 }), + makeInterval({ min_tokens: 100, max_tokens: 200, input_price: 2, output_price: 2 }), + ] + expect(validateIntervals(intervals)).toMatch(/无上限/) + }) + }) + + describe('image / per_request mode', () => { + it('allows multiple unbounded tiers identified by label', () => { + const intervals: IntervalFormEntry[] = [ + makeInterval({ tier_label: '1K', per_request_price: 0.04 }), + makeInterval({ tier_label: '2K', per_request_price: 0.06 }), + makeInterval({ tier_label: '4K', per_request_price: 0.08 }), + ] + expect(validateIntervals(intervals, 'image')).toBeNull() + expect(validateIntervals(intervals, 'per_request')).toBeNull() + }) + + it('still rejects negative prices', () => { + const intervals: IntervalFormEntry[] = [ + makeInterval({ tier_label: '1K', per_request_price: -1 }), + ] + expect(validateIntervals(intervals, 'image')).toMatch(/不能为负数/) + }) + + it('still rejects max <= min on a single tier', () => { + const intervals: IntervalFormEntry[] = [ + makeInterval({ tier_label: '1K', min_tokens: 100, max_tokens: 50, per_request_price: 0.04 }), + ] + expect(validateIntervals(intervals, 'image')).toMatch(/必须大于/) + }) + }) +}) diff --git a/frontend/src/components/admin/channel/types.ts b/frontend/src/components/admin/channel/types.ts index 955b6487..0f3cfc20 100644 --- a/frontend/src/components/admin/channel/types.ts +++ b/frontend/src/components/admin/channel/types.ts @@ -115,8 +115,17 @@ export function findModelConflict(models: string[]): [string, string] | null { // ── 区间校验 ────────────────────────────────────────────── -/** 校验区间列表的合法性,返回错误消息;通过则返回 null */ -export function validateIntervals(intervals: IntervalFormEntry[]): string | null { +/** 校验区间列表的合法性,返回错误消息;通过则返回 null + * + * mode 决定区间语义: + * - token:区间是上下文 token 数分段 (min, max],不能重叠,无上限段必须放最后 + * - per_request / image:区间是按 tier_label 分层(1K/2K/4K 等),后端按 label + * 匹配,不依赖 min/max,因此跳过重叠 / last-unlimited 校验 + */ +export function validateIntervals( + intervals: IntervalFormEntry[], + mode: BillingMode = 'token', +): string | null { if (!intervals || intervals.length === 0) return null // 按 min_tokens 排序(不修改原数组) @@ -126,6 +135,9 @@ export function validateIntervals(intervals: IntervalFormEntry[]): string | null const err = validateSingleInterval(sorted[i], i) if (err) return err } + + // per_request / image 模式按 tier_label 匹配,不做 token 区间重叠校验 + if (mode !== 'token') return null return checkIntervalOverlap(sorted) } diff --git a/frontend/src/views/admin/ChannelsView.vue b/frontend/src/views/admin/ChannelsView.vue index 89be573e..b2d6d8e6 100644 --- a/frontend/src/views/admin/ChannelsView.vue +++ b/frontend/src/views/admin/ChannelsView.vue @@ -1418,7 +1418,7 @@ async function handleSubmit() { for (const section of form.platforms.filter(s => s.enabled)) { for (const entry of section.model_pricing) { if (!entry.intervals || entry.intervals.length === 0) continue - const intervalErr = validateIntervals(entry.intervals) + const intervalErr = validateIntervals(entry.intervals, entry.billing_mode) if (intervalErr) { const platformLabel = t('admin.groups.platforms.' + section.platform, section.platform) const modelLabel = entry.models.join(', ') || t('admin.channels.form.unnamed')