things3-core 1.0.1

Core library for Things 3 database access and data models
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
use chrono::Utc;
use std::path::Path;
use tempfile::{tempdir, NamedTempFile};
use things3_core::{
    models::{TaskStatus, TaskType},
    ThingsDatabase,
};
use uuid::Uuid;

// Helper function to create test schema and data
#[allow(clippy::too_many_lines)]
async fn create_test_schema(db: &ThingsDatabase) -> Result<(), Box<dyn std::error::Error>> {
    let pool = db.pool();

    // Create the Things 3 schema - matches real database structure
    sqlx::query(
        r"
        -- TMTask table (main tasks table) - matches real Things 3 schema
        CREATE TABLE IF NOT EXISTS TMTask (
            uuid TEXT PRIMARY KEY,
            title TEXT NOT NULL,
            type INTEGER NOT NULL DEFAULT 0,
            status INTEGER NOT NULL DEFAULT 0,
            notes TEXT,
            startDate INTEGER,
            deadline INTEGER,
            stopDate REAL,
            creationDate REAL NOT NULL,
            userModificationDate REAL NOT NULL,
            project TEXT,
            area TEXT,
            heading TEXT,
            trashed INTEGER NOT NULL DEFAULT 0,
            tags TEXT DEFAULT '[]',
            cachedTags BLOB,
            todayIndex INTEGER
        )
        ",
    )
    .execute(pool)
    .await?;

    // Note: Projects are stored in TMTask table with type=1, no separate TMProject table

    sqlx::query(
        r"
        -- TMArea table (areas table) - matches real Things 3 schema
        CREATE TABLE IF NOT EXISTS TMArea (
            uuid TEXT PRIMARY KEY,
            title TEXT NOT NULL,
            visible INTEGER NOT NULL DEFAULT 1,
            'index' INTEGER NOT NULL DEFAULT 0
        )
        ",
    )
    .execute(pool)
    .await?;

    // Insert test data
    // Use a safe conversion for timestamp to avoid precision loss
    let timestamp_i64 = Utc::now().timestamp();
    let now_timestamp = if timestamp_i64 <= i64::from(i32::MAX) {
        f64::from(i32::try_from(timestamp_i64).unwrap_or(0))
    } else {
        // For very large timestamps, use a reasonable test value
        1_700_000_000.0 // Represents a date around 2023
    };
    let area_uuid = Uuid::new_v4().to_string();
    let project_uuid = Uuid::new_v4().to_string();
    let inbox_task_uuid = Uuid::new_v4().to_string();
    let project_task_uuid = Uuid::new_v4().to_string();

    // Insert test area
    sqlx::query("INSERT INTO TMArea (uuid, title, visible, 'index') VALUES (?, ?, ?, ?)")
        .bind(&area_uuid)
        .bind("Work")
        .bind(1) // Visible
        .bind(0) // Index
        .execute(pool)
        .await?;

    // Insert test project (as TMTask with type=1)
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, area, creationDate, userModificationDate, trashed) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind(&project_uuid)
    .bind("Website Redesign")
    .bind(1) // Project type
    .bind(0) // Incomplete
    .bind(&area_uuid)
    .bind(now_timestamp)
    .bind(now_timestamp)
    .bind(0) // Not trashed
    .execute(pool).await?;

    // Insert inbox task (no project) with today's date
    // Convert Unix timestamp to Things 3 format (seconds since 2001-01-01)
    let base_2001 = chrono::DateTime::parse_from_rfc3339("2001-01-01T00:00:00Z")
        .unwrap()
        .timestamp();
    let today_things3 = Utc::now().timestamp() - base_2001;
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, project, area, creationDate, userModificationDate, startDate, trashed, todayIndex) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind(&inbox_task_uuid)
    .bind("Inbox Task")
    .bind(0) // Todo type
    .bind(0) // Incomplete
    .bind::<Option<String>>(None) // No project (inbox)
    .bind(&area_uuid) // Has area
    .bind(now_timestamp)
    .bind(now_timestamp)
    .bind(today_things3)
    .bind(0) // Not trashed
    .bind(1) // todayIndex = 1 (appears in Today)
    .execute(pool).await?;

    // Insert project task with today's date
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, project, area, creationDate, userModificationDate, startDate, trashed, todayIndex) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind(&project_task_uuid)
    .bind("Research competitors")
    .bind(0) // Todo type
    .bind(0) // Incomplete
    .bind(&project_uuid)
    .bind(&area_uuid) // Has area
    .bind(now_timestamp)
    .bind(now_timestamp)
    .bind(today_things3)
    .bind(0) // Not trashed
    .bind(2) // todayIndex = 2 (appears in Today)
    .execute(pool).await?;

    Ok(())
}

