ai-dispatch 11.2.0

Multi-AI CLI team orchestrator
// Tests for the `run` command module after splitting from run.rs.
// Covers dispatch validation, quota detection, cascade behavior, and dry-run flow.
// Depends on the parent run module, store, paths, tokio, and tempfile.
use super::*;
use crate::store::Store;
use crate::types::{AgentKind, Task, TaskStatus, VerifyStatus};
use std::process::Command;
use std::sync::Arc;
use tempfile::TempDir;

fn git(dir: &std::path::Path, args: &[&str]) {
    let output = Command::new("git")
        .current_dir(dir)
        .args(args)
        .output()
        .expect("git command failed");
    assert!(output.status.success(), "git {:?} failed: {}", args, String::from_utf8_lossy(&output.stderr));
}

fn make_failed_task(task_id: &str) -> Task {
    Task {
        id: TaskId(task_id.to_string()),
        agent: AgentKind::Codex,
        custom_agent_name: None,
        prompt: "prompt".to_string(),
        resolved_prompt: None,
        category: None,
        status: TaskStatus::Failed,
        parent_task_id: None,
        workgroup_id: None,
        caller_kind: None,
        caller_session_id: None,
        agent_session_id: None,
        repo_path: None, project_id: None,
        worktree_path: None, effective_dir: None,
        worktree_branch: None,
        final_head_sha: None,
        final_branch: None,
        start_sha: None,
        log_path: None,
        output_path: None,
        tokens: None,
        prompt_tokens: None,
        duration_ms: None,
        requested_model: None, observed_model: None, attribution_source: None,
        cost_usd: None,
        exit_code: Some(1),
        created_at: chrono::Local::now(),
        completed_at: None,
        verify: None,
        verify_status: VerifyStatus::Failed,
        pending_reason: None,
        read_only: false,
        budget: false,
        audit_verdict: None,
        audit_report_path: None,
        delivery_assessment: None,
    }
}

#[path = "delivery_recovery_tests.rs"]
mod delivery_recovery_tests;

#[path = "transcript_tests.rs"]
mod run_transcript_tests;

#[path = "async_tests.rs"]
mod run_async_tests;

fn quota_worktree(dir: &std::path::Path, branch: Option<&str>) -> std::path::PathBuf {
    let wt_dir = dir.join("wt");
    std::fs::create_dir_all(&wt_dir).unwrap();
    if let Some(branch) = branch {
        git(&wt_dir, &["init", "-b", branch]);
    } else {
        git(&wt_dir, &["init"]);
    }
    git(&wt_dir, &["config", "user.email", "aid@example.com"]);
    git(&wt_dir, &["config", "user.name", "Aid Tester"]);
    std::fs::write(wt_dir.join("file.txt"), "initial").unwrap();
    git(&wt_dir, &["add", "file.txt"]);
    git(&wt_dir, &["commit", "-m", "initial"]);
    wt_dir
}

fn assert_terminal_failure_preserved(store: &Store, id: &TaskId, quota: Option<&str>) {
    assert!(quota.is_some(), "quota fixture must be recognized");
    let before = store.get_task(id.as_str()).unwrap().unwrap();
    let events = store.get_events(id.as_str()).unwrap().len();
    assert!(!store.update_task_status(id.as_str(), TaskStatus::Done).unwrap());
    let after = store.get_task(id.as_str()).unwrap().unwrap();
    assert_eq!(after.status, TaskStatus::Failed);
    assert_eq!(after.completed_at, before.completed_at);
    assert_eq!(after.duration_ms, before.duration_ms);
    assert_eq!(store.get_events(id.as_str()).unwrap().len(), events);
}

#[path = "tests_quota.rs"]
mod quota;

#[path = "tests_validation.rs"]
mod validation;

#[path = "tests_lifecycle.rs"]
mod lifecycle;