返回 last30days-skill
test_transcript_harvest.py
根目录 / tests / test_transcript_harvest.py
1 """Unit and CLI subprocess tests for offline `library harvest` transcript backfill."""
2
3 from __future__ import annotations
4
5 import json
6 import os
7 import stat
8 import subprocess
9 import sys
10 from datetime import date
11 from pathlib import Path
12
13 from lib import transcript_harvest
14
15
16 REPO_ROOT = Path(__file__).resolve().parents[1]
17 CLI_SCRIPT = REPO_ROOT / "skills" / "last30days" / "scripts" / "last30days.py"
18
19 BRIEF_ANTHROPIC = """🌐 last30days · Anthropic · Oct 22 to Nov 21, 2026 · v4.5.0
20
21 ## What I learned
22
23 Anthropic developer threads focus on tool use and reliability.
24
25 ```text
26 ---
27 ✅ All agents reported back!
28 ├─ 🟠 Reddit: 12 threads
29 └─ 🗓️ Oct 22 to Nov 21, 2026
30 ---
31 ```
32
33 ---
34 I'm now an expert on Anthropic. Just ask.
35 """
36
37 BRIEF_LEGACY_NVIDIA = """🌐 last30days v3.32.0 · synced 2026-11-09
38
39 # Nvidia
40
41 Blackwell racks dominate infrastructure threads.
42
43 ```text
44 ---
45 ✅ All agents reported back!
46 ├─ 🟠 Reddit: 8 threads
47 └─ 🗓️ Oct 10 to Nov 9, 2026
48 ---
49 ```
50 """
51
52 BRIEF_CODEX_STRIPE = """🌐 last30days · Stripe · Oct 20 to Nov 19, 2026 · v4.5.0
53
54 ## What I learned
55
56 Developers debate Stripe Billing migration ergonomics.
57
58 ```text
59 ---
60 ✅ All agents reported back!
61 ├─ 🟠 Reddit: 9 threads
62 └─ 🗓️ Oct 20 to Nov 19, 2026
63 ---
64 ```
65 """
66
67 BRIEF_AG_CURSOR = """🌐 last30days · Cursor · Oct 21 to Nov 20, 2026 · v4.5.0
68
69 ## What I learned
70
71 Cursor teams share hook workflows for local lint and logging.
72
73 ```text
74 ---
75 ✅ All agents reported back!
76 ├─ 🟠 Reddit: 7 threads
77 └─ 🗓️ Oct 21 to Nov 20, 2026
78 ---
79 ```
80 """
81
82 BRIEF_GEMINI_CORPUS = """🌐 last30days · Internal Launch · Oct 22 to Nov 21, 2026 · v4.5.0
83
84 ## What I learned
85
86 Public market chatter is quiet.
87
88 ## From your files
89
90 Private Q4 roadmap doc mentions codename Orion.
91
92 ```text
93 ---
94 ✅ All agents reported back!
95 ├─ 🟠 Reddit: 2 threads
96 ├─ 📁 Your files: 1 note
97 └─ 🗓️ Oct 22 to Nov 21, 2026
98 ---
99 ```
100 """
101
102
103 def _seed_transcripts(home: Path) -> None:
104 # Claude Code JSONL (modern + legacy + corrupt line + non-brief line)
105 claude_dir = home / ".claude" / "projects" / "demo-project"
106 claude_dir.mkdir(parents=True)
107 (claude_dir / "session-1.jsonl").write_text(
108 "\n".join(
109 [
110 "{broken json line",
111 json.dumps(
112 {
113 "type": "assistant",
114 "timestamp": "2026-11-21T10:00:00Z",
115 "message": {
116 "role": "assistant",
117 "content": [{"type": "text", "text": BRIEF_ANTHROPIC}],
118 },
119 }
120 ),
121 json.dumps(
122 {
123 "type": "assistant",
124 "timestamp": "2026-11-09T10:00:00Z",
125 "message": {
126 "role": "assistant",
127 "content": [{"type": "text", "text": BRIEF_LEGACY_NVIDIA}],
128 },
129 }
130 ),
131 json.dumps(
132 {
133 "type": "assistant",
134 "message": {"role": "assistant", "content": "Unrelated chat reply."},
135 }
136 ),
137 ]
138 )
139 + "\n",
140 encoding="utf-8",
141 )
142
143 # Codex CLI rollout JSONL
144 codex_dir = home / ".codex" / "sessions" / "2026" / "11" / "19"
145 codex_dir.mkdir(parents=True)
146 (codex_dir / "rollout-1.jsonl").write_text(
147 json.dumps(
148 {
149 "type": "response_item",
150 "timestamp": "2026-11-19T12:00:00Z",
151 "payload": {
152 "type": "message",
153 "role": "assistant",
154 "content": [{"type": "output_text", "text": BRIEF_CODEX_STRIPE}],
155 },
156 }
157 )
158 + "\n",
159 encoding="utf-8",
160 )
161
162 # Antigravity transcript_full.jsonl (preferred over truncated sibling transcript.jsonl)
163 ag_dir = (
164 home
165 / ".t3"
166 / "userdata"
167 / "providers"
168 / "antigravity"
169 / "inst1"
170 / "brain"
171 / "conv1"
172 / ".system_generated"
173 / "logs"
174 )
175 ag_dir.mkdir(parents=True)
176 (ag_dir / "transcript.jsonl").write_text(
177 json.dumps(
178 {
179 "type": "PLANNER_RESPONSE",
180 "content": BRIEF_AG_CURSOR[:80],
181 "truncated_fields": ["content"],
182 }
183 )
184 + "\n",
185 encoding="utf-8",
186 )
187 (ag_dir / "transcript_full.jsonl").write_text(
188 json.dumps({"type": "PLANNER_RESPONSE", "content": BRIEF_AG_CURSOR}) + "\n",
189 encoding="utf-8",
190 )
191
192 # Gemini CLI session JSON (with private corpus section)
193 gem_dir = home / ".gemini" / "tmp" / "proj1" / "chats"
194 gem_dir.mkdir(parents=True)
195 (gem_dir / "session-2026-11-21.json").write_text(
196 json.dumps(
197 {
198 "messages": [
199 {"type": "user", "content": "/last30days Internal Launch"},
200 {"type": "gemini", "content": BRIEF_GEMINI_CORPUS},
201 ]
202 }
203 ),
204 encoding="utf-8",
205 )
206
207
208 def test_harvest_extracts_all_harnesses_and_is_idempotent(tmp_path):
209 _seed_transcripts(tmp_path)
210 save_dir = tmp_path / "memory"
211
212 first = transcript_harvest.harvest_transcripts(save_dir=save_dir, home=tmp_path)
213 assert first.saved == 5
214 assert first.duplicates == 0
215 finished = save_dir / "finished"
216 names = sorted(p.name for p in finished.glob("*.md"))
217 assert names == [
218 "anthropic-2026-11-21.md",
219 "cursor-2026-11-20.md",
220 "internal-launch-2026-11-21.md",
221 "nvidia-2026-11-09.md",
222 "stripe-2026-11-19.md",
223 ]
224 if os.name == "posix":
225 assert stat.S_IMODE((finished / "internal-launch-2026-11-21.md").stat().st_mode) == 0o600
226
227 second = transcript_harvest.harvest_transcripts(save_dir=save_dir, home=tmp_path)
228 assert second.saved == 0
229 assert second.duplicates == 5
230
231
232 def test_harvest_dry_run_since_and_source_filtering(tmp_path):
233 _seed_transcripts(tmp_path)
234 save_dir = tmp_path / "memory"
235
236 dry = transcript_harvest.harvest_transcripts(
237 save_dir=save_dir,
238 source="claude",
239 since=date(2026, 11, 15),
240 dry_run=True,
241 home=tmp_path,
242 )
243 assert dry.dry_run is True
244 assert dry.saved == 1
245 assert dry.duplicates == 0
246 assert not save_dir.exists()
247
248
249 def test_cli_library_harvest_end_to_end_and_flag_guards(tmp_path):
250 _seed_transcripts(tmp_path)
251 save_dir = tmp_path / "memory"
252 env = os.environ.copy()
253 env["HOME"] = str(tmp_path)
254 env["LAST30DAYS_MEMORY_DIR"] = str(save_dir)
255
256 res = subprocess.run(
257 [
258 sys.executable,
259 str(CLI_SCRIPT),
260 "library",
261 "harvest",
262 "--source",
263 "claude",
264 "--save-dir",
265 str(save_dir),
266 ],
267 cwd=REPO_ROOT,
268 text=True,
269 capture_output=True,
270 env=env,
271 timeout=15,
272 check=False,
273 )
274 assert res.returncode == 0, res.stderr
275 assert "Saved: 2" in res.stdout
276 assert (save_dir / "finished" / "anthropic-2026-11-21.md").is_file()
277 assert (save_dir / "finished" / "nvidia-2026-11-09.md").is_file()
278
279 # Orphaned --dry-run outside library harvest must fail loudly with exit 2.
280 bad = subprocess.run(
281 [sys.executable, str(CLI_SCRIPT), "AI agents", "--dry-run", "--mock"],
282 cwd=REPO_ROOT,
283 text=True,
284 capture_output=True,
285 env=env,
286 timeout=15,
287 check=False,
288 )
289 assert bad.returncode == 2
290 assert "library harvest" in bad.stderr
291
292 # Malformed --since exits 2.
293 bad_since = subprocess.run(
294 [sys.executable, str(CLI_SCRIPT), "library", "harvest", "--since", "not-a-date"],
295 cwd=REPO_ROOT,
296 text=True,
297 capture_output=True,
298 env=env,
299 timeout=15,
300 check=False,
301 )
302 assert bad_since.returncode == 2
303
303 lines PYTHON