orch-cli 0.119.0

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
Documentation
use super::*;
use std::fs as std_fs;
use std::sync::Mutex;

/// Answers from a queue and records each ETag it was sent
struct Fake {
    answers: Mutex<Vec<FileFetch>>,
    sent: Mutex<Vec<Option<String>>>,
}

impl Fake {
    fn new(mut answers: Vec<FileFetch>) -> Self {
        answers.reverse();
        Self {
            answers: Mutex::new(answers),
            sent: Mutex::new(Vec::new()),
        }
    }
}

#[async_trait]
impl Contents for Fake {
    async fn orch_yml(&self, _repo: &str, etag: Option<&str>) -> anyhow::Result<FileFetch> {
        self.sent.lock().unwrap().push(etag.map(str::to_string));
        Ok(self
            .answers
            .lock()
            .unwrap()
            .pop()
            .expect("an answer for each call"))
    }
}

fn found(content: &str, etag: &str) -> FileFetch {
    FileFetch::Found {
        content: content.into(),
        etag: Some(etag.into()),
    }
}

#[tokio::test]
async fn a_304_keeps_the_cached_config_and_costs_one_conditional_request() {
    let dir = tempfile::tempdir().unwrap();
    let fake = Fake::new(vec![
        found("workflow:\n  max_attempts: 3\n", "\"e1\""),
        FileFetch::NotModified,
    ]);

    assert!(refresh(&fake, dir.path(), "o/r").await.unwrap());
    assert!(!refresh(&fake, dir.path(), "o/r").await.unwrap());

    let file = dir.path().join(CONFIG_FILE);
    assert_eq!(
        std_fs::read_to_string(&file).unwrap(),
        "workflow:\n  max_attempts: 3\n"
    );
    assert_eq!(
        config::get_for_project(dir.path(), "workflow.max_attempts").unwrap(),
        "3"
    );
    assert_eq!(
        *fake.sent.lock().unwrap(),
        vec![None, Some("\"e1\"".to_string())]
    );
}

#[tokio::test]
async fn a_changed_file_replaces_the_cached_one() {
    let dir = tempfile::tempdir().unwrap();
    let fake = Fake::new(vec![
        found("workflow:\n  max_attempts: 3\n", "\"e1\""),
        found("workflow:\n  max_attempts: 7\n", "\"e2\""),
    ]);

    refresh(&fake, dir.path(), "o/r").await.unwrap();
    refresh(&fake, dir.path(), "o/r").await.unwrap();

    assert_eq!(
        config::get_for_project(dir.path(), "workflow.max_attempts").unwrap(),
        "7"
    );
    assert_eq!(
        std_fs::read_to_string(dir.path().join(ETAG_FILE)).unwrap(),
        "\"e2\""
    );
}

#[tokio::test]
async fn an_invalid_file_keeps_the_last_good_one_and_a_missing_one_leaves_none() {
    let dir = tempfile::tempdir().unwrap();
    let fake = Fake::new(vec![
        found("gh:\n  repo: o/r\n", "\"e1\""),
        found("gh: [unclosed\n", "\"e2\""),
        FileFetch::Missing,
    ]);
    let file = dir.path().join(CONFIG_FILE);

    refresh(&fake, dir.path(), "o/r").await.unwrap();
    assert!(!refresh(&fake, dir.path(), "o/r").await.unwrap());
    assert_eq!(std_fs::read_to_string(&file).unwrap(), "gh:\n  repo: o/r\n");

    assert!(refresh(&fake, dir.path(), "o/r").await.unwrap());
    assert_eq!(std_fs::read_to_string(&file).unwrap(), "{}\n");
    assert!(!dir.path().join(ETAG_FILE).exists());
}

#[tokio::test]
async fn an_etag_without_its_file_is_not_sent() {
    let dir = tempfile::tempdir().unwrap();
    std_fs::write(dir.path().join(ETAG_FILE), "\"stale\"").unwrap();
    let fake = Fake::new(vec![found("{}\n", "\"e1\"")]);

    refresh(&fake, dir.path(), "o/r").await.unwrap();
    assert_eq!(*fake.sent.lock().unwrap(), vec![None]);
}

#[tokio::test]
async fn one_machine_lists_no_project_to_read() {
    let store = TaskStore::open_memory().await.unwrap();
    // A registry row left from a cluster changes nothing without one
    store
        .kv_set(
            "node:b",
            r#"{"node":"b","projects":["o/r"],"agents":["claude"],"max_parallel":1,"seen":"2099-01-01T00:00:00Z"}"#,
        )
        .await
        .unwrap();
    assert!(remote_projects(&store, &HashSet::new()).await.is_empty());
}

#[test]
fn a_slug_with_path_tricks_has_no_config_dir() {
    for bad in ["../x", "o/..", "o/r/x", "o", "/r", "o/r name", ""] {
        assert!(!valid_slug(bad), "{bad}");
    }
    for good in ["o/r", "my-org/my_repo.rs"] {
        assert!(valid_slug(good), "{good}");
    }
}