use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
use async_trait::async_trait;
use serde_json::{Value, json};
use crate::core::sm::control::{LaunchParams, SessionControl, SessionControlError};
pub struct MockSessionControl {
next_id: AtomicUsize,
launches: Mutex<Vec<(String, LaunchParams)>>,
sends: Mutex<Vec<(String, String)>>,
get_body: Mutex<Value>,
}
impl Default for MockSessionControl {
fn default() -> Self {
Self {
next_id: AtomicUsize::new(0),
launches: Mutex::new(Vec::new()),
sends: Mutex::new(Vec::new()),
get_body: Mutex::new(json!({ "session": { "state": "running" } })),
}
}
}
impl MockSessionControl {
pub fn with_evidence(evidence: &str) -> Self {
let mock = Self::default();
*mock.get_body.lock().expect("lock") =
json!({ "session": { "state": "running", "pane": evidence } });
mock
}
pub fn launches(&self) -> Vec<(String, LaunchParams)> {
self.launches.lock().expect("lock").clone()
}
pub fn sends(&self) -> Vec<(String, String)> {
self.sends.lock().expect("lock").clone()
}
}
#[async_trait]
impl SessionControl for MockSessionControl {
async fn launch(&self, params: LaunchParams) -> Result<Value, SessionControlError> {
let n = self.next_id.fetch_add(1, Ordering::SeqCst) + 1;
let session_id = format!("s-{n}");
self.launches
.lock()
.expect("lock")
.push((session_id.clone(), params));
Ok(json!({ "session_id": session_id }))
}
async fn list(&self) -> Result<Value, SessionControlError> {
Ok(json!({ "sessions": [] }))
}
async fn get(&self, _session_id: &str) -> Result<Value, SessionControlError> {
Ok(self.get_body.lock().expect("lock").clone())
}
async fn send(&self, session_id: &str, text: &str) -> Result<Value, SessionControlError> {
self.sends
.lock()
.expect("lock")
.push((session_id.to_string(), text.to_string()));
Ok(json!({ "ok": true }))
}
async fn stop(&self, _session_id: &str) -> Result<Value, SessionControlError> {
Ok(json!({ "ok": true }))
}
async fn resume(&self, _session_id: &str) -> Result<Value, SessionControlError> {
Ok(json!({ "ok": true }))
}
async fn kill(&self, _session_id: &str) -> Result<Value, SessionControlError> {
Ok(json!({ "ok": true }))
}
}