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