okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
049ae1c67
main
1"""Round 0213 TUICFG, step 1: profile identity and keys (#4395, #4396). 2 3Real temporary profile files (isolated home); only the runtime status is a double. A damaged profile must neither throw nor lend its 4neighbour's endpoint/config/key, in either order.""" 5from __future__import annotations 6 7import json 8import sys 9import unittest 10from pathlib import Path 11from unittest import mock 12 13ROOT = Path (__file__ ).resolve ().parents [2 ] 14sys .path .insert (0 ,str (ROOT )) 15 16from tests .terminal .launch_support import Home # noqa: E402 17 18 19def _cfg (home :Home ,name :str ,key :str )-> Path : 20path = home .base / f" { name } .run.json" 21path .write_text (json .dumps ({"exe" :str (home .base / "engine.exe" ),"tokenizer" :str (home .base / "tok.json" ),"args" : [], 22"api_key" :key ,"model_name" :name }),encoding = "utf-8" ) 23return path 24 25 26def _profile (home :Home ,pid :str ,cfg :Path | None ,port :int | None = None ,host :str | None = None )-> None : 27d = Path (home .env ["STRATA_PROFILE_DIR" ]) 28d .mkdir (parents = True ,exist_ok = True ) 29ov = {} 30if port is not None : 31ov ["port" ]= port 32if host is not None : 33ov ["host" ]= host 34 (d / f" { pid } .json" ).write_text (json .dumps ({"schema_version" :1 ,"id" :pid ,"source_config" :str (cfg ),"overrides" :ov }),encoding = "utf-8" ) 35 36 37def _broken (home :Home ,pid :str ,how :str = "garbage" )-> None : 38d = Path (home .env ["STRATA_PROFILE_DIR" ]) 39d .mkdir (parents = True ,exist_ok = True ) 40 (d / f" { pid } .json" ).write_text ("{not json" if how == "garbage" else json .dumps ({"schema_version" :1 ,"id" :pid }),encoding = "utf-8" ) 41 42 43class RunningLaunchesBrokenProfile (unittest .TestCase ): 44"""#4395: prof was read outside the try that binds it.""" 45 46def _rows (self ,first_broken :bool ): 47from strata_cli .launch import cmd 48with Home ()as home : 49good_cfg = _cfg (home ,"good" ,"sk-strata-GOOD" ) 50if first_broken : 51_broken (home ,"launch-a-broken" ) 52_profile (home ,"launch-b-good" ,good_cfg ,port = 18123 ,host = "0.0.0.0" ) 53else : 54_profile (home ,"launch-a-good" ,good_cfg ,port = 18123 ,host = "0.0.0.0" ) 55_broken (home ,"launch-b-broken" ) 56with mock .patch ("strata_control.runtime.status" ,return_value = {"state" :"ready" }): 57return cmd .running_launches () 58 59def test_broken_first_does_not_throw_and_good_row_is_intact (self ): 60rows = self ._rows (first_broken = True ) 61self .assertEqual (len (rows ),2 ) 62bad = next (r for r in rows if r ["name" ].endswith ("broken" )) 63good = next (r for r in rows if r ["name" ].endswith ("good" )) 64self .assertIsNone (bad ["config" ]) 65self .assertEqual (good ["port" ],18123 ) 66self .assertTrue (good ["config" ].endswith ("good.run.json" )) 67 68def test_broken_after_valid_never_shows_the_previous_rows_endpoint_or_config (self ): 69rows = self ._rows (first_broken = False ) 70self .assertEqual (len (rows ),2 ) 71bad = next (r for r in rows if r ["name" ].endswith ("broken" )) 72self .assertIsNone (bad ["config" ]) 73self .assertNotEqual (bad ["port" ],18123 ) 74self .assertEqual (bad ["host" ],"127.0.0.1" ) 75 76def test_connect_info_never_returns_another_profiles_key (self ): 77from strata_cli .launch import cmd 78for first_broken in (True ,False ): 79with self .subTest (first_broken = first_broken ),Home ()as home : 80good_cfg = _cfg (home ,"good" ,"sk-strata-GOOD" ) 81bad_id ,good_id = ("launch-a-broken" ,"launch-b-good" )if first_broken else ("launch-b-broken" ,"launch-a-good" ) 82_broken (home ,bad_id ) 83_profile (home ,good_id ,good_cfg ,port = 18123 ) 84with mock .patch ("strata_control.runtime.status" ,return_value = {"state" :"ready" }): 85bad = cmd .launch_info (bad_id [7 :]) 86good = cmd .launch_info (good_id [7 :]) 87self .assertIsNone (bad ["key" ]) 88self .assertNotEqual (bad ["port" ],18123 ) 89self .assertEqual (good ["key" ],"sk-strata-GOOD" ) 90self .assertEqual (good ["port" ],18123 ) 91 92 93class KeptKeyBrokenProfile (unittest .TestCase ): 94"""#4396: one unreadable profile must not make kept_key() mint a new key.""" 95 96def test_valid_key_survives_a_damaged_profile_in_either_order (self ): 97from strata_cli .launch import access 98for order in ("broken-first" ,"broken-last" ): 99for how in ("garbage" ,"missing-fields" ): 100with self .subTest (order = order ,how = how ),Home ()as home : 101cfg = _cfg (home ,"ok" ,"sk-strata-KEEP" ) 102if order == "broken-first" : 103_broken (home ,"launch-a-bad" ,how ) 104_profile (home ,"launch-b-ok" ,cfg ) 105else : 106_profile (home ,"launch-a-ok" ,cfg ) 107_broken (home ,"launch-b-bad" ,how ) 108self .assertEqual (access .kept_key (),"sk-strata-KEEP" ) 109 110def test_the_named_launch_with_a_damaged_profile_falls_back_to_the_other_valid_key (self ): 111from strata_cli .launch import access 112with Home ()as home : 113cfg = _cfg (home ,"ok" ,"sk-strata-KEEP" ) 114_broken (home ,"launch-mine" ) 115_profile (home ,"launch-other" ,cfg ) 116self .assertEqual (access .kept_key ("mine" ),"sk-strata-KEEP" ) 117 118def test_a_config_row_without_a_source_is_skipped_not_fatal (self ): 119from strata_cli .launch import access 120with Home ()as home : 121cfg = _cfg (home ,"ok" ,"sk-strata-KEEP" ) 122_profile (home ,"launch-ok" ,cfg ) 123rows = [{"id" :"launch-err" ,"error" :"cannot read profile (PermissionError)" }, {"id" :"launch-ok" ,"source_config" :str (cfg )}] 124with mock .patch ("strata_control.profiles.list_profiles" ,return_value = rows ): 125self .assertEqual (access .kept_key (),"sk-strata-KEEP" ) 126 127def test_no_valid_profile_still_returns_none (self ): 128from strata_cli .launch import access 129with Home ()as home : 130_broken (home ,"launch-only" ) 131self .assertIsNone (access .kept_key ()) 132 133 134if __name__ == "__main__" : 135unittest .main ()