import json from tamq.cli import main from tamq.store import Store def test_replay_reports_batch(tmp_path, monkeypatch, capsys): source = tmp_path / "messages.jsonl" source.write_text(json.dumps({"message_id": "old-1", "sender_repo": "a", "target_repo": "b", "body": "hello"}) + "\n") monkeypatch.setenv("TAMQ_STATE_DIR", str(tmp_path / "state")) assert main(["replay", str(source)]) == 0 output = json.loads(capsys.readouterr().out) assert output["batch_id"].startswith("replay-") assert output["count"] == 1 def test_replay_preserves_endpoint(tmp_path, monkeypatch, capsys): source = tmp_path / "messages.jsonl" source.write_text(json.dumps({"sender_repo": "a", "target_repo": "b", "body": "hello", "endpoint_id": "tmux-amq-42"}) + "\n") monkeypatch.setenv("TAMQ_STATE_DIR", str(tmp_path / "state")) assert main(["replay", str(source)]) == 0 row = Store(tmp_path / "state" / "tamq.sqlite3").list()[0] assert row["endpoint_id"] == "tmux-amq-42" def test_replay_rejects_bad_json(tmp_path, monkeypatch, capsys): source = tmp_path / "bad.jsonl" source.write_text("not-json\n") monkeypatch.setenv("TAMQ_STATE_DIR", str(tmp_path / "state")) assert main(["replay", str(source)]) == 2 assert "cannot replay" in capsys.readouterr().err