hermes-agent/tests/hermes_cli/test_prompt_compose_command.py
Teknium 9e96e70995
feat(cli): /prompt — compose your next prompt in $EDITOR (#50509)
* feat(cli): /prompt — compose your next prompt in $EDITOR

Adds /prompt (alias /compose): opens $VISUAL/$EDITOR on a temp markdown
file so you can hand-edit a multi-line prompt, then sends the saved buffer
as the next agent turn. Text after the command pre-seeds the buffer; an
empty save cancels. Reuses the one-shot _pending_agent_seed the interactive
loop already consumes (same mechanism as /blueprint), so no changes to the
input event loop or message pipeline. CLI-only.

* feat(tui): /prompt slash command opens $EDITOR (parity with CLI)

The TUI already opens $EDITOR via Ctrl+G (openEditor), but had no /prompt
slash command like the classic CLI. Wire openEditor into the slash handler
context and register /prompt (alias /compose) to call it; inline text after
the command is dropped into the composer first so it carries into the editor,
matching the CLI's /prompt <text>.
2026-06-21 20:21:33 -07:00

76 lines
2.5 KiB
Python

"""Tests for the CLI `/prompt` editor-compose command.
`/prompt` opens `$VISUAL`/`$EDITOR` on a temp markdown file so the user can
hand-edit a multi-line prompt, then queues the saved buffer as the next
agent turn via the one-shot `_pending_agent_seed` (same path `/blueprint`
uses). These drive a fake editor subprocess to verify read-back, header
stripping, seeding, and the empty-buffer cancel path.
"""
import os
import stat
import tempfile
import pytest
from hermes_cli.cli_commands_mixin import CLICommandsMixin
from hermes_cli.commands import resolve_command
class _Stub(CLICommandsMixin):
def __init__(self):
self._pending_agent_seed = None
def _fake_editor(body: str, mode: str = "append") -> str:
"""Write a tiny shell 'editor' that mutates the file it is handed."""
f = tempfile.NamedTemporaryFile("w", suffix=".sh", delete=False)
if mode == "append":
f.write("#!/usr/bin/env bash\n")
f.write(f"cat >> \"$1\" <<'EOF'\n{body}\nEOF\n")
else: # clear
f.write("#!/usr/bin/env bash\n: > \"$1\"\n")
f.close()
os.chmod(f.name, os.stat(f.name).st_mode | stat.S_IEXEC)
return f.name
@pytest.fixture(autouse=True)
def _no_visual(monkeypatch):
monkeypatch.delenv("VISUAL", raising=False)
def test_command_registered():
cd = resolve_command("prompt")
assert cd and cd.name == "prompt"
assert resolve_command("compose").name == "prompt"
def test_compose_reads_and_strips_header(monkeypatch):
monkeypatch.setenv("EDITOR", _fake_editor("Refactor the auth module.\nUse pytest."))
out = _Stub()._compose_in_editor("")
assert "Refactor the auth module." in out
assert "Use pytest." in out
assert "#!" not in out # the instructional header is stripped
def test_prompt_sets_pending_seed(monkeypatch):
monkeypatch.setenv("EDITOR", _fake_editor("Write a haiku about caching."))
s = _Stub()
s._handle_prompt_compose_command("/prompt")
assert s._pending_agent_seed
assert "haiku about caching" in s._pending_agent_seed
def test_initial_text_is_seeded(monkeypatch):
# The fake editor appends, so the initial text leads the buffer.
monkeypatch.setenv("EDITOR", _fake_editor("rest of prompt"))
out = _Stub()._compose_in_editor("DRAFT: ")
assert out.startswith("DRAFT:")
def test_empty_buffer_does_not_seed(monkeypatch):
monkeypatch.setenv("EDITOR", _fake_editor("", mode="clear"))
s = _Stub()
s._handle_prompt_compose_command("/prompt")
assert s._pending_agent_seed is None