22
33import importlib
44import sys
5+ from collections .abc import Generator
56
67import pytest
78from rich .console import Console
89
910from aignostics_foundry_core .console import console
11+ from aignostics_foundry_core .foundry import FoundryContext , set_context
1012
1113EXPECTED_THEME_KEYS = ["success" , "info" , "warning" , "error" , "debug" , "logging.level.info" ]
14+ CUSTOM_ENV_PREFIX = "TESTPROJ_"
15+ CUSTOM_WIDTH = "120"
16+ EXPECTED_CUSTOM_WIDTH = 120
17+
18+
19+ @pytest .fixture (autouse = True )
20+ def reset_context (monkeypatch : pytest .MonkeyPatch ) -> Generator [None , None , None ]:
21+ """Reset global _context to None before and after every test."""
22+ monkeypatch .setattr ("aignostics_foundry_core.foundry._context" , None )
23+ yield
24+ monkeypatch .setattr ("aignostics_foundry_core.foundry._context" , None )
1225
1326
1427class TestConsole :
@@ -22,18 +35,32 @@ def test_console_is_console_instance(self) -> None:
2235 @pytest .mark .unit
2336 @pytest .mark .sequential
2437 def test_console_default_width (self , monkeypatch : pytest .MonkeyPatch ) -> None :
25- """Module-level console has Rich's default width (80) when env var is not set."""
26- monkeypatch .delenv ("AIGNOSTICS_CONSOLE_WIDTH" , raising = False )
38+ """Module-level console has Rich's default width (80) when no context is set."""
2739 reloaded = importlib .reload (sys .modules ["aignostics_foundry_core.console" ])
2840 assert reloaded .console .width == 80
2941
3042 @pytest .mark .unit
3143 @pytest .mark .sequential
32- def test_console_custom_width (self , monkeypatch : pytest .MonkeyPatch ) -> None :
33- """Module-level console uses width from AIGNOSTICS_CONSOLE_WIDTH env var."""
34- monkeypatch .setenv ("AIGNOSTICS_CONSOLE_WIDTH" , "100" )
44+ def test_console_width_uses_env_prefix_from_context (self , monkeypatch : pytest .MonkeyPatch ) -> None :
45+ """Console width is read from {env_prefix}CONSOLE_WIDTH when a context is set."""
46+ ctx = FoundryContext (
47+ name = "testproj" ,
48+ version = "0.0.0" ,
49+ version_full = "0.0.0" ,
50+ environment = "test" ,
51+ env_prefix = CUSTOM_ENV_PREFIX ,
52+ )
53+ set_context (ctx )
54+ monkeypatch .setenv (f"{ CUSTOM_ENV_PREFIX } CONSOLE_WIDTH" , CUSTOM_WIDTH )
3555 reloaded = importlib .reload (sys .modules ["aignostics_foundry_core.console" ])
36- assert reloaded .console .width == 100
56+ assert reloaded .console .width == EXPECTED_CUSTOM_WIDTH
57+
58+ @pytest .mark .unit
59+ @pytest .mark .sequential
60+ def test_console_width_is_auto_when_no_context (self , monkeypatch : pytest .MonkeyPatch ) -> None :
61+ """Console width defaults to Rich's auto-detection (80 in non-TTY) when no context is set."""
62+ reloaded = importlib .reload (sys .modules ["aignostics_foundry_core.console" ])
63+ assert reloaded .console .width == 80
3764
3865 @pytest .mark .unit
3966 def test_console_theme_contains_expected_keys (self ) -> None :
0 commit comments