makeover-tui 0.65.0

The terminal renderer for makeover-layout, on ratatui. Colour stops being the constraint above 256 entries; geometry never does, because an edge occupies a whole cell on every side.
Documentation
//! Tests for [`super`].

use super::*;
use std::time::Duration;

/// The style the drawings are read against: one distinguishable modifier
/// per role, so a test can say which style landed without a colour.
fn style() -> Ink {
    let mut ink = plain();
    ink.content = Style::new().add_modifier(Modifier::BOLD);
    ink.secondary = Style::new().add_modifier(Modifier::ITALIC);
    ink.muted = Style::new().add_modifier(Modifier::DIM);
    ink.danger = Style::new().add_modifier(Modifier::CROSSED_OUT);
    ink
}

/// The ink a palette with no tones gives: a terminal with no colour to spend.
fn plain() -> Ink {
    Ink::new(&bare_palette(), &PieceStyle::default())
}

fn bare_palette() -> Palette {
    Palette::new(
        Color::Reset,
        Color::Reset,
        Color::Reset,
        Color::Reset,
        Color::Reset,
        crate::Fidelity::TrueColor,
    )
}

#[test]
fn a_badge_is_round_and_a_chip_is_square() {
    // The one affordance a cell has left once colour is spent on the tone,
    // and the whole of how a terminal says "this one answers a press".
    let style = style();
    let badge = token("draft", Token::Badge, Tone::Neutral, false, false, &style);
    assert_eq!(badge.to_string(), "(draft)");
    let chip = token(
        "rust",
        Token::Chip { removable: false },
        Tone::Neutral,
        false,
        false,
        &style,
    );
    assert_eq!(chip.to_string(), "[rust]");
}

#[test]
fn a_filled_badge_is_an_edge_its_label_and_an_edge_in_the_same_two_cells() {
    // The table model's chip in cells. The label takes the fill and the ink;
    // each edge takes the fill behind the tone, so the glyph's empty half is
    // the badge's own ground. Two cells, the same as the parentheses, so a
    // terminal that gains the fill moves nothing beside the badge.
    let palette = bare_palette().tones(
        crate::Tones::NONE
            .content(Color::White)
            .danger(Color::LightRed)
            .danger_surface(Color::Red),
    );
    let marks = PieceStyle {
        badge_edges: ["<", ">"],
        ..PieceStyle::default()
    };
    let style = Ink::new(&palette, &marks);
    let danger = style.badge(Tone::Danger).expect("the palette fills danger");
    assert_eq!(danger.fill, Style::new().fg(Color::White).bg(Color::Red));
    let badge = token("failed", Token::Badge, Tone::Danger, false, false, &style);
    assert_eq!(badge.to_string(), "<failed>");
    assert_eq!(badge.width(), "(failed)".len());
    assert_eq!(badge.spans[1].style, danger.fill);
    assert_eq!(badge.spans[0].style.fg, Some(Color::LightRed));
    assert_eq!(badge.spans[0].style.bg, Some(Color::Red));
    assert_eq!(badge.spans[2].style, badge.spans[0].style);

    // A chip is not a badge and keeps its bracket.
    let chip = token(
        "rust",
        Token::Chip { removable: true },
        Tone::Danger,
        false,
        false,
        &style,
    );
    assert_eq!(chip.to_string(), "[rust]");
}

#[test]
fn a_latched_chip_reads_the_same_as_a_focused_one() {
    // The collision a terminal cannot avoid, asserted rather than left to
    // be rediscovered: latched is "this filter is on" and focused is "you
    // are here", and there is one spare axis for two facts.
    let style = style();
    let kind = Token::Chip { removable: false };
    let latched = token("rust", kind, Tone::Neutral, true, false, &style);
    let focused = token("rust", kind, Tone::Neutral, false, true, &style);
    assert_eq!(latched.spans[0].style, focused.spans[0].style);
    assert!(
        latched.spans[0]
            .style
            .add_modifier
            .contains(Modifier::REVERSED)
    );
}

