use super::geometry::{block, is_drawn}; use super::grid::parse_hex; use super::palette::with_variables; use super::svg::text; use super::*; use luxe_tui::fixture::FixtureScenario; use ratatui::buffer::Buffer; use ratatui::style::Color; fn shot() -> Buffer { render("luxe", FixtureScenario::ChatTools, None, 120, 30).0 } /// One palette picture stands in for every theme: no placeholder survives into the document, /// the fallbacks are the first theme's own colors, and every theme gets a value for every /// variable the picture uses. #[test] fn a_palette_picture_names_every_color_it_draws() { let themes = ["luxe".to_string(), "nord".to_string()]; let (document, values) = palette_svg(&themes, FixtureScenario::ChatTools, 120, 30); assert!(!document.contains("=\"#0f"), "a placeholder leaked into the picture"); assert!(document.contains("fill=\"var(--p0, #"), "{}", &document[..400]); let used = document.matches("var(--p").count(); assert!(used > 20, "the picture is drawn in variables: {used}"); let (luxe, nord) = (&values["luxe"], &values["nord"]); assert_eq!(luxe.len(), nord.len()); assert!(luxe.iter().any(|(key, value)| nord[key] != *value), "the themes differ"); for (key, value) in luxe { assert!(document.contains(&format!("var(--{key}, {value})")), "{key} unused"); } } /// Only attributes are rewritten: a hex color in the terminal's *text* is somebody's code. #[test] fn a_placeholder_in_the_text_is_left_alone() { let document = "#0f0001"; let out = with_variables(document, &["#aaaaaa", "#bbbbbb"]); assert_eq!(out, "#0f0001"); } /// The export is a real render, not a blank grid — the cheapest way to catch a fixture that /// stopped seeding itself. #[test] fn the_export_carries_the_render() { let svg = svg(&shot()); assert!(svg.starts_with("")); assert!(svg.contains(" 5, "suspiciously few rows"); } /// Text stays text. The whole reason not to ship a PNG is that a reader can select a command /// out of the picture, so a run of real characters has to survive the trip. #[test] fn commands_in_the_render_are_still_selectable_text() { let svg = svg(&shot()); assert!(svg.contains("cargo test"), "a command the fixture renders went missing"); assert!(svg.contains("xml:space=\"preserve\""), "leading columns would collapse"); assert!(svg.contains("lengthAdjust=\"spacingAndGlyphs\""), "the grid needs pinning"); } /// Every glyph defined as an exact cell fraction is drawn rather than typeset. These are the /// ones a font approximates, which is what makes a terminal picture look wrong. #[test] fn cell_fraction_glyphs_are_drawn_not_typeset() { for glyph in [ '\u{2588}', '\u{2580}', '\u{2584}', '\u{258c}', '\u{2590}', '\u{2581}', '\u{2587}', '\u{258f}', '\u{2589}', '\u{2594}', '\u{2595}', '\u{2596}', '\u{259f}', '\u{2591}', '\u{2593}', ] { assert!(is_drawn(glyph), "U+{:04X} should be geometry", glyph as u32); } for glyph in ['\u{2500}', '\u{2502}', '\u{250c}', '\u{253c}', '\u{256d}', '\u{2570}'] { assert!(is_drawn(glyph), "U+{:04X} should be geometry", glyph as u32); } for glyph in ['\u{e0b8}', '\u{e0ba}', '\u{e0bc}', '\u{e0be}'] { assert!(is_drawn(glyph), "U+{:04X} should be geometry", glyph as u32); } // Letters and the geometric shapes luxe uses as marks stay text: any font has them, and // they carry meaning that should remain selectable. for glyph in ['a', '\u{276f}', '\u{25cf}', '\u{25c6}'] { assert!(!is_drawn(glyph), "U+{:04X} should be typeset", glyph as u32); } } /// A drawn glyph leaves a space in its row, so the columns after it still land on the grid and /// the mark is not painted twice. #[test] fn a_drawn_glyph_holds_its_column_in_the_text_layer() { let mut buffer = Buffer::empty(ratatui::layout::Rect::new(0, 0, 3, 1)); buffer[(0, 0)].set_symbol("\u{2588}"); buffer[(1, 0)].set_symbol("A"); buffer[(2, 0)].set_symbol("B"); let text = text(&buffer); assert!(text.contains("AB"), "typeset glyphs survive: {text}"); assert!(!text.contains('\u{2588}'), "the block is drawn, not typeset: {text}"); assert!(text.contains(' '), "the block still occupies its column: {text}"); } /// Quadrant masks are transcribed correctly. Getting one wrong is invisible in review and /// obvious on the page, so it is checked against the halves they must add up to. #[test] fn quadrant_masks_agree_with_the_halves_they_compose() { let area = |glyph: char| { block(glyph).expect("a block").0.iter().map(|(_, _, w, h)| w * h).sum::() }; assert_eq!(area('\u{2596}'), 0.25, "one quadrant"); assert_eq!(area('\u{259f}'), 0.75, "three quadrants"); // ▘ upper-left plus ▗ lower-right is the same ink as ▚. assert_eq!(area('\u{2598}') + area('\u{2597}'), area('\u{259a}')); // Every eighth block covers the fraction it is named for. assert!((area('\u{2581}') - 0.125).abs() < f64::EPSILON, "one lower eighth"); assert!((area('\u{2587}') - 0.875).abs() < f64::EPSILON, "seven lower eighths"); assert!((area('\u{258f}') - 0.125).abs() < f64::EPSILON, "one left eighth"); assert!((area('\u{2589}') - 0.875).abs() < f64::EPSILON, "seven left eighths"); } /// Markup that would break a parser is escaped. A diff of HTML is an ordinary thing to render. #[test] fn markup_in_the_render_is_escaped() { let mut buffer = Buffer::empty(ratatui::layout::Rect::new(0, 0, 5, 1)); for (index, glyph) in "