use async_trait::async_trait;
use ye::{Ye, YePlugin, YePluginResultValue};
const EXAMPLE_DID_DOCUMENT_STR: &str = r###"{
"@context": "https://www.w3.org/ns/did/v1",
"id": "did:example:123456789abcdefghi"
}"###;
pub struct TestPlugin {}
impl TestPlugin {
pub fn new() -> Self {
TestPlugin {}
}
}
impl Default for TestPlugin {
fn default() -> Self {
TestPlugin::new()
}
}
#[async_trait(?Send)]
impl YePlugin for TestPlugin {
async fn did_create(
&mut self,
_did_method: &str,
_options: &str,
_payload: &str,
) -> Result<YePluginResultValue<Option<String>>, Box<dyn std::error::Error>> {
Ok(YePluginResultValue::Success(Some(
EXAMPLE_DID_DOCUMENT_STR.to_string(),
)))
}
async fn did_resolve(
&mut self,
_did: &str,
) -> Result<YePluginResultValue<Option<String>>, Box<dyn std::error::Error>> {
Ok(YePluginResultValue::Ignored)
}
async fn did_update(
&mut self,
_did: &str,
_options: &str,
_payload: &str,
) -> Result<YePluginResultValue<Option<String>>, Box<dyn std::error::Error>> {
Err(Box::from("yikes"))
}
}
#[tokio::test]
async fn ye_plugin_plugin_can_call_functions_implemented_in_plugin() {
let mut tp: TestPlugin = TestPlugin::new();
match tp.did_create("", "", "").await {
Ok(response) => match response {
YePluginResultValue::Success(result) => {
assert_eq!(result.unwrap(), EXAMPLE_DID_DOCUMENT_STR.to_string())
}
_ => panic!("unexpected result"),
},
Err(e) => panic!(format!("{}", e)),
}
}
#[tokio::test]
async fn ye_plugin_plugin_can_call_fallback_for_not_implemented() {
let mut tp: TestPlugin = TestPlugin::new();
match tp.vc_zkp_verify_proof("", "", "").await {
Ok(response) => {
assert!(matches!(response, YePluginResultValue::NotImplemented));
}
Err(e) => panic!(format!("{}", e)),
}
}
#[tokio::test]
async fn ye_plugin_ye_can_call_functions_implemented_in_plugin() {
let tp: TestPlugin = TestPlugin::new();
let mut ye = Ye::new();
ye.register_plugin(Box::from(tp));
match ye.did_create("", "", "").await {
Ok(results) => {
assert_eq!(
results[0].as_ref().unwrap().to_string(),
EXAMPLE_DID_DOCUMENT_STR.to_string()
);
println!("created did: {}", results[0].as_ref().unwrap().to_string());
}
Err(e) => panic!(format!("{}", e)),
};
}