From f192c40b2a3643a9ab97303310db8dbc7547ee35 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 14:59:31 +0100 Subject: Robustness: retry with backoff, explained errors, --list, --session creates - four attempts on 408/425/429/500/502/503/504 and transport errors, honouring Retry-After - backoff is interruptible so Ctrl-C is not held for up to 8s - context overflow, auth failure and rate limit messages say what to do - --list shows id/model/messages/age without needing a key - --session creates the session when the id does not exist - probes recorded: session header is mandatory, consecutive user messages allowed --- tests/sessions.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/sessions.rs (limited to 'tests/sessions.rs') diff --git a/tests/sessions.rs b/tests/sessions.rs new file mode 100644 index 0000000..8d17fc5 --- /dev/null +++ b/tests/sessions.rs @@ -0,0 +1,69 @@ +use std::fs; +use std::path::PathBuf; +use std::process::Command; + +use serde_json::json; + +use agent686::session::{self, Session}; + +fn temp_dir(tag: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("agent686-sess-{tag}-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir +} + +#[test] +fn sessions_create_list_and_replay() { + let dir = temp_dir("roundtrip"); + std::env::set_var("AGENT686_DATA", &dir); + + let system = json!({ "role": "system", "content": "frozen" }); + let mut alpha = Session::create_as("alpha".into(), "model-a", &system).unwrap(); + assert!( + Session::create_as("alpha".into(), "model-a", &system).is_err(), + "duplicate ids must not overwrite a session" + ); + + alpha + .append(&json!({ "role": "user", "content": "hello" }), None) + .unwrap(); + alpha + .append( + &json!({ "role": "assistant", "content": "hi" }), + Some("because"), + ) + .unwrap(); + + assert!(session::exists("alpha")); + assert!(!session::exists("nope")); + + let listed = session::list().unwrap(); + assert_eq!(listed.len(), 1); + assert_eq!(listed[0].id, "alpha"); + assert_eq!(listed[0].model, "model-a"); + assert_eq!(listed[0].messages, 2); + + let (reopened, system_back, messages) = Session::open("alpha").unwrap(); + assert_eq!(reopened.id, "alpha"); + assert_eq!(system_back["content"], "frozen"); + assert_eq!(messages.len(), 2); + assert_eq!(messages[1]["content"], "hi"); + // Reasoning is stored beside the message but must not be replayed. + assert!(messages[1].get("_reasoning").is_none()); +} + +#[test] +fn cli_lists_sessions_without_a_key() { + let dir = temp_dir("cli"); + let output = Command::new(env!("CARGO_BIN_EXE_agent686")) + .env("AGENT686_DATA", &dir) + .env_remove("OPENCODE_API_KEY") + .arg("--list") + .output() + .unwrap(); + + assert!(output.status.success(), "{output:?}"); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(stdout.contains("no sessions"), "{stdout}"); +} -- cgit v1.2.3