use async_trait::async_trait;
use crate::interpreter::ExecResult;
use crate::tools::{ExecContext, Tool, ToolArgs, ToolSchema};
pub struct True;
#[async_trait]
impl Tool for True {
fn name(&self) -> &str {
"true"
}
fn schema(&self) -> ToolSchema {
ToolSchema::new("true", "Exit with success (code 0)")
.example("Always succeeds", "true")
}
async fn execute(&self, _args: ToolArgs, _ctx: &mut ExecContext) -> ExecResult {
ExecResult::success("")
}
}
pub struct False;
#[async_trait]
impl Tool for False {
fn name(&self) -> &str {
"false"
}
fn schema(&self) -> ToolSchema {
ToolSchema::new("false", "Exit with failure (code 1)")
.example("Always fails", "false")
}
async fn execute(&self, _args: ToolArgs, _ctx: &mut ExecContext) -> ExecResult {
ExecResult::failure(1, "")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::Value;
use crate::vfs::{MemoryFs, VfsRouter};
use std::sync::Arc;
#[tokio::test]
async fn true_returns_success() {
let mut vfs = VfsRouter::new();
vfs.mount("/", MemoryFs::new());
let mut ctx = ExecContext::new(Arc::new(vfs));
let result = True.execute(ToolArgs::new(), &mut ctx).await;
assert!(result.ok());
assert_eq!(result.code, 0);
}
#[tokio::test]
async fn true_with_args_still_succeeds() {
let mut vfs = VfsRouter::new();
vfs.mount("/", MemoryFs::new());
let mut ctx = ExecContext::new(Arc::new(vfs));
let mut args = ToolArgs::new();
args.positional.push(Value::String("ignored".into()));
let result = True.execute(args, &mut ctx).await;
assert!(result.ok());
assert_eq!(result.code, 0);
}
#[tokio::test]
async fn false_returns_failure() {
let mut vfs = VfsRouter::new();
vfs.mount("/", MemoryFs::new());
let mut ctx = ExecContext::new(Arc::new(vfs));
let result = False.execute(ToolArgs::new(), &mut ctx).await;
assert!(!result.ok());
assert_eq!(result.code, 1);
}
#[tokio::test]
async fn false_with_args_still_fails() {
let mut vfs = VfsRouter::new();
vfs.mount("/", MemoryFs::new());
let mut ctx = ExecContext::new(Arc::new(vfs));
let mut args = ToolArgs::new();
args.positional.push(Value::String("ignored".into()));
let result = False.execute(args, &mut ctx).await;
assert!(!result.ok());
assert_eq!(result.code, 1);
}
}