tod 0.11.2

An unofficial Todoist command-line client
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
use futures::future;
use pad::PadStr;
use std::fmt::Display;
use tokio::task::JoinHandle;

use crate::config::Config;
use crate::errors::Error;
use crate::sections::Section;
use crate::tasks::{FormatType, Task};
use crate::{SortOrder, color, input, sections, tasks, todoist};
use serde::{Deserialize, Serialize};

const PAD_WIDTH: usize = 30;
const PROJECT_URL: &str = "https://app.todoist.com/app/project";

/// Projects are split into sections
// This struct is from the v2 REST API and is deprecated
#[derive(PartialEq, Eq, Serialize, Deserialize, Clone, Debug)]
pub struct LegacyProject {
    pub id: String,
    pub name: String,
    pub color: String,
    pub comment_count: u8,
    pub order: u8,
    pub is_shared: bool,
    pub is_favorite: bool,
    pub is_inbox_project: bool,
    pub is_team_inbox: bool,
    pub view_style: String,
    pub url: String,
    pub parent_id: Option<String>,
}
// Projects are split into sections
#[derive(PartialEq, Eq, Serialize, Deserialize, Clone, Debug)]
pub struct Project {
    pub id: String,
    pub can_assign_tasks: bool,
    pub child_order: u32,
    pub color: String,
    pub created_at: Option<String>,
    pub is_archived: bool,
    pub is_deleted: bool,
    pub is_favorite: bool,
    pub is_frozen: bool,
    pub name: String,
    pub updated_at: Option<String>,
    pub view_style: String,
    pub default_order: u32,
    pub description: String,
    pub parent_id: Option<String>,
    pub inbox_project: Option<bool>,
    pub is_collapsed: bool,
    pub is_shared: bool,
}

#[derive(PartialEq, Eq, Serialize, Deserialize, Clone, Debug)]
pub struct ProjectResponse {
    pub results: Vec<Project>,
    pub next_cursor: Option<String>,
}

pub enum TaskFilter {
    /// Does not have a date or datetime on it
    Unscheduled,
    /// Date or datetime is before today
    Overdue,
    /// Is a repeating task
    Recurring,
}

impl Display for LegacyProject {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}\n{}", self.name, self.url)
    }
}
impl Display for Project {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}\n{}/{}", self.name, PROJECT_URL, self.id)
    }
}
pub fn json_to_project(json: String) -> Result<Project, Error> {
    let project: Project = serde_json::from_str(&json)?;
    Ok(project)
}
pub fn json_to_projects_response(json: String) -> Result<ProjectResponse, Error> {
    let response: ProjectResponse = serde_json::from_str(&json)?;
    Ok(response)
}

pub async fn create(
    config: &mut Config,
    name: String,
    description: &str,
    is_favorite: bool,
) -> Result<String, Error> {
    let project = todoist::create_project(config, &name, description, is_favorite, true).await?;
    add(config, &project).await?;
    Ok(format!("Created project {name} and added to config"))
}
/// List the projects in config with task counts
pub async fn list(config: &mut Config) -> Result<String, Error> {
    config.reload_projects().await?;

    let mut project_handles = Vec::new();

    for project in config.projects().await? {
        let config = config.clone();
        let handle = tokio::spawn(async move { project_name_with_count(&config, &project).await });

        project_handles.push(handle);
    }

    let mut projects: Vec<String> = future::join_all(project_handles)
        .await
        .into_iter()
        .map(|p| p.unwrap_or_default())
        .collect();
    if projects.is_empty() {
        return Ok("No projects found".into());
    }
    projects.sort();
    let mut buffer = String::new();
    buffer.push_str(&color::green_string("Projects").pad_to_width(PAD_WIDTH + 5));
    buffer.push_str(&color::green_string("# Tasks"));

    for key in projects {
        buffer.push_str("\n - ");
        buffer.push_str(&key);
    }
    Ok(buffer)
}

/// Formats a string with project name and the count that is a standard length
async fn project_name_with_count(config: &Config, project: &Project) -> String {
    let count = match count_processable_tasks(config, project).await {
        Ok(num) => format!("{num}"),
        Err(_) => String::new(),
    };

    format!("{}{}", project.name.pad_to_width(PAD_WIDTH), count)
}

