judo 2.0.2

Judo - TUI for ToDo lists
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
use crate::helpers::db::setup_test_db_shared;
use anyhow::Result;
use judo::app::App;
use judo::cli::ops::{add_item, add_list, delete_item, delete_list, toggle_done_item};
use judo::db::config::{Config, DBConfig};
use judo::db::models::{NewTodoList, TodoItem, TodoList};

/// Build a test App backed by a named shared in-memory database.
///
/// Because the connection string uses `cache=shared`, the pools that
/// CLI ops create internally (via `get_db_pool`) will hit the **same**
/// in-memory database as the pool stored on the returned `App`.
async fn setup_test_app() -> Result<App> {
    let (pool, connection_str) = setup_test_db_shared().await?;

    let test_db_config = DBConfig {
        name: "test_db".to_string(),
        connection_str,
    };

    let config = Config {
        default: "test_db".to_string(),
        dbs: vec![test_db_config.clone()],
        colours: Default::default(),
    };

    Ok(App {
        config,
        current_db_config: test_db_config,
        current_screen: judo::app::state::CurrentScreen::Main,
        pool,
        lists_component: judo::ui::components::ListsComponent::new(),
        input_state: judo::ui::components::InputState::new(),
        selected_db_index: 0,
        exit: false,
    })
}

// ===== List Operations Tests =====

#[tokio::test]
async fn test_add_list_default_db() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Shopping List".to_string(), &None).await?;

    let lists = TodoList::get_all(&app.pool).await?;
    assert_eq!(lists.len(), 1);
    assert_eq!(lists[0].name, "Shopping List");

    Ok(())
}

#[tokio::test]
async fn test_add_multiple_lists() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "List 1".to_string(), &None).await?;
    add_list(&app, "List 2".to_string(), &None).await?;
    add_list(&app, "List 3".to_string(), &None).await?;

    let lists = TodoList::get_all(&app.pool).await?;
    assert_eq!(lists.len(), 3);

    let names: Vec<String> = lists.iter().map(|l| l.name.clone()).collect();
    assert!(names.contains(&"List 1".to_string()));
    assert!(names.contains(&"List 2".to_string()));
    assert!(names.contains(&"List 3".to_string()));

    Ok(())
}

#[tokio::test]
async fn test_add_list_with_empty_name() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "".to_string(), &None).await?;

    let lists = TodoList::get_all(&app.pool).await?;
    assert_eq!(lists.len(), 1);
    assert_eq!(lists[0].name, "");

    Ok(())
}

#[tokio::test]
async fn test_add_list_with_special_characters() -> Result<()> {
    let app = setup_test_app().await?;

    let special_name = "Special List! @#$% & *()";
    add_list(&app, special_name.to_string(), &None).await?;

    let lists = TodoList::get_all(&app.pool).await?;
    assert_eq!(lists.len(), 1);
    assert_eq!(lists[0].name, special_name);

    Ok(())
}

#[tokio::test]
async fn test_add_list_with_unicode() -> Result<()> {
    let app = setup_test_app().await?;

    let unicode_name = "🚀 Rocket List 测试";
    add_list(&app, unicode_name.to_string(), &None).await?;

    let lists = TodoList::get_all(&app.pool).await?;
    assert_eq!(lists.len(), 1);
    assert_eq!(lists[0].name, unicode_name);

    Ok(())
}

#[tokio::test]
async fn test_delete_list_by_name() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "To Delete".to_string(), &None).await?;
    assert_eq!(TodoList::get_all(&app.pool).await?.len(), 1);

    delete_list(&app, Some("To Delete".to_string()), None, &None).await?;

    assert_eq!(TodoList::get_all(&app.pool).await?.len(), 0);

    Ok(())
}

