batch_mode_batch_triple/
ensure_input_matches_error.rs

1// ---------------- [ File: batch-mode-batch-triple/src/ensure_input_matches_error.rs ]
2crate::ix!();
3
4impl BatchFileTriple {
5
6    pub async fn ensure_input_matches_error(&self) 
7        -> Result<(), BatchValidationError> 
8    {
9        // Load input and error files
10        let input_data = load_input_file(self.input().as_ref().unwrap()).await?;
11        let error_data = load_error_file(self.error().as_ref().unwrap()).await?;
12
13        // Compare request IDs
14        let input_ids: HashSet<_> = input_data.request_ids().into_iter().collect();
15        let error_ids: HashSet<_> = error_data.request_ids().into_iter().collect();
16
17        if input_ids != error_ids {
18            return Err(BatchValidationError::RequestIdsMismatch {
19                index: self.index().clone(),
20                input_ids:  Some(input_ids),
21                output_ids: None,
22                error_ids:  Some(error_ids),
23            });
24        }
25
26        info!("for our batch triple {:#?}, we have now ensured the input request ids match the request ids from the error file", self);
27
28        Ok(())
29    }
30}
31
32#[cfg(test)]
33mod batch_file_triple_ensure_input_matches_error_exhaustive_tests {
34    use super::*;
35    use tempfile::NamedTempFile;
36    use std::io::Write;
37    use tokio::runtime::Runtime;
38    use tracing::*;
39
40    #[traced_test]
41    fn ensure_input_matches_error_succeeds_with_identical_ids() {
42        info!("Starting test: ensure_input_matches_error_succeeds_with_identical_ids");
43
44        let mut input_file = NamedTempFile::new()
45            .expect("Failed to create temp file for input");
46        {
47            // Each entire JSON object must be on exactly one line:
48            let req_a = LanguageModelBatchAPIRequest::mock("id-a");
49            let req_b = LanguageModelBatchAPIRequest::mock("id-b");
50
51            writeln!(input_file, "{}", serde_json::to_string(&req_a).unwrap())
52                .expect("Failed to write req_a");
53            writeln!(input_file, "{}", serde_json::to_string(&req_b).unwrap())
54                .expect("Failed to write req_b");
55            }
56
57        // code=400 => error scenario. Must match the shape for a single-line BatchResponseRecord
58        // so that each line is parseable as a full JSON object
59        let mut error_file = NamedTempFile::new()
60            .expect("Failed to create temp file for error");
61        {
62            // Single-line JSON for the first record:
63            let line_a = r#"{"id":"batch_req_id-a","custom_id":"id-a","response":{"status_code":400,"request_id":"resp_req_id-a","body":{"error":{"message":"Error for id-a","type":"test_error","param":null,"code":null}}},"error":null}"#;
64
65            // Single-line JSON for the second record:
66            let line_b = r#"{"id":"batch_req_id-b","custom_id":"id-b","response":{"status_code":400,"request_id":"resp_req_id-b","body":{"error":{"message":"Error for id-b","type":"test_error","param":null,"code":null}}},"error":null}"#;
67
68            writeln!(error_file, "{}", line_a)
69                .expect("Failed to write line_a to error file");
70            writeln!(error_file, "{}", line_b)
71                .expect("Failed to write line_b to error file");
72            }
73
74        let triple = BatchFileTriple::new_direct(
75            &BatchIndex::Usize(6),
76            Some(input_file.path().to_path_buf()),
77            None,
78            Some(error_file.path().to_path_buf()),
79            None,
80            Arc::new(MockBatchWorkspace::default()),
81        );
82
83        let rt = Runtime::new().expect("Failed to create tokio runtime");
84        let res = rt.block_on(async { triple.ensure_input_matches_error().await });
85
86        debug!("Result: {:?}", res);
87        assert!(
88            res.is_ok(),
89            "Should succeed for matching IDs in input vs error"
90        );
91
92        info!("Finished test: ensure_input_matches_error_succeeds_with_identical_ids");
93    }
94}