mirror-log 0.1.9

Append-only event log for personal knowledge management with semantic chunking using SQLite.
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
use std::fs;
use std::path::PathBuf;

fn temp_db() -> PathBuf {
    let mut path = std::env::temp_dir();
    path.push("mirror_log_database_test_");
    let random: u64 = rand::random();
    path.push(format!("database_test_{}.db", random));

    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).ok();
    }

    path
}

#[cfg(test)]
mod database_tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn test_database_corruption_recovery() {
        let db_path = temp_db();
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add some events
        mirror_log::log::append(&conn, "source1", "Event content 1", None)
            .expect("Failed to append");
        mirror_log::log::append(&conn, "source2", "Event content 2", None)
            .expect("Failed to append");

        // Corrupt the database file
        use std::fs::File;
        let mut file = File::create(&db_path).expect("Failed to create file");
        file.write_all(b"corrupted database content")
            .expect("Failed to write");
        fs::remove_file(&db_path).ok();

        // Try to initialize DB - should handle corruption gracefully
        let result = mirror_log::db::init_db(&db_path);
        assert!(result.is_err() || fs::metadata(&db_path).is_ok());

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_migration_scenario() {
        let db_path = temp_db();

        // Initialize with old schema
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add events
        mirror_log::log::append(&conn, "source1", "Event content 1", None)
            .expect("Failed to append");

        // Simulate migration by closing and reopening
        drop(conn);

        // Reinitialize - should handle migration
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);
        assert_eq!(_unique, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_concurrent_access() {
        let db_path = temp_db();
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add events
        mirror_log::log::append(&conn, "source1", "Event content 1", None)
            .expect("Failed to append");
        mirror_log::log::append(&conn, "source2", "Event content 2", None)
            .expect("Failed to append");

        // Simulate concurrent access by spawning multiple threads
        use std::thread;

        let mut handles = vec![];
        for i in 0..5 {
            let db_path_clone = db_path.clone();
            let handle = thread::spawn(move || {
                let conn =
                    mirror_log::db::init_db(&db_path_clone).expect("Failed to initialize DB");
                mirror_log::log::append(&conn, "concurrent", &format!("Event {}", i), None)
                    .expect("Failed to append");
                drop(conn);
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().expect("Thread failed");
        }

        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");

        // Should have at least 7 events (2 original + 5 concurrent)
        assert!(total >= 7);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_backup_restore() {
        let db_path = temp_db();
        let mut backup_path = temp_db();
        backup_path.set_extension("backup");

        // Initialize and add events
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");
        mirror_log::log::append(&conn, "source1", "Event content 1", None)
            .expect("Failed to append");
        mirror_log::log::append(&conn, "source2", "Event content 2", None)
            .expect("Failed to append");
        drop(conn);

        // Backup database
        fs::copy(&db_path, &backup_path).expect("Failed to backup");

        // Add more events
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");
        mirror_log::log::append(&conn, "source3", "Event content 3", None)
            .expect("Failed to append");
        drop(conn);

        // Restore from backup
        fs::copy(&backup_path, &db_path).expect("Failed to restore");

        // Verify backup content
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");

        assert_eq!(total, 2);
        assert_eq!(_unique, 2);

        fs::remove_file(&db_path).ok();
        fs::remove_file(&backup_path).ok();
    }

    #[test]
    fn test_multiple_databases() {
        let db_path1 = temp_db();
        let db_path2 = temp_db();
        let db_path3 = temp_db();

        // Initialize three databases
        let conn1 = mirror_log::db::init_db(&db_path1).expect("Failed to initialize DB1");
        let conn2 = mirror_log::db::init_db(&db_path2).expect("Failed to initialize DB2");
        let conn3 = mirror_log::db::init_db(&db_path3).expect("Failed to initialize DB3");

        // Add events to each database
        mirror_log::log::append(&conn1, "source1", "Event from DB1", None)
            .expect("Failed to append");
        mirror_log::log::append(&conn2, "source2", "Event from DB2", None)
            .expect("Failed to append");
        mirror_log::log::append(&conn3, "source3", "Event from DB3", None)
            .expect("Failed to append");

        // Verify each database has its own data
        let conn1 = mirror_log::db::init_db(&db_path1).expect("Failed to initialize DB1");
        let conn2 = mirror_log::db::init_db(&db_path2).expect("Failed to initialize DB2");
        let conn3 = mirror_log::db::init_db(&db_path3).expect("Failed to initialize DB3");

        let (total1, unique1, _, _) = mirror_log::log::stats(&conn1).expect("Failed to get stats");
        let (total2, unique2, _, _) = mirror_log::log::stats(&conn2).expect("Failed to get stats");
        let (total3, unique3, _, _) = mirror_log::log::stats(&conn3).expect("Failed to get stats");

        assert_eq!(total1, 1);
        assert_eq!(unique1, 1);
        assert_eq!(total2, 1);
        assert_eq!(unique2, 1);
        assert_eq!(total3, 1);
        assert_eq!(unique3, 1);

        fs::remove_file(&db_path1).ok();
        fs::remove_file(&db_path2).ok();
        fs::remove_file(&db_path3).ok();
    }

    #[test]
    fn test_database_size_limits() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add many events to test database size handling
        for i in 0..100 {
            let content = format!("Event content number {}", i);
            mirror_log::log::append(&conn, "size_test", &content, None).expect("Failed to append");
        }

        // Verify all events were stored
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 100);
        assert_eq!(_unique, 100);

        // Verify database file exists
        assert!(fs::metadata(&db_path).is_ok());

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_locking() {
        let db_path = temp_db();

        // Initialize database
        let conn1 = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add events
        mirror_log::log::append(&conn1, "source1", "Event content 1", None)
            .expect("Failed to append");

        // Try to access from another connection
        let conn2 = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");
        let (total, _unique, _, _) = mirror_log::log::stats(&conn2).expect("Failed to get stats");

        assert_eq!(total, 1);
        assert_eq!(_unique, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_cleanup() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add events
        mirror_log::log::append(&conn, "source1", "Event content 1", None)
            .expect("Failed to append");
        mirror_log::log::append(&conn, "source2", "Event content 2", None)
            .expect("Failed to append");

        // Verify events exist
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 2);

        // Close connection
        drop(conn);

        // Verify database file still exists
        assert!(fs::metadata(&db_path).is_ok());

        // Manually remove database
        fs::remove_file(&db_path).ok();

        // Verify cleanup
        assert!(fs::metadata(&db_path).is_err());
    }

    #[test]
    fn test_database_path_handling() {
        let db_path = temp_db();

        // Test with path-like input
        let result = mirror_log::db::init_db(&db_path);
        assert!(result.is_ok());

        // Test with PathBuf
        let result = mirror_log::db::init_db(db_path.clone());
        assert!(result.is_ok());

        // Test with string path
        let result = mirror_log::db::init_db(db_path.to_str().unwrap());
        assert!(result.is_ok());

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_error_handling() {
        let db_path = temp_db();

        // Test with non-existent database
        let result = mirror_log::db::init_db(&db_path);
        assert!(result.is_ok());

        // Close database
        drop(result.unwrap());

        // Try to open again
        let result = mirror_log::db::init_db(&db_path);
        assert!(result.is_ok());

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_transaction_handling() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add events
        mirror_log::log::append(&conn, "source1", "Event content 1", None)
            .expect("Failed to append");
        mirror_log::log::append(&conn, "source2", "Event content 2", None)
            .expect("Failed to append");

        // Verify events exist
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 2);

        // Close connection
        drop(conn);

        // Verify database file still exists
        assert!(fs::metadata(&db_path).is_ok());

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_special_characters_in_path() {
        let mut db_path = temp_db();
        db_path.set_file_name("test_database_#1$2%3&4'5.db");

        // Initialize database with special characters in path
        let result = mirror_log::db::init_db(&db_path);
        assert!(result.is_ok());

        // Add events
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");
        mirror_log::log::append(&conn, "source1", "Event content 1", None)
            .expect("Failed to append");

        // Verify events
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_empty_database() {
        let db_path = temp_db();

        // Initialize empty database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Verify empty state
        let (total, _unique, oldest, newest) =
            mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 0);
        assert_eq!(_unique, 0);
        assert_eq!(oldest, 0);
        assert_eq!(newest, 0);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_large_number_of_sources() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add events from many sources
        for i in 0..50 {
            let source = format!("source_{}", i);
            let content = format!("Event from source {}", i);
            mirror_log::log::append(&conn, &source, &content, None).expect("Failed to append");
        }

        // Verify events
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 50);
        assert_eq!(_unique, 50);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_long_source_names() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add events with long source names
        let long_source = "a".repeat(100);
        let content = "Event content";
        mirror_log::log::append(&conn, &long_source, content, None).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_unicode_paths() {
        let mut db_path = temp_db();
        db_path.set_file_name("测试数据库_🌍.db");

        // Initialize database with unicode path
        let result = mirror_log::db::init_db(&db_path);
        assert!(result.is_ok());

        // Add events
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");
        mirror_log::log::append(&conn, "source1", "Event content 1", None)
            .expect("Failed to append");

        // Verify events
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_null_bytes() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with null byte in content
        let content = "Event with null\x00 byte";
        mirror_log::log::append(&conn, "source1", content, None).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_binary_content() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with binary content
        let content = vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05];
        let content_string = String::from_utf8_lossy(&content).to_string();
        mirror_log::log::append(&conn, "source1", &content_string, None).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_malformed_json_meta() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with malformed JSON meta
        let content = "Event content";
        let meta = r#"{"key": "value", "invalid": "}}"#;
        mirror_log::log::append(&conn, "source1", content, Some(meta)).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_complex_meta() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with complex JSON meta
        let content = "Event content";
        let meta = r#"{"key": "value", "number": 123, "array": [1, 2, 3], "nested": {"key": "value", "number": 456}, "unicode": "你好世界 🌍", "boolean": true, "null": null}"#;
        mirror_log::log::append(&conn, "source1", content, Some(meta)).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_empty_meta() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with empty meta
        let content = "Event content";
        mirror_log::log::append(&conn, "source1", content, None).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_special_characters_in_meta() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with special characters in meta
        let content = "Event content";
        let meta = r#"{"special": "!@#$%^&*()_+-=[]{}|;':\",./<>?", "unicode": "你好世界 🌍", "emoji": "🎉🎊🎈"}"#;
        mirror_log::log::append(&conn, "source1", content, Some(meta)).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_very_long_meta() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with very long meta
        let content = "Event content";
        let meta = format!("{{\"key\": \"{}\"}}", "x".repeat(10000));
        mirror_log::log::append(&conn, "source1", content, Some(&meta)).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_duplicate_deduplication() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add duplicate events
        let content = "Duplicate content";
        mirror_log::log::append(&conn, "source1", content, None).expect("Failed to append");
        mirror_log::log::append(&conn, "source2", content, None).expect("Failed to append");

        // Verify duplicate detection
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 2);
        assert_eq!(_unique, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_chunked_content() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with chunked content
        let content = "This is a test chunked content with specific text to search for";
        let id =
            mirror_log::log::append(&conn, "source1", content, None).expect("Failed to append");

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        // Create chunks
        let _chunk_count = mirror_log::chunk::create_chunks(&conn, &id, content, timestamp, 20)
            .expect("Failed to create chunks");

        // Verify chunks
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_chunk_search() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with chunked content
        let content = "This is a test chunked content with specific text to search for";
        let id =
            mirror_log::log::append(&conn, "source1", content, None).expect("Failed to append");

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        // Create chunks
        mirror_log::chunk::create_chunks(&conn, &id, content, timestamp, 20)
            .expect("Failed to create chunks");

        // Search for text in chunks
        let events = mirror_log::view::search(&conn, "specific text").expect("Failed to search");
        assert!(!events.is_empty());

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_empty_chunk_content() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with empty content
        let content = "";
        let _id =
            mirror_log::log::append(&conn, "source1", content, None).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_multiline_content() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with multiline content
        let content = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5";
        let _id =
            mirror_log::log::append(&conn, "source1", content, None).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_very_long_content() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with very long content
        let content = "A".repeat(1000000); // 1MB content
        let _id =
            mirror_log::log::append(&conn, "source1", &content, None).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_special_characters_in_content() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with special characters
        let content = "Special characters: !@#$%^&*()_+-=[]{}|;':\",./<>?\n\t\r\n";
        let _id =
            mirror_log::log::append(&conn, "source1", content, None).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }

    #[test]
    fn test_database_with_unicode_content() {
        let db_path = temp_db();

        // Initialize database
        let conn = mirror_log::db::init_db(&db_path).expect("Failed to initialize DB");

        // Add event with unicode content
        let content = "你好世界 🌍";
        let _id =
            mirror_log::log::append(&conn, "source1", content, None).expect("Failed to append");

        // Verify event
        let (total, _unique, _, _) = mirror_log::log::stats(&conn).expect("Failed to get stats");
        assert_eq!(total, 1);

        fs::remove_file(&db_path).ok();
    }
}