cortex-mem-tools 2.5.0

High-level tools and utilities for Cortex Memory Integration
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
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
//! Cortex-Mem 核心功能测试
//!
//! 测试分类:
//! 1. 单元测试 (unit_*) - 不依赖外部服务,使用 Mock
//! 2. 集成测试 (integration_*) - 需要外部服务 (Qdrant, LLM, Embedding)
//!
//! 运行方式:
//! - 单元测试: `cargo test`
//! - 集成测试: `cargo test -- --ignored` (需要配置外部服务)

#![cfg(test)]

use std::collections::HashMap;
use std::sync::Arc;
use tempfile::TempDir;
use tokio::sync::RwLock;

// ==================== Mock 实现 ====================

mod mock {
    use async_trait::async_trait;
    use cortex_mem_core::llm::LLMClient;
    use cortex_mem_core::llm::{LLMConfig, MemoryExtractionResponse};
    use cortex_mem_core::llm::extractor_types::{StructuredFactExtraction, DetailedFactExtraction};
    use cortex_mem_core::Result;

    /// Mock LLM Client - 返回预定义的响应
    pub struct MockLLMClient {
        config: LLMConfig,
    }

    impl MockLLMClient {
        pub fn new() -> Self {
            Self {
                config: LLMConfig::default(),
            }
        }
    }

    impl Default for MockLLMClient {
        fn default() -> Self {
            Self::new()
        }
    }

    #[async_trait]
    impl LLMClient for MockLLMClient {
        async fn complete(&self, _prompt: &str) -> Result<String> {
            Ok("Mock LLM response".to_string())
        }

        async fn complete_with_system(&self, _system: &str, _prompt: &str) -> Result<String> {
            Ok("Mock LLM response with system prompt".to_string())
        }

        async fn extract_memories(&self, _prompt: &str) -> Result<MemoryExtractionResponse> {
            Ok(MemoryExtractionResponse {
                facts: vec![],
                decisions: vec![],
                entities: vec![],
            })
        }

        async fn extract_structured_facts(&self, _prompt: &str) -> Result<StructuredFactExtraction> {
            Ok(StructuredFactExtraction { facts: vec![] })
        }

        async fn extract_detailed_facts(&self, _prompt: &str) -> Result<DetailedFactExtraction> {
            Ok(DetailedFactExtraction { facts: vec![] })
        }

        fn model_name(&self) -> &str {
            "mock-model"
        }

        fn config(&self) -> &LLMConfig {
            &self.config
        }
    }
}

// ==================== 测试辅助函数 ====================

mod test_utils {
    use super::*;
    use cortex_mem_core::{
        CortexFilesystem, FilesystemOperations, SessionConfig, SessionManager,
        layers::manager::LayerManager,
    };

    /// 测试用的上下文封装
    /// 
    /// 由于 MemoryOperations 的字段是 pub(crate),测试无法直接构造。
    /// 这个结构体封装了测试所需的核心功能。
    #[allow(dead_code)]
    pub struct TestContext {
        pub filesystem: Arc<CortexFilesystem>,
        pub session_manager: Arc<RwLock<SessionManager>>,
        pub layer_manager: Arc<LayerManager>,
        pub temp_dir: TempDir,
    }

    impl TestContext {
        /// 创建新的测试上下文
        pub async fn new() -> Self {
            let temp_dir = TempDir::new().unwrap();
            let data_dir = temp_dir.path().to_str().unwrap();
            
            let filesystem = Arc::new(CortexFilesystem::new(data_dir));
            filesystem.initialize().await.unwrap();

            let config = SessionConfig::default();
            let session_manager = SessionManager::new(filesystem.clone(), config);
            let session_manager = Arc::new(RwLock::new(session_manager));

            let llm_client = Arc::new(mock::MockLLMClient::new());
            let layer_manager = Arc::new(LayerManager::new(filesystem.clone(), llm_client));

            Self {
                filesystem,
                session_manager,
                layer_manager,
                temp_dir,
            }
        }

        /// 创建带有租户隔离的测试上下文
        pub async fn with_tenant(tenant_id: &str) -> Self {
            let temp_dir = TempDir::new().unwrap();
            let data_dir = temp_dir.path().to_str().unwrap();
            
            let filesystem = Arc::new(CortexFilesystem::with_tenant(data_dir, tenant_id));
            filesystem.initialize().await.unwrap();

            let config = SessionConfig::default();
            let session_manager = SessionManager::new(filesystem.clone(), config);
            let session_manager = Arc::new(RwLock::new(session_manager));

            let llm_client = Arc::new(mock::MockLLMClient::new());
            let layer_manager = Arc::new(LayerManager::new(filesystem.clone(), llm_client));

            Self {
                filesystem,
                session_manager,
                layer_manager,
                temp_dir,
            }
        }

