Skip to content

Commit f3c6b13

Browse files
authored
Use shared network gating helper in chat completion tests (#6461)
## Summary - replace the bespoke network check in the chat completion payload and SSE tests with the existing `skip_if_no_network!` helper so they follow the same gating convention as the rest of the suite ## Testing - `just fmt` ------ [Codex Task](https://chatgpt.com/codex/tasks/task_i_69112d4cb9f08321ba773e8ccf39778e)
1 parent 9890ceb commit f3c6b13

File tree

2 files changed

+18
-106
lines changed

2 files changed

+18
-106
lines changed

codex-rs/core/tests/chat_completions_payload.rs

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use codex_core::ModelProviderInfo;
1010
use codex_core::Prompt;
1111
use codex_core::ResponseItem;
1212
use codex_core::WireApi;
13-
use codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR;
1413
use codex_otel::otel_event_manager::OtelEventManager;
1514
use codex_protocol::ConversationId;
1615
use codex_protocol::models::ReasoningItemContent;
1716
use core_test_support::load_default_config_for_test;
17+
use core_test_support::skip_if_no_network;
1818
use futures::StreamExt;
1919
use serde_json::Value;
2020
use tempfile::TempDir;
@@ -24,10 +24,6 @@ use wiremock::ResponseTemplate;
2424
use wiremock::matchers::method;
2525
use wiremock::matchers::path;
2626

27-
fn network_disabled() -> bool {
28-
std::env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok()
29-
}
30-
3127
async fn run_request(input: Vec<ResponseItem>) -> Value {
3228
let server = MockServer::start().await;
3329

@@ -191,12 +187,7 @@ fn first_assistant(messages: &[Value]) -> &Value {
191187

192188
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
193189
async fn omits_reasoning_when_none_present() {
194-
if network_disabled() {
195-
println!(
196-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
197-
);
198-
return;
199-
}
190+
skip_if_no_network!();
200191

201192
let body = run_request(vec![user_message("u1"), assistant_message("a1")]).await;
202193
let messages = messages_from(&body);
@@ -208,12 +199,7 @@ async fn omits_reasoning_when_none_present() {
208199

209200
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
210201
async fn attaches_reasoning_to_previous_assistant() {
211-
if network_disabled() {
212-
println!(
213-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
214-
);
215-
return;
216-
}
202+
skip_if_no_network!();
217203

218204
let body = run_request(vec![
219205
user_message("u1"),
@@ -230,12 +216,7 @@ async fn attaches_reasoning_to_previous_assistant() {
230216

231217
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
232218
async fn attaches_reasoning_to_function_call_anchor() {
233-
if network_disabled() {
234-
println!(
235-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
236-
);
237-
return;
238-
}
219+
skip_if_no_network!();
239220

240221
let body = run_request(vec![
241222
user_message("u1"),
@@ -257,12 +238,7 @@ async fn attaches_reasoning_to_function_call_anchor() {
257238

258239
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
259240
async fn attaches_reasoning_to_local_shell_call() {
260-
if network_disabled() {
261-
println!(
262-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
263-
);
264-
return;
265-
}
241+
skip_if_no_network!();
266242

267243
let body = run_request(vec![
268244
user_message("u1"),
@@ -282,12 +258,7 @@ async fn attaches_reasoning_to_local_shell_call() {
282258

283259
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
284260
async fn drops_reasoning_when_last_role_is_user() {
285-
if network_disabled() {
286-
println!(
287-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
288-
);
289-
return;
290-
}
261+
skip_if_no_network!();
291262

292263
let body = run_request(vec![
293264
assistant_message("aPrev"),
@@ -301,12 +272,7 @@ async fn drops_reasoning_when_last_role_is_user() {
301272

302273
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
303274
async fn ignores_reasoning_before_last_user() {
304-
if network_disabled() {
305-
println!(
306-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
307-
);
308-
return;
309-
}
275+
skip_if_no_network!();
310276

311277
let body = run_request(vec![
312278
user_message("u1"),
@@ -321,12 +287,7 @@ async fn ignores_reasoning_before_last_user() {
321287

322288
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
323289
async fn skips_empty_reasoning_segments() {
324-
if network_disabled() {
325-
println!(
326-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
327-
);
328-
return;
329-
}
290+
skip_if_no_network!();
330291

331292
let body = run_request(vec![
332293
user_message("u1"),
@@ -342,12 +303,7 @@ async fn skips_empty_reasoning_segments() {
342303

343304
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
344305
async fn suppresses_duplicate_assistant_messages() {
345-
if network_disabled() {
346-
println!(
347-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
348-
);
349-
return;
350-
}
306+
skip_if_no_network!();
351307

352308
let body = run_request(vec![assistant_message("dup"), assistant_message("dup")]).await;
353309
let messages = messages_from(&body);

codex-rs/core/tests/chat_completions_sse.rs

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use codex_core::Prompt;
1010
use codex_core::ResponseEvent;
1111
use codex_core::ResponseItem;
1212
use codex_core::WireApi;
13-
use codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR;
1413
use codex_otel::otel_event_manager::OtelEventManager;
1514
use codex_protocol::ConversationId;
1615
use codex_protocol::models::ReasoningItemContent;
1716
use core_test_support::load_default_config_for_test;
17+
use core_test_support::skip_if_no_network;
1818
use futures::StreamExt;
1919
use tempfile::TempDir;
2020
use wiremock::Mock;
@@ -23,10 +23,6 @@ use wiremock::ResponseTemplate;
2323
use wiremock::matchers::method;
2424
use wiremock::matchers::path;
2525

26-
fn network_disabled() -> bool {
27-
std::env::var(CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok()
28-
}
29-
3026
async fn run_stream(sse_body: &str) -> Vec<ResponseEvent> {
3127
run_stream_with_bytes(sse_body.as_bytes()).await
3228
}
@@ -157,12 +153,7 @@ fn assert_reasoning(item: &ResponseItem, expected: &str) {
157153

158154
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
159155
async fn streams_text_without_reasoning() {
160-
if network_disabled() {
161-
println!(
162-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
163-
);
164-
return;
165-
}
156+
skip_if_no_network!();
166157

167158
let sse = concat!(
168159
"data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}]}\n\n",
@@ -193,12 +184,7 @@ async fn streams_text_without_reasoning() {
193184

194185
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
195186
async fn streams_reasoning_from_string_delta() {
196-
if network_disabled() {
197-
println!(
198-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
199-
);
200-
return;
201-
}
187+
skip_if_no_network!();
202188

203189
let sse = concat!(
204190
"data: {\"choices\":[{\"delta\":{\"reasoning\":\"think1\"}}]}\n\n",
@@ -250,12 +236,7 @@ async fn streams_reasoning_from_string_delta() {
250236

251237
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
252238
async fn streams_reasoning_from_object_delta() {
253-
if network_disabled() {
254-
println!(
255-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
256-
);
257-
return;
258-
}
239+
skip_if_no_network!();
259240

260241
let sse = concat!(
261242
"data: {\"choices\":[{\"delta\":{\"reasoning\":{\"text\":\"partA\"}}}]}\n\n",
@@ -319,12 +300,7 @@ async fn streams_reasoning_from_object_delta() {
319300

320301
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
321302
async fn streams_reasoning_from_final_message() {
322-
if network_disabled() {
323-
println!(
324-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
325-
);
326-
return;
327-
}
303+
skip_if_no_network!();
328304

329305
let sse = "data: {\"choices\":[{\"message\":{\"reasoning\":\"final-cot\"},\"finish_reason\":\"stop\"}]}\n\n";
330306

@@ -357,12 +333,7 @@ async fn streams_reasoning_from_final_message() {
357333

358334
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
359335
async fn streams_reasoning_before_tool_call() {
360-
if network_disabled() {
361-
println!(
362-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
363-
);
364-
return;
365-
}
336+
skip_if_no_network!();
366337

367338
let sse = concat!(
368339
"data: {\"choices\":[{\"delta\":{\"reasoning\":\"pre-tool\"}}]}\n\n",
@@ -413,12 +384,7 @@ async fn streams_reasoning_before_tool_call() {
413384
#[tokio::test]
414385
#[traced_test]
415386
async fn chat_sse_emits_failed_on_parse_error() {
416-
if network_disabled() {
417-
println!(
418-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
419-
);
420-
return;
421-
}
387+
skip_if_no_network!();
422388

423389
let sse_body = concat!("data: not-json\n\n", "data: [DONE]\n\n");
424390

@@ -450,12 +416,7 @@ async fn chat_sse_emits_failed_on_parse_error() {
450416
#[tokio::test]
451417
#[traced_test]
452418
async fn chat_sse_done_chunk_emits_event() {
453-
if network_disabled() {
454-
println!(
455-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
456-
);
457-
return;
458-
}
419+
skip_if_no_network!();
459420

460421
let sse_body = "data: [DONE]\n\n";
461422

@@ -473,12 +434,7 @@ async fn chat_sse_done_chunk_emits_event() {
473434
#[tokio::test]
474435
#[traced_test]
475436
async fn chat_sse_emits_error_on_invalid_utf8() {
476-
if network_disabled() {
477-
println!(
478-
"Skipping test because it cannot execute when network is disabled in a Codex sandbox."
479-
);
480-
return;
481-
}
437+
skip_if_no_network!();
482438

483439
let _ = run_stream_with_bytes(b"data: \x80\x80\n\n").await;
484440

0 commit comments

Comments
 (0)