aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mock.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/mock.rs')
-rw-r--r--tests/mock.rs116
1 files changed, 116 insertions, 0 deletions
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() {
326 let message = format!("{error}"); 326 let message = format!("{error}");
327 assert!(message.contains("OPENCODE_API_KEY"), "{message}"); 327 assert!(message.contains("OPENCODE_API_KEY"), "{message}");
328} 328}
329
330#[test]
331fn a_truncated_stream_is_an_error_not_an_answer() {
332 const TRUNCATED: &str = concat!(
333 "HTTP/1.1 200 OK\r\n",
334 "Content-Type: text/event-stream\r\n",
335 "Connection: close\r\n",
336 "\r\n",
337 "data: {\"choices\":[{\"delta\":{\"content\":\"partial\"}}]}\n\n",
338 );
339 let (base, _rx) = serve_once(TRUNCATED);
340 let mut sink = Collector::default();
341 let error = provider::chat(
342 &config(base),
343 &[json!({ "role": "user", "content": "hi" })],
344 &[],
345 &AtomicBool::new(false),
346 &mut sink,
347 )
348 .expect_err("a stream without [DONE] or finish_reason must fail");
349
350 let message = format!("{error}");
351 assert!(message.contains("ended before completion"), "{message}");
352}
353
354#[test]
355fn a_non_sse_200_is_an_error_not_an_empty_answer() {
356 const JSON: &str = concat!(
357 "HTTP/1.1 200 OK\r\n",
358 "Content-Type: application/json\r\n",
359 "Connection: close\r\n",
360 "\r\n",
361 "{\"id\":\"x\",\"choices\":[{\"message\":{\"content\":\"hello\"}}]}",
362 );
363 let (base, _rx) = serve_once(JSON);
364 let mut sink = Collector::default();
365 let error = provider::chat(
366 &config(base),
367 &[json!({ "role": "user", "content": "hi" })],
368 &[],
369 &AtomicBool::new(false),
370 &mut sink,
371 )
372 .expect_err("a non-streaming body must not look like a blank answer");
373 assert!(format!("{error}").contains("ended before completion"));
374}
375
376#[test]
377fn finish_reason_without_done_marker_is_accepted() {
378 const SSE: &str = concat!(
379 "HTTP/1.1 200 OK\r\n",
380 "Content-Type: text/event-stream\r\n",
381 "Connection: close\r\n",
382 "\r\n",
383 "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"},\"finish_reason\":\"stop\"}]}\n\n",
384 );
385 let (base, _rx) = serve_once(SSE);
386 let mut sink = Collector::default();
387 let outcome = provider::chat(
388 &config(base),
389 &[json!({ "role": "user", "content": "hi" })],
390 &[],
391 &AtomicBool::new(false),
392 &mut sink,
393 )
394 .expect("finish_reason is enough");
395 assert_eq!(outcome.content, "ok");
396}
397
398#[test]
399fn a_tool_call_without_an_index_still_assembles() {
400 const SSE: &str = concat!(
401 "HTTP/1.1 200 OK\r\n",
402 "Content-Type: text/event-stream\r\n",
403 "Connection: close\r\n",
404 "\r\n",
405 "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"id\":\"call_1\",\"function\":{\"name\":\"read\",\"arguments\":\"{\\\"path\\\":\\\"a\\\"}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n\n",
406 "data: [DONE]\n\n",
407 );
408 let (base, _rx) = serve_once(SSE);
409 let mut sink = Collector::default();
410 let outcome = provider::chat(
411 &config(base),
412 &[json!({ "role": "user", "content": "hi" })],
413 &[],
414 &AtomicBool::new(false),
415 &mut sink,
416 )
417 .expect("chat should succeed");
418
419 assert_eq!(outcome.tool_calls.len(), 1);
420 assert_eq!(outcome.tool_calls[0].name, "read");
421 assert_eq!(outcome.tool_calls[0].arguments, "{\"path\":\"a\"}");
422}
423
424#[test]
425fn a_final_frame_without_a_newline_is_not_lost() {
426 const SSE: &str = concat!(
427 "HTTP/1.1 200 OK\r\n",
428 "Content-Type: text/event-stream\r\n",
429 "Connection: close\r\n",
430 "\r\n",
431 "data: {\"choices\":[{\"delta\":{\"content\":\"last\"},\"finish_reason\":\"stop\"}]}",
432 );
433 let (base, _rx) = serve_once(SSE);
434 let mut sink = Collector::default();
435 let outcome = provider::chat(
436 &config(base),
437 &[json!({ "role": "user", "content": "hi" })],
438 &[],
439 &AtomicBool::new(false),
440 &mut sink,
441 )
442 .expect("a trailing frame without a newline should still count");
443 assert_eq!(outcome.content, "last");
444}