ryo-storage 0.1.0

Persistent storage and transaction log for RYO
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
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
//! Global RYO storage management.
//!
//! Manages the `~/.ryo/` directory structure:
//! ```text
//! ~/.ryo/
//! ├── sessions/           # Session transaction logs
//! │   ├── {id}.txlog.json
//! │   └── ...
//! ├── index.json          # Session metadata index
//! └── config.toml         # (future) Global configuration
//! ```

use super::format::{get_serializer, Format, FormatError};
use super::index::{SessionIndex, SessionMeta};
use super::project::{ProjectIndex, ProjectMeta};
use crate::txlog::TxLog;
use std::fs::{self, File};
use std::io::BufReader;
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Errors from storage operations.
#[derive(Debug, Error)]
pub enum StorageError {
    /// Underlying filesystem I/O failure.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Session payload (de)serialization failure; wraps [`FormatError`].
    #[error("Format error: {0}")]
    Format(#[from] FormatError),

    /// The requested session id does not exist in the store.
    #[error("Session not found: {0}")]
    SessionNotFound(String),

    /// The requested project id does not exist in the store.
    #[error("Project not found: {0}")]
    ProjectNotFound(String),

    /// The storage directory has not been initialized yet (no metadata
    /// file). Carries the directory that was inspected.
    #[error("Storage not initialized at {0}")]
    NotInitialized(PathBuf),

    /// Session/project index lookup or update failure. Carries a rendered
    /// message for diagnostics.
    #[error("Index error: {0}")]
    Index(String),
}

/// Result type for storage operations.
pub type StorageResult<T> = Result<T, StorageError>;

/// Global RYO storage manager.
///
/// Provides high-level API for:
/// - Dumping session logs
/// - Loading past sessions
/// - Querying session history
/// - Managing imported projects
/// - Managing storage lifecycle
#[derive(Debug)]
pub struct RyoStorage {
    /// Root directory (~/.ryo/)
    root: PathBuf,
    /// Sessions directory (~/.ryo/sessions/)
    sessions_dir: PathBuf,
    /// Projects directory (~/.ryo/projects/)
    projects_dir: PathBuf,
    /// Session index file path (~/.ryo/index.json)
    index_path: PathBuf,
    /// Project index file path (~/.ryo/projects.json)
    project_index_path: PathBuf,
    /// Serialization format
    format: Format,
    /// Cached session index (lazy-loaded)
    index: Option<SessionIndex>,
    /// Cached project index (lazy-loaded)
    project_index: Option<ProjectIndex>,
}

impl RyoStorage {
    /// Default RYO directory name.
    pub const DIR_NAME: &'static str = ".ryo";
    /// Sessions subdirectory name.
    pub const SESSIONS_DIR: &'static str = "sessions";
    /// Projects subdirectory name.
    pub const PROJECTS_DIR: &'static str = "projects";
    /// Session index file name.
    pub const INDEX_FILE: &'static str = "index.json";
    /// Project index file name.
    pub const PROJECT_INDEX_FILE: &'static str = "projects.json";

