返回 CodeWhale
marketplace_tests.rs
根目录 / crates / tui / src / commands / groups / plugins / marketplace_tests.rs
1 //! Command-surface tests for `/plugin marketplace` (#5311): add/list/show/
2 //! remove over real catalog schemas, install routed through the existing
3 //! reviewed installer, and the no-auto-anything invariants.
4
5 use super::*;
6 use crate::tui::app::{App, TuiOptions};
7 use codewhale_localization::Locale;
8 use codewhale_runtime::config::Config;
9 use std::fs;
10 use std::path::Path;
11 use tempfile::TempDir;
12
13 fn create_test_app(root: &Path) -> (App, TempDir) {
14 let temp = TempDir::new().expect("tempdir");
15 let tools_dir = root.join("tools");
16 fs::create_dir_all(&tools_dir).unwrap();
17 let config_path = temp.path().join("config.toml");
18 fs::write(&config_path, "[tools]\n").unwrap();
19 let options = TuiOptions {
20 config_path: Some(config_path),
21 skills_dir: temp.path().join("skills"),
22 memory_path: temp.path().join("memory.md"),
23 mcp_config_path: temp.path().join("mcp.json"),
24 ..crate::tui::test_support::test_tui_options(root)
25 };
26 let config = Config::default();
27 let discovery = codewhale_plugins::PluginDiscoveryContext::capture_pre_dotenv();
28 let registry = discovery.registry_for_workspace(root);
29 let mut app = App::new_with_plugin_registry(options, &config, registry);
30 app.ui_locale = Locale::En;
31 (app, temp)
32 }
33
34 /// A real-schema Kimi catalog: two entries, one local (installable via the
35 /// reviewed installer once a bundle exists) and one zip URL (honestly
36 /// unsupported until fetch support exists).
37 fn write_kimi_catalog(dir: &Path) -> std::path::PathBuf {
38 let catalog = serde_json::json!({
39 "version": "2",
40 "plugins": [
41 {
42 "id": "demo-bundle",
43 "source": "./demo-bundle",
44 "tier": "official",
45 "displayName": "Demo Bundle",
46 "version": "1.2.3",
47 "description": "Local demo bundle",
48 "homepage": "https://example.invalid/demo",
49 "keywords": ["demo"]
50 },
51 {
52 "id": "remote-thing",
53 "source": "https://example.invalid/remote-thing.zip",
54 "tier": "curated",
55 "displayName": "Remote Thing"
56 }
57 ]
58 });
59 let path = dir.join("kimi-marketplace.json");
60 fs::write(&path, serde_json::to_string_pretty(&catalog).unwrap()).unwrap();
61 path
62 }
63
64 fn write_demo_bundle(dir: &Path) {
65 let bundle = dir.join("demo-bundle");
66 fs::create_dir_all(bundle.join("skills/hello")).unwrap();
67 fs::write(
68 bundle.join("plugin.toml"),
69 "schema_version = 1\n[plugin]\nname = \"demo-bundle\"\nversion = \"1.0.0\"\ndescription = \"Demo\"\n[skills]\npath = \"skills\"\n",
70 )
71 .unwrap();
72 fs::write(
73 bundle.join("skills/hello/SKILL.md"),
74 "---\nname: hello\ndescription: hello\n---\nbody\n",
75 )
76 .unwrap();
77 }
78
79 fn marketplace_state_path(codewhale_home: &Path) -> std::path::PathBuf {
80 codewhale_home.join("plugins/marketplaces.json")
81 }
82
83 #[test]
84 fn marketplace_add_list_show_remove_roundtrip() {
85 let _lock = crate::test_support::lock_test_env();
86 let root = TempDir::new().unwrap();
87 let codewhale_home = root.path().join("home");
88 let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", &codewhale_home);
89 let (mut app, _temp) = create_test_app(root.path());
90 let catalogs = root.path().join("catalogs");
91 fs::create_dir_all(&catalogs).unwrap();
92 let catalog_path = write_kimi_catalog(&catalogs);
93
94 // Usage errors are honest before anything is touched.
95 assert!(!plugins_with_kimi_home_override(&mut app, Some("marketplace"), None).is_error); // list, empty
96 assert!(plugins_with_kimi_home_override(&mut app, Some("marketplace add"), None).is_error);
97 assert!(
98 plugins_with_kimi_home_override(&mut app, Some("marketplace add 'bad name' x"), None)
99 .is_error
100 );
101
102 let added = plugins_with_kimi_home_override(
103 &mut app,
104 Some(&format!("marketplace add kimi {}", catalog_path.display())),
105 None,
106 );
107 assert!(!added.is_error, "{:?}", added.message);
108 let message = added.message.unwrap();
109 assert!(message.contains("Added marketplace `kimi`"), "{message}");
110 assert!(message.contains("2 candidate(s)"), "{message}");
111 assert!(message.contains("display-only"), "{message}");
112 assert!(marketplace_state_path(&codewhale_home).exists());
113
114 let list = plugins_with_kimi_home_override(&mut app, Some("marketplace list"), None)
115 .message
116 .unwrap();
117 eprintln!("LIST2 >>>{list}<<<");
118 assert!(list.contains("`kimi`"), "{list}");
119 assert!(list.contains(r"demo\-bundle"), "{list}");
120 assert!(list.contains(r"remote\-thing"), "{list}");
121 assert!(list.contains("tier=official"), "{list}");
122 assert!(list.contains("tier=curated"), "{list}");
123
124 // Stored plans keep stable codes; rendering resolves the current locale.
125 app.ui_locale = Locale::Es419;
126 let localized = plugins_with_kimi_home_override(&mut app, Some("marketplace list"), None)
127 .message
128 .unwrap();
129 assert!(localized.contains("no admite paquetes ZIP"), "{localized}");
130 assert!(!localized.contains("kimi_zip_unsupported"), "{localized}");
131 assert!(
132 !localized.contains("ZIP bundles are not supported"),
133 "{localized}"
134 );
135 app.ui_locale = Locale::En;
136
137 let show = plugins_with_kimi_home_override(&mut app, Some("marketplace show kimi"), None)
138 .message
139 .unwrap();
140 assert!(show.contains("Demo Bundle"), "{show}");
141 assert!(show.contains(r"v1\.2\.3"), "{show}");
142 assert!(show.contains("catalogs"), "{show}");
143
144 // read-only verbs never rewrite the store
145 let before = fs::read_to_string(marketplace_state_path(&codewhale_home)).unwrap();
146 plugins_with_kimi_home_override(&mut app, Some("marketplace list"), None);
147 plugins_with_kimi_home_override(&mut app, Some("marketplace show kimi"), None);
148 let after = fs::read_to_string(marketplace_state_path(&codewhale_home)).unwrap();
149 assert_eq!(
150 before, after,
151 "list/show must not rewrite marketplace state"
152 );
153
154 // Re-adding the same source is a refresh, not a duplicate: a catalog is
155 // keyed by its document, so the second add updates it in place. (The old
156 // refusal here is what produced a second snapshot of one marketplace
157 // under a hand-made name.)
158 let refreshed = plugins_with_kimi_home_override(
159 &mut app,
160 Some(&format!("marketplace add kimi {}", catalog_path.display())),
161 None,
162 );
163 assert!(!refreshed.is_error, "{:?}", refreshed.message);
164 // A *different* source under the same name is still refused.
165 let other_catalog = catalogs.join("other-marketplace.json");
166 fs::write(
167 &other_catalog,
168 serde_json::to_string_pretty(&serde_json::json!({
169 "version": "2",
170 "plugins": [
171 {
172 "id": "other-bundle",
173 "source": "./other-bundle",
174 "displayName": "Other Bundle"
175 }
176 ]
177 }))
178 .unwrap(),
179 )
180 .unwrap();
181 let clash = plugins_with_kimi_home_override(
182 &mut app,
183 Some(&format!("marketplace add kimi {}", other_catalog.display())),
184 None,
185 );
186 assert!(clash.is_error, "{:?}", clash.message);
187
188 let removed = plugins_with_kimi_home_override(&mut app, Some("marketplace remove kimi"), None);
189 assert!(!removed.is_error, "{:?}", removed.message);
190 assert!(
191 removed
192 .message
193 .unwrap()
194 .contains("Installed plugins and their trust state are unaffected")
195 );
196 assert!(
197 plugins_with_kimi_home_override(&mut app, Some("marketplace show kimi"), None).is_error
198 );
199 let empty = plugins_with_kimi_home_override(&mut app, Some("marketplace list"), None)
200 .message
201 .unwrap();
202 assert!(
203 empty.contains("codewhale") && empty.contains("whalewiki"),
204 "{empty}"
205 );
206 }
207
208 #[test]
209 fn plugin_suggest_preserves_current_main_marketplace_candidates() {
210 let _lock = crate::test_support::lock_test_env();
211 let root = TempDir::new().unwrap();
212 let codewhale_home = root.path().join("home");
213 let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", &codewhale_home);
214 let (mut app, _temp) = create_test_app(root.path());
215 let catalogs = root.path().join("catalogs");
216 fs::create_dir_all(&catalogs).unwrap();
217 let catalog_path = write_kimi_catalog(&catalogs);
218
219 let added = plugins_with_kimi_home_override(
220 &mut app,
221 Some(&format!("marketplace add kimi {}", catalog_path.display())),
222 None,
223 );
224 assert!(!added.is_error, "{:?}", added.message);
225
226 let suggested = plugins_with_kimi_home_override(&mut app, Some("suggest demo"), None);
227 assert!(!suggested.is_error, "{suggested:?}");
228 let message = suggested.message.expect("suggestion message");
229 assert!(message.contains("Suggested plugins"), "{message}");
230 assert!(
231 message.contains(r"demo\-bundle — not installed"),
232 "{message}"
233 );
234 assert!(
235 message.contains("/plugin marketplace install kimi demo-bundle"),
236 "{message}"
237 );
238 assert!(message.contains("Nothing was installed, trusted, or enabled."));
239 assert!(app.plugin_registry.get("demo-bundle").is_none());
240 }
241
242 #[test]
243 fn marketplace_add_rejects_symlinks_and_bad_documents() {
244 let _lock = crate::test_support::lock_test_env();
245 let root = TempDir::new().unwrap();
246 let codewhale_home = root.path().join("home");
247 let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", &codewhale_home);
248 let (mut app, _temp) = create_test_app(root.path());
249 let catalogs = root.path().join("catalogs");
250 fs::create_dir_all(&catalogs).unwrap();
251 let catalog_path = write_kimi_catalog(&catalogs);
252
253 // missing file
254 let missing = plugins_with_kimi_home_override(
255 &mut app,
256 Some("marketplace add nope /nonexistent/x.json"),
257 None,
258 );
259 assert!(missing.is_error);
260
261 // symlink to a real catalog is refused, not followed
262 let link = catalogs.join("link.json");
263 #[cfg(unix)]
264 std::os::unix::fs::symlink(&catalog_path, &link).unwrap();
265 #[cfg(not(unix))]
266 fs::copy(&catalog_path, &link).unwrap();
267 #[cfg(unix)]
268 {
269 let symlinked = plugins_with_kimi_home_override(
270 &mut app,
271 Some(&format!("marketplace add evil {}", link.display())),
272 None,
273 );
274 assert!(symlinked.is_error);
275 assert!(symlinked.message.unwrap().contains("symlink"));
276 }
277
278 // a document with no documented markers fails honestly and is not stored
279 let junk = catalogs.join("junk.json");
280 fs::write(&junk, "{\"hello\": \"world\"}").unwrap();
281 let bad = plugins_with_kimi_home_override(
282 &mut app,
283 Some(&format!("marketplace add junk {}", junk.display())),
284 None,
285 );
286 assert!(bad.is_error);
287 assert!(bad.message.unwrap().contains("could not be parsed"));
288 assert!(
289 plugins_with_kimi_home_override(&mut app, Some("marketplace list"), None)
290 .message
291 .unwrap()
292 .contains("codewhale")
293 );
294
295 // corrupt stored state fails closed and is never rewritten
296 let store_path = marketplace_state_path(&codewhale_home);
297 fs::create_dir_all(store_path.parent().unwrap()).unwrap();
298 fs::write(&store_path, "{ not json").unwrap();
299 let corrupt = plugins_with_kimi_home_override(&mut app, Some("marketplace list"), None);
300 assert!(corrupt.is_error);
301 assert!(corrupt.message.unwrap().contains("fail-closed"));
302 assert_eq!(fs::read_to_string(&store_path).unwrap(), "{ not json");
303 }
304
305 #[test]
306 fn marketplace_install_routes_through_reviewed_installer() {
307 let _lock = crate::test_support::lock_test_env();
308 let root = TempDir::new().unwrap();
309 let codewhale_home = root.path().join("home");
310 let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", &codewhale_home);
311 let (mut app, _temp) = create_test_app(root.path());
312 let catalogs = root.path().join("catalogs");
313 fs::create_dir_all(&catalogs).unwrap();
314 write_kimi_catalog(&catalogs);
315 write_demo_bundle(&catalogs);
316 assert!(
317 !plugins_with_kimi_home_override(
318 &mut app,
319 Some("marketplace add kimi catalogs/kimi-marketplace.json"),
320 None
321 )
322 .is_error
323 );
324
325 // The unsupported plan is refused before any runtime or network work.
326 let remote = plugins_with_kimi_home_override(
327 &mut app,
328 Some("marketplace install kimi remote-thing"),
329 None,
330 );
331 assert!(remote.is_error);
332 assert!(remote.message.unwrap().contains("cannot be installed"));
333
334 let runtime = tokio::runtime::Builder::new_multi_thread()
335 .worker_threads(2)
336 .enable_all()
337 .build()
338 .unwrap();
339 runtime.block_on(async {
340 let installed = plugins_with_kimi_home_override(
341 &mut app,
342 Some("marketplace install kimi demo-bundle"),
343 None,
344 );
345 assert!(!installed.is_error, "{:?}", installed.message);
346 let message = installed.message.unwrap();
347 assert!(message.contains("disabled and untrusted"), "{message}");
348 assert!(
349 message
350 .lines()
351 .any(|line| line.starts_with("/plugin trust demo-bundle ")),
352 "marketplace install must route into the trust review:\n{message}"
353 );
354
355 // No auto-trust, no auto-enable, no inherited vendor trust.
356 let plugin = app.plugin_registry.get("demo-bundle").unwrap();
357 assert!(!plugin.enabled);
358 assert!(!plugin.trusted());
359 assert!(
360 codewhale_home
361 .join("plugins/demo-bundle/.installed-from")
362 .exists()
363 );
364 });
365 }
366
367 /// A Codex catalog whose entry declares `INSTALLED_BY_DEFAULT`: the policy is
368 /// visible but nothing is auto-installed, and the npm source stays honestly
369 /// unsupported.
370 #[test]
371 fn marketplace_codex_installed_by_default_never_auto_installs() {
372 let _lock = crate::test_support::lock_test_env();
373 let root = TempDir::new().unwrap();
374 let codewhale_home = root.path().join("home");
375 let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", &codewhale_home);
376 let (mut app, _temp) = create_test_app(root.path());
377 let catalogs = root.path().join("catalogs");
378 fs::create_dir_all(&catalogs).unwrap();
379 let codex = serde_json::json!({
380 "name": "codex-catalog",
381 "plugins": [
382 {
383 "name": "defaulted-thing",
384 "source": { "source": "npm", "package": "@scope/defaulted-thing" },
385 "policy": { "installation": "INSTALLED_BY_DEFAULT" }
386 }
387 ]
388 });
389 let path = catalogs.join("codex-marketplace.json");
390 fs::write(&path, serde_json::to_string_pretty(&codex).unwrap()).unwrap();
391
392 let added = plugins_with_kimi_home_override(
393 &mut app,
394 Some(&format!("marketplace add codex {}", path.display())),
395 None,
396 );
397 assert!(!added.is_error, "{:?}", added.message);
398
399 let list = plugins_with_kimi_home_override(&mut app, Some("marketplace list"), None)
400 .message
401 .unwrap();
402 assert!(list.contains(r"defaulted\-thing"), "{list}");
403 assert!(list.contains("not installable"), "{list}");
404 assert!(list.contains("npm"), "{list}");
405 // Foreign auto-install policy never ran anything: no bundle on disk.
406 assert!(!codewhale_home.join("plugins/defaulted-thing").exists());
407 assert!(app.plugin_registry.get("defaulted-thing").is_none());
408 }
409
409 lines RUST