things3-cli 2.1.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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
//! Error handling tests for MCP server

#![cfg(feature = "mcp-server")]
#![allow(deprecated)]

use super::common::create_test_mcp_server;
use serde_json::json;
use things3_cli::mcp::{CallToolRequest, Content, McpError};

#[tokio::test]
async fn test_mcp_error_creation() {
    // Test McpError creation methods
    let tool_not_found = McpError::tool_not_found("test_tool");
    assert!(
        matches!(tool_not_found, McpError::ToolNotFound { tool_name } if tool_name == "test_tool")
    );

    let resource_not_found = McpError::resource_not_found("test://resource");
    assert!(
        matches!(resource_not_found, McpError::ResourceNotFound { uri } if uri == "test://resource")
    );

    let prompt_not_found = McpError::prompt_not_found("test_prompt");
    assert!(
        matches!(prompt_not_found, McpError::PromptNotFound { prompt_name } if prompt_name == "test_prompt")
    );

    let missing_param = McpError::missing_parameter("test_param");
    assert!(
        matches!(missing_param, McpError::MissingParameter { parameter_name } if parameter_name == "test_param")
    );

    let invalid_param = McpError::invalid_parameter("test_param", "invalid value");
    assert!(
        matches!(invalid_param, McpError::InvalidParameter { parameter_name, message }
        if parameter_name == "test_param" && message == "invalid value")
    );

    let invalid_format = McpError::invalid_format("xml", "json, csv");
    assert!(
        matches!(invalid_format, McpError::InvalidFormat { format, supported }
        if format == "xml" && supported == "json, csv")
    );

    let invalid_data_type = McpError::invalid_data_type("xml", "tasks, projects");
    assert!(
        matches!(invalid_data_type, McpError::InvalidDataType { data_type, supported }
        if data_type == "xml" && supported == "tasks, projects")
    );
}

#[tokio::test]
async fn test_mcp_error_to_call_result() {
    // Test tool not found error
    let tool_error = McpError::tool_not_found("unknown_tool");
    let call_result = tool_error.to_call_result();
    assert!(call_result.is_error);
    assert_eq!(call_result.content.len(), 1);
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Tool 'unknown_tool' not found"));
            assert!(text.contains("Available tools can be listed"));
        }
    }

    // Test missing parameter error
    let param_error = McpError::missing_parameter("query");
    let call_result = param_error.to_call_result();
    assert!(call_result.is_error);
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Missing required parameter 'query'"));
            assert!(text.contains("Please provide this parameter"));
        }
    }

    // Test invalid format error
    let format_error = McpError::invalid_format("xml", "json, csv, markdown");
    let call_result = format_error.to_call_result();
    assert!(call_result.is_error);
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Invalid format 'xml'"));
            assert!(text.contains("Supported formats: json, csv, markdown"));
        }
    }
}

#[tokio::test]
async fn test_mcp_error_to_prompt_result() {
    // Test prompt not found error
    let prompt_error = McpError::prompt_not_found("unknown_prompt");
    let prompt_result = prompt_error.to_prompt_result();
    assert!(prompt_result.is_error);
    match &prompt_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Prompt 'unknown_prompt' not found"));
            assert!(text.contains("Available prompts can be listed"));
        }
    }

    // Test missing parameter error
    let param_error = McpError::missing_parameter("task_title");
    let prompt_result = param_error.to_prompt_result();
    assert!(prompt_result.is_error);
    match &prompt_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Missing required parameter 'task_title'"));
        }
    }
}

#[tokio::test]
async fn test_mcp_error_to_resource_result() {
    // Test resource not found error
    let resource_error = McpError::resource_not_found("things://unknown");
    let resource_result = resource_error.to_resource_result();
    match &resource_result.contents[0] {
        Content::Text { text } => {
            assert!(text.contains("Resource 'things://unknown' not found"));
            assert!(text.contains("Available resources can be listed"));
        }
    }
}

