billing-header-rectifier.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { describe, expect, test } from "vitest";
  2. import { rectifyBillingHeader } from "@/app/v1/_lib/proxy/billing-header-rectifier";
  3. describe("rectifyBillingHeader", () => {
  4. test("system array with single billing header block - removes it and returns extractedValues", () => {
  5. const message: Record<string, unknown> = {
  6. system: [
  7. {
  8. type: "text",
  9. text: "x-anthropic-billing-header: cc_version=2.1.36; cc_entrypoint=cli; cch=1;",
  10. },
  11. ],
  12. };
  13. const result = rectifyBillingHeader(message);
  14. expect(result.applied).toBe(true);
  15. expect(result.removedCount).toBe(1);
  16. expect(result.extractedValues).toEqual([
  17. "x-anthropic-billing-header: cc_version=2.1.36; cc_entrypoint=cli; cch=1;",
  18. ]);
  19. expect(message.system).toEqual([]);
  20. });
  21. test("system array with multiple billing header blocks - removes all", () => {
  22. const message: Record<string, unknown> = {
  23. system: [
  24. { type: "text", text: "x-anthropic-billing-header: cc_version=2.1.36;" },
  25. { type: "text", text: "x-anthropic-billing-header: cc_entrypoint=cli;" },
  26. ],
  27. };
  28. const result = rectifyBillingHeader(message);
  29. expect(result.applied).toBe(true);
  30. expect(result.removedCount).toBe(2);
  31. expect(result.extractedValues).toHaveLength(2);
  32. expect(message.system).toEqual([]);
  33. });
  34. test("system array with no billing header - applied=false, array unchanged", () => {
  35. const blocks = [
  36. { type: "text", text: "You are a helpful assistant." },
  37. { type: "text", text: "Follow instructions carefully." },
  38. ];
  39. const message: Record<string, unknown> = {
  40. system: [...blocks],
  41. };
  42. const result = rectifyBillingHeader(message);
  43. expect(result.applied).toBe(false);
  44. expect(result.removedCount).toBe(0);
  45. expect(result.extractedValues).toEqual([]);
  46. expect(message.system).toEqual(blocks);
  47. });
  48. test("system array with billing header mixed with real prompts - only removes billing header blocks", () => {
  49. const message: Record<string, unknown> = {
  50. system: [
  51. { type: "text", text: "You are a helpful assistant." },
  52. { type: "text", text: "x-anthropic-billing-header: cc_version=2.1.36; cch=1;" },
  53. { type: "text", text: "Follow instructions carefully." },
  54. ],
  55. };
  56. const result = rectifyBillingHeader(message);
  57. expect(result.applied).toBe(true);
  58. expect(result.removedCount).toBe(1);
  59. expect(message.system).toEqual([
  60. { type: "text", text: "You are a helpful assistant." },
  61. { type: "text", text: "Follow instructions carefully." },
  62. ]);
  63. });
  64. test("system as plain string that IS a billing header - deletes system field", () => {
  65. const message: Record<string, unknown> = {
  66. system: "x-anthropic-billing-header: cc_version=2.1.36;",
  67. };
  68. const result = rectifyBillingHeader(message);
  69. expect(result.applied).toBe(true);
  70. expect(result.removedCount).toBe(1);
  71. expect(result.extractedValues).toEqual(["x-anthropic-billing-header: cc_version=2.1.36;"]);
  72. expect(message.system).toBeUndefined();
  73. });
  74. test("system as plain string that is NOT a billing header - applied=false", () => {
  75. const message: Record<string, unknown> = {
  76. system: "You are a helpful assistant.",
  77. };
  78. const result = rectifyBillingHeader(message);
  79. expect(result.applied).toBe(false);
  80. expect(result.removedCount).toBe(0);
  81. expect(message.system).toBe("You are a helpful assistant.");
  82. });
  83. test("system undefined/missing - applied=false", () => {
  84. const message: Record<string, unknown> = { model: "claude-3" };
  85. const result = rectifyBillingHeader(message);
  86. expect(result.applied).toBe(false);
  87. expect(result.removedCount).toBe(0);
  88. expect(result.extractedValues).toEqual([]);
  89. });
  90. test("system null - applied=false", () => {
  91. const message: Record<string, unknown> = { system: null };
  92. const result = rectifyBillingHeader(message);
  93. expect(result.applied).toBe(false);
  94. expect(result.removedCount).toBe(0);
  95. });
  96. test("billing header mid-string (not at start) - should NOT remove", () => {
  97. const message: Record<string, unknown> = {
  98. system: [
  99. {
  100. type: "text",
  101. text: "Some preamble text x-anthropic-billing-header: cc_version=2.1.36;",
  102. },
  103. ],
  104. };
  105. const result = rectifyBillingHeader(message);
  106. expect(result.applied).toBe(false);
  107. expect(result.removedCount).toBe(0);
  108. expect((message.system as unknown[]).length).toBe(1);
  109. });
  110. test("case insensitivity (X-Anthropic-Billing-Header:)", () => {
  111. const message: Record<string, unknown> = {
  112. system: [{ type: "text", text: "X-Anthropic-Billing-Header: cc_version=2.1.36;" }],
  113. };
  114. const result = rectifyBillingHeader(message);
  115. expect(result.applied).toBe(true);
  116. expect(result.removedCount).toBe(1);
  117. expect(message.system).toEqual([]);
  118. });
  119. test("system array becoming empty after removal - remains empty array", () => {
  120. const message: Record<string, unknown> = {
  121. system: [
  122. { type: "text", text: "x-anthropic-billing-header: val1" },
  123. { type: "text", text: "x-anthropic-billing-header: val2" },
  124. ],
  125. };
  126. const result = rectifyBillingHeader(message);
  127. expect(result.applied).toBe(true);
  128. expect(result.removedCount).toBe(2);
  129. expect(message.system).toEqual([]);
  130. });
  131. test("various billing header value formats", () => {
  132. const message: Record<string, unknown> = {
  133. system: [
  134. { type: "text", text: "x-anthropic-billing-header:cc_version=2.1.36" },
  135. {
  136. type: "text",
  137. text: " x-anthropic-billing-header: cc_version=2.2.0; cc_entrypoint=vscode;",
  138. },
  139. { type: "text", text: "x-anthropic-billing-header: " },
  140. ],
  141. };
  142. const result = rectifyBillingHeader(message);
  143. expect(result.applied).toBe(true);
  144. expect(result.removedCount).toBe(3);
  145. expect(result.extractedValues).toHaveLength(3);
  146. expect(message.system).toEqual([]);
  147. });
  148. test("non-text type blocks are preserved", () => {
  149. const message: Record<string, unknown> = {
  150. system: [
  151. { type: "image", source: { type: "base64" } },
  152. { type: "text", text: "x-anthropic-billing-header: val" },
  153. { type: "text", text: "Keep this" },
  154. ],
  155. };
  156. const result = rectifyBillingHeader(message);
  157. expect(result.applied).toBe(true);
  158. expect(result.removedCount).toBe(1);
  159. expect(message.system).toEqual([
  160. { type: "image", source: { type: "base64" } },
  161. { type: "text", text: "Keep this" },
  162. ]);
  163. });
  164. test("system as non-string non-array type (e.g. number) - no-op", () => {
  165. const message: Record<string, unknown> = { system: 42 };
  166. const result = rectifyBillingHeader(message);
  167. expect(result.applied).toBe(false);
  168. expect(result.removedCount).toBe(0);
  169. expect(message.system).toBe(42);
  170. });
  171. });