stint-core 0.3.0

Core domain logic, data models, and storage traits for Stint
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
//! High-level business logic for Stint operations.
//!
//! The service layer wraps the `Storage` trait with validation and business rules,
//! keeping CLI handlers thin.

use time::OffsetDateTime;

use crate::error::StintError;
use crate::models::entry::{EntryFilter, EntrySource, TimeEntry};
use crate::models::project::{Project, ProjectStatus};
use crate::models::types::{EntryId, ProjectId};
use crate::storage::Storage;

/// High-level operations for Stint, wrapping a storage backend.
pub struct StintService<S: Storage> {
    storage: S,
}

impl<S: Storage> StintService<S> {
    /// Creates a new service wrapping the given storage backend.
    pub fn new(storage: S) -> Self {
        Self { storage }
    }

    /// Returns a reference to the underlying storage.
    pub fn storage(&self) -> &S {
        &self.storage
    }

    /// Looks up an active project by name, returning an error if not found or archived.
    fn resolve_active_project(&self, name: &str) -> Result<Project, StintError> {
        let project = self
            .storage
            .get_project_by_name(name)?
            .ok_or_else(|| StintError::InvalidInput(format!("project '{name}' not found")))?;

        if project.status == ProjectStatus::Archived {
            return Err(StintError::ProjectNotActive(name.to_string()));
        }

        Ok(project)
    }

    /// Starts a timer for the named project.
    ///
    /// Fails if a timer is already running or the project is not found/archived.
    pub fn start_timer(&self, project_name: &str) -> Result<(TimeEntry, Project), StintError> {
        let project = self.resolve_active_project(project_name)?;

        // Check for any running timer across all projects
        if let Some(running) = self.storage.get_any_running_entry()? {
            let running_project = self.storage.get_project(&running.project_id)?;
            let name = running_project
                .map(|p| p.name)
                .unwrap_or_else(|| "unknown".to_string());
            return Err(StintError::TimerAlreadyRunning(name));
        }

        let now = OffsetDateTime::now_utc();
        let entry = TimeEntry {
            id: EntryId::new(),
            project_id: project.id.clone(),
            session_id: None,
            start: now,
            end: None,
            duration_secs: None,
            source: EntrySource::Manual,
            notes: None,
            tags: vec![],
            created_at: now,
            updated_at: now,
        };

        self.storage.create_entry(&entry)?;
        Ok((entry, project))
    }

    /// Stops the currently running timer.
    ///
    /// Fails if no timer is running.
    pub fn stop_timer(&self) -> Result<(TimeEntry, Project), StintError> {
        let mut entry = self
            .storage
            .get_any_running_entry()?
            .ok_or(StintError::NoRunningTimer)?;

        let now = OffsetDateTime::now_utc();
        entry.end = Some(now);
        entry.duration_secs = Some((now - entry.start).whole_seconds());
        entry.updated_at = now;

        self.storage.update_entry(&entry)?;

        let project = self
            .storage
            .get_project(&entry.project_id)?
            .ok_or_else(|| StintError::InvalidInput("project not found".to_string()))?;

        Ok((entry, project))
    }

    /// Adds a completed time entry retroactively.
    ///
    /// If no date is provided, uses today. The entry is created with `source: Added`.
    pub fn add_time(
        &self,
        project_name: &str,
        duration_secs: i64,
        date: Option<OffsetDateTime>,
        notes: Option<&str>,
    ) -> Result<(TimeEntry, Project), StintError> {
        if duration_secs <= 0 {
            return Err(StintError::InvalidInput(
                "duration must be greater than zero".to_string(),
            ));
        }

        let project = self.resolve_active_project(project_name)?;

        let start = date.unwrap_or_else(|| {
            let now = OffsetDateTime::now_utc();
            now.date().midnight().assume_utc()
        });
        let end = start + time::Duration::seconds(duration_secs);
        let now = OffsetDateTime::now_utc();

        let entry = TimeEntry {
            id: EntryId::new(),
            project_id: project.id.clone(),
            session_id: None,
            start,
            end: Some(end),
            duration_secs: Some(duration_secs),
            source: EntrySource::Added,
            notes: notes.map(|s| s.to_string()),
            tags: vec![],
            created_at: now,
            updated_at: now,
        };

        self.storage.create_entry(&entry)?;
        Ok((entry, project))
    }

