mcp-oxidized 1.2.0

MCP server for Oxidized network device configuration backup system
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
//! Actionable error handling framework for mcp-oxidized.
//!
//! This module provides LLM-optimized error messages that enable AI assistants
//! to understand errors and take appropriate actions autonomously.
//!
//! # Key Differentiator
//!
//! The `Actionable` trait is the main differentiator of mcp-oxidized. All errors
//! implement this trait to provide:
//! - Clear error description
//! - Context about what was attempted
//! - Suggestions for alternatives
//! - Next step guidance
//!
//! # Example Output
//!
//! ```text
//! [Error] Node 'SW-Unknown' not found.
//! [Context] Search performed in Oxidized inventory.
//! [Suggestions] Similar nodes: SW-Core-01, SW-Access-02.
//! [Next Step] Use 'oxidized://nodes' to list all available nodes.
//! ```

use thiserror::Error;

use crate::config::ConfigError;

// ============================================================================
// Error Message Constants
// ============================================================================

/// Prefix for error description in LLM-optimized messages.
///
/// Used as the first line of error output to clearly identify the error type.
///
/// # Example
/// ```text
/// [Error] Node 'SW-Unknown' not found.
/// ```
pub const ERROR_PREFIX: &str = "[Error]";

/// Prefix for context information in LLM-optimized messages.
///
/// Provides background about what operation was attempted when the error occurred.
///
/// # Example
/// ```text
/// [Context] Search performed in Oxidized inventory.
/// ```
pub const CONTEXT_PREFIX: &str = "[Context]";

/// Prefix for suggestions in LLM-optimized messages.
///
/// Lists alternatives or similar items that might help resolve the error.
///
/// # Example
/// ```text
/// [Suggestions] Similar nodes: SW-Core-01, SW-Access-02.
/// ```
pub const SUGGESTIONS_PREFIX: &str = "[Suggestions]";

/// Prefix for next step guidance in LLM-optimized messages.
///
/// Provides actionable guidance on what to do next to resolve or work around the error.
///
/// # Example
/// ```text
/// [Next Step] Use 'oxidized://nodes' to list all available nodes.
/// ```
pub const NEXT_STEP_PREFIX: &str = "[Next Step]";

// ============================================================================
// Actionable Trait
// ============================================================================

/// Trait for errors that can provide LLM-optimized messages.
///
/// This is a key differentiator for mcp-oxidized - all errors implement this trait
/// to provide actionable guidance to AI assistants.
///
/// # Format
///
/// The `to_llm_message()` method returns a structured message:
///
/// ```text
/// [Error] description
/// [Context] what was attempted
/// [Suggestions] similar items or alternatives
/// [Next Step] actionable guidance
/// ```
///
/// # Retry Classification
///
/// The `is_transient()` method classifies errors for retry logic:
/// - `true` → timeout, connection error, 5xx status → RETRY
/// - `false` → 4xx status, parse error, auth failed → NO RETRY
///
/// # Example
///
/// ```ignore
/// use mcp_oxidized::error::{OxidizedError, Actionable};
///
/// let error = OxidizedError::NodeNotFound(
///     "SW-Unknown".to_string(),
///     vec!["SW-Core-01".to_string(), "SW-Access-02".to_string()],
/// );
///
/// // Get LLM-optimized message
/// let message = error.to_llm_message();
/// println!("{}", message);
///
/// // Check if error is retryable
/// if error.is_transient() {
///     // Retry the operation
/// }
/// ```
pub trait Actionable {
    /// Returns a formatted message optimized for LLM understanding.
    ///
    /// The message uses structured prefixes to help AI assistants parse
    /// and act on errors autonomously.
    fn to_llm_message(&self) -> String;

    /// Classifies error as transient (retryable) or permanent.
    ///
    /// - `true` → timeout, connection error, 5xx status → RETRY
    /// - `false` → 4xx status, parse error, auth failed → NO RETRY
    fn is_transient(&self) -> bool;
}

// ============================================================================
// OxidizedError Enum
// ============================================================================

