"""The local trace-upload CLI (training/share_trace.py). Network-free.""" import json import pytest from server import trace from training import share_trace def _write_trace(tmp_path): t = trace.export_run() # empty-but-valid envelope is fine for these tests p = tmp_path / "trace.json" return trace.write_trace(t, path=str(p)) def test_dry_run_makes_no_network_call(tmp_path, monkeypatch, capsys): path = _write_trace(tmp_path) # If the CLI tried to use the Hub in --dry-run, this would raise. import huggingface_hub def _boom(*a, **k): raise AssertionError("HfApi must not be constructed during --dry-run") monkeypatch.setattr(huggingface_hub, "HfApi", _boom) share_trace.main([path, "--repo", "me/traces", "--dry-run"]) out = capsys.readouterr().out assert "https://huggingface.co/datasets/me/traces" in out assert "dry-run" in out def test_rejects_non_trace_json(tmp_path): bad = tmp_path / "bad.json" bad.write_text(json.dumps({"hello": "world"})) with pytest.raises(SystemExit): share_trace.main([str(bad), "--dry-run"]) def test_public_flag_parses(tmp_path): path = _write_trace(tmp_path) args = share_trace._parse_args([path, "--public"]) assert args.private is False args = share_trace._parse_args([path]) assert args.private is True # private by default