Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 99 additions & 10 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,6 @@ async fn plan_restore(
.map_err(|e| format!("{e:#}"))
}

/// 把所有接管过的客户端一次性还原(第二层的第二个入口)。
///
/// **这个按钮要一直看得见。**用户敢按下「接管」的前提,就是看得见退路
/// —— 藏起来的退路等于没有退路,他会在心里给接管打上「不可逆」的标签。
///
/// **一家失败不影响别家。**逐个还原、逐个记结果:五个客户端里有一个的
/// 文件被改坏了,不该让另外四个也留在接管状态。
/// 真的退出。**只有确认过的界面能调它** ——托盘那一项只是把
/// 窗口拉起来问一句。
#[tauri::command]
Expand All @@ -680,6 +673,28 @@ async fn quit_app(app: tauri::AppHandle) -> Result<(), String> {
Ok(())
}

/// 要还原哪几个,以及各自用什么去指代。返回 `(id, 名字)`。
///
/// **对 core 说 id,对用户说名字。**core 的路由是 `/clients/{id}/restore`,
/// 而 `name` 是显示名:拿「Claude Code」去拼 URI,那个空格连请求都发不出去;
/// 拿「Zed」去发,换来的是一句「未知的客户端 Zed」。两者只有 opencode 恰好
/// 相同,所以挑错了字段,测一遍还会看到一个成功的例子。把这一步单独拎出来,
/// 就是为了让「哪个字段进地址」有地方可测。
fn restore_targets(list: &tw_api::ClientsResponse) -> Vec<(&str, &str)> {
list.clients
.iter()
.filter(|c| c.adopted_at_ms.is_some())
.map(|c| (c.id.as_str(), c.name.as_str()))
.collect()
}

/// 把所有接管过的客户端一次性还原(第二层的第二个入口)。
///
/// **这个按钮要一直看得见。**用户敢按下「接管」的前提,就是看得见退路
/// —— 藏起来的退路等于没有退路,他会在心里给接管打上「不可逆」的标签。
///
/// **一家失败不影响别家。**逐个还原、逐个记结果:五个客户端里有一个的
/// 文件被改坏了,不该让另外四个也留在接管状态。
#[tauri::command]
async fn restore_all(state: tauri::State<'_, AppState>) -> Result<Vec<RestoreOutcome>, String> {
let list = state
Expand All @@ -688,10 +703,10 @@ async fn restore_all(state: tauri::State<'_, AppState>) -> Result<Vec<RestoreOut
.await
.map_err(|e| format!("{e:#}"))?;
let mut out = Vec::new();
for c in list.clients.iter().filter(|c| c.adopted_at_ms.is_some()) {
let r = state.control.restore(&c.name).await;
for (id, name) in restore_targets(&list) {
let r = state.control.restore(id).await;
out.push(RestoreOutcome {
client: c.name.clone(),
client: name.to_string(),
ok: r.is_ok(),
detail: match r {
Ok(_) => tr!("已还原", "Restored").to_string(),
Expand Down Expand Up @@ -2342,3 +2357,77 @@ async fn supervise(sup: Arc<Supervisor>, app: tauri::AppHandle) {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

/// 还原一个客户端时,进地址的必须是 id,不是显示名。
///
/// **这条缝裂过一次。**「全部还原」当时逐个传的是 `name`:
/// 「Claude Code」带空格,连 URI 都拼不出来;「Zed」换来一句
/// 「未知的客户端 Zed」。五个里只有 opencode 的 id 和名字一样,于是
/// 它每次都成功 —— 手上有一个能用的例子,这个错就很难被看见。
/// 夹着 opencode 一起断言,就是不让那个巧合再当证据用。
#[test]
fn restore_addresses_a_client_by_id_and_reports_it_by_name() {
let list = clients_json(&[
(r#""claude-code""#, r#""Claude Code""#, "1700000000000"),
(r#""codex""#, r#""Codex CLI""#, "null"),
(r#""opencode""#, r#""opencode""#, "1700000000001"),
(r#""zed""#, r#""Zed""#, "1700000000002"),
]);

// 没接管过的那个不在里面:还原一个本来就没动过的客户端,core 那边
// 是一次没有意义的写
assert_eq!(
restore_targets(&list),
vec![
("claude-code", "Claude Code"),
("opencode", "opencode"),
("zed", "Zed"),
]
);
}

/// 按 core 实际发来的形状造 `/clients` 的响应。
///
/// 直接填结构体的话,字段一加一减这里就编不过,而这个测试要问的事情
/// 跟那些字段无关;走 JSON 还顺带钉住了 `id`、`name`、`adopted_at_ms`
/// 这三个字段名 —— 它们要是在 core 那边改了名,这里会响。
fn clients_json(each: &[(&str, &str, &str)]) -> tw_api::ClientsResponse {
let clients = each
.iter()
.map(|(id, name, adopted_at_ms)| {
format!(
r#"{{
"id": {id},
"name": {name},
"path": "/tmp/x",
"real": "/tmp/x",
"installed": true,
"has_config": true,
"adopted_at_ms": {adopted_at_ms},
"endpoint": "http://127.0.0.1:8080",
"shadows": [],
"takes_effect": "immediately",
"warns_when_silent": true,
"verified": "measured",
"costs": [],
"last_seen_ms": null
}}"#
)
})
.collect::<Vec<_>>()
.join(",");
serde_json::from_str(&format!(
r#"{{
"clients": [{clients}],
"manual": [],
"gateway_base": "http://127.0.0.1:8080",
"keys": ["tw-x"]
}}"#
))
.expect("core 发来的 /clients 解不动")
}
}
Loading