chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
//! Extensive tests for storage operations
//!
//! This file contains comprehensive unit tests for:
//! - SQLite database operations
//! - Chat session index read/write
//! - Session registration
//! - VS Code process detection
//! - Backup functionality

#[allow(unused_imports)]
use chasm::models::ChatSession;
use chasm::models::{ChatSessionIndex, ChatSessionIndexEntry};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

// ============================================================================
// Test Database Setup Helpers
// ============================================================================

/// Create a test SQLite database with VS Code schema
fn create_test_database(path: &std::path::Path) -> rusqlite::Result<()> {
    let conn = rusqlite::Connection::open(path)?;
    conn.execute(
        "CREATE TABLE IF NOT EXISTS ItemTable (key TEXT PRIMARY KEY, value TEXT)",
        [],
    )?;
    Ok(())
}

/// Insert a key-value pair into the test database
fn insert_into_db(path: &std::path::Path, key: &str, value: &str) -> rusqlite::Result<()> {
    let conn = rusqlite::Connection::open(path)?;
    conn.execute(
        "INSERT OR REPLACE INTO ItemTable (key, value) VALUES (?, ?)",
        [key, value],
    )?;
    Ok(())
}

/// Read a value from the test database
fn read_from_db(path: &std::path::Path, key: &str) -> rusqlite::Result<Option<String>> {
    let conn = rusqlite::Connection::open(path)?;
    let result: rusqlite::Result<String> =
        conn.query_row("SELECT value FROM ItemTable WHERE key = ?", [key], |row| {
            row.get(0)
        });

    match result {
        Ok(value) => Ok(Some(value)),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
        Err(e) => Err(e),
    }
}

// ============================================================================
// Chat Session Index Read Tests
// ============================================================================

mod read_chat_session_index_tests {
    use super::*;
    use chasm::storage::read_chat_session_index;

    #[test]
    fn test_read_empty_index() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        let result = read_chat_session_index(&db_path);
        assert!(result.is_ok());

        let index = result.unwrap();
        assert_eq!(index.version, 1); // Default version
        assert!(index.entries.is_empty());
    }

    #[test]
    fn test_read_populated_index() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        let index_json = r#"{
            "version": 1,
            "entries": {
                "session-123": {
                    "sessionId": "session-123",
                    "title": "Test Session",
                    "lastMessageDate": 1700000000000,
                    "isImported": false,
                    "initialLocation": "panel",
                    "isEmpty": false
                }
            }
        }"#;

        insert_into_db(&db_path, "chat.ChatSessionStore.index", index_json).unwrap();

        let result = read_chat_session_index(&db_path);
        assert!(result.is_ok());

        let index = result.unwrap();
        assert_eq!(index.entries.len(), 1);
        assert!(index.entries.contains_key("session-123"));
    }

    #[test]
    fn test_read_multiple_entries() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        let mut entries = HashMap::new();
        for i in 0..10 {
            entries.insert(
                format!("session-{}", i),
                ChatSessionIndexEntry {
                    session_id: format!("session-{}", i),
                    title: format!("Session {}", i),
                    last_message_date: 1700000000000 + i,
                    timing: None,
                    last_response_state: 1,
                    initial_location: "panel".to_string(),
                    is_empty: false,
                is_imported: None,
                has_pending_edits: None,
                is_external: None,
                },
            );
        }

        let index = ChatSessionIndex {
            version: 1,
            entries,
        };
        let json = serde_json::to_string(&index).unwrap();

        insert_into_db(&db_path, "chat.ChatSessionStore.index", &json).unwrap();

        let result = read_chat_session_index(&db_path);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().entries.len(), 10);
    }

    #[test]
    fn test_read_nonexistent_database() {
        let path = PathBuf::from("/nonexistent/path/state.vscdb");
        let result = read_chat_session_index(&path);
        assert!(result.is_err());
    }

    #[test]
    fn test_read_invalid_json() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        insert_into_db(&db_path, "chat.ChatSessionStore.index", "invalid json").unwrap();

        let result = read_chat_session_index(&db_path);
        assert!(result.is_err());
    }
}

