concinnity-dev 0.19.23

The Concinnity dev tooling library: world authoring, the in-engine editor, the debug server, docs and packaging
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// src/editor/console.rs
//
// The Console panel's core: the bounded log ring, the shared sink other editor
// code (and worker threads) push lines into, a tracing layer mirroring this
// crate's events into that sink, the slash-command parser, and the /del name
// autocomplete matcher. Everything here is pure or lock-guarded state; the
// panel layout lives in `console_panel.rs` and the actions in
// `hook/console_edit.rs`.

use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

// Retained log lines; older lines fall off the front.
pub(crate) const LOG_CAP: usize = 300;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Severity {
    Info,
    Warn,
    Error,
    // An echoed input line (drawn dimmer, prefixed "> ").
    Command,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ConsoleLine {
    pub severity: Severity,
    pub text: String,
}

// The bounded log ring. Multi-line pushes split into one entry per line so the
// window math stays line-based.
#[derive(Debug)]
pub(crate) struct ConsoleLog {
    lines: VecDeque<ConsoleLine>,
    cap: usize,
}

impl ConsoleLog {
    pub(crate) fn new(cap: usize) -> Self {
        Self {
            lines: VecDeque::new(),
            cap: cap.max(1),
        }
    }

    pub(crate) fn push(&mut self, severity: Severity, text: &str) {
        for line in text.lines() {
            if self.lines.len() == self.cap {
                self.lines.pop_front();
            }
            self.lines.push_back(ConsoleLine {
                severity,
                text: line.to_string(),
            });
        }
    }

    pub(crate) fn len(&self) -> usize {
        self.lines.len()
    }

    // Clone the `count` lines starting at `first` (short at the tail).
    pub(crate) fn window(&self, first: usize, count: usize) -> Vec<ConsoleLine> {
        self.lines.iter().skip(first).take(count).cloned().collect()
    }
}

// The shared handle to the log: cloned into worker threads and the tracing
// layer, and read by the panel each frame. A poisoned lock drops the line
// rather than panicking the frame loop.
#[derive(Clone)]
pub(crate) struct ConsoleSink(Arc<Mutex<ConsoleLog>>);

impl Default for ConsoleSink {
    fn default() -> Self {
        Self(Arc::new(Mutex::new(ConsoleLog::new(LOG_CAP))))
    }
}

impl ConsoleSink {
    pub(crate) fn push(&self, severity: Severity, text: &str) {
        if let Ok(mut log) = self.0.lock() {
            log.push(severity, text);
        }
    }

    // Never blocks: the tracing layer may fire from any thread mid-frame, and
    // losing a mirrored line beats stalling whoever holds the lock.
    fn push_nonblocking(&self, severity: Severity, text: &str) {
        if let Ok(mut log) = self.0.try_lock() {
            log.push(severity, text);
        }
    }

    pub(crate) fn info(&self, text: &str) {
        self.push(Severity::Info, text);
    }
    pub(crate) fn warn(&self, text: &str) {
        self.push(Severity::Warn, text);
    }
    pub(crate) fn error(&self, text: &str) {
        self.push(Severity::Error, text);
    }

    pub(crate) fn len(&self) -> usize {
        self.0.lock().map(|log| log.len()).unwrap_or(0)
    }

    pub(crate) fn window(&self, first: usize, count: usize) -> Vec<ConsoleLine> {
        self.0
            .lock()
            .map(|log| log.window(first, count))
            .unwrap_or_default()
    }
}

// A tracing layer mirroring this crate's events into the sink, so editor
// logging (save failures, live-preview rebuild errors, rm notices) shows in
// the console without each site pushing explicitly. Filtered to the editor
// crate and INFO+ at install (`install_tracing`); bounded by the ring and
// non-blocking by `push_nonblocking`.
struct ConsoleTracingLayer {
    sink: ConsoleSink,
}

struct MessageVisitor(String);

impl tracing::field::Visit for MessageVisitor {
    fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
        if field.name() == "message" {
            use std::fmt::Write;
            let _ = write!(self.0, "{value:?}");
        }
    }
}

impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for ConsoleTracingLayer {
    fn on_event(
        &self,
        event: &tracing::Event<'_>,
        _ctx: tracing_subscriber::layer::Context<'_, S>,
    ) {
        let mut visitor = MessageVisitor(String::new());
        event.record(&mut visitor);
        if visitor.0.is_empty() {
            return;
        }
        let severity = match *event.metadata().level() {
            tracing::Level::ERROR => Severity::Error,
            tracing::Level::WARN => Severity::Warn,
            _ => Severity::Info,
        };
        self.sink.push_nonblocking(severity, &visitor.0);
    }
}

