| 1 | mod fleet; |
| 2 | mod lane; |
| 3 | |
| 4 | pub use fleet::FleetCmd; |
| 5 | pub use lane::LaneCmd; |
| 6 | |
| 7 | #[cfg(test)] |
| 8 | mod tests { |
| 9 | use super::*; |
| 10 | use codewhale_command_contract::fleet_lane::{ |
| 11 | CommandFleetContext, CommandLaneContext, ControlOperationHelp, ControlReceiptOutcome, |
| 12 | FleetAction, FleetControlVerb, FleetRouteSaveKind, FleetUsageKind, LaneControlVerb, |
| 13 | }; |
| 14 | use codewhale_command_contract::handler::{ |
| 15 | CommandCapabilities, CommandContexts, CommandHandler, |
| 16 | }; |
| 17 | use codewhale_command_contract::metadata::RegisterCommand; |
| 18 | use codewhale_command_contract::outcome::CommandResult; |
| 19 | |
| 20 | struct FakeLane { |
| 21 | calls: Vec<(LaneControlVerb, Option<String>)>, |
| 22 | } |
| 23 | |
| 24 | impl CommandLaneContext for FakeLane { |
| 25 | fn control_operations(&self) -> Vec<ControlOperationHelp> { |
| 26 | vec![ControlOperationHelp { |
| 27 | slash_invocation: "/lane list".into(), |
| 28 | authority: "read".into(), |
| 29 | summary: "List lanes".into(), |
| 30 | cli_invocation: "codewhale lane list".into(), |
| 31 | }] |
| 32 | } |
| 33 | |
| 34 | fn resolve_control_verb(&self, verb: &str) -> Option<LaneControlVerb> { |
| 35 | match verb { |
| 36 | "list" => Some(LaneControlVerb::List), |
| 37 | "stop" => Some(LaneControlVerb::Interrupt), |
| 38 | _ => None, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn execute_control( |
| 43 | &mut self, |
| 44 | verb: LaneControlVerb, |
| 45 | target: Option<&str>, |
| 46 | ) -> ControlReceiptOutcome { |
| 47 | self.calls.push((verb, target.map(str::to_string))); |
| 48 | ControlReceiptOutcome { |
| 49 | rendered: format!("lane:{verb:?}:{target:?}"), |
| 50 | is_error: false, |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | struct FakeFleet { |
| 56 | workers_prepared: bool, |
| 57 | last_control: Option<(FleetControlVerb, Option<String>)>, |
| 58 | } |
| 59 | |
| 60 | impl CommandFleetContext for FakeFleet { |
| 61 | fn control_operations(&self) -> Vec<ControlOperationHelp> { |
| 62 | vec![ControlOperationHelp { |
| 63 | slash_invocation: "/fleet list".into(), |
| 64 | authority: "read".into(), |
| 65 | summary: "List fleet runs".into(), |
| 66 | cli_invocation: "codewhale fleet list".into(), |
| 67 | }] |
| 68 | } |
| 69 | |
| 70 | fn resolve_control_verb(&self, verb: &str) -> Option<FleetControlVerb> { |
| 71 | match verb { |
| 72 | "status" => Some(FleetControlVerb::Status), |
| 73 | _ => None, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn run_control( |
| 78 | &mut self, |
| 79 | verb: FleetControlVerb, |
| 80 | target: Option<&str>, |
| 81 | ) -> ControlReceiptOutcome { |
| 82 | self.last_control = Some((verb, target.map(str::to_string))); |
| 83 | ControlReceiptOutcome { |
| 84 | rendered: format!("fleet:{verb:?}:{target:?}"), |
| 85 | is_error: false, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fn fleet_models_text(&self) -> String { |
| 90 | "models-ok".into() |
| 91 | } |
| 92 | |
| 93 | fn save_route(&mut self, kind: FleetRouteSaveKind) -> String { |
| 94 | format!("saved:{kind:?}") |
| 95 | } |
| 96 | |
| 97 | fn usage_text(&self, kind: FleetUsageKind) -> String { |
| 98 | format!("Usage: /fleet {kind:?}") |
| 99 | } |
| 100 | |
| 101 | fn prepare_workers_view(&mut self) { |
| 102 | self.workers_prepared = true; |
| 103 | } |
| 104 | |
| 105 | fn run_project_context(&mut self, args: Option<&str>) -> ControlReceiptOutcome { |
| 106 | ControlReceiptOutcome { |
| 107 | rendered: format!("context:{args:?}"), |
| 108 | is_error: false, |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | fn run_project_board(&mut self, args: Option<&str>) -> ControlReceiptOutcome { |
| 113 | ControlReceiptOutcome { |
| 114 | rendered: format!("board:{args:?}"), |
| 115 | is_error: false, |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | struct RejectingAction; |
| 121 | impl TryFrom<FleetAction> for RejectingAction { |
| 122 | type Error = String; |
| 123 | fn try_from(_: FleetAction) -> Result<Self, String> { |
| 124 | Err("action rejected by host".into()) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn fleet_and_lane_declare_exact_capability_bits() { |
| 130 | assert_eq!( |
| 131 | match <FleetCmd as RegisterCommand<CommandResult<FleetAction>>>::handler() { |
| 132 | CommandHandler::Contextual { capabilities, .. } => capabilities, |
| 133 | CommandHandler::Pure(_) => panic!("expected contextual"), |
| 134 | }, |
| 135 | CommandCapabilities::FLEET |
| 136 | ); |
| 137 | assert_eq!( |
| 138 | match <LaneCmd as RegisterCommand<CommandResult<std::convert::Infallible>>>::handler() { |
| 139 | CommandHandler::Contextual { capabilities, .. } => capabilities, |
| 140 | CommandHandler::Pure(_) => panic!("expected contextual"), |
| 141 | }, |
| 142 | CommandCapabilities::LANE |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn lane_rejects_unknown_verbs_and_routes_through_single_control_method() { |
| 148 | let CommandHandler::Contextual { handler, .. } = |
| 149 | <LaneCmd as RegisterCommand<CommandResult<std::convert::Infallible>>>::handler() |
| 150 | else { |
| 151 | panic!("expected contextual"); |
| 152 | }; |
| 153 | let mut lane = FakeLane { calls: Vec::new() }; |
| 154 | let rejected = handler( |
| 155 | CommandContexts::empty().with_lane(&mut lane), |
| 156 | Some("obliterate lane-1"), |
| 157 | ); |
| 158 | assert!(rejected.is_error); |
| 159 | assert!( |
| 160 | rejected |
| 161 | .message |
| 162 | .as_deref() |
| 163 | .is_some_and(|m| m.contains("Unknown /lane verb 'obliterate'")) |
| 164 | ); |
| 165 | assert!(lane.calls.is_empty()); |
| 166 | |
| 167 | let listed = handler(CommandContexts::empty().with_lane(&mut lane), None); |
| 168 | assert!(!listed.is_error); |
| 169 | assert_eq!(listed.message.as_deref(), Some("lane:List:None")); |
| 170 | |
| 171 | let stopped = handler( |
| 172 | CommandContexts::empty().with_lane(&mut lane), |
| 173 | Some("stop lane-1@2"), |
| 174 | ); |
| 175 | assert!(!stopped.is_error); |
| 176 | assert_eq!( |
| 177 | lane.calls, |
| 178 | vec![ |
| 179 | (LaneControlVerb::List, None), |
| 180 | (LaneControlVerb::Interrupt, Some("lane-1@2".to_string())), |
| 181 | ] |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn fleet_parses_actions_validates_with_try_from_and_routes_control() { |
| 187 | let CommandHandler::Contextual { handler, .. } = |
| 188 | <FleetCmd as RegisterCommand<CommandResult<FleetAction>>>::handler() |
| 189 | else { |
| 190 | panic!("expected contextual"); |
| 191 | }; |
| 192 | let open = handler(CommandContexts::empty(), None); |
| 193 | assert_eq!(open.action, Some(FleetAction::OpenMissions)); |
| 194 | |
| 195 | let added = handler( |
| 196 | CommandContexts::empty(), |
| 197 | Some("add openrouter z-ai/glm-5.3-flash explore"), |
| 198 | ); |
| 199 | assert_eq!( |
| 200 | added.action, |
| 201 | Some(FleetAction::AddModel { |
| 202 | provider: "openrouter".into(), |
| 203 | model: "z-ai/glm-5.3-flash".into(), |
| 204 | roles: vec!["explore".into()], |
| 205 | }) |
| 206 | ); |
| 207 | |
| 208 | let mut fleet = FakeFleet { |
| 209 | workers_prepared: false, |
| 210 | last_control: None, |
| 211 | }; |
| 212 | let workers = handler( |
| 213 | CommandContexts::empty().with_fleet(&mut fleet), |
| 214 | Some("workers"), |
| 215 | ); |
| 216 | assert!(fleet.workers_prepared); |
| 217 | assert_eq!(workers.action, Some(FleetAction::OpenWorkers)); |
| 218 | |
| 219 | let status = handler( |
| 220 | CommandContexts::empty().with_fleet(&mut fleet), |
| 221 | Some("status"), |
| 222 | ); |
| 223 | assert!(!status.is_error); |
| 224 | assert_eq!(fleet.last_control, Some((FleetControlVerb::Status, None))); |
| 225 | |
| 226 | let context = handler( |
| 227 | CommandContexts::empty().with_fleet(&mut fleet), |
| 228 | Some("context read design"), |
| 229 | ); |
| 230 | assert!(!context.is_error); |
| 231 | assert_eq!( |
| 232 | context.message.as_deref(), |
| 233 | Some("context:Some(\"read design\")") |
| 234 | ); |
| 235 | |
| 236 | let board = handler( |
| 237 | CommandContexts::empty().with_fleet(&mut fleet), |
| 238 | Some("board add Ship local coordinator"), |
| 239 | ); |
| 240 | assert!(!board.is_error); |
| 241 | assert_eq!( |
| 242 | board.message.as_deref(), |
| 243 | Some("board:Some(\"add Ship local coordinator\")") |
| 244 | ); |
| 245 | |
| 246 | let CommandHandler::Contextual { |
| 247 | handler: reject_handler, |
| 248 | .. |
| 249 | } = <FleetCmd as RegisterCommand<CommandResult<RejectingAction>>>::handler() |
| 250 | else { |
| 251 | panic!("expected contextual"); |
| 252 | }; |
| 253 | fleet.workers_prepared = false; |
| 254 | let rejected = reject_handler( |
| 255 | CommandContexts::empty().with_fleet(&mut fleet), |
| 256 | Some("workers"), |
| 257 | ); |
| 258 | assert!(!fleet.workers_prepared); |
| 259 | assert!(rejected.is_error); |
| 260 | assert!(rejected.action.is_none()); |
| 261 | assert_eq!( |
| 262 | rejected.message.as_deref(), |
| 263 | Some("Error: action rejected by host") |
| 264 | ); |
| 265 | } |
| 266 | } |
| 267 |