Files
optimclaw/tests/e2e_tool_coverage.rs
T
cf96a3253c fix(tests): replace hardcoded /tmp paths with tempdir + add 300 unit tests (#659)
* test: add unit tests across 20 modules for coverage push

Add 300+ unit tests covering config, context, evaluation, extensions,
LLM, secrets, tools/builder, and tools/mcp modules. All tests are
pure unit tests (no mocks) exercising serde roundtrips, edge cases,
error paths, and business logic.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(tests): replace hardcoded /tmp paths with tempfile::tempdir

The e2e_metrics_test::test_metrics_collected_from_tool_trace test was
failing because setup_test_dir() created /tmp/ironclaw_metrics_test but
the fixture referenced /tmp/ironclaw_e2e_test/hello.txt (path mismatch).

Added LlmTrace::replace_paths() to substitute fixture paths at runtime,
then converted all 12 test files from hardcoded /tmp/ironclaw_* paths to
tempfile::tempdir(). Tests are now isolated, parallel-safe, and leave no
debris on disk.

Regression test: test_metrics_collected_from_tool_trace now passes
consistently regardless of prior /tmp state.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
2026-03-07 08:24:24 +00:00

196 lines
6.6 KiB
Rust

//! E2E trace tests: tool coverage.
//!
//! Exercises tools that were previously untested: json, shell, list_dir,
//! apply_patch, memory_read, and memory_tree.
#[cfg(feature = "libsql")]
mod support;
#[cfg(feature = "libsql")]
mod tests {
use std::time::Duration;
use crate::support::test_rig::TestRigBuilder;
use crate::support::trace_llm::LlmTrace;
// -----------------------------------------------------------------------
// json tool
// -----------------------------------------------------------------------
#[tokio::test]
async fn test_json_operations() {
let trace = LlmTrace::from_file(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/llm_traces/coverage/json_operations.json"
))
.expect("failed to load json_operations.json");
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("Parse and query this json data").await;
let responses = rig.wait_for_responses(1, Duration::from_secs(15)).await;
rig.verify_trace_expects(&trace, &responses);
// Extra: verify json tool was called at least 3 times.
let started = rig.tool_calls_started();
assert!(
started.iter().filter(|n| n.as_str() == "json").count() >= 3,
"Expected at least 3 json tool calls, got: {:?}",
started
);
// Extra: metrics checks.
let metrics = rig.collect_metrics().await;
assert!(
metrics.llm_calls >= 4,
"Expected >= 4 LLM calls, got {}",
metrics.llm_calls
);
rig.shutdown();
}
// -----------------------------------------------------------------------
// shell tool
// -----------------------------------------------------------------------
#[tokio::test]
async fn test_shell_echo() {
let trace = LlmTrace::from_file(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/llm_traces/coverage/shell_echo.json"
))
.expect("failed to load shell_echo.json");
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("Run a shell command for me").await;
let responses = rig.wait_for_responses(1, Duration::from_secs(15)).await;
rig.verify_trace_expects(&trace, &responses);
rig.shutdown();
}
// -----------------------------------------------------------------------
// list_dir tool
// -----------------------------------------------------------------------
#[tokio::test]
async fn test_list_dir() {
let tmp = tempfile::tempdir().expect("failed to create tempdir");
let test_dir = tmp.path().join("test_dir");
std::fs::create_dir_all(&test_dir).unwrap();
std::fs::write(test_dir.join("file_a.txt"), "content a").unwrap();
std::fs::write(test_dir.join("file_b.txt"), "content b").unwrap();
let mut trace = LlmTrace::from_file(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/llm_traces/coverage/list_dir.json"
))
.expect("failed to load list_dir.json");
trace.replace_paths(
"/tmp/ironclaw_coverage_test_list_dir",
test_dir.to_str().unwrap(),
);
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("List the test directory").await;
let responses = rig.wait_for_responses(1, Duration::from_secs(15)).await;
rig.verify_trace_expects(&trace, &responses);
rig.shutdown();
}
// -----------------------------------------------------------------------
// apply_patch tool
// -----------------------------------------------------------------------
#[tokio::test]
async fn test_apply_patch_chain() {
let tmp = tempfile::tempdir().expect("failed to create tempdir");
let test_dir = tmp.path().join("test_dir");
std::fs::create_dir_all(&test_dir).unwrap();
let mut trace = LlmTrace::from_file(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/llm_traces/coverage/apply_patch_chain.json"
))
.expect("failed to load apply_patch_chain.json");
trace.replace_paths(
"/tmp/ironclaw_coverage_test_apply_patch",
test_dir.to_str().unwrap(),
);
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("Write a file and patch it").await;
let responses = rig.wait_for_responses(1, Duration::from_secs(15)).await;
rig.verify_trace_expects(&trace, &responses);
// Extra: verify the patch was applied on disk.
let content = std::fs::read_to_string(test_dir.join("patch_target.txt"))
.expect("patch_target.txt should exist");
assert!(
content.contains("PATCHED"),
"Expected 'PATCHED' in file content, got: {content:?}"
);
assert!(
!content.contains("original"),
"Expected 'original' to be replaced, but it still exists in: {content:?}"
);
// Extra: metrics checks.
let metrics = rig.collect_metrics().await;
assert!(metrics.llm_calls >= 4, "Expected >= 4 LLM calls");
assert!(metrics.total_tool_calls() >= 3, "Expected >= 3 tool calls");
rig.shutdown();
}
// -----------------------------------------------------------------------
// memory_read + memory_tree (full memory cycle)
// -----------------------------------------------------------------------
#[tokio::test]
async fn test_memory_full_cycle() {
let trace = LlmTrace::from_file(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/llm_traces/coverage/memory_full_cycle.json"
))
.expect("failed to load memory_full_cycle.json");
let rig = TestRigBuilder::new()
.with_trace(trace.clone())
.build()
.await;
rig.send_message("Exercise all four memory operations")
.await;
let responses = rig.wait_for_responses(1, Duration::from_secs(15)).await;
rig.verify_trace_expects(&trace, &responses);
// Extra: metrics checks.
let metrics = rig.collect_metrics().await;
assert!(metrics.llm_calls >= 5, "Expected >= 5 LLM calls");
assert!(metrics.total_tool_calls() >= 4, "Expected >= 4 tool calls");
rig.shutdown();
}
}