"""lab.py(SmTIAS-Lab の場所と撮影ファイルの解決)の仕様ベーステスト.
仕様出典:
- docs/02_ENV/ENV_04_開発コマンド.md「実機のデータ」「環境変数」
- docs/04_SPEC/SPEC_04_評価仕様.md(実測は SmTIAS-Lab の experiments/<日付>_<内容>/raw/)
- CLAUDE.md「関連リポジトリと実験データ」(Lab は ../SmTIAS-Lab)
Lab の実データには依存しない(環境変数 SMTIAS_LAB を tmp_path に向けて検証する).
既定の ../SmTIAS-Lab の解決だけは,手元に Lab がある場合のみ確かめる.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from sm_tias_lightsim import lab
from sm_tias_lightsim.lab import (
LAB_ENV_VAR,
V3_FULL_UNIFORMITY_CSV,
V3_PNG_WHITEBOARD,
V3_SHOTS,
LabShot,
find_lab_file,
lab_file,
lab_path,
lab_root,
shot_dng,
shot_meta,
)
_EXP = "2026-01-01_test"
@pytest.fixture
def fake_lab(tmp_path, monkeypatch) -> Path:
"""SMTIAS_LAB を tmp_path 配下の空の Lab に向ける."""
root = tmp_path / "MyLab"
(root / "experiments" / _EXP / "raw").mkdir(parents=True)
monkeypatch.setenv(LAB_ENV_VAR, str(root))
return root
def test_env_var_name_is_smtias_lab():
"""ENV_04: Lab の場所は環境変数 SMTIAS_LAB で上書きする."""
assert LAB_ENV_VAR == "SMTIAS_LAB"
class TestLabRoot:
def test_env_var_points_to_lab(self, fake_lab):
assert lab_root() == fake_lab.resolve()
def test_env_var_whitespace_only_falls_back_to_default(self, monkeypatch, tmp_path):
"""空白だけの SMTIAS_LAB は未設定扱い(既定の ../SmTIAS-Lab を見る)."""
monkeypatch.setenv(LAB_ENV_VAR, " ")
fake_repo = tmp_path / "repos" / "SmTIAS-LightSim"
fake_repo.mkdir(parents=True)
(tmp_path / "repos" / "SmTIAS-Lab").mkdir()
monkeypatch.setattr(lab, "REPO_ROOT", fake_repo)
assert lab_root() == tmp_path / "repos" / "SmTIAS-Lab"
def test_env_var_missing_dir_raises_with_hint(self, monkeypatch, tmp_path):
missing = tmp_path / "nope"
monkeypatch.setenv(LAB_ENV_VAR, str(missing))
with pytest.raises(FileNotFoundError) as exc:
lab_root()
assert str(missing) in str(exc.value)
assert "SMTIAS_LAB" in str(exc.value)
def test_default_is_sibling_smtias_lab(self, monkeypatch, tmp_path):
"""未設定ならリポジトリの隣の SmTIAS-Lab を使う."""
monkeypatch.delenv(LAB_ENV_VAR, raising=False)
fake_repo = tmp_path / "SmTIAS-LightSim"
fake_repo.mkdir()
(tmp_path / "SmTIAS-Lab").mkdir()
monkeypatch.setattr(lab, "REPO_ROOT", fake_repo)
assert lab_root() == tmp_path / "SmTIAS-Lab"
def test_default_missing_raises_with_hint(self, monkeypatch, tmp_path):
monkeypatch.delenv(LAB_ENV_VAR, raising=False)
fake_repo = tmp_path / "SmTIAS-LightSim"
fake_repo.mkdir()
monkeypatch.setattr(lab, "REPO_ROOT", fake_repo)
with pytest.raises(FileNotFoundError) as exc:
lab_root()
msg = str(exc.value)
assert "SmTIAS-Lab" in msg
assert "SMTIAS_LAB" in msg
def test_real_default_points_next_to_repo(self, monkeypatch):
"""手元に ../SmTIAS-Lab があれば,未設定時にそこを指す."""
monkeypatch.delenv(LAB_ENV_VAR, raising=False)
repo = Path(__file__).resolve().parents[1]
expected = repo.parent / "SmTIAS-Lab"
if not expected.is_dir():
pytest.skip(f"隣に SmTIAS-Lab がありません: {expected}")
assert lab_root().resolve() == expected.resolve()
class TestLabPath:
def test_joins_experiments_dir(self, fake_lab):
p = lab_path(_EXP, "raw", "a.dng")
assert p == fake_lab.resolve() / "experiments" / _EXP / "raw" / "a.dng"
def test_does_not_check_existence(self, fake_lab):
p = lab_path(_EXP, "raw", "missing.dng")
assert not p.exists()
@pytest.mark.parametrize(
"parts",
[
("..",),
("../other",),
(_EXP, ".."),
(_EXP, "raw", "../../x"),
(_EXP, "..", "secret.txt"),
],
)
def test_rejects_parent_dir(self, fake_lab, parts):
with pytest.raises(ValueError):
lab_path(*parts)
def test_rejects_absolute_path(self, fake_lab, tmp_path):
with pytest.raises(ValueError):
lab_path(str(tmp_path / "abs"))
with pytest.raises(ValueError):
lab_path(_EXP, str(tmp_path / "abs.dng"))
def test_rejects_posix_absolute_path(self, fake_lab):
with pytest.raises(ValueError):
lab_path(_EXP, "/etc/passwd")
def test_rejects_empty(self, fake_lab):
with pytest.raises(ValueError):
lab_path("")
with pytest.raises(ValueError):
lab_path(_EXP, "")
def test_validation_before_lab_lookup(self, monkeypatch, tmp_path):
"""不正なパスは Lab が無くても ValueError(FileNotFoundError ではない)."""
monkeypatch.setenv(LAB_ENV_VAR, str(tmp_path / "nope"))
with pytest.raises(ValueError):
find_lab_file("..", "x")
class TestLabFile:
def test_returns_existing_file(self, fake_lab):
f = fake_lab / "experiments" / _EXP / "raw" / "a.dng"
f.write_bytes(b"x")
assert lab_file(_EXP, "raw", "a.dng") == f.resolve()
def test_missing_file_message(self, fake_lab):
with pytest.raises(FileNotFoundError) as exc:
lab_file(_EXP, "raw", "missing.dng")
msg = str(exc.value)
assert "missing.dng" in msg
assert "SMTIAS_LAB" in msg
def test_directory_is_not_a_file(self, fake_lab):
with pytest.raises(FileNotFoundError):
lab_file(_EXP, "raw")
def test_missing_lab_message_explains_setup(self, monkeypatch, tmp_path):
monkeypatch.setenv(LAB_ENV_VAR, str(tmp_path / "nope"))
with pytest.raises(FileNotFoundError) as exc:
lab_file(_EXP, "raw", "a.dng")
assert "SMTIAS_LAB" in str(exc.value)
class TestFindLabFile:
def test_returns_path_when_exists(self, fake_lab):
f = fake_lab / "experiments" / _EXP / "raw" / "a.dng"
f.write_bytes(b"x")
assert find_lab_file(_EXP, "raw", "a.dng") == f.resolve()
def test_none_when_file_missing(self, fake_lab):
assert find_lab_file(_EXP, "raw", "missing.dng") is None
def test_none_when_lab_missing(self, monkeypatch, tmp_path):
monkeypatch.setenv(LAB_ENV_VAR, str(tmp_path / "nope"))
assert find_lab_file(_EXP, "raw", "a.dng") is None
def test_invalid_path_still_raises(self, fake_lab):
with pytest.raises(ValueError):
find_lab_file(_EXP, "..", "a.dng")
class TestShots:
def test_v3_shot_keys(self):
"""ENV_04: 現行機の全灯・単灯 3 種(キー full/bottom/left/right)."""
assert set(V3_SHOTS) == {"full", "bottom", "left", "right"}
def test_full_shot_is_2026_06_01_all_leds(self):
full = V3_SHOTS["full"]
assert full.experiment == "2026-06-01_v3_all_leds"
assert full.stem == "SmTIAS_QM_20260601_124500"
def test_file_names(self):
shot = LabShot(_EXP, "S_1")
assert shot.dng_name == "S_1.dng"
assert shot.meta_name == "S_1.meta.json"
def test_shot_dng_and_meta_are_in_raw(self, fake_lab):
raw = fake_lab / "experiments" / _EXP / "raw"
(raw / "S_1.dng").write_bytes(b"x")
(raw / "S_1.meta.json").write_text("{}", encoding="utf-8")
shot = LabShot(_EXP, "S_1")
assert shot_dng(shot) == (raw / "S_1.dng").resolve()
assert shot_meta(shot) == (raw / "S_1.meta.json").resolve()
def test_shot_dng_missing_raises(self, fake_lab):
with pytest.raises(FileNotFoundError):
shot_dng(LabShot(_EXP, "absent"))
def test_constant_paths_are_valid_parts(self, fake_lab):
"""定数のパスは lab_path に渡せる(.. や絶対パスを含まない)."""
lab_path(*V3_FULL_UNIFORMITY_CSV)
lab_path(*V3_PNG_WHITEBOARD)
def test_real_lab_has_v3_shots():
"""手元に Lab のデータがあれば,V3_SHOTS の DNG と meta.json がすべて揃っている."""
full = V3_SHOTS["full"]
if find_lab_file(full.experiment, "raw", full.dng_name) is None:
pytest.skip("SmTIAS-Lab に実機のデータがありません")
for key, shot in V3_SHOTS.items():
assert find_lab_file(shot.experiment, "raw", shot.dng_name) is not None, key
assert find_lab_file(shot.experiment, "raw", shot.meta_name) is not None, key
assert find_lab_file(*V3_FULL_UNIFORMITY_CSV) is not None