// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Headless tests for the `rich_text` layout logic, run against a software
//! `Canvas` (no GL needed). Line counts are forced with hard `\n` breaks so the
//! assertions do not depend on the measured width of any particular system font.

use super::*;
use crate::render::Canvas;
use crate::types::Rect;

fn canvas() -> Canvas
{
	Canvas::new( 800, 600 )
}

fn rect( w: f32, h: f32 ) -> Rect
{
	Rect { x: 0.0, y: 0.0, width: w, height: h }
}

#[ test ]
fn link_spanning_two_lines_yields_one_rect_per_line()
{
	let c = canvas();
	// Hard break splits the paragraph into two visual lines regardless of font
	// metrics; the single link covers both words.
	let rt = rich_text::<i32>( "first\nsecond" ).link( 0, 12, 7 );
	let rects = rt.link_rects( rect( 1000.0, 200.0 ), &c );

	assert_eq!( rects.len(), 2, "a link crossing two lines must emit one rect per line" );
	assert!( rects.iter().all( |( _, m )| *m == 7 ), "every line rect carries the link message" );
	assert!( rects[ 1 ].0.y > rects[ 0 ].0.y, "the second line's rect sits below the first" );
}

#[ test ]
fn single_line_link_yields_one_rect()
{
	let c = canvas();
	// "world" is bytes [6, 11). A generous width keeps the paragraph on one line.
	let rt = rich_text::<i32>( "hello world" ).link( 6, 11, 1 );
	let rects = rt.link_rects( rect( 10_000.0, 100.0 ), &c );

	assert_eq!( rects.len(), 1 );
	assert_eq!( rects[ 0 ].1, 1 );
	assert!( rects[ 0 ].0.width > 0.0, "the link rect spans the measured substring" );
}

#[ test ]
fn paragraph_without_links_has_no_hit_rects()
{
	let c = canvas();
	let rt = rich_text::<i32>( "plain paragraph, no links here" );
	assert!( rt.link_rects( rect( 1000.0, 100.0 ), &c ).is_empty() );
}

#[ test ]
fn link_off_a_line_is_not_reported_on_that_line()
{
	let c = canvas();
	// Link only covers "first" (bytes [0, 5)); the second line must report nothing.
	let rt = rich_text::<i32>( "first\nsecond" ).link( 0, 5, 9 );
	let rects = rt.link_rects( rect( 1000.0, 200.0 ), &c );
	assert_eq!( rects.len(), 1, "a link confined to one line yields exactly one rect" );
}

#[ test ]
fn preferred_size_height_grows_with_hard_breaks()
{
	let c = canvas();
	let one   = rich_text::<i32>( "a" ).preferred_size( 1000.0, &c );
	let three = rich_text::<i32>( "a\nb\nc" ).preferred_size( 1000.0, &c );

	assert_eq!( one.0, 1000.0, "preferred width echoes the max width" );
	assert!( one.1 > 0.0, "a single line has positive height" );
	assert!( three.1 > one.1 * 2.0, "three lines are taller than one ({:?} vs {:?})", three.1, one.1 );
}

#[ test ]
fn preferred_size_empty_content_is_one_line()
{
	let c = canvas();
	let empty = rich_text::<i32>( "" ).preferred_size( 1000.0, &c );
	let one   = rich_text::<i32>( "a" ).preferred_size( 1000.0, &c );
	assert!( empty.1 > 0.0, "empty content still reserves one line" );
	assert!( ( empty.1 - one.1 ).abs() < 0.5, "empty and single-char are both one line tall" );
}

#[ test ]
fn map_msg_preserves_link_ranges_and_count()
{
	let rt = rich_text::<i32>( "alpha beta" ).link( 0, 5, 11 ).link( 6, 10, 22 );
	let f: std::sync::Arc<dyn Fn( i32 ) -> String> = std::sync::Arc::new( |m| format!( "msg-{m}" ) );
	let mapped = rt.map_msg( &f );

	assert_eq!( mapped.links.len(), 2 );
	assert_eq!( ( mapped.links[ 0 ].start, mapped.links[ 0 ].end ), ( 0, 5 ) );
	assert_eq!( ( mapped.links[ 1 ].start, mapped.links[ 1 ].end ), ( 6, 10 ) );
	assert_eq!( mapped.links[ 0 ].msg, "msg-11" );
	assert_eq!( mapped.links[ 1 ].msg, "msg-22" );
}
