model-switch-integration.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * LLM 模型切换 - 集成测试
  3. *
  4. * 导入真实的 config.models.shouldSwitchModel 验证修复在生产代码中生效。
  5. * 不 mock 核心逻辑,直接测实际编译后的代码路径。
  6. */
  7. import { describe, it, expect, beforeAll } from 'vitest';
  8. // 实际 config 模块,包含修复后的 shouldSwitchModel
  9. let realShouldSwitchModel: (error: any) => boolean;
  10. beforeAll(async () => {
  11. // config 模块有副作用(读 models.json/.env),但测试环境应该没问题
  12. const { config } = await import('@/config');
  13. realShouldSwitchModel = config.models.shouldSwitchModel;
  14. });
  15. describe('真实 config.models.shouldSwitchModel(修复验证)', () => {
  16. describe('█████ 核心场景:本次修复的关键 █████', () => {
  17. it('【关键】真实 429 错误(Error对象)→ 必须返回 true', () => {
  18. const err = new Error('429 已达到 Token Plan 用量上限:请升级 Token Plan 套餐或购买积分补充用量。 (2056)');
  19. expect(realShouldSwitchModel(err)).toBe(true);
  20. });
  21. it('【关键】真实 429 错误(字符串)- 修复前此用例返回 false → 导致切换失败', () => {
  22. // 这是修复前所有 call site 传参方式:error?.message || ''
  23. const errorMessage = '429 已达到 Token Plan 用量上限:请升级 Token Plan 套餐或购买积分补充用量。 (2056)';
  24. const result = realShouldSwitchModel(errorMessage);
  25. expect(result).toBe(true);
  26. // 如果这里返回 false,说明修复没生效!
  27. });
  28. it('【关键】MiniMax API 常见的 rate limit 错误', () => {
  29. const err = new Error('Request rate limit exceeded, please try again later.');
  30. expect(realShouldSwitchModel(err)).toBe(true);
  31. });
  32. it('【关键】阿里云百炼 quota 耗尽错误', () => {
  33. const err = new Error('You have exceeded your quota. Please check your balance.');
  34. expect(realShouldSwitchModel(err)).toBe(true);
  35. });
  36. });
  37. describe('不可切换的错误 - 认证类', () => {
  38. it('Invalid API key → false', () => {
  39. expect(realShouldSwitchModel(new Error('Invalid API key provided'))).toBe(false);
  40. });
  41. it('Authentication failed → false', () => {
  42. expect(realShouldSwitchModel(new Error('Authentication failed'))).toBe(false);
  43. });
  44. });
  45. describe('确认 import 的是修复后的版本', () => {
  46. it('函数存在且可调用', () => {
  47. expect(typeof realShouldSwitchModel).toBe('function');
  48. });
  49. it('字符串 "429" 不会被误判为不可切换', () => {
  50. // 这个测试验证字符串入参也能正确处理
  51. expect(realShouldSwitchModel('429')).toBe(true);
  52. });
  53. it('空字符串不会崩溃', () => {
  54. expect(() => realShouldSwitchModel('')).not.toThrow();
  55. expect(realShouldSwitchModel('')).toBe(false);
  56. });
  57. });
  58. });