diff options
Diffstat (limited to 'tests/sessions.rs')
| -rw-r--r-- | tests/sessions.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/sessions.rs b/tests/sessions.rs index b445242..f21be24 100644 --- a/tests/sessions.rs +++ b/tests/sessions.rs | |||
| @@ -4,6 +4,9 @@ use std::process::Command; | |||
| 4 | 4 | ||
| 5 | use serde_json::json; | 5 | use serde_json::json; |
| 6 | 6 | ||
| 7 | /// Tests in this binary share process-wide environment variables. | ||
| 8 | static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); | ||
| 9 | |||
| 7 | use agent686::session::{self, Session}; | 10 | use agent686::session::{self, Session}; |
| 8 | 11 | ||
| 9 | fn temp_dir(tag: &str) -> PathBuf { | 12 | fn temp_dir(tag: &str) -> PathBuf { |
| @@ -15,6 +18,7 @@ fn temp_dir(tag: &str) -> PathBuf { | |||
| 15 | 18 | ||
| 16 | #[test] | 19 | #[test] |
| 17 | fn sessions_create_list_and_replay() { | 20 | fn sessions_create_list_and_replay() { |
| 21 | let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); | ||
| 18 | let dir = temp_dir("roundtrip"); | 22 | let dir = temp_dir("roundtrip"); |
| 19 | std::env::set_var("AGENT686_DATA", &dir); | 23 | std::env::set_var("AGENT686_DATA", &dir); |
| 20 | 24 | ||
| @@ -50,6 +54,8 @@ fn sessions_create_list_and_replay() { | |||
| 50 | assert_eq!(listed[0].model, "model-a"); | 54 | assert_eq!(listed[0].model, "model-a"); |
| 51 | assert_eq!(listed[0].messages, 2); | 55 | assert_eq!(listed[0].messages, 2); |
| 52 | 56 | ||
| 57 | // A session holds an exclusive lock while open. | ||
| 58 | drop(alpha); | ||
| 53 | let (reopened, system_back, messages) = Session::open("alpha").unwrap(); | 59 | let (reopened, system_back, messages) = Session::open("alpha").unwrap(); |
| 54 | assert_eq!(reopened.id, "alpha"); | 60 | assert_eq!(reopened.id, "alpha"); |
| 55 | assert_eq!(system_back["content"], "frozen"); | 61 | assert_eq!(system_back["content"], "frozen"); |
| @@ -73,3 +79,19 @@ fn cli_lists_sessions_without_a_key() { | |||
| 73 | let stdout = String::from_utf8_lossy(&output.stdout); | 79 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 74 | assert!(stdout.contains("no sessions"), "{stdout}"); | 80 | assert!(stdout.contains("no sessions"), "{stdout}"); |
| 75 | } | 81 | } |
| 82 | |||
| 83 | #[test] | ||
| 84 | fn a_session_cannot_be_open_twice() { | ||
| 85 | let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); | ||
| 86 | let dir = temp_dir("lock"); | ||
| 87 | std::env::set_var("AGENT686_DATA", &dir); | ||
| 88 | |||
| 89 | let system = json!({ "role": "system", "content": "x" }); | ||
| 90 | let first = Session::create_as("locked".into(), "m", &system).unwrap(); | ||
| 91 | |||
| 92 | let error = Session::open("locked").err().expect("second open must fail"); | ||
| 93 | assert!(error.contains("already open"), "{error}"); | ||
| 94 | |||
| 95 | drop(first); | ||
| 96 | Session::open("locked").expect("after the first handle closes it is available"); | ||
| 97 | } | ||
