use crate::p_session_support;
use crate::reopen_support;
use p_session_support::{in_order, text, user_texts};
use protocol::watch::QueueKind;
use reopen_support::Home;
use serde_json::{Value, json};
use session_core::core::Op;
use session_journal::ns::LANE_STATE;
use std::collections::HashMap;
fn fixture() -> Value {
serde_json::from_str(include_str!("../fixtures/idle_queue.json")).unwrap()
}
fn users(request: &Value) -> Vec<Value> {
user_texts(request)
.into_iter()
.filter(|t| t != "[clock] <CLOCK>")
.collect()
}
fn message_text(message: &Value) -> String {
match &message["content"] {
Value::String(text) => text.clone(),
Value::Array(blocks) => blocks.iter().filter_map(|b| b["text"].as_str()).collect(),
_ => String::new(),
}
}
fn replay(scenario: &Value) {
let name = scenario["name"].as_str().unwrap();
let home = Home::new(None);
let answers: Vec<String> = scenario["answers"]
.as_array()
.unwrap()
.iter()
.map(|a| text(a.as_str().unwrap()))
.collect();
let w = home.open(in_order(answers), &[], None);
for (kind, key) in [
(QueueKind::Steer, "steeringMode"),
(QueueKind::FollowUp, "followUpMode"),
] {
w.call(Op::SetQueueMode {
kind,
mode: scenario[key].as_str().unwrap().into(),
})
.unwrap();
}
let mut texts = HashMap::new();
for queued in scenario["queued"].as_array().unwrap() {
let kind = QueueKind::parse(queued[0].as_str().unwrap()).unwrap();
let message = queued[1].as_str().unwrap().to_string();
let entry = w
.call(Op::Enqueue {
kind,
message: message.clone(),
images: Vec::new(),
entry_id: None,
})
.unwrap()
.unwrap();
texts.insert(entry.as_str().unwrap().to_string(), message);
}
assert!(w.requests().is_empty(), "{name}");
let lanes_before = home.values(LANE_STATE).len();
let ended = w
.call(Op::PromptAndWait {
message: "request".into(),
images: Vec::new(),
})
.unwrap()
.unwrap();
assert_eq!(ended["error"], Value::Null, "{name}: {ended}");
let requests: Vec<Vec<Value>> = w.requests().iter().map(users).collect();
assert_eq!(json!(requests), scenario["requests"], "{name}: requests");
let admitted = home.values(LANE_STATE)[lanes_before..]
.iter()
.find(|state| !state["currentOperationId"].is_null())
.cloned()
.unwrap();
let inbox: Vec<Value> = admitted["inbox"]
.as_array()
.unwrap()
.iter()
.map(|item| json!([item["kind"], texts[item["entryId"].as_str().unwrap()]]))
.collect();
assert_eq!(json!(inbox), scenario["inboxAfterAdmission"], "{name}: inbox");
let transcript: Vec<String> = w
.events("message_end")
.iter()
.map(|e| {
format!(
"{}: {}",
e["message"]["role"].as_str().unwrap(),
message_text(&e["message"])
)
})
.collect();
assert_eq!(json!(transcript), scenario["branch"], "{name}: transcript");
assert_eq!(home.values(LANE_STATE).pop().unwrap()["inbox"], json!([]), "{name}");
w.close();
}
#[test]
fn session_core_idle_queue_takes_every_queued_message_before_the_prompt_in_all_mode() {
replay(&fixture()["scenarios"][0]);
}
#[test]
fn session_core_idle_queue_takes_the_first_of_each_kind_one_at_a_time_and_delivers_the_rest() {
replay(&fixture()["scenarios"][1]);
}
#[test]
fn session_core_idle_queue_mixes_one_at_a_time_steering_with_all_follow_ups() {
replay(&fixture()["scenarios"][2]);
}
#[test]
fn session_core_idle_queue_places_a_lone_follow_up_before_the_prompt() {
replay(&fixture()["scenarios"][3]);
}
#[test]
fn session_core_queue_modes_default_to_all() {
assert_eq!(config::keys::QUEUE_DEFAULT, "all");
let home = Home::new(None);
let w = home.open(in_order(Vec::new()), &[], None);
let state = w.call(Op::State).unwrap().unwrap();
assert_eq!(
(&state["steeringMode"], &state["followUpMode"]),
(&json!("all"), &json!("all"))
);
assert_eq!(session_journal::run::default_settings()["steeringMode"], "all");
assert_eq!(session_journal::run::default_settings()["followUpMode"], "all");
w.close();
}