batch_mode_batch_client/
get_batch_file_content.rs

1// ---------------- [ File: batch-mode-batch-client/src/get_batch_file_content.rs ]
2crate::ix!();
3
4#[async_trait]
5impl<E> GetBatchFileContent for OpenAIClientHandle<E>
6where
7    E: Debug + Send + Sync + From<OpenAIClientError> + From<std::io::Error>, 
8{
9    type Error = E;
10
11    async fn file_content(&self, file_id: &str) -> Result<Bytes, Self::Error> {
12        info!("retrieving file {} content from online", file_id);
13
14        let bytes = self.files().content(file_id)
15            .await
16            // If that returns `OpenAIApiError`, do something like:
17            .map_err(|api_err| E::from(OpenAIClientError::OpenAIError(api_err)))?;
18
19        Ok(bytes)
20    }
21}
22#[cfg(test)]
23mod get_batch_file_content_tests {
24    use super::*;
25    use futures::executor::block_on;
26    use tempfile::tempdir;
27    use tracing::{debug, error, info, trace, warn};
28    use std::sync::Arc;
29
30    /// Exhaustive test suite for the `GetBatchFileContent` implementation on `OpenAIClientHandle`.
31    /// We use the mock client to simulate various scenarios (success, error, invalid input, etc).
32    #[traced_test]
33    async fn test_file_content_success() {
34        info!("Beginning test_file_content_success");
35        trace!("Constructing mock client...");
36        let mock_client = MockLanguageModelClientBuilder::<MockBatchClientError>::default()
37            .build()
38            .unwrap();
39        debug!("Mock client built: {:?}", mock_client);
40
41        let file_id = "valid_file_id";
42
43        trace!("Calling file_content on mock_client with file_id={}", file_id);
44        let result = mock_client.file_content(file_id).await;
45        debug!("Result from file_content: {:?}", result);
46
47        // Expect an Ok result for a valid file
48        assert!(
49            result.is_ok(),
50            "Expected file_content to succeed with a valid file_id"
51        );
52        let bytes = result.unwrap();
53        assert!(
54            !bytes.is_empty(),
55            "Expected the returned bytes not to be empty for a valid file"
56        );
57        info!("test_file_content_success passed.");
58    }
59
60    #[traced_test]
61    async fn test_file_content_empty_input() {
62        info!("Beginning test_file_content_empty_input");
63        trace!("Constructing mock client...");
64        let mock_client = MockLanguageModelClientBuilder::<MockBatchClientError>::default()
65            .build()
66            .unwrap();
67        debug!("Mock client built: {:?}", mock_client);
68
69        let file_id = "";
70
71        trace!("Calling file_content with an empty file_id");
72        let result = mock_client.file_content(file_id).await;
73        debug!("Result from file_content: {:?}", result);
74
75        // We expect the mock or the real call to fail if the file_id is invalid.
76        assert!(
77            result.is_err(),
78            "Expected file_content to produce an error for empty file_id"
79        );
80
81        info!("test_file_content_empty_input passed.");
82    }
83
84    #[traced_test]
85    async fn test_file_content_openai_api_error() {
86        info!("Beginning test_file_content_openai_api_error");
87        trace!("Constructing mock client that simulates an OpenAI error...");
88        let mock_client = {
89            let mut builder = MockLanguageModelClientBuilder::<MockBatchClientError>::default();
90            // Hypothetically configure builder to fail on file_content, if supported:
91            // builder.fail_on_file_content_openai_error(true);
92            builder.build().unwrap()
93        };
94        debug!("Mock client built: {:?}", mock_client);
95
96        let file_id = "trigger_api_error";
97
98        trace!("Calling file_content expecting an API error scenario...");
99        let result = mock_client.file_content(file_id).await;
100        debug!("Result from file_content: {:?}", result);
101
102        // We expect an Err if the mock is configured to simulate an OpenAI error
103        assert!(
104            result.is_err(),
105            "Expected file_content to return an error due to OpenAI API error"
106        );
107
108        info!("test_file_content_openai_api_error passed.");
109    }
110
111    #[traced_test]
112    async fn test_file_content_other_error() {
113        info!("Beginning test_file_content_other_error");
114        trace!("Constructing mock client that simulates a non-OpenAI error...");
115        let mock_client = {
116            let mut builder = MockLanguageModelClientBuilder::<MockBatchClientError>::default();
117            // builder.fail_on_file_content_with_io_error(true);
118            builder.build().unwrap()
119        };
120        debug!("Mock client built: {:?}", mock_client);
121
122        let file_id = "trigger_other_error";
123
124        trace!("Calling file_content expecting a different kind of error...");
125        let result = mock_client.file_content(file_id).await;
126        debug!("Result from file_content: {:?}", result);
127
128        assert!(
129            result.is_err(),
130            "Expected file_content to return a non-OpenAI error scenario"
131        );
132
133        info!("test_file_content_other_error passed.");
134    }
135}