#[tokio::test]
async fn test_from_traits() {
    // Test From<ThingsError> for McpError
    let things_error = things3_core::ThingsError::validation("Test validation error");
    let mcp_error: McpError = things_error.into();
    assert!(matches!(mcp_error, McpError::ValidationError { message }
        if message == "Test validation error"));

    // Test From<serde_json::Error> for McpError
    let json_error = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
    let mcp_error: McpError = json_error.into();
    assert!(
        matches!(mcp_error, McpError::SerializationFailed { operation, .. }
        if operation == "json serialization")
    );

    // Test From<std::io::Error> for McpError
    let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
    let mcp_error: McpError = io_error.into();
    assert!(
        matches!(mcp_error, McpError::IoOperationFailed { operation, .. }
        if operation == "file operation")
    );
}

#[tokio::test]
async fn test_from_traits_comprehensive() {
    // Test all ThingsError variants
    let db_error = things3_core::ThingsError::Database("TypeNotFound: test_column".to_string());
    let mcp_error: McpError = db_error.into();
    assert!(
        matches!(mcp_error, McpError::DatabaseOperationFailed { operation, .. } if operation == "database operation")
    );

    let serialization_error = things3_core::ThingsError::Serialization(
        serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err(),
    );
    let mcp_error: McpError = serialization_error.into();
    assert!(
        matches!(mcp_error, McpError::SerializationFailed { operation, .. } if operation == "serialization")
    );

    let io_error = things3_core::ThingsError::Io(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "file not found",
    ));
    let mcp_error: McpError = io_error.into();
    assert!(
        matches!(mcp_error, McpError::IoOperationFailed { operation, .. } if operation == "io operation")
    );

    let db_not_found = things3_core::ThingsError::DatabaseNotFound {
        path: "/test/path".to_string(),
    };
    let mcp_error: McpError = db_not_found.into();
    assert!(
        matches!(mcp_error, McpError::ConfigurationError { message } if message.contains("Database not found at: /test/path"))
    );

    let invalid_uuid = things3_core::ThingsError::InvalidUuid {
        uuid: "invalid-uuid".to_string(),
    };
    let mcp_error: McpError = invalid_uuid.into();
    assert!(
        matches!(mcp_error, McpError::ValidationError { message } if message.contains("Invalid UUID format: invalid-uuid"))
    );

    let invalid_date = things3_core::ThingsError::InvalidDate {
        date: "invalid-date".to_string(),
    };
    let mcp_error: McpError = invalid_date.into();
    assert!(
        matches!(mcp_error, McpError::ValidationError { message } if message.contains("Invalid date format: invalid-date"))
    );

    let task_not_found = things3_core::ThingsError::TaskNotFound {
        uuid: "task-uuid".to_string(),
    };
    let mcp_error: McpError = task_not_found.into();
    assert!(
        matches!(mcp_error, McpError::ValidationError { message } if message.contains("Task not found: task-uuid"))
    );

    let project_not_found = things3_core::ThingsError::ProjectNotFound {
        uuid: "project-uuid".to_string(),
    };
    let mcp_error: McpError = project_not_found.into();
    assert!(
        matches!(mcp_error, McpError::ValidationError { message } if message.contains("Project not found: project-uuid"))
    );

    let area_not_found = things3_core::ThingsError::AreaNotFound {
        uuid: "area-uuid".to_string(),
    };
    let mcp_error: McpError = area_not_found.into();
    assert!(
        matches!(mcp_error, McpError::ValidationError { message } if message.contains("Area not found: area-uuid"))
    );

    let validation_error = things3_core::ThingsError::Validation {
        message: "test validation".to_string(),
    };
    let mcp_error: McpError = validation_error.into();
    assert!(
        matches!(mcp_error, McpError::ValidationError { message } if message == "test validation")
    );

    let config_error = things3_core::ThingsError::Configuration {
        message: "test config".to_string(),
    };
    let mcp_error: McpError = config_error.into();
    assert!(
        matches!(mcp_error, McpError::ConfigurationError { message } if message == "test config")
    );

    let unknown_error = things3_core::ThingsError::Unknown {
        message: "test unknown".to_string(),
    };
    let mcp_error: McpError = unknown_error.into();
    assert!(matches!(mcp_error, McpError::InternalError { message } if message == "test unknown"));
}

