use error_forge::{registry::register_error_code, AppError, ForgeError};
#[test]
fn test_error_with_code() {
let error = AppError::config("Invalid configuration").with_code("CONFIG-001");
assert_eq!(
error.to_string(),
"[CONFIG-001] ⚙️ Configuration Error: Invalid configuration"
);
assert_eq!(error.kind(), "Config"); }
#[test]
fn test_register_and_retrieve_code() {
let _ = register_error_code(
"TEST-001",
"Test error code",
Some("https://docs.example.com/errors/TEST-001"),
true,
);
let error = AppError::network("Connection timeout", None).with_code("TEST-001");
assert!(error.to_string().starts_with("[TEST-001]"));
assert!(error.is_retryable()); }
#[test]
fn test_error_code_chaining() {
let error = AppError::filesystem("Permission denied", None)
.with_status(403)
.with_code("PERM-001")
.with_fatal(true);
assert!(error.to_string().contains("[PERM-001]"));
assert!(error.is_fatal());
assert_eq!(error.status_code(), 403);
}
#[test]
fn test_duplicate_registration() {
let result1 = register_error_code("UNIQUE-001", "First registration", None::<String>, false);
let result2 = register_error_code("UNIQUE-001", "Second registration", None::<String>, true);
assert!(result1.is_ok());
assert!(result2.is_err());
assert!(result2.unwrap_err().contains("already registered"));
}
#[test]
fn test_error_code_in_dev_message() {
let _ = register_error_code(
"DOC-001",
"Error with documentation",
Some("https://example.com/docs/errors/DOC-001"),
false,
);
let error = AppError::config("Missing required field").with_code("DOC-001");
let dev_message = error.dev_message();
assert!(dev_message.contains("[DOC-001]"));
assert!(dev_message.contains("https://example.com/docs/errors/DOC-001"));
}
#[test]
fn test_coded_error_instance_overrides() {
let _ = register_error_code(
"OVERRIDE-001",
"Retryable registry code",
None::<String>,
true,
);
let error = AppError::network("Connection timeout", None)
.with_code("OVERRIDE-001")
.with_retryable(false)
.with_status(429)
.with_fatal(true);
assert!(!error.is_retryable());
assert_eq!(error.status_code(), 429);
assert!(error.is_fatal());
}