/// Gets the number of tasks for a project that are not in the future
async fn count_processable_tasks(config: &Config, project: &Project) -> Result<u8, Error> {
    let all_tasks = todoist::all_tasks_by_project(config, project, None).await?;
    let count = tasks::filter_not_in_future(all_tasks, config)?.len();

    Ok(count as u8)
}

/// Add a project to the projects HashMap in Config
pub async fn add(config: &mut Config, project: &Project) -> Result<String, Error> {
    config.add_project(project.clone());
    config.save().await
}

/// Remove a project from the projects HashMap in Config
pub async fn remove(config: &mut Config, project: &Project) -> Result<String, Error> {
    config.remove_project(project);
    config.save().await
}

/// Remove a project from the projects HashMap in Config
pub async fn delete(config: &mut Config, project: &Project) -> Result<String, Error> {
    todoist::delete_project(config, project, true).await?;
    config.remove_project(project);
    config.save().await
}

/// Rename a project in config
pub async fn rename(config: &mut Config, project: &Project) -> Result<String, Error> {
    let new_name = input::string_with_default(input::NAME, &project.name)?;

    let new_project = Project {
        name: new_name,
        ..project.clone()
    };
    remove(config, project).await?;
    add(config, &new_project).await
}

/// Get the next task by priority and save its id to config
pub async fn next_task(config: Config, project: &Project) -> Result<String, Error> {
    match fetch_next_task(&config, project).await {
        Ok(Some((task, remaining))) => {
            let comments = todoist::all_comments(&config, &task, None).await?;
            config.set_next_task(task.clone()).save().await?;
            let task_string = task
                .fmt(comments, &config, FormatType::Single, false)
                .await?;
            Ok(format!("{task_string}\n{remaining} task(s) remaining"))
        }
        Ok(None) => Ok(color::green_string("No tasks on list")),
        Err(e) => Err(e),
    }
}

async fn fetch_next_task(
    config: &Config,
    project: &Project,
) -> Result<Option<(Task, usize)>, Error> {
    let tasks = todoist::all_tasks_by_project(config, project, None).await?;
    let filtered_tasks = tasks::filter_not_in_future(tasks, config)?;
    let tasks = tasks::sort_by_value(filtered_tasks, config);

    Ok(tasks.first().map(|task| (task.to_owned(), tasks.len())))
}

/// Removes all projects from config that don't exist in Todoist
pub async fn remove_auto(config: &mut Config) -> Result<String, Error> {
    let projects = todoist::all_projects(config, None).await?;
    let missing_projects = filter_missing_projects(config, projects).await?;

    if missing_projects.is_empty() {
        return Ok(color::green_string("No projects to auto remove"));
    }

    for project in &missing_projects {
        config.remove_project(project);
    }
    config.save().await?;
    let project_names = missing_projects
        .iter()
        .map(|p| p.name.clone())
        .collect::<Vec<String>>()
        .join(", ");
    let message = format!("Auto removed: '{project_names}'");
    Ok(color::green_string(&message))
}

/// Removes all projects from config
pub async fn remove_all(config: &mut Config) -> Result<String, Error> {
    let options = vec!["Cancel", "Confirm"];
    let selection = input::select(
        "Confirm removing all projects from config",
        options,
        config.mock_select,
    )?;

    if selection == "Cancel" {
        return Ok("Cancelled".into());
    }

    let projects = config.projects().await?;
    if projects.is_empty() {
        return Ok(color::green_string("No projects to remove"));
    }

    for project in &projects {
        config.remove_project(project);
    }
    config.save().await?;
    let message = "Removed all projects from config";
    Ok(color::green_string(message))
}

/// Returns the projects that are not already in config
async fn filter_missing_projects(
    config: &mut Config,
    projects: Vec<Project>,
) -> Result<Vec<Project>, Error> {
    let project_ids: Vec<String> = projects.into_iter().map(|v| v.id).collect();
    let config = config
        .projects()
        .await?
        .into_iter()
        .filter(|p| !project_ids.contains(&p.id))
        .collect();

    Ok(config)
}

