| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use chrono::{DateTime, Utc}; |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::component::tooltip::Tooltip; |
| 6 | use gpui_kit::component::{h_flex, v_flex}; |
| 7 | use gpui_kit::prelude::FluentBuilder as _; |
| 8 | use gpui_kit::*; |
| 9 | use teams_core::{Draft, MentionInput, OBJECT_MARK}; |
| 10 | |
| 11 | #[cfg(test)] |
| 12 | use crate::agent_api::body::body_blocks; |
| 13 | use crate::agent_api::{ProposalEntry, ProposalPicture, ProposalStatus, agent_label}; |
| 14 | use crate::render::render_blocks; |
| 15 | use crate::theme; |
| 16 | use crate::views::attachment_tray::{Classification, OutgoingImage, classify, inline_image}; |
| 17 | use crate::views::composer::{Outgoing, ReplyPreview}; |
| 18 | use crate::views::widgets::icon; |
| 19 | |
| 20 | const CARD_MARGIN_X: f32 = 20.; |
| 21 | const CARD_MARGIN_BOTTOM: f32 = 8.; |
| 22 | const CARD_PADDING: f32 = 12.; |
| 23 | const CARD_RADIUS: f32 = 10.; |
| 24 | const CARD_GAP: f32 = 8.; |
| 25 | const BUTTON_HEIGHT: f32 = 28.; |
| 26 | const BUTTON_RADIUS: f32 = 7.; |
| 27 | const BODY_LINE_HEIGHT: f32 = 20.; |
| 28 | const BODY_MAX_LINES: f32 = 12.; |
| 29 | const QUOTE_BAR_WIDTH: f32 = 2.; |
| 30 | const THUMBNAIL_HEIGHT: f32 = 48.; |
| 31 | const THUMBNAIL_MAX_WIDTH: f32 = 120.; |
| 32 | const THUMBNAIL_MIN_WIDTH: f32 = 16.; |
| 33 | const THUMBNAIL_RADIUS: f32 = 6.; |
| 34 | pub const PROPOSAL_CARD: &str = "shell/islands/main/proposal-card"; |
| 35 | pub const PROPOSAL_THUMBNAILS: &str = "shell/islands/main/proposal-card/thumbnails"; |
| 36 | pub const PROPOSAL_BUTTONS: &str = "shell/islands/main/proposal-card/buttons"; |
| 37 | |
| 38 | pub type CardAction = Rc<dyn Fn(&mut Window, &mut App)>; |
| 39 | pub type AllowAction = Rc<dyn Fn(u32, &mut Window, &mut App)>; |
| 40 | |
| 41 | #[derive(Clone)] |
| 42 | pub struct CardActions { |
| 43 | pub send: CardAction, |
| 44 | pub edit: CardAction, |
| 45 | pub discard: CardAction, |
| 46 | pub allow: AllowAction, |
| 47 | } |
| 48 | |
| 49 | pub const FREE_WRITING_CHOICES: [u32; 3] = [15, 60, 240]; |
| 50 | pub const FREE_WRITING_BAR: &str = "shell/islands/main/free-writing-bar"; |
| 51 | |
| 52 | pub fn free_writing_choices(asked: u32) -> Vec<u32> { |
| 53 | let mut choices: Vec<u32> = FREE_WRITING_CHOICES.to_vec(); |
| 54 | if !choices.contains(&asked) { |
| 55 | choices.push(asked); |
| 56 | choices.sort_unstable(); |
| 57 | } |
| 58 | choices |
| 59 | } |
| 60 | |
| 61 | pub fn duration_label(minutes: u32) -> String { |
| 62 | match (minutes / 60, minutes % 60) { |
| 63 | (0, rest) => format!("{rest} min"), |
| 64 | (hours, 0) => format!("{hours} h"), |
| 65 | (hours, rest) => format!("{hours} h {rest} min"), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | fn free_writing_row(entry: &ProposalEntry, allow: AllowAction) -> Option<Div> { |
| 70 | let ask = entry.proposal.free_writing_ask.as_ref()?; |
| 71 | let agent = agent_label(entry.proposal.agent.as_deref()); |
| 72 | let chips = free_writing_choices(ask.minutes).into_iter().map(|minutes| { |
| 73 | let allow = allow.clone(); |
| 74 | div() |
| 75 | .id(ElementId::Name(format!("proposal-allow-{minutes}").into())) |
| 76 | .h(px(BUTTON_HEIGHT)) |
| 77 | .px(px(10.)) |
| 78 | .flex() |
| 79 | .items_center() |
| 80 | .rounded(px(BUTTON_RADIUS)) |
| 81 | .cursor_pointer() |
| 82 | .text_size(px(12.)) |
| 83 | .bg(theme::surface_raised()) |
| 84 | .text_color(theme::text()) |
| 85 | .hover(|chip| chip.opacity(0.85)) |
| 86 | .child(duration_label(minutes)) |
| 87 | .on_click(move |_, window, cx| allow(minutes, window, cx)) |
| 88 | }); |
| 89 | let reason = (!ask.reason.is_empty()).then(|| format!(": {}", ask.reason)).unwrap_or_default(); |
| 90 | Some( |
| 91 | v_flex() |
| 92 | .w_full() |
| 93 | .gap(px(6.)) |
| 94 | .child( |
| 95 | div() |
| 96 | .text_size(px(12.)) |
| 97 | .text_color(theme::text_muted()) |
| 98 | .child(format!( |
| 99 | "{agent} asks to keep writing here without review for {}{reason}", |
| 100 | duration_label(ask.minutes) |
| 101 | )), |
| 102 | ) |
| 103 | .child( |
| 104 | h_flex() |
| 105 | .flex_wrap() |
| 106 | .items_center() |
| 107 | .gap(px(6.)) |
| 108 | .child(div().text_size(px(12.)).text_color(theme::text_soft()).child("Send and allow for")) |
| 109 | .children(chips), |
| 110 | ), |
| 111 | ) |
| 112 | } |
| 113 | |
| 114 | pub fn free_writing_bar(agent: &str, until: DateTime<Utc>, stop: CardAction) -> Div { |
| 115 | let until = until.with_timezone(&chrono::Local).format(crate::format::clock_format()); |
| 116 | h_flex() |
| 117 | .flex_none() |
| 118 | .mx(px(CARD_MARGIN_X)) |
| 119 | .mb(px(CARD_MARGIN_BOTTOM)) |
| 120 | .px(px(CARD_PADDING)) |
| 121 | .h(px(BUTTON_HEIGHT + CARD_GAP)) |
| 122 | .gap(px(CARD_GAP)) |
| 123 | .items_center() |
| 124 | .rounded(px(CARD_RADIUS)) |
| 125 | .bg(theme::row_hover()) |
| 126 | .debug_selector(|| FREE_WRITING_BAR.into()) |
| 127 | .child( |
| 128 | div() |
| 129 | .flex_1() |
| 130 | .min_w_0() |
| 131 | .truncate() |
| 132 | .text_size(px(12.)) |
| 133 | .text_color(theme::text_muted()) |
| 134 | .child(format!("{agent} writes here without review until {until}")), |
| 135 | ) |
| 136 | .child(card_button("free-writing-stop", "Stop", theme::surface_raised(), theme::text(), stop)) |
| 137 | } |
| 138 | |
| 139 | fn open_session_available(session_id: &str, cx: &App) -> bool { |
| 140 | crate::agents::has_session(session_id, cx) |
| 141 | } |
| 142 | |
| 143 | fn open_session(session_id: &str, _window: &mut Window, cx: &mut App) { |
| 144 | crate::agents::open_session(session_id, cx); |
| 145 | } |
| 146 | |
| 147 | fn inline_flags(pictures: &[ProposalPicture]) -> Vec<bool> { |
| 148 | let mut inline_used = 0; |
| 149 | pictures |
| 150 | .iter() |
| 151 | .map(|picture| { |
| 152 | let inline = matches!( |
| 153 | classify(&picture.name, &picture.bytes, inline_used), |
| 154 | Classification::Inline(_) |
| 155 | ); |
| 156 | if inline { |
| 157 | inline_used += picture.bytes.len() as u64; |
| 158 | } |
| 159 | inline |
| 160 | }) |
| 161 | .collect() |
| 162 | } |
| 163 | |
| 164 | pub fn needs_upload(entry: &ProposalEntry) -> bool { |
| 165 | inline_flags(&entry.images).contains(&false) |
| 166 | } |
| 167 | |
| 168 | pub fn picture_files(entry: &ProposalEntry) -> Vec<(String, Vec<u8>)> { |
| 169 | entry |
| 170 | .images |
| 171 | .iter() |
| 172 | .map(|picture| (picture.name.clone(), picture.bytes.to_vec())) |
| 173 | .collect() |
| 174 | } |
| 175 | |
| 176 | /// Carries only the images the composer would send inline; `needs_upload` tells when others are left. |
| 177 | pub fn outgoing_for(entry: &ProposalEntry, reply: Option<ReplyPreview>) -> Outgoing { |
| 178 | let flags = inline_flags(&entry.images); |
| 179 | let images: Vec<OutgoingImage> = entry |
| 180 | .images |
| 181 | .iter() |
| 182 | .zip(&flags) |
| 183 | .filter(|(_, inline)| **inline) |
| 184 | .filter_map(|(picture, _)| inline_image(picture.name.clone(), picture.bytes.to_vec())) |
| 185 | .map(OutgoingImage::Inline) |
| 186 | .collect(); |
| 187 | let mut markdown = entry.proposal.text.trim().to_owned(); |
| 188 | for _ in &images { |
| 189 | markdown.push_str("\n\n"); |
| 190 | markdown.push(OBJECT_MARK); |
| 191 | } |
| 192 | Outgoing { |
| 193 | banner: None, |
| 194 | draft: Draft::from_markdown(&markdown), |
| 195 | subject: None, |
| 196 | mentions: entry |
| 197 | .proposal |
| 198 | .mentions |
| 199 | .iter() |
| 200 | .map(|mention| MentionInput::user(&mention.user_id, &mention.name)) |
| 201 | .collect(), |
| 202 | reply, |
| 203 | edit: None, |
| 204 | images, |
| 205 | files: Vec::new(), |
| 206 | link_preview: None, |
| 207 | importance: Default::default(), |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | fn thumbnail_size(dimensions: Option<(u32, u32)>) -> (f32, f32) { |
| 212 | let width = dimensions |
| 213 | .filter(|(width, height)| *width > 0 && *height > 0) |
| 214 | .map_or(THUMBNAIL_HEIGHT, |(width, height)| { |
| 215 | THUMBNAIL_HEIGHT * width as f32 / height as f32 |
| 216 | }); |
| 217 | ( |
| 218 | width |
| 219 | .clamp(THUMBNAIL_HEIGHT / 2., THUMBNAIL_MAX_WIDTH) |
| 220 | .round(), |
| 221 | THUMBNAIL_HEIGHT, |
| 222 | ) |
| 223 | } |
| 224 | |
| 225 | fn thumbnails(pictures: &[ProposalPicture]) -> Div { |
| 226 | h_flex() |
| 227 | .w_full() |
| 228 | .overflow_hidden() |
| 229 | .gap(px(CARD_GAP)) |
| 230 | .debug_selector(|| PROPOSAL_THUMBNAILS.into()) |
| 231 | .children(pictures.iter().map(|picture| { |
| 232 | let thumbnail = picture.thumbnail.as_ref(); |
| 233 | let (width, height) = thumbnail_size(thumbnail.map(|thumbnail| thumbnail.dimensions)); |
| 234 | div() |
| 235 | .w(px(width)) |
| 236 | .h(px(height)) |
| 237 | .min_w(px(THUMBNAIL_MIN_WIDTH)) |
| 238 | .overflow_hidden() |
| 239 | .rounded(px(THUMBNAIL_RADIUS)) |
| 240 | .bg(theme::surface_raised()) |
| 241 | .children(thumbnail.map(|thumbnail| { |
| 242 | img(ImageSource::from(thumbnail.image.clone())) |
| 243 | .size_full() |
| 244 | .object_fit(ObjectFit::Cover) |
| 245 | })) |
| 246 | })) |
| 247 | } |
| 248 | |
| 249 | pub fn age_label(created_at: DateTime<Utc>, now: DateTime<Utc>) -> String { |
| 250 | let minutes = (now - created_at).num_minutes().max(0); |
| 251 | match minutes { |
| 252 | 0 => "just now".to_owned(), |
| 253 | 1..=59 => format!("{minutes} min ago"), |
| 254 | 60..=1439 => format!("{} h ago", minutes / 60), |
| 255 | _ => format!("{} d ago", minutes / 1440), |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | pub(super) fn card_button( |
| 260 | id: &'static str, |
| 261 | label: &'static str, |
| 262 | background: Hsla, |
| 263 | color: Hsla, |
| 264 | action: CardAction, |
| 265 | ) -> Stateful<Div> { |
| 266 | div() |
| 267 | .id(id) |
| 268 | .h(px(BUTTON_HEIGHT)) |
| 269 | .px(px(12.)) |
| 270 | .flex() |
| 271 | .items_center() |
| 272 | .rounded(px(BUTTON_RADIUS)) |
| 273 | .cursor_pointer() |
| 274 | .text_size(px(12.)) |
| 275 | .font_weight(FontWeight::SEMIBOLD) |
| 276 | .bg(background) |
| 277 | .text_color(color) |
| 278 | .hover(|button| button.opacity(0.85)) |
| 279 | .child(label) |
| 280 | .on_click(move |_, window, cx| action(window, cx)) |
| 281 | } |
| 282 | |
| 283 | fn header_text(entry: &ProposalEntry, now: DateTime<Utc>) -> (String, Hsla, Option<String>) { |
| 284 | let agent = agent_label(entry.proposal.agent.as_deref()); |
| 285 | match &entry.status { |
| 286 | ProposalStatus::Pending => ( |
| 287 | format!("{agent} proposes a reply"), |
| 288 | theme::accent_text(), |
| 289 | Some(format!(" - {}", age_label(entry.proposal.created_at, now))), |
| 290 | ), |
| 291 | ProposalStatus::Stale { answered_at } => ( |
| 292 | format!( |
| 293 | "{agent}'s reply is outdated - you answered at {}", |
| 294 | answered_at.format(crate::format::clock_format()) |
| 295 | ), |
| 296 | theme::text_muted(), |
| 297 | None, |
| 298 | ), |
| 299 | ProposalStatus::Failed { reason } => ( |
| 300 | format!("Could not send - {reason}"), |
| 301 | theme::red_soft(), |
| 302 | None, |
| 303 | ), |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | pub fn render( |
| 308 | entry: &ProposalEntry, |
| 309 | quote: Option<String>, |
| 310 | actions: CardActions, |
| 311 | now: DateTime<Utc>, |
| 312 | cx: &App, |
| 313 | ) -> Div { |
| 314 | let stale = matches!(entry.status, ProposalStatus::Stale { .. }); |
| 315 | let failed = matches!(entry.status, ProposalStatus::Failed { .. }); |
| 316 | let (title, title_color, age) = header_text(entry, now); |
| 317 | let close = actions.discard.clone(); |
| 318 | let header = h_flex() |
| 319 | .w_full() |
| 320 | .items_center() |
| 321 | .gap(px(4.)) |
| 322 | .child( |
| 323 | h_flex() |
| 324 | .flex_1() |
| 325 | .min_w_0() |
| 326 | .items_baseline() |
| 327 | .overflow_hidden() |
| 328 | .text_size(px(12.)) |
| 329 | .child( |
| 330 | div() |
| 331 | .truncate() |
| 332 | .font_weight(FontWeight::SEMIBOLD) |
| 333 | .text_color(title_color) |
| 334 | .child(title), |
| 335 | ) |
| 336 | .children( |
| 337 | age.map(|age| div().flex_none().text_color(theme::text_faint()).child(age)), |
| 338 | ), |
| 339 | ) |
| 340 | .child( |
| 341 | div() |
| 342 | .id("proposal-close") |
| 343 | .size(px(BUTTON_HEIGHT)) |
| 344 | .flex_none() |
| 345 | .flex() |
| 346 | .items_center() |
| 347 | .justify_center() |
| 348 | .rounded(px(BUTTON_RADIUS)) |
| 349 | .cursor_pointer() |
| 350 | .hover(|button| button.bg(theme::surface_raised())) |
| 351 | .child(icon(IconName::Close, 14., theme::text_muted())) |
| 352 | .on_click(move |_, window, cx| close(window, cx)), |
| 353 | ); |
| 354 | let quote = quote.map(|quote| { |
| 355 | div() |
| 356 | .w_full() |
| 357 | .truncate() |
| 358 | .pl(px(8.)) |
| 359 | .border_l(px(QUOTE_BAR_WIDTH)) |
| 360 | .border_color(theme::border_strong()) |
| 361 | .text_size(px(12.)) |
| 362 | .text_color(theme::text_muted()) |
| 363 | .child(quote) |
| 364 | }); |
| 365 | let labels = entry.mention_labels.clone(); |
| 366 | let body = div() |
| 367 | .id("proposal-text") |
| 368 | .w_full() |
| 369 | .max_h(px(BODY_LINE_HEIGHT * BODY_MAX_LINES)) |
| 370 | .overflow_y_scroll() |
| 371 | .text_size(px(14.)) |
| 372 | .line_height(px(BODY_LINE_HEIGHT)) |
| 373 | .when(stale, |body| body.opacity(0.6)) |
| 374 | .child(render_blocks(&entry.body, "proposal-body", false, cx)) |
| 375 | .when(!labels.is_empty(), |body| { |
| 376 | body.tooltip(move |window, cx| { |
| 377 | Tooltip::new(format!("Mentions: {}", labels.join(", "))).build(window, cx) |
| 378 | }) |
| 379 | }); |
| 380 | let images = (!entry.images.is_empty()).then(|| thumbnails(&entry.images)); |
| 381 | let free_writing = (!stale && !failed) |
| 382 | .then(|| free_writing_row(entry, actions.allow.clone())) |
| 383 | .flatten(); |
| 384 | let session = entry |
| 385 | .proposal |
| 386 | .session_id |
| 387 | .clone() |
| 388 | .filter(|session_id| open_session_available(session_id, cx)); |
| 389 | let buttons = h_flex() |
| 390 | .w_full() |
| 391 | .items_center() |
| 392 | .gap(px(8.)) |
| 393 | .debug_selector(|| PROPOSAL_BUTTONS.into()) |
| 394 | .when(!stale, |row| { |
| 395 | row.child(card_button( |
| 396 | "proposal-send", |
| 397 | if failed { "Retry" } else { "Send" }, |
| 398 | theme::accent(), |
| 399 | theme::on_accent(), |
| 400 | actions.send.clone(), |
| 401 | )) |
| 402 | }) |
| 403 | .child(card_button( |
| 404 | "proposal-edit", |
| 405 | "Edit", |
| 406 | theme::surface_raised(), |
| 407 | theme::text(), |
| 408 | actions.edit.clone(), |
| 409 | )) |
| 410 | .child(card_button( |
| 411 | "proposal-discard", |
| 412 | "Discard", |
| 413 | theme::surface_raised(), |
| 414 | theme::text(), |
| 415 | actions.discard.clone(), |
| 416 | )) |
| 417 | .children(session.map(|session_id| { |
| 418 | div() |
| 419 | .id("proposal-open-session") |
| 420 | .ml_auto() |
| 421 | .cursor_pointer() |
| 422 | .text_size(px(12.)) |
| 423 | .text_color(theme::accent_text()) |
| 424 | .child("Open session") |
| 425 | .on_click(move |_, window, cx| open_session(&session_id, window, cx)) |
| 426 | })); |
| 427 | v_flex() |
| 428 | .flex_none() |
| 429 | .mx(px(CARD_MARGIN_X)) |
| 430 | .mb(px(CARD_MARGIN_BOTTOM)) |
| 431 | .p(px(CARD_PADDING)) |
| 432 | .gap(px(CARD_GAP)) |
| 433 | .rounded(px(CARD_RADIUS)) |
| 434 | .bg(theme::row_hover()) |
| 435 | .debug_selector(|| PROPOSAL_CARD.into()) |
| 436 | .child(header) |
| 437 | .children(quote) |
| 438 | .child(body) |
| 439 | .children(images) |
| 440 | .child(buttons) |
| 441 | .children(free_writing) |
| 442 | } |
| 443 | |
| 444 | #[cfg(test)] |
| 445 | pub fn sample_png(side: u32, padded_to: usize) -> Vec<u8> { |
| 446 | let mut bytes = Vec::new(); |
| 447 | image::RgbaImage::new(side, side) |
| 448 | .write_to( |
| 449 | &mut std::io::Cursor::new(&mut bytes), |
| 450 | image::ImageFormat::Png, |
| 451 | ) |
| 452 | .unwrap(); |
| 453 | if bytes.len() < padded_to { |
| 454 | bytes.resize(padded_to, 0); |
| 455 | } |
| 456 | bytes |
| 457 | } |
| 458 | |
| 459 | #[cfg(test)] |
| 460 | pub fn sample_entry(text: &str, mentions: &[(&str, &str)], images: &[Vec<u8>]) -> ProposalEntry { |
| 461 | ProposalEntry { |
| 462 | body: std::sync::Arc::new(body_blocks(text, &[])), |
| 463 | mention_labels: Vec::new(), |
| 464 | proposal: mcp::Proposal { |
| 465 | id: "p1".to_owned(), |
| 466 | conversation_id: "c1".to_owned(), |
| 467 | reply_to_message_id: None, |
| 468 | text: text.to_owned(), |
| 469 | mentions: mentions |
| 470 | .iter() |
| 471 | .map(|(user_id, name)| mcp::ProposalMention { |
| 472 | user_id: (*user_id).to_owned(), |
| 473 | name: (*name).to_owned(), |
| 474 | }) |
| 475 | .collect(), |
| 476 | images: Vec::new(), |
| 477 | free_writing_ask: None, |
| 478 | agent: None, |
| 479 | session_id: None, |
| 480 | created_at: Utc::now(), |
| 481 | direct: false, |
| 482 | }, |
| 483 | images: images |
| 484 | .iter() |
| 485 | .enumerate() |
| 486 | .map(|(index, bytes)| ProposalPicture { |
| 487 | name: format!("image-{index}.png"), |
| 488 | bytes: std::sync::Arc::new(bytes.clone()), |
| 489 | thumbnail: None, |
| 490 | }) |
| 491 | .collect(), |
| 492 | status: crate::agent_api::ProposalStatus::Pending, |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | #[cfg(test)] |
| 497 | mod tests { |
| 498 | use chrono::{Duration, Utc}; |
| 499 | use teams_core::MentionInput; |
| 500 | |
| 501 | use super::{ |
| 502 | age_label, duration_label, free_writing_choices, needs_upload, outgoing_for, picture_files, sample_entry, |
| 503 | sample_png, thumbnail_size, |
| 504 | }; |
| 505 | use crate::views::attachment_tray::{AttachmentTray, INLINE_IMAGE_MAX_BYTES, OutgoingImage}; |
| 506 | |
| 507 | const TWO_MEGABYTES: usize = 2 * 1024 * 1024; |
| 508 | |
| 509 | #[test] |
| 510 | fn the_asked_window_joins_the_usual_choices() { |
| 511 | assert_eq!(free_writing_choices(60), [15, 60, 240]); |
| 512 | assert_eq!(free_writing_choices(30), [15, 30, 60, 240]); |
| 513 | assert_eq!(duration_label(15), "15 min"); |
| 514 | assert_eq!(duration_label(240), "4 h"); |
| 515 | assert_eq!(duration_label(90), "1 h 30 min"); |
| 516 | } |
| 517 | |
| 518 | #[test] |
| 519 | fn age_labels_follow_the_elapsed_time() { |
| 520 | let now = Utc::now(); |
| 521 | let label = |minutes: i64| age_label(now - Duration::minutes(minutes), now); |
| 522 | assert_eq!(label(0), "just now"); |
| 523 | assert_eq!(label(1), "1 min ago"); |
| 524 | assert_eq!(label(59), "59 min ago"); |
| 525 | assert_eq!(label(120), "2 h ago"); |
| 526 | assert_eq!(label(3000), "2 d ago"); |
| 527 | assert_eq!(age_label(now + Duration::minutes(5), now), "just now"); |
| 528 | } |
| 529 | |
| 530 | #[test] |
| 531 | fn thumbnails_keep_the_height_and_clamp_the_width() { |
| 532 | assert_eq!(thumbnail_size(Some((100, 100))), (48., 48.)); |
| 533 | assert_eq!(thumbnail_size(Some((1000, 100))), (120., 48.)); |
| 534 | assert_eq!(thumbnail_size(Some((10, 100))), (24., 48.)); |
| 535 | assert_eq!(thumbnail_size(None), (48., 48.)); |
| 536 | } |
| 537 | |
| 538 | #[test] |
| 539 | fn send_hands_images_and_mentions_to_the_outgoing_message() { |
| 540 | let entry = sample_entry( |
| 541 | "Thanks @Ada Example and @Bo", |
| 542 | &[("u1", "Ada Example"), ("u2", "Bo")], |
| 543 | &[sample_png(2, 0), sample_png(3, 0)], |
| 544 | ); |
| 545 | assert!(!needs_upload(&entry)); |
| 546 | let outgoing = outgoing_for(&entry, None); |
| 547 | assert_eq!( |
| 548 | outgoing.mentions, |
| 549 | [ |
| 550 | MentionInput::user("u1", "Ada Example"), |
| 551 | MentionInput::user("u2", "Bo") |
| 552 | ] |
| 553 | ); |
| 554 | assert_eq!(outgoing.images.len(), 2); |
| 555 | assert_eq!(outgoing.extras().images.len(), 2); |
| 556 | assert!(outgoing.html().starts_with("Thanks @Ada Example and @Bo")); |
| 557 | assert_eq!( |
| 558 | outgoing |
| 559 | .html() |
| 560 | .matches("<img src=\"../hostedContents/") |
| 561 | .count(), |
| 562 | 2 |
| 563 | ); |
| 564 | assert_eq!(outgoing.text().trim(), "Thanks @Ada Example and @Bo"); |
| 565 | } |
| 566 | |
| 567 | #[test] |
| 568 | fn a_proposal_without_extras_stays_plain_text() { |
| 569 | let outgoing = outgoing_for(&sample_entry("hello", &[], &[]), None); |
| 570 | assert!(outgoing.mentions.is_empty()); |
| 571 | assert!(outgoing.images.is_empty()); |
| 572 | assert_eq!(outgoing.html(), "hello"); |
| 573 | } |
| 574 | |
| 575 | #[test] |
| 576 | fn images_over_the_inline_limits_are_left_for_upload() { |
| 577 | let big = || sample_png(2, TWO_MEGABYTES); |
| 578 | let entry = sample_entry("two big ones", &[], &[big(), big()]); |
| 579 | assert!(needs_upload(&entry)); |
| 580 | let outgoing = outgoing_for(&entry, None); |
| 581 | assert!(outgoing.images.is_empty()); |
| 582 | assert_eq!(picture_files(&entry).len(), 2); |
| 583 | |
| 584 | let mixed = sample_entry("mixed", &[], &[sample_png(2, 0), big(), sample_png(3, 0)]); |
| 585 | assert!(needs_upload(&mixed)); |
| 586 | assert_eq!(outgoing_for(&mixed, None).images.len(), 2); |
| 587 | |
| 588 | let over_one = sample_entry( |
| 589 | "limit", |
| 590 | &[], |
| 591 | &[sample_png(2, INLINE_IMAGE_MAX_BYTES as usize + 1)], |
| 592 | ); |
| 593 | assert!(needs_upload(&over_one)); |
| 594 | let at_limit = sample_entry( |
| 595 | "limit", |
| 596 | &[], |
| 597 | &[sample_png(2, INLINE_IMAGE_MAX_BYTES as usize)], |
| 598 | ); |
| 599 | assert!(!needs_upload(&at_limit)); |
| 600 | } |
| 601 | |
| 602 | #[test] |
| 603 | fn the_total_inline_budget_counts_across_images() { |
| 604 | let almost_one_megabyte = || sample_png(2, 1024 * 1024); |
| 605 | let entry = sample_entry( |
| 606 | "three", |
| 607 | &[], |
| 608 | &[ |
| 609 | almost_one_megabyte(), |
| 610 | almost_one_megabyte(), |
| 611 | almost_one_megabyte(), |
| 612 | ], |
| 613 | ); |
| 614 | assert!(needs_upload(&entry)); |
| 615 | assert_eq!(outgoing_for(&entry, None).images.len(), 2); |
| 616 | } |
| 617 | |
| 618 | #[test] |
| 619 | fn inline_images_go_back_into_the_tray_in_order() { |
| 620 | let entry = sample_entry("look", &[], &[sample_png(2, 0), sample_png(3, 0)]); |
| 621 | let outgoing = outgoing_for(&entry, None); |
| 622 | let mut tray = AttachmentTray::default(); |
| 623 | let ids = tray.restore(&outgoing.images, &outgoing.files); |
| 624 | assert_eq!(ids.len(), 2); |
| 625 | let restored = tray.images_in(&ids); |
| 626 | assert_eq!(restored, outgoing.images); |
| 627 | assert!(matches!(restored[0], OutgoingImage::Inline(_))); |
| 628 | assert_eq!( |
| 629 | outgoing |
| 630 | .draft |
| 631 | .text() |
| 632 | .matches(teams_core::OBJECT_MARK) |
| 633 | .count(), |
| 634 | 2 |
| 635 | ); |
| 636 | } |
| 637 | } |