ziyuan-cli 1.49.1

紫垣 ZiYuan:用中国古代朝廷角色组织多智能体协作的命令行工具,改动须用户审阅批准才写入文件。
use super::*;

fn command(args: &[&str]) -> Result<Command, Box<dyn Error>> {
    parse(args.iter().map(|s| (*s).to_owned()).collect())
}

#[test]
fn dag_add_and_deps_parse_and_reject_bad_forms() {
    match command(&[
        "dag",
        "add",
        "3",
        "4:补写回归测试",
        "--deps",
        "5,#6",
        "--artifact",
        "tests/x.rs",
        "--attempts",
        "2",
        "--budget",
        "4000",
        "--approval",
        "--key",
        "k1",
    ])
    .unwrap()
    {
        Command::DagAdd { parent, subtask } => {
            assert_eq!(parent, TaskId(3));
            assert_eq!(subtask.worker, AgentId(4));
            assert_eq!(subtask.text, "补写回归测试");
            assert_eq!(subtask.dependencies, vec![TaskId(5), TaskId(6)]);
            assert_eq!(subtask.artifact.as_deref(), Some("tests/x.rs"));
            assert_eq!(subtask.attempts, Some(2));
            assert_eq!(subtask.budget, Some(4000));
            assert!(subtask.approval_required);
            assert_eq!(subtask.idempotency_key.as_deref(), Some("k1"));
        }
        _ => panic!("unexpected command"),
    }
    assert!(matches!(
        command(&["dag", "deps", "6", "--deps", "5"]).unwrap(),
        Command::DagDeps {
            subtask: TaskId(6),
            dependencies
        } if dependencies == vec![TaskId(5)]
    ));
    assert!(matches!(
        command(&["dag", "deps", "6", "--clear"]).unwrap(),
        Command::DagDeps {
            subtask: TaskId(6),
            dependencies
        } if dependencies.is_empty()
    ));
    for bad in [
        vec!["dag"],
        vec!["dag", "add", "3"],
        vec!["dag", "add", "3", "badspec"],
        vec!["dag", "deps", "6"],
        vec!["dag", "deps", "6", "--deps", ""],
        vec!["dag", "deps", "6", "--clear", "--deps", "5"],
    ] {
        assert!(command(&bad).is_err(), "{bad:?}");
    }
}

#[test]
fn split_forms_are_exclusive_and_legacy_forms_remain() {
    assert!(matches!(
        command(&["split", "7"]).unwrap(),
        Command::Split {
            task: TaskId(7),
            plan,
            file: None,
            parallel_writes: false,
        } if plan.is_empty()
    ));
    assert!(matches!(
        command(&["split", "7", "4:first", "4:second"]).unwrap(),
        Command::Split {
            task: TaskId(7),
            plan,
            file: None,
            parallel_writes: false,
        } if plan.len() == 2
    ));
    assert!(matches!(
        command(&["split", "7", "--plan", "plan.json"]).unwrap(),
        Command::Split {
            task: TaskId(7),
            plan,
            file: Some(f),
            parallel_writes: false,
        } if plan.is_empty() && f == "plan.json"
    ));
    assert!(matches!(
        command(&["split", "7", "--parallel-writes", "--plan", "plan.json"]).unwrap(),
        Command::Split {
            task: TaskId(7),
            plan,
            file: Some(f),
            parallel_writes: true,
        } if plan.is_empty() && f == "plan.json"
    ));
    assert!(matches!(
        command(&["split", "7", "--parallel-writes", "4:a", "4:b"]).unwrap(),
        Command::Split {
            task: TaskId(7),
            plan,
            file: None,
            parallel_writes: true,
        } if plan.len() == 2
    ));
    for args in [
        vec!["split", "7", "--plan"],
        vec!["split", "7", "--plan", "p.json", "4:x"],
        vec!["split", "7", "4:x", "--plan", "p.json"],
        vec!["split", "7", "--plan", "p.json", "--plan", "q.json"],
        vec!["split", "7", "--other", "p.json"],
        vec!["split", "7", "--plan", "--plan"],
        vec!["split", "7", "--parallel-writes", "--parallel-writes"],
    ] {
        assert!(command(&args).is_err(), "{args:?}");
    }
}

#[test]
fn edict_plan_and_from_plan_flags_validate_cleanly() {
    match command(&["edict", "--plan", "--queue", "先出改动方案"]).unwrap() {
        Command::Edict {
            template: Some(choice),
            queue: true,
            text,
            ..
        } => {
            assert_eq!(choice.name, ziyuan_governance::PLAN_TEMPLATE);
            assert_eq!(text, "先出改动方案");
        }
        _ => panic!("expected Command::Edict with plan template"),
    }
    assert!(command(&["edict", "--plan", "--tools", "write_file", "x"]).is_err());
    assert!(command(&["edict", "--plan", "--template", "写文档", "x"]).is_err());
    assert!(command(&["edict", "--no-tests", "x"]).is_err());

    match command(&[
        "edict",
        "--from-plan",
        "5",
        "--scope",
        "crates/ziyuan-app",
        "--no-tests",
        "--queue",
        "按方案实施",
    ])
    .unwrap()
    {
        Command::EdictFromPlan {
            plan,
            scope,
            run_tests,
            queue,
            extra,
        } => {
            assert_eq!(plan, TaskId(5));
            assert_eq!(scope, ["crates/ziyuan-app"]);
            assert!(!run_tests);
            assert!(queue);
            assert_eq!(extra, "按方案实施");
        }
        _ => panic!("expected Command::EdictFromPlan"),
    }
    assert!(command(&["edict", "--from-plan", "5", "--dept", "rites"]).is_err());
    assert!(command(&["edict", "--from-plan", "5", "--plan"]).is_err());
}

