sparrow-cli 0.5.1

A local-first Rust agent cockpit — route, run, replay, rewind
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
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
use sparrow::tui::theme::{THEME_NAMES, by_name};

#[test]
fn three_built_in_themes_are_resolvable_by_name() {
    assert_eq!(THEME_NAMES, &["captain", "midnight", "paper"]);
    let cap = by_name("captain");
    let mid = by_name("midnight");
    let pap = by_name("paper");
    // The three variants must produce distinct backgrounds — that is what
    // makes them visibly different in the cockpit.
    assert_ne!(cap.bg, mid.bg);
    assert_ne!(cap.bg, pap.bg);
    assert_ne!(mid.bg, pap.bg);
}

#[test]
fn unknown_theme_name_falls_back_to_captain() {
    let unknown = by_name("does-not-exist");
    let captain = by_name("captain");
    // Same background → same theme.
    assert_eq!(unknown.bg, captain.bg);
    assert_eq!(unknown.brand, captain.brand);
}

#[test]
fn theme_lookup_is_case_insensitive_and_trims() {
    let a = by_name("Midnight");
    let b = by_name("  MIDNIGHT  ");
    let c = by_name("midnight");
    assert_eq!(a.bg, b.bg);
    assert_eq!(b.bg, c.bg);
}

#[test]
fn keyboard_doc_lists_critical_shortcuts() {
    let doc = std::fs::read_to_string("docs/keyboard.md")
        .expect("docs/keyboard.md must ship with the TUI");
    for shortcut in [
        "Ctrl+I",
        "Ctrl+L",
        "Ctrl+O",
        "Shift+Enter",
        "Tab",
        "Up",
        "Down",
    ] {
        assert!(
            doc.contains(shortcut),
            "docs/keyboard.md must document {}",
            shortcut
        );
    }
    for theme in ["captain", "midnight", "paper"] {
        assert!(doc.contains(theme), "docs/keyboard.md must list {}", theme);
    }
    for shortcut in [
        "Cmd/Ctrl+K",
        "Cmd/Ctrl+Shift+L",
        "Cmd/Ctrl+M",
        "Drag files onto page",
        "@",
    ] {
        assert!(
            doc.contains(shortcut),
            "docs/keyboard.md must document WebView shortcut {}",
            shortcut
        );
    }
}

#[test]
fn console_html_has_dynamic_swarm_hooks() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    // The Sprint 1 deliverable 1bis: the swarm row must expose the anchor
    // and the overflow chip so loadSwarmAgents() can inject extras.
    assert!(
        html.contains("swarm-extras-anchor"),
        "console.html must expose #swarm-extras-anchor for dynamic agent lanes"
    );
    assert!(
        html.contains("swarm-more"),
        "console.html must expose #swarm-more for the +N overflow chip"
    );
    assert!(
        html.contains("loadSwarmAgents"),
        "console.html must call loadSwarmAgents() on connect"
    );
    // No truncation: `text-overflow: ellipsis` must not gate `.lane .msg`.
    let lane_msg_block = html
        .split(".lane .msg")
        .nth(1)
        .expect("expected `.lane .msg` CSS block");
    let first_rule = lane_msg_block.split('}').next().unwrap_or("");
    assert!(
        !first_rule.contains("text-overflow:ellipsis"),
        "`.lane .msg` must not truncate text in the new swarm row, got: {}",
        first_rule
    );
}

#[test]
fn console_html_has_drawer_with_seven_panels() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    // Rail buttons for the 7 panels of Sprint 1 inc 2.
    for panel in [
        "sessions",
        "memory",
        "plugins",
        "tools",
        "permissions",
        "security",
        "artifacts",
    ] {
        let rail_marker = format!("data-panel=\"{}\"", panel);
        let body_marker = format!("data-panel-body=\"{}\"", panel);
        assert!(
            html.contains(&rail_marker),
            "rail must expose button for {}",
            panel
        );
        assert!(
            html.contains(&body_marker),
            "drawer must expose body for {}",
            panel
        );
    }
    assert!(
        html.contains("PANEL_LOADERS"),
        "panel loader registry must be present"
    );
    assert!(
        html.contains("openPanel"),
        "openPanel() switch handler must be present"
    );
}

