test: serialize CONFIRM/HANDOFF env mutations across crates

Avoid parallel cargo test flakes when multiple tests set process env.
This commit is contained in:
王性驊 2026-09-13 16:54:48 +08:00
parent 15d271b656
commit 44b0d7c038
4 changed files with 20 additions and 0 deletions

View File

@ -743,6 +743,7 @@ mod tests {
#[test]
fn handoff_auto_resume_and_abort() {
let _env_lock = crate::test_env::lock();
// SAFETY: tests run serially for this env in practice; restore after.
let prev = std::env::var("GROKBOY_HANDOFF_AUTO").ok();
unsafe { std::env::set_var("GROKBOY_HANDOFF_AUTO", "1") };

View File

@ -267,6 +267,7 @@ mod tests {
#[test]
fn confirm_auto_approve_via_confirm_env() {
let _env_lock = crate::test_env::lock();
let prev_c = std::env::var("GROKBOY_CONFIRM_AUTO").ok();
let prev_h = std::env::var("GROKBOY_HANDOFF_AUTO").ok();
unsafe {
@ -288,6 +289,7 @@ mod tests {
#[test]
fn confirm_auto_falls_back_to_handoff_env() {
let _env_lock = crate::test_env::lock();
let prev_c = std::env::var("GROKBOY_CONFIRM_AUTO").ok();
let prev_h = std::env::var("GROKBOY_HANDOFF_AUTO").ok();
unsafe {
@ -308,6 +310,7 @@ mod tests {
#[test]
fn execute_approve_and_deny_offline() {
let _env_lock = crate::test_env::lock();
let prev_c = std::env::var("GROKBOY_CONFIRM_AUTO").ok();
let prev_h = std::env::var("GROKBOY_HANDOFF_AUTO").ok();
unsafe {

View File

@ -19,3 +19,17 @@ pub use config::Config;
pub use model::{ChatMessage, FunctionCall, Role, ToolCall, chat_completion, stream_chat};
pub use session::{Session, load_or_create, load_session, save_session, sessions_dir};
pub use tools::{ToolContext, execute_tool, is_completion_tool, tool_definitions};
/// Serialize tests that mutate process env (CONFIRM_AUTO / HANDOFF_AUTO).
#[cfg(test)]
pub(crate) mod test_env {
use std::sync::{Mutex, MutexGuard, OnceLock};
pub(crate) fn lock() -> MutexGuard<'static, ()> {
static M: OnceLock<Mutex<()>> = OnceLock::new();
M.get_or_init(|| Mutex::new(()))
.lock()
.unwrap_or_else(|e| e.into_inner())
}
}

View File

@ -522,6 +522,7 @@ mod tests {
#[tokio::test]
async fn browser_handoff_auto_resume_protocol() {
let _env_lock = crate::test_env::lock();
// Offline: with GROKBOY_HANDOFF_AUTO=1, missing Chromium still fail-closes
// OR (if Chromium present) resumes and returns snapshot/error JSON — never hangs.
let prev = std::env::var("GROKBOY_HANDOFF_AUTO").ok();
@ -553,6 +554,7 @@ mod tests {
#[tokio::test]
async fn request_user_confirm_auto_approve_and_deny() {
let _env_lock = crate::test_env::lock();
let prev_c = std::env::var("GROKBOY_CONFIRM_AUTO").ok();
let prev_h = std::env::var("GROKBOY_HANDOFF_AUTO").ok();
unsafe {