Testing LLM-based applications presents unique challenges due to the non-deterministic nature of AI models. Agent Air promotes a “Mock-First” approach.

Unit Testing Tools

Since tools implement the Executable trait, they are easy to test in isolation.

#[tokio::test]
async fn test_calculator_tool() {
    let tool = CalculatorTool::new();
    let input = json!({ "expression": "2 + 2" });
    let context = ToolContext::mock(); // Helper to create a dummy context
    
    let result = tool.execute(context, input).await;
    assert_eq!(result.output, "4");
}

Mocking the LLM

To test the controller logic without spending money on API tokens, use the MockProvider.

let provider = MockProvider::new()
    .expect_request("Hello")
    .return_response("Hi there!");

let agent = Agent::builder()
    .with_provider(provider)
    .build();
    
// Run agent and verify it handles the mocked response correctly

Integration Tests

For full system tests, we recommend recording actual LLM interactions and replaying them (VCR-style) to ensure regression testing is deterministic.