Skip to content

Commit

Permalink
all tests are passing with some json stuff going on
Browse files Browse the repository at this point in the history
  • Loading branch information
carllippert committed May 25, 2024
1 parent fd33068 commit ba2a019
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 46 deletions.
1 change: 1 addition & 0 deletions plugin-core/crates/pdk-mock-host-functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ crate_type = ["cdylib"]

[dependencies]
extism-pdk = "1.1.0"
anything-pdk = { path = "../pdk" }
serde = { version = "1.0.201", features = ["derive"] }
serde_json = "1.0.117"
25 changes: 13 additions & 12 deletions plugin-core/crates/pdk-mock-host-functions/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use anything_pdk::{AnythingPlugin, Event, Handle, Log};
use extism_pdk::Memory;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct Log {
pub time: String,
pub message: String,
}
// #[derive(Deserialize, Serialize)]
// struct Log {
// pub time: String,
// pub message: String,
// }

#[derive(Deserialize, Serialize)]
struct Event {
pub id: String,
pub name: String,
pub description: String,
pub timestamp: String,
}
// #[derive(Deserialize, Serialize)]
// struct Event {
// pub id: String,
// pub name: String,
// pub description: String,
// pub timestamp: String,
// }

#[no_mangle]
pub extern "C" fn host_log(log_ptr: i64) -> i64 {
Expand Down
2 changes: 0 additions & 2 deletions plugin-core/crates/pdk-mock-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ use serde_json::Value;
#[host_fn]
extern "ExtismHost" {
fn host_log(log: String) -> Json<Log>;
fn create_event(event: String) -> Json<Event>;
}

#[plugin_fn]
pub fn execute(config: Value) -> FnResult<Value> {
// let _res = unsafe { host_log(message.clone())? };
Ok(config)
}

Expand Down
69 changes: 53 additions & 16 deletions plugin-core/crates/pdk-mock-simple-host/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
use anything_pdk::AnythingPlugin;
use anything_pdk::{AnythingPlugin, Event};
use extism::*;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
use crate::convert::Json;

// Define the Event struct
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Event {
pub id: String,
pub name: String,
pub description: String,
pub timestamp: String,
}
// #[derive(Deserialize, Serialize, Debug, Clone)]
// pub struct Event {
// pub id: String,
// pub name: String,
// pub description: String,
// pub timestamp: String,
// }

// Define a simple counter to track the number of events
type EventCounter = usize;
// Implement the create_event host function
host_fn!(create_event(user_data: EventCounter; _id: String, _name: String, _description: String, _timestamp: String) {
let counter = user_data.get()?;
let mut counter = counter.lock().unwrap();
*counter += 1;
Ok(())
host_fn!(create_event(user_data: EventCounter; event: Json<Event>) -> String {
// let counter = user_data.get()?;
// let mut counter = counter.lock().unwrap();
// *counter += 1;
Ok("Success".to_string())
});

// #[no_mangle]
// pub extern "C" fn create_event(event_ptr: i64) -> i64 {
// // Find the memory at the given pointer
// let event_mem = Memory::find(event_ptr as u64).expect("can't find event memory");

// // Convert memory to a string
// let event_data = event_mem.to_string().expect("bad data?");

// // Deserialize the event data to the Event struct
// let event: Event = serde_json::from_str(&event_data).expect("deserialization failed");

// // Here you can perform any operations you need with the event
// // For example, log the event details or store them somewhere
// // println!("Received event: {:?}", event);

// // Serialize the event back to JSON
// let output = serde_json::to_vec(&event).expect("serialization failed");

// // Create new memory for the output
// let output_mem = Memory::from_bytes(&output);

// // Return the offset of the new memory as an i64
// return output_mem.expect("cant return offset").offset() as i64;
// }

fn main() {
println!("Run `cargo test` to execute the tests.");
}
Expand Down Expand Up @@ -82,8 +108,8 @@ mod tests {
.with_wasi(false)
.with_function(
"create_event",
[ValType::I32, ValType::I32, ValType::I32, ValType::I32],
[],
[ValType::I64],
[ValType::I64],
event_count.clone(), //Resources liek sql that need to be piped into implementations
create_event, //the host function
)
Expand Down Expand Up @@ -221,9 +247,20 @@ mod tests {
.unwrap();
println!("Scheduled cron_execute_res {:?}", cron_execute_res);

sleep(Duration::from_secs(1)).await;
sleep(Duration::from_secs(2)).await;
}

let binding = event_count.get().unwrap();
let counter = binding.lock().unwrap();

println!("Counter: {:?}", *counter);
// Check if the cron plugin was triggered at least twice
// let counter = event_count.get().unwrap().lock().unwrap();
// assert!(
// *counter >= expected_minimum_triggers,
// "Cron Plugin was not triggered the expected number of times"
// );

println!("Cron Plugin Test Complete");
}
}
22 changes: 18 additions & 4 deletions plugin-core/crates/pdk-mock-trigger-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@ use serde_json::Value;

#[host_fn]
extern "ExtismHost" {
fn create_event(event: String) -> Json<Event>;
fn create_event(event: Json<Event>) -> String;
}

#[plugin_fn]
pub fn execute(config: Value) -> FnResult<Value> {
//TODO: Determine what events need to be created. Create Them
//TODO: host function for creating a real event not just stubbed string
let message = "Creating an event".to_string();
let _res = unsafe { create_event(message.clone())? };
Ok(config)
let event = Event {
id: "1".to_string(),
name: "Test Event".to_string(),
description: "This is a test event".to_string(),
timestamp: "2021-01-01T00:00:00Z".to_string(),
};

//Call Create Event
let _res = unsafe { create_event(Json(event))? };

let res = serde_json::json!({
"status": "success",
"output": {},
"error": {},
});

Ok(res)
}

#[plugin_fn]
Expand Down
40 changes: 28 additions & 12 deletions plugin-core/crates/plugins/anything-cron-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ use extism_pdk::*;
use serde::Deserialize;
use serde_json::{json, Value};


#[host_fn]
extern "ExtismHost" {
fn create_event(event: Json<Event>) -> String;
}

#[plugin_fn]
pub fn execute(config: Value) -> FnResult<Value> {
//TODO: add create event
let event = Event {
id: "1".to_string(),
name: "Test Event".to_string(),
description: "This is a test event".to_string(),
timestamp: "2021-01-01T00:00:00Z".to_string(),
};

//Call Create Event
let _res = unsafe { create_event(Json(event))? };

let res = serde_json::json!({
"status": "success",
"output": {},
"error": {},
});

Ok(res)
}

//Called when the plugin is loaded. This gives the host the needed information about the plugin.
//It also provides Information to generate a nice UI including icons and labels.
//Not all hosts will use all information ( likely )
Expand Down Expand Up @@ -49,15 +77,3 @@ pub fn register() -> FnResult<AnythingPlugin> {

Ok(plugin)
}

#[plugin_fn]
pub fn execute(config: Value) -> FnResult<Value> {
//TODO: add create event
//TODO: add to simple host
let res = serde_json::json!({
"status": "success",
"output": {},
"error": {},
});
Ok(res)
}

0 comments on commit ba2a019

Please sign in to comment.