from pathlib import Path import pytest from gpu_rent.llm_runtime import ( decide_runtime, normalize_runtime, parse_llamacpp_models, parse_ollama_models, pick_llamacpp_linux_asset_url, remap_llamacpp_url, write_ollama_models_preset, ) def test_normalize_runtime(): assert normalize_runtime(None) == "none" assert normalize_runtime("OLLAMA") == "ollama" assert normalize_runtime("llama-cpp") == "llamacpp" with pytest.raises(ValueError): normalize_runtime("foo") def test_decide_runtime_flags_win(): assert ( decide_runtime(flag=None, ollama_flag=True, llamacpp_flag=False, from_config="none") == "ollama" ) assert ( decide_runtime(flag="llamacpp", ollama_flag=False, llamacpp_flag=False, from_config="ollama") == "llamacpp" ) with pytest.raises(ValueError): decide_runtime(flag=None, ollama_flag=True, llamacpp_flag=True, from_config="none") def test_parse_ollama_models(tmp_path: Path): path = tmp_path / "m.yaml" path.write_text( "models:\n - name: huihui_ai/qwen2.5-abliterate:7b\n default: true\n - qwen2.5:3b\n", encoding="utf-8", ) entries = parse_ollama_models(path) assert [e.name for e in entries] == [ "huihui_ai/qwen2.5-abliterate:7b", "qwen2.5:3b", ] assert entries[0].default is True def test_parse_empty_manifest(tmp_path: Path): path = tmp_path / "empty.yaml" path.write_text("models: []\n", encoding="utf-8") assert parse_ollama_models(path) == [] assert parse_ollama_models(tmp_path / "missing.yaml") == [] def test_write_preset(tmp_path: Path): path = tmp_path / "out.yaml" write_ollama_models_preset(path, "recommended") entries = parse_ollama_models(path) assert entries[0].name == "huihui_ai/qwen2.5-vl-abliterated:7b" def test_write_llamacpp_preset_includes_mmproj(tmp_path: Path): from gpu_rent.llm_runtime import write_llamacpp_models_preset path = tmp_path / "lc.yaml" write_llamacpp_models_preset(path, "recommended") entries = parse_llamacpp_models(path) assert len(entries) == 1 assert "VL" in entries[0].url or "vl" in entries[0].url.lower() assert entries[0].mmproj_url assert "mmproj" in entries[0].mmproj_url def test_remap_dead_bartowski_abliterate_url(tmp_path: Path): dead = ( "https://huggingface.co/bartowski/huihui-ai_Qwen2.5-7B-Instruct-abliterated-GGUF/" "resolve/main/huihui-ai_Qwen2.5-7B-Instruct-abliterated-Q4_K_M.gguf" ) fixed = remap_llamacpp_url(dead) assert "RichardErkhov" in fixed assert "Q4_K_M.gguf" in fixed path = tmp_path / "lc.yaml" path.write_text(f"models:\n - url: {dead}\n default: true\n", encoding="utf-8") entries = parse_llamacpp_models(path) assert len(entries) == 1 assert entries[0].url == fixed def test_pick_llamacpp_linux_asset_skips_windows_cuda(): assets = [ { "name": "cudart-llama-bin-win-cuda-12.4-x64.zip", "browser_download_url": "https://example/cudart-win.zip", }, { "name": "llama-b10545-bin-win-cuda-12.4-x64.zip", "browser_download_url": "https://example/win-cuda.zip", }, { "name": "llama-b10545-bin-ubuntu-x64.tar.gz", "browser_download_url": "https://example/ubuntu-cpu.tar.gz", }, { "name": "llama-b10545-bin-ubuntu-vulkan-x64.tar.gz", "browser_download_url": "https://example/ubuntu-vulkan.tar.gz", }, ] assert pick_llamacpp_linux_asset_url(assets) == "https://example/ubuntu-vulkan.tar.gz" def test_pick_llamacpp_linux_asset_prefers_ubuntu_cuda(): assets = [ { "name": "llama-b1-bin-ubuntu-vulkan-x64.tar.gz", "browser_download_url": "https://example/vulkan.tar.gz", }, { "name": "llama-b1-bin-ubuntu-cuda-12.4-x64.tar.gz", "browser_download_url": "https://example/cuda.tar.gz", }, ] assert pick_llamacpp_linux_asset_url(assets) == "https://example/cuda.tar.gz"