// ============================================================================
// Chat Session Index Write Tests
// ============================================================================

mod write_chat_session_index_tests {
    use super::*;
    use chasm::storage::{read_chat_session_index, write_chat_session_index};

    #[test]
    fn test_write_empty_index() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        let index = ChatSessionIndex::default();
        let result = write_chat_session_index(&db_path, &index);
        assert!(result.is_ok());

        // Verify it was written
        let stored = read_from_db(&db_path, "chat.ChatSessionStore.index").unwrap();
        assert!(stored.is_some());
    }

    #[test]
    fn test_write_populated_index() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        let mut entries = HashMap::new();
        entries.insert(
            "test-session".to_string(),
            ChatSessionIndexEntry {
                session_id: "test-session".to_string(),
                title: "Test".to_string(),
                last_message_date: 1700000000000,
                timing: None,
                last_response_state: 1,
                initial_location: "editor".to_string(),
                is_empty: false,
                is_imported: None,
                has_pending_edits: None,
                is_external: None,
            },
        );

        let index = ChatSessionIndex {
            version: 1,
            entries,
        };
        write_chat_session_index(&db_path, &index).unwrap();

        // Read back and verify
        let read_index = read_chat_session_index(&db_path).unwrap();
        assert_eq!(read_index.entries.len(), 1);
        assert!(read_index.entries.contains_key("test-session"));
    }

    #[test]
    fn test_write_overwrites_existing() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        // Write initial index
        let mut entries1 = HashMap::new();
        entries1.insert(
            "session-1".to_string(),
            ChatSessionIndexEntry {
                session_id: "session-1".to_string(),
                title: "First".to_string(),
                last_message_date: 1700000000000,
                timing: None,
                last_response_state: 1,
                initial_location: "panel".to_string(),
                is_empty: false,
                is_imported: None,
                has_pending_edits: None,
                is_external: None,
            },
        );

        write_chat_session_index(
            &db_path,
            &ChatSessionIndex {
                version: 1,
                entries: entries1,
            },
        )
        .unwrap();

        // Write new index
        let mut entries2 = HashMap::new();
        entries2.insert(
            "session-2".to_string(),
            ChatSessionIndexEntry {
                session_id: "session-2".to_string(),
                title: "Second".to_string(),
                last_message_date: 1700000001000,
                timing: None,
                last_response_state: 1,
                initial_location: "terminal".to_string(),
                is_empty: false,
                is_imported: None,
                has_pending_edits: None,
                is_external: None,
            },
        );

        write_chat_session_index(
            &db_path,
            &ChatSessionIndex {
                version: 1,
                entries: entries2,
            },
        )
        .unwrap();

        // Should only have the second entry
        let read_index = read_chat_session_index(&db_path).unwrap();
        assert_eq!(read_index.entries.len(), 1);
        assert!(read_index.entries.contains_key("session-2"));
        assert!(!read_index.entries.contains_key("session-1"));
    }

    #[test]
    fn test_write_roundtrip() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        let mut entries = HashMap::new();
        for i in 0..5 {
            entries.insert(
                format!("sess-{}", i),
                ChatSessionIndexEntry {
                    session_id: format!("sess-{}", i),
                    title: format!("Title {}", i),
                    last_message_date: 1700000000000 + i * 1000,
                    timing: None,
                    last_response_state: 1,
                    initial_location: ["panel", "editor", "terminal", "notebook", "inline"]
                        [i as usize % 5]
                        .to_string(),
                    is_empty: i == 0,
                is_imported: None,
                has_pending_edits: None,
                is_external: None,
                },
            );
        }

        let original = ChatSessionIndex {
            version: 1,
            entries,
        };
        write_chat_session_index(&db_path, &original).unwrap();

        let restored = read_chat_session_index(&db_path).unwrap();
        assert_eq!(restored.version, original.version);
        assert_eq!(restored.entries.len(), original.entries.len());

        for (key, entry) in &restored.entries {
            let orig_entry = original.entries.get(key).unwrap();
            assert_eq!(entry.title, orig_entry.title);
            assert_eq!(entry.is_empty, orig_entry.is_empty);
        }
    }
}

