From f8947adf62e8dabdee7157624556fefed8f36d84 Mon Sep 17 00:00:00 2001 From: Hampus Date: Sun, 6 Sep 2026 18:40:20 +0200 Subject: [PATCH] fix(admin): map the index refresh status response union (#2543) --- fluxer_admin/src/api/search.rs | 83 +++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/fluxer_admin/src/api/search.rs b/fluxer_admin/src/api/search.rs index c914e513e..0bc376b41 100644 --- a/fluxer_admin/src/api/search.rs +++ b/fluxer_admin/src/api/search.rs @@ -2,7 +2,7 @@ use crate::api::generated::types as generated_types; -use super::client::{AdminApiClient, ApiResult}; +use super::client::{AdminApiClient, ApiError, ApiResult}; use super::types::{IndexRefreshStatusResponse, RefreshSearchIndexResponse}; impl AdminApiClient { @@ -32,6 +32,85 @@ impl AdminApiClient { .get_admin_search_index_refresh(job_id) .await .map_err(|e| self.generated_error(e))?; - self.generated_value(response.into_inner()) + index_refresh_status(response.into_inner()) + } +} + +fn index_refresh_status( + response: generated_types::IndexRefreshStatusResponse, +) -> ApiResult { + match response { + generated_types::IndexRefreshStatusResponse::Variant0 { status } => { + Ok(IndexRefreshStatusResponse::NotFound { + status: status.to_string(), + }) + } + generated_types::IndexRefreshStatusResponse::Variant1 { + status, + index_type, + total, + indexed, + started_at, + completed_at, + failed_at, + error, + } => Ok(IndexRefreshStatusResponse::Progress { + status: status.to_string(), + index_type: Some(index_type), + total: total + .map(|value| float_to_u64(value, "total")) + .transpose()?, + indexed: indexed + .map(|value| float_to_u64(value, "indexed")) + .transpose()?, + started_at, + completed_at, + failed_at, + error, + }), + } +} + +fn float_to_u64(value: f64, field: &str) -> ApiResult { + crate::api::generated::number_to_u64(value, field).map_err(ApiError::Parse) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn maps_a_running_refresh_to_progress() { + let json = r#"{"status":"in_progress","index_type":"users","total":50000,"indexed":1200,"started_at":"2026-09-06T00:00:00Z"}"#; + let response: generated_types::IndexRefreshStatusResponse = + serde_json::from_str(json).unwrap(); + match index_refresh_status(response).unwrap() { + IndexRefreshStatusResponse::Progress { + status, + index_type, + total, + indexed, + started_at, + .. + } => { + assert_eq!(status, "in_progress"); + assert_eq!(index_type.as_deref(), Some("users")); + assert_eq!(total, Some(50_000)); + assert_eq!(indexed, Some(1_200)); + assert_eq!(started_at.as_deref(), Some("2026-09-06T00:00:00Z")); + } + other => panic!("expected a progress status, got {other:?}"), + } + } + + #[test] + fn maps_a_missing_refresh_to_not_found() { + let json = r#"{"status":"not_found"}"#; + let response: generated_types::IndexRefreshStatusResponse = + serde_json::from_str(json).unwrap(); + match index_refresh_status(response).unwrap() { + IndexRefreshStatusResponse::NotFound { status } => assert_eq!(status, "not_found"), + other => panic!("expected a not found status, got {other:?}"), + } } }