artifactize-core 0.27.0

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

use serde_json::json;

use super::*;
use crate::{config::read_workspace_config, graph::Graph};

/// Public selection-file contracts, independent of the parser's implementation constants.
const FILE_BYTES: usize = 4 * 1024 * 1024;
/// Duplicate input IDs still count against the declared 100000-ID input bound.
const FILE_IDS: usize = 100_000;
const INVALID_FILE: &str =
    "Selection file must contain 1–100000 nonempty IDs, as a JSON string array or one ID per line.";

fn fixture() -> (tempfile::TempDir, RepoConfig) {
    let directory = crate::test_os::tempdir();
    for (name, instruction) in [
        ("blocked", "Check {red}."),
        ("cycle-a", "Check {cycle-b}."),
        ("cycle-b", "Check {cycle-a}."),
        ("green", "Check {input}."),
        ("red", "Check."),
        ("timeout", "Check."),
        ("waiting", "Check {timeout}."),
    ] {
        let path = directory.path().join(name);
        fs::create_dir(&path).unwrap();
        crate::test_declaration::write(
            directory.path(),
            path.join("index.artf"),
            json!({"name":name,"evals":[{"id":"check","title":"Check",
                "profile":{"kind":"human"},"payload":{"instruction":instruction}}]})
            .to_string(),
        )
        .unwrap();
    }
    fs::create_dir(directory.path().join("input")).unwrap();
    crate::test_declaration::write(
        directory.path(),
        directory.path().join("input/index.artf"),
        json!({"name":"input","basis":true}).to_string(),
    )
    .unwrap();
    let config = read_workspace_config(directory.path()).unwrap();
    (directory, config)
}

fn eval_ids(selection: &Selection, config: &RepoConfig) -> Vec<String> {
    selection
        .resolve(config)
        .unwrap()
        .evals
        .iter()
        .map(|eval| eval.id.to_string())
        .collect()
}

#[test]
fn ordered_union_keeps_first_occurrence_and_rejects_unknown_or_empty_roots() {
    let (_directory, config) = fixture();
    let artifacts = Selection::Artifacts {
        artifact_ids: vec![
            "green".parse().unwrap(),
            "cycle-b".parse().unwrap(),
            "green".parse().unwrap(),
            "cycle-a".parse().unwrap(),
        ],
    };
    let evals = Selection::Evals {
        eval_ids: vec![
            "green/check".parse().unwrap(),
            "cycle-b/check".parse().unwrap(),
            "green/check".parse().unwrap(),
            "cycle-a/check".parse().unwrap(),
        ],
    };
    let expected = ["green/check", "cycle-b/check", "cycle-a/check"];
    assert_eq!(eval_ids(&artifacts, &config), expected);
    assert_eq!(eval_ids(&evals, &config), expected);
    assert_eq!(
        artifacts.resolve(&config).unwrap().roots,
        ["green", "cycle-b", "cycle-a"]
    );
    assert_eq!(
        eval_ids(&Selection::All, &config),
        [
            "blocked/check",
            "cycle-a/check",
            "cycle-b/check",
            "green/check",
            "red/check",
            "timeout/check",
            "waiting/check",
        ]
    );
    let selected = Selection::Eval {
        eval_id: "cycle-a/check".parse().unwrap(),
    }
    .resolve(&config)
    .unwrap();
    assert_eq!(selected.evals.len(), 1);
    assert_eq!(
        Graph::new(&config)
            .unwrap()
            .dependency_closure(&selected.roots)
            .unwrap(),
        ["cycle-a", "cycle-b"]
    );
    assert!(
        eval_ids(
            &Selection::Artifact {
                artifact_id: "input".parse().unwrap()
            },
            &config
        )
        .is_empty()
    );
    for value in [
        json!({"kind":"eval","evalId":"green"}),
        json!({"kind":"artifacts","artifactIds":["green", ""]}),
        json!({"kind":"evals","evalIds":["green/check", "missing"]}),
    ] {
        assert!(serde_json::from_value::<Selection>(value).is_err());
    }
    for selection in [
        Selection::Artifact {
            artifact_id: "missing".parse().unwrap(),
        },
        Selection::Eval {
            eval_id: "green/missing".parse().unwrap(),
        },
        Selection::Artifacts {
            artifact_ids: vec!["green".parse().unwrap(), "missing".parse().unwrap()],
        },
        Selection::Evals {
            eval_ids: vec![
                "green/check".parse().unwrap(),
                "missing/check".parse().unwrap(),
            ],
        },
        Selection::Artifacts {
            artifact_ids: vec![],
        },
        Selection::Evals { eval_ids: vec![] },
    ] {
        assert!(selection.resolve(&config).is_err(), "{selection:?}");
    }
}

#[test]
fn json_and_line_files_select_the_same_ordered_evals() {
    let directory = crate::test_os::tempdir();
    let json_path = directory.path().join("selection.json");
    let line_path = directory.path().join("selection.txt");
    fs::write(
        crate::test_declaration::source_path(&json_path),
        "\u{feff}[\"green\",\"cycle-b\",\"green\",\"cycle-a\"]",
    )
    .unwrap();
    fs::write(
        crate::test_declaration::source_path(&line_path),
        "\u{feff} green \r\n\r\n cycle-b\r\ngreen\ncycle-a \n",
    )
    .unwrap();
    let json = read_selection_file(&json_path).unwrap();
    let lines = read_selection_file(&line_path).unwrap();
    assert_eq!(json, ["green", "cycle-b", "cycle-a"]);
    assert_eq!(json, lines);
    let (_directory, config) = fixture();
    assert_eq!(
        eval_ids(
            &Selection::Artifacts {
                artifact_ids: json.iter().map(|id| id.parse().unwrap()).collect()
            },
            &config
        ),
        eval_ids(
            &Selection::Artifacts {
                artifact_ids: lines.iter().map(|id| id.parse().unwrap()).collect()
            },
            &config
        )
    );
}

#[test]
fn selection_files_validate_before_deduplication_and_never_fallback_from_json() {
    for malformed in [
        "[broken",
        "[1,",
        "[null,]",
        "[{}] trailing",
        "[null,1e400]",
        "[null,{\"nested\":[1e400]}]",
    ] {
        assert_eq!(
            parse_selection_file(malformed).unwrap_err(),
            "Selection file contains invalid JSON."
        );
    }
    assert_eq!(
        parse_selection_file("\u{feff}[\"x\",\"y\",\"x\"]\r\n").unwrap(),
        ["x", "y"]
    );
    assert_eq!(
        parse_selection_file("\u{feff} x\r\n y\r\n").unwrap(),
        ["x", "y"]
    );
    // Non-array-looking input remains the line-file form, not JSON fallback.
    assert_eq!(parse_selection_file("null\r\n17").unwrap(), ["null", "17"]);
    assert_eq!(
        parse_selection_file("[broken").unwrap_err(),
        "Selection file contains invalid JSON."
    );
    for input in [
        "",
        " \r\n ",
        "[]",
        "[null]",
        "[1]",
        "[18446744073709551616]",
        "[{}]",
        "[\"\"]",
        "[\" x\"]",
        "[\"x \"]",
        "x,y",
        "x y",
        "a\tb",
        "a\0b",
        "a\u{7f}b",
        "[\"x\\ny\"]",
        "[\"\u{feff}x\"]",
    ] {
        assert_eq!(
            parse_selection_file(input).unwrap_err(),
            INVALID_FILE,
            "{input:?}"
        );
    }
    assert_eq!(
        parse_selection_file("x\n".repeat(FILE_IDS).as_str()).unwrap(),
        ["x"]
    );
    assert_eq!(
        parse_selection_file("x\n".repeat(FILE_IDS + 1).as_str()).unwrap_err(),
        INVALID_FILE
    );
    let ids = vec!["x"; FILE_IDS + 1];
    assert_eq!(
        parse_selection_file(&serde_json::to_string(&ids).unwrap()).unwrap_err(),
        INVALID_FILE
    );
    let text = "x".repeat(FILE_BYTES);
    assert_eq!(parse_selection_file(&text).unwrap()[0].len(), FILE_BYTES);
    assert_eq!(
        parse_selection_file(&(text + "x")).unwrap_err(),
        "Selection file exceeds 4 MiB."
    );
}

#[test]
fn file_reads_reject_nonregular_and_oversized_inputs() {
    let directory = crate::test_os::tempdir();
    let path = directory.path().join("ids");
    let file = fs::File::create(&path).unwrap();
    file.set_len(FILE_BYTES as u64 + 1).unwrap();
    let error = "Selection input must be a regular file no larger than 4 MiB.";
    assert_eq!(read_selection_file(&path).unwrap_err(), error);
    assert_eq!(read_selection_file(directory.path()).unwrap_err(), error);
    // The null device is no regular file on any system.
    assert!(read_selection_file(crate::test_os::null_device()).is_err());
    let fifo = directory.path().join("fifo");
    if crate::test_os::fifo_if_supported(&fifo).is_some() {
        assert_eq!(read_selection_file(&fifo).unwrap_err(), error);
    }
    assert!(read_selection_file(&directory.path().join("missing")).is_err());
}