thinking-budget-rectifier.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. detectThinkingBudgetRectifierTrigger,
  4. rectifyThinkingBudget,
  5. } from "@/app/v1/_lib/proxy/thinking-budget-rectifier";
  6. describe("ThinkingBudgetRectifier", () => {
  7. describe("detectThinkingBudgetRectifierTrigger", () => {
  8. it("should detect budget_tokens_too_low from typical Anthropic error", () => {
  9. const trigger = detectThinkingBudgetRectifierTrigger(
  10. "thinking.enabled.budget_tokens: Input should be greater than or equal to 1024"
  11. );
  12. expect(trigger).toBe("budget_tokens_too_low");
  13. });
  14. it("should return null for unrelated errors", () => {
  15. const trigger = detectThinkingBudgetRectifierTrigger("rate limit exceeded");
  16. expect(trigger).toBeNull();
  17. });
  18. it("should return null for null/undefined input", () => {
  19. expect(detectThinkingBudgetRectifierTrigger(null)).toBeNull();
  20. expect(detectThinkingBudgetRectifierTrigger(undefined)).toBeNull();
  21. });
  22. });
  23. describe("rectifyThinkingBudget", () => {
  24. it("should rectify standard thinking budget", () => {
  25. const message: Record<string, unknown> = {
  26. model: "claude-opus-4-6",
  27. max_tokens: 4000,
  28. thinking: { type: "enabled", budget_tokens: 500 },
  29. };
  30. const result = rectifyThinkingBudget(message);
  31. expect(result.applied).toBe(true);
  32. expect(result.after.thinkingBudgetTokens).toBe(32000);
  33. expect(result.after.thinkingType).toBe("enabled");
  34. });
  35. it("should skip rectification when thinking.type is adaptive", () => {
  36. const message: Record<string, unknown> = {
  37. model: "claude-opus-4-6",
  38. max_tokens: 8000,
  39. thinking: { type: "adaptive" },
  40. };
  41. const result = rectifyThinkingBudget(message);
  42. expect(result.applied).toBe(false);
  43. expect(result.before.thinkingType).toBe("adaptive");
  44. expect(result.after.thinkingType).toBe("adaptive");
  45. });
  46. });
  47. });