        /// 添加消息到会话
        pub async fn add_message(&self, thread_id: &str, role: &str, content: &str) -> String {
            let thread_id = if thread_id.is_empty() { "default" } else { thread_id };

            {
                let sm = self.session_manager.read().await;
                if sm.session_exists(thread_id).await.unwrap() {
                    // Session exists, proceed to add message
                } else {
                    drop(sm);
                    let sm = self.session_manager.write().await;
                    sm.create_session_with_ids(thread_id, None, None).await.unwrap();
                }
            }

            let sm = self.session_manager.read().await;
            let message = cortex_mem_core::Message::new(
                match role {
                    "user" => cortex_mem_core::MessageRole::User,
                    "assistant" => cortex_mem_core::MessageRole::Assistant,
                    "system" => cortex_mem_core::MessageRole::System,
                    _ => cortex_mem_core::MessageRole::User,
                },
                content,
            );

            sm.message_storage().save_message(thread_id, &message).await.unwrap()
        }

        /// 列出会话
        pub async fn list_sessions(&self) -> Vec<SessionInfo> {
            let entries = self.filesystem.list("cortex://session").await.unwrap();
            let mut session_infos = Vec::new();
            
            for entry in entries {
                if entry.is_directory {
                    let thread_id = entry.name.clone();
                    if let Ok(metadata) = self.session_manager.read().await.load_session(&thread_id).await {
                        let status_str = match metadata.status {
                            cortex_mem_core::session::manager::SessionStatus::Active => "active",
                            cortex_mem_core::session::manager::SessionStatus::Closed => "closed",
                            cortex_mem_core::session::manager::SessionStatus::Archived => "archived",
                        };

                        session_infos.push(SessionInfo {
                            thread_id: metadata.thread_id,
                            status: status_str.to_string(),
                            message_count: 0,
                            created_at: metadata.created_at,
                            updated_at: metadata.updated_at,
                        });
                    }
                }
            }

            session_infos
        }

        /// 获取会话信息
        pub async fn get_session(&self, thread_id: &str) -> Result<SessionInfo, String> {
            let sm = self.session_manager.read().await;
            let metadata = sm.load_session(thread_id).await.map_err(|e| e.to_string())?;

            let status_str = match metadata.status {
                cortex_mem_core::session::manager::SessionStatus::Active => "active",
                cortex_mem_core::session::manager::SessionStatus::Closed => "closed",
                cortex_mem_core::session::manager::SessionStatus::Archived => "archived",
            };

            Ok(SessionInfo {
                thread_id: metadata.thread_id,
                status: status_str.to_string(),
                message_count: 0,
                created_at: metadata.created_at,
                updated_at: metadata.updated_at,
            })
        }

        /// 关闭会话
        pub async fn close_session(&self, thread_id: &str) -> Result<(), String> {
            let mut sm = self.session_manager.write().await;
            sm.close_session(thread_id).await.map_err(|e| e.to_string())?;
            Ok(())
        }

        /// 存储内容
        pub async fn store(&self, args: StoreArgs) -> StoreResponse {
            let scope = match args.scope.as_str() {
                "user" | "session" | "agent" => args.scope.as_str(),
                _ => "session",
            };

            let uri = match scope {
                "user" => {
                    let user_id = args.user_id.as_deref().unwrap_or("default");
                    let now = chrono::Utc::now();
                    let year_month = now.format("%Y-%m").to_string();
                    let day = now.format("%d").to_string();
                    let filename = format!(
                        "{}_{}.md",
                        now.format("%H_%M_%S"),
                        uuid::Uuid::new_v4().to_string().split('-').next().unwrap_or("unknown")
                    );
                    format!("cortex://user/{}/memories/{}/{}/{}", user_id, year_month, day, filename)
                },
                "agent" => {
                    let agent_id = args.agent_id.as_deref()
                        .or_else(|| if args.thread_id.is_empty() { None } else { Some(&args.thread_id) })
                        .unwrap_or("default");
                    let now = chrono::Utc::now();
                    let year_month = now.format("%Y-%m").to_string();
                    let day = now.format("%d").to_string();
                    let filename = format!(
                        "{}_{}.md",
                        now.format("%H_%M_%S"),
                        uuid::Uuid::new_v4().to_string().split('-').next().unwrap_or("unknown")
                    );
                    format!("cortex://agent/{}/memories/{}/{}/{}", agent_id, year_month, day, filename)
                },
                "session" => {
                    self.add_message(
                        if args.thread_id.is_empty() { "default" } else { &args.thread_id },
                        "user",
                        &args.content
                    ).await
                },
                _ => unreachable!(),
            };

            if scope == "user" || scope == "agent" {
                self.filesystem.write(&uri, &args.content).await.unwrap();
            }

            if args.auto_generate_layers.unwrap_or(true) {
                let _ = self.layer_manager.generate_all_layers(&uri, &args.content).await;
            }

            StoreResponse {
                uri,
                layers_generated: HashMap::new(),
                success: true,
            }
        }