/// Comprehensive error types for mcp-oxidized operations.
///
/// All variants implement the `Actionable` trait to provide LLM-optimized
/// error messages with context, suggestions, and next steps.
#[derive(Debug, Error)]
pub enum OxidizedError {
    /// Node not found in Oxidized inventory.
    ///
    /// Contains the requested node name and a list of similar node names
    /// as suggestions.
    #[error("Node '{0}' not found")]
    NodeNotFound(String, Vec<String>),

    /// Oxidized API is unreachable.
    ///
    /// Contains the underlying request error, retry attempt count,
    /// and timestamp of last successful connection (if available).
    #[error("Oxidized API unreachable: {source}")]
    ApiUnreachable {
        /// The underlying reqwest error
        #[source]
        source: reqwest::Error,
        /// Current retry attempt number (1-based)
        attempt: u8,
        /// ISO 8601 timestamp of last successful connection, if any
        last_success: Option<String>,
    },

    /// Invalid regex pattern provided.
    ///
    /// Contains the malformed pattern string.
    #[error("Invalid regex pattern: {0}")]
    InvalidRegex(String),

    /// Authentication failed (401/403 response).
    ///
    /// Does NOT contain credential details for security (NFR6).
    #[error("Authentication failed")]
    AuthFailed,

    /// Configuration error.
    ///
    /// Wraps errors from the config module.
    #[error("Configuration error: {0}")]
    ConfigError(#[from] ConfigError),

    /// JSON parsing failed.
    ///
    /// Contains context about what was being parsed and the underlying error.
    #[error("JSON parse error in {context}: {source}")]
    ParseError {
        /// Description of what was being parsed (e.g., "node list response")
        context: String,
        /// The underlying serde_json error
        #[source]
        source: serde_json::Error,
    },

    /// HTTP error response (4xx/5xx status codes not covered by specific variants).
    ///
    /// Used for server errors (5xx) which are transient and retryable,
    /// and other client errors (4xx) not covered by NodeNotFound or AuthFailed.
    #[error("HTTP error {status_code} for {context}")]
    HttpError {
        /// HTTP status code
        status_code: u16,
        /// Context describing the operation that failed
        context: String,
    },
}

// ============================================================================
// Actionable Implementation
// ============================================================================

impl OxidizedError {
    /// Returns a short, safe error type name for logging.
    ///
    /// This method is designed for tracing output where we need a concise
    /// error identifier that is safe to log (no credentials or sensitive data).
    pub fn error_type(&self) -> &'static str {
        match self {
            OxidizedError::NodeNotFound(_, _) => "NodeNotFound",
            OxidizedError::ApiUnreachable { .. } => "ApiUnreachable",
            OxidizedError::InvalidRegex(_) => "InvalidRegex",
            OxidizedError::AuthFailed => "AuthFailed",
            OxidizedError::ConfigError(_) => "ConfigError",
            OxidizedError::ParseError { .. } => "ParseError",
            OxidizedError::HttpError { .. } => "HttpError",
        }
    }
}

