diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 02:54:21 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 02:54:21 +0100 |
| commit | 7175543e00caa9ce35487fa7f8b1b479c1ba00a2 (patch) | |
| tree | 10f89f85ca35d6ac7515bbe88518255788d6dc22 /tests/mock.rs | |
| download | agent686-7175543e00caa9ce35487fa7f8b1b479c1ba00a2.tar.gz agent686-7175543e00caa9ce35487fa7f8b1b479c1ba00a2.zip | |
M0: streaming provider, SSE decoder, mock provider, i686 build gate
Diffstat (limited to '')
| -rw-r--r-- | tests/mock.rs | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/tests/mock.rs b/tests/mock.rs new file mode 100644 index 0000000..80d965b --- /dev/null +++ b/tests/mock.rs | |||
| @@ -0,0 +1,192 @@ | |||
| 1 | use std::io::{BufRead, BufReader, Read, Write}; | ||
| 2 | use std::net::TcpListener; | ||
| 3 | use std::sync::atomic::AtomicBool; | ||
| 4 | use std::sync::mpsc; | ||
| 5 | use std::thread; | ||
| 6 | use std::time::Duration; | ||
| 7 | |||
| 8 | use serde_json::json; | ||
| 9 | |||
| 10 | use agent686::config::Config; | ||
| 11 | use agent686::provider::{self, Error, Outcome, Sink}; | ||
| 12 | |||
| 13 | #[derive(Default)] | ||
| 14 | struct Collector { | ||
| 15 | text: String, | ||
| 16 | reasoning: String, | ||
| 17 | finished: bool, | ||
| 18 | } | ||
| 19 | |||
| 20 | impl Sink for Collector { | ||
| 21 | fn text(&mut self, s: &str) { | ||
| 22 | self.text.push_str(s); | ||
| 23 | } | ||
| 24 | fn reasoning(&mut self, s: &str) { | ||
| 25 | self.reasoning.push_str(s); | ||
| 26 | } | ||
| 27 | fn done(&mut self, _outcome: &Outcome, _elapsed: Duration) { | ||
| 28 | self.finished = true; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | /// Serves exactly one request on a random port, then closes. Returns the base | ||
| 33 | /// URL and a receiver for the request as the client sent it. | ||
| 34 | fn serve_once(response: &'static str) -> (String, mpsc::Receiver<String>) { | ||
| 35 | let listener = TcpListener::bind("127.0.0.1:0").unwrap(); | ||
| 36 | let addr = listener.local_addr().unwrap(); | ||
| 37 | let (tx, rx) = mpsc::channel(); | ||
| 38 | |||
| 39 | thread::spawn(move || { | ||
| 40 | let (mut stream, _) = listener.accept().unwrap(); | ||
| 41 | let mut reader = BufReader::new(stream.try_clone().unwrap()); | ||
| 42 | |||
| 43 | let mut request = String::new(); | ||
| 44 | let mut content_length = 0usize; | ||
| 45 | loop { | ||
| 46 | let mut line = String::new(); | ||
| 47 | if reader.read_line(&mut line).unwrap_or(0) == 0 { | ||
| 48 | break; | ||
| 49 | } | ||
| 50 | if line == "\r\n" || line == "\n" { | ||
| 51 | break; | ||
| 52 | } | ||
| 53 | if let Some(value) = line.to_ascii_lowercase().strip_prefix("content-length:") { | ||
| 54 | content_length = value.trim().parse().unwrap_or(0); | ||
| 55 | } | ||
| 56 | request.push_str(&line); | ||
| 57 | } | ||
| 58 | let mut body = vec![0u8; content_length]; | ||
| 59 | let _ = reader.read_exact(&mut body); | ||
| 60 | request.push_str(&String::from_utf8_lossy(&body)); | ||
| 61 | let _ = tx.send(request); | ||
| 62 | |||
| 63 | let _ = stream.write_all(response.as_bytes()); | ||
| 64 | let _ = stream.flush(); | ||
| 65 | }); | ||
| 66 | |||
| 67 | (format!("http://{addr}"), rx) | ||
| 68 | } | ||
| 69 | |||
| 70 | fn config(base_url: String) -> Config { | ||
| 71 | Config { | ||
| 72 | base_url, | ||
| 73 | api_key: "test-key".into(), | ||
| 74 | model: "test-model".into(), | ||
| 75 | session: "testsession".into(), | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | #[test] | ||
| 80 | fn streams_text_reasoning_and_usage() { | ||
| 81 | const SSE: &str = concat!( | ||
| 82 | "HTTP/1.1 200 OK\r\n", | ||
| 83 | "Content-Type: text/event-stream\r\n", | ||
| 84 | "Connection: close\r\n", | ||
| 85 | "\r\n", | ||
| 86 | "data: {\"choices\":[{\"delta\":{\"reasoning_content\":\"thinking\"}}]}\n\n", | ||
| 87 | "data: {\"choices\":[{\"delta\":{\"content\":\"Hello\"}}]}\n\n", | ||
| 88 | "data: {\"choices\":[{\"delta\":{\"content\":\" from mock\"},\"finish_reason\":\"stop\"}]}\n\n", | ||
| 89 | "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":5,\"completion_tokens\":2}}\n\n", | ||
| 90 | "data: [DONE]\n\n", | ||
| 91 | ); | ||
| 92 | let (base, rx) = serve_once(SSE); | ||
| 93 | let mut sink = Collector::default(); | ||
| 94 | let outcome = provider::chat( | ||
| 95 | &config(base), | ||
| 96 | &[json!({ "role": "user", "content": "hi" })], | ||
| 97 | &AtomicBool::new(false), | ||
| 98 | &mut sink, | ||
| 99 | ) | ||
| 100 | .expect("chat should succeed"); | ||
| 101 | |||
| 102 | assert_eq!(outcome.content, "Hello from mock"); | ||
| 103 | assert_eq!(outcome.reasoning, "thinking"); | ||
| 104 | assert_eq!(sink.text, "Hello from mock"); | ||
| 105 | assert_eq!(sink.reasoning, "thinking"); | ||
| 106 | assert!(sink.finished); | ||
| 107 | assert_eq!(outcome.usage.unwrap()["completion_tokens"], 2); | ||
| 108 | |||
| 109 | let request = rx.recv().unwrap().to_ascii_lowercase(); | ||
| 110 | assert!( | ||
| 111 | request.contains("x-opencode-session: testsession"), | ||
| 112 | "session header missing from request:\n{request}" | ||
| 113 | ); | ||
| 114 | assert!(request.contains("user-agent: agent686/")); | ||
| 115 | assert!(request.contains("\"stream\":true")); | ||
| 116 | assert!(request.contains("\"include_usage\":true")); | ||
| 117 | } | ||
| 118 | |||
| 119 | #[test] | ||
| 120 | fn assembles_tool_call_arguments_byte_for_byte() { | ||
| 121 | const SSE: &str = concat!( | ||
| 122 | "HTTP/1.1 200 OK\r\n", | ||
| 123 | "Content-Type: text/event-stream\r\n", | ||
| 124 | "Connection: close\r\n", | ||
| 125 | "\r\n", | ||
| 126 | "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"function\":{\"name\":\"read\",\"arguments\":\"{\\\"path\\\":\"}}]}}]}\n\n", | ||
| 127 | "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"\\\"a.txt\\\"}\"}}]}}]}\n\n", | ||
| 128 | "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n", | ||
| 129 | "data: [DONE]\n\n", | ||
| 130 | ); | ||
| 131 | let (base, _rx) = serve_once(SSE); | ||
| 132 | let mut sink = Collector::default(); | ||
| 133 | let outcome = provider::chat( | ||
| 134 | &config(base), | ||
| 135 | &[json!({ "role": "user", "content": "read a.txt" })], | ||
| 136 | &AtomicBool::new(false), | ||
| 137 | &mut sink, | ||
| 138 | ) | ||
| 139 | .expect("chat should succeed"); | ||
| 140 | |||
| 141 | assert_eq!(outcome.tool_calls.len(), 1); | ||
| 142 | let call = &outcome.tool_calls[0]; | ||
| 143 | assert_eq!(call.id, "call_1"); | ||
| 144 | assert_eq!(call.name, "read"); | ||
| 145 | assert_eq!(call.arguments, r#"{"path":"a.txt"}"#); | ||
| 146 | assert_eq!(outcome.finish.as_deref(), Some("tool_calls")); | ||
| 147 | } | ||
| 148 | |||
| 149 | #[test] | ||
| 150 | fn surfaces_provider_error_text() { | ||
| 151 | const RESPONSE: &str = concat!( | ||
| 152 | "HTTP/1.1 401 Unauthorized\r\n", | ||
| 153 | "Content-Type: application/json\r\n", | ||
| 154 | "Connection: close\r\n", | ||
| 155 | "\r\n", | ||
| 156 | "{\"error\":{\"type\":\"authentication_error\",\"code\":\"api_key_expired\",", | ||
| 157 | "\"message\":\"This API key has expired.\"}}", | ||
| 158 | ); | ||
| 159 | let (base, _rx) = serve_once(RESPONSE); | ||
| 160 | let mut sink = Collector::default(); | ||
| 161 | let error = provider::chat( | ||
| 162 | &config(base), | ||
| 163 | &[json!({ "role": "user", "content": "hi" })], | ||
| 164 | &AtomicBool::new(false), | ||
| 165 | &mut sink, | ||
| 166 | ) | ||
| 167 | .expect_err("should fail"); | ||
| 168 | |||
| 169 | match error { | ||
| 170 | Error::Status { code, message } => { | ||
| 171 | assert_eq!(code, 401); | ||
| 172 | assert!(message.contains("This API key has expired."), "{message}"); | ||
| 173 | assert!(message.contains("api_key_expired"), "{message}"); | ||
| 174 | } | ||
| 175 | other => panic!("unexpected error: {other:?}"), | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | #[test] | ||
| 180 | fn honours_a_cancelled_turn() { | ||
| 181 | const SSE: &str = "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\ndata: {\"choices\":[{\"delta\":{\"content\":\"x\"}}]}\n\n"; | ||
| 182 | let (base, _rx) = serve_once(SSE); | ||
| 183 | let mut sink = Collector::default(); | ||
| 184 | let error = provider::chat( | ||
| 185 | &config(base), | ||
| 186 | &[json!({ "role": "user", "content": "hi" })], | ||
| 187 | &AtomicBool::new(true), | ||
| 188 | &mut sink, | ||
| 189 | ) | ||
| 190 | .expect_err("cancelled turn should abort"); | ||
| 191 | assert!(matches!(error, Error::Aborted)); | ||
| 192 | } | ||
