File size: 3,477 Bytes
7f9dfed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

from models.model_catalog import load_model_catalog
from training.export import (
    ToolStatus,
    build_export_plan,
    detect_llama_cpp_tools,
    list_exported_files,
)
from ui.export_tab import export_download_paths


class ExportPlanTest(unittest.TestCase):
    def test_detects_llama_cpp_tools_without_requiring_install(self) -> None:
        statuses = detect_llama_cpp_tools(which_func=lambda name: f"C:/tools/{name}.exe")

        self.assertEqual(
            [status.name for status in statuses],
            ["llama-server", "llama-cli", "llama-quantize"],
        )
        self.assertTrue(all(status.available for status in statuses))

    def test_builds_non_executing_official_gguf_plan(self) -> None:
        catalog = load_model_catalog("config/models.yaml")
        tools = [ToolStatus("llama-quantize", False, "")]

        plan = build_export_plan(catalog["minicpm_v46"], "Q4_K_M", "exports", tools)
        data = plan.as_dict()

        self.assertEqual(data["official_gguf_repo"], "openbmb/MiniCPM-V-4.6-gguf")
        self.assertIn("huggingface-cli", data["download_command"])
        self.assertIn("llama-quantize", data["quantize_command"])
        self.assertFalse(data["executes_commands"])
        self.assertFalse(data["startup_downloads"])

    def test_rejects_unknown_quantization(self) -> None:
        catalog = load_model_catalog("config/models.yaml")

        with self.assertRaises(ValueError):
            build_export_plan(catalog["minicpm5_1b"], "Q2_UNKNOWN")

    def test_lists_exported_files(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            output = Path(tmp)
            file_path = output / "model.gguf"
            file_path.write_bytes(b"gguf")

            rows = list_exported_files(output)

            self.assertEqual(rows, [[str(file_path), "4"]])

    def test_export_download_paths_prioritizes_existing_planned_files(self) -> None:
        catalog = load_model_catalog("config/models.yaml")
        tools = [ToolStatus("llama-quantize", False, "")]

        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            model_dir = root / "minicpm_v46"
            model_dir.mkdir()
            extra_file = root / "notes.txt"
            extra_file.write_text("manual note", encoding="utf-8")

            plan = build_export_plan(catalog["minicpm_v46"], "Q4_K_M", root, tools)
            planned_file = model_dir / plan.official_gguf_file
            planned_file.write_bytes(b"planned gguf")

            rows = list_exported_files(root)
            paths = export_download_paths(plan.as_dict(), rows)

            self.assertEqual(paths[0], str(planned_file))
            self.assertIn(str(extra_file), paths)

    def test_export_download_paths_omits_missing_planned_files(self) -> None:
        catalog = load_model_catalog("config/models.yaml")
        tools = [ToolStatus("llama-quantize", False, "")]

        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            extra_file = root / "already-exported.gguf"
            extra_file.write_bytes(b"existing")

            plan = build_export_plan(catalog["minicpm_v46"], "Q4_K_M", root, tools)
            paths = export_download_paths(plan.as_dict(), list_exported_files(root))

            self.assertEqual(paths, [str(extra_file)])


if __name__ == "__main__":
    unittest.main()