"""Tests for qpip.cli."""

from __future__ import annotations

import unittest

from qpip.cli import CLIUsageError, parse_cli_args


class ParseCliArgsTests(unittest.TestCase):
    def test_supports_positional_mirror(self) -> None:
        options, warning = parse_cli_args(["tsinghua", "install", "numpy"])

        self.assertIsNone(warning)
        self.assertEqual(options.mirror, "tsinghua")
        self.assertEqual(options.pip_args, ("install", "numpy"))

    def test_supports_named_mirror(self) -> None:
        options, _ = parse_cli_args(["--mirror", "douban", "install", "pandas"])

        self.assertEqual(options.mirror, "douban")
        self.assertEqual(options.pip_args, ("install", "pandas"))

    def test_respects_separator_for_pip_options(self) -> None:
        options, _ = parse_cli_args(["--", "--version"])

        self.assertEqual(options.pip_args, ("--version",))

    def test_detects_conflicting_mirrors(self) -> None:
        with self.assertRaises(CLIUsageError):
            parse_cli_args(["ali", "--mirror", "douban", "install", "requests"])

    def test_recognizes_qpip_flags_before_pip_args(self) -> None:
        options, _ = parse_cli_args(["--dry-run", "--list-mirrors"])

        self.assertTrue(options.dry_run)
        self.assertTrue(options.list_mirrors)
        self.assertEqual(options.pip_args, ())


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