mach-tui 0.4.0

A terminal-first task manager for people who live in the shell and work with agents
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//! The task dialog — title, description, due date and subtasks — used for
//! both creating and editing a task.

use std::time::Instant;

use ratatui::layout::Rect;

use crate::body::BodyEditor;
use crate::due;
use crate::duepicker::DuePicker;
use crate::image::{GifLoad, GifPlayback};
use crate::model::{Block, Category, MAX_TITLE_LEN, Task};
use crate::text_input::TextInput;
use crate::undo::{EditKind, History};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Field {
    Title,
    Category,
    Due,
    Importance,
    Body,
}

impl Field {
    /// Tab order follows the layout: title, metadata, then body.
    pub fn next(self) -> Self {
        match self {
            Self::Title => Self::Category,
            Self::Category => Self::Due,
            Self::Due => Self::Importance,
            Self::Importance => Self::Body,
            Self::Body => Self::Title,
        }
    }

    pub fn prev(self) -> Self {
        match self {
            Self::Title => Self::Body,
            Self::Category => Self::Title,
            Self::Due => Self::Category,
            Self::Importance => Self::Due,
            Self::Body => Self::Importance,
        }
    }
}

/// Editable content of the category dialog (for undo).
#[derive(Debug, Clone, PartialEq, Eq)]
struct CategorySnap {
    name: TextInput,
    description: BodyEditor,
    on_description: bool,
}

/// The category dialog: a name and a note about what it is for, both
/// plain text.
pub struct CategoryForm {
    pub name: TextInput,
    pub description: BodyEditor,
    pub on_description: bool,
    pub error: Option<String>,
    /// The category being edited; `None` when creating one.
    pub editing: Option<String>,
    pub name_area: Rect,
    pub description_area: Rect,
    history: History<CategorySnap>,
    initial_name: String,
    initial_description: String,
}

impl CategoryForm {
    pub fn new() -> Self {
        Self {
            name: TextInput::new("", crate::model::MAX_CATEGORY_NAME_LEN),
            description: BodyEditor::plain(""),
            on_description: false,
            error: None,
            editing: None,
            name_area: Rect::ZERO,
            description_area: Rect::ZERO,
            history: History::new(),
            initial_name: String::new(),
            initial_description: String::new(),
        }
    }

    pub fn edit(category: &crate::model::Category) -> Self {
        Self {
            name: TextInput::new(&category.name, crate::model::MAX_CATEGORY_NAME_LEN),
            description: BodyEditor::plain(&category.description),
            editing: Some(category.id.clone()),
            initial_name: category.name.clone(),
            initial_description: category.description.clone(),
            ..Self::new()
        }
    }

    pub fn title_text(&self) -> &'static str {
        if self.editing.is_some() {
            "Edit category"
        } else {
            "New category"
        }
    }

    pub fn toggle_field(&mut self) {
        self.set_description_focus(!self.on_description);
    }

    /// Focus a particular field while preserving undo coalescing boundaries.
    pub fn set_description_focus(&mut self, description: bool) {
        if self.on_description != description {
            self.history.break_coalesce();
            self.on_description = description;
        }
    }

    pub fn is_dirty(&self) -> bool {
        self.name.value() != self.initial_name
            || self.description.plain_value() != self.initial_description
    }

    fn snap(&self) -> CategorySnap {
        CategorySnap {
            name: self.name.clone(),
            description: self.description.clone(),
            on_description: self.on_description,
        }
    }

    fn restore(&mut self, s: CategorySnap) {
        self.name = s.name;
        self.description = s.description;
        self.on_description = s.on_description;
        self.error = None;
    }

    /// Snapshot before a content edit.
    pub fn before_edit(&mut self, kind: EditKind) {
        let Self {
            name,
            description,
            on_description,
            history,
            ..
        } = self;
        history.before_edit_with(kind, || CategorySnap {
            name: name.clone(),
            description: description.clone(),
            on_description: *on_description,
        });
    }

    pub fn break_coalesce(&mut self) {
        self.history.break_coalesce();
    }

    pub fn undo(&mut self) -> bool {
        let Some(prev) = self.history.undo(self.snap()) else {
            return false;
        };
        self.restore(prev);
        true
    }

    pub fn redo(&mut self) -> bool {
        let Some(next) = self.history.redo(self.snap()) else {
            return false;
        };
        self.restore(next);
        true
    }

    /// `(name, description)` once there is a name to save.
    pub fn submit(&mut self) -> Option<(String, String)> {
        self.submit_with(|_, _| Ok(()))
    }

    /// Validate and submit through a caller-provided uniqueness/policy hook.
    /// The hook receives the normalized name and the edited category id.
    pub fn submit_with<F>(&mut self, validate_name: F) -> Option<(String, String)>
    where
        F: FnOnce(&str, Option<&str>) -> Result<(), String>,
    {
        let name = self.name.value().trim().to_string();
        if name.is_empty() {
            self.error = Some("A name is required".to_string());
            self.on_description = false;
            return None;
        }
        if let Err(error) = validate_name(&name, self.editing.as_deref()) {
            self.error = Some(error);
            self.on_description = false;
            return None;
        }
        self.error = None;
        Some((name, self.description.plain_value()))
    }
}

