|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from textual.app import App, ComposeResult |
| 4 | +from textual.widgets import Markdown |
| 5 | + |
| 6 | +from agent_chat_cli.components.chat_history import ChatHistory |
| 7 | +from agent_chat_cli.components.messages import ( |
| 8 | + SystemMessage, |
| 9 | + UserMessage, |
| 10 | + AgentMessage, |
| 11 | + ToolMessage, |
| 12 | +) |
| 13 | +from agent_chat_cli.utils.save_conversation import save_conversation |
| 14 | + |
| 15 | + |
| 16 | +class TestSaveConversation: |
| 17 | + async def test_saves_user_and_agent_messages(self, tmp_path, monkeypatch): |
| 18 | + monkeypatch.setattr(Path, "home", lambda: tmp_path) |
| 19 | + |
| 20 | + class TestApp(App): |
| 21 | + def compose(self) -> ComposeResult: |
| 22 | + yield ChatHistory() |
| 23 | + |
| 24 | + app = TestApp() |
| 25 | + async with app.run_test(): |
| 26 | + chat_history = app.query_one(ChatHistory) |
| 27 | + |
| 28 | + user_msg = UserMessage() |
| 29 | + user_msg.message = "Hello" |
| 30 | + await chat_history.mount(user_msg) |
| 31 | + |
| 32 | + agent_msg = AgentMessage() |
| 33 | + agent_msg.message = "Hi there!" |
| 34 | + await chat_history.mount(agent_msg) |
| 35 | + markdown_widget = agent_msg.query_one(Markdown) |
| 36 | + markdown_widget.update("Hi there!") |
| 37 | + |
| 38 | + file_path = save_conversation(chat_history) |
| 39 | + |
| 40 | + assert Path(file_path).exists() |
| 41 | + content = Path(file_path).read_text() |
| 42 | + assert "# You" in content |
| 43 | + assert "Hello" in content |
| 44 | + assert "# Agent" in content |
| 45 | + assert "Hi there!" in content |
| 46 | + |
| 47 | + async def test_saves_system_messages(self, tmp_path, monkeypatch): |
| 48 | + monkeypatch.setattr(Path, "home", lambda: tmp_path) |
| 49 | + |
| 50 | + class TestApp(App): |
| 51 | + def compose(self) -> ComposeResult: |
| 52 | + yield ChatHistory() |
| 53 | + |
| 54 | + app = TestApp() |
| 55 | + async with app.run_test(): |
| 56 | + chat_history = app.query_one(ChatHistory) |
| 57 | + |
| 58 | + system_msg = SystemMessage() |
| 59 | + system_msg.message = "Connection established" |
| 60 | + await chat_history.mount(system_msg) |
| 61 | + |
| 62 | + file_path = save_conversation(chat_history) |
| 63 | + |
| 64 | + assert Path(file_path).exists() |
| 65 | + content = Path(file_path).read_text() |
| 66 | + assert "# System" in content |
| 67 | + assert "Connection established" in content |
| 68 | + |
| 69 | + async def test_saves_tool_messages(self, tmp_path, monkeypatch): |
| 70 | + monkeypatch.setattr(Path, "home", lambda: tmp_path) |
| 71 | + |
| 72 | + class TestApp(App): |
| 73 | + def compose(self) -> ComposeResult: |
| 74 | + yield ChatHistory() |
| 75 | + |
| 76 | + app = TestApp() |
| 77 | + async with app.run_test(): |
| 78 | + chat_history = app.query_one(ChatHistory) |
| 79 | + |
| 80 | + tool_msg = ToolMessage() |
| 81 | + tool_msg.tool_name = "fetch_url" |
| 82 | + tool_msg.tool_input = {"url": "https://example.com"} |
| 83 | + await chat_history.mount(tool_msg) |
| 84 | + |
| 85 | + file_path = save_conversation(chat_history) |
| 86 | + |
| 87 | + assert Path(file_path).exists() |
| 88 | + content = Path(file_path).read_text() |
| 89 | + assert "# Tool: fetch_url" in content |
| 90 | + assert "https://example.com" in content |
| 91 | + |
| 92 | + async def test_creates_directory_structure(self, tmp_path, monkeypatch): |
| 93 | + monkeypatch.setattr(Path, "home", lambda: tmp_path) |
| 94 | + |
| 95 | + class TestApp(App): |
| 96 | + def compose(self) -> ComposeResult: |
| 97 | + yield ChatHistory() |
| 98 | + |
| 99 | + app = TestApp() |
| 100 | + async with app.run_test(): |
| 101 | + chat_history = app.query_one(ChatHistory) |
| 102 | + file_path = save_conversation(chat_history) |
| 103 | + |
| 104 | + output_dir = tmp_path / ".claude" / "agent-chat-cli" |
| 105 | + assert output_dir.exists() |
| 106 | + assert Path(file_path).parent == output_dir |
| 107 | + |
| 108 | + async def test_uses_timestamp_in_filename(self, tmp_path, monkeypatch): |
| 109 | + monkeypatch.setattr(Path, "home", lambda: tmp_path) |
| 110 | + |
| 111 | + class TestApp(App): |
| 112 | + def compose(self) -> ComposeResult: |
| 113 | + yield ChatHistory() |
| 114 | + |
| 115 | + app = TestApp() |
| 116 | + async with app.run_test(): |
| 117 | + chat_history = app.query_one(ChatHistory) |
| 118 | + file_path = save_conversation(chat_history) |
| 119 | + |
| 120 | + filename = Path(file_path).name |
| 121 | + assert filename.startswith("convo-") |
| 122 | + assert filename.endswith(".md") |
0 commit comments