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