返回 CodeWhale
lifecycle_test_support.rs
根目录 / crates / tui / src / commands / groups / session / lifecycle_test_support.rs
1 //! FEAT-023 Phase 4/6 test support: a deterministic canned implementation of
2 //! `CommandSessionLifecycleContext` so portable handlers are unit-tested for
3 //! exact message/action composition without host state.
4
5 use std::cell::Cell;
6
7 use codewhale_command_contract::facets::{
8 CommandSessionLifecycleContext, SessionArchiveReceipt, SessionBranchOutcome,
9 SessionForkFromReceipt, SessionForkReceipt, SessionNewReceipt, SessionSaveReceipt,
10 SessionSyncPayload, TreeBodyProjection,
11 };
12 use std::path::PathBuf;
13
14 /// Every delegate returns the canned value set by the test and records its
15 /// arguments so handler-routing assertions cannot pass without the expected
16 /// facet call. Unconfigured result slots return a descriptive canned error.
17 pub(crate) struct CannedLifecycle {
18 pub blocked: bool,
19 pub blockers: Vec<String>,
20 pub transition_checks: Cell<usize>,
21 pub leaf_hint: Option<String>,
22 pub branch: Result<SessionBranchOutcome, String>,
23 pub tree: Result<TreeBodyProjection, String>,
24 pub save: Result<SessionSaveReceipt, String>,
25 pub fork_active: Result<SessionForkReceipt, String>,
26 pub fork_from: Result<SessionForkFromReceipt, String>,
27 pub fresh: Result<SessionNewReceipt, String>,
28 pub load: Result<PathBuf, String>,
29 pub archived: Result<SessionArchiveReceipt, String>,
30 pub prune: Result<usize, String>,
31 pub branch_entries: Vec<String>,
32 pub save_paths: Vec<Option<String>>,
33 pub fork_sources: Vec<String>,
34 pub fresh_forces: Vec<bool>,
35 pub fresh_modes: Vec<Option<String>>,
36 pub load_paths: Vec<String>,
37 pub picker_calls: Vec<Option<String>>,
38 pub archive_calls: Vec<(String, bool)>,
39 pub prune_days: Vec<u64>,
40 }
41
42 impl Default for CannedLifecycle {
43 fn default() -> Self {
44 Self {
45 blocked: false,
46 blockers: Vec::new(),
47 transition_checks: Cell::new(0),
48 leaf_hint: None,
49 branch: Err("canned: branch_to not configured".to_string()),
50 tree: Ok(TreeBodyProjection::NoSession),
51 save: Err("canned: save not configured".to_string()),
52 fork_active: Err("canned: fork_active not configured".to_string()),
53 fork_from: Err("canned: fork_from not configured".to_string()),
54 fresh: Err("canned: fresh_session not configured".to_string()),
55 load: Err("canned: load not configured".to_string()),
56 archived: Err("canned: set_archived not configured".to_string()),
57 prune: Err("canned: prune not configured".to_string()),
58 branch_entries: Vec::new(),
59 save_paths: Vec::new(),
60 fork_sources: Vec::new(),
61 fresh_forces: Vec::new(),
62 fresh_modes: Vec::new(),
63 load_paths: Vec::new(),
64 picker_calls: Vec::new(),
65 archive_calls: Vec::new(),
66 prune_days: Vec::new(),
67 }
68 }
69 }
70
71 pub(crate) fn sync_payload(session_id: &str) -> SessionSyncPayload {
72 SessionSyncPayload {
73 session_id: Some(session_id.to_string()),
74 messages: vec![],
75 system_prompt: None,
76 model: "test-model".to_string(),
77 workspace: PathBuf::from("/workspace"),
78 mode: codewhale_command_contract::types::CommandMode::Agent,
79 agent_preset: None,
80 }
81 }
82
83 impl CommandSessionLifecycleContext for CannedLifecycle {
84 fn transition_blocked(&self) -> bool {
85 self.transition_checks
86 .set(self.transition_checks.get().saturating_add(1));
87 self.blocked
88 }
89 fn transition_blockers(&self) -> Vec<String> {
90 self.blockers.clone()
91 }
92 fn branch_current_leaf_hint(&self) -> Option<String> {
93 self.leaf_hint.clone()
94 }
95 fn branch_to(&mut self, entry_id: &str) -> Result<SessionBranchOutcome, String> {
96 self.branch_entries.push(entry_id.to_string());
97 self.branch.clone()
98 }
99 fn tree_body(&self) -> Result<TreeBodyProjection, String> {
100 self.tree.clone()
101 }
102 fn save_session(&mut self, path: Option<String>) -> Result<SessionSaveReceipt, String> {
103 self.save_paths.push(path);
104 self.save.clone()
105 }
106 fn fork_active(&mut self) -> Result<SessionForkReceipt, String> {
107 self.fork_active.clone()
108 }
109 fn fork_from(&mut self, id: &str) -> Result<SessionForkFromReceipt, String> {
110 self.fork_sources.push(id.to_string());
111 self.fork_from.clone()
112 }
113 fn fresh_session(
114 &mut self,
115 force: bool,
116 conversation_mode: Option<&str>,
117 ) -> Result<SessionNewReceipt, String> {
118 self.fresh_forces.push(force);
119 self.fresh_modes.push(conversation_mode.map(str::to_string));
120 self.fresh.clone()
121 }
122 fn load_session(&mut self, path: &str) -> Result<PathBuf, String> {
123 self.load_paths.push(path.to_string());
124 self.load.clone()
125 }
126 fn open_picker(&mut self, preselected: Option<String>) {
127 self.picker_calls.push(preselected);
128 }
129 fn set_archived(&mut self, id: &str, archived: bool) -> Result<SessionArchiveReceipt, String> {
130 self.archive_calls.push((id.to_string(), archived));
131 self.archived.clone()
132 }
133 fn prune_sessions(&mut self, days: u64) -> Result<usize, String> {
134 self.prune_days.push(days);
135 self.prune.clone()
136 }
137 }
138
138 lines RUST