okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
538acd098
main
1"""#3570: `strata launch --show-key` printed a key only for a launch (a running one or a preset). A model set up by setup.py keeps its key in 2strata-<model>.json, so there the command said "no key" and people read the JSON by hand. It now reads that file when no launch has one.""" 3from __future__import annotations 4 5import contextlib 6import io 7import json 8import tempfile 9import unittest 10from pathlib import Path 11from unittest import mock 12 13from strata_cli import cli 14from strata_cli .launch import cmd as launch_cmd 15 16 17class ShowKeyOfSetupConfigs (unittest .TestCase ): 18def setUp (self ): 19tmp = tempfile .TemporaryDirectory () 20self .addCleanup (tmp .cleanup ) 21self .root = Path (tmp .name ) 22 23def config (self ,model_id ,key = "__none__" ,** extra ): 24path = self .root / f"strata- { model_id } .json" 25body = {"exe" :"x" ,"args" : [],** extra } 26if key != "__none__" : 27body ["api_key" ]= key 28path .write_text (json .dumps (body ),encoding = "utf-8" ) 29return {"id" :model_id ,"source_config" :str (path )} 30 31def run_cli (self ,entries ,* argv ,launch = None ): 32out ,err = io .StringIO (),io .StringIO () 33with contextlib .redirect_stdout (out ),contextlib .redirect_stderr (err ), \ 34mock .patch .object (launch_cmd ,"launch_info" ,return_value = launch ), \ 35mock .patch ("strata_control.models.list_installed" ,return_value = {"models" :entries }): 36code = cli .main (["launch" ,"--show-key" ,* argv ]) 37return code ,out .getvalue ().strip (),err .getvalue () 38 39def test_the_only_model_with_a_key_is_shown (self ): 40code ,out ,err = self .run_cli ([self .config ("coder-iq1_m" ,"abc123-secret" ),self .config ("qwen-q2_0" )]) 41self .assertEqual ((code ,out ), (0 ,"abc123-secret" )) 42self .assertIn ("coder-iq1_m" ,err )# the hint is on stderr: stdout stays the key alone, for scripts 43 44def test_a_launch_that_has_a_key_comes_first (self ): 45code ,out ,_ = self .run_cli ([self .config ("coder-iq1_m" ,"from-the-file" )],launch = {"key" :"from-the-launch" }) 46self .assertEqual ((code ,out ), (0 ,"from-the-launch" )) 47 48def test_several_models_need_a_name_and_the_name_picks_one_in_any_case (self ): 49entries = [self .config ("coder-iq1_m" ,"key-one" ),self .config ("qwen-iq3_xxs" ,"key-two" )] 50code ,out ,err = self .run_cli (entries ) 51self .assertEqual (code ,5 ) 52self .assertEqual (out ,"" ) 53self .assertIn ("coder-iq1_m" ,err ) 54self .assertIn ("--model NAME" ,err ) 55self .assertNotIn ("key-one" ,err + out )# no key is printed while the choice is open 56code ,out ,_ = self .run_cli (entries ,"--model" ,"IQ3_XXS" ) 57self .assertEqual ((code ,out ), (0 ,"key-two" )) 58 59def test_no_key_anywhere_and_a_name_that_matches_nothing_are_told_plainly (self ): 60code ,out ,err = self .run_cli ([self .config ("qwen-q2_0" )]) 61self .assertEqual ((code ,out ), (5 ,"" )) 62self .assertIn ("There is no API key" ,err ) 63code ,out ,err = self .run_cli ([self .config ("qwen-q2_0" ,"none" )],"--model" ,"iq1_m" ) 64self .assertEqual (code ,5 ) 65self .assertIn ("iq1_m" ,err ) 66 67def test_a_preset_name_never_falls_back_to_a_setup_config (self ): 68code ,out ,err = self .run_cli ([self .config ("coder-iq1_m" ,"abc123-secret" )],"--preset" ,"mine" ) 69self .assertEqual ((code ,out ), (5 ,"" )) 70self .assertNotIn ("abc123" ,err ) 71 72 73if __name__ == "__main__" : 74unittest .main ()