// ============================================================================
// Add Session to Index Tests
// ============================================================================

mod add_session_to_index_tests {
    use super::*;
    use chasm::storage::{add_session_to_index, read_chat_session_index};

    #[test]
    fn test_add_session_to_empty_index() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        let result = add_session_to_index(
            &db_path,
            "new-session-123",
            "New Session",
            1700000000000,
            false,
            "panel",
            false,
        );
        assert!(result.is_ok());

        let index = read_chat_session_index(&db_path).unwrap();
        assert_eq!(index.entries.len(), 1);
        assert!(index.entries.contains_key("new-session-123"));

        let entry = index.entries.get("new-session-123").unwrap();
        assert_eq!(entry.title, "New Session");
        assert!(!entry.is_empty);
    }

    #[test]
    fn test_add_session_to_existing_index() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        // Add first session
        add_session_to_index(
            &db_path,
            "session-1",
            "First",
            1700000000000,
            false,
            "panel",
            false,
        )
        .unwrap();

        // Add second session
        add_session_to_index(
            &db_path,
            "session-2",
            "Second",
            1700000001000,
            true,
            "editor",
            false,
        )
        .unwrap();

        let index = read_chat_session_index(&db_path).unwrap();
        assert_eq!(index.entries.len(), 2);
        assert!(index.entries.contains_key("session-1"));
        assert!(index.entries.contains_key("session-2"));
    }

    #[test]
    fn test_add_session_updates_existing() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        // Add session with initial title
        add_session_to_index(
            &db_path,
            "session-1",
            "Original Title",
            1700000000000,
            false,
            "panel",
            false,
        )
        .unwrap();

        // Update same session with new title
        add_session_to_index(
            &db_path,
            "session-1",
            "Updated Title",
            1700000001000,
            true,
            "terminal",
            true,
        )
        .unwrap();

        let index = read_chat_session_index(&db_path).unwrap();
        assert_eq!(index.entries.len(), 1);

        let entry = index.entries.get("session-1").unwrap();
        assert_eq!(entry.title, "Updated Title");
        assert!(entry.is_empty);
    }

    #[test]
    fn test_add_imported_session() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        add_session_to_index(
            &db_path,
            "imported-123",
            "Imported Session",
            1700000000000,
            true,
            "imported",
            false,
        )
        .unwrap();

        let index = read_chat_session_index(&db_path).unwrap();
        let entry = index.entries.get("imported-123").unwrap();
        assert_eq!(entry.initial_location, "imported");
    }

    #[test]
    fn test_add_empty_session() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        add_session_to_index(
            &db_path,
            "empty-session",
            "Empty",
            1700000000000,
            false,
            "panel",
            true,
        )
        .unwrap();

        let index = read_chat_session_index(&db_path).unwrap();
        let entry = index.entries.get("empty-session").unwrap();
        assert!(entry.is_empty);
    }

    #[test]
    fn test_add_many_sessions() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");
        create_test_database(&db_path).unwrap();

        for i in 0..100 {
            add_session_to_index(
                &db_path,
                &format!("session-{:03}", i),
                &format!("Session {}", i),
                1700000000000 + i * 1000,
                i % 5 == 0,
                "panel",
                i % 10 == 0,
            )
            .unwrap();
        }

        let index = read_chat_session_index(&db_path).unwrap();
        assert_eq!(index.entries.len(), 100);
    }
}

// ============================================================================
// VS Code Running Detection Tests
// ============================================================================

mod vscode_running_tests {
    use chasm::storage::is_vscode_running;