        /// 获取 L0 abstract
        pub async fn get_abstract(&self, uri: &str) -> Result<AbstractResponse, String> {
            let text: String = self.layer_manager
                .load(uri, cortex_mem_core::ContextLayer::L0Abstract)
                .await
                .map_err(|e: cortex_mem_core::Error| e.to_string())?;
            Ok(AbstractResponse {
                uri: uri.to_string(),
                abstract_text: text.clone(),
                layer: "L0".to_string(),
                token_count: text.split_whitespace().count(),
            })
        }

        /// 获取 L1 overview
        pub async fn get_overview(&self, uri: &str) -> Result<OverviewResponse, String> {
            let text: String = self.layer_manager
                .load(uri, cortex_mem_core::ContextLayer::L1Overview)
                .await
                .map_err(|e: cortex_mem_core::Error| e.to_string())?;
            Ok(OverviewResponse {
                uri: uri.to_string(),
                overview_text: text.clone(),
                layer: "L1".to_string(),
                token_count: text.split_whitespace().count(),
            })
        }

        /// 获取 L2 完整内容
        pub async fn get_read(&self, uri: &str) -> Result<ReadResponse, String> {
            let content = self.filesystem.read(uri).await.map_err(|e| e.to_string())?;
            Ok(ReadResponse {
                uri: uri.to_string(),
                content: content.clone(),
                layer: "L2".to_string(),
                token_count: content.split_whitespace().count(),
                metadata: None,
            })
        }

        /// 列出目录
        pub async fn list(&self, uri: &str) -> Vec<String> {
            self.filesystem.list(uri).await
                .map(|entries| entries.into_iter().map(|e| e.uri).collect())
                .unwrap_or_default()
        }

        /// 读取文件
        pub async fn read(&self, uri: &str) -> Result<String, String> {
            self.filesystem.read(uri).await.map_err(|e| e.to_string())
        }

        /// 删除文件
        pub async fn delete(&self, uri: &str) -> Result<(), String> {
            self.filesystem.delete(uri).await.map_err(|e| e.to_string())
        }

        /// 检查文件是否存在
        pub async fn exists(&self, uri: &str) -> bool {
            self.filesystem.exists(uri).await.unwrap_or(false)
        }

        /// 写入文件
        pub async fn write(&self, uri: &str, content: &str) -> Result<(), String> {
            self.filesystem.write(uri, content).await.map_err(|e| e.to_string())
        }
    }

    // 类型定义
    #[allow(dead_code)]
    #[derive(Debug, Clone)]
    pub struct SessionInfo {
        pub thread_id: String,
        pub status: String,
        pub message_count: usize,
        pub created_at: chrono::DateTime<chrono::Utc>,
        pub updated_at: chrono::DateTime<chrono::Utc>,
    }

    #[allow(dead_code)]
    #[derive(Debug, Clone)]
    pub struct StoreArgs {
        pub content: String,
        pub thread_id: String,
        pub metadata: Option<serde_json::Value>,
        pub auto_generate_layers: Option<bool>,
        pub scope: String,
        pub user_id: Option<String>,
        pub agent_id: Option<String>,
    }

    #[allow(dead_code)]
    #[derive(Debug, Clone)]
    pub struct StoreResponse {
        pub uri: String,
        pub layers_generated: HashMap<String, String>,
        pub success: bool,
    }

    #[allow(dead_code)]
    #[derive(Debug, Clone)]
    pub struct AbstractResponse {
        pub uri: String,
        pub abstract_text: String,
        pub layer: String,
        pub token_count: usize,
    }

    #[allow(dead_code)]
    #[derive(Debug, Clone)]
    pub struct OverviewResponse {
        pub uri: String,
        pub overview_text: String,
        pub layer: String,
        pub token_count: usize,
    }

    #[allow(dead_code)]
    #[derive(Debug, Clone)]
    pub struct ReadResponse {
        pub uri: String,
        pub content: String,
        pub layer: String,
        pub token_count: usize,
        pub metadata: Option<FileMetadata>,
    }

    #[allow(dead_code)]
    #[derive(Debug, Clone)]
    pub struct FileMetadata {
        pub created_at: chrono::DateTime<chrono::Utc>,
        pub updated_at: chrono::DateTime<chrono::Utc>,
    }
}

// ==================== 单元测试: 文件系统基础操作 ====================

mod unit_filesystem_tests {
    use super::*;

