aimcal-core 0.12.1

AIM - Analyze. Interact. Manage Your Time, with calendar 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
// SPDX-FileCopyrightText: 2025-2026 Zexin Yuan <aim@yzx9.xyz>
//
// SPDX-License-Identifier: Apache-2.0

use std::num::NonZeroU32;

use sqlx::SqlitePool;

use crate::{Kind, short_id::UidAndShortId};

#[derive(Debug, Clone)]
pub struct ShortIds {
    pool: SqlitePool,
}

impl ShortIds {
    pub fn new(pool: SqlitePool) -> Self {
        Self { pool }
    }

    pub async fn get_by_short_id(
        &self,
        short_id: NonZeroU32,
    ) -> Result<Option<UidAndShortId>, sqlx::Error> {
        let row: Option<(String, String)> =
            sqlx::query_as("SELECT uid, kind FROM short_ids WHERE short_id = ?;")
                .bind(i64::from(short_id.get()))
                .fetch_optional(&self.pool)
                .await?;

        match row {
            Some((uid, kind)) => Ok(if let Some(kind) = Kind::parse_stable(&kind) {
                Some(UidAndShortId {
                    uid,
                    short_id,
                    kind,
                })
            } else {
                tracing::warn!(kind, "unknown short_id kind");
                None
            }),
            None => Ok(None),
        }
    }

    pub async fn get_or_assign_short_id(
        &self,
        uid: &str,
        kind: Kind,
    ) -> Result<NonZeroU32, sqlx::Error> {
        // In SQLite, every table (unless declared WITHOUT ROWID) maintains a hidden ROWID column.
        //
        // When a column is defined as `INTEGER PRIMARY KEY`, it becomes an alias for the ROWID,
        // and SQLite will automatically assign it a value one greater than the current maximum.
        //
        // `AUTOINCREMENT` is an alternative that guarantees IDs are never reused, even after
        // deletions or conflicts. However, unlike ROWID, it may reserve or skip IDs when an insert
        // fails or is ignored due to a conflict.
        //
        // In our case, we prefer `short_id` values to remain as small and compact as possible,
        // so we intentionally avoid using AUTOINCREMENT.
        const SQL: &str = "\
INSERT INTO short_ids (uid, kind) VALUES (?, ?)
ON CONFLICT(uid) DO NOTHING
RETURNING short_id;
";

        if let Some((short_id,)) = sqlx::query_as::<_, (NonZeroU32,)>(SQL)
            .bind(uid)
            .bind(kind.to_str_stable())
            .fetch_optional(&self.pool)
            .await?
        {
            return Ok(short_id);
        }

        // if the insert did not return a short_id, it means the uid already exists
        let (short_id,): (NonZeroU32,) =
            sqlx::query_as("SELECT short_id FROM short_ids WHERE uid = ?")
                .bind(uid)
                .fetch_one(&self.pool)
                .await?;

        Ok(short_id)
    }