impl Default for CategoryForm {
    fn default() -> Self {
        Self::new()
    }
}

/// Where each field's box landed in the last frame, so a click can find
/// the field under the pointer.
#[derive(Debug, Default, Clone, Copy)]
pub struct FieldAreas {
    pub title: Rect,
    pub category: Rect,
    pub due: Rect,
    pub importance: Rect,
    pub body: Rect,
}

impl FieldAreas {
    pub fn field_at(&self, x: u16, y: u16) -> Option<Field> {
        let pos = ratatui::layout::Position { x, y };
        if self.title.contains(pos) {
            Some(Field::Title)
        } else if self.category.contains(pos) {
            Some(Field::Category)
        } else if self.due.contains(pos) {
            Some(Field::Due)
        } else if self.importance.contains(pos) {
            Some(Field::Importance)
        } else if self.body.contains(pos) {
            Some(Field::Body)
        } else {
            None
        }
    }

    pub fn rect(&self, field: Field) -> Rect {
        match field {
            Field::Title => self.title,
            Field::Category => self.category,
            Field::Due => self.due,
            Field::Importance => self.importance,
            Field::Body => self.body,
        }
    }
}

/// The values a submitted form hands back to the app.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TaskDraft {
    pub title: String,
    /// Real category UUID, or `None` for Uncategorized.
    pub category_id: Option<String>,
    pub due: String,
    pub importance: u8,
    pub body: Vec<Block>,
}

impl TaskDraft {
    pub fn new(title: &str) -> Self {
        Self {
            title: title.to_string(),
            ..Self::default()
        }
    }
}

/// Editable content of the task dialog (for undo). UI chrome is excluded.
#[derive(Debug, Clone, PartialEq, Eq)]
struct TaskSnap {
    title: TextInput,
    category_id: Option<String>,
    due: TextInput,
    importance: u8,
    body: BodyEditor,
    field: Field,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct CategoryChoice {
    id: Option<String>,
    name: String,
}

impl CategoryChoice {
    fn uncategorized() -> Self {
        Self {
            id: None,
            name: "Uncategorized".to_string(),
        }
    }
}

pub struct TaskForm {
    pub title: TextInput,
    category_id: Option<String>,
    category_choices: Vec<CategoryChoice>,
    pub due: TextInput,
    pub importance: u8,
    pub body: BodyEditor,
    pub field: Field,
    pub error: Option<String>,
    /// The task being edited; `None` when creating a new one.
    pub editing: Option<String>,
    /// Filled in while drawing; used to hit-test clicks.
    pub areas: FieldAreas,
    /// Whether the description's image is shown full size.
    pub preview: bool,
    /// Decoded GIF for preview, keyed by path (kept after close for fast reopen).
    pub gif: Option<(std::path::PathBuf, GifPlayback)>,
    /// GIF decode in progress; polled by the normal animation tick.
    pub gif_pending: Option<GifLoad>,
    /// The calendar, while a due date is being picked.
    pub picker: Option<DuePicker>,
    /// Last body click (line index), for double-click to open a picture.
    pub last_body_click: Option<(Instant, usize)>,
    /// Whether the `/` menu was open last frame — used to re-emit images
    /// after the menu is dismissed (see `ui::draw_body`).
    pub menu_was_open: bool,
    /// Body scroll after last paint — scroll changes need the same protocol
    /// drop as menu close so pictures that shrink/move do not ghost.
    pub body_scroll: usize,
    /// Screen rect of the open body `/` dropdown (for mouse hit-testing).
    pub body_menu_area: Option<Rect>,
    /// Where each body picture was drawn last frame: `(line index, screen rect)`.
    /// Clicks only select a picture when they land inside this rect — not the
    /// full-width letterbox gutter.
    pub image_hits: Vec<(usize, Rect)>,
    history: History<TaskSnap>,
    initial: TaskDraft,
}

impl TaskForm {
    pub fn new() -> Self {
        Self {
            title: TextInput::new("", MAX_TITLE_LEN),
            category_id: None,
            category_choices: vec![CategoryChoice::uncategorized()],
            due: TextInput::new("", 32),
            importance: 0,
            body: BodyEditor::new(&[]),
            field: Field::Title,
            error: None,
            editing: None,
            areas: FieldAreas::default(),
            preview: false,
            gif: None,
            gif_pending: None,
            picker: None,
            last_body_click: None,
            menu_was_open: false,
            body_scroll: 0,
            body_menu_area: None,
            image_hits: Vec::new(),
            history: History::new(),
            initial: TaskDraft::default(),
        }
    }

