| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /**
- * LLM 模型切换 - 集成测试
- *
- * 导入真实的 config.models.shouldSwitchModel 验证修复在生产代码中生效。
- * 不 mock 核心逻辑,直接测实际编译后的代码路径。
- */
- import { describe, it, expect, beforeAll } from 'vitest';
- // 实际 config 模块,包含修复后的 shouldSwitchModel
- let realShouldSwitchModel: (error: any) => boolean;
- beforeAll(async () => {
- // config 模块有副作用(读 models.json/.env),但测试环境应该没问题
- const { config } = await import('@/config');
- realShouldSwitchModel = config.models.shouldSwitchModel;
- });
- describe('真实 config.models.shouldSwitchModel(修复验证)', () => {
- describe('█████ 核心场景:本次修复的关键 █████', () => {
- it('【关键】真实 429 错误(Error对象)→ 必须返回 true', () => {
- const err = new Error('429 已达到 Token Plan 用量上限:请升级 Token Plan 套餐或购买积分补充用量。 (2056)');
- expect(realShouldSwitchModel(err)).toBe(true);
- });
- it('【关键】真实 429 错误(字符串)- 修复前此用例返回 false → 导致切换失败', () => {
- // 这是修复前所有 call site 传参方式:error?.message || ''
- const errorMessage = '429 已达到 Token Plan 用量上限:请升级 Token Plan 套餐或购买积分补充用量。 (2056)';
- const result = realShouldSwitchModel(errorMessage);
- expect(result).toBe(true);
- // 如果这里返回 false,说明修复没生效!
- });
- it('【关键】MiniMax API 常见的 rate limit 错误', () => {
- const err = new Error('Request rate limit exceeded, please try again later.');
- expect(realShouldSwitchModel(err)).toBe(true);
- });
- it('【关键】阿里云百炼 quota 耗尽错误', () => {
- const err = new Error('You have exceeded your quota. Please check your balance.');
- expect(realShouldSwitchModel(err)).toBe(true);
- });
- });
- describe('不可切换的错误 - 认证类', () => {
- it('Invalid API key → false', () => {
- expect(realShouldSwitchModel(new Error('Invalid API key provided'))).toBe(false);
- });
- it('Authentication failed → false', () => {
- expect(realShouldSwitchModel(new Error('Authentication failed'))).toBe(false);
- });
- });
- describe('确认 import 的是修复后的版本', () => {
- it('函数存在且可调用', () => {
- expect(typeof realShouldSwitchModel).toBe('function');
- });
- it('字符串 "429" 不会被误判为不可切换', () => {
- // 这个测试验证字符串入参也能正确处理
- expect(realShouldSwitchModel('429')).toBe(true);
- });
- it('空字符串不会崩溃', () => {
- expect(() => realShouldSwitchModel('')).not.toThrow();
- expect(realShouldSwitchModel('')).toBe(false);
- });
- });
- });
|