// Install the editor's global tracing subscriber: the usual stderr formatter
// (same RUST_LOG handling as the engine's `init_logging`) plus the console
// mirror for this crate's INFO+ events. Replaces the `init_logging` call in
// the editor entry point; a no-op if a subscriber is already installed.
pub(crate) fn install_tracing(sink: ConsoleSink) {
    use tracing_subscriber::layer::SubscriberExt;
    use tracing_subscriber::util::SubscriberInitExt;
    use tracing_subscriber::{EnvFilter, Layer};

    let default = if cfg!(debug_assertions) {
        "info"
    } else {
        "warn"
    };
    let fmt = tracing_subscriber::fmt::layer()
        .with_filter(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default)));
    let mirror =
        ConsoleTracingLayer { sink }.with_filter(tracing_subscriber::filter::filter_fn(|meta| {
            meta.target().starts_with("concinnity_dev") && *meta.level() <= tracing::Level::INFO
        }));
    let _ = tracing_subscriber::registry()
        .with(fmt)
        .with(mirror)
        .with(concinnity_engine::crash::RingLayer)
        .try_init();
}

// A parsed console input line. (`PartialEq` only: /snap carries a float step.)
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Command {
    // A line without a leading '/': just echoed to the log.
    Echo(String),
    Add {
        target: String,
        name: Option<String>,
    },
    Del {
        name: String,
    },
    // Compile the current world's blobs (named "cook" in user-facing text).
    Cook,
    Snap(SnapCmd),
    // Duplicate the selection / drop it onto the surface below.
    Dup,
    Floor,
    // Grow the selection by relationship.
    Select(SelectCmd),
    // Export a skinned mesh as .glb beside the project. No name: the selected
    // entry. `bake` folds the current CharacterShape in instead of targets.
    Export {
        name: Option<String>,
        bake: bool,
    },
    Help,
}

// A /select relationship.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum SelectCmd {
    // Everything in the active member's origin group (the outliner grouping).
    Origin,
    // Everything referencing the named asset.
    Using(String),
    // Everything of the named type.
    Type(String),
}

// A /snap adjustment.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum SnapCmd {
    // Bare /snap: report the current settings.
    Status,
    // /snap on|off: both families at once.
    All(bool),
    Move(SnapSet),
    Rotate(SnapSet),
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum SnapSet {
    Enable(bool),
    // Setting a step also enables the family.
    Step(f32),
}

// One registered slash command: its /help row plus its argument parser.
// Adding a command is one entry here (plus its `Command` variant and dispatch
// arm in `hook/console_edit.rs`).
pub(crate) struct CommandSpec {
    pub name: &'static str,
    pub usage: &'static str,
    pub blurb: &'static str,
    parse: fn(&str) -> Result<Command, String>,
}

pub(crate) const COMMANDS: &[CommandSpec] = &[
    CommandSpec {
        name: "add",
        usage: "/add <target> [name]",
        blurb: "add an asset: a file path, a type name, or inline JSON",
        parse: parse_add,
    },
    CommandSpec {
        name: "del",
        usage: "/del <name>",
        blurb: "remove an authored asset by name",
        parse: parse_del,
    },
    CommandSpec {
        name: "cook",
        usage: "/cook",
        blurb: "compile the current world's blobs",
        parse: parse_cook,
    },
    CommandSpec {
        name: "snap",
        usage: "/snap [move|rot] [on|off|<step>]",
        blurb: "gizmo grid / angle snapping (bare /snap shows the settings)",
        parse: parse_snap,
    },
    CommandSpec {
        name: "dup",
        usage: "/dup",
        blurb: "duplicate the selection in place (Ctrl+D)",
        parse: parse_dup,
    },
    CommandSpec {
        name: "floor",
        usage: "/floor",
        blurb: "drop the selection onto the surface below it (Ctrl+Down)",
        parse: parse_floor,
    },
    CommandSpec {
        name: "select",
        usage: "/select origin | using <asset> | type <Type>",
        blurb: "select by relationship: shared origin, references, or type",
        parse: parse_select,
    },
    CommandSpec {
        name: "export",
        usage: "/export [name] [bake]",
        blurb: "write a skinned mesh (or the selection) as .glb beside the project",
        parse: parse_export,
    },
    CommandSpec {
        name: "help",
        usage: "/help",
        blurb: "list commands",
        parse: parse_help,
    },
];

