okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd0e3766a
main
1"""Owner rule 2026-10-18: the Terminal never opens a browser by itself; only an explicit --open does (the monitor's b key is also a request). The generic 2start message promises a browser only when one will open.""" 3 4from __future__import annotations 5 6import contextlib 7import io 8import tempfile 9import threading 10import time 11import unittest 12from pathlib import Path 13from unittest import mock 14 15from strata_cli .launch import cmd ,settings as S 16from strata_cli .launch .runner import Result ,Runner ,derive_config 17from strata_control import tune_knobs 18from tests .terminal .launch_support import Home ,run_cli 19 20 21def served ()-> Result : 22return Result ("launch-x" , ["http://127.0.0.1:8081" ],None ,None , [],"/x" ) 23 24 25class NoBrowserByItself (unittest .TestCase ): 26def setUp (self ): 27self .home = Home ().__enter__ () 28self .addCleanup (self .home .__exit__ ,None ,None ,None ) 29 30def test_the_launch_config_forbids_a_browser_and_drops_open_from_the_args (self ): 31base = {"exe" :"e" ,"args" : ["--native" ,"x.gguf" ,"--open" ,"--max-context" ,"32768" ],"open_browser" :True } 32cfg = derive_config (base ,S .LaunchSettings (),bool_flags = tune_knobs .BOOL_FLAGS ) 33self .assertIs (cfg ["open_browser" ],False ) 34self .assertNotIn ("--open" ,cfg ["args" ]) 35self .assertEqual (cfg ["args" ][:2 ], ["--native" ,"x.gguf" ]) 36 37def test_a_plain_launch_opens_no_browser (self ): 38with mock .patch .object (Runner ,"run" ,return_value = served ()),mock .patch ("webbrowser.open" )as opened : 39code ,out ,err = run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ]) 40self .assertEqual (code ,0 ,err ) 41opened .assert_not_called () 42 43def test_open_opens_the_local_page_once (self ): 44with mock .patch .object (Runner ,"run" ,return_value = served ()),mock .patch ("webbrowser.open" ,return_value = True )as opened : 45code ,out ,err = run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ,"--open" ]) 46self .assertEqual (code ,0 ,err ) 47opened .assert_called_once_with ("http://127.0.0.1:8081" ) 48 49def test_the_screens_open_a_browser_only_for_open (self ): 50from strata_cli .launch import app as launch_app 51from tests .terminal .launch_support import HW_12 ,catalog 52for flag ,calls in ((False ,0 ), (True ,1 )): 53with mock .patch .object (launch_app .LaunchApp ,"run" ,lambda self ,** kw :served ()), \ 54mock .patch .object (launch_app ,"run_monitor" ,lambda info ,** kw :0 ),mock .patch ("webbrowser.open" ,return_value = True )as opened : 55with contextlib .redirect_stdout (io .StringIO ()): 56launch_app .run_tui (S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ,vision = "off" ),catalog = catalog (HW_12 ), 57auto_run = True ,open_page = flag ) 58self .assertEqual (opened .call_count ,calls ,flag ) 59 60def test_open_is_in_the_help (self ): 61code ,out ,err = run_cli (["launch" ,"--help" ]) 62self .assertIn ("--open" ,out ) 63self .assertIn ("nothing opens a browser without this" ,out .replace ("\n" ," " ).replace (" " ," " )) 64 65 66class StartMessage (unittest .TestCase ): 67def narrate (self ,opens :bool )-> str : 68from serve import server 69d = tempfile .mkdtemp () 70log = Path (d )/ "engine.log" 71log .write_text ("strata generate: expert arena: resident, 31.64 GiB\n" ,encoding = "utf-8" ) 72done ,out = threading .Event (),io .StringIO () 73with mock .patch .object (server ,"OPENS_BROWSER" ,opens ),contextlib .redirect_stdout (out ): 74t = threading .Thread (target = server .narrate_start ,args = (str (log ),0 , [],done )) 75t .start () 76time .sleep (0.8 ) 77done .set () 78t .join (5 ) 79return out .getvalue () 80 81def test_the_browser_is_promised_only_with_open (self ): 82self .assertNotIn ("browser" ,self .narrate (False )) 83self .assertIn ("the server says when it is ready" ,self .narrate (False )) 84self .assertIn ("the browser opens when it is ready" ,self .narrate (True )) 85 86 87if __name__ == "__main__" : 88unittest .main ()