subspace-dashboard 0.3.0

Subspace Framework board kit: the Dashboard trait, shared widgets and design tokens. Internal crate; apps depend on `subspace-framework`.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! The chat input: a GPUI Kit [`InputGroup`] around a [`Textarea`] with
//! `submit_on_enter`, so Enter sends and Shift+Enter inserts a new line.
//! Kit reports Shift+Enter as a `PressEnter` too, after the line break, so
//! only a plain Enter sends. Option+Enter inserts a new line too, through
//! the `alt-enter` binding in this component's key context.
//!
//! The panel keeps the input in an `Entity<ChatInput>`, renders it and
//! subscribes to [`ChatInputEvent`]. The toolbar is passed in with
//! [`ChatInput::set_addon`], so this component knows nothing about it. The
//! shared comment [`crate::composer::Composer`] is a different interface and
//! stays unchanged.

use gpui_kit::component::input::{InputEvent, TextareaState};
use gpui_kit::{
    AnyView, App, AppContext as _, Context, Entity, EventEmitter, InteractiveElement as _,
    IntoElement, KeyBinding, KeyContext, OperatingSystem, ParentElement as _, Render, SharedString,
    Styled as _, Subscription, Window, actions, div,
};

use crate::tokens::Tokens;
use attachments::IngestionBatch;
use std::fmt;
pub mod attachments;
pub mod completion;
mod counter;
mod message;
pub use counter::DraftLimits;
#[cfg(test)]
mod layout_tests;
mod resize;
mod sizing;
mod view;

actions!(
    subspace_chat_input,
    [
        ChatNewline,
        ChatBlockedSubmit,
        ChatNext,
        ChatPrevious,
        ChatComplete,
        ChatCancel,
        ChatNewTab,
        ChatCloseTab,
        ChatNextTab,
        ChatPreviousTab,
        ChatRenameCancel,
        ChatSessionNext,
        ChatSessionPrevious,
        ChatSessionOpen,
        ChatSessionRename,
        ChatSessionDelete
    ]
);

pub const CHAT_INPUT_CONTEXT: &str = "SubspaceChatInput";
/// The chat panel's own context, which carries the tab keys.
pub const CHAT_PANEL_CONTEXT: &str = "SubspaceChat";
/// The rename editor's context, so Escape cancels only while it is open.
pub const CHAT_RENAME_CONTEXT: &str = "SubspaceChatRename";
/// The session list's context, which carries its own keys.
pub const CHAT_SESSIONS_CONTEXT: &str = "SubspaceChatSessions";
/// Added to the key context only while a completion list is open, so the
/// arrow keys keep moving the cursor the rest of the time.
pub const CHAT_SLASH_CONTEXT: &str = "SubspaceChatSlash";
const CHAT_BLOCKED_SUBMIT_CONTEXT: &str = "SubspaceChatBlockedSubmit";
/// Added to the key context of an input whose owner recalls sent lines with
/// Up and Down and completes with Tab, such as an IRC composer.
pub const CHAT_RECALL_CONTEXT: &str = "SubspaceChatRecall";
/// Added beside [`CHAT_RECALL_CONTEXT`] while the draft is empty, one line
/// or an unedited recalled line ([`ChatInput::set_recalled`]), so Up and
/// Down recall sent lines; in a multi-line draft they move the cursor
/// between its lines.
pub const CHAT_LINE_RECALL_CONTEXT: &str = "SubspaceChatLineRecall";
const MIN_ROWS: usize = 2;
const MAX_ROWS: usize = 12;

