rouchdb 0.3.2

Local-first document database with CouchDB replication protocol support
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
//! Bug-hunting tests: edge cases, boundary conditions, and regression tests.
//! These tests are designed to expose bugs in the implementation.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use rouchdb::{
    AllDocsOptions, BulkDocsOptions, ChangesOptions, ChangesStreamOptions, Database,
    DesignDocument, Document, FindOptions, Plugin, Result, Revision, SecurityDocument,
    SecurityGroup, SortField, ViewDef, ViewEngine, ViewQueryOptions, query_view,
};

// =========================================================================
// BUG: Partition find() doesn't escape regex metacharacters
// =========================================================================

#[tokio::test]
async fn partition_name_with_regex_metacharacters() {
    let db = Database::memory("test");

    // Put docs with different prefixes
    db.put("user.test:doc1", serde_json::json!({"type": "a"}))
        .await
        .unwrap();
    db.put("userXtest:doc2", serde_json::json!({"type": "b"}))
        .await
        .unwrap();
    db.put("user_test:doc3", serde_json::json!({"type": "c"}))
        .await
        .unwrap();

    // "user.test" as partition name — the "." is a regex metacharacter
    let partition = db.partition("user.test");
    let result = partition
        .find(FindOptions {
            selector: serde_json::json!({"type": {"$exists": true}}),
            ..Default::default()
        })
        .await
        .unwrap();

    // Should ONLY match "user.test:" prefix, not "userXtest:"
    // Because "." in regex matches any character
    for doc in &result.docs {
        let id = doc["_id"].as_str().unwrap();
        assert!(
            id.starts_with("user.test:"),
            "Partition find should only return docs with exact prefix, got: {}",
            id
        );
    }
}

// =========================================================================
// BUG: Partition all_docs() can escape partition boundaries
// =========================================================================

#[tokio::test]
async fn partition_all_docs_enforces_boundaries() {
    let db = Database::memory("test");

    db.put("users:alice", serde_json::json!({"type": "user"}))
        .await
        .unwrap();
    db.put("users:bob", serde_json::json!({"type": "user"}))
        .await
        .unwrap();
    db.put("zzz:other", serde_json::json!({"type": "other"}))
        .await
        .unwrap();

    let partition = db.partition("users");

    // User tries to escape partition by providing start_key outside bounds
    let result = partition
        .all_docs(AllDocsOptions {
            start_key: Some("a".to_string()), // before "users:"
            include_docs: true,
            ..AllDocsOptions::new()
        })
        .await
        .unwrap();

    // Should still only return docs from users partition
    for row in &result.rows {
        assert!(
            row.id.starts_with("users:"),
            "Partition should enforce boundaries, got: {}",
            row.id
        );
    }
}

// =========================================================================
// BUG: Empty partition name
// =========================================================================

#[tokio::test]
async fn partition_empty_name() {
    let db = Database::memory("test");
    db.put(":doc1", serde_json::json!({"v": 1})).await.unwrap();
    db.put("normal_doc", serde_json::json!({"v": 2}))
        .await
        .unwrap();

    let partition = db.partition("");
    let result = partition.all_docs(AllDocsOptions::new()).await.unwrap();

    // Empty partition name creates ":" prefix
    // This should only match docs starting with ":"
    for row in &result.rows {
        assert!(
            row.id.starts_with(':'),
            "Empty partition should match ':' prefix, got: {}",
            row.id
        );
    }
}

// =========================================================================
// BUG: Document from_json/to_json roundtrip loses _attachments
// =========================================================================

#[tokio::test]
async fn document_roundtrip_preserves_attachments() {
    let json = serde_json::json!({
        "_id": "doc1",
        "_attachments": {
            "file.txt": {
                "content_type": "text/plain",
                "digest": "md5-abc123",
                "length": 5,
                "stub": true
            }
        },
        "name": "test"
    });

    let doc = Document::from_json(json).unwrap();
    assert_eq!(doc.attachments.len(), 1);
    assert!(doc.attachments.contains_key("file.txt"));

    // Roundtrip
    let json_out = doc.to_json();
    let doc2 = Document::from_json(json_out).unwrap();
    assert_eq!(doc2.attachments.len(), 1);
    assert!(doc2.attachments.contains_key("file.txt"));
    assert_eq!(doc2.attachments["file.txt"].content_type, "text/plain");
}

