Skip to content

Commit 104e198

Browse files
committed
test(tap-agent): add SimpleSubgraphClient abstraction
1 parent 49553e6 commit 104e198

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

crates/tap-agent/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub mod cli;
3636
pub mod database;
3737
/// Prometheus Metrics server
3838
pub mod metrics;
39+
pub mod subgraph_client_simple;
3940
pub mod tap;
4041
pub mod task_lifecycle;
4142

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//! Simple SubgraphClient Abstraction for Testing
2+
//!
3+
//! This provides a minimal abstraction to enable Layer 2 integration testing
4+
//! without the complexity of async trait objects.
5+
6+
use crate::agent::sender_accounts_manager::AllocationId;
7+
use anyhow::Result;
8+
use indexer_monitor::SubgraphClient;
9+
use std::sync::Arc;
10+
11+
/// Simple enum wrapper for different SubgraphClient implementations
12+
/// This solves the dependency injection problem for testing
13+
#[derive(Clone)]
14+
pub enum SimpleSubgraphClient {
15+
/// Production implementation using real SubgraphClient
16+
Production(Arc<SubgraphClient>),
17+
/// Mock implementation for testing
18+
Mock(SimpleSubgraphMock),
19+
}
20+
21+
impl SimpleSubgraphClient {
22+
/// Create a production client wrapper
23+
pub fn production(client: Arc<SubgraphClient>) -> Self {
24+
Self::Production(client)
25+
}
26+
27+
/// Create a mock client for testing
28+
pub fn mock(mock: SimpleSubgraphMock) -> Self {
29+
Self::Mock(mock)
30+
}
31+
32+
/// Validate allocation status for receipt processing
33+
/// This is the key operation that SenderAllocationTask needs
34+
pub async fn validate_allocation(&self, _allocation_id: &AllocationId) -> Result<bool> {
35+
match self {
36+
Self::Production(_client) => {
37+
// TODO: Implement real allocation validation logic
38+
// This would query the network subgraph to check allocation status
39+
Ok(true)
40+
}
41+
Self::Mock(mock) => Ok(mock.should_validate_allocation),
42+
}
43+
}
44+
45+
/// Check if the subgraph client is healthy and ready
46+
pub async fn is_healthy(&self) -> bool {
47+
match self {
48+
Self::Production(_client) => {
49+
// TODO: Implement health check logic
50+
// This could query a simple health endpoint or check connectivity
51+
true
52+
}
53+
Self::Mock(mock) => mock.is_healthy,
54+
}
55+
}
56+
}
57+
58+
/// Simple mock for testing SubgraphClient behavior
59+
#[derive(Clone)]
60+
pub struct SimpleSubgraphMock {
61+
/// Controls whether allocation validation succeeds
62+
pub should_validate_allocation: bool,
63+
/// Controls whether the client appears healthy
64+
pub is_healthy: bool,
65+
}
66+
67+
impl SimpleSubgraphMock {
68+
/// Create a new mock with default settings
69+
pub fn new() -> Self {
70+
Self {
71+
should_validate_allocation: true,
72+
is_healthy: true,
73+
}
74+
}
75+
76+
/// Configure the mock to simulate allocation validation failures
77+
pub fn with_allocation_validation(mut self, should_validate: bool) -> Self {
78+
self.should_validate_allocation = should_validate;
79+
self
80+
}
81+
82+
/// Configure the mock to simulate health check results
83+
pub fn with_health_status(mut self, is_healthy: bool) -> Self {
84+
self.is_healthy = is_healthy;
85+
self
86+
}
87+
}
88+
89+
impl Default for SimpleSubgraphMock {
90+
fn default() -> Self {
91+
Self::new()
92+
}
93+
}
94+
95+
#[cfg(test)]
96+
mod tests {
97+
use super::*;
98+
use crate::agent::sender_accounts_manager::AllocationId;
99+
use thegraph_core::alloy::primitives::Address;
100+
101+
#[tokio::test]
102+
async fn test_mock_allocation_validation_success() {
103+
let mock = SimpleSubgraphMock::new().with_allocation_validation(true);
104+
let client = SimpleSubgraphClient::mock(mock);
105+
106+
let test_address = Address::from([0x42; 20]);
107+
let allocation_id = AllocationId::Legacy(test_address.into());
108+
let result = client.validate_allocation(&allocation_id).await.unwrap();
109+
110+
assert!(result);
111+
}
112+
113+
#[tokio::test]
114+
async fn test_mock_allocation_validation_failure() {
115+
let mock = SimpleSubgraphMock::new().with_allocation_validation(false);
116+
let client = SimpleSubgraphClient::mock(mock);
117+
118+
let test_address = Address::from([0x42; 20]);
119+
let allocation_id = AllocationId::Legacy(test_address.into());
120+
let result = client.validate_allocation(&allocation_id).await.unwrap();
121+
122+
assert!(!result);
123+
}
124+
125+
#[tokio::test]
126+
async fn test_mock_health_check() {
127+
let healthy_mock = SimpleSubgraphMock::new().with_health_status(true);
128+
let healthy_client = SimpleSubgraphClient::mock(healthy_mock);
129+
130+
let unhealthy_mock = SimpleSubgraphMock::new().with_health_status(false);
131+
let unhealthy_client = SimpleSubgraphClient::mock(unhealthy_mock);
132+
133+
assert!(healthy_client.is_healthy().await);
134+
assert!(!unhealthy_client.is_healthy().await);
135+
}
136+
}

0 commit comments

Comments
 (0)