"""Unit tests for git proxy helpers.""" from mytoolkit.commands.git import _proxy_loopback_port def test_proxy_loopback_port_http(): assert _proxy_loopback_port("http://127.0.0.1:38457") == 38457 assert _proxy_loopback_port("http://localhost:10080") == 10080 def test_proxy_loopback_port_rejects_remote(): assert _proxy_loopback_port("http://10.211.55.2:38457") is None assert _proxy_loopback_port("socks5://127.0.0.1:1080") is None def test_procs_matching_filters_by_command(): from mytoolkit.commands.git import _procs_matching import os pid = os.getpid() # Current test process command line contains "python". assert pid in _procs_matching([pid], "python") assert _procs_matching([pid], "definitely-not-a-real-proc-xyz") == [] def test_restore_stale_git_proxy_restores_when_port_dead(monkeypatch): from mytoolkit.commands.git import _restore_stale_git_proxy restored: dict[str, object] = {} monkeypatch.setattr( "mytoolkit.commands.git._read_state", lambda: {"old_http": "http://old.example:1", "old_https": None}, ) monkeypatch.setattr( "mytoolkit.commands.git._get_git_proxy", lambda key: "http://127.0.0.1:59999", ) monkeypatch.setattr( "mytoolkit.commands.git._proxy_loopback_port", lambda url: 59999 ) monkeypatch.setattr( "mytoolkit.commands.git._port_listening_on", lambda host, port: False ) monkeypatch.setattr( "mytoolkit.commands.git._restore_proxy_key", lambda key, old: restored.__setitem__(key, old), ) assert _restore_stale_git_proxy() is True assert restored == {"http.proxy": "http://old.example:1", "https.proxy": None} def test_restore_stale_git_proxy_noop_when_alive(monkeypatch): from mytoolkit.commands.git import _restore_stale_git_proxy restored: dict[str, object] = {} monkeypatch.setattr( "mytoolkit.commands.git._read_state", lambda: {"old_http": "http://old.example:1"}, ) monkeypatch.setattr( "mytoolkit.commands.git._get_git_proxy", lambda key: "http://127.0.0.1:59999", ) monkeypatch.setattr( "mytoolkit.commands.git._proxy_loopback_port", lambda url: 59999 ) monkeypatch.setattr( "mytoolkit.commands.git._port_listening_on", lambda host, port: True ) monkeypatch.setattr( "mytoolkit.commands.git._restore_proxy_key", lambda key, old: restored.__setitem__(key, old), ) assert _restore_stale_git_proxy() is False assert restored == {}