aa-cli 0.0.1-alpha.8

aasm — command-line tool for Agent Assembly
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
//! TUI rendering — draws the 4-panel dashboard layout.

use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Cell, Clear, Gauge, List, ListItem, Paragraph, Row, Table, Wrap};
use ratatui::Frame;

use crate::commands::approvals::models::{compute_timeout_color, format_countdown, TimeoutColor};

use super::state::{DashboardState, Panel};

/// Approval timeout in seconds (5 minutes, matching `approvals watch`).
const APPROVAL_TIMEOUT_SECS: i64 = 300;

/// Render the entire dashboard UI to the terminal frame.
pub fn draw(f: &mut Frame, state: &DashboardState) {
    let size = f.area();

    // Split vertically: top half and bottom half, plus a 1-line footer.
    let outer = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage(50),
            Constraint::Percentage(50),
            Constraint::Min(1),
        ])
        .split(size);

    // Top: Agents (left) | Event Log (right)
    let top = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(55), Constraint::Percentage(45)])
        .split(outer[0]);

    // Bottom: Budget (left) | Approvals (right)
    let bottom = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(45), Constraint::Percentage(55)])
        .split(outer[1]);

    draw_agents_panel(f, top[0], state);
    draw_event_log_panel(f, top[1], state);
    draw_budget_panel(f, bottom[0], state);
    draw_approvals_panel(f, bottom[1], state);
    draw_footer(f, outer[2], state);

    if state.show_help {
        draw_help_overlay(f, size);
    }
    if state.show_inspect {
        draw_inspect_overlay(f, state);
    }
    if state.show_policy {
        draw_policy_overlay(f, state);
    }
}

/// Build a Block with a highlighted border when the panel is focused.
fn panel_block(title: &str, panel: Panel, state: &DashboardState) -> Block<'static> {
    let is_active = state.active_panel == panel;
    let border_style = if is_active {
        Style::default().fg(Color::Cyan)
    } else {
        Style::default().fg(Color::DarkGray)
    };
    Block::default()
        .title(format!(" {title} "))
        .borders(Borders::ALL)
        .border_style(border_style)
}

/// Top-left: runtime health header + agents table.
fn draw_agents_panel(f: &mut Frame, area: Rect, state: &DashboardState) {
    let block = panel_block("Agents", Panel::Agents, state);
    let inner = block.inner(area);
    f.render_widget(block, area);

    // Reserve 2 lines for the health header.
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Min(1)])
        .split(inner);

    // Health header line.
    let status_indicator = if state.runtime.reachable { "" } else { "" };
    let status_color = if state.runtime.reachable {
        Color::Green
    } else {
        Color::Red
    };
    let uptime = format_duration(state.runtime.uptime_secs);
    let header_line = Line::from(vec![
        Span::styled(format!("{status_indicator} "), Style::default().fg(status_color)),
        Span::raw(format!(
            "{} | up {} | {} conns | lag {}ms",
            state.runtime.status, uptime, state.runtime.active_connections, state.runtime.pipeline_lag_ms,
        )),
    ]);
    f.render_widget(Paragraph::new(header_line), chunks[0]);

    // Agents table.
    let header = Row::new(vec!["ID", "NAME", "STATUS", "FW", "SESS", "LAST EVT", "VIOL", "LAYER"])
        .style(Style::default().add_modifier(Modifier::BOLD))
        .bottom_margin(0);

    let rows: Vec<Row> = state
        .agents
        .iter()
        .enumerate()
        .map(|(i, a)| {
            let is_selected = i == state.agent_selected;
            let status_style = match a.status.as_str() {
                "Running" | "Active" => Style::default().fg(Color::Green),
                "Error" | "Failed" => Style::default().fg(Color::Red),
                _ => Style::default().fg(Color::Yellow),
            };
            let row = Row::new(vec![
                Cell::from(truncate(&a.id, 8)),
                Cell::from(a.name.as_str()),
                Cell::from(a.status.as_str()).style(status_style),
                Cell::from(a.framework.as_str()),
                Cell::from(a.sessions.to_string()),
                Cell::from(a.last_event.as_str()),
                Cell::from(a.violations_today.to_string()),
                Cell::from(a.layer.as_str()),
            ]);
            if is_selected {
                row.style(
                    Style::default()
                        .fg(Color::Black)
                        .bg(Color::Cyan)
                        .add_modifier(Modifier::BOLD),
                )
            } else {
                row
            }
        })
        .collect();

    let table = Table::new(
        rows,
        [
            Constraint::Length(9),
            Constraint::Min(10),
            Constraint::Length(8),
            Constraint::Length(10),
            Constraint::Length(5),
            Constraint::Length(20),
            Constraint::Length(5),
            Constraint::Length(8),
        ],
    )
    .header(header);

    f.render_widget(table, chunks[1]);
}

