contentArray.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { describe, expect, test } from "bun:test";
  2. import { insertBlockAfterToolResults } from "../contentArray";
  3. describe("insertBlockAfterToolResults", () => {
  4. test("inserts after last tool_result", () => {
  5. const content: any[] = [
  6. { type: "tool_result", content: "r1" },
  7. { type: "text", text: "hello" },
  8. ];
  9. insertBlockAfterToolResults(content, { type: "text", text: "inserted" });
  10. expect(content[1]).toEqual({ type: "text", text: "inserted" });
  11. expect(content).toHaveLength(3);
  12. });
  13. test("inserts after last of multiple tool_results", () => {
  14. const content: any[] = [
  15. { type: "tool_result", content: "r1" },
  16. { type: "tool_result", content: "r2" },
  17. { type: "text", text: "end" },
  18. ];
  19. insertBlockAfterToolResults(content, { type: "text", text: "new" });
  20. expect(content[2]).toEqual({ type: "text", text: "new" });
  21. });
  22. test("appends continuation when inserted block would be last", () => {
  23. const content: any[] = [{ type: "tool_result", content: "r1" }];
  24. insertBlockAfterToolResults(content, { type: "text", text: "new" });
  25. expect(content).toHaveLength(3); // original + inserted + continuation
  26. expect(content[2]).toEqual({ type: "text", text: "." });
  27. });
  28. test("inserts before last block when no tool_results", () => {
  29. const content: any[] = [
  30. { type: "text", text: "a" },
  31. { type: "text", text: "b" },
  32. ];
  33. insertBlockAfterToolResults(content, { type: "text", text: "new" });
  34. expect(content[1]).toEqual({ type: "text", text: "new" });
  35. expect(content).toHaveLength(3);
  36. });
  37. test("handles empty array", () => {
  38. const content: any[] = [];
  39. insertBlockAfterToolResults(content, { type: "text", text: "new" });
  40. expect(content).toHaveLength(1);
  41. expect(content[0]).toEqual({ type: "text", text: "new" });
  42. });
  43. test("handles single element array with no tool_result", () => {
  44. const content: any[] = [{ type: "text", text: "only" }];
  45. insertBlockAfterToolResults(content, { type: "text", text: "new" });
  46. expect(content[0]).toEqual({ type: "text", text: "new" });
  47. expect(content[1]).toEqual({ type: "text", text: "only" });
  48. });
  49. test("inserts after last tool_result with mixed interleaving", () => {
  50. const content: any[] = [
  51. { type: "tool_result", content: "r1" },
  52. { type: "text", text: "mid1" },
  53. { type: "tool_result", content: "r2" },
  54. { type: "text", text: "mid2" },
  55. { type: "tool_result", content: "r3" },
  56. { type: "text", text: "end" },
  57. ];
  58. insertBlockAfterToolResults(content, { type: "text", text: "inserted" });
  59. // Inserted after r3 (index 4), so at index 5
  60. expect(content[5]).toEqual({ type: "text", text: "inserted" });
  61. // Original end text should shift to index 6
  62. expect(content[6]).toEqual({ type: "text", text: "end" });
  63. expect(content).toHaveLength(7);
  64. });
  65. });