"""CLI smoke tests: the same flow exercised by scripts/demo.sh, driven in-process so failures produce useful tracebacks.""" from __future__ import annotations from pathlib import Path from pmp.cli import main from pmp.errors import PMPError def run(argv) -> int: """Invoke the CLI entry point, normalising SystemExit to a return code.""" try: rc = main([str(a) for a in argv]) except SystemExit as exc: rc = exc.code if exc.code is not None else 0 return 0 if rc is None else int(rc) def test_init_succeeds(tmp_path: Path): rc = run(["--node", tmp_path / "node", "init", "--name", "Avery Quinn"]) assert rc == 0 assert (tmp_path / "node").is_dir() def test_full_import_flow(tmp_path: Path, samples_dir: Path, capsys): node = tmp_path / "node" assert run(["--node", node, "init", "--name", "Avery Quinn"]) == 0 assert run(["--node", node, "import", "--adapter", "calendar", samples_dir / "calendar" / "avery-personal.ics"]) == 0 assert run(["--node", node, "import", "--adapter", "notes", samples_dir / "notes"]) == 0 assert run(["--node", node, "import", "--adapter", "photos", samples_dir / "photos" / "avery-photos.json"]) == 0 assert run(["--node", node, "verify"]) == 0 capsys.readouterr() # discard accumulated output assert run(["--node", node, "log"]) == 0 out = capsys.readouterr().out assert out.strip(), "`pmp log` must print the operation log" def test_import_is_idempotent_via_cli(tmp_path: Path, samples_dir: Path, capsys): node = tmp_path / "node" assert run(["--node", node, "init", "--name", "Avery Quinn"]) == 0 assert run(["--node", node, "import", "--adapter", "notes", samples_dir / "notes"]) == 0 capsys.readouterr() assert run(["--node", node, "log"]) == 0 first = capsys.readouterr().out assert run(["--node", node, "import", "--adapter", "notes", samples_dir / "notes"]) == 0 capsys.readouterr() assert run(["--node", node, "log"]) == 0 second = capsys.readouterr().out assert first.count("\n") == second.count("\n"), ( "re-importing identical sources must not grow the log with new evidence" ) def test_unknown_adapter_is_rejected(tmp_path: Path, samples_dir: Path): node = tmp_path / "node" assert run(["--node", node, "init", "--name", "Avery Quinn"]) == 0 try: rc = run(["--node", node, "import", "--adapter", "carrier-pigeon", samples_dir / "notes"]) except PMPError: rc = 1 assert rc != 0 def test_verify_fails_for_missing_node(tmp_path: Path): try: rc = run(["--node", tmp_path / "absent", "verify"]) except (PMPError, FileNotFoundError, OSError): rc = 1 assert rc != 0