-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_thinking_capture.py
More file actions
308 lines (252 loc) · 9.35 KB
/
test_thinking_capture.py
File metadata and controls
308 lines (252 loc) · 9.35 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""
Tests for thinking capture functionality
"""
import os
import pytest
import tempfile
from datetime import datetime
from sugar.executor.thinking_display import (
ThinkingCapture,
ThinkingBlock,
read_thinking_log,
list_thinking_logs,
)
class TestThinkingBlock:
"""Test ThinkingBlock dataclass"""
def test_basic_thinking_block(self):
"""Test creating a basic thinking block"""
block = ThinkingBlock(
timestamp=datetime.now(),
content="Test thinking content",
)
assert block.content == "Test thinking content"
assert block.tool_use is None
assert block.signature is None
def test_thinking_block_with_tool(self):
"""Test thinking block with tool use"""
block = ThinkingBlock(
timestamp=datetime.now(),
content="Considering reading a file",
tool_use="Read",
signature="sig123",
)
assert block.content == "Considering reading a file"
assert block.tool_use == "Read"
assert block.signature == "sig123"
class TestThinkingCapture:
"""Test ThinkingCapture class"""
@pytest.fixture
def temp_dir(self):
"""Create a temporary directory for testing"""
with tempfile.TemporaryDirectory() as tmpdir:
# Change to temp dir for tests
original_dir = os.getcwd()
os.chdir(tmpdir)
yield tmpdir
os.chdir(original_dir)
def test_thinking_capture_initialization(self, temp_dir):
"""Test initializing thinking capture"""
capture = ThinkingCapture(
task_id="test-123",
task_title="Test Task",
)
assert capture.task_id == "test-123"
assert capture.task_title == "Test Task"
assert len(capture.thinking_blocks) == 0
assert capture.log_to_file is True
def test_capture_thinking_block(self, temp_dir):
"""Test capturing a thinking block"""
capture = ThinkingCapture(
task_id="test-123",
task_title="Test Task",
)
capture.capture(
thinking_content="This is my reasoning about the problem",
tool_use="Read",
)
assert len(capture.thinking_blocks) == 1
assert (
capture.thinking_blocks[0].content
== "This is my reasoning about the problem"
)
assert capture.thinking_blocks[0].tool_use == "Read"
def test_capture_multiple_blocks(self, temp_dir):
"""Test capturing multiple thinking blocks"""
capture = ThinkingCapture(
task_id="test-123",
task_title="Test Task",
)
capture.capture("First thought")
capture.capture("Second thought", tool_use="Write")
capture.capture("Third thought", tool_use="Bash")
assert len(capture.thinking_blocks) == 3
def test_get_summary(self, temp_dir):
"""Test getting thinking summary"""
capture = ThinkingCapture(
task_id="test-123",
task_title="Test Task",
)
# No thinking captured
summary = capture.get_summary()
assert "No thinking captured" in summary
# With thinking
capture.capture("Some thinking" * 100) # Long thinking
summary = capture.get_summary()
assert "1 thinking blocks" in summary
assert ".sugar/thinking/test-123.md" in summary
def test_get_stats(self, temp_dir):
"""Test getting thinking statistics"""
capture = ThinkingCapture(
task_id="test-123",
task_title="Test Task",
)
# No thinking
stats = capture.get_stats()
assert stats["count"] == 0
assert stats["total_characters"] == 0
# With thinking
capture.capture("Thinking 1" * 10, tool_use="Read")
capture.capture("Thinking 2" * 20, tool_use="Write")
capture.capture("Thinking 3" * 5)
stats = capture.get_stats()
assert stats["count"] == 3
assert stats["total_characters"] > 0
assert stats["average_length"] > 0
assert "Read" in stats["tool_uses_considered"]
assert "Write" in stats["tool_uses_considered"]
assert "first_thinking" in stats
assert "last_thinking" in stats
def test_thinking_log_file_creation(self, temp_dir):
"""Test that thinking log file is created"""
capture = ThinkingCapture(
task_id="test-123",
task_title="Test Task",
)
capture.capture("Test thinking content")
capture.finalize()
# Check file exists
log_path = ".sugar/thinking/test-123.md"
assert os.path.exists(log_path)
# Check file content
with open(log_path, "r") as f:
content = f.read()
assert "Test Task" in content
assert "test-123" in content
assert "Test thinking content" in content
def test_thinking_log_without_file(self, temp_dir):
"""Test thinking capture without file logging"""
capture = ThinkingCapture(
task_id="test-123",
task_title="Test Task",
log_to_file=False,
)
capture.capture("Test thinking")
capture.finalize()
# File should not be created
log_path = ".sugar/thinking/test-123.md"
assert not os.path.exists(log_path)
# But thinking should still be captured in memory
assert len(capture.thinking_blocks) == 1
def test_skip_empty_thinking(self, temp_dir):
"""Test that empty thinking is skipped"""
capture = ThinkingCapture(
task_id="test-123",
task_title="Test Task",
)
capture.capture("")
capture.capture(" ")
capture.capture("Valid thinking")
assert len(capture.thinking_blocks) == 1
assert capture.thinking_blocks[0].content == "Valid thinking"
class TestThinkingReadFunctions:
"""Test helper functions for reading thinking logs"""
@pytest.fixture
def temp_dir(self):
"""Create a temporary directory for testing"""
with tempfile.TemporaryDirectory() as tmpdir:
original_dir = os.getcwd()
os.chdir(tmpdir)
yield tmpdir
os.chdir(original_dir)
def test_read_thinking_log(self, temp_dir):
"""Test reading a thinking log"""
# Create a thinking log
capture = ThinkingCapture(
task_id="read-test",
task_title="Read Test",
)
capture.capture("Test thinking content")
capture.finalize()
# Read it back
content = read_thinking_log("read-test")
assert content is not None
assert "Test thinking content" in content
def test_read_nonexistent_log(self, temp_dir):
"""Test reading a non-existent log"""
content = read_thinking_log("nonexistent")
assert content is None
def test_list_thinking_logs(self, temp_dir):
"""Test listing thinking logs"""
# Create multiple logs
for i in range(3):
capture = ThinkingCapture(
task_id=f"task-{i}",
task_title=f"Task {i}",
)
capture.capture(f"Thinking {i}")
capture.finalize()
# List them
logs = list_thinking_logs()
assert len(logs) == 3
# Check structure
task_id, log_path, modified_time = logs[0]
assert task_id.startswith("task-")
assert log_path.endswith(".md")
assert isinstance(modified_time, datetime)
def test_list_no_logs(self, temp_dir):
"""Test listing when no logs exist"""
logs = list_thinking_logs()
assert len(logs) == 0
class TestThinkingIntegration:
"""Integration tests for thinking capture"""
@pytest.fixture
def temp_dir(self):
"""Create a temporary directory for testing"""
with tempfile.TemporaryDirectory() as tmpdir:
original_dir = os.getcwd()
os.chdir(tmpdir)
yield tmpdir
os.chdir(original_dir)
def test_full_thinking_capture_workflow(self, temp_dir):
"""Test complete workflow of capturing thinking"""
# Setup
task_id = "workflow-test"
capture = ThinkingCapture(
task_id=task_id,
task_title="Workflow Test Task",
)
# Simulate thinking during execution
capture.capture("First, I need to understand the requirements")
capture.capture("Now I'll read the relevant files", tool_use="Read")
capture.capture("I should write the implementation", tool_use="Write")
capture.capture("Finally, I'll test the changes", tool_use="Bash")
# Finalize
capture.finalize()
# Verify results
assert len(capture.thinking_blocks) == 4
# Check stats
stats = capture.get_stats()
assert stats["count"] == 4
assert len(stats["tool_uses_considered"]) == 3
# Check file
content = read_thinking_log(task_id)
assert content is not None
assert "understand the requirements" in content
assert "Read" in content
assert "Write" in content
assert "Bash" in content
assert "Summary" in content
# Check it appears in list
logs = list_thinking_logs()
task_ids = [log[0] for log in logs]
assert task_id in task_ids