revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! Worker channel for communication between workers and UI

use std::collections::VecDeque;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

use crate::utils::lock as lock_util;

/// Shared inner state for worker channel
/// This reduces Arc clones from 10 to 6 (3 per split, shared between sender/receiver)
struct ChannelInner<T> {
    /// Messages from worker to UI
    to_ui: Arc<Mutex<VecDeque<WorkerMessage<T>>>>,
    /// Messages from UI to worker
    to_worker: Arc<Mutex<VecDeque<WorkerCommand>>>,
    /// Channel capacity
    capacity: usize,
    /// Atomic counter for lock-free message count reads
    message_count: Arc<AtomicUsize>,
    /// Atomic counter for lock-free command count reads
    command_count: Arc<AtomicUsize>,
}

impl<T: Clone> Clone for ChannelInner<T> {
    fn clone(&self) -> Self {
        Self {
            to_ui: Arc::clone(&self.to_ui),
            to_worker: Arc::clone(&self.to_worker),
            capacity: self.capacity,
            message_count: Arc::clone(&self.message_count),
            command_count: Arc::clone(&self.command_count),
        }
    }
}

/// Message types for worker communication
#[derive(Debug, Clone)]
pub enum WorkerMessage<T> {
    /// Progress update (0.0 to 1.0)
    Progress(f32),
    /// Status message
    Status(String),
    /// Partial result
    Partial(T),
    /// Final result
    Complete(T),
    /// Error occurred
    Error(String),
    /// Custom message
    Custom(String),
}

/// Bidirectional channel for worker communication
pub struct WorkerChannel<T> {
    inner: ChannelInner<T>,
}

impl<T: Clone> WorkerChannel<T> {
    /// Create a new channel with default capacity
    pub fn new() -> Self {
        Self::with_capacity(100)
    }

    /// Create with specific capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            inner: ChannelInner {
                to_ui: Arc::new(Mutex::new(VecDeque::with_capacity(capacity))),
                to_worker: Arc::new(Mutex::new(VecDeque::with_capacity(capacity))),
                capacity,
                message_count: Arc::new(AtomicUsize::new(0)),
                command_count: Arc::new(AtomicUsize::new(0)),
            },
        }
    }

    /// Create a sender/receiver pair
    pub fn split(&self) -> (WorkerSender<T>, WorkerReceiver<T>) {
        (
            WorkerSender {
                inner: self.inner.clone(),
            },
            WorkerReceiver {
                inner: self.inner.clone(),
            },
        )
    }

    /// Send message from worker side
    pub fn send(&self, msg: WorkerMessage<T>) -> bool {
        let mut queue = lock_util::lock_or_recover(&self.inner.to_ui);
        if queue.len() < self.inner.capacity {
            queue.push_back(msg);
            self.inner.message_count.fetch_add(1, Ordering::Release);
            true
        } else {
            log_warn!(
                "Worker channel overflow: message dropped (queue full at {} items)",
                self.inner.capacity
            );
            false
        }
    }

    /// Receive message on UI side
    pub fn recv(&self) -> Option<WorkerMessage<T>> {
        let mut queue = lock_util::lock_or_recover(&self.inner.to_ui);
        let msg = queue.pop_front();
        if msg.is_some() {
            self.inner.message_count.fetch_sub(1, Ordering::Release);
        }
        msg
    }

    /// Send command from UI to worker
    pub fn send_command(&self, cmd: WorkerCommand) -> bool {
        let mut queue = lock_util::lock_or_recover(&self.inner.to_worker);
        if queue.len() < self.inner.capacity {
            queue.push_back(cmd);
            self.inner.command_count.fetch_add(1, Ordering::Release);
            true
        } else {
            log_warn!(
                "Worker channel overflow: command {:?} dropped (queue full at {} items)",
                cmd,
                self.inner.capacity
            );
            false
        }
    }

    /// Receive command on worker side
    pub fn recv_command(&self) -> Option<WorkerCommand> {
        let mut queue = lock_util::lock_or_recover(&self.inner.to_worker);
        let cmd = queue.pop_front();
        if cmd.is_some() {
            self.inner.command_count.fetch_sub(1, Ordering::Release);
        }
        cmd
    }

    /// Check if there are pending messages for UI (lock-free)
    pub fn has_messages(&self) -> bool {
        self.inner.message_count.load(Ordering::Acquire) > 0
    }

    /// Check if there are pending commands for worker (lock-free)
    pub fn has_commands(&self) -> bool {
        self.inner.command_count.load(Ordering::Acquire) > 0
    }

    /// Get number of pending messages (lock-free)
    pub fn message_count(&self) -> usize {
        self.inner.message_count.load(Ordering::Acquire)
    }
}