    /// Truncate the `short_ids` table, removing all entries.
    pub async fn truncate(&self) -> Result<(), sqlx::Error> {
        sqlx::query("DELETE FROM short_ids;")
            .execute(&self.pool)
            .await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use super::*;
    use crate::{
        Event as EventTrait, EventStatus, Id, LooseDateTime, Priority, Todo as TodoTrait,
        TodoStatus,
    };

    // Re-export ical types for tests
    use aimcal_ical::{
        Completed, Description, DtEnd, DtStamp, DtStart, Due, PercentComplete,
        Priority as IcalPriority, Summary, Uid, VEvent, VTodo,
    };

    // Re-export EventStatus and TodoStatus from ical
    use aimcal_ical::{
        EventStatus as IcalEventStatus, EventStatusValue, TodoStatus as IcalTodoStatus,
        TodoStatusValue,
    };

    // Re-export ShortIds wrapper types
    use crate::short_id::ShortIds;

    /// Test helper to create a test database
    async fn setup_test_db() -> crate::db::Db {
        crate::db::Db::open(None)
            .await
            .expect("Failed to create test database")
    }

    /// Helper to create a test `VEvent`
    fn test_event(uid: &str, summary: &str) -> VEvent<String> {
        let now = jiff::Zoned::now();
        VEvent {
            uid: Uid::new(uid.to_string()),
            dt_stamp: DtStamp::new(now.datetime()),
            dt_start: DtStart::new(LooseDateTime::Local(now.clone())),
            dt_end: Some(DtEnd::new(LooseDateTime::Local(now.clone()))),
            duration: None,
            summary: Some(Summary::new(summary.to_string())),
            description: Some(Description::new("Test description".to_string())),
            status: Some(IcalEventStatus::new(EventStatusValue::Confirmed)),
            location: None,
            geo: None,
            url: None,
            organizer: None,
            attendees: Vec::new(),
            last_modified: None,
            transparency: None,
            sequence: None,
            priority: None,
            classification: None,
            resources: None,
            categories: None,
            rrule: None,
            rdates: Vec::new(),
            ex_dates: Vec::new(),
            x_properties: Vec::new(),
            retained_properties: Vec::new(),
            alarms: Vec::new(),
        }
    }

    /// Helper to create a test `VTodo`
    fn test_todo(uid: &str, summary: &str) -> VTodo<String> {
        let now = jiff::Zoned::now();
        let utc_now = now.with_time_zone(jiff::tz::TimeZone::UTC);
        VTodo {
            uid: Uid::new(uid.to_string()),
            dt_stamp: DtStamp::new(now.datetime()),
            dt_start: None,
            due: Some(Due::new(LooseDateTime::Local(now.clone()))),
            completed: Some(Completed::new(utc_now.datetime())),
            duration: None,
            summary: Some(Summary::new(summary.to_string())),
            description: Some(Description::new("Test todo description".to_string())),
            location: None,
            geo: None,
            url: None,
            organizer: None,
            attendees: Vec::new(),
            last_modified: None,
            status: Some(IcalTodoStatus::new(TodoStatusValue::NeedsAction)),
            sequence: None,
            priority: Some(IcalPriority::new(5)),
            percent_complete: Some(PercentComplete::new(0)),
            classification: None,
            resources: None,
            categories: None,
            rrule: None,
            rdates: Vec::new(),
            ex_dates: Vec::new(),
            x_properties: Vec::new(),
            retained_properties: Vec::new(),
            alarms: Vec::new(),
        }
    }

    #[tokio::test]
    async fn short_ids_get_or_assign_short_id_assigns_new_id() {
        // Arrange
        let db = setup_test_db().await;
        let uid = "test-uid-1";

        // Act
        let short_id = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Todo)
            .await
            .expect("Failed to assign short ID");

        // Assert
        assert_eq!(short_id.get(), 1);
    }

    #[tokio::test]
    async fn short_ids_get_or_assign_short_id_returns_existing_id() {
        // Arrange
        let db = setup_test_db().await;
        let uid = "test-uid-1";
        let first_id = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Todo)
            .await
            .expect("Failed to assign short ID");

