1use super::TimePoint;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct WorkRecord {
6 pub id: u32,
7 pub name: String,
8 pub start: TimePoint,
9 pub end: TimePoint,
10 pub total_minutes: u32,
11 #[serde(default)]
12 pub description: String,
13}
14
15impl WorkRecord {
16 pub fn new(id: u32, name: String, start: TimePoint, end: TimePoint) -> Self {
17 let total_minutes = Self::calculate_duration(&start, &end);
18 WorkRecord {
19 id,
20 name,
21 start,
22 end,
23 total_minutes,
24 description: String::new(),
25 }
26 }
27
28 pub fn calculate_duration(start: &TimePoint, end: &TimePoint) -> u32 {
29 let start_mins = start.to_minutes_since_midnight();
30 let end_mins = end.to_minutes_since_midnight();
31
32 if end_mins >= start_mins {
33 end_mins - start_mins
34 } else {
35 (24 * 60 - start_mins) + end_mins
36 }
37 }
38
39 pub fn update_duration(&mut self) {
40 self.total_minutes = Self::calculate_duration(&self.start, &self.end);
41 }
42
43 pub fn format_duration(&self) -> String {
44 let hours = self.total_minutes / 60;
45 let minutes = self.total_minutes % 60;
46 format!("{}h {:02}m", hours, minutes)
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_new_work_record() {
56 let start = TimePoint::new(9, 0).unwrap();
57 let end = TimePoint::new(17, 0).unwrap();
58 let record = WorkRecord::new(1, "Coding".to_string(), start, end);
59
60 assert_eq!(record.id, 1);
61 assert_eq!(record.name, "Coding");
62 assert_eq!(record.start, start);
63 assert_eq!(record.end, end);
64 assert_eq!(record.total_minutes, 480); assert_eq!(record.description, "");
66 }
67
68 #[test]
69 fn test_calculate_duration_same_day() {
70 let start = TimePoint::new(9, 0).unwrap();
71 let end = TimePoint::new(17, 30).unwrap();
72 let duration = WorkRecord::calculate_duration(&start, &end);
73 assert_eq!(duration, 510); }
75
76 #[test]
77 fn test_calculate_duration_zero() {
78 let time = TimePoint::new(12, 0).unwrap();
79 let duration = WorkRecord::calculate_duration(&time, &time);
80 assert_eq!(duration, 0);
81 }
82
83 #[test]
84 fn test_calculate_duration_one_minute() {
85 let start = TimePoint::new(12, 0).unwrap();
86 let end = TimePoint::new(12, 1).unwrap();
87 let duration = WorkRecord::calculate_duration(&start, &end);
88 assert_eq!(duration, 1);
89 }
90
91 #[test]
92 fn test_calculate_duration_overnight() {
93 let start = TimePoint::new(23, 0).unwrap();
94 let end = TimePoint::new(1, 0).unwrap();
95 let duration = WorkRecord::calculate_duration(&start, &end);
96 assert_eq!(duration, 120); }
98
99 #[test]
100 fn test_calculate_duration_overnight_complex() {
101 let start = TimePoint::new(22, 30).unwrap();
102 let end = TimePoint::new(2, 15).unwrap();
103 let duration = WorkRecord::calculate_duration(&start, &end);
104 assert_eq!(duration, 225); }
106
107 #[test]
108 fn test_calculate_duration_full_day() {
109 let start = TimePoint::new(0, 0).unwrap();
110 let end = TimePoint::new(0, 0).unwrap();
111 let duration = WorkRecord::calculate_duration(&start, &end);
112 assert_eq!(duration, 0); }
114
115 #[test]
116 fn test_calculate_duration_almost_full_day() {
117 let start = TimePoint::new(0, 1).unwrap();
118 let end = TimePoint::new(0, 0).unwrap();
119 let duration = WorkRecord::calculate_duration(&start, &end);
120 assert_eq!(duration, 1439); }
122
123 #[test]
124 fn test_update_duration() {
125 let start = TimePoint::new(9, 0).unwrap();
126 let end = TimePoint::new(10, 0).unwrap();
127 let mut record = WorkRecord::new(1, "Task".to_string(), start, end);
128 assert_eq!(record.total_minutes, 60);
129
130 record.end = TimePoint::new(11, 30).unwrap();
132 record.update_duration();
133 assert_eq!(record.total_minutes, 150); }
135
136 #[test]
137 fn test_format_duration_zero() {
138 let start = TimePoint::new(9, 0).unwrap();
139 let end = TimePoint::new(9, 0).unwrap();
140 let record = WorkRecord::new(1, "Task".to_string(), start, end);
141 assert_eq!(record.format_duration(), "0h 00m");
142 }
143
144 #[test]
145 fn test_format_duration_minutes_only() {
146 let start = TimePoint::new(9, 0).unwrap();
147 let end = TimePoint::new(9, 45).unwrap();
148 let record = WorkRecord::new(1, "Task".to_string(), start, end);
149 assert_eq!(record.format_duration(), "0h 45m");
150 }
151
152 #[test]
153 fn test_format_duration_hours_only() {
154 let start = TimePoint::new(9, 0).unwrap();
155 let end = TimePoint::new(12, 0).unwrap();
156 let record = WorkRecord::new(1, "Task".to_string(), start, end);
157 assert_eq!(record.format_duration(), "3h 00m");
158 }
159
160 #[test]
161 fn test_format_duration_hours_and_minutes() {
162 let start = TimePoint::new(9, 15).unwrap();
163 let end = TimePoint::new(17, 45).unwrap();
164 let record = WorkRecord::new(1, "Task".to_string(), start, end);
165 assert_eq!(record.format_duration(), "8h 30m");
166 }
167
168 #[test]
169 fn test_format_duration_long() {
170 let start = TimePoint::new(0, 0).unwrap();
171 let end = TimePoint::new(23, 59).unwrap();
172 let record = WorkRecord::new(1, "Task".to_string(), start, end);
173 assert_eq!(record.format_duration(), "23h 59m");
174 }
175
176 #[test]
177 fn test_description_field() {
178 let start = TimePoint::new(9, 0).unwrap();
179 let end = TimePoint::new(10, 0).unwrap();
180 let mut record = WorkRecord::new(1, "Task".to_string(), start, end);
181
182 assert_eq!(record.description, "");
183 record.description = "Important meeting notes".to_string();
184 assert_eq!(record.description, "Important meeting notes");
185 }
186
187 #[test]
188 fn test_clone() {
189 let start = TimePoint::new(9, 0).unwrap();
190 let end = TimePoint::new(17, 0).unwrap();
191 let record1 = WorkRecord::new(1, "Coding".to_string(), start, end);
192 let record2 = record1.clone();
193
194 assert_eq!(record1.id, record2.id);
195 assert_eq!(record1.name, record2.name);
196 assert_eq!(record1.start, record2.start);
197 assert_eq!(record1.end, record2.end);
198 assert_eq!(record1.total_minutes, record2.total_minutes);
199 }
200}