#[test]
fn a_tone_and_a_heading_map_without_a_fallback_arm() {
    // Both source enums are closed, which is what lets these be total. A
    // renderer that had to guess would be picking its own colours again.
    let style = style();
    assert_eq!(style.tone(Tone::Neutral), style.content);
    assert_eq!(style.tone(Tone::Danger), style.danger);
    assert_eq!(style.heading(Heading::Page), style.page);
    assert_eq!(style.heading(Heading::Subsection), style.subsection);
}

#[test]
fn the_default_style_carries_no_colour_at_all() {
    // A two-colour terminal is the case where a foreground will not land,
    // so the default is modifiers only rather than a placeholder palette.
    let style = plain();
    for painted in [style.content, style.danger, style.page, style.action] {
        assert_eq!(painted.fg, None);
        assert_eq!(painted.bg, None);
    }
}

#[test]
fn the_three_states_of_a_wait_are_three_drawings() {
    // The whole done condition of `5db1e0ed`: a measured wait and an
    // unmeasured one stopped being the same line.
    let style = plain();
    let bare = awaiting(Awaiting::unmeasured(), Progress::default(), true, &style);
    let sized = awaiting(Awaiting::of(41_943_040), Progress::default(), true, &style);
    let watched = awaiting(
        Awaiting::of(40),
        Progress {
            delivered: Some(20),
            elapsed: Some(Duration::from_secs(4)),
        },
        true,
        &style,
    );
    let read = |line: &Line<'_>| {
        line.spans
            .iter()
            .map(|s| s.content.to_string())
            .collect::<String>()
    };
    assert_eq!(read(&bare), "#");
    assert_eq!(read(&sized), "# 41943040");
    assert_eq!(
        sized.spans[1].style, style.muted,
        "the total in the reading's ink"
    );
    assert_eq!(read(&watched), "#####----- 20/40  4s");
}

#[test]
fn a_dark_mark_still_occupies_its_cell() {
    // Not absent. A line that reflowed every half second would move the
    // content beside it, and the reader would lose where to look.
    let style = plain();
    assert_eq!(activity(true, &style).content.chars().count(), 1);
    assert_eq!(activity(false, &style).content.chars().count(), 1);
}

#[test]
fn an_over_delivered_wait_clamps_and_does_not_panic() {
    // A transfer can hand over more than the size it announced, and the
    // bar has ten cells whatever happens.
    let style = plain();
    let over = awaiting(
        Awaiting::of(4),
        Progress {
            delivered: Some(9),
            elapsed: None,
        },
        true,
        &style,
    );
    assert!(over.spans[0].content.chars().all(|c| c == '#'));
    assert_eq!(over.spans[0].content.chars().count(), 10);
    // A zero payload is no payload rather than a finished one.
    let empty = awaiting(
        Awaiting::of(0),
        Progress {
            delivered: Some(9),
            elapsed: None,
        },
        true,
        &style,
    );
    assert!(empty.spans[0].content.starts_with('-'));
}

#[test]
fn a_toned_chip_says_its_tone_in_its_brackets_and_its_label_in_content() {
    // Toned text ruling: a status ink is an edge or a glyph, never the words.
    let style = style();
    for kind in [Token::Chip { removable: false }, Token::Badge] {
        let line = token("overdue", kind, Tone::Danger, false, false, &style);
        let label = line
            .spans
            .iter()
            .find(|span| span.content == "overdue")
            .expect("the label is its own span");
        assert_ne!(
            label.style, style.danger,
            "{kind:?}: the label is in the tone"
        );
        let [open, .., close] = &line.spans[..] else {
            panic!("bracketed: {line:?}");
        };
        if kind == (Token::Chip { removable: false }) {
            assert_eq!(label.style, style.content);
            assert_eq!(open.style, style.danger);
            assert_eq!(close.style, style.danger);
        }
    }
}

#[test]
fn a_heat_band_with_no_colour_adds_nothing() {
    let ink = plain();
    for band in 0..=makeover_layout::HEAT_BANDS {
        assert_eq!(ink.heat(makeover_layout::Heat(band)), Style::new());
    }
}