elliot-stack 1.0.17 → 1.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +17 -0
  2. package/bin/install.cjs +322 -43
  3. package/hooks/repo-search-nudge.js +31 -0
  4. package/package.json +3 -2
  5. package/skills/estack-read-claude-session-history/SKILL.md +196 -0
  6. package/skills/estack-read-claude-session-history/references/jsonl-schema.md +126 -0
  7. package/skills/estack-read-claude-session-history/references/modes.md +366 -0
  8. package/skills/estack-read-claude-session-history/references/recipes.md +237 -0
  9. package/skills/estack-read-claude-session-history/scripts/lib/__init__.py +1 -0
  10. package/skills/estack-read-claude-session-history/scripts/lib/parser.py +460 -0
  11. package/skills/estack-read-claude-session-history/scripts/lib/paths.py +234 -0
  12. package/skills/estack-read-claude-session-history/scripts/lib/search.py +179 -0
  13. package/skills/estack-read-claude-session-history/scripts/lib/subagents.py +88 -0
  14. package/skills/estack-read-claude-session-history/scripts/lib/tools.py +144 -0
  15. package/skills/estack-read-claude-session-history/scripts/read_transcript.py +1448 -0
  16. package/skills/estack-read-claude-session-history/scripts/tests/conftest.py +40 -0
  17. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/README.md +20 -0
  18. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/all-noise.jsonl +4 -0
  19. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/basic-session.jsonl +2 -0
  20. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/interrupted.jsonl +2 -0
  21. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/multi-compact.jsonl +8 -0
  22. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/pending-user.jsonl +2 -0
  23. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-no-meta/subagents/agent-aaa.jsonl +2 -0
  24. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-no-meta.jsonl +2 -0
  25. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-parent/subagents/agent-xyz123.jsonl +2 -0
  26. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-parent/subagents/agent-xyz123.meta.json +1 -0
  27. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-parent.jsonl +4 -0
  28. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/time-spread.jsonl +6 -0
  29. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/timeline-day-test.jsonl +5 -0
  30. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/tool-zoo.jsonl +10 -0
  31. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/truncated.jsonl +3 -0
  32. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/unicode.jsonl +2 -0
  33. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/with-advisor.jsonl +3 -0
  34. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/with-compact.jsonl +5 -0
  35. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/with-thinking.jsonl +2 -0
  36. package/skills/estack-read-claude-session-history/scripts/tests/test_backup_roots.py +56 -0
  37. package/skills/estack-read-claude-session-history/scripts/tests/test_json_format.py +201 -0
  38. package/skills/estack-read-claude-session-history/scripts/tests/test_modes.py +199 -0
  39. package/skills/estack-read-claude-session-history/scripts/tests/test_parser.py +195 -0
  40. package/skills/estack-read-claude-session-history/scripts/tests/test_paths.py +133 -0
  41. package/skills/estack-read-claude-session-history/scripts/tests/test_search.py +78 -0
  42. package/skills/estack-read-claude-session-history/scripts/tests/test_subagents.py +43 -0
  43. package/skills/estack-read-claude-session-history/scripts/tests/test_timeline.py +175 -0
  44. package/skills/estack-read-claude-session-history/scripts/tests/test_timezone_and_project.py +212 -0
  45. package/skills/estack-read-claude-session-history/scripts/tests/test_tools.py +80 -0
  46. package/skills/estack-repo-search/SKILL.md +0 -65
