Skip to main content

agentmux_mcp/tools/
browser_key_text.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//! What `Browser` `key` says about a custom list a key acted on (#47619).
4
5/// What a key did to a custom list that kept focus (`was` is how it stood before, for what differs):
6/// it closed or opened, and the option it shows changed. A list's value is its trigger's text, so a key
7/// that took an option shows here, not as a changed field (#47619).
8pub(super) fn list_effect_text(was: Option<&serde_json::Value>, after: &serde_json::Value) -> Option<String> {
9    let was = was?.as_object()?;
10    let mut parts = Vec::new();
11    if let Some(before) = was.get("expanded").and_then(|v| v.as_bool()) {
12        parts.push(if before { "The list closed.".to_string() } else { "The list opened.".to_string() });
13    }
14    if let Some(before) = was.get("text") {
15        let now = after.get("text").and_then(|v| v.as_str()).unwrap_or("");
16        let before = before.as_str().unwrap_or("");
17        parts.push(format!(
18            "What it shows changed from \"{before}\" to \"{now}\": a key chose that option, check it is the one you meant."
19        ));
20    }
21    (!parts.is_empty()).then(|| parts.join(" "))
22}
23
24#[cfg(test)]
25mod tests {
26    use super::super::browser::key_result_text;
27    use serde_json::json;
28
29        #[test]
30        fn a_list_that_closed_or_took_an_option_says_so() {
31            let after = json!({ "tag": "button", "text": "AGENTMUX", "expanded": false, "element": "node:7" });
32            let took = json!({ "after": after, "changed": true, "focus_moved": false, "was": { "text": "Choose an option", "expanded": true } });
33            let text = key_result_text(Some(&took));
34            assert!(text.contains("The list closed."), "{text}");
35            assert!(text.contains("changed from \"Choose an option\" to \"AGENTMUX\""), "{text}");
36            assert!(!text.contains("didn't change"), "{text}");
37            let closed = json!({ "after": { "tag": "button", "text": "Choose an option", "expanded": false }, "changed": false, "focus_moved": false, "was": { "expanded": true } });
38            let text = key_result_text(Some(&closed));
39            assert!(text.starts_with("Dispatched. The list closed. Now: "), "{text}");
40            assert!(!text.contains("changed from"), "{text}");
41            let opened = json!({ "after": { "tag": "button", "text": "A", "expanded": true }, "changed": false, "was": { "expanded": false } });
42            assert!(key_result_text(Some(&opened)).contains("The list opened."));
43        }
44}