trusty-mpm 1.0.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Unit tests for the Slack `mrkdwn` formatter.
//!
//! Why: rendering is pure (no network, no Slack runtime), so every
//! [`CommandResult`] variant's `mrkdwn` output can be asserted directly — this
//! is the adapter's presentation contract.
//! What: one test per representative variant, plus the small render helpers.
//! Test: this IS the test module.

use super::*;
use crate::client::{
    CommandResult, DecisionCounts, DiscoveredProjectSummary, HealthReport, ManagedSessionView,
    ProjectFleetView, SessionSummary, TmuxSessionSummary,
};
use crate::core::doctor::{CheckStatus, DoctorCheck, DoctorReport};

#[test]
fn format_sessions_empty() {
    let body = SlackFormatter::format(&CommandResult::Sessions(vec![]));
    assert_eq!(body, "No active sessions.");
}

#[test]
fn format_sessions_lists_each() {
    let body = SlackFormatter::format(&CommandResult::Sessions(vec![SessionSummary {
        id: "abcdef0123456789".into(),
        status: "active".into(),
        workdir: "/work/p".into(),
    }]));
    // mrkdwn bold heading, status dot, short id in inline code, and workdir.
    assert!(body.contains("*trusty-mpm sessions*"));
    assert!(body.contains("🟢"));
    assert!(body.contains("`abcdef01…`"));
    assert!(body.contains("`/work/p`"));
}

#[test]
fn format_session_detail_lists_events() {
    let body = SlackFormatter::format(&CommandResult::SessionDetail {
        id: "abcdef0123456789".into(),
        status: "active".into(),
        events: vec!["PreToolUse".into(), "Stop".into()],
    });
    assert!(body.contains("*Session abcdef01…*"));
    assert!(body.contains("• PreToolUse"));
    assert!(body.contains("• Stop"));
}

#[test]
fn format_tmux_lists_each() {
    let body = SlackFormatter::format(&CommandResult::TmuxSessions(vec![
        TmuxSessionSummary {
            name: "tmpm-quiet-falcon".into(),
            managed: true,
        },
        TmuxSessionSummary {
            name: "random-shell".into(),
            managed: false,
        },
    ]));
    assert!(body.contains("`tmpm-quiet-falcon` — 🟢 managed"));
    assert!(body.contains("`random-shell` — ⚪ external"));
}

#[test]
fn format_discovered_projects_lists_each() {
    let body = format_discovered_projects(&[DiscoveredProjectSummary {
        path: "/work/proj".into(),
        session_count: 3,
        last_session: Some("2026-06-19T12:00:00Z".into()),
    }]);
    assert!(body.contains("*Discovered projects*"));
    assert!(body.contains("`/work/proj`"));
    assert!(body.contains("3 session(s) · last used 2026-06-19"));
}

#[test]
fn format_managed_sessions_lists_each() {
    let body = format_managed_sessions(&[ManagedSessionView {
        id: "abcdef0123456789".into(),
        name: "blue-otter".into(),
        state: "running".into(),
        workspace_path: None,
        repo_url: None,
        branch: None,
        pending_decision: Some("apply patch?".into()),
        proposed_default: Some("yes".into()),
        slot: 0,
        deleted: false,
    }]);
    assert!(body.contains("*managed sessions*"));
    assert!(body.contains("`abcdef01…` blue-otter [running]"));
    // A blocked session is flagged.
    assert!(body.contains("⚠️"));
}

#[test]
fn format_managed_session_renders_fields() {
    let body = format_managed_session(&ManagedSessionView {
        id: "abcdef0123456789".into(),
        name: "blue-otter".into(),
        state: "running".into(),
        workspace_path: Some("/ws/blue".into()),
        repo_url: Some("git@host:repo.git".into()),
        branch: Some("main".into()),
        pending_decision: Some("apply patch?".into()),
        proposed_default: Some("yes".into()),
        slot: 0,
        deleted: false,
    });
    assert!(body.contains("*blue-otter* (`abcdef01…`) [running]"));
    assert!(body.contains("📁 `/ws/blue`"));
    assert!(body.contains("🔗 git@host:repo.git main"));
    assert!(body.contains("⚠️ pending: apply patch? (default: `yes`)"));
}

#[test]
fn format_health_up_and_down() {
    let up = SlackFormatter::format(&CommandResult::Health(HealthReport {
        reachable: true,
        url: "http://localhost:9999".into(),
        status: "ok".into(),
        catalog_stale: true,
        catalog_unknown: false,
        managed_total: 4,
        managed_pending_decisions: 1,
    }));
    assert!(up.contains("✅ *daemon ok*"));
    assert!(up.contains("catalog: ⚠️ updates available"));
    assert!(up.contains("fleet: 4 session(s), 1 awaiting a decision"));

    let down = SlackFormatter::format(&CommandResult::Health(HealthReport {
        reachable: false,
        url: "http://localhost:9999".into(),
        ..Default::default()
    }));
    assert!(down.contains("❌ *daemon unreachable*"));
    // A down daemon must not render misleading fleet counts.
    assert!(!down.contains("fleet:"));
}