fn parse_add(rest: &str) -> Result<Command, String> {
    if rest.is_empty() {
        return Err("usage: /add <target> [name]".to_string());
    }
    // Inline JSON contains spaces; the whole remainder is the target.
    if rest.starts_with('{') {
        return Ok(Command::Add {
            target: rest.to_string(),
            name: None,
        });
    }
    let mut words = rest.split_whitespace();
    let target = words.next().unwrap_or_default().to_string();
    let name = words.next().map(String::from);
    if words.next().is_some() {
        return Err("too many arguments; usage: /add <target> [name]".to_string());
    }
    Ok(Command::Add { target, name })
}

fn parse_del(rest: &str) -> Result<Command, String> {
    let mut words = rest.split_whitespace();
    match (words.next(), words.next()) {
        (Some(name), None) => Ok(Command::Del {
            name: name.to_string(),
        }),
        _ => Err("usage: /del <name>".to_string()),
    }
}

fn parse_cook(rest: &str) -> Result<Command, String> {
    if rest.is_empty() {
        Ok(Command::Cook)
    } else {
        Err("usage: /cook".to_string())
    }
}

const SNAP_USAGE: &str = "usage: /snap [move|rot] [on|off|<step>]";

// The shared on|off|<step> tail of a /snap form.
fn parse_snap_set(word: &str) -> Result<SnapSet, String> {
    match word {
        "on" => Ok(SnapSet::Enable(true)),
        "off" => Ok(SnapSet::Enable(false)),
        _ => {
            let step: f32 = word.parse().map_err(|_| SNAP_USAGE.to_string())?;
            if step > 0.0 && step.is_finite() {
                Ok(SnapSet::Step(step))
            } else {
                Err("snap step must be a positive number".to_string())
            }
        }
    }
}

fn parse_snap(rest: &str) -> Result<Command, String> {
    let mut words = rest.split_whitespace();
    let cmd = match (words.next(), words.next()) {
        (None, _) => SnapCmd::Status,
        (Some("on"), None) => SnapCmd::All(true),
        (Some("off"), None) => SnapCmd::All(false),
        (Some("move"), Some(w)) => SnapCmd::Move(parse_snap_set(w)?),
        (Some("rot"), Some(w)) => SnapCmd::Rotate(parse_snap_set(w)?),
        // A bare number is the common case: the move grid step.
        (Some(w), None) => match parse_snap_set(w)? {
            SnapSet::Step(s) => SnapCmd::Move(SnapSet::Step(s)),
            SnapSet::Enable(_) => return Err(SNAP_USAGE.to_string()),
        },
        _ => return Err(SNAP_USAGE.to_string()),
    };
    if words.next().is_some() {
        return Err(SNAP_USAGE.to_string());
    }
    Ok(Command::Snap(cmd))
}

fn parse_dup(rest: &str) -> Result<Command, String> {
    if rest.is_empty() {
        Ok(Command::Dup)
    } else {
        Err("usage: /dup".to_string())
    }
}

fn parse_floor(rest: &str) -> Result<Command, String> {
    if rest.is_empty() {
        Ok(Command::Floor)
    } else {
        Err("usage: /floor".to_string())
    }
}

const SELECT_USAGE: &str = "usage: /select origin | using <asset> | type <Type>";

fn parse_select(rest: &str) -> Result<Command, String> {
    let mut words = rest.split_whitespace();
    let cmd = match (words.next(), words.next()) {
        (Some("origin"), None) => SelectCmd::Origin,
        (Some("using"), Some(name)) => SelectCmd::Using(name.to_string()),
        (Some("type"), Some(ty)) => SelectCmd::Type(ty.to_string()),
        _ => return Err(SELECT_USAGE.to_string()),
    };
    if words.next().is_some() {
        return Err(SELECT_USAGE.to_string());
    }
    Ok(Command::Select(cmd))
}

const EXPORT_USAGE: &str = "usage: /export [name] [bake]";

fn parse_export(rest: &str) -> Result<Command, String> {
    let mut name = None;
    let mut bake = false;
    for word in rest.split_whitespace() {
        match word {
            "bake" if !bake => bake = true,
            _ if name.is_none() && !bake => name = Some(word.to_string()),
            _ => return Err(EXPORT_USAGE.to_string()),
        }
    }
    Ok(Command::Export { name, bake })
}

fn parse_help(rest: &str) -> Result<Command, String> {
    if rest.is_empty() {
        Ok(Command::Help)
    } else {
        Err("usage: /help".to_string())
    }
}

