"""compare_single_leds.py の入力の決め方のテスト.
仕様:
- SPEC_01 / ENV_04: 入力を固定で読む比較スクリプトは src/lab.py で Lab の場所を決める
(環境変数 SMTIAS_LAB → 無ければ ../SmTIAS-Lab)
- 実装サマリー: --bottom/--left/--right を全部渡せば Lab が無くても動く.
渡さなかったものは Lab の 2026-06-15_v3_single_led/raw/ の既定ファイルを使う
テスト方針 (.claude/rules/testing.md):
- pytest を使用.DNG の読み込み(_luminance)は合成輝度に差し替え,図の生成は tmp_path に書く
- ROI はリポジトリの config/roi_config.json(smtias.whiteboard)を読むだけ
"""
import sys
from pathlib import Path
import matplotlib
import numpy
import pytest
matplotlib.use("Agg") # GUI なし環境でも動作するバックエンドを設定
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
from scripts import compare_single_leds # noqa: E402
from src.lab import LAB_ENV_VAR, V3_SINGLE_LED # noqa: E402
@pytest.fixture
def fake_luminance(monkeypatch: pytest.MonkeyPatch) -> list[Path]:
"""_luminance を合成輝度に差し替え,読まれたパスを記録する fixture."""
seen: list[Path] = []
rng = numpy.random.default_rng(0)
def _fake(dng_path: Path, roi: dict) -> numpy.ndarray:
seen.append(Path(dng_path))
return 100.0 + rng.normal(0.0, 1.0, size=(64, 48))
monkeypatch.setattr(compare_single_leds, "_luminance", _fake)
return seen
def _run(monkeypatch: pytest.MonkeyPatch, args: list[str]) -> None:
monkeypatch.setattr(sys, "argv", ["compare_single_leds.py", *args])
compare_single_leds.main()
def test_all_paths_given_works_without_lab(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, fake_luminance: list[Path]
) -> None:
"""正常: 3 つとも渡せば Lab が無くても動き,渡したパスをこの順で読むこと."""
monkeypatch.setenv(LAB_ENV_VAR, str(tmp_path / "no_lab"))
inputs = [tmp_path / f"{name}.dng" for name in ("bottom", "left", "right")]
output = tmp_path / "out" / "cmp.png"
_run(
monkeypatch,
[
"--bottom",
str(inputs[0]),
"--left",
str(inputs[1]),
"--right",
str(inputs[2]),
"--output",
str(output),
],
)
assert fake_luminance == inputs
assert output.is_file()
def test_missing_arg_without_lab_raises(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, fake_luminance: list[Path]
) -> None:
"""エラー: 1 つでも省き Lab も無ければ FileNotFoundError(SMTIAS_LAB を案内)."""
monkeypatch.setenv(LAB_ENV_VAR, str(tmp_path / "no_lab"))
with pytest.raises(FileNotFoundError, match="SMTIAS_LAB"):
_run(
monkeypatch,
[
"--bottom",
str(tmp_path / "b.dng"),
"--left",
str(tmp_path / "l.dng"),
"--output",
str(tmp_path / "cmp.png"),
],
)
assert fake_luminance == []
def test_defaults_come_from_lab_single_led_raw(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, fake_luminance: list[Path]
) -> None:
"""正常: 省いた入力は Lab の単灯実験の raw/ にある既定ファイルを読むこと."""
lab_root = tmp_path / "Lab"
raw_dir = lab_root / "experiments" / V3_SINGLE_LED / "raw"
raw_dir.mkdir(parents=True)
names = [
"SmTIAS_QM_20260615_141420.dng",
"SmTIAS_QM_20260615_150300.dng",
"SmTIAS_QM_20260615_170948.dng",
]
for name in names:
(raw_dir / name).write_bytes(b"x")
monkeypatch.setenv(LAB_ENV_VAR, str(lab_root))
given_right = tmp_path / "my_right.dng"
_run(monkeypatch, ["--right", str(given_right), "--output", str(tmp_path / "cmp.png")])
assert [p.resolve() for p in fake_luminance] == [
(raw_dir / names[0]).resolve(),
(raw_dir / names[1]).resolve(),
given_right.resolve(),
]