interactive-mode-import-command.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { describe, expect, it, vi } from "vitest";
  2. import { SessionImportFileNotFoundError } from "../src/core/agent-session-runtime.ts";
  3. import { InteractiveMode } from "../src/modes/interactive/interactive-mode.ts";
  4. type PathCommand = "/export" | "/import";
  5. type InteractiveModePrototype = {
  6. getPathCommandArgument(this: unknown, text: string, command: PathCommand): string | undefined;
  7. handleImportCommand(this: ImportCommandContext, text: string): Promise<void>;
  8. };
  9. type ImportCommandContext = {
  10. loadingAnimation?: { stop: () => void };
  11. statusContainer: { clear: () => void };
  12. runtimeHost: { importFromJsonl: (inputPath: string, cwdOverride?: string) => Promise<{ cancelled: boolean }> };
  13. showError: (message: string) => void;
  14. showStatus: (message: string) => void;
  15. showExtensionConfirm: (title: string, message: string) => Promise<boolean>;
  16. handleRuntimeSessionChange: () => Promise<void>;
  17. renderCurrentSessionState: () => void;
  18. handleFatalRuntimeError: (prefix: string, error: unknown) => Promise<never>;
  19. promptForMissingSessionCwd: (error: unknown) => Promise<string | undefined>;
  20. getPathCommandArgument: (text: string, command: PathCommand) => string | undefined;
  21. };
  22. const interactiveModePrototype = InteractiveMode.prototype as unknown as InteractiveModePrototype;
  23. describe("InteractiveMode /import parsing", () => {
  24. it("strips quotes from /import path arguments", () => {
  25. expect(interactiveModePrototype.getPathCommandArgument('/import "path/to/session.jsonl"', "/import")).toBe(
  26. "path/to/session.jsonl",
  27. );
  28. expect(
  29. interactiveModePrototype.getPathCommandArgument('/import "path with spaces/session.jsonl"', "/import"),
  30. ).toBe("path with spaces/session.jsonl");
  31. });
  32. it("preserves apostrophes in unquoted /import path arguments", () => {
  33. expect(interactiveModePrototype.getPathCommandArgument("/import john's/session.jsonl", "/import")).toBe(
  34. "john's/session.jsonl",
  35. );
  36. });
  37. it("enforces command token boundaries", () => {
  38. expect(interactiveModePrototype.getPathCommandArgument("/important /tmp/session.jsonl", "/import")).toBe(
  39. undefined,
  40. );
  41. expect(interactiveModePrototype.getPathCommandArgument("/exporter out.html", "/export")).toBe(undefined);
  42. expect(interactiveModePrototype.getPathCommandArgument("/import /tmp/session.jsonl", "/import")).toBe(
  43. "/tmp/session.jsonl",
  44. );
  45. });
  46. it("passes unquoted path to runtimeHost.importFromJsonl", async () => {
  47. const importFromJsonl = vi.fn(async () => ({ cancelled: false }));
  48. const showExtensionConfirm = vi.fn(async () => true);
  49. const showStatus = vi.fn();
  50. const showError = vi.fn();
  51. const context: ImportCommandContext = {
  52. statusContainer: { clear: vi.fn() },
  53. runtimeHost: { importFromJsonl },
  54. showError,
  55. showStatus,
  56. showExtensionConfirm,
  57. handleRuntimeSessionChange: vi.fn(async () => {}),
  58. renderCurrentSessionState: vi.fn(),
  59. handleFatalRuntimeError: vi.fn(async () => {
  60. throw new Error("unexpected fatal error");
  61. }),
  62. promptForMissingSessionCwd: vi.fn(async () => undefined),
  63. getPathCommandArgument: interactiveModePrototype.getPathCommandArgument,
  64. };
  65. await interactiveModePrototype.handleImportCommand.call(context, '/import "path/to/session.jsonl"');
  66. expect(showExtensionConfirm).toHaveBeenCalledWith(
  67. "Import session",
  68. "Replace current session with path/to/session.jsonl?",
  69. );
  70. expect(importFromJsonl).toHaveBeenCalledWith("path/to/session.jsonl");
  71. expect(showError).not.toHaveBeenCalled();
  72. expect(showStatus).toHaveBeenCalledWith("Session imported from: path/to/session.jsonl");
  73. });
  74. it("passes unquoted apostrophe path to runtimeHost.importFromJsonl unchanged", async () => {
  75. const importFromJsonl = vi.fn(async () => ({ cancelled: false }));
  76. const showExtensionConfirm = vi.fn(async () => true);
  77. const showStatus = vi.fn();
  78. const showError = vi.fn();
  79. const context: ImportCommandContext = {
  80. statusContainer: { clear: vi.fn() },
  81. runtimeHost: { importFromJsonl },
  82. showError,
  83. showStatus,
  84. showExtensionConfirm,
  85. handleRuntimeSessionChange: vi.fn(async () => {}),
  86. renderCurrentSessionState: vi.fn(),
  87. handleFatalRuntimeError: vi.fn(async () => {
  88. throw new Error("unexpected fatal error");
  89. }),
  90. promptForMissingSessionCwd: vi.fn(async () => undefined),
  91. getPathCommandArgument: interactiveModePrototype.getPathCommandArgument,
  92. };
  93. await interactiveModePrototype.handleImportCommand.call(context, "/import john's/session.jsonl");
  94. expect(importFromJsonl).toHaveBeenCalledWith("john's/session.jsonl");
  95. expect(showError).not.toHaveBeenCalled();
  96. expect(showStatus).toHaveBeenCalledWith("Session imported from: john's/session.jsonl");
  97. });
  98. it("shows a non-fatal error when /import path does not exist", async () => {
  99. const importFromJsonl = vi.fn(async () => {
  100. throw new SessionImportFileNotFoundError("/tmp/missing-session.jsonl");
  101. });
  102. const showExtensionConfirm = vi.fn(async () => true);
  103. const showStatus = vi.fn();
  104. const showError = vi.fn();
  105. const handleFatalRuntimeError = vi.fn(async () => {
  106. throw new Error("unexpected fatal error");
  107. });
  108. const context: ImportCommandContext = {
  109. statusContainer: { clear: vi.fn() },
  110. runtimeHost: { importFromJsonl },
  111. showError,
  112. showStatus,
  113. showExtensionConfirm,
  114. handleRuntimeSessionChange: vi.fn(async () => {}),
  115. renderCurrentSessionState: vi.fn(),
  116. handleFatalRuntimeError,
  117. promptForMissingSessionCwd: vi.fn(async () => undefined),
  118. getPathCommandArgument: interactiveModePrototype.getPathCommandArgument,
  119. };
  120. await interactiveModePrototype.handleImportCommand.call(context, "/import /tmp/missing-session.jsonl");
  121. expect(showError).toHaveBeenCalledWith("Failed to import session: File not found: /tmp/missing-session.jsonl");
  122. expect(showStatus).not.toHaveBeenCalled();
  123. expect(handleFatalRuntimeError).not.toHaveBeenCalled();
  124. });
  125. });