    #[test]
    fn test_is_vscode_running_returns_bool() {
        // This test just verifies the function runs without panicking
        let _result = is_vscode_running();
        // Function always returns a bool, which is what we want
    }

    #[test]
    fn test_is_vscode_running_multiple_calls() {
        // Should be consistent
        let result1 = is_vscode_running();
        let result2 = is_vscode_running();
        // Results might differ if VS Code is launched/closed between calls,
        // but the function should not panic
        let _ = result1;
        let _ = result2;
    }
}

// ============================================================================
// Backup Workspace Sessions Tests
// ============================================================================

mod backup_workspace_sessions_tests {
    use super::*;
    use chasm::storage::backup_workspace_sessions;

    #[test]
    fn test_backup_nonexistent_workspace() {
        let temp_dir = TempDir::new().unwrap();
        let result = backup_workspace_sessions(temp_dir.path());
        assert!(result.is_ok());
        assert!(result.unwrap().is_none()); // No chatSessions dir
    }

    #[test]
    fn test_backup_empty_chat_sessions() {
        let temp_dir = TempDir::new().unwrap();
        let chat_sessions = temp_dir.path().join("chatSessions");
        fs::create_dir(&chat_sessions).unwrap();

        let result = backup_workspace_sessions(temp_dir.path());
        assert!(result.is_ok());

        if let Some(backup_path) = result.unwrap() {
            assert!(backup_path.exists());
            assert!(backup_path
                .to_string_lossy()
                .contains("chatSessions-backup"));
        }
    }

    #[test]
    fn test_backup_with_sessions() {
        let temp_dir = TempDir::new().unwrap();
        let chat_sessions = temp_dir.path().join("chatSessions");
        fs::create_dir(&chat_sessions).unwrap();

        // Create some session files
        for i in 0..5 {
            let session_json = format!(
                r#"{{
                "version": 3,
                "sessionId": "session-{}",
                "creationDate": {},
                "lastMessageDate": {},
                "requests": []
            }}"#,
                i,
                1700000000000i64 + i as i64 * 1000,
                1700000000000i64 + i as i64 * 1000
            );

            fs::write(
                chat_sessions.join(format!("session-{}.json", i)),
                session_json,
            )
            .unwrap();
        }

        let result = backup_workspace_sessions(temp_dir.path());
        assert!(result.is_ok());

        let backup_path = result.unwrap().unwrap();
        assert!(backup_path.exists());

        // Verify all files were copied
        let backup_entries: Vec<_> = fs::read_dir(&backup_path)
            .unwrap()
            .filter_map(|e| e.ok())
            .collect();
        assert_eq!(backup_entries.len(), 5);
    }

    #[test]
    fn test_backup_with_subdirectories() {
        let temp_dir = TempDir::new().unwrap();
        let chat_sessions = temp_dir.path().join("chatSessions");
        fs::create_dir(&chat_sessions).unwrap();

        // Create a subdirectory
        let subdir = chat_sessions.join("subdir");
        fs::create_dir(&subdir).unwrap();
        fs::write(subdir.join("file.txt"), "content").unwrap();

        let result = backup_workspace_sessions(temp_dir.path());
        assert!(result.is_ok());

        let backup_path = result.unwrap().unwrap();
        assert!(backup_path.join("subdir").exists());
        assert!(backup_path.join("subdir").join("file.txt").exists());
    }

    #[test]
    fn test_backup_preserves_content() {
        let temp_dir = TempDir::new().unwrap();
        let chat_sessions = temp_dir.path().join("chatSessions");
        fs::create_dir(&chat_sessions).unwrap();

        let original_content = r#"{"version": 3, "sessionId": "test", "requests": []}"#;
        fs::write(chat_sessions.join("test.json"), original_content).unwrap();

        let result = backup_workspace_sessions(temp_dir.path());
        let backup_path = result.unwrap().unwrap();

        let backed_up_content = fs::read_to_string(backup_path.join("test.json")).unwrap();
        assert_eq!(original_content, backed_up_content);
    }

    #[test]
    fn test_multiple_backups() {
        let temp_dir = TempDir::new().unwrap();
        let chat_sessions = temp_dir.path().join("chatSessions");
        fs::create_dir(&chat_sessions).unwrap();
        fs::write(chat_sessions.join("session.json"), "{}").unwrap();

        // Create multiple backups
        let backup1 = backup_workspace_sessions(temp_dir.path()).unwrap().unwrap();
        std::thread::sleep(std::time::Duration::from_millis(1100)); // Wait for different timestamp
        let backup2 = backup_workspace_sessions(temp_dir.path()).unwrap().unwrap();

        // Both should exist and be different
        assert!(backup1.exists());
        assert!(backup2.exists());
        assert_ne!(backup1, backup2);
    }
}

