#![cfg(feature = "schema")]
use plexus_client::{DenyReason, Note, ParamProblem, RemoteError};
use schemars::{schema_for, JsonSchema};
use serde_json::Value;
fn implements_json_schema<T: JsonSchema>() {}
#[test]
fn the_vocabulary_implements_json_schema() {
implements_json_schema::<Note>();
implements_json_schema::<RemoteError>();
implements_json_schema::<ParamProblem>();
implements_json_schema::<DenyReason>();
}
#[test]
fn note_schema_requires_only_message() {
let schema = serde_json::to_value(schema_for!(Note)).unwrap();
assert_eq!(
schema["required"],
serde_json::json!(["message"]),
"{schema:#}"
);
assert!(schema["properties"]["percentage"].is_object(), "{schema:#}");
}
#[test]
fn remote_error_schema_names_every_kind() {
let text = serde_json::to_string(&schema_for!(RemoteError)).unwrap();
for kind in [
"activation_not_found",
"method_not_found",
"invalid_params",
"unauthenticated",
"forbidden",
"handle_not_supported",
"execution",
"missing",
"malformed",
"missing_scope",
"tenant_boundary",
"not_accepted",
] {
assert!(
text.contains(&format!("\"{kind}\"")),
"{kind} missing from {text}"
);
}
let schema: Value = serde_json::from_str(&text).unwrap();
assert!(schema.is_object());
}
#[test]
fn remote_error_schema_is_one_synapse_reads() {
let schema = serde_json::to_value(schema_for!(RemoteError)).unwrap();
let defs = &schema["$defs"];
let members = schema["anyOf"].as_array().expect("an anyOf");
let refs: Vec<&str> = members.iter().map(|m| m["$ref"].as_str().expect("each member a $ref")).collect();
assert_eq!(refs, ["#/$defs/KnownRemoteError", "#/$defs/OtherError"]);
let known = defs["KnownRemoteError"]["oneOf"].as_array().expect("the known kinds a oneOf");
assert_eq!(known.len(), 10);
for v in known {
assert!(v["properties"]["kind"]["const"].is_string(), "every known kind a const: {v}");
}
let other = &defs["OtherError"];
assert_eq!(other["properties"]["kind"]["type"], "string");
assert!(other.get("additionalProperties").is_none(), "no map beside properties: {other}");
}