#[tokio::test]
async fn test_specific_error_types_in_tool_handlers() {
    let server = create_test_mcp_server().await;

    // Test missing parameter error
    let request = CallToolRequest {
        name: "search_tasks".to_string(),
        arguments: Some(json!({ "limit": 5 })), // Missing required 'query' parameter
    };

    let result = server.call_tool(request).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        McpError::MissingParameter { parameter_name } => {
            assert_eq!(parameter_name, "query");
        }
        _ => panic!("Expected MissingParameter error"),
    }
}

#[tokio::test]
async fn test_invalid_format_error() {
    let server = create_test_mcp_server().await;

    // Test invalid format error
    let request = CallToolRequest {
        name: "export_data".to_string(),
        arguments: Some(json!({
            "format": "xml", // Invalid format
            "data_type": "tasks"
        })),
    };

    let result = server.call_tool(request).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        McpError::InvalidFormat { format, supported } => {
            assert_eq!(format, "xml");
            assert_eq!(supported, "json, csv, markdown");
        }
        _ => panic!("Expected InvalidFormat error"),
    }
}

#[tokio::test]
async fn test_invalid_data_type_error() {
    let server = create_test_mcp_server().await;

    // Test invalid data type error
    let request = CallToolRequest {
        name: "export_data".to_string(),
        arguments: Some(json!({
            "format": "json",
            "data_type": "invalid_type" // Invalid data type
        })),
    };

    let result = server.call_tool(request).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        McpError::InvalidDataType {
            data_type,
            supported,
        } => {
            assert_eq!(data_type, "invalid_type");
            assert_eq!(supported, "tasks, projects, areas, all");
        }
        _ => panic!("Expected InvalidDataType error"),
    }
}

#[tokio::test]
async fn test_tool_not_found_error() {
    let server = create_test_mcp_server().await;

    // Test tool not found error
    let request = CallToolRequest {
        name: "nonexistent_tool".to_string(),
        arguments: None,
    };

    let result = server.call_tool(request).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        McpError::ToolNotFound { tool_name } => {
            assert_eq!(tool_name, "nonexistent_tool");
        }
        _ => panic!("Expected ToolNotFound error"),
    }
}

#[tokio::test]
async fn test_prompt_not_found_error() {
    let server = create_test_mcp_server().await;

    // Test prompt not found error
    let request = things3_cli::mcp::GetPromptRequest {
        name: "nonexistent_prompt".to_string(),
        arguments: None,
    };

    let result = server.get_prompt(request).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        McpError::PromptNotFound { prompt_name } => {
            assert_eq!(prompt_name, "nonexistent_prompt");
        }
        _ => panic!("Expected PromptNotFound error"),
    }
}

#[tokio::test]
async fn test_resource_not_found_error() {
    let server = create_test_mcp_server().await;

    // Test resource not found error
    let request = things3_cli::mcp::ReadResourceRequest {
        uri: "things://nonexistent".to_string(),
    };

    let result = server.read_resource(request).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        McpError::ResourceNotFound { uri } => {
            assert_eq!(uri, "things://nonexistent");
        }
        _ => panic!("Expected ResourceNotFound error"),
    }
}

#[tokio::test]
async fn test_error_message_quality() {
    // Test that error messages are helpful and actionable
    let errors = vec![
        McpError::tool_not_found("test_tool"),
        McpError::missing_parameter("test_param"),
        McpError::invalid_format("xml", "json, csv"),
        McpError::invalid_data_type("xml", "tasks, projects"),
    ];

    for error in errors {
        let call_result = error.to_call_result();
        assert!(call_result.is_error);

        match &call_result.content[0] {
            Content::Text { text } => {
                // Error messages should be informative
                assert!(text.len() > 20);
                // Should contain helpful suggestions
                assert!(
                    text.contains("Please")
                        || text.contains("Available")
                        || text.contains("Supported")
                );
                // Should not be just generic error messages
                assert!(!text.contains("Error: Error"));
            }
        }
    }
}

