fix: cover api edge cases

This commit is contained in:
2026-06-13 10:44:47 +02:00
parent aa3b678cc5
commit ce959b59bf
15 changed files with 400 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
use crate::{domain::TranslationKind, error::AppError};
use crate::{clients::body, domain::TranslationKind, error::AppError};
use serde::Deserialize;
use std::time::Duration;
use tracing::instrument;
@@ -40,11 +40,14 @@ impl TranslationClient {
)));
}
response
.json::<TranslationResponse>()
.await
.map(|payload| payload.contents.translated)
.map_err(|error| map_reqwest_error(&error))
let payload = body::read_json::<TranslationResponse>(response, "FunTranslations").await?;
if payload.contents.translated.trim().is_empty() {
Err(AppError::InvalidUpstreamData(
"FunTranslations returned an empty translation".to_owned(),
))
} else {
Ok(payload.contents.translated)
}
}
}
@@ -112,6 +115,26 @@ mod tests {
assert_eq!(translated, "Created, it was.");
}
#[tokio::test]
async fn rejects_empty_translations_so_callers_can_fallback() {
let server = MockServer::start().await;
Mock::given(matchers::method("POST"))
.and(matchers::path("/translate/yoda.json"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"contents": { "translated": " " }
})))
.mount(&server)
.await;
let client = client_for(&server);
let error = client
.translate(TranslationKind::Yoda, "hello")
.await
.expect_err("empty translation should fail");
assert!(error.to_string().contains("empty translation"));
}
#[tokio::test]
async fn maps_translation_http_failures_to_upstream_errors() {
let server = MockServer::start().await;