Files
codex/codex-rs/attachment-store/src/lib_tests.rs
T
Sean Huang db9778b73c Distinguish image and tool audio attachment uploads (#62047)
## What changed

Introduce `UploadContent::Image` and `UploadContent::ToolAudio` in `UploadRequest`. Keep image filenames and policy tags on the image variant, and carry tool audio's declared MIME type explicitly instead of encoding its type and MIME metadata in tags.

Update image preparation, image generation, and tool audio callers to use the typed content. Preserve inline bytes for both variants and redact their payloads from debug output. Document that persistent backends must verify the declared audio MIME type against the uploaded bytes.

## Testing

Add coverage for inline tool audio byte preservation and audio debug redaction. Update integration assertions for typed image and audio uploads, and verify that Code Mode does not upload audio when `FileBackedToolAudio` is disabled.

GitOrigin-RevId: 7dbd69922b377aead2dc28d1e5b3938cce6343b7
2026-11-17 13:38:34 +00:00

120 lines
3.8 KiB
Rust

use pretty_assertions::assert_eq;
use super::*;
/// Debug output redacts attachment bytes in requests and results.
#[test]
fn attachment_debug_output_redacts_bytes() {
let request = UploadRequest {
thread_id: "thread-1".to_string(),
ephemeral: false,
content: UploadContent::Image {
file_name: Some("image.png".to_string()),
tags: HashMap::new(),
data: b"secret".to_vec(),
},
};
let result = UploadResult::Inline {
bytes: b"secret".to_vec(),
};
let tool_audio = UploadContent::ToolAudio {
declared_mime_type: "audio/wav".to_string(),
data: b"secret".to_vec(),
};
assert_eq!(
(
format!("{request:?}"),
format!("{tool_audio:?}"),
format!("{result:?}"),
),
(
r#"UploadRequest { thread_id: "thread-1", ephemeral: false, content: Image { file_name: Some("image.png"), tags: {}, data: "<redacted>" } }"#.to_string(),
r#"ToolAudio { declared_mime_type: "audio/wav", data: "<redacted>" }"#.to_string(),
r#"Inline { bytes: "<redacted>" }"#.to_string(),
)
);
}
/// Debug output redacts credential-bearing file URLs in resolved metadata.
#[test]
fn attachment_metadata_debug_output_redacts_file_url() {
let metadata = AttachmentMetadata {
file_url: Some("https://attachments.test/file?signed=secret".to_string()),
..AttachmentMetadata::default()
};
let debug = format!("{metadata:?}");
assert!(!debug.contains("signed=secret"));
assert!(debug.contains("<redacted>"));
}
/// The inline store preserves PNG and JPEG attachment bytes.
#[tokio::test]
async fn inline_store_preserves_image_bytes() {
let cases: [(&str, &[u8]); 2] = [
("image.png", b"\x89PNG\r\n\x1a\n"),
("image.jpg", b"\xff\xd8\xff\xe0JFIF\x00\xff\xd9"),
];
for (file_name, data) in cases {
let attachment = InlineAttachmentStore
.upload(UploadRequest {
thread_id: "thread-1".to_string(),
ephemeral: false,
content: UploadContent::Image {
file_name: Some(file_name.to_string()),
tags: HashMap::new(),
data: data.to_vec(),
},
})
.await
.expect("inline attachment");
let UploadResult::Inline { bytes } = attachment else {
panic!("inline store returned a file reference");
};
assert_eq!(bytes, data);
}
}
/// The inline store preserves tool audio bytes without interpreting them.
#[tokio::test]
async fn inline_store_preserves_tool_audio_bytes() {
let data = b"RIFF\x04\x00\x00\x00WAVE";
let attachment = InlineAttachmentStore
.upload(UploadRequest {
thread_id: "thread-1".to_string(),
ephemeral: false,
content: UploadContent::ToolAudio {
declared_mime_type: "audio/wav".to_string(),
data: data.to_vec(),
},
})
.await
.expect("inline attachment");
let UploadResult::Inline { bytes } = attachment else {
panic!("inline store returned a file reference");
};
assert_eq!(bytes, data);
}
/// The inline store preserves opaque file references even though it cannot resolve them.
#[tokio::test]
async fn inline_store_cannot_resolve_file_references() {
assert!(InlineAttachmentStore.is_resolvable("file_123").await);
let error = InlineAttachmentStore
.resolve(ResolveRequest {
file_id: "file_123",
tags: HashMap::new(),
download_url_ttl: None,
})
.await
.expect_err("inline store cannot resolve file references");
assert_eq!(error.kind(), AttachmentStoreErrorKind::NotFound);
}