use async_trait::async_trait;
use crate::domain::models::CreditsTransactionType;
use crate::domain::services::rate_limiting_service::{
BacklogService, ConcurrencyConfig, ConcurrencyControlService, ConcurrencyResult, QuotaService,
RateLimitConfig, RateLimitResult, RateLimitService, RateLimitingError, RateLimitingService,
};
#[derive(Debug, Clone, Default)]
pub struct NoopRateLimitingService;
impl NoopRateLimitingService {
pub fn new() -> Self {
Self
}
}
#[async_trait]
impl RateLimitService for NoopRateLimitingService {
async fn check_rate_limit(
&self,
_api_key: &str,
_endpoint: &str,
) -> Result<RateLimitResult, RateLimitingError> {
Ok(RateLimitResult::Allowed)
}
async fn get_team_rate_limit_config(
&self,
_team_id: crate::domain::id::Id,
) -> Result<RateLimitConfig, RateLimitingError> {
Ok(RateLimitConfig::default())
}
async fn update_team_rate_limit_config(
&self,
_team_id: crate::domain::id::Id,
_config: RateLimitConfig,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn cleanup_expired_rate_limits(&self) -> Result<u64, RateLimitingError> {
Ok(0)
}
}
#[async_trait]
impl ConcurrencyControlService for NoopRateLimitingService {
async fn check_team_concurrency(
&self,
_team_id: crate::domain::id::Id,
_task_id: crate::domain::id::Id,
) -> Result<ConcurrencyResult, RateLimitingError> {
Ok(ConcurrencyResult::Allowed)
}
async fn release_team_concurrency_slot(
&self,
_team_id: crate::domain::id::Id,
_task_id: crate::domain::id::Id,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn get_team_current_concurrency(
&self,
_team_id: crate::domain::id::Id,
) -> Result<u32, RateLimitingError> {
Ok(0)
}
async fn get_team_concurrency_config(
&self,
_team_id: crate::domain::id::Id,
) -> Result<ConcurrencyConfig, RateLimitingError> {
Ok(ConcurrencyConfig::default())
}
async fn update_team_concurrency_config(
&self,
_team_id: crate::domain::id::Id,
_config: ConcurrencyConfig,
) -> Result<(), RateLimitingError> {
Ok(())
}
}
#[async_trait]
impl BacklogService for NoopRateLimitingService {
async fn process_backlog_tasks(
&self,
_team_id: crate::domain::id::Id,
) -> Result<u32, RateLimitingError> {
Ok(0)
}
}
#[async_trait]
impl QuotaService for NoopRateLimitingService {
async fn check_and_deduct_quota(
&self,
_team_id: crate::domain::id::Id,
_amount: i64,
_transaction_type: CreditsTransactionType,
_description: String,
_reference_id: Option<crate::domain::id::Id>,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn refund_quota(
&self,
_team_id: crate::domain::id::Id,
_amount: i64,
_description: String,
_reference_id: Option<crate::domain::id::Id>,
) -> Result<(), RateLimitingError> {
Ok(())
}
async fn get_quota_balance(
&self,
_team_id: crate::domain::id::Id,
) -> Result<i64, RateLimitingError> {
Ok(i64::MAX)
}
}
#[async_trait]
impl RateLimitingService for NoopRateLimitingService {}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::models::CreditsTransactionType;
use crate::domain::services::rate_limiting_service::RateLimitResult;
#[tokio::test]
async fn test_noop_check_rate_limit_returns_allowed() {
let svc = NoopRateLimitingService::new();
let result = svc.check_rate_limit("test_api_key", "/v1/crawl").await;
assert!(result.is_ok(), "check_rate_limit should always succeed");
assert_eq!(
result.unwrap(),
RateLimitResult::Allowed,
"check_rate_limit should return Allowed (no rate limiting)"
);
}
#[tokio::test]
async fn test_noop_get_quota_balance_returns_max() {
let svc = NoopRateLimitingService::new();
let team_id = crate::domain::id::Id::generate();
let result = svc.get_quota_balance(team_id).await;
assert!(result.is_ok(), "get_quota_balance should always succeed");
assert_eq!(
result.unwrap(),
i64::MAX,
"get_quota_balance should return i64::MAX (unlimited quota)"
);
}
#[tokio::test]
async fn test_noop_check_and_deduct_quota_returns_ok() {
let svc = NoopRateLimitingService::new();
let team_id = crate::domain::id::Id::generate();
let result = svc
.check_and_deduct_quota(
team_id,
100,
CreditsTransactionType::Crawl,
"test deduction".to_string(),
None,
)
.await;
assert!(
result.is_ok(),
"check_and_deduct_quota should always succeed (no deduction)"
);
}
#[tokio::test]
async fn test_noop_process_backlog_tasks_returns_zero() {
let svc = NoopRateLimitingService::new();
let team_id = crate::domain::id::Id::generate();
let result = svc.process_backlog_tasks(team_id).await;
assert!(
result.is_ok(),
"process_backlog_tasks should always succeed"
);
assert_eq!(
result.unwrap(),
0,
"process_backlog_tasks should return 0 (no backlog processed)"
);
}
#[tokio::test]
async fn test_noop_process_backlog_tasks_idempotent_across_calls() {
let svc = NoopRateLimitingService::new();
let team_id = crate::domain::id::Id::generate();
for i in 0..3 {
let result = svc.process_backlog_tasks(team_id).await;
assert!(result.is_ok(), "call {} should succeed", i);
assert_eq!(
result.unwrap(),
0,
"call {} should return 0 (idempotent)",
i
);
}
}
}