#[tokio::test]
async fn test_database_new() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    // Test that we can create a database connection
    assert!(db.is_connected().await);
}

// Removed test_database_with_config - method no longer exists

// Removed test_database_with_default_path - methods no longer exist

#[tokio::test]
async fn test_database_default_path() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    // Test that we can create a database connection with in-memory database
    assert!(db.is_connected().await);
}

#[tokio::test]
async fn test_get_inbox() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    let inbox = db.get_inbox(None).await.unwrap();

    // Should have 1 inbox task from test data
    assert_eq!(inbox.len(), 1);

    // Verify task properties
    let first_task = &inbox[0];
    assert_eq!(first_task.title, "Inbox Task");
    assert_eq!(first_task.status, TaskStatus::Incomplete);
    assert_eq!(first_task.task_type, TaskType::Todo);
}

#[tokio::test]
async fn test_get_today() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    let today = db.get_today(None).await.unwrap();

    // Should have tasks for today
    assert!(!today.is_empty());
}

#[tokio::test]
async fn test_get_projects() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    let projects = db.get_projects(None).await.unwrap();

    // Should have projects from test data
    assert!(!projects.is_empty());

    // Verify project properties
    let first_project = &projects[0];
    assert_eq!(first_project.title, "Website Redesign");
    assert_eq!(first_project.status, TaskStatus::Incomplete);
}

#[tokio::test]
async fn test_get_areas() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    let areas = db.get_areas().await.unwrap();

    // Should have areas from test data
    assert!(!areas.is_empty());

    // Verify area properties
    let first_area = &areas[0];
    assert_eq!(first_area.title, "Work");
}

#[tokio::test]
async fn test_search_tasks() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    let results = db.search_tasks("competitors").await.unwrap();

    // Should find tasks containing "competitors"
    assert!(!results.is_empty());

    // Verify search results contain the search term
    let found_task = results.iter().find(|t| t.title.contains("competitors"));
    assert!(found_task.is_some());
}

#[tokio::test]
async fn test_search_tasks_empty_query() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();
    let results = db.search_tasks("").await.unwrap();

    // Empty query should return all tasks
    assert!(!results.is_empty());
}

#[tokio::test]
async fn test_search_tasks_no_results() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();
    let results = db.search_tasks("nonexistent").await.unwrap();

    // Should return empty results for non-matching query
    assert!(results.is_empty());
}

#[tokio::test]
async fn test_database_error_handling() {
    // Test with invalid path
    let invalid_path = Path::new("/invalid/path/that/does/not/exist/database.sqlite");
    let result = ThingsDatabase::new(Path::new(invalid_path));
    assert!(result.await.is_err());
}

#[tokio::test]
async fn test_database_connection_persistence() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    // Test that we can create multiple connections to the same in-memory database
    let db2 = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data for the second database
    create_test_schema(&db2).await.unwrap();

    // Both should work independently
    let _inbox1 = db.get_inbox(None).await.unwrap();
    let _inbox2 = db2.get_inbox(None).await.unwrap();
    // In-memory databases are independent, so they may have different data
    // Both databases should have non-negative lengths (always true for usize)
    // This test verifies that both databases are independent and functional
}

