codewhale-tui 0.9.8

Terminal UI for open-source and open-weight coding models
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
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
//! Focused Gherkin acceptance evidence for FEAT-011 dispatch precedence and
//! error semantics. Bound through separate scenario-level cucumber worlds
//! that prove AT-004 through AT-007 with the live dispatch entry point.

use cucumber::{World as _, given, then, when, writer::Stats as _};
use tempfile::TempDir;

use crate::commands::{self, CommandResult};
use crate::config::Config;
use crate::tui::app::{App, TuiOptions};

// --- FEAT-011 dispatch precedence constants ---

const DISPATCH_FEATURE_NAME: &str = "FEAT-011 Dispatch Precedence And Error Semantics";
const DISPATCH_FEATURE_PATH: &str = concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/tests/features/feat-011-dispatch-precedence.feature"
);

const AT004_SCENARIO: &str = "AT-004 User command shadows built-in canonical name";
const AT005_SCENARIO: &str = "AT-005 User command shadows built-in alias";
const AT006_SCENARIO: &str = "AT-006 Absent user command falls back to built-in";
const AT007_SCENARIO: &str = "AT-007 Invalid user command produces user error without fallback";

// --- Shared helpers ---

fn create_dispatch_app(tmpdir: &TempDir) -> App {
    let options = TuiOptions {
        skills_dir: tmpdir.path().join("skills"),
        memory_path: tmpdir.path().join("memory.md"),
        notes_path: tmpdir.path().join("notes.txt"),
        mcp_config_path: tmpdir.path().join("mcp.json"),
        ..crate::test_support::test_tui_options(tmpdir.path())
    };
    App::new(options, &Config::default())
}

fn write_user_command(tmpdir: &TempDir, name: &str, content: &str) {
    let commands_dir = tmpdir.path().join(".codewhale").join("commands");
    std::fs::create_dir_all(commands_dir).expect("create commands dir");
    let path = tmpdir
        .path()
        .join(".codewhale")
        .join("commands")
        .join(format!("{name}.md"));
    std::fs::write(path, content).expect("write user command");
}

fn sent_message(result: &CommandResult) -> String {
    match &result.action {
        Some(crate::tui::app::AppAction::SendMessage(message)) => message.clone(),
        other => panic!("expected SendMessage action, got {other:?}"),
    }
}

// --- AT-004: User command shadows built-in canonical name ---

#[derive(cucumber::World)]
#[world(init = Self::new)]
struct DispatchWorld004 {
    tmpdir: Option<TempDir>,
    app: Option<Box<App>>,
    result: Option<CommandResult>,
}

impl std::fmt::Debug for DispatchWorld004 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DispatchWorld004")
            .field("has_tmpdir", &self.tmpdir.is_some())
            .field("has_app", &self.app.is_some())
            .field("has_result", &self.result.is_some())
            .finish()
    }
}

impl DispatchWorld004 {
    fn new() -> Self {
        Self {
            tmpdir: None,
            app: None,
            result: None,
        }
    }
}

#[given("a workspace with a user command shadowing a built-in canonical name")]
fn at004_given_shadow_canonical(world: &mut DispatchWorld004) {
    let tmpdir = TempDir::new().expect("AT-004 TempDir");
    write_user_command(
        &tmpdir,
        "help",
        "---\ndescription: Custom help\n---\ncustom help $ARGUMENTS",
    );
    let mut app = create_dispatch_app(&tmpdir);
    app.workspace = tmpdir.path().to_path_buf();
    commands::user_registry::reload(Some(tmpdir.path()));
    world.tmpdir = Some(tmpdir);
    world.app = Some(Box::new(app));
}

#[when(regex = r#"^the user runs "/help config"$"#)]
fn at004_when_run_shadowed(world: &mut DispatchWorld004) {
    let app = world.app.as_deref_mut().expect("app should exist");
    let result = commands::execute("/help config", app);
    world.result = Some(result);
}

#[then("the user command executes instead of the built-in")]
fn at004_then_user_executes(world: &mut DispatchWorld004) {
    let result = world.result.as_ref().expect("result should exist");
    assert!(
        !result.is_error,
        "user command should succeed: {:?}",
        result.message
    );
    assert_eq!(
        sent_message(result),
        "custom help config",
        "user command should produce custom content"
    );
}

