things3-cli 1.0.0

CLI tool for Things 3 with integrated MCP server
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
//! MCP lifecycle operation integration tests

use serde_json::{json, Value};
use things3_cli::mcp::test_harness::McpTestHarness;
use uuid::Uuid;

// Helper to create harness (non-async)
fn create_harness() -> McpTestHarness {
    McpTestHarness::new()
}

// Helper to parse CallToolResult into JSON Value
fn parse_tool_result(result: &things3_cli::mcp::CallToolResult) -> Value {
    if result.is_error {
        return json!({"error": "Tool call failed"});
    }

    match &result.content[0] {
        things3_cli::mcp::Content::Text { text } => {
            serde_json::from_str(text).unwrap_or(json!({"text": text}))
        }
    }
}

// Helper function to create a task via MCP
async fn create_task_via_mcp(harness: &McpTestHarness) -> String {
    let result = harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": "Test Task",
                "notes": "Test notes"
            })),
        )
        .await;

    let response = parse_tool_result(&result);
    response["uuid"].as_str().unwrap().to_string()
}

// ============================================================================
// MCP Tool Tests (12 tests)
// ============================================================================

#[tokio::test]
async fn test_complete_task_tool() {
    let harness = create_harness();

    // Create a task
    let uuid = create_task_via_mcp(&harness).await;

    // Complete it via MCP
    let result = harness
        .call_tool(
            "complete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;
    let response = parse_tool_result(&result);

    assert!(
        response.get("message").is_some(),
        "Response should contain message"
    );
    assert_eq!(
        response["message"], "Task completed successfully",
        "Should return success message"
    );
    assert_eq!(response["uuid"], uuid, "Should return the task UUID");
}

#[tokio::test]
async fn test_complete_task_tool_response_format() {
    let harness = create_harness();

    // Create a task
    let uuid = create_task_via_mcp(&harness).await;

    // Complete it
    let result = harness
        .call_tool(
            "complete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;
    let response = parse_tool_result(&result);

    // Verify response structure
    assert!(response.is_object(), "Response should be a JSON object");
    assert!(response.get("message").is_some());
    assert!(response.get("uuid").is_some());
    assert!(!response["message"].as_str().unwrap().is_empty());
}

#[tokio::test]
async fn test_uncomplete_task_tool() {
    let harness = create_harness();

    // Create and complete a task
    let uuid = create_task_via_mcp(&harness).await;
    harness
        .call_tool(
            "complete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;

    // Uncomplete it via MCP
    let result = harness
        .call_tool(
            "uncomplete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;
    let response = parse_tool_result(&result);

    assert!(
        response.get("message").is_some(),
        "Response should contain message"
    );
    assert_eq!(
        response["message"], "Task marked as incomplete successfully",
        "Should return success message"
    );
}

#[tokio::test]
async fn test_delete_task_tool_error_mode() {
    let harness = create_harness();

    // Create a parent task
    let parent_result = harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": "Parent Task"
            })),
        )
        .await;
    let parent_response = parse_tool_result(&parent_result);
    let parent_uuid = parent_response["uuid"].as_str().unwrap();

    // Create a child task
    harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": "Child Task",
                "parent_uuid": parent_uuid
            })),
        )
        .await;

    // Try to delete parent with error mode (default)
    let delete_response = harness
        .call_tool_with_fallback(
            "delete_task",
            Some(json!({
                "uuid": parent_uuid,
                "child_handling": "error"
            })),
        )
        .await;

    // Should return an error
    assert!(
        delete_response.is_error,
        "Should fail when parent has children in error mode"
    );
}

#[tokio::test]
async fn test_delete_task_tool_cascade_mode() {
    let harness = create_harness();

    // Create a parent task
    let parent_result = harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": "Parent Task"
            })),
        )
        .await;
    let parent_response = parse_tool_result(&parent_result);
    let parent_uuid = parent_response["uuid"].as_str().unwrap();

    // Create a child task
    let child_result = harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": "Child Task",
                "parent_uuid": parent_uuid
            })),
        )
        .await;
    let child_response = parse_tool_result(&child_result);
    let child_uuid = child_response["uuid"].as_str().unwrap();

    // Delete parent with cascade mode
    let delete_result = harness
        .call_tool(
            "delete_task",
            Some(json!({
                "uuid": parent_uuid,
                "child_handling": "cascade"
            })),
        )
        .await;
    let delete_response = parse_tool_result(&delete_result);

    assert_eq!(
        delete_response["message"], "Task deleted successfully",
        "Should successfully delete with cascade"
    );

    // Verify both are deleted by searching
    let search_result = harness
        .call_tool(
            "search_tasks",
            Some(json!({
                "query": parent_uuid
            })),
        )
        .await;
    let search_results = parse_tool_result(&search_result);

    // Parent should not be found
    assert!(
        search_results["tasks"]
            .as_array()
            .map(|a| a.as_slice())
            .unwrap_or(&[])
            .is_empty(),
        "Parent should be deleted"
    );

    let child_search_result = harness
        .call_tool(
            "search_tasks",
            Some(json!({
                "query": child_uuid
            })),
        )
        .await;
    let child_search = parse_tool_result(&child_search_result);

    assert!(
        child_search["tasks"]
            .as_array()
            .map(|a| a.as_slice())
            .unwrap_or(&[])
            .is_empty(),
        "Child should be deleted in cascade mode"
    );
}