// ============================================================================
// Get Workspace Storage DB Tests
// ============================================================================

mod get_workspace_storage_db_tests {
    use chasm::storage::get_workspace_storage_db;

    #[test]
    fn test_get_db_path() {
        let result = get_workspace_storage_db("test-workspace-hash");
        assert!(result.is_ok());

        let path = result.unwrap();
        assert!(path.to_string_lossy().contains("test-workspace-hash"));
        assert!(path.to_string_lossy().contains("state.vscdb"));
    }

    #[test]
    fn test_get_db_path_different_workspaces() {
        let path1 = get_workspace_storage_db("workspace-1").unwrap();
        let path2 = get_workspace_storage_db("workspace-2").unwrap();

        assert_ne!(path1, path2);
    }

    #[test]
    fn test_get_db_path_special_chars() {
        // Workspace hashes are usually alphanumeric, but test robustness
        let result = get_workspace_storage_db("test-with-special-chars");
        assert!(result.is_ok());
    }
}

// ============================================================================
// Register All Sessions Tests
// ============================================================================

mod register_all_sessions_tests {
    use super::*;
    use chasm::storage::register_all_sessions_from_directory;

    #[allow(dead_code)]
    fn create_test_session_file(dir: &std::path::Path, session_id: &str, title: &str) {
        let session_json = format!(
            r#"{{
            "version": 3,
            "sessionId": "{}",
            "creationDate": 1700000000000,
            "lastMessageDate": 1700000000000,
            "customTitle": "{}",
            "initialLocation": "panel",
            "requests": [
                {{
                    "timestamp": 1700000000000,
                    "message": {{"text": "Test message"}},
                    "response": {{"value": [{{"value": "Test response"}}]}}
                }}
            ]
        }}"#,
            session_id, title
        );

        fs::write(dir.join(format!("{}.json", session_id)), session_json).unwrap();
    }

    // Note: These tests would require mocking the VS Code storage path and database
    // For now, we test the basic error handling

    #[test]
    fn test_register_with_nonexistent_workspace() {
        let temp_dir = TempDir::new().unwrap();
        let chat_sessions = temp_dir.path().join("chatSessions");
        fs::create_dir(&chat_sessions).unwrap();

        // This should fail because the database doesn't exist
        let result = register_all_sessions_from_directory(
            "nonexistent-workspace-hash",
            &chat_sessions,
            true, // force
        );
        assert!(result.is_err());
    }
}

// ============================================================================
// Error Handling Tests
// ============================================================================

mod storage_error_tests {
    use super::*;
    #[allow(unused_imports)]
    use chasm::error::CsmError;
    use chasm::storage::read_chat_session_index;

    #[test]
    fn test_read_from_directory() {
        let temp_dir = TempDir::new().unwrap();
        // Trying to read a directory as a database should fail
        let result = read_chat_session_index(temp_dir.path());
        assert!(result.is_err());
    }