    pub fn edit(task: &Task) -> Self {
        Self {
            title: TextInput::new(&task.title, MAX_TITLE_LEN),
            category_id: task.category_id.clone(),
            due: TextInput::new(&task.due, 32),
            importance: task.importance,
            body: BodyEditor::new(&task.body),
            editing: Some(task.id.clone()),
            initial: TaskDraft {
                title: task.title.clone(),
                category_id: task.category_id.clone(),
                due: task.due.clone(),
                importance: task.importance,
                body: task.body.clone(),
            },
            ..Self::new()
        }
    }

    /// Whether `(x, y)` sits on the drawn picture for body line `line`.
    pub fn image_hit_at(&self, line: usize, x: u16, y: u16) -> bool {
        use ratatui::layout::Position;
        self.image_hits
            .iter()
            .any(|(i, r)| *i == line && r.contains(Position { x, y }))
    }

    pub fn set_image_root(&mut self, image_root: std::path::PathBuf) {
        self.body.set_image_root(image_root);
    }

    pub fn set_attachments(&mut self, attachments: &[crate::store::Attachment]) {
        self.body.set_attachments(attachments);
    }

    /// Install the real categories available to this form and select the
    /// task's starting category. `All tasks` is a view, not a destination;
    /// Uncategorized is always the first choice.
    ///
    /// Call this once when opening the form, before the user edits it. The
    /// selected value becomes part of the dirty-state baseline.
    pub fn set_categories(&mut self, categories: &[Category], selected_id: Option<&str>) {
        self.category_choices.clear();
        self.category_choices.push(CategoryChoice::uncategorized());
        self.category_choices
            .extend(
                categories
                    .iter()
                    .filter(|category| !category.is_all())
                    .map(|category| CategoryChoice {
                        id: Some(category.id.clone()),
                        name: category.name.clone(),
                    }),
            );
        self.category_id = selected_id
            .filter(|id| {
                self.category_choices
                    .iter()
                    .any(|choice| choice.id.as_deref() == Some(*id))
            })
            .map(str::to_string);
        self.initial.category_id = self.category_id.clone();
    }

    pub fn category_id(&self) -> Option<&str> {
        self.category_id.as_deref()
    }

    pub fn category_label(&self) -> &str {
        self.category_choices
            .iter()
            .find(|choice| choice.id.as_deref() == self.category_id.as_deref())
            .map(|choice| choice.name.as_str())
            .unwrap_or("Uncategorized")
    }

    /// Select the previous/next category, wrapping at either end.
    pub fn cycle_category(&mut self, delta: i32) {
        let len = self.category_choices.len();
        if len <= 1 {
            return;
        }
        let current = self
            .category_choices
            .iter()
            .position(|choice| choice.id.as_deref() == self.category_id.as_deref())
            .unwrap_or_default();
        let next = (current as i32 + delta).rem_euclid(len as i32) as usize;
        if next == current {
            return;
        }
        self.before_edit(EditKind::Atomic);
        self.category_id = self.category_choices[next].id.clone();
    }

    pub fn clear_category(&mut self) {
        if self.category_id.is_none() {
            return;
        }
        self.before_edit(EditKind::Atomic);
        self.category_id = None;
    }

