okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cd9745050
main
1"""ModelScope as a download source (--source / STRATA_SOURCE): which source is used, the URL mapping, and that a file 2from ModelScope is checked against the SHA-256 ModelScope publishes (a wrong one is deleted). No network: a local 3HTTP server plays ModelScope. 4 5python -m unittest tools.test_setup_modelscope 6""" 7from __future__import annotations 8 9import hashlib 10import http .server 11import json 12import os 13import sys 14import tempfile 15import threading 16import unittest 17from pathlib import Path 18from unittest import mock 19 20ROOT = Path (__file__ ).resolve ().parents [1 ] 21sys .path .insert (0 ,str (ROOT )) 22import setup # noqa: E402 23 24REPO = "ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF" 25PATH = "Q2_0/Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00002-of-00002.gguf" 26HF_URL = setup .hf (REPO )+ PATH 27 28 29class Quiet (http .server .SimpleHTTPRequestHandler ): 30def log_message (self ,* a ): 31pass 32 33 34class Base (unittest .TestCase ): 35def setUp (self ): 36for name in ("say" ,"ok" ,"warn" ): 37p = mock .patch .object (setup ,name ,lambda * a ,** k :None ) 38p .start () 39self .addCleanup (p .stop ) 40env = mock .patch .dict (os .environ , {},clear = False ) 41env .start () 42self .addCleanup (env .stop ) 43for k in ("STRATA_SOURCE" ,"HF_ENDPOINT" ,"MODELSCOPE_ENDPOINT" ): 44os .environ .pop (k ,None ) 45setup ._sources .clear () 46setup ._ms_files .clear () 47self .addCleanup (setup ._sources .clear ) 48 49 50class Source (Base ): 51def test_explicit (self ): 52for value ,want in (("modelscope" ,"modelscope" ), ("ms" ,"modelscope" ), ("huggingface" ,"huggingface" ), 53 ("hf" ,"huggingface" )): 54setup ._sources .clear () 55os .environ ["STRATA_SOURCE" ]= value 56self .assertEqual (setup .model_source (),want ,value ) 57 58def test_auto_is_huggingface_and_never_asks_the_network (self ): 59"""#908: setup does not switch hosts by itself, whatever answers.""" 60with mock .patch .object (setup ,"reachable" ,side_effect = AssertionError ("no probe" )): 61self .assertEqual (setup .model_source (),"huggingface" ) 62 63def test_a_failed_download_recommends_modelscope (self ): 64self .assertIn ("--source modelscope" ,setup .source_hint (HF_URL )) 65self .assertEqual (setup .source_hint ("https://github.com/example/repo/archive/main.zip" ),"" ) 66 67def test_hf_endpoint_is_a_choice (self ): 68os .environ ["HF_ENDPOINT" ]= "https://hf-mirror.com" 69with mock .patch .object (setup ,"reachable" ,return_value = True )as r : 70self .assertEqual (setup .model_source (),"huggingface" ) 71r .assert_not_called () 72 73def test_mapping (self ): 74self .assertEqual (setup .ms_file (HF_URL ), (REPO ,PATH )) 75self .assertEqual (setup .ms_url (REPO ,PATH ), 76"https://www.modelscope.cn/models/" + REPO + "/resolve/master/" + PATH ) 77self .assertIsNone (setup .ms_file ("https://huggingface.co/someone/other/resolve/main/x.gguf" )) 78self .assertIsNone (setup .ms_file ("https://github.com/example/repo/archive/main.zip" )) 79 80 81class Download (Base ): 82"""download() through a local server laid out as ModelScope: /models/<repo>/resolve/master/<path> and 83/api/v1/models/<repo>/repo/files (the query string is ignored by the test server).""" 84 85def setUp (self ): 86super ().setUp () 87self .tmp = tempfile .TemporaryDirectory () 88self .addCleanup (self .tmp .cleanup ) 89self .www = Path (self .tmp .name )/ "www" 90self .data = os .urandom (300_000 ) 91f = self .www / "models" / REPO / "resolve" / "master" / PATH 92f .parent .mkdir (parents = True ) 93f .write_bytes (self .data ) 94self .server = http .server .ThreadingHTTPServer ( 95 ("127.0.0.1" ,0 ),lambda * a ,** k :Quiet (* a ,directory = str (self .www ),** k )) 96threading .Thread (target = self .server .serve_forever ,daemon = True ).start () 97self .addCleanup (self .server .server_close ) 98self .addCleanup (self .server .shutdown ) 99os .environ ["MODELSCOPE_ENDPOINT" ]= "http://127.0.0.1:%d" % self .server .server_address [1 ] 100os .environ ["STRATA_SOURCE" ]= "modelscope" 101self .dst = Path (self .tmp .name )/ "out" / Path (PATH ).name 102# a file with no pin of its own (the hash ModelScope publishes is the check); the pinned case is below 103pins = {** setup .REPO_PINS ,REPO : {k :v for k ,v in setup .REPO_PINS [REPO ].items ()if k != Path (PATH ).name }} 104patch = mock .patch .object (setup ,"REPO_PINS" ,pins ) 105patch .start () 106self .addCleanup (patch .stop ) 107 108def api (self ,sha ): 109p = self .www / "api" / "v1" / "models" / REPO / "repo" / "files" 110p .parent .mkdir (parents = True ,exist_ok = True ) 111p .write_text (json .dumps ({"Data" : {"Files" : [{"Path" :PATH ,"Size" :len (self .data ),"Sha256" :sha }]}})) 112 113def test_right_hash_is_kept (self ): 114sha = hashlib .sha256 (self .data ).hexdigest () 115self .api (sha ) 116setup .download (HF_URL ,self .dst ) 117self .assertEqual (self .dst .read_bytes (),self .data ) 118self .assertIn ("sha256 " + sha ,self .dst .with_name (self .dst .name + ".done" ).read_text ()) 119 120def test_wrong_hash_is_deleted (self ): 121self .api ("0" * 64 ) 122with mock .patch .object (setup ,"say" ),self .assertRaises (SystemExit ): 123setup .download (HF_URL ,self .dst ) 124self .assertFalse (self .dst .exists ()) 125self .assertFalse (self .dst .with_name (self .dst .name + ".done" ).exists ()) 126 127def test_no_hash_published (self ): 128setup .download (HF_URL ,self .dst )# no API file: the download is kept, as from Hugging Face 129self .assertEqual (self .dst .read_bytes (),self .data ) 130self .assertTrue (self .dst .with_name (self .dst .name + ".done" ).exists ()) 131 132def test_the_pin_decides_over_what_modelscope_publishes (self ): 133"""A pinned file is checked against its fixed SHA-256 whatever the host says: ModelScope serves current files.""" 134sha = hashlib .sha256 (self .data ).hexdigest () 135self .api (sha )# ModelScope vouches for its own copy ... 136pins = {** setup .REPO_PINS ,REPO : {** setup .REPO_PINS [REPO ],Path (PATH ).name : (len (self .data ),"1" * 64 )}} 137with mock .patch .object (setup ,"REPO_PINS" ,pins ),mock .patch .object (setup ,"say" ),self .assertRaises (SystemExit ):# ... but it is not the pinned file 138setup .download (HF_URL ,self .dst ) 139self .assertFalse (self .dst .exists ()) 140pins [REPO ][Path (PATH ).name ]= (len (self .data ),sha ) 141with mock .patch .object (setup ,"REPO_PINS" ,pins ): 142setup .download (HF_URL ,self .dst ) 143self .assertIn ("sha256 " + sha ,self .dst .with_name (self .dst .name + ".done" ).read_text ()) 144 145def test_falls_back_when_modelscope_is_silent (self ): 146calls = [] 147with mock .patch .object (setup ,"reachable" ,return_value = False ), \ 148mock .patch .object (setup .netpolicy ,"urlopen" , 149side_effect = lambda req ,timeout = 0 :calls .append (req .full_url )or (_ for _ in ()).throw ( 150OSError ("offline" ))), \ 151mock .patch .object (setup .time ,"sleep" ),self .assertRaises (SystemExit ): 152setup .download (HF_URL ,self .dst ) 153self .assertTrue (calls and all (u == HF_URL for u in calls ),calls [:2 ]) 154 155 156if __name__ == "__main__" : 157unittest .main ()