#[test]
fn format_overseer_status() {
    let body = SlackFormatter::format(&CommandResult::OverseerStatus {
        enabled: true,
        handler: "llm".into(),
        decisions: DecisionCounts {
            allow: 5,
            block: 1,
            flag: 2,
        },
    });
    assert!(body.contains("*Overseer Status*"));
    assert!(body.contains("Handler: `llm`"));
    assert!(body.contains("allow (5), block (1), flag (2)"));
}

#[test]
fn format_doctor_report_lists_each_check() {
    let report = DoctorReport {
        checks: vec![
            DoctorCheck {
                name: "daemon".into(),
                status: CheckStatus::Ok,
                message: "reachable".into(),
            },
            DoctorCheck {
                name: "catalog".into(),
                status: CheckStatus::Warn,
                message: "stale".into(),
            },
        ],
        overall: CheckStatus::Warn,
        generated_at: chrono::Utc::now(),
    };
    let body = SlackFormatter::format(&CommandResult::Doctor(report));
    assert!(body.contains("✅ *daemon* — reachable"));
    assert!(body.contains("⚠️ *catalog* — stale"));
    assert!(body.contains("⚠️ *overall: warnings*"));
}

#[test]
fn format_command_sent_uses_code_block() {
    let body = SlackFormatter::format(&CommandResult::CommandSent {
        session: "frontend".into(),
        output: "build complete".into(),
    });
    assert!(body.contains("*📨 frontend*"));
    assert!(body.contains("```\nbuild complete\n```"));
}

#[test]
fn format_command_sent_no_output() {
    let body = SlackFormatter::format(&CommandResult::CommandSent {
        session: "frontend".into(),
        output: "   ".into(),
    });
    assert_eq!(body, "📨 Sent to `frontend` — no output captured");
}

#[test]
fn format_snapshot_tails_output() {
    // 60 lines collapse to the last 50 (MAX_OUTPUT_LINES).
    let output = (1..=60)
        .map(|n| format!("line{n}"))
        .collect::<Vec<_>>()
        .join("\n");
    let body = SlackFormatter::format(&CommandResult::Snapshot {
        session: "sess".into(),
        output,
    });
    assert!(body.contains("*Snapshot: sess*"));
    assert!(body.contains("```"));
    // The oldest line is dropped; the newest is kept.
    assert!(!body.contains("line1\n"));
    assert!(body.contains("line60"));
}

#[test]
fn format_error_renders_message() {
    let body = SlackFormatter::format(&CommandResult::Error("daemon unreachable".into()));
    assert_eq!(body, "❌ daemon unreachable");
}

#[test]
fn format_chat_reply_passthrough() {
    // A free-text chat reply renders verbatim (Slack mrkdwn from the LLM).
    let body = SlackFormatter::format(&CommandResult::ChatReply {
        reply: "spun up *blue-otter*".into(),
    });
    assert_eq!(body, "spun up *blue-otter*");
}

#[test]
fn format_managed_spawned() {
    let body = SlackFormatter::format(&CommandResult::ManagedSpawned {
        id: "abcdef0123456789".into(),
        name: "blue-otter".into(),
        state: "running".into(),
        runtime: "claude-code".into(),
        attach_cmd: "tmux attach -t blue-otter".into(),
    });
    assert!(body.contains("✅ Spawned *blue-otter* (`abcdef01…`) [running] runtime=claude-code"));
    assert!(body.contains("attach: `tmux attach -t blue-otter`"));
}

#[test]
fn short_id_truncates_long_ids() {
    assert_eq!(short_id("abcdef0123456789"), "abcdef01…");
    // Already short ids pass through unchanged.
    assert_eq!(short_id("short"), "short");
    // An id of exactly SHORT_ID_LEN chars has no 9th char → no ellipsis.
    assert_eq!(short_id("12345678"), "12345678");
}

#[test]
fn short_id_handles_multibyte() {
    // A multi-byte UTF-8 id must truncate on a CHAR boundary, never panic on a
    // byte slice (regression: `&id[..8]` panicked mid-codepoint).
    let id = "日本語のセッションです"; // 11 chars, 3 bytes each
    let out = short_id(id);
    // First SHORT_ID_LEN (8) chars: 日本語のセッショ, then the ellipsis.
    assert_eq!(out, "日本語のセッショ…");
    // Char count of the head is SHORT_ID_LEN; the ellipsis is the only extra char.
    assert_eq!(out.chars().count(), SHORT_ID_LEN + 1);
}

// ── WI-B (#1586): fleet-by-project formatter (Slack mrkdwn) ──────────────────

/// Empty fleet renders a placeholder, not an empty body.
///
/// Why: the Slack handler must always emit non-empty text; the sentinel message
/// lets the operator know the project registry is empty.
/// What: `SlackFormatter::format` on a `ManagedFleet([])` returns
/// "No registered projects."
/// Test: this function IS the test.
#[test]
fn format_fleet_by_project_slack_empty_returns_placeholder() {
    let body = SlackFormatter::format(&CommandResult::ManagedFleet(vec![]));
    assert_eq!(body, "No registered projects.");
}