    /// 测试基本的文件写入和读取
    #[tokio::test]
    async fn test_basic_write_and_read() {
        let ctx = test_utils::TestContext::new().await;

        let content = "Hello, Cortex Memory!";
        let uri = "cortex://resources/test.md";
        ctx.write(uri, content).await.unwrap();

        let read_content = ctx.read(uri).await.unwrap();
        assert_eq!(read_content, content);
    }

    /// 测试文件存在性检查
    #[tokio::test]
    async fn test_file_exists() {
        let ctx = test_utils::TestContext::new().await;

        assert!(!ctx.exists("cortex://resources/nonexistent.md").await);

        ctx.write("cortex://resources/test.md", "content").await.unwrap();
        assert!(ctx.exists("cortex://resources/test.md").await);
    }

    /// 测试文件删除
    #[tokio::test]
    async fn test_file_delete() {
        let ctx = test_utils::TestContext::new().await;

        let uri = "cortex://resources/to_delete.md";
        
        ctx.write(uri, "content").await.unwrap();
        assert!(ctx.exists(uri).await);

        ctx.delete(uri).await.unwrap();
        assert!(!ctx.exists(uri).await);

        let result = ctx.delete(uri).await;
        assert!(result.is_err());
    }

    /// 测试目录列表
    #[tokio::test]
    async fn test_list_directory() {
        let ctx = test_utils::TestContext::new().await;

        ctx.write("cortex://resources/file1.md", "content1").await.unwrap();
        ctx.write("cortex://resources/file2.md", "content2").await.unwrap();
        ctx.write("cortex://resources/subdir/file3.md", "content3").await.unwrap();

        let entries = ctx.list("cortex://resources").await;
        
        assert!(entries.len() >= 2);
        
        let names: Vec<&str> = entries.iter().map(|e| e.rsplit('/').next().unwrap()).collect();
        assert!(names.contains(&"file1.md"));
        assert!(names.contains(&"file2.md"));
    }

    /// 测试嵌套目录创建
    #[tokio::test]
    async fn test_nested_directory_creation() {
        let ctx = test_utils::TestContext::new().await;

        let uri = "cortex://resources/level1/level2/level3/deep.md";
        ctx.write(uri, "deep content").await.unwrap();

        let content = ctx.read(uri).await.unwrap();
        assert_eq!(content, "deep content");
    }

    /// 测试空内容存储
    #[tokio::test]
    async fn test_empty_content() {
        let ctx = test_utils::TestContext::new().await;

        let uri = "cortex://resources/empty.md";
        ctx.write(uri, "").await.unwrap();

        let content = ctx.read(uri).await.unwrap();
        assert!(content.is_empty());
    }

    /// 测试读取不存在的文件
    #[tokio::test]
    async fn test_read_nonexistent_file() {
        let ctx = test_utils::TestContext::new().await;

        let result = ctx.read("cortex://resources/nonexistent.md").await;
        assert!(result.is_err());
    }
}

// ==================== 单元测试: 会话管理 ====================

mod unit_session_tests {
    use super::*;

    /// 测试添加消息到会话
    #[tokio::test]
    async fn test_add_message() {
        let ctx = test_utils::TestContext::new().await;

        let thread_id = "test_thread";
        let msg_id = ctx.add_message(thread_id, "user", "Hello, world!").await;

        assert!(!msg_id.is_empty());

        let sessions = ctx.list_sessions().await;
        assert_eq!(sessions.len(), 1);
        assert_eq!(sessions[0].thread_id, thread_id);
    }

    /// 测试空 thread_id 使用默认值
    #[tokio::test]
    async fn test_empty_thread_id_defaults() {
        let ctx = test_utils::TestContext::new().await;

        let msg_id = ctx.add_message("", "user", "test message").await;
        assert!(!msg_id.is_empty());

        let session = ctx.get_session("default").await.unwrap();
        assert_eq!(session.thread_id, "default");
        assert_eq!(session.status, "active");
    }

    /// 测试多角色消息
    #[tokio::test]
    async fn test_multiple_roles() {
        let ctx = test_utils::TestContext::new().await;

        let thread_id = "multi_role_thread";

        ctx.add_message(thread_id, "user", "User message").await;
        ctx.add_message(thread_id, "assistant", "Assistant response").await;
        ctx.add_message(thread_id, "system", "System instruction").await;

        let session = ctx.get_session(thread_id).await.unwrap();
        assert_eq!(session.thread_id, thread_id);
    }

    /// 测试会话关闭
    #[tokio::test]
    async fn test_session_close() {
        let ctx = test_utils::TestContext::new().await;

        let thread_id = "session_to_close";
        ctx.add_message(thread_id, "user", "message").await;

        ctx.close_session(thread_id).await.unwrap();

        let session = ctx.get_session(thread_id).await.unwrap();
        assert_eq!(session.status, "closed");
    }