#[tokio::test]
async fn test_database_with_mock_data_consistency() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    // Test that all mock data is accessible
    let inbox = db.get_inbox(None).await.unwrap();
    let projects = db.get_projects(None).await.unwrap();
    let areas = db.get_areas().await.unwrap();

    // Verify we have the expected number of items
    assert_eq!(inbox.len(), 1); // 1 inbox task
    assert_eq!(projects.len(), 1); // 1 mock project
    assert_eq!(areas.len(), 1); // 1 mock area

    // Verify task relationships (check all tasks, not just inbox)
    let all_tasks = db.search_tasks("").await.unwrap();

    // Verify we have the expected number of total tasks
    assert_eq!(all_tasks.len(), 2); // 1 inbox + 1 project task

    // Verify task-area relationships (projects have areas)
    assert_eq!(
        all_tasks.iter().filter(|t| t.area_uuid.is_some()).count(),
        2
    ); // 2 projects have areas

    // Verify that we have the expected task types
    assert_eq!(
        all_tasks
            .iter()
            .filter(|t| t.task_type == TaskType::Todo)
            .count(),
        2
    ); // 2 todo tasks
}

#[tokio::test]
async fn test_database_query_consistency() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    // Test that different query methods return consistent results
    let inbox = db.get_inbox(None).await.unwrap();
    let all_tasks = db.search_tasks("").await.unwrap();

    // Search should return more tasks than inbox (includes projects)
    assert!(all_tasks.len() >= inbox.len());

    // Verify that inbox tasks are a subset of all tasks
    for inbox_task in &inbox {
        let found = all_tasks.iter().any(|t| t.uuid == inbox_task.uuid);
        assert!(
            found,
            "Inbox task {} not found in all tasks",
            inbox_task.uuid
        );
    }
}

#[tokio::test]
async fn test_database_date_filtering() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    // Test today's tasks
    let today = db.get_today(None).await.unwrap();

    // All today's tasks should have start_date or deadline today
    let today_date = Utc::now().date_naive();
    for task in &today {
        let is_today = (task.start_date == Some(today_date)) || (task.deadline == Some(today_date));
        assert!(is_today, "Task {} is not for today", task.title);
    }
}

#[tokio::test]
async fn test_database_error_recovery() {
    let temp_dir = tempdir().unwrap();
    let _db_path = temp_dir.path().join("test.db");

    // Create a valid database first
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    // Test that operations work
    let inbox = db.get_inbox(None).await.unwrap();
    assert!(!inbox.is_empty());

    // Test that we can still access the database after operations
    let projects = db.get_projects(None).await.unwrap();
    assert!(!projects.is_empty());
}

#[tokio::test]
async fn test_database_concurrent_access() {
    let temp_dir = tempdir().unwrap();
    let _db_path = temp_dir.path().join("test.db");

    // Create multiple database connections
    let db1 = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let db2 = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data for both
    create_test_schema(&db1).await.unwrap();
    create_test_schema(&db2).await.unwrap();

    // Both should be able to read concurrently
    let inbox1 = db1.get_inbox(None).await.unwrap();
    let inbox2 = db2.get_inbox(None).await.unwrap();

    // Both should have the same number of tasks (both have same test data)
    assert_eq!(inbox1.len(), inbox2.len());
    assert_eq!(inbox1.len(), 1); // We have 1 inbox task

    // Both should return the same task titles (UUIDs will be different since they're separate databases)
    assert_eq!(inbox1[0].title, inbox2[0].title);
    assert_eq!(inbox1[0].title, "Inbox Task");
}

