Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

5.2 KB
Raw Blame
use super::*;
use crate::domain::model::{
    AgentRequestId, Author, PermissionOutcome, ToolDetail, ToolName, ToolStatus, ToolUseId, TurnId,
    UserToolOutcome,
};
use non_empty::NonEmpty;
use serde_json::json;

#[test]
fn domain_parts_serialize_directly_into_the_browser_contract() {
    let message = ModelFoldedMessage {
        id: TurnId(3),
        author: Author::Agent,
        request_id: None,
        parts: NonEmpty::one(MessagePart::ToolUse {
            id: ToolUseId("tool-1".to_owned()),
            name: ToolName::native("Bash"),
            status: ToolStatus::Running,
            detail: ToolDetail::Terminal {
                command: Some("ls".to_owned()),
                output: None,
                exit_code: None,
            },
        }),
        stop: None,
        pending: false,
        posted_at: None,
    };
    let session = AgentSessionId::new_from_uuid(macro_uuid::Uuid::from_u128(7));

    assert_eq!(
        serde_json::to_value(FoldedMessage::new(session, message)).unwrap(),
        json!({
            "agentSessionId": session.to_string(),
            "key": "3:agent",
            "turn": 3,
            "author": { "kind": "agent" },
            "requestId": null,
            "parts": [{
                "kind": "tool_use",
                "id": "tool-1",
                "name": { "kind": "native", "name": "Bash" },
                "status": "running",
                "detail": {
                    "kind": "terminal",
                    "command": "ls",
                    "output": null,
                    "exitCode": null
                }
            }],
            "stop": null,
            "pending": false,
            "segments": [{
                "index": 0,
                "kind": "activity",
                "start": 0,
                "end": 1,
                "sealed": false,
                "rows": [{
                    "id": "tool-1",
                    "label": "Running",
                    "detail": "ls",
                    "status": "running",
                    "card": null
                }]
            }],
            "phase": "working",
            "postedAt": null
        })
    );

    assert_eq!(
        serde_json::to_value(ToolDetail::UserTool {
            input: json!({ "subject": "Hi" }),
            outcome: UserToolOutcome::Draft {
                draft_id: "d1".to_owned(),
                thread_id: None,
            },
        })
        .unwrap(),
        json!({
            "kind": "user_tool",
            "input": { "subject": "Hi" },
            "outcome": { "kind": "draft", "draftId": "d1", "threadId": null }
        })
    );
    assert_eq!(
        serde_json::to_value(ToolDetail::Macro {
            input: json!({ "documentId": "x" }),
            output: None,
            error: None,
        })
        .unwrap(),
        json!({
            "kind": "macro",
            "input": { "documentId": "x" },
            "output": null,
            "error": null
        })
    );

    assert_eq!(
        serde_json::to_value(MessagePart::Permission {
            request_id: AgentRequestId::Number(7),
            tool_call: ToolUseId("tool-1".to_owned()),
            options: Vec::new(),
            outcome: PermissionOutcome::Pending,
        })
        .unwrap(),
        json!({
            "kind": "permission",
            "requestId": 7,
            "toolCall": "tool-1",
            "options": [],
            "outcome": { "kind": "pending" }
        })
    );
    assert_eq!(
        serde_json::to_value(AgentRequestId::Str("req-1".to_owned())).unwrap(),
        json!("req-1")
    );
}

#[test]
fn empty_replacement_is_an_explicit_browser_event() {
    let event = FoldEvent::MessagesReplaced(std::borrow::Cow::Owned(vec![])).into_owned();
    assert!(event.message().is_none());
    let session = AgentSessionId::new_from_uuid(macro_uuid::Uuid::from_u128(7));
    assert_eq!(
        serde_json::to_value(FoldedStreamEvent::new(session, event)).unwrap(),
        json!({"kind":"replace", "messages":[]})
    );
}

#[test]
fn a_prompt_carries_when_it_was_posted() {
    let message = ModelFoldedMessage {
        id: TurnId(0),
        author: Author::User { user_id: None },
        request_id: None,
        parts: NonEmpty::one(MessagePart::Text {
            text: "tighten the fold".to_owned(),
        }),
        stop: None,
        pending: false,
        posted_at: Some("2026-10-15T14:02:00Z".parse().unwrap()),
    };
    let session = AgentSessionId::new_from_uuid(macro_uuid::Uuid::from_u128(7));

    assert_eq!(
        serde_json::to_value(FoldedMessage::new(session, message)).unwrap()["postedAt"],
        json!("2026-10-15T14:02:00+00:00")
    );
}

#[test]
fn a_message_key_names_its_turn_and_side() {
    let message = ModelFoldedMessage {
        id: TurnId(12),
        author: Author::User { user_id: None },
        request_id: None,
        parts: NonEmpty::one(MessagePart::Text {
            text: "again".to_owned(),
        }),
        stop: None,
        pending: true,
        posted_at: None,
    };
    let session = AgentSessionId::new_from_uuid(macro_uuid::Uuid::from_u128(7));

    assert_eq!(
        serde_json::to_value(FoldedMessage::new(session, message)).unwrap()["key"],
        json!("12:user")
    );
}