    /// 测试多个会话
    #[tokio::test]
    async fn test_multiple_sessions() {
        let ctx = test_utils::TestContext::new().await;

        ctx.add_message("thread1", "user", "message 1").await;
        ctx.add_message("thread2", "user", "message 2").await;
        ctx.add_message("thread3", "user", "message 3").await;

        let sessions = ctx.list_sessions().await;
        assert_eq!(sessions.len(), 3);

        for session in &sessions {
            assert_eq!(session.status, "active");
        }
    }

    /// 测试获取不存在的会话
    #[tokio::test]
    async fn test_get_nonexistent_session() {
        let ctx = test_utils::TestContext::new().await;

        let result = ctx.get_session("nonexistent_session").await;
        assert!(result.is_err());
    }
}

// ==================== 单元测试: 存储操作 ====================

mod unit_storage_tests {
    use super::*;

    /// 测试 session scope 存储
    #[tokio::test]
    async fn test_store_session_scope() {
        let ctx = test_utils::TestContext::new().await;

        let args = test_utils::StoreArgs {
            content: "Session content".to_string(),
            thread_id: "test_session".to_string(),
            metadata: None,
            auto_generate_layers: Some(true),
            scope: "session".to_string(),
            user_id: None,
            agent_id: None,
        };

        let result = ctx.store(args).await;
        assert!(result.success);
        assert!(result.uri.starts_with("cortex://session/test_session/timeline"));
        assert!(result.uri.ends_with(".md"));

        let content = ctx.read(&result.uri).await.unwrap();
        assert!(content.contains("Session content"));
    }

    /// 测试 user scope 存储
    #[tokio::test]
    async fn test_store_user_scope() {
        let ctx = test_utils::TestContext::new().await;

        let args = test_utils::StoreArgs {
            content: "User preference content".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(true),
            scope: "user".to_string(),
            user_id: Some("user_123".to_string()),
            agent_id: None,
        };

        let result = ctx.store(args).await;
        assert!(result.success);
        assert!(result.uri.starts_with("cortex://user/user_123/memories"));
        assert!(result.uri.ends_with(".md"));
    }

    /// 测试 agent scope 存储
    #[tokio::test]
    async fn test_store_agent_scope() {
        let ctx = test_utils::TestContext::new().await;

        let args = test_utils::StoreArgs {
            content: "Agent case content".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(true),
            scope: "agent".to_string(),
            user_id: None,
            agent_id: Some("agent_456".to_string()),
        };

        let result = ctx.store(args).await;
        assert!(result.success);
        assert!(result.uri.starts_with("cortex://agent/agent_456/memories"));
        assert!(result.uri.ends_with(".md"));
    }

    /// 测试自动生成层
    #[tokio::test]
    async fn test_auto_generate_layers() {
        let ctx = test_utils::TestContext::new().await;

        let content = r#"# Test Document

This is a test document with some content.

## Section 1
Content for section 1.
"#;

        let args = test_utils::StoreArgs {
            content: content.to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(true),
            scope: "user".to_string(),
            user_id: Some("layer_test_user".to_string()),
            agent_id: None,
        };

        let result = ctx.store(args).await;
        assert!(result.success);

        // 验证 L2 可读取
        let l2 = ctx.get_read(&result.uri).await.unwrap();
        assert!(l2.content.contains("Test Document"));
        assert_eq!(l2.layer, "L2");

        // 验证 L0 摘要可获取
        let l0 = ctx.get_abstract(&result.uri).await.unwrap();
        assert!(!l0.abstract_text.is_empty());
        assert_eq!(l0.layer, "L0");

        // 验证 L1 概览可获取
        let l1 = ctx.get_overview(&result.uri).await.unwrap();
        assert!(!l1.overview_text.is_empty());
        assert_eq!(l1.layer, "L1");
    }

    /// 测试存储带元数据
    #[tokio::test]
    async fn test_store_with_metadata() {
        let ctx = test_utils::TestContext::new().await;

        let metadata = serde_json::json!({
            "importance": "high",
            "tags": ["rust", "testing"],
        });

        let args = test_utils::StoreArgs {
            content: "Content with metadata".to_string(),
            thread_id: "".to_string(),
            metadata: Some(metadata),
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("metadata_user".to_string()),
            agent_id: None,
        };

        let result = ctx.store(args).await;
        assert!(result.success);
    }
}

// ==================== 单元测试: 多租户隔离 ====================

mod unit_tenant_isolation_tests {
    use super::*;