    /// Returns the currently running timer and its project, if any.
    pub fn get_status(&self) -> Result<Option<(TimeEntry, Project)>, StintError> {
        let entry = self.storage.get_any_running_entry()?;
        match entry {
            Some(e) => {
                let project = self
                    .storage
                    .get_project(&e.project_id)?
                    .ok_or_else(|| StintError::InvalidInput("project not found".to_string()))?;
                Ok(Some((e, project)))
            }
            None => Ok(None),
        }
    }

    /// Archives a project, hiding it from default listings.
    ///
    /// Stops any running timer for the project first.
    pub fn archive_project(&self, name: &str) -> Result<Project, StintError> {
        let mut project = self
            .storage
            .get_project_by_name(name)?
            .ok_or_else(|| StintError::InvalidInput(format!("project '{name}' not found")))?;

        if project.status == ProjectStatus::Archived {
            return Err(StintError::InvalidInput(format!(
                "project '{name}' is already archived"
            )));
        }

        // Stop any running timer for this project
        if let Some(mut entry) = self.storage.get_running_entry(&project.id)? {
            let now = OffsetDateTime::now_utc();
            entry.end = Some(now);
            entry.duration_secs = Some((now - entry.start).whole_seconds());
            entry.updated_at = now;
            self.storage.update_entry(&entry)?;
        }

        project.status = ProjectStatus::Archived;
        project.updated_at = OffsetDateTime::now_utc();
        self.storage.update_project(&project)?;

        Ok(project)
    }

    /// Deletes a project and all its entries.
    pub fn delete_project(&self, name: &str) -> Result<String, StintError> {
        let project = self
            .storage
            .get_project_by_name(name)?
            .ok_or_else(|| StintError::InvalidInput(format!("project '{name}' not found")))?;

        self.storage.delete_project(&project.id)?;
        Ok(project.name)
    }

    /// Lists entries matching the given filter, enriched with project data.
    pub fn get_entries(
        &self,
        filter: &EntryFilter,
    ) -> Result<Vec<(TimeEntry, Project)>, StintError> {
        let entries = self.storage.list_entries(filter)?;
        let mut results = Vec::with_capacity(entries.len());

        // Cache projects to avoid repeated lookups
        let mut project_cache: std::collections::HashMap<String, Project> =
            std::collections::HashMap::new();

        for entry in entries {
            let pid_str = entry.project_id.as_str().to_owned();
            let project = if let Some(cached) = project_cache.get(&pid_str) {
                cached.clone()
            } else {
                let p = self
                    .storage
                    .get_project(&entry.project_id)?
                    .ok_or_else(|| {
                        StintError::InvalidInput(format!(
                            "project not found for entry {}",
                            entry.id
                        ))
                    })?;
                project_cache.insert(pid_str, p.clone());
                p
            };

            results.push((entry, project));
        }

        Ok(results)
    }

    /// Returns the most recent time entry with its project.
    pub fn get_last_entry(&self) -> Result<Option<(TimeEntry, Project)>, StintError> {
        match self.storage.get_last_entry()? {
            Some(entry) => {
                let project = self
                    .storage
                    .get_project(&entry.project_id)?
                    .ok_or_else(|| {
                        StintError::InvalidInput("project not found for entry".to_string())
                    })?;
                Ok(Some((entry, project)))
            }
            None => Ok(None),
        }
    }

    /// Deletes a time entry by ID.
    pub fn delete_entry(&self, id: &EntryId) -> Result<(), StintError> {
        self.storage.delete_entry(id)?;
        Ok(())
    }

    /// Updates a time entry.
    pub fn update_entry(&self, entry: &TimeEntry) -> Result<(), StintError> {
        self.storage.update_entry(entry)?;
        Ok(())
    }