// Parse one submitted input line. A line without a leading '/' echoes; an
// unknown or malformed command errors with its usage.
pub(crate) fn parse_command(line: &str) -> Result<Command, String> {
    let line = line.trim();
    let Some(rest) = line.strip_prefix('/') else {
        return Ok(Command::Echo(line.to_string()));
    };
    let (name, args) = rest.split_once(char::is_whitespace).unwrap_or((rest, ""));
    let Some(spec) = COMMANDS.iter().find(|c| c.name == name) else {
        return Err(format!("unknown command /{name}; try /help"));
    };
    (spec.parse)(args.trim())
}

// The /help body, one row per registered command.
pub(crate) fn help_lines() -> Vec<String> {
    let width = COMMANDS.iter().map(|c| c.usage.len()).max().unwrap_or(0);
    let mut lines: Vec<String> = COMMANDS
        .iter()
        .map(|c| format!("{:width$}  {}", c.usage, c.blurb))
        .collect();
    lines.push("a line without / is echoed to the log".to_string());
    lines
}

// The best completion of `prefix` among `names`: the shortest strictly longer
// prefix match, ties broken lexicographically so the pick is deterministic.
pub(crate) fn completion<'a>(
    prefix: &str,
    names: impl IntoIterator<Item = &'a str>,
) -> Option<&'a str> {
    if prefix.is_empty() {
        return None;
    }
    names
        .into_iter()
        .filter(|n| n.starts_with(prefix) && n.len() > prefix.len())
        .min_by_key(|n| (n.len(), *n))
}