#[test]
fn edict_revise_flags_validate_cleanly() {
    match command(&[
        "edict",
        "--revise",
        "7",
        "--scope",
        "src",
        "--no-tests",
        "--queue",
        "--note",
        "保留旧签名",
        "并补单测",
    ])
    .unwrap()
    {
        Command::EdictRevise {
            source,
            scope,
            run_tests,
            queue,
            note,
        } => {
            assert_eq!(source, TaskId(7));
            assert_eq!(scope, ["src"]);
            assert_eq!(run_tests, Some(false));
            assert!(queue);
            assert_eq!(note, "保留旧签名\n并补单测");
        }
        _ => panic!("expected Command::EdictRevise"),
    }
    assert!(command(&["edict", "--revise", "7", "--from-plan", "6"]).is_err());
    assert!(command(&["edict", "--revise", "7", "--dept", "rites"]).is_err());
    assert!(command(&["edict", "--revise", "7", "--plan"]).is_err());
    match command(&[
        "edict",
        "--follow-up",
        "5",
        "--dept",
        "rites",
        "--scope",
        "src",
        "--queue",
        "再核查边界条件",
    ])
    .unwrap()
    {
        Command::EdictFollowUp {
            source,
            department,
            scope,
            queue,
            question,
        } => {
            assert_eq!(source, TaskId(5));
            assert_eq!(department, Some(Department::Rites));
            assert_eq!(scope, vec!["src"]);
            assert!(queue);
            assert_eq!(question, "再核查边界条件");
        }
        _ => panic!("expected Command::EdictFollowUp"),
    }
    assert!(command(&["edict", "--follow-up", "5"]).is_err());
    assert!(command(&["edict", "--follow-up", "5", "--tools", "write_file", "查"]).is_err());
    assert!(command(&["edict", "--follow-up", "5", "--revise", "4", "查"]).is_err());
    assert!(command(&["edict", "--note", "只有说明", "普通诏令"]).is_err());
}

#[test]
fn parse_edict_resume_failed_supports_scope_note_and_rejects_conflicts() {
    match command(&[
        "edict",
        "--resume-failed",
        "7",
        "--scope",
        "src",
        "--queue",
        "--note",
        "先单测",
        "继续修复",
    ])
    .unwrap()
    {
        Command::EdictResumeFailed {
            source,
            scope,
            queue,
            note,
        } => {
            assert_eq!(source, TaskId(7));
            assert_eq!(scope, vec!["src"]);
            assert!(queue);
            assert_eq!(note, "先单测\n继续修复");
        }
        _ => panic!("expected Command::EdictResumeFailed"),
    }
    assert!(command(&["edict", "--resume-failed", "7", "--tools", "write_file"]).is_err());
    assert!(command(&["edict", "--resume-failed", "7", "--follow-up", "2", "查"]).is_err());
}

#[test]
fn revert_command_parses_id_and_dry_run() {
    assert!(matches!(
        command(&["revert", "7"]).unwrap(),
        Command::Revert {
            task: TaskId(7),
            dry_run: false,
        }
    ));
    assert!(matches!(
        command(&["revert", "7", "--dry-run"]).unwrap(),
        Command::Revert {
            task: TaskId(7),
            dry_run: true,
        }
    ));
    assert!(matches!(
        command(&["revert", "--dry-run", "7"]).unwrap(),
        Command::Revert {
            task: TaskId(7),
            dry_run: true,
        }
    ));
    assert!(command(&["revert"]).is_err());
    assert!(command(&["revert", "abc"]).is_err());
    assert!(command(&["revert", "7", "--force"]).is_err());
}

#[test]
fn budget_export_parses_csv_json_and_bom_flags() {
    let cmd = parse(vec![
        "budget".into(),
        "--export".into(),
        "report.csv".into(),
        "--bom".into(),
    ])
    .unwrap();
    match cmd {
        Command::BudgetExport {
            output,
            format,
            bom,
        } => {
            assert_eq!(output, std::path::PathBuf::from("report.csv"));
            assert_eq!(format, BudgetExportFormat::Csv);
            assert!(bom);
        }
        _ => panic!("应解析为 BudgetExport"),
    }
    let inferred = parse(vec![
        "budget".into(),
        "--export".into(),
        "out/duzhi.json".into(),
    ])
    .unwrap();
    assert!(matches!(
        inferred,
        Command::BudgetExport {
            format: BudgetExportFormat::Json,
            bom: false,
            ..
        }
    ));
    assert!(parse(vec!["budget".into(), "--format".into(), "csv".into()]).is_err());
    assert!(
        parse(vec![
            "budget".into(),
            "--export".into(),
            "a.csv".into(),
            "--format".into(),
            "yaml".into()
        ])
        .is_err()
    );
}