    /// Open the full-size image viewer. GIF frames decode asynchronously;
    /// the still-image cache can paint a placeholder in the meantime.
    pub fn open_image_preview(&mut self) -> Option<String> {
        // Only the image under the cursor — never fall back to "first in body".
        let Some(path) = self.body.selected_image() else {
            self.preview = false;
            return Some("No image to preview".into());
        };
        self.preview = true;
        if crate::image::is_gif(&path) {
            // Keep a decoded GIF across close/reopen while this form is open.
            if matches!(&self.gif, Some((p, _)) if p == &path) {
                return None;
            }
            if self
                .gif_pending
                .as_ref()
                .is_none_or(|pending| pending.path() != path)
            {
                self.gif = None;
                self.gif_pending = Some(GifLoad::start(path));
            }
            None
        } else {
            // Different still — drop any previous GIF cache.
            if !matches!(&self.gif, Some((p, _)) if p == &path) {
                self.gif = None;
            }
            self.gif_pending = None;
            None
        }
    }

    pub fn close_image_preview(&mut self) {
        self.preview = false;
        // Keep `gif` so reopening the same animation is instant.
    }

    pub fn gif_playing(&self) -> bool {
        self.preview
            && (self.gif_pending.is_some()
                || (!crate::theme::reduced_motion()
                    && self
                        .gif
                        .as_ref()
                        .is_some_and(|(_, g)| g.is_animated() && !g.is_paused())))
    }

    /// Advance GIF animation; returns true when the frame changed.
    pub fn tick_gif(&mut self) -> bool {
        if let Some(result) = self.gif_pending.as_ref().and_then(GifLoad::poll) {
            let path = self
                .gif_pending
                .take()
                .map(|pending| pending.path().to_path_buf());
            match (path, result) {
                (Some(path), Ok(gif)) => self.gif = Some((path, gif)),
                (_, Err(error)) => {
                    self.gif = None;
                    self.error = Some(error);
                }
                (None, Ok(_)) => {}
            }
            return true;
        }
        if crate::theme::reduced_motion() {
            return false;
        }
        if let Some((_, gif)) = &mut self.gif {
            return gif.tick();
        }
        false
    }

    /// Click while preview is open: pause/resume an animated GIF.
    /// Static images ignore the click (Esc still closes).
    pub fn preview_click(&mut self) {
        if crate::theme::reduced_motion() {
            return;
        }
        if let Some((_, gif)) = &mut self.gif {
            gif.toggle_pause();
        }
    }

    pub fn is_edit(&self) -> bool {
        self.editing.is_some()
    }

    pub fn is_dirty(&self) -> bool {
        self.content() != self.initial
    }

    fn content(&self) -> TaskDraft {
        TaskDraft {
            title: self.title.value(),
            category_id: self.category_id.clone(),
            due: self.due.value(),
            importance: self.importance,
            body: self.body.value(),
        }
    }

    pub fn title_text(&self) -> &'static str {
        if self.is_edit() {
            "Edit task"
        } else {
            "New task"
        }
    }

    fn snap(&self) -> TaskSnap {
        TaskSnap {
            title: self.title.clone(),
            category_id: self.category_id.clone(),
            due: self.due.clone(),
            importance: self.importance,
            body: self.body.clone(),
            field: self.field,
        }
    }

    fn restore(&mut self, s: TaskSnap) {
        self.title = s.title;
        self.category_id = s.category_id;
        self.due = s.due;
        self.importance = s.importance;
        self.body = s.body;
        self.field = s.field;
        self.error = None;
        // Overlays are not part of content history.
        self.picker = None;
        self.preview = false;
        self.gif_pending = None;
        self.body.close_menu();
    }

    /// Snapshot before a content edit (call before mutating).
    pub fn before_edit(&mut self, kind: EditKind) {
        let Self {
            title,
            category_id,
            due,
            importance,
            body,
            field,
            history,
            ..
        } = self;
        history.before_edit_with(kind, || TaskSnap {
            title: title.clone(),
            category_id: category_id.clone(),
            due: due.clone(),
            importance: *importance,
            body: body.clone(),
            field: *field,
        });
    }

    pub fn break_coalesce(&mut self) {
        self.history.break_coalesce();
    }

