//! Story 3.8 AC02 / AT-3.8-02: `resources/read` returns the exact published text/JSON/binary content //! and MIME in the negotiated shape, with Request ID/Trace ID preserved and no content capture in //! telemetry. use serde_json::Value; use super::fixtures::*; #[allow(clippy::too_many_arguments)] fn check_content( checks: &mut RevisionChecks, body: &Value, revision: &str, uri: &str, expected_text: Option<&str>, expected_blob: Option<&str>, mime: &str, ) { let Some(result) = checks.result(body, revision, "resources/read") else { return; }; if revision != LEGACY_REVISION { // The pinned 2026-07-28 schema: `ReadResourceResult extends CacheableResult`. checks.require(result["resultType"].as_str() == Some("complete"), || { format!("a modern read result must carry Result.resultType=\"complete\" on {revision}: {body}") }); checks.require( result.get("ttlMs").and_then(Value::as_u64).is_some(), || { format!( "a modern read result must carry CacheableResult.ttlMs on {revision}: {body}" ) }, ); checks.require( matches!(result.get("cacheScope").and_then(Value::as_str), Some("public" | "private")), || format!("a modern read result must carry CacheableResult.cacheScope on {revision}: {body}"), ); } let Some(contents) = result["contents"].as_array() else { checks.require(false, || { format!("resources/read must return contents[] on {revision}: {body}") }); return; }; checks.require(contents.len() == 1, || { format!("an exact published static version must yield exactly one content entry on {revision}: {body}") }); let Some(content) = contents.first() else { return; }; checks.require(content["uri"].as_str() == Some(uri), || { format!("the read content must address the requested published URI on {revision}: {body}") }); checks.require(content["mimeType"].as_str() == Some(mime), || { format!("the read must return the published MIME type {mime} on {revision}: {body}") }); if let Some(expected_text) = expected_text { checks.require(content["text"].as_str() == Some(expected_text), || { format!("the read must return the exact published text bytes on {revision}: {body}") }); checks.require(content.get("blob").is_none(), || { format!( "a text/JSON Resource must not be converted to a base64 blob on {revision}: {body}" ) }); } if let Some(expected_blob) = expected_blob { checks.require(content["blob"].as_str() == Some(expected_blob), || { format!( "a binary Resource must be returned as the exact base64 blob on {revision}: {body}" ) }); checks.require(content.get("text").is_none(), || { format!( "binary bytes that happen to be valid UTF-8 must still be a blob (no sniffing) on {revision}: {body}" ) }); } } #[tokio::test] async fn read_serves_the_exact_published_text_for_every_supported_revision() { let endpoint = standard_endpoint("read_text").await; let expected = String::from_utf8(endpoint.text.bytes.clone()).expect("text fixture is UTF-8"); let mut checks = RevisionChecks::default(); for revision in supported_revisions() { let body = json_body(read_resource(&endpoint, &revision, &endpoint.text.uri, None).await).await; check_content( &mut checks, &body, &revision, &endpoint.text.uri, Some(&expected), None, "text/plain", ); } checks.finish(); } /// The typed kind decides the wire conversion: text/JSON => `text`, binary => `blob` base64, with no /// content sniffing (the binary fixture bytes are valid UTF-8 on purpose). #[tokio::test] async fn read_converts_json_to_text_and_binary_to_base64_without_sniffing() { let endpoint = standard_endpoint("read_kinds").await; let json_text = String::from_utf8(endpoint.json.bytes.clone()).expect("JSON fixture is UTF-8"); let json_blob = base64_of(&endpoint.json.bytes); let binary_blob = base64_of(&endpoint.binary.bytes); assert_ne!( json_blob, binary_blob, "the two fixtures must differ so a wrong content cannot pass" ); let mut checks = RevisionChecks::default(); for revision in supported_revisions() { let json_wire = json_body(read_resource(&endpoint, &revision, &endpoint.json.uri, None).await).await; check_content( &mut checks, &json_wire, &revision, &endpoint.json.uri, Some(&json_text), None, "application/json", ); let binary_body = json_body(read_resource(&endpoint, &revision, &endpoint.binary.uri, None).await).await; check_content( &mut checks, &binary_body, &revision, &endpoint.binary.uri, None, Some(&binary_blob), "image/png", ); } checks.finish(); } /// Request/Trace identity is preserved and the Resource content is never captured into telemetry. #[tokio::test] async fn read_preserves_request_identity_without_capturing_content_in_telemetry() { let endpoint = standard_endpoint("read_telemetry").await; let canary = String::from_utf8(endpoint.text.bytes.clone()).expect("text fixture is UTF-8"); assert!( !canary.trim().is_empty(), "the content canary must be non-empty" ); let logs = CapturedLogs::default(); let (body, request_id, trace_id) = { let _capture = logs.install(); let response = read_resource( &endpoint, MODERN_REVISION, &endpoint.text.uri, Some("story38-read-corr"), ) .await; let request_id = response.headers()["x-request-id"] .to_str() .expect("x-request-id header") .to_owned(); let trace_id = response.headers()["x-trace-id"] .to_str() .expect("x-trace-id header") .to_owned(); (json_body(response).await, request_id, trace_id) }; let mut checks = RevisionChecks::default(); check_content( &mut checks, &body, MODERN_REVISION, &endpoint.text.uri, Some(&canary), None, "text/plain", ); checks.require(request_id == "story38-read-corr", || { format!("the read response must preserve the request id, got {request_id}") }); checks.require(!trace_id.is_empty(), || { "the read response must carry a trace id, got an empty header".to_owned() }); let captured = logs.output(); checks.require(captured.contains("story38-read-corr"), || { format!("the read telemetry must keep the Request ID, captured logs: {captured}") }); checks.require(captured.contains("resource read outcome"), || { format!("the read telemetry must record the read outcome event, captured logs: {captured}") }); checks.require( captured.contains("outcome=\"ok\"") && captured.contains("content_bytes="), || { format!( "the read telemetry must record a successful read outcome without content, captured logs: {captured}" ) }, ); checks.require(!captured.contains(canary.trim()), || { format!("the read telemetry must not capture Resource content, captured logs: {captured}") }); checks.finish(); }