use async_trait::async_trait;
use crate::domain::models::{Crawl, CrawlStatus, Task};
use crate::domain::repositories::crawl_repository::CrawlRepository;
use crate::domain::repositories::task_repository::RepositoryError;
use crate::domain::services::search_service::{
SearchQuery, SearchResponse, SearchResult, SearchServiceError, SearchServiceTrait,
};
use crate::queue::task_queue::{QueueError, TaskQueue};
pub struct MockSearchService;
#[async_trait]
impl SearchServiceTrait for MockSearchService {
async fn search(
&self,
_team_id: crate::domain::id::Id,
_api_key_id: crate::domain::id::Id,
query: SearchQuery,
) -> Result<SearchResponse, SearchServiceError> {
Ok(SearchResponse {
query: query.query,
results: vec![SearchResult {
title: "Mock Title".to_string(),
url: "https://example.com".to_string(),
description: Some("Mock desc".to_string()),
engine: "mock".to_string(),
score: None,
}],
crawl_id: None,
credits_used: 1,
})
}
}
pub struct MockTaskQueue;
#[async_trait]
impl TaskQueue for MockTaskQueue {
async fn enqueue(&self, task: Task) -> Result<Task, QueueError> {
Ok(task)
}
async fn dequeue(&self, _worker_id: crate::domain::id::Id) -> Result<Option<Task>, QueueError> {
Ok(None)
}
async fn complete(&self, _task_id: crate::domain::id::Id) -> Result<(), QueueError> {
Ok(())
}
async fn fail(&self, _task_id: crate::domain::id::Id) -> Result<(), QueueError> {
Ok(())
}
async fn cancel(&self, _task_id: crate::domain::id::Id) -> Result<(), QueueError> {
Ok(())
}
}
pub struct MockCrawlRepository;
#[async_trait]
impl CrawlRepository for MockCrawlRepository {
async fn create(&self, crawl: &Crawl) -> Result<Crawl, RepositoryError> {
Ok(crawl.clone())
}
async fn find_by_id(
&self,
_id: crate::domain::id::Id,
) -> Result<Option<Crawl>, RepositoryError> {
Ok(None)
}
async fn update(&self, crawl: &Crawl) -> Result<Crawl, RepositoryError> {
Ok(crawl.clone())
}
async fn increment_completed_tasks(
&self,
_id: crate::domain::id::Id,
) -> Result<(), RepositoryError> {
Ok(())
}
async fn increment_failed_tasks(
&self,
_id: crate::domain::id::Id,
) -> Result<(), RepositoryError> {
Ok(())
}
async fn update_status(
&self,
_id: crate::domain::id::Id,
_status: CrawlStatus,
) -> Result<(), RepositoryError> {
Ok(())
}
async fn increment_total_tasks(
&self,
_id: crate::domain::id::Id,
) -> Result<(), RepositoryError> {
Ok(())
}
async fn find_by_team_id_paginated(
&self,
_team_id: crate::domain::id::Id,
_limit: u32,
_offset: u32,
) -> Result<Vec<Crawl>, RepositoryError> {
Ok(vec![])
}
async fn count_by_team_id(
&self,
_team_id: crate::domain::id::Id,
) -> Result<u64, RepositoryError> {
Ok(0)
}
}
#[allow(dead_code)]
pub struct MockSearchServiceError;
#[async_trait]
impl SearchServiceTrait for MockSearchServiceError {
async fn search(
&self,
_team_id: crate::domain::id::Id,
_api_key_id: crate::domain::id::Id,
_query: SearchQuery,
) -> Result<SearchResponse, SearchServiceError> {
Err(SearchServiceError::SearchEngine(
"mock search engine error".to_string(),
))
}
}
#[allow(dead_code)]
pub struct MockTaskQueueError;
#[async_trait]
impl TaskQueue for MockTaskQueueError {
async fn enqueue(&self, _task: Task) -> Result<Task, QueueError> {
Err(QueueError::Repository(RepositoryError::Database(
anyhow::anyhow!("mock queue enqueue error"),
)))
}
async fn dequeue(&self, _worker_id: crate::domain::id::Id) -> Result<Option<Task>, QueueError> {
Ok(None)
}
async fn complete(&self, _task_id: crate::domain::id::Id) -> Result<(), QueueError> {
Ok(())
}
async fn fail(&self, _task_id: crate::domain::id::Id) -> Result<(), QueueError> {
Ok(())
}
async fn cancel(&self, _task_id: crate::domain::id::Id) -> Result<(), QueueError> {
Ok(())
}
}
#[allow(dead_code)]
pub struct MockCrawlRepositoryError;
#[async_trait]
impl CrawlRepository for MockCrawlRepositoryError {
async fn create(&self, _crawl: &Crawl) -> Result<Crawl, RepositoryError> {
Err(RepositoryError::Database(anyhow::anyhow!(
"mock crawl repo create error"
)))
}
async fn find_by_id(
&self,
_id: crate::domain::id::Id,
) -> Result<Option<Crawl>, RepositoryError> {
Ok(None)
}
async fn update(&self, crawl: &Crawl) -> Result<Crawl, RepositoryError> {
Ok(crawl.clone())
}
async fn increment_completed_tasks(
&self,
_id: crate::domain::id::Id,
) -> Result<(), RepositoryError> {
Ok(())
}
async fn increment_failed_tasks(
&self,
_id: crate::domain::id::Id,
) -> Result<(), RepositoryError> {
Ok(())
}
async fn update_status(
&self,
_id: crate::domain::id::Id,
_status: CrawlStatus,
) -> Result<(), RepositoryError> {
Ok(())
}
async fn increment_total_tasks(
&self,
_id: crate::domain::id::Id,
) -> Result<(), RepositoryError> {
Ok(())
}
async fn find_by_team_id_paginated(
&self,
_team_id: crate::domain::id::Id,
_limit: u32,
_offset: u32,
) -> Result<Vec<Crawl>, RepositoryError> {
Ok(vec![])
}
async fn count_by_team_id(
&self,
_team_id: crate::domain::id::Id,
) -> Result<u64, RepositoryError> {
Ok(0)
}
}