"""Tests for src/askme/gpu.py.""" from __future__ import annotations import asyncio from unittest.mock import AsyncMock, patch from askme.gpu import unload_llamacpp_model, unload_ollama_model def test_unload_requests_zero_keep_alive(): """ Scenario: free the GPU between eval phases Given a model resident in Ollama When unload_ollama_model("qwen2.5:7b") runs Then Ollama is asked to generate with keep_alive=0 against settings.ollama_host (Ollama's documented way to evict a model immediately) """ client = AsyncMock() with patch("askme.gpu.ollama.AsyncClient", return_value=client) as ctor: asyncio.run(unload_ollama_model("qwen2.5:7b")) ctor.assert_called_once_with(host="http://localhost:11434") client.generate.assert_awaited_once_with(model="qwen2.5:7b", keep_alive=0) def test_unload_llamacpp_posts_to_router(): """ Scenario: free the GPU held by the llama.cpp router Given a model resident in the router When unload_llamacpp_model("gemma-4-12b-qat") runs Then POST /models/unload is sent with that model id """ client = AsyncMock() client.__aenter__.return_value = client with patch("askme.gpu.httpx.AsyncClient", return_value=client): asyncio.run(unload_llamacpp_model("gemma-4-12b-qat")) client.post.assert_awaited_once_with( "http://localhost:8080/models/unload", json={"model": "gemma-4-12b-qat"} )