#[test]
fn console_html_has_paper_theme_and_chrome_chips() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    // Sprint 1 inc 4-5: paper theme variant + chrome chips for palette /
    // replay / sound / theme. The selector must come from a CSS block keyed
    // on `data-theme="paper"`.
    assert!(
        html.contains("[data-theme=\"paper\"]"),
        "console.html must declare a paper theme block"
    );
    for chip in ["cmdkBtn", "replayBtn", "soundBtn", "themeBtn"] {
        let marker = format!("id=\"{}\"", chip);
        assert!(html.contains(&marker), "chrome must expose {} chip", chip);
    }
    // Hero welcome + boot animation must be wired.
    assert!(
        html.contains("injectHero") && html.contains(".hero{"),
        "hero welcome must be injected at term boot"
    );
    assert!(
        html.contains("runBootAnimation") && html.contains("bootOverlay"),
        "boot overlay must be present and triggered"
    );
    // Sound system + Cmd+M mute toggle.
    assert!(
        html.contains("function chirp(") && html.contains("'sparrow-muted'"),
        "WebAudio chirp + mute persistence must be wired"
    );
}

#[test]
fn console_html_has_slash_palette_and_agent_picker() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    // Slash palette modal + Cmd+K wiring.
    assert!(
        html.contains("id=\"palette\""),
        "console.html must declare the slash palette modal"
    );
    assert!(
        html.contains("paletteOpen") && html.contains("paletteFilter"),
        "palette open / filter functions must be present"
    );
    assert!(
        html.contains("key.toLowerCase()==='k'"),
        "Cmd/Ctrl+K shortcut must be wired"
    );
    // Inline @-picker.
    assert!(
        html.contains("id=\"agentPicker\""),
        "console.html must declare the inline @-picker"
    );
    assert!(
        html.contains("agentPickerState") && html.contains("agentPickerAccept"),
        "@-picker state/accept helpers must be present"
    );
    // Caches load on connect.
    assert!(
        html.contains("loadCommandsCache") && html.contains("loadAgentsCache"),
        "both /commands and /agents must be pre-fetched on connect"
    );
    assert!(
        html.contains("cmd.usage") && html.contains("paletteSourceLabel"),
        "slash palette and /help must render command usage plus readable sources"
    );
    assert!(
        html.contains("runWebviewCliCommand") && html.contains("fetch('/cli'"),
        "unknown slash commands must be executable through the WebView CLI bridge"
    );
}

#[test]
fn console_html_has_sprint2_composer_hooks() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    assert!(
        html.contains("<textarea id=\"taskInput\""),
        "composer must use a textarea for multi-line Shift+Enter input"
    );
    for marker in [
        "loadHistoryCache",
        "fetch('/history?limit=80')",
        "composerKeydown",
        "sparrow-composer-draft",
        "composerPaste",
        "dropZone",
        "attachFiles",
        "fetch('/upload'",
        "MAX_ATTACHMENT_BYTES",
    ] {
        assert!(
            html.contains(marker),
            "console.html must expose Sprint 2 composer hook `{}`",
            marker
        );
    }
}

#[test]
fn console_html_has_sprint3_micro_animation_hooks() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    for marker in [
        "approvalModal",
        "showApprovalModal",
        "resolveApprovalFromModal",
        "error-banner",
        "showError",
        "checkpoint-timeline",
        "addCheckpointNode",
        "prefers-reduced-motion: reduce",
        "fold-in",
    ] {
        assert!(
            html.contains(marker),
            "console.html must expose Sprint 3 hook `{}`",
            marker
        );
    }
}

#[test]
fn console_html_matches_v0_3_visual_polish_contract() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    for marker in [
        "v0.3.6",
        "get('boot')==='0'",
        ".boot-overlay[hidden]",
        "route-step",
        "class=\"arrow\"",
        "drawer-in",
        "drw-row.cur",
        "_none yet_ · unable to load /sessions",
        ":root[data-theme=\"paper\"] .context-track",
    ] {
        assert!(
            html.contains(marker),
            "console.html must keep v0.3 polish marker `{}`",
            marker
        );
    }
}

