import unittest

from build_gallery import _selfcheck_exit_code
from gallery_builder.types import BuildReport


class BuildGallerySelfcheckTest(unittest.TestCase):
    def test_accepts_multiple_embeddings_per_person(self) -> None:
        report = BuildReport(ok_images=3)
        selfcheck = {"person_count": 2, "embedding_count": 3, "sample_lengths_ok": True}

        self.assertEqual(_selfcheck_exit_code(selfcheck, report, fail_on_empty=True), 0)

    def test_rejects_missing_embedding_for_person(self) -> None:
        report = BuildReport(ok_images=1)
        selfcheck = {"person_count": 2, "embedding_count": 1, "sample_lengths_ok": True}

        self.assertEqual(_selfcheck_exit_code(selfcheck, report, fail_on_empty=True), 4)

    def test_rejects_embedding_count_that_does_not_match_successful_images(self) -> None:
        report = BuildReport(ok_images=2)
        selfcheck = {"person_count": 2, "embedding_count": 3, "sample_lengths_ok": True}

        self.assertEqual(_selfcheck_exit_code(selfcheck, report, fail_on_empty=False), 4)

    def test_rejects_bad_embedding_lengths(self) -> None:
        report = BuildReport(ok_images=2)
        selfcheck = {"person_count": 2, "embedding_count": 2, "sample_lengths_ok": False}

        self.assertEqual(_selfcheck_exit_code(selfcheck, report, fail_on_empty=False), 5)


if __name__ == "__main__":
    unittest.main()
