cloacina 0.3.2

A Rust library for resilient task execution and orchestration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 *  Copyright 2025 Colliery Software
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

//! Unit tests for packaging functionality

#[cfg(test)]
mod tests {
    use super::super::*;
    use std::path::PathBuf;
    use tempfile::TempDir;

    /// Create a minimal test Cargo.toml structure
    fn create_test_cargo_toml() -> types::CargoToml {
        types::CargoToml {
            package: Some(types::CargoPackage {
                name: "test-package".to_string(),
                version: "1.0.0".to_string(),
                description: Some("Test description".to_string()),
                authors: Some(vec!["Test Author <test@example.com>".to_string()]),
                keywords: Some(vec!["test".to_string(), "packaging".to_string()]),
                rust_version: None,
            }),
            lib: Some(types::CargoLib {
                crate_type: Some(vec!["cdylib".to_string()]),
            }),
            dependencies: None,
        }
    }

    /// Create a mock compiled library file for testing
    fn create_mock_library_file() -> (TempDir, PathBuf) {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let lib_path = temp_dir.path().join("libtest.so");

        // Create a simple mock library file
        std::fs::write(&lib_path, b"mock library content").expect("Failed to write mock library");

        (temp_dir, lib_path)
    }

    /// Create a test project structure
    fn create_test_project() -> (TempDir, PathBuf) {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let project_path = temp_dir.path().to_path_buf();

        // Create src directory
        let src_dir = project_path.join("src");
        std::fs::create_dir_all(&src_dir).expect("Failed to create src dir");

        // Create lib.rs with test workflow
        let lib_rs_content = r#"
use cloacina::packaged_workflow;

#[packaged_workflow(package = "test-package")]
pub fn simple_task() -> Result<serde_json::Value, Box<dyn std::error::Error>> {
    Ok(serde_json::json!({"status": "complete"}))
}

#[packaged_workflow(package = "test-package")]
pub fn complex_task() -> Result<serde_json::Value, Box<dyn std::error::Error>> {
    Ok(serde_json::json!({"data": "processed"}))
}
"#;
        std::fs::write(src_dir.join("lib.rs"), lib_rs_content).expect("Failed to write lib.rs");

        (temp_dir, project_path)
    }

    #[test]
    fn test_generate_manifest_basic() {
        let cargo_toml = create_test_cargo_toml();
        let (_temp_dir, lib_path) = create_mock_library_file();
        let (_project_temp, project_path) = create_test_project();

        // This will fail at FFI loading since we have a mock library,
        // but we can test the basic structure
        let result = manifest::generate_manifest(&cargo_toml, &lib_path, &None, &project_path);

        match result {
            Ok(manifest) => {
                assert_eq!(manifest.package.name, "test-package");
                assert_eq!(manifest.package.version, "1.0.0");
                assert!(manifest.package.description.contains("Packaged workflow"));
                assert!(!manifest.library.filename.is_empty());
                assert_eq!(manifest.library.symbols, vec!["cloacina_execute_task"]);
            }
            Err(e) => {
                // Expected to fail due to mock library, but should be FFI-related
                let error_msg = format!("{}", e);
                assert!(
                    error_msg.contains("Failed to load library")
                        || error_msg.contains("metadata")
                        || error_msg.contains("symbol"),
                    "Error should be FFI-related: {}",
                    error_msg
                );
            }
        }
    }

    #[test]
    fn test_generate_manifest_with_target() {
        let cargo_toml = create_test_cargo_toml();
        let (_temp_dir, lib_path) = create_mock_library_file();
        let (_project_temp, project_path) = create_test_project();
        let target = Some("x86_64-unknown-linux-gnu".to_string());

        let result = manifest::generate_manifest(&cargo_toml, &lib_path, &target, &project_path);

        match result {
            Ok(manifest) => {
                assert_eq!(manifest.library.architecture, "x86_64-unknown-linux-gnu");
            }
            Err(_) => {
                // Expected to fail due to mock library, but architecture should be set before FFI
            }
        }
    }