#[tokio::test]
async fn test_error_consistency() {
    // Test that similar errors produce consistent messages
    let param_errors = vec![
        McpError::missing_parameter("param1"),
        McpError::missing_parameter("param2"),
    ];

    for error in param_errors {
        let call_result = error.to_call_result();
        match &call_result.content[0] {
            Content::Text { text } => {
                assert!(text.contains("Missing required parameter"));
                assert!(text.contains("Please provide this parameter"));
            }
        }
    }
}

#[tokio::test]
async fn test_error_serialization() {
    // Test that McpError can be serialized/deserialized for logging
    let error = McpError::tool_not_found("test_tool");
    let error_string = format!("{error:?}");
    assert!(error_string.contains("ToolNotFound"));
    assert!(error_string.contains("test_tool"));
}

#[tokio::test]
async fn test_mcp_error_helper_methods() {
    // Test all the helper methods for creating specific error types
    let tool_not_found = McpError::tool_not_found("test_tool");
    assert!(
        matches!(tool_not_found, McpError::ToolNotFound { tool_name } if tool_name == "test_tool")
    );

    let prompt_not_found = McpError::prompt_not_found("test_prompt");
    assert!(
        matches!(prompt_not_found, McpError::PromptNotFound { prompt_name } if prompt_name == "test_prompt")
    );

    let resource_not_found = McpError::resource_not_found("test_resource");
    assert!(
        matches!(resource_not_found, McpError::ResourceNotFound { uri } if uri == "test_resource")
    );

    let invalid_param = McpError::invalid_parameter("test_param", "invalid value");
    assert!(
        matches!(invalid_param, McpError::InvalidParameter { parameter_name, message }
        if parameter_name == "test_param" && message == "invalid value")
    );

    let missing_param = McpError::missing_parameter("test_param");
    assert!(
        matches!(missing_param, McpError::MissingParameter { parameter_name } if parameter_name == "test_param")
    );

    let invalid_format = McpError::invalid_format("xml", "json, csv");
    assert!(
        matches!(invalid_format, McpError::InvalidFormat { format, supported }
        if format == "xml" && supported == "json, csv")
    );

    let invalid_data_type = McpError::invalid_data_type("xml", "tasks, projects");
    assert!(
        matches!(invalid_data_type, McpError::InvalidDataType { data_type, supported }
        if data_type == "xml" && supported == "tasks, projects")
    );

    let db_error = McpError::database_operation_failed(
        "test_op",
        things3_core::ThingsError::validation("test error"),
    );
    assert!(
        matches!(db_error, McpError::DatabaseOperationFailed { operation, .. } if operation == "test_op")
    );

    let backup_error = McpError::backup_operation_failed(
        "test_backup",
        things3_core::ThingsError::validation("backup error"),
    );
    assert!(
        matches!(backup_error, McpError::BackupOperationFailed { operation, .. } if operation == "test_backup")
    );

    let export_error = McpError::export_operation_failed(
        "test_export",
        things3_core::ThingsError::validation("export error"),
    );
    assert!(
        matches!(export_error, McpError::ExportOperationFailed { operation, .. } if operation == "test_export")
    );

    let perf_error = McpError::performance_monitoring_failed(
        "test_perf",
        things3_core::ThingsError::validation("perf error"),
    );
    assert!(
        matches!(perf_error, McpError::PerformanceMonitoringFailed { operation, .. } if operation == "test_perf")
    );

    let cache_error = McpError::cache_operation_failed(
        "test_cache",
        things3_core::ThingsError::validation("cache error"),
    );
    assert!(
        matches!(cache_error, McpError::CacheOperationFailed { operation, .. } if operation == "test_cache")
    );

    let serialization_error = McpError::serialization_failed(
        "test_serialization",
        serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err(),
    );
    assert!(
        matches!(serialization_error, McpError::SerializationFailed { operation, .. } if operation == "test_serialization")
    );

    let io_error = McpError::io_operation_failed(
        "test_io",
        std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"),
    );
    assert!(
        matches!(io_error, McpError::IoOperationFailed { operation, .. } if operation == "test_io")
    );

    let config_error = McpError::configuration_error("test config error");
    assert!(
        matches!(config_error, McpError::ConfigurationError { message } if message == "test config error")
    );

    let validation_error = McpError::validation_error("test validation error");
    assert!(
        matches!(validation_error, McpError::ValidationError { message } if message == "test validation error")
    );

    let internal_error = McpError::internal_error("test internal error");
    assert!(
        matches!(internal_error, McpError::InternalError { message } if message == "test internal error")
    );
}

