import csv from pathlib import Path from src.config_loader import FaceLoggingSettings from src.face_logger import FaceLogger from src.models import FaceTrackState, HeadPoseResult def test_face_logs_success_failure_bom_and_summary_deduplication(tmp_path: Path) -> None: logger = FaceLogger(FaceLoggingSettings(True, True, True, 1, str(tmp_path)), "exp", "20260101_000000") logger.open() success = HeadPoseResult(1, 1, "now", .1, True, True, 1.0, -2.0, 3.0, 468, None, .9) failure = HeadPoseResult(1, 2, "later", .2, False, False, None, None, None, 0, None, None, "face_not_detected") logger.write_results([success, failure]) state = FaceTrackState(1, total_frames=2, face_detected_frames=1, pose_estimated_frames=1, last_pitch=1.0, last_yaw=-2.0, last_roll=3.0, min_yaw=-2.0, max_yaw=-2.0, yaw_sum=-2.0, last_result=failure, finalized=True) logger.write_summary(state) logger.write_summary(state) logger.close() logger.close() paths = list(tmp_path.glob("*.csv")) assert len(paths) == 2 assert all(path.read_bytes().startswith(b"\xef\xbb\xbf") for path in paths) frame_path = next(path for path in paths if "frames" in path.name) with frame_path.open(encoding="utf-8-sig", newline="") as stream: rows = list(csv.DictReader(stream)) assert len(rows) == 2 and rows[1]["failure_reason"] == "face_not_detected" assert "yaw" in rows[0] summary_path = next(path for path in paths if "summary" in path.name) with summary_path.open(encoding="utf-8-sig", newline="") as stream: assert len(list(csv.DictReader(stream))) == 1