test(opencode): real-HTTP and live inference tests

Integration tests drive the curl client against a local TCP server
(status preservation, chunked multibyte bodies, oversized bodies,
redirects, abrupt disconnects, authentication). The live tests obtain a
real model response when PHOEBUS_OPENCODE_LIVE_* is set and fail as
BLOCKED when a live run is required but not configured.
This commit is contained in:
Phoebus 2026-09-22 02:36:28 -07:00
parent 565521564c
commit 5c3674e3c2
2 changed files with 526 additions and 0 deletions

View file

@ -0,0 +1,386 @@
//! Integration tests for the OpenCode HTTP client and protocol adapter over a
//! **real** local TCP server and the **real** curl transport.
//!
//! These exercise the parts the fake-client unit tests cannot: the curl config
//! encoding, HTTP status/marker parsing, de-chunking (including a multibyte
//! character split across chunks), body-size limits, redirect refusal, and the
//! request authentication. The server is a minimal hand-rolled HTTP/1.1
//! responder that terminates cleanly.
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use phoebus_core::{Redactor, SecretString};
use phoebus_executor::opencode_client::{CurlHttpClient, HttpClient, HttpRequest, OpenCodeClient};
use phoebus_executor::opencode_server::{probe, Endpoint, ProbeRequest};
use phoebus_executor::{Exec, Logger, RealRunner};
#[derive(Clone)]
struct Route {
method: String,
/// Exact path match, or a prefix if it ends with `*`, or any method when
/// `method` is `*`.
path: String,
status: u16,
body: Vec<u8>,
chunked: bool,
require_auth: Option<String>,
location: Option<String>,
close_without_response: bool,
}
impl Route {
fn get(path: &str, status: u16, body: &str) -> Self {
Self {
method: "GET".into(),
path: path.into(),
status,
body: body.as_bytes().to_vec(),
chunked: false,
require_auth: None,
location: None,
close_without_response: false,
}
}
fn method(method: &str, path: &str, status: u16, body: &str) -> Self {
let mut route = Self::get(path, status, body);
route.method = method.into();
route
}
fn matches(&self, method: &str, path: &str) -> bool {
if self.method != "*" && self.method != method {
return false;
}
if let Some(prefix) = self.path.strip_suffix('*') {
path.starts_with(prefix)
} else {
path == self.path
}
}
}
struct Server {
port: u16,
stop: Arc<AtomicBool>,
auth_seen: Arc<Mutex<Vec<String>>>,
}
impl Drop for Server {
fn drop(&mut self) {
self.stop.store(true, Ordering::SeqCst);
}
}
fn spawn_server(mut routes: Vec<Route>) -> Server {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
listener.set_nonblocking(true).unwrap();
let stop = Arc::new(AtomicBool::new(false));
let stop_thread = stop.clone();
let auth_seen = Arc::new(Mutex::new(Vec::new()));
let auth_thread = auth_seen.clone();
thread::spawn(move || {
while !stop_thread.load(Ordering::SeqCst) {
match listener.accept() {
Ok((mut stream, _)) => {
let _ = stream.set_read_timeout(Some(Duration::from_secs(5)));
let _ = handle(&mut stream, &mut routes, &auth_thread);
}
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
thread::sleep(Duration::from_millis(5));
}
Err(_) => break,
}
}
});
Server {
port,
stop,
auth_seen,
}
}
fn handle(
stream: &mut TcpStream,
routes: &mut Vec<Route>,
auth_seen: &Arc<Mutex<Vec<String>>>,
) -> std::io::Result<()> {
let mut buf = [0u8; 16384];
let n = stream.read(&mut buf)?;
if n == 0 {
return Ok(());
}
let request = String::from_utf8_lossy(&buf[..n]).to_string();
let mut lines = request.lines();
let request_line = lines.next().unwrap_or("");
let mut parts = request_line.split_whitespace();
let method = parts.next().unwrap_or("").to_string();
let path = parts.next().unwrap_or("").to_string();
let auth = request
.lines()
.find_map(|l| {
l.strip_prefix("Authorization:")
.map(|v| v.trim().to_string())
})
.unwrap_or_default();
if !auth.is_empty() {
auth_seen.lock().unwrap().push(auth.clone());
}
// Routes are permanent: a test may issue the same request more than once
// (e.g. once unauthenticated, once authenticated).
let Some(route) = routes.iter().find(|r| r.matches(&method, &path)).cloned() else {
let body = b"{}";
write_response(stream, 404, &[], body, false, None)?;
return Ok(());
};
if route.close_without_response {
return Ok(()); // drop the stream without a response
}
if let Some(expected) = &route.require_auth {
if &auth != expected {
write_response(stream, 401, &[], b"{}", false, None)?;
return Ok(());
}
}
write_response(
stream,
route.status,
&[],
&route.body,
route.chunked,
route.location.as_deref(),
)
}
fn write_response(
stream: &mut TcpStream,
status: u16,
extra: &[(String, String)],
body: &[u8],
chunked: bool,
location: Option<&str>,
) -> std::io::Result<()> {
let mut head = format!("HTTP/1.1 {status} X\r\n");
if let Some(location) = location {
head.push_str(&format!("Location: {location}\r\n"));
}
for (k, v) in extra {
head.push_str(&format!("{k}: {v}\r\n"));
}
if chunked {
head.push_str("Transfer-Encoding: chunked\r\n");
} else {
head.push_str(&format!("Content-Length: {}\r\n", body.len()));
}
head.push_str("Connection: close\r\n\r\n");
stream.write_all(head.as_bytes())?;
if chunked {
// Split the body into small chunks; the first cut may fall inside a
// multibyte UTF-8 sequence.
for chunk in body.chunks(3) {
stream.write_all(format!("{:x}\r\n", chunk.len()).as_bytes())?;
stream.write_all(chunk)?;
stream.write_all(b"\r\n")?;
}
stream.write_all(b"0\r\n\r\n")?;
} else {
stream.write_all(body)?;
}
stream.flush()
}
fn test_exec() -> Exec {
let dir = tempfile::tempdir().unwrap().keep();
let logger = Logger::open(dir.join("opencode.log"), Redactor::new(), false);
Exec::new(Box::new(RealRunner), logger, false, dir.clone(), dir)
}
fn base(port: u16) -> String {
format!("http://127.0.0.1:{port}")
}
#[test]
fn curl_transport_round_trips_and_preserves_status() {
let server = spawn_server(vec![
Route::get("/global/health", 200, r#"{"healthy":true}"#),
Route::get("/provider", 500, r#"{"all":[{"id":"x","models":{}}]}"#),
]);
let exec = test_exec();
let http = CurlHttpClient::new(&exec);
let ok = http
.request(&HttpRequest::get(format!(
"{}/global/health",
base(server.port)
)))
.unwrap();
assert_eq!(ok.status, 200);
assert!(ok.body.contains("healthy"));
// A 500 with a providers-shaped body is a 500; the status is not discarded.
let failure = http
.request(&HttpRequest::get(format!("{}/provider", base(server.port))))
.unwrap();
assert_eq!(failure.status, 500);
}
#[test]
fn curl_transport_dechunks_a_multibyte_body_split_across_chunks() {
// "héllo wörld" in UTF-8; chunk boundaries at 3 bytes split the multibyte
// characters. curl must reassemble the bytes before we decode them.
let text = "héllo wörld";
let server = spawn_server(vec![Route {
chunked: true,
..Route::get("/chunked", 200, text)
}]);
let exec = test_exec();
let http = CurlHttpClient::new(&exec);
let response = http
.request(&HttpRequest::get(format!("{}/chunked", base(server.port))))
.unwrap();
assert_eq!(response.status, 200);
assert_eq!(response.body, text);
}
#[test]
fn curl_transport_refuses_an_oversized_body() {
let big = "x".repeat(2 * 1024 * 1024);
let server = spawn_server(vec![Route::get("/big", 200, &big)]);
let exec = test_exec();
let http = CurlHttpClient::new(&exec);
let mut request = HttpRequest::get(format!("{}/big", base(server.port)));
request.max_body = 4096;
assert!(
http.request(&request).is_err(),
"an oversized body must fail"
);
}
#[test]
fn curl_transport_does_not_follow_redirects() {
let mut redirect = Route::get("/redir", 302, "");
redirect.location = Some("/final".into());
let server = spawn_server(vec![redirect, Route::get("/final", 200, "landed")]);
let exec = test_exec();
let http = CurlHttpClient::new(&exec);
let response = http
.request(&HttpRequest::get(format!("{}/redir", base(server.port))))
.unwrap();
assert_eq!(response.status, 302, "a redirect must not be followed");
assert!(!response.body.contains("landed"));
}
#[test]
fn curl_transport_reports_an_abrupt_disconnect() {
let server = spawn_server(vec![Route {
close_without_response: true,
..Route::get("/drop", 200, "")
}]);
let exec = test_exec();
let http = CurlHttpClient::new(&exec);
assert!(http
.request(&HttpRequest::get(format!("{}/drop", base(server.port))))
.is_err());
}
#[test]
fn opencode_client_adapts_over_real_http() {
let server = spawn_server(vec![
Route::get(
"/global/health",
200,
r#"{"healthy":true,"version":"1.18.32"}"#,
),
Route::get(
"/provider",
200,
r#"{"all":[{"id":"deepseek","name":"DeepSeek","models":{"deepseek-chat":{}}}],
"connected":["deepseek"],"default":{"deepseek":"deepseek-chat"}}"#,
),
Route::method("POST", "/session", 200, r#"{"id":"ses_abc"}"#),
Route::method(
"POST",
"/session/ses_abc/message",
200,
r#"{"info":{"role":"assistant","time":{"created":1,"completed":2}},
"parts":[{"type":"text","text":"{\"version\":1}"}]}"#,
),
Route::method("DELETE", "/session/ses_abc", 200, "true"),
]);
let exec = test_exec();
let http = CurlHttpClient::new(&exec);
let endpoint = Endpoint::parse(&base(server.port)).unwrap();
let mut request = ProbeRequest::new();
request.provider = Some("deepseek".into());
request.model = Some("deepseek-chat".into());
let capability = probe(&http, &endpoint, &request);
assert_eq!(
capability.state,
phoebus_executor::opencode_server::ConnectionState::ModelCatalog
);
assert!(capability.selected_model_available);
let client = OpenCodeClient::new(&http, base(server.port), "deepseek", "deepseek-chat");
let reply = client.one_shot("system", "user").unwrap();
assert_eq!(reply.content, "{\"version\":1}");
assert!(reply.completed);
}
#[test]
fn authentication_is_required_and_presented() {
// The endpoint demands basic auth; the matching header is recorded.
// base64("opencode:sekret").
let required = "Basic b3BlbmNvZGU6c2VrcmV0".to_string();
let mut route = Route::get(
"/provider",
200,
r#"{"all":[{"id":"x","models":{"m":{}}}],"connected":[]}"#,
);
route.require_auth = Some(required.clone());
let server = spawn_server(vec![
Route {
require_auth: Some(required),
..Route::get("/global/health", 200, r#"{"healthy":true}"#)
},
route,
]);
let exec = test_exec();
let http = CurlHttpClient::new(&exec);
let endpoint = Endpoint::parse(&base(server.port)).unwrap();
// Without a credential the probe reports "authentication required".
let mut request = ProbeRequest::new();
request.provider = Some("x".into());
request.model = Some("m".into());
let capability = probe(&http, &endpoint, &request);
assert_eq!(
capability.state,
phoebus_executor::opencode_server::ConnectionState::AuthenticationRequired
);
// With the credential it succeeds and the header was actually sent.
let mut request = ProbeRequest::new();
request.auth = Some(SecretString::new("sekret"));
let capability = probe(&http, &endpoint, &request);
assert_eq!(
capability.state,
phoebus_executor::opencode_server::ConnectionState::ModelCatalog,
"{capability:?}"
);
assert!(
server
.auth_seen
.lock()
.unwrap()
.iter()
.any(|h| h.starts_with("Basic ")),
"the Authorization header never reached the server"
);
}

View file

@ -0,0 +1,140 @@
//! Live OpenCode acceptance test.
//!
//! This is the only test that proves **real model inference** through OpenCode.
//! It is skipped unless the environment names a server, provider and model:
//!
//! ```sh
//! PHOEBUS_OPENCODE_LIVE_URL=http://127.0.0.1:4096 \
//! PHOEBUS_OPENCODE_LIVE_PROVIDER=deepseek \
//! PHOEBUS_OPENCODE_LIVE_MODEL=deepseek-chat \
//! cargo test -p phoebus-executor --test opencode_live -- --nocapture
//! ```
//!
//! When `PHOEBUS_OPENCODE_REQUIRE_LIVE=1` is set (as `make opencode-acceptance`
//! does), a missing configuration is a hard failure, so an acceptance command
//! cannot pass by silently not running.
use std::time::Duration;
use phoebus_core::SecretString;
use phoebus_executor::opencode_client::{CurlHttpClient, OpenCodeClient};
use phoebus_executor::opencode_server::{probe, ConnectionState, Endpoint, ProbeRequest};
use phoebus_executor::{Exec, Logger, RealRunner};
struct Live {
url: String,
provider: String,
model: String,
password: Option<SecretString>,
}
fn live() -> Option<Live> {
let url = std::env::var("PHOEBUS_OPENCODE_LIVE_URL").unwrap_or_default();
let provider = std::env::var("PHOEBUS_OPENCODE_LIVE_PROVIDER").unwrap_or_default();
let model = std::env::var("PHOEBUS_OPENCODE_LIVE_MODEL").unwrap_or_default();
let required = std::env::var("PHOEBUS_OPENCODE_REQUIRE_LIVE")
.map(|v| matches!(v.as_str(), "1" | "yes" | "true"))
.unwrap_or(false);
if url.trim().is_empty() || provider.trim().is_empty() || model.trim().is_empty() {
if required {
panic!(
"BLOCKED: a live OpenCode acceptance run was required, but \
PHOEBUS_OPENCODE_LIVE_URL/PROVIDER/MODEL are not all set"
);
}
eprintln!(
"SKIPPED: set PHOEBUS_OPENCODE_LIVE_URL, PHOEBUS_OPENCODE_LIVE_PROVIDER and \
PHOEBUS_OPENCODE_LIVE_MODEL to run the live acceptance test"
);
return None;
}
let password = match std::env::var("PHOEBUS_OPENCODE_LIVE_PASSWORD_FILE") {
Ok(path) if !path.is_empty() => {
Some(phoebus_executor::ai::load_api_key(std::path::Path::new(&path)).unwrap())
}
_ => None,
};
Some(Live {
url,
provider,
model,
password,
})
}
fn exec() -> Exec {
let dir = tempfile::tempdir().unwrap().keep();
let logger = Logger::open(
dir.join("opencode-live.log"),
phoebus_core::Redactor::new(),
false,
);
Exec::new(Box::new(RealRunner), logger, false, dir.clone(), dir)
}
#[test]
fn live_inference_through_opencode() {
let Some(live) = live() else { return };
let exec = exec();
let http = CurlHttpClient::new(&exec);
let endpoint = Endpoint::parse(&live.url).expect("a valid live endpoint");
// 1. The catalog must list the selected model.
let mut request = ProbeRequest::new();
request.provider = Some(live.provider.clone());
request.model = Some(live.model.clone());
request.auth = live.password.clone();
let capability = probe(&http, &endpoint, &request);
eprintln!("live capability: {capability:?}");
assert!(
matches!(
capability.state,
ConnectionState::ModelCatalog | ConnectionState::SelectedModelUnavailable
),
"the live server is not usable: {capability:?}"
);
assert!(
capability.selected_model_available,
"the selected model {}:{} is not in the catalog: {capability:?}",
live.provider, live.model
);
// 2. A real inference round-trip must return a valid JSON response.
let client = OpenCodeClient::new(&http, endpoint.base(), &live.provider, &live.model)
.with_auth(live.password.clone())
.with_timeout(Duration::from_secs(120));
let reply = client
.one_shot(
"You are a connectivity test. Reply with JSON only.",
"Reply with exactly {\"phoebus\":\"ok\"} and nothing else.",
)
.unwrap_or_else(|e| panic!("live inference failed: {e}"));
let value: serde_json::Value =
serde_json::from_str(&reply.content).expect("the live model returned non-JSON");
assert_eq!(
value.get("phoebus").and_then(|v| v.as_str()),
Some("ok"),
"the live model did not follow the instruction: {}",
reply.content
);
eprintln!("live inference OK: {}", reply.content);
}
#[test]
fn live_endpoint_is_reported_truthfully() {
let Some(live) = live() else { return };
let exec = exec();
let http = CurlHttpClient::new(&exec);
let endpoint = Endpoint::parse(&live.url).unwrap();
// A deliberate wrong model must be reported as unavailable, not accepted.
let mut request = ProbeRequest::new();
request.provider = Some(live.provider.clone());
request.model = Some("definitely-not-a-real-model-xyz".into());
request.auth = live.password.clone();
let capability = probe(&http, &endpoint, &request);
assert_eq!(
capability.state,
ConnectionState::SelectedModelUnavailable,
"an unknown model was not reported as unavailable: {capability:?}"
);
}