mirror of
https://github.com/openai/codex.git
synced 2026-11-01 19:33:35 +00:00
78 lines
2.4 KiB
Rust
78 lines
2.4 KiB
Rust
#![allow(clippy::expect_used)]
|
|
|
|
use tempfile::TempDir;
|
|
|
|
use codex_core::config::Config;
|
|
use codex_core::config::ConfigOverrides;
|
|
use codex_core::config::ConfigToml;
|
|
|
|
pub fn load_default_config_for_test(codex_home: &TempDir) -> Config {
|
|
Config::load_from_base_config_with_overrides(
|
|
ConfigToml::default(),
|
|
ConfigOverrides::default(),
|
|
codex_home.path().to_path_buf(),
|
|
)
|
|
.expect("defaults for test should always succeed")
|
|
}
|
|
|
|
pub fn load_sse_fixture(path: impl AsRef<std::path::Path>) -> String {
|
|
let events: Vec<serde_json::Value> =
|
|
serde_json::from_reader(std::fs::File::open(path).expect("read fixture"))
|
|
.expect("parse JSON fixture");
|
|
events
|
|
.into_iter()
|
|
.map(|e| {
|
|
let kind = e
|
|
.get("type")
|
|
.and_then(|v| v.as_str())
|
|
.expect("fixture event missing type");
|
|
if e.as_object().map(|o| o.len() == 1).unwrap_or(false) {
|
|
format!("event: {kind}\n\n")
|
|
} else {
|
|
format!("event: {kind}\ndata: {e}\n\n")
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub fn load_sse_fixture_with_id(path: impl AsRef<std::path::Path>, id: &str) -> String {
|
|
let raw = std::fs::read_to_string(path).expect("read fixture template");
|
|
let replaced = raw.replace("__ID__", id);
|
|
let events: Vec<serde_json::Value> =
|
|
serde_json::from_str(&replaced).expect("parse JSON fixture");
|
|
events
|
|
.into_iter()
|
|
.map(|e| {
|
|
let kind = e
|
|
.get("type")
|
|
.and_then(|v| v.as_str())
|
|
.expect("fixture event missing type");
|
|
if e.as_object().map(|o| o.len() == 1).unwrap_or(false) {
|
|
format!("event: {kind}\n\n")
|
|
} else {
|
|
format!("event: {kind}\ndata: {e}\n\n")
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub async fn wait_for_event<F>(
|
|
codex: &codex_core::Codex,
|
|
mut predicate: F,
|
|
) -> codex_core::protocol::EventMsg
|
|
where
|
|
F: FnMut(&codex_core::protocol::EventMsg) -> bool,
|
|
{
|
|
use tokio::time::Duration;
|
|
use tokio::time::timeout;
|
|
loop {
|
|
let ev = timeout(Duration::from_secs(1), codex.next_event())
|
|
.await
|
|
.expect("timeout waiting for event")
|
|
.expect("stream ended unexpectedly");
|
|
if predicate(&ev.msg) {
|
|
return ev.msg;
|
|
}
|
|
}
|
|
}
|