use crate::trace::common::{storage::async_ops, Interaction};
pub async fn last_interaction() -> Option<Interaction> {
async_ops::get_last_interaction().await.map(|arc| (*arc).clone())
}
pub async fn record_request(message: String) {
async_ops::start_new_interaction(message).await;
}
pub async fn record_response(message: String) {
async_ops::add_response_to_current(message).await;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::trace::common::storage::async_ops;
use serial_test::serial;
#[tokio::test]
#[serial]
async fn test_no_interaction_initially() {
async_ops::clear().await;
assert!(last_interaction().await.is_none());
}
#[tokio::test]
#[serial]
async fn test_record_and_retrieve_interaction() {
record_request("TEST_REQUEST".to_string()).await;
let interaction = last_interaction().await.expect("Should have interaction");
assert_eq!(interaction.request, "TEST_REQUEST");
assert_eq!(interaction.responses.len(), 0);
}
#[tokio::test]
#[serial]
async fn test_record_request_and_responses() {
record_request("REQUEST_1".to_string()).await;
record_response("RESPONSE_1".to_string()).await;
record_response("RESPONSE_2".to_string()).await;
let interaction = last_interaction().await.expect("Should have interaction");
assert_eq!(interaction.request, "REQUEST_1");
assert_eq!(interaction.responses.len(), 2);
assert_eq!(interaction.responses[0], "RESPONSE_1");
assert_eq!(interaction.responses[1], "RESPONSE_2");
}
#[tokio::test]
#[serial]
async fn test_new_request_replaces_old() {
record_request("REQUEST_1".to_string()).await;
record_response("RESPONSE_1".to_string()).await;
record_request("REQUEST_2".to_string()).await;
record_response("RESPONSE_2".to_string()).await;
let interaction = last_interaction().await.expect("Should have interaction");
assert_eq!(interaction.request, "REQUEST_2");
assert_eq!(interaction.responses.len(), 1);
assert_eq!(interaction.responses[0], "RESPONSE_2");
}
}