impl Actionable for OxidizedError {
    fn to_llm_message(&self) -> String {
        match self {
            OxidizedError::NodeNotFound(node_name, suggestions) => {
                let suggestions_str = if suggestions.is_empty() {
                    "No similar nodes found.".to_string()
                } else {
                    format!("Similar nodes: {}.", suggestions.join(", "))
                };

                format!(
                    "{} Node '{}' not found.\n{} Search performed in Oxidized inventory.\n{} {}\n{} Use 'oxidized://nodes' to list all available nodes.",
                    ERROR_PREFIX,
                    node_name,
                    CONTEXT_PREFIX,
                    SUGGESTIONS_PREFIX,
                    suggestions_str,
                    NEXT_STEP_PREFIX
                )
            }

            OxidizedError::ApiUnreachable {
                source,
                attempt,
                last_success,
            } => {
                let last_success_info = match last_success {
                    Some(timestamp) => format!("Last successful connection: {}.", timestamp),
                    None => "No previous successful connection recorded.".to_string(),
                };

                // Detect SSL certificate errors for better suggestions
                let error_string = source.to_string().to_lowercase();
                let is_ssl_error = error_string.contains("certificate")
                    || error_string.contains("ssl")
                    || error_string.contains("tls")
                    || error_string.contains("invalid peer certificate");

                let (error_details, suggestion) = if is_ssl_error {
                    (
                        "SSL/TLS certificate verification failed.",
                        "For self-signed certificates, set OXIDIZED_SSL_VERIFY=false. WARNING: Only do this if you trust the server.",
                    )
                } else if source.is_timeout() {
                    (
                        "Connection timed out.",
                        "Check if Oxidized server is running and accessible. Verify OXIDIZED_URL configuration.",
                    )
                } else if source.is_connect() {
                    (
                        "Connection refused or network unreachable.",
                        "Check if Oxidized server is running and accessible. Verify OXIDIZED_URL configuration.",
                    )
                } else {
                    (
                        "Network error occurred.",
                        "Check if Oxidized server is running and accessible. Verify OXIDIZED_URL configuration.",
                    )
                };

                format!(
                    "{} Oxidized API unreachable - {}\n{} Attempt {}/3 to connect to Oxidized server. {}\n{} {}\n{} Wait a moment and retry, or check network connectivity and server status.",
                    ERROR_PREFIX,
                    error_details,
                    CONTEXT_PREFIX,
                    attempt,
                    last_success_info,
                    SUGGESTIONS_PREFIX,
                    suggestion,
                    NEXT_STEP_PREFIX
                )
            }

            OxidizedError::InvalidRegex(pattern) => {
                format!(
                    "{} Invalid regex pattern: '{}'.\n{} Attempted to compile regex for node filtering.\n{} Check for unescaped special characters. Use simple wildcards or valid regex syntax.\n{} Refer to Rust regex documentation for valid patterns.",
                    ERROR_PREFIX, pattern, CONTEXT_PREFIX, SUGGESTIONS_PREFIX, NEXT_STEP_PREFIX
                )
            }

            OxidizedError::AuthFailed => {
                // SECURITY (NFR6): Never mention which credential failed
                format!(
                    "{} Authentication failed.\n{} Attempted to authenticate with Oxidized server using provided credentials.\n{} Verify OXIDIZED_USER and OXIDIZED_PASSWORD environment variables are correctly set.\n{} Check credentials with Oxidized administrator or test with curl.",
                    ERROR_PREFIX, CONTEXT_PREFIX, SUGGESTIONS_PREFIX, NEXT_STEP_PREFIX
                )
            }

            OxidizedError::ConfigError(config_error) => {
                // Delegate to ConfigError's own message, wrapped in our format
                format!(
                    "{} Configuration error: {}.\n{} Loading mcp-oxidized configuration from environment variables.\n{} Check environment variables: OXIDIZED_URL, OXIDIZED_USER, OXIDIZED_PASSWORD, OXIDIZED_PASSWORD_FILE.\n{} Review configuration documentation and verify environment variable values.",
                    ERROR_PREFIX,
                    config_error,
                    CONTEXT_PREFIX,
                    SUGGESTIONS_PREFIX,
                    NEXT_STEP_PREFIX
                )
            }

            OxidizedError::ParseError { context, source } => {
                format!(
                    "{} Failed to parse JSON in {}.\n{} Received response from Oxidized but could not parse it. Error: {}.\n{} Verify Oxidized API version compatibility. Response format may have changed.\n{} Check Oxidized server logs for errors or contact administrator.",
                    ERROR_PREFIX,
                    context,
                    CONTEXT_PREFIX,
                    source,
                    SUGGESTIONS_PREFIX,
                    NEXT_STEP_PREFIX
                )
            }

            OxidizedError::HttpError {
                status_code,
                context,
            } => {
                let error_type = if *status_code >= 500 {
                    "Server error"
                } else {
                    "Client error"
                };

                let suggestion = if *status_code >= 500 {
                    "This is a server-side issue. The request may succeed if retried."
                } else {
                    "Check the request parameters and resource existence."
                };

                format!(
                    "{} {} (HTTP {}) for {}.\n{} HTTP request to Oxidized API returned error status.\n{} {}\n{} Retry the request or check Oxidized server status.",
                    ERROR_PREFIX,
                    error_type,
                    status_code,
                    context,
                    CONTEXT_PREFIX,
                    SUGGESTIONS_PREFIX,
                    suggestion,
                    NEXT_STEP_PREFIX
                )
            }
        }
    }

