aboutsummaryrefslogtreecommitdiffstats
path: root/tests/sessions.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/sessions.rs69
1 files changed, 69 insertions, 0 deletions
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 @@
1use std::fs;
2use std::path::PathBuf;
3use std::process::Command;
4
5use serde_json::json;
6
7use agent686::session::{self, Session};
8
9fn temp_dir(tag: &str) -> PathBuf {
10 let dir = std::env::temp_dir().join(format!("agent686-sess-{tag}-{}", std::process::id()));
11 let _ = fs::remove_dir_all(&dir);
12 fs::create_dir_all(&dir).unwrap();
13 dir
14}
15
16#[test]
17fn sessions_create_list_and_replay() {
18 let dir = temp_dir("roundtrip");
19 std::env::set_var("AGENT686_DATA", &dir);
20
21 let system = json!({ "role": "system", "content": "frozen" });
22 let mut alpha = Session::create_as("alpha".into(), "model-a", &system).unwrap();
23 assert!(
24 Session::create_as("alpha".into(), "model-a", &system).is_err(),
25 "duplicate ids must not overwrite a session"
26 );
27
28 alpha
29 .append(&json!({ "role": "user", "content": "hello" }), None)
30 .unwrap();
31 alpha
32 .append(
33 &json!({ "role": "assistant", "content": "hi" }),
34 Some("because"),
35 )
36 .unwrap();
37
38 assert!(session::exists("alpha"));
39 assert!(!session::exists("nope"));
40
41 let listed = session::list().unwrap();
42 assert_eq!(listed.len(), 1);
43 assert_eq!(listed[0].id, "alpha");
44 assert_eq!(listed[0].model, "model-a");
45 assert_eq!(listed[0].messages, 2);
46
47 let (reopened, system_back, messages) = Session::open("alpha").unwrap();
48 assert_eq!(reopened.id, "alpha");
49 assert_eq!(system_back["content"], "frozen");
50 assert_eq!(messages.len(), 2);
51 assert_eq!(messages[1]["content"], "hi");
52 // Reasoning is stored beside the message but must not be replayed.
53 assert!(messages[1].get("_reasoning").is_none());
54}
55
56#[test]
57fn cli_lists_sessions_without_a_key() {
58 let dir = temp_dir("cli");
59 let output = Command::new(env!("CARGO_BIN_EXE_agent686"))
60 .env("AGENT686_DATA", &dir)
61 .env_remove("OPENCODE_API_KEY")
62 .arg("--list")
63 .output()
64 .unwrap();
65
66 assert!(output.status.success(), "{output:?}");
67 let stdout = String::from_utf8_lossy(&output.stdout);
68 assert!(stdout.contains("no sessions"), "{stdout}");
69}