#[tokio::test]
async fn test_delete_task_tool_orphan_mode() {
    let harness = create_harness();

    // Create a parent task
    let parent_result = harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": "Parent Task For Orphan Test"
            })),
        )
        .await;
    let parent_response = parse_tool_result(&parent_result);
    let parent_uuid = parent_response["uuid"].as_str().unwrap();

    // Create a child task
    harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": "Child Task For Orphan Test",
                "parent_uuid": parent_uuid
            })),
        )
        .await;

    // Delete parent with orphan mode - should succeed even with children
    let delete_result = harness
        .call_tool(
            "delete_task",
            Some(json!({
                "uuid": parent_uuid,
                "child_handling": "orphan"
            })),
        )
        .await;
    let delete_response = parse_tool_result(&delete_result);

    // The key test: orphan mode should allow deletion of parent with children
    assert_eq!(
        delete_response["message"], "Task deleted successfully",
        "Should successfully delete parent with orphan mode even when children exist"
    );
}

#[tokio::test]
async fn test_complete_task_invalid_uuid() {
    let harness = create_harness();

    // Try to complete with invalid UUID
    let response = harness
        .call_tool_with_fallback(
            "complete_task",
            Some(json!({
                "uuid": "not-a-valid-uuid"
            })),
        )
        .await;

    assert!(response.is_error, "Should return error for invalid UUID");
}

#[tokio::test]
async fn test_delete_task_missing_uuid() {
    let harness = create_harness();

    // Try to delete without UUID
    let response = harness
        .call_tool_with_fallback("delete_task", Some(json!({})))
        .await;

    assert!(response.is_error, "Should return error for missing UUID");
}

#[tokio::test]
async fn test_delete_task_invalid_child_handling() {
    let harness = create_harness();

    // Create a task
    let uuid = create_task_via_mcp(&harness).await;

    // Delete with invalid child_handling value (should default to error mode)
    let result = harness
        .call_tool(
            "delete_task",
            Some(json!({
                "uuid": uuid,
                "child_handling": "invalid_mode"
            })),
        )
        .await;
    let response = parse_tool_result(&result);

    // Should still succeed (invalid value defaults to error mode)
    assert_eq!(
        response["message"], "Task deleted successfully",
        "Should default to error mode for invalid child_handling"
    );
}