// The inline ghost suffix for a partially typed `/del <name>`, or `None` when
// the input is any other shape (ghost only ever completes the name argument).
pub(crate) fn del_ghost<'a>(
    input: &str,
    names: impl IntoIterator<Item = &'a str>,
) -> Option<String> {
    let rest = input.strip_prefix("/del")?;
    let prefix = rest.strip_prefix(' ')?.trim_start();
    if prefix.is_empty() || prefix.contains(char::is_whitespace) {
        return None;
    }
    completion(prefix, names).map(|full| full[prefix.len()..].to_string())
}

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

    #[test]
    fn log_ring_drops_the_oldest_past_the_cap() {
        let mut log = ConsoleLog::new(3);
        for i in 0..5 {
            log.push(Severity::Info, &format!("line {i}"));
        }
        assert_eq!(log.len(), 3);
        let lines = log.window(0, 10);
        let texts: Vec<&str> = lines.iter().map(|l| l.text.as_str()).collect();
        assert_eq!(texts, ["line 2", "line 3", "line 4"]);
    }

    #[test]
    fn multi_line_push_splits_and_empty_push_adds_nothing() {
        let mut log = ConsoleLog::new(10);
        log.push(Severity::Error, "first\nsecond");
        log.push(Severity::Info, "");
        assert_eq!(log.len(), 2);
        let lines = log.window(0, 10);
        assert_eq!(lines[0].text, "first");
        assert_eq!(lines[1].text, "second");
        assert_eq!(lines[1].severity, Severity::Error);
    }

    #[test]
    fn window_clips_to_the_tail() {
        let mut log = ConsoleLog::new(10);
        for i in 0..4 {
            log.push(Severity::Info, &format!("{i}"));
        }
        let lines = log.window(2, 5);
        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0].text, "2");
        assert!(log.window(9, 5).is_empty());
    }

    #[test]
    fn sink_is_shared_across_clones() {
        let sink = ConsoleSink::default();
        let clone = sink.clone();
        clone.info("from a worker");
        assert_eq!(sink.len(), 1);
        assert_eq!(sink.window(0, 1)[0].text, "from a worker");
    }

    #[test]
    fn bare_lines_echo() {
        assert_eq!(
            parse_command("hello world"),
            Ok(Command::Echo("hello world".to_string()))
        );
    }

    #[test]
    fn add_parses_target_optional_name_and_inline_json() {
        assert_eq!(
            parse_command("/add Logger"),
            Ok(Command::Add {
                target: "Logger".to_string(),
                name: None,
            })
        );
        assert_eq!(
            parse_command("/add models/scene.glb hall"),
            Ok(Command::Add {
                target: "models/scene.glb".to_string(),
                name: Some("hall".to_string()),
            })
        );
        let json = r#"{"type": "Logger", "name": "log"}"#;
        assert_eq!(
            parse_command(&format!("/add {json}")),
            Ok(Command::Add {
                target: json.to_string(),
                name: None,
            })
        );
        assert!(parse_command("/add").is_err());
        assert!(parse_command("/add a b c").is_err());
    }

    #[test]
    fn del_takes_exactly_one_name() {
        assert_eq!(
            parse_command("/del cube"),
            Ok(Command::Del {
                name: "cube".to_string(),
            })
        );
        assert!(parse_command("/del").is_err());
        assert!(parse_command("/del a b").is_err());
    }

    #[test]
    fn cook_and_help_take_no_arguments() {
        assert_eq!(parse_command("/cook"), Ok(Command::Cook));
        assert_eq!(parse_command("/help"), Ok(Command::Help));
        assert!(parse_command("/cook now").is_err());
        assert!(parse_command("/help me").is_err());
    }

    #[test]
    fn snap_parses_status_toggles_and_steps() {
        assert_eq!(parse_command("/snap"), Ok(Command::Snap(SnapCmd::Status)));
        assert_eq!(
            parse_command("/snap on"),
            Ok(Command::Snap(SnapCmd::All(true)))
        );
        assert_eq!(
            parse_command("/snap off"),
            Ok(Command::Snap(SnapCmd::All(false)))
        );
        assert_eq!(
            parse_command("/snap 0.5"),
            Ok(Command::Snap(SnapCmd::Move(SnapSet::Step(0.5)))),
            "a bare number is the move grid step"
        );
        assert_eq!(
            parse_command("/snap move off"),
            Ok(Command::Snap(SnapCmd::Move(SnapSet::Enable(false))))
        );
        assert_eq!(
            parse_command("/snap rot 15"),
            Ok(Command::Snap(SnapCmd::Rotate(SnapSet::Step(15.0))))
        );
        assert_eq!(
            parse_command("/snap rot on"),
            Ok(Command::Snap(SnapCmd::Rotate(SnapSet::Enable(true))))
        );
    }

    #[test]
    fn snap_rejects_bad_steps_and_extra_words() {
        assert!(parse_command("/snap 0").is_err());
        assert!(parse_command("/snap -1").is_err());
        assert!(parse_command("/snap nan").is_err());
        assert!(parse_command("/snap rot").is_err());
        assert!(parse_command("/snap move 0.5 1").is_err());
        assert!(parse_command("/snap sideways 3").is_err());
    }

    #[test]
    fn export_parses_its_optional_name_and_bake_flag() {
        assert_eq!(
            parse_command("/export"),
            Ok(Command::Export {
                name: None,
                bake: false
            })
        );
        assert_eq!(
            parse_command("/export body"),
            Ok(Command::Export {
                name: Some("body".to_string()),
                bake: false
            })
        );
        assert_eq!(
            parse_command("/export body bake"),
            Ok(Command::Export {
                name: Some("body".to_string()),
                bake: true
            })
        );
        assert_eq!(
            parse_command("/export bake"),
            Ok(Command::Export {
                name: None,
                bake: true
            })
        );
        assert!(parse_command("/export a b").is_err());
        assert!(parse_command("/export bake body").is_err());
    }

    #[test]
    fn unknown_commands_point_at_help() {
        let err = parse_command("/frobnicate").unwrap_err();
        assert!(err.contains("/frobnicate"), "got: {err}");
        assert!(err.contains("/help"), "got: {err}");
    }

    #[test]
    fn help_covers_every_registered_command() {
        let help = help_lines().join("\n");
        for spec in COMMANDS {
            assert!(help.contains(spec.usage), "missing {}", spec.usage);
        }
    }

    #[test]
    fn completion_prefers_the_shortest_then_lexicographic_match() {
        let names = ["cube_red", "cube", "cube_blue", "wall"];
        assert_eq!(completion("cu", names), Some("cube"));
        assert_eq!(
            completion("cube_", names),
            Some("cube_red"),
            "shorter beats lexicographically-earlier"
        );
        assert_eq!(completion("wal", names), Some("wall"));
        assert_eq!(completion("x", names), None);
        assert_eq!(completion("", names), None, "empty prefix never completes");
        assert_eq!(
            completion("cube", ["cube"]),
            None,
            "an exact match has nothing left to complete"
        );
    }

    #[test]
    fn del_ghost_completes_only_the_del_name_argument() {
        let names = ["cube_red", "wall"];
        assert_eq!(del_ghost("/del cu", names), Some("be_red".to_string()));
        assert_eq!(del_ghost("/del cube_red", names), None);
        assert_eq!(del_ghost("/del ", names), None);
        assert_eq!(del_ghost("/del a b", names), None);
        assert_eq!(del_ghost("/add cu", names), None);
        assert_eq!(del_ghost("cu", names), None);
    }
}