#[tokio::test]
async fn test_database_helper_functions_indirectly() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    // Test convert_task_type indirectly through get_inbox
    let tasks = db.get_inbox(Some(1)).await.unwrap();
    if !tasks.is_empty() {
        let task = &tasks[0];
        // Verify task types are properly converted
        assert!(matches!(
            task.task_type,
            things3_core::models::TaskType::Todo
                | things3_core::models::TaskType::Project
                | things3_core::models::TaskType::Heading
                | things3_core::models::TaskType::Area
        ));
    }

    // Test convert_task_status indirectly
    let tasks = db.get_inbox(None).await.unwrap();
    for task in &tasks {
        assert!(matches!(
            task.status,
            things3_core::models::TaskStatus::Incomplete
                | things3_core::models::TaskStatus::Completed
                | things3_core::models::TaskStatus::Canceled
                | things3_core::models::TaskStatus::Trashed
        ));
    }

    // Test convert_timestamp indirectly
    for task in &tasks {
        assert!(task.created <= chrono::Utc::now());
        assert!(task.modified <= chrono::Utc::now());
    }

    // Test convert_date indirectly through tasks with dates
    for task in &tasks {
        if let Some(start_date) = task.start_date {
            // Verify dates are reasonable (not in the far future or past)
            let now = chrono::Utc::now().date_naive();
            let year_ago = now - chrono::Duration::days(365);
            let year_from_now = now + chrono::Duration::days(365);

            assert!(start_date >= year_ago);
            assert!(start_date <= year_from_now);
        }
    }

    // Test convert_uuid indirectly
    for task in &tasks {
        // Verify UUIDs are valid
        assert!(!task.uuid.is_nil());
    }
}

#[tokio::test]
async fn test_database_error_handling_comprehensive() {
    // Test with invalid database path
    let invalid_path = "/invalid/path/that/does/not/exist/database.sqlite";
    let result = ThingsDatabase::new(Path::new(invalid_path));
    assert!(result.await.is_err());

    // Test with valid path but invalid database file
    let temp_file = NamedTempFile::new().unwrap();
    let db_path = temp_file.path();

    // Create an empty file (not a valid SQLite database)
    std::fs::write(db_path, "not a database").unwrap();

    let result = ThingsDatabase::new(db_path).await;
    // The database might still open successfully even with invalid content
    // or it might fail - both are valid test cases
    let _ = result;
}

#[tokio::test]
async fn test_database_edge_cases() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    // Test search with empty string - should return all tasks
    let empty_results = db.search_tasks("").await.unwrap();
    assert_eq!(empty_results.len(), 2); // We have 2 tasks in our test data

    // Test search with very long query
    let long_query = "a".repeat(1000);
    let long_results = db.search_tasks(&long_query).await.unwrap();
    // Should not panic and return empty results
    assert!(long_results.is_empty() || !long_results.is_empty());

    // Test limit edge cases
    let tasks = db.get_inbox(Some(0)).await.unwrap();
    assert_eq!(tasks.len(), 0);

    let tasks = db.get_inbox(Some(1)).await.unwrap();
    assert!(tasks.len() <= 1);

    // Test today with limit
    let today_tasks = db.get_today(Some(0)).await.unwrap();
    assert_eq!(today_tasks.len(), 0);
}

// Removed test_database_with_malformed_data - uses rusqlite which is not available

#[tokio::test]
async fn test_database_performance_with_large_limits() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();

    // Create schema and insert test data
    create_test_schema(&db).await.unwrap();

    // Test with very large limit (should not cause issues)
    let tasks = db.get_inbox(Some(10000)).await.unwrap();
    assert!(tasks.len() <= 10000);

    let tasks = db.get_today(Some(10000)).await.unwrap();
    assert!(tasks.len() <= 10000);

    let tasks = db.search_tasks("test").await.unwrap();
    assert!(tasks.len() <= 10000);
}

// ============================================================================
// Comprehensive get_today Tests with todayIndex Variations
// ============================================================================