#[test]
fn console_html_has_typed_event_renderers() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    // Sprint 1 inc 6: every Event variant of v0.2.0 that the mockup
    // promises has a styled renderer must actually have one in the
    // shipping console.
    for needle in [
        ".tool-card",
        ".diff-card",
        ".compact-banner",
        ".skill-pop",
        ".streaming::after",
    ] {
        assert!(
            html.contains(needle),
            "console.html must define the `{}` renderer block",
            needle
        );
    }
    // And the JS dispatch must route the Event types into those renderers.
    for case in [
        "case 'ToolUseProposed'",
        "case 'ToolOutput'",
        "case 'DiffProposed'",
        "case 'Compacted'",
    ] {
        assert!(
            html.contains(case),
            "handleEvent() must wire `{}` into the new renderers",
            case
        );
    }
    assert!(
        html.contains("renderDiffCard") && html.contains("renderCompactBanner"),
        "diff + compact renderers must be implemented"
    );
}

#[test]
fn console_html_plan_mode_has_explicit_accept_edit_reject() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    for marker in [
        "function renderPlan",
        "data-plan-action=\"run\"",
        "data-plan-action=\"edit\"",
        "data-plan-action=\"reject\"",
        "function rejectPlan",
        "plan rejected",
    ] {
        assert!(html.contains(marker), "plan mode must expose `{}`", marker);
    }
}

#[test]
fn console_html_diff_view_exposes_per_hunk_controls() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    for marker in [
        "function parseDiffHunks",
        "function renderHunkedDiff",
        "function bindHunkControls",
        "data-hunk-action=\"accept\"",
        "data-hunk-action=\"reject\"",
        "data-state=\"pending\"",
    ] {
        assert!(
            html.contains(marker),
            "diff view must expose per-hunk marker `{}`",
            marker
        );
    }
}

#[test]
fn console_html_styles_streamed_code_cards() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    assert!(
        html.contains("d.className='code-card streaming-code'"),
        "streaming fenced code must render through the code-card component"
    );
    for marker in [
        ".code-card,.code-block",
        ".code-card summary",
        ".code-card summary::-webkit-details-marker",
        ".code-card .cc-copy",
        ".code-card pre",
        ".code-card code",
        ".syn-key",
        "function highlightCode",
        "applyCodeHighlight(card)",
        "if(!raw.trim())",
    ] {
        assert!(
            html.contains(marker),
            "console.html must style code card marker `{}`",
            marker
        );
    }
}

#[test]
fn console_html_keeps_cost_updates_out_of_the_transcript() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    let cost_case = html
        .split("case 'CostUpdate':")
        .nth(1)
        .expect("CostUpdate handler must exist")
        .split("case 'TokenUsageEstimated':")
        .next()
        .expect("CostUpdate handler must be followed by TokenUsageEstimated");
    assert!(
        cost_case.contains("setCost(ev.usd)") && !cost_case.contains("verboseLine"),
        "CostUpdate must update meters only, without adding transcript spam: {}",
        cost_case
    );
}

#[test]
fn console_html_uses_system_ui_typography() {
    let html =
        std::fs::read_to_string("console.html").expect("console.html must ship with the WebView");
    for marker in [
        "--ui-font:-apple-system",
        "--mono-font:\"SF Mono\"",
        "body{font-family:var(--ui-font)",
        ".code-card code,.code-block .cb-body code{font-family:var(--mono-font)",
    ] {
        assert!(
            html.contains(marker),
            "console.html must keep Apple-style typography marker `{}`",
            marker
        );
    }
}

#[test]
fn classify_agent_color_falls_back_to_steel() {
    use sparrow::console::classify_agent_color;
    assert_eq!(classify_agent_color("blue"), "planner");
    assert_eq!(classify_agent_color("TEAL"), "coder");
    assert_eq!(classify_agent_color("gold"), "gold");
    assert_eq!(classify_agent_color("coral"), "coral");
    assert_eq!(classify_agent_color("something-unknown"), "steel");
    assert_eq!(classify_agent_color(""), "steel");
}

#[tokio::test]
async fn webview_app_builds_with_phase13_routes() {
    // Spin up the server on a free port, then shut it down — proves all
    // routes (sessions, artifacts, upload, security, memory, plugins, tools,
    // permissions, commands) compile and bind without panicking.
    use std::net::SocketAddr;
    use tokio::sync::broadcast;

    let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
    let (tx, _rx) = broadcast::channel(16);
    let server = sparrow::console::WebViewServer::new(addr, tx, None, None, None, None, None, None);
    // Just verify the constructor accepts the expected shape — actually
    // binding requires a tokio TcpListener which we skip to keep the test
    // hermetic and fast on all platforms.
    drop(server);
}