hefesto-widgets 0.7.2

Ratatui widgets for the Hefesto TUI toolkit: popups, scrollable lists, trees, text input and spinners
Documentation
use super::*;
use crate::scroll_list::ScrollListState;
use insta::assert_snapshot;
use ratatui::{backend::TestBackend, Terminal};

use crate::{Badge, BadgeAnchor, BadgeStack};

fn render_stateful(name: &str, popup: SpinPopup<'_>, state: &mut SpinState) {
    let backend = TestBackend::new(60, 20);
    let mut terminal = Terminal::new(backend).unwrap();
    terminal
        .draw(|f| f.render_stateful_widget(popup, f.area(), state))
        .unwrap();
    assert_snapshot!(name, terminal.backend());
}

// ── resolve_rect ──

#[test]
fn resolve_rect_delegates_to_popup() {
    let popup = SpinPopup::new()
        .width(PopupSize::Fixed(30))
        .height(PopupSize::Fixed(10));
    let state = SpinState::default();
    let rect = popup.resolve_rect(Rect::new(0, 0, 100, 50), &state);
    assert_eq!(rect.width, 30);
    assert_eq!(rect.height, 10);
}

#[test]
fn resolve_rect_clamps_when_larger_than_area() {
    let popup = SpinPopup::new()
        .width(PopupSize::Fixed(100))
        .height(PopupSize::Fixed(50));
    let state = SpinState::default();
    let rect = popup.resolve_rect(Rect::new(0, 0, 50, 20), &state);
    assert_eq!(rect.width, 50);
    assert_eq!(rect.height, 20);
}

#[test]
fn resolve_rect_auto_uses_auto_height() {
    let popup = SpinPopup::new();
    let state = SpinState::default();
    let auto_h = popup.auto_height(&state, Rect::new(0, 0, 100, 50));
    assert_eq!(auto_h, 5);
    let rect = popup.resolve_rect(Rect::new(0, 0, 100, 50), &state);
    assert_eq!(rect.height, 5);
}

#[test]
fn resolve_rect_auto_centers_correctly() {
    let popup = SpinPopup::new();
    let state = SpinState::default();
    let rect = popup.resolve_rect(Rect::new(0, 0, 100, 50), &state);
    // height = auto_height = 5, width = 80% of 100 = 80
    assert_eq!(rect.x, 10);
    assert_eq!(rect.y, 22); // (50 - 5) / 2
}

// ── snapshots ──

#[test]
fn snapshot_default() {
    let popup = SpinPopup::new();
    let mut state = SpinState::default();
    render_stateful("spin_popup_default", popup, &mut state);
}

#[test]
fn snapshot_with_title() {
    let popup = SpinPopup::new().title("Procesando archivos");
    let mut state = SpinState::default();
    render_stateful("spin_popup_with_title", popup, &mut state);
}

#[test]
fn snapshot_bg_color() {
    let popup = SpinPopup::new()
        .title("Procesando")
        .bg_color(Color::Blue);
    let mut state = SpinState::default();
    render_stateful("spin_popup_bg_color", popup, &mut state);
}

#[test]
fn snapshot_finished() {
    let popup = SpinPopup::new()
        .title("Completado");
    let mut state = SpinState {
        finished: true,
        exit_code: Some(0),
        frame: 0,
        scroll_list: ScrollListState::default(),
    };
    render_stateful("spin_popup_finished", popup, &mut state);
}

// ── auto_height minimums ──

#[test]
fn auto_height_min_5_when_idle() {
    let popup = SpinPopup::new();
    let state = SpinState::default();
    let h = popup.auto_height(&state, Rect::new(0, 0, 100, 50));
    assert_eq!(h, 5);
}

#[test]
fn auto_height_min_7_when_finished() {
    let popup = SpinPopup::new();
    let state = SpinState {
        finished: true,
        exit_code: Some(0),
        frame: 0,
        scroll_list: ScrollListState::default(),
    };
    let h = popup.auto_height(&state, Rect::new(0, 0, 100, 50));
    assert_eq!(h, 7);
}

#[test]
fn auto_height_min_5_when_finished_no_msg() {
    let popup = SpinPopup::new().no_finished_msg();
    let state = SpinState {
        finished: true,
        exit_code: Some(0),
        frame: 0,
        scroll_list: ScrollListState::default(),
    };
    let h = popup.auto_height(&state, Rect::new(0, 0, 100, 50));
    assert_eq!(h, 5);
}

#[test]
fn auto_height_with_command_adds_one() {
    let popup = SpinPopup::new().command("cargo build");
    let state = SpinState::default();
    let h = popup.auto_height(&state, Rect::new(0, 0, 100, 50));
    assert_eq!(h, 5);
}

#[test]
fn auto_height_with_output_adds_lines_plus_border() {
    let popup = SpinPopup::new().output_lines(&["line1", "line2"]);
    let state = SpinState::default();
    let h = popup.auto_height(&state, Rect::new(0, 0, 100, 50));
    assert_eq!(h, 7);
}

#[test]
fn auto_height_full() {
    let popup = SpinPopup::new()
        .title("Test")
        .command("echo hello")
        .output_lines(&["out1", "out2", "out3"]);
    let state = SpinState {
        finished: true,
        exit_code: Some(0),
        frame: 0,
        scroll_list: ScrollListState::default(),
    };
    let h = popup.auto_height(&state, Rect::new(0, 0, 100, 50));
    // spin_block(2) + command(1) + output(3+1) + footer(2) + borders(2) = 11
    assert_eq!(h, 11);
}

// ── badges integration ──

#[test]
fn snapshot_with_badges() {
    let badges = BadgeStack::new()
        .push(Badge::new(" main", Color::Green).anchor(BadgeAnchor::TopLeft));
    let popup = SpinPopup::new()
        .title("Building")
        .command("cargo build")
        .badges(badges);
    let mut state = SpinState::default();
    render_stateful("spin_with_badges", popup, &mut state);
}