interactive-mode-compaction.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { describe, expect, test, vi } from "vitest";
  2. import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts";
  3. describe("InteractiveMode compaction events", () => {
  4. test("rebuilds chat and appends a synthetic compaction summary at the bottom", async () => {
  5. const fakeThis = {
  6. isInitialized: true,
  7. footer: { invalidate: vi.fn() },
  8. autoCompactionEscapeHandler: undefined as (() => void) | undefined,
  9. autoCompactionLoader: undefined,
  10. defaultEditor: {},
  11. statusContainer: { clear: vi.fn() },
  12. chatContainer: { clear: vi.fn() },
  13. rebuildChatFromMessages: vi.fn(),
  14. addMessageToChat: vi.fn(),
  15. showError: vi.fn(),
  16. showStatus: vi.fn(),
  17. flushCompactionQueue: vi.fn().mockResolvedValue(undefined),
  18. settingsManager: { getShowTerminalProgress: () => false },
  19. ui: { requestRender: vi.fn(), terminal: { setProgress: vi.fn() } },
  20. };
  21. const handleEvent = Reflect.get(InteractiveMode.prototype, "handleEvent") as (
  22. this: typeof fakeThis,
  23. event: {
  24. type: "compaction_end";
  25. reason: "manual" | "threshold" | "overflow";
  26. result: { tokensBefore: number; summary: string } | undefined;
  27. aborted: boolean;
  28. willRetry: boolean;
  29. errorMessage?: string;
  30. },
  31. ) => Promise<void>;
  32. await handleEvent.call(fakeThis, {
  33. type: "compaction_end",
  34. reason: "manual",
  35. result: {
  36. tokensBefore: 123,
  37. summary: "summary",
  38. },
  39. aborted: false,
  40. willRetry: false,
  41. });
  42. expect(fakeThis.chatContainer.clear).toHaveBeenCalledTimes(1);
  43. expect(fakeThis.rebuildChatFromMessages).toHaveBeenCalledTimes(1);
  44. expect(fakeThis.addMessageToChat).toHaveBeenCalledTimes(1);
  45. expect(fakeThis.addMessageToChat).toHaveBeenCalledWith(
  46. expect.objectContaining({
  47. role: "compactionSummary",
  48. tokensBefore: 123,
  49. summary: "summary",
  50. }),
  51. );
  52. expect(fakeThis.flushCompactionQueue).toHaveBeenCalledWith({ willRetry: false });
  53. });
  54. });