special-settings.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 no longer derives header override (1M context GA)", () => {
  71. const settings = buildUnifiedSpecialSettings({
  72. existing: null,
  73. context1mApplied: true,
  74. });
  75. expect(settings).toBe(null);
  76. });
  77. test("应合并 existing specialSettings 与派生 specialSettings", () => {
  78. const existing: SpecialSetting[] = [
  79. {
  80. type: "provider_parameter_override",
  81. scope: "provider",
  82. providerId: 1,
  83. providerName: "p",
  84. providerType: "codex",
  85. hit: true,
  86. changed: true,
  87. changes: [{ path: "temperature", before: 1, after: 0.2, changed: true }],
  88. },
  89. ];
  90. const settings = buildUnifiedSpecialSettings({
  91. existing,
  92. blockedBy: "warmup",
  93. blockedReason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  94. statusCode: 200,
  95. });
  96. expect(settings).not.toBeNull();
  97. expect(settings).toEqual(
  98. expect.arrayContaining([
  99. expect.objectContaining({ type: "provider_parameter_override" }),
  100. expect.objectContaining({ type: "guard_intercept", guard: "warmup" }),
  101. ])
  102. );
  103. });
  104. test("应对重复的派生项去重(例如 existing 已包含同类 guard_intercept)", () => {
  105. const existing: SpecialSetting[] = [
  106. {
  107. type: "guard_intercept",
  108. scope: "guard",
  109. hit: true,
  110. guard: "warmup",
  111. action: "intercept_response",
  112. statusCode: 200,
  113. reason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  114. },
  115. ];
  116. const settings = buildUnifiedSpecialSettings({
  117. existing,
  118. blockedBy: "warmup",
  119. blockedReason: JSON.stringify({ reason: "anthropic_warmup_intercepted" }),
  120. statusCode: 200,
  121. });
  122. expect(settings).not.toBeNull();
  123. expect(settings?.filter((s) => s.type === "guard_intercept").length).toBe(1);
  124. });
  125. test("guard_intercept 去重时不应受 reason 差异影响", () => {
  126. const existing: SpecialSetting[] = [
  127. {
  128. type: "guard_intercept",
  129. scope: "guard",
  130. hit: true,
  131. guard: "warmup",
  132. action: "intercept_response",
  133. statusCode: 200,
  134. reason: JSON.stringify({ reason: "a" }),
  135. },
  136. ];
  137. const settings = buildUnifiedSpecialSettings({
  138. existing,
  139. blockedBy: "warmup",
  140. blockedReason: JSON.stringify({ reason: "b" }),
  141. statusCode: 200,
  142. });
  143. expect(settings).not.toBeNull();
  144. expect(settings?.filter((s) => s.type === "guard_intercept").length).toBe(1);
  145. });
  146. });
  147. describe("hasPriorityServiceTierSpecialSetting", () => {
  148. test("returns true when codex actual service tier is priority", () => {
  149. expect(
  150. hasPriorityServiceTierSpecialSetting([
  151. {
  152. type: "codex_service_tier_result",
  153. scope: "response",
  154. hit: true,
  155. requestedServiceTier: "default",
  156. actualServiceTier: "priority",
  157. effectivePriority: true,
  158. },
  159. ])
  160. ).toBe(true);
  161. });
  162. test("returns false when codex actual service tier is non-priority even if request was priority", () => {
  163. expect(
  164. hasPriorityServiceTierSpecialSetting([
  165. {
  166. type: "provider_parameter_override",
  167. scope: "provider",
  168. providerId: 1,
  169. providerName: "p",
  170. providerType: "codex",
  171. hit: true,
  172. changed: true,
  173. changes: [{ path: "service_tier", before: null, after: "priority", changed: true }],
  174. },
  175. {
  176. type: "codex_service_tier_result",
  177. scope: "response",
  178. hit: true,
  179. requestedServiceTier: "priority",
  180. actualServiceTier: "default",
  181. effectivePriority: false,
  182. },
  183. ])
  184. ).toBe(false);
  185. });
  186. });