    /// 测试租户数据隔离
    #[tokio::test]
    async fn test_tenant_data_isolation() {
        let ctx_a = test_utils::TestContext::with_tenant("tenant_a").await;
        let ctx_b = test_utils::TestContext::with_tenant("tenant_b").await;

        // 租户 A 存储数据
        let args_a = test_utils::StoreArgs {
            content: "Tenant A private data".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("shared_user".to_string()),
            agent_id: None,
        };
        let result_a = ctx_a.store(args_a).await;

        // 租户 B 存储数据
        let args_b = test_utils::StoreArgs {
            content: "Tenant B private data".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("shared_user".to_string()),
            agent_id: None,
        };
        let result_b = ctx_b.store(args_b).await;

        // 验证 URI 不同
        assert_ne!(result_a.uri, result_b.uri);

        // 验证租户 A 能读取自己的数据
        let content_a = ctx_a.read(&result_a.uri).await.unwrap();
        assert!(content_a.contains("Tenant A"));

        // 验证租户 B 能读取自己的数据
        let content_b = ctx_b.read(&result_b.uri).await.unwrap();
        assert!(content_b.contains("Tenant B"));

        // 验证租户 A 不能读取租户 B 的数据
        let read_result = ctx_a.read(&result_b.uri).await;
        assert!(read_result.is_err());
    }

    /// 测试会话隔离
    #[tokio::test]
    async fn test_session_isolation() {
        let ctx_a = test_utils::TestContext::with_tenant("tenant_a").await;
        let ctx_b = test_utils::TestContext::with_tenant("tenant_b").await;

        ctx_a.add_message("shared_thread_id", "user", "Tenant A message").await;
        ctx_b.add_message("shared_thread_id", "user", "Tenant B message").await;

        let sessions_a = ctx_a.list_sessions().await;
        let sessions_b = ctx_b.list_sessions().await;

        assert_eq!(sessions_a.len(), 1);
        assert_eq!(sessions_b.len(), 1);
    }
}

// ==================== 单元测试: Scope 隔离 ====================

mod unit_scope_isolation_tests {
    use super::*;

    /// 测试不同 scope 的存储路径
    #[tokio::test]
    async fn test_scope_path_isolation() {
        let ctx = test_utils::TestContext::new().await;

        // Session scope
        let session_args = test_utils::StoreArgs {
            content: "Session data".to_string(),
            thread_id: "my_thread".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "session".to_string(),
            user_id: None,
            agent_id: None,
        };
        let session_result = ctx.store(session_args).await;
        assert!(session_result.uri.starts_with("cortex://session/my_thread"));

        // User scope
        let user_args = test_utils::StoreArgs {
            content: "User data".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("user_001".to_string()),
            agent_id: None,
        };
        let user_result = ctx.store(user_args).await;
        assert!(user_result.uri.starts_with("cortex://user/user_001"));

        // Agent scope
        let agent_args = test_utils::StoreArgs {
            content: "Agent data".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "agent".to_string(),
            user_id: None,
            agent_id: Some("agent_001".to_string()),
        };
        let agent_result = ctx.store(agent_args).await;
        assert!(agent_result.uri.starts_with("cortex://agent/agent_001"));

        // 验证所有 URI 都不同
        assert_ne!(session_result.uri, user_result.uri);
        assert_ne!(user_result.uri, agent_result.uri);
        assert_ne!(session_result.uri, agent_result.uri);
    }

    /// 测试不同 user_id 之间的隔离
    #[tokio::test]
    async fn test_user_id_isolation() {
        let ctx = test_utils::TestContext::new().await;

        let args_a = test_utils::StoreArgs {
            content: "User A data".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("user_a".to_string()),
            agent_id: None,
        };
        let result_a = ctx.store(args_a).await;

        let args_b = test_utils::StoreArgs {
            content: "User B data".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("user_b".to_string()),
            agent_id: None,
        };
        let result_b = ctx.store(args_b).await;

        assert_ne!(result_a.uri, result_b.uri);
        assert!(result_a.uri.contains("user_a"));
        assert!(result_b.uri.contains("user_b"));
    }
}

// ==================== 单元测试: 边界情况 ====================

mod unit_edge_case_tests {
    use super::*;

    /// 测试特殊字符内容
    #[tokio::test]
    async fn test_special_characters() {
        let ctx = test_utils::TestContext::new().await;

        let special_contents = vec![
            ("中文内容", "Chinese characters"),
            ("Emoji 🎉🚀💡", "Emojis"),
            ("Tabs\tand\tspaces", "Tabs"),
            ("Newlines\nLine1\nLine2", "Newlines"),
            ("Quotes: \"double\" 'single'", "Quotes"),
            ("HTML <tag> & entities", "HTML"),
            ("Code: `fn main() {}`", "Code"),
        ];

        for (content, desc) in special_contents {
            let args = test_utils::StoreArgs {
                content: content.to_string(),
                thread_id: "".to_string(),
                metadata: None,
                auto_generate_layers: Some(false),
                scope: "user".to_string(),
                user_id: Some("special_char_test".to_string()),
                agent_id: None,
            };

            let result = ctx.store(args).await;
            assert!(result.success, "Failed for: {}", desc);

            let read_content = ctx.read(&result.uri).await.unwrap();
            assert!(read_content.contains(content), "Content mismatch for: {}", desc);
        }
    }

