-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cli_dispatch.py
More file actions
307 lines (266 loc) · 9.36 KB
/
Copy pathtest_cli_dispatch.py
File metadata and controls
307 lines (266 loc) · 9.36 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
import json
import os
import sqlite3
import subprocess
from pathlib import Path
ROOT = Path(__file__).parent
CLI = ROOT / "ai-hist"
RUST_CLI = ROOT / "ai-hist-rust"
def run_cli(args, env, check=False):
result = subprocess.run(
[str(CLI), *args],
cwd=ROOT,
env=env,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if check:
assert result.returncode == 0, result.stderr
return result
def isolated_env(tmp_path):
home = tmp_path / "home"
home.mkdir()
db = tmp_path / "ai-history.db"
env = os.environ.copy()
env.update(
{
"HOME": str(home),
"AI_HIST_DB": str(db),
"OPENCODE_DB": str(tmp_path / "missing-opencode.db"),
"TRAJECTORY_ROOT": str(tmp_path / "trajectories"),
}
)
return env, db, home
def write_history(home):
claude = home / ".claude"
codex = home / ".codex"
claude.mkdir()
codex.mkdir()
claude.joinpath("history.jsonl").write_text(
json.dumps(
{
"display": "dispatch unique claude prompt",
"timestamp": 1700000000000,
"project": "/tmp/dispatch/project",
"sessionId": "claude-dispatch",
}
)
+ "\n"
)
codex.joinpath("history.jsonl").write_text(
json.dumps(
{
"text": "dispatch unique codex prompt",
"ts": 1700000001,
"session_id": "codex-dispatch",
}
)
+ "\n"
)
def seed_via_wrapper(tmp_path):
env, db, home = isolated_env(tmp_path)
write_history(home)
result = run_cli(["sync"], env)
assert result.returncode == 0, result.stderr
assert "using deprecated Python fallback" not in result.stderr
assert "Total:" in result.stdout
return env, db
def ensure_rust_binary():
metadata = subprocess.run(
["cargo", "metadata", "--format-version", "1", "--no-deps"],
cwd=ROOT,
check=True,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
target_dir = Path(json.loads(metadata.stdout)["target_directory"])
built = target_dir / "debug" / ("ai-hist.exe" if os.name == "nt" else "ai-hist")
if not built.exists():
subprocess.run(["cargo", "build", "-q", "-p", "ai-hist-cli"], cwd=ROOT, check=True)
return built
def test_sync_routes_to_rust_and_preserves_db(tmp_path):
env, db = seed_via_wrapper(tmp_path)
conn = sqlite3.connect(db)
rows = conn.execute(
"SELECT source, session_id, prompt FROM history ORDER BY source"
).fetchall()
conn.close()
assert rows == [
("claude", "claude-dispatch", "dispatch unique claude prompt"),
("codex", "codex-dispatch", "dispatch unique codex prompt"),
]
def test_search_json_routes_to_rust_with_python_compatible_shape(tmp_path):
env, _db = seed_via_wrapper(tmp_path)
result = run_cli(["search", "dispatch", "--json"], env)
assert result.returncode == 0, result.stderr
assert "deprecated Python fallback" not in result.stderr
rows = json.loads(result.stdout)
assert len(rows) == 2
assert set(rows[0]) == {
"id",
"source",
"session_id",
"project",
"prompt",
"timestamp_ms",
}
def test_search_tag_without_query_routes_to_rust(tmp_path):
env, _db = seed_via_wrapper(tmp_path)
tag = run_cli(["tag", "claude-dispatch", "relayfile-migration", "--source", "claude"], env)
assert tag.returncode == 0, tag.stderr
result = run_cli(["search", "--tag", "relayfile-migration", "--json"], env)
assert result.returncode == 0, result.stderr
assert "deprecated Python fallback" not in result.stderr
rows = json.loads(result.stdout)
assert [row["session_id"] for row in rows] == ["claude-dispatch"]
def test_session_full_routes_to_rust(tmp_path):
env, _db = seed_via_wrapper(tmp_path)
result = run_cli(["session", "claude-dispatch", "--full"], env)
assert result.returncode == 0, result.stderr
assert "using deprecated Python fallback" not in result.stderr
assert "dispatch unique claude prompt" in result.stdout
def test_session_missing_json_has_no_extra_stderr(tmp_path):
env, _db = seed_via_wrapper(tmp_path)
result = run_cli(["session", "missing-session", "--json"], env)
assert result.returncode == 1
assert result.stdout == "[]\n"
assert result.stderr == ""
def test_previously_fallback_commands_route_to_rust(tmp_path):
env, _db = seed_via_wrapper(tmp_path)
export_path = tmp_path / "export.jsonl"
for args in (
["stats", "--json"],
["tag", "claude-dispatch", "release", "--source", "claude", "--json"],
["resume", "dispatch", "--json"],
["tags", "--sessions"],
["show", "1", "--json"],
["context", "1"],
["pack", "dispatch", "--json"],
):
result = run_cli(args, env)
assert result.returncode == 0, (args, result.stderr)
assert "using deprecated Python fallback" not in result.stderr
export_result = run_cli(["export", str(export_path), "--source", "claude"], env)
assert export_result.returncode == 0, export_result.stderr
assert export_path.exists()
import_result = run_cli(["import", str(export_path), "--dry-run"], env)
assert import_result.returncode == 0, import_result.stderr
assert "dry-run" in import_result.stdout
def test_escape_hatches_force_python_or_rust(tmp_path):
env, _db = seed_via_wrapper(tmp_path)
python_env = env | {"AI_HIST_CLI": "python"}
result = run_cli(["search", "dispatch", "--json"], python_env)
assert result.returncode == 0, result.stderr
assert "deprecated Python fallback" not in result.stderr
rust_env = env | {"AI_HIST_CLI": "rust"}
result = run_cli(["stats"], rust_env)
assert result.returncode == 0, result.stderr
assert "Total entries:" in result.stdout
def test_explicit_rust_binary_escape_hatch(tmp_path):
env, _db = seed_via_wrapper(tmp_path)
built = ensure_rust_binary()
result = run_cli(
["search", "dispatch", "--json"],
env | {"AI_HIST_RUST_BIN": str(built)},
)
assert result.returncode == 0, result.stderr
assert len(json.loads(result.stdout)) == 2
def test_top_level_help_lists_rust_commands(tmp_path):
env, _db, _home = isolated_env(tmp_path)
result = run_cli(["--help"], env)
assert result.returncode == 0
assert "Rust-default commands:" in result.stdout
assert "search" in result.stdout
assert "sync" in result.stdout
assert "show" in result.stdout
assert "login" in result.stdout
assert "coverage" in result.stdout
assert "pair" in result.stdout
assert "learn" in result.stdout
assert "DISPATCH_MATRIX.md" not in result.stdout
assert "Python fallback" not in result.stdout
def test_rust_binary_help_uses_public_name_and_describes_commands(tmp_path):
env, _db, _home = isolated_env(tmp_path)
built = ensure_rust_binary()
result = subprocess.run(
[str(built), "--help"],
cwd=ROOT,
env=env,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert result.returncode == 0, result.stderr
assert "Usage: ai-hist [OPTIONS] <COMMAND>" in result.stdout
assert "ai-hist-rust-bin" not in result.stdout
assert "Search prompts and sessions" in result.stdout
assert "Authenticate to relayhistory-cloud" in result.stdout
def test_all_rust_command_help_forms_work(tmp_path):
env, _db, _home = isolated_env(tmp_path)
built = ensure_rust_binary()
commands = [
["search"],
["recent"],
["session"],
["show"],
["context"],
["stats"],
["tag"],
["untag"],
["tags"],
["resume"],
["sync-opencode"],
["sync"],
["watch"],
["pack"],
["export"],
["import"],
["setup"],
["setup", "git"],
["link"],
["link", "commit"],
["login"],
["admin-mint"],
["push"],
["coverage"],
["pair"],
["pair", "check"],
["learn"],
["learn", "distill"],
]
for command in commands:
result = subprocess.run(
[str(built), *command, "--help"],
cwd=ROOT,
env=env,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert result.returncode == 0, (command, result.stderr)
assert "ai-hist-rust-bin" not in result.stdout
assert "Usage: ai-hist" in result.stdout
def test_rust_default_validates_source_choices(tmp_path):
env, _db = seed_via_wrapper(tmp_path)
result = run_cli(["search", "dispatch", "--source", "not-a-source"], env)
assert result.returncode != 0
assert "invalid source" in result.stderr
def test_rust_default_uses_xdg_data_home_when_ai_hist_db_is_unset(tmp_path):
env = os.environ.copy()
home = tmp_path / "home"
xdg = tmp_path / "xdg"
home.mkdir()
env.update({"HOME": str(home), "XDG_DATA_HOME": str(xdg)})
env.pop("AI_HIST_DB", None)
result = subprocess.run(
[str(RUST_CLI), "recent", "--json"],
cwd=ROOT,
env=env,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert result.returncode == 0, result.stderr
assert (xdg / "ai-hist" / "ai-history.db").exists()