rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use std::{path::PathBuf, sync::Arc, time::Duration};

use pretty_assertions::assert_eq;
use tempfile::TempDir;
use tokio::{sync::watch, time::timeout};

use super::JobFeed;
use crate::app::{
    daemon_host::{
        jobs::{JobId, JobRecord, JobRegistry, JobStore},
        sessions::Sessions,
    },
    pull_requests::ScriptedForge,
};

/// Failure bound for each change a test waits for; the test synchronizes
/// on the changes themselves.
const CHANGE_DEADLINE: Duration = Duration::from_secs(10);

fn ids(receiver: &watch::Receiver<Arc<rho_protocol::JobListResponse>>) -> Vec<String> {
    let mut ids: Vec<_> = receiver
        .borrow()
        .jobs
        .iter()
        .map(|job| job.id.to_string())
        .collect();
    ids.sort();
    ids
}

async fn add_session_job(registry: &JobRegistry) -> String {
    let id = JobId::generate();
    let record = JobRecord::new_session(
        id.clone(),
        PathBuf::from("/work"),
        rho_sdk::SessionId::new(),
    );
    registry.register(record).unwrap();
    id.to_string()
}

// Covers: a followed list publishes a job the registry adds with no turn
// under way, so no tick, and a follower that subscribes after the last
// one left gets a feed that still moves.
// Owner: daemon job feed.
#[tokio::test]
async fn a_followed_list_moves_with_the_registry_across_followers() {
    let home = TempDir::new().unwrap();
    let registry = Arc::new(JobRegistry::open(JobStore::open(home.path()).unwrap()));
    let sessions = Sessions::new(Arc::clone(&registry), home.path(), ScriptedForge::new());
    let feed = JobFeed::new(Arc::clone(&registry), sessions);

    let mut first = feed.subscribe();
    assert_eq!(ids(&first), Vec::<String>::new());
    let added = add_session_job(&registry).await;
    timeout(CHANGE_DEADLINE, first.changed())
        .await
        .unwrap()
        .unwrap();
    assert_eq!(ids(&first), vec![added.clone()]);
    drop(first);

    let mut second = feed.subscribe();
    second.borrow_and_update();
    let later = add_session_job(&registry).await;
    timeout(CHANGE_DEADLINE, second.changed())
        .await
        .unwrap()
        .unwrap();
    let mut expected = vec![added, later];
    expected.sort();
    assert_eq!(ids(&second), expected);
}