special-settings.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { describe, expect, test } from "vitest";
  2. import type { SpecialSetting } from "@/types/special-settings";
  3. import {
  4. buildUnifiedSpecialSettings,
  5. hasPriorityServiceTierSpecialSetting,
  6. } from "@/lib/utils/special-settings";
  7. describe("buildUnifiedSpecialSettings", () => {
  8. test("无任何输入时应返回 null", () => {
  9. expect(buildUnifiedSpecialSettings({ existing: null })).toBe(null);
  10. });
  11. test("blockedBy=warmup 时应派生 guard_intercept 特殊设置", () => {
  12. const settings = buildUnifiedSpecialSettings({
  13. existing: null,
  14. blockedBy: "warmup",
  15. blockedReason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  16. statusCode: 200,
  17. });
  18. expect(settings).not.toBeNull();
  19. expect(settings).toEqual(
  20. expect.arrayContaining([
  21. expect.objectContaining({
  22. type: "guard_intercept",
  23. scope: "guard",
  24. hit: true,
  25. guard: "warmup",
  26. action: "intercept_response",
  27. statusCode: 200,
  28. }),
  29. ])
  30. );
  31. });
  32. test("blockedBy=sensitive_word 时应派生 guard_intercept 特殊设置", () => {
  33. const settings = buildUnifiedSpecialSettings({
  34. existing: null,
  35. blockedBy: "sensitive_word",
  36. blockedReason: JSON.stringify({ word: "x" }),
  37. statusCode: 400,
  38. });
  39. expect(settings).not.toBeNull();
  40. expect(settings).toEqual(
  41. expect.arrayContaining([
  42. expect.objectContaining({
  43. type: "guard_intercept",
  44. scope: "guard",
  45. hit: true,
  46. guard: "sensitive_word",
  47. action: "block_request",
  48. statusCode: 400,
  49. }),
  50. ])
  51. );
  52. });
  53. test("cacheTtlApplied 存在时应派生 anthropic_cache_ttl_header_override 特殊设置", () => {
  54. const settings = buildUnifiedSpecialSettings({
  55. existing: null,
  56. cacheTtlApplied: "1h",
  57. });
  58. expect(settings).not.toBeNull();
  59. expect(settings).toEqual(
  60. expect.arrayContaining([
  61. expect.objectContaining({
  62. type: "anthropic_cache_ttl_header_override",
  63. scope: "request_header",
  64. hit: true,
  65. ttl: "1h",
  66. }),
  67. ])
  68. );
  69. });
  70. test("context1mApplied=true 时应在无显式记录时回退派生 anthropic_context_1m_header_override", () => {
  71. const settings = buildUnifiedSpecialSettings({
  72. existing: null,
  73. context1mApplied: true,
  74. });
  75. expect(settings).not.toBeNull();
  76. expect(settings).toEqual(
  77. expect.arrayContaining([
  78. expect.objectContaining({
  79. type: "anthropic_context_1m_header_override",
  80. scope: "request_header",
  81. hit: true,
  82. header: "anthropic-beta",
  83. }),
  84. ])
  85. );
  86. });
  87. test("context1mApplied=true 但已有 codex service tier result 时不应回退派生 anthropic_context_1m_header_override", () => {
  88. const settings = buildUnifiedSpecialSettings({
  89. existing: [
  90. {
  91. type: "codex_service_tier_result",
  92. scope: "response",
  93. hit: true,
  94. requestedServiceTier: "priority",
  95. actualServiceTier: null,
  96. effectivePriority: true,
  97. },
  98. ],
  99. context1mApplied: true,
  100. });
  101. expect(settings?.some((item) => item.type === "anthropic_context_1m_header_override")).toBe(
  102. false
  103. );
  104. });
  105. test("应合并 existing specialSettings 与派生 specialSettings", () => {
  106. const existing: SpecialSetting[] = [
  107. {
  108. type: "provider_parameter_override",
  109. scope: "provider",
  110. providerId: 1,
  111. providerName: "p",
  112. providerType: "codex",
  113. hit: true,
  114. changed: true,
  115. changes: [{ path: "temperature", before: 1, after: 0.2, changed: true }],
  116. },
  117. ];
  118. const settings = buildUnifiedSpecialSettings({
  119. existing,
  120. blockedBy: "warmup",
  121. blockedReason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  122. statusCode: 200,
  123. });
  124. expect(settings).not.toBeNull();
  125. expect(settings).toEqual(
  126. expect.arrayContaining([
  127. expect.objectContaining({ type: "provider_parameter_override" }),
  128. expect.objectContaining({ type: "guard_intercept", guard: "warmup" }),
  129. ])
  130. );
  131. });
  132. test("应对重复的派生项去重(例如 existing 已包含同类 guard_intercept)", () => {
  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: "anthropic_warmup_intercepted" }),
  142. },
  143. ];
  144. const settings = buildUnifiedSpecialSettings({
  145. existing,
  146. blockedBy: "warmup",
  147. blockedReason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  148. statusCode: 200,
  149. });
  150. expect(settings).not.toBeNull();
  151. expect(settings?.filter((s) => s.type === "guard_intercept").length).toBe(1);
  152. });
  153. test("guard_intercept 去重时不应受 reason 差异影响", () => {
  154. const existing: SpecialSetting[] = [
  155. {
  156. type: "guard_intercept",
  157. scope: "guard",
  158. hit: true,
  159. guard: "warmup",
  160. action: "intercept_response",
  161. statusCode: 200,
  162. reason: JSON.stringify({ reason: "a" }),
  163. },
  164. ];
  165. const settings = buildUnifiedSpecialSettings({
  166. existing,
  167. blockedBy: "warmup",
  168. blockedReason: JSON.stringify({ reason: "b" }),
  169. statusCode: 200,
  170. });
  171. expect(settings).not.toBeNull();
  172. expect(settings?.filter((s) => s.type === "guard_intercept").length).toBe(1);
  173. });
  174. });
  175. describe("hasPriorityServiceTierSpecialSetting", () => {
  176. test("returns true when codex actual service tier is priority", () => {
  177. expect(
  178. hasPriorityServiceTierSpecialSetting([
  179. {
  180. type: "codex_service_tier_result",
  181. scope: "response",
  182. hit: true,
  183. requestedServiceTier: "default",
  184. actualServiceTier: "priority",
  185. effectivePriority: true,
  186. },
  187. ])
  188. ).toBe(true);
  189. });
  190. test("returns false when codex actual service tier is non-priority even if request was priority", () => {
  191. expect(
  192. hasPriorityServiceTierSpecialSetting([
  193. {
  194. type: "provider_parameter_override",
  195. scope: "provider",
  196. providerId: 1,
  197. providerName: "p",
  198. providerType: "codex",
  199. hit: true,
  200. changed: true,
  201. changes: [{ path: "service_tier", before: null, after: "priority", changed: true }],
  202. },
  203. {
  204. type: "codex_service_tier_result",
  205. scope: "response",
  206. hit: true,
  207. requestedServiceTier: "priority",
  208. actualServiceTier: "default",
  209. effectivePriority: false,
  210. },
  211. ])
  212. ).toBe(false);
  213. });
  214. });