"""Tests for harness/producer.py — the subprocess plumbing (no LLM/render involved). Uses a throwaway variant whose produce.py just writes a dummy file + artifacts.json, so we exercise the REAL subprocess path (stdin request, cwd isolation, artifacts.json parsing, relative-path resolution, error handling) without any heavy deps. """ import json import textwrap from pathlib import Path import pytest from harness.genome import VariantManifest from harness.producer import produce_videos def _make_variant(root: Path, vid: str, produce_src: str) -> VariantManifest: d = root / "variants" / vid d.mkdir(parents=True) (d / "manifest.json").write_text(json.dumps({"variant_id": vid, "genome": {"k": 1}}), encoding="utf-8") (d / "produce.py").write_text(textwrap.dedent(produce_src), encoding="utf-8") return VariantManifest.load(d) def test_produce_videos_happy_path(tmp_path): manifest = _make_variant(tmp_path, "vfake", """ import json, sys from pathlib import Path req = json.load(sys.stdin) out = Path(req["output_dir"]) videos = [] for i in range(req["n"]): p = out / f"v{i}.mp4" p.write_bytes(b"\\x00\\x01\\x02") videos.append({"video_path": f"v{i}.mp4", "title": f"t{i}", "description": "d", "music_name": "m", "music_attribution": "attr"}) (out / "artifacts.json").write_text(json.dumps({"videos": videos, "errors": []})) """) out = tmp_path / "out" arts = produce_videos(manifest, 2, output_dir=out, gemini_api_key="x", repo_root=tmp_path) assert len(arts) == 2 assert all(Path(a.video_path).exists() for a in arts) assert arts[0].title == "t0" and arts[0].music_attribution == "attr" # relative path was resolved against output_dir assert Path(arts[0].video_path).parent == out.resolve() or Path(arts[0].video_path).parent == out def test_produce_videos_passes_request_fields(tmp_path): manifest = _make_variant(tmp_path, "vreq", """ import json, sys from pathlib import Path req = json.load(sys.stdin) out = Path(req["output_dir"]) # Echo the genome + run_date back through a (dummy) video's title to prove they arrived. out.joinpath("v.mp4").write_bytes(b"x") (out / "artifacts.json").write_text(json.dumps({ "videos": [{"video_path": "v.mp4", "title": req["genome"].get("k"), "description": req["run_date"]}], "errors": [], })) """) out = tmp_path / "out" arts = produce_videos(manifest, 1, output_dir=out, gemini_api_key="x", run_date="2026-06-22", repo_root=tmp_path) assert arts[0].title == "1" assert arts[0].description == "2026-06-22" def test_produce_videos_missing_script_raises(tmp_path): d = tmp_path / "variants" / "noproducer" d.mkdir(parents=True) (d / "manifest.json").write_text(json.dumps({"variant_id": "noproducer", "genome": {}}), encoding="utf-8") manifest = VariantManifest.load(d) with pytest.raises(RuntimeError, match="no produce.py"): produce_videos(manifest, 1, output_dir=tmp_path / "out", gemini_api_key="x", repo_root=tmp_path) def test_produce_videos_no_artifacts_json_raises(tmp_path): manifest = _make_variant(tmp_path, "vbad", """ import sys sys.exit(3) # crash before writing artifacts.json """) with pytest.raises(RuntimeError, match="produced no artifacts.json"): produce_videos(manifest, 1, output_dir=tmp_path / "out", gemini_api_key="x", repo_root=tmp_path) def test_produce_videos_drops_missing_files(tmp_path): manifest = _make_variant(tmp_path, "vmiss", """ import json, sys from pathlib import Path out = Path(json.load(sys.stdin)["output_dir"]) (out / "artifacts.json").write_text(json.dumps({ "videos": [{"video_path": "nope.mp4", "title": "ghost"}], "errors": ["render failed"], })) """) arts = produce_videos(manifest, 1, output_dir=tmp_path / "out", gemini_api_key="x", repo_root=tmp_path) assert arts == [] # file never written → dropped