    fn is_transient(&self) -> bool {
        match self {
            // Retryable: network issues may be temporary
            OxidizedError::ApiUnreachable { .. } => true,

            // Retryable: 5xx server errors are transient
            OxidizedError::HttpError { status_code, .. } => *status_code >= 500,

            // Not retryable: these are permanent errors
            OxidizedError::NodeNotFound(_, _) => false,
            OxidizedError::AuthFailed => false,
            OxidizedError::InvalidRegex(_) => false,
            OxidizedError::ConfigError(_) => false,
            OxidizedError::ParseError { .. } => false,
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // -------------------------------------------------------------------------
    // Error Constants Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_error_prefix_value() {
        assert_eq!(ERROR_PREFIX, "[Error]");
    }

    #[test]
    fn test_context_prefix_value() {
        assert_eq!(CONTEXT_PREFIX, "[Context]");
    }

    #[test]
    fn test_suggestions_prefix_value() {
        assert_eq!(SUGGESTIONS_PREFIX, "[Suggestions]");
    }

    #[test]
    fn test_next_step_prefix_value() {
        assert_eq!(NEXT_STEP_PREFIX, "[Next Step]");
    }

    // -------------------------------------------------------------------------
    // NodeNotFound Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_node_not_found_message_format() {
        let error = OxidizedError::NodeNotFound(
            "SW-Unknown".to_string(),
            vec!["SW-Core-01".to_string(), "SW-Access-02".to_string()],
        );

        let message = error.to_llm_message();

        // Verify all prefixes are used (not hardcoded strings)
        assert!(
            message.contains(ERROR_PREFIX),
            "Should contain ERROR_PREFIX"
        );
        assert!(
            message.contains(CONTEXT_PREFIX),
            "Should contain CONTEXT_PREFIX"
        );
        assert!(
            message.contains(SUGGESTIONS_PREFIX),
            "Should contain SUGGESTIONS_PREFIX"
        );
        assert!(
            message.contains(NEXT_STEP_PREFIX),
            "Should contain NEXT_STEP_PREFIX"
        );

        // Verify content
        assert!(message.contains("SW-Unknown"), "Should contain node name");
        assert!(
            message.contains("SW-Core-01"),
            "Should contain first suggestion"
        );
        assert!(
            message.contains("SW-Access-02"),
            "Should contain second suggestion"
        );
        assert!(
            message.contains("oxidized://nodes"),
            "Should contain resource URI"
        );
    }

    #[test]
    fn test_node_not_found_with_single_suggestion() {
        let error =
            OxidizedError::NodeNotFound("test-node".to_string(), vec!["similar-node".to_string()]);

        let message = error.to_llm_message();

        assert!(message.contains("similar-node"));
        assert!(!message.contains("No similar nodes found"));
    }

    #[test]
    fn test_node_not_found_handles_empty_suggestions_gracefully() {
        // Note: Empty suggestions can occur when no similar nodes exist in inventory.
        // The error should handle this gracefully with a clear message.
        let error = OxidizedError::NodeNotFound("orphan-node".to_string(), vec![]);

        let message = error.to_llm_message();

        // Should handle empty suggestions gracefully with fallback message
        assert!(message.contains("orphan-node"));
        assert!(
            message.contains("No similar nodes found"),
            "Should provide fallback when no suggestions available"
        );
    }

    #[test]
    fn test_node_not_found_is_not_transient() {
        let error = OxidizedError::NodeNotFound("test".to_string(), vec!["similar".to_string()]);

        assert!(
            !error.is_transient(),
            "NodeNotFound should not be transient"
        );
    }

    #[test]
    fn test_node_not_found_display() {
        let error = OxidizedError::NodeNotFound("my-node".to_string(), vec!["other".to_string()]);

        let display = format!("{}", error);
        assert_eq!(display, "Node 'my-node' not found");
    }

    // -------------------------------------------------------------------------
    // ApiUnreachable Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_api_unreachable_message_format() {
        // Create a mock reqwest error using builder
        let error = create_api_unreachable_error(2, Some("2025-01-15T10:30:00Z".to_string()));

        let message = error.to_llm_message();

        // Verify all prefixes are used
        assert!(message.contains(ERROR_PREFIX));
        assert!(message.contains(CONTEXT_PREFIX));
        assert!(message.contains(SUGGESTIONS_PREFIX));
        assert!(message.contains(NEXT_STEP_PREFIX));