    /// 测试大内容存储
    #[tokio::test]
    async fn test_large_content() {
        let ctx = test_utils::TestContext::new().await;

        let large_content = "X".repeat(50 * 1024);

        let args = test_utils::StoreArgs {
            content: large_content.clone(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("large_content_user".to_string()),
            agent_id: None,
        };

        let result = ctx.store(args).await;
        assert!(result.success);

        let read_content = ctx.read(&result.uri).await.unwrap();
        assert!(read_content.len() >= large_content.len() - 10);
    }

    /// 测试特殊 thread_id
    #[tokio::test]
    async fn test_special_thread_ids() {
        let ctx = test_utils::TestContext::new().await;

        let special_ids = vec![
            "thread-with-dash",
            "thread_with_underscore",
            "thread.with.dot",
            "thread123",
            "123thread",
        ];

        for thread_id in special_ids {
            let result = ctx.add_message(thread_id, "user", "test message").await;
            assert!(!result.is_empty(), "Failed for thread_id: {}", thread_id);
        }
    }

    /// 测试无效的 scope
    #[tokio::test]
    async fn test_invalid_scope() {
        let ctx = test_utils::TestContext::new().await;

        let args = test_utils::StoreArgs {
            content: "test".to_string(),
            thread_id: "test_thread".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "invalid_scope".to_string(),
            user_id: None,
            agent_id: None,
        };

        let result = ctx.store(args).await;
        assert!(result.uri.starts_with("cortex://session"));
    }
}

// ==================== 单元测试: 并发操作 ====================

mod unit_concurrent_tests {
    use super::*;

    /// 测试并发写入
    #[tokio::test]
    async fn test_concurrent_writes() {
        let ctx = Arc::new(test_utils::TestContext::new().await);

        let mut handles = vec![];

        for i in 0..20 {
            let ctx_clone = ctx.clone();
            let handle = tokio::spawn(async move {
                ctx_clone.add_message("concurrent_test", "user", &format!("Message {}", i)).await
            });
            handles.push(handle);
        }

        let results: Vec<_> = futures::future::join_all(handles).await;
        let success_count = results.iter().filter(|r| !r.as_ref().unwrap().is_empty()).count();

        assert_eq!(success_count, 20, "All concurrent writes should succeed");
    }

    /// 测试并发读取
    #[tokio::test]
    async fn test_concurrent_reads() {
        let ctx = Arc::new(test_utils::TestContext::new().await);

        let args = test_utils::StoreArgs {
            content: "Shared content for concurrent reads".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("concurrent_read_user".to_string()),
            agent_id: None,
        };
        let result = ctx.store(args).await;
        let uri = Arc::new(result.uri);

        let mut handles = vec![];

        for _ in 0..50 {
            let ctx_clone = ctx.clone();
            let uri_clone = uri.clone();
            let handle = tokio::spawn(async move {
                ctx_clone.read(&uri_clone).await
            });
            handles.push(handle);
        }

        let results: Vec<_> = futures::future::join_all(handles).await;
        let success_count = results.iter().filter(|r| r.is_ok() && r.as_ref().unwrap().is_ok()).count();

        assert_eq!(success_count, 50, "All concurrent reads should succeed");
    }

    /// 测试并发读写
    #[tokio::test]
    async fn test_concurrent_read_write() {
        let ctx = Arc::new(test_utils::TestContext::new().await);

        for i in 0..5 {
            ctx.add_message("rw_test", "user", &format!("Initial {}", i)).await;
        }

        let mut handles: Vec<tokio::task::JoinHandle<Result<(), String>>> = vec![];

        for i in 0..20 {
            let ctx_clone = ctx.clone();
            let handle = tokio::spawn(async move {
                if i % 2 == 0 {
                    ctx_clone.add_message("rw_test", "user", &format!("Concurrent {}", i)).await;
                    Ok(())
                } else {
                    ctx_clone.list_sessions().await;
                    Ok(())
                }
            });
            handles.push(handle);
        }

        let results: Vec<_> = futures::future::join_all(handles).await;
        let success_count = results.iter().filter(|r| r.is_ok()).count();

        assert_eq!(success_count, 20, "All concurrent operations should succeed");
    }
}

// ==================== 单元测试: 分层访问 ====================

mod unit_layer_access_tests {
    use super::*;

