Files
2026-05-28 14:19:01 +08:00

139 lines
5.2 KiB
Python

from __future__ import annotations
from pathlib import Path
from pyxray import cli
from pyxray.libs.xray_assets import XrayAssets
def test_no_subcommand_defaults_to_web(monkeypatch) -> None: # noqa: ANN001
captured = {}
def fake_run_web(host: str, port: int, xray_dir: str) -> None:
captured.update({"host": host, "port": port, "xray_dir": xray_dir})
monkeypatch.setattr(cli, "run_web", fake_run_web)
cli.main([])
assert captured == {"host": "0.0.0.0", "port": 8000, "xray_dir": "data/xray"}
def test_web_subcommand_still_uses_web_runner(monkeypatch) -> None: # noqa: ANN001
captured = {}
def fake_run_web(host: str, port: int, xray_dir: str) -> None:
captured.update({"host": host, "port": port, "xray_dir": xray_dir})
monkeypatch.setattr(cli, "run_web", fake_run_web)
cli.main(["web", "--host", "127.0.0.1", "--port", "9000", "--xray-dir", "runtime/xray"])
assert captured == {"host": "127.0.0.1", "port": 9000, "xray_dir": "runtime/xray"}
def test_configs_download_prints_download_toml(tmp_path: Path, capsys, monkeypatch) -> None: # noqa: ANN001
monkeypatch.chdir(tmp_path)
data = tmp_path / "data"
data.mkdir()
(data / "download.toml").write_text('version = "v1.2.3"\n', encoding="utf-8")
cli.main(["configs", "--download", "--xray-dir", "data/xray"])
assert capsys.readouterr().out == 'version = "v1.2.3"\n'
def test_clear_download_removes_only_download_settings_and_known_assets(tmp_path: Path, monkeypatch) -> None: # noqa: ANN001
monkeypatch.chdir(tmp_path)
data = tmp_path / "data"
xray_dir = data / "xray"
xray_dir.mkdir(parents=True)
(data / "download.toml").write_text("download", encoding="utf-8")
(data / "nodes.toml").write_text("nodes", encoding="utf-8")
(xray_dir / "xray").write_bytes(b"xray")
(xray_dir / "xray.exe").write_bytes(b"xray.exe")
(xray_dir / "geoip.dat").write_bytes(b"geoip")
(xray_dir / "geosite.dat").write_bytes(b"geosite")
(xray_dir / "user-file.txt").write_text("keep", encoding="utf-8")
cli.main(["clear", "--download", "--xray-dir", "data/xray"])
assert not (data / "download.toml").exists()
assert not (xray_dir / "xray").exists()
assert not (xray_dir / "xray.exe").exists()
assert not (xray_dir / "geoip.dat").exists()
assert not (xray_dir / "geosite.dat").exists()
assert (data / "nodes.toml").exists()
assert (xray_dir / "user-file.txt").exists()
def test_clear_all_removes_known_data_and_keeps_unrelated_files(tmp_path: Path, monkeypatch) -> None: # noqa: ANN001
monkeypatch.chdir(tmp_path)
data = tmp_path / "data"
xray_dir = data / "xray"
transparent_dir = data / "transparent"
transparent_dir.mkdir(parents=True)
xray_dir.mkdir()
for name in ("download.toml", "nodes.toml", "settings.toml", "config.json", "service-state.json", "xray.log"):
(data / name).write_text(name, encoding="utf-8")
(transparent_dir / "transparent-iptables-setup.sh").write_text("script", encoding="utf-8")
(xray_dir / "xray").write_bytes(b"xray")
(xray_dir / "geoip.dat").write_bytes(b"geoip")
(data / "notes.txt").write_text("keep", encoding="utf-8")
(xray_dir / "custom.dat").write_text("keep", encoding="utf-8")
cli.main(["clear", "--all", "--xray-dir", "data/xray"])
for name in ("download.toml", "nodes.toml", "settings.toml", "config.json", "service-state.json", "xray.log"):
assert not (data / name).exists()
assert not transparent_dir.exists()
assert not (xray_dir / "xray").exists()
assert not (xray_dir / "geoip.dat").exists()
assert (data / "notes.txt").exists()
assert (xray_dir / "custom.dat").exists()
def test_download_command_persists_settings_and_reuses_ensure(monkeypatch, tmp_path: Path) -> None: # noqa: ANN001
monkeypatch.chdir(tmp_path)
captured = {}
def fake_ensure_xray_assets(directory, **options): # noqa: ANN001
captured["directory"] = directory
captured["options"] = options
path = Path(directory)
path.mkdir(parents=True, exist_ok=True)
xray = path / "xray"
xray.write_bytes(b"xray")
geoip = path / "geoip.dat"
geoip.write_bytes(b"geoip")
geosite = path / "geosite.dat"
geosite.write_bytes(b"geosite")
return XrayAssets(directory=path, xray=xray, geoip=geoip, geosite=geosite, downloaded=("geoip.dat",))
monkeypatch.setattr("pyxray.libs.app_data.ensure_xray_assets", fake_ensure_xray_assets)
cli.main(
[
"download",
"--target",
"geoip",
"--directory",
"data/xray",
"--version",
"v1.2.3",
"--geoip-url",
"https://mirror.example.invalid/geoip.dat",
"--force",
]
)
assert captured["directory"] == "data/xray"
assert captured["options"]["target"] == "geoip"
assert captured["options"]["version"] == "v1.2.3"
assert captured["options"]["geoip_url"] == "https://mirror.example.invalid/geoip.dat"
assert captured["options"]["force"] is True
content = (tmp_path / "data" / "download.toml").read_text(encoding="utf-8")
assert 'directory = "data/xray"' in content
assert 'target = "geoip"' in content
assert 'version = "v1.2.3"' in content