interactive-mode-clone-command.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { describe, expect, it, vi } from "vitest";
  2. import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts";
  3. type CloneCommandContext = {
  4. sessionManager: { getLeafId: () => string | null };
  5. runtimeHost: {
  6. fork: (entryId: string, options?: { position?: "before" | "at" }) => Promise<{ cancelled: boolean }>;
  7. };
  8. renderCurrentSessionState: () => void;
  9. editor: { setText: (text: string) => void };
  10. showStatus: (message: string) => void;
  11. showError: (message: string) => void;
  12. ui: { requestRender: () => void };
  13. };
  14. type InteractiveModePrototype = {
  15. handleCloneCommand(this: CloneCommandContext): Promise<void>;
  16. };
  17. const interactiveModePrototype = InteractiveMode.prototype as unknown as InteractiveModePrototype;
  18. describe("InteractiveMode /clone", () => {
  19. it("clones the current leaf into a new session", async () => {
  20. const fork = vi.fn(async () => ({ cancelled: false }));
  21. const renderCurrentSessionState = vi.fn();
  22. const setText = vi.fn();
  23. const showStatus = vi.fn();
  24. const showError = vi.fn();
  25. const requestRender = vi.fn();
  26. const context: CloneCommandContext = {
  27. sessionManager: { getLeafId: () => "leaf-123" },
  28. runtimeHost: { fork },
  29. renderCurrentSessionState,
  30. editor: { setText },
  31. showStatus,
  32. showError,
  33. ui: { requestRender },
  34. };
  35. await interactiveModePrototype.handleCloneCommand.call(context);
  36. expect(fork).toHaveBeenCalledWith("leaf-123", { position: "at" });
  37. expect(renderCurrentSessionState).toHaveBeenCalled();
  38. expect(setText).toHaveBeenCalledWith("");
  39. expect(showStatus).toHaveBeenCalledWith("Cloned to new session");
  40. expect(showError).not.toHaveBeenCalled();
  41. expect(requestRender).not.toHaveBeenCalled();
  42. });
  43. it("shows a status message when there is nothing to clone", async () => {
  44. const fork = vi.fn(async () => ({ cancelled: false }));
  45. const showStatus = vi.fn();
  46. const showError = vi.fn();
  47. const context: CloneCommandContext = {
  48. sessionManager: { getLeafId: () => null },
  49. runtimeHost: { fork },
  50. renderCurrentSessionState: vi.fn(),
  51. editor: { setText: vi.fn() },
  52. showStatus,
  53. showError,
  54. ui: { requestRender: vi.fn() },
  55. };
  56. await interactiveModePrototype.handleCloneCommand.call(context);
  57. expect(fork).not.toHaveBeenCalled();
  58. expect(showStatus).toHaveBeenCalledWith("Nothing to clone yet");
  59. expect(showError).not.toHaveBeenCalled();
  60. });
  61. });