#[tokio::test]
async fn test_error_conversion_methods_comprehensive() {
    // Test to_call_result with all error types
    let tool_error = McpError::tool_not_found("test_tool");
    let call_result = tool_error.to_call_result();
    assert!(call_result.is_error);
    assert_eq!(call_result.content.len(), 1);
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Tool 'test_tool' not found"));
        }
    }

    let resource_error = McpError::resource_not_found("test_resource");
    let call_result = resource_error.to_call_result();
    assert!(call_result.is_error);
    assert_eq!(call_result.content.len(), 1);
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Resource 'test_resource' not found"));
        }
    }

    let prompt_error = McpError::prompt_not_found("test_prompt");
    let call_result = prompt_error.to_call_result();
    assert!(call_result.is_error);
    assert_eq!(call_result.content.len(), 1);
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Prompt 'test_prompt' not found"));
        }
    }

    // Test to_prompt_result
    let prompt_error = McpError::prompt_not_found("test_prompt");
    let prompt_result = prompt_error.to_prompt_result();
    assert!(prompt_result.is_error);
    assert_eq!(prompt_result.content.len(), 1);
    match &prompt_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Prompt 'test_prompt' not found"));
        }
    }

    // Test to_resource_result
    let resource_error = McpError::resource_not_found("test_resource");
    let resource_result = resource_error.to_resource_result();
    assert_eq!(resource_result.contents.len(), 1);
    match &resource_result.contents[0] {
        Content::Text { text } => {
            assert!(text.contains("Resource 'test_resource' not found"));
        }
    }
}

#[tokio::test]
async fn test_error_message_formatting() {
    // Test that error messages are properly formatted with context
    let invalid_param = McpError::invalid_parameter("test_param", "invalid value");
    let call_result = invalid_param.to_call_result();
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Invalid parameter 'test_param'"));
            assert!(text.contains("invalid value"));
            assert!(text.contains("Please check the parameter format"));
        }
    }

    let missing_param = McpError::missing_parameter("test_param");
    let call_result = missing_param.to_call_result();
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Missing required parameter 'test_param'"));
            assert!(text.contains("Please provide this parameter"));
        }
    }

    let invalid_format = McpError::invalid_format("xml", "json, csv");
    let call_result = invalid_format.to_call_result();
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Invalid format 'xml'"));
            assert!(text.contains("Supported formats: json, csv"));
            assert!(text.contains("Please use one of the supported formats"));
        }
    }

    let db_error = McpError::database_operation_failed(
        "test_op",
        things3_core::ThingsError::validation("test error"),
    );
    let call_result = db_error.to_call_result();
    match &call_result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Database operation 'test_op' failed"));
            assert!(text.contains("Please check your database connection"));
        }
    }
}

#[tokio::test]
async fn test_error_display() {
    // Test that McpError implements Display trait properly
    let error = McpError::missing_parameter("test_param");
    let error_string = error.to_string();
    assert!(error_string.contains("Missing required parameter"));
    assert!(error_string.contains("test_param"));
}

#[tokio::test]
async fn test_error_chain() {
    // Test error chaining and source information
    let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
    let mcp_error: McpError = io_error.into();

    match mcp_error {
        McpError::IoOperationFailed { operation, source } => {
            assert_eq!(operation, "file operation");
            assert_eq!(source.kind(), std::io::ErrorKind::NotFound);
        }
        _ => panic!("Expected IoOperationFailed error"),
    }
}