    pub fn undo(&mut self) -> bool {
        let Some(prev) = self.history.undo(self.snap()) else {
            return false;
        };
        self.restore(prev);
        true
    }

    pub fn redo(&mut self) -> bool {
        let Some(next) = self.history.redo(self.snap()) else {
            return false;
        };
        self.restore(next);
        true
    }

    /// Opens the calendar on whatever the field already reads.
    pub fn open_due_picker(&mut self) {
        self.history.break_coalesce();
        self.field = Field::Due;
        self.picker = Some(DuePicker::new(self.due.value().trim()));
    }

    /// Writes the picked day back into the field.
    pub fn take_due_picker(&mut self) {
        if self.picker.is_some() {
            self.before_edit(EditKind::Atomic);
            if let Some(picker) = self.picker.take() {
                self.due = TextInput::new(&picker.value(), 32);
            }
        }
    }

    /// Steps importance up, wrapping back to none after three.
    pub fn cycle_importance(&mut self) {
        let next = (self.importance + 1) % (crate::model::MAX_IMPORTANCE + 1);
        self.set_importance(next);
    }

    pub fn set_importance(&mut self, importance: u8) {
        let next = importance.min(crate::model::MAX_IMPORTANCE);
        if next == self.importance {
            return;
        }
        self.before_edit(EditKind::Atomic);
        self.importance = next;
    }

    pub fn clear_due(&mut self) {
        if self.due.is_empty() && self.picker.is_none() {
            return;
        }
        self.before_edit(EditKind::Atomic);
        self.picker = None;
        self.due = TextInput::new("", 32);
    }

    pub fn focus_next(&mut self) {
        self.history.break_coalesce();
        self.picker = None;
        self.body.close_menu();
        self.field = self.field.next();
    }

    pub fn focus_prev(&mut self) {
        self.history.break_coalesce();
        self.picker = None;
        self.body.close_menu();
        self.field = self.field.prev();
    }

    /// Move focus; dismiss the calendar and slash menu when leaving
    /// the fields that own them.
    pub fn set_field(&mut self, field: Field) {
        self.history.break_coalesce();
        if field != Field::Due {
            self.picker = None;
        }
        if field != Field::Body {
            self.body.close_menu();
        }
        self.field = field;
    }

    /// Validates the form. On success returns the draft; on failure sets
    /// `error` and focuses the offending field.
    pub fn submit(&mut self) -> Option<TaskDraft> {
        self.error = None;

        let title = self.title.value().trim().to_string();
        if title.is_empty() {
            self.error = Some("A title is required".to_string());
            self.field = Field::Title;
            return None;
        }

        let due_text = self.due.value().trim().to_string();
        if !due::is_valid(&due_text) {
            self.error = Some(format!("'{due_text}' is not a date mach understands"));
            self.field = Field::Due;
            return None;
        }

        // A `[date]` typed into the title still works, and fills the due
        // field when it was left empty. It gets the same check the Due
        // field does — otherwise it is a way around it.
        let (inline_due, title) = due::parse(&title);
        if title.is_empty() {
            self.error = Some("A title is required".to_string());
            self.field = Field::Title;
            return None;
        }
        if !due::is_valid(&inline_due) {
            self.error = Some(format!("'{inline_due}' is not a date mach understands"));
            self.field = Field::Title;
            return None;
        }

        Some(TaskDraft {
            title,
            category_id: self.category_id.clone(),
            due: if due_text.is_empty() {
                inline_due
            } else {
                due_text
            },
            importance: self.importance,
            body: self.body.value(),
        })
    }
}

impl Default for TaskForm {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn requires_a_title() {
        let mut form = TaskForm::new();
        assert!(form.submit().is_none());
        assert_eq!(form.field, Field::Title);
        assert!(form.error.is_some());
    }

    #[test]
    fn rejects_an_unparsable_date() {
        let mut form = TaskForm::new();
        form.title = TextInput::new("something", MAX_TITLE_LEN);
        form.due = TextInput::new("next tuesday", 32);
        assert!(form.submit().is_none());
        assert_eq!(form.field, Field::Due);
    }

    #[test]
    fn takes_a_date_typed_into_the_title() {
        let mut form = TaskForm::new();
        form.title = TextInput::new("pay rent [2030-01-02]", MAX_TITLE_LEN);
        let draft = form.submit().expect("valid");
        assert_eq!(draft.title, "pay rent");
        assert_eq!(draft.due, "2030-01-02");
    }