/// Fetch projects and prompt to add them to config one by one
pub async fn import(config: &mut Config, auto: &bool) -> Result<String, Error> {
    let projects = todoist::all_projects(config, None).await?;
    let new_projects = filter_new_projects(config, projects).await?;
    for project in new_projects {
        maybe_add_project(config, project, auto).await?;
    }
    Ok(color::green_string("No more projects"))
}

/// Returns the projects that are not already in config
async fn filter_new_projects(
    config: &mut Config,
    projects: Vec<Project>,
) -> Result<Vec<Project>, Error> {
    let project_ids: Vec<String> = config
        .projects()
        .await?
        .iter()
        .map(|v| v.id.clone())
        .collect();
    let new_projects: Vec<Project> = projects
        .into_iter()
        .filter(|p| !project_ids.contains(&p.id))
        .collect();

    Ok(new_projects)
}

/// Prompt the user if they want to add project to config and maybe add
async fn maybe_add_project(
    config: &mut Config,
    project: Project,
    auto: &bool,
) -> Result<String, Error> {
    if *auto {
        println!("Adding {project}");
        return add(config, &project).await;
    }

    let options = vec!["add", "skip"];
    println!("{project}");
    match input::select("Select an option", options.clone(), config.mock_select) {
        Ok(string) => {
            if string == "add" {
                add(config, &project).await
            } else if string == "skip" {
                Ok("Skipped".into())
            } else {
                Err(Error::new("add_project", "Invalid option"))
            }
        }
        Err(e) => Err(e)?,
    }
}

pub async fn edit_task(config: &Config, project: &Project) -> Result<String, Error> {
    let project_tasks = todoist::all_tasks_by_project(config, project, None).await?;

    let task = input::select(
        "Choose a task of the project:",
        project_tasks,
        config.mock_select,
    )?;

    let options = tasks::edit_task_attributes();

    let selections = input::multi_select("Choose attributes to edit", options, config.mock_select)?;

    if selections.is_empty() {
        return Err(Error {
            message: "Nothing selected".to_string(),
            source: "edit_task".to_string(),
        });
    }

    let mut handles = Vec::new();
    for attribute in selections {
        // Stops the inputs from rolling over each other in terminal
        println!();
        if let Some(handle) = tasks::update_task(config, &task, &attribute).await? {
            handles.push(handle);
        }
    }

    future::join_all(handles).await;
    Ok("Finished editing task".into())
}

/// Empty a project by sending tasks to other projects one at a time
pub async fn empty(config: &mut Config, project: &Project) -> Result<String, Error> {
    let tasks = todoist::all_tasks_by_project(config, project, None).await?;

    if tasks.is_empty() {
        Ok(color::green_string(&format!(
            "No tasks to empty from '{}'",
            project.name
        )))
    } else {
        let sections = sections::all_sections(config).await?;

        let tasks = tasks
            .into_iter()
            .filter(|task| task.parent_id.is_none())
            .collect::<Vec<Task>>();

        let mut handles = Vec::new();
        for task in tasks.iter() {
            match move_task_to_project(config, task.to_owned(), &sections).await {
                Ok(handle) => handles.push(handle),
                Err(e) => return Err(e),
            };
        }
        future::join_all(handles).await;
        Ok(color::green_string(&format!(
            "Successfully emptied '{}'",
            project.name
        )))
    }
}

