84 lines
2.7 KiB
Python
84 lines
2.7 KiB
Python
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from lincodex_ml.model_files import (
|
|
LOCK_FILE_NAME,
|
|
ModelFilesError,
|
|
PinnedFile,
|
|
load_lock,
|
|
main,
|
|
verify_directory,
|
|
verify_file,
|
|
)
|
|
|
|
LOCK_PATH = Path(__file__).resolve().parents[1] / "model" / "giga-embeddings-480m.lock.json"
|
|
|
|
|
|
def _write_lock(path: Path, files: dict[str, object], **overrides: object) -> Path:
|
|
document = {
|
|
"id": "example/model",
|
|
"revision": "0" * 40,
|
|
"files": files,
|
|
**overrides,
|
|
}
|
|
path.write_text(json.dumps(document), encoding="utf-8")
|
|
return path
|
|
|
|
|
|
def test_the_checked_in_lock_pins_safetensors_and_no_code() -> None:
|
|
lock = load_lock(LOCK_PATH)
|
|
names = {pinned.name for pinned in lock.files}
|
|
assert lock.model_id == "ai-sage/Giga-Embeddings-instruct-480M-0826"
|
|
assert lock.revision == "1763d603adac8057bd6482a708001b0ed2e0a903"
|
|
assert "model.safetensors" in names
|
|
assert not any(name.endswith((".py", ".bin", ".pt", ".pkl")) for name in names)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("files", "overrides"),
|
|
[
|
|
({"model.bin": {"size": 1, "sha256": "0" * 64}}, {}),
|
|
({"modeling.py": {"size": 1, "sha256": "0" * 64}}, {}),
|
|
({"../escape": {"size": 1, "sha256": "0" * 64}}, {}),
|
|
({"config.json": {"size": 1, "sha256": "short"}}, {}),
|
|
({"config.json": {"size": 1, "sha256": "0" * 64}}, {"revision": "main"}),
|
|
({}, {}),
|
|
],
|
|
)
|
|
def test_lock_rejects_unsafe_or_unpinned_entries(
|
|
tmp_path: Path, files: dict[str, object], overrides: dict[str, object]
|
|
) -> None:
|
|
with pytest.raises(ModelFilesError):
|
|
load_lock(_write_lock(tmp_path / "lock.json", files, **overrides))
|
|
|
|
|
|
def test_verification_detects_size_and_digest_changes(tmp_path: Path) -> None:
|
|
content = b"weights"
|
|
target = tmp_path / "model.safetensors"
|
|
target.write_bytes(content)
|
|
pinned = PinnedFile("model.safetensors", len(content), hashlib.sha256(content).hexdigest())
|
|
verify_file(target, pinned)
|
|
|
|
target.write_bytes(b"weightz")
|
|
with pytest.raises(ModelFilesError):
|
|
verify_file(target, pinned)
|
|
target.write_bytes(b"longer weights")
|
|
with pytest.raises(ModelFilesError):
|
|
verify_file(target, pinned)
|
|
|
|
|
|
def test_verify_command_checks_a_directory(tmp_path: Path) -> None:
|
|
content = b"{}"
|
|
(tmp_path / "config.json").write_bytes(content)
|
|
lock = _write_lock(
|
|
tmp_path / LOCK_FILE_NAME,
|
|
{"config.json": {"size": len(content), "sha256": hashlib.sha256(content).hexdigest()}},
|
|
)
|
|
verify_directory(load_lock(lock), tmp_path)
|
|
assert main(["verify", "--lock", str(lock), "--dir", str(tmp_path)]) == 0
|
|
(tmp_path / "config.json").write_bytes(b"[]")
|
|
assert main(["verify", "--lock", str(lock), "--dir", str(tmp_path)]) == 1
|