    #[test]
    fn test_generate_manifest_missing_package() {
        let mut cargo_toml = create_test_cargo_toml();
        cargo_toml.package = None; // Remove package section

        let (_temp_dir, lib_path) = create_mock_library_file();
        let (_project_temp, project_path) = create_test_project();

        let result = manifest::generate_manifest(&cargo_toml, &lib_path, &None, &project_path);

        assert!(result.is_err());
        let error_msg = format!("{}", result.unwrap_err());
        assert!(error_msg.contains("Missing package section"));
    }

    #[test]
    fn test_extract_package_names_from_source() {
        let (_temp_dir, project_path) = create_test_project();

        let result = manifest::extract_package_names_from_source(&project_path);

        match result {
            Ok(package_names) => {
                assert!(!package_names.is_empty());
                assert!(package_names.contains(&"test-package".to_string()));
            }
            Err(e) => {
                panic!("Should be able to extract package names: {}", e);
            }
        }
    }

    #[test]
    fn test_extract_package_names_no_packages() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let project_path = temp_dir.path().to_path_buf();

        // Create src directory with no packaged workflows
        let src_dir = project_path.join("src");
        std::fs::create_dir_all(&src_dir).expect("Failed to create src dir");

        let lib_rs_content = r#"
pub fn regular_function() -> String {
    "not a packaged workflow".to_string()
}
"#;
        std::fs::write(src_dir.join("lib.rs"), lib_rs_content).expect("Failed to write lib.rs");

        let result = manifest::extract_package_names_from_source(&project_path);

