返回 CodeWhale
task.rs
根目录 / crates / tui / src / commands / groups / utility / task.rs
1 //! Task commands: add/list/show/cancel
2
3 use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler};
4 use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand};
5
6 use crate::commands::CommandResult;
7 use crate::tui::app::AppAction;
8
9 pub(in crate::commands) const COMMAND_INFO: CommandInfo = CommandInfo {
10 name: "task",
11 aliases: &["tasks"],
12 usage: "/task [add <prompt>|list|digest|show <id>|cancel <id>]",
13 description_key: "cmd_task_description",
14 };
15
16 pub(in crate::commands) struct TaskCmd;
17
18 impl RegisterCommand<CommandResult> for TaskCmd {
19 fn info() -> &'static CommandInfo {
20 &COMMAND_INFO
21 }
22
23 fn handler() -> CommandHandler<CommandResult> {
24 CommandHandler::Contextual {
25 capabilities: CommandCapabilities::WORKSPACE,
26 handler: task_contextual,
27 }
28 }
29 }
30
31 fn task_contextual(contexts: CommandContexts<'_>, arg: Option<&str>) -> CommandResult {
32 let mut parts = contexts.into_parts();
33 let Some(workspace) = parts.workspace.as_deref_mut() else {
34 return CommandResult::error("Command capability unavailable: workspace");
35 };
36 task(workspace, arg)
37 }
38
39 fn task(
40 workspace: &mut dyn codewhale_command_contract::facets::CommandWorkspaceContext,
41 args: Option<&str>,
42 ) -> CommandResult {
43 let raw = args.unwrap_or("").trim();
44 if raw.is_empty() || raw.eq_ignore_ascii_case("list") {
45 return CommandResult::action(AppAction::TaskList);
46 }
47
48 let mut parts = raw.splitn(2, char::is_whitespace);
49 let action = parts.next().unwrap_or("").to_ascii_lowercase();
50 let remainder = parts.next().map(str::trim).filter(|s| !s.is_empty());
51
52 match action.as_str() {
53 "add" => {
54 let Some(prompt) = remainder else {
55 return CommandResult::error("Usage: /task add <prompt>");
56 };
57 CommandResult::action(AppAction::TaskAdd {
58 prompt: prompt.to_string(),
59 })
60 }
61 "list" => CommandResult::action(AppAction::TaskList),
62 "digest" => match workspace.operation_digest() {
63 Ok(text) => CommandResult::message(text),
64 Err(error) => CommandResult::error(error),
65 },
66 "show" => {
67 let Some(id) = remainder else {
68 return CommandResult::error("Usage: /task show <id>");
69 };
70 CommandResult::action(AppAction::TaskShow { id: id.to_string() })
71 }
72 "cancel" | "stop" => {
73 let Some(id) = remainder else {
74 return CommandResult::error("Usage: /task cancel <id>");
75 };
76 CommandResult::action(AppAction::TaskCancel { id: id.to_string() })
77 }
78 _ => CommandResult::error("Usage: /task [add <prompt>|list|digest|show <id>|cancel <id>]"),
79 }
80 }
81
82 #[cfg(test)]
83 mod tests {
84 use super::*;
85 use std::path::PathBuf;
86
87 struct FakeWorkspace;
88 impl codewhale_command_contract::facets::CommandWorkspaceContext for FakeWorkspace {
89 fn effective_home_dir(&self) -> Option<std::path::PathBuf> {
90 unreachable!("navigation is not used by task")
91 }
92 fn inspect_workspace_target(
93 &self,
94 _: &std::path::Path,
95 ) -> codewhale_command_contract::navigation::WorkspaceTarget {
96 unreachable!("navigation is not used by task")
97 }
98 fn read_anchor_document(&self) -> Result<Option<String>, String> {
99 unreachable!("anchors are not used by task")
100 }
101 fn append_anchor_bytes(
102 &mut self,
103 _: &[&[u8]],
104 ) -> Result<std::path::PathBuf, codewhale_command_contract::navigation::AnchorWriteError>
105 {
106 unreachable!("anchors are not used by task")
107 }
108 fn replace_anchor_document(&mut self, _: &[u8]) -> Result<(), String> {
109 unreachable!("anchors are not used by task")
110 }
111 fn workspace(&self) -> PathBuf {
112 PathBuf::from(".")
113 }
114 fn work_state_snapshot(&self) -> Result<Option<String>, String> {
115 Ok(None)
116 }
117 fn operation_digest(&mut self) -> Result<String, String> {
118 Ok("No active operations or to-do items.".to_string())
119 }
120 }
121
122 struct FailingWorkspace;
123 impl codewhale_command_contract::facets::CommandWorkspaceContext for FailingWorkspace {
124 fn effective_home_dir(&self) -> Option<std::path::PathBuf> {
125 unreachable!("navigation is not used by task")
126 }
127 fn inspect_workspace_target(
128 &self,
129 _: &std::path::Path,
130 ) -> codewhale_command_contract::navigation::WorkspaceTarget {
131 unreachable!("navigation is not used by task")
132 }
133 fn read_anchor_document(&self) -> Result<Option<String>, String> {
134 unreachable!("anchors are not used by task")
135 }
136 fn append_anchor_bytes(
137 &mut self,
138 _: &[&[u8]],
139 ) -> Result<std::path::PathBuf, codewhale_command_contract::navigation::AnchorWriteError>
140 {
141 unreachable!("anchors are not used by task")
142 }
143 fn replace_anchor_document(&mut self, _: &[u8]) -> Result<(), String> {
144 unreachable!("anchors are not used by task")
145 }
146 fn workspace(&self) -> PathBuf {
147 PathBuf::from(".")
148 }
149 fn work_state_snapshot(&self) -> Result<Option<String>, String> {
150 Ok(None)
151 }
152 fn operation_digest(&mut self) -> Result<String, String> {
153 Err("Operation digest is temporarily unavailable: boom".to_string())
154 }
155 }
156
157 #[test]
158 fn parses_add_and_cancel() {
159 let add = task(&mut FakeWorkspace, Some("add write tests"));
160 assert!(matches!(
161 add.action,
162 Some(AppAction::TaskAdd { prompt }) if prompt == "write tests"
163 ));
164
165 let cancel = task(&mut FakeWorkspace, Some("cancel task_1234"));
166 assert!(matches!(
167 cancel.action,
168 Some(AppAction::TaskCancel { id }) if id == "task_1234"
169 ));
170 }
171
172 #[test]
173 fn validates_usage() {
174 let result = task(&mut FakeWorkspace, Some("add"));
175 assert!(result.message.is_some());
176 assert!(result.action.is_none());
177 }
178
179 #[test]
180 fn digest_uses_canonical_work_runtime_without_another_state_store() {
181 let result = task(&mut FakeWorkspace, Some("digest"));
182 assert_eq!(
183 result.message.as_deref(),
184 Some("No active operations or to-do items.")
185 );
186 assert!(result.action.is_none());
187
188 let failing = task(&mut FailingWorkspace, Some("digest"));
189 assert!(failing.is_error);
190 assert!(
191 failing
192 .message
193 .as_deref()
194 .unwrap_or_default()
195 .contains("Operation digest is temporarily unavailable: boom")
196 );
197 }
198
199 #[test]
200 fn handler_is_contextual() {
201 let CommandHandler::Contextual {
202 capabilities,
203 handler,
204 } = TaskCmd::handler()
205 else {
206 panic!("task must be contextual");
207 };
208 assert_eq!(capabilities, CommandCapabilities::WORKSPACE);
209 let missing = handler(CommandContexts::empty(), Some("list"));
210 assert!(missing.is_error);
211 assert_eq!(
212 missing.message.as_deref(),
213 Some("Error: Command capability unavailable: workspace")
214 );
215 assert_eq!(TaskCmd::info().description_key, "cmd_task_description");
216 assert_eq!(TaskCmd::info().aliases, &["tasks"]);
217 }
218 }
219
219 lines RUST