    /// 测试 L0 abstract 获取
    #[tokio::test]
    async fn test_get_abstract() {
        let ctx = test_utils::TestContext::new().await;

        let args = test_utils::StoreArgs {
            content: "Content for abstract testing. This should be summarized.".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(true),
            scope: "user".to_string(),
            user_id: Some("abstract_test_user".to_string()),
            agent_id: None,
        };

        let result = ctx.store(args).await;
        
        let abstract_result = ctx.get_abstract(&result.uri).await.unwrap();
        assert_eq!(abstract_result.layer, "L0");
        assert!(!abstract_result.abstract_text.is_empty());
    }

    /// 测试 L1 overview 获取
    #[tokio::test]
    async fn test_get_overview() {
        let ctx = test_utils::TestContext::new().await;

        let args = test_utils::StoreArgs {
            content: "Content for overview testing. This should be expanded into an overview.".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(true),
            scope: "user".to_string(),
            user_id: Some("overview_test_user".to_string()),
            agent_id: None,
        };

        let result = ctx.store(args).await;
        
        let overview_result = ctx.get_overview(&result.uri).await.unwrap();
        assert_eq!(overview_result.layer, "L1");
        assert!(!overview_result.overview_text.is_empty());
    }

    /// 测试 L2 完整内容获取
    #[tokio::test]
    async fn test_get_read() {
        let ctx = test_utils::TestContext::new().await;

        let original_content = "Original content for L2 read test.";

        let args = test_utils::StoreArgs {
            content: original_content.to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("read_test_user".to_string()),
            agent_id: None,
        };

        let result = ctx.store(args).await;
        
        let read_result = ctx.get_read(&result.uri).await.unwrap();
        assert_eq!(read_result.layer, "L2");
        assert!(read_result.content.contains(original_content));
    }
}

// ==================== 集成测试 (需要外部服务) ====================

mod integration_tests {
    //! 集成测试 - 需要 Qdrant, LLM, Embedding 服务
    //!
    //! 运行方式:
    //! 1. 启动 Qdrant: docker run -p 6334:6334 qdrant/qdrant
    //! 2. 配置环境变量: LLM_API_BASE_URL, LLM_API_KEY, EMBEDDING_API_BASE_URL, EMBEDDING_API_KEY
    //! 3. 运行: cargo test -- --ignored integration

    /// 测试向量搜索 (需要 Qdrant 和 Embedding 服务)
    #[tokio::test]
    #[ignore]
    async fn integration_test_vector_search() {
        println!("Integration test: vector_search - requires Qdrant and Embedding service");
    }

    /// 测试 LLM 记忆提取 (需要 LLM 服务)
    #[tokio::test]
    #[ignore]
    async fn integration_test_llm_extraction() {
        println!("Integration test: llm_extraction - requires LLM service");
    }

    /// 测试完整的存储和检索流程 (需要全部外部服务)
    #[tokio::test]
    #[ignore]
    async fn integration_test_full_workflow() {
        println!("Integration test: full_workflow - requires all external services");
    }
}

// ==================== 性能测试 ====================

mod performance_tests {
    use super::*;
    use std::time::Instant;

    /// 测试存储性能
    #[tokio::test]
    async fn test_storage_performance() {
        let ctx = test_utils::TestContext::new().await;

        let start = Instant::now();

        for i in 0..100 {
            ctx.add_message("perf_test", "user", &format!("Performance test message {}", i)).await;
        }

        let duration = start.elapsed();
        println!("Storage of 100 messages took: {:?}", duration);

        assert!(duration.as_secs() < 10, "Storage took too long: {:?}", duration);
    }

    /// 测试读取性能
    #[tokio::test]
    async fn test_read_performance() {
        let ctx = test_utils::TestContext::new().await;

        let args = test_utils::StoreArgs {
            content: "Performance test content for reading.".to_string(),
            thread_id: "".to_string(),
            metadata: None,
            auto_generate_layers: Some(false),
            scope: "user".to_string(),
            user_id: Some("read_perf_user".to_string()),
            agent_id: None,
        };
        let result = ctx.store(args).await;

        let start = Instant::now();

        for _ in 0..100 {
            ctx.read(&result.uri).await.unwrap();
        }

        let duration = start.elapsed();
        println!("100 reads took: {:?}", duration);

        assert!(duration.as_secs() < 10, "Reads took too long: {:?}", duration);
    }

    /// 测试列表性能
    #[tokio::test]
    async fn test_list_performance() {
        let ctx = test_utils::TestContext::new().await;

        for i in 0..50 {
            ctx.add_message(&format!("list_perf_{}", i), "user", "message").await;
        }

        let start = Instant::now();

        for _ in 0..100 {
            ctx.list_sessions().await;
        }

        let duration = start.elapsed();
        println!("100 list operations took: {:?}", duration);

        assert!(duration.as_secs() < 10, "List operations took too long: {:?}", duration);
    }
}