(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: MIT ---------------------------------------------------------------------------*) open Crowbar open Tty (* Width is always non-negative *) let test_width_non_negative s = let w = Width.string_width s in check (w >= 0) (* Span width matches underlying width *) let test_span_width_consistency s = let span = Span.text s in let span_w = Span.width span in let direct_w = Width.string_width s in check (span_w = direct_w) (* Span concatenation width is sum of widths *) let test_span_concat_width s1 s2 = let span1 = Span.text s1 in let span2 = Span.text s2 in let combined = Span.(span1 ++ span2) in let w1 = Span.width span1 in let w2 = Span.width span2 in let w_combined = Span.width combined in check (w_combined = w1 + w2) (* Table rendering doesn't crash *) let test_table_render_no_crash headers row = if List.length headers > 0 && List.length row > 0 then let cols = List.map Table.column headers in let rows = [ List.map Span.text row ] in let table = Table.of_rows cols rows in let _ = Table.to_string table in check true else check true (* Tree rendering doesn't crash *) let test_tree_render_no_crash label = let tree = Tree.of_tree (Tree.Node (Span.text label, [])) in let _ = Tree.to_string tree in check true (* Panel rendering doesn't crash *) let test_panel_render_no_crash content = let panel = Panel.v (Span.text content) in let _ = Panel.to_string panel in check true (* Color hex parsing and equality *) let test_color_rgb_roundtrip r g b = let c = Color.rgb r g b in let c2 = Color.rgb r g b in check (Color.equal c c2) (* Truncate never increases width *) let test_truncate_width s width = let width = abs width mod 1000 in let truncated = Width.truncate width s in let truncated_w = Width.string_width truncated in check (truncated_w <= width) let suite = ( "tty", [ test_case "width non-negative" [ bytes ] test_width_non_negative; test_case "span width consistency" [ bytes ] test_span_width_consistency; test_case "span concat width" [ bytes; bytes ] test_span_concat_width; test_case "table render" [ list bytes; list bytes ] test_table_render_no_crash; test_case "tree render" [ bytes ] test_tree_render_no_crash; test_case "panel render" [ bytes ] test_panel_render_no_crash; test_case "color rgb roundtrip" [ int; int; int ] test_color_rgb_roundtrip; test_case "truncate width" [ bytes; int ] test_truncate_width; ] )