use std::sync::Arc;
use pretty_assertions::assert_eq;
use rho_sdk::{model::ContentBlock, UserInput};
use tokio::sync::mpsc;
use crate::{
app::session_runtime::{
test_support::{idle_runtime, runtime_on, HangUntilCancel},
SessionEvent, SessionRuntime, Turn, TurnIo, TurnOutcome,
},
config::Config,
};
fn config() -> Config {
Config {
provider: "test".into(),
model: "model".into(),
..Config::default()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Ending {
Cancelled,
Failed,
}
async fn turn(
runtime: &mut SessionRuntime,
config: &Config,
message: &str,
ending: Ending,
) -> Vec<SessionEvent> {
let prompt = runtime.begin_prompt(config).await.unwrap();
match ending {
Ending::Cancelled => runtime.handle().cancel(),
Ending::Failed => {}
}
let (events, mut shown) = mpsc::unbounded_channel();
let (_controls, control) = mpsc::unbounded_channel();
let outcome = runtime
.run_turn(
&prompt,
Turn {
display: UserInput::text(message),
model: UserInput::text(format!("context a client added\n\n{message}")),
calls: Vec::new(),
},
TurnIo { events, control },
config,
)
.await;
drop(prompt);
let ended = match outcome {
Ok(TurnOutcome::Cancelled) => Some(Ending::Cancelled),
Err(_) => Some(Ending::Failed),
Ok(TurnOutcome::Completed(_)) => None,
};
assert_eq!(ended, Some(ending), "{message}");
std::iter::from_fn(|| shown.try_recv().ok()).collect()
}
fn named_from(shown: [Vec<SessionEvent>; 3]) -> [Option<UserInput>; 3] {
shown.map(|turn| {
turn.into_iter().find_map(|event| {
if let SessionEvent::TitleRequested(job) = event {
Some(job.request.input)
} else {
None
}
})
})
}
fn first_message(message: &str) -> Option<UserInput> {
Some(crate::title::session_input(&[ContentBlock::Text(
message.into(),
)]))
}
#[tokio::test]
async fn the_first_saved_prompt_asks_for_a_title_once() {
let config = config();
let root = tempfile::tempdir().unwrap();
let mut runtime = runtime_on(root.path(), &config, Arc::new(HangUntilCancel)).await;
let shown = [
turn(
&mut runtime,
&config,
"name this session",
Ending::Cancelled,
)
.await,
turn(&mut runtime, &config, "and go on", Ending::Cancelled).await,
turn(&mut runtime, &config, "and on", Ending::Cancelled).await,
];
let order: Vec<_> = shown[0]
.iter()
.filter_map(|event| {
if matches!(event, SessionEvent::MessageInserted(_)) {
Some("message saved")
} else if matches!(event, SessionEvent::TitleRequested(_)) {
Some("title requested")
} else if matches!(event, SessionEvent::RunStarted) {
Some("run started")
} else {
None
}
})
.collect();
assert_eq!(
(order, named_from(shown)),
(
vec!["message saved", "title requested", "run started"],
[first_message("name this session"), None, None]
)
);
runtime.shutdown().await;
}
#[tokio::test]
async fn the_prompt_after_a_failed_turn_asks_to_name_the_first_message_again() {
let config = config();
let root = tempfile::tempdir().unwrap();
let mut runtime = idle_runtime(root.path(), &config).await;
let shown = [
turn(&mut runtime, &config, "name this session", Ending::Failed).await,
turn(&mut runtime, &config, "try again", Ending::Cancelled).await,
turn(&mut runtime, &config, "and go on", Ending::Cancelled).await,
];
assert_eq!(
named_from(shown),
[
first_message("name this session"),
first_message("name this session"),
None
]
);
runtime.shutdown().await;
}