"""Model installation invariants without network or real model downloads.""" from __future__ import annotations import hashlib import threading from unittest.mock import MagicMock import httpx import pytest from voice_dictation.core import model_manager as module from voice_dictation.core.model_manager import ( DownloadCancelled, ModelError, ModelManager, ModelSpec, ) @pytest.fixture def fixture_model(monkeypatch): content = b"small synthetic model bytes" spec = ModelSpec( "test", "Test", "https://example.invalid/model", len(content), hashlib.sha256(content).hexdigest(), ) monkeypatch.setattr(module, "CATALOG", (spec,)) response = MagicMock() response.iter_bytes.return_value = iter([content[:8], content[8:]]) stream = MagicMock() stream.__enter__.return_value = response monkeypatch.setattr(module.httpx, "stream", lambda *a, **kw: stream) return spec, content, response def test_verified_atomic_download(tmp_path, fixture_model): spec, content, _ = fixture_model manager = ModelManager(tmp_path) progress = [] path = manager.download(spec.id, lambda done, total: progress.append((done, total))) assert path.read_bytes() == content assert manager.is_installed(spec.id) assert progress[-1] == (len(content), len(content)) assert not list(tmp_path.glob("*.part")) manager.delete(spec.id) assert not manager.is_installed(spec.id) def test_corrupt_download_never_installed(tmp_path, fixture_model): spec, _, response = fixture_model response.iter_bytes.return_value = iter([b"corrupt"]) manager = ModelManager(tmp_path) with pytest.raises(ModelError, match="SHA256"): manager.download(spec.id) assert not manager.is_installed(spec.id) assert not list(tmp_path.glob("*.part")) def test_cancel_removes_partial(tmp_path, fixture_model): spec, _, _ = fixture_model cancel = threading.Event() manager = ModelManager(tmp_path) with pytest.raises(DownloadCancelled): manager.download(spec.id, lambda *_: cancel.set(), cancel) assert not manager.path_for(spec.id).exists() assert not list(tmp_path.glob("*.part")) def test_concurrent_download_and_delete_rejected(tmp_path, fixture_model): spec, _, _ = fixture_model manager = ModelManager(tmp_path) with manager._lock(spec.id): with pytest.raises(ModelError, match="другом окне"): ModelManager(tmp_path).download(spec.id) with pytest.raises(ModelError, match="другом окне"): ModelManager(tmp_path).delete(spec.id) def test_network_failure_preserves_old_model(tmp_path, fixture_model): spec, content, response = fixture_model manager = ModelManager(tmp_path) path = manager.path_for(spec.id) path.write_bytes(b"previous corrupt model") response.raise_for_status.side_effect = httpx.ConnectError("failed") with pytest.raises(ModelError): manager.download(spec.id) assert path.read_bytes() == b"previous corrupt model" assert not list(tmp_path.glob("*.part")) path.write_bytes(content) assert manager.download(spec.id) == path @pytest.mark.parametrize("model_id", ["../escape", "/tmp/escape", "unknown", "test/../../escape"]) def test_unknown_ids_rejected(tmp_path, fixture_model, model_id): with pytest.raises(ModelError): ModelManager(tmp_path).download(model_id) def test_symlink_rejected(tmp_path, fixture_model): target = tmp_path / "target" target.write_bytes(b"keep") (tmp_path / "test.bin").symlink_to(target) with pytest.raises(ModelError, match="ссылкой"): ModelManager(tmp_path).delete("test") assert target.read_bytes() == b"keep" def test_inference_lease_blocks_delete_and_releases(tmp_path, fixture_model): spec, content, _ = fixture_model manager = ModelManager(tmp_path) manager.path_for(spec.id).write_bytes(content) with manager.use(spec.id) as path: assert path.read_bytes() == content with pytest.raises(ModelError, match="другом окне"): ModelManager(tmp_path).delete(spec.id) manager.delete(spec.id) assert not manager.is_installed(spec.id) @pytest.mark.parametrize("oversize", [False, True]) def test_wrong_hash_and_oversize_rejected(tmp_path, fixture_model, oversize): spec, content, response = fixture_model response.iter_bytes.return_value = iter([b"x" * (len(content) + int(oversize))]) manager = ModelManager(tmp_path) with pytest.raises(ModelError): manager.download(spec.id) assert not manager.path_for(spec.id).exists() assert not list(tmp_path.glob("*.part")) def test_precancelled_download_makes_no_request(tmp_path, fixture_model, monkeypatch): spec, _, _ = fixture_model request = MagicMock() monkeypatch.setattr(module.httpx, "stream", request) cancel = threading.Event() cancel.set() with pytest.raises(DownloadCancelled): ModelManager(tmp_path).download(spec.id, cancel=cancel) request.assert_not_called() def test_partial_symlink_cannot_overwrite_target(tmp_path, fixture_model): spec, _, _ = fixture_model target = tmp_path / "target" target.write_bytes(b"keep") (tmp_path / "test.bin.part").symlink_to(target) with pytest.raises(ModelError, match="ссылкой"): ModelManager(tmp_path).download(spec.id) assert target.read_bytes() == b"keep"