#[then("no built-in /help side effect occurs")]
fn at004_then_no_builtin(world: &mut DispatchWorld004) {
    let result = world.result.as_ref().expect("result should exist");
    assert!(!result.is_error, "no error");
    match &result.action {
        Some(crate::tui::app::AppAction::SendMessage(message)) => {
            assert!(
                message.contains("custom help"),
                "message should contain user command content: {message}"
            );
        }
        other => panic!("expected SendMessage, got {other:?}"),
    }
}

#[tokio::test(flavor = "current_thread")]
async fn feat011_at004_user_command_shadows_builtin_canonical_name() {
    let writer = DispatchWorld004::cucumber()
        .fail_on_skipped()
        .with_default_cli()
        .filter_run(DISPATCH_FEATURE_PATH, move |feature, _, scenario| {
            feature.name == DISPATCH_FEATURE_NAME && scenario.name == AT004_SCENARIO
        })
        .await;
    assert_eq!(
        writer.failed_steps(),
        0,
        "scenario failed: {AT004_SCENARIO}"
    );
    assert_eq!(
        writer.skipped_steps(),
        0,
        "scenario skipped steps: {AT004_SCENARIO}"
    );
    assert_eq!(
        writer.passed_steps(),
        4,
        "scenario did not run: {AT004_SCENARIO}"
    );
}

// --- AT-005: User command shadows built-in alias ---

#[derive(cucumber::World)]
#[world(init = Self::new)]
struct DispatchWorld005 {
    tmpdir: Option<TempDir>,
    app: Option<Box<App>>,
    alias_result: Option<CommandResult>,
    canonical_result: Option<CommandResult>,
}

impl std::fmt::Debug for DispatchWorld005 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DispatchWorld005")
            .field("has_tmpdir", &self.tmpdir.is_some())
            .field("has_app", &self.app.is_some())
            .field("has_alias_result", &self.alias_result.is_some())
            .field("has_canonical_result", &self.canonical_result.is_some())
            .finish()
    }
}

impl DispatchWorld005 {
    fn new() -> Self {
        Self {
            tmpdir: None,
            app: None,
            alias_result: None,
            canonical_result: None,
        }
    }
}

#[given("a workspace with a user command shadowing a built-in alias")]
fn at005_given_shadow_alias(world: &mut DispatchWorld005) {
    let tmpdir = TempDir::new().expect("AT-005 TempDir");
    // /links has alias /dashboard and /api. Create a user command that
    // shadows the /dashboard alias.
    write_user_command(
        &tmpdir,
        "attach-review",
        "---\nalias: dashboard\n---\ncustom dashboard $ARGUMENTS",
    );
    let mut app = create_dispatch_app(&tmpdir);
    app.workspace = tmpdir.path().to_path_buf();
    commands::user_registry::reload(Some(tmpdir.path()));
    world.tmpdir = Some(tmpdir);
    world.app = Some(Box::new(app));
}

#[when("the user runs the shadowed alias")]
fn at005_when_run_alias(world: &mut DispatchWorld005) {
    let app = world.app.as_deref_mut().expect("app should exist");
    // Use /dashboard which is shadowed by the user command's alias.
    let alias_result = commands::execute("/dashboard", app);
    world.alias_result = Some(alias_result);

    // Also test that the built-in canonical name (/links) still works.
    let canonical_result = commands::execute("/links", app);
    world.canonical_result = Some(canonical_result);
}

#[then("the user command executes")]
fn at005_then_user_executes(world: &mut DispatchWorld005) {
    let result = world
        .alias_result
        .as_ref()
        .expect("alias result should exist");
    assert!(!result.is_error, "user command dispatch should succeed");
    assert_eq!(
        sent_message(result),
        "custom dashboard ",
        "user alias should produce custom content"
    );
}