/// Helper function to create a minimal TMTask schema for testing
async fn create_minimal_task_schema(pool: &sqlx::SqlitePool) {
    sqlx::query(
        r"
        CREATE TABLE IF NOT EXISTS TMTask (
            uuid TEXT PRIMARY KEY,
            title TEXT NOT NULL,
            type INTEGER NOT NULL DEFAULT 0,
            status INTEGER NOT NULL DEFAULT 0,
            notes TEXT,
            startDate INTEGER,
            deadline INTEGER,
            stopDate REAL,
            creationDate REAL NOT NULL,
            userModificationDate REAL NOT NULL,
            project TEXT,
            area TEXT,
            heading TEXT,
            trashed INTEGER NOT NULL DEFAULT 0,
            cachedTags BLOB,
            todayIndex INTEGER
        )
        ",
    )
    .execute(pool)
    .await
    .unwrap();
}

#[tokio::test]
async fn test_get_today_with_null_today_index() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let pool = db.pool();

    // Create schema
    create_minimal_task_schema(pool).await;

    let now = 1_700_000_000.0;

    // Insert task with NULL todayIndex (should NOT appear in Today)
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, creationDate, userModificationDate, trashed, todayIndex) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind("task-null-today")
    .bind("Task with NULL todayIndex")
    .bind(0)
    .bind(0)
    .bind(now)
    .bind(now)
    .bind(0)
    .bind(Option::<i64>::None) // NULL todayIndex
    .execute(pool)
    .await
    .unwrap();

    let today_tasks = db.get_today(None).await.unwrap();
    assert_eq!(
        today_tasks.len(),
        0,
        "Tasks with NULL todayIndex should not appear in Today"
    );
}

#[tokio::test]
async fn test_get_today_with_zero_today_index() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let pool = db.pool();

    // Create schema
    create_minimal_task_schema(pool).await;

    let now = 1_700_000_000.0;

    // Insert task with todayIndex = 0 (should NOT appear in Today)
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, creationDate, userModificationDate, trashed, todayIndex) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind("task-zero-today")
    .bind("Task with zero todayIndex")
    .bind(0)
    .bind(0)
    .bind(now)
    .bind(now)
    .bind(0)
    .bind(0) // todayIndex = 0
    .execute(pool)
    .await
    .unwrap();

    let today_tasks = db.get_today(None).await.unwrap();
    assert_eq!(
        today_tasks.len(),
        0,
        "Tasks with todayIndex = 0 should not appear in Today"
    );
}

#[tokio::test]
async fn test_get_today_with_positive_today_index() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let pool = db.pool();

    // Create schema
    create_minimal_task_schema(pool).await;

    let now = 1_700_000_000.0;

    // Insert task with positive todayIndex (SHOULD appear in Today)
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, creationDate, userModificationDate, trashed, todayIndex) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind("task-positive-today")
    .bind("Task in Today")
    .bind(0)
    .bind(0)
    .bind(now)
    .bind(now)
    .bind(0)
    .bind(1) // todayIndex = 1
    .execute(pool)
    .await
    .unwrap();

    let today_tasks = db.get_today(None).await.unwrap();
    assert_eq!(
        today_tasks.len(),
        1,
        "Tasks with positive todayIndex should appear in Today"
    );
    assert_eq!(today_tasks[0].title, "Task in Today");
}

#[tokio::test]
async fn test_get_today_excludes_trashed() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let pool = db.pool();

    // Create schema
    create_minimal_task_schema(pool).await;

    let now = 1_700_000_000.0;

    // Insert trashed task with positive todayIndex
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, creationDate, userModificationDate, trashed, todayIndex) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind("task-trashed")
    .bind("Trashed Task")
    .bind(0)
    .bind(0)
    .bind(now)
    .bind(now)
    .bind(1) // trashed = 1
    .bind(1) // todayIndex = 1
    .execute(pool)
    .await
    .unwrap();

    let today_tasks = db.get_today(None).await.unwrap();
    assert_eq!(
        today_tasks.len(),
        0,
        "Trashed tasks should not appear in Today"
    );
}