@@ -0,0 +1,195 @@
1
+ """Tests for lib.parser."""
2
+
3
+ import json
4
+ from datetime import datetime
5
+ from pathlib import Path
6
+
7
+ import pytest
8
+
9
+ from lib import parser as PR
10
+
11
+
12
+ def _load(fixtures_dir, name):
13
+ return PR.parse_lines(fixtures_dir / name)
14
+
15
+
16
+ def test_parse_basic(fixtures_dir):
17
+ lines = _load(fixtures_dir, "basic-session.jsonl")
18
+ assert len(lines) == 2
19
+ assert lines[0]["type"] == "user"
20
+
21
+
22
+ def test_get_messages_basic(fixtures_dir):
23
+ lines = _load(fixtures_dir, "basic-session.jsonl")
24
+ msgs = PR.get_messages(lines)
25
+ assert len(msgs) == 2
26
+ assert msgs[0]["role"] == "user"
27
+ assert msgs[1]["role"] == "assistant"
28
+
29
+
30
+ def test_compact_marker_single(fixtures_dir):
31
+ lines = _load(fixtures_dir, "with-compact.jsonl")
32
+ msgs = PR.get_messages(lines)
33
+ compact = [m for m in msgs if m["is_compact"]]
34
+ assert len(compact) == 1
35
+
36
+
37
+ def test_compact_marker_multiple(fixtures_dir):
38
+ lines = _load(fixtures_dir, "multi-compact.jsonl")
39
+ msgs = PR.get_messages(lines)
40
+ compact = [m for m in msgs if m["is_compact"]]
41
+ assert len(compact) == 2
42
+
43
+
44
+ def test_compact_marker_absent(fixtures_dir):
45
+ lines = _load(fixtures_dir, "basic-session.jsonl")
46
+ msgs = PR.get_messages(lines)
47
+ assert all(not m["is_compact"] for m in msgs)
48
+
49
+
50
+ def test_extract_text_blocks_string():
51
+ assert PR.extract_text_blocks("hello") == ["hello"]
52
+
53
+
54
+ def test_extract_text_blocks_empty_string():
55
+ assert PR.extract_text_blocks("") == []
56
+ assert PR.extract_text_blocks(" ") == []
57
+
58
+
59
+ def test_extract_text_blocks_array():
60
+ blocks = [{"type": "text", "text": "hi"}, {"type": "tool_use", "name": "X"}]
61
+ assert PR.extract_text_blocks(blocks) == ["hi"]
62
+
63
+
64
+ def test_extract_text_blocks_with_thinking():
65
+ blocks = [
66
+ {"type": "thinking", "thinking": "reasoning..."},
67
+ {"type": "text", "text": "answer"},
68
+ ]
69
+ out = PR.extract_text_blocks(blocks, include_thinking=True)
70
+ assert any("THINKING" in t for t in out)
71
+ assert "answer" in out
72
+
73
+
74
+ def test_extract_text_blocks_with_tool_use():
75
+ blocks = [{"type": "tool_use", "name": "Bash", "input": {"command": "ls"}}]
76
+ out = PR.extract_text_blocks(blocks, include_tool_use=True)
77
+ assert any("TOOL_USE Bash" in t for t in out)
78
+
79
+
80
+ def test_extract_advisor():
81
+ blocks = [{"type": "advisor_tool_result", "content": {"text": "advice"}}]
82
+ out = PR.extract_text_blocks(blocks)
83
+ assert any("[ADVISOR]" in t for t in out)
84
+ assert any("advice" in t for t in out)
85
+
86
+
87
+ def test_classify_entry_user():
88
+ obj = {"type": "user", "message": {"role": "user", "content": "hi"}}
89
+ assert PR.classify_entry(obj) == "user"
90
+
91
+
92
+ def test_classify_entry_compact():
93
+ obj = {
94
+ "type": "user",
95
+ "message": {"role": "user", "content": PR.COMPACT_MARKER + " more"},
96
+ }
97
+ assert PR.classify_entry(obj) == "compact"
98
+
99
+
100
+ def test_classify_entry_assistant():
101
+ obj = {"type": "assistant", "message": {"role": "assistant", "content": []}}
102
+ assert PR.classify_entry(obj) == "assistant"
103
+
104
+
105
+ def test_classify_entry_noise():
106
+ obj = {"type": "ai-title", "aiTitle": "X"}
107
+ assert PR.classify_entry(obj) == "title"
108
+
109
+
110
+ def test_all_noise_fixture_yields_no_messages(fixtures_dir):
111
+ lines = _load(fixtures_dir, "all-noise.jsonl")
112
+ msgs = PR.get_messages(lines)
113
+ assert msgs == []
114
+
115
+
116
+ def test_filter_by_role(fixtures_dir):
117
+ lines = _load(fixtures_dir, "basic-session.jsonl")
118
+ msgs = PR.get_messages(lines)
119
+ user_only = PR.filter_by_role(msgs, "user")
120
+ assert all(m["role"] == "user" for m in user_only)
121
+ assert len(user_only) == 1
122
+
123
+
124
+ def test_filter_by_time(fixtures_dir):
125
+ lines = _load(fixtures_dir, "time-spread.jsonl")
126
+ msgs = PR.get_messages(lines)
127
+ since = datetime(2026, 5, 1)
128
+ filtered = PR.filter_by_time(msgs, since=since, until=None)
129
+ assert all(
130
+ PR._parse_timestamp(m["timestamp"]).replace(tzinfo=None) >= since
131
+ for m in filtered if m["timestamp"]
132
+ )
133
+
134
+
135
+ def test_truncated_line_dropped(fixtures_dir, capsys):
136
+ # Should not raise, and warning should be on stderr
137
+ lines = _load(fixtures_dir, "truncated.jsonl")
138
+ # 2 valid + 1 truncated = 2 valid records
139
+ assert len(lines) == 2
140
+
141
+
142
+ def test_unicode_roundtrip(fixtures_dir):
143
+ lines = _load(fixtures_dir, "unicode.jsonl")
144
+ msgs = PR.get_messages(lines)
145
+ user_text = msgs[0]["texts"][0]
146
+ assert "🌍" in user_text or "你好" in user_text
147
+
148
+
149
+ def test_infer_status_clean(fixtures_dir):
150
+ lines = _load(fixtures_dir, "basic-session.jsonl")
151
+ mtime = (fixtures_dir / "basic-session.jsonl").stat().st_mtime
152
+ status = PR.infer_status(lines, mtime, current_session_id=None, session_uuid=None)
153
+ assert status == "clean"
154
+
155
+
156
+ def test_infer_status_pending_user(fixtures_dir):
157
+ lines = _load(fixtures_dir, "pending-user.jsonl")
158
+ mtime = (fixtures_dir / "pending-user.jsonl").stat().st_mtime
159
+ status = PR.infer_status(lines, mtime, current_session_id=None, session_uuid=None)
160
+ assert status == "pending-user"
161
+
162
+
163
+ def test_infer_status_interrupted(fixtures_dir):
164
+ lines = _load(fixtures_dir, "interrupted.jsonl")
165
+ mtime = (fixtures_dir / "interrupted.jsonl").stat().st_mtime
166
+ status = PR.infer_status(lines, mtime, current_session_id=None, session_uuid=None)
167
+ assert status == "interrupted"
168
+
169
+
170
+ def test_infer_status_active(fixtures_dir):
171
+ import time
172
+ # Touch the fixture to make it fresh, then set CLAUDE_SESSION_ID to its stem
173
+ f = fixtures_dir / "basic-session.jsonl"
174
+ mtime = time.time()
175
+ lines = _load(fixtures_dir, "basic-session.jsonl")
176
+ status = PR.infer_status(
177
+ lines, mtime, current_session_id="basic-session", session_uuid="basic-session"
178
+ )
179
+ assert status == "active"
180
+
181
+
182
+ def test_session_summary_fields(fixtures_dir):
183
+ s = PR.session_summary(fixtures_dir / "tool-zoo.jsonl")
184
+ assert s["exists"]
185
+ assert s["msg_count"] > 0
186
+ assert "Bash" in s["tool_counts"]
187
+ assert "Read" in s["tool_counts"]
188
+
189
+
190
+ def test_parse_lines_cache(fixtures_dir):
191
+ f = fixtures_dir / "basic-session.jsonl"
192
+ a = PR.parse_lines(f)
193
+ b = PR.parse_lines(f)
194
+ # Same object thanks to cache
195
+ assert a is b
@@ -0,0 +1,133 @@
1
+ """Tests for lib.paths."""
2
+
3
+ import os
4
+ from datetime import datetime, timedelta
5
+ from pathlib import Path
6
+
7
+ import pytest
8
+
9
+ from lib import paths as P
10
+
11
+
12
+ def test_encode_cwd_basic():
13
+ assert P.encode_cwd("C:\\Users\\foo\\bar") == "C--Users-foo-bar"
14
+
15
+
16
+ def test_encode_cwd_spaces():
17
+ assert P.encode_cwd("C:\\Users\\2supe\\Other Claude Code") == "C--Users-2supe-Other-Claude-Code"
18
+
19
+
20
+ def test_encode_cwd_mixed_slashes():
21
+ assert P.encode_cwd("C:/Users/foo bar") == "C--Users-foo-bar"
22
+
23
+
24
+ def test_decode_project_name_strips_prefix():
25
+ enc = "C--Users-elliot-Other-Claude-Code-Personal-Brand-Project"
26
+ decoded = P.decode_project_name(enc)
27
+ assert "Other" in decoded
28
+ assert "Personal" in decoded
29
+ assert "C--Users" not in decoded
30
+
31
+
32
+ def test_decode_project_name_fallback_when_unparseable():
33
+ decoded = P.decode_project_name("totally-weird-thing")
34
+ assert decoded # Non-empty, falls back gracefully
35
+
36
+
37
+ def test_resolve_root_live():
38
+ assert P.resolve_root("live").name == "projects"
39
+
40
+
41
+ def test_resolve_root_mirror():
42
+ r = P.resolve_root("mirror")
43
+ assert "mirror" in str(r)
44
+ assert r.name == "projects"
45
+
46
+
47
+ def test_resolve_root_unknown_relative_raises():
48
+ with pytest.raises(ValueError):
49
+ P.resolve_root("not-a-known-root")
50
+
51
+
52
+ def test_resolve_root_absolute_passes_through(tmp_path: Path):
53
+ r = P.resolve_root(str(tmp_path))
54
+ assert r == tmp_path
55
+
56
+
57
+ def test_current_session_id_unset(monkeypatch):
58
+ monkeypatch.delenv("CLAUDE_SESSION_ID", raising=False)
59
+ assert P.current_session_id() is None
60
+
61
+
62
+ def test_current_session_id_set(monkeypatch):
63
+ monkeypatch.setenv("CLAUDE_SESSION_ID", "abc-123")
64
+ assert P.current_session_id() == "abc-123"
65
+
66
+
67
+ def test_parse_timespec_relative():
68
+ now = datetime.now()
69
+ delta = now - P.parse_timespec("1h")
70
+ assert timedelta(seconds=3590) <= delta <= timedelta(seconds=3610)
71
+
72
+
73
+ def test_parse_timespec_iso_date():
74
+ assert P.parse_timespec("2026-05-01") == datetime(2026, 5, 1)
75
+
76
+
77
+ def test_parse_timespec_yesterday():
78
+ y = P.parse_timespec("yesterday")
79
+ assert y.hour == 0
80
+ assert y.minute == 0
81
+
82
+
83
+ def test_parse_timespec_invalid():
84
+ with pytest.raises(ValueError):
85
+ P.parse_timespec("not-a-time")
86
+
87
+
88
+ def test_list_transcripts_empty(tmp_path: Path):
89
+ assert P.list_transcripts(tmp_path) == []
90
+
91
+
92
+ def test_list_transcripts_excludes_agent_prefix(tmp_path: Path):
93
+ (tmp_path / "real.jsonl").write_text("{}\n")
94
+ (tmp_path / "agent-foo.jsonl").write_text("{}\n")
95
+ files = P.list_transcripts(tmp_path)
96
+ names = [f.name for f in files]
97
+ assert "real.jsonl" in names
98
+ assert "agent-foo.jsonl" not in names
99
+
100
+
101
+ def test_list_transcripts_time_filter(tmp_path: Path):
102
+ old = tmp_path / "old.jsonl"
103
+ new = tmp_path / "new.jsonl"
104
+ old.write_text("{}\n")
105
+ new.write_text("{}\n")
106
+ # Backdate old
107
+ past = datetime.now() - timedelta(days=10)
108
+ os.utime(old, (past.timestamp(), past.timestamp()))
109
+
110
+ since = datetime.now() - timedelta(days=5)
111
+ files = P.list_transcripts(tmp_path, since=since)
112
+ assert [f.name for f in files] == ["new.jsonl"]
113
+
114
+
115
+ def test_list_projects_empty(tmp_path: Path):
116
+ assert P.list_projects(tmp_path) == []
117
+
118
+
119
+ def test_list_subagents_returns_empty_when_no_dir(tmp_path: Path):
120
+ f = tmp_path / "session.jsonl"
121
+ f.write_text("{}\n")
122
+ assert P.list_subagents(f) == []
123
+
124
+
125
+ def test_list_subagents_finds_agent_files(tmp_path: Path):
126
+ f = tmp_path / "session.jsonl"
127
+ f.write_text("{}\n")
128
+ sub_dir = tmp_path / "session" / "subagents"
129
+ sub_dir.mkdir(parents=True)
130
+ (sub_dir / "agent-x.jsonl").write_text("{}\n")
131
+ (sub_dir / "agent-y.jsonl").write_text("{}\n")
132
+ subs = P.list_subagents(f)
133
+ assert len(subs) == 2
@@ -0,0 +1,78 @@
1
+ """Tests for lib.search."""
2
+
3
+ from datetime import datetime
4
+ from pathlib import Path
5
+
6
+ from lib import search as S
7
+
8
+
9
+ def test_search_session_text(fixtures_dir):
10
+ matches = S.search_session(
11
+ fixtures_dir / "basic-session.jsonl", "Hello", role="both", in_channel="text"
12
+ )
13
+ assert len(matches) >= 1
14
+
15
+
16
+ def test_search_session_assistant_only(fixtures_dir):
17
+ matches = S.search_session(
18
+ fixtures_dir / "basic-session.jsonl", "Hello",
19
+ role="assistant", in_channel="text",
20
+ )
21
+ # "Hello" is in the user message only
22
+ assert len(matches) == 0
23
+
24
+
25
+ def test_search_session_user_only(fixtures_dir):
26
+ matches = S.search_session(
27
+ fixtures_dir / "basic-session.jsonl", "Hello",
28
+ role="user", in_channel="text",
29
+ )
30
+ assert len(matches) >= 1
31
+
32
+
33
+ def test_search_in_tool_use(fixtures_dir):
34
+ matches = S.search_session(
35
+ fixtures_dir / "tool-zoo.jsonl", "ls -la",
36
+ role="both", in_channel="tool_use",
37
+ )
38
+ assert len(matches) >= 1
39
+ assert matches[0].where == "tool_use"
40
+
41
+
42
+ def test_search_in_thinking(fixtures_dir):
43
+ matches = S.search_session(
44
+ fixtures_dir / "with-thinking.jsonl", "step by step",
45
+ role="both", in_channel="thinking",
46
+ )
47
+ assert len(matches) >= 1
48
+ assert matches[0].where == "thinking"
49
+
50
+
51
+ def test_search_no_match(fixtures_dir):
52
+ matches = S.search_session(
53
+ fixtures_dir / "basic-session.jsonl", "this-string-not-present",
54
+ role="both", in_channel="text",
55
+ )
56
+ assert matches == []
57
+
58
+
59
+ def test_search_with_time_filter(fixtures_dir):
60
+ # "Hello" appears in basic-session.jsonl at 2026-05-01T10:00:00Z
61
+ # Excluding that date should yield no matches
62
+ matches = S.search_session(
63
+ fixtures_dir / "basic-session.jsonl", "Hello",
64
+ role="both", in_channel="text",
65
+ since=datetime(2026, 6, 1),
66
+ )
67
+ assert matches == []
68
+
69
+
70
+ def test_search_project(fixtures_dir, tmp_path):
71
+ # Copy 2 fixtures into a fake project dir and search
72
+ import shutil
73
+ pd = tmp_path / "fake-proj"
74
+ pd.mkdir()
75
+ shutil.copy(fixtures_dir / "basic-session.jsonl", pd / "session-a.jsonl")
76
+ shutil.copy(fixtures_dir / "tool-zoo.jsonl", pd / "session-b.jsonl")
77
+ matches = list(S.search_project(pd, "Hello", progress=False))
78
+ assert len(matches) >= 1
@@ -0,0 +1,43 @@
1
+ """Tests for lib.subagents."""
2
+
3
+ from pathlib import Path
4
+
5
+ from lib import subagents as SA
6
+ from lib import paths as P
7
+
8
+
9
+ def test_load_meta_hit(fixtures_dir):
10
+ agent_file = fixtures_dir / "subagent-parent" / "subagents" / "agent-xyz123.jsonl"
11
+ meta = SA.load_meta(agent_file)
12
+ assert meta["agentType"] == "Explore"
13
+ assert "bug" in meta["description"].lower()
14
+
15
+
16
+ def test_load_meta_miss(fixtures_dir):
17
+ agent_file = fixtures_dir / "subagent-no-meta" / "subagents" / "agent-aaa.jsonl"
18
+ meta = SA.load_meta(agent_file)
19
+ assert meta["agentType"] == "unknown"
20
+ assert meta["description"] == ""
21
+
22
+
23
+ def test_agent_finals(fixtures_dir):
24
+ parent = fixtures_dir / "subagent-parent.jsonl"
25
+ finals = SA.agent_finals(parent)
26
+ assert len(finals) == 1
27
+ agent_id, meta, text = finals[0]
28
+ assert agent_id == "agent-xyz123"
29
+ assert meta["agentType"] == "Explore"
30
+ assert "Found it" in text
31
+
32
+
33
+ def test_agent_finals_no_subagents(fixtures_dir):
34
+ parent = fixtures_dir / "basic-session.jsonl"
35
+ finals = SA.agent_finals(parent)
36
+ assert finals == []
37
+
38
+
39
+ def test_list_subagents(fixtures_dir):
40
+ parent = fixtures_dir / "subagent-parent.jsonl"
41
+ subs = P.list_subagents(parent)
42
+ assert len(subs) == 1
43
+ assert subs[0].stem == "agent-xyz123"
@@ -0,0 +1,175 @@
1
+ """Tests for the timeline mode (build/render/gap parsing + CLI)."""
2
+
3
+ import json
4
+ import os
5
+ import shutil
6
+ import subprocess
7
+ import sys
8
+ from datetime import datetime, timedelta
9
+ from pathlib import Path
10
+
11
+ import pytest
12
+
13
+ import read_transcript as RT
14
+
15
+
16
+ def _run_cli(cli_path, *args, env_overrides=None):
17
+ env = dict(os.environ)
18
+ env["PYTHONIOENCODING"] = "utf-8"
19
+ if env_overrides:
20
+ env.update(env_overrides)
21
+ return subprocess.run(
22
+ [sys.executable, str(cli_path), *args],
23
+ capture_output=True,
24
+ text=True,
25
+ encoding="utf-8",
26
+ env=env,
27
+ )
28
+
29
+
30
+ @pytest.fixture
31
+ def fake_root(fixtures_dir, tmp_path):
32
+ root = tmp_path / "projects"
33
+ proj = root / "C--fake-proj"
34
+ proj.mkdir(parents=True)
35
+ shutil.copy(fixtures_dir / "timeline-day-test.jsonl", proj / "abc12345.jsonl")
36
+ return root
37
+
38
+
39
+ # ── unit: gap + duration helpers ─────────────────────────────────────────────
40
+
41
+ def test_parse_gap_default():
42
+ assert RT._parse_gap(None) == 15
43
+
44
+
45
+ def test_parse_gap_minutes():
46
+ assert RT._parse_gap("20m") == 20
47
+ assert RT._parse_gap("20") == 20
48
+
49
+
50
+ def test_parse_gap_hours():
51
+ assert RT._parse_gap("1h") == 60
52
+
53
+
54
+ def test_parse_gap_invalid():
55
+ with pytest.raises(ValueError):
56
+ RT._parse_gap("soon")
57
+
58
+
59
+ def test_fmt_dur():
60
+ assert RT._fmt_dur(timedelta(minutes=8)) == "8m"
61
+ assert RT._fmt_dur(timedelta(minutes=72)) == "1h12m"
62
+ assert RT._fmt_dur(timedelta(seconds=30)) == "<1m"
63
+
64
+
65
+ # ── unit: block grouping ─────────────────────────────────────────────────────
66
+
67
+ def test_build_timeline_blocks(fake_root):
68
+ from lib import parser as PR
69
+ PR.set_timezone("UTC")
70
+ try:
71
+ data = RT.build_timeline(
72
+ [fake_root / "C--fake-proj"],
73
+ since=datetime(2026, 5, 1),
74
+ until=datetime(2026, 5, 2),
75
+ gap_minutes=15,
76
+ current_uuid=None,
77
+ )
78
+ finally:
79
+ PR.set_timezone(None)
80
+ blocks = data["blocks"]
81
+ assert len(blocks) == 2
82
+ assert blocks[0]["start"] == datetime(2026, 5, 1, 10, 0)
83
+ assert blocks[0]["end"] == datetime(2026, 5, 1, 10, 8)
84
+ assert blocks[1]["start"] == datetime(2026, 5, 1, 12, 0)
85
+ assert blocks[1]["end"] == datetime(2026, 5, 1, 12, 2)
86
+
87
+
88
+ def test_build_timeline_wide_gap_merges(fake_root):
89
+ from lib import parser as PR
90
+ PR.set_timezone("UTC")
91
+ try:
92
+ data = RT.build_timeline(
93
+ [fake_root / "C--fake-proj"],
94
+ since=datetime(2026, 5, 1),
95
+ until=datetime(2026, 5, 2),
96
+ gap_minutes=180, # 3h gap threshold swallows the 1h52m idle
97
+ current_uuid=None,
98
+ )
99
+ finally:
100
+ PR.set_timezone(None)
101
+ assert len(data["blocks"]) == 1
102
+
103
+
104
+ # ── CLI ──────────────────────────────────────────────────────────────────────
105
+
106
+ def test_timeline_cli_text(cli_path, fake_root):
107
+ r = _run_cli(
108
+ cli_path, "--root", str(fake_root), "--tz", "UTC",
109
+ "--mode", "timeline", "--date", "2026-05-01",
110
+ )
111
+ assert r.returncode == 0
112
+ assert "10:00" in r.stdout
113
+ assert "12:02" in r.stdout
114
+ assert "idle" in r.stdout
115
+ assert "2 active block(s)" in r.stdout
116
+
117
+
118
+ def test_timeline_cli_json(cli_path, fake_root):
119
+ r = _run_cli(
120
+ cli_path, "--root", str(fake_root), "--tz", "UTC",
121
+ "--mode", "timeline", "--date", "2026-05-01", "--format", "json",
122
+ )
123
+ assert r.returncode == 0
124
+ data = json.loads(r.stdout)
125
+ assert data["totals"]["blocks"] == 2
126
+ assert data["totals"]["sessions"] == 1
127
+ assert data["blocks"][0]["start"].endswith("10:00:00")
128
+ assert data["blocks"][0]["sessions"][0]["uuid"] == "abc12345"
129
+
130
+
131
+ def test_timeline_cli_empty_range(cli_path, fake_root):
132
+ r = _run_cli(
133
+ cli_path, "--root", str(fake_root), "--tz", "UTC",
134
+ "--mode", "timeline", "--date", "2020-01-01",
135
+ )
136
+ assert r.returncode == 0
137
+ assert "no activity" in r.stdout
138
+
139
+
140
+ def test_timeline_cli_project_filter(cli_path, fake_root):
141
+ r = _run_cli(
142
+ cli_path, "--root", str(fake_root), "--tz", "UTC",
143
+ "--mode", "timeline", "--date", "2026-05-01", "--project", "fake",
144
+ )
145
+ assert r.returncode == 0
146
+ assert "10:00" in r.stdout
147
+
148
+
149
+ def test_timeline_cli_exclude_current(cli_path, fake_root):
150
+ r = _run_cli(
151
+ cli_path, "--root", str(fake_root), "--tz", "UTC",
152
+ "--mode", "timeline", "--date", "2026-05-01", "--exclude-current",
153
+ env_overrides={"CLAUDE_SESSION_ID": "abc12345"},
154
+ )
155
+ assert r.returncode == 0
156
+ assert "no activity" in r.stdout
157
+
158
+
159
+ def test_journal_cli_exclude_current(cli_path, fake_root):
160
+ r = _run_cli(
161
+ cli_path, "--root", str(fake_root),
162
+ "--mode", "journal", "--since", "2020-01-01", "--all-projects",
163
+ "--exclude-current",
164
+ env_overrides={"CLAUDE_SESSION_ID": "abc12345"},
165
+ )
166
+ assert r.returncode == 0
167
+ assert "abc12345" not in r.stdout
168
+
169
+
170
+ def test_timeline_cli_project_no_match(cli_path, fake_root):
171
+ r = _run_cli(
172
+ cli_path, "--root", str(fake_root), "--tz", "UTC",
173
+ "--mode", "timeline", "--date", "2026-05-01", "--project", "zzz-nope",
174
+ )
175
+ assert r.returncode == 1