/// Binds Option+Enter ahead of the text area's own Enter binding, the
/// completion-list keys, which match only while the list is open, and the
/// tab keys of the chat panel. The shell calls this once, beside the other
/// key bindings.
pub fn bind_keys(cx: &mut App) {
    resize::bind_keys(cx);
    let input = Some("SubspaceChatInput > Input");
    let slash = Some("SubspaceChatSlash > Input");
    let blocked_submit = Some("SubspaceChatBlockedSubmit > Input");
    let panel = Some(CHAT_PANEL_CONTEXT);
    let rename = Some("SubspaceChatRename > Input");
    let sessions = Some(CHAT_SESSIONS_CONTEXT);
    let recall = Some("SubspaceChatRecall > Input");
    let line_recall = Some("SubspaceChatLineRecall > Input");
    cx.bind_keys([
        KeyBinding::new("up", ChatPrevious, line_recall),
        KeyBinding::new("down", ChatNext, line_recall),
        KeyBinding::new("tab", ChatComplete, recall),
        KeyBinding::new("alt-enter", ChatNewline, input),
        KeyBinding::new("down", ChatNext, slash),
        KeyBinding::new("up", ChatPrevious, slash),
        KeyBinding::new("tab", ChatComplete, slash),
        // Ahead of the text area's own Enter, which would also type a line
        // break where the platform delivers one as text.
        KeyBinding::new("enter", ChatComplete, slash),
        KeyBinding::new("enter", ChatBlockedSubmit, blocked_submit),
        KeyBinding::new("escape", ChatCancel, slash),
        KeyBinding::new("escape", ChatRenameCancel, rename),
        KeyBinding::new(crate::secondary!("t"), ChatNewTab, panel),
        KeyBinding::new(crate::secondary!("w"), ChatCloseTab, panel),
        KeyBinding::new(crate::secondary!("shift-]"), ChatNextTab, panel),
        KeyBinding::new(crate::secondary!("shift-["), ChatPreviousTab, panel),
        KeyBinding::new("down", ChatSessionNext, sessions),
        KeyBinding::new("up", ChatSessionPrevious, sessions),
        KeyBinding::new("enter", ChatSessionOpen, sessions),
        KeyBinding::new("f2", ChatSessionRename, sessions),
        KeyBinding::new("delete", ChatSessionDelete, sessions),
        KeyBinding::new("backspace", ChatSessionDelete, sessions),
    ]);
}

#[derive(Clone)]
pub struct AttachmentPaste(pub(crate) IngestionBatch);

impl fmt::Debug for AttachmentPaste {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(formatter)
    }
}

impl AttachmentPaste {
    pub fn batch(&self) -> &IngestionBatch {
        &self.0
    }
}

#[derive(Debug, Clone)]
pub enum ChatInputEvent {
    /// Enter with text to send: already trimmed. Empty only when the owner
    /// opted in with [`ChatInput::set_submit_empty`].
    Submit(String),
    /// Enter or Tab while a completion list is open: the list completes.
    Complete,
    /// Arrow down or up while the list is open.
    Next,
    Previous,
    /// Escape while the list is open.
    Cancel,
    /// The text changed, for the `/` list.
    Changed(String),
    /// Non-text clipboard entries intercepted by the actual textarea paste action.
    PasteAttachments(AttachmentPaste),
}

/// The text Enter sends, if it sends anything. Empty text sends nothing. A
/// running turn is no longer a reason to hold the text back: the message
/// steers that turn.
pub fn submission(text: &str) -> Option<String> {
    let text = text.trim();
    (!text.is_empty()).then(|| text.to_owned())
}

// qual:allow(srp, god_struct) reason: "ChatInput is the shared composer facade; each setter is an owner option on one native textarea, and set_submit_empty is one more such option."
pub struct ChatInput {
    state: Entity<TextareaState>,
    attachment_budget: attachments::AttachmentBudget,
    addon: Option<AnyView>,
    /// A completion list is open, so Enter completes instead of sending.
    completing: bool,
    /// The chat shown takes no message, so the text area is disabled.
    disabled: bool,
    /// Up, Down and Tab go to the owner as Previous, Next and Complete.
    recall: bool,
    /// The draft is a line the owner recalled, still unedited, so Up and
    /// Down keep recalling even when it has several lines.
    recalled: bool,
    message: Option<message::MessageState>,
    limits: Option<DraftLimits>,
    row_limits: (usize, usize),
    /// Enter with an empty draft still submits, for an owner that sends
    /// something else with the text, such as attachments. Off by default.
    submit_empty: bool,
    _subscription: Subscription,
}

impl EventEmitter<ChatInputEvent> for ChatInput {}

impl ChatInput {
    /// A composer that asks `engine`, the chat engine's display name.
    pub fn new(engine: &str, window: &mut Window, cx: &mut Context<Self>) -> Self {
        Self::with_placeholder(format!("Ask {engine}, / for commands"), window, cx)
    }

    fn with_placeholder(placeholder: String, window: &mut Window, cx: &mut Context<Self>) -> Self {
        let state = cx.new(|cx| {
            TextareaState::new(window, cx)
                .placeholder(placeholder)
                .submit_on_enter(true)
                .auto_grow(MIN_ROWS, MAX_ROWS)
        });
        let subscription = cx.subscribe(&state, Self::on_input_event);
        Self {
            state,
            attachment_budget: attachments::AttachmentBudget::empty_draft(),
            addon: None,
            completing: false,
            disabled: false,
            recall: false,
            recalled: false,
            message: None,
            limits: None,
            row_limits: (MIN_ROWS, MAX_ROWS),
            submit_empty: false,
            _subscription: subscription,
        }
    }