impl<T: Clone> Default for WorkerChannel<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Clone> Clone for WorkerChannel<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

/// Commands from UI to worker
#[derive(Debug, Clone)]
pub enum WorkerCommand {
    /// Cancel the task
    Cancel,
    /// Pause the task
    Pause,
    /// Resume the task
    Resume,
    /// Custom command
    Custom(String),
}

/// Sender half of worker channel (used by worker)
pub struct WorkerSender<T> {
    inner: ChannelInner<T>,
}

impl<T: Clone> WorkerSender<T> {
    /// Send message to UI
    pub fn send(&self, msg: WorkerMessage<T>) -> bool {
        let mut queue = lock_util::lock_or_recover(&self.inner.to_ui);
        if queue.len() < self.inner.capacity {
            queue.push_back(msg);
            self.inner.message_count.fetch_add(1, Ordering::Release);
            true
        } else {
            false
        }
    }

    /// Send progress update
    pub fn progress(&self, value: f32) -> bool {
        self.send(WorkerMessage::Progress(value.clamp(0.0, 1.0)))
    }

    /// Send status message
    pub fn status(&self, msg: impl Into<String>) -> bool {
        self.send(WorkerMessage::Status(msg.into()))
    }

    /// Send partial result
    pub fn partial(&self, value: T) -> bool {
        self.send(WorkerMessage::Partial(value))
    }

    /// Send complete message
    pub fn complete(&self, value: T) -> bool {
        self.send(WorkerMessage::Complete(value))
    }

    /// Send error
    pub fn error(&self, msg: impl Into<String>) -> bool {
        self.send(WorkerMessage::Error(msg.into()))
    }

    /// Check for commands from UI
    pub fn check_command(&self) -> Option<WorkerCommand> {
        let mut queue = lock_util::lock_or_recover(&self.inner.to_worker);
        let cmd = queue.pop_front();
        if cmd.is_some() {
            self.inner.command_count.fetch_sub(1, Ordering::Release);
        }
        cmd
    }

    /// Check if cancelled (lock-free check for command count, full check for Cancel command)
    pub fn is_cancelled(&self) -> bool {
        if self.inner.command_count.load(Ordering::Acquire) == 0 {
            return false;
        }
        let queue = lock_util::lock_or_recover(&self.inner.to_worker);
        queue.iter().any(|cmd| matches!(cmd, WorkerCommand::Cancel))
    }
}

impl<T: Clone> Clone for WorkerSender<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

/// Receiver half of worker channel (used by UI)
pub struct WorkerReceiver<T> {
    inner: ChannelInner<T>,
}

impl<T: Clone> WorkerReceiver<T> {
    /// Receive message from worker
    pub fn recv(&self) -> Option<WorkerMessage<T>> {
        let mut queue = lock_util::lock_or_recover(&self.inner.to_ui);
        let msg = queue.pop_front();
        if msg.is_some() {
            self.inner.message_count.fetch_sub(1, Ordering::Release);
        }
        msg
    }

    /// Receive all pending messages
    pub fn recv_all(&self) -> Vec<WorkerMessage<T>> {
        let mut queue = lock_util::lock_or_recover(&self.inner.to_ui);
        let count = queue.len();
        let messages: Vec<WorkerMessage<T>> = queue.drain(..).collect();
        // Update the atomic counter
        if count > 0 {
            self.inner.message_count.fetch_sub(count, Ordering::Release);
        }
        messages
    }