#[tokio::test]
async fn test_get_today_with_limit() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let pool = db.pool();

    // Create schema
    create_minimal_task_schema(pool).await;

    let now = 1_700_000_000.0;

    // Insert 5 tasks
    for i in 1..=5 {
        sqlx::query(
            "INSERT INTO TMTask (uuid, title, type, status, creationDate, userModificationDate, trashed, todayIndex) 
             VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
        )
        .bind(format!("task-{}", i))
        .bind(format!("Task {}", i))
        .bind(0)
        .bind(0)
        .bind(now)
        .bind(now)
        .bind(0)
        .bind(i as i64)
        .execute(pool)
        .await
        .unwrap();
    }

    // Test with limit
    let today_tasks = db.get_today(Some(3)).await.unwrap();
    assert_eq!(today_tasks.len(), 3, "Should respect limit parameter");
}

// ============================================================================
// Comprehensive get_inbox Error Scenario Tests
// ============================================================================

#[tokio::test]
async fn test_get_inbox_empty_database() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let pool = db.pool();

    // Create schema but insert no data
    create_minimal_task_schema(pool).await;

    let inbox = db.get_inbox(None).await.unwrap();
    assert_eq!(inbox.len(), 0, "Empty database should return empty inbox");
}

#[tokio::test]
async fn test_get_inbox_excludes_tasks_with_project() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let pool = db.pool();

    // Create schema
    create_minimal_task_schema(pool).await;

    let now = 1_700_000_000.0;

    // Insert task with project (should NOT be in inbox)
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, creationDate, userModificationDate, project, trashed) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind("task-with-project")
    .bind("Task in Project")
    .bind(0)
    .bind(0)
    .bind(now)
    .bind(now)
    .bind("project-uuid")
    .bind(0)
    .execute(pool)
    .await
    .unwrap();

    // Insert task without project (SHOULD be in inbox)
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, creationDate, userModificationDate, project, trashed) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind("task-inbox")
    .bind("Inbox Task")
    .bind(0)
    .bind(0)
    .bind(now)
    .bind(now)
    .bind(Option::<String>::None)
    .bind(0)
    .execute(pool)
    .await
    .unwrap();

    let inbox = db.get_inbox(None).await.unwrap();
    assert_eq!(
        inbox.len(),
        1,
        "Only tasks without project should be in inbox"
    );
    assert_eq!(inbox[0].title, "Inbox Task");
}

#[tokio::test]
async fn test_get_inbox_with_limit_zero() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let pool = db.pool();

    // Create schema
    create_minimal_task_schema(pool).await;

    let now = 1_700_000_000.0;

    // Insert task
    sqlx::query(
        "INSERT INTO TMTask (uuid, title, type, status, creationDate, userModificationDate, project, trashed) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    )
    .bind("task-1")
    .bind("Task 1")
    .bind(0)
    .bind(0)
    .bind(now)
    .bind(now)
    .bind(Option::<String>::None)
    .bind(0)
    .execute(pool)
    .await
    .unwrap();

    let inbox = db.get_inbox(Some(0)).await.unwrap();
    assert_eq!(inbox.len(), 0, "Limit of 0 should return no tasks");
}

#[tokio::test]
async fn test_get_inbox_large_result_set() {
    let db = ThingsDatabase::from_connection_string("sqlite::memory:")
        .await
        .unwrap();
    let pool = db.pool();

    // Create schema
    create_minimal_task_schema(pool).await;

    let now = 1_700_000_000.0;

    // Insert 100 tasks
    for i in 0..100 {
        sqlx::query(
            "INSERT INTO TMTask (uuid, title, type, status, creationDate, userModificationDate, project, trashed) 
             VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
        )
        .bind(format!("task-{}", i))
        .bind(format!("Task {}", i))
        .bind(0)
        .bind(0)
        .bind(now)
        .bind(now)
        .bind(Option::<String>::None)
        .bind(0)
        .execute(pool)
        .await
        .unwrap();
    }

    let inbox = db.get_inbox(None).await.unwrap();
    assert_eq!(inbox.len(), 100, "Should handle large result sets");

    let limited_inbox = db.get_inbox(Some(10)).await.unwrap();
    assert_eq!(
        limited_inbox.len(),
        10,
        "Should respect limit with large datasets"
    );
}