    #[test]
    fn test_read_from_empty_file() {
        let temp_dir = TempDir::new().unwrap();
        let empty_file = temp_dir.path().join("empty.vscdb");
        fs::write(&empty_file, "").unwrap();

        // Empty file is not a valid SQLite database
        let result = read_chat_session_index(&empty_file);
        assert!(result.is_err());
    }

    #[test]
    fn test_read_from_corrupted_db() {
        let temp_dir = TempDir::new().unwrap();
        let corrupt_file = temp_dir.path().join("corrupt.vscdb");
        fs::write(&corrupt_file, "not a sqlite database").unwrap();

        let result = read_chat_session_index(&corrupt_file);
        assert!(result.is_err());
    }
}

// ============================================================================
// Index Serialization Tests
// ============================================================================

mod index_serialization_tests {
    use super::*;

    #[test]
    fn test_index_json_structure() {
        let mut entries = HashMap::new();
        entries.insert(
            "test-id".to_string(),
            ChatSessionIndexEntry {
                session_id: "test-id".to_string(),
                title: "Test Title".to_string(),
                last_message_date: 1700000000000,
                timing: None,
                last_response_state: 1,
                initial_location: "terminal".to_string(),
                is_empty: false,
                is_imported: None,
                has_pending_edits: None,
                is_external: None,
            },
        );

        let index = ChatSessionIndex {
            version: 1,
            entries,
        };
        let json = serde_json::to_string_pretty(&index).unwrap();

        // Verify JSON structure
        assert!(json.contains("\"version\": 1"));
        assert!(json.contains("\"entries\""));
        assert!(json.contains("\"sessionId\""));
        assert!(json.contains("\"lastMessageDate\""));
        assert!(json.contains("\"isEmpty\": false"));
    }

    #[test]
    fn test_index_handles_special_characters_in_title() {
        let mut entries = HashMap::new();
        entries.insert(
            "special-chars".to_string(),
            ChatSessionIndexEntry {
                session_id: "special-chars".to_string(),
                title: "Title with \"quotes\" and \\backslashes\\".to_string(),
                last_message_date: 1700000000000,
                timing: None,
                last_response_state: 1,
                initial_location: "panel".to_string(),
                is_empty: false,
                is_imported: None,
                has_pending_edits: None,
                is_external: None,
            },
        );

        let index = ChatSessionIndex {
            version: 1,
            entries,
        };
        let json = serde_json::to_string(&index).unwrap();
        let restored: ChatSessionIndex = serde_json::from_str(&json).unwrap();

        let entry = restored.entries.get("special-chars").unwrap();
        assert!(entry.title.contains("quotes"));
        assert!(entry.title.contains("backslashes"));
    }

    #[test]
    fn test_index_handles_unicode_in_title() {
        let mut entries = HashMap::new();
        entries.insert(
            "unicode".to_string(),
            ChatSessionIndexEntry {
                session_id: "unicode".to_string(),
                title: "Test Title".to_string(),
                last_message_date: 1700000000000,
                timing: None,
                last_response_state: 1,
                initial_location: "panel".to_string(),
                is_empty: false,
                is_imported: None,
                has_pending_edits: None,
                is_external: None,
            },
        );

        let index = ChatSessionIndex {
            version: 1,
            entries,
        };
        let json = serde_json::to_string(&index).unwrap();
        let restored: ChatSessionIndex = serde_json::from_str(&json).unwrap();

        let entry = restored.entries.get("unicode").unwrap();
        assert!(entry.title.contains("Test"));
        assert!(entry.title.contains("Title"));
    }

    #[test]
    fn test_index_empty_title() {
        let mut entries = HashMap::new();
        entries.insert(
            "empty-title".to_string(),
            ChatSessionIndexEntry {
                session_id: "empty-title".to_string(),
                title: "".to_string(),
                last_message_date: 1700000000000,
                timing: None,
                last_response_state: 1,
                initial_location: "panel".to_string(),
                is_empty: true,
                is_imported: None,
                has_pending_edits: None,
                is_external: None,
            },
        );

        let index = ChatSessionIndex {
            version: 1,
            entries,
        };
        let json = serde_json::to_string(&index).unwrap();
        let restored: ChatSessionIndex = serde_json::from_str(&json).unwrap();

        let entry = restored.entries.get("empty-title").unwrap();
        assert!(entry.title.is_empty());
    }
}

