From 44f155bf83c6a69736e91a7bb6178fefac5880ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Sun, 22 Nov 2026 23:28:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=A3=E5=88=B0=E5=A4=A7=E8=85=A6=E7=9A=84?= =?UTF-8?q?=E9=9B=BB=E8=85=A6=EF=BC=88=E8=9E=A2=E5=B9=95=EF=BC=89=E4=B8=8A?= =?UTF-8?q?=EF=BC=8CBot=20=E8=A8=AD=E5=AE=9A=E7=9A=84=E3=80=8C=E5=81=9A?= =?UTF-8?q?=E4=BA=8B=E7=9A=84=E9=9B=BB=E8=85=A6=E3=80=8D=E9=81=B8=E4=BA=86?= =?UTF-8?q?=E5=B0=B1=E9=A1=AF=E7=A4=BA=E6=96=B0=E7=9A=84=E9=82=A3=E5=8F=B0?= =?UTF-8?q?=EF=BC=9A=E4=BB=A5=E5=89=8D=E9=81=B8=E5=96=AE=E4=B8=80=E7=9B=B4?= =?UTF-8?q?=E9=A1=AF=E7=A4=BA=E5=A4=A7=E8=85=A6=E5=AD=98=E7=9A=84=E9=82=A3?= =?UTF-8?q?=E5=8F=B0=EF=BC=8C=E7=9C=8B=E8=B5=B7=E4=BE=86=E5=83=8F=E6=B2=92?= =?UTF-8?q?=E6=8F=9B=EF=BC=88=E6=8C=89=E5=84=B2=E5=AD=98=E5=85=B6=E5=AF=A6?= =?UTF-8?q?=E6=9C=89=E6=8F=9B=EF=BC=89=EF=BC=9B=E6=B8=85=E5=96=AE=E4=B9=9F?= =?UTF-8?q?=E8=B7=9F=E8=91=97=E5=89=9B=E5=8B=BE=E7=9A=84=E3=80=8C=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E7=94=A8=E7=9A=84=E9=9B=BB=E8=85=A6=E3=80=8D=E3=80=82?= =?UTF-8?q?=E8=9E=A2=E5=B9=95=E4=B8=8A=E6=B8=85=E7=A9=BA=E3=80=81=E9=97=9C?= =?UTF-8?q?=E6=8E=89=E6=88=96=E6=94=B9=E5=9B=9E=E9=A0=90=E8=A8=AD=E7=9A=84?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A=EF=BC=88=E5=8F=AF=E4=BB=A5=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E9=9B=BB=E8=85=A6=E5=85=A8=E9=83=A8=E5=8B=BE=E5=9B=9E=E3=80=81?= =?UTF-8?q?=E5=9C=98=E9=9A=8A=E9=97=9C=E6=8E=89=E3=80=81=E6=AC=84=E4=BD=8D?= =?UTF-8?q?=E6=B8=85=E7=A9=BA=E3=80=81=E6=94=B6=E8=B5=B7=E7=9A=84=E5=88=86?= =?UTF-8?q?=E5=8D=80=E6=89=93=E9=96=8B=E3=80=81=E9=A2=A8=E6=A0=BC=E6=94=B9?= =?UTF-8?q?=E6=88=90=E8=87=AA=E9=81=B8=E7=9A=84=E5=BC=B7=E8=AA=BF=E8=89=B2?= =?UTF-8?q?=E2=80=A6=EF=BC=89=E7=85=A7=E6=A8=A3=E9=80=81=E5=88=B0=E5=A4=A7?= =?UTF-8?q?=E8=85=A6=EF=BC=8C=E7=94=A8=E8=88=8A=E5=A4=A7=E8=85=A6=E4=B9=9F?= =?UTF-8?q?=E6=87=82=E7=9A=84=E4=B8=80=E8=88=AC=E4=BF=AE=E6=94=B9=EF=BC=9A?= =?UTF-8?q?=E6=9C=89=E5=90=8D=E5=AD=97=E7=9A=84=E6=B8=85=E5=96=AE=E5=92=8C?= =?UTF-8?q?=E5=B0=8D=E7=85=A7=E8=A1=A8=E5=8F=AA=E6=8B=BF=E6=8E=89=E9=80=99?= =?UTF-8?q?=E5=8F=B0=E5=8E=9F=E6=9C=AC=E6=9C=89=E7=9A=84=E9=A0=85=E7=9B=AE?= =?UTF-8?q?=EF=BC=8C=E5=A4=A7=E8=85=A6=E9=82=A3=E9=82=8A=E5=90=8C=E6=99=82?= =?UTF-8?q?=E5=8A=A0=E7=9A=84=E7=95=99=E8=91=97=E3=80=82=E4=BF=AE=E5=A5=BD?= =?UTF-8?q?=E3=80=8C=E5=BC=B7=E8=AA=BF=E8=89=B2=E6=94=B9=E4=BA=86=E5=8F=88?= =?UTF-8?q?=E8=B7=B3=E5=9B=9E=E5=8E=BB=E3=80=8D=EF=BC=9A=E6=B8=85=E6=8E=89?= =?UTF-8?q?=E9=A2=A8=E6=A0=BC=E9=82=A3=E4=B8=80=E4=B8=8B=E6=B2=92=E9=80=81?= =?UTF-8?q?=E5=88=B0=EF=BC=8C=E5=A4=A7=E8=85=A6=E7=9A=84=E9=A2=A8=E6=A0=BC?= =?UTF-8?q?=E5=8F=88=E8=93=8B=E9=81=8E=E9=81=B8=E7=9A=84=E9=A1=8F=E8=89=B2?= =?UTF-8?q?=E3=80=82=E9=A0=86=E6=89=8B=E4=BF=AE=EF=BC=9A=E8=9E=A2=E5=B9=95?= =?UTF-8?q?=E6=94=B9=E4=B8=8A=E5=B1=A4=E8=A8=AD=E5=AE=9A=E6=99=82=EF=BC=8C?= =?UTF-8?q?=E5=A4=A7=E8=85=A6=E4=B8=8A=E6=AD=A3=E5=9C=A8=E8=B7=91=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E7=B7=9A=E8=A2=AB=E5=BC=84=E6=8E=89=E3=80=82=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=9C=B0=E5=9C=96=20computers=20=E5=92=8C=20FEATURES?= =?UTF-8?q?=20=E8=B7=9F=E8=91=97=E6=94=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 已驗證: cargo test 全過(888);新測試:螢幕上選電腦馬上顯示(選別台、選回自己、取消勾選、改名的 Bot)、清空的設定送到大腦(送出的修改長相、比這台新的設定不碰、大腦同時加的分區和支線留著)、清空清單不拿掉大腦加的、強調色選了留著(重送和大腦改過之後都一樣);兩次獨立審查,第二次沒有 P0/P1。 未驗證: 真的桌面視窗連到真的大腦(要使用者的 App 和帳號伺服器);「聊天的顏色」在手機上不跟著 Mac(這台的顏色本來就不送到大腦,等使用者說是哪一邊)。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm --- FEATURES.md | 3 +- docs/feature-map/computers.md | 1 + src/app.rs | 21 ++-- src/app/mirror_ui.rs | 194 ++++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 9 deletions(-) diff --git a/FEATURES.md b/FEATURES.md index 5e55dea..1060550 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -349,6 +349,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。 - 照樣能送:訊息(連引用、附件、勾選的記憶)馬上出現在聊天最下面,淡淡的,下面寫「等連上大腦後送出」(正在送的那一刻是「傳送中…」,連上了還輪不到的是「排隊中…」),還沒送出去過的,旁邊「取消」把它放回輸入框(字、引用、附件都回去);送過一次、可能已經到了大腦的不能取消。排著的訊息存在這台(`brain-queue-<大腦>.json`,每個大腦一份,跟大腦的副本放一起,金鑰封存;讀不懂、或裡面的金鑰現在打不開的檔案改名成 `….unread-時間.json` 留著,不送),關掉 App 再開還在;連上後依序送出,斷線多久都等;附件傳上去的記在那則裡,重試不再傳一次(大檔案的期限照大小放寬)。大腦用每則的編號只收一次(記一天);離第一次送出超過 23 小時才再送的,先看大腦的聊天裡有沒有這句(回答在路上掉了、其實已經送到),有就不再送。大腦自己拒收的,或大腦一直連得到、這則卻連續 20 次都送不過去,才放棄、放回輸入框並說原因(試過的會提醒它也可能已經到了),後面的照送;放棄的設定改動退回大腦的設定並說原因。離線時新增的 Bot、例行任務,編號從大腦上次的編號之後 1000–9000 起跳(每台螢幕不同),不用副本裡的下一個:大腦在這期間可能已經把下一個給了別的 Bot、支線或例行任務,這台之後對它的改動、傳給它的訊息會送錯對象。「下一個編號」照舊隨改動送回大腦、大腦取比較大的,所以離線時新增過 Bot 或例行任務,之後的編號會往後跳一大段(不影響使用;只是讓出編號、沒新增東西的不會送);大腦收到例行任務時也會自己數過它們的編號。重開 App 後還在離線,新的編號也會排在已經新增的後面。帳號伺服器要重新登入、太忙、網路回了不是大腦的頁面(公共 Wi-Fi 登入頁)都算網路問題,繼續等。 - 照樣能改設定:改動先排著(一樣存在這台),連上後疊在大腦那時的設定上送出,大腦那邊同時改的別的項目不會被蓋掉(同一個項目以這台改的為準);秘密欄位照舊只送「已設定」的替身,大腦用自己的;例行任務的「上次執行、下次執行」以大腦的為準,舊副本不會讓它多跑一次。順便修好:大腦送來的設定裡沒有觸發條件的例行任務多了一個空的 `trigger`,螢幕電腦每次存檔都把所有 Bot 的例行任務當成改動送回去。 +- 清空、關掉或改回預設的設定也送得到大腦:存檔時不寫空的欄位,以前螢幕電腦就不送,大腦照舊、過一下又回到原本的樣子(「可以用的電腦」勾回全部、「團隊成員可以使用這個 Bot」關掉、擅長清空、刪掉最後一個工作流、風格改回自訂…)。現在照一般的修改送出(舊版大腦也看得懂):沒選、關、空白;有名字或編號的清單(工作流、收起的分區…)和對照表只拿掉這台原本有的項目,大腦那邊同時加的留著,沒有名字的清單(例如待改進的「全部清除」)整份換掉;大腦有、這台版本太舊讀不懂的設定不會被清掉。強調色因此也修好了:點一個顏色會把「風格」改回自訂,以前大腦的風格又回來蓋掉選的顏色。順便修好:螢幕電腦改了任何一項整體設定,大腦會丟掉正在跑的支線。 - 停止、重新產生、還原、執行例行任務、新對話、清除、回答卡片、按鈕送的指令(清除目標、清除計畫、關掉一律放行):這些動到大腦上正在跑的東西,要連上才能做,不排隊、也不存起來;斷線不到約 100 秒的照樣送、連回來就到,更久的不做,輸入框下說「這個動作要連上才能做」。它們走自己的路,不會卡在等連線的訊息後面(只讓剛排進去的訊息和設定改動先走,最多等 10 秒:改完例行任務馬上按執行,跑的是改過的;回答卡片不等)。 - 不擋畫面:聊天頂端「分享」旁邊一個小小的「離線」(有在等的寫「離線 · N 則等送出」,剛打開還沒連上是「連線中…」),滑鼠移上去說原因、顯示的是幾點同步的內容、做的事連上後送出。以前是聊天上方一整塊紅色提示,佔掉聊天的高度。輸入框的提示字換成「離線:訊息會在連上大腦後送出」。 - 打開 App 時有這個大腦的副本就直接進去顯示副本,不等網路(以前一定先停在「正在連到「…」…」等大腦第一次回答,香港中繼慢的時候要等好幾秒到一分鐘);只有第一次、還沒有副本時才等。 @@ -487,7 +488,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。 - **能力**分 11 類:上網、瀏覽器、看螢幕與操作、檔案與指令、Mac App、連接器、電話簡訊 X、例行任務、頁面、和其他 Bot 合作、管理 App。沒勾的不會提供給它,也不會寫進它的說明(每一步都少送很多 token);它硬要用也會被擋下,告訴它去找名冊裡會的 Bot。記憶、技能、問你問題、請你接手螢幕、傳檔案到聊天、表情和卡片每個 Bot 都有。 - **連接器**:有勾「連接器」能力的 Bot 才拿得到 MCP 工具和它們的使用說明(舊的 Bot 全部都有);連接器的工具超過 20 個時(例如 SccMCP 的 82 個)用「找工具/用工具」兩步,不會整包送。沒勾的 Bot 用名字硬叫(找、用、直接呼叫)都會被擋下。 - **技能**:技能庫裝一次,每個 Bot 勾自己會的;說明裡只列它會的,use_skill 也只讀得到它會的。它自己學會(存成)的技能自動算它的;其他 Bot 要勾選才會。 -- **可以用的電腦**:它自己的電腦一定可以用,其他的勾選才看得到;預設電腦不在名單裡時回到它自己的電腦。匯出 Bot 時不帶電腦名單和草稿。 +- **可以用的電腦**:它自己的電腦一定可以用,其他的勾選才看得到;預設電腦不在名單裡時回到它自己的電腦。連到大腦的螢幕電腦上,Bot 設定裡「做事的電腦」選了就顯示選的那台(以前一直顯示大腦存的那台,看起來像沒選到;按「儲存」才送到大腦),選單也跟著剛勾的「可以用的電腦」。匯出 Bot 時不帶電腦名單和草稿。 - 舊的 Bot 一開始全部勾選(能力、技能、電腦),行為和以前一樣;在 Bot 設定 › 個人檔案、能力、技能、電腦操作裡調。 - **例行任務每次開新對話**(給模型看的):每一輪只從那則例行任務訊息讀起,前面的巡邏記錄不再重送;要接續上次就讀它的頁面或查記憶。聊天畫面照樣完整,你自己傳的下一則訊息也照樣看得到整段對話。 - 實測(你的四個 Bot,照建議勾能力、5 個技能):明日之星每次請求的固定內容從約 2.2 萬降到 1.15 萬 token,FSD 追蹤專員從約 2.5 萬降到 1.3 萬;例行任務每一輪的歷史從最多約 7 萬降到幾百。 diff --git a/docs/feature-map/computers.md b/docs/feature-map/computers.md index f39524a..404980d 100644 --- a/docs/feature-map/computers.md +++ b/docs/feature-map/computers.md @@ -46,6 +46,7 @@ - 「在這台電腦上執行」設成永不允許(或被管理員鎖住)時,這台的檔案也不能傳出去、打開或發佈。 - SSH 電腦上的檔案不能直接傳到聊天、打開給使用者看或發佈網站,要先複製到 Bot 自己的電腦。 - 螢幕電腦的 AnyBot 比大腦舊時,在它上面按允許卡片都不算(舊版說不出是哪一張卡),大腦的聊天寫「螢幕電腦上的回答沒有用上…」:在那台 設定 › 更新 換新版,或在大腦、手機上答。 +- 連到大腦的電腦上,Bot 設定的「做事的電腦」選了就顯示新的那台(清單跟著剛勾的「可以用的電腦」),按「儲存」才送到大腦;勾回全部、清空或關掉的設定照樣送到大腦,有名字的清單只拿掉這台有的項目(大腦那邊同時加的留著)。 - 帳號裡的其他電腦:看畫面、操作畫面各自要使用者允許(可以一律允許);改檔案和指令照 SSH 電腦的規則問。 - `send_to_device`:工作資料夾外的檔案先問,機密檔案不傳;從使用者的其他電腦拿檔案最多 20 MB。沒登入帳號就找不到其他裝置。 - 沒有 Docker 時,Bot 自己的電腦在手機上是「離線」;在 Mac 上它照樣能用(封閉資料夾、沙盒裡跑指令),Linux、Windows 上要有 Docker 才能跑指令。 diff --git a/src/app.rs b/src/app.rs index 7ac8445..adc3674 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2338,17 +2338,22 @@ impl App { } /// The computers to choose from for a bot (name, what it is) and the chosen one. A screen of a - /// brain asks the brain (only it knows the account's computers and which one is chosen). + /// brain takes the computers from the brain (only it knows the account's computers) and + /// chooses among them as the brain would with `b`: Bot 設定's copy shows what was just picked + /// there — it used to show the brain's saved one, so a pick looked like it did nothing. pub(super) fn computer_choices(&self, b: &Bot) -> (Vec<(String, String)>, String) { if self.mir.on { let own = crate::model::own_computer_name(&b.name); - return match self.mir.computers.get(&b.id) { - Some(c) => ( - c["names"].as_array().map(|a| a.iter().filter_map(|x| Some((x["name"].as_str()?.to_string(), x["kind"].as_str().unwrap_or("").to_string()))).collect()).unwrap_or_default(), - c["current"].as_str().unwrap_or(&own).to_string(), - ), - None => (vec![(own.clone(), "own".into())], own), - }; + let mut all = self.every_computer(b); + if all.is_empty() { + return (vec![(own.clone(), "own".into())], own); + } + // (Its own comes first, under the name in this copy — renamed in the editor, too.) + all[0].0 = own.clone(); + let names: Vec<(String, String)> = all.into_iter().enumerate().filter(|(i, (n, _))| *i == 0 || b.computers.as_ref().is_none_or(|c| c.contains(n))).map(|(_, x)| x).collect(); + let want = b.default_computer.trim(); + let current = if want != crate::model::THIS_COMPUTER_NAME && names.iter().any(|(n, _)| n == want) { want.to_string() } else { own }; + return (names, current); } let t = self.targets_for(b); let r = self.roles_for(b, &t); diff --git a/src/app/mirror_ui.rs b/src/app/mirror_ui.rs index 5b56372..b340a62 100644 --- a/src/app/mirror_ui.rs +++ b/src/app/mirror_ui.rs @@ -438,6 +438,18 @@ pub(super) fn diff_ops(base: &Value, cur: &Value) -> Vec { ops.push(if p.get("value").is_some() { json!({"op": "set", "key": k, "value": v}) } else { json!({"op": "patch", "key": k, "patch": p}) }); } } + // A setting left out because it was emptied here (cleared, switched off, back to all of + // them): sent as an edit to its empty value — it used to stay as it was at the brain. (A list + // goes item by item: only what this screen had is taken away, not what the brain added.) + for (k, was) in b { + if c.contains_key(k) || k == "bots" || LOCAL_KEYS.contains(&k.as_str()) || k.starts_with("next_") { + continue; + } + if let Some(v) = emptied::(c, k, was) { + let p = if k == "workflows" { workflow_patch(was, &v) } else { patch_of(was, &v) }; + ops.push(if p.get("value").is_some() { json!({"op": "set", "key": k, "value": v}) } else { json!({"op": "patch", "key": k, "patch": p}) }); + } + } let id = |x: &Value| x["id"].as_u64().unwrap_or(0); let empty = vec![]; let old = base["bots"].as_array().unwrap_or(&empty); @@ -461,6 +473,18 @@ pub(super) fn diff_ops(base: &Value, cur: &Value) -> Vec { ops.push(if p.get("value").is_some() { json!({"op": "bot_set", "id": id(nb), "key": k, "value": v}) } else { json!({"op": "bot_patch", "id": id(nb), "key": k, "patch": p}) }); } } + // (Emptied here, as above: 可以用的電腦 all ticked again, 團隊 off, a field cleared.) + if let (Some(o), Some(n)) = (ob.as_object(), nb.as_object()) { + for (k, was) in o { + if n.contains_key(k) || k == "unread" || BOT_RUNTIME.contains(&k.as_str()) { + continue; + } + if let Some(v) = emptied::(n, k, was) { + let p = patch_of(was, &v); + ops.push(if p.get("value").is_some() { json!({"op": "bot_set", "id": id(nb), "key": k, "value": v}) } else { json!({"op": "bot_patch", "id": id(nb), "key": k, "patch": p}) }); + } + } + } } } } @@ -479,6 +503,34 @@ pub(super) fn diff_ops(base: &Value, cur: &Value) -> Vec { ops } +/// A key this screen's settings leave out though the brain's have it: the value this version +/// leaves out (an empty list or map, "", false, or null for a nothing-chosen), when it is this +/// version's to empty — a newer brain's setting this one can't read is left out too, and isn't +/// the person's to clear. The value goes as an ordinary edit, which every brain understands. +fn emptied(cur: &serde_json::Map, key: &str, was: &Value) -> Option { + // What this version writes for `key` when it holds `v` (None inside: it leaves it out). + let written = |v: &Value| -> Option> { + let mut probe = cur.clone(); + probe.insert(key.to_string(), v.clone()); + // (The bots don't matter for a top-level key; they only make the round trip slow.) + if probe.contains_key("bots") { + probe.insert("bots".into(), json!([])); + } + let t = serde_json::from_value::(Value::Object(probe)).ok()?; + Some(serde_json::to_value(t).ok()?.get(key).cloned()) + }; + written(was)??; + let first = match was { + Value::Array(_) => json!([]), + Value::Object(_) => json!({}), + Value::String(_) => json!(""), + Value::Bool(_) => json!(false), + Value::Number(_) => json!(0), + _ => Value::Null, + }; + [first, Value::Null].into_iter().find(|v| written(v) == Some(None)) +} + /// Replay edit operations on settings JSON: a screen keeps its own not-yet-confirmed edits on /// top of what the brain last sent, so typing isn't undone by the brain's slightly older copy. pub(super) fn apply_ops(cfg: &mut Value, ops: &[Value]) { @@ -1366,6 +1418,8 @@ impl App { let ok = match serde_json::from_value::(top) { Ok(mut st) => { st.remote = std::mem::take(&mut self.state.remote); + // (支線 running here aren't in the JSON: a screen's edit dropped them.) + st.branch_bots = std::mem::take(&mut self.state.branch_bots); self.state = st; true } @@ -3522,4 +3576,144 @@ mod tests { assert_eq!(brain.state.mcp_servers[0].password, ""); crate::mcp::sync(&[]); } + + /// Bot 設定 on a screen: 做事的電腦 shows the computer just picked (and 可以用的電腦 just + /// ticked) before 儲存 — it showed the brain's saved one, so a pick looked like it did nothing. + #[test] + fn a_screen_shows_the_computer_just_picked() { + let ctx = egui::Context::default(); + let mut screen = App::new(&ctx, Some(std::env::temp_dir().join("anybot-screen-pick.png"))); + let mut b = screen.state.bots[0].clone(); + let own = crate::model::own_computer_name(&b.name); + screen.mir.on = true; + // What the brain says: the bot's own computer and the account's MacBook, its own chosen. + let both = json!([{"name": own, "kind": "own"}, {"name": "王的 MacBook", "kind": "device"}]); + screen.mir.computers.insert(b.id, json!({"names": both, "all": both, "current": own})); + assert_eq!(screen.computer_choices(&b), (vec![(own.clone(), "own".to_string()), ("王的 MacBook".to_string(), "device".to_string())], own.clone())); + // Picked in the editor, not saved yet. + b.default_computer = App::computer_to_keep(&b, "王的 MacBook"); + assert_eq!(screen.computer_choices(&b).1, "王的 MacBook"); + // Its own again (kept under the fixed name). + b.default_computer = App::computer_to_keep(&b, &own); + assert_eq!(screen.computer_choices(&b).1, own); + // The MacBook unticked in 可以用的電腦: not offered, and not the chosen one either. + b.default_computer = "王的 MacBook".into(); + b.computers = Some(vec![]); + assert_eq!(screen.computer_choices(&b), (vec![(own.clone(), "own".to_string())], own.clone())); + // Renamed in the editor: its own is offered under the new name (and picking it keeps it). + b.computers = None; + b.name = "改過的名字".into(); + let renamed = crate::model::own_computer_name(&b.name); + let (names, _) = screen.computer_choices(&b); + assert_eq!(names[0].0, renamed); + assert_eq!(App::computer_to_keep(&b, &names[0].0), crate::model::BOT_COMPUTER_NAME); + } + + /// A setting emptied on a screen is left out of its settings: it reaches the brain all the + /// same (可以用的電腦 all ticked again, 團隊 off, a field cleared…) — it used to stay as it was + /// there and come back. A setting this version doesn't know (a newer brain's) isn't touched. + #[test] + fn what_a_screen_empties_is_emptied_at_the_brain() { + let ctx = egui::Context::default(); + let mut brain = App::new(&ctx, Some(std::env::temp_dir().join("anybot-screen-empty.png"))); + let id = brain.state.bots[0].id; + { + let b = brain.state.bot_mut(id).unwrap(); + b.computers = Some(vec!["王的 MacBook".into()]); + b.team = true; + b.strengths = "整理資料".into(); + } + brain.state.folded = vec!["工作".into()]; + let mut base = brain.brain_pull("empty", 0, true, false)["config"].clone(); + // (And what a newer brain has that this version can't read.) + base["from_a_newer_brain"] = json!("keep"); + base["bots"][0]["from_a_newer_brain"] = json!("keep"); + let mut st: State = serde_json::from_value(base.clone()).unwrap(); + let sb = st.bot_mut(id).unwrap(); + sb.computers = None; + sb.team = false; + sb.strengths.clear(); + st.folded.clear(); + let ops = diff_ops(&base, &config_json(&mut st)); + // Ordinary edits (a brain of any version takes them): nothing chosen, off, empty, and the + // list item by item. + let op = |key: &str| ops.iter().find(|o| o["key"] == key).cloned().unwrap_or_else(|| panic!("{key}: {ops:?}")); + assert_eq!((op("computers")["op"].as_str(), &op("computers")["value"]), (Some("bot_set"), &Value::Null)); + assert_eq!(op("team")["value"], json!(false)); + assert_eq!(op("strengths")["value"], json!("")); + assert_eq!((op("folded")["op"].as_str(), &op("folded")["patch"]["list"]["del"]), (Some("patch"), &json!(["工作"]))); + assert!(!ops.iter().any(|o| o["key"] == "from_a_newer_brain"), "{ops:?}"); + assert_eq!(ops.len(), 4, "{ops:?}"); + // The screen's own copy, while they are on the way, reads them as emptied. + let mut held = base.clone(); + apply_ops(&mut held, &ops); + let held: State = serde_json::from_value(held).unwrap(); + assert!(held.bot(id).unwrap().computers.is_none() && held.folded.is_empty()); + // Meanwhile at the brain: a group folded there, and a 支線 running. + brain.state.folded.push("私人".into()); + let mut branch = brain.state.bots[0].clone(); + branch.id = 9_999; + brain.state.branch_bots.push(branch); + // The brain takes each, and keeps what it had that the screen never saw. + assert_eq!(brain.brain_edit(&json!({"ops": ops}))["applied"], 4); + let b = brain.state.bot(id).unwrap(); + assert_eq!((b.computers.clone(), b.team, b.strengths.as_str()), (None, false, "")); + assert_eq!(brain.state.folded, vec!["私人".to_string()]); + assert!(brain.state.branch_bots.iter().any(|b| b.id == 9_999), "a 支線 running at the brain stays"); + assert!(!brain.state.bots.is_empty(), "the bots stay"); + } + + /// A list emptied on a screen while the brain added to it: the screen's copy (its edit on + /// the way, laid over the brain's newer settings) and then the brain keep the new item. + #[test] + fn emptying_a_list_on_a_screen_keeps_what_the_brain_added() { + let ctx = egui::Context::default(); + let dir = fresh_dir("empty-list"); + let (mut brain, mut screen) = brain_and_screen(&ctx, &dir); + brain.state.folded = vec!["工作".into()]; + brain.mark(); + screen.mirror_apply(brain.brain_pull("screen-1", 0, true, false)); + assert_eq!(screen.state.folded, vec!["工作".to_string()]); + screen.state.folded.clear(); + screen.mirror_save(); + let ops: Vec = screen.mir.pending.lock().unwrap().iter().flat_map(|(_, o)| o.clone()).collect(); + let seq = screen.mir.seq; + brain.state.folded.push("私人".into()); + brain.mark(); + screen.mirror_apply(brain.brain_pull("screen-1", 0, true, false)); + assert_eq!(screen.state.folded, vec!["私人".to_string()], "only what the screen had goes"); + brain.brain_edit(&json!({"ops": ops, "client": "screen-1", "seq": seq})); + assert_eq!(brain.state.folded, vec!["私人".to_string()]); + let _ = std::fs::remove_dir_all(&dir); + } + + /// 強調色、聊天的顏色 picked on a screen while the brain wears a 風格: the colours stay, and + /// the 風格 goes off at the brain too (picking a colour of one's own clears it, and an + /// emptied setting wasn't sent: the brain's 風格 came back and covered the colour). + #[test] + fn colours_picked_on_a_screen_stay() { + let ctx = egui::Context::default(); + let dir = fresh_dir("colours"); + let (mut brain, mut screen) = brain_and_screen(&ctx, &dir); + brain.state.shell_template = "night".into(); + brain.mark(); + screen.mirror_apply(brain.brain_pull("screen-1", 0, true, false)); + assert_eq!(screen.state.shell_template, "night"); + // The dots in 設定 › 一般. + screen.state.shell_accent = "teal".into(); + screen.state.shell_template.clear(); + screen.state.accent = crate::model::Accent::Black; + screen.mirror_save(); + let ops: Vec = screen.mir.pending.lock().unwrap().iter().flat_map(|(_, o)| o.clone()).collect(); + let seq = screen.mir.seq; + // The brain's settings come before the edit got there: the picks hold. + screen.mirror_apply(brain.brain_pull("screen-1", 0, true, false)); + assert_eq!((screen.state.shell_template.as_str(), screen.state.shell_accent.as_str(), screen.state.accent), ("", "teal", crate::model::Accent::Black)); + // The edit arrives; the brain's next settings agree. + assert_eq!(brain.brain_edit(&json!({"ops": ops, "client": "screen-1", "seq": seq}))["applied"].as_u64(), Some(ops.len() as u64), "{ops:?}"); + assert_eq!(brain.state.shell_template, "", "the 風格 is off at the brain too"); + screen.mirror_apply(brain.brain_pull("screen-1", 0, true, false)); + assert_eq!((screen.state.shell_template.as_str(), screen.state.shell_accent.as_str(), screen.state.accent), ("", "teal", crate::model::Accent::Black)); + let _ = std::fs::remove_dir_all(&dir); + } }