        // Verify content
        assert!(message.contains("Attempt 2/3"));
        assert!(message.contains("2025-01-15T10:30:00Z"));
        assert!(message.contains("OXIDIZED_URL"));
    }

    #[test]
    fn test_api_unreachable_without_last_success() {
        let error = create_api_unreachable_error(1, None);

        let message = error.to_llm_message();

        assert!(message.contains("No previous successful connection recorded"));
    }

    #[test]
    fn test_api_unreachable_includes_attempt_count() {
        let error = create_api_unreachable_error(3, None);

        let message = error.to_llm_message();

        assert!(
            message.contains("Attempt 3/3"),
            "Should include attempt count in message"
        );
    }

    #[test]
    fn test_api_unreachable_is_transient() {
        let error = create_api_unreachable_error(1, None);

        assert!(error.is_transient(), "ApiUnreachable should be transient");
    }

    // -------------------------------------------------------------------------
    // InvalidRegex Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_invalid_regex_message_format() {
        let error = OxidizedError::InvalidRegex("[invalid".to_string());

        let message = error.to_llm_message();

        assert!(message.contains(ERROR_PREFIX));
        assert!(message.contains(CONTEXT_PREFIX));
        assert!(message.contains(SUGGESTIONS_PREFIX));
        assert!(message.contains(NEXT_STEP_PREFIX));
        assert!(message.contains("[invalid"));
        assert!(message.contains("regex"));
    }

    #[test]
    fn test_invalid_regex_is_not_transient() {
        let error = OxidizedError::InvalidRegex("bad-pattern".to_string());

        assert!(
            !error.is_transient(),
            "InvalidRegex should not be transient"
        );
    }

    #[test]
    fn test_invalid_regex_display() {
        let error = OxidizedError::InvalidRegex("(unclosed".to_string());

        let display = format!("{}", error);
        assert_eq!(display, "Invalid regex pattern: (unclosed");
    }

    // -------------------------------------------------------------------------
    // AuthFailed Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_auth_failed_message_format() {
        let error = OxidizedError::AuthFailed;

        let message = error.to_llm_message();

        assert!(message.contains(ERROR_PREFIX));
        assert!(message.contains(CONTEXT_PREFIX));
        assert!(message.contains(SUGGESTIONS_PREFIX));
        assert!(message.contains(NEXT_STEP_PREFIX));
        assert!(message.contains("OXIDIZED_USER"));
        assert!(message.contains("OXIDIZED_PASSWORD"));
    }

    #[test]
    fn test_auth_failed_no_credentials_in_message() {
        let error = OxidizedError::AuthFailed;

        let message = error.to_llm_message();

        // NFR6: Verify no actual credential values could leak
        // The message should mention env var names but never actual values
        assert!(!message.contains("password123"));
        assert!(!message.contains("secret"));
        assert!(!message.contains("admin_password"));

        // Should NOT specify which credential failed (security best practice)
        assert!(!message.contains("incorrect password"));
        assert!(!message.contains("invalid username"));
        assert!(!message.contains("wrong user"));
    }

    #[test]
    fn test_auth_failed_is_not_transient() {
        let error = OxidizedError::AuthFailed;

        assert!(!error.is_transient(), "AuthFailed should not be transient");
    }

    #[test]
    fn test_auth_failed_display() {
        let error = OxidizedError::AuthFailed;

        let display = format!("{}", error);
        assert_eq!(display, "Authentication failed");
    }

    // -------------------------------------------------------------------------
    // ConfigError Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_config_error_message_format() {
        let config_err = ConfigError::InvalidUrl("ftp://invalid".to_string());
        let error = OxidizedError::ConfigError(config_err);

        let message = error.to_llm_message();

        assert!(message.contains(ERROR_PREFIX));
        assert!(message.contains(CONTEXT_PREFIX));
        assert!(message.contains(SUGGESTIONS_PREFIX));
        assert!(message.contains(NEXT_STEP_PREFIX));
        assert!(message.contains("ftp://invalid"));
    }

    #[test]
    fn test_config_error_is_not_transient() {
        let config_err = ConfigError::InvalidUrl("bad-url".to_string());
        let error = OxidizedError::ConfigError(config_err);

        assert!(!error.is_transient(), "ConfigError should not be transient");
    }

    #[test]
    fn test_config_error_from_conversion() {
        let config_err = ConfigError::InvalidUrl("test".to_string());

        // Test #[from] attribute - should convert automatically
        let oxidized_err: OxidizedError = config_err.into();

        match oxidized_err {
            OxidizedError::ConfigError(_) => {}
            _ => panic!("Expected ConfigError variant"),
        }
    }

    // -------------------------------------------------------------------------
    // ParseError Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_parse_error_message_format() {
        let json_err = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
        let error = OxidizedError::ParseError {
            context: "node list response".to_string(),
            source: json_err,
        };

        let message = error.to_llm_message();

        assert!(message.contains(ERROR_PREFIX));
        assert!(message.contains(CONTEXT_PREFIX));
        assert!(message.contains(SUGGESTIONS_PREFIX));
        assert!(message.contains(NEXT_STEP_PREFIX));
        assert!(message.contains("node list response"));
    }

    #[test]
    fn test_parse_error_is_not_transient() {
        let json_err = serde_json::from_str::<serde_json::Value>("{").unwrap_err();
        let error = OxidizedError::ParseError {
            context: "test".to_string(),
            source: json_err,
        };

        assert!(!error.is_transient(), "ParseError should not be transient");
    }

    #[test]
    fn test_parse_error_display() {
        let json_err = serde_json::from_str::<serde_json::Value>("not json").unwrap_err();
        let error = OxidizedError::ParseError {
            context: "config file".to_string(),
            source: json_err,
        };

        let display = format!("{}", error);
        assert!(display.contains("config file"));
        assert!(display.contains("JSON parse error"));
    }

    // -------------------------------------------------------------------------
    // HttpError Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_http_error_5xx_message_format() {
        let error = OxidizedError::HttpError {
            status_code: 503,
            context: "get_nodes".to_string(),
        };

        let message = error.to_llm_message();

        assert!(message.contains(ERROR_PREFIX));
        assert!(message.contains(CONTEXT_PREFIX));
        assert!(message.contains(SUGGESTIONS_PREFIX));
        assert!(message.contains(NEXT_STEP_PREFIX));
        assert!(message.contains("503"));
        assert!(message.contains("Server error"));
        assert!(message.contains("get_nodes"));
    }

    #[test]
    fn test_http_error_4xx_message_format() {
        let error = OxidizedError::HttpError {
            status_code: 400,
            context: "bad_request".to_string(),
        };

        let message = error.to_llm_message();

        assert!(message.contains("400"));
        assert!(message.contains("Client error"));
        assert!(message.contains("bad_request"));
    }

    #[test]
    fn test_http_error_display() {
        let error = OxidizedError::HttpError {
            status_code: 502,
            context: "proxy".to_string(),
        };

        let display = format!("{}", error);
        assert_eq!(display, "HTTP error 502 for proxy");
    }

    // -------------------------------------------------------------------------
    // Security Tests (NFR6)
    // -------------------------------------------------------------------------

    #[test]
    fn test_no_credentials_in_any_error_message() {
        // Test all error variants to ensure no credentials leak

        let errors: Vec<OxidizedError> = vec![
            OxidizedError::NodeNotFound("node".to_string(), vec!["other".to_string()]),
            create_api_unreachable_error(1, None),
            OxidizedError::InvalidRegex("pattern".to_string()),
            OxidizedError::AuthFailed,
            OxidizedError::ConfigError(ConfigError::InvalidUrl("url".to_string())),
            OxidizedError::ParseError {
                context: "test".to_string(),
                source: serde_json::from_str::<serde_json::Value>("{").unwrap_err(),
            },
            OxidizedError::HttpError {
                status_code: 500,
                context: "test".to_string(),
            },
        ];

        // Common credential patterns that should NEVER appear
        let forbidden_patterns = [
            "password:",
            "secret:",
            "token:",
            "api_key:",
            "Authorization:",
            "Basic ",
            "Bearer ",
        ];

        for error in errors {
            let message = error.to_llm_message();

            for pattern in &forbidden_patterns {
                assert!(
                    !message.to_lowercase().contains(&pattern.to_lowercase()),
                    "Error message should not contain '{}': {}",
                    pattern,
                    message
                );
            }
        }
    }

    #[test]
    fn test_api_unreachable_no_url_credentials() {
        // Even if URL contains credentials, they should not appear in message
        let error = create_api_unreachable_error(1, None);
        let message = error.to_llm_message();

        // The error message should reference OXIDIZED_URL but not expose actual URL content
        assert!(message.contains("OXIDIZED_URL"));
        assert!(!message.contains("user:pass@"));
        assert!(!message.contains("admin:secret"));
    }

    // -------------------------------------------------------------------------
    // Comprehensive is_transient Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_all_variants_is_transient_classification() {
        // Transient (retryable)
        assert!(create_api_unreachable_error(1, None).is_transient());

        // HttpError: 5xx is transient, 4xx is not
        assert!(
            OxidizedError::HttpError {
                status_code: 500,
                context: "test".to_string()
            }
            .is_transient()
        );
        assert!(
            OxidizedError::HttpError {
                status_code: 503,
                context: "test".to_string()
            }
            .is_transient()
        );
        assert!(
            !OxidizedError::HttpError {
                status_code: 400,
                context: "test".to_string()
            }
            .is_transient()
        );
        assert!(
            !OxidizedError::HttpError {
                status_code: 429,
                context: "test".to_string()
            }
            .is_transient()
        );

        // Not transient (permanent)
        assert!(!OxidizedError::NodeNotFound("n".to_string(), vec![]).is_transient());
        assert!(!OxidizedError::InvalidRegex("p".to_string()).is_transient());
        assert!(!OxidizedError::AuthFailed.is_transient());
        assert!(
            !OxidizedError::ConfigError(ConfigError::InvalidUrl("u".to_string())).is_transient()
        );
        assert!(
            !OxidizedError::ParseError {
                context: "c".to_string(),
                source: serde_json::from_str::<serde_json::Value>("{").unwrap_err(),
            }
            .is_transient()
        );
    }

    // -------------------------------------------------------------------------
    // Error Constants Usage Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_all_messages_use_error_constants() {
        let errors: Vec<OxidizedError> = vec![
            OxidizedError::NodeNotFound("n".to_string(), vec!["s".to_string()]),
            create_api_unreachable_error(1, Some("ts".to_string())),
            OxidizedError::InvalidRegex("p".to_string()),
            OxidizedError::AuthFailed,
            OxidizedError::ConfigError(ConfigError::InvalidUrl("u".to_string())),
            OxidizedError::ParseError {
                context: "c".to_string(),
                source: serde_json::from_str::<serde_json::Value>("{").unwrap_err(),
            },
            OxidizedError::HttpError {
                status_code: 502,
                context: "c".to_string(),
            },
        ];

        for error in errors {
            let message = error.to_llm_message();

            // All messages must contain all four prefixes
            assert!(
                message.contains(ERROR_PREFIX),
                "Message missing ERROR_PREFIX: {}",
                message
            );
            assert!(
                message.contains(CONTEXT_PREFIX),
                "Message missing CONTEXT_PREFIX: {}",
                message
            );
            assert!(
                message.contains(SUGGESTIONS_PREFIX),
                "Message missing SUGGESTIONS_PREFIX: {}",
                message
            );
            assert!(
                message.contains(NEXT_STEP_PREFIX),
                "Message missing NEXT_STEP_PREFIX: {}",
                message
            );
        }
    }

    // -------------------------------------------------------------------------
    // Helper Functions
    // -------------------------------------------------------------------------

    // Creates an ApiUnreachable error for testing.
    //
    // Note: reqwest::Error has no public constructor, so we must create a real
    // connection error. We use a minimal runtime and connect to port 0 which
    // is guaranteed to fail immediately. This approach is acceptable for tests
    // as the runtime creation overhead is negligible (~1ms per test).
    fn create_api_unreachable_error(attempt: u8, last_success: Option<String>) -> OxidizedError {
        let runtime = tokio::runtime::Runtime::new().expect("Failed to create test runtime");
        let error =
            runtime.block_on(async { reqwest::get("http://127.0.0.1:0").await.unwrap_err() });

        OxidizedError::ApiUnreachable {
            source: error,
            attempt,
            last_success,
        }
    }
}