// ============================================================================
// Empty Window Sessions (ALL SESSIONS) Tests
// ============================================================================

mod empty_window_sessions_tests {
    use chasm::models::ChatSession;
    use std::fs;
    use tempfile::TempDir;

    /// Helper to create a test chat session
    fn create_test_session(session_id: &str, title: &str) -> ChatSession {
        let json = format!(
            r#"{{
                "version": 3,
                "sessionId": "{}",
                "creationDate": 1700000000000,
                "lastMessageDate": 1700000000000,
                "customTitle": "{}",
                "initialLocation": "panel",
                "requests": [
                    {{
                        "timestamp": 1700000000000,
                        "message": {{"text": "Test message"}},
                        "response": {{"value": [{{"value": "Test response"}}]}}
                    }}
                ]
            }}"#,
            session_id, title
        );
        serde_json::from_str(&json).unwrap()
    }

    /// Helper to create a temp directory simulating emptyWindowChatSessions
    fn setup_test_sessions_dir() -> TempDir {
        TempDir::new().unwrap()
    }

    #[test]
    fn test_empty_window_session_json_parsing() {
        let session = create_test_session("test-session-123", "My Test Session");
        assert_eq!(session.session_id, Some("test-session-123".to_string()));
        assert_eq!(session.custom_title, Some("My Test Session".to_string()));
        assert_eq!(session.version, 3);
    }

    #[test]
    fn test_empty_window_session_file_read() {
        let temp_dir = setup_test_sessions_dir();
        let session_id = "abc123-def456";
        let session = create_test_session(session_id, "Test Title");

        // Write session to temp directory
        let session_path = temp_dir.path().join(format!("{}.json", session_id));
        let json = serde_json::to_string_pretty(&session).unwrap();
        fs::write(&session_path, &json).unwrap();

        // Read it back
        let content = fs::read_to_string(&session_path).unwrap();
        let restored: ChatSession = serde_json::from_str(&content).unwrap();

        assert_eq!(restored.session_id, Some(session_id.to_string()));
        assert_eq!(restored.custom_title, Some("Test Title".to_string()));
    }

    #[test]
    fn test_empty_window_sessions_directory_scan() {
        let temp_dir = setup_test_sessions_dir();

        // Create multiple session files
        for i in 1..=3 {
            let session_id = format!("session-{}", i);
            let session = create_test_session(&session_id, &format!("Session {}", i));
            let session_path = temp_dir.path().join(format!("{}.json", session_id));
            let json = serde_json::to_string_pretty(&session).unwrap();
            fs::write(&session_path, &json).unwrap();
        }

        // Count JSON files
        let count = fs::read_dir(temp_dir.path())
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().is_some_and(|ext| ext == "json"))
            .count();

        assert_eq!(count, 3);
    }

    #[test]
    fn test_empty_window_session_with_no_session_id() {
        // Some sessions might not have a session_id
        let json = r#"{
            "version": 3,
            "creationDate": 1700000000000,
            "lastMessageDate": 1700000000000,
            "customTitle": "No ID Session",
            "initialLocation": "panel",
            "requests": []
        }"#;

        let session: ChatSession = serde_json::from_str(json).unwrap();
        assert!(session.session_id.is_none());
        assert_eq!(session.custom_title, Some("No ID Session".to_string()));
    }

    #[test]
    fn test_empty_window_session_sorting_by_date() {
        let mut sessions = [
            {
                let mut s = create_test_session("old", "Old Session");
                s.last_message_date = 1000;
                s
            },
            {
                let mut s = create_test_session("new", "New Session");
                s.last_message_date = 3000;
                s
            },
            {
                let mut s = create_test_session("mid", "Mid Session");
                s.last_message_date = 2000;
                s
            },
        ];

        // Sort by last_message_date descending (most recent first)
        sessions.sort_by(|a, b| b.last_message_date.cmp(&a.last_message_date));

        assert_eq!(sessions[0].session_id, Some("new".to_string()));
        assert_eq!(sessions[1].session_id, Some("mid".to_string()));
        assert_eq!(sessions[2].session_id, Some("old".to_string()));
    }

    #[test]
    fn test_empty_window_session_request_count() {
        let session = create_test_session("test", "Test");
        assert_eq!(session.request_count(), 1);
    }

    #[test]
    fn test_empty_window_session_empty_requests() {
        let json = r#"{
            "version": 3,
            "sessionId": "empty-requests",
            "creationDate": 1700000000000,
            "lastMessageDate": 1700000000000,
            "initialLocation": "panel",
            "requests": []
        }"#;

        let session: ChatSession = serde_json::from_str(json).unwrap();
        assert_eq!(session.request_count(), 0);
        assert!(session.is_empty());
    }

    #[test]
    fn test_empty_window_session_title_extraction() {
        // Session with custom title
        let session = create_test_session("test", "My Custom Title");
        assert_eq!(session.title(), "My Custom Title");

        // Session without custom title (should fall back to first message or "Untitled")
        let json = r#"{
            "version": 3,
            "sessionId": "no-title",
            "creationDate": 1700000000000,
            "lastMessageDate": 1700000000000,
            "initialLocation": "panel",
            "requests": []
        }"#;
        let session: ChatSession = serde_json::from_str(json).unwrap();
        assert_eq!(session.title(), "Untitled");
    }

    #[test]
    fn test_empty_window_session_file_naming() {
        let session_id = "0a9b131f-2644-41df-abe0-34eb3dc658fe";
        let expected_filename = format!("{}.json", session_id);
        assert_eq!(
            expected_filename,
            "0a9b131f-2644-41df-abe0-34eb3dc658fe.json"
        );
    }

    #[test]
    fn test_empty_window_session_ignore_non_json_files() {
        let temp_dir = setup_test_sessions_dir();

        // Create a valid session file
        let session = create_test_session("valid", "Valid Session");
        let json = serde_json::to_string_pretty(&session).unwrap();
        fs::write(temp_dir.path().join("valid.json"), &json).unwrap();

        // Create some non-JSON files that should be ignored
        fs::write(temp_dir.path().join("readme.txt"), "ignore me").unwrap();
        fs::write(temp_dir.path().join(".hidden"), "hidden file").unwrap();
        fs::write(temp_dir.path().join("backup.json.bak"), "backup").unwrap();

        // Only count .json files
        let json_count = fs::read_dir(temp_dir.path())
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().is_some_and(|ext| ext == "json"))
            .count();

        assert_eq!(json_count, 1);
    }

    #[test]
    fn test_empty_window_session_invalid_json_handling() {
        let temp_dir = setup_test_sessions_dir();

        // Create a valid session
        let session = create_test_session("valid", "Valid");
        let json = serde_json::to_string_pretty(&session).unwrap();
        fs::write(temp_dir.path().join("valid.json"), &json).unwrap();

        // Create an invalid JSON file
        fs::write(temp_dir.path().join("invalid.json"), "{ not valid json }").unwrap();

        // Reading the valid one should work
        let valid_content = fs::read_to_string(temp_dir.path().join("valid.json")).unwrap();
        let valid_session: Result<ChatSession, _> = serde_json::from_str(&valid_content);
        assert!(valid_session.is_ok());

        // Reading the invalid one should fail
        let invalid_content = fs::read_to_string(temp_dir.path().join("invalid.json")).unwrap();
        let invalid_session: Result<ChatSession, _> = serde_json::from_str(&invalid_content);
        assert!(invalid_session.is_err());
    }
}