    /// Whether Enter on an empty draft submits an empty text. Off by
    /// default; an owner turns it on while it has something else to send.
    // qual:api - Apps use this to submit attachment-only drafts.
    pub fn set_submit_empty(&mut self, submit_empty: bool, cx: &mut Context<Self>) {
        if self.submit_empty != submit_empty {
            self.submit_empty = submit_empty;
            cx.notify();
        }
    }

    /// The toolbar rendered in the group's bottom addon.
    pub fn set_addon(&mut self, addon: Option<AnyView>, cx: &mut Context<Self>) {
        self.addon = addon;
        cx.notify();
    }

    pub fn text(&self, cx: &App) -> String {
        self.state.read(cx).value().to_string()
    }

    /// Opts into the in-frame counter; the host owns protocol validation.
    /// Other hosts leave this unset and show no counter.
    pub fn set_limits(&mut self, limits: Option<DraftLimits>, cx: &mut Context<Self>) {
        if self.limits != limits {
            self.limits = limits;
            cx.notify();
        }
    }

    fn counter(&self, cx: &App) -> Option<counter::Counter> {
        self.limits?.counter(&self.text(cx))
    }

    pub fn set_text(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) {
        let text = text.to_owned();
        self.state
            .update(cx, |state, cx| state.set_value(text, window, cx));
        cx.notify();
    }

    /// The hint shown while the draft is empty, such as `Message #channel`.
    pub fn set_placeholder(
        &mut self,
        placeholder: impl Into<SharedString>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let placeholder = placeholder.into();
        self.state.update(cx, |state, cx| {
            state.set_placeholder(placeholder, window, cx)
        });
    }

    pub fn clear(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.set_text("", window, cx);
    }

    /// The text cursor's byte offset, for completing the word before it.
    pub fn cursor(&self, cx: &App) -> usize {
        self.state.read(cx).cursor()
    }

    /// Replaces the bytes in `range` with `text`, leaving the cursor after
    /// it, as an edit the user can undo. The text then reports
    /// [`ChatInputEvent::Changed`].
    pub fn replace_range(
        &mut self,
        range: std::ops::Range<usize>,
        text: &str,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let text = text.to_owned();
        self.state.update(cx, |state, cx| {
            state.set_selected_range(range, cx);
            state.replace(text, window, cx);
        });
        cx.notify();
    }

    pub fn focus(&self, window: &mut Window, cx: &mut App) {
        self.state.update(cx, |state, cx| state.focus(window, cx));
    }

    /// While a completion list is open, Enter emits
    /// [`ChatInputEvent::Complete`] rather than sending.
    pub fn set_completing(&mut self, completing: bool, cx: &mut Context<Self>) {
        self.completing = completing;
        cx.notify();
    }

    /// Up and Down emit [`ChatInputEvent::Previous`] and
    /// [`ChatInputEvent::Next`], and Tab emits [`ChatInputEvent::Complete`],
    /// for an owner that recalls sent lines and completes names. The arrow
    /// keys recall only while the draft is empty, one line or recalled; in
    /// a multi-line draft they still move the cursor between lines.
    // qual:api
    pub fn set_recall_keys(&mut self, recall: bool, cx: &mut Context<Self>) {
        self.recall = recall;
        cx.notify();
    }

    /// Whether the draft is a line the owner recalled and the user has not
    /// edited. Up and Down then keep recalling even when it has several
    /// lines, so a multi-line message does not stop the walk through the
    /// sent lines.
    // qual:api
    pub fn set_recalled(&mut self, recalled: bool, cx: &mut Context<Self>) {
        if self.recalled != recalled {
            self.recalled = recalled;
            cx.notify();
        }
    }

    /// Disables the text area, for a chat that takes no message, such as a
    /// replay of a cloud agent session.
    pub fn set_disabled(&mut self, disabled: bool, cx: &mut Context<Self>) {
        self.disabled = disabled;
        cx.notify();
    }

    fn on_input_event(
        &mut self,
        _: Entity<TextareaState>,
        event: &InputEvent,
        cx: &mut Context<Self>,
    ) {
        match event {
            InputEvent::Change => {
                if let Some(message) = &mut self.message {
                    message.error = None;
                }
                let text = self.text(cx);
                cx.emit(ChatInputEvent::Changed(text));
                cx.notify();
            }
            InputEvent::PressEnter { shift: false, .. } => self.enter(cx),
            _ => {}
        }
    }

