import json import subprocess import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from sky_trajectories import ConversionError, TrajectoryRecord, find_leaks, fingerprint_directory, from_sky_raw # noqa: E402 from sky_trajectories.stats import summarize, wilson # noqa: E402 HIDDEN = "def test_secret_edge_case():\n assert parse_markdown('# **b**') == '

b

'\n" def raw(i=0, accepted=True, code="def f():\n return 1\n"): return { "run_id": "r1", "task_id": f"t{i}", "task": "Écris f.", "accepted": accepted, "judge": "deepseek", "created_at": 1_700_000_000, "steps": [{"role": "coder", "model": "qwen", "prompt": "p", "output": "o", "files": [{"path": "a.py", "content": code}]}], "final_files": [{"path": "a.py", "content": code}], } def rec(i=0, accepted=True, hidden=True, split="train", **kw): return from_sky_raw(raw(i, accepted, **kw), factory_version="v3.4.1", split=split, hidden_passed=hidden) def test_roundtrip_and_strict(): r = rec() again = TrajectoryRecord.model_validate(json.loads(r.to_jsonl())) assert again == r and r.trainable d = json.loads(r.to_jsonl()); d["inconnu"] = 1 with pytest.raises(Exception): TrajectoryRecord.model_validate(d) def test_conversion_errors_are_clear(): bad = raw(); del bad["accepted"] with pytest.raises(ConversionError, match="accepted"): from_sky_raw(bad, factory_version="v") bad = raw(); bad["final_files"][0]["path"] = "../etc/passwd" with pytest.raises(ConversionError): from_sky_raw(bad, factory_version="v") def test_not_trainable_without_hidden_confirmation(): assert not rec(hidden=None).trainable assert not rec(hidden=False).trainable assert not rec(split="dev").trainable assert not rec(accepted=False).trainable def test_leak_guard(tmp_path): (tmp_path / "test_h.py").write_text(HIDDEN) fps = fingerprint_directory(tmp_path) assert fps assert find_leaks(rec(), fps) == [] leaky = rec(code=HIDDEN) assert find_leaks(leaky, fps) def test_wilson_matches_adr_numbers(): lo, hi = wilson(16, 20) assert (round(lo, 2), round(hi, 2)) == (0.58, 0.92) lo, hi = wilson(2, 20) assert (round(lo, 2), round(hi, 2)) == (0.03, 0.30) def test_summary_false_positive(): s = summarize([rec(0, True, True), rec(1, True, False), rec(2, False, False), rec(3, True, None)]) assert s["evaluated"] == 3 assert s["false_positive_among_accepted"]["k"] == 1 and s["false_positive_among_accepted"]["n"] == 2 def run(*args): return subprocess.run([sys.executable, *map(str, args)], capture_output=True, text=True) def test_validate_cli(tmp_path): (tmp_path / "h").mkdir(); (tmp_path / "h" / "t.py").write_text(HIDDEN) good = tmp_path / "good.jsonl" good.write_text("\n".join(rec(i).to_jsonl() for i in range(3)) + "\n") r = run(ROOT / "scripts/validate.py", good, "--hidden-dir", tmp_path / "h") assert r.returncode == 0, r.stderr dup = tmp_path / "dup.jsonl"; dup.write_text(rec(0).to_jsonl() + "\n" + rec(0).to_jsonl() + "\n") assert run(ROOT / "scripts/validate.py", dup).returncode == 1 leak = tmp_path / "leak.jsonl"; leak.write_text(rec(code=HIDDEN).to_jsonl() + "\n") r = run(ROOT / "scripts/validate.py", leak, "--hidden-dir", tmp_path / "h") assert r.returncode == 1 and "FUITE" in r.stderr broken = tmp_path / "broken.jsonl"; broken.write_text("{pas du json}\n") assert run(ROOT / "scripts/validate.py", broken).returncode == 1 def test_push_is_dry_run_by_default_and_blocks_invalid(tmp_path): data = tmp_path / "d"; data.mkdir() (data / "train.jsonl").write_text(rec().to_jsonl() + "\n") r = run(ROOT / "scripts/create_and_push.py", "--data", data) assert r.returncode == 0 and "Simulation" in r.stdout (data / "train.jsonl").write_text("{}\n") r = run(ROOT / "scripts/create_and_push.py", "--data", data, "--yes") assert r.returncode == 1 and "rien n'est poussé" in r.stderr