/// Top-right: scrollable event log from the WebSocket stream.
fn draw_event_log_panel(f: &mut Frame, area: Rect, state: &DashboardState) {
    let block = panel_block("Event Log", Panel::EventLog, state);
    let inner = block.inner(area);
    f.render_widget(block, area);

    let items: Vec<ListItem> = state
        .event_log
        .iter()
        .rev()
        .map(|e| {
            let type_color = match e.event_type.as_str() {
                "violation" => Color::Red,
                "approval" => Color::Yellow,
                "budget" => Color::Magenta,
                _ => Color::White,
            };
            ListItem::new(Line::from(vec![
                Span::styled(
                    format!("[{}] ", short_timestamp(&e.timestamp)),
                    Style::default().fg(Color::DarkGray),
                ),
                Span::styled(format!("{:<10} ", e.event_type), Style::default().fg(type_color)),
                Span::raw(&e.message),
            ]))
        })
        .collect();

    let list = List::new(items);
    f.render_widget(list, inner);
}

/// Bottom-right: pending approval requests with countdown timers and selection highlight.
fn draw_approvals_panel(f: &mut Frame, area: Rect, state: &DashboardState) {
    let title = format!("Approvals ({} pending)", state.approvals_summary.pending_count);
    let block = panel_block(&title, Panel::Approvals, state);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if state.pending_approvals.is_empty() {
        let msg = Paragraph::new("No pending approvals").style(Style::default().fg(Color::DarkGray));
        f.render_widget(msg, inner);
        return;
    }

    let now = chrono::Utc::now().timestamp();

    let items: Vec<ListItem> = state
        .pending_approvals
        .iter()
        .enumerate()
        .map(|(i, ap)| {
            let style = if i == state.approval_selected {
                Style::default()
                    .fg(Color::Black)
                    .bg(Color::Cyan)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default()
            };

            // Compute countdown timer.
            let submitted = chrono::DateTime::parse_from_rfc3339(&ap.created_at)
                .map(|dt| dt.timestamp())
                .unwrap_or(0);
            let remaining = (submitted + APPROVAL_TIMEOUT_SECS) - now;
            let countdown = format_countdown(remaining);
            let countdown_color = match compute_timeout_color(remaining) {
                TimeoutColor::Red => Color::Red,
                TimeoutColor::Yellow => Color::Yellow,
                TimeoutColor::Green => Color::Green,
            };

            let routing_label = if !ap.routing_status.is_empty() {
                format!(" [{}]", ap.routing_status)
            } else {
                String::new()
            };
            ListItem::new(Line::from(vec![
                Span::styled(format!("{countdown:<8} "), Style::default().fg(countdown_color)),
                Span::raw(format!("{}{}{}", ap.agent_id, ap.action, routing_label)),
            ]))
            .style(style)
        })
        .collect();

    let list = List::new(items);
    f.render_widget(list, inner);
}

