Files
orisys-sensor-design/tests/test_binary_preview_cli.py
T
2026-10-03 20:41:30 +08:00

74 lines
2.6 KiB
Python

"""The binary CLI must keep trials separate from the source colour artwork."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
from orisys_sensor_design import cli
from orisys_sensor_design.manifest import RevisionPaths
REVISION = Path(__file__).resolve().parents[1] / "sensors" / "finger-xynova" / "r01"
def _args(job: Path, output_dir: Path) -> argparse.Namespace:
return argparse.Namespace(
views=False,
source_job=job,
output_dir=output_dir,
camera_width_fraction=0.5,
)
def test_binary_preview_uses_job_and_preserves_colour_source(tmp_path, monkeypatch):
paths = RevisionPaths.from_revision_dir(REVISION)
colour = tmp_path / "colour.png"
colour.write_bytes(b"original colour")
job = {
"schema": "orisys-sensor-design/texture-job-v3",
"revision_dir": str(paths.root),
"source_blend": str(paths.source_blend),
"outputs": [{"uv_map": "compute", "artifacts": {"png": str(colour)}}],
}
job_path = tmp_path / "_texture_job.json"
job_path.write_text(json.dumps(job), encoding="utf-8")
output_dir = tmp_path / "binary"
calls = []
def run_blender(script, blend, args):
calls.append((script, blend, args))
output_dir.mkdir()
(output_dir / "compute_camera_compensated_width.png").write_bytes(b"binary")
return 0
monkeypatch.setattr(cli, "_run_blender_script", run_blender)
assert cli.cmd_binary_preview(REVISION, _args(job_path, output_dir)) == 0
assert colour.read_bytes() == b"original colour"
assert calls[0][0] == "binary_pattern_from_color.py"
assert "--camera-width-fraction" in calls[0][2]
assert cli.cmd_binary_preview(REVISION, _args(job_path, output_dir)) == 2
assert len(calls) == 1
def test_binary_preview_rejects_job_from_other_scene(tmp_path, monkeypatch):
paths = RevisionPaths.from_revision_dir(REVISION)
job_path = tmp_path / "_texture_job.json"
job_path.write_text(
json.dumps(
{
"schema": "orisys-sensor-design/texture-job-v3",
"revision_dir": str(paths.root),
"source_blend": str(tmp_path / "other.blend"),
"outputs": [{"uv_map": "compute", "artifacts": {"png": "missing.png"}}],
}
),
encoding="utf-8",
)
monkeypatch.setattr(
cli,
"_run_blender_script",
lambda *_: (_ for _ in ()).throw(AssertionError("Blender must not run")),
)
assert cli.cmd_binary_preview(REVISION, _args(job_path, tmp_path / "binary")) == 2