#[then("the built-in canonical name remains reachable")]
fn at005_then_canonical_reachable(world: &mut DispatchWorld005) {
    let result = world
        .canonical_result
        .as_ref()
        .expect("canonical result should exist");
    assert!(!result.is_error, "canonical built-in should still work");
    assert!(
        result
            .message
            .as_deref()
            .is_some_and(|msg| msg.contains("https://")),
        "canonical /links should return platform links: {:?}",
        result.message
    );
}

#[tokio::test(flavor = "current_thread")]
async fn feat011_at005_user_command_shadows_builtin_alias() {
    let writer = DispatchWorld005::cucumber()
        .fail_on_skipped()
        .with_default_cli()
        .filter_run(DISPATCH_FEATURE_PATH, move |feature, _, scenario| {
            feature.name == DISPATCH_FEATURE_NAME && scenario.name == AT005_SCENARIO
        })
        .await;
    assert_eq!(
        writer.failed_steps(),
        0,
        "scenario failed: {AT005_SCENARIO}"
    );
    assert_eq!(
        writer.skipped_steps(),
        0,
        "scenario skipped steps: {AT005_SCENARIO}"
    );
    assert_eq!(
        writer.passed_steps(),
        4,
        "scenario did not run: {AT005_SCENARIO}"
    );
}

// --- AT-006: Absent user command falls back to built-in ---

#[derive(cucumber::World)]
#[world(init = Self::new)]
struct DispatchWorld006 {
    tmpdir: Option<TempDir>,
    app: Option<Box<App>>,
    after_removal_result: Option<CommandResult>,
}

impl std::fmt::Debug for DispatchWorld006 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DispatchWorld006")
            .field("has_tmpdir", &self.tmpdir.is_some())
            .field("has_app", &self.app.is_some())
            .field("has_result", &self.after_removal_result.is_some())
            .finish()
    }
}

impl DispatchWorld006 {
    fn new() -> Self {
        Self {
            tmpdir: None,
            app: None,
            after_removal_result: None,
        }
    }
}

#[given("a workspace with a previously loaded user command")]
fn at006_given_loaded_user_command(world: &mut DispatchWorld006) {
    let tmpdir = TempDir::new().expect("AT-006 TempDir");
    write_user_command(&tmpdir, "help", "user help");
    let mut app = create_dispatch_app(&tmpdir);
    app.workspace = tmpdir.path().to_path_buf();
    commands::user_registry::reload(Some(tmpdir.path()));

    // Verify user command dispatches first
    let initial_result = commands::execute("/help config", &mut app);
    assert!(
        matches!(
            &initial_result.action,
            Some(crate::tui::app::AppAction::SendMessage(_))
        ),
        "user command should dispatch initially"
    );

    world.tmpdir = Some(tmpdir);
    world.app = Some(Box::new(app));
}

#[when("the user command file is removed and the command is invoked again")]
fn at006_when_removed_and_invoked(world: &mut DispatchWorld006) {
    let app = world.app.as_deref_mut().expect("app should exist");
    let tmpdir = world.tmpdir.as_ref().expect("tmpdir should exist");
    let command_path = tmpdir
        .path()
        .join(".codewhale")
        .join("commands")
        .join("help.md");

    // Remove the user command file
    std::fs::remove_file(&command_path).expect("remove user command file");
    commands::user_registry::reload(Some(tmpdir.path()));

    // Invoke the (now absent) command — should fall back to built-in
    let result = commands::execute("/help config", app);
    world.after_removal_result = Some(result);
}

#[then("the built-in command executes without a user-command error message")]
fn at006_then_builtin_executes(world: &mut DispatchWorld006) {
    let result = world
        .after_removal_result
        .as_ref()
        .expect("result should exist");
    assert!(!result.is_error, "built-in fallback should not error");
    let message = result.message.as_deref().unwrap_or("");
    // The built-in /help config message should mention the config command.
    assert!(
        message.contains("config"),
        "built-in /help should handle the command: {message}"
    );
    // No user-command error text should appear.
    assert!(
        !message.contains("User command"),
        "should not contain user-command error: {message}"
    );
}

