/builds/framezero/productivity/src/core/task.rs
Line | Count | Source |
1 | | //! Task rules and types, independent of UI and storage. |
2 | | use std::{cmp::Ordering, fmt, str::FromStr}; |
3 | | |
4 | | use anyhow::{Result, bail}; |
5 | | use chrono::{DateTime, NaiveDate, Utc}; |
6 | | use uuid::Uuid; |
7 | | |
8 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
9 | | pub struct TaskDraft { |
10 | | pub title: String, |
11 | | pub notes: String, |
12 | | pub start_date: Option<NaiveDate>, |
13 | | pub due_date: Option<NaiveDate>, |
14 | | pub important: bool, |
15 | | pub urgent: bool, |
16 | | pub status: TaskStatus, |
17 | | pub kind: TaskKind, |
18 | | pub project_id: Option<Uuid>, |
19 | | pub prerequisite_ids: Vec<Uuid>, |
20 | | /// A web link such as a Jira issue or GitLab merge request; see `core::link`. |
21 | | pub link: Option<String>, |
22 | | } |
23 | | |
24 | | impl TaskDraft { |
25 | 1.38k | pub fn validate(&self) -> Result<()> { |
26 | 1.38k | if self.title.trim().is_empty() { |
27 | 5 | bail!("Give the task a title."); |
28 | 1.38k | } |
29 | 1.38k | if let Some(link) = &self.link { |
30 | 14 | crate::core::link::normalize_link(link)?2 ; |
31 | 1.36k | } |
32 | 1.38k | if let (Some(start), Some(due)) = (self.start_date, self.due_date) |
33 | 239 | && start > due |
34 | | { |
35 | 5 | bail!("Start date must be on or before the due date."); |
36 | 1.37k | } |
37 | 1.37k | Ok(()) |
38 | 1.38k | } |
39 | | } |
40 | | |
41 | | #[derive(Clone, Debug, PartialEq, Eq)] |
42 | | pub struct Task { |
43 | | pub id: Uuid, |
44 | | pub title: String, |
45 | | pub notes: String, |
46 | | /// Calendar dates, not UTC midnights: task dates do not shift with time zones. |
47 | | pub start_date: Option<NaiveDate>, |
48 | | pub due_date: Option<NaiveDate>, |
49 | | pub important: bool, |
50 | | pub urgent: bool, |
51 | | pub status: TaskStatus, |
52 | | pub kind: TaskKind, |
53 | | pub project_id: Option<Uuid>, |
54 | | pub prerequisite_ids: Vec<Uuid>, |
55 | | /// Trimmed http(s) URL, or `None`. |
56 | | pub link: Option<String>, |
57 | | pub created_at: DateTime<Utc>, |
58 | | pub updated_at: DateTime<Utc>, |
59 | | } |
60 | | |
61 | | impl Task { |
62 | 711 | pub fn new(draft: TaskDraft) -> Result<Self> { |
63 | 711 | let now = Utc::now(); |
64 | 711 | let mut task = Self { |
65 | 711 | id: Uuid::new_v4(), |
66 | 711 | title: String::new(), |
67 | 711 | notes: String::new(), |
68 | 711 | start_date: None, |
69 | 711 | due_date: None, |
70 | 711 | important: false, |
71 | 711 | urgent: false, |
72 | 711 | status: TaskStatus::ToDo, |
73 | 711 | kind: TaskKind::Task, |
74 | 711 | project_id: None, |
75 | 711 | prerequisite_ids: Vec::new(), |
76 | 711 | link: None, |
77 | 711 | created_at: now, |
78 | 711 | updated_at: now, |
79 | 711 | }; |
80 | 711 | task.apply(draft)?4 ; |
81 | 707 | Ok(task) |
82 | 711 | } |
83 | | |
84 | 748 | pub fn draft(&self) -> TaskDraft { |
85 | 748 | TaskDraft { |
86 | 748 | title: self.title.clone(), |
87 | 748 | notes: self.notes.clone(), |
88 | 748 | start_date: self.start_date, |
89 | 748 | due_date: self.due_date, |
90 | 748 | important: self.important, |
91 | 748 | urgent: self.urgent, |
92 | 748 | status: self.status, |
93 | 748 | kind: self.kind, |
94 | 748 | project_id: self.project_id, |
95 | 748 | prerequisite_ids: self.prerequisite_ids.clone(), |
96 | 748 | link: self.link.clone(), |
97 | 748 | } |
98 | 748 | } |
99 | | |
100 | | /// Validate before mutating so failed edits leave the task untouched. |
101 | 756 | pub fn apply(&mut self, draft: TaskDraft) -> Result<()> { |
102 | 756 | draft.validate()?9 ; |
103 | 747 | self.title = draft.title.trim().to_owned(); |
104 | 747 | self.notes = draft.notes; |
105 | 747 | self.start_date = draft.start_date; |
106 | 747 | self.due_date = draft.due_date; |
107 | 747 | self.important = draft.important; |
108 | 747 | self.urgent = draft.urgent; |
109 | 747 | self.status = draft.status; |
110 | 747 | self.kind = draft.kind; |
111 | 747 | self.project_id = draft.project_id; |
112 | 747 | self.prerequisite_ids = draft.prerequisite_ids; |
113 | 747 | self.prerequisite_ids.sort_unstable(); |
114 | 747 | self.prerequisite_ids.dedup(); |
115 | 747 | self.link = draft |
116 | 747 | .link |
117 | 747 | .as_deref() |
118 | 747 | .map(crate::core::link::normalize_link) |
119 | 747 | .transpose()?0 |
120 | | .flatten(); |
121 | 747 | self.updated_at = Utc::now(); |
122 | 747 | Ok(()) |
123 | 756 | } |
124 | | |
125 | 12 | pub fn set_completed(&mut self, completed: bool) { |
126 | 12 | self.set_status(if completed { |
127 | 8 | TaskStatus::Done |
128 | | } else { |
129 | 4 | TaskStatus::ToDo |
130 | | }); |
131 | 12 | } |
132 | | |
133 | 42.0k | pub fn is_completed(&self) -> bool { |
134 | 42.0k | self.status == TaskStatus::Done |
135 | 42.0k | } |
136 | | |
137 | 34 | pub fn set_status(&mut self, status: TaskStatus) { |
138 | 34 | self.status = status; |
139 | 34 | self.updated_at = Utc::now(); |
140 | 34 | } |
141 | | |
142 | 314 | pub fn quadrant(&self) -> Quadrant { |
143 | 314 | Quadrant::from_flags(self.important, self.urgent) |
144 | 314 | } |
145 | | |
146 | 9 | pub fn move_to(&mut self, quadrant: Quadrant) { |
147 | 9 | (self.important, self.urgent) = quadrant.flags(); |
148 | 9 | self.updated_at = Utc::now(); |
149 | 9 | } |
150 | | |
151 | 14 | pub fn move_to_project(&mut self, project_id: Option<Uuid>) { |
152 | 14 | self.project_id = project_id; |
153 | 14 | self.updated_at = Utc::now(); |
154 | 14 | } |
155 | | |
156 | 4.15k | pub fn is_overdue(&self, today: NaiveDate) -> bool { |
157 | 4.15k | !self.is_completed() && self.due_date4.05k .is_some_and(|date| date753 < today753 ) |
158 | 4.15k | } |
159 | | } |
160 | | |
161 | | /// The single source of truth for progress and completion in every view. |
162 | | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
163 | | pub enum TaskStatus { |
164 | | #[default] |
165 | | ToDo, |
166 | | InProgress, |
167 | | Done, |
168 | | OnHold, |
169 | | } |
170 | | |
171 | | impl TaskStatus { |
172 | | pub const ALL: [Self; 4] = [Self::ToDo, Self::InProgress, Self::Done, Self::OnHold]; |
173 | | |
174 | 4.43k | pub fn title(self) -> &'static str { |
175 | 4.43k | match self { |
176 | 2.95k | Self::ToDo => "To do", |
177 | 456 | Self::InProgress => "In progress", |
178 | 540 | Self::Done => "Done", |
179 | 483 | Self::OnHold => "On hold", |
180 | | } |
181 | 4.43k | } |
182 | | |
183 | 6.34k | pub fn as_str(self) -> &'static str { |
184 | 6.34k | match self { |
185 | 2.24k | Self::ToDo => "to_do", |
186 | 1.41k | Self::InProgress => "in_progress", |
187 | 1.40k | Self::Done => "done", |
188 | 1.27k | Self::OnHold => "on_hold", |
189 | | } |
190 | 6.34k | } |
191 | | } |
192 | | |
193 | | #[derive(Debug)] |
194 | | pub struct ParseTaskStatusError(String); |
195 | | |
196 | | impl fmt::Display for ParseTaskStatusError { |
197 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
198 | 0 | write!(f, "Unknown task status: {}", self.0) |
199 | 0 | } |
200 | | } |
201 | | |
202 | | impl std::error::Error for ParseTaskStatusError {} |
203 | | |
204 | | impl FromStr for TaskStatus { |
205 | | type Err = ParseTaskStatusError; |
206 | | |
207 | 6.04k | fn from_str(value: &str) -> Result<Self, Self::Err> { |
208 | 6.04k | match value { |
209 | 4.35k | "to_do" => Ok(Self::ToDo), |
210 | 541 | "in_progress" => Ok(Self::InProgress), |
211 | 716 | "done" => Ok(Self::Done), |
212 | 433 | "on_hold" => Ok(Self::OnHold), |
213 | 3 | _ => Err(ParseTaskStatusError(value.to_owned())), |
214 | | } |
215 | 6.04k | } |
216 | | } |
217 | | |
218 | | /// What sort of item a task is; purely descriptive and independent of status. |
219 | | /// Merge request, Issue, and Jira describe tracked work and are suggested by a task's link. |
220 | | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] |
221 | | pub enum TaskKind { |
222 | | #[default] |
223 | | Task, |
224 | | Idea, |
225 | | Bug, |
226 | | MergeRequest, |
227 | | Issue, |
228 | | Jira, |
229 | | } |
230 | | |
231 | | impl TaskKind { |
232 | | pub const ALL: [Self; 6] = [ |
233 | | Self::Task, |
234 | | Self::Idea, |
235 | | Self::Bug, |
236 | | Self::MergeRequest, |
237 | | Self::Issue, |
238 | | Self::Jira, |
239 | | ]; |
240 | | |
241 | 2.53k | pub fn title(self) -> &'static str { |
242 | 2.53k | match self { |
243 | 1.21k | Self::Task => "Task", |
244 | 248 | Self::Idea => "Idea", |
245 | 287 | Self::Bug => "Bug", |
246 | 243 | Self::MergeRequest => "Merge request", |
247 | 241 | Self::Issue => "Issue", |
248 | 299 | Self::Jira => "Jira", |
249 | | } |
250 | 2.53k | } |
251 | | |
252 | 623 | pub fn as_str(self) -> &'static str { |
253 | 623 | match self { |
254 | 550 | Self::Task => "task", |
255 | 33 | Self::Idea => "idea", |
256 | 26 | Self::Bug => "bug", |
257 | 4 | Self::MergeRequest => "merge_request", |
258 | 4 | Self::Issue => "issue", |
259 | 6 | Self::Jira => "jira", |
260 | | } |
261 | 623 | } |
262 | | |
263 | | /// The type a link suggests: GitLab merge requests and GitHub pull requests are merge |
264 | | /// requests, their issues are issues, and Jira links are Jira. Other links suggest none. |
265 | 8 | pub fn for_link(link: &str) -> Option<Self> { |
266 | | use crate::core::link::{LinkKind, describe}; |
267 | 8 | match describe(link).kind { |
268 | 3 | LinkKind::GitLabMergeRequest | LinkKind::GitHubPullRequest => Some(Self::MergeRequest), |
269 | 2 | LinkKind::GitLabIssue | LinkKind::GitHubIssue => Some(Self::Issue), |
270 | 2 | LinkKind::Jira => Some(Self::Jira), |
271 | 1 | LinkKind::Web => None, |
272 | | } |
273 | 8 | } |
274 | | } |
275 | | |
276 | | #[derive(Debug)] |
277 | | pub struct ParseTaskKindError(String); |
278 | | |
279 | | impl fmt::Display for ParseTaskKindError { |
280 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
281 | 0 | write!(f, "Unknown task type: {}", self.0) |
282 | 0 | } |
283 | | } |
284 | | |
285 | | impl std::error::Error for ParseTaskKindError {} |
286 | | |
287 | | impl FromStr for TaskKind { |
288 | | type Err = ParseTaskKindError; |
289 | | |
290 | 4.63k | fn from_str(value: &str) -> Result<Self, Self::Err> { |
291 | 4.63k | match value { |
292 | 4.37k | "task" => Ok(Self::Task), |
293 | 116 | "idea" => Ok(Self::Idea), |
294 | 115 | "bug" => Ok(Self::Bug), |
295 | 8 | "merge_request" => Ok(Self::MergeRequest), |
296 | 8 | "issue" => Ok(Self::Issue), |
297 | 12 | "jira" => Ok(Self::Jira), |
298 | 2 | _ => Err(ParseTaskKindError(value.to_owned())), |
299 | | } |
300 | 4.63k | } |
301 | | } |
302 | | |
303 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
304 | | pub enum Quadrant { |
305 | | Do, |
306 | | Schedule, |
307 | | Delegate, |
308 | | Eliminate, |
309 | | } |
310 | | |
311 | | impl Quadrant { |
312 | | pub const ALL: [Self; 4] = [Self::Do, Self::Schedule, Self::Delegate, Self::Eliminate]; |
313 | | |
314 | 866 | pub fn from_flags(important: bool, urgent: bool) -> Self { |
315 | 866 | match (important, urgent) { |
316 | 63 | (true, true) => Self::Do, |
317 | 45 | (true, false) => Self::Schedule, |
318 | 9 | (false, true) => Self::Delegate, |
319 | 749 | (false, false) => Self::Eliminate, |
320 | | } |
321 | 866 | } |
322 | | |
323 | 3.29k | pub fn flags(self) -> (bool, bool) { |
324 | 3.29k | match self { |
325 | 577 | Self::Do => (true, true), |
326 | 602 | Self::Schedule => (true, false), |
327 | 559 | Self::Delegate => (false, true), |
328 | 1.55k | Self::Eliminate => (false, false), |
329 | | } |
330 | 3.29k | } |
331 | | |
332 | 6.74k | pub fn title(self) -> &'static str { |
333 | 6.74k | match self { |
334 | 1.67k | Self::Do => "Urgent & important", |
335 | 1.67k | Self::Schedule => "Important", |
336 | 1.67k | Self::Delegate => "Urgent", |
337 | 1.72k | Self::Eliminate => "Neither", |
338 | | } |
339 | 6.74k | } |
340 | | |
341 | 784 | pub fn description(self) -> &'static str { |
342 | 784 | match self { |
343 | 196 | Self::Do => "Important · Urgent", |
344 | 196 | Self::Schedule => "Important · Not urgent", |
345 | 196 | Self::Delegate => "Not important · Urgent", |
346 | 196 | Self::Eliminate => "Not important · Not urgent", |
347 | | } |
348 | 784 | } |
349 | | } |
350 | | |
351 | | /// Incomplete first, dated before undated, then stable creation/ID tie-breakers. |
352 | 6.27k | pub fn compare_tasks(a: &Task, b: &Task) -> Ordering { |
353 | 6.27k | a.is_completed() |
354 | 6.27k | .cmp(&b.is_completed()) |
355 | 4.14k | .then_with(|| match (a.due_date, b.due_date) { |
356 | 871 | (Some(a), Some(b)) => a.cmp(&b), |
357 | 98 | (Some(_), None) => Ordering::Less, |
358 | 194 | (None, Some(_)) => Ordering::Greater, |
359 | 2.97k | (None, None) => Ordering::Equal, |
360 | 4.14k | }) |
361 | 3.10k | .then_with(|| a.created_at.cmp(&b.created_at)) |
362 | 41 | .then_with(|| a.id.cmp(&b.id)) |
363 | 6.27k | } |
364 | | |
365 | 42 | pub fn parse_due_date(value: &str) -> Result<Option<NaiveDate>> { |
366 | 42 | let value = value.trim(); |
367 | 42 | if value.is_empty() { |
368 | 3 | return Ok(None); |
369 | 39 | } |
370 | 39 | let date = NaiveDate::parse_from_str(value, "%Y-%m-%d") |
371 | 2 | .map_err(|_| anyhow::anyhow!("Use a valid due date in YYYY-MM-DD format."))?; |
372 | 37 | if date.format("%Y-%m-%d").to_string() != value { |
373 | 1 | bail!("Use a valid due date in YYYY-MM-DD format."); |
374 | 36 | } |
375 | 36 | Ok(Some(date)) |
376 | 42 | } |
377 | | |
378 | | #[cfg(test)] |
379 | | mod tests { |
380 | | use super::*; |
381 | | |
382 | 9 | fn task(title: &str, due: &str) -> Task { |
383 | 9 | Task::new(TaskDraft { |
384 | 9 | title: title.into(), |
385 | 9 | due_date: parse_due_date(due).unwrap(), |
386 | 9 | ..Default::default() |
387 | 9 | }) |
388 | 9 | .unwrap() |
389 | 9 | } |
390 | | |
391 | | #[test] |
392 | 1 | fn links_are_trimmed_and_invalid_links_leave_the_task_untouched() { |
393 | 1 | let mut task = Task::new(TaskDraft { |
394 | 1 | title: "Review MR".into(), |
395 | 1 | link: Some(" https://gitlab.com/group/app/-/merge_requests/7 ".into()), |
396 | 1 | ..Default::default() |
397 | 1 | }) |
398 | 1 | .unwrap(); |
399 | 1 | assert_eq!( |
400 | 1 | task.link.as_deref(), |
401 | 1 | Some("https://gitlab.com/group/app/-/merge_requests/7") |
402 | | ); |
403 | 1 | let before = task.clone(); |
404 | 1 | let mut draft = task.draft(); |
405 | 1 | draft.link = Some("not a link".into()); |
406 | 1 | assert!(task.apply(draft).is_err()); |
407 | 1 | assert_eq!(task, before); |
408 | 1 | let mut draft = task.draft(); |
409 | 1 | draft.link = Some(" ".into()); |
410 | 1 | task.apply(draft).unwrap(); |
411 | 1 | assert_eq!(task.link, None); |
412 | 1 | } |
413 | | |
414 | | #[test] |
415 | 1 | fn links_suggest_tracker_types_and_types_round_trip() { |
416 | 1 | for (link, kind) in [ |
417 | 1 | ( |
418 | 1 | "https://gitlab.com/g/app/-/merge_requests/232", |
419 | 1 | Some(TaskKind::MergeRequest), |
420 | 1 | ), |
421 | 1 | ( |
422 | 1 | "https://github.com/o/repo/pull/7", |
423 | 1 | Some(TaskKind::MergeRequest), |
424 | 1 | ), |
425 | 1 | ( |
426 | 1 | "https://git.example.com/g/app/-/issues/4", |
427 | 1 | Some(TaskKind::Issue), |
428 | 1 | ), |
429 | 1 | ("https://github.com/o/repo/issues/4", Some(TaskKind::Issue)), |
430 | 1 | ( |
431 | 1 | "https://company.atlassian.net/browse/PROJ-1", |
432 | 1 | Some(TaskKind::Jira), |
433 | 1 | ), |
434 | 1 | ("https://example.com/page", None), |
435 | 1 | ] { |
436 | 6 | assert_eq!(TaskKind::for_link(link), kind, "{link}"); |
437 | | } |
438 | 1 | for kind in TaskKind::ALL { |
439 | 6 | assert_eq!(kind.as_str().parse::<TaskKind>().unwrap(), kind); |
440 | | } |
441 | 1 | assert!("epic".parse::<TaskKind>().is_err()); |
442 | 1 | } |
443 | | |
444 | | #[test] |
445 | 1 | fn validates_title_without_losing_existing_values() { |
446 | 1 | let mut task = task(" Keep me ", ""); |
447 | 1 | assert_eq!(task.title, "Keep me"); |
448 | 1 | let before = task.clone(); |
449 | 1 | assert!(task.apply(TaskDraft::default()).is_err()); |
450 | 1 | assert_eq!(task, before); |
451 | 1 | assert!(Task::new(TaskDraft::default()).is_err()); |
452 | 1 | } |
453 | | |
454 | | #[test] |
455 | 1 | fn date_parsing_is_strict_and_optional() { |
456 | 1 | assert_eq!(parse_due_date(" ").unwrap(), None); |
457 | 1 | assert!(parse_due_date("2026-02-29").is_err()); |
458 | 1 | assert!(parse_due_date("2026-1-2").is_err()); |
459 | 1 | assert!(parse_due_date("next week").is_err()); |
460 | 1 | assert!(parse_due_date("2028-02-29").unwrap().is_some()); |
461 | 1 | } |
462 | | |
463 | | #[test] |
464 | 1 | fn start_and_due_dates_are_independently_optional_and_validate_before_mutation() { |
465 | 1 | let start = parse_due_date("2026-11-09").unwrap(); |
466 | 1 | let due = parse_due_date("2026-11-20").unwrap(); |
467 | 1 | for (start_date, due_date) in [ |
468 | 1 | (None, None), |
469 | 1 | (start, None), |
470 | 1 | (None, due), |
471 | 1 | (start, due), |
472 | 1 | (start, start), |
473 | 1 | ] { |
474 | 5 | let task = Task::new(TaskDraft { |
475 | 5 | title: "Flexible dates".into(), |
476 | 5 | start_date, |
477 | 5 | due_date, |
478 | 5 | ..Default::default() |
479 | 5 | }) |
480 | 5 | .unwrap(); |
481 | 5 | assert_eq!(task.draft().start_date, start_date); |
482 | 5 | assert_eq!(task.draft().due_date, due_date); |
483 | | } |
484 | 1 | let mut task = task("Keep original", "2026-11-20"); |
485 | 1 | let before = task.clone(); |
486 | 1 | let mut invalid = task.draft(); |
487 | 1 | invalid.title = "Do not save this".into(); |
488 | 1 | invalid.start_date = parse_due_date("2026-11-21").unwrap(); |
489 | 1 | assert!(task.apply(invalid.clone()).is_err()); |
490 | 1 | assert_eq!(task, before); |
491 | 1 | assert!(Task::new(invalid).is_err()); |
492 | 1 | } |
493 | | |
494 | | #[test] |
495 | 1 | fn sorts_overdue_upcoming_undated_then_completed() { |
496 | 1 | let mut completed = task("completed", "2020-01-01"); |
497 | 1 | completed.set_completed(true); |
498 | 1 | let mut tasks = [ |
499 | 1 | task("undated", ""), |
500 | 1 | task("upcoming", "2030-01-01"), |
501 | 1 | completed, |
502 | 1 | task("overdue", "2020-01-02"), |
503 | 1 | ]; |
504 | 1 | tasks.sort_by(compare_tasks); |
505 | 1 | assert_eq!( |
506 | 4 | tasks.iter()1 .map(|t| t.title.as_str()).collect::<Vec<_>>(), |
507 | 1 | ["overdue", "upcoming", "undated", "completed"] |
508 | | ); |
509 | 1 | } |
510 | | |
511 | | #[test] |
512 | 1 | fn quadrant_moves_preserve_deadlines_and_identity() { |
513 | 1 | let mut task = task("Example", "2020-01-01"); |
514 | 1 | task.start_date = parse_due_date("2019-12-20").unwrap(); |
515 | 1 | let start_date = task.start_date; |
516 | 1 | let id = task.id; |
517 | 1 | let date = task.due_date; |
518 | 1 | for quadrant in Quadrant::ALL { |
519 | 4 | task.move_to(quadrant); |
520 | 4 | assert_eq!(task.quadrant(), quadrant); |
521 | 4 | assert_eq!(task.id, id); |
522 | 4 | assert_eq!(task.due_date, date); |
523 | 4 | assert_eq!(task.start_date, start_date); |
524 | | } |
525 | | // An overdue date does not make urgency automatic. |
526 | 1 | assert!(!task.urgent); |
527 | 1 | assert!(task.is_overdue(NaiveDate::from_ymd_opt(2026, 1, 1).unwrap())); |
528 | 1 | } |
529 | | |
530 | | #[test] |
531 | 1 | fn completing_and_reopening_retains_task_data() { |
532 | 1 | let mut task = task("Example", "2026-01-01"); |
533 | 1 | let draft = task.draft(); |
534 | 1 | task.set_completed(true); |
535 | 1 | assert!(!task.is_overdue(NaiveDate::from_ymd_opt(2026, 1, 2).unwrap())); |
536 | 1 | task.set_completed(false); |
537 | 1 | assert_eq!(task.draft(), draft); |
538 | 1 | assert!(task.is_overdue(NaiveDate::from_ymd_opt(2026, 1, 2).unwrap())); |
539 | 1 | } |
540 | | |
541 | | #[test] |
542 | 1 | fn status_changes_preserve_identity_deadline_and_priority() { |
543 | 1 | let mut task = task("Example", "2026-01-01"); |
544 | 1 | task.start_date = parse_due_date("2025-12-20").unwrap(); |
545 | 1 | task.move_to(Quadrant::Schedule); |
546 | 1 | let original = task.clone(); |
547 | 1 | for status in TaskStatus::ALL { |
548 | 4 | task.set_status(status); |
549 | 4 | assert_eq!(task.status, status); |
550 | 4 | assert_eq!(task.is_completed(), status == TaskStatus::Done); |
551 | 4 | assert_eq!(task.id, original.id); |
552 | 4 | assert_eq!(task.due_date, original.due_date); |
553 | 4 | assert_eq!(task.start_date, original.start_date); |
554 | 4 | assert_eq!(task.quadrant(), original.quadrant()); |
555 | 4 | assert_eq!(task.title, original.title); |
556 | 4 | assert_eq!(status.as_str().parse::<TaskStatus>().unwrap(), status); |
557 | | } |
558 | 1 | assert!("unknown".parse::<TaskStatus>().is_err()); |
559 | 1 | task.set_completed(true); |
560 | 1 | assert_eq!(task.status, TaskStatus::Done); |
561 | 1 | task.set_completed(false); |
562 | 1 | assert_eq!(task.status, TaskStatus::ToDo); |
563 | 1 | } |
564 | | |
565 | | #[test] |
566 | 1 | fn task_kind_defaults_to_task_and_round_trips() { |
567 | 1 | assert_eq!(TaskDraft::default().kind, TaskKind::Task); |
568 | 1 | for kind in TaskKind::ALL { |
569 | 6 | assert_eq!(kind.as_str().parse::<TaskKind>().unwrap(), kind); |
570 | | } |
571 | 1 | assert!("chore".parse::<TaskKind>().is_err()); |
572 | 1 | let mut task = Task::new(TaskDraft { |
573 | 1 | title: "Kind".into(), |
574 | 1 | ..Default::default() |
575 | 1 | }) |
576 | 1 | .unwrap(); |
577 | 1 | assert_eq!(task.kind, TaskKind::Task); |
578 | 1 | let mut draft = task.draft(); |
579 | 1 | draft.kind = TaskKind::Bug; |
580 | 1 | task.apply(draft).unwrap(); |
581 | 1 | assert_eq!(task.kind, TaskKind::Bug); |
582 | 1 | } |
583 | | |
584 | | #[test] |
585 | 1 | fn project_moves_preserve_prerequisites_and_other_task_fields() { |
586 | 1 | let mut task = Task::new(TaskDraft { |
587 | 1 | title: "Move me".into(), |
588 | 1 | notes: "Context".into(), |
589 | 1 | important: true, |
590 | 1 | start_date: parse_due_date("2026-11-01").unwrap(), |
591 | 1 | prerequisite_ids: vec![Uuid::new_v4()], |
592 | 1 | due_date: parse_due_date("2026-11-09").unwrap(), |
593 | 1 | status: TaskStatus::InProgress, |
594 | 1 | ..Default::default() |
595 | 1 | }) |
596 | 1 | .unwrap(); |
597 | 1 | let id = task.id; |
598 | 1 | let original = task.draft(); |
599 | 1 | for project in [Some(Uuid::new_v4()), Some(Uuid::new_v4()), None] { |
600 | 3 | task.move_to_project(project); |
601 | 3 | let mut expected = original.clone(); |
602 | 3 | expected.project_id = project; |
603 | 3 | assert_eq!(task.draft(), expected); |
604 | 3 | assert_eq!(task.id, id); |
605 | | } |
606 | 1 | } |
607 | | } |