aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/loop.rs53
-rw-r--r--tests/mock.rs25
-rw-r--r--tests/sessions.rs59
-rw-r--r--tests/tools.rs15
4 files changed, 152 insertions, 0 deletions
diff --git a/tests/loop.rs b/tests/loop.rs
index b7e56f9..f90d19d 100644
--- a/tests/loop.rs
+++ b/tests/loop.rs
@@ -334,3 +334,56 @@ fn opening_a_session_with_unanswered_calls_repairs_it() {
334 }); 334 });
335 assert!(repaired, "missing result for call_2 should be synthesised"); 335 assert!(repaired, "missing result for call_2 should be synthesised");
336} 336}
337
338#[test]
339fn an_empty_bash_call_is_never_approved() {
340 let dir = temp_dir("empty-bash");
341 let empty_call = sse(&format!(
342 "data: {}\n\ndata: [DONE]\n\n",
343 json!({
344 "choices": [{
345 "delta": { "tool_calls": [{
346 "index": 0,
347 "id": "call_empty",
348 "function": { "name": "bash", "arguments": "{}" }
349 }]},
350 "finish_reason": "tool_calls"
351 }]
352 })
353 ));
354 let final_round = sse(
355 "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"},\"finish_reason\":\"stop\"}]}\n\n\
356 data: [DONE]\n\n",
357 );
358
359 let base = serve_sequence(vec![empty_call, final_round]);
360 std::env::set_var("AGENT686_DATA", dir.join("data"));
361
362 let system = agent::system_prompt();
363 let mut session = Session::create("test-model", &system).unwrap();
364 let cfg = Config {
365 base_url: base,
366 api_key: "test-key".into(),
367 model: "test-model".into(),
368 session: session.id.clone(),
369 yolo: false,
370 };
371 let mut history = agent::new_history(system);
372 // Not a terminal: if this reaches the approval path it is denied, which the
373 // assertion below would catch.
374 let mut approver = Approver::new(false);
375
376 agent::run_turn(
377 &cfg,
378 &mut session,
379 &mut history,
380 "run something",
381 &AtomicBool::new(false),
382 &mut approver,
383 )
384 .expect("the turn should survive a malformed call");
385
386 let result = history[3]["content"].as_str().unwrap();
387 assert!(result.contains("`cmd` is required"), "{result}");
388 assert!(!result.contains("denied"), "must not reach an approval prompt: {result}");
389}
diff --git a/tests/mock.rs b/tests/mock.rs
index 9b9a56d..7e076e3 100644
--- a/tests/mock.rs
+++ b/tests/mock.rs
@@ -442,3 +442,28 @@ fn a_final_frame_without_a_newline_is_not_lost() {
442 .expect("a trailing frame without a newline should still count"); 442 .expect("a trailing frame without a newline should still count");
443 assert_eq!(outcome.content, "last"); 443 assert_eq!(outcome.content, "last");
444} 444}
445
446#[test]
447fn an_empty_placeholder_tool_call_is_dropped() {
448 const SSE: &str = concat!(
449 "HTTP/1.1 200 OK\r\n",
450 "Content-Type: text/event-stream\r\n",
451 "Connection: close\r\n",
452 "\r\n",
453 "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"function\":{\"name\":\"read\",\"arguments\":\"{\\\"path\\\":\\\"a\\\"}\"}},{\"index\":1}]},\"finish_reason\":\"tool_calls\"}]}\n\n",
454 "data: [DONE]\n\n",
455 );
456 let (base, _rx) = serve_once(SSE);
457 let mut sink = Collector::default();
458 let outcome = provider::chat(
459 &config(base),
460 &[json!({ "role": "user", "content": "hi" })],
461 &[],
462 &AtomicBool::new(false),
463 &mut sink,
464 )
465 .expect("chat should succeed");
466
467 assert_eq!(outcome.tool_calls.len(), 1, "{:?}", outcome.tool_calls);
468 assert_eq!(outcome.tool_calls[0].name, "read");
469}
diff --git a/tests/sessions.rs b/tests/sessions.rs
index 1d40f64..df2f3b2 100644
--- a/tests/sessions.rs
+++ b/tests/sessions.rs
@@ -110,3 +110,62 @@ fn listing_reports_the_model_in_use_after_a_switch() {
110 assert_eq!(listed.len(), 1); 110 assert_eq!(listed.len(), 1);
111 assert_eq!(listed[0].model, "model-b"); 111 assert_eq!(listed[0].model, "model-b");
112} 112}
113
114#[test]
115fn a_torn_utf8_line_does_not_brick_a_session() {
116 let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
117 let dir = temp_dir("torn");
118 std::env::set_var("AGENT686_DATA", &dir);
119 let sessions = dir.join("sessions");
120 fs::create_dir_all(&sessions).unwrap();
121
122 let mut raw = Vec::new();
123 raw.extend_from_slice(
124 br#"{"t":"header","session":"torn","model":"m","cwd":"/tmp","system":{"role":"system","content":"s"}}
125"#,
126 );
127 raw.extend_from_slice(br#"{"t":"msg","role":"user","content":"hello"}
128"#);
129 // A killed write that split a multi-byte character.
130 raw.extend_from_slice(b"{\"t\":\"msg\",\"role\":\"user\",\"content\":\"caf\xc3\"}\n");
131 fs::write(sessions.join("torn.jsonl"), raw).unwrap();
132
133 let (_session, _system, messages) = Session::open("torn").expect("torn line must not fail");
134 // Lossy decoding keeps the message (with a replacement character) rather
135 // than dropping a valid JSON line; what matters is that open succeeds.
136 assert_eq!(messages.len(), 2);
137 assert_eq!(messages[0]["content"], "hello");
138 assert!(messages[1]["content"].as_str().unwrap().starts_with("caf"));
139}
140
141#[test]
142fn last_reply_prefers_content_and_falls_back_to_reasoning() {
143 let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
144 let dir = temp_dir("last-reply");
145 std::env::set_var("AGENT686_DATA", &dir);
146
147 let system = json!({ "role": "system", "content": "x" });
148 let mut session = Session::create_as("reply".into(), "m", &system).unwrap();
149 session
150 .append(
151 &json!({ "role": "assistant", "content": "the answer" }),
152 Some("the thinking"),
153 )
154 .unwrap();
155 session
156 .append(&json!({ "role": "assistant", "content": null }), Some("only thinking"))
157 .unwrap();
158
159 let reply = session::last_reply("reply").expect("there is a reply");
160 assert_eq!(reply.content, "the answer", "prefers a real block");
161 assert_eq!(reply.reasoning, "the thinking", "and its own reasoning");
162
163 // With no content anywhere, reasoning is the fallback.
164 let mut session = Session::create_as("reasoning-only".into(), "m", &system).unwrap();
165 session
166 .append(&json!({ "role": "assistant", "content": null }), Some("thoughts"))
167 .unwrap();
168 let reply = session::last_reply("reasoning-only").unwrap();
169 assert!(reply.content.is_empty());
170 assert_eq!(reply.reasoning, "thoughts");
171}
diff --git a/tests/tools.rs b/tests/tools.rs
index 77c1698..a400df0 100644
--- a/tests/tools.rs
+++ b/tests/tools.rs
@@ -419,3 +419,18 @@ fn glob_follows_file_symlinks_but_not_directories() {
419 assert!(text.contains("link.rs"), "{text}"); 419 assert!(text.contains("link.rs"), "{text}");
420 assert!(text.contains("inner.rs"), "{text}"); 420 assert!(text.contains("inner.rs"), "{text}");
421} 421}
422
423#[test]
424fn validate_rejects_calls_that_cannot_run() {
425 use agent686::tools::validate;
426
427 assert!(validate("bash", &json!({ "cmd": "" })).is_err());
428 assert!(validate("bash", &json!({})).is_err());
429 assert!(validate("bash", &json!({ "cmd": "ls" })).is_ok());
430
431 assert!(validate("read", &json!({ "path": " " })).is_err());
432 assert!(validate("edit", &json!({ "path": "a", "old": "" })).is_err());
433 assert!(validate("grep", &json!({ "pattern": "" })).is_err());
434 // Empty content is how you create an empty file.
435 assert!(validate("write", &json!({ "path": "a", "content": "" })).is_ok());
436}