tests/tui_spec: remove the NO_COLOR env race behind the flaky suite #164

Merged
sirius merged 2 commits from onionwire/t_75caf868-onionwire-tui-tests-no_color-env-race-ma into main 2026-09-27 18:08:48 +00:00
2 changed files with 34 additions and 13 deletions

View file

@ -56,8 +56,17 @@ fn ellipsis() -> &'static str {
/// Cells, not chars: the message can carry a peer-supplied transport error, and /// Cells, not chars: the message can carry a peer-supplied transport error, and
/// a CJK/emoji byte there must not push the alert into the next pane. /// a CJK/emoji byte there must not push the alert into the next pane.
pub fn alert_line(msg: &str, width: u16) -> String { pub fn alert_line(msg: &str, width: u16) -> String {
alert_line_for_mode(color_mode(), msg, width)
}
/// [`alert_line`] with the colour mode supplied by the caller.
///
/// Tests pin the `! ` ornament of the no-colour path through this entry point
/// rather than flipping `NO_COLOR`, which is process-global and mutated
/// concurrently by other tests in the same binary.
pub fn alert_line_for_mode(mode: ColorMode, msg: &str, width: u16) -> String {
let mut text = msg.to_string(); let mut text = msg.to_string();
if color_mode() == ColorMode::None && !text.starts_with("! ") { if mode == ColorMode::None && !text.starts_with("! ") {
text = format!("! {text}"); text = format!("! {text}");
} }
fit_cells(&text, width as usize) fit_cells(&text, width as usize)

View file

@ -833,7 +833,7 @@ async fn renders_a_queued_row_from_a_real_outbox_send() {
// Colour token set (card t_49b7c2b1) // Colour token set (card t_49b7c2b1)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
use onionwire::tui::{ColorMode, tokens_for}; use onionwire::tui::{ColorMode, alert_line_for_mode, tokens_for};
use ratatui::style::{Color, Modifier}; use ratatui::style::{Color, Modifier};
#[test] #[test]
@ -910,17 +910,24 @@ fn tokens_for_is_deterministic_across_calls() {
#[test] #[test]
fn alert_line_prefixes_when_no_color() { fn alert_line_prefixes_when_no_color() {
// NO_COLOR → ColorMode::None → alert_line prepends "! " // ColorMode::None (NO_COLOR) → alert_line prepends "! ". The mode is
let _guard = std::env::var_os("NO_COLOR"); // injected rather than read from the process env: other tests in this
unsafe { std::env::set_var("NO_COLOR", "1") }; // binary flip NO_COLOR concurrently, which made the env version flaky.
let line = alert_line("peer down", 80); let plain = alert_line_for_mode(ColorMode::None, "peer down", 80);
assert!(line.starts_with("! "), "expected '! ' prefix in None mode: {line:?}"); assert!(
unsafe { plain.starts_with("! "),
match _guard { "expected '! ' prefix in None mode: {plain:?}"
Some(v) => std::env::set_var("NO_COLOR", v), );
None => std::env::remove_var("NO_COLOR"), // The ornament belongs to the None path only; RGB keeps the message verbatim.
} assert_eq!(
}; alert_line_for_mode(ColorMode::Rgb, "peer down", 80),
"peer down"
);
// An alert that already carries the ornament is not double-prefixed.
assert_eq!(
alert_line_for_mode(ColorMode::None, "! peer down", 80),
"! peer down"
);
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -974,6 +981,11 @@ fn render_menu(w: u16, h: u16, st: &WalletMenuState) -> String {
#[test] #[test]
fn wallets_settings_menu_frame_shows_the_shipped_footer() { fn wallets_settings_menu_frame_shows_the_shipped_footer() {
// Both sides of the assertion read the process environment through
// `ascii_chrome()` / `color_mode()` (glyphs and alert ornaments), so this
// test must serialise with the other env-mutating tests in this binary.
let _guard = ASCII_ENV.lock().expect("ascii env lock");
force_unicode_mode();
// The app renders this screen's one status line with `status_footer_phased` // The app renders this screen's one status line with `status_footer_phased`
// (the chrome redesign put the Tor phase in it) and the hint from // (the chrome redesign put the Tor phase in it) and the hint from
// `footer_hints`; the fixture has to do the same or every menu frame in the // `footer_hints`; the fixture has to do the same or every menu frame in the