//! Static checks that preserve connector–factory discipline (see `ARCHITECTURE.md`
//! at the repository root). Failing tests here usually mean a new code path
//! bypassed `run_pipeline` or duplication crept back into delegation SQL.

use std::path::PathBuf;

fn repo_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..")
}

fn agent_connector_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/api/routes/agent")
}

#[test]
fn thin_agent_connectors_invoke_run_pipeline() {
    for file in [
        "handlers.rs",
        "streaming.rs",
        "channel_message.rs",
        "scheduled_tasks.rs",
    ] {
        let path = agent_connector_dir().join(file);
        let src = std::fs::read_to_string(&path)
            .unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
        assert!(
            src.contains("run_pipeline"),
            "{} must call run_pipeline — connectors stay thin (ARCHITECTURE.md §1)",
            path.display()
        );
    }
}

#[test]
fn architecture_md_documents_off_pipeline_exemption() {
    let arch = repo_root().join("ARCHITECTURE.md");
    let text =
        std::fs::read_to_string(&arch).unwrap_or_else(|e| panic!("read {}: {e}", arch.display()));
    assert!(
        text.contains("Off-pipeline") && text.contains("/api/interview"),
        "ARCHITECTURE.md must document off-pipeline exemptions and the interview routes"
    );
}

#[test]
fn retire_unused_subagents_uses_shared_disable_primitive() {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/api/routes/subagents.rs");
    let src =
        std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
    assert!(
        src.contains("disable_subagents_by_name"),
        "retirement must call roboticus_db::agents::disable_subagents_by_name so soft-disable stays one code path"
    );
}

#[test]
fn db_agents_delegation_sql_uses_core_helper() {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../roboticus-db/src/agents.rs");
    let src =
        std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
    assert!(
        src.contains("delegation_output_tool_name_sql_in_predicate"),
        "roboticus-db agents.rs must build delegation SQL from roboticus_core::delegation_tools"
    );
}