        // Act
        let second_id = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Todo)
            .await
            .expect("Failed to get short ID");

        // Assert
        assert_eq!(first_id, second_id);
        assert_eq!(first_id.get(), 1);
    }

    #[tokio::test]
    async fn short_ids_get_by_short_id_returns_correct_data() {
        // Arrange
        let db = setup_test_db().await;
        let uid = "test-uid-1";
        let short_id = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Event)
            .await
            .expect("Failed to assign short ID");

        // Act
        let result = db
            .short_ids
            .get_by_short_id(short_id)
            .await
            .expect("Failed to get by short ID");

        // Assert
        assert!(result.is_some());
        let data = result.unwrap();
        assert_eq!(data.uid, uid);
        assert_eq!(data.short_id, short_id);
        assert_eq!(data.kind, Kind::Event);
    }

    #[tokio::test]
    async fn short_ids_get_by_short_id_returns_none_for_missing_id() {
        // Arrange
        let db = setup_test_db().await;

        // Act
        let result = db
            .short_ids
            .get_by_short_id(NonZeroU32::new(999).unwrap())
            .await
            .expect("Failed to get by short ID");

        // Assert
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn short_ids_get_or_assign_short_id_increments_for_new_uids() {
        // Arrange
        let db = setup_test_db().await;

        // Act
        let id1 = db
            .short_ids
            .get_or_assign_short_id("uid-1", Kind::Todo)
            .await
            .expect("Failed to assign short ID");
        let id2 = db
            .short_ids
            .get_or_assign_short_id("uid-2", Kind::Todo)
            .await
            .expect("Failed to assign short ID");
        let id3 = db
            .short_ids
            .get_or_assign_short_id("uid-3", Kind::Event)
            .await
            .expect("Failed to assign short ID");

        // Assert
        assert_eq!(id1.get(), 1);
        assert_eq!(id2.get(), 2);
        assert_eq!(id3.get(), 3);
    }

    #[tokio::test]
    async fn short_ids_handles_same_uid_with_same_kind() {
        // Arrange
        let db = setup_test_db().await;
        let uid = "test-uid-1";

        // Act - assign the same UID with the same kind
        let id1 = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Todo)
            .await
            .expect("Failed to assign short ID");
        let id2 = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Todo)
            .await
            .expect("Failed to get short ID");

        // Assert - should return the same ID (ON CONFLICT DO NOTHING)
        assert_eq!(id1, id2);
        assert_eq!(id1.get(), 1);
    }

    #[tokio::test]
    async fn short_ids_truncate_removes_all_entries() {
        // Arrange
        let db = setup_test_db().await;
        for i in 1..=5 {
            let uid = format!("uid-{i}");
            db.short_ids
                .get_or_assign_short_id(&uid, Kind::Todo)
                .await
                .expect("Failed to assign short ID");
        }

        // Act
        db.short_ids
            .truncate()
            .await
            .expect("Failed to truncate short_ids");

        // Assert - all entries should be removed
        let result = db
            .short_ids
            .get_by_short_id(NonZeroU32::new(1).unwrap())
            .await
            .expect("Failed to get by short ID");
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn short_ids_truncate_resets_id_generation() {
        // Arrange
        let db = setup_test_db().await;
        let id1 = db
            .short_ids
            .get_or_assign_short_id("uid-1", Kind::Todo)
            .await
            .expect("Failed to assign short ID");
        assert_eq!(id1.get(), 1);

        // Act - truncate and then assign a new ID
        db.short_ids
            .truncate()
            .await
            .expect("Failed to truncate short_ids");

        let id2 = db
            .short_ids
            .get_or_assign_short_id("uid-2", Kind::Todo)
            .await
            .expect("Failed to assign short ID");

        // Assert - ID generation should restart from 1
        assert_eq!(id2.get(), 1);
    }

    #[tokio::test]
    async fn short_ids_assign_sequential_ids_starting_from_one() {
        // Arrange
        let db = setup_test_db().await;

        // Act
        let id1 = db
            .short_ids
            .get_or_assign_short_id("uid-1", Kind::Event)
            .await
            .expect("Failed to assign short ID");
        let id2 = db
            .short_ids
            .get_or_assign_short_id("uid-2", Kind::Event)
            .await
            .expect("Failed to assign short ID");
        let id3 = db
            .short_ids
            .get_or_assign_short_id("uid-3", Kind::Event)
            .await
            .expect("Failed to assign short ID");

        // Assert
        assert_eq!(id1.get(), 1);
        assert_eq!(id2.get(), 2);
        assert_eq!(id3.get(), 3);
    }

    #[tokio::test]
    async fn short_ids_increment_across_different_kinds() {
        // Arrange
        let db = setup_test_db().await;

        // Act
        let id1 = db
            .short_ids
            .get_or_assign_short_id("event-1", Kind::Event)
            .await
            .expect("Failed to assign short ID");
        let id2 = db
            .short_ids
            .get_or_assign_short_id("todo-1", Kind::Todo)
            .await
            .expect("Failed to assign short ID");
        let id3 = db
            .short_ids
            .get_or_assign_short_id("event-2", Kind::Event)
            .await
            .expect("Failed to assign short ID");

        // Assert
        // IDs are sequential across different kinds (no separate sequences)
        assert_eq!(id1.get(), 1);
        assert_eq!(id2.get(), 2);
        assert_eq!(id3.get(), 3);
    }

    #[tokio::test]
    async fn short_ids_flush_removes_all_mappings() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db.clone());

        // Create some mappings
        db.short_ids
            .get_or_assign_short_id("uid-1", Kind::Event)
            .await
            .expect("Failed to assign short ID");
        db.short_ids
            .get_or_assign_short_id("uid-2", Kind::Todo)
            .await
            .expect("Failed to assign short ID");

        // Act
        short_ids.flush().await.expect("Failed to flush short IDs");

        // Assert - all mappings should be removed
        let result = db
            .short_ids
            .get_by_short_id(1.try_into().unwrap())
            .await
            .expect("Failed to check short ID");
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn short_ids_restart_from_one_after_flush() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db.clone());

        // Create some mappings
        let id1 = db
            .short_ids
            .get_or_assign_short_id("uid-1", Kind::Event)
            .await
            .expect("Failed to assign short ID");
        assert_eq!(id1.get(), 1);
        let id2 = db
            .short_ids
            .get_or_assign_short_id("uid-2", Kind::Event)
            .await
            .expect("Failed to assign short ID");
        assert_eq!(id2.get(), 2);

        // Act - flush and assign new IDs
        short_ids.flush().await.expect("Failed to flush short IDs");

        let id3 = db
            .short_ids
            .get_or_assign_short_id("uid-3", Kind::Event)
            .await
            .expect("Failed to assign short ID");

        // Assert - ID generation should restart from 1
        assert_eq!(id3.get(), 1);
    }

    #[tokio::test]
    async fn short_ids_preserve_existing_mapping_on_reassign() {
        // Arrange
        let db = setup_test_db().await;
        let uid = "persistent-uid";

        // Act - assign the same UID twice
        let id1 = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Event)
            .await
            .expect("Failed to assign short ID");
        let id2 = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Event)
            .await
            .expect("Failed to get short ID");

        // Assert - should return the same ID
        assert_eq!(id1, id2);
        assert_eq!(id1.get(), 1);
    }

    #[tokio::test]
    async fn short_ids_continue_sequentially_after_flush_with_new_uids() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db.clone());

        // Create initial mappings
        db.short_ids
            .get_or_assign_short_id("uid-1", Kind::Event)
            .await
            .expect("Failed to assign short ID");
        db.short_ids
            .get_or_assign_short_id("uid-2", Kind::Event)
            .await
            .expect("Failed to assign short ID");
        db.short_ids
            .get_or_assign_short_id("uid-3", Kind::Event)
            .await
            .expect("Failed to assign short ID");

        // Flush
        short_ids.flush().await.expect("Failed to flush short IDs");

        // Act - create new mappings after flush
        let id1 = db
            .short_ids
            .get_or_assign_short_id("new-uid-1", Kind::Event)
            .await
            .expect("Failed to assign short ID");
        let id2 = db
            .short_ids
            .get_or_assign_short_id("new-uid-2", Kind::Event)
            .await
            .expect("Failed to assign short ID");

        // Assert - should restart from 1
        assert_eq!(id1.get(), 1);
        assert_eq!(id2.get(), 2);
    }

    #[tokio::test]
    async fn short_ids_get_returns_uid_and_short_id_for_short_id() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db.clone());
        let uid = "test-uid-event-1";

        // Assign a short ID first
        let assigned_short_id = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Event)
            .await
            .expect("Failed to assign short ID");

        let id = Id::ShortIdOrUid(assigned_short_id.get().to_string());

        // Act
        let result = short_ids
            .get(&id)
            .await
            .expect("Failed to get short ID mapping");

        // Assert
        assert!(result.is_some());
        let data = result.unwrap();
        assert_eq!(data.uid, uid);
        assert_eq!(data.short_id, assigned_short_id);
        assert_eq!(data.kind, Kind::Event);
    }

    #[tokio::test]
    async fn short_ids_get_returns_none_for_uid_variant() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let uid = "test-uid-123";
        let id = Id::Uid(uid.to_string());

        // Act
        let result = short_ids
            .get(&id)
            .await
            .expect("Failed to get short ID mapping");

        // Assert
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn short_ids_get_returns_none_for_non_existent_short_id() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let id = Id::ShortIdOrUid("999".to_string()); // Non-existent short ID

        // Act
        let result = short_ids
            .get(&id)
            .await
            .expect("Failed to get short ID mapping");

        // Assert
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn short_ids_get_uid_resolves_short_id_to_uid() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db.clone());
        let uid = "test-uid-todo-1";

        // Assign a short ID
        let assigned_short_id = db
            .short_ids
            .get_or_assign_short_id(uid, Kind::Todo)
            .await
            .expect("Failed to assign short ID");

        let id = Id::ShortIdOrUid(assigned_short_id.get().to_string());

        // Act
        let resolved_uid = short_ids.get_uid(&id).await.expect("Failed to resolve UID");

        // Assert
        assert_eq!(resolved_uid, uid);
    }

    #[tokio::test]
    async fn short_ids_get_uid_returns_uid_string_for_uid_variant() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let uid = "direct-uid-string";
        let id = Id::Uid(uid.to_string());

        // Act
        let resolved_uid = short_ids.get_uid(&id).await.expect("Failed to resolve UID");

        // Assert
        assert_eq!(resolved_uid, uid);
    }

    #[tokio::test]
    async fn short_ids_get_uid_returns_string_for_short_id_or_uid_lookalike() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        // A UUID-like string that could be a UID
        let uid_string = "abc-123-def-456";
        let id = Id::ShortIdOrUid(uid_string.to_string());

        // Act
        let resolved_uid = short_ids.get_uid(&id).await.expect("Failed to resolve UID");

        // Assert
        // Should return the string as-is since it's not a valid short ID
        assert_eq!(resolved_uid, uid_string);
    }

    #[tokio::test]
    async fn short_ids_get_returns_none_for_invalid_short_id_zero() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        // "0" is not a valid NonZeroU32
        let id = Id::ShortIdOrUid("0".to_string());

        // Act
        let result = short_ids
            .get(&id)
            .await
            .expect("Failed to get short ID mapping");

        // Assert
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn event_with_short_id_delegates_short_id() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let event = test_event("event-1", "Test Event");

        // Act
        let wrapped = short_ids
            .event(event)
            .await
            .expect("Failed to wrap event with short ID");

        // Assert
        assert_eq!(wrapped.short_id(), Some(wrapped.short_id));
        assert!(wrapped.short_id.get() > 0);
    }

    #[tokio::test]
    async fn event_with_short_id_delegates_uid() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let uid = "event-delegates-uid";
        let event = test_event(uid, "Test Event");

        // Act
        let wrapped = short_ids
            .event(event)
            .await
            .expect("Failed to wrap event with short ID");

        // Assert
        assert_eq!(wrapped.uid(), Cow::Borrowed(uid));
    }

    #[tokio::test]
    async fn event_with_short_id_delegates_summary() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let summary = "Event Summary Test";
        let event = test_event("event-1", summary);

        // Act
        let wrapped = short_ids
            .event(event)
            .await
            .expect("Failed to wrap event with short ID");

        // Assert
        assert_eq!(wrapped.summary(), Cow::Borrowed(summary));
    }

    #[tokio::test]
    async fn event_with_short_id_delegates_description() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let event = test_event("event-1", "Test");

        // Act
        let wrapped = short_ids
            .event(event)
            .await
            .expect("Failed to wrap event with short ID");

        // Assert
        assert_eq!(
            wrapped.description(),
            Some(Cow::Borrowed("Test description"))
        );
    }

    #[tokio::test]
    async fn event_with_short_id_delegates_status() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let event = test_event("event-1", "Test");

        // Act
        let wrapped = short_ids
            .event(event)
            .await
            .expect("Failed to wrap event with short ID");

        // Assert
        assert_eq!(wrapped.status(), Some(EventStatus::Confirmed));
    }

    #[tokio::test]
    async fn event_with_short_id_delegates_start_end() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let event = test_event("event-1", "Test");

        // Act
        let wrapped = short_ids
            .event(event)
            .await
            .expect("Failed to wrap event with short ID");

        // Assert
        assert!(wrapped.start().is_some());
        assert!(wrapped.end().is_some());
    }

    #[tokio::test]
    async fn todo_with_short_id_delegates_short_id() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let todo = test_todo("todo-1", "Test Todo");

        // Act
        let wrapped = short_ids
            .todo(todo)
            .await
            .expect("Failed to wrap todo with short ID");

        // Assert
        assert_eq!(wrapped.short_id(), Some(wrapped.short_id));
        assert!(wrapped.short_id.get() > 0);
    }

    #[tokio::test]
    async fn todo_with_short_id_delegates_uid() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let uid = "todo-delegates-uid";
        let todo = test_todo(uid, "Test Todo");

        // Act
        let wrapped = short_ids
            .todo(todo)
            .await
            .expect("Failed to wrap todo with short ID");

        // Assert
        assert_eq!(wrapped.uid(), Cow::Borrowed(uid));
    }

    #[tokio::test]
    async fn todo_with_short_id_delegates_summary() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let summary = "Todo Summary Test";
        let todo = test_todo("todo-1", summary);

        // Act
        let wrapped = short_ids
            .todo(todo)
            .await
            .expect("Failed to wrap todo with short ID");

        // Assert
        assert_eq!(wrapped.summary(), Cow::Borrowed(summary));
    }

    #[tokio::test]
    async fn todo_with_short_id_delegates_description() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let todo = test_todo("todo-1", "Test");

        // Act
        let wrapped = short_ids
            .todo(todo)
            .await
            .expect("Failed to wrap todo with short ID");

        // Assert
        assert_eq!(
            wrapped.description(),
            Some(Cow::Borrowed("Test todo description"))
        );
    }

    #[tokio::test]
    async fn todo_with_short_id_delegates_status() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let todo = test_todo("todo-1", "Test");

        // Act
        let wrapped = short_ids
            .todo(todo)
            .await
            .expect("Failed to wrap todo with short ID");

        // Assert
        assert_eq!(wrapped.status(), TodoStatus::NeedsAction);
    }

    #[tokio::test]
    async fn todo_with_short_id_delegates_priority() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let todo = test_todo("todo-1", "Test");

        // Act
        let wrapped = short_ids
            .todo(todo)
            .await
            .expect("Failed to wrap todo with short ID");

        // Assert
        assert_eq!(wrapped.priority(), Priority::P5);
    }

    #[tokio::test]
    async fn todo_with_short_id_delegates_percent_complete() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let todo = test_todo("todo-1", "Test");

        // Act
        let wrapped = short_ids
            .todo(todo)
            .await
            .expect("Failed to wrap todo with short ID");

        // Assert
        assert_eq!(wrapped.percent_complete(), Some(0));
    }

    #[tokio::test]
    async fn todo_with_short_id_delegates_due_and_completed() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let todo = test_todo("todo-1", "Test");

        // Act
        let wrapped = short_ids
            .todo(todo)
            .await
            .expect("Failed to wrap todo with short ID");

        // Assert
        assert!(wrapped.due().is_some());
        assert!(wrapped.completed().is_some());
    }

    #[tokio::test]
    async fn short_ids_events_wraps_multiple_events() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let events = vec![
            test_event("event-1", "Event 1"),
            test_event("event-2", "Event 2"),
            test_event("event-3", "Event 3"),
        ];

        // Act
        let wrapped = short_ids
            .events(events)
            .await
            .expect("Failed to wrap events with short IDs");

        // Assert
        assert_eq!(wrapped.len(), 3);
        let first = wrapped.first().expect("should have first element");
        let second = wrapped.get(1).expect("should have second element");
        let third = wrapped.get(2).expect("should have third element");
        assert!(first.short_id.get() > 0);
        assert!(second.short_id.get() > first.short_id.get());
        assert!(third.short_id.get() > second.short_id.get());
    }

    #[tokio::test]
    async fn short_ids_todos_wraps_multiple_todos() {
        // Arrange
        let db = setup_test_db().await;
        let short_ids = ShortIds::new(db);
        let todos = vec![
            test_todo("todo-1", "Todo 1"),
            test_todo("todo-2", "Todo 2"),
            test_todo("todo-3", "Todo 3"),
        ];

        // Act
        let wrapped = short_ids
            .todos(todos)
            .await
            .expect("Failed to wrap todos with short IDs");

        // Assert
        assert_eq!(wrapped.len(), 3);
        let first = wrapped.first().expect("should have first element");
        let second = wrapped.get(1).expect("should have second element");
        let third = wrapped.get(2).expect("should have third element");
        assert!(first.short_id.get() > 0);
        assert!(second.short_id.get() > first.short_id.get());
        assert!(third.short_id.get() > second.short_id.get());
    }
}