返回 CodeWhale
acceptance.rs
根目录 / crates / tui / src / commands / groups / core / acceptance.rs
1 //! Gherkin acceptance coverage for visible core command surfaces.
2
3 use cucumber::{World as _, given, then, when, writer::Stats as _};
4 use tempfile::TempDir;
5
6 use crate::commands::{self, CommandResult};
7 use crate::test_support::{EnvVarGuard, lock_test_env};
8 use crate::tui::app::{App, TuiOptions};
9 use crate::tui::history::HistoryCell;
10 use codewhale_config::ProviderKind;
11 use codewhale_runtime::config::Config;
12
13 const FEATURE_NAME: &str = "Core command visible surfaces";
14 const FEATURE_PATH: &str = concat!(
15 env!("CARGO_MANIFEST_DIR"),
16 "/tests/features/core_command_surfaces.feature"
17 );
18 const INFORMATIONAL_SCENARIO: &str =
19 "Core informational commands write visible transcript messages";
20 const STATE_SCENARIO: &str = "Core state commands report visible changes";
21 const CLEAR_SCENARIO: &str = "Clear replaces prior transcript with visible confirmation";
22 const PERSISTENT_WORK_SCENARIO: &str = "Persistent work commands report visible dispatch requests";
23
24 #[derive(Default, cucumber::World)]
25 struct CoreCommandWorld {
26 tmpdir: Option<TempDir>,
27 app: Option<Box<App>>,
28 home_path: Option<std::path::PathBuf>,
29 last_message: Option<String>,
30 last_result_is_error: Option<bool>,
31 }
32
33 impl std::fmt::Debug for CoreCommandWorld {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 f.debug_struct("CoreCommandWorld")
36 .field("has_tmpdir", &self.tmpdir.is_some())
37 .field("has_app", &self.app.is_some())
38 .field("home_path", &self.home_path)
39 .field("last_message", &self.last_message)
40 .field("last_result_is_error", &self.last_result_is_error)
41 .finish()
42 }
43 }
44
45 #[given("a CodeWhale core command workspace")]
46 fn core_command_workspace(world: &mut CoreCommandWorld) {
47 let tmpdir = TempDir::new().expect("core command TempDir");
48 let mut app = create_test_app_with_tmpdir(&tmpdir);
49 app.ui_locale = codewhale_localization::Locale::En;
50 app.api_provider = ProviderKind::Deepseek;
51 app.model = "deepseek-v4-pro".to_string();
52 app.auto_model = false;
53 app.model_ids_passthrough = false;
54
55 world.home_path = Some(tmpdir.path().join("home"));
56 world.app = Some(Box::new(app));
57 world.tmpdir = Some(tmpdir);
58 }
59
60 #[given("a CodeWhale core command workspace with one visible user message")]
61 fn core_command_workspace_with_one_visible_user_message(world: &mut CoreCommandWorld) {
62 core_command_workspace(world);
63 let app = world.app.as_deref_mut().expect("app should exist");
64 app.add_message(HistoryCell::User {
65 content: "Remember the whale migration".to_string(),
66 });
67 }
68
69 #[when(regex = r#"^the user runs the core command "([^"]+)"$"#)]
70 fn user_runs_core_command(world: &mut CoreCommandWorld, command: String) {
71 let result = execute_isolated(world, &command);
72 record_visible_result(world, result);
73 }
74
75 #[then(regex = r#"^the message window should include "([^"]+)"$"#)]
76 fn message_window_should_include(world: &mut CoreCommandWorld, expected: String) {
77 let visible = visible_message_window(world);
78
79 assert!(
80 visible.contains(&expected),
81 "message window should include {expected:?}\nvisible transcript:\n{visible}"
82 );
83 }
84
85 #[then(regex = r#"^the message window should not include "([^"]+)"$"#)]
86 fn message_window_should_not_include(world: &mut CoreCommandWorld, forbidden: String) {
87 let visible = visible_message_window(world);
88
89 assert!(
90 !visible.contains(&forbidden),
91 "message window should not include {forbidden:?}\nvisible transcript:\n{visible}"
92 );
93 }
94
95 #[tokio::test(flavor = "current_thread")]
96 async fn core_informational_commands_write_visible_transcript_messages() {
97 run_scenario(INFORMATIONAL_SCENARIO, 11).await;
98 }
99
100 #[tokio::test(flavor = "current_thread")]
101 async fn core_state_commands_report_visible_changes() {
102 run_scenario(STATE_SCENARIO, 8).await;
103 }
104
105 #[tokio::test(flavor = "current_thread")]
106 async fn clear_replaces_prior_transcript_with_visible_confirmation() {
107 run_scenario(CLEAR_SCENARIO, 4).await;
108 }
109
110 #[tokio::test(flavor = "current_thread")]
111 async fn persistent_work_commands_report_visible_dispatch_requests() {
112 run_scenario(PERSISTENT_WORK_SCENARIO, 10).await;
113 }
114
115 async fn run_scenario(name: &'static str, expected_steps: usize) {
116 let writer = CoreCommandWorld::cucumber()
117 .fail_on_skipped()
118 .with_default_cli()
119 .filter_run(FEATURE_PATH, move |feature, _, scenario| {
120 feature.name == FEATURE_NAME && scenario.name == name
121 })
122 .await;
123 assert_eq!(writer.failed_steps(), 0, "scenario failed: {name}");
124 assert_eq!(writer.skipped_steps(), 0, "scenario skipped steps: {name}");
125 assert_eq!(
126 writer.passed_steps(),
127 expected_steps,
128 "scenario did not run: {name}"
129 );
130 }
131
132 fn create_test_app_with_tmpdir(tmpdir: &TempDir) -> App {
133 let options = TuiOptions {
134 skills_dir: tmpdir.path().join("skills"),
135 memory_path: tmpdir.path().join("memory.md"),
136 mcp_config_path: tmpdir.path().join("mcp.json"),
137 ..crate::tui::test_support::test_tui_options(tmpdir.path())
138 };
139 App::new(options, &Config::default())
140 }
141
142 fn execute_isolated(world: &mut CoreCommandWorld, command: &str) -> CommandResult {
143 let home = world
144 .home_path
145 .as_ref()
146 .expect("test home should exist")
147 .clone();
148 std::fs::create_dir_all(&home).expect("create isolated test home");
149
150 let _lock = lock_test_env();
151 let _home = EnvVarGuard::set("HOME", &home);
152 let _codewhale_home = EnvVarGuard::set("CODEWHALE_HOME", home.join(".codewhale"));
153
154 let app = world.app.as_deref_mut().expect("app should exist");
155 commands::user_registry::reload(Some(&app.workspace));
156 commands::execute(command, app)
157 }
158
159 fn record_visible_result(world: &mut CoreCommandWorld, result: CommandResult) {
160 world.last_result_is_error = Some(result.is_error);
161 world.last_message = result.message.clone();
162
163 if let Some(message) = result.message {
164 let app = world.app.as_deref_mut().expect("app should exist");
165 app.add_message(HistoryCell::System { content: message });
166 }
167 }
168
169 fn visible_message_window(world: &CoreCommandWorld) -> String {
170 let app = world.app.as_deref().expect("app should exist");
171 app.history
172 .iter()
173 .filter_map(|cell| match cell {
174 HistoryCell::User { content }
175 | HistoryCell::Assistant { content, .. }
176 | HistoryCell::System { content }
177 | HistoryCell::Thinking { content, .. } => Some(content.clone()),
178 HistoryCell::Error { message, .. } => Some(message.clone()),
179 HistoryCell::ArchivedContext { summary, .. } => Some(summary.clone()),
180 HistoryCell::Automation(cell) => Some(cell.plain_summary()),
181 HistoryCell::ApprovalReceipt(cell) => Some(cell.plain_summary()),
182 HistoryCell::Tool(_) | HistoryCell::SubAgent(_) => None,
183 })
184 .collect::<Vec<_>>()
185 .join("\n")
186 }
187
187 lines RUST