from __future__ import annotations

import contextlib
import io
import unittest

from scienith_openapi_contract_tooling.cli import main


class CliHelpTests(unittest.TestCase):
    def run_cli(self, *args: str) -> tuple[int, str]:
        stdout = io.StringIO()
        with contextlib.redirect_stdout(stdout):
            try:
                code = main(args)
            except SystemExit as exc:
                code = int(exc.code or 0)
        return code, stdout.getvalue()

    def test_root_help(self) -> None:
        code, output = self.run_cli("--help")
        self.assertEqual(code, 0)
        self.assertIn("scienith-contract", output)
        self.assertIn("build", output)
        self.assertIn("openapi", output)
        self.assertIn("generate", output)

    def test_required_help_surfaces(self) -> None:
        for args in [
            ("build", "--help"),
            ("build", "all", "--help"),
            ("openapi", "--help"),
            ("generate", "--help"),
        ]:
            with self.subTest(args=args):
                code, output = self.run_cli(*args)
                self.assertEqual(code, 0)
                self.assertIn("usage:", output)

    def test_reserved_commands_exit_zero(self) -> None:
        for args in [
            ("openapi",),
            ("generate",),
        ]:
            with self.subTest(args=args):
                code, output = self.run_cli(*args)
                self.assertEqual(code, 0)
                self.assertIn("reserved for profile-driven generation", output)


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