#[tokio::test]
async fn document_from_json_with_empty_attachments() {
    let json = serde_json::json!({
        "_id": "doc1",
        "_attachments": {},
        "name": "test"
    });

    let doc = Document::from_json(json).unwrap();
    assert!(doc.attachments.is_empty());
}

#[tokio::test]
async fn document_from_json_without_id() {
    let json = serde_json::json!({"name": "test"});
    let doc = Document::from_json(json).unwrap();
    assert!(doc.id.is_empty());
}

#[tokio::test]
async fn document_from_json_non_object() {
    let json = serde_json::json!("just a string");
    let result = Document::from_json(json);
    assert!(result.is_err());
}

#[tokio::test]
async fn document_from_json_with_invalid_rev() {
    let json = serde_json::json!({
        "_id": "doc1",
        "_rev": "not-a-valid-rev"
    });
    let result = Document::from_json(json);
    assert!(result.is_err());
}

#[tokio::test]
async fn document_to_json_preserves_all_fields() {
    let doc = Document {
        id: "doc1".into(),
        rev: Some(Revision::new(1, "abc".into())),
        deleted: false,
        data: serde_json::json!({"name": "Alice", "nested": {"a": 1}}),
        attachments: HashMap::new(),
    };

    let json = doc.to_json();
    assert_eq!(json["_id"], "doc1");
    assert_eq!(json["_rev"], "1-abc");
    assert_eq!(json["name"], "Alice");
    assert_eq!(json["nested"]["a"], 1);
    assert!(json.get("_deleted").is_none());
}

#[tokio::test]
async fn document_to_json_deleted_doc() {
    let doc = Document {
        id: "doc1".into(),
        rev: Some(Revision::new(2, "def".into())),
        deleted: true,
        data: serde_json::json!({}),
        attachments: HashMap::new(),
    };

    let json = doc.to_json();
    assert_eq!(json["_deleted"], true);
}

// =========================================================================
// BUG: Purge doesn't clean rev_tree
// =========================================================================

#[tokio::test]
async fn purge_then_get_returns_not_found() {
    let db = Database::memory("test");
    let r1 = db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();
    let rev = r1.rev.unwrap();

    db.purge("doc1", vec![rev]).await.unwrap();

    // After purge, doc should be completely gone
    let result = db.get("doc1").await;
    assert!(result.is_err(), "Purged doc should not be retrievable");
}

#[tokio::test]
async fn purge_then_changes_excludes_purged() {
    let db = Database::memory("test");
    let r1 = db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();
    db.put("doc2", serde_json::json!({"v": 2})).await.unwrap();

    db.purge("doc1", vec![r1.rev.unwrap()]).await.unwrap();

    let changes = db.changes(ChangesOptions::default()).await.unwrap();
    let ids: Vec<&str> = changes.results.iter().map(|r| r.id.as_str()).collect();
    assert!(
        !ids.contains(&"doc1"),
        "Purged doc should not appear in changes"
    );
    assert!(ids.contains(&"doc2"));
}

#[tokio::test]
async fn purge_partial_revs() {
    let db = Database::memory("test");

    // Create doc with two revisions
    let r1 = db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();
    let rev1 = r1.rev.unwrap();
    let r2 = db
        .update("doc1", &rev1, serde_json::json!({"v": 2}))
        .await
        .unwrap();
    let _rev2 = r2.rev.unwrap();

    // Purge only the first revision — doc should still exist with rev2
    let purge_result = db.purge("doc1", vec![rev1]).await.unwrap();
    assert!(purge_result.purged.contains_key("doc1"));

    // Doc should still be accessible via latest rev
    let doc = db.get("doc1").await.unwrap();
    assert_eq!(doc.data["v"], 2);
}

// =========================================================================
// BUG: ViewQueryOptions descending with start/end key
// =========================================================================

