opencrabs 0.5.11

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Recommended: the 40MB prebuilt binary for macOS, Linux and Windows: https://github.com/opencrabs/opencrabs/releases
//! A final answer that follows tool groups must be a row of its own, after
//! those groups. Overwriting an earlier text row moves it above the tools,
//! where the fold hides it.

use crate::tui::app::answer_placement::last_text_precedes_tool_group;
use crate::tui::app::{DisplayMessage, ToolCallEntry, ToolCallGroup};
use uuid::Uuid;

fn msg(role: &str, content: &str) -> DisplayMessage {
    DisplayMessage {
        id: Uuid::new_v4(),
        role: role.to_string(),
        content: content.to_string(),
        timestamp: chrono::Utc::now(),
        token_count: None,
        cost: None,
        approval: None,
        approve_menu: None,
        details: None,
        expanded: false,
        expanded_full: false,
        tool_group: None,
        duration_secs: None,
    }
}

fn group_msg() -> DisplayMessage {
    let mut m = msg("tool_group", "1 tool call");
    m.tool_group = Some(ToolCallGroup {
        calls: vec![ToolCallEntry {
            description: "suggest options".to_string(),
            success: true,
            details: None,
            completed: true,
            tool_input: serde_json::Value::Null,
        }],
        expanded: false,
    });
    m
}

#[test]
fn text_then_tool_group_is_stale_so_answer_must_append() {
    // Earlier text, then the options tool ran with no text row after it.
    let msgs = vec![
        msg("user", "hi"),
        msg("assistant", "Let me check"),
        group_msg(),
    ];
    assert!(last_text_precedes_tool_group(&msgs));
}

#[test]
fn text_after_the_last_tool_group_may_be_overwritten() {
    let msgs = vec![
        msg("user", "hi"),
        group_msg(),
        msg("assistant", "Here is the answer"),
    ];
    assert!(!last_text_precedes_tool_group(&msgs));
}

#[test]
fn no_tool_group_is_never_stale() {
    let msgs = vec![msg("user", "hi"), msg("assistant", "Hello")];
    assert!(!last_text_precedes_tool_group(&msgs));
}

#[test]
fn no_assistant_row_is_never_stale() {
    let msgs = vec![msg("user", "hi"), group_msg()];
    assert!(!last_text_precedes_tool_group(&msgs));
}