"""Tests for GGUF utilities""" import os import subprocess from subprocess import CompletedProcess import pytest from src.gguf_utils import ( get_llama_gguf_split_path, calculate_optimal_split_size, split_gguf_file, ) class TestGetLlamaGgufSplitPath: """Tests for get_llama_gguf_split_path function""" def test_finds_binary(self): path = get_llama_gguf_split_path() assert path is not None assert os.path.isfile(path) assert os.access(path, os.X_OK) def test_binary_runs_help(self): path = get_llama_gguf_split_path() result = subprocess.run([path, "--help"], capture_output=True, text=True) assert result.returncode == 0 assert "split" in result.stdout.lower() or "usage" in result.stdout.lower() class TestCalculateOptimalSplitSize: """Tests for calculate_optimal_split_size function""" def test_returns_positive_integer(self, monkeypatch, temp_dir): gguf_path = temp_dir / "test.gguf" gguf_path.write_bytes(b"GGUF") def _mock_run(cmd, capture_output, text, timeout): return CompletedProcess(args=cmd, returncode=0, stdout="total_size = 50M\n", stderr="") monkeypatch.setattr(subprocess, "run", _mock_run) output_prefix = temp_dir / "split" max_size_mb = calculate_optimal_split_size(str(gguf_path), str(output_prefix)) assert isinstance(max_size_mb, int) assert max_size_mb > 0 def test_fallback_on_invalid_file(self, temp_dir): invalid_path = temp_dir / "nonexistent.gguf" output_prefix = temp_dir / "split" max_size_mb = calculate_optimal_split_size(str(invalid_path), str(output_prefix)) assert max_size_mb == 256 class TestSplitGgufFile: """Tests for split_gguf_file function""" def test_splits_file_successfully(self, monkeypatch, temp_dir): gguf_path = temp_dir / "test.gguf" gguf_path.write_bytes(b"GGUF") def _mock_run(cmd, capture_output, text, timeout=None): if "--dry-run" in cmd: return CompletedProcess(args=cmd, returncode=0, stdout="total_size = 50M\n", stderr="") output_pattern = cmd[-1] open(f"{output_pattern}a.gguf", "wb").write(b"GGUF") open(f"{output_pattern}b.gguf", "wb").write(b"GGUF") return CompletedProcess(args=cmd, returncode=0, stdout="", stderr="") monkeypatch.setattr(subprocess, "run", _mock_run) output_prefix = temp_dir / "split" max_size_mb = calculate_optimal_split_size(str(gguf_path), str(output_prefix)) output_pattern = temp_dir / "split-" success = split_gguf_file(str(gguf_path), str(output_pattern), max_size_mb) assert success is True split_files = list(temp_dir.glob("split-*.gguf")) assert len(split_files) >= 2 def test_returns_false_on_invalid_file(self, temp_dir): invalid_path = temp_dir / "nonexistent.gguf" output_pattern = temp_dir / "split-" success = split_gguf_file(str(invalid_path), str(output_pattern), 50) assert success is False