agentmux_srv/server/
browser_handoff_reason.rs1pub(crate) const HANDOFF_REASON_MAX: usize = 600;
8
9pub(crate) fn handoff_reason(reason: &str) -> Result<String, String> {
11 let n = reason.chars().count();
12 if n == 0 {
13 return Err("say what the user should do (`reason`)".to_string());
14 }
15 if n > HANDOFF_REASON_MAX {
16 return Err(format!(
17 "the reason is {n} characters and the most a hand-off carries is {HANDOFF_REASON_MAX}: the person reads all of it on the card, so it is not cut. Shorten it to what they must do"
18 ));
19 }
20 Ok(reason.to_string())
21}
22
23#[cfg(test)]
24mod tests {
25 use super::*;
26
27 #[test]
29 fn a_handoff_reason_is_carried_whole_or_refused_with_the_limit() {
30 let long_but_ok = "Upload the file on the form, then press Done. ".repeat(12);
31 assert!(long_but_ok.chars().count() > 300 && long_but_ok.chars().count() <= HANDOFF_REASON_MAX);
32 assert_eq!(handoff_reason(&long_but_ok).unwrap(), long_but_ok, "past the old 300-character cut, carried whole");
33 let exactly = "x".repeat(HANDOFF_REASON_MAX);
34 assert_eq!(handoff_reason(&exactly).unwrap(), exactly);
35 let over = "x".repeat(HANDOFF_REASON_MAX + 1);
36 let why = handoff_reason(&over).unwrap_err();
37 assert!(why.contains(&(HANDOFF_REASON_MAX + 1).to_string()) && why.contains(&HANDOFF_REASON_MAX.to_string()) && why.contains("not cut"), "{why}");
38 assert!(handoff_reason("").unwrap_err().contains("what the user should do"));
39 assert!(handoff_reason(&"é".repeat(HANDOFF_REASON_MAX)).is_ok());
41 }
42}