/// Projects with sessions are rendered with correct glyph/flag/mrkdwn structure.
///
/// Why: Slack `mrkdwn` uses `*bold*` and backtick code — not HTML tags — so the
/// fleet-by-project output must differ from the Telegram counterpart in markup
/// while covering the same semantic content.
/// What: heading, bold project name, inline-code repo URL, session rows with
/// state glyphs and ⚠️ flag for pending decisions.
/// Test: this function IS the test.
#[test]
fn format_fleet_by_project_slack_renders_projects() {
    let fleet = vec![
        ProjectFleetView {
            project_name: "proj-alpha".into(),
            repo_url: "https://github.com/org/alpha".into(),
            sessions: vec![
                ManagedSessionView {
                    id: "aaaa1111bbbb2222".into(),
                    name: "tmpm-alpha-1".into(),
                    state: "active".into(),
                    workspace_path: None,
                    repo_url: None,
                    branch: None,
                    pending_decision: None,
                    proposed_default: None,
                    slot: 0,
                    deleted: false,
                },
                ManagedSessionView {
                    id: "cccc3333dddd4444".into(),
                    name: "tmpm-alpha-2".into(),
                    state: "stopped".into(),
                    workspace_path: None,
                    repo_url: None,
                    branch: None,
                    pending_decision: Some("apply patch?".into()),
                    proposed_default: None,
                    slot: 0,
                    deleted: false,
                },
            ],
        },
        ProjectFleetView {
            project_name: "proj-beta".into(),
            repo_url: "https://github.com/org/beta".into(),
            sessions: vec![],
        },
    ];
    let body = SlackFormatter::format(&CommandResult::ManagedFleet(fleet));

    // Heading uses mrkdwn bold (asterisks, not HTML)
    assert!(body.contains("*fleet by project*"), "heading: {body}");

    // Project alpha: mrkdwn bold name, backtick-code repo url
    assert!(body.contains("*proj-alpha*"), "bold project name: {body}");
    assert!(
        body.contains("`https://github.com/org/alpha`"),
        "code repo url: {body}"
    );

    // Active session → green glyph
    assert!(body.contains("🟢"), "active → 🟢: {body}");
    assert!(body.contains("`aaaa1111…`"), "short id: {body}");
    assert!(body.contains("tmpm-alpha-1"), "session name: {body}");
    assert!(body.contains("[active]"), "state bracket: {body}");

    // Stopped + pending decision → red glyph + warning flag
    assert!(body.contains("🔴"), "stopped → 🔴: {body}");
    assert!(body.contains("⚠️"), "pending decision flag: {body}");

    // Project beta empty → dash placeholder
    assert!(body.contains("*proj-beta*"), "beta project header: {body}");
    assert!(body.contains("\n  —"), "empty project placeholder: {body}");
}

/// Provisioning state maps to the yellow glyph in Slack output.
///
/// Why: `fleet_state_glyph` must emit 🟡 for `"provisioning"` in the Slack
/// formatter, matching the Telegram formatter's three-tier convention.
/// What: a `ManagedFleet` entry with one `provisioning` session renders 🟡.
/// Test: this function IS the test.
#[test]
fn format_fleet_by_project_slack_provisioning_glyph() {
    let fleet = vec![ProjectFleetView {
        project_name: "proj".into(),
        repo_url: "https://example.com/repo".into(),
        sessions: vec![ManagedSessionView {
            id: "eeee5555ffff6666".into(),
            name: "tmpm-prov".into(),
            state: "provisioning".into(),
            workspace_path: None,
            repo_url: None,
            branch: None,
            pending_decision: None,
            proposed_default: None,
            slot: 0,
            deleted: false,
        }],
    }];
    let body = SlackFormatter::format(&CommandResult::ManagedFleet(fleet));
    assert!(body.contains("🟡"), "provisioning → 🟡: {body}");
}

#[test]
fn mrkdwn_escape_escapes_ampersand_lt_gt() {
    // The three mrkdwn-significant characters are escaped, in an order that
    // never double-escapes the `&` produced by the `<`/`>` substitutions.
    assert_eq!(mrkdwn_escape("a & b"), "a &amp; b");
    assert_eq!(mrkdwn_escape("<tag>"), "&lt;tag&gt;");
    assert_eq!(mrkdwn_escape("a < b & c > d"), "a &lt; b &amp; c &gt; d");
    // Plain text is unchanged.
    assert_eq!(mrkdwn_escape("plain text"), "plain text");
}

#[test]
fn mrkdwn_escape_neutralizes_channel_broadcast_span() {
    // Why (#2565 review): an unescaped `<!channel>` in a session name would
    // broadcast-ping the whole channel when interpolated into a proxy reply.
    // Escaping must turn it into inert literal text.
    let hostile = "<!channel> pwned";
    let escaped = mrkdwn_escape(hostile);
    assert_eq!(escaped, "&lt;!channel&gt; pwned");
    assert!(
        !escaped.contains("<!channel>"),
        "the literal broadcast span must not survive escaping: {escaped}"
    );
}