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"
);
}
#[test]
fn every_published_code_is_unique_and_in_the_jsonrpc_server_range() {
use std::collections::HashMap;
let mut seen: HashMap<i32, ErrorCode> = HashMap::new();
for &code in ErrorCode::ALL {
let n = code.code();
if let Some(&prior) = seen.get(&n) {
panic!(
"error code {n} is claimed by BOTH {} and {} — a client cannot \
branch on it, so one of them must be renumbered to a free value",
prior.machine_code(),
code.machine_code()
);
}
seen.insert(n, code);
assert!(
code.is_jsonrpc_reserved(),
"{} = {n} lies outside the JSON-RPC implementation-defined server range \
(-32768..=-32000) this crate publishes",
code.machine_code()
);
}
assert_eq!(
seen.len(),
ErrorCode::ALL.len(),
"every variant in ALL must contribute a distinct number"
);
}
#[test]
fn content_miss_inconclusive_does_not_squat_range_metadata_unrepresentable() {
let inconclusive = ErrorCode::ContentMissInconclusive;
assert_eq!(inconclusive.code(), -32015);
assert_ne!(
inconclusive.code(),
ErrorCode::RangeMetadataUnrepresentable.code(),
"-32009 is RANGE_METADATA_UNREPRESENTABLE, which is holder-FATAL; this code \
means the opposite (keep looking, this holder is still eligible)"
);
assert!(
!ONION_BAND.contains(&inconclusive.code()),
"must not fall inside the published onion band"
);
assert_eq!(inconclusive.machine_code(), "CONTENT_MISS_INCONCLUSIVE");
}