    /// Create a new storage manager at the default location (~/.ryo/).
    pub fn global() -> StorageResult<Self> {
        let home = dirs::home_dir().ok_or_else(|| {
            StorageError::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "Could not find home directory",
            ))
        })?;
        Self::new(home.join(Self::DIR_NAME))
    }

    /// Create a new storage manager at a custom location.
    pub fn new(root: PathBuf) -> StorageResult<Self> {
        let sessions_dir = root.join(Self::SESSIONS_DIR);
        let projects_dir = root.join(Self::PROJECTS_DIR);
        let index_path = root.join(Self::INDEX_FILE);
        let project_index_path = root.join(Self::PROJECT_INDEX_FILE);

        Ok(Self {
            root,
            sessions_dir,
            projects_dir,
            index_path,
            project_index_path,
            format: Format::default(),
            index: None,
            project_index: None,
        })
    }

    /// Set the serialization format.
    pub fn with_format(mut self, format: Format) -> Self {
        self.format = format;
        self
    }

    /// Get the root directory path.
    pub fn root(&self) -> &Path {
        &self.root
    }

    /// Check if storage is initialized (directories exist).
    pub fn is_initialized(&self) -> bool {
        self.root.exists() && self.sessions_dir.exists()
    }

    /// Initialize storage directories.
    pub fn init(&self) -> StorageResult<()> {
        fs::create_dir_all(&self.sessions_dir)?;
        fs::create_dir_all(&self.projects_dir)?;

        // Create empty session index if it doesn't exist
        if !self.index_path.exists() {
            let index = SessionIndex::new();
            let json = serde_json::to_string_pretty(&index)
                .map_err(|e| StorageError::Format(FormatError::Json(e)))?;
            fs::write(&self.index_path, json)?;
        }

        // Create empty project index if it doesn't exist
        if !self.project_index_path.exists() {
            let index = ProjectIndex::new();
            let json = serde_json::to_string_pretty(&index)
                .map_err(|e| StorageError::Format(FormatError::Json(e)))?;
            fs::write(&self.project_index_path, json)?;
        }

        Ok(())
    }

    /// Ensure storage is initialized, creating directories if needed.
    pub fn ensure_init(&self) -> StorageResult<()> {
        if !self.is_initialized() {
            self.init()?;
        }
        Ok(())
    }

    // ========================================================================
    // Session Dump/Load
    // ========================================================================

    /// Dump a session log to storage.
    ///
    /// Returns the session ID.
    pub fn dump(&mut self, log: &TxLog) -> StorageResult<String> {
        self.ensure_init()?;

        let session_id = log.session_id.clone();
        let filename = self.session_filename(&session_id);
        let path = self.sessions_dir.join(&filename);

        // Serialize and write
        let serializer = get_serializer(self.format);
        let file = File::create(&path)?;
        serializer.serialize_to_file(log, file)?;

        // Update index
        self.add_to_index(log)?;

        Ok(session_id)
    }

    /// Load a session log by ID.
    ///
    /// This method automatically detects the file format by trying all known formats.
    pub fn load(&self, session_id: &str) -> StorageResult<TxLog> {
        // Try the configured format first, then fall back to other formats
        let formats_to_try = [self.format, Format::Json, Format::JsonCompact];

        for format in formats_to_try {
            let filename = format!("{}.txlog.{}", session_id, format.extension());
            let path = self.sessions_dir.join(&filename);

            if path.exists() {
                let file = File::open(&path)?;
                let reader = BufReader::new(file);
                let serializer = get_serializer(format);
                let log = serializer.deserialize_from_reader(reader)?;
                return Ok(log);
            }
        }

        Err(StorageError::SessionNotFound(session_id.to_string()))
    }

    /// Check if a session exists (in any format).
    pub fn exists(&self, session_id: &str) -> bool {
        self.find_session_path(session_id).is_some()
    }

    /// Delete a session (in any format).
    pub fn delete(&mut self, session_id: &str) -> StorageResult<()> {
        if let Some(path) = self.find_session_path(session_id) {
            fs::remove_file(&path)?;
        }

        // Update index
        self.remove_from_index(session_id)?;

        Ok(())
    }

    /// Find the path to a session file, trying all known formats.
    fn find_session_path(&self, session_id: &str) -> Option<PathBuf> {
        for format in [Format::Json, Format::JsonCompact] {
            let filename = format!("{}.txlog.{}", session_id, format.extension());
            let path = self.sessions_dir.join(&filename);
            if path.exists() {
                return Some(path);
            }
        }
        None
    }

    /// Generate filename for a session.
    fn session_filename(&self, session_id: &str) -> String {
        format!("{}.txlog.{}", session_id, self.format.extension())
    }

    // ========================================================================
    // Index Management
    // ========================================================================

    /// Get the session index.
    pub fn index(&mut self) -> StorageResult<&SessionIndex> {
        if self.index.is_none() {
            self.load_index()?;
        }
        Ok(self
            .index
            .as_ref()
            .expect("load_index() above sets self.index to Some"))
    }

    /// Get mutable access to the session index.
    fn index_mut(&mut self) -> StorageResult<&mut SessionIndex> {
        if self.index.is_none() {
            self.load_index()?;
        }
        Ok(self
            .index
            .as_mut()
            .expect("load_index() above sets self.index to Some"))
    }

    /// Load the index from disk.
    fn load_index(&mut self) -> StorageResult<()> {
        if self.index_path.exists() {
            let content = fs::read_to_string(&self.index_path)?;
            let index: SessionIndex = serde_json::from_str(&content)
                .map_err(|e| StorageError::Format(FormatError::Json(e)))?;
            self.index = Some(index);
        } else {
            self.index = Some(SessionIndex::new());
        }
        Ok(())
    }

    /// Save the index to disk.
    fn save_index(&self) -> StorageResult<()> {
        if let Some(ref index) = self.index {
            let json = serde_json::to_string_pretty(index)
                .map_err(|e| StorageError::Format(FormatError::Json(e)))?;
            fs::write(&self.index_path, json)?;
        }
        Ok(())
    }

    /// Add a session to the index.
    fn add_to_index(&mut self, log: &TxLog) -> StorageResult<()> {
        let meta = SessionMeta::from_log(log);
        self.index_mut()?.add(meta);
        self.save_index()?;
        Ok(())
    }

    /// Remove a session from the index.
    fn remove_from_index(&mut self, session_id: &str) -> StorageResult<()> {
        self.index_mut()?.remove(session_id);
        self.save_index()?;
        Ok(())
    }

    // ========================================================================
    // Query API
    // ========================================================================

    /// List all sessions.
    pub fn list_sessions(&mut self) -> StorageResult<Vec<&SessionMeta>> {
        Ok(self.index()?.list())
    }

    /// Find sessions by project path.
    pub fn sessions_for_project(
        &mut self,
        project_path: &Path,
    ) -> StorageResult<Vec<&SessionMeta>> {
        Ok(self.index()?.by_project(project_path))
    }

    /// Get the most recent session.
    pub fn latest_session(&mut self) -> StorageResult<Option<&SessionMeta>> {
        Ok(self.index()?.latest())
    }

    /// Get the most recent session for a project.
    pub fn latest_for_project(
        &mut self,
        project_path: &Path,
    ) -> StorageResult<Option<&SessionMeta>> {
        Ok(self.index()?.latest_for_project(project_path))
    }

    // ========================================================================
    // Maintenance
    // ========================================================================

    /// Clean up old sessions (keep last N per project).
    pub fn cleanup(&mut self, keep_per_project: usize) -> StorageResult<usize> {
        let to_delete = self.index_mut()?.cleanup(keep_per_project);
        let count = to_delete.len();

        for session_id in to_delete {
            let filename = self.session_filename(&session_id);
            let path = self.sessions_dir.join(&filename);
            if path.exists() {
                fs::remove_file(&path)?;
            }
        }

        self.save_index()?;
        Ok(count)
    }

    /// Get total storage size in bytes.
    pub fn storage_size(&self) -> StorageResult<u64> {
        let mut total = 0u64;

        if self.sessions_dir.exists() {
            for entry in fs::read_dir(&self.sessions_dir)? {
                let entry = entry?;
                if entry.file_type()?.is_file() {
                    total += entry.metadata()?.len();
                }
            }
        }

        if self.index_path.exists() {
            total += fs::metadata(&self.index_path)?.len();
        }

        Ok(total)
    }

    // ========================================================================
    // Project Management
    // ========================================================================

    /// Get the project index.
    pub fn project_index(&mut self) -> StorageResult<&ProjectIndex> {
        if self.project_index.is_none() {
            self.load_project_index()?;
        }
        Ok(self
            .project_index
            .as_ref()
            .expect("load_project_index() above sets self.project_index to Some"))
    }

    /// Get mutable access to the project index.
    fn project_index_mut(&mut self) -> StorageResult<&mut ProjectIndex> {
        if self.project_index.is_none() {
            self.load_project_index()?;
        }
        Ok(self
            .project_index
            .as_mut()
            .expect("load_project_index() above sets self.project_index to Some"))
    }

    /// Load the project index from disk.
    fn load_project_index(&mut self) -> StorageResult<()> {
        if self.project_index_path.exists() {
            let content = fs::read_to_string(&self.project_index_path)?;
            let index: ProjectIndex = serde_json::from_str(&content)
                .map_err(|e| StorageError::Format(FormatError::Json(e)))?;
            self.project_index = Some(index);
        } else {
            self.project_index = Some(ProjectIndex::new());
        }
        Ok(())
    }

    /// Save the project index to disk.
    fn save_project_index(&self) -> StorageResult<()> {
        if let Some(ref index) = self.project_index {
            let json = serde_json::to_string_pretty(index)
                .map_err(|e| StorageError::Format(FormatError::Json(e)))?;
            fs::write(&self.project_index_path, json)?;
        }
        Ok(())
    }

    /// Register a project in the index.
    pub fn register_project(&mut self, meta: ProjectMeta) -> StorageResult<String> {
        self.ensure_init()?;
        let project_id = meta.project_id.clone();
        self.project_index_mut()?.add(meta);
        self.save_project_index()?;
        Ok(project_id)
    }

    /// Unregister a project from the index.
    pub fn unregister_project(&mut self, project_id: &str) -> StorageResult<Option<ProjectMeta>> {
        let meta = self.project_index_mut()?.remove(project_id);
        self.save_project_index()?;
        Ok(meta)
    }

    /// Get a project by ID.
    pub fn get_project(&mut self, project_id: &str) -> StorageResult<Option<&ProjectMeta>> {
        Ok(self.project_index()?.get(project_id))
    }

    /// Get a project by path.
    pub fn get_project_by_path(&mut self, path: &Path) -> StorageResult<Option<&ProjectMeta>> {
        Ok(self.project_index()?.get_by_path(path))
    }

    /// Get a mutable project by path.
    pub fn get_project_by_path_mut(
        &mut self,
        path: &Path,
    ) -> StorageResult<Option<&mut ProjectMeta>> {
        // First find the project_id
        let project_id = self
            .project_index()?
            .get_by_path(path)
            .map(|p| p.project_id.clone());

        // Then get mutable reference
        if let Some(id) = project_id {
            Ok(self.project_index_mut()?.get_mut(&id))
        } else {
            Ok(None)
        }
    }

    /// Save the project index (public for external updates).
    pub fn save_projects(&self) -> StorageResult<()> {
        self.save_project_index()
    }

    /// Search projects by name pattern.
    pub fn search_projects_by_name(&mut self, pattern: &str) -> StorageResult<Vec<&ProjectMeta>> {
        Ok(self.project_index()?.search_by_name(pattern))
    }

    /// Check if a project path is already registered.
    pub fn is_project_registered(&mut self, path: &Path) -> StorageResult<bool> {
        Ok(self.project_index()?.contains_path(path))
    }

    /// List all registered projects.
    pub fn list_projects(&mut self) -> StorageResult<Vec<&ProjectMeta>> {
        Ok(self.project_index()?.list())
    }

    /// Touch a project (update last accessed time).
    pub fn touch_project(&mut self, project_id: &str) -> StorageResult<()> {
        if let Some(meta) = self.project_index_mut()?.get_mut(project_id) {
            meta.touch();
            self.save_project_index()?;
        }
        Ok(())
    }

    /// Get project statistics.
    pub fn project_stats(&mut self) -> StorageResult<(usize, usize, usize)> {
        let index = self.project_index()?;
        Ok((index.count(), index.total_files(), index.total_lines()))
    }

    /// Cleanup dead server PIDs from all projects.
    ///
    /// Checks each project's server_pid and clears it if the process is no longer running.
    /// Returns the number of projects that were cleaned up.
    pub fn cleanup_dead_servers(&mut self) -> StorageResult<usize> {
        let cleaned = self.project_index_mut()?.cleanup_dead_servers();
        if cleaned > 0 {
            self.save_project_index()?;
        }
        Ok(cleaned)
    }

    /// List all registered projects, with optional dead server cleanup.
    ///
    /// If `cleanup` is true, dead server PIDs are automatically cleared before listing.
    pub fn list_projects_with_cleanup(
        &mut self,
        cleanup: bool,
    ) -> StorageResult<Vec<&ProjectMeta>> {
        if cleanup {
            self.cleanup_dead_servers()?;
        }
        Ok(self.project_index()?.list())
    }

    // ========================================================================
    // CodeGraph Cache
    // ========================================================================

    /// Cache subdirectory name.
    pub const CACHE_DIR: &'static str = "cache";

    /// Get the cache directory path.
    pub fn cache_dir(&self) -> PathBuf {
        self.root.join(Self::CACHE_DIR)
    }

    /// Ensure cache directory exists.
    fn ensure_cache_dir(&self) -> StorageResult<()> {
        let dir = self.cache_dir();
        if !dir.exists() {
            fs::create_dir_all(&dir)?;
        }
        Ok(())
    }

    /// Save a CodeGraph cache.
    ///
    /// Returns the cache file path.
    pub fn save_graph_cache(&self, project_hash: &str, data: &[u8]) -> StorageResult<PathBuf> {
        self.ensure_cache_dir()?;
        let path = self.cache_dir().join(format!("{}.graph.bin", project_hash));
        fs::write(&path, data)?;
        Ok(path)
    }

    /// Load a CodeGraph cache.
    ///
    /// Returns None if cache doesn't exist.
    pub fn load_graph_cache(&self, project_hash: &str) -> StorageResult<Option<Vec<u8>>> {
        let path = self.cache_dir().join(format!("{}.graph.bin", project_hash));
        if !path.exists() {
            return Ok(None);
        }
        let data = fs::read(&path)?;
        Ok(Some(data))
    }

    /// Delete a CodeGraph cache.
    pub fn delete_graph_cache(&self, project_hash: &str) -> StorageResult<()> {
        let path = self.cache_dir().join(format!("{}.graph.bin", project_hash));
        if path.exists() {
            fs::remove_file(&path)?;
        }
        Ok(())
    }

    /// List all cached project hashes.
    pub fn list_graph_caches(&self) -> StorageResult<Vec<String>> {
        let dir = self.cache_dir();
        if !dir.exists() {
            return Ok(Vec::new());
        }

        let mut hashes = Vec::new();
        for entry in fs::read_dir(&dir)? {
            let entry = entry?;
            let path = entry.path();
            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                if name.ends_with(".graph.bin") {
                    let hash = name.trim_end_matches(".graph.bin").to_string();
                    hashes.push(hash);
                }
            }
        }
        Ok(hashes)
    }

    /// Get cache storage size in bytes.
    pub fn cache_size(&self) -> StorageResult<u64> {
        let dir = self.cache_dir();
        if !dir.exists() {
            return Ok(0);
        }

        let mut size = 0u64;
        for entry in fs::read_dir(&dir)? {
            let entry = entry?;
            if let Ok(meta) = entry.metadata() {
                size += meta.len();
            }
        }
        Ok(size)
    }

    /// Clear all graph caches.
    pub fn clear_graph_caches(&self) -> StorageResult<usize> {
        let dir = self.cache_dir();
        if !dir.exists() {
            return Ok(0);
        }

        let mut count = 0;
        for entry in fs::read_dir(&dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().map(|e| e == "bin").unwrap_or(false) {
                fs::remove_file(&path)?;
                count += 1;
            }
        }
        Ok(count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::txlog::TxAction;
    use tempfile::TempDir;

    fn create_test_log(project: &str) -> TxLog {
        let mut log = TxLog::with_project(project);
        log.log(TxAction::GoalSet {
            query: "test".to_string(),
            intent_type: "test".to_string(),
            confidence: 0.9,
        });
        log
    }

    #[test]
    fn test_init_and_dump() {
        let temp = TempDir::new().unwrap();
        let mut storage = RyoStorage::new(temp.path().join(".ryo")).unwrap();

        storage.init().unwrap();
        assert!(storage.is_initialized());

        let log = create_test_log("/test/project");
        let session_id = storage.dump(&log).unwrap();

        assert!(storage.exists(&session_id));
    }

    #[test]
    fn test_load_session() {
        let temp = TempDir::new().unwrap();
        let mut storage = RyoStorage::new(temp.path().join(".ryo")).unwrap();
        storage.init().unwrap();

        let log = create_test_log("/test/project");
        let session_id = storage.dump(&log).unwrap();

        let loaded = storage.load(&session_id).unwrap();
        assert_eq!(loaded.session_id, log.session_id);
        assert_eq!(loaded.entries().len(), log.entries().len());
    }

    #[test]
    fn test_session_index() {
        let temp = TempDir::new().unwrap();
        let mut storage = RyoStorage::new(temp.path().join(".ryo")).unwrap();
        storage.init().unwrap();

        // Dump multiple sessions
        let log1 = create_test_log("/project/a");
        let log2 = create_test_log("/project/b");
        let log3 = create_test_log("/project/a");

        storage.dump(&log1).unwrap();
        storage.dump(&log2).unwrap();
        storage.dump(&log3).unwrap();

        // List all
        let all = storage.list_sessions().unwrap();
        assert_eq!(all.len(), 3);

        // Filter by project
        let proj_a = storage
            .sessions_for_project(Path::new("/project/a"))
            .unwrap();
        assert_eq!(proj_a.len(), 2);
    }

    #[test]
    fn test_delete_session() {
        let temp = TempDir::new().unwrap();
        let mut storage = RyoStorage::new(temp.path().join(".ryo")).unwrap();
        storage.init().unwrap();

        let log = create_test_log("/test/project");
        let session_id = storage.dump(&log).unwrap();

        assert!(storage.exists(&session_id));
        storage.delete(&session_id).unwrap();
        assert!(!storage.exists(&session_id));
    }
}