From 714534475818e6e5dced91ecad100ca9e286ccce Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:37:56 +0100 Subject: 0.4.1 + 0.4.2: abort integrity, stream completeness, tool-call indexing - an aborted multi-call step now answers every announced tool call - sessions with unanswered calls are repaired by appending results - a stream without [DONE]/finish_reason is an error, not an answer - tool call deltas keyed by index can no longer vanish or invent empty calls - one-shot exit codes: 130 aborted, 3 denied --- tests/mock.rs | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) (limited to 'tests/mock.rs') diff --git a/tests/mock.rs b/tests/mock.rs index 9b3cd5a..9b9a56d 100644 --- a/tests/mock.rs +++ b/tests/mock.rs @@ -326,3 +326,119 @@ fn auth_failure_says_where_the_key_lives() { let message = format!("{error}"); assert!(message.contains("OPENCODE_API_KEY"), "{message}"); } + +#[test] +fn a_truncated_stream_is_an_error_not_an_answer() { + const TRUNCATED: &str = concat!( + "HTTP/1.1 200 OK\r\n", + "Content-Type: text/event-stream\r\n", + "Connection: close\r\n", + "\r\n", + "data: {\"choices\":[{\"delta\":{\"content\":\"partial\"}}]}\n\n", + ); + let (base, _rx) = serve_once(TRUNCATED); + let mut sink = Collector::default(); + let error = provider::chat( + &config(base), + &[json!({ "role": "user", "content": "hi" })], + &[], + &AtomicBool::new(false), + &mut sink, + ) + .expect_err("a stream without [DONE] or finish_reason must fail"); + + let message = format!("{error}"); + assert!(message.contains("ended before completion"), "{message}"); +} + +#[test] +fn a_non_sse_200_is_an_error_not_an_empty_answer() { + const JSON: &str = concat!( + "HTTP/1.1 200 OK\r\n", + "Content-Type: application/json\r\n", + "Connection: close\r\n", + "\r\n", + "{\"id\":\"x\",\"choices\":[{\"message\":{\"content\":\"hello\"}}]}", + ); + let (base, _rx) = serve_once(JSON); + let mut sink = Collector::default(); + let error = provider::chat( + &config(base), + &[json!({ "role": "user", "content": "hi" })], + &[], + &AtomicBool::new(false), + &mut sink, + ) + .expect_err("a non-streaming body must not look like a blank answer"); + assert!(format!("{error}").contains("ended before completion")); +} + +#[test] +fn finish_reason_without_done_marker_is_accepted() { + const SSE: &str = concat!( + "HTTP/1.1 200 OK\r\n", + "Content-Type: text/event-stream\r\n", + "Connection: close\r\n", + "\r\n", + "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"},\"finish_reason\":\"stop\"}]}\n\n", + ); + let (base, _rx) = serve_once(SSE); + let mut sink = Collector::default(); + let outcome = provider::chat( + &config(base), + &[json!({ "role": "user", "content": "hi" })], + &[], + &AtomicBool::new(false), + &mut sink, + ) + .expect("finish_reason is enough"); + assert_eq!(outcome.content, "ok"); +} + +#[test] +fn a_tool_call_without_an_index_still_assembles() { + const SSE: &str = concat!( + "HTTP/1.1 200 OK\r\n", + "Content-Type: text/event-stream\r\n", + "Connection: close\r\n", + "\r\n", + "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"id\":\"call_1\",\"function\":{\"name\":\"read\",\"arguments\":\"{\\\"path\\\":\\\"a\\\"}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n\n", + "data: [DONE]\n\n", + ); + let (base, _rx) = serve_once(SSE); + let mut sink = Collector::default(); + let outcome = provider::chat( + &config(base), + &[json!({ "role": "user", "content": "hi" })], + &[], + &AtomicBool::new(false), + &mut sink, + ) + .expect("chat should succeed"); + + assert_eq!(outcome.tool_calls.len(), 1); + assert_eq!(outcome.tool_calls[0].name, "read"); + assert_eq!(outcome.tool_calls[0].arguments, "{\"path\":\"a\"}"); +} + +#[test] +fn a_final_frame_without_a_newline_is_not_lost() { + const SSE: &str = concat!( + "HTTP/1.1 200 OK\r\n", + "Content-Type: text/event-stream\r\n", + "Connection: close\r\n", + "\r\n", + "data: {\"choices\":[{\"delta\":{\"content\":\"last\"},\"finish_reason\":\"stop\"}]}", + ); + let (base, _rx) = serve_once(SSE); + let mut sink = Collector::default(); + let outcome = provider::chat( + &config(base), + &[json!({ "role": "user", "content": "hi" })], + &[], + &AtomicBool::new(false), + &mut sink, + ) + .expect("a trailing frame without a newline should still count"); + assert_eq!(outcome.content, "last"); +} -- cgit v1.2.3