#[tokio::test]
async fn test_lifecycle_e2e_flow() {
    let harness = create_harness();

    // Create task
    let create_result = harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": "E2E Lifecycle Task",
                "notes": "Testing full lifecycle"
            })),
        )
        .await;
    let create_response = parse_tool_result(&create_result);
    let uuid = create_response["uuid"].as_str().unwrap().to_string();

    // Update task
    let update_result = harness
        .call_tool(
            "update_task",
            Some(json!({
                "uuid": uuid,
                "notes": "Updated notes"
            })),
        )
        .await;
    let update_response = parse_tool_result(&update_result);
    assert_eq!(update_response["message"], "Task updated successfully");

    // Complete task
    let complete_result = harness
        .call_tool(
            "complete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;
    let complete_response = parse_tool_result(&complete_result);
    assert_eq!(complete_response["message"], "Task completed successfully");

    // Uncomplete task
    let uncomplete_result = harness
        .call_tool(
            "uncomplete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;
    let uncomplete_response = parse_tool_result(&uncomplete_result);
    assert_eq!(
        uncomplete_response["message"],
        "Task marked as incomplete successfully"
    );

    // Delete task
    let delete_result = harness
        .call_tool(
            "delete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;
    let delete_response = parse_tool_result(&delete_result);
    assert_eq!(delete_response["message"], "Task deleted successfully");

    // Verify task is gone
    let search_result = harness
        .call_tool(
            "search_tasks",
            Some(json!({
                "query": uuid
            })),
        )
        .await;
    let search_response = parse_tool_result(&search_result);
    assert!(search_response["tasks"]
        .as_array()
        .map(|a| a.as_slice())
        .unwrap_or(&[])
        .is_empty());
}

#[tokio::test]
async fn test_task_not_in_inbox_after_completion() {
    let harness = create_harness();

    // Create a task
    let uuid = create_task_via_mcp(&harness).await;

    // Get inbox before completion
    let inbox_before_result = harness.call_tool("get_inbox", None).await;
    let inbox_before = parse_tool_result(&inbox_before_result);
    let tasks_before: Vec<Value> = inbox_before["tasks"]
        .as_array()
        .unwrap_or(&Vec::new())
        .clone();

    // Complete the task
    harness
        .call_tool(
            "complete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;

    // Get inbox after completion
    let inbox_after_result = harness.call_tool("get_inbox", None).await;
    let inbox_after = parse_tool_result(&inbox_after_result);
    let tasks_after: Vec<Value> = inbox_after["tasks"]
        .as_array()
        .unwrap_or(&Vec::new())
        .clone();

    // Completed task should not be in inbox (inbox shows incomplete tasks)
    assert!(
        tasks_after.len() <= tasks_before.len(),
        "Inbox should have same or fewer tasks after completion"
    );

    // Verify our specific task is not in the inbox
    let found_in_inbox = tasks_after
        .iter()
        .any(|t| t["uuid"].as_str() == Some(&uuid));
    assert!(!found_in_inbox, "Completed task should not appear in inbox");
}

#[tokio::test]
async fn test_task_not_in_queries_after_deletion() {
    let harness = create_harness();

    // Create a task with unique title
    let unique_title = format!("Unique Task {}", Uuid::new_v4());
    let create_result = harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": unique_title
            })),
        )
        .await;
    let create_response = parse_tool_result(&create_result);
    let uuid = create_response["uuid"].as_str().unwrap().to_string();

    // Delete the task
    let delete_result = harness
        .call_tool(
            "delete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;
    let delete_response = parse_tool_result(&delete_result);
    assert_eq!(
        delete_response["message"], "Task deleted successfully",
        "Delete operation should succeed"
    );

    // Verify task no longer appears in search
    let search_after_result = harness
        .call_tool(
            "search_tasks",
            Some(json!({
                "query": unique_title
            })),
        )
        .await;
    let search_after = parse_tool_result(&search_after_result);
    assert!(
        search_after["tasks"]
            .as_array()
            .map(|a| a.as_slice())
            .unwrap_or(&[])
            .is_empty(),
        "Deleted task should not appear in search results"
    );

    // Verify task no longer in inbox
    let inbox_result = harness.call_tool("get_inbox", None).await;
    let inbox = parse_tool_result(&inbox_result);
    let found_in_inbox = inbox["tasks"]
        .as_array()
        .map(|a| a.as_slice())
        .unwrap_or(&[])
        .iter()
        .any(|t| t["uuid"].as_str() == Some(&uuid));
    assert!(!found_in_inbox, "Deleted task should not appear in inbox");
}

// ============================================================================
// Error Handling Tests (3 tests)
// ============================================================================

#[tokio::test]
async fn test_mcp_error_propagation() {
    let harness = create_harness();

    // Try to complete a nonexistent task
    let nonexistent_uuid = Uuid::new_v4().to_string();
    let response = harness
        .call_tool_with_fallback(
            "complete_task",
            Some(json!({
                "uuid": nonexistent_uuid
            })),
        )
        .await;

    // Should propagate database error
    assert!(
        response.is_error,
        "Should propagate error for nonexistent task"
    );
}

#[tokio::test]
async fn test_mcp_validation_errors() {
    let harness = create_harness();

    // Test missing required parameter
    let response1 = harness
        .call_tool_with_fallback("complete_task", Some(json!({})))
        .await;
    assert!(response1.is_error, "Should return error for missing uuid");

    // Test invalid UUID format
    let response2 = harness
        .call_tool_with_fallback(
            "complete_task",
            Some(json!({
                "uuid": "not-a-uuid"
            })),
        )
        .await;
    assert!(
        response2.is_error,
        "Should return error for invalid UUID format"
    );
}

