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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
import io
import json
import os
import re
import tempfile
import unittest
import unittest.mock
from contextlib import redirect_stderr, redirect_stdout
from claude_logbook import cli
from .fixtures import (
memory_tree, simple_tree, ts, user, write_memory, write_session,
)
PAYLOAD_RE = re.compile(
r'<script id="payload" type="application/json">(.*?)</script>', re.S)
class CliCase(unittest.TestCase):
"""Each test runs against a fake ~/.claude and cache."""
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.addCleanup(self._tmp.cleanup)
self.home = self._tmp.name
self.root = os.path.join(self.home, ".claude", "projects")
os.makedirs(self.root)
env = unittest.mock.patch.dict(os.environ, {
"CLAUDE_CONFIG_DIR": os.path.join(self.home, ".claude"),
"XDG_CACHE_HOME": os.path.join(self.home, "cache"),
"NO_COLOR": "1",
})
env.start()
self.addCleanup(env.stop)
def run_cli(self, *argv):
out, err = io.StringIO(), io.StringIO()
with redirect_stdout(out), redirect_stderr(err):
code = cli.main(list(argv))
return code, out.getvalue(), err.getvalue()
class TestTable(CliCase):
def test_lists_the_sessions(self):
simple_tree(self.root)
code, out, _ = self.run_cli()
self.assertEqual(code, 0)
self.assertIn("Fix the build", out)
self.assertIn("3 sessions · 2 projects", out)
def test_filters_by_text(self):
simple_tree(self.root)
code, out, _ = self.run_cli("fix the")
self.assertEqual(code, 0)
self.assertIn("1 of 3 sessions", out)
def test_limits_the_count(self):
simple_tree(self.root)
_, out, _ = self.run_cli("-n", "1")
self.assertIn("1 of 3 sessions", out)
def test_filter_without_results_exits_with_1(self):
simple_tree(self.root)
code, _, err = self.run_cli("nothing-like-this")
self.assertEqual(code, 1)
self.assertIn("No session matches", err)
def test_no_claude_directory_exits_with_2(self):
with unittest.mock.patch.dict(os.environ, {"CLAUDE_CONFIG_DIR": "/does/not/exist"}):
code, _, err = self.run_cli()
self.assertEqual(code, 2)
self.assertIn("does not exist", err)
def test_no_session_exits_with_1(self):
code, _, err = self.run_cli()
self.assertEqual(code, 1)
self.assertIn("No sessions recorded", err)
class TestExport(CliCase):
def test_json(self):
simple_tree(self.root)
code, out, _ = self.run_cli("--json")
data = json.loads(out)
self.assertEqual(code, 0)
self.assertEqual(len(data["s"]), 3)
self.assertEqual(data["m"], [])
self.assertNotIn("project_dir", data["s"][0])
def test_html(self):
simple_tree(self.root)
out_path = os.path.join(self.home, "s.html")
code, _, err = self.run_cli("--html", out_path)
self.assertEqual(code, 0)
self.assertIn("3 sessions", err)
with open(out_path, encoding="utf-8") as f:
html = f.read()
payload = json.loads(PAYLOAD_RE.findall(html)[0])
self.assertEqual(len(payload["s"]), 3)
self.assertEqual(payload["m"], [])
def test_json_of_one_session(self):
simple_tree(self.root)
code, out, _ = self.run_cli("-s", "cccccccc", "--json")
self.assertEqual(code, 0)
data = json.loads(out)
self.assertEqual([r["id"][:8] for r in data["s"]], ["cccccccc"])
def test_json_honours_filters_and_limit(self):
simple_tree(self.root)
_, out, _ = self.run_cli("-p", "/home/u/proj", "--json")
self.assertTrue(all(r["p"] == "/home/u/proj" for r in json.loads(out)["s"]))
_, out, _ = self.run_cli("-n", "1", "--json")
self.assertEqual(len(json.loads(out)["s"]), 1)
def test_export_with_no_match(self):
simple_tree(self.root)
code, _, err = self.run_cli("nothing-like-this", "--json")
self.assertEqual(code, 1)
self.assertIn("No session matches", err)
def test_html_of_one_session_gets_its_own_name(self):
simple_tree(self.root)
cwd = os.getcwd()
os.chdir(self.home)
self.addCleanup(os.chdir, cwd)
code, _, err = self.run_cli("-s", "aaaaaaaa", "--html")
self.assertEqual(code, 0)
self.assertIn("1 sessions", err)
with open("session-aaaaaaaa.html", encoding="utf-8") as f:
payload = json.loads(PAYLOAD_RE.findall(f.read())[0])
self.assertEqual(len(payload["s"]), 1)
def test_default_html_name_without_show(self):
simple_tree(self.root)
cwd = os.getcwd()
os.chdir(self.home)
self.addCleanup(os.chdir, cwd)
self.run_cli("--html")
self.assertTrue(os.path.exists("sessions.html"))
def test_memories_follow_the_selection(self):
simple_tree(self.root)
memory_tree(self.root)
_, out, _ = self.run_cli("--json")
everything = json.loads(out)["m"]
_, out, _ = self.run_cli("-p", "/home/u/proj", "--json")
some = json.loads(out)["m"]
self.assertTrue(everything)
self.assertTrue(all(m["p"] == "/home/u/proj" for m in some))
self.assertLess(len(some), len(everything))
def test_html_leaves_stdout_alone(self):
# The summary goes to stderr so `--html /dev/stdout` keeps working.
simple_tree(self.root)
_, out, _ = self.run_cli("--html", os.path.join(self.home, "s.html"))
self.assertEqual(out, "")
class TestReading(CliCase):
def test_show_by_index(self):
simple_tree(self.root)
code, out, _ = self.run_cli("-s", "1", "--no-pager")
self.assertEqual(code, 0)
self.assertIn("why does the build fail?", out)
def test_show_by_uuid_prefix(self):
simple_tree(self.root)
_, out, _ = self.run_cli("-s", "cccccccc", "--no-pager")
self.assertIn("hello", out)
def test_show_honours_the_previous_filter(self):
simple_tree(self.root)
_, out, _ = self.run_cli("-p", "/home/u/other", "-s", "1", "--no-pager")
self.assertIn("hello", out)
def test_missing_reference_exits_with_2(self):
simple_tree(self.root)
code, _, err = self.run_cli("-s", "99")
self.assertEqual(code, 2)
self.assertIn("out of range", err)
def test_resume_prints_the_command(self):
simple_tree(self.root)
code, out, _ = self.run_cli("-r", "1")
self.assertEqual(code, 0)
self.assertEqual(out.strip(),
"cd /home/u/proj && claude --resume "
"aaaaaaaa-0000-0000-0000-000000000001")
class TestDeletion(CliCase):
def paths(self):
return sorted(os.listdir(os.path.join(self.root, "-home-u-proj")))
def test_dry_run_touches_nothing(self):
simple_tree(self.root)
before = self.paths()
code, out, _ = self.run_cli("--delete-empty", "--dry-run")
self.assertEqual(code, 0)
self.assertIn("nothing was touched", out)
self.assertEqual(self.paths(), before)
def test_deletes_the_empty_ones(self):
simple_tree(self.root)
code, out, _ = self.run_cli("--delete-empty", "-y")
self.assertEqual(code, 0)
self.assertIn("1 session deleted", out)
self.assertEqual(self.paths(),
["aaaaaaaa-0000-0000-0000-000000000001.jsonl"])
def test_deletes_a_single_one_by_prefix(self):
simple_tree(self.root)
code, _, _ = self.run_cli("-D", "aaaaaaaa", "-y")
self.assertEqual(code, 0)
self.assertEqual(self.paths(),
["bbbbbbbb-0000-0000-0000-000000000002.jsonl"])
def test_no_repeat_if_requested_twice(self):
simple_tree(self.root)
code, out, _ = self.run_cli("-D", "aaaaaaaa", "1", "-y")
self.assertEqual(code, 0)
self.assertIn("1 session deleted", out)
def test_deleted_one_does_not_come_back_from_cache(self):
simple_tree(self.root)
self.run_cli() # fills the cache
self.run_cli("--delete-empty", "-y")
_, out, _ = self.run_cli()
self.assertIn("2 sessions", out)
self.assertNotIn("bbbbbbbb", out)
def test_filter_limits_what_is_deleted(self):
write_session(
|