interactive-mode-startup-input.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { describe, expect, it, vi } from "vitest";
  2. import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts";
  3. type SubmitContext = {
  4. defaultEditor: { onSubmit?: (text: string) => void };
  5. editor: {
  6. addToHistory?: (text: string) => void;
  7. setText: (text: string) => void;
  8. };
  9. session: {
  10. isCompacting: boolean;
  11. isStreaming: boolean;
  12. isBashRunning: boolean;
  13. prompt: (text: string, options?: unknown) => Promise<void>;
  14. };
  15. flushPendingBashComponents: () => void;
  16. onInputCallback?: (text: string) => void;
  17. pendingUserInputs: string[];
  18. };
  19. type InputContext = {
  20. onInputCallback?: (text: string) => void;
  21. pendingUserInputs: string[];
  22. };
  23. type InteractiveModePrivate = {
  24. setupEditorSubmitHandler(this: SubmitContext): void;
  25. getUserInput(this: InputContext): Promise<string>;
  26. };
  27. const interactiveModePrototype = InteractiveMode.prototype as unknown as InteractiveModePrivate;
  28. function createSubmitContext(): SubmitContext {
  29. return {
  30. defaultEditor: {},
  31. editor: {
  32. addToHistory: vi.fn(),
  33. setText: vi.fn(),
  34. },
  35. session: {
  36. isCompacting: false,
  37. isStreaming: false,
  38. isBashRunning: false,
  39. prompt: vi.fn(async () => {}),
  40. },
  41. flushPendingBashComponents: vi.fn(),
  42. pendingUserInputs: [],
  43. };
  44. }
  45. describe("InteractiveMode startup input", () => {
  46. it("queues a normal prompt submitted before the input callback is installed", async () => {
  47. const context = createSubmitContext();
  48. interactiveModePrototype.setupEditorSubmitHandler.call(context);
  49. await context.defaultEditor.onSubmit?.(" early prompt ");
  50. expect(context.pendingUserInputs).toEqual(["early prompt"]);
  51. expect(context.flushPendingBashComponents).toHaveBeenCalledTimes(1);
  52. expect(context.editor.addToHistory).toHaveBeenCalledWith("early prompt");
  53. });
  54. it("returns queued startup input before installing a new input callback", async () => {
  55. const context: InputContext = {
  56. pendingUserInputs: ["queued prompt"],
  57. };
  58. await expect(interactiveModePrototype.getUserInput.call(context)).resolves.toBe("queued prompt");
  59. expect(context.onInputCallback).toBeUndefined();
  60. expect(context.pendingUserInputs).toEqual([]);
  61. });
  62. });