#[tokio::test]
#[allow(clippy::too_many_lines)]
async fn test_all_error_variants_to_call_result() {
    // Test all error variants in to_call_result method
    let tool_error = McpError::tool_not_found("test_tool");
    let result = tool_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Tool 'test_tool' not found"));
            assert!(text.contains("list_tools method"));
        }
    }

    let resource_error = McpError::resource_not_found("test_resource");
    let result = resource_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Resource 'test_resource' not found"));
            assert!(text.contains("list_resources method"));
        }
    }

    let prompt_error = McpError::prompt_not_found("test_prompt");
    let result = prompt_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Prompt 'test_prompt' not found"));
            assert!(text.contains("list_prompts method"));
        }
    }

    let invalid_data_type = McpError::invalid_data_type("xml", "json, csv");
    let result = invalid_data_type.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Invalid data type 'xml'"));
            assert!(text.contains("Supported types: json, csv"));
            assert!(text.contains("Please use one of the supported types"));
        }
    }

    let backup_error = McpError::backup_operation_failed(
        "test_backup",
        things3_core::ThingsError::validation("backup error"),
    );
    let result = backup_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Backup operation 'test_backup' failed"));
            assert!(text.contains("Please check backup permissions"));
        }
    }

    let export_error = McpError::export_operation_failed(
        "test_export",
        things3_core::ThingsError::validation("export error"),
    );
    let result = export_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Export operation 'test_export' failed"));
            assert!(text.contains("Please check export parameters"));
        }
    }

    let perf_error = McpError::performance_monitoring_failed(
        "test_perf",
        things3_core::ThingsError::validation("perf error"),
    );
    let result = perf_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Performance monitoring 'test_perf' failed"));
            assert!(text.contains("Please try again later"));
        }
    }

    let cache_error = McpError::cache_operation_failed(
        "test_cache",
        things3_core::ThingsError::validation("cache error"),
    );
    let result = cache_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Cache operation 'test_cache' failed"));
            assert!(text.contains("Please try again later"));
        }
    }

    let serialization_error = McpError::serialization_failed(
        "test_serialization",
        serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err(),
    );
    let result = serialization_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Serialization 'test_serialization' failed"));
            assert!(text.contains("Please check data format"));
        }
    }

    let io_error = McpError::io_operation_failed(
        "test_io",
        std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"),
    );
    let result = io_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("IO operation 'test_io' failed"));
            assert!(text.contains("Please check file permissions"));
        }
    }

    let config_error = McpError::configuration_error("test config error");
    let result = config_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Configuration error: test config error"));
            assert!(text.contains("Please check your configuration"));
        }
    }

    let validation_error = McpError::validation_error("test validation error");
    let result = validation_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Validation error: test validation error"));
            assert!(text.contains("Please check your input"));
        }
    }

    let internal_error = McpError::internal_error("test internal error");
    let result = internal_error.to_call_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Internal error: test internal error"));
            assert!(text.contains("Please try again later or contact support"));
        }
    }
}

