Files
Jarvise-project/frontend/tests/chat_layout.rs
T
BlackAngelTV b9e23cf975 feat(frontend): manage projects and their context in the desktop client
A conversation that only lives in memory is lost when the window closes, and
long answers used to be cut off at the window edge because a scroll area
inherits the layout of the parent it is added to. Projects give each exchange a
durable context, the reload and verification actions make that context
observable instead of a black box, and laying the text out before drawing the
bubble keeps every answer inside the column.
2026-10-05 21:39:55 +02:00

134 lines
4.5 KiB
Rust

//! Headless layout tests of the conversation area.
//!
//! `egui` runs fine without a window: a pass is fed a `RawInput` with a fixed
//! screen size and the painted shapes come back in the `FullOutput`. Bubbles
//! are painted as filled rectangles with their own colours, so they can be
//! measured to prove that long answers wrap *inside* the centred column instead
//! of running past the edge of the window.
use eframe::egui;
use jarvise_frontend::app::{COLUMN_MAX_WIDTH, COLUMN_MIN_MARGIN, chat_area};
use jarvise_frontend::model::ChatMessage;
/// Colour of the user's bubbles.
const USER_FILL: egui::Color32 = egui::Color32::from_rgb(0x2f, 0x6f, 0xb5);
/// Colour of Jarvis' bubbles.
const JARVIS_FILL: egui::Color32 = egui::Color32::from_gray(58);
/// A paragraph long enough to need several lines in a bubble.
fn long_text() -> String {
"Jarvise garde le contexte de chaque projet et le renvoie au modèle à chaque tour de parole. "
.repeat(8)
}
/// Column `chat_area` builds inside a window of `screen_width`.
fn column(screen_width: f32) -> (f32, f32) {
let width = (screen_width - 2.0 * COLUMN_MIN_MARGIN).min(COLUMN_MAX_WIDTH);
let side = ((screen_width - width) / 2.0).max(0.0);
(side, side + width)
}
/// Runs one egui pass on a window of the given width and returns what was painted.
fn painted(screen_width: f32, messages: &[ChatMessage]) -> Vec<egui::epaint::ClippedShape> {
let ctx = egui::Context::default();
ctx.set_fonts(egui::FontDefinitions::default());
let input = egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(
egui::Pos2::ZERO,
egui::vec2(screen_width, 900.0),
)),
..Default::default()
};
let mut output = ctx.run_ui(input, |ui| chat_area(ui, messages));
let shapes = std::mem::take(&mut output.shapes);
// Nothing rasterises the fonts here, so the texture deltas can be dropped.
output.drop_without_applying_deltas();
shapes
}
/// Filled rectangles painted with a bubble colour, i.e. the bubbles themselves.
fn bubbles(shapes: &[egui::epaint::ClippedShape]) -> Vec<egui::Rect> {
shapes
.iter()
.filter_map(|clipped| match &clipped.shape {
egui::Shape::Rect(rect) if rect.fill == USER_FILL || rect.fill == JARVIS_FILL => {
Some(rect.rect)
}
_ => None,
})
.collect()
}
#[test]
fn long_answers_wrap_inside_the_centred_column() {
let messages = vec![
ChatMessage::assistant(long_text()),
ChatMessage::user(long_text()),
];
let shapes = painted(1400.0, &messages);
let rects = bubbles(&shapes);
assert_eq!(rects.len(), 2, "one bubble per message, got {rects:?}");
let (left, right) = column(1400.0);
for rect in &rects {
assert!(
rect.min.x >= left - 1.0 && rect.max.x <= right + 1.0,
"bubble {rect:?} escapes the column [{left}, {right}]"
);
assert!(
rect.width() <= (right - left) * 0.83,
"bubble {rect:?} is wider than the column"
);
// A single line would be ~20 pt high: several lines prove the text wrapped.
assert!(
rect.height() > 40.0,
"long text was not wrapped, bubble {rect:?} is one line high"
);
}
}
#[test]
fn bubbles_stay_on_screen_in_a_narrow_window() {
let messages = vec![
ChatMessage::user(long_text()),
ChatMessage::assistant(long_text()),
];
let shapes = painted(420.0, &messages);
let (left, right) = column(420.0);
for rect in bubbles(&shapes) {
assert!(
rect.min.x >= left - 1.0 && rect.max.x <= right + 1.0,
"bubble {rect:?} escapes the column [{left}, {right}] of a 420 pt window"
);
assert!(rect.max.x <= 420.0, "bubble {rect:?} runs off the window");
}
}
#[test]
fn user_bubbles_hug_the_right_and_jarvis_the_left() {
let messages = vec![
ChatMessage::assistant("Salut"),
ChatMessage::user("Salut"),
];
let shapes = painted(1400.0, &messages);
let rects = bubbles(&shapes);
assert_eq!(rects.len(), 2);
let jarvis = rects.iter().find(|rect| rect.width() > 0.0 && rect.min.x < 300.0);
assert!(jarvis.is_some(), "Jarvis' bubble must start at the left: {rects:?}");
}
#[test]
fn an_empty_conversation_paints_no_bubble() {
let shapes = painted(1400.0, &[]);
assert!(
bubbles(&shapes).is_empty(),
"the placeholder must not look like a message"
);
}