#[tokio::test]
async fn view_descending_with_key_range() {
    let db = Database::memory("test");

    for i in 0..10 {
        db.put(&format!("doc{}", i), serde_json::json!({"n": i}))
            .await
            .unwrap();
    }

    let map_fn = |doc: &serde_json::Value| -> Vec<(serde_json::Value, serde_json::Value)> {
        vec![(doc["n"].clone(), serde_json::json!(1))]
    };

    // Descending order with start_key=7, end_key=3
    // Should return 7, 6, 5, 4, 3
    let results = query_view(
        db.adapter(),
        &map_fn,
        None,
        ViewQueryOptions {
            descending: true,
            start_key: Some(serde_json::json!(7)),
            end_key: Some(serde_json::json!(3)),
            inclusive_end: true,
            ..ViewQueryOptions::new()
        },
    )
    .await
    .unwrap();

    assert_eq!(
        results.rows.len(),
        5,
        "Descending range 7..=3 should have 5 rows"
    );

    // Verify order is descending
    let keys: Vec<i64> = results
        .rows
        .iter()
        .map(|r| r.key.as_i64().unwrap())
        .collect();
    assert_eq!(keys, vec![7, 6, 5, 4, 3]);
}

#[tokio::test]
async fn view_descending_without_range() {
    let db = Database::memory("test");

    db.put("a", serde_json::json!({"n": 1})).await.unwrap();
    db.put("b", serde_json::json!({"n": 2})).await.unwrap();
    db.put("c", serde_json::json!({"n": 3})).await.unwrap();

    let map_fn = |doc: &serde_json::Value| -> Vec<(serde_json::Value, serde_json::Value)> {
        vec![(doc["n"].clone(), serde_json::json!(null))]
    };

    let results = query_view(
        db.adapter(),
        &map_fn,
        None,
        ViewQueryOptions {
            descending: true,
            ..ViewQueryOptions::new()
        },
    )
    .await
    .unwrap();

    let keys: Vec<i64> = results
        .rows
        .iter()
        .map(|r| r.key.as_i64().unwrap())
        .collect();
    assert_eq!(keys, vec![3, 2, 1]);
}

// =========================================================================
// Plugin: multiple plugins, first modifies then second rejects
// =========================================================================

struct AddFieldPlugin {
    field: String,
    value: serde_json::Value,
}

#[async_trait::async_trait]
impl Plugin for AddFieldPlugin {
    fn name(&self) -> &str {
        "add-field"
    }
    async fn before_write(&self, docs: &mut Vec<Document>) -> Result<()> {
        for doc in docs.iter_mut() {
            if let serde_json::Value::Object(ref mut map) = doc.data {
                map.insert(self.field.clone(), self.value.clone());
            }
        }
        Ok(())
    }
}

struct RejectPlugin;

#[async_trait::async_trait]
impl Plugin for RejectPlugin {
    fn name(&self) -> &str {
        "reject"
    }
    async fn before_write(&self, _docs: &mut Vec<Document>) -> Result<()> {
        Err(rouchdb::RouchError::BadRequest("rejected".into()))
    }
}

#[tokio::test]
async fn plugin_first_modifies_second_rejects_no_write() {
    let db = Database::memory("test")
        .with_plugin(Arc::new(AddFieldPlugin {
            field: "modified".into(),
            value: serde_json::json!(true),
        }))
        .with_plugin(Arc::new(RejectPlugin));

    let result = db.put("doc1", serde_json::json!({"v": 1})).await;
    assert!(result.is_err());

    // Doc should NOT have been written
    let get_result = db.get("doc1").await;
    assert!(get_result.is_err(), "Rejected doc should not be stored");
}

#[tokio::test]
async fn plugin_before_write_called_on_update() {
    let counter = Arc::new(std::sync::atomic::AtomicU64::new(0));
    let counter_clone = counter.clone();

    struct WriteCountPlugin(Arc<std::sync::atomic::AtomicU64>);

    #[async_trait::async_trait]
    impl Plugin for WriteCountPlugin {
        fn name(&self) -> &str {
            "write-count"
        }
        async fn before_write(&self, _docs: &mut Vec<Document>) -> Result<()> {
            self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            Ok(())
        }
    }

    let db = Database::memory("test").with_plugin(Arc::new(WriteCountPlugin(counter_clone)));

    let r1 = db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();
    assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);

    db.update("doc1", &r1.rev.unwrap(), serde_json::json!({"v": 2}))
        .await
        .unwrap();
    assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 2);
}