    fn enter(&mut self, cx: &mut Context<Self>) {
        if self.disabled
            || self
                .message
                .as_ref()
                .is_some_and(|message| message.submit_blocked)
        {
            return;
        }
        if self.completing {
            cx.emit(ChatInputEvent::Complete);
            return;
        }
        match submission(&self.text(cx)) {
            Some(text) => cx.emit(ChatInputEvent::Submit(text)),
            None if self.submit_empty => cx.emit(ChatInputEvent::Submit(String::new())),
            None => {}
        }
    }

    fn newline(&mut self, _: &ChatNewline, window: &mut Window, cx: &mut Context<Self>) {
        self.state
            .update(cx, |state, cx| state.insert("\n", window, cx));
    }

    /// The completion-list keys match only while the list is open, and the
    /// recall arrows only while the draft is `single_line` or recalled.
    fn key_context(&self, os: OperatingSystem, single_line: bool) -> KeyContext {
        let mut context = KeyContext::new_with_defaults(os);
        context.add(CHAT_INPUT_CONTEXT);
        if self
            .message
            .as_ref()
            .is_some_and(|message| message.submit_blocked)
        {
            context.add(CHAT_BLOCKED_SUBMIT_CONTEXT);
        } else if self.completing {
            context.add(CHAT_SLASH_CONTEXT);
        }
        if self.recall {
            context.add(CHAT_RECALL_CONTEXT);
            if single_line || self.recalled {
                context.add(CHAT_LINE_RECALL_CONTEXT);
            }
        }
        context
    }

    /// The draft has no line break.
    fn single_line(&self, cx: &App) -> bool {
        !self.state.read(cx).value().contains('\n')
    }
}

impl Render for ChatInput {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        resize::sync_rows(self, window, cx);
        let tokens = Tokens::current(cx);
        div()
            .id("chat-input-frame")
            .relative()
            .w_full()
            .min_w_0()
            .flex_shrink_0()
            .border_t_1()
            .border_color(tokens.line)
            .key_context(self.key_context(cx.platform_info().os, self.single_line(cx)))
            .on_action(cx.listener(Self::newline))
            .on_action(|_: &ChatBlockedSubmit, _, _| {})
            .on_action(cx.listener(|_, _: &ChatNext, _, cx| cx.emit(ChatInputEvent::Next)))
            .on_action(cx.listener(|_, _: &ChatPrevious, _, cx| cx.emit(ChatInputEvent::Previous)))
            .on_action(cx.listener(|_, _: &ChatComplete, _, cx| cx.emit(ChatInputEvent::Complete)))
            .on_action(cx.listener(|_, _: &ChatCancel, _, cx| cx.emit(ChatInputEvent::Cancel)))
            .text_size(tokens.row_font)
            .on_drag_move::<resize::InputResize>(cx.listener(resize::drag))
            .child(view::group(self, window, cx))
            .children(resize::handle(self, cx))
    }
}

#[cfg(test)]
mod tests {
    use super::{ChatInput, ChatInputEvent, submission};
    use crate::chat_input::attachments::{AttachmentSource, MAX_DRAFT_IMAGES};
    use gpui_kit::component::input::Paste;
    use gpui_kit::{
        AppContext as _, ClipboardEntry, ClipboardItem, Entity, Image, ImageFormat, IntoElement,
        ParentElement as _, Render, Subscription, TestAppContext, Window, div,
    };
    use std::cell::RefCell;
    use std::rc::Rc;
    use subspace_agent_model::DRAFT_ATTACHMENT_LIMIT;

    struct PasteHarness {
        input: Entity<ChatInput>,
        _subscription: Subscription,
    }

    impl PasteHarness {
        fn new(
            events: Rc<RefCell<Vec<ChatInputEvent>>>,
            window: &mut Window,
            cx: &mut gpui_kit::Context<Self>,
        ) -> Self {
            let input = cx.new(|cx| ChatInput::new("Copilot", window, cx));
            let subscription = cx.subscribe(&input, move |_, _, event, _| {
                events.borrow_mut().push(event.clone());
            });
            Self {
                input,
                _subscription: subscription,
            }
        }
    }

    impl Render for PasteHarness {
        fn render(&mut self, _: &mut Window, _: &mut gpui_kit::Context<Self>) -> impl IntoElement {
            div().child(self.input.clone())
        }
    }

    #[test]
    fn enter_sends_trimmed_text_and_nothing_else() {
        assert_eq!(submission("  hello \n").as_deref(), Some("hello"));
        assert_eq!(submission("   "), None, "empty text");
        assert_eq!(
            submission("steer left").as_deref(),
            Some("steer left"),
            "a running turn no longer blocks the input"
        );
    }

