| 1 | //! One-line install summary shared by every install surface. |
| 2 | //! |
| 3 | //! A pure builder: everything it needs arrives in [`SummaryInput`], nothing is |
| 4 | //! read from disk, the registry or the environment. Each clause carries a |
| 5 | //! stable message id so a surface can localize the clause while the English |
| 6 | //! text here stays the fallback. |
| 7 | |
| 8 | use std::collections::BTreeSet; |
| 9 | |
| 10 | use super::dsh::DshConversion; |
| 11 | use crate::host_services::{HostService, HostServiceReview, PluginApprover, approver_label}; |
| 12 | use crate::manifest::PluginInventory; |
| 13 | |
| 14 | pub const MSG_HEAD: &str = "plugin.summary.head"; |
| 15 | pub const MSG_STATE_ON: &str = "plugin.summary.state_on"; |
| 16 | pub const MSG_STATE_OFF: &str = "plugin.summary.state_off"; |
| 17 | pub const MSG_APPROVER: &str = "plugin.summary.approver"; |
| 18 | pub const MSG_ADDS: &str = "plugin.summary.adds"; |
| 19 | pub const MSG_ADDS_NOTHING_NEW: &str = "plugin.summary.adds_nothing_new"; |
| 20 | pub const MSG_NOW_ALSO_ADDS: &str = "plugin.summary.now_also_adds"; |
| 21 | pub const MSG_WONT_RUN: &str = "plugin.summary.wont_run"; |
| 22 | pub const MSG_NEEDS_EXTENSION_HOST: &str = "plugin.summary.needs_extension_host"; |
| 23 | pub const MSG_NEEDS_NODE: &str = "plugin.summary.needs_node"; |
| 24 | pub const MSG_REPLACES: &str = "plugin.summary.replaces"; |
| 25 | pub const MSG_DOWNGRADED: &str = "plugin.summary.downgraded"; |
| 26 | pub const MSG_SERVICES: &str = "plugin.summary.services"; |
| 27 | pub const MSG_SERVICES_NOW_ALSO: &str = "plugin.summary.services_now_also"; |
| 28 | pub const MSG_SERVICES_WITHHELD: &str = "plugin.summary.services_withheld"; |
| 29 | pub const MSG_SERVICES_UNOFFERED: &str = "plugin.summary.services_unoffered"; |
| 30 | pub const MSG_SERVICES_PAUSE: &str = "plugin.summary.services_pause"; |
| 31 | pub const MSG_PROVIDER_NEW_SESSION: &str = "plugin.summary.provider_new_session"; |
| 32 | |
| 33 | const HOST_LIST_LIMIT: usize = 3; |
| 34 | const VERSION_GATE_REASON: &str = "incompatible-version"; |
| 35 | |
| 36 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 37 | pub struct SummaryClause { |
| 38 | pub id: &'static str, |
| 39 | pub text: String, |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 43 | pub struct PluginSummary { |
| 44 | pub clauses: Vec<SummaryClause>, |
| 45 | } |
| 46 | |
| 47 | impl PluginSummary { |
| 48 | #[must_use] |
| 49 | pub fn line(&self) -> String { |
| 50 | let mut clauses = self.clauses.iter(); |
| 51 | let Some(head) = clauses.next() else { |
| 52 | return String::new(); |
| 53 | }; |
| 54 | let rest: Vec<&str> = clauses.map(|clause| clause.text.as_str()).collect(); |
| 55 | if rest.is_empty() { |
| 56 | head.text.clone() |
| 57 | } else { |
| 58 | format!("{}: {}", head.text, rest.join("; ")) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #[must_use] |
| 63 | pub fn ids(&self) -> Vec<&'static str> { |
| 64 | self.clauses.iter().map(|clause| clause.id).collect() |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[derive(Debug, Clone)] |
| 69 | pub struct PreviousInstall<'a> { |
| 70 | pub name: &'a str, |
| 71 | pub version: &'a str, |
| 72 | pub source: Option<&'a str>, |
| 73 | pub inventory: &'a PluginInventory, |
| 74 | pub host_services: &'a [HostService], |
| 75 | } |
| 76 | |
| 77 | #[derive(Debug, Clone)] |
| 78 | pub struct SummaryInput<'a> { |
| 79 | pub name: &'a str, |
| 80 | pub version: &'a str, |
| 81 | pub source: Option<&'a str>, |
| 82 | pub enabled: bool, |
| 83 | pub approver: Option<&'a PluginApprover>, |
| 84 | pub inventory: &'a PluginInventory, |
| 85 | pub dsh: Option<&'a DshConversion>, |
| 86 | pub host_services: Option<&'a HostServiceReview>, |
| 87 | pub extension_host_on: bool, |
| 88 | pub previous: Option<PreviousInstall<'a>>, |
| 89 | pub downgrade_allowed: bool, |
| 90 | } |
| 91 | |
| 92 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 93 | struct Capabilities { |
| 94 | skills: usize, |
| 95 | local_mcp: usize, |
| 96 | remote_mcp: usize, |
| 97 | commands: usize, |
| 98 | agents: usize, |
| 99 | hooks: usize, |
| 100 | lsp: usize, |
| 101 | native: usize, |
| 102 | providers: usize, |
| 103 | panels: usize, |
| 104 | filesystem_roots: BTreeSet<String>, |
| 105 | network_hosts: BTreeSet<String>, |
| 106 | lifecycle_mutation: bool, |
| 107 | } |
| 108 | |
| 109 | impl Capabilities { |
| 110 | fn from_inventory(inventory: &PluginInventory) -> Self { |
| 111 | let split = inventory.stdio_mcp_servers + inventory.remote_mcp_servers; |
| 112 | let (local_mcp, remote_mcp) = if split == 0 { |
| 113 | (inventory.mcp_servers, 0) |
| 114 | } else { |
| 115 | ( |
| 116 | inventory.stdio_mcp_servers + inventory.mcp_servers.saturating_sub(split), |
| 117 | inventory.remote_mcp_servers, |
| 118 | ) |
| 119 | }; |
| 120 | Self { |
| 121 | skills: inventory.skills, |
| 122 | local_mcp, |
| 123 | remote_mcp, |
| 124 | commands: inventory.commands, |
| 125 | agents: inventory.agents, |
| 126 | hooks: inventory.hooks, |
| 127 | lsp: inventory.lsp, |
| 128 | native: inventory.native, |
| 129 | providers: inventory.providers, |
| 130 | panels: 0, |
| 131 | filesystem_roots: inventory.filesystem_roots.iter().cloned().collect(), |
| 132 | network_hosts: inventory.network_hosts.iter().cloned().collect(), |
| 133 | lifecycle_mutation: inventory.lifecycle_mutation, |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fn with_dsh(mut self, dsh: &DshConversion) -> Self { |
| 138 | self.skills = self.skills.max(dsh.skills.len()); |
| 139 | self.local_mcp = self.local_mcp.max(dsh.local_servers.len()); |
| 140 | self.remote_mcp = self.remote_mcp.max(dsh.remote_servers.len()); |
| 141 | self.commands = self.commands.max(dsh.commands.len()); |
| 142 | self.agents = self.agents.max(dsh.agents.len()); |
| 143 | self.hooks = self.hooks.max(dsh.hooks.len()); |
| 144 | self.native = self.native.max(dsh.native_rows.len()); |
| 145 | if dsh.requires_native && self.native == 0 { |
| 146 | self.native = 1; |
| 147 | } |
| 148 | self.panels = self.panels.max(dsh.client_closures.len()); |
| 149 | self.network_hosts.extend(dsh.network_hosts.iter().cloned()); |
| 150 | self |
| 151 | } |
| 152 | |
| 153 | fn since(&self, old: &Self) -> Self { |
| 154 | let more = |new: usize, old: usize| new.saturating_sub(old); |
| 155 | Self { |
| 156 | skills: more(self.skills, old.skills), |
| 157 | local_mcp: more(self.local_mcp, old.local_mcp), |
| 158 | remote_mcp: more(self.remote_mcp, old.remote_mcp), |
| 159 | commands: more(self.commands, old.commands), |
| 160 | agents: more(self.agents, old.agents), |
| 161 | hooks: more(self.hooks, old.hooks), |
| 162 | lsp: more(self.lsp, old.lsp), |
| 163 | native: more(self.native, old.native), |
| 164 | providers: more(self.providers, old.providers), |
| 165 | panels: more(self.panels, old.panels), |
| 166 | filesystem_roots: self |
| 167 | .filesystem_roots |
| 168 | .difference(&old.filesystem_roots) |
| 169 | .cloned() |
| 170 | .collect(), |
| 171 | network_hosts: self |
| 172 | .network_hosts |
| 173 | .difference(&old.network_hosts) |
| 174 | .cloned() |
| 175 | .collect(), |
| 176 | lifecycle_mutation: self.lifecycle_mutation && !old.lifecycle_mutation, |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | fn phrases(&self) -> Vec<String> { |
| 181 | let mut phrases = Vec::new(); |
| 182 | let mut count = |n: usize, one: &str, many: &str| { |
| 183 | if n > 0 { |
| 184 | phrases.push(format!("{n} {}", if n == 1 { one } else { many })); |
| 185 | } |
| 186 | }; |
| 187 | count(self.skills, "skill", "skills"); |
| 188 | count(self.local_mcp, "local MCP server", "local MCP servers"); |
| 189 | count(self.remote_mcp, "remote MCP server", "remote MCP servers"); |
| 190 | count(self.commands, "command", "commands"); |
| 191 | count(self.agents, "agent profile", "agent profiles"); |
| 192 | count(self.hooks, "hook", "hooks"); |
| 193 | count(self.lsp, "language server", "language servers"); |
| 194 | count(self.native, "native plugin", "native plugins"); |
| 195 | count(self.providers, "provider", "providers"); |
| 196 | count(self.panels, "panel", "panels"); |
| 197 | if !self.filesystem_roots.is_empty() { |
| 198 | phrases.push(format!("file access ({})", limited(&self.filesystem_roots))); |
| 199 | } |
| 200 | if !self.network_hosts.is_empty() { |
| 201 | phrases.push(format!("network access ({})", limited(&self.network_hosts))); |
| 202 | } |
| 203 | if self.lifecycle_mutation { |
| 204 | phrases.push("permission to change plugins".to_string()); |
| 205 | } |
| 206 | phrases |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | fn limited(items: &BTreeSet<String>) -> String { |
| 211 | let shown: Vec<&str> = items |
| 212 | .iter() |
| 213 | .take(HOST_LIST_LIMIT) |
| 214 | .map(String::as_str) |
| 215 | .collect(); |
| 216 | let mut text = shown.join(", "); |
| 217 | if items.len() > HOST_LIST_LIMIT { |
| 218 | text.push_str(&format!(" and {} more", items.len() - HOST_LIST_LIMIT)); |
| 219 | } |
| 220 | text |
| 221 | } |
| 222 | |
| 223 | fn and_list(items: &[String]) -> String { |
| 224 | match items { |
| 225 | [] => String::new(), |
| 226 | [only] => only.clone(), |
| 227 | [init @ .., last] => format!("{} and {last}", init.join(", ")), |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | fn rows(n: usize) -> String { |
| 232 | format!("{n} {}", if n == 1 { "row" } else { "rows" }) |
| 233 | } |
| 234 | |
| 235 | fn quoted(peers: &[crate::host_services::ReviewPeer]) -> String { |
| 236 | peers |
| 237 | .iter() |
| 238 | .map(|peer| format!("`{}`", peer.name)) |
| 239 | .collect::<Vec<_>>() |
| 240 | .join(", ") |
| 241 | } |
| 242 | |
| 243 | fn push(clauses: &mut Vec<SummaryClause>, id: &'static str, text: String) { |
| 244 | clauses.push(SummaryClause { id, text }); |
| 245 | } |
| 246 | |
| 247 | #[must_use] |
| 248 | pub fn build_summary(input: &SummaryInput<'_>) -> PluginSummary { |
| 249 | let mut clauses = Vec::new(); |
| 250 | |
| 251 | let mut head = input.name.to_string(); |
| 252 | if !input.version.is_empty() { |
| 253 | head.push(' '); |
| 254 | head.push_str(input.version); |
| 255 | } |
| 256 | if let Some(source) = input.source.filter(|source| !source.is_empty()) { |
| 257 | head.push_str(&format!(" from {source}")); |
| 258 | } |
| 259 | push(&mut clauses, MSG_HEAD, head); |
| 260 | |
| 261 | if input.enabled { |
| 262 | push(&mut clauses, MSG_STATE_ON, "on".to_string()); |
| 263 | } else { |
| 264 | push(&mut clauses, MSG_STATE_OFF, "off".to_string()); |
| 265 | } |
| 266 | push( |
| 267 | &mut clauses, |
| 268 | MSG_APPROVER, |
| 269 | approver_label(input.approver).to_string(), |
| 270 | ); |
| 271 | |
| 272 | if let Some(previous) = &input.previous { |
| 273 | if input.downgrade_allowed && previous.version != input.version { |
| 274 | push( |
| 275 | &mut clauses, |
| 276 | MSG_DOWNGRADED, |
| 277 | format!("downgraded from {}", previous.version), |
| 278 | ); |
| 279 | } |
| 280 | if previous.source != input.source { |
| 281 | let from = previous |
| 282 | .source |
| 283 | .filter(|source| !source.is_empty()) |
| 284 | .map_or_else(|| "an unrecorded source".to_string(), str::to_string); |
| 285 | push( |
| 286 | &mut clauses, |
| 287 | MSG_REPLACES, |
| 288 | format!("replaces {} from {from}", previous.name), |
| 289 | ); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | let mut now = Capabilities::from_inventory(input.inventory); |
| 294 | if let Some(dsh) = input.dsh { |
| 295 | now = now.with_dsh(dsh); |
| 296 | } |
| 297 | match &input.previous { |
| 298 | None => { |
| 299 | let phrases = now.phrases(); |
| 300 | if !phrases.is_empty() { |
| 301 | push( |
| 302 | &mut clauses, |
| 303 | MSG_ADDS, |
| 304 | format!("adds {}", and_list(&phrases)), |
| 305 | ); |
| 306 | } |
| 307 | } |
| 308 | Some(previous) => { |
| 309 | let added = now |
| 310 | .since(&Capabilities::from_inventory(previous.inventory)) |
| 311 | .phrases(); |
| 312 | if added.is_empty() { |
| 313 | push( |
| 314 | &mut clauses, |
| 315 | MSG_ADDS_NOTHING_NEW, |
| 316 | "adds nothing new".to_string(), |
| 317 | ); |
| 318 | } else { |
| 319 | push( |
| 320 | &mut clauses, |
| 321 | MSG_NOW_ALSO_ADDS, |
| 322 | format!("now also adds {}", and_list(&added)), |
| 323 | ); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if let Some(dsh) = input.dsh { |
| 329 | let skipped: Vec<_> = dsh |
| 330 | .outcomes |
| 331 | .iter() |
| 332 | .filter(|outcome| outcome.needs_manual_port()) |
| 333 | .collect(); |
| 334 | if !skipped.is_empty() { |
| 335 | let gated = skipped |
| 336 | .iter() |
| 337 | .filter(|outcome| outcome.reason.starts_with(VERSION_GATE_REASON)) |
| 338 | .count(); |
| 339 | let manual = skipped.len() - gated; |
| 340 | let reason = match (gated, manual) { |
| 341 | (0, _) => "each needs a manual port".to_string(), |
| 342 | (_, 0) => "refused by the host version gate".to_string(), |
| 343 | _ => format!( |
| 344 | "{gated} refused by the host version gate, {manual} with no portable form" |
| 345 | ), |
| 346 | }; |
| 347 | push( |
| 348 | &mut clauses, |
| 349 | MSG_WONT_RUN, |
| 350 | format!("won't run: {} ({reason})", rows(skipped.len())), |
| 351 | ); |
| 352 | } |
| 353 | if dsh.requires_node { |
| 354 | push( |
| 355 | &mut clauses, |
| 356 | MSG_NEEDS_NODE, |
| 357 | "needs Node.js for its local servers".to_string(), |
| 358 | ); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if now.native > 0 && !input.extension_host_on { |
| 363 | push( |
| 364 | &mut clauses, |
| 365 | MSG_NEEDS_EXTENSION_HOST, |
| 366 | "needs the plugin host (off)".to_string(), |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | if let Some(review) = input.host_services { |
| 371 | let granted = review.grants(); |
| 372 | let previously: &[HostService] = input |
| 373 | .previous |
| 374 | .as_ref() |
| 375 | .map_or(&[], |previous| previous.host_services); |
| 376 | let names = |services: Vec<HostService>| -> Vec<String> { |
| 377 | services |
| 378 | .into_iter() |
| 379 | .map(|service| service.name().to_string()) |
| 380 | .collect() |
| 381 | }; |
| 382 | match &input.previous { |
| 383 | None => { |
| 384 | if !granted.is_empty() { |
| 385 | let mut text = |
| 386 | format!("uses host services {}", and_list(&names(granted.clone()))); |
| 387 | let mut peers: Vec<_> = review |
| 388 | .lines |
| 389 | .iter() |
| 390 | .filter(|line| line.offered) |
| 391 | .flat_map(|line| line.shared_with.iter()) |
| 392 | .collect(); |
| 393 | peers.sort_by(|a, b| a.id.cmp(&b.id)); |
| 394 | peers.dedup_by(|a, b| a.id == b.id); |
| 395 | if !peers.is_empty() { |
| 396 | let peers: Vec<_> = peers.into_iter().cloned().collect(); |
| 397 | text.push_str(&format!(" (shared with {})", quoted(&peers))); |
| 398 | } |
| 399 | push(&mut clauses, MSG_SERVICES, text); |
| 400 | } |
| 401 | } |
| 402 | Some(_) => { |
| 403 | let fresh: Vec<_> = granted |
| 404 | .iter() |
| 405 | .copied() |
| 406 | .filter(|service| !previously.contains(service)) |
| 407 | .collect(); |
| 408 | if !fresh.is_empty() { |
| 409 | push( |
| 410 | &mut clauses, |
| 411 | MSG_SERVICES_NOW_ALSO, |
| 412 | format!("now also uses host services {}", and_list(&names(fresh))), |
| 413 | ); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | let withheld: Vec<String> = review |
| 418 | .lines |
| 419 | .iter() |
| 420 | .filter(|line| line.offered && !line.withheld_by.is_empty()) |
| 421 | .map(|line| { |
| 422 | format!( |
| 423 | "{} ({} is turned on without it)", |
| 424 | line.service.name(), |
| 425 | quoted(&line.withheld_by) |
| 426 | ) |
| 427 | }) |
| 428 | .collect(); |
| 429 | if !withheld.is_empty() { |
| 430 | push( |
| 431 | &mut clauses, |
| 432 | MSG_SERVICES_WITHHELD, |
| 433 | format!("off for now: {}", withheld.join(", ")), |
| 434 | ); |
| 435 | } |
| 436 | let unoffered: Vec<String> = review |
| 437 | .lines |
| 438 | .iter() |
| 439 | .filter(|line| !line.offered) |
| 440 | .map(|line| line.service.name().to_string()) |
| 441 | .collect(); |
| 442 | if !unoffered.is_empty() { |
| 443 | push( |
| 444 | &mut clauses, |
| 445 | MSG_SERVICES_UNOFFERED, |
| 446 | format!( |
| 447 | "this Codewhale does not offer {} to plugins", |
| 448 | and_list(&unoffered) |
| 449 | ), |
| 450 | ); |
| 451 | } |
| 452 | if !granted.is_empty() { |
| 453 | push( |
| 454 | &mut clauses, |
| 455 | MSG_SERVICES_PAUSE, |
| 456 | "services pause for every plugin in its host until the host is idle".to_string(), |
| 457 | ); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if now.providers > 0 { |
| 462 | push( |
| 463 | &mut clauses, |
| 464 | MSG_PROVIDER_NEW_SESSION, |
| 465 | "provider changes apply in a new session".to_string(), |
| 466 | ); |
| 467 | } |
| 468 | |
| 469 | PluginSummary { clauses } |
| 470 | } |
| 471 | |
| 472 | #[cfg(test)] |
| 473 | mod tests { |
| 474 | use super::*; |
| 475 | use crate::host_services::{ |
| 476 | AgentApproval, AgentPosture, HostServiceLine, PersonVia, PluginCohort, ReviewPeer, |
| 477 | }; |
| 478 | use crate::install::dsh::DshOutcome; |
| 479 | |
| 480 | fn inventory() -> PluginInventory { |
| 481 | PluginInventory::default() |
| 482 | } |
| 483 | |
| 484 | fn input<'a>(inventory: &'a PluginInventory, approver: &'a PluginApprover) -> SummaryInput<'a> { |
| 485 | SummaryInput { |
| 486 | name: "notes", |
| 487 | version: "1.2.0", |
| 488 | source: Some("github:acme/notes"), |
| 489 | enabled: true, |
| 490 | approver: Some(approver), |
| 491 | inventory, |
| 492 | dsh: None, |
| 493 | host_services: None, |
| 494 | extension_host_on: true, |
| 495 | previous: None, |
| 496 | downgrade_allowed: false, |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | fn person() -> PluginApprover { |
| 501 | PluginApprover::person(PersonVia::Terminal) |
| 502 | } |
| 503 | |
| 504 | fn conversion() -> DshConversion { |
| 505 | DshConversion { |
| 506 | plugin_name: "dsh-whale-widget".into(), |
| 507 | source_package: Some("dsh-whale-widget".into()), |
| 508 | source_version: Some("0.4.2".into()), |
| 509 | manifest_sha256: "0".repeat(64), |
| 510 | layers: Vec::new(), |
| 511 | outcomes: Vec::new(), |
| 512 | diagnostics: Vec::new(), |
| 513 | skills: vec!["balance".into()], |
| 514 | commands: Vec::new(), |
| 515 | agents: Vec::new(), |
| 516 | hooks: Vec::new(), |
| 517 | remote_servers: vec!["account".into()], |
| 518 | local_servers: vec!["bridge".into(), "helper".into()], |
| 519 | network_hosts: vec!["api.example.com".into()], |
| 520 | requires_node: false, |
| 521 | requires_native: false, |
| 522 | native_rows: Vec::new(), |
| 523 | client_closures: Vec::new(), |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | fn skipped(kind: &str, reason: &str) -> DshOutcome { |
| 528 | DshOutcome { |
| 529 | row: Some("row".into()), |
| 530 | package: Some("pkg".into()), |
| 531 | kind: kind.into(), |
| 532 | outcome: "skipped".into(), |
| 533 | reason: reason.into(), |
| 534 | layer: None, |
| 535 | patch: None, |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | fn text_of(summary: &PluginSummary, id: &str) -> Option<String> { |
| 540 | summary |
| 541 | .clauses |
| 542 | .iter() |
| 543 | .find(|clause| clause.id == id) |
| 544 | .map(|clause| clause.text.clone()) |
| 545 | } |
| 546 | |
| 547 | #[test] |
| 548 | fn summary_plain_plugin_names_source_state_approver_and_adds() { |
| 549 | let inventory = PluginInventory { |
| 550 | skills: 2, |
| 551 | mcp_servers: 1, |
| 552 | stdio_mcp_servers: 1, |
| 553 | hooks: 1, |
| 554 | network_hosts: vec!["api.example.com".into()], |
| 555 | ..inventory() |
| 556 | }; |
| 557 | let approver = person(); |
| 558 | let summary = build_summary(&input(&inventory, &approver)); |
| 559 | assert_eq!( |
| 560 | summary.line(), |
| 561 | "notes 1.2.0 from github:acme/notes: on; approved by you; adds 2 skills, 1 local MCP server, 1 hook and network access (api.example.com)" |
| 562 | ); |
| 563 | assert_eq!( |
| 564 | summary.ids(), |
| 565 | vec![MSG_HEAD, MSG_STATE_ON, MSG_APPROVER, MSG_ADDS] |
| 566 | ); |
| 567 | } |
| 568 | |
| 569 | #[test] |
| 570 | fn summary_off_plugin_and_each_approver_kind_read_distinctly() { |
| 571 | let inventory = inventory(); |
| 572 | let agent = PluginApprover::Agent(AgentApproval { |
| 573 | posture: AgentPosture::AutoReview, |
| 574 | workspace: "/work".into(), |
| 575 | thread_id: None, |
| 576 | turn_id: None, |
| 577 | tool_call_id: None, |
| 578 | }); |
| 579 | let mut request = input(&inventory, &agent); |
| 580 | request.enabled = false; |
| 581 | request.source = None; |
| 582 | let summary = build_summary(&request); |
| 583 | assert_eq!( |
| 584 | summary.line(), |
| 585 | "notes 1.2.0: off; approved by the agent in Auto-Review" |
| 586 | ); |
| 587 | let mut request = input(&inventory, &agent); |
| 588 | request.approver = None; |
| 589 | assert!( |
| 590 | build_summary(&request) |
| 591 | .line() |
| 592 | .contains("approver not recorded") |
| 593 | ); |
| 594 | } |
| 595 | |
| 596 | #[test] |
| 597 | fn summary_dsh_package_counts_conversion_fields_and_panels() { |
| 598 | let mut dsh = conversion(); |
| 599 | dsh.requires_node = true; |
| 600 | dsh.client_closures = vec![crate::install::dsh::DshClientClosure { |
| 601 | package: "dsh-whale-widget".into(), |
| 602 | root: String::new(), |
| 603 | entry: "source/client.js".into(), |
| 604 | platform: None, |
| 605 | inject: Vec::new(), |
| 606 | host_provided: Vec::new(), |
| 607 | files: Default::default(), |
| 608 | imports: Default::default(), |
| 609 | bytes: 10, |
| 610 | }]; |
| 611 | let inventory = inventory(); |
| 612 | let approver = person(); |
| 613 | let mut request = input(&inventory, &approver); |
| 614 | request.dsh = Some(&dsh); |
| 615 | let summary = build_summary(&request); |
| 616 | assert_eq!( |
| 617 | text_of(&summary, MSG_ADDS).unwrap(), |
| 618 | "adds 1 skill, 2 local MCP servers, 1 remote MCP server, 1 panel and network access (api.example.com)" |
| 619 | ); |
| 620 | assert_eq!( |
| 621 | text_of(&summary, MSG_NEEDS_NODE).unwrap(), |
| 622 | "needs Node.js for its local servers" |
| 623 | ); |
| 624 | assert!(text_of(&summary, MSG_WONT_RUN).is_none()); |
| 625 | } |
| 626 | |
| 627 | #[test] |
| 628 | fn summary_refused_and_version_refused_rows_are_counted_with_reasons() { |
| 629 | let mut dsh = conversion(); |
| 630 | dsh.outcomes = vec![ |
| 631 | skipped("foreign", "no portable representation"), |
| 632 | skipped("mcp", "needs a manual port"), |
| 633 | DshOutcome { |
| 634 | outcome: "converted".into(), |
| 635 | ..skipped("mcp", "") |
| 636 | }, |
| 637 | ]; |
| 638 | let inventory = inventory(); |
| 639 | let approver = person(); |
| 640 | let mut request = input(&inventory, &approver); |
| 641 | request.dsh = Some(&dsh); |
| 642 | assert_eq!( |
| 643 | text_of(&build_summary(&request), MSG_WONT_RUN).unwrap(), |
| 644 | "won't run: 2 rows (each needs a manual port)" |
| 645 | ); |
| 646 | |
| 647 | dsh.outcomes = vec![skipped( |
| 648 | "patch", |
| 649 | "incompatible-version: this bundle requires DSH `^0.1.0-rc.6`", |
| 650 | )]; |
| 651 | let mut request = input(&inventory, &approver); |
| 652 | request.dsh = Some(&dsh); |
| 653 | assert_eq!( |
| 654 | text_of(&build_summary(&request), MSG_WONT_RUN).unwrap(), |
| 655 | "won't run: 1 row (refused by the host version gate)" |
| 656 | ); |
| 657 | |
| 658 | dsh.outcomes |
| 659 | .push(skipped("foreign", "no portable representation")); |
| 660 | let mut request = input(&inventory, &approver); |
| 661 | request.dsh = Some(&dsh); |
| 662 | assert_eq!( |
| 663 | text_of(&build_summary(&request), MSG_WONT_RUN).unwrap(), |
| 664 | "won't run: 2 rows (1 refused by the host version gate, 1 with no portable form)" |
| 665 | ); |
| 666 | } |
| 667 | |
| 668 | #[test] |
| 669 | fn summary_native_rows_need_the_extension_host_only_while_it_is_off() { |
| 670 | let mut dsh = conversion(); |
| 671 | dsh.requires_native = true; |
| 672 | dsh.native_rows = vec!["worker".into()]; |
| 673 | let inventory = inventory(); |
| 674 | let approver = person(); |
| 675 | let mut request = input(&inventory, &approver); |
| 676 | request.dsh = Some(&dsh); |
| 677 | request.extension_host_on = false; |
| 678 | let off = build_summary(&request); |
| 679 | assert_eq!( |
| 680 | text_of(&off, MSG_NEEDS_EXTENSION_HOST).unwrap(), |
| 681 | "needs the plugin host (off)" |
| 682 | ); |
| 683 | assert!(text_of(&off, MSG_ADDS).unwrap().contains("1 native plugin")); |
| 684 | request.extension_host_on = true; |
| 685 | assert!(text_of(&build_summary(&request), MSG_NEEDS_EXTENSION_HOST).is_none()); |
| 686 | } |
| 687 | |
| 688 | #[test] |
| 689 | fn summary_update_names_only_what_changed() { |
| 690 | let before = PluginInventory { |
| 691 | skills: 2, |
| 692 | ..inventory() |
| 693 | }; |
| 694 | let after = PluginInventory { |
| 695 | skills: 2, |
| 696 | hooks: 1, |
| 697 | network_hosts: vec!["api.example.com".into()], |
| 698 | ..inventory() |
| 699 | }; |
| 700 | let approver = person(); |
| 701 | let mut request = input(&after, &approver); |
| 702 | request.previous = Some(PreviousInstall { |
| 703 | name: "notes", |
| 704 | version: "1.1.0", |
| 705 | source: Some("github:acme/notes"), |
| 706 | inventory: &before, |
| 707 | host_services: &[], |
| 708 | }); |
| 709 | let summary = build_summary(&request); |
| 710 | assert_eq!( |
| 711 | text_of(&summary, MSG_NOW_ALSO_ADDS).unwrap(), |
| 712 | "now also adds 1 hook and network access (api.example.com)" |
| 713 | ); |
| 714 | assert!(text_of(&summary, MSG_ADDS).is_none()); |
| 715 | assert!(text_of(&summary, MSG_REPLACES).is_none()); |
| 716 | assert!(text_of(&summary, MSG_DOWNGRADED).is_none()); |
| 717 | |
| 718 | request.previous = Some(PreviousInstall { |
| 719 | name: "notes", |
| 720 | version: "1.1.0", |
| 721 | source: Some("github:acme/notes"), |
| 722 | inventory: &after, |
| 723 | host_services: &[], |
| 724 | }); |
| 725 | assert_eq!( |
| 726 | text_of(&build_summary(&request), MSG_ADDS_NOTHING_NEW).unwrap(), |
| 727 | "adds nothing new" |
| 728 | ); |
| 729 | } |
| 730 | |
| 731 | #[test] |
| 732 | fn summary_source_change_and_downgrade_are_stated() { |
| 733 | let inventory = inventory(); |
| 734 | let approver = person(); |
| 735 | let mut request = input(&inventory, &approver); |
| 736 | request.previous = Some(PreviousInstall { |
| 737 | name: "notes", |
| 738 | version: "1.4.0", |
| 739 | source: Some("github:old/notes"), |
| 740 | inventory: &inventory, |
| 741 | host_services: &[], |
| 742 | }); |
| 743 | request.downgrade_allowed = true; |
| 744 | let summary = build_summary(&request); |
| 745 | assert_eq!( |
| 746 | text_of(&summary, MSG_REPLACES).unwrap(), |
| 747 | "replaces notes from github:old/notes" |
| 748 | ); |
| 749 | assert_eq!( |
| 750 | text_of(&summary, MSG_DOWNGRADED).unwrap(), |
| 751 | "downgraded from 1.4.0" |
| 752 | ); |
| 753 | request.downgrade_allowed = false; |
| 754 | assert!(text_of(&build_summary(&request), MSG_DOWNGRADED).is_none()); |
| 755 | request.previous.as_mut().unwrap().source = None; |
| 756 | assert_eq!( |
| 757 | text_of(&build_summary(&request), MSG_REPLACES).unwrap(), |
| 758 | "replaces notes from an unrecorded source" |
| 759 | ); |
| 760 | } |
| 761 | |
| 762 | fn review(lines: Vec<HostServiceLine>) -> HostServiceReview { |
| 763 | HostServiceReview::new("user/0/notes", "c", "k", &PluginCohort::Reviewed, lines) |
| 764 | } |
| 765 | |
| 766 | fn peer(name: &str) -> ReviewPeer { |
| 767 | ReviewPeer { |
| 768 | id: format!("user/0/{name}"), |
| 769 | name: name.into(), |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | #[test] |
| 774 | fn summary_host_services_state_sharing_withheld_and_the_pause() { |
| 775 | let inventory = inventory(); |
| 776 | let approver = person(); |
| 777 | let review = review(vec![ |
| 778 | HostServiceLine { |
| 779 | service: HostService::Workspace, |
| 780 | offered: true, |
| 781 | shared_with: vec![peer("other")], |
| 782 | withheld_by: Vec::new(), |
| 783 | }, |
| 784 | HostServiceLine { |
| 785 | service: HostService::Network, |
| 786 | offered: true, |
| 787 | shared_with: Vec::new(), |
| 788 | withheld_by: vec![peer("legacy")], |
| 789 | }, |
| 790 | HostServiceLine { |
| 791 | service: HostService::Remote, |
| 792 | offered: false, |
| 793 | shared_with: Vec::new(), |
| 794 | withheld_by: Vec::new(), |
| 795 | }, |
| 796 | ]); |
| 797 | let mut request = input(&inventory, &approver); |
| 798 | request.host_services = Some(&review); |
| 799 | let summary = build_summary(&request); |
| 800 | assert_eq!( |
| 801 | text_of(&summary, MSG_SERVICES).unwrap(), |
| 802 | "uses host services workspace and network (shared with `other`)" |
| 803 | ); |
| 804 | assert_eq!( |
| 805 | text_of(&summary, MSG_SERVICES_WITHHELD).unwrap(), |
| 806 | "off for now: network (`legacy` is turned on without it)" |
| 807 | ); |
| 808 | assert_eq!( |
| 809 | text_of(&summary, MSG_SERVICES_UNOFFERED).unwrap(), |
| 810 | "this Codewhale does not offer remote to plugins" |
| 811 | ); |
| 812 | assert_eq!( |
| 813 | text_of(&summary, MSG_SERVICES_PAUSE).unwrap(), |
| 814 | "services pause for every plugin in its host until the host is idle" |
| 815 | ); |
| 816 | |
| 817 | let before = [HostService::Workspace]; |
| 818 | let prior = PluginInventory::default(); |
| 819 | request.previous = Some(PreviousInstall { |
| 820 | name: "notes", |
| 821 | version: "1.1.0", |
| 822 | source: Some("github:acme/notes"), |
| 823 | inventory: &prior, |
| 824 | host_services: &before, |
| 825 | }); |
| 826 | let update = build_summary(&request); |
| 827 | assert_eq!( |
| 828 | text_of(&update, MSG_SERVICES_NOW_ALSO).unwrap(), |
| 829 | "now also uses host services network" |
| 830 | ); |
| 831 | assert!(text_of(&update, MSG_SERVICES).is_none()); |
| 832 | |
| 833 | let empty = self::review(Vec::new()); |
| 834 | request.previous = None; |
| 835 | request.host_services = Some(&empty); |
| 836 | assert!(text_of(&build_summary(&request), MSG_SERVICES_PAUSE).is_none()); |
| 837 | } |
| 838 | |
| 839 | #[test] |
| 840 | fn summary_provider_declarations_say_they_apply_in_a_new_session() { |
| 841 | let inventory = PluginInventory { |
| 842 | providers: 1, |
| 843 | ..inventory() |
| 844 | }; |
| 845 | let approver = person(); |
| 846 | let summary = build_summary(&input(&inventory, &approver)); |
| 847 | assert_eq!( |
| 848 | text_of(&summary, MSG_PROVIDER_NEW_SESSION).unwrap(), |
| 849 | "provider changes apply in a new session" |
| 850 | ); |
| 851 | assert!(text_of(&summary, MSG_ADDS).unwrap().contains("1 provider")); |
| 852 | } |
| 853 | |
| 854 | #[test] |
| 855 | fn summary_long_host_lists_are_shortened() { |
| 856 | let inventory = PluginInventory { |
| 857 | network_hosts: ["a.io", "b.io", "c.io", "d.io", "e.io"] |
| 858 | .map(String::from) |
| 859 | .to_vec(), |
| 860 | ..inventory() |
| 861 | }; |
| 862 | let approver = person(); |
| 863 | assert_eq!( |
| 864 | text_of(&build_summary(&input(&inventory, &approver)), MSG_ADDS).unwrap(), |
| 865 | "adds network access (a.io, b.io, c.io and 2 more)" |
| 866 | ); |
| 867 | } |
| 868 | } |
| 869 |