tempo-cli 0.4.0

Automatic project time tracking CLI tool with beautiful terminal interface
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
use anyhow::{Context, Result};
use std::path::PathBuf;

use crate::db::queries::ProjectQueries;
use crate::db::{get_database_path, Database};
use crate::models::Project;
use crate::utils::paths::{
    canonicalize_path, detect_project_name, get_git_hash, has_tempo_marker, is_git_repository,
};
use crate::utils::validation::{
    validate_project_description, validate_project_id, validate_project_name,
    validate_project_path_enhanced,
};

/// Service layer for project-related business logic
pub struct ProjectService;

impl ProjectService {
    /// Create a new project with validation and auto-detection
    pub async fn create_project(
        name: Option<String>,
        path: Option<PathBuf>,
        description: Option<String>,
    ) -> Result<Project> {
        // Validate inputs early
        let validated_name = if let Some(n) = name {
            Some(validate_project_name(&n).context("Invalid project name provided")?)
        } else {
            None
        };

        let validated_description = if let Some(d) = description {
            Some(validate_project_description(&d).context("Invalid project description provided")?)
        } else {
            None
        };

        let project_path = if let Some(path) = path {
            validate_project_path_enhanced(&path).context("Invalid project path provided")?
        } else {
            std::env::current_dir().context("Failed to get current directory")?
        };

        let canonical_path = canonicalize_path(&project_path)?;

        // Auto-detect project name if not provided
        let project_name = validated_name.unwrap_or_else(|| {
            let detected = detect_project_name(&canonical_path);
            validate_project_name(&detected).unwrap_or_else(|_| "project".to_string())
        });

        // Perform database operations in a blocking task
        let mut project = Project::new(project_name, canonical_path.clone());
        project = project.with_description(validated_description);

        // Add Git metadata if available
        let git_hash = get_git_hash(&canonical_path);
        project = project.with_git_hash(git_hash);

        // Set description based on project type
        if project.description.is_none() {
            let auto_description = if is_git_repository(&canonical_path) {
                Some("Git repository".to_string())
            } else if has_tempo_marker(&canonical_path) {
                Some("Tempo tracked project".to_string())
            } else {
                None
            };
            project = project.with_description(auto_description);
        }

        // Database operations in blocking task
        let canonical_path_clone = canonical_path.clone();
        let project_clone = project.clone();

        let project_id = tokio::task::spawn_blocking(move || -> Result<i64> {
            let db = Self::get_database_sync()?;

            // Check if project already exists
            if let Some(existing) = ProjectQueries::find_by_path(&db.connection, &canonical_path_clone)? {
                return Err(anyhow::anyhow!(
                    "A project named '{}' already exists at this path. Use 'tempo list' to see existing projects.",
                    existing.name
                ));
            }

            // Save to database
            ProjectQueries::create(&db.connection, &project_clone)
        }).await??;

        project.id = Some(project_id);

        Ok(project)
    }

    /// List projects with optional filtering
    pub async fn list_projects(
        include_archived: bool,
        _tag_filter: Option<String>,
    ) -> Result<Vec<Project>> {
        tokio::task::spawn_blocking(move || -> Result<Vec<Project>> {
            let db = Self::get_database_sync()?;

            // TODO: Add tag filtering logic when tag system is implemented
            let projects = ProjectQueries::list_all(&db.connection, include_archived)?;

            Ok(projects)
        })
        .await?
    }

    /// Get a project by ID
    pub async fn get_project_by_id(project_id: i64) -> Result<Option<Project>> {
        let validated_id = validate_project_id(project_id).context("Invalid project ID")?;

        tokio::task::spawn_blocking(move || -> Result<Option<Project>> {
            let db = Self::get_database_sync()?;
            ProjectQueries::find_by_id(&db.connection, validated_id)
        })
        .await?
    }

    /// Get a project by path
    pub async fn get_project_by_path(path: &PathBuf) -> Result<Option<Project>> {
        let canonical_path = canonicalize_path(path)?;
        tokio::task::spawn_blocking(move || -> Result<Option<Project>> {
            let db = Self::get_database_sync()?;
            ProjectQueries::find_by_path(&db.connection, &canonical_path)
        })
        .await?
    }

    /// Update project metadata
    pub async fn update_project(
        project_id: i64,
        name: Option<String>,
        description: Option<String>,
    ) -> Result<bool> {
        let validated_id = validate_project_id(project_id).context("Invalid project ID")?;

        let validated_name = if let Some(n) = name {
            Some(validate_project_name(&n).context("Invalid project name")?)
        } else {
            None
        };

        let validated_description = if let Some(d) = description {
            Some(validate_project_description(&d).context("Invalid project description")?)
        } else {
            None
        };

        tokio::task::spawn_blocking(move || -> Result<bool> {
            let db = Self::get_database_sync()?;

            let mut updated = false;

            if let Some(name) = validated_name {
                let result = ProjectQueries::update_name(&db.connection, validated_id, name)?;
                if !result {
                    return Err(anyhow::anyhow!(
                        "Project with ID {} not found",
                        validated_id
                    ));
                }
                updated = true;
            }

            if let Some(description) = validated_description {
                let result = ProjectQueries::update_project_description(
                    &db.connection,
                    validated_id,
                    Some(description),
                )?;
                if !result {
                    return Err(anyhow::anyhow!(
                        "Project with ID {} not found",
                        validated_id
                    ));
                }
                updated = true;
            }

            Ok(updated)
        })
        .await?
    }

