use dig_rpc_protocol::error::ErrorCode;
const ONION_BAND: std::ops::RangeInclusive<i32> = -32022..=-32020;
#[test]
fn range_metadata_unrepresentable_is_outside_the_onion_band() {
let code = ErrorCode::RangeMetadataUnrepresentable.code();
assert_ne!(code, -32021, "-32021 is PRIVACY_REQUIRES_LOCAL_NODE");
assert!(
!ONION_BAND.contains(&code),
"code {code} falls inside the published onion band {ONION_BAND:?}"
);
assert_eq!(
ErrorCode::RangeMetadataUnrepresentable.machine_code(),
"RANGE_METADATA_UNREPRESENTABLE"
);
assert!(
ErrorCode::RangeMetadataUnrepresentable.is_jsonrpc_reserved(),
"the code must live in the JSON-RPC implementation-defined server band"
);
}
#[test]
fn the_rustdoc_taxonomy_table_lists_every_code() {
const ERROR_MODULE_SOURCE: &str = include_str!("../src/error.rs");
let table: String = ERROR_MODULE_SOURCE
.lines()
.take_while(|line| line.starts_with("//!"))
.collect::<Vec<_>>()
.join("\n");
for code in ErrorCode::ALL {
let row = format!("| `{}` | [`{:?}`]", code.code(), code);
assert!(
table.contains(&row),
"the rustdoc taxonomy table is missing a row for {} ({}); expected to find {row:?}",
code.machine_code(),
code.code()
);
}
}
#[test]
fn range_metadata_unrepresentable_is_published_in_openrpc() {
let doc = dig_rpc_protocol::openrpc::openrpc_document("0.0.0-test");
let catalogue = doc["components"]["x-dig-errors"]
.as_array()
.expect("error catalogue is an array");
let entry = catalogue
.iter()
.find(|e| e["machineCode"] == "RANGE_METADATA_UNREPRESENTABLE")
.expect("RANGE_METADATA_UNREPRESENTABLE is absent from the OpenRPC catalogue");
assert_eq!(
entry["code"],
ErrorCode::RangeMetadataUnrepresentable.code(),
"the published number must match the enum"
);
}