TestingΒΆ

Testing strategies for Perspt development.

Running TestsΒΆ

# All tests
cargo test --all

# Specific crate
cargo test -p perspt-agent

# Specific test
cargo test test_name

# With output
cargo test -- --nocapture

Test OrganizationΒΆ

Each crate has its own tests:

crates/
└── perspt-agent/
    └── src/
        β”œβ”€β”€ orchestrator.rs
        └── orchestrator_test.rs  # Unit tests
    └── tests/
        └── integration_test.rs   # Integration tests

Unit TestsΒΆ

Test individual functions in the same file:

// orchestrator.rs

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_energy_calculation() {
        let energy = Energy::compute(
            &[Diagnostic::error("test")],
            &TestResults::default(),
            1.0, 0.5, 2.0,
        );
        assert!(energy.v_syn > 0.0);
    }

    #[tokio::test]
    async fn test_sheafification() {
        let plan = TaskPlan::from_prompt("Create file").await.unwrap();
        assert!(!plan.nodes.is_empty());
    }
}

Integration TestsΒΆ

Test crate interactions in tests/:

// tests/integration_test.rs

use perspt_agent::SRBNOrchestrator;
use perspt_core::GenAIProvider;
use std::sync::Arc;

#[tokio::test]
async fn test_full_workflow() {
    let provider = Arc::new(GenAIProvider::new().unwrap());
    let mut orchestrator = SRBNOrchestrator::new(
        provider,
        "./test_workspace".into(),
        Default::default(),
    ).await.unwrap();

    let result = orchestrator.execute("Create hello.py").await;
    assert!(result.is_ok());
}

MockingΒΆ

Use mockall for mocking:

use mockall::mock;

mock! {
    pub LspClient {
        async fn get_diagnostics(&self, path: &Path) -> Result<Vec<Diagnostic>>;
    }
}

#[tokio::test]
async fn test_with_mock_lsp() {
    let mut mock_lsp = MockLspClient::new();
    mock_lsp
        .expect_get_diagnostics()
        .returning(|_| Ok(vec![]));

    // Use mock in test
}

CoverageΒΆ

# Install tarpaulin
cargo install cargo-tarpaulin

# Run with coverage
cargo tarpaulin --out Html

# Open report
open tarpaulin-report.html

Documentation TestsΒΆ

Test code examples in docs:

cargo test --doc

BenchmarksΒΆ

Performance benchmarks in benches/:

// benches/energy_bench.rs

use criterion::{criterion_group, criterion_main, Criterion};
use perspt_agent::Energy;

fn energy_benchmark(c: &mut Criterion) {
    c.bench_function("energy_compute", |b| {
        b.iter(|| Energy::compute(&[], &Default::default(), 1.0, 0.5, 2.0))
    });
}

criterion_group!(benches, energy_benchmark);
criterion_main!(benches);
cargo bench

CI TestingΒΆ

Tests run in GitHub Actions on every PR:

# .github/workflows/test.yml
name: Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo test --all

See AlsoΒΆ