special-settings.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { describe, expect, test } from "vitest";
  2. import type { SpecialSetting } from "@/types/special-settings";
  3. import { buildUnifiedSpecialSettings } from "@/lib/utils/special-settings";
  4. describe("buildUnifiedSpecialSettings", () => {
  5. test("无任何输入时应返回 null", () => {
  6. expect(buildUnifiedSpecialSettings({ existing: null })).toBe(null);
  7. });
  8. test("blockedBy=warmup 时应派生 guard_intercept 特殊设置", () => {
  9. const settings = buildUnifiedSpecialSettings({
  10. existing: null,
  11. blockedBy: "warmup",
  12. blockedReason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  13. statusCode: 200,
  14. });
  15. expect(settings).not.toBeNull();
  16. expect(settings).toEqual(
  17. expect.arrayContaining([
  18. expect.objectContaining({
  19. type: "guard_intercept",
  20. scope: "guard",
  21. hit: true,
  22. guard: "warmup",
  23. action: "intercept_response",
  24. statusCode: 200,
  25. }),
  26. ])
  27. );
  28. });
  29. test("blockedBy=sensitive_word 时应派生 guard_intercept 特殊设置", () => {
  30. const settings = buildUnifiedSpecialSettings({
  31. existing: null,
  32. blockedBy: "sensitive_word",
  33. blockedReason: JSON.stringify({ word: "x" }),
  34. statusCode: 400,
  35. });
  36. expect(settings).not.toBeNull();
  37. expect(settings).toEqual(
  38. expect.arrayContaining([
  39. expect.objectContaining({
  40. type: "guard_intercept",
  41. scope: "guard",
  42. hit: true,
  43. guard: "sensitive_word",
  44. action: "block_request",
  45. statusCode: 400,
  46. }),
  47. ])
  48. );
  49. });
  50. test("cacheTtlApplied 存在时应派生 anthropic_cache_ttl_header_override 特殊设置", () => {
  51. const settings = buildUnifiedSpecialSettings({
  52. existing: null,
  53. cacheTtlApplied: "1h",
  54. });
  55. expect(settings).not.toBeNull();
  56. expect(settings).toEqual(
  57. expect.arrayContaining([
  58. expect.objectContaining({
  59. type: "anthropic_cache_ttl_header_override",
  60. scope: "request_header",
  61. hit: true,
  62. ttl: "1h",
  63. }),
  64. ])
  65. );
  66. });
  67. test("context1mApplied=true 时应派生 anthropic_context_1m_header_override 特殊设置", () => {
  68. const settings = buildUnifiedSpecialSettings({
  69. existing: null,
  70. context1mApplied: true,
  71. });
  72. expect(settings).not.toBeNull();
  73. expect(settings).toEqual(
  74. expect.arrayContaining([
  75. expect.objectContaining({
  76. type: "anthropic_context_1m_header_override",
  77. scope: "request_header",
  78. hit: true,
  79. header: "anthropic-beta",
  80. }),
  81. ])
  82. );
  83. });
  84. test("应合并 existing specialSettings 与派生 specialSettings", () => {
  85. const existing: SpecialSetting[] = [
  86. {
  87. type: "provider_parameter_override",
  88. scope: "provider",
  89. providerId: 1,
  90. providerName: "p",
  91. providerType: "codex",
  92. hit: true,
  93. changed: true,
  94. changes: [{ path: "temperature", before: 1, after: 0.2, changed: true }],
  95. },
  96. ];
  97. const settings = buildUnifiedSpecialSettings({
  98. existing,
  99. blockedBy: "warmup",
  100. blockedReason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  101. statusCode: 200,
  102. });
  103. expect(settings).not.toBeNull();
  104. expect(settings).toEqual(
  105. expect.arrayContaining([
  106. expect.objectContaining({ type: "provider_parameter_override" }),
  107. expect.objectContaining({ type: "guard_intercept", guard: "warmup" }),
  108. ])
  109. );
  110. });
  111. test("应对重复的派生项去重(例如 existing 已包含同类 guard_intercept)", () => {
  112. const existing: SpecialSetting[] = [
  113. {
  114. type: "guard_intercept",
  115. scope: "guard",
  116. hit: true,
  117. guard: "warmup",
  118. action: "intercept_response",
  119. statusCode: 200,
  120. reason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  121. },
  122. ];
  123. const settings = buildUnifiedSpecialSettings({
  124. existing,
  125. blockedBy: "warmup",
  126. blockedReason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  127. statusCode: 200,
  128. });
  129. expect(settings).not.toBeNull();
  130. expect(settings?.filter((s) => s.type === "guard_intercept").length).toBe(1);
  131. });
  132. test("guard_intercept 去重时不应受 reason 差异影响", () => {
  133. const existing: SpecialSetting[] = [
  134. {
  135. type: "guard_intercept",
  136. scope: "guard",
  137. hit: true,
  138. guard: "warmup",
  139. action: "intercept_response",
  140. statusCode: 200,
  141. reason: JSON.stringify({ reason: "a" }),
  142. },
  143. ];
  144. const settings = buildUnifiedSpecialSettings({
  145. existing,
  146. blockedBy: "warmup",
  147. blockedReason: JSON.stringify({ reason: "b" }),
  148. statusCode: 200,
  149. });
  150. expect(settings).not.toBeNull();
  151. expect(settings?.filter((s) => s.type === "guard_intercept").length).toBe(1);
  152. });
  153. });