"""Unit tests for git proxy helpers.""" from mytoolkit.commands.git import _proxy_loopback_port, _restore_baseline 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_restore_baseline_daemon_own_port_becomes_unset(): # Git already pointing at the daemon's loopback port → treat as no proxy. assert _restore_baseline("http://127.0.0.1:38457", 38457) == "(not set)" assert _restore_baseline("http://localhost:38457", 38457) == "(not set)" def test_restore_baseline_foreign_values_pass_through(): assert _restore_baseline("http://127.0.0.1:10080", 38457) == "http://127.0.0.1:10080" assert _restore_baseline("(not set)", 38457) == "(not set)" assert _restore_baseline("http://127.0.0.1:38457", None) == "http://127.0.0.1:38457" 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 == {} def test_parallels_host_ips_includes_lan(monkeypatch): from mytoolkit.commands.git import _parallels_host_ips monkeypatch.setattr("mytoolkit.commands.git.sys.platform", "darwin") monkeypatch.delenv("XIAOHE_CI_PARALLELS_HOST_IP", raising=False) monkeypatch.setattr( "mytoolkit.commands.git.subprocess.check_output", lambda *a, **k: "inet 10.211.55.2 netmask\n", ) monkeypatch.setattr("mytoolkit.commands.git._primary_lan_ip", lambda: "192.168.1.168") assert _parallels_host_ips() == ["10.211.55.2", "192.168.1.168"] def test_should_kill_proxy_only_if_we_spawned(): from mytoolkit.commands.git import _should_kill_proxy_process assert _should_kill_proxy_process(None, "fastgithub") is False assert _should_kill_proxy_process({"name": "other"}, "fastgithub") is False assert _should_kill_proxy_process( {"name": "fastgithub", "adopted": True}, "fastgithub" ) is False assert _should_kill_proxy_process( {"name": "fastgithub", "adopted": False}, "fastgithub" ) is True # Old state files without adopted: keep previous stop-kills-daemon behavior assert _should_kill_proxy_process({"name": "fastgithub"}, "fastgithub") is True