use super::*;
#[derive(Deserialize)]
struct SetGoalParams {
session_id: String,
slot: usize,
text: String,
}
#[derive(Deserialize)]
struct ClearGoalParams {
session_id: String,
slot: usize,
}
pub(super) async fn set_goal(
registry: &SessionRegistry,
params: Value,
_ctx: ConnectionContext,
) -> Result<Value, RpcError> {
let p: SetGoalParams = parse(params, "session.set_goal")?;
registry.set_goal(&p.session_id, p.slot, &p.text)?;
Ok(json!({}))
}
pub(super) async fn clear_goal(
registry: &SessionRegistry,
params: Value,
_ctx: ConnectionContext,
) -> Result<Value, RpcError> {
let p: ClearGoalParams = parse(params, "session.clear_goal")?;
registry.clear_goal(&p.session_id, p.slot)?;
Ok(json!({}))
}
pub(super) async fn get_goals(
registry: &SessionRegistry,
params: Value,
_ctx: ConnectionContext,
) -> Result<Value, RpcError> {
let p: SessionIdParams = parse(params, "session.get_goals")?;
Ok(json!({ "goals": registry.get_goals(&p.session_id)? }))
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::sync::mpsc;
fn test_ctx() -> ConnectionContext {
let (tx, _rx) = mpsc::unbounded_channel();
ConnectionContext::new(tx)
}
fn seed_pm_transcript(registry: &SessionRegistry, id: &str) {
let transcript = registry
.begin_pm_transcript(id, "you are the pm", "first task")
.unwrap();
registry.store_pm_transcript(id, transcript);
}
#[tokio::test]
async fn set_goal_writes_operator_goal() {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
seed_pm_transcript(®istry, &session.id);
let result = set_goal(
®istry,
json!({"session_id": session.id, "slot": 2, "text": "ship it"}),
test_ctx(),
)
.await
.unwrap();
assert_eq!(result, json!({}));
let goals = get_goals(®istry, json!({"session_id": session.id}), test_ctx())
.await
.unwrap();
assert_eq!(goals["goals"][0]["slot"], 2);
assert_eq!(goals["goals"][0]["text"], "ship it");
assert_eq!(goals["goals"][0]["source"], "operator");
}
#[tokio::test]
async fn set_goal_out_of_range_slot_maps_to_invalid_argument() {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
seed_pm_transcript(®istry, &session.id);
let err = set_goal(
®istry,
json!({"session_id": session.id, "slot": 6, "text": "x"}),
test_ctx(),
)
.await
.unwrap_err();
assert_eq!(err.code, -32003);
}
#[tokio::test]
async fn set_goal_unknown_session_maps_to_session_not_found() {
let registry = SessionRegistry::new();
let err = set_goal(
®istry,
json!({"session_id": "nope", "slot": 1, "text": "x"}),
test_ctx(),
)
.await
.unwrap_err();
assert_eq!(err.code, -32007);
}
#[tokio::test]
async fn clear_goal_clears_operator_goal() {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
seed_pm_transcript(®istry, &session.id);
set_goal(
®istry,
json!({"session_id": session.id, "slot": 3, "text": "temp"}),
test_ctx(),
)
.await
.unwrap();
clear_goal(
®istry,
json!({"session_id": session.id, "slot": 3}),
test_ctx(),
)
.await
.unwrap();
let goals = get_goals(®istry, json!({"session_id": session.id}), test_ctx())
.await
.unwrap();
assert_eq!(goals["goals"].as_array().unwrap().len(), 0);
}
#[tokio::test]
async fn clear_goal_out_of_range_slot_maps_to_invalid_argument() {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
seed_pm_transcript(®istry, &session.id);
let err = clear_goal(
®istry,
json!({"session_id": session.id, "slot": 0}),
test_ctx(),
)
.await
.unwrap_err();
assert_eq!(err.code, -32003);
}
#[tokio::test]
async fn get_goals_returns_goals_key() {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
let result = get_goals(®istry, json!({"session_id": session.id}), test_ctx())
.await
.unwrap();
assert!(result["goals"].is_array());
}
#[tokio::test]
async fn get_goals_on_never_run_session_is_empty() {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
let result = get_goals(®istry, json!({"session_id": session.id}), test_ctx())
.await
.unwrap();
assert_eq!(result["goals"].as_array().unwrap().len(), 0);
}
#[tokio::test]
async fn get_goals_unknown_session_maps_to_session_not_found() {
let registry = SessionRegistry::new();
let err = get_goals(®istry, json!({"session_id": "nope"}), test_ctx())
.await
.unwrap_err();
assert_eq!(err.code, -32007);
}
}