| 1 | //! FEAT-024 Phase 4: deterministic fake session-control facet for portable |
| 2 | //! handler tests. Canned returns plus a call log prove exact strings, actions, |
| 3 | //! call arguments, operation counts, and check order without touching the host. |
| 4 | |
| 5 | #![cfg(test)] |
| 6 | |
| 7 | use codewhale_command_contract::facets::{ |
| 8 | CommandSessionControlContext, HostedWorkTarget, PlanProjection, RelayProjection, RemoteLink, |
| 9 | RemoteOpenOutcome, RemoteStartInfo, ResumeImportReceipt, ResumeSource, SessionTitleReceipt, |
| 10 | TitleReport, TodoProjection, |
| 11 | }; |
| 12 | use std::cell::RefCell; |
| 13 | use std::path::PathBuf; |
| 14 | |
| 15 | #[derive(Default)] |
| 16 | pub(crate) struct FakeControl { |
| 17 | pub(crate) blocked: bool, |
| 18 | pub(crate) relay: Option<RelayProjection>, |
| 19 | pub(crate) resume: Option<Result<ResumeSource, String>>, |
| 20 | pub(crate) import: Option<Result<ResumeImportReceipt, String>>, |
| 21 | pub(crate) sanitized_title: Option<String>, |
| 22 | pub(crate) rename: Option<Result<SessionTitleReceipt, String>>, |
| 23 | pub(crate) title_report: Option<TitleReport>, |
| 24 | pub(crate) set_title: Option<Result<(), String>>, |
| 25 | pub(crate) clear_title: Option<Result<(), String>>, |
| 26 | pub(crate) remote_status: Option<String>, |
| 27 | pub(crate) remote_link: Option<Option<RemoteLink>>, |
| 28 | pub(crate) browser_open: Option<RemoteOpenOutcome>, |
| 29 | pub(crate) start_info: Option<RemoteStartInfo>, |
| 30 | pub(crate) stop_refusal: Option<Option<String>>, |
| 31 | pub(crate) hosted: Option<Option<HostedWorkTarget>>, |
| 32 | pub(crate) blockers: Vec<String>, |
| 33 | pub(crate) calls: RefCell<Vec<String>>, |
| 34 | } |
| 35 | |
| 36 | pub(crate) fn message(result: &super::CommandResult) -> &str { |
| 37 | result |
| 38 | .message |
| 39 | .as_deref() |
| 40 | .map(|message| message.strip_prefix("Error: ").unwrap_or(message)) |
| 41 | .unwrap_or("") |
| 42 | } |
| 43 | |
| 44 | impl FakeControl { |
| 45 | fn call(&self, name: &str, arg: Option<&str>) { |
| 46 | let mut calls = self.calls.borrow_mut(); |
| 47 | match arg { |
| 48 | Some(arg) => calls.push(format!("{name}({arg})")), |
| 49 | None => calls.push(name.to_string()), |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl CommandSessionControlContext for FakeControl { |
| 55 | fn transition_blocked(&self) -> bool { |
| 56 | self.call("transition_blocked", None); |
| 57 | self.blocked |
| 58 | } |
| 59 | fn transition_blockers(&self) -> Vec<String> { |
| 60 | self.call("transition_blockers", None); |
| 61 | self.blockers.clone() |
| 62 | } |
| 63 | fn relay_projection(&self) -> RelayProjection { |
| 64 | self.call("relay_projection", None); |
| 65 | self.relay.clone().expect("unexpected relay_projection()") |
| 66 | } |
| 67 | fn open_resume_picker(&mut self) { |
| 68 | self.call("open_resume_picker", None); |
| 69 | } |
| 70 | fn resolve_resume_source(&mut self, raw: &str) -> Result<ResumeSource, String> { |
| 71 | self.call("resolve_resume_source", Some(raw)); |
| 72 | match self.resume.clone() { |
| 73 | Some(result) => result, |
| 74 | None => Ok(ResumeSource::NotFound { |
| 75 | raw: raw.to_string(), |
| 76 | error: "missing".to_string(), |
| 77 | }), |
| 78 | } |
| 79 | } |
| 80 | fn import_session_file(&mut self, path: PathBuf) -> Result<ResumeImportReceipt, String> { |
| 81 | self.call("import_session_file", Some(&path.display().to_string())); |
| 82 | match self.import.clone() { |
| 83 | Some(result) => result, |
| 84 | None => Err(format!( |
| 85 | "unexpected import_session_file({}) on empty fake", |
| 86 | path.display() |
| 87 | )), |
| 88 | } |
| 89 | } |
| 90 | fn sanitize_session_title(&self, raw: &str) -> String { |
| 91 | self.call("sanitize_session_title", Some(raw)); |
| 92 | self.sanitized_title |
| 93 | .clone() |
| 94 | .unwrap_or_else(|| raw.to_string()) |
| 95 | } |
| 96 | fn rename_session(&mut self, title: &str) -> Result<SessionTitleReceipt, String> { |
| 97 | self.call("rename_session", Some(title)); |
| 98 | match self.rename.clone() { |
| 99 | Some(result) => result, |
| 100 | None => Err(format!("unexpected rename_session({title}) on empty fake")), |
| 101 | } |
| 102 | } |
| 103 | fn title_report(&self) -> TitleReport { |
| 104 | self.call("title_report", None); |
| 105 | self.title_report |
| 106 | .clone() |
| 107 | .expect("unexpected title_report()") |
| 108 | } |
| 109 | fn set_window_title(&mut self, title: String) -> Result<(), String> { |
| 110 | self.call("set_window_title", Some(&title)); |
| 111 | match self.set_title.clone() { |
| 112 | Some(result) => result, |
| 113 | None => Err("unexpected set_window_title on empty fake".to_string()), |
| 114 | } |
| 115 | } |
| 116 | fn clear_window_title(&mut self) -> Result<(), String> { |
| 117 | self.call("clear_window_title", None); |
| 118 | match self.clear_title.clone() { |
| 119 | Some(result) => result, |
| 120 | None => Err("unexpected clear_window_title on empty fake".to_string()), |
| 121 | } |
| 122 | } |
| 123 | fn composer_color(&self) -> Option<String> { |
| 124 | self.call("composer_color", None); |
| 125 | None |
| 126 | } |
| 127 | fn set_composer_color(&mut self, color: Option<String>) -> Result<(), String> { |
| 128 | self.call("set_composer_color", color.as_deref()); |
| 129 | Err("unexpected set_composer_color on empty fake".to_string()) |
| 130 | } |
| 131 | fn remote_status(&self) -> String { |
| 132 | self.call("remote_status", None); |
| 133 | self.remote_status |
| 134 | .clone() |
| 135 | .expect("unexpected remote_status()") |
| 136 | } |
| 137 | fn remote_link(&self) -> Option<RemoteLink> { |
| 138 | self.call("remote_link", None); |
| 139 | self.remote_link.clone().expect("unexpected remote_link()") |
| 140 | } |
| 141 | fn remote_browser_open(&self) -> RemoteOpenOutcome { |
| 142 | self.call("remote_browser_open", None); |
| 143 | self.browser_open |
| 144 | .clone() |
| 145 | .expect("unexpected browser_open()") |
| 146 | } |
| 147 | fn remote_start_info(&self) -> RemoteStartInfo { |
| 148 | self.call("remote_start_info", None); |
| 149 | self.start_info.clone().expect("unexpected start_info()") |
| 150 | } |
| 151 | fn remote_stop_refusal(&self) -> Option<String> { |
| 152 | self.call("remote_stop_refusal", None); |
| 153 | self.stop_refusal |
| 154 | .clone() |
| 155 | .expect("unexpected stop_refusal()") |
| 156 | } |
| 157 | fn resolve_hosted_work_target(&self) -> Option<HostedWorkTarget> { |
| 158 | self.call("resolve_hosted_work_target", None); |
| 159 | self.hosted.clone().expect("unexpected hosted()") |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | pub(crate) fn relay_projection_fixture() -> RelayProjection { |
| 164 | RelayProjection { |
| 165 | compact_template: "# Session relay".to_string(), |
| 166 | handoff_path: ".codewhale/handoff.md".to_string(), |
| 167 | workspace: "/work".to_string(), |
| 168 | mode: "operate".to_string(), |
| 169 | model: "model-x".to_string(), |
| 170 | goal_objective: Some("objective-y".to_string()), |
| 171 | goal_token_budget: Some(900), |
| 172 | todos: TodoProjection::Absent, |
| 173 | plan: PlanProjection::Absent, |
| 174 | } |
| 175 | } |
| 176 |