#[tokio::test]
async fn test_delete_list_by_id() -> Result<()> {
    let app = setup_test_app().await?;

    let created = TodoList::create(
        &app.pool,
        NewTodoList {
            name: "To Delete".to_string(),
        },
    )
    .await?;

    delete_list(&app, None, Some(created.id), &None).await?;

    assert_eq!(TodoList::get_all(&app.pool).await?.len(), 0);

    Ok(())
}

#[tokio::test]
async fn test_delete_list_keeps_others() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Keep 1".to_string(), &None).await?;
    add_list(&app, "Delete Me".to_string(), &None).await?;
    add_list(&app, "Keep 2".to_string(), &None).await?;

    delete_list(&app, Some("Delete Me".to_string()), None, &None).await?;

    let lists = TodoList::get_all(&app.pool).await?;
    assert_eq!(lists.len(), 2);

    let names: Vec<String> = lists.iter().map(|l| l.name.clone()).collect();
    assert!(names.contains(&"Keep 1".to_string()));
    assert!(names.contains(&"Keep 2".to_string()));
    assert!(!names.contains(&"Delete Me".to_string()));

    Ok(())
}

// ===== Item Operations Tests =====

#[tokio::test]
async fn test_add_item_to_list_by_name() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Shopping".to_string(), &None).await?;
    add_item(
        &app,
        "Buy milk".to_string(),
        &None,
        None,
        Some("Shopping".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    assert_eq!(items.len(), 1);
    assert_eq!(items[0].name, "Buy milk");
    assert!(!items[0].is_done);

    Ok(())
}

#[tokio::test]
async fn test_add_item_to_list_by_id() -> Result<()> {
    let app = setup_test_app().await?;

    let created = TodoList::create(
        &app.pool,
        NewTodoList {
            name: "Tasks".to_string(),
        },
    )
    .await?;

    add_item(
        &app,
        "Complete project".to_string(),
        &None,
        Some(created.id),
        None,
    )
    .await?;

    let items = TodoItem::get_by_list_id(&app.pool, created.id).await?;
    assert_eq!(items.len(), 1);
    assert_eq!(items[0].name, "Complete project");

    Ok(())
}

#[tokio::test]
async fn test_add_multiple_items_to_list() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Work".to_string(), &None).await?;

    for name in ["Item 1", "Item 2", "Item 3"] {
        add_item(
            &app,
            name.to_string(),
            &None,
            None,
            Some("Work".to_string()),
        )
        .await?;
    }

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    assert_eq!(items.len(), 3);

    let names: Vec<String> = items.iter().map(|i| i.name.clone()).collect();
    assert!(names.contains(&"Item 1".to_string()));
    assert!(names.contains(&"Item 2".to_string()));
    assert!(names.contains(&"Item 3".to_string()));

    Ok(())
}