    /// Archive a project
    pub async fn archive_project(project_id: i64) -> Result<bool> {
        let validated_id = validate_project_id(project_id).context("Invalid project ID")?;

        tokio::task::spawn_blocking(move || -> Result<bool> {
            let db = Self::get_database_sync()?;
            let result = ProjectQueries::update_archived(&db.connection, validated_id, true)?;
            if !result {
                return Err(anyhow::anyhow!(
                    "Project with ID {} not found",
                    validated_id
                ));
            }
            Ok(result)
        })
        .await?
    }

    /// Unarchive a project
    pub async fn unarchive_project(project_id: i64) -> Result<bool> {
        let validated_id = validate_project_id(project_id).context("Invalid project ID")?;

        tokio::task::spawn_blocking(move || -> Result<bool> {
            let db = Self::get_database_sync()?;
            let result = ProjectQueries::update_archived(&db.connection, validated_id, false)?;
            if !result {
                return Err(anyhow::anyhow!(
                    "Project with ID {} not found",
                    validated_id
                ));
            }
            Ok(result)
        })
        .await?
    }

    // Private helper to get database connection (synchronous for use in blocking tasks)
    fn get_database_sync() -> Result<Database> {
        let db_path = get_database_path()?;
        Database::new(&db_path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_create_project_with_auto_detection() {
        let temp_dir = tempdir().unwrap();
        let project_path = temp_dir.path().to_path_buf();

        let result = ProjectService::create_project(
            None, // Auto-detect name
            Some(project_path.clone()),
            None,
        )
        .await;

        assert!(result.is_ok());
        let project = result.unwrap();
        // Compare canonicalized paths since temp paths can be symlinked
        let expected_canonical = canonicalize_path(&project_path).unwrap();
        assert_eq!(project.path, expected_canonical);
        assert!(!project.name.is_empty());
    }

    #[tokio::test]
    async fn test_path_validation() {
        // Test invalid path
        let invalid_path = PathBuf::from("/nonexistent/path/that/should/not/exist");
        let result = ProjectService::create_project(
            Some("Test Project".to_string()),
            Some(invalid_path),
            None,
        )
        .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_project_name_detection() {
        let temp_dir = tempdir().unwrap();

        // Test with specific directory name
        let project_dir = temp_dir.path().join("my-awesome-project");
        fs::create_dir_all(&project_dir).unwrap();

        let detected_name = detect_project_name(&project_dir);
        assert_eq!(detected_name, "my-awesome-project");
    }

    #[tokio::test]
    async fn test_git_repository_detection() {
        let temp_dir = tempdir().unwrap();
        let git_dir = temp_dir.path().join("git_project");
        fs::create_dir_all(&git_dir).unwrap();

        // Create .git directory
        let git_meta = git_dir.join(".git");
        fs::create_dir_all(&git_meta).unwrap();
        fs::write(git_meta.join("HEAD"), "ref: refs/heads/main\n").unwrap();

        // Test Git repository detection
        assert!(is_git_repository(&git_dir));

        // Test project creation with Git repo
        let result = ProjectService::create_project(
            Some("Git Test".to_string()),
            Some(git_dir.clone()),
            None,
        )
        .await;

        if let Ok(project) = result {
            assert_eq!(project.description, Some("Git repository".to_string()));
        }
    }

    #[tokio::test]
    async fn test_tempo_marker_detection() {
        let temp_dir = tempdir().unwrap();
        let tempo_dir = temp_dir.path().join("tempo_project");
        fs::create_dir_all(&tempo_dir).unwrap();

        // Create .tempo marker
        fs::write(tempo_dir.join(".tempo"), "").unwrap();

        // Test Tempo marker detection
        assert!(has_tempo_marker(&tempo_dir));

        // Test project creation with Tempo marker
        let result = ProjectService::create_project(
            Some("Tempo Test".to_string()),
            Some(tempo_dir.clone()),
            None,
        )
        .await;

        if let Ok(project) = result {
            assert_eq!(
                project.description,
                Some("Tempo tracked project".to_string())
            );
        }
    }

    #[tokio::test]
    async fn test_project_filtering() {
        // Test list projects with no tag filter
        let result = ProjectService::list_projects(false, None).await;
        assert!(result.is_ok());

        // Test list projects including archived
        let result_archived = ProjectService::list_projects(true, None).await;
        assert!(result_archived.is_ok());

        // Test tag filtering (placeholder for future implementation)
        let result_filtered = ProjectService::list_projects(false, Some("work".to_string())).await;
        assert!(result_filtered.is_ok());
    }

    #[tokio::test]
    async fn test_project_retrieval_edge_cases() {
        // Test get project by non-existent ID
        let result = ProjectService::get_project_by_id(99999).await;
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());

        // Test get project by non-existent path
        // Use a temp directory that exists but has no project
        let temp_dir = tempdir().unwrap();
        let nonexistent_project_path = temp_dir.path().join("nonexistent_project");
        std::fs::create_dir_all(&nonexistent_project_path).unwrap();

        let result = ProjectService::get_project_by_path(&nonexistent_project_path).await;
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_project_update_operations() {
        // Test updating non-existent project (should now fail with better error)
        let result = ProjectService::update_project(
            99999,
            Some("New Name".to_string()),
            Some("New Description".to_string()),
        )
        .await;
        // Should fail with "Project not found" error
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Project with ID 99999 not found"));

        // Test archiving non-existent project
        let archive_result = ProjectService::archive_project(99999).await;
        assert!(archive_result.is_err());
        assert!(archive_result
            .unwrap_err()
            .to_string()
            .contains("Project with ID 99999 not found"));

        // Test unarchiving non-existent project
        let unarchive_result = ProjectService::unarchive_project(99999).await;
        assert!(unarchive_result.is_err());
        assert!(unarchive_result
            .unwrap_err()
            .to_string()
            .contains("Project with ID 99999 not found"));
    }
}