@@ -810,6 +810,71 @@ BINDING-SPEC is (DIR CHAT-NAME INPUT-NAME PROC). DIR is evaluated once."
810810 (should (member " fix-tests" (nth 2 completion)))
811811 (should (member " review" (nth 2 completion)))))))
812812
813+ (ert-deftest pi-coding-agent-test-run-command-formats-command-text ()
814+ " run-command builds literal slash commands from NAME and optional args."
815+ (let ((sent-messages nil )
816+ (fake-proc (start-process " test" nil " cat" )))
817+ (set-process-query-on-exit-flag fake-proc nil )
818+ (unwind-protect
819+ (with-temp-buffer
820+ (pi-coding-agent-chat-mode)
821+ (let ((pi-coding-agent--process fake-proc))
822+ (cl-letf (((symbol-function 'pi-coding-agent--rpc-async )
823+ (lambda (_proc msg _cb )
824+ (push (plist-get msg :message ) sent-messages))))
825+ (pi-coding-agent-run-command " greet" )
826+ (pi-coding-agent-run-command " greet" " " )
827+ (pi-coding-agent-run-command " greet" " world" )
828+ (should (equal (nreverse sent-messages)
829+ '(" /greet" " /greet" " /greet world" ))))))
830+ (delete-process fake-proc))))
831+
832+ (ert-deftest pi-coding-agent-test-run-command-uses-linked-input-session ()
833+ " run-command sends through the chat buffer linked to current input."
834+ (let ((sent-message nil )
835+ (fake-proc (start-process " test" nil " cat" ))
836+ (chat-buf (generate-new-buffer " *pi-command-chat*" ))
837+ (input-buf (generate-new-buffer " *pi-command-input*" )))
838+ (set-process-query-on-exit-flag fake-proc nil )
839+ (unwind-protect
840+ (progn
841+ (with-current-buffer chat-buf
842+ (pi-coding-agent-chat-mode)
843+ (setq pi-coding-agent--process fake-proc)
844+ (pi-coding-agent--set-input-buffer input-buf))
845+ (with-current-buffer input-buf
846+ (pi-coding-agent-input-mode)
847+ (pi-coding-agent--set-chat-buffer chat-buf)
848+ (cl-letf (((symbol-function 'pi-coding-agent--rpc-async )
849+ (lambda (_proc msg _cb )
850+ (setq sent-message (plist-get msg :message )))))
851+ (pi-coding-agent-run-command " greet" " world" )))
852+ (should (equal sent-message " /greet world" )))
853+ (pi-coding-agent-test--kill-live-buffers input-buf chat-buf)
854+ (delete-process fake-proc))))
855+
856+ (ert-deftest pi-coding-agent-test-run-command-requires-current-session ()
857+ " run-command reports a missing current pi session."
858+ (with-temp-buffer
859+ (should-error (pi-coding-agent-run-command " greet" )
860+ :type 'user-error )))
861+
862+ (ert-deftest pi-coding-agent-test-run-command-interactive-requires-session-first ()
863+ " run-command reports a missing session before prompting interactively."
864+ (with-temp-buffer
865+ (let (prompted)
866+ (cl-letf (((symbol-function 'completing-read )
867+ (lambda (&rest _args )
868+ (setq prompted t )
869+ " greet" ))
870+ ((symbol-function 'read-string )
871+ (lambda (&rest _args )
872+ (setq prompted t )
873+ " " )))
874+ (should-error (call-interactively #'pi-coding-agent-run-command )
875+ :type 'user-error )
876+ (should-not prompted)))))
877+
813878(ert-deftest pi-coding-agent-test-run-custom-command-sends-literal ()
814879 " run-custom-command sends literal /command text, not expanded."
815880 (let* ((sent-message nil )
0 commit comments