#[tokio::test]
async fn plugin_before_write_called_on_remove() {
    let counter = Arc::new(std::sync::atomic::AtomicU64::new(0));
    let counter_clone = counter.clone();

    struct WriteCountPlugin2(Arc<std::sync::atomic::AtomicU64>);

    #[async_trait::async_trait]
    impl Plugin for WriteCountPlugin2 {
        fn name(&self) -> &str {
            "write-count"
        }
        async fn before_write(&self, _docs: &mut Vec<Document>) -> Result<()> {
            self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            Ok(())
        }
    }

    let db = Database::memory("test").with_plugin(Arc::new(WriteCountPlugin2(counter_clone)));

    let r1 = db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();
    db.remove("doc1", &r1.rev.unwrap()).await.unwrap();
    assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 2);
}

// =========================================================================
// Changes feed: selector with all results filtered should still advance seq
// =========================================================================

#[tokio::test]
async fn changes_selector_advances_last_seq_even_when_all_filtered() {
    let db = Database::memory("test");
    db.put("a", serde_json::json!({"type": "x"})).await.unwrap();
    db.put("b", serde_json::json!({"type": "x"})).await.unwrap();

    // Selector matches nothing — but last_seq should still advance
    let changes = db
        .changes(ChangesOptions {
            selector: Some(serde_json::json!({"type": "nonexistent"})),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(changes.results.len(), 0);
    // last_seq should NOT be the default — it should reflect the adapter's state
    assert_ne!(
        changes.last_seq,
        rouchdb::Seq::default(),
        "last_seq should advance even when all changes are filtered"
    );
}

// =========================================================================
// allDocs: edge cases
// =========================================================================

#[tokio::test]
async fn all_docs_skip_beyond_total() {
    let db = Database::memory("test");
    db.put("a", serde_json::json!({})).await.unwrap();
    db.put("b", serde_json::json!({})).await.unwrap();

    let result = db
        .all_docs(AllDocsOptions {
            skip: 100,
            ..AllDocsOptions::new()
        })
        .await
        .unwrap();

    assert_eq!(result.rows.len(), 0);
    assert_eq!(result.total_rows, 2);
}

#[tokio::test]
async fn all_docs_limit_zero() {
    let db = Database::memory("test");
    db.put("a", serde_json::json!({})).await.unwrap();

    let result = db
        .all_docs(AllDocsOptions {
            limit: Some(0),
            ..AllDocsOptions::new()
        })
        .await
        .unwrap();

    assert_eq!(result.rows.len(), 0);
}

#[tokio::test]
async fn all_docs_descending() {
    let db = Database::memory("test");
    db.put("a", serde_json::json!({})).await.unwrap();
    db.put("b", serde_json::json!({})).await.unwrap();
    db.put("c", serde_json::json!({})).await.unwrap();

    let result = db
        .all_docs(AllDocsOptions {
            descending: true,
            ..AllDocsOptions::new()
        })
        .await
        .unwrap();

    let ids: Vec<&str> = result.rows.iter().map(|r| r.id.as_str()).collect();
    assert_eq!(ids, vec!["c", "b", "a"]);
}

#[tokio::test]
async fn all_docs_start_end_key_range() {
    let db = Database::memory("test");
    db.put("a", serde_json::json!({})).await.unwrap();
    db.put("b", serde_json::json!({})).await.unwrap();
    db.put("c", serde_json::json!({})).await.unwrap();
    db.put("d", serde_json::json!({})).await.unwrap();

    let result = db
        .all_docs(AllDocsOptions {
            start_key: Some("b".into()),
            end_key: Some("c".into()),
            ..AllDocsOptions::new()
        })
        .await
        .unwrap();

    let ids: Vec<&str> = result.rows.iter().map(|r| r.id.as_str()).collect();
    assert_eq!(ids, vec!["b", "c"]);
}

#[tokio::test]
async fn all_docs_with_nonexistent_keys() {
    let db = Database::memory("test");
    db.put("a", serde_json::json!({})).await.unwrap();

    let result = db
        .all_docs(AllDocsOptions {
            keys: Some(vec!["a".into(), "nonexistent".into()]),
            ..AllDocsOptions::new()
        })
        .await
        .unwrap();

    // Should return what exists
    assert!(result.rows.iter().any(|r| r.id == "a"));
}

// =========================================================================
// Design doc: roundtrip via put/get preserves all fields
// =========================================================================

#[tokio::test]
async fn design_doc_full_roundtrip() {
    let db = Database::memory("test");

    let ddoc = DesignDocument {
        id: "_design/full".into(),
        rev: None,
        views: {
            let mut v = HashMap::new();
            v.insert(
                "by_type".into(),
                ViewDef {
                    map: "function(doc) { emit(doc.type, 1); }".into(),
                    reduce: Some("_count".into()),
                },
            );
            v.insert(
                "by_name".into(),
                ViewDef {
                    map: "function(doc) { emit(doc.name, null); }".into(),
                    reduce: None,
                },
            );
            v
        },
        filters: {
            let mut f = HashMap::new();
            f.insert("my_filter".into(), "function(doc) { return true; }".into());
            f
        },
        validate_doc_update: Some("function(n,o,u) {}".into()),
        shows: {
            let mut s = HashMap::new();
            s.insert("detail".into(), "function(doc,req) {}".into());
            s
        },
        lists: {
            let mut l = HashMap::new();
            l.insert("all".into(), "function(head,req) {}".into());
            l
        },
        updates: {
            let mut u = HashMap::new();
            u.insert("bump".into(), "function(doc,req) {}".into());
            u
        },
        language: Some("javascript".into()),
    };

    db.put_design(ddoc.clone()).await.unwrap();

    let retrieved = db.get_design("full").await.unwrap();
    assert_eq!(retrieved.views.len(), 2);
    assert_eq!(retrieved.filters.len(), 1);
    assert!(retrieved.validate_doc_update.is_some());
    assert_eq!(retrieved.shows.len(), 1);
    assert_eq!(retrieved.lists.len(), 1);
    assert_eq!(retrieved.updates.len(), 1);
    assert_eq!(retrieved.language, Some("javascript".into()));

    // Verify individual fields survived the roundtrip
    assert_eq!(retrieved.views["by_type"].reduce, Some("_count".into()));
    assert_eq!(retrieved.views["by_name"].reduce, None);
}

// =========================================================================
// Mango find: edge cases
// =========================================================================

#[tokio::test]
async fn find_with_empty_selector_matches_all() {
    let db = Database::memory("test");
    db.put("a", serde_json::json!({"v": 1})).await.unwrap();
    db.put("b", serde_json::json!({"v": 2})).await.unwrap();

    let result = db
        .find(FindOptions {
            selector: serde_json::json!({}),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(result.docs.len(), 2);
}

#[tokio::test]
async fn find_with_sort_and_limit() {
    let db = Database::memory("test");
    db.put("a", serde_json::json!({"name": "Zara", "age": 20}))
        .await
        .unwrap();
    db.put("b", serde_json::json!({"name": "Alice", "age": 30}))
        .await
        .unwrap();
    db.put("c", serde_json::json!({"name": "Bob", "age": 25}))
        .await
        .unwrap();

    let result = db
        .find(FindOptions {
            selector: serde_json::json!({"age": {"$exists": true}}),
            sort: Some(vec![SortField::Simple("name".into())]),
            limit: Some(2),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(result.docs.len(), 2);
    assert_eq!(result.docs[0]["name"], "Alice");
    assert_eq!(result.docs[1]["name"], "Bob");
}

#[tokio::test]
async fn find_with_skip() {
    let db = Database::memory("test");
    for i in 0..5 {
        db.put(&format!("doc{}", i), serde_json::json!({"i": i}))
            .await
            .unwrap();
    }

    let result = db
        .find(FindOptions {
            selector: serde_json::json!({"i": {"$exists": true}}),
            skip: Some(3),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(result.docs.len(), 2);
}

#[tokio::test]
async fn find_with_fields_projection() {
    let db = Database::memory("test");
    db.put(
        "a",
        serde_json::json!({"name": "Alice", "age": 30, "email": "alice@example.com"}),
    )
    .await
    .unwrap();

    let result = db
        .find(FindOptions {
            selector: serde_json::json!({"name": "Alice"}),
            fields: Some(vec!["name".into(), "age".into()]),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(result.docs.len(), 1);
    assert_eq!(result.docs[0]["name"], "Alice");
    assert_eq!(result.docs[0]["age"], 30);
    // email should NOT be present
    assert!(result.docs[0].get("email").is_none());
}

// =========================================================================
// Security: update and verify
// =========================================================================

#[tokio::test]
async fn security_overwrite() {
    let db = Database::memory("test");

    let sec1 = SecurityDocument {
        admins: SecurityGroup {
            names: vec!["admin1".into()],
            roles: vec![],
        },
        members: SecurityGroup::default(),
    };
    db.put_security(sec1).await.unwrap();

    let sec2 = SecurityDocument {
        admins: SecurityGroup {
            names: vec!["admin2".into()],
            roles: vec!["role1".into()],
        },
        members: SecurityGroup::default(),
    };
    db.put_security(sec2).await.unwrap();

    let fetched = db.get_security().await.unwrap();
    assert_eq!(fetched.admins.names, vec!["admin2"]);
    assert_eq!(fetched.admins.roles, vec!["role1"]);
}

// =========================================================================
// Replication: sync with concurrent modifications
// =========================================================================

#[tokio::test]
async fn sync_idempotent() {
    let a = Database::memory("a");
    let b = Database::memory("b");

    a.put("doc1", serde_json::json!({"v": 1})).await.unwrap();

    // Sync twice — second should be a no-op
    a.sync(&b).await.unwrap();
    let (push, pull) = a.sync(&b).await.unwrap();
    assert!(push.ok);
    assert!(pull.ok);
    assert_eq!(push.docs_written, 0);
}

// =========================================================================
// Live changes: adding docs during streaming
// =========================================================================

#[tokio::test]
async fn live_changes_picks_up_docs_added_after_start() {
    let db = Database::memory("test");

    let (mut rx, handle) = db.live_changes(ChangesStreamOptions {
        poll_interval: Duration::from_millis(50),
        ..Default::default()
    });

    // Add a doc after the live stream started
    db.put("late_doc", serde_json::json!({"v": 1}))
        .await
        .unwrap();

    let event = tokio::time::timeout(Duration::from_secs(2), rx.recv())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(event.id, "late_doc");

    handle.cancel();
}

// =========================================================================
// ViewEngine: multiple emits per document
// =========================================================================

#[tokio::test]
async fn view_engine_multiple_emits_per_doc() {
    let db = Database::memory("test");

    db.put(
        "doc1",
        serde_json::json!({"tags": ["rust", "db", "local-first"]}),
    )
    .await
    .unwrap();

    let mut engine = ViewEngine::new();
    engine.register_map("app", "by_tag", |doc| {
        let mut emitted = vec![];
        if let Some(tags) = doc.get("tags").and_then(|t| t.as_array()) {
            for tag in tags {
                emitted.push((tag.clone(), serde_json::json!(1)));
            }
        }
        emitted
    });

    engine
        .update_index(db.adapter(), "app", "by_tag")
        .await
        .unwrap();

    let index = engine.get_index("app", "by_tag").unwrap();
    // doc1 should have 3 emitted pairs
    assert_eq!(index.entries["doc1"].len(), 3);
}

// =========================================================================
// Conflicts: creating and querying
// =========================================================================

#[tokio::test]
async fn create_conflict_via_bulk_docs() {
    let db = Database::memory("test");

    // Create initial doc
    db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();

    // Force a conflicting revision via replication mode
    let conflict_doc = Document {
        id: "doc1".into(),
        rev: Some(Revision::new(1, "conflicting_hash".into())),
        deleted: false,
        data: serde_json::json!({"v": "conflict"}),
        attachments: HashMap::new(),
    };

    db.bulk_docs(vec![conflict_doc], BulkDocsOptions::replication())
        .await
        .unwrap();

    // Get the doc — should get the winning revision
    let doc = db.get("doc1").await.unwrap();
    assert!(doc.data.get("v").is_some());
}

// =========================================================================
// Edge case: put with very large document
// =========================================================================

#[tokio::test]
async fn put_large_document() {
    let db = Database::memory("test");

    let large_array: Vec<i64> = (0..10000).collect();
    let result = db
        .put("large", serde_json::json!({"data": large_array}))
        .await
        .unwrap();
    assert!(result.ok);

    let doc = db.get("large").await.unwrap();
    assert_eq!(doc.data["data"].as_array().unwrap().len(), 10000);
}

// =========================================================================
// Edge case: special characters in document IDs
// =========================================================================

#[tokio::test]
async fn special_characters_in_doc_id() {
    let db = Database::memory("test");

    let ids = vec![
        "doc with spaces",
        "doc/with/slashes",
        "doc-with-dashes",
        "doc_with_underscores",
        "123numeric",
        "UPPERCASE",
    ];

    for id in &ids {
        let result = db.put(id, serde_json::json!({"id": id})).await.unwrap();
        assert!(result.ok, "Failed to put doc with id: {}", id);

        let doc = db.get(id).await.unwrap();
        assert_eq!(doc.data["id"], *id);
    }
}

// =========================================================================
// Edge case: empty database operations
// =========================================================================

#[tokio::test]
async fn operations_on_empty_db() {
    let db = Database::memory("test");

    let info = db.info().await.unwrap();
    assert_eq!(info.doc_count, 0);

    let all = db.all_docs(AllDocsOptions::new()).await.unwrap();
    assert_eq!(all.rows.len(), 0);

    let changes = db.changes(ChangesOptions::default()).await.unwrap();
    assert_eq!(changes.results.len(), 0);

    let find = db
        .find(FindOptions {
            selector: serde_json::json!({}),
            ..Default::default()
        })
        .await
        .unwrap();
    assert_eq!(find.docs.len(), 0);
}

// =========================================================================
// Edge case: update with wrong rev
// =========================================================================

#[tokio::test]
async fn update_with_wrong_rev_fails() {
    let db = Database::memory("test");
    db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();

    let result = db
        .update("doc1", "1-wronghash", serde_json::json!({"v": 2}))
        .await
        .unwrap();
    assert!(!result.ok, "Update with wrong rev should fail");
}

#[tokio::test]
async fn remove_with_wrong_rev_fails() {
    let db = Database::memory("test");
    db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();

    let result = db.remove("doc1", "1-wronghash").await.unwrap();
    assert!(!result.ok, "Remove with wrong rev should fail");
}

// =========================================================================
// Explain: with multiple indexes, picks the right one
// =========================================================================

#[tokio::test]
async fn explain_picks_matching_index() {
    let db = Database::memory("test");

    db.put(
        "a",
        serde_json::json!({"name": "Alice", "age": 30, "city": "NYC"}),
    )
    .await
    .unwrap();

    db.create_index(rouchdb::IndexDefinition {
        name: "".into(),
        fields: vec![SortField::Simple("age".into())],
        ddoc: None,
    })
    .await
    .unwrap();

    db.create_index(rouchdb::IndexDefinition {
        name: "".into(),
        fields: vec![SortField::Simple("city".into())],
        ddoc: None,
    })
    .await
    .unwrap();

    // Query on age — should pick age index
    let explanation = db
        .explain(FindOptions {
            selector: serde_json::json!({"age": {"$gt": 20}}),
            ..Default::default()
        })
        .await;
    assert_eq!(explanation.index.name, "idx-age");

    // Query on city — should pick city index
    let explanation = db
        .explain(FindOptions {
            selector: serde_json::json!({"city": "NYC"}),
            ..Default::default()
        })
        .await;
    assert_eq!(explanation.index.name, "idx-city");
}