/// Put dates on all tasks without dates
pub async fn schedule(
    config: &Config,
    project: &Project,
    filter: TaskFilter,
    skip_recurring: bool,
    sort: &SortOrder,
) -> Result<String, Error> {
    let tasks = todoist::all_tasks_by_project(config, project, None).await?;
    let tasks = tasks::sort(tasks, config, sort);

    let filtered_tasks: Vec<Task> = if skip_recurring {
        tasks
            .into_iter()
            .filter(|task| {
                task.filter(config, &filter) && !task.filter(config, &TaskFilter::Recurring)
            })
            .collect::<Vec<Task>>()
    } else {
        tasks
            .into_iter()
            .filter(|task| task.filter(config, &filter))
            .collect::<Vec<Task>>()
    };

    if filtered_tasks.is_empty() {
        Ok(color::green_string(&format!(
            "No tasks to schedule in '{}'",
            project.name
        )))
    } else {
        let mut handles = Vec::new();
        for task in filtered_tasks.iter() {
            if let Some(handle) = tasks::spawn_schedule_task(config.clone(), task.clone()).await? {
                handles.push(handle);
            }
        }

        future::join_all(handles).await;
        Ok(color::green_string(&format!(
            "Successfully scheduled tasks in '{}'",
            project.name
        )))
    }
}
pub async fn deadline(
    config: &Config,
    project: &Project,
    sort: &SortOrder,
) -> Result<String, Error> {
    let tasks = todoist::all_tasks_by_project(config, project, None).await?;
    let tasks = tasks::sort(tasks, config, sort);

    let filtered_tasks: Vec<Task> = tasks
        .into_iter()
        .filter(|task| !task.filter(config, &TaskFilter::Recurring) && task.deadline.is_none())
        .collect::<Vec<Task>>();

    if filtered_tasks.is_empty() {
        Ok(color::green_string(&format!(
            "No tasks to deadline in '{}'",
            project.name
        )))
    } else {
        let mut handles = Vec::new();
        for task in filtered_tasks.iter() {
            if let Some(handle) = tasks::spawn_deadline_task(config.clone(), task.clone()).await? {
                handles.push(handle);
            }
        }

        future::join_all(handles).await;
        Ok(color::green_string(&format!(
            "Successfully deadlined tasks in '{}'",
            project.name
        )))
    }
}

