返回 CodeWhale
discovery.rs
根目录 / crates / tui / src / commands / discovery.rs
1 //! Shared command-discovery shadowing contract.
2 //!
3 //! Both the command palette (`tui::command_palette`) and slash completion
4 //! (`tui::widgets`) must interpret user-command ownership over built-in
5 //! command tokens identically. This module owns the three decisions they
6 //! share:
7 //!
8 //! - whether a user command (by canonical name or accepted alias) shadows a
9 //! built-in canonical token,
10 //! - whether a user command shadows a specific built-in alias token, and
11 //! - which built-in aliases remain unshadowed and may be presented.
12 //!
13 //! The functions are pure: they consume immutable built-in metadata and
14 //! accepted user-command metadata and never persist or cache registry state.
15
16 use super::traits::CommandInfo;
17 use super::user_registry::UserCommandMetadata;
18
19 /// Returns true when any user command claims the built-in canonical token,
20 /// either through its canonical name or through an accepted alias.
21 ///
22 /// Hidden user commands retain token ownership even though they are excluded
23 /// from discovery output rows.
24 pub fn user_command_shadows_builtin_canonical(
25 builtin: &CommandInfo,
26 user_commands: &[&UserCommandMetadata],
27 ) -> bool {
28 user_commands.iter().any(|user| {
29 user.name == builtin.name || user.aliases.iter().any(|alias| alias == builtin.name)
30 })
31 }
32
33 /// Returns true when any user command claims the given built-in alias token,
34 /// either through its canonical name or through an accepted alias.
35 pub fn user_command_shadows_builtin_alias(
36 builtin_alias: &str,
37 user_commands: &[&UserCommandMetadata],
38 ) -> bool {
39 user_commands.iter().any(|user| {
40 user.name == builtin_alias || user.aliases.iter().any(|alias| alias == builtin_alias)
41 })
42 }
43
44 /// Returns the built-in aliases that are not claimed by any user command,
45 /// preserving the built-in declaration order.
46 ///
47 /// The built-in canonical token itself is not part of this projection; callers
48 /// decide canonical visibility through [`user_command_shadows_builtin_canonical`].
49 pub fn unshadowed_builtin_aliases<'a>(
50 builtin: &'a CommandInfo,
51 user_commands: &[&UserCommandMetadata],
52 ) -> Vec<&'a str> {
53 builtin
54 .aliases
55 .iter()
56 .copied()
57 .filter(|alias| !user_command_shadows_builtin_alias(alias, user_commands))
58 .collect()
59 }
60
61 #[cfg(test)]
62 mod tests {
63 use super::*;
64
65 use crate::commands::get_command_info;
66
67 fn help_builtin() -> &'static CommandInfo {
68 get_command_info("help").expect("built-in help must be registered")
69 }
70
71 fn attach_builtin() -> &'static CommandInfo {
72 get_command_info("attach").expect("built-in attach must be registered")
73 }
74
75 fn metadata(name: &str, aliases: &[&str], hidden: bool) -> UserCommandMetadata {
76 UserCommandMetadata {
77 name: name.to_string(),
78 body: String::new(),
79 description: Some(format!("description of {name}")),
80 usage: None,
81 arguments: None,
82 argument_hint: None,
83 allowed_tools: None,
84 pausable: false,
85 aliases: aliases.iter().map(|s| s.to_string()).collect(),
86 hidden,
87 plugin_authority: None,
88 workspace_sourced: false,
89 extension: None,
90 }
91 }
92
93 fn slice(commands: &[UserCommandMetadata]) -> Vec<&UserCommandMetadata> {
94 commands.iter().collect()
95 }
96
97 #[test]
98 fn canonical_name_claims_builtin_canonical_token() {
99 let user = metadata("help", &[], false);
100 let owned = [user];
101 let users = slice(&owned);
102 assert!(user_command_shadows_builtin_canonical(
103 help_builtin(),
104 &users
105 ));
106 }
107
108 #[test]
109 fn accepted_alias_claims_builtin_canonical_token() {
110 let user = metadata("assistant", &["help"], false);
111 let owned = [user];
112 let users = slice(&owned);
113 assert!(user_command_shadows_builtin_canonical(
114 help_builtin(),
115 &users
116 ));
117 }
118
119 #[test]
120 fn hidden_user_command_retains_canonical_shadow_ownership() {
121 let user = metadata("help", &[], true);
122 let owned = [user];
123 let users = slice(&owned);
124 assert!(user_command_shadows_builtin_canonical(
125 help_builtin(),
126 &users
127 ));
128 }
129
130 #[test]
131 fn unrelated_user_commands_do_not_shadow() {
132 let user = metadata("assistant", &["a"], false);
133 let owned = [user];
134 let users = slice(&owned);
135 assert!(!user_command_shadows_builtin_canonical(
136 help_builtin(),
137 &users
138 ));
139 assert!(!user_command_shadows_builtin_alias("?", &users));
140 }
141
142 #[test]
143 fn canonical_name_claims_builtin_alias_token() {
144 let user = metadata("?", &[], false);
145 let owned = [user];
146 let users = slice(&owned);
147 assert!(user_command_shadows_builtin_alias("?", &users));
148 }
149
150 #[test]
151 fn accepted_alias_claims_builtin_alias_token() {
152 let user = metadata("assistant", &["image"], false);
153 let owned = [user];
154 let users = slice(&owned);
155 assert!(user_command_shadows_builtin_alias("image", &users));
156 }
157
158 #[test]
159 fn unshadowed_aliases_preserve_declaration_order() {
160 let user = metadata("assistant", &["image"], false);
161 let owned = [user];
162 let users = slice(&owned);
163 let aliases = unshadowed_builtin_aliases(attach_builtin(), &users);
164 assert_eq!(aliases, vec!["media", "fujian"]);
165 }
166
167 #[test]
168 fn claimed_canonical_token_does_not_change_alias_projection() {
169 // A user command claiming the built-in canonical token makes the whole
170 // built-in invisible; the alias projection stays stable so consumers
171 // can rely on it for the description fallback path.
172 let user = metadata("attach", &[], false);
173 let owned = [user];
174 let users = slice(&owned);
175 let aliases = unshadowed_builtin_aliases(attach_builtin(), &users);
176 assert_eq!(aliases, vec!["image", "media", "fujian"]);
177 }
178
179 #[test]
180 fn hidden_commands_shadow_aliases_too() {
181 let user = metadata("secret", &["image"], true);
182 let owned = [user];
183 let users = slice(&owned);
184 assert!(user_command_shadows_builtin_alias("image", &users));
185 let aliases = unshadowed_builtin_aliases(attach_builtin(), &users);
186 assert_eq!(aliases, vec!["media", "fujian"]);
187 }
188
189 #[test]
190 fn all_aliases_shadowed_yields_empty_projection() {
191 let user = metadata("assistant", &["image", "media", "fujian"], false);
192 let owned = [user];
193 let users = slice(&owned);
194 let aliases = unshadowed_builtin_aliases(attach_builtin(), &users);
195 assert!(aliases.is_empty());
196 }
197
198 #[test]
199 fn rejected_aliases_do_not_shadow() {
200 // Rejected aliases are absent from accepted metadata and therefore
201 // cannot claim any token.
202 let user = metadata("assistant", &[], false);
203 let owned = [user];
204 let users = slice(&owned);
205 assert!(!user_command_shadows_builtin_alias("collision", &users));
206 }
207
208 #[test]
209 fn empty_metadata_shadows_nothing() {
210 let users: Vec<&UserCommandMetadata> = Vec::new();
211 assert!(!user_command_shadows_builtin_canonical(
212 help_builtin(),
213 &users
214 ));
215 assert!(!user_command_shadows_builtin_alias("?", &users));
216 let aliases = unshadowed_builtin_aliases(help_builtin(), &users);
217 assert_eq!(aliases, vec!["?", "bangzhu", "帮助"]);
218 }
219
220 #[test]
221 fn discovery_predicates_agree_with_registry_lookup() {
222 // Contract guard: the shared predicates must agree with the registry's
223 // own alias-aware `get` lookup used by the palette, so the Phase 3/4
224 // rewiring cannot introduce a behavioral divergence.
225 let user = metadata("assistant", &["help"], false);
226 let owned = [user];
227 let users = slice(&owned);
228 let registry = crate::commands::user_registry::UserCommandRegistry::from_loaded(vec![(
229 "assistant".to_string(),
230 "---\ndescription: d\naliases: help\n---\nbody".to_string(),
231 )]);
232 assert_eq!(
233 registry.get("help").is_some(),
234 user_command_shadows_builtin_canonical(help_builtin(), &users),
235 "registry lookup and shared predicate must agree"
236 );
237 }
238 }
239
239 lines RUST