A Python port of the Invisible Internet Project (I2P)
at main 76 lines 3.1 kB view raw
1"""Tests for SAM bridge CLI entry point.""" 2 3import sys 4from unittest.mock import AsyncMock, patch, MagicMock 5 6import pytest 7 8from i2p_sam.__main__ import main 9 10 11class TestSAMCLI: 12 """Tests for the i2p-sam CLI entry point.""" 13 14 def test_help_flag(self, capsys): 15 """--help should print usage and exit.""" 16 with pytest.raises(SystemExit) as exc_info: 17 with patch.object(sys, "argv", ["i2p-sam", "--help"]): 18 main() 19 assert exc_info.value.code == 0 20 captured = capsys.readouterr() 21 assert "i2p-sam" in captured.out 22 assert "SAM bridge" in captured.out 23 24 def test_default_args(self): 25 """Default args should be host=127.0.0.1, port=7656.""" 26 import argparse 27 28 with patch.object(sys, "argv", ["i2p-sam"]): 29 with patch("i2p_sam.__main__.SAMBridge") as mock_bridge: 30 mock_instance = MagicMock() 31 mock_instance.start = AsyncMock() 32 mock_instance.stop = AsyncMock() 33 mock_instance.port = 7656 34 mock_bridge.return_value = mock_instance 35 36 with patch("i2p_sam.__main__.asyncio") as mock_asyncio: 37 # Make asyncio.run just call the coroutine sync 38 mock_asyncio.run = MagicMock(side_effect=SystemExit(0)) 39 with pytest.raises(SystemExit): 40 main() 41 42 mock_bridge.assert_called_once_with(host="127.0.0.1", port=7656) 43 44 def test_custom_port(self): 45 """Custom --port should be passed to SAMBridge.""" 46 with patch.object(sys, "argv", ["i2p-sam", "--port", "7700"]): 47 with patch("i2p_sam.__main__.SAMBridge") as mock_bridge: 48 mock_instance = MagicMock() 49 mock_instance.start = AsyncMock() 50 mock_instance.stop = AsyncMock() 51 mock_instance.port = 7700 52 mock_bridge.return_value = mock_instance 53 54 with patch("i2p_sam.__main__.asyncio") as mock_asyncio: 55 mock_asyncio.run = MagicMock(side_effect=SystemExit(0)) 56 with pytest.raises(SystemExit): 57 main() 58 59 mock_bridge.assert_called_once_with(host="127.0.0.1", port=7700) 60 61 def test_custom_host(self): 62 """Custom --host should be passed to SAMBridge.""" 63 with patch.object(sys, "argv", ["i2p-sam", "--host", "0.0.0.0"]): 64 with patch("i2p_sam.__main__.SAMBridge") as mock_bridge: 65 mock_instance = MagicMock() 66 mock_instance.start = AsyncMock() 67 mock_instance.stop = AsyncMock() 68 mock_instance.port = 7656 69 mock_bridge.return_value = mock_instance 70 71 with patch("i2p_sam.__main__.asyncio") as mock_asyncio: 72 mock_asyncio.run = MagicMock(side_effect=SystemExit(0)) 73 with pytest.raises(SystemExit): 74 main() 75 76 mock_bridge.assert_called_once_with(host="0.0.0.0", port=7656)