import io
import unittest

from h8536.connect_ok_matrix import build_cases, main


class ConnectOkMatrixTest(unittest.TestCase):
    def test_minimal_suite_starts_with_known_sequence_and_single_ok(self):
        cases = build_cases("minimal")

        self.assertEqual(cases[0].name, "baseline-40-80-c0")
        self.assertEqual(cases[1].name, "single-80")
        self.assertIn("pair-40-80", [case.name for case in cases])

    def test_gap_suite_uses_requested_inter_frame_gaps(self):
        cases = build_cases("gap", gaps=[0.01, 0.7])

        self.assertEqual([case.name for case in cases], ["gap-10ms-40-80-c0", "gap-700ms-40-80-c0"])
        self.assertEqual([case.gap for case in cases], [0.01, 0.7])

    def test_dry_run_defaults_to_even_parity_and_lists_frames(self):
        stdout = io.StringIO()

        exit_code = main(["--dry-run", "--suite", "minimal", "--limit", "2"], stdout=stdout)

        self.assertEqual(exit_code, 0)
        output = stdout.getvalue()
        self.assertIn("device=COM5 38400 8E1", output)
        self.assertIn("case[1]=baseline-40-80-c0", output)
        self.assertIn("case[2]=single-80", output)
        self.assertIn("frame=04 00 00 80 00 DE checksum_ok=1", output)

    def test_case_filter_selects_named_subset(self):
        stdout = io.StringIO()

        exit_code = main(["--dry-run", "--suite", "all", "--case", "pair-c0-80"], stdout=stdout)

        self.assertEqual(exit_code, 0)
        output = stdout.getvalue()
        self.assertIn("cases=1", output)
        self.assertIn("case[1]=pair-c0-80", output)


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