#[tokio::test]
#[allow(clippy::too_many_lines)]
async fn test_all_error_variants_to_prompt_result() {
    // Test all error variants in to_prompt_result method
    let prompt_error = McpError::prompt_not_found("test_prompt");
    let result = prompt_error.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Prompt 'test_prompt' not found"));
            assert!(text.contains("list_prompts method"));
        }
    }

    let invalid_param = McpError::invalid_parameter("test_param", "invalid value");
    let result = invalid_param.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Invalid parameter 'test_param'"));
            assert!(text.contains("invalid value"));
            assert!(text.contains("Please check the parameter format"));
        }
    }

    let missing_param = McpError::missing_parameter("test_param");
    let result = missing_param.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Missing required parameter 'test_param'"));
            assert!(text.contains("Please provide this parameter"));
        }
    }

    let invalid_format = McpError::invalid_format("xml", "json, csv");
    let result = invalid_format.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            // to_prompt_result uses catch-all pattern for InvalidFormat
            assert!(text.contains("Error: Invalid format: xml - supported formats: json, csv"));
            assert!(text.contains("Please try again later"));
        }
    }

    let invalid_data_type = McpError::invalid_data_type("xml", "json, csv");
    let result = invalid_data_type.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            // to_prompt_result uses catch-all pattern for InvalidDataType
            assert!(text.contains("Error: Invalid data type: xml - supported types: json, csv"));
            assert!(text.contains("Please try again later"));
        }
    }

    let db_error = McpError::database_operation_failed(
        "test_op",
        things3_core::ThingsError::validation("test error"),
    );
    let result = db_error.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Database operation 'test_op' failed"));
            assert!(text.contains("Please check your database connection"));
        }
    }

    let serialization_error = McpError::serialization_failed(
        "test_serialization",
        serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err(),
    );
    let result = serialization_error.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Serialization 'test_serialization' failed"));
            assert!(text.contains("Please check data format"));
        }
    }

    let io_error = McpError::io_operation_failed(
        "test_io",
        std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"),
    );
    let result = io_error.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            // to_prompt_result uses catch-all pattern for IoOperationFailed
            assert!(text.contains("Error: IO operation failed: test_io"));
            assert!(text.contains("Please try again later"));
        }
    }

    let config_error = McpError::configuration_error("test config error");
    let result = config_error.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            // to_prompt_result uses catch-all pattern for ConfigurationError
            assert!(text.contains("Error: Configuration error: test config error"));
            assert!(text.contains("Please try again later"));
        }
    }

    let validation_error = McpError::validation_error("test validation error");
    let result = validation_error.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Validation error: test validation error"));
            assert!(text.contains("Please check your input"));
        }
    }

    let internal_error = McpError::internal_error("test internal error");
    let result = internal_error.to_prompt_result();
    assert!(result.is_error);
    match &result.content[0] {
        Content::Text { text } => {
            assert!(text.contains("Internal error: test internal error"));
            assert!(text.contains("Please try again later or contact support"));
        }
    }
}

#[tokio::test]
async fn test_all_error_variants_to_resource_result() {
    // Test all error variants in to_resource_result method
    let resource_error = McpError::resource_not_found("test_resource");
    let result = resource_error.to_resource_result();
    match &result.contents[0] {
        Content::Text { text } => {
            assert!(text.contains("Resource 'test_resource' not found"));
            assert!(text.contains("list_resources method"));
        }
    }

    let db_error = McpError::database_operation_failed(
        "test_op",
        things3_core::ThingsError::validation("test error"),
    );
    let result = db_error.to_resource_result();
    match &result.contents[0] {
        Content::Text { text } => {
            assert!(text.contains("Database operation 'test_op' failed"));
            assert!(text.contains("Please check your database connection"));
        }
    }

    let serialization_error = McpError::serialization_failed(
        "test_serialization",
        serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err(),
    );
    let result = serialization_error.to_resource_result();
    match &result.contents[0] {
        Content::Text { text } => {
            assert!(text.contains("Serialization 'test_serialization' failed"));
            assert!(text.contains("Please check data format"));
        }
    }

    let io_error = McpError::io_operation_failed(
        "test_io",
        std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"),
    );
    let result = io_error.to_resource_result();
    match &result.contents[0] {
        Content::Text { text } => {
            // to_resource_result uses catch-all pattern for IoOperationFailed
            assert!(text.contains("Error: IO operation failed: test_io"));
            assert!(text.contains("Please try again later"));
        }
    }

    let config_error = McpError::configuration_error("test config error");
    let result = config_error.to_resource_result();
    match &result.contents[0] {
        Content::Text { text } => {
            // to_resource_result uses catch-all pattern for ConfigurationError
            assert!(text.contains("Error: Configuration error: test config error"));
            assert!(text.contains("Please try again later"));
        }
    }

    let validation_error = McpError::validation_error("test validation error");
    let result = validation_error.to_resource_result();
    match &result.contents[0] {
        Content::Text { text } => {
            // to_resource_result uses catch-all pattern for ValidationError
            assert!(text.contains("Error: Validation error: test validation error"));
            assert!(text.contains("Please try again later"));
        }
    }

    let internal_error = McpError::internal_error("test internal error");
    let result = internal_error.to_resource_result();
    match &result.contents[0] {
        Content::Text { text } => {
            assert!(text.contains("Internal error: test internal error"));
            assert!(text.contains("Please try again later or contact support"));
        }
    }
}