Private
Public Access
1
0
Files
Kordophone/kordophone/src/tests/test_client.rs

55 lines
1.4 KiB
Rust
Raw Normal View History

2024-04-21 23:09:37 -07:00
use async_trait::async_trait;
2025-01-20 19:43:21 -08:00
use std::collections::HashMap;
2024-04-21 23:09:37 -07:00
2024-04-21 15:14:16 -07:00
pub use crate::APIInterface;
2025-01-20 19:43:21 -08:00
use crate::{
api::http_client::Credentials,
model::{conversation, Conversation, ConversationID, JwtToken, Message}
};
2024-04-21 15:14:16 -07:00
2024-04-21 23:09:37 -07:00
pub struct TestClient {
pub version: &'static str,
pub conversations: Vec<Conversation>,
2025-01-20 19:43:21 -08:00
pub messages: HashMap<ConversationID, Vec<Message>>,
2024-04-21 23:09:37 -07:00
}
#[derive(Debug)]
2025-01-20 19:43:21 -08:00
pub enum TestError {
ConversationNotFound,
}
2024-04-21 15:14:16 -07:00
2024-04-21 23:09:37 -07:00
impl TestClient {
pub fn new() -> TestClient {
TestClient {
version: "KordophoneTest-1.0",
conversations: vec![],
2025-01-20 19:43:21 -08:00
messages: HashMap::<ConversationID, Vec<Message>>::new(),
2024-04-21 23:09:37 -07:00
}
}
}
#[async_trait]
2024-04-21 15:14:16 -07:00
impl APIInterface for TestClient {
2024-04-21 23:09:37 -07:00
type Error = TestError;
2024-06-14 20:26:56 -07:00
async fn authenticate(&mut self, _credentials: Credentials) -> Result<JwtToken, Self::Error> {
Ok(JwtToken::dummy())
}
async fn get_version(&mut self) -> Result<String, Self::Error> {
2024-04-21 23:09:37 -07:00
Ok(self.version.to_string())
2024-04-21 15:14:16 -07:00
}
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error> {
2024-04-21 23:09:37 -07:00
Ok(self.conversations.clone())
2024-04-21 15:14:16 -07:00
}
2025-01-20 19:43:21 -08:00
async fn get_messages(&mut self, conversation_id: &ConversationID) -> Result<Vec<Message>, Self::Error> {
if let Some(messages) = self.messages.get(conversation_id) {
2025-01-20 19:43:21 -08:00
return Ok(messages.clone())
}
Err(TestError::ConversationNotFound)
}
2024-04-21 15:14:16 -07:00
}