trusty-mpm 0.19.4

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
Documentation
//! Unit tests for the `session` command helpers.
//!
//! Why: extracted from `session.rs` to keep that production file under the
//! 500-SLOC cap while retaining full test coverage for the private helpers.
//! What: tests for `derive_source_id_from_path` and `matches_session`.
//! Test: this file (budget: 1500 SLOC as a `_tests.rs` file).

use super::{derive_source_id_from_path, matches_session};

#[test]
fn derive_source_id_from_cwd_returns_none_without_git() {
    // A fresh TempDir has no git repo, so get_origin_url will return None
    // and derive_source_id_from_path must return None (not panic).
    let dir = tempfile::TempDir::new().expect("tempdir");
    assert!(
        derive_source_id_from_path(dir.path()).is_none(),
        "non-git directory must yield None"
    );
}

#[test]
fn ls_source_id_filter_selects_correct_slug() {
    // When source_id = Some("owner/repo") and current = false, the slug
    // comes straight from the --source-id flag (no git lookup needed).
    // We verify the slug is correctly passed by asserting it would be used
    // as-is in the query param (no transformation needed for a valid slug).
    let slug = "myorg/myrepo";
    // A valid slug contains exactly one '/' and no whitespace.
    assert!(slug.contains('/'), "slug must be owner/repo form");
    assert_eq!(
        slug.split('/').count(),
        2,
        "slug must have exactly two parts"
    );
    // derive_source_id_from_path returns the same owner/repo format.
    // This test guards the format invariant that the daemon expects.
    let (owner, repo) = slug.split_once('/').unwrap();
    assert!(!owner.is_empty());
    assert!(!repo.is_empty());
}

#[test]
fn info_managed_fallback_matches_by_id_and_name() {
    let session = serde_json::json!({
        "id": "abc-1234-uuid",
        "name": "tmpm-red-owl",
        "state": "active"
    });
    assert!(
        matches_session(&session, "abc-1234-uuid"),
        "must match by exact id"
    );
    assert!(
        matches_session(&session, "tmpm-red-owl"),
        "must match by exact name"
    );
    assert!(
        !matches_session(&session, "tmpm-blue-fox"),
        "must not match a different name"
    );
    assert!(
        !matches_session(&session, "xyz-9999"),
        "must not match a different id"
    );
    // Partial matches are not accepted — the lookup is exact.
    assert!(
        !matches_session(&session, "abc-1234"),
        "partial id must not match"
    );
}