"""Tests for tools/mtp_fetch.py (#327): a range read needs a 206 with the asked Content-Range, the pinned revision's tensors are checked against their sha256 (a wrong one is fetched again; a corrupt install is found by `verify` and setup fetches it again), and an inventory from another revision is read again. A fake checkpoint behind a mocked urlopen - nothing is downloaded. python -m unittest tools.test_mtp_fetch """ from __future__ import annotations import contextlib import hashlib import io import json import os import struct import sys import tempfile import unittest from pathlib import Path from unittest import mock ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) sys.path.insert(0, str(ROOT / "tools")) import mtp_fetch as M # noqa: E402 def shard(tensors): """A safetensors file: [u64 header length][JSON header][data].""" header, data = {}, b"" for name, blob in tensors: header[name] = {"dtype": "BF16", "shape": [len(blob) // 2], "data_offsets": [len(data), len(data) + len(blob)]} data += blob h = json.dumps(header).encode() return struct.pack("