        match result {
            Ok(package_names) => {
                assert!(package_names.is_empty());
            }
            Err(e) => {
                panic!("Should handle no packages gracefully: {}", e);
            }
        }
    }

    #[test]
    fn test_extract_package_names_missing_src() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let project_path = temp_dir.path().to_path_buf();
        // Don't create src directory

        let result = manifest::extract_package_names_from_source(&project_path);

        assert!(result.is_err());
        let error_msg = format!("{}", result.unwrap_err());
        assert!(error_msg.contains("Failed to read src directory"));
    }

    #[test]
    fn test_get_current_architecture() {
        let arch = manifest::get_current_architecture();

        assert!(!arch.is_empty());
        // Should be one of the common architectures
        assert!(
            arch == "x86_64"
                || arch == "aarch64"
                || arch == "arm"
                || arch.starts_with("x86")
                || arch.starts_with("aarch")
                || arch.starts_with("arm")
        );
    }

    #[test]
    fn test_compile_options_builder_pattern() {
        let options = CompileOptions {
            target: Some("aarch64-apple-darwin".to_string()),
            profile: "release".to_string(),
            cargo_flags: vec!["--features".to_string(), "postgres".to_string()],
            jobs: Some(8),
        };

        assert_eq!(options.target.as_ref().unwrap(), "aarch64-apple-darwin");
        assert_eq!(options.profile, "release");
        assert_eq!(options.cargo_flags.len(), 2);
        assert_eq!(options.jobs.unwrap(), 8);
    }

    #[test]
    fn test_package_info_creation() {
        let package_info = types::PackageInfo {
            name: "test-workflow".to_string(),
            version: "2.1.0".to_string(),
            description: "Test workflow package".to_string(),
            author: Some("Test Author".to_string()),
            workflow_fingerprint: Some("abc123def456".to_string()),
            cloacina_version: "0.2.0".to_string(),
        };

        assert_eq!(package_info.name, "test-workflow");
        assert_eq!(package_info.version, "2.1.0");
        assert!(!package_info.description.is_empty());
        assert!(!package_info.cloacina_version.is_empty());
    }

    #[test]
    fn test_library_info_creation() {
        let library_info = types::LibraryInfo {
            filename: "libworkflow.dylib".to_string(),
            symbols: vec![
                "cloacina_execute_task".to_string(),
                "cloacina_get_task_metadata".to_string(),
            ],
            architecture: "aarch64-apple-darwin".to_string(),
        };

        assert!(library_info.filename.ends_with(".dylib"));
        assert_eq!(library_info.symbols.len(), 2);
        assert!(library_info
            .symbols
            .contains(&"cloacina_execute_task".to_string()));
        assert!(!library_info.architecture.is_empty());
    }

    #[test]
    fn test_task_info_creation() {
        let task_info = types::TaskInfo {
            index: 0,
            id: "process_data".to_string(),
            dependencies: vec!["validate_input".to_string()],
            description: "Process the input data".to_string(),
            source_location: "src/tasks.rs:42".to_string(),
        };

        assert_eq!(task_info.index, 0);
        assert_eq!(task_info.id, "process_data");
        assert_eq!(task_info.dependencies.len(), 1);
        assert!(!task_info.description.is_empty());
        assert!(task_info.source_location.contains("src/"));
    }

    #[test]
    fn test_package_manifest_serialization_roundtrip() {
        let original_manifest = types::PackageManifest {
            package: types::PackageInfo {
                name: "test-package".to_string(),
                version: "1.0.0".to_string(),
                description: "Test package".to_string(),
                author: None,
                workflow_fingerprint: None,
                cloacina_version: "0.2.0".to_string(),
            },
            library: types::LibraryInfo {
                filename: "libtest.so".to_string(),
                symbols: vec!["cloacina_execute_task".to_string()],
                architecture: "x86_64".to_string(),
            },
            tasks: vec![
                types::TaskInfo {
                    index: 0,
                    id: "task1".to_string(),
                    dependencies: vec![],
                    description: "First task".to_string(),
                    source_location: "src/lib.rs:10".to_string(),
                },
                types::TaskInfo {
                    index: 1,
                    id: "task2".to_string(),
                    dependencies: vec!["task1".to_string()],
                    description: "Second task".to_string(),
                    source_location: "src/lib.rs:20".to_string(),
                },
            ],
            graph: None,
        };

        // Serialize to JSON
        let json = serde_json::to_string(&original_manifest).expect("Should serialize");
        assert!(!json.is_empty());

        // Deserialize back
        let deserialized: types::PackageManifest =
            serde_json::from_str(&json).expect("Should deserialize");

        // Verify all fields
        assert_eq!(deserialized.package.name, original_manifest.package.name);
        assert_eq!(
            deserialized.package.version,
            original_manifest.package.version
        );
        assert_eq!(
            deserialized.library.filename,
            original_manifest.library.filename
        );
        assert_eq!(deserialized.tasks.len(), original_manifest.tasks.len());
        assert_eq!(deserialized.tasks[0].id, original_manifest.tasks[0].id);
        assert_eq!(
            deserialized.tasks[1].dependencies,
            original_manifest.tasks[1].dependencies
        );
    }

    #[test]
    fn test_constants() {
        assert_eq!(types::MANIFEST_FILENAME, "manifest.json");
        assert_eq!(types::EXECUTE_TASK_SYMBOL, "cloacina_execute_task");
        assert!(!types::CLOACINA_VERSION.is_empty());

        // Verify version follows semver format
        let version_parts: Vec<&str> = types::CLOACINA_VERSION.split('.').collect();
        assert!(
            version_parts.len() >= 2,
            "Version should have at least major.minor"
        );

        // Each part should be numeric
        for part in version_parts.iter().take(2) {
            assert!(
                part.parse::<u32>().is_ok(),
                "Version parts should be numeric: {}",
                part
            );
        }
    }

    // FFI Validation Helper Tests

    #[test]
    fn test_safe_cstr_to_string_null_pointer() {
        use super::super::manifest::{safe_cstr_to_string, ManifestError};
        use std::ptr;

        let result = safe_cstr_to_string(ptr::null(), "test_field");
        assert!(result.is_err());
        match result.unwrap_err() {
            ManifestError::NullString { field } => {
                assert_eq!(field, "test_field");
            }
            _ => panic!("Expected NullString error"),
        }
    }

    #[test]
    fn test_safe_cstr_to_string_valid() {
        use super::super::manifest::safe_cstr_to_string;
        use std::ffi::CString;

        let c_string = CString::new("hello world").unwrap();
        let result = safe_cstr_to_string(c_string.as_ptr(), "test_field");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "hello world");
    }

    #[test]
    fn test_safe_cstr_to_option_string_null_returns_none() {
        use super::super::manifest::safe_cstr_to_option_string;
        use std::ptr;

        let result = safe_cstr_to_option_string(ptr::null(), "test_field");
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[test]
    fn test_safe_cstr_to_option_string_valid() {
        use super::super::manifest::safe_cstr_to_option_string;
        use std::ffi::CString;

        let c_string = CString::new("optional value").unwrap();
        let result = safe_cstr_to_option_string(c_string.as_ptr(), "test_field");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Some("optional value".to_string()));
    }

    #[test]
    fn test_validate_ptr_null_pointer() {
        use super::super::manifest::{validate_ptr, ManifestError};
        use std::ptr;

        let result: Result<&u32, ManifestError> = unsafe { validate_ptr(ptr::null(), "test_ptr") };
        assert!(result.is_err());
        match result.unwrap_err() {
            ManifestError::NullPointer { field } => {
                assert_eq!(field, "test_ptr");
            }
            _ => panic!("Expected NullPointer error"),
        }
    }

    #[test]
    fn test_validate_ptr_valid() {
        use super::super::manifest::validate_ptr;

        let value: u32 = 42;
        let result = unsafe { validate_ptr(&value as *const u32, "test_ptr") };
        assert!(result.is_ok());
        assert_eq!(*result.unwrap(), 42);
    }

    #[test]
    fn test_validate_slice_null_with_nonzero_count() {
        use super::super::manifest::{validate_slice, ManifestError};
        use std::ptr;

        let result: Result<&[u32], ManifestError> =
            unsafe { validate_slice(ptr::null(), 5, "test_slice") };
        assert!(result.is_err());
        match result.unwrap_err() {
            ManifestError::NullTaskSlice { count } => {
                assert_eq!(count, 5);
            }
            _ => panic!("Expected NullTaskSlice error"),
        }
    }

    #[test]
    fn test_validate_slice_null_with_zero_count() {
        use super::super::manifest::validate_slice;
        use std::ptr;

        let result: Result<&[u32], _> = unsafe { validate_slice(ptr::null(), 0, "test_slice") };
        assert!(result.is_ok());
        assert!(result.unwrap().is_empty());
    }

    #[test]
    fn test_validate_slice_exceeds_max_tasks() {
        use super::super::manifest::{validate_slice, ManifestError};

        let value: u32 = 42;
        // MAX_TASKS is 10_000, so we test with 10_001
        let result: Result<&[u32], ManifestError> =
            unsafe { validate_slice(&value as *const u32, 10_001, "test_slice") };
        assert!(result.is_err());
        match result.unwrap_err() {
            ManifestError::TooManyTasks { count, max } => {
                assert_eq!(count, 10_001);
                assert_eq!(max, 10_000);
            }
            _ => panic!("Expected TooManyTasks error"),
        }
    }

    #[test]
    fn test_validate_slice_valid() {
        use super::super::manifest::validate_slice;

        let values: [u32; 3] = [1, 2, 3];
        let result = unsafe { validate_slice(values.as_ptr(), 3, "test_slice") };
        assert!(result.is_ok());
        let slice = result.unwrap();
        assert_eq!(slice.len(), 3);
        assert_eq!(slice[0], 1);
        assert_eq!(slice[1], 2);
        assert_eq!(slice[2], 3);
    }

    #[test]
    fn test_manifest_error_display() {
        use super::super::manifest::ManifestError;

        let err = ManifestError::NullPointer {
            field: "test_field",
        };
        assert!(err.to_string().contains("test_field"));

        let err = ManifestError::NullString {
            field: "string_field".to_string(),
        };
        assert!(err.to_string().contains("string_field"));

        let err = ManifestError::NullTaskSlice { count: 42 };
        assert!(err.to_string().contains("42"));

        let err = ManifestError::TooManyTasks {
            count: 20000,
            max: 10000,
        };
        assert!(err.to_string().contains("20000"));
        assert!(err.to_string().contains("10000"));
    }
}