Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(tokenjuice): add generate tests for tokenjuice module

Steven Enamakel03667f64b62d
diff --git a/crates/openhuman-core/src/inference/tokenjuice/generate_tests.rs b/crates/openhuman-core/src/inference/tokenjuice/generate_tests.rs
new file mode 100644
index 0000000000..c6e30ba570
--- /dev/null
+++ b/crates/openhuman-core/src/inference/tokenjuice/generate_tests.rs
@@ -0,0 +1,52 @@
+use super::*;
+
+fn request(token: &str) -> GenerateRequest {
+ GenerateRequest {
+ context_token: token.to_string(),
+ purpose: "tool_output_summary".into(),
+ system: "system".into(),
+ prompt: "prompt".into(),
+ max_output_tokens: 16,
+ }
+}
+
+#[tokio::test]
+async fn an_unknown_token_declines() {
+ assert_eq!(serve(request("never-registered")).await, Ok(None));
+}
+
+#[tokio::test]
+async fn a_registered_call_runs_once_with_the_request() {
+ let ticket = register(Box::new(|request: GenerateRequest| {
+ Box::pin(async move { Ok(format!("{} / {}", request.system, request.prompt)) })
+ }));
+ let token = ticket.token().to_string();
+ assert_eq!(
+ serve(request(&token)).await,
+ Ok(Some("system / prompt".to_string()))
+ );
+ // Used up: a second call for the same token declines.
+ assert_eq!(serve(request(&token)).await, Ok(None));
+}
+
+#[tokio::test]
+async fn a_dropped_ticket_withdraws_its_call() {
+ let ticket = register(Box::new(|_| {
+ Box::pin(async { panic!("a withdrawn call must not run") })
+ }));
+ let token = ticket.token().to_string();
+ drop(ticket);
+ assert_eq!(serve(request(&token)).await, Ok(None));
+}
+
+#[tokio::test]
+async fn a_failed_model_call_is_an_error() {
+ let ticket = register(Box::new(|_| {
+ Box::pin(async { Err(anyhow::anyhow!("provider offline")) })
+ }));
+ let token = ticket.token().to_string();
+ assert_eq!(
+ serve(request(&token)).await,
+ Err("provider offline".to_string())
+ );
+}