batch-edit-prefill.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { describe, expect, it } from "vitest";
  2. import type { ProviderDisplay } from "@/types/provider";
  3. import { createInitialState } from "@/app/[locale]/settings/providers/_components/forms/provider-form/provider-form-context";
  4. describe("批量编辑预填充集成测试", () => {
  5. it("应该在批量模式下预填充相同的设置值", () => {
  6. const providers: ProviderDisplay[] = [
  7. {
  8. id: 1,
  9. name: "Provider A",
  10. priority: 10,
  11. weight: 5,
  12. costMultiplier: 1.5,
  13. modelRedirects: { "model-a": "model-b" },
  14. allowedModels: ["model-1", "model-2"],
  15. limit5hUsd: 100,
  16. circuitBreakerFailureThreshold: 5,
  17. circuitBreakerOpenDuration: 300000, // 5 minutes
  18. } as ProviderDisplay,
  19. {
  20. id: 2,
  21. name: "Provider B",
  22. priority: 10,
  23. weight: 5,
  24. costMultiplier: 1.5,
  25. modelRedirects: { "model-a": "model-b" },
  26. allowedModels: ["model-1", "model-2"],
  27. limit5hUsd: 100,
  28. circuitBreakerFailureThreshold: 5,
  29. circuitBreakerOpenDuration: 300000,
  30. } as ProviderDisplay,
  31. ];
  32. const state = createInitialState("batch", undefined, undefined, undefined, providers);
  33. // 验证预填充的值
  34. expect(state.routing.priority).toBe(10);
  35. expect(state.routing.weight).toBe(5);
  36. expect(state.routing.costMultiplier).toBe(1.5);
  37. expect(state.routing.modelRedirects).toEqual({ "model-a": "model-b" });
  38. expect(state.routing.allowedModels).toEqual(["model-1", "model-2"]);
  39. expect(state.rateLimit.limit5hUsd).toBe(100);
  40. expect(state.circuitBreaker.failureThreshold).toBe(5);
  41. expect(state.circuitBreaker.openDurationMinutes).toBe(5);
  42. });
  43. it("应该在批量模式下对不同的设置值使用默认值", () => {
  44. const providers: ProviderDisplay[] = [
  45. {
  46. id: 1,
  47. name: "Provider A",
  48. priority: 10,
  49. weight: 5,
  50. } as ProviderDisplay,
  51. {
  52. id: 2,
  53. name: "Provider B",
  54. priority: 20, // 不同的值
  55. weight: 10, // 不同的值
  56. } as ProviderDisplay,
  57. ];
  58. const state = createInitialState("batch", undefined, undefined, undefined, providers);
  59. // 验证使用默认值
  60. expect(state.routing.priority).toBe(0); // 默认值
  61. expect(state.routing.weight).toBe(1); // 默认值
  62. });
  63. it("应该在没有 batchProviders 时使用默认值", () => {
  64. const state = createInitialState("batch");
  65. // 验证所有字段都是默认值
  66. expect(state.routing.priority).toBe(0);
  67. expect(state.routing.weight).toBe(1);
  68. expect(state.routing.costMultiplier).toBe(1.0);
  69. expect(state.routing.modelRedirects).toEqual({});
  70. expect(state.routing.allowedModels).toEqual([]);
  71. expect(state.rateLimit.limit5hUsd).toBeNull();
  72. expect(state.circuitBreaker.failureThreshold).toBeUndefined();
  73. });
  74. });