|
| 1 | +use anyhow::{ensure, Result}; |
| 2 | +use aws_lambda_action_filter::{Action, Priority}; |
| 3 | +use chrono::{Duration, Utc}; |
| 4 | +use serde_json::{self, Value}; |
| 5 | +use std::collections::HashMap; |
| 6 | +use std::fs; |
| 7 | +use std::process::Command; |
| 8 | + |
| 9 | +/// Helper function to run cargo lambda invoke and parse the result |
| 10 | +fn run_lambda_invoke(data_file: &str) -> Result<Vec<Action>> { |
| 11 | + // --- |
| 12 | + let output = |
| 13 | + Command::new("cargo").args(["lambda", "invoke", "--data-file", data_file]).output()?; |
| 14 | + |
| 15 | + ensure!( |
| 16 | + output.status.success(), |
| 17 | + "cargo lambda invoke failed with status: {}\nstderr: {}", |
| 18 | + output.status, |
| 19 | + String::from_utf8_lossy(&output.stderr) |
| 20 | + ); |
| 21 | + |
| 22 | + let stdout = String::from_utf8(output.stdout)?; |
| 23 | + |
| 24 | + // The output should be a JSON array of actions |
| 25 | + let json_value: Value = serde_json::from_str(&stdout)?; |
| 26 | + let actions: Vec<Action> = serde_json::from_value(json_value)?; |
| 27 | + |
| 28 | + Ok(actions) |
| 29 | +} |
| 30 | + |
| 31 | +struct TestCase { |
| 32 | + entity_id: &'static str, |
| 33 | + next_offset: i64, // Days from now (positive = future) |
| 34 | + last_offset: i64, // Days from now (negative = past) |
| 35 | + priority: Priority, |
| 36 | + should_pass: bool, // Expected to pass filtering? |
| 37 | + description: &'static str, |
| 38 | +} |
| 39 | + |
| 40 | +#[rustfmt::skip] |
| 41 | +const EDGE_CASES: &[TestCase] = &[ |
| 42 | + TestCase { entity_id: "dedup_first_occurrence", next_offset: 30, last_offset: -10, priority: Priority::Urgent, should_pass: true, description: "Tests deduplication (first occurrence)" }, |
| 43 | + TestCase { entity_id: "dedup_first_occurrence", next_offset: 35, last_offset: -15, priority: Priority::Normal, should_pass: true, description: "Tests deduplication (last occurrence wins)" }, |
| 44 | + TestCase { entity_id: "more_than_7_days_ago_fail", next_offset: 20, last_offset: -7, priority: Priority::Urgent, should_pass: false, description: "Tests 'more than 7 days ago' rule (should fail)" }, |
| 45 | + TestCase { entity_id: "more_than_7_days_ago_pass", next_offset: 20, last_offset: -8, priority: Priority::Urgent, should_pass: true, description: "Tests 'more than 7 days ago' rule (should pass)" }, |
| 46 | + TestCase { entity_id: "more_than_7_days_ago_pass_2", next_offset: 25, last_offset: -10, priority: Priority::Urgent, should_pass: true, description: "Tests 'more than 7 days ago' rule (should pass)" }, |
| 47 | + TestCase { entity_id: "within_90_days_fail", next_offset: 91, last_offset: -30, priority: Priority::Normal, should_pass: false, description: "Tests 'within 90 days' rule (should fail at 91 days)" }, |
| 48 | + TestCase { entity_id: "within_90_days_pass", next_offset: 90, last_offset: -30, priority: Priority::Normal, should_pass: true, description: "Tests 'within 90 days' rule boundary (should pass)" }, |
| 49 | + TestCase { entity_id: "within_90_days_pass_2", next_offset: 89, last_offset: -20, priority: Priority::Normal, should_pass: true, description: "Tests 'within 90 days' rule (should pass)" }, |
| 50 | +]; |
| 51 | + |
| 52 | +fn create_action( |
| 53 | + entity_id: &str, |
| 54 | + last_offset: i64, |
| 55 | + next_offset: i64, |
| 56 | + priority: Priority, |
| 57 | +) -> Action { |
| 58 | + // --- |
| 59 | + let now = Utc::now(); |
| 60 | + Action { |
| 61 | + entity_id: entity_id.to_string(), |
| 62 | + last_action_time: now + Duration::days(last_offset), |
| 63 | + next_action_time: now + Duration::days(next_offset), |
| 64 | + priority, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +fn generate_test_data() -> Result<String> { |
| 69 | + // --- |
| 70 | + let actions: Vec<Action> = EDGE_CASES |
| 71 | + .iter() |
| 72 | + .map(|test_case| { |
| 73 | + // --- |
| 74 | + create_action( |
| 75 | + test_case.entity_id, |
| 76 | + test_case.last_offset, |
| 77 | + test_case.next_offset, |
| 78 | + test_case.priority.clone(), |
| 79 | + ) |
| 80 | + }) |
| 81 | + .collect(); |
| 82 | + |
| 83 | + let json = serde_json::to_string_pretty(&actions)?; |
| 84 | + Ok(json) |
| 85 | +} |
| 86 | + |
| 87 | +fn verify_test_expectations(results: &[Action]) -> Result<()> { |
| 88 | + // --- |
| 89 | + let prefix = "verify_test_expectations"; |
| 90 | + |
| 91 | + // Convert results to a map for O(1) lookup |
| 92 | + let result_map: HashMap<&str, &Action> = |
| 93 | + results.iter().map(|action| (action.entity_id.as_str(), action)).collect(); |
| 94 | + |
| 95 | + // Iterate over test expectations and verify against results |
| 96 | + for test_case in EDGE_CASES { |
| 97 | + // --- |
| 98 | + let found_in_results = result_map.contains_key(test_case.entity_id); |
| 99 | + |
| 100 | + match (test_case.should_pass, found_in_results) { |
| 101 | + // --- |
| 102 | + (true, false) => { |
| 103 | + // --- |
| 104 | + ensure!( |
| 105 | + false, |
| 106 | + "{prefix}: {} - Expected to pass but was filtered out. {}", |
| 107 | + test_case.entity_id, |
| 108 | + test_case.description |
| 109 | + ); |
| 110 | + } |
| 111 | + (false, true) => { |
| 112 | + // --- |
| 113 | + ensure!( |
| 114 | + false, |
| 115 | + "{prefix}: {} - Expected to be filtered out but found in results. {}", |
| 116 | + test_case.entity_id, |
| 117 | + test_case.description |
| 118 | + ); |
| 119 | + } |
| 120 | + (true, true) => { |
| 121 | + // --- |
| 122 | + println!("✓ {:<28}: PASS - {}", test_case.entity_id, test_case.description); |
| 123 | + } |
| 124 | + (false, false) => { |
| 125 | + // --- |
| 126 | + println!("✓ {:<28}: FILTERED - {}", test_case.entity_id, test_case.description); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + Ok(()) |
| 132 | +} |
| 133 | + |
| 134 | +#[test] |
| 135 | +fn test_dynamic_edge_cases() -> Result<()> { |
| 136 | + // --- |
| 137 | + println!("Generating dynamic edge case test data..."); |
| 138 | + |
| 139 | + // Generate test data with current timestamps |
| 140 | + let test_data = generate_test_data()?; |
| 141 | + |
| 142 | + // Write to temporary file |
| 143 | + let temp_file = "testdata/edge-cases-dynamic.json"; |
| 144 | + fs::write(temp_file, &test_data)?; |
| 145 | + |
| 146 | + println!("Generated test data written to: {}", temp_file); |
| 147 | + println!("Test data preview:"); |
| 148 | + println!("{}", test_data); |
| 149 | + println!(); |
| 150 | + |
| 151 | + // Run the lambda with our generated data |
| 152 | + let results = run_lambda_invoke(temp_file)?; |
| 153 | + |
| 154 | + println!("Lambda returned {} actions", results.len()); |
| 155 | + |
| 156 | + // Verify all test expectations |
| 157 | + verify_test_expectations(&results)?; |
| 158 | + |
| 159 | + // Additional verification: check expected count |
| 160 | + // Should have 5 actions: 6 that should pass - 1 duplicate = 5 |
| 161 | + // (dedup_first_occurrence appears twice but deduplicated to 1) |
| 162 | + let expected_count = 5; |
| 163 | + ensure!( |
| 164 | + results.len() == expected_count, |
| 165 | + "Expected {} actions after filtering and deduplication, got {}", |
| 166 | + expected_count, |
| 167 | + results.len() |
| 168 | + ); |
| 169 | + |
| 170 | + // Verify priority sorting (urgent before normal) |
| 171 | + let mut seen_normal = false; |
| 172 | + for action in &results { |
| 173 | + // --- |
| 174 | + if action.priority == Priority::Normal { |
| 175 | + // --- |
| 176 | + seen_normal = true; |
| 177 | + } else if action.priority == Priority::Urgent && seen_normal { |
| 178 | + // --- |
| 179 | + ensure!(false, "Found urgent priority after normal priority - sorting failed"); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + // Verify deduplication worked correctly |
| 184 | + let duplicate_count = |
| 185 | + results.iter().filter(|a| a.entity_id == "dedup_first_occurrence").count(); |
| 186 | + ensure!( |
| 187 | + duplicate_count == 1, |
| 188 | + "Expected exactly 1 'duplicate' entity after deduplication, found {}", |
| 189 | + duplicate_count |
| 190 | + ); |
| 191 | + |
| 192 | + // Verify that the duplicate kept the last occurrence (Normal priority) |
| 193 | + if let Some(duplicate_action) = results.iter().find(|a| a.entity_id == "duplicate") { |
| 194 | + // --- |
| 195 | + ensure!( |
| 196 | + duplicate_action.priority == Priority::Normal, |
| 197 | + "Expected duplicate entity to keep last occurrence (Normal priority), got {:?}", |
| 198 | + duplicate_action.priority |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + // Cleanup |
| 203 | + fs::remove_file(temp_file).ok(); |
| 204 | + |
| 205 | + println!(); |
| 206 | + println!("✅ All dynamic edge case tests passed!"); |
| 207 | + println!(" - Boundary conditions verified"); |
| 208 | + println!(" - Deduplication working correctly"); |
| 209 | + println!(" - Priority sorting maintained"); |
| 210 | + |
| 211 | + Ok(()) |
| 212 | +} |
0 commit comments