BlueGit

ict rusty-teams

chore(chat): guard against gaps when a new message arrives

- layout_guard checks frame by frame that the spacing between the rows above a new message stays the same

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-11 · 2 weeks ago

Hash
a60f3fa7e6cc00cb44b8c62743f1b8ff27548e90
Parent
49bd26c8
1 file changed +81 −0
crates/app/src/layout_guard.rs +81 −0
@@ -1303,6 +1303,87 @@ mod frames {
1303 1303 }
1304 1304 }
1305 1305
1306 #[gpui_kit::test]
1307 fn a_new_message_never_opens_a_gap_between_the_rows_above(cx: &mut TestAppContext) {
1308 const GAP_CHAT: &str = "demo-chat-priya";
1309 let (handle, app) = mount(cx);
1310 let mut visual = VisualTestContext::from_window(handle, cx);
1311 visual.simulate_resize(size(px(1280.), px(800.)));
1312 visual.update(|_, cx| {
1313 app.update(cx, |state, cx| {
1314 state.select(Selection::Chat(GAP_CHAT.to_owned()), cx)
1315 })
1316 });
1317 collect(&mut visual, Some(TIMELINE_TAG));
1318 collect(&mut visual, None);
1319 let bubble_positions = |visual: &mut VisualTestContext| -> Vec<(usize, f32, f32)> {
1320 BUBBLE_TAGS
1321 .iter()
1322 .enumerate()
1323 .filter_map(|(index, tag)| {
1324 visual
1325 .debug_bounds(tag)
1326 .map(|bounds| (index, f32::from(bounds.top()), f32::from(bounds.bottom())))
1327 })
1328 .collect()
1329 };
1330 let before = bubble_positions(&mut visual);
1331 visual.update(|_, cx| {
1332 app.update(cx, |state, cx| {
1333 let record = store::MessageRecord {
1334 conversation_id: GAP_CHAT.to_owned(),
1335 message_id: "guard-new".to_owned(),
1336 sender_id: Some(demo::PRIYA_ID.to_owned()),
1337 sender_name: Some("Priya Nair".to_owned()),
1338 created_at: chrono::Utc::now(),
1339 body_html: "<p>Fresh message</p>".to_owned(),
1340 links_json: "[]".to_owned(),
1341 attachments_json: "[]".to_owned(),
1342 reactions_json: "[]".to_owned(),
1343 mentions_json: "[]".to_owned(),
1344 ..Default::default()
1345 };
1346 state.store.upsert_messages(&[record]).expect("store message");
1347 state.apply(
1348 crate::backend::BackendEvent::Core(teams_core::CoreEvent::MessagesChanged {
1349 conversation_id: GAP_CHAT.to_owned(),
1350 }),
1351 cx,
1352 );
1353 })
1354 });
1355 let mut frames = Vec::new();
1356 for _ in 0..12 {
1357 visual.executor().advance_clock(Duration::from_millis(16));
1358 visual.run_until_parked();
1359 visual.update(|window, cx| window.render_frame(cx));
1360 frames.push(bubble_positions(&mut visual));
1361 }
1362 let spacing = |positions: &[(usize, f32, f32)]| -> Vec<(usize, f32)> {
1363 positions
1364 .windows(2)
1365 .map(|pair| (pair[1].0, pair[1].1 - pair[0].2))
1366 .collect()
1367 };
1368 let resting = spacing(&before);
1369 let found: Vec<String> = frames
1370 .iter()
1371 .enumerate()
1372 .flat_map(|(frame, positions)| {
1373 let resting = resting.clone();
1374 spacing(positions)
1375 .into_iter()
1376 .filter_map(move |(index, gap)| {
1377 let expected = resting.iter().find(|(known, _)| *known == index)?.1;
1378 ((gap - expected).abs() > TOLERANCE).then(|| {
1379 format!("frame {frame}: gap above bubble {index} is {gap}, was {expected}")
1380 })
1381 })
1382 })
1383 .collect();
1384 assert!(found.is_empty(), "{}", found.join("\n"));
1385 }
1386
1306 1387 fn inside_horizontally(outer: Bounds<Pixels>, inner: Bounds<Pixels>) -> bool {
1307 1388 let tolerance = px(TOLERANCE);
1308 1389 inner.left() >= outer.left() - tolerance && inner.right() <= outer.right() + tolerance
@@ -1303,6 +1303,87 @@ mod frames {
1303 }
1303 }
1304 }
1304 }
1305
1305
1306 #[gpui_kit::test]
1307 fn a_new_message_never_opens_a_gap_between_the_rows_above(cx: &mut TestAppContext) {
1308 const GAP_CHAT: &str = "demo-chat-priya";
1309 let (handle, app) = mount(cx);
1310 let mut visual = VisualTestContext::from_window(handle, cx);
1311 visual.simulate_resize(size(px(1280.), px(800.)));
1312 visual.update(|_, cx| {
1313 app.update(cx, |state, cx| {
1314 state.select(Selection::Chat(GAP_CHAT.to_owned()), cx)
1315 })
1316 });
1317 collect(&mut visual, Some(TIMELINE_TAG));
1318 collect(&mut visual, None);
1319 let bubble_positions = |visual: &mut VisualTestContext| -> Vec<(usize, f32, f32)> {
1320 BUBBLE_TAGS
1321 .iter()
1322 .enumerate()
1323 .filter_map(|(index, tag)| {
1324 visual
1325 .debug_bounds(tag)
1326 .map(|bounds| (index, f32::from(bounds.top()), f32::from(bounds.bottom())))
1327 })
1328 .collect()
1329 };
1330 let before = bubble_positions(&mut visual);
1331 visual.update(|_, cx| {
1332 app.update(cx, |state, cx| {
1333 let record = store::MessageRecord {
1334 conversation_id: GAP_CHAT.to_owned(),
1335 message_id: "guard-new".to_owned(),
1336 sender_id: Some(demo::PRIYA_ID.to_owned()),
1337 sender_name: Some("Priya Nair".to_owned()),
1338 created_at: chrono::Utc::now(),
1339 body_html: "<p>Fresh message</p>".to_owned(),
1340 links_json: "[]".to_owned(),
1341 attachments_json: "[]".to_owned(),
1342 reactions_json: "[]".to_owned(),
1343 mentions_json: "[]".to_owned(),
1344 ..Default::default()
1345 };
1346 state.store.upsert_messages(&[record]).expect("store message");
1347 state.apply(
1348 crate::backend::BackendEvent::Core(teams_core::CoreEvent::MessagesChanged {
1349 conversation_id: GAP_CHAT.to_owned(),
1350 }),
1351 cx,
1352 );
1353 })
1354 });
1355 let mut frames = Vec::new();
1356 for _ in 0..12 {
1357 visual.executor().advance_clock(Duration::from_millis(16));
1358 visual.run_until_parked();
1359 visual.update(|window, cx| window.render_frame(cx));
1360 frames.push(bubble_positions(&mut visual));
1361 }
1362 let spacing = |positions: &[(usize, f32, f32)]| -> Vec<(usize, f32)> {
1363 positions
1364 .windows(2)
1365 .map(|pair| (pair[1].0, pair[1].1 - pair[0].2))
1366 .collect()
1367 };
1368 let resting = spacing(&before);
1369 let found: Vec<String> = frames
1370 .iter()
1371 .enumerate()
1372 .flat_map(|(frame, positions)| {
1373 let resting = resting.clone();
1374 spacing(positions)
1375 .into_iter()
1376 .filter_map(move |(index, gap)| {
1377 let expected = resting.iter().find(|(known, _)| *known == index)?.1;
1378 ((gap - expected).abs() > TOLERANCE).then(|| {
1379 format!("frame {frame}: gap above bubble {index} is {gap}, was {expected}")
1380 })
1381 })
1382 })
1383 .collect();
1384 assert!(found.is_empty(), "{}", found.join("\n"));
1385 }
1386
1306 fn inside_horizontally(outer: Bounds<Pixels>, inner: Bounds<Pixels>) -> bool {
1387 fn inside_horizontally(outer: Bounds<Pixels>, inner: Bounds<Pixels>) -> bool {
1307 let tolerance = px(TOLERANCE);
1388 let tolerance = px(TOLERANCE);
1308 inner.left() >= outer.left() - tolerance && inner.right() <= outer.right() + tolerance
1389 inner.left() >= outer.left() - tolerance && inner.right() <= outer.right() + tolerance