Skip to content

TDD Workflow

Test-Driven Development is required for all changes to Arbiter-Bot.

Red-Green-Refactor

Follow the classic TDD cycle:

  1. Red - Write a failing test that defines expected behavior
  2. Green - Implement the minimum code to pass the test
  3. Refactor - Clean up while keeping tests green

Test Organization

Tests are inline within modules under #[cfg(test)]:

// src/execution/detector.rs

pub struct ArbDetector;

impl ArbDetector {
    pub fn detect(&self, book_a: &OrderBook, book_b: &OrderBook) -> Option<Opportunity> {
        // Implementation
    }
}

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

    #[test]
    fn test_no_arb_when_spread_negative() {
        let detector = ArbDetector;
        let book_a = mock_orderbook(0.45, 0.46);
        let book_b = mock_orderbook(0.55, 0.56);

        assert!(detector.detect(&book_a, &book_b).is_none());
    }

    #[test]
    fn test_detects_profitable_spread() {
        let detector = ArbDetector;
        let book_a = mock_orderbook(0.40, 0.41);  // Buy at 0.41
        let book_b = mock_orderbook(0.62, 0.63);  // Sell at 0.62

        let opp = detector.detect(&book_a, &book_b);
        assert!(opp.is_some());
        assert!(opp.unwrap().expected_profit > 0.0);
    }
}

Async Tests

Use #[tokio::test] for async code:

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

    #[tokio::test]
    async fn test_actor_processes_message() {
        let (tx, rx) = tokio::sync::mpsc::channel(10);
        let actor = TestActor::new();

        tx.send(TestMsg::Ping).await.unwrap();

        let result = actor.handle(rx.recv().await.unwrap()).await;
        assert!(result.is_ok());
    }
}

Running Tests

# Run all tests
cargo test --manifest-path arbiter-engine/Cargo.toml

# Run specific test
cargo test --manifest-path arbiter-engine/Cargo.toml test_happy_path

# Run tests for a module
cargo test --manifest-path arbiter-engine/Cargo.toml execution::state_machine

# Run with output visible
cargo test --manifest-path arbiter-engine/Cargo.toml -- --nocapture

Test Coverage

Current test coverage by module:

Module Tests Focus
execution/state_machine.rs 4 Happy path, leg failures, compensation
execution/detector.rs 2 No-arb and profitable spread detection
execution/hedge.rs 2 Side reversal logic
market/mapping.rs 2 Safety gate for verified mappings
market/polymarket.rs 1 JSON parsing
actors/traits.rs 1 Actor lifecycle

Before Committing

  1. All tests must pass: cargo test
  2. New code must have corresponding tests
  3. Tests should cover both happy path and error cases