    #[test]
    fn an_explicit_date_wins_over_the_inline_one() {
        let mut form = TaskForm::new();
        form.title = TextInput::new("pay rent [2030-01-02]", MAX_TITLE_LEN);
        form.due = TextInput::new("09:00", 32);
        let draft = form.submit().expect("valid");
        assert_eq!(draft.due, "09:00");
    }

    #[test]
    fn undo_restores_title_and_body() {
        let mut form = TaskForm::new();
        form.before_edit(EditKind::Typing);
        form.title = TextInput::new("hello", MAX_TITLE_LEN);
        form.before_edit(EditKind::Atomic);
        form.body.insert_str("note");
        assert!(form.undo());
        assert!(form.body.is_empty() || form.body.plain_value().is_empty());
        assert_eq!(form.title.value(), "hello");
        assert!(form.undo());
        assert_eq!(form.title.value(), "");
        assert!(form.redo());
        assert_eq!(form.title.value(), "hello");
    }

    #[test]
    fn undo_restores_importance() {
        let mut form = TaskForm::new();
        form.set_importance(2);
        assert_eq!(form.importance, 2);
        assert!(form.undo());
        assert_eq!(form.importance, 0);
        assert!(form.redo());
        assert_eq!(form.importance, 2);
    }

    #[test]
    fn dirty_state_tracks_content_not_focus() {
        let mut task = TaskForm::new();
        task.focus_next();
        assert!(!task.is_dirty());
        task.title.insert('x');
        assert!(task.is_dirty());

        let mut category = CategoryForm::new();
        category.set_description_focus(true);
        assert!(!category.is_dirty());
        category.name.insert('x');
        assert!(category.is_dirty());
    }

    #[test]
    fn category_submit_exposes_a_shared_name_policy_hook() {
        let mut form = CategoryForm::new();
        form.name.insert_str("Work");
        assert!(
            form.submit_with(|name, _| {
                (name != "Work")
                    .then_some(())
                    .ok_or_else(|| "A category with that name already exists".to_string())
            })
            .is_none()
        );
        assert_eq!(
            form.error.as_deref(),
            Some("A category with that name already exists")
        );
    }

    #[test]
    fn opening_a_gif_never_decodes_on_the_input_path() {
        let path = std::env::temp_dir().join(format!("mach-async-{}.gif", std::process::id()));
        std::fs::write(&path, b"not a real gif").unwrap();
        let mut task = Task::new("gif", 0, None, "");
        task.body = vec![Block::Image {
            attachment_id: path.display().to_string(),
        }];
        let mut form = TaskForm::edit(&task);

        assert!(form.open_image_preview().is_none());
        assert!(form.gif.is_none());
        assert!(form.gif_pending.is_some());
    }

    #[test]
    fn category_selector_includes_uncategorized_and_is_part_of_the_draft() {
        let categories = [
            crate::model::Category::all_tasks(),
            crate::model::Category {
                id: "work".into(),
                name: "Work".into(),
                description: String::new(),
            },
        ];
        let mut form = TaskForm::new();
        form.set_categories(&categories, Some("work"));

        assert_eq!(form.category_id(), Some("work"));
        assert_eq!(form.category_label(), "Work");
        assert!(!form.is_dirty());

        form.cycle_category(1);
        assert_eq!(form.category_id(), None);
        assert_eq!(form.category_label(), "Uncategorized");
        assert!(form.is_dirty());

        form.title.insert_str("portable task");
        assert_eq!(form.submit().expect("valid draft").category_id, None);
        assert!(form.undo());
        assert_eq!(form.category_id(), Some("work"));
    }

    #[test]
    fn editing_a_task_keeps_its_category_in_the_dirty_baseline() {
        let mut task = Task::new("move me", 0, Some("work".into()), "");
        task.id = "task".into();
        let categories = [crate::model::Category {
            id: "work".into(),
            name: "Work".into(),
            description: String::new(),
        }];
        let mut form = TaskForm::edit(&task);
        form.set_categories(&categories, task.category_id.as_deref());

        assert_eq!(form.category_id(), Some("work"));
        assert!(!form.is_dirty());
        assert_eq!(
            form.submit().expect("valid draft").category_id.as_deref(),
            Some("work")
        );
    }
}