okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
be32c7f3f
main
1"""The config snapshot test: for every machine class x model set x entry point, what the engine would be started with must equal the golden file. 2 3python -m pytest tests/snapshot -q (or: python -m unittest tests.snapshot.test_config_snapshot) 4STRATA_SNAPSHOT_UPDATE=1 python -m pytest tests/snapshot (rewrite the golden files after an intended change; the diff of golden/ is the review) 5 6No GPU, no model, no download, no network and no real user folder is needed (see tests/snapshot/README.md). The app entry points run the host's own code in Node, 7so `node` (24 or newer) must be installed; STRATA_SNAPSHOT_SKIP_APP=1 skips those two entries by name and the test says so. 8""" 9 10from __future__import annotations 11 12import difflib 13import json 14import os 15import shutil 16import sys 17import unittest 18from pathlib import Path 19 20ROOT = Path (__file__ ).resolve ().parents [2 ] 21if str (ROOT )not in sys .path : 22sys .path .insert (0 ,str (ROOT )) 23 24from tests .snapshot .driver import drive # noqa: E402 25from tests .snapshot .generate import GOLDEN ,dump # noqa: E402 26from tests .snapshot .matrix import ENTRY_POINTS ,MACHINES ,MODEL_SETS # noqa: E402 27 28UPDATE = os .environ .get ("STRATA_SNAPSHOT_UPDATE" )== "1" 29SKIP_APP = os .environ .get ("STRATA_SNAPSHOT_SKIP_APP" )== "1" 30APP_ENTRIES = ("app_plain" ,"app_models" ) 31 32 33def readable_diff (want :dict ,got :dict ,label :str )-> str : 34a = json .dumps (want ,indent = 1 ,ensure_ascii = False ).splitlines () 35b = json .dumps (got ,indent = 1 ,ensure_ascii = False ).splitlines () 36lines = list (difflib .unified_diff (a ,b ,"golden " + label ,"now " + label ,lineterm = "" ,n = 2 )) 37head = [f"changed top-level fields: { sorted ( k for k in set ( want ) | set ( got ) if want . get ( k ) != got . get ( k )) } " ] 38return "\n" .join (head + lines [:160 ]+ (["... (%d more diff lines)" % (len (lines )- 160 )]if len (lines )> 160 else [])) 39 40 41class ConfigSnapshot (unittest .TestCase ): 42maxDiff = None 43 44def check_cell (self ,machine_key :str ,set_key :str )-> None : 45path = GOLDEN / machine_key / f" { set_key } .json" 46want = json .loads (path .read_text (encoding = "utf-8" ))if path .is_file ()else None 47got_entries = {} 48for entry in ENTRY_POINTS : 49if entry in APP_ENTRIES and SKIP_APP : 50f"SKIPPED (STRATA_SNAPSHOT_SKIP_APP=1): { machine_key } / { set_key } / { entry } " ) 51continue 52if entry in APP_ENTRIES and not shutil .which (os .environ .get ("STRATA_SNAPSHOT_NODE" )or "node" ): 53self .fail ("node is not installed: the app entry points cannot be driven (install Node 24, or set STRATA_SNAPSHOT_SKIP_APP=1 to skip them by name)" ) 54got_entries [entry ]= drive (machine_key ,set_key ,entry ) 55if UPDATE : 56merged = dict (want ["entries" ])if want else {} 57merged .update (got_entries ) 58path .parent .mkdir (parents = True ,exist_ok = True ) 59path .write_text (dump ({"machine" :machine_key ,"set" :set_key ,"entries" : {e :merged [e ]for e in ENTRY_POINTS if e in merged }}),encoding = "utf-8" ,newline = "\n" ) 60return 61self .assertIsNotNone (want ,f"no golden file { path } : run with STRATA_SNAPSHOT_UPDATE=1 and review the new file" ) 62for entry ,got in got_entries .items (): 63with self .subTest (machine = machine_key ,set = set_key ,entry = entry ): 64self .assertNotIn ("failed" ,got ,f" { machine_key } / { set_key } / { entry } could not be driven: { got . get ( 'failed' ) } " ) 65if want ["entries" ].get (entry )!= got : 66self .fail (f"\n { machine_key } / { set_key } / { entry } : what the engine would be started with changed.\n" 67+ readable_diff (want ["entries" ].get (entry , {}),got ,f" { machine_key } / { set_key } / { entry } " ) 68+ "\nIf the change is intended: STRATA_SNAPSHOT_UPDATE=1 python -m pytest tests/snapshot, then review the golden/ diff." ) 69 70def test_every_golden_file_belongs_to_the_matrix (self ): 71have = {(p .parent .name ,p .stem )for p in GOLDEN .glob ("*/*.json" )} 72want = {(m .key ,s .key )for m in MACHINES for s in MODEL_SETS } 73self .assertEqual (sorted (have - want ), [],"golden files for a class or set that is no longer in the matrix" ) 74if not UPDATE : 75self .assertEqual (sorted (want - have ), [],"a class x set without a golden file" ) 76 77 78def _add (machine_key :str ,set_key :str )-> None : 79def test (self ): 80self .check_cell (machine_key ,set_key ) 81test .__name__ = f"test_ { machine_key } __ { set_key } " 82setattr (ConfigSnapshot ,test .__name__ ,test ) 83 84 85for _m in MACHINES : 86for _s in MODEL_SETS : 87_add (_m .key ,_s .key ) 88 89if __name__ == "__main__" : 90unittest .main ()