fix(daemon): 使用人类格式输出 RPC 下载进度

This commit is contained in:
2026-07-15 23:05:28 +08:00
parent 7890b667d7
commit 5b31112e91
2 changed files with 68 additions and 5 deletions
+64 -1
View File
@@ -1894,7 +1894,7 @@ fn print_human_json_value(value: &serde_json::Value) -> anyhow::Result<()> {
print_json_field(value, "next_check_unix_seconds", "下次检查时间");
print_json_field(value, "current_stage", "当前阶段");
print_json_field(value, "current_message", "当前消息");
print_json_field(value, "download_progress", "下载进度");
print_daemon_download_progress_json_summary(value.get("download_progress"));
print_json_field(value, "version_state_path", "版本状态路径");
print_daemon_version_state_json_summary(value.get("version_state"));
print_json_field(value, "resource_output_root", "资源目录");
@@ -1935,6 +1935,26 @@ fn print_human_json_value(value: &serde_json::Value) -> anyhow::Result<()> {
Ok(())
}
fn print_daemon_download_progress_json_summary(value: Option<&serde_json::Value>) {
let Some(value) = value else {
return;
};
if value.is_null() {
return;
}
print_field(
"下载进度",
format_daemon_download_progress_json(value)
.unwrap_or_else(|error| format!("无法解析:{error}")),
);
}
fn format_daemon_download_progress_json(value: &serde_json::Value) -> Result<String, String> {
let progress = serde_json::from_value::<DaemonDownloadProgress>(value.clone())
.map_err(|error| error.to_string())?;
Ok(format_daemon_download_progress(&progress))
}
fn print_daemon_version_state_json_summary(value: Option<&serde_json::Value>) {
let Some(value) = value else {
return;
@@ -4337,6 +4357,49 @@ mod tests {
assert_eq!(json["zip_structure_verified_count"], 3);
}
#[test]
fn daemon_rpc_download_progress_formats_as_human_text() {
let progress = serde_json::json!({
"index": 2,
"total": 19,
"url": "https://prod-clientpatch.bluearchiveyostar.com/file.zip",
"status": "finished",
"bytes": 100,
"transferred_bytes": 100
});
let formatted = format_daemon_download_progress_json(&progress).unwrap();
assert_eq!(
formatted,
"2/19 finished https://prod-clientpatch.bluearchiveyostar.com/file.zip"
);
assert!(!formatted.contains('{'));
}
#[test]
fn daemon_rpc_failed_download_progress_formats_as_human_text() {
let progress = serde_json::json!({
"index": 3,
"total": 19,
"url": "https://prod-clientpatch.bluearchiveyostar.com/missing.zip",
"status": "failed",
"failure_kind": "http_404",
"failure_http_status": 404,
"failure_retryable": false,
"failure_attempts": 1,
"quarantined": true
});
let formatted = format_daemon_download_progress_json(&progress).unwrap();
assert_eq!(
formatted,
"3/19 failed kind=http_404 http=404 retryable=false attempts=1 quarantined=true https://prod-clientpatch.bluearchiveyostar.com/missing.zip"
);
assert!(!formatted.contains('{'));
}
#[test]
fn daemon_socket_path_uses_state_dir() {
assert_eq!(