mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 00:05:30 +00:00
fix(admin): mirror the full quota platform list in the frontend
后端对 default_platform_quotas(setting_update.go)与用户平台配额 (user_handler.go PUT)都是整体替换语义,且以 AllowedQuotaPlatforms (11 平台)为白名单。前端配额平台镜像此前只硬编码 5 平台 (anthropic/openai/gemini/antigravity/grok),导致: - 保存 Settings 会静默丢弃 kimi/zhipu/deepseek/minimax/opencode_go/ ollama_cloud 六个平台的默认配额; - 管理员在用户配额弹窗不做修改直接保存,就会软删该用户经 API 配置的 ollama_cloud 等平台配额行(缺行等价于不限额 → 限额 fail-open)。 - settings.ts 新增单一常量 QUOTA_PLATFORMS(对齐后端 AllowedQuotaPlatforms 全部 11 平台),PlatformType/PlatformQuotaPlatform 由它派生,删除其余 5 平台硬编码副本(users.ts、配额弹窗、 UserPlatformQuotaCell、SettingsView 两处模板字面量); - normalizePlatformQuotasMap / sanitizePlatformQuotasMap 保留输入中 前端未知(后端新加)的平台 key;用户配额弹窗保留响应中的未知平台行, 避免后端先行加平台时再次静默删数据; - 测试:用户配额弹窗预置 ollama_cloud 不修改直接保存仍包含该配额; Settings 同路径覆盖;kimi/zhipu/deepseek/minimax/opencode_go 不再 被丢;未知平台 key 保留。
This commit is contained in:
1 parent
4a53d49717
commit
ea8489cd41
8 files changed
+264
-54
No files matched your search
@@ -5,18 +5,15 @@ import {
|
||||
buildAuthSourceDefaultsState,
|
||||
normalizePlatformQuotasMap,
|
||||
sanitizePlatformQuotasMap,
|
||||
QUOTA_PLATFORMS,
|
||||
type UpdateSettingsRequest,
|
||||
type DefaultPlatformQuotasMap,
|
||||
} from "@/api/admin/settings";
|
||||
|
||||
/** 全 null 的 5 平台 map,用于断言归一化默认值 */
|
||||
const allNullQuotas: DefaultPlatformQuotasMap = {
|
||||
anthropic: { daily: null, weekly: null, monthly: null },
|
||||
openai: { daily: null, weekly: null, monthly: null },
|
||||
gemini: { daily: null, weekly: null, monthly: null },
|
||||
antigravity: { daily: null, weekly: null, monthly: null },
|
||||
grok: { daily: null, weekly: null, monthly: null },
|
||||
}
|
||||
/** 全 null 的全平台 map,用于断言归一化默认值 */
|
||||
const allNullQuotas: DefaultPlatformQuotasMap = Object.fromEntries(
|
||||
QUOTA_PLATFORMS.map((p) => [p, { daily: null, weekly: null, monthly: null }] as const),
|
||||
);
|
||||
|
||||
describe("admin settings auth source defaults helpers", () => {
|
||||
it("builds auth source defaults state from flat settings fields", () => {
|
||||
@@ -240,14 +237,47 @@ describe("normalizePlatformQuotasMap", () => {
|
||||
expect(result.grok).toEqual({ daily: null, weekly: null, monthly: null });
|
||||
});
|
||||
|
||||
it("无参数时返回全 5 平台全 null", () => {
|
||||
it("无参数时返回全平台(对齐后端 AllowedQuotaPlatforms)全 null", () => {
|
||||
const result = normalizePlatformQuotasMap();
|
||||
expect(Object.keys(result)).toHaveLength(5);
|
||||
expect(Object.keys(result)).toHaveLength(QUOTA_PLATFORMS.length);
|
||||
for (const v of Object.values(result)) {
|
||||
expect(v).toEqual({ daily: null, weekly: null, monthly: null });
|
||||
}
|
||||
});
|
||||
|
||||
it("预置 ollama_cloud 配额在 normalize 后保留(不再被旧 5 平台列表丢弃)", () => {
|
||||
const result = normalizePlatformQuotasMap({
|
||||
ollama_cloud: { daily: 3, weekly: 30, monthly: 300 },
|
||||
kimi: { daily: 1, weekly: null, monthly: null },
|
||||
zhipu: { daily: null, weekly: 2, monthly: null },
|
||||
deepseek: { daily: null, weekly: null, monthly: 4 },
|
||||
minimax: { daily: 0.5, weekly: null, monthly: null },
|
||||
opencode_go: { daily: null, weekly: null, monthly: null },
|
||||
});
|
||||
expect(result.ollama_cloud).toEqual({ daily: 3, weekly: 30, monthly: 300 });
|
||||
expect(result.kimi).toEqual({ daily: 1, weekly: null, monthly: null });
|
||||
expect(result.zhipu).toEqual({ daily: null, weekly: 2, monthly: null });
|
||||
expect(result.deepseek).toEqual({ daily: null, weekly: null, monthly: 4 });
|
||||
expect(result.minimax).toEqual({ daily: 0.5, weekly: null, monthly: null });
|
||||
expect(result.opencode_go).toEqual({ daily: null, weekly: null, monthly: null });
|
||||
});
|
||||
|
||||
it("保留未知平台 key(后端新加、前端未收录)", () => {
|
||||
const result = normalizePlatformQuotasMap({
|
||||
anthropic: { daily: 5, weekly: null, monthly: null },
|
||||
some_future_platform: { daily: 7, weekly: 8, monthly: 9 },
|
||||
} as DefaultPlatformQuotasMap);
|
||||
expect(result.some_future_platform).toEqual({ daily: 7, weekly: 8, monthly: 9 });
|
||||
expect(result.anthropic).toEqual({ daily: 5, weekly: null, monthly: null });
|
||||
});
|
||||
|
||||
it("未知平台的非 number 值归一化为 null", () => {
|
||||
const result = normalizePlatformQuotasMap({
|
||||
some_future_platform: { daily: "50" as unknown as number, weekly: undefined as unknown as number, monthly: null },
|
||||
} as DefaultPlatformQuotasMap);
|
||||
expect(result.some_future_platform).toEqual({ daily: null, weekly: null, monthly: null });
|
||||
});
|
||||
|
||||
it("非 number 类型的值归一化为 null", () => {
|
||||
const result = normalizePlatformQuotasMap({
|
||||
anthropic: { daily: "50" as unknown as number, weekly: undefined as unknown as number, monthly: null },
|
||||
@@ -288,9 +318,23 @@ describe("sanitizePlatformQuotasMap", () => {
|
||||
expect(result.gemini?.weekly).toBe(null);
|
||||
});
|
||||
|
||||
it("预置 ollama_cloud 配额清洗后保留(提交 payload 不再丢行)", () => {
|
||||
const result = sanitizePlatformQuotasMap({
|
||||
ollama_cloud: { daily: 3, weekly: 0, monthly: null },
|
||||
});
|
||||
expect(result.ollama_cloud).toEqual({ daily: 3, weekly: 0, monthly: null });
|
||||
});
|
||||
|
||||
it("保留未知平台 key 且同样清洗其值", () => {
|
||||
const result = sanitizePlatformQuotasMap({
|
||||
some_future_platform: { daily: 7, weekly: -3, monthly: "x" as unknown as number },
|
||||
} as DefaultPlatformQuotasMap);
|
||||
expect(result.some_future_platform).toEqual({ daily: 7, weekly: null, monthly: null });
|
||||
});
|
||||
|
||||
it("缺失平台填充为全 null", () => {
|
||||
const result = sanitizePlatformQuotasMap({});
|
||||
expect(Object.keys(result)).toHaveLength(5);
|
||||
expect(Object.keys(result)).toHaveLength(QUOTA_PLATFORMS.length);
|
||||
for (const v of Object.values(result)) {
|
||||
expect(v).toEqual({ daily: null, weekly: null, monthly: null });
|
||||
}
|
||||
|
||||
@@ -18,7 +18,27 @@ export interface DefaultSubscriptionSetting {
|
||||
}
|
||||
|
||||
// ── 平台限额类型 ──────────────────────────────────────────────────
|
||||
export type PlatformType = "anthropic" | "openai" | "gemini" | "antigravity" | "grok" | "typesafe"
|
||||
|
||||
// 配额平台镜像:与后端 service.AllowedQuotaPlatforms 保持一致(单一权威来源:
|
||||
// backend/internal/service/domain_constants.go)。后端对 default_platform_quotas
|
||||
// 与用户平台配额均为整体替换语义:这份列表缺一个平台,保存时就会静默丢弃
|
||||
// 该平台的配额行(缺行等价于不限额 → 限额 fail-open)。新增平台必须同步此列表。
|
||||
export const QUOTA_PLATFORMS = [
|
||||
"anthropic",
|
||||
"openai",
|
||||
"gemini",
|
||||
"antigravity",
|
||||
"grok",
|
||||
"kimi",
|
||||
"zhipu",
|
||||
"deepseek",
|
||||
"minimax",
|
||||
"opencode_go",
|
||||
"typesafe",
|
||||
"ollama_cloud",
|
||||
] as const
|
||||
|
||||
export type PlatformType = (typeof QUOTA_PLATFORMS)[number]
|
||||
export type QuotaWindowType = "daily" | "weekly" | "monthly"
|
||||
|
||||
/** 单平台三档限额;null = 不限制,undefined = 未填(等价 null) */
|
||||
@@ -28,10 +48,13 @@ export interface PlatformQuotaLimits {
|
||||
monthly: number | null
|
||||
}
|
||||
|
||||
/** 全平台默认限额 map(key = PlatformType) */
|
||||
export type DefaultPlatformQuotasMap = Partial<Record<PlatformType, PlatformQuotaLimits>>
|
||||
|
||||
const PLATFORMS: PlatformType[] = ["anthropic", "openai", "gemini", "antigravity", "grok", "typesafe"]
|
||||
/**
|
||||
* 全平台默认限额 map(key = PlatformType)。
|
||||
* 可能额外含后端新加、前端尚未收录的平台 key:整体替换语义下 normalize/sanitize
|
||||
* 必须原样保留这些 key,否则一次保存就会静默删除该平台的默认配额。
|
||||
*/
|
||||
export type DefaultPlatformQuotasMap = Partial<Record<PlatformType, PlatformQuotaLimits>> &
|
||||
Record<string, PlatformQuotaLimits | undefined>
|
||||
|
||||
export type SchedulingThresholdPlatformType =
|
||||
| "openai"
|
||||
@@ -78,27 +101,45 @@ export function sanitizeAccountSchedulingThresholdsMap(
|
||||
return normalizeAccountSchedulingThresholdsMap(input)
|
||||
}
|
||||
|
||||
/** 归一化为全 4 平台 × 3 窗口(缺失填 null),供模板非空绑定 */
|
||||
/** 归一化为全平台 × 3 窗口(缺失填 null),供模板非空绑定;保留输入中的未知平台 key */
|
||||
export function normalizePlatformQuotasMap(input?: DefaultPlatformQuotasMap | null): DefaultPlatformQuotasMap {
|
||||
const normalizeOne = (src?: PlatformQuotaLimits): PlatformQuotaLimits => ({
|
||||
daily: typeof src?.daily === "number" ? src.daily : null,
|
||||
weekly: typeof src?.weekly === "number" ? src.weekly : null,
|
||||
monthly: typeof src?.monthly === "number" ? src.monthly : null,
|
||||
})
|
||||
const result: DefaultPlatformQuotasMap = {}
|
||||
for (const p of PLATFORMS) {
|
||||
const src = input?.[p]
|
||||
result[p] = {
|
||||
daily: typeof src?.daily === "number" ? src.daily : null,
|
||||
weekly: typeof src?.weekly === "number" ? src.weekly : null,
|
||||
monthly: typeof src?.monthly === "number" ? src.monthly : null,
|
||||
for (const p of QUOTA_PLATFORMS) {
|
||||
result[p] = normalizeOne(input?.[p])
|
||||
}
|
||||
// 稳健性护栏:后端新加、前端尚未收录的平台 key 原样保留(整体替换语义下
|
||||
// 丢弃 = 静默删除该平台的默认配额)。
|
||||
if (input) {
|
||||
for (const [key, value] of Object.entries(input)) {
|
||||
if (!(QUOTA_PLATFORMS as readonly string[]).includes(key)) {
|
||||
result[key] = normalizeOne(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** 提交前清洗:非有限数/负数/空字符串 → null(保留 0 = 显式禁用),返回全 4 平台嵌套 map */
|
||||
/** 提交前清洗:非有限数/负数/空字符串 → null(保留 0 = 显式禁用),返回全平台嵌套 map;保留未知平台 key */
|
||||
export function sanitizePlatformQuotasMap(input?: DefaultPlatformQuotasMap | null): DefaultPlatformQuotasMap {
|
||||
const clean = (v: unknown): number | null => (typeof v === "number" && Number.isFinite(v) && v >= 0 ? v : null)
|
||||
const sanitizeOne = (src?: PlatformQuotaLimits): PlatformQuotaLimits => ({
|
||||
daily: clean(src?.daily), weekly: clean(src?.weekly), monthly: clean(src?.monthly),
|
||||
})
|
||||
const result: DefaultPlatformQuotasMap = {}
|
||||
for (const p of PLATFORMS) {
|
||||
const src = input?.[p]
|
||||
result[p] = { daily: clean(src?.daily), weekly: clean(src?.weekly), monthly: clean(src?.monthly) }
|
||||
for (const p of QUOTA_PLATFORMS) {
|
||||
result[p] = sanitizeOne(input?.[p])
|
||||
}
|
||||
if (input) {
|
||||
for (const [key, value] of Object.entries(input)) {
|
||||
if (!(QUOTA_PLATFORMS as readonly string[]).includes(key)) {
|
||||
result[key] = sanitizeOne(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
import { apiClient } from '../client'
|
||||
import type { AdminUser, UpdateUserRequest, PaginatedResponse, ApiKey } from '@/types'
|
||||
import type { PlatformType } from './settings'
|
||||
|
||||
export interface AdminBindAuthIdentityChannelRequest {
|
||||
channel: string
|
||||
@@ -330,12 +331,9 @@ export async function bindUserAuthIdentity(
|
||||
/**
|
||||
* Platform quota types
|
||||
*/
|
||||
// Keep aligned with backend/internal/service/domain_constants.go AllowedQuotaPlatforms.
|
||||
export const PLATFORM_QUOTA_PLATFORMS = [
|
||||
'anthropic', 'openai', 'gemini', 'antigravity', 'grok',
|
||||
'kimi', 'zhipu', 'deepseek', 'minimax', 'opencode_go', 'typesafe',
|
||||
] as const
|
||||
export type PlatformQuotaPlatform = typeof PLATFORM_QUOTA_PLATFORMS[number]
|
||||
// 与后端 AllowedQuotaPlatforms 对齐;单一来源为 settings.ts 的 QUOTA_PLATFORMS
|
||||
// (后端用户平台配额 PUT 是整体替换语义,前端列表缺平台 = 保存即静默删行)。
|
||||
export type PlatformQuotaPlatform = PlatformType
|
||||
export type PlatformQuotaWindow = 'daily' | 'weekly' | 'monthly'
|
||||
|
||||
export interface PlatformQuotaItem {
|
||||
|
||||
@@ -119,7 +119,7 @@ import { ref, reactive, watch, computed } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useAppStore } from '@/stores/app'
|
||||
import { adminAPI } from '@/api/admin'
|
||||
import { PLATFORM_QUOTA_PLATFORMS } from '@/api/admin/users'
|
||||
import { QUOTA_PLATFORMS } from '@/api/admin/settings'
|
||||
import type { AdminUser, PlatformQuotaItem, PlatformQuotaPlatform, PlatformQuotaWindow } from '@/types'
|
||||
import BaseDialog from '@/components/common/BaseDialog.vue'
|
||||
|
||||
@@ -129,6 +129,10 @@ const emit = defineEmits(['close', 'success'])
|
||||
const { t } = useI18n()
|
||||
const appStore = useAppStore()
|
||||
|
||||
// 配额平台镜像,单一来源 settings.ts 的 QUOTA_PLATFORMS(对齐后端 AllowedQuotaPlatforms)。
|
||||
// PUT /admin/users/:id/platform-quotas 是整体替换语义:列表缺平台 = 保存即静默删行。
|
||||
const PLATFORMS = QUOTA_PLATFORMS
|
||||
|
||||
interface QuotaRow {
|
||||
platform: PlatformQuotaPlatform
|
||||
daily_limit_usd: number | null
|
||||
@@ -173,9 +177,9 @@ function emptyRow(p: PlatformQuotaPlatform): QuotaRow {
|
||||
}
|
||||
|
||||
function normalize(items: PlatformQuotaItem[]): QuotaRow[] {
|
||||
const byPlatform = new Map<PlatformQuotaPlatform, PlatformQuotaItem>()
|
||||
const byPlatform = new Map<string, PlatformQuotaItem>()
|
||||
for (const it of items) byPlatform.set(it.platform, it)
|
||||
return PLATFORM_QUOTA_PLATFORMS.map((p) => {
|
||||
const rows = PLATFORMS.map((p) => {
|
||||
const it = byPlatform.get(p)
|
||||
if (!it) return emptyRow(p)
|
||||
return {
|
||||
@@ -188,6 +192,22 @@ function normalize(items: PlatformQuotaItem[]): QuotaRow[] {
|
||||
monthly_usage_usd: it.monthly_usage_usd ?? 0,
|
||||
}
|
||||
})
|
||||
// 稳健性护栏:保留响应中前端未收录(后端新加)的平台行。整体替换语义下丢弃
|
||||
// 这些行,管理员不做任何修改直接保存也会软删其配额(限额 fail-open)。
|
||||
// platform 在前端收录前以服务端返回值透传(服务端返回即其白名单成员)。
|
||||
for (const it of items) {
|
||||
if (PLATFORMS.includes(it.platform)) continue
|
||||
rows.push({
|
||||
platform: it.platform as PlatformQuotaPlatform,
|
||||
daily_limit_usd: it.daily_limit_usd ?? null,
|
||||
weekly_limit_usd: it.weekly_limit_usd ?? null,
|
||||
monthly_limit_usd: it.monthly_limit_usd ?? null,
|
||||
daily_usage_usd: it.daily_usage_usd ?? 0,
|
||||
weekly_usage_usd: it.weekly_usage_usd ?? 0,
|
||||
monthly_usage_usd: it.monthly_usage_usd ?? 0,
|
||||
})
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
function formatUsage(n: number): string {
|
||||
@@ -204,7 +224,7 @@ async function load() {
|
||||
savedConfigured.value = configuredPlatforms(data.platform_quotas || [])
|
||||
} catch {
|
||||
appStore.showError(t('admin.users.platformQuota.loadFailed'))
|
||||
quotas.value = PLATFORM_QUOTA_PLATFORMS.map(emptyRow)
|
||||
quotas.value = PLATFORMS.map(emptyRow)
|
||||
savedConfigured.value = new Set()
|
||||
} finally {
|
||||
loading.value = false
|
||||
|
||||
@@ -50,6 +50,7 @@ vi.mock('@/components/common/BaseDialog.vue', () => ({
|
||||
}))
|
||||
|
||||
import UserPlatformQuotaModal from '../UserPlatformQuotaModal.vue'
|
||||
import { QUOTA_PLATFORMS } from '@/api/admin/settings'
|
||||
import type { PlatformQuotaUpdateItem, UserSubscription } from '@/types'
|
||||
|
||||
function makeUser(overrides: { subscriptions?: UserSubscription[] } = {}) {
|
||||
@@ -101,19 +102,19 @@ describe('UserPlatformQuotaModal', () => {
|
||||
expect(apiMocks.getPlatformQuotas).toHaveBeenCalledWith(99)
|
||||
})
|
||||
|
||||
it('renders all eleven supported platforms with empty limits', async () => {
|
||||
it('空数据渲染全平台(对齐后端 AllowedQuotaPlatforms)行', async () => {
|
||||
const w = await mountAndOpen()
|
||||
const html = w.html()
|
||||
for (const p of QUOTA_PLATFORMS) {
|
||||
expect(html).toContain(p)
|
||||
}
|
||||
const rows = w.findAll('tbody tr')
|
||||
expect(rows.map(row => row.find('td').text())).toEqual([
|
||||
'anthropic', 'openai', 'gemini', 'antigravity', 'grok',
|
||||
'kimi', 'zhipu', 'deepseek', 'minimax', 'opencode_go', 'typesafe',
|
||||
])
|
||||
expect(rows.map(row => row.find('td').text())).toEqual([...QUOTA_PLATFORMS])
|
||||
for (const row of rows) {
|
||||
const inputs = row.findAll('input[type=number]')
|
||||
expect(inputs).toHaveLength(3)
|
||||
expect(inputs.map(input => input.element.value)).toEqual(['', '', ''])
|
||||
}
|
||||
w.unmount()
|
||||
})
|
||||
|
||||
it.each(['kimi', 'zhipu', 'deepseek', 'minimax', 'opencode_go', 'typesafe'] as const)(
|
||||
@@ -137,7 +138,7 @@ describe('UserPlatformQuotaModal', () => {
|
||||
: item)
|
||||
expect(apiMocks.updatePlatformQuotas).toHaveBeenCalledTimes(1)
|
||||
expect(apiMocks.updatePlatformQuotas).toHaveBeenCalledWith(99, expect.arrayContaining(expected))
|
||||
expect(apiMocks.updatePlatformQuotas.mock.calls[0][1]).toHaveLength(11)
|
||||
expect(apiMocks.updatePlatformQuotas.mock.calls[0][1]).toHaveLength(QUOTA_PLATFORMS.length)
|
||||
expect(w.emitted('success')).toHaveLength(1)
|
||||
w.unmount()
|
||||
},
|
||||
@@ -152,13 +153,13 @@ describe('UserPlatformQuotaModal', () => {
|
||||
})
|
||||
const w = await mountAndOpen()
|
||||
const inputs = w.findAll('input[type=number]')
|
||||
// 11 platforms × 3 windows = 33 inputs
|
||||
expect(inputs.length).toBe(33)
|
||||
// 全平台 × 3 windows = QUOTA_PLATFORMS.length × 3 inputs
|
||||
expect(inputs.length).toBe(QUOTA_PLATFORMS.length * 3)
|
||||
// 第一个 input 是 anthropic.daily = 10
|
||||
expect((inputs[0].element as HTMLInputElement).value).toBe('10')
|
||||
})
|
||||
|
||||
it('保存提交完整 11 platform payload', async () => {
|
||||
it('保存提交完整全平台 payload', async () => {
|
||||
apiMocks.getPlatformQuotas.mockResolvedValueOnce({
|
||||
platform_quotas: [
|
||||
{ platform: 'openai', daily_limit_usd: null, weekly_limit_usd: 20, monthly_limit_usd: null,
|
||||
@@ -175,11 +176,79 @@ describe('UserPlatformQuotaModal', () => {
|
||||
expect(apiMocks.updatePlatformQuotas).toHaveBeenCalledTimes(1)
|
||||
const [uid, payload] = apiMocks.updatePlatformQuotas.mock.calls[0]
|
||||
expect(uid).toBe(99)
|
||||
expect(payload).toHaveLength(11) // 11 platforms always submitted
|
||||
expect(payload).toHaveLength(QUOTA_PLATFORMS.length) // 全平台总是提交(整体替换语义)
|
||||
const openai = payload.find((p: any) => p.platform === 'openai')
|
||||
expect(openai.weekly_limit_usd).toBe(20)
|
||||
})
|
||||
|
||||
it('预置 ollama_cloud 配额后不做修改直接保存,payload 仍包含该配额(整体替换不丢行)', async () => {
|
||||
apiMocks.getPlatformQuotas.mockResolvedValueOnce({
|
||||
platform_quotas: [
|
||||
{ platform: 'ollama_cloud', daily_limit_usd: 3, weekly_limit_usd: 30, monthly_limit_usd: 300,
|
||||
daily_usage_usd: 0.5, weekly_usage_usd: 2, monthly_usage_usd: 10 },
|
||||
],
|
||||
})
|
||||
const w = await mountAndOpen()
|
||||
const saveBtn = w.findAll('button').find((b) => b.text() === 'admin.users.platformQuota.save')
|
||||
await saveBtn!.trigger('click')
|
||||
await flushPromises()
|
||||
expect(apiMocks.updatePlatformQuotas).toHaveBeenCalledTimes(1)
|
||||
const [, payload] = apiMocks.updatePlatformQuotas.mock.calls[0]
|
||||
const ollama = payload.find((p: any) => p.platform === 'ollama_cloud')
|
||||
expect(ollama).toEqual({
|
||||
platform: 'ollama_cloud',
|
||||
daily_limit_usd: 3,
|
||||
weekly_limit_usd: 30,
|
||||
monthly_limit_usd: 300,
|
||||
})
|
||||
})
|
||||
|
||||
it('kimi/zhipu/deepseek/minimax/opencode_go 预置配额保存后不丢', async () => {
|
||||
apiMocks.getPlatformQuotas.mockResolvedValueOnce({
|
||||
platform_quotas: (['kimi', 'zhipu', 'deepseek', 'minimax', 'opencode_go'] as const).map((platform, i) => ({
|
||||
platform,
|
||||
daily_limit_usd: i + 1,
|
||||
weekly_limit_usd: null,
|
||||
monthly_limit_usd: null,
|
||||
daily_usage_usd: 0,
|
||||
weekly_usage_usd: 0,
|
||||
monthly_usage_usd: 0,
|
||||
})),
|
||||
})
|
||||
const w = await mountAndOpen()
|
||||
const saveBtn = w.findAll('button').find((b) => b.text() === 'admin.users.platformQuota.save')
|
||||
await saveBtn!.trigger('click')
|
||||
await flushPromises()
|
||||
const [, payload] = apiMocks.updatePlatformQuotas.mock.calls[0]
|
||||
for (const platform of ['kimi', 'zhipu', 'deepseek', 'minimax', 'opencode_go']) {
|
||||
const row = payload.find((p: any) => p.platform === platform)
|
||||
expect(row).toBeDefined()
|
||||
expect(row.daily_limit_usd).not.toBeNull()
|
||||
}
|
||||
})
|
||||
|
||||
it('响应中的未知平台 key 被保留并随保存提交', async () => {
|
||||
apiMocks.getPlatformQuotas.mockResolvedValueOnce({
|
||||
platform_quotas: [
|
||||
{ platform: 'some_future_platform', daily_limit_usd: 7, weekly_limit_usd: null, monthly_limit_usd: 9,
|
||||
daily_usage_usd: 0, weekly_usage_usd: 0, monthly_usage_usd: 0 },
|
||||
],
|
||||
})
|
||||
const w = await mountAndOpen()
|
||||
expect(w.html()).toContain('some_future_platform')
|
||||
const saveBtn = w.findAll('button').find((b) => b.text() === 'admin.users.platformQuota.save')
|
||||
await saveBtn!.trigger('click')
|
||||
await flushPromises()
|
||||
const [, payload] = apiMocks.updatePlatformQuotas.mock.calls[0]
|
||||
const row = payload.find((p: any) => p.platform === 'some_future_platform')
|
||||
expect(row).toEqual({
|
||||
platform: 'some_future_platform',
|
||||
daily_limit_usd: 7,
|
||||
weekly_limit_usd: null,
|
||||
monthly_limit_usd: 9,
|
||||
})
|
||||
})
|
||||
|
||||
it('全部清空把所有 limit 置 null(确认通过)', async () => {
|
||||
const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true)
|
||||
apiMocks.getPlatformQuotas.mockResolvedValueOnce({
|
||||
@@ -240,7 +309,7 @@ describe('UserPlatformQuotaModal', () => {
|
||||
it('未配置限额的平台重置按钮禁用并提示不可用', async () => {
|
||||
const w = await mountAndOpen()
|
||||
const resetBtns = w.findAll('button').filter((b) => b.text() === '↻')
|
||||
expect(resetBtns.length).toBe(33) // 11 平台 × 3 窗口
|
||||
expect(resetBtns.length).toBe(QUOTA_PLATFORMS.length * 3) // 全平台 × 3 窗口
|
||||
for (const b of resetBtns) {
|
||||
expect((b.element as HTMLButtonElement).disabled).toBe(true)
|
||||
expect(b.attributes('title')).toBe('admin.users.platformQuota.reset.unavailable')
|
||||
|
||||
@@ -29,12 +29,14 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import type { PlatformQuotaItem, PlatformQuotaPlatform } from '@/api/admin/users'
|
||||
import type { PlatformQuotaItem } from '@/api/admin/users'
|
||||
import { QUOTA_PLATFORMS } from '@/api/admin/settings'
|
||||
|
||||
const props = defineProps<{ quotas?: PlatformQuotaItem[] }>()
|
||||
const { t } = useI18n()
|
||||
|
||||
const PLATFORM_ORDER: PlatformQuotaPlatform[] = ['anthropic', 'openai', 'gemini', 'antigravity', 'grok', 'typesafe']
|
||||
// 展示顺序镜像 quota 平台列表(单一来源 settings.ts 的 QUOTA_PLATFORMS)
|
||||
const PLATFORM_ORDER = QUOTA_PLATFORMS
|
||||
|
||||
// 仅展示「至少一档限额非空」的平台(配额列,非用量列)
|
||||
const configured = computed(() => {
|
||||
|
||||
@@ -4055,7 +4055,7 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="space-y-2">
|
||||
<tr v-for="p in (['anthropic', 'openai', 'gemini', 'antigravity', 'grok', 'typesafe'] as const)" :key="p" class="align-top">
|
||||
<tr v-for="p in QUOTA_PLATFORMS" :key="p" class="align-top">
|
||||
<td class="pr-4 py-1">
|
||||
<span class="font-mono text-xs text-gray-700 dark:text-gray-300">{{ p }}</span>
|
||||
</td>
|
||||
@@ -4390,7 +4390,7 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="p in (['anthropic', 'openai', 'gemini', 'antigravity', 'grok', 'typesafe'] as const)" :key="`${authSource.source}-pq-${p}`" class="align-top">
|
||||
<tr v-for="p in QUOTA_PLATFORMS" :key="`${authSource.source}-pq-${p}`" class="align-top">
|
||||
<td class="pr-4 py-1">
|
||||
<span class="font-mono text-xs text-gray-700 dark:text-gray-300">{{ p }}</span>
|
||||
</td>
|
||||
@@ -8985,6 +8985,7 @@ import {
|
||||
normalizePlatformQuotasMap,
|
||||
sanitizeAccountSchedulingThresholdsMap,
|
||||
sanitizePlatformQuotasMap,
|
||||
QUOTA_PLATFORMS,
|
||||
SCHEDULING_THRESHOLD_PLATFORMS,
|
||||
defaultWeChatConnectScopesForMode,
|
||||
deriveWeChatConnectStoredMode,
|
||||
|
||||
@@ -2032,7 +2032,7 @@ describe("admin SettingsView platform quota matrix", () => {
|
||||
expect(html).toContain("typesafe");
|
||||
});
|
||||
|
||||
it("保存时 updateSettings payload 应包含嵌套 default_platform_quotas 对象(含全 6 平台)", async () => {
|
||||
it("保存时 updateSettings payload 应包含嵌套 default_platform_quotas 对象(含全平台)", async () => {
|
||||
const wrapper = mountView();
|
||||
await flushPromises();
|
||||
await openUsersTab(wrapper);
|
||||
@@ -2062,6 +2062,41 @@ describe("admin SettingsView platform quota matrix", () => {
|
||||
expect(payload).not.toHaveProperty("default_platform_quota_openai_weekly");
|
||||
});
|
||||
|
||||
it("预置 ollama_cloud/kimi 等新平台默认配额后不做修改直接保存,payload 仍包含(整体替换不丢行)", async () => {
|
||||
getSettings.mockResolvedValueOnce({
|
||||
...baseSettingsResponse,
|
||||
default_platform_quotas: {
|
||||
anthropic: { daily: 5, weekly: null, monthly: null },
|
||||
ollama_cloud: { daily: 3, weekly: 30, monthly: 300 },
|
||||
kimi: { daily: 1, weekly: null, monthly: null },
|
||||
zhipu: { daily: null, weekly: 2, monthly: null },
|
||||
deepseek: { daily: null, weekly: null, monthly: 4 },
|
||||
minimax: { daily: 0.5, weekly: null, monthly: null },
|
||||
opencode_go: { daily: 0.6, weekly: 6, monthly: 60 },
|
||||
},
|
||||
});
|
||||
|
||||
const wrapper = mountView();
|
||||
await flushPromises();
|
||||
await openUsersTab(wrapper);
|
||||
|
||||
await wrapper.find("form").trigger("submit.prevent");
|
||||
await flushPromises();
|
||||
|
||||
const payload = updateSettings.mock.calls.at(-1)![0] as Record<string, unknown>;
|
||||
const quotas = payload["default_platform_quotas"] as Record<string, Record<string, unknown>>;
|
||||
|
||||
// 整体替换语义下,这些平台配额行必须原样出现在提交 payload 中
|
||||
expect(quotas["ollama_cloud"]).toEqual({ daily: 3, weekly: 30, monthly: 300 });
|
||||
expect(quotas["kimi"]).toEqual({ daily: 1, weekly: null, monthly: null });
|
||||
expect(quotas["zhipu"]).toEqual({ daily: null, weekly: 2, monthly: null });
|
||||
expect(quotas["deepseek"]).toEqual({ daily: null, weekly: null, monthly: 4 });
|
||||
expect(quotas["minimax"]).toEqual({ daily: 0.5, weekly: null, monthly: null });
|
||||
expect(quotas["opencode_go"]).toEqual({ daily: 0.6, weekly: 6, monthly: 60 });
|
||||
// 已知平台不受影响
|
||||
expect(quotas["anthropic"]).toEqual({ daily: 5, weekly: null, monthly: null });
|
||||
});
|
||||
|
||||
it("加载后 form.default_platform_quotas 含全 6 平台,从嵌套 JSON 正确读取数值", async () => {
|
||||
getSettings.mockResolvedValueOnce({
|
||||
...baseSettingsResponse,
|
||||
|
||||
Reference in new issue
Block a user