    #[gpui_kit::test]
    fn empty_enter_submits_only_when_the_owner_opts_in(cx: &mut TestAppContext) {
        cx.update(gpui_kit::init);
        let events = Rc::new(RefCell::new(Vec::new()));
        let captured = events.clone();
        let (harness, cx) =
            cx.add_window_view(|window, cx| PasteHarness::new(captured, window, cx));
        let input = harness.read_with(cx, |harness, _| harness.input.clone());
        input.update(cx, |input, cx| input.enter(cx));
        assert!(events.borrow().is_empty(), "off by default");
        input.update(cx, |input, cx| {
            input.set_submit_empty(true, cx);
            input.enter(cx);
        });
        assert!(matches!(
            events.borrow().as_slice(),
            [ChatInputEvent::Submit(text)] if text.is_empty()
        ));
    }

    #[gpui_kit::test]
    fn textarea_paste_emits_images_but_preserves_ordinary_text(cx: &mut TestAppContext) {
        cx.update(gpui_kit::init);
        let events = Rc::new(RefCell::new(Vec::new()));
        let captured = events.clone();
        let (harness, cx) =
            cx.add_window_view(|window, cx| PasteHarness::new(captured, window, cx));
        cx.update(|window, cx| {
            let input = harness.read(cx).input.clone();
            input.update(cx, |input, cx| input.focus(window, cx));
            let _ = window.draw(cx);
            cx.write_to_clipboard(ClipboardItem::new_image(&Image::from_bytes(
                ImageFormat::Png,
                vec![1, 2, 3],
            )));
            window.dispatch_action(Box::new(Paste), cx);
        });
        cx.run_until_parked();
        cx.update(|window, cx| {
            cx.write_to_clipboard(ClipboardItem::new_string("ordinary text".into()));
            window.dispatch_action(Box::new(Paste), cx);
        });
        cx.run_until_parked();
        assert!(
            events.borrow().iter().any(|event| matches!(
                event,
                ChatInputEvent::PasteAttachments(super::AttachmentPaste(batch))
                    if matches!(batch.sources.as_slice(), [AttachmentSource::ClipboardImage { .. }])
            )),
            "the actual textarea callback should emit one bounded image batch"
        );
        assert_eq!(
            cx.update(|_, cx| harness.read(cx).input.read(cx).text(cx)),
            "ordinary text"
        );
    }

    #[gpui_kit::test]
    fn textarea_paste_never_emits_unbounded_image_payloads(cx: &mut TestAppContext) {
        cx.update(gpui_kit::init);
        let events = Rc::new(RefCell::new(Vec::new()));
        let captured = events.clone();
        let (harness, cx) =
            cx.add_window_view(|window, cx| PasteHarness::new(captured, window, cx));
        cx.update(|window, cx| {
            let input = harness.read(cx).input.clone();
            input.update(cx, |input, cx| input.focus(window, cx));
            let _ = window.draw(cx);
            let entries =
                std::iter::once(ClipboardEntry::Image(Image::from_bytes(
                    ImageFormat::Png,
                    vec![0; crate::images::MAX_ENCODED_BYTES + 1],
                )))
                .chain((0..3).map(|_| {
                    ClipboardEntry::Image(Image::from_bytes(
                        ImageFormat::Png,
                        vec![1_u8; crate::images::MAX_ENCODED_BYTES],
                    ))
                }))
                .chain((0..MAX_DRAFT_IMAGES + 20).map(|_| {
                    ClipboardEntry::Image(Image::from_bytes(ImageFormat::Png, vec![1_u8; 1]))
                }))
                .collect();
            cx.write_to_clipboard(ClipboardItem { entries });
            window.dispatch_action(Box::new(Paste), cx);
        });
        cx.run_until_parked();
        let events = events.borrow();
        let Some(ChatInputEvent::PasteAttachments(super::AttachmentPaste(batch))) = events.last()
        else {
            panic!("the actual textarea callback did not emit attachments");
        };
        let image_bytes = batch
            .sources
            .iter()
            .filter_map(|source| match source {
                AttachmentSource::ClipboardImage { bytes, .. } => Some(bytes.len()),
                AttachmentSource::Path(_) => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(image_bytes.len(), MAX_DRAFT_IMAGES);
        assert_eq!(
            image_bytes
                .iter()
                .filter(|size| **size == crate::images::MAX_ENCODED_BYTES)
                .count(),
            2
        );
        assert!(image_bytes.iter().sum::<usize>() <= DRAFT_ATTACHMENT_LIMIT);
        assert!(batch.notice.is_some());
    }
}