/// Bottom-left: budget utilization bars per agent.
fn draw_budget_panel(f: &mut Frame, area: Rect, state: &DashboardState) {
    let block = panel_block("Budget", Panel::Budget, state);
    let inner = block.inner(area);
    f.render_widget(block, area);

    let agent_count = state.budget.per_agent.len();
    // Reserve 3 lines for the total gauge, then 1 line per agent bar.
    let mut constraints: Vec<Constraint> = vec![Constraint::Length(3)];
    for _ in 0..agent_count {
        constraints.push(Constraint::Length(1));
    }
    constraints.push(Constraint::Min(0)); // absorb remaining space
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints(constraints)
        .split(inner);

    // Total daily spend gauge.
    let (ratio, label) = compute_budget_ratio(state);
    let gauge_color = if ratio > 0.9 {
        Color::Red
    } else if ratio > 0.7 {
        Color::Yellow
    } else {
        Color::Green
    };
    let gauge = Gauge::default()
        .gauge_style(Style::default().fg(gauge_color))
        .ratio(ratio)
        .label(label);
    f.render_widget(gauge, chunks[0]);

    // Per-agent utilization bars.
    let daily_limit: f64 = state
        .budget
        .daily_limit_usd
        .as_deref()
        .and_then(|s| s.parse().ok())
        .unwrap_or(0.0);

    for (i, entry) in state.budget.per_agent.iter().enumerate() {
        let spend: f64 = entry.daily_spend_usd.parse().unwrap_or(0.0);
        let agent_ratio = if daily_limit > 0.0 {
            (spend / daily_limit).min(1.0)
        } else {
            0.0
        };
        let bar_width = chunks[1 + i].width.saturating_sub(2) as usize; // leave margins
        let filled = ((agent_ratio * bar_width as f64) as usize).min(bar_width);
        let empty = bar_width.saturating_sub(filled);
        let bar_color = if agent_ratio > 0.5 { Color::Yellow } else { Color::Green };
        let label = format!("{:<8} ", truncate(&entry.agent_id, 8));
        let bar_line = Line::from(vec![
            Span::raw(label),
            Span::styled("".repeat(filled), Style::default().fg(bar_color)),
            Span::styled("".repeat(empty), Style::default().fg(Color::DarkGray)),
            Span::raw(format!(" ${}", entry.daily_spend_usd)),
        ]);
        f.render_widget(Paragraph::new(bar_line), chunks[1 + i]);
    }
}

