artifactize-core 0.27.1

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
use super::*;
use std::fs;

fn read(text: &str) -> Result<Limits, String> {
    let directory = crate::test_os::tempdir();
    fs::write(directory.path().join(FILE), text).unwrap();
    Limits::read(directory.path())
}

#[test]
fn agent_sessions_default_and_validate() {
    assert_eq!(
        read("{}").unwrap().agent_sessions(),
        AgentSessions {
            enabled: true,
            max_bytes: 1_073_741_824,
            target_bytes: 805_306_368,
        }
    );
    assert_eq!(
        read(r#"{"agentSessions":{"enabled":false}}"#)
            .unwrap()
            .agent_sessions(),
        AgentSessions {
            enabled: false,
            max_bytes: 1_073_741_824,
            target_bytes: 805_306_368,
        }
    );
    assert_eq!(
        read(r#"{"agentSessions":{"maxBytes":4000,"targetBytes":1000}}"#)
            .unwrap()
            .agent_sessions(),
        AgentSessions {
            enabled: true,
            max_bytes: 4000,
            target_bytes: 1000
        }
    );
    // A small maxBytes alone collects down to three quarters of it.
    assert_eq!(
        read(r#"{"agentSessions":{"maxBytes":4000}}"#)
            .unwrap()
            .agent_sessions()
            .target_bytes,
        3000
    );
    // Quarter-block rounding stays divide-then-multiply, not floor(3*n/4).
    for (max_bytes, target_bytes) in [(1, 0), (3, 0), (5, 3), (7, 3), (8, 6), (11, 6)] {
        assert_eq!(
            read(&format!(
                r#"{{"agentSessions":{{"maxBytes":{max_bytes}}}}}"#
            ))
            .unwrap()
            .agent_sessions()
            .target_bytes,
            target_bytes
        );
    }
    assert_eq!(
        read(r#"{"agentSessions":{"maxBytes":18446744073709551615}}"#)
            .unwrap()
            .agent_sessions()
            .target_bytes,
        805306368
    );
    for (text, error) in [
        (
            r#"{"agentSessions":{"maxBytes":100,"targetBytes":100}}"#,
            "agentSessions.targetBytes (100) must be lower than maxBytes (100).",
        ),
        (
            r#"{"agentSessions":{"targetBytes":2147483648}}"#,
            "must be lower than maxBytes (1073741824)",
        ),
        (
            r#"{"agentSessions":{"maxBytes":0}}"#,
            "agentSessions.maxBytes must be at least 1.",
        ),
        (r#"{"agentSessions":{"maxbytes":10}}"#, "unknown field"),
        (r#"{"agentSessions":{"maxBytes":-1}}"#, "invalid value"),
        (r#"{"agentSessions":{"enabled":"no"}}"#, "invalid type"),
        (r#"{"agentSessions":true}"#, "invalid type"),
    ] {
        let message = read(text).unwrap_err();
        assert!(message.contains(error), "{text}: {message}");
        assert!(message.contains(FILE), "{message}");
    }
}

#[test]
fn signoff_idle_grace_defaults_validates_and_saves_beside_other_limits() {
    assert_eq!(
        read("{}").unwrap().signoff_idle_grace(),
        Duration::from_secs(120)
    );
    assert_eq!(
        read(r#"{"signoffIdleGraceSeconds":0}"#)
            .unwrap()
            .signoff_idle_grace(),
        Duration::ZERO
    );
    for (text, error) in [
        (
            r#"{"signoffIdleGraceSeconds":86401}"#,
            "must be at most 86400",
        ),
        (r#"{"signoffIdleGraceSeconds":-1}"#, "invalid value"),
        (r#"{"signoffIdleGraceSeconds":"2m"}"#, "invalid type"),
    ] {
        let message = read(text).unwrap_err();
        assert!(message.contains(error), "{text}: {message}");
    }

    let directory = crate::test_os::tempdir();
    let state = directory.path().join("state");
    let saved = Limits::set_signoff_idle_grace(&state, Duration::from_secs(300)).unwrap();
    assert_eq!(saved.signoff_idle_grace(), Duration::from_secs(300));
    fs::write(
        state.join(FILE),
        r#"{"backends":{"codex":4},"agentSessions":{"maxBytes":4000,"enabled":null}}"#,
    )
    .unwrap();
    let saved = Limits::set_signoff_idle_grace(&state, Duration::ZERO).unwrap();
    assert_eq!(saved.signoff_idle_grace(), Duration::ZERO);
    assert_eq!(saved.limit(&crate::config::Provider::Codex.into()), Some(4));
    assert_eq!(saved.agent_sessions().max_bytes, 4000);
    assert_eq!(saved.agent_sessions().target_bytes, 3000);
    assert_eq!(Limits::read(&state).unwrap(), saved);
    let declared: serde_json::Value =
        serde_json::from_slice(&fs::read(state.join(FILE)).unwrap()).unwrap();
    assert_eq!(
        declared,
        serde_json::json!({
            "backends": {"codex": 4}, "agentSessions": {"maxBytes": 4000, "enabled": null},
            "signoffIdleGraceSeconds": 0,
        })
    );
    assert!(
        Limits::set_signoff_idle_grace(&state, Duration::from_secs(86401))
            .unwrap_err()
            .contains("at most 86400")
    );
    for invalid in [r#"{"unknown":1}"#, r#"{"signoffIdleGraceSeconds":86401}"#] {
        fs::write(state.join(FILE), invalid).unwrap();
        assert!(Limits::set_signoff_idle_grace(&state, Duration::ZERO).is_err());
        assert_eq!(fs::read_to_string(state.join(FILE)).unwrap(), invalid);
    }
}