#[tokio::test]
async fn test_mcp_concurrent_calls() {
    let harness = create_harness();

    // Create multiple tasks
    let mut uuids = Vec::new();
    for i in 0..5 {
        let result = harness
            .call_tool(
                "create_task",
                Some(json!({
                    "title": format!("Concurrent Task {}", i)
                })),
            )
            .await;
        let response = parse_tool_result(&result);
        uuids.push(response["uuid"].as_str().unwrap().to_string());
    }

    // Complete all tasks sequentially (test harness doesn't support concurrent access)
    for uuid in uuids {
        let result = harness
            .call_tool(
                "complete_task",
                Some(json!({
                    "uuid": uuid
                })),
            )
            .await;
        let response = parse_tool_result(&result);
        assert_eq!(response["message"], "Task completed successfully");
    }
}

// ============================================================================
// Integration Tests (3 tests)
// ============================================================================

#[tokio::test]
async fn test_complete_task_appears_in_logbook() {
    let harness = create_harness();

    // Create a task
    let uuid = create_task_via_mcp(&harness).await;

    // Complete the task
    harness
        .call_tool(
            "complete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;

    // Search for the task by UUID - it should still be found (not trashed)
    let search_result = harness
        .call_tool(
            "search_tasks",
            Some(json!({
                "query": uuid
            })),
        )
        .await;
    let search_response = parse_tool_result(&search_result);

    let tasks = search_response["tasks"]
        .as_array()
        .map(|a| a.as_slice())
        .unwrap_or(&[]);
    if !tasks.is_empty() {
        // If found, verify it's completed
        assert_eq!(
            tasks[0]["status"], "completed",
            "Task should have completed status"
        );
    }
    // Note: Completed tasks may or may not appear in search depending on query filters
}

#[tokio::test]
async fn test_update_then_complete() {
    let harness = create_harness();

    // Create a task
    let create_result = harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": "Task to Update and Complete"
            })),
        )
        .await;
    let create_response = parse_tool_result(&create_result);
    let uuid = create_response["uuid"].as_str().unwrap();

    // Update the task
    let update_result = harness
        .call_tool(
            "update_task",
            Some(json!({
                "uuid": uuid,
                "notes": "Updated before completion",
                "tags": ["important", "urgent"]
            })),
        )
        .await;
    let update_response = parse_tool_result(&update_result);
    assert_eq!(update_response["message"], "Task updated successfully");

    // Complete the task
    let complete_result = harness
        .call_tool(
            "complete_task",
            Some(json!({
                "uuid": uuid
            })),
        )
        .await;
    let complete_response = parse_tool_result(&complete_result);
    assert_eq!(complete_response["message"], "Task completed successfully");

    // Verify combined state
    let search_result = harness
        .call_tool(
            "search_tasks",
            Some(json!({
                "query": uuid
            })),
        )
        .await;
    let search_response = parse_tool_result(&search_result);

    let tasks = search_response["tasks"]
        .as_array()
        .map(|a| a.as_slice())
        .unwrap_or(&[]);
    if !tasks.is_empty() {
        let task = &tasks[0];
        assert_eq!(task["status"], "completed");
        // Notes should be updated
        if let Some(notes) = task["notes"].as_str() {
            assert_eq!(notes, "Updated before completion");
        }
    }
}

#[tokio::test]
async fn test_search_excludes_deleted_tasks() {
    let harness = create_harness();

    // Create a task with simple searchable title
    let task_title = "ExcludeDeletedTestTask";
    let create_result = harness
        .call_tool(
            "create_task",
            Some(json!({
                "title": task_title
            })),
        )
        .await;
    let create_response = parse_tool_result(&create_result);
    let task_uuid = create_response["uuid"].as_str().unwrap().to_string();

    // Delete the task
    let delete_result = harness
        .call_tool(
            "delete_task",
            Some(json!({
                "uuid": task_uuid
            })),
        )
        .await;
    let delete_response = parse_tool_result(&delete_result);
    assert_eq!(
        delete_response["message"], "Task deleted successfully",
        "Delete operation should succeed"
    );

    // Search after deletion - verify the deleted task doesn't appear
    let search_result = harness
        .call_tool(
            "search_tasks",
            Some(json!({
                "query": task_title
            })),
        )
        .await;
    let search_response = parse_tool_result(&search_result);

    let tasks = search_response["tasks"]
        .as_array()
        .map(|a| a.as_slice())
        .unwrap_or(&[]);

    // Verify the deleted task is not in results
    let found_deleted = tasks.iter().any(|t| t["uuid"].as_str() == Some(&task_uuid));
    assert!(
        !found_deleted,
        "Deleted task should not appear in search results"
    );
}