#[tokio::test]
async fn test_add_item_with_special_characters() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Test".to_string(), &None).await?;

    let special_name = "Special! @#$% & *() item";
    add_item(
        &app,
        special_name.to_string(),
        &None,
        None,
        Some("Test".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    assert_eq!(items[0].name, special_name);

    Ok(())
}

#[tokio::test]
async fn test_add_item_with_unicode() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Test".to_string(), &None).await?;

    let unicode_name = "🎉 Party 测试 item";
    add_item(
        &app,
        unicode_name.to_string(),
        &None,
        None,
        Some("Test".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    assert_eq!(items[0].name, unicode_name);

    Ok(())
}

#[tokio::test]
async fn test_delete_item_by_id() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Test".to_string(), &None).await?;
    add_item(
        &app,
        "To Delete".to_string(),
        &None,
        None,
        Some("Test".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    assert_eq!(items.len(), 1);

    delete_item(&app, items[0].id, &None).await?;

    assert_eq!(lists[0].get_all_items(&app.pool).await?.len(), 0);

    Ok(())
}

#[tokio::test]
async fn test_delete_item_keeps_others() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Test".to_string(), &None).await?;
    for name in ["Item 1", "Item 2", "Item 3"] {
        add_item(
            &app,
            name.to_string(),
            &None,
            None,
            Some("Test".to_string()),
        )
        .await?;
    }

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    let id_to_delete = items.iter().find(|i| i.name == "Item 2").unwrap().id;

    delete_item(&app, id_to_delete, &None).await?;

    let remaining = lists[0].get_all_items(&app.pool).await?;
    assert_eq!(remaining.len(), 2);

    let names: Vec<String> = remaining.iter().map(|i| i.name.clone()).collect();
    assert!(names.contains(&"Item 1".to_string()));
    assert!(names.contains(&"Item 3".to_string()));
    assert!(!names.contains(&"Item 2".to_string()));

    Ok(())
}

#[tokio::test]
async fn test_toggle_done_item() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Test".to_string(), &None).await?;
    add_item(
        &app,
        "Toggle Me".to_string(),
        &None,
        None,
        Some("Test".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    let item_id = items[0].id;
    assert!(!items[0].is_done);

    // Toggle to done
    toggle_done_item(&app, item_id, &None).await?;
    let item = TodoItem::get_by_id(&app.pool, item_id).await?.unwrap();
    assert!(item.is_done);

    // Toggle back to not done
    toggle_done_item(&app, item_id, &None).await?;
    let item = TodoItem::get_by_id(&app.pool, item_id).await?.unwrap();
    assert!(!item.is_done);

    Ok(())
}

#[tokio::test]
async fn test_toggle_done_multiple_times() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Test".to_string(), &None).await?;
    add_item(
        &app,
        "Toggle Test".to_string(),
        &None,
        None,
        Some("Test".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    let item_id = items[0].id;

    for i in 0..10 {
        toggle_done_item(&app, item_id, &None).await?;
        let item = TodoItem::get_by_id(&app.pool, item_id).await?.unwrap();
        let expected_done = (i + 1) % 2 == 1;
        assert_eq!(item.is_done, expected_done);
    }

    Ok(())
}

// ===== Edge Cases =====

#[tokio::test]
async fn test_add_item_with_empty_name() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Test".to_string(), &None).await?;
    add_item(&app, "".to_string(), &None, None, Some("Test".to_string())).await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    assert_eq!(items.len(), 1);
    assert_eq!(items[0].name, "");

    Ok(())
}

#[tokio::test]
async fn test_add_item_with_very_long_name() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Test".to_string(), &None).await?;

    let long_name = "A".repeat(1000);
    add_item(
        &app,
        long_name.clone(),
        &None,
        None,
        Some("Test".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    assert_eq!(items[0].name, long_name);

    Ok(())
}

#[tokio::test]
async fn test_delete_list_with_items() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "To Delete".to_string(), &None).await?;
    add_item(
        &app,
        "Item 1".to_string(),
        &None,
        None,
        Some("To Delete".to_string()),
    )
    .await?;
    add_item(
        &app,
        "Item 2".to_string(),
        &None,
        None,
        Some("To Delete".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    assert_eq!(lists[0].get_all_items(&app.pool).await?.len(), 2);

    delete_list(&app, Some("To Delete".to_string()), None, &None).await?;

    assert_eq!(TodoList::get_all(&app.pool).await?.len(), 0);

    Ok(())
}

#[tokio::test]
async fn test_multiple_lists_with_items() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "List 1".to_string(), &None).await?;
    add_list(&app, "List 2".to_string(), &None).await?;

    add_item(
        &app,
        "L1 Item 1".to_string(),
        &None,
        None,
        Some("List 1".to_string()),
    )
    .await?;
    add_item(
        &app,
        "L1 Item 2".to_string(),
        &None,
        None,
        Some("List 1".to_string()),
    )
    .await?;
    add_item(
        &app,
        "L2 Item 1".to_string(),
        &None,
        None,
        Some("List 2".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let list1 = lists.iter().find(|l| l.name == "List 1").unwrap();
    let list2 = lists.iter().find(|l| l.name == "List 2").unwrap();

    assert_eq!(list1.get_all_items(&app.pool).await?.len(), 2);
    assert_eq!(list2.get_all_items(&app.pool).await?.len(), 1);

    Ok(())
}

#[tokio::test]
async fn test_list_isolation() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "List A".to_string(), &None).await?;
    add_list(&app, "List B".to_string(), &None).await?;

    add_item(
        &app,
        "Item A1".to_string(),
        &None,
        None,
        Some("List A".to_string()),
    )
    .await?;
    add_item(
        &app,
        "Item B1".to_string(),
        &None,
        None,
        Some("List B".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let list_a = lists.iter().find(|l| l.name == "List A").unwrap();
    let list_b = lists.iter().find(|l| l.name == "List B").unwrap();

    // Delete item from List A
    let items_a = list_a.get_all_items(&app.pool).await?;
    delete_item(&app, items_a[0].id, &None).await?;

    // Verify List A empty, List B untouched
    assert_eq!(list_a.get_all_items(&app.pool).await?.len(), 0);
    let items_b = list_b.get_all_items(&app.pool).await?;
    assert_eq!(items_b.len(), 1);
    assert_eq!(items_b[0].name, "Item B1");

    Ok(())
}

// ===== Integration / Workflow Tests =====

#[tokio::test]
async fn test_complex_workflow() -> Result<()> {
    let app = setup_test_app().await?;

    // Create a shopping list with items
    add_list(&app, "Shopping".to_string(), &None).await?;
    for name in ["Milk", "Bread", "Eggs"] {
        add_item(
            &app,
            name.to_string(),
            &None,
            None,
            Some("Shopping".to_string()),
        )
        .await?;
    }

    // Mark Milk as done
    let lists = TodoList::get_all(&app.pool).await?;
    let items = lists[0].get_all_items(&app.pool).await?;
    let milk_id = items.iter().find(|i| i.name == "Milk").unwrap().id;
    toggle_done_item(&app, milk_id, &None).await?;

    // Delete Bread
    let bread_id = items.iter().find(|i| i.name == "Bread").unwrap().id;
    delete_item(&app, bread_id, &None).await?;

    // Verify final state
    let final_items = lists[0].get_all_items(&app.pool).await?;
    assert_eq!(final_items.len(), 2);

    let milk = final_items.iter().find(|i| i.name == "Milk").unwrap();
    let eggs = final_items.iter().find(|i| i.name == "Eggs").unwrap();
    assert!(milk.is_done);
    assert!(!eggs.is_done);

    Ok(())
}

#[tokio::test]
async fn test_multiple_operations_on_same_item() -> Result<()> {
    let app = setup_test_app().await?;

    add_list(&app, "Test".to_string(), &None).await?;
    add_item(
        &app,
        "Test Item".to_string(),
        &None,
        None,
        Some("Test".to_string()),
    )
    .await?;

    let lists = TodoList::get_all(&app.pool).await?;
    let item_id = lists[0].get_all_items(&app.pool).await?[0].id;

    // Toggle 3 times: false -> true -> false -> true
    toggle_done_item(&app, item_id, &None).await?;
    toggle_done_item(&app, item_id, &None).await?;
    toggle_done_item(&app, item_id, &None).await?;

    let item = TodoItem::get_by_id(&app.pool, item_id).await?.unwrap();
    assert!(item.is_done);

    Ok(())
}

#[tokio::test]
async fn test_empty_database_operations() -> Result<()> {
    let app = setup_test_app().await?;

    assert_eq!(TodoList::get_all(&app.pool).await?.len(), 0);

    // Add then immediately delete
    add_list(&app, "Temporary".to_string(), &None).await?;
    delete_list(&app, Some("Temporary".to_string()), None, &None).await?;

    assert_eq!(TodoList::get_all(&app.pool).await?.len(), 0);
    Ok(())
}