    /// Resolves a project name to its ID for use in filters.
    pub fn resolve_project_id(&self, name: &str) -> Result<ProjectId, StintError> {
        let project = self
            .storage
            .get_project_by_name(name)?
            .ok_or_else(|| StintError::InvalidInput(format!("project '{name}' not found")))?;
        Ok(project.id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::project::{Project, ProjectSource};
    use crate::storage::sqlite::SqliteStorage;
    use std::path::PathBuf;

    fn setup() -> StintService<SqliteStorage> {
        let storage = SqliteStorage::open_in_memory().unwrap();
        StintService::new(storage)
    }

    fn create_project(service: &StintService<SqliteStorage>, name: &str) {
        let now = OffsetDateTime::now_utc();
        let project = Project {
            id: ProjectId::new(),
            name: name.to_string(),
            paths: vec![PathBuf::from(format!("/home/user/{name}"))],
            tags: vec![],
            hourly_rate_cents: None,
            status: ProjectStatus::Active,
            source: ProjectSource::Manual,
            created_at: now,
            updated_at: now,
        };
        service.storage().create_project(&project).unwrap();
    }

    #[test]
    fn start_and_stop_timer() {
        let service = setup();
        create_project(&service, "my-app");

        let (entry, project) = service.start_timer("my-app").unwrap();
        assert!(entry.is_running());
        assert_eq!(project.name, "my-app");

        let (stopped, _) = service.stop_timer().unwrap();
        assert!(!stopped.is_running());
        assert!(stopped.duration_secs.unwrap() >= 0);
    }

    #[test]
    fn start_while_running_errors() {
        let service = setup();
        create_project(&service, "app-1");
        create_project(&service, "app-2");

        service.start_timer("app-1").unwrap();
        let result = service.start_timer("app-2");
        assert!(matches!(result, Err(StintError::TimerAlreadyRunning(_))));
    }

    #[test]
    fn stop_without_running_errors() {
        let service = setup();
        let result = service.stop_timer();
        assert!(matches!(result, Err(StintError::NoRunningTimer)));
    }

    #[test]
    fn start_archived_project_errors() {
        let service = setup();
        create_project(&service, "old-app");
        service.archive_project("old-app").unwrap();

        let result = service.start_timer("old-app");
        assert!(matches!(result, Err(StintError::ProjectNotActive(_))));
    }

    #[test]
    fn start_nonexistent_project_errors() {
        let service = setup();
        let result = service.start_timer("no-such-project");
        assert!(matches!(result, Err(StintError::InvalidInput(_))));
    }

    #[test]
    fn add_time_zero_duration_errors() {
        let service = setup();
        create_project(&service, "my-app");

        let result = service.add_time("my-app", 0, None, None);
        assert!(matches!(result, Err(StintError::InvalidInput(_))));
    }

    #[test]
    fn add_time_negative_duration_errors() {
        let service = setup();
        create_project(&service, "my-app");

        let result = service.add_time("my-app", -3600, None, None);
        assert!(matches!(result, Err(StintError::InvalidInput(_))));
    }

    #[test]
    fn add_time() {
        let service = setup();
        create_project(&service, "my-app");

        let (entry, project) = service
            .add_time("my-app", 3600, None, Some("Retroactive"))
            .unwrap();

        assert!(!entry.is_running());
        assert_eq!(entry.duration_secs, Some(3600));
        assert_eq!(entry.source, EntrySource::Added);
        assert_eq!(entry.notes.as_deref(), Some("Retroactive"));
        assert_eq!(project.name, "my-app");
    }

    #[test]
    fn get_status_running() {
        let service = setup();
        create_project(&service, "my-app");
        service.start_timer("my-app").unwrap();

        let status = service.get_status().unwrap();
        assert!(status.is_some());
        let (entry, project) = status.unwrap();
        assert!(entry.is_running());
        assert_eq!(project.name, "my-app");
    }

    #[test]
    fn get_status_idle() {
        let service = setup();
        let status = service.get_status().unwrap();
        assert!(status.is_none());
    }

    #[test]
    fn archive_stops_running_timer() {
        let service = setup();
        create_project(&service, "my-app");
        service.start_timer("my-app").unwrap();

        service.archive_project("my-app").unwrap();

        // Timer should be stopped
        let status = service.get_status().unwrap();
        assert!(status.is_none());

        // Project should be archived
        let project = service
            .storage()
            .get_project_by_name("my-app")
            .unwrap()
            .unwrap();
        assert_eq!(project.status, ProjectStatus::Archived);
    }

    #[test]
    fn delete_project() {
        let service = setup();
        create_project(&service, "doomed");

        let name = service.delete_project("doomed").unwrap();
        assert_eq!(name, "doomed");
        assert!(service
            .storage()
            .get_project_by_name("doomed")
            .unwrap()
            .is_none());
    }

    #[test]
    fn get_entries_with_project() {
        let service = setup();
        create_project(&service, "my-app");
        service.add_time("my-app", 3600, None, None).unwrap();
        service.add_time("my-app", 1800, None, None).unwrap();

        let entries = service.get_entries(&EntryFilter::default()).unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].1.name, "my-app");
    }
}