pub async fn move_task_to_project(
    config: &mut Config,
    task: Task,
    sections: &[Section],
) -> Result<JoinHandle<()>, Error> {
    let comments = Vec::new();
    let text = task
        .fmt(comments, config, FormatType::Single, false)
        .await?;
    println!("{text}");

    let options = ["Pick project", "Complete", "Skip", "Delete"]
        .iter()
        .map(|o| o.to_string())
        .collect::<Vec<String>>();
    let selection = input::select("Choose", options, config.mock_select)?;

    match selection.as_str() {
        "Complete" => Ok(tasks::spawn_complete_task(config.clone(), task)),

        "Delete" => Ok(tasks::spawn_delete_task(config.clone(), task)),
        "Skip" => Ok(tokio::spawn(async move {})),
        _ => {
            let projects = config.projects().await?;
            let project = input::select("Select project", projects, config.mock_select)?;

            let sections: Vec<Section> = sections
                .iter()
                .filter(|s| s.project_id == project.id)
                .cloned()
                .collect();

            let section_names: Vec<String> = sections.clone().into_iter().map(|x| x.name).collect();
            if section_names.is_empty() || config.no_sections.unwrap_or_default() {
                let config = config.clone();
                Ok(tokio::spawn(async move {
                    if let Err(e) =
                        todoist::move_task_to_project(&config, &task, &project, false).await
                    {
                        config
                            .tx()
                            .send(e)
                            .expect("expected value or result, got None or Err");
                    }
                }))
            } else {
                let section_name =
                    input::select("Select section", section_names, config.mock_select)?;
                let section = sections
                    .iter()
                    .find(|x| x.name == section_name.as_str())
                    .expect("Section does not exist")
                    .clone();
                let config = config.clone();
                Ok(tokio::spawn(async move {
                    if let Err(e) =
                        todoist::move_task_to_section(&config, &task, &section, false).await
                    {
                        config
                            .tx()
                            .send(e)
                            .expect("expected value or result, got None or Err");
                    }
                }))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test;
    use crate::test::responses::ResponseFromFile;
    use pretty_assertions::assert_eq;

    #[tokio::test]
    async fn should_add_and_remove_projects() {
        let mut config = test::fixtures::config()
            .await
            .create()
            .await
            .expect("expected value or result, got None or Err");

        let binding = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");

        let result = remove(&mut config, project).await;
        assert_eq!(Ok("✓".to_string()), result);
        let result = add(&mut config, project).await;
        assert_eq!(Ok("✓".to_string()), result);
    }
    #[tokio::test]
    async fn test_list() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/projects?limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Projects.read().await)
            .create_async()
            .await;

        let mut config = test::fixtures::config()
            .await
            .create()
            .await
            .expect("expected value or result, got None or Err")
            .with_mock_url(server.url())
            .with_projects(vec![test::fixtures::project()]);

        config
            .save()
            .await
            .expect("expected value or result, got None or Err");

        let str = "Projects                           # Tasks\n - Doomsday                      ";

        assert_eq!(list(&mut config).await, Ok(String::from(str)));
        mock.expect(3);
    }

    #[tokio::test]
    async fn test_get_next_task() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/tasks/?project_id=123&limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTasks.read().await)
            .create_async()
            .await;

        let mock2 = server
            .mock(
                "GET",
                "/api/v1/comments/?task_id=6Xqhv4cwxgjwG9w8&limit=200",
            )
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::CommentsAllTypes.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config().await.with_mock_url(server.url());

        let config_dir = dirs::config_dir().expect("Could not find config directory");

        let config_with_timezone = config
            .with_timezone("America/Vancouver")
            .with_path(config_dir.join("test3"))
            .with_mock_url(server.url());
        let binding = config_with_timezone
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");

        config_with_timezone
            .clone()
            .create()
            .await
            .expect("expected value or result, got None or Err");

        let response = next_task(config_with_timezone, project)
            .await
            .expect("expected value or result, got None or Err");

        assert!(response.contains("TEST"));
        assert!(response.contains("1 task(s) remaining"));
        mock.assert();
        mock2.assert();
    }

    #[tokio::test]
    async fn test_import() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/projects?limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::NewProjects.read().await)
            .create_async()
            .await;

        let mut config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .mock_select(0)
            .create()
            .await
            .expect("expected value or result, got None or Err");

        assert_eq!(
            import(&mut config, &false).await,
            Ok("No more projects".to_string())
        );
        mock.assert_async().await;

        let config = config
            .reload()
            .await
            .expect("expected value or result, got None or Err");
        let config_keys: Vec<String> = config
            .projects()
            .await
            .expect("expected value or result, got None or Err")
            .iter()
            .map(|p| p.name.to_owned())
            .collect();
        assert!(config_keys.contains(&"Doomsday".to_string()))
    }

    #[tokio::test]
    async fn test_remove_auto() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/projects?limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::NewProjects.read().await)
            .create_async()
            .await;

        let mut config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .create()
            .await
            .expect("expected value or result, got None or Err");

        let result = remove_auto(&mut config);
        let expected: Result<String, Error> = Ok(String::from("Auto removed: 'myproject'"));
        assert_eq!(result.await, expected);
        mock.assert_async().await;
        let projects = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        assert_eq!(projects.is_empty(), true);
    }

    #[tokio::test]
    async fn test_remove_all() {
        let mut config = test::fixtures::config()
            .await
            .mock_select(1)
            .create()
            .await
            .expect("expected value or result, got None or Err");

        let result = remove_all(&mut config).await;
        let expected: Result<String, Error> = Ok(String::from("Removed all projects from config"));
        assert_eq!(result, expected);

        let projects = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        assert_eq!(projects.is_empty(), true);
    }

    #[tokio::test]
    async fn test_empty() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/tasks/?project_id=123&limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTasks.read().await)
            .create_async()
            .await;

        let mock2 = server
            .mock("POST", "/api/v1/tasks/6Xqhv4cwxgjwG9w8/move")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Task.read().await)
            .create_async()
            .await;

        let mock3 = server
            .mock("GET", "/api/v1/sections?project_id=123&limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Sections.read().await)
            .create_async()
            .await;

        let mock4 = server
            .mock("GET", "/api/v1/id_mappings/projects/123")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Ids.read().await)
            .create_async()
            .await;
        let mock5 = server
            .mock(
                "GET",
                "/api/v1/comments/?task_id=6Xqhv4cwxgjwG9w8&limit=200",
            )
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::CommentsAllTypes.read().await)
            .create_async()
            .await;

        let mut config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .with_mock_string("newtext")
            .mock_select(0);

        let binding = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");
        let result = empty(&mut config, project).await;
        assert_eq!(result, Ok(String::from("Successfully emptied 'myproject'")));
        mock.expect(2);
        mock2.assert();
        mock3.assert();
        mock4.expect(2);
        mock5.expect(2);
    }

    #[tokio::test]
    async fn test_move_task_to_project() {
        let mut config = test::fixtures::config().await.mock_select(2);
        let task = test::fixtures::today_task().await;
        let sections: Vec<Section> = Vec::new();

        move_task_to_project(&mut config, task, &sections)
            .await
            .expect("expected value or result, got None or Err")
            .await
            .expect("expected value or result, got None or Err");
    }

    #[tokio::test]
    async fn test_rename_task() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/tasks/?project_id=123&limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTasks.read().await)
            .create_async()
            .await;

        let config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .mock_select(0);
        let binding = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");

        let result = edit_task(&config, project);
        assert_eq!(result.await, Ok("Finished editing task".to_string()));
        mock.assert();
    }
    #[tokio::test]
    async fn test_project_delete() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("DELETE", "/api/v1/projects/123")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Project.read().await)
            .create_async()
            .await;

        let mut config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .mock_select(0)
            .create()
            .await
            .expect("expected value or result, got None or Err");
        let binding = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");

        let result = delete(&mut config, project).await;
        assert_eq!(result, Ok("✓".to_string()));
        mock.assert_async().await;
    }
    #[tokio::test]
    async fn test_schedule() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/tasks/?project_id=123&limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::UnscheduledTasks.read().await)
            .create_async()
            .await;

        let mock2 = server
            .mock("POST", "/rest/v2/tasks/999999")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let mock3 = server
            .mock("GET", "/api/v1/id_mappings/projects/123")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Ids.read().await)
            .create_async()
            .await;
        let mock4 = server
            .mock("GET", "/api/v1/comments/?task_id=999999&limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::CommentsAllTypes.read().await)
            .create_async()
            .await;
        let config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .mock_select(1)
            .with_mock_string("tod");

        let binding = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");
        let sort = &SortOrder::Value;
        let result = schedule(&config, project, TaskFilter::Unscheduled, false, sort);
        assert_eq!(
            result.await,
            Ok("Successfully scheduled tasks in 'myproject'".to_string())
        );

        let config = config.mock_select(2);

        let binding = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");
        let result = schedule(&config, project, TaskFilter::Overdue, false, sort);
        assert_eq!(
            result.await,
            Ok("No tasks to schedule in 'myproject'".to_string())
        );

        let config = config.mock_select(3);

        let binding = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");
        let result = schedule(&config, project, TaskFilter::Unscheduled, false, sort);
        assert_eq!(
            result.await,
            Ok("Successfully scheduled tasks in 'myproject'".to_string())
        );

        let result = schedule(&config, project, TaskFilter::Unscheduled, true, sort);
        assert_eq!(
            result.await,
            Ok("Successfully scheduled tasks in 'myproject'".to_string())
        );
        mock.expect(2);
        mock2.expect(2);
        mock3.expect(4);
        mock4.expect(4);
    }

    #[tokio::test]
    async fn test_deadline() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/api/v1/tasks/?project_id=123&limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::UnscheduledTasks.read().await)
            .create_async()
            .await;

        let mock2 = server
            .mock("POST", "/rest/v2/tasks/999999")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::TodayTask.read().await)
            .create_async()
            .await;

        let mock3 = server
            .mock("GET", "/api/v1/id_mappings/projects/123")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::Ids.read().await)
            .create_async()
            .await;
        let config = test::fixtures::config()
            .await
            .with_mock_url(server.url())
            .mock_select(1)
            .with_mock_string("tod");

        let mock4 = server
            .mock("GET", "/api/v1/comments/?task_id=999999&limit=200")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(ResponseFromFile::CommentsAllTypes.read().await)
            .create_async()
            .await;
        let binding = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");
        let sort = &SortOrder::Value;
        let result = deadline(&config, project, sort);
        assert_eq!(
            result.await,
            Ok("Successfully deadlined tasks in 'myproject'".to_string())
        );

        let config = config.mock_select(3);

        let binding = config
            .projects()
            .await
            .expect("expected value or result, got None or Err");
        let project = binding
            .first()
            .expect("expected value or result, got None or Err");
        let result = deadline(&config, project, sort);
        assert_eq!(
            result.await,
            Ok("Successfully deadlined tasks in 'myproject'".to_string())
        );

        let result = deadline(&config, project, sort);
        assert_eq!(
            result.await,
            Ok("Successfully deadlined tasks in 'myproject'".to_string())
        );
        mock.expect(2);
        mock2.expect(2);
        mock3.expect(4);
        mock4.expect(4);
    }
}