#[tokio::test(flavor = "current_thread")]
async fn feat011_at006_absent_user_command_falls_back_to_builtin() {
    let writer = DispatchWorld006::cucumber()
        .fail_on_skipped()
        .with_default_cli()
        .filter_run(DISPATCH_FEATURE_PATH, move |feature, _, scenario| {
            feature.name == DISPATCH_FEATURE_NAME && scenario.name == AT006_SCENARIO
        })
        .await;
    assert_eq!(
        writer.failed_steps(),
        0,
        "scenario failed: {AT006_SCENARIO}"
    );
    assert_eq!(
        writer.skipped_steps(),
        0,
        "scenario skipped steps: {AT006_SCENARIO}"
    );
    assert_eq!(
        writer.passed_steps(),
        3,
        "scenario did not run: {AT006_SCENARIO}"
    );
}

// --- AT-007: Invalid user command produces user error without fallback ---

#[derive(cucumber::World)]
#[world(init = Self::new)]
struct DispatchWorld007 {
    tmpdir: Option<TempDir>,
    app: Option<Box<App>>,
    result: Option<CommandResult>,
}

impl std::fmt::Debug for DispatchWorld007 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DispatchWorld007")
            .field("has_tmpdir", &self.tmpdir.is_some())
            .field("has_app", &self.app.is_some())
            .field("has_result", &self.result.is_some())
            .finish()
    }
}

impl DispatchWorld007 {
    fn new() -> Self {
        Self {
            tmpdir: None,
            app: None,
            result: None,
        }
    }
}

#[given("a workspace with an invalid user command")]
fn at007_given_invalid_command(world: &mut DispatchWorld007) {
    let tmpdir = TempDir::new().expect("AT-007 TempDir");
    // Invalid frontmatter (not valid YAML) on a name that shadows a built-in.
    write_user_command(
        &tmpdir,
        "help",
        "---\ndescription: Custom help\nnot valid yaml\n---\ncustom help",
    );
    let mut app = create_dispatch_app(&tmpdir);
    app.workspace = tmpdir.path().to_path_buf();
    commands::user_registry::reload(Some(tmpdir.path()));
    world.tmpdir = Some(tmpdir);
    world.app = Some(Box::new(app));
}

#[when("the user runs the invalid command")]
fn at007_when_run_invalid(world: &mut DispatchWorld007) {
    let app = world.app.as_deref_mut().expect("app should exist");
    let result = commands::execute("/help", app);
    world.result = Some(result);
}

#[then("a user-command-specific error is returned")]
fn at007_then_user_error(world: &mut DispatchWorld007) {
    let result = world.result.as_ref().expect("result should exist");
    assert!(result.is_error, "invalid command should produce error");
    let message = result
        .message
        .as_deref()
        .expect("error message should exist");
    assert!(
        message.contains("User command"),
        "error should identify the user command: {message}"
    );
    assert!(
        message.contains("invalid frontmatter"),
        "error should describe the problem: {message}"
    );
}

#[then("no built-in fallback occurs")]
fn at007_then_no_fallback(world: &mut DispatchWorld007) {
    let result = world.result.as_ref().expect("result should exist");
    assert!(result.is_error, "result should remain an error");
    // The built-in /help would return a success result. An error result
    // with a user-command-specific message proves no built-in fallback.
    let message = result.message.as_deref().expect("error message");
    assert!(
        !message.contains("Type /help for available commands"),
        "should not suggest built-in help: {message}"
    );
}

#[tokio::test(flavor = "current_thread")]
async fn feat011_at007_invalid_user_command_produces_user_error_without_fallback() {
    let writer = DispatchWorld007::cucumber()
        .fail_on_skipped()
        .with_default_cli()
        .filter_run(DISPATCH_FEATURE_PATH, move |feature, _, scenario| {
            feature.name == DISPATCH_FEATURE_NAME && scenario.name == AT007_SCENARIO
        })
        .await;
    assert_eq!(
        writer.failed_steps(),
        0,
        "scenario failed: {AT007_SCENARIO}"
    );
    assert_eq!(
        writer.skipped_steps(),
        0,
        "scenario skipped steps: {AT007_SCENARIO}"
    );
    assert_eq!(
        writer.passed_steps(),
        4,
        "scenario did not run: {AT007_SCENARIO}"
    );
}