    /// Send command to worker
    pub fn send_command(&self, cmd: WorkerCommand) -> bool {
        let mut queue = lock_util::lock_or_recover(&self.inner.to_worker);
        queue.push_back(cmd);
        self.inner.command_count.fetch_add(1, Ordering::Release);
        true
    }

    /// Send cancel command
    pub fn cancel(&self) -> bool {
        self.send_command(WorkerCommand::Cancel)
    }

    /// Send pause command
    pub fn pause(&self) -> bool {
        self.send_command(WorkerCommand::Pause)
    }

    /// Send resume command
    pub fn resume(&self) -> bool {
        self.send_command(WorkerCommand::Resume)
    }

    /// Check if there are pending messages (lock-free)
    pub fn has_messages(&self) -> bool {
        self.inner.message_count.load(Ordering::Acquire) > 0
    }

    /// Get message count (lock-free)
    pub fn message_count(&self) -> usize {
        self.inner.message_count.load(Ordering::Acquire)
    }
}

impl<T: Clone> Clone for WorkerReceiver<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_channel_send_recv() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();

        channel.send(WorkerMessage::Progress(0.5));
        channel.send(WorkerMessage::Status("working".to_string()));
        channel.send(WorkerMessage::Complete(42));

        assert!(
            matches!(channel.recv(), Some(WorkerMessage::Progress(p)) if (p - 0.5).abs() < 0.01)
        );
        assert!(matches!(channel.recv(), Some(WorkerMessage::Status(_))));
        assert!(matches!(channel.recv(), Some(WorkerMessage::Complete(42))));
        assert!(channel.recv().is_none());
    }

    #[test]
    fn test_channel_split() {
        let channel: WorkerChannel<String> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        sender.progress(0.75);
        sender.status("Loading...");
        sender.complete("Done".to_string());

        let messages = receiver.recv_all();
        assert_eq!(messages.len(), 3);
    }

    #[test]
    fn test_commands() {
        let channel: WorkerChannel<()> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        receiver.cancel();
        assert!(sender.is_cancelled());
    }

    // =========================================================================
    // WorkerMessage tests
    // =========================================================================

    #[test]
    fn test_worker_message_progress() {
        let msg: WorkerMessage<i32> = WorkerMessage::Progress(0.5);
        assert!(matches!(msg, WorkerMessage::Progress(p) if (p - 0.5).abs() < 0.01));
    }

    #[test]
    fn test_worker_message_status() {
        let msg: WorkerMessage<i32> = WorkerMessage::Status("working".to_string());
        assert!(matches!(msg, WorkerMessage::Status(s) if s == "working"));
    }

    #[test]
    fn test_worker_message_partial() {
        let msg: WorkerMessage<i32> = WorkerMessage::Partial(42);
        assert!(matches!(msg, WorkerMessage::Partial(42)));
    }

    #[test]
    fn test_worker_message_complete() {
        let msg: WorkerMessage<i32> = WorkerMessage::Complete(100);
        assert!(matches!(msg, WorkerMessage::Complete(100)));
    }

    #[test]
    fn test_worker_message_error() {
        let msg: WorkerMessage<i32> = WorkerMessage::Error("failed".to_string());
        assert!(matches!(msg, WorkerMessage::Error(e) if e == "failed"));
    }

    #[test]
    fn test_worker_message_custom() {
        let msg: WorkerMessage<i32> = WorkerMessage::Custom("custom data".to_string());
        assert!(matches!(msg, WorkerMessage::Custom(c) if c == "custom data"));
    }

    #[test]
    fn test_worker_message_clone() {
        let msg: WorkerMessage<i32> = WorkerMessage::Complete(42);
        let cloned = msg.clone();
        assert!(matches!(cloned, WorkerMessage::Complete(42)));
    }

    // =========================================================================
    // WorkerChannel tests
    // =========================================================================

    #[test]
    fn test_channel_default() {
        let channel: WorkerChannel<i32> = WorkerChannel::default();
        assert!(!channel.has_messages());
        assert!(!channel.has_commands());
    }

    #[test]
    fn test_channel_with_capacity() {
        let channel: WorkerChannel<i32> = WorkerChannel::with_capacity(10);
        assert_eq!(channel.message_count(), 0);
    }

    #[test]
    fn test_channel_has_messages() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        assert!(!channel.has_messages());

        channel.send(WorkerMessage::Complete(42));
        assert!(channel.has_messages());
    }

    #[test]
    fn test_channel_has_commands() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        assert!(!channel.has_commands());

        channel.send_command(WorkerCommand::Cancel);
        assert!(channel.has_commands());
    }

    #[test]
    fn test_channel_message_count() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        assert_eq!(channel.message_count(), 0);

        channel.send(WorkerMessage::Progress(0.1));
        channel.send(WorkerMessage::Progress(0.2));
        channel.send(WorkerMessage::Progress(0.3));
        assert_eq!(channel.message_count(), 3);
    }

    #[test]
    fn test_channel_recv_command() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        channel.send_command(WorkerCommand::Pause);

        let cmd = channel.recv_command();
        assert!(matches!(cmd, Some(WorkerCommand::Pause)));
        assert!(channel.recv_command().is_none());
    }

    #[test]
    fn test_channel_clone() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        channel.send(WorkerMessage::Complete(42));

        let cloned = channel.clone();
        // Both should share the same queue
        let msg = cloned.recv();
        assert!(matches!(msg, Some(WorkerMessage::Complete(42))));
    }

    // =========================================================================
    // WorkerCommand tests
    // =========================================================================

    #[test]
    fn test_worker_command_cancel() {
        let cmd = WorkerCommand::Cancel;
        assert!(matches!(cmd, WorkerCommand::Cancel));
    }

    #[test]
    fn test_worker_command_pause() {
        let cmd = WorkerCommand::Pause;
        assert!(matches!(cmd, WorkerCommand::Pause));
    }

    #[test]
    fn test_worker_command_resume() {
        let cmd = WorkerCommand::Resume;
        assert!(matches!(cmd, WorkerCommand::Resume));
    }

    #[test]
    fn test_worker_command_custom() {
        let cmd = WorkerCommand::Custom("stop-early".to_string());
        assert!(matches!(cmd, WorkerCommand::Custom(s) if s == "stop-early"));
    }

    #[test]
    fn test_worker_command_clone() {
        let cmd = WorkerCommand::Custom("test".to_string());
        let cloned = cmd.clone();
        assert!(matches!(cloned, WorkerCommand::Custom(s) if s == "test"));
    }

    // =========================================================================
    // WorkerSender tests
    // =========================================================================

    #[test]
    fn test_sender_progress() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(sender.progress(0.5));
        let msg = receiver.recv();
        assert!(matches!(msg, Some(WorkerMessage::Progress(p)) if (p - 0.5).abs() < 0.01));
    }

    #[test]
    fn test_sender_progress_clamp() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        // Should clamp to 0.0-1.0 range
        assert!(sender.progress(-0.5));
        assert!(sender.progress(1.5));

        let msg1 = receiver.recv();
        let msg2 = receiver.recv();
        assert!(matches!(msg1, Some(WorkerMessage::Progress(p)) if (p - 0.0).abs() < 0.01));
        assert!(matches!(msg2, Some(WorkerMessage::Progress(p)) if (p - 1.0).abs() < 0.01));
    }

    #[test]
    fn test_sender_status() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(sender.status("Processing..."));
        let msg = receiver.recv();
        assert!(matches!(msg, Some(WorkerMessage::Status(s)) if s == "Processing..."));
    }

    #[test]
    fn test_sender_partial() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(sender.partial(42));
        let msg = receiver.recv();
        assert!(matches!(msg, Some(WorkerMessage::Partial(42))));
    }

    #[test]
    fn test_sender_complete() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(sender.complete(100));
        let msg = receiver.recv();
        assert!(matches!(msg, Some(WorkerMessage::Complete(100))));
    }

    #[test]
    fn test_sender_error() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(sender.error("Something went wrong"));
        let msg = receiver.recv();
        assert!(matches!(msg, Some(WorkerMessage::Error(e)) if e == "Something went wrong"));
    }

    #[test]
    fn test_sender_check_command() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        receiver.send_command(WorkerCommand::Pause);
        let cmd = sender.check_command();
        assert!(matches!(cmd, Some(WorkerCommand::Pause)));
    }

    #[test]
    fn test_sender_is_cancelled() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(!sender.is_cancelled());
        receiver.cancel();
        assert!(sender.is_cancelled());
    }

    #[test]
    fn test_sender_clone() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, _receiver) = channel.split();

        let cloned = sender.clone();
        cloned.progress(0.5);
        // Both senders share the same queue
        assert_eq!(channel.message_count(), 1);
    }

    // =========================================================================
    // WorkerReceiver tests
    // =========================================================================

    #[test]
    fn test_receiver_recv_all() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        sender.progress(0.1);
        sender.progress(0.2);
        sender.progress(0.3);

        let messages = receiver.recv_all();
        assert_eq!(messages.len(), 3);
        assert!(receiver.recv_all().is_empty());
    }

    #[test]
    fn test_receiver_cancel() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(receiver.cancel());
        assert!(sender.is_cancelled());
    }

    #[test]
    fn test_receiver_pause() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(receiver.pause());
        let cmd = sender.check_command();
        assert!(matches!(cmd, Some(WorkerCommand::Pause)));
    }

    #[test]
    fn test_receiver_resume() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(receiver.resume());
        let cmd = sender.check_command();
        assert!(matches!(cmd, Some(WorkerCommand::Resume)));
    }

    #[test]
    fn test_receiver_has_messages() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert!(!receiver.has_messages());
        sender.complete(42);
        assert!(receiver.has_messages());
    }

    #[test]
    fn test_receiver_message_count() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        assert_eq!(receiver.message_count(), 0);
        sender.progress(0.1);
        sender.progress(0.2);
        assert_eq!(receiver.message_count(), 2);
    }

    #[test]
    fn test_receiver_clone() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        sender.complete(42);
        let cloned = receiver.clone();
        // Both receivers share the same queue
        let msg = cloned.recv();
        assert!(matches!(msg, Some(WorkerMessage::Complete(42))));
        assert!(receiver.recv().is_none());
    }

    // =========================================================================
    // Integration tests
    // =========================================================================

    #[test]
    fn test_bidirectional_communication() {
        let channel: WorkerChannel<String> = WorkerChannel::new();
        let (sender, receiver) = channel.split();

        // Worker sends progress
        sender.progress(0.25);
        sender.status("Started");

        // UI receives
        let _ = receiver.recv();
        let _ = receiver.recv();

        // UI sends command
        receiver.pause();

        // Worker receives command
        let cmd = sender.check_command();
        assert!(matches!(cmd, Some(WorkerCommand::Pause)));

        // Worker continues
        sender.complete("Done".to_string());

        let msg = receiver.recv();
        assert!(matches!(msg, Some(WorkerMessage::Complete(s)) if s == "Done"));
    }

    #[test]
    fn test_multiple_messages_fifo() {
        let channel: WorkerChannel<i32> = WorkerChannel::new();

        channel.send(WorkerMessage::Progress(0.1));
        channel.send(WorkerMessage::Progress(0.2));
        channel.send(WorkerMessage::Progress(0.3));

        // Should receive in FIFO order
        assert!(
            matches!(channel.recv(), Some(WorkerMessage::Progress(p)) if (p - 0.1).abs() < 0.01)
        );
        assert!(
            matches!(channel.recv(), Some(WorkerMessage::Progress(p)) if (p - 0.2).abs() < 0.01)
        );
        assert!(
            matches!(channel.recv(), Some(WorkerMessage::Progress(p)) if (p - 0.3).abs() < 0.01)
        );
    }
}