返回 CodeWhale
epic_dispatch_acceptance.rs
根目录 / crates / tui / src / commands / epic_dispatch_acceptance.rs
1 //! Focused Gherkin acceptance evidence for FEAT-011 dispatch precedence and
2 //! error semantics. Bound through separate scenario-level cucumber worlds
3 //! that prove AT-004 through AT-007 with the live dispatch entry point.
4
5 use cucumber::{World as _, given, then, when, writer::Stats as _};
6 use tempfile::TempDir;
7
8 use crate::commands::{self, CommandResult};
9 use crate::tui::app::{App, TuiOptions};
10 use codewhale_runtime::config::Config;
11
12 // --- FEAT-011 dispatch precedence constants ---
13
14 const DISPATCH_FEATURE_NAME: &str = "FEAT-011 Dispatch Precedence And Error Semantics";
15 const DISPATCH_FEATURE_PATH: &str = concat!(
16 env!("CARGO_MANIFEST_DIR"),
17 "/tests/features/feat-011-dispatch-precedence.feature"
18 );
19
20 const AT004_SCENARIO: &str = "AT-004 User command shadows built-in canonical name";
21 const AT005_SCENARIO: &str = "AT-005 User command shadows built-in alias";
22 const AT006_SCENARIO: &str = "AT-006 Absent user command falls back to built-in";
23 const AT007_SCENARIO: &str = "AT-007 Invalid user command produces user error without fallback";
24
25 // --- Shared helpers ---
26
27 fn create_dispatch_app(tmpdir: &TempDir) -> App {
28 let options = TuiOptions {
29 skills_dir: tmpdir.path().join("skills"),
30 memory_path: tmpdir.path().join("memory.md"),
31 mcp_config_path: tmpdir.path().join("mcp.json"),
32 ..crate::tui::test_support::test_tui_options(tmpdir.path())
33 };
34 App::new(options, &Config::default())
35 }
36
37 fn write_user_command(tmpdir: &TempDir, name: &str, content: &str) {
38 // Workspace commands load only in a trusted workspace.
39 crate::test_support::trust_workspace(tmpdir.path());
40 let commands_dir = tmpdir.path().join(".codewhale").join("commands");
41 std::fs::create_dir_all(commands_dir).expect("create commands dir");
42 let path = tmpdir
43 .path()
44 .join(".codewhale")
45 .join("commands")
46 .join(format!("{name}.md"));
47 std::fs::write(path, content).expect("write user command");
48 }
49
50 fn sent_message(result: &CommandResult) -> String {
51 match &result.action {
52 Some(crate::tui::app::AppAction::SendMessage(message)) => message.clone(),
53 other => panic!("expected SendMessage action, got {other:?}"),
54 }
55 }
56
57 // --- AT-004: User command shadows built-in canonical name ---
58
59 #[derive(cucumber::World)]
60 #[world(init = Self::new)]
61 struct DispatchWorld004 {
62 tmpdir: Option<TempDir>,
63 app: Option<Box<App>>,
64 result: Option<CommandResult>,
65 }
66
67 impl std::fmt::Debug for DispatchWorld004 {
68 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69 f.debug_struct("DispatchWorld004")
70 .field("has_tmpdir", &self.tmpdir.is_some())
71 .field("has_app", &self.app.is_some())
72 .field("has_result", &self.result.is_some())
73 .finish()
74 }
75 }
76
77 impl DispatchWorld004 {
78 fn new() -> Self {
79 Self {
80 tmpdir: None,
81 app: None,
82 result: None,
83 }
84 }
85 }
86
87 #[given("a workspace with a user command shadowing a built-in canonical name")]
88 fn at004_given_shadow_canonical(world: &mut DispatchWorld004) {
89 let tmpdir = TempDir::new().expect("AT-004 TempDir");
90 write_user_command(
91 &tmpdir,
92 "help",
93 "---\ndescription: Custom help\n---\ncustom help $ARGUMENTS",
94 );
95 let mut app = create_dispatch_app(&tmpdir);
96 app.workspace = tmpdir.path().to_path_buf();
97 commands::user_registry::reload(Some(tmpdir.path()));
98 world.tmpdir = Some(tmpdir);
99 world.app = Some(Box::new(app));
100 }
101
102 #[when(regex = r#"^the user runs "/help config"$"#)]
103 fn at004_when_run_shadowed(world: &mut DispatchWorld004) {
104 let app = world.app.as_deref_mut().expect("app should exist");
105 let result = commands::execute("/help config", app);
106 world.result = Some(result);
107 }
108
109 #[then("the user command executes instead of the built-in")]
110 fn at004_then_user_executes(world: &mut DispatchWorld004) {
111 let result = world.result.as_ref().expect("result should exist");
112 assert!(
113 !result.is_error,
114 "user command should succeed: {:?}",
115 result.message
116 );
117 assert_eq!(
118 sent_message(result),
119 "custom help config",
120 "user command should produce custom content"
121 );
122 }
123
124 #[then("no built-in /help side effect occurs")]
125 fn at004_then_no_builtin(world: &mut DispatchWorld004) {
126 let result = world.result.as_ref().expect("result should exist");
127 assert!(!result.is_error, "no error");
128 match &result.action {
129 Some(crate::tui::app::AppAction::SendMessage(message)) => {
130 assert!(
131 message.contains("custom help"),
132 "message should contain user command content: {message}"
133 );
134 }
135 other => panic!("expected SendMessage, got {other:?}"),
136 }
137 }
138
139 #[tokio::test(flavor = "current_thread")]
140 async fn feat011_at004_user_command_shadows_builtin_canonical_name() {
141 let writer = DispatchWorld004::cucumber()
142 .fail_on_skipped()
143 .with_default_cli()
144 .filter_run(DISPATCH_FEATURE_PATH, move |feature, _, scenario| {
145 feature.name == DISPATCH_FEATURE_NAME && scenario.name == AT004_SCENARIO
146 })
147 .await;
148 assert_eq!(
149 writer.failed_steps(),
150 0,
151 "scenario failed: {AT004_SCENARIO}"
152 );
153 assert_eq!(
154 writer.skipped_steps(),
155 0,
156 "scenario skipped steps: {AT004_SCENARIO}"
157 );
158 assert_eq!(
159 writer.passed_steps(),
160 4,
161 "scenario did not run: {AT004_SCENARIO}"
162 );
163 }
164
165 // --- AT-005: User command shadows built-in alias ---
166
167 #[derive(cucumber::World)]
168 #[world(init = Self::new)]
169 struct DispatchWorld005 {
170 tmpdir: Option<TempDir>,
171 app: Option<Box<App>>,
172 alias_result: Option<CommandResult>,
173 canonical_result: Option<CommandResult>,
174 }
175
176 impl std::fmt::Debug for DispatchWorld005 {
177 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
178 f.debug_struct("DispatchWorld005")
179 .field("has_tmpdir", &self.tmpdir.is_some())
180 .field("has_app", &self.app.is_some())
181 .field("has_alias_result", &self.alias_result.is_some())
182 .field("has_canonical_result", &self.canonical_result.is_some())
183 .finish()
184 }
185 }
186
187 impl DispatchWorld005 {
188 fn new() -> Self {
189 Self {
190 tmpdir: None,
191 app: None,
192 alias_result: None,
193 canonical_result: None,
194 }
195 }
196 }
197
198 #[given("a workspace with a user command shadowing a built-in alias")]
199 fn at005_given_shadow_alias(world: &mut DispatchWorld005) {
200 let tmpdir = TempDir::new().expect("AT-005 TempDir");
201 // /links has alias /dashboard and /api. Create a user command that
202 // shadows the /dashboard alias.
203 write_user_command(
204 &tmpdir,
205 "attach-review",
206 "---\nalias: dashboard\n---\ncustom dashboard $ARGUMENTS",
207 );
208 let mut app = create_dispatch_app(&tmpdir);
209 app.workspace = tmpdir.path().to_path_buf();
210 commands::user_registry::reload(Some(tmpdir.path()));
211 world.tmpdir = Some(tmpdir);
212 world.app = Some(Box::new(app));
213 }
214
215 #[when("the user runs the shadowed alias")]
216 fn at005_when_run_alias(world: &mut DispatchWorld005) {
217 let app = world.app.as_deref_mut().expect("app should exist");
218 // Use /dashboard which is shadowed by the user command's alias.
219 let alias_result = commands::execute("/dashboard", app);
220 world.alias_result = Some(alias_result);
221
222 // Also test that the built-in canonical name (/links) still works.
223 let canonical_result = commands::execute("/links", app);
224 world.canonical_result = Some(canonical_result);
225 }
226
227 #[then("the user command executes")]
228 fn at005_then_user_executes(world: &mut DispatchWorld005) {
229 let result = world
230 .alias_result
231 .as_ref()
232 .expect("alias result should exist");
233 assert!(!result.is_error, "user command dispatch should succeed");
234 assert_eq!(
235 sent_message(result),
236 "custom dashboard ",
237 "user alias should produce custom content"
238 );
239 }
240
241 #[then("the built-in canonical name remains reachable")]
242 fn at005_then_canonical_reachable(world: &mut DispatchWorld005) {
243 let result = world
244 .canonical_result
245 .as_ref()
246 .expect("canonical result should exist");
247 assert!(!result.is_error, "canonical built-in should still work");
248 assert!(
249 result
250 .message
251 .as_deref()
252 .is_some_and(|msg| msg.contains("https://")),
253 "canonical /links should return platform links: {:?}",
254 result.message
255 );
256 }
257
258 #[tokio::test(flavor = "current_thread")]
259 async fn feat011_at005_user_command_shadows_builtin_alias() {
260 let writer = DispatchWorld005::cucumber()
261 .fail_on_skipped()
262 .with_default_cli()
263 .filter_run(DISPATCH_FEATURE_PATH, move |feature, _, scenario| {
264 feature.name == DISPATCH_FEATURE_NAME && scenario.name == AT005_SCENARIO
265 })
266 .await;
267 assert_eq!(
268 writer.failed_steps(),
269 0,
270 "scenario failed: {AT005_SCENARIO}"
271 );
272 assert_eq!(
273 writer.skipped_steps(),
274 0,
275 "scenario skipped steps: {AT005_SCENARIO}"
276 );
277 assert_eq!(
278 writer.passed_steps(),
279 4,
280 "scenario did not run: {AT005_SCENARIO}"
281 );
282 }
283
284 // --- AT-006: Absent user command falls back to built-in ---
285
286 #[derive(cucumber::World)]
287 #[world(init = Self::new)]
288 struct DispatchWorld006 {
289 tmpdir: Option<TempDir>,
290 app: Option<Box<App>>,
291 after_removal_result: Option<CommandResult>,
292 }
293
294 impl std::fmt::Debug for DispatchWorld006 {
295 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
296 f.debug_struct("DispatchWorld006")
297 .field("has_tmpdir", &self.tmpdir.is_some())
298 .field("has_app", &self.app.is_some())
299 .field("has_result", &self.after_removal_result.is_some())
300 .finish()
301 }
302 }
303
304 impl DispatchWorld006 {
305 fn new() -> Self {
306 Self {
307 tmpdir: None,
308 app: None,
309 after_removal_result: None,
310 }
311 }
312 }
313
314 #[given("a workspace with a previously loaded user command")]
315 fn at006_given_loaded_user_command(world: &mut DispatchWorld006) {
316 let tmpdir = TempDir::new().expect("AT-006 TempDir");
317 write_user_command(&tmpdir, "help", "user help");
318 let mut app = create_dispatch_app(&tmpdir);
319 app.workspace = tmpdir.path().to_path_buf();
320 commands::user_registry::reload(Some(tmpdir.path()));
321
322 // Verify user command dispatches first
323 let initial_result = commands::execute("/help config", &mut app);
324 assert!(
325 matches!(
326 &initial_result.action,
327 Some(crate::tui::app::AppAction::SendMessage(_))
328 ),
329 "user command should dispatch initially"
330 );
331
332 world.tmpdir = Some(tmpdir);
333 world.app = Some(Box::new(app));
334 }
335
336 #[when("the user command file is removed and the command is invoked again")]
337 fn at006_when_removed_and_invoked(world: &mut DispatchWorld006) {
338 let app = world.app.as_deref_mut().expect("app should exist");
339 let tmpdir = world.tmpdir.as_ref().expect("tmpdir should exist");
340 let command_path = tmpdir
341 .path()
342 .join(".codewhale")
343 .join("commands")
344 .join("help.md");
345
346 // Remove the user command file
347 std::fs::remove_file(&command_path).expect("remove user command file");
348 commands::user_registry::reload(Some(tmpdir.path()));
349
350 // Invoke the (now absent) command — should fall back to built-in
351 let result = commands::execute("/help config", app);
352 world.after_removal_result = Some(result);
353 }
354
355 #[then("the built-in command executes without a user-command error message")]
356 fn at006_then_builtin_executes(world: &mut DispatchWorld006) {
357 let result = world
358 .after_removal_result
359 .as_ref()
360 .expect("result should exist");
361 assert!(!result.is_error, "built-in fallback should not error");
362 let message = result.message.as_deref().unwrap_or("");
363 // The built-in /help config message should mention the config command.
364 assert!(
365 message.contains("config"),
366 "built-in /help should handle the command: {message}"
367 );
368 // No user-command error text should appear.
369 assert!(
370 !message.contains("User command"),
371 "should not contain user-command error: {message}"
372 );
373 }
374
375 #[tokio::test(flavor = "current_thread")]
376 async fn feat011_at006_absent_user_command_falls_back_to_builtin() {
377 let writer = DispatchWorld006::cucumber()
378 .fail_on_skipped()
379 .with_default_cli()
380 .filter_run(DISPATCH_FEATURE_PATH, move |feature, _, scenario| {
381 feature.name == DISPATCH_FEATURE_NAME && scenario.name == AT006_SCENARIO
382 })
383 .await;
384 assert_eq!(
385 writer.failed_steps(),
386 0,
387 "scenario failed: {AT006_SCENARIO}"
388 );
389 assert_eq!(
390 writer.skipped_steps(),
391 0,
392 "scenario skipped steps: {AT006_SCENARIO}"
393 );
394 assert_eq!(
395 writer.passed_steps(),
396 3,
397 "scenario did not run: {AT006_SCENARIO}"
398 );
399 }
400
401 // --- AT-007: Invalid user command produces user error without fallback ---
402
403 #[derive(cucumber::World)]
404 #[world(init = Self::new)]
405 struct DispatchWorld007 {
406 tmpdir: Option<TempDir>,
407 app: Option<Box<App>>,
408 result: Option<CommandResult>,
409 }
410
411 impl std::fmt::Debug for DispatchWorld007 {
412 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
413 f.debug_struct("DispatchWorld007")
414 .field("has_tmpdir", &self.tmpdir.is_some())
415 .field("has_app", &self.app.is_some())
416 .field("has_result", &self.result.is_some())
417 .finish()
418 }
419 }
420
421 impl DispatchWorld007 {
422 fn new() -> Self {
423 Self {
424 tmpdir: None,
425 app: None,
426 result: None,
427 }
428 }
429 }
430
431 #[given("a workspace with an invalid user command")]
432 fn at007_given_invalid_command(world: &mut DispatchWorld007) {
433 let tmpdir = TempDir::new().expect("AT-007 TempDir");
434 // Invalid frontmatter (not valid YAML) on a name that shadows a built-in.
435 write_user_command(
436 &tmpdir,
437 "help",
438 "---\ndescription: Custom help\nnot valid yaml\n---\ncustom help",
439 );
440 let mut app = create_dispatch_app(&tmpdir);
441 app.workspace = tmpdir.path().to_path_buf();
442 commands::user_registry::reload(Some(tmpdir.path()));
443 world.tmpdir = Some(tmpdir);
444 world.app = Some(Box::new(app));
445 }
446
447 #[when("the user runs the invalid command")]
448 fn at007_when_run_invalid(world: &mut DispatchWorld007) {
449 let app = world.app.as_deref_mut().expect("app should exist");
450 let result = commands::execute("/help", app);
451 world.result = Some(result);
452 }
453
454 #[then("a user-command-specific error is returned")]
455 fn at007_then_user_error(world: &mut DispatchWorld007) {
456 let result = world.result.as_ref().expect("result should exist");
457 assert!(result.is_error, "invalid command should produce error");
458 let message = result
459 .message
460 .as_deref()
461 .expect("error message should exist");
462 assert!(
463 message.contains("User command"),
464 "error should identify the user command: {message}"
465 );
466 assert!(
467 message.contains("invalid frontmatter"),
468 "error should describe the problem: {message}"
469 );
470 }
471
472 #[then("no built-in fallback occurs")]
473 fn at007_then_no_fallback(world: &mut DispatchWorld007) {
474 let result = world.result.as_ref().expect("result should exist");
475 assert!(result.is_error, "result should remain an error");
476 // The built-in /help would return a success result. An error result
477 // with a user-command-specific message proves no built-in fallback.
478 let message = result.message.as_deref().expect("error message");
479 assert!(
480 !message.contains("Type /help for available commands"),
481 "should not suggest built-in help: {message}"
482 );
483 }
484
485 #[tokio::test(flavor = "current_thread")]
486 async fn feat011_at007_invalid_user_command_produces_user_error_without_fallback() {
487 let writer = DispatchWorld007::cucumber()
488 .fail_on_skipped()
489 .with_default_cli()
490 .filter_run(DISPATCH_FEATURE_PATH, move |feature, _, scenario| {
491 feature.name == DISPATCH_FEATURE_NAME && scenario.name == AT007_SCENARIO
492 })
493 .await;
494 assert_eq!(
495 writer.failed_steps(),
496 0,
497 "scenario failed: {AT007_SCENARIO}"
498 );
499 assert_eq!(
500 writer.skipped_steps(),
501 0,
502 "scenario skipped steps: {AT007_SCENARIO}"
503 );
504 assert_eq!(
505 writer.passed_steps(),
506 4,
507 "scenario did not run: {AT007_SCENARIO}"
508 );
509 }
510
510 lines RUST