/// Footer bar with keyboard shortcuts.
fn draw_footer(f: &mut Frame, area: Rect, _state: &DashboardState) {
    let footer = Line::from(vec![
        Span::styled(" Tab", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" panel  "),
        Span::styled("↑↓", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" scroll  "),
        Span::styled("Enter", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" inspect  "),
        Span::styled("a", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw("/"),
        Span::styled("r", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" approve/reject  "),
        Span::styled("p", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" policy  "),
        Span::styled("?", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" help  "),
        Span::styled("q", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw(" quit"),
    ]);
    f.render_widget(Paragraph::new(footer).style(Style::default().fg(Color::DarkGray)), area);
}

/// Render a centered help overlay listing all keyboard shortcuts.
fn draw_help_overlay(f: &mut Frame, area: Rect) {
    let overlay = centered_rect(60, 60, area);
    f.render_widget(Clear, overlay);

    let block = Block::default()
        .title(" Help — Keyboard Shortcuts ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan));
    let inner = block.inner(overlay);
    f.render_widget(block, overlay);

    let lines = vec![
        Line::from(vec![
            Span::styled("Tab      ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("Next panel"),
        ]),
        Line::from(vec![
            Span::styled("Shift+Tab", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("Previous panel"),
        ]),
        Line::from(""),
        Line::from(vec![
            Span::styled("↑ / k    ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("Scroll up / select previous"),
        ]),
        Line::from(vec![
            Span::styled("↓ / j    ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("Scroll down / select next"),
        ]),
        Line::from(vec![
            Span::styled("Enter    ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("Inspect selected agent/approval"),
        ]),
        Line::from(""),
        Line::from(vec![
            Span::styled("a        ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("Approve selected request"),
        ]),
        Line::from(vec![
            Span::styled("r        ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("Reject selected request"),
        ]),
        Line::from(vec![
            Span::styled("p        ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("View active policy"),
        ]),
        Line::from(""),
        Line::from(vec![
            Span::styled("?        ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("Toggle this help overlay"),
        ]),
        Line::from(vec![
            Span::styled("q / Esc  ", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw("Quit dashboard"),
        ]),
    ];

    let help = Paragraph::new(lines);
    f.render_widget(help, inner);
}

/// Render a centered inspect overlay showing details of the selected agent or approval.
pub fn draw_inspect_overlay(f: &mut Frame, state: &DashboardState) {
    let area = f.area();
    let overlay = centered_rect(70, 60, area);
    f.render_widget(Clear, overlay);

    match state.active_panel {
        Panel::Agents => {
            let block = Block::default()
                .title(" Agent Detail ")
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Cyan));
            let inner = block.inner(overlay);
            f.render_widget(block, overlay);

            if let Some(agent) = state.agents.get(state.agent_selected) {
                let lines = vec![
                    Line::from(vec![
                        Span::styled("ID:          ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&agent.id),
                    ]),
                    Line::from(vec![
                        Span::styled("Name:        ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&agent.name),
                    ]),
                    Line::from(vec![
                        Span::styled("Framework:   ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&agent.framework),
                    ]),
                    Line::from(vec![
                        Span::styled("Status:      ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::styled(
                            &agent.status,
                            match agent.status.as_str() {
                                "Running" | "Active" => Style::default().fg(Color::Green),
                                "Error" | "Failed" => Style::default().fg(Color::Red),
                                _ => Style::default().fg(Color::Yellow),
                            },
                        ),
                    ]),
                    Line::from(vec![
                        Span::styled("Sessions:    ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(agent.sessions.to_string()),
                    ]),
                    Line::from(vec![
                        Span::styled("Violations:  ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(agent.violations_today.to_string()),
                    ]),
                    Line::from(vec![
                        Span::styled("Layer:       ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&agent.layer),
                    ]),
                    Line::from(""),
                    Line::from(Span::styled("Press Esc to close", Style::default().fg(Color::DarkGray))),
                ];
                f.render_widget(Paragraph::new(lines), inner);
            }
        }
        Panel::Approvals => {
            let block = Block::default()
                .title(" Approval Detail ")
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Cyan));
            let inner = block.inner(overlay);
            f.render_widget(block, overlay);

            if let Some(ap) = state.pending_approvals.get(state.approval_selected) {
                let team_display = if ap.team_id.is_empty() {
                    "(none)".to_string()
                } else {
                    ap.team_id.clone()
                };
                let routing_display = if ap.routing_status.is_empty() {
                    "(unknown)".to_string()
                } else {
                    ap.routing_status.clone()
                };
                let lines = vec![
                    Line::from(vec![
                        Span::styled("ID:          ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&ap.id),
                    ]),
                    Line::from(vec![
                        Span::styled("Agent:       ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&ap.agent_id),
                    ]),
                    Line::from(vec![
                        Span::styled("Action:      ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&ap.action),
                    ]),
                    Line::from(vec![
                        Span::styled("Reason:      ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&ap.reason),
                    ]),
                    Line::from(vec![
                        Span::styled("Status:      ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&ap.status),
                    ]),
                    Line::from(vec![
                        Span::styled("Team:        ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::styled(team_display, Style::default().fg(Color::Cyan)),
                    ]),
                    Line::from(vec![
                        Span::styled("Routing:     ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::styled(routing_display, Style::default().fg(Color::Yellow)),
                    ]),
                    Line::from(vec![
                        Span::styled("Created:     ", Style::default().add_modifier(Modifier::BOLD)),
                        Span::raw(&ap.created_at),
                    ]),
                    Line::from(""),
                    Line::from(Span::styled("Press Esc to close", Style::default().fg(Color::DarkGray))),
                ];
                f.render_widget(Paragraph::new(lines), inner);
            }
        }
        _ => {
            // No inspect for EventLog or Budget panels.
        }
    }
}

/// Render a centered policy viewer overlay showing policy YAML content.
pub fn draw_policy_overlay(f: &mut Frame, state: &DashboardState) {
    let area = f.area();
    let overlay = centered_rect(75, 80, area);
    f.render_widget(Clear, overlay);

    let block = Block::default()
        .title(" Policy Viewer ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan));
    let inner = block.inner(overlay);
    f.render_widget(block, overlay);

    let content = match &state.policy_yaml {
        Some(yaml) => yaml.as_str(),
        None => "(loading policy…)",
    };

    let paragraph = Paragraph::new(content).wrap(Wrap { trim: false });
    f.render_widget(paragraph, inner);
}

/// Compute a centered rectangle within `area`.
fn centered_rect(percent_x: u16, percent_y: u16, area: Rect) -> Rect {
    let vertical = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Percentage(percent_y),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(area);

    let horizontal = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(vertical[1]);

    horizontal[1]
}

/// Compute the budget gauge ratio and label string.
fn compute_budget_ratio(state: &DashboardState) -> (f64, String) {
    let spend: f64 = state.budget.daily_spend_usd.parse().unwrap_or(0.0);

    let limit: Option<f64> = state.budget.daily_limit_usd.as_deref().and_then(|s| s.parse().ok());

    match limit {
        Some(lim) if lim > 0.0 => {
            let ratio = (spend / lim).min(1.0);
            (ratio, format!("${spend:.2} / ${lim:.2}"))
        }
        _ => (0.0, format!("${spend:.2} (no limit set)")),
    }
}

/// Truncate a string to `max` characters, appending "…" if shortened.
fn truncate(s: &str, max: usize) -> String {
    if s.len() <= max {
        s.to_string()
    } else {
        format!("{}", &s[..max - 1])
    }
}

/// Extract HH:MM:SS from an ISO timestamp for compact display.
fn short_timestamp(ts: &str) -> &str {
    // "2026-04-30T10:00:00Z" → "10:00:00"
    if ts.len() >= 19 {
        &ts[11..19]
    } else {
        ts
    }
}

/// Format seconds into a human-readable duration string.
fn format_duration(secs: u64) -> String {
    let h = secs / 3600;
    let m = (secs % 3600) / 60;
    if h > 0 {
        format!("{h}h {m}m")
    } else {
        format!("{m}m")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn truncate_short_string_unchanged() {
        assert_eq!(truncate("abc", 5), "abc");
    }

    #[test]
    fn truncate_long_string_adds_ellipsis() {
        assert_eq!(truncate("abcdef", 4), "abc…");
    }

    #[test]
    fn short_timestamp_extracts_time() {
        assert_eq!(short_timestamp("2026-04-30T10:15:30Z"), "10:15:30");
    }

    #[test]
    fn short_timestamp_returns_input_if_too_short() {
        assert_eq!(short_timestamp("short"), "short");
    }

    #[test]
    fn format_duration_hours_and_minutes() {
        assert_eq!(format_duration(3661), "1h 1m");
    }

    #[test]
    fn format_duration_minutes_only() {
        assert_eq!(format_duration(300), "5m");
    }

    #[test]
    fn compute_budget_ratio_with_limit() {
        let state = DashboardState::new();
        let mut state = state;
        state.budget.daily_spend_usd = "50.00".to_string();
        state.budget.daily_limit_usd = Some("100.00".to_string());
        let (ratio, label) = compute_budget_ratio(&state);
        assert!((ratio - 0.5).abs() < 0.01);
        assert!(label.contains("50.00"));
        assert!(label.contains("100.00"));
    }

    #[test]
    fn compute_budget_ratio_no_limit() {
        let state = DashboardState::new();
        let (ratio, label) = compute_budget_ratio(&state);
        assert!((ratio - 0.0).abs() < 0.01);
        assert!(label.contains("no limit"));
    }

    #[test]
    fn compute_budget_ratio_capped_at_one() {
        let mut state = DashboardState::new();
        state.budget.daily_spend_usd = "150.00".to_string();
        state.budget.daily_limit_usd = Some("100.00".to_string());
        let (ratio, _) = compute_budget_ratio(&state);
        assert!((ratio - 1.0).abs() < 0.01);
    }
}