batch_mode_batch_workspace_interface/
failing_mock.rs

1// ---------------- [ File: batch-mode-batch-workspace-interface/src/failing_mock.rs ]
2crate::ix!();
3
4// A similarly "failing" workspace that might simulate an error if needed
5#[derive(Default, Debug)]
6pub struct FailingWorkspace {}
7
8impl GetTargetDir for FailingWorkspace {
9
10    fn get_target_dir(&self) -> PathBuf {
11        todo!()
12    }
13}
14
15impl GetInputFilenameAtIndex for FailingWorkspace {
16    fn input_filename(&self, _batch_idx: &BatchIndex) -> PathBuf {
17        PathBuf::from("/this/path/does/not/exist/any_input.json")
18    }
19}
20impl GetOutputFilenameAtIndex for FailingWorkspace {
21    fn output_filename(&self, _batch_idx: &BatchIndex) -> PathBuf {
22        PathBuf::from("/this/path/does/not/exist/any_output.json")
23    }
24}
25impl GetErrorFilenameAtIndex for FailingWorkspace {
26    fn error_filename(&self, _batch_idx: &BatchIndex) -> PathBuf {
27        PathBuf::from("/this/path/does/not/exist/any_error.json")
28    }
29}
30impl GetMetadataFilenameAtIndex for FailingWorkspace {
31    fn metadata_filename(&self, _batch_idx: &BatchIndex) -> PathBuf {
32        PathBuf::from("/this/path/does/not/exist/any_metadata.json")
33    }
34}
35impl GetDoneDirectory for FailingWorkspace {
36    fn get_done_directory(&self) -> &PathBuf {
37        static DIR: once_cell::sync::Lazy<PathBuf> =
38            once_cell::sync::Lazy::new(|| PathBuf::from("/this/path/does/not/exist/done_dir"));
39        &DIR
40    }
41}
42impl GetFailedJsonRepairsDir for FailingWorkspace {
43    fn failed_json_repairs_dir(&self) -> PathBuf {
44        PathBuf::from("/this/path/does/not/exist/failing_json_repairs")
45    }
46}
47impl GetFailedItemsDir for FailingWorkspace {
48    fn failed_items_dir(&self) -> PathBuf {
49        PathBuf::from("/this/path/does/not/exist/failing_items")
50    }
51}
52impl GetTextStoragePath for FailingWorkspace {
53    fn text_storage_path(&self, _batch_idx: &BatchIndex) -> PathBuf {
54        PathBuf::from("/this/path/does/not/exist/failing_text_storage.txt")
55    }
56}
57impl GetWorkdir for FailingWorkspace {
58    fn workdir(&self) -> PathBuf {
59        PathBuf::from("/this/path/does/not/exist/workdir")
60    }
61}
62
63impl GetTargetPath for FailingWorkspace
64{
65    type Item = Arc<dyn GetTargetPathForAIExpansion + Send + Sync + 'static>;
66
67    fn target_path(
68        &self,
69        item: &Self::Item,
70        expected_content_type: &ExpectedContentType
71    ) -> PathBuf {
72        let broken_dir = self.workdir().join("this_cannot_be_created");
73        item.target_path_for_ai_json_expansion(&broken_dir, expected_content_type)
74    }
75}
76impl BatchWorkspaceInterface for FailingWorkspace {}
77
78#[cfg(test)]
79mod batch_workspace_interface_exhaustive_tests {
80    use super::*;
81
82    // ===========================
83    // EXHAUSTIVE TESTS
84    // ===========================
85    #[traced_test]
86    fn mock_workspace_implements_all_traits() {
87        info!("Starting test: mock_workspace_implements_all_traits");
88        let workspace = Arc::new(MockBatchWorkspace::default());
89        let idx = BatchIndex::Usize(123);
90
91        // Check input filename
92        let in_file = workspace.input_filename(&idx);
93        debug!("input_filename => {:?}", in_file);
94
95        // Check output filename
96        let out_file = workspace.output_filename(&idx);
97        debug!("output_filename => {:?}", out_file);
98
99        // Check error filename
100        let err_file = workspace.error_filename(&idx);
101        debug!("error_filename => {:?}", err_file);
102
103        // Check metadata filename
104        let meta_file = workspace.metadata_filename(&idx);
105        debug!("metadata_filename => {:?}", meta_file);
106
107        // Check done directory
108        let done_dir = workspace.get_done_directory();
109        debug!("done_directory => {:?}", done_dir);
110
111        // Check text storage
112        let txt_store = workspace.text_storage_path(&idx);
113        debug!("text_storage_path => {:?}", txt_store);
114
115        // Check failed JSON repairs
116        let repairs_dir = workspace.failed_json_repairs_dir();
117        debug!("failed_json_repairs_dir => {:?}", repairs_dir);
118
119        // Check failed items
120        let fails_dir = workspace.failed_items_dir();
121        debug!("failed_items_dir => {:?}", fails_dir);
122
123        // Check workdir
124        let wd = workspace.workdir();
125        debug!("workdir => {:?}", wd);
126
127        // Here is the fix: make sure we pass an Arc<dyn GetTargetPathForAIExpansion + Send + Sync>
128        let item: Arc<dyn GetTargetPathForAIExpansion + Send + Sync> =
129            Arc::new(MockItem { name: "test_item".to_string() });
130
131        let targ = workspace.target_path(&item, &ExpectedContentType::Json);
132        debug!("target_path => {:?}", targ);
133
134        // Basic sanity checks
135        assert!(in_file.to_string_lossy().contains("mock_input_123"));
136        assert!(out_file.to_string_lossy().contains("mock_output_123"));
137        assert!(err_file.to_string_lossy().contains("mock_error_123"));
138        assert!(meta_file.to_string_lossy().contains("mock_metadata_123"));
139        assert!(!done_dir.as_os_str().is_empty());
140        assert!(txt_store.to_string_lossy().contains("text_storage_123"));
141        assert!(!repairs_dir.as_os_str().is_empty());
142        assert!(!fails_dir.as_os_str().is_empty());
143        assert!(!wd.as_os_str().is_empty());
144        assert!(targ.to_string_lossy().contains("json_output"));
145
146        info!("Finished test: mock_workspace_implements_all_traits");
147    }
148
149    #[traced_test]
150    fn failing_workspace_implements_all_traits() {
151        info!("Starting test: failing_workspace_implements_all_traits");
152        let workspace = Arc::new(FailingWorkspace::default());
153        let idx = BatchIndex::new(); // random UUID or random index
154
155        let in_file = workspace.input_filename(&idx);
156        debug!("failing input_filename => {:?}", in_file);
157
158        let out_file = workspace.output_filename(&idx);
159        debug!("failing output_filename => {:?}", out_file);
160
161        let err_file = workspace.error_filename(&idx);
162        debug!("failing error_filename => {:?}", err_file);
163
164        let meta_file = workspace.metadata_filename(&idx);
165        debug!("failing metadata_filename => {:?}", meta_file);
166
167        let done_dir = workspace.get_done_directory();
168        debug!("failing done_directory => {:?}", done_dir);
169
170        let txt_store = workspace.text_storage_path(&idx);
171        debug!("failing text_storage_path => {:?}", txt_store);
172
173        let repairs_dir = workspace.failed_json_repairs_dir();
174        debug!("failing failed_json_repairs_dir => {:?}", repairs_dir);
175
176        let fails_dir = workspace.failed_items_dir();
177        debug!("failing failed_items_dir => {:?}", fails_dir);
178
179        let wd = workspace.workdir();
180        debug!("failing workdir => {:?}", wd);
181
182        // Again, fix to pass the correct trait-object type:
183        let item: Arc<dyn GetTargetPathForAIExpansion + Send + Sync> =
184            Arc::new(MockItem { name: "test_failing_item".to_string() });
185
186        let targ = workspace.target_path(&item, &ExpectedContentType::PlainText);
187        debug!("failing target_path => {:?}", targ);
188
189        assert!(in_file.to_string_lossy().contains("/this/path/does/not/exist"));
190        assert!(out_file.to_string_lossy().contains("/this/path/does/not/exist"));
191        assert!(err_file.to_string_lossy().contains("/this/path/does/not/exist"));
192        assert!(meta_file.to_string_lossy().contains("/this/path/does/not/exist"));
193        assert!(done_dir.to_string_lossy().contains("/this/path/does/not/exist/done_dir"));
194        assert!(txt_store.to_string_lossy().contains("/this/path/does/not/exist"));
195        assert!(repairs_dir.to_string_lossy().contains("/this/path/does/not/exist/failing_json_repairs"));
196        assert!(fails_dir.to_string_lossy().contains("/this/path/does/not/exist/failing_items"));
197        assert!(wd.to_string_lossy().contains("/this/path/does/not/exist/workdir"));
198        assert!(targ.to_string_lossy().contains("this_cannot_be_created"));
199
200        info!("Finished test: failing_workspace_implements_all_traits");
201    }
202}