trusty-mpm 1.8.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
//! Tests for [`super::refresh_resume_compiled_prompt`] (#4752).
//!
//! Why: the resume path never runs `prepare_session*`, so this helper is the
//! ONLY thing standing between a resumed session and a stale (or absent)
//! compiled prompt. It is also fatal, so its failure message is operator-facing
//! and must be assertable.
//! What: covers the success write and the actionable-failure message.
//! Test: this file IS the test suite.

use super::*;

/// A fixed managed id for the resume-refresh tests.
///
/// Why (#4832): the compiled prompt is now per-session, so the test must name
/// the same session the call under test is given — otherwise it would assert
/// against a path nothing wrote.
fn test_session_id() -> ManagedSessionId {
    ManagedSessionId::new()
}

#[test]
fn refresh_resume_compiled_prompt_writes_the_project_local_file() {
    // Why (#4752 review, HIGH 3): before this helper existed, resume ran a
    // prompt that never reached the compiled path. FIXTURE: pre-seed a stale
    // sentinel so "the file exists" cannot pass — only a real refresh can.
    let tmp = tempfile::tempdir().expect("tempdir");
    let id = test_session_id();
    let dest = crate::core::instruction_pipeline::compiled_prompt_path(tmp.path(), &id.to_string());
    std::fs::create_dir_all(dest.parent().unwrap()).expect("create parent");
    const STALE: &str = "STALE-FROM-A-PREVIOUS-START";
    std::fs::write(&dest, STALE).expect("seed stale");

    // #7514: root-taking seam, so the fold lands under `root` and not in the
    // operator's own `~/.trusty-mpm/usage/`.
    let root = tempfile::tempdir().expect("framework root");
    refresh_resume_compiled_prompt_in(root.path(), tmp.path(), &id).expect("refresh succeeds");

    let on_disk = std::fs::read_to_string(&dest).expect("readable");
    assert_ne!(
        on_disk, STALE,
        "resume must overwrite a stale compiled prompt"
    );
    assert!(
        on_disk.contains("# PM Agent -- Trusty MPM"),
        "must hold the resolved PM prompt, not a fragment: {on_disk}"
    );
    // Project-local, never global.
    assert!(dest.starts_with(tmp.path()));
}

#[test]
fn refresh_resume_compiled_prompt_reports_an_actionable_failure() {
    // Why (#4752 ruling follow-up 1): the write is fatal, so a refused resume
    // must name the path and say the session did not start — not surface a bare
    // io error. FIXTURE: a directory planted at the exact path so only this
    // write fails.
    let tmp = tempfile::tempdir().expect("tempdir");
    let id = test_session_id();
    let dest = crate::core::instruction_pipeline::compiled_prompt_path(tmp.path(), &id.to_string());
    std::fs::create_dir_all(&dest).expect("plant a directory at the compiled path");

    // #7514: root-taking seam — see the sibling test above.
    let root = tempfile::tempdir().expect("framework root");
    let msg = refresh_resume_compiled_prompt_in(root.path(), tmp.path(), &id)
        .expect_err("a failed compiled write must refuse the resume");
    assert!(
        msg.contains(&dest.display().to_string()),
        "must name the path: {msg}"
    );
    assert!(
        msg.contains("was NOT started"),
        "must say it did not start: {msg}"
    );
    assert!(msg.contains("permissions"), "must point at a remedy: {msg}");
}