okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1d0397652
main
1"""The monitor in the classic Windows console (Cascadia Mono switch, the solid look, the tip) and with several cards / CPUs.""" 2 3from __future__import annotations 4 5import io 6import os 7import time 8import unittest 9from unittest import mock 10 11from rich .console import Console 12 13from strata_cli import consoleinfo ,monitor ,overview 14from tests .terminal .test_overview import GIB ,metrics 15 16TIP = "For the full look, use Windows Terminal (built into Windows 11)" 17 18 19def draw (model ,width = 110 ,height = 40 ,** kw )-> str : 20c = Console (width = width + 2 ,record = True ,file = io .StringIO (),force_terminal = False ,legacy_windows = False ) 21c .overview .render (model ,width = width ,height = height ,connection = "http://127.0.0.1:8080" ,** kw )) 22return c .export_text () 23 24 25def three_cards (sockets = 2 ): 26m = metrics ("generating" ) 27cards = [{"index" :i ,"name" :"NVIDIA GeForce RTX 3090" ,"util" :u ,"mem_used" :used * GIB ,"mem_total" :24 * GIB ,"temp" :t ,"power" :p ,"power_limit" :350 } 28for i , (u ,used ,t ,p )in enumerate (((88 ,22.1 ,61 ,236 ), (71 ,21.4 ,58 ,201 ), (64 ,20.8 ,55 ,188 )))] 29m ["hardware" ]["gpus" ]= cards 30m ["hardware_static" ].update ({"gpu_name" :"NVIDIA GeForce RTX 3090" ,"gpu_count" :3 ,"cores" :64 ,"threads" :128 ,"sockets" :sockets ,"numa_nodes" :2 }) 31return m 32 33 34class WhichLook (unittest .TestCase ): 35def setUp (self ): 36consoleinfo .forget_style () 37self .addCleanup (consoleinfo .forget_style ) 38 39def test_decide_style_by_the_facts (self ): 40d = consoleinfo .decide_style (legacy = False ,cascadia_ok = False ,font = None ) 41self .assertEqual ((d ["mode" ],d ["tip" ]), ("none" ,None ))# Windows Terminal, Linux, macOS: nothing changes 42d = consoleinfo .decide_style (legacy = True ,cascadia_ok = True ,font = "Cascadia Mono" ) 43self .assertEqual ((d ["mode" ],d ["tip" ]), ("full" ,TIP ))# the font was switched: the full Unicode look, the tip anyway 44d = consoleinfo .decide_style (legacy = True ,cascadia_ok = False ,font = "Consolas" ) 45self .assertEqual ((d ["mode" ],d ["bullet" ],d ["tip" ]), ("solid" ,"●" ,TIP )) 46d = consoleinfo .decide_style (legacy = True ,cascadia_ok = False ,font = "Lucida Console" ) 47self .assertEqual ((d ["mode" ],d ["bullet" ]), ("solid" ,"•" ))# Lucida has no ● 48self .assertEqual (consoleinfo .decide_style (legacy = True ,cascadia_ok = False ,font = None )["bullet" ],"•" ) 49 50def test_classic_style_switches_the_font_once_when_cascadia_is_installed (self ): 51with mock .patch .object (consoleinfo ,"legacy_console" ,return_value = True ),mock .patch .object (consoleinfo .os ,"name" ,"nt" ), \ 52mock .patch .dict (os .environ , {"STRATA_GLYPHS" :"" }),mock .patch .object (consoleinfo ,"cascadia_installed" ,return_value = True ), \ 53mock .patch .object (consoleinfo ,"console_font" ,side_effect = ["Consolas" ,"Cascadia Mono" ]), \ 54mock .patch .object (consoleinfo ,"switch_font" ,return_value = True )as sw : 55s = consoleinfo .classic_style () 56self .assertEqual (s ["mode" ],"full" ) 57sw .assert_called_once_with ("Cascadia Mono" ) 58consoleinfo .classic_style () 59sw .assert_called_once ()# cached: the font API is not called again 60 61def test_without_cascadia_or_when_the_switch_fails_the_solid_look_with_the_font_it_has (self ): 62for installed ,switched in ((False ,False ), (True ,False )): 63consoleinfo .forget_style () 64with mock .patch .object (consoleinfo ,"legacy_console" ,return_value = True ),mock .patch .object (consoleinfo .os ,"name" ,"nt" ), \ 65mock .patch .dict (os .environ , {"STRATA_GLYPHS" :"" }),mock .patch .object (consoleinfo ,"cascadia_installed" ,return_value = installed ), \ 66mock .patch .object (consoleinfo ,"console_font" ,return_value = "Lucida Console" ), \ 67mock .patch .object (consoleinfo ,"switch_font" ,return_value = switched ): 68s = consoleinfo .classic_style () 69self .assertEqual ((s ["mode" ],s ["bullet" ]), ("solid" ,"•" )) 70 71def test_never_in_windows_terminal_linux_or_mac (self ): 72with mock .patch .object (consoleinfo ,"legacy_console" ,return_value = False ),mock .patch .object (consoleinfo ,"switch_font" )as sw : 73self .assertEqual (consoleinfo .classic_style ()["mode" ],"none" ) 74sw .assert_not_called () 75env = {k :v for k ,v in os .environ .items ()if k not in ("WT_SESSION" ,"TERM_PROGRAM" ,"STRATA_GLYPHS" )} 76env ["WT_SESSION" ]= "x" 77with mock .patch .dict (os .environ ,env ,clear = True ),mock .patch .object (consoleinfo .os ,"name" ,"nt" ),mock .patch .object (consoleinfo .sys ,"platform" ,"win32" ): 78self .assertFalse (consoleinfo .legacy_console ())# Windows Terminal 79with mock .patch .dict (os .environ , {},clear = True ),mock .patch .object (consoleinfo .os ,"name" ,"posix" ),mock .patch .object (consoleinfo .sys ,"platform" ,"linux" ): 80self .assertFalse (consoleinfo .legacy_console ()) 81with mock .patch .object (consoleinfo .os ,"name" ,"posix" ): 82self .assertIsNone (consoleinfo .console_font ()) 83self .assertFalse (consoleinfo .switch_font ()) 84self .assertFalse (consoleinfo .cascadia_installed ()) 85 86def test_choose_glyphs (self ): 87with mock .patch .object (consoleinfo ,"legacy_console" ,return_value = True ),mock .patch .object (consoleinfo ,"classic_style" ,return_value = {"mode" :"full" }): 88self .assertEqual (monitor .choose_glyphs ("auto" ,"utf-8" ,"xterm" ),"unicode" ) 89with mock .patch .object (consoleinfo ,"legacy_console" ,return_value = True ),mock .patch .object (consoleinfo ,"classic_style" ,return_value = {"mode" :"solid" }): 90self .assertEqual (monitor .choose_glyphs ("auto" ,"utf-8" ,None ),"solid" ) 91with mock .patch .dict (os .environ , {"STRATA_GLYPHS" :"ascii" }): 92self .assertEqual (monitor .choose_glyphs ("auto" ,"utf-8" ,None ),"ascii" )# forced ASCII stays ASCII 93self .assertEqual (monitor .choose_glyphs ("unicode" ,"utf-8" ,None ),"unicode" )# an explicit --glyphs wins 94with mock .patch .object (consoleinfo ,"legacy_console" ,return_value = False ): 95self .assertEqual (monitor .choose_glyphs ("auto" ,"utf-8" ,"xterm" ),"unicode" ) 96 97 98class SolidLook (unittest .TestCase ): 99def test_solid_blocks_only_no_dithered_shades_and_no_eighth_blocks (self ): 100text = draw (overview .build (metrics ("generating" )),glyphs = "solid" ) 101for ch in "░▒▓▁▂▃▅▆▇╭╮╰╯" : 102self .assertNotIn (ch ,text ,ch ) 103for ch in "█▄┌┐└┘" : 104self .assertIn (ch ,text ,ch ) 105 106def test_bars_use_a_thin_track_and_boxes_are_square (self ): 107bar = overview .bar (0.25 ,20 ,"solid" ,"bright_green" ) 108self .assertEqual (bar .plain ,"█" * 5 + "▄" * 15 ) 109self .assertEqual (overview .bar (None ,4 ,"solid" ,"x" ).plain ,"▄▄▄▄" ) 110self .assertEqual (overview .spark ([0 ,50 ,100 ],3 ,"solid" ,lo = 0 ,hi = 100 ).plain ,"▄▄█" ) 111 112def test_the_16_palette_names_not_hex_colours (self ): 113group = overview .render (overview .build (metrics ("generating" )),width = 110 ,height = 40 ,connection = "x" ,glyphs = "solid" ) 114c = Console (width = 112 ,record = True ,file = io .StringIO (),force_terminal = True ,color_system = "standard" ,legacy_windows = False ) 115c .group ) 116out = c .export_text (styles = True ) 117self .assertNotIn ("38;2;" ,out )# no 24-bit colours: conhost maps them to cyan or black 118 119def test_the_bullet_follows_the_font (self ): 120m = overview .build (metrics ("generating" )) 121self .assertIn ("● Generating" ,draw (m ,glyphs = "solid" ,bullet = "●" )) 122self .assertIn ("• Generating" ,draw (m ,glyphs = "solid" ,bullet = "•" )) 123 124def test_the_other_looks_are_unchanged (self ): 125m = overview .build (metrics ("generating" )) 126self .assertIn ("╭" ,draw (m ,glyphs = "unicode" )) 127self .assertNotIn ("╭" ,draw (m ,glyphs = "ascii" )) 128 129 130class SeveralCards (unittest .TestCase ): 131def test_one_card_looks_exactly_as_before (self ): 132base = metrics ("generating" ) 133one = metrics ("generating" ) 134one ["hardware" ]["gpus" ]= [{"index" :0 ,"name" :"NVIDIA GeForce RTX 5070" ,"util" :88 ,"mem_used" :11.2 * GIB ,"mem_total" :12 * GIB ,"temp" :54 ,"power" :114 }] 135for glyphs in ("unicode" ,"ascii" ,"solid" ): 136self .assertEqual (draw (overview .build (base ),glyphs = glyphs ,now = 1.0 ),draw (overview .build (one ),glyphs = glyphs ,now = 1.0 ),glyphs ) 137self .assertNotIn ("Cards" ,draw (overview .build (base ))) 138 139def test_the_tiles_say_it_for_the_whole_computer (self ): 140m = overview .build (three_cards ()) 141g = m ["gpu" ] 142self .assertEqual ((g ["util" ],g ["temp" ],g ["count" ]), (88.0 ,61.0 ,3.0 ))# the busiest, the hottest 143self .assertAlmostEqual (g ["vram_used" ],64.3 ,places = 1 ) 144self .assertEqual (g ["vram_total" ],72.0 )# all cards 145self .assertEqual ((g ["power" ],g ["power_limit" ]), (625.0 ,1050.0 )) 146text = draw (m ) 147for want in ("busiest of 3 cards" ,"all 3 cards" ,"hottest card" ,"64.3 / 72 GB" ,"625 W" ,"of 1,050 W limit" ,"61 °C" ): 148self .assertIn (want ,text ,want ) 149self .assertNotIn ("+2" ,text )# no more "RTX 3090 +2" 150 151def test_the_cards_strip_one_line_per_card (self ): 152text = draw (overview .build (three_cards ())) 153lines = [ln for ln in text .splitlines ()if ln .startswith ("GPU " )] 154self .assertEqual (len (lines ),3 ) 155self .assertIn ("Cards" ,text ) 156for want ,line in zip (("88 %" ,"71 %" ,"64 %" ),lines ): 157self .assertIn (want ,line ) 158self .assertIn ("22.1 / 24 GB" ,lines [0 ]) 159self .assertIn ("61 °C" ,lines [0 ]) 160self .assertIn ("236 W" ,lines [0 ]) 161self .assertIn ("RTX 3090" ,lines [0 ]) 162 163def test_up_to_four_cards_then_a_pointer_to_view_5 (self ): 164m = three_cards () 165m ["hardware" ]["gpus" ]+= [dict (m ["hardware" ]["gpus" ][0 ],index = i )for i in (3 ,4 ,5 )] 166text = draw (overview .build (m ),height = 44 ) 167self .assertEqual (len ([ln for ln in text .splitlines ()if ln .startswith ("GPU " )]),4 ) 168self .assertIn ("+ 2 more cards: press 5" ,text ) 169 170def test_the_cpu_tile_says_how_many_cpus (self ): 171self .assertIn ("2 CPUs · 64 cores" ,draw (overview .build (three_cards (sockets = 2 )))) 172one = draw (overview .build (three_cards (sockets = 1 ))) 173self .assertIn ("64 cores · 128 threads" ,one ) 174self .assertNotIn ("CPUs" ,one ) 175self .assertIn ("6 cores · 12 threads" ,draw (overview .build (metrics ("generating" ))))# no socket count reported: as before 176 177def test_a_missing_reading_stays_missing (self ): 178m = three_cards () 179for c in m ["hardware" ]["gpus" ]: 180c ["temp" ]= None 181c ["power" ]= None 182g = overview .build (m )["gpu" ] 183self .assertIsNone (g ["temp" ]) 184self .assertIsNone (g ["power" ]) 185self .assertIn ("-- °C" ,draw (overview .build (m ))) 186 187def test_it_fits_the_width_and_the_classic_looks_too (self ): 188for glyphs in ("unicode" ,"ascii" ,"solid" ): 189for ln in draw (overview .build (three_cards ()),glyphs = glyphs ,width = 100 ).splitlines (): 190self .assertLessEqual (len (ln .rstrip ()),102 , (glyphs ,ln )) 191 192 193class FooterAndViews (unittest .TestCase ): 194def screen_text (self ,classic ): 195import asyncio 196from tests .terminal .test_overview import Source 197 198async def go (): 199consoleinfo .forget_style () 200style = consoleinfo .decide_style (legacy = bool (classic ),cascadia_ok = classic == "full" ,font = "Consolas" ) 201with mock .patch .object (consoleinfo ,"legacy_console" ,return_value = bool (classic )),mock .patch .object (consoleinfo ,"classic_style" ,return_value = style ): 202app = monitor .MonitorApp (Source (three_cards ()),None ,interval = 30 ) 203async with app .run_test (size = (120 ,40 ))as pilot : 204await pilot .pause (0.5 ) 205return "\n" .join (s .text for s in app .screen ._compositor .render_strips ()) 206try : 207return asyncio .run (go ()) 208finally : 209consoleinfo .forget_style () 210 211def test_the_tip_is_in_the_classic_console_only (self ): 212self .assertIn (TIP ,self .screen_text ("solid" )) 213self .assertIn (TIP ,self .screen_text ("full" ))# also when the font was switched: one line, always 214self .assertNotIn (TIP ,self .screen_text (None ))# Windows Terminal, Linux, macOS 215 216def test_the_monitor_draws_the_several_cards_in_every_look (self ): 217for classic in ("solid" ,"full" ,None ): 218shown = self .screen_text (classic ) 219self .assertIn ("busiest of 3 cards" ,shown ,classic ) 220self .assertIn ("Cards" ,shown ,classic ) 221self .assertIn ("2 CPUs" ,shown ,classic ) 222 223def test_view_5_shows_the_whole_card_name (self ): 224from strata_cli .render import _device 225row = {"index" :1 ,"name" :"NVIDIA GeForce RTX 3090" ,"mem_used" :21.4 * GIB ,"mem_total" :24 * GIB ,"util" :71 ,"temp" :58 ,"power" :201 } 226line = _device (row ,100 ) 227self .assertIn ("RTX 3090" ,line ) 228self .assertNotIn ("NVIDIA" ,line ) 229 230 231if __name__ == "__main__" : 232unittest .main ()