"""Tests for tools/glm_draft_fetch.py: the draft block is found in a GGUF's tensor directory, only its bytes are read, and what is written is a GGUF of its own with the tensors as the source has them. A fake GGUF in memory - nothing is downloaded. python -m unittest tools.test_glm_draft_fetch """ from __future__ import annotations 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 glm_draft_fetch as G # noqa: E402 def text(s): b = s.encode() return struct.pack(" 0: # a > 0: not a directory read self.assertFalse(any(a <= hi and lo <= b for lo, hi in other), (a, b)) self.assertEqual([r for r in self.read if r[0] == "repo/1.gguf" and r[1] > 0], []) def test_a_fetch_that_stopped_goes_on(self): self.fetch() with open(self.out, "rb") as f: whole = f.read() os.remove(self.out) with open(self.out + ".part", "wb") as f: f.write(whole[:len(whole) - 300]) # stopped inside the last tensor but one self.read.clear() self.fetch() with open(self.out, "rb") as f: self.assertEqual(f.read(), whole) gate = [r for r in self.read if r[0] == "repo/3.gguf" and r[1] > 0 and r[2] - r[1] == 255] self.assertLess(len(gate), 3) # the first pieces of the large tensor were kept def test_a_part_of_another_block_starts_over(self): os.makedirs(os.path.dirname(self.out)) with open(self.out + ".part", "wb") as f: f.write(b"\x09" * 2000) self.fetch() self.assertEqual(self.block()["blk.1.nextn.eh_proj.weight"], ([8, 2], PROJ)) def test_a_wrong_hash_is_refused(self): with mock.patch.multiple(G, SHA256="0" * 64, REVISION=G.PINNED_REVISION), self.assertRaises(SystemExit): self.fetch() self.assertFalse(os.path.exists(self.out)) self.assertFalse(os.path.exists(self.out + ".part")) def test_no_block_anywhere(self): self.files["repo/3.gguf"] = PLAIN with self.assertRaises(SystemExit): self.fetch() class Directory(unittest.TestCase): def test_a_directory_cut_short_asks_for_more(self): _, start, _ = G.directory(SOURCE) for n in (3, 30, 120, start - 40): with self.assertRaises(G.Short): G.directory(SOURCE[:n]) def test_not_a_gguf(self): with self.assertRaises(ValueError): G.directory(b"PK\x03\x04" + b"\0" * 64) def test_verify(self): with tempfile.TemporaryDirectory() as d: path = os.path.join(d, "x.gguf") with mock.patch("builtins.print"), mock.patch.object(G, "SHA256", None): self.assertEqual(G.verify(path), G.BAD) # missing with open(path, "wb") as f: f.write(PLAIN) self.assertEqual(G.verify(path), G.BAD) # a GGUF without the block with open(path, "wb") as f: f.write(SOURCE) self.assertEqual(G.verify(path), 0) with mock.patch("builtins.print"), mock.patch.object(G, "REVISION", G.PINNED_REVISION): self.assertEqual(G.verify(path), G.BAD) # not the pinned revision's bytes def test_the_block_is_pinned(self): self.assertRegex(G.SHA256, "^[0-9a-f]{64}$") if __name__ == "__main__": unittest.main()