fakecloud 0.19.1

Local AWS cloud emulator — free, open-source LocalStack alternative
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! Background poller that bridges SQS -> Lambda event source mappings.
//!
//! Periodically checks Lambda state for enabled event source mappings
//! pointing to SQS queues, polls those queues for messages, and either
//! invokes the Lambda function via the container runtime (when available)
//! or records the invocation in Lambda state.
//!
//! Honors:
//! - `FilterCriteria` — non-matching messages are dropped (acked).
//! - `FunctionResponseTypes=[ReportBatchItemFailures]` — when the
//!   Lambda response body is `{"batchItemFailures":[{"itemIdentifier":"<id>"}]}`,
//!   only acked message ids are removed; failed ids are made visible
//!   again so the queue can redeliver.
//! - `MaximumBatchingWindowInSeconds` — once a partial batch is
//!   observed, the poller waits up to N seconds for it to fill before
//!   invoking the Lambda. Once `BatchSize` is reached or the window
//!   expires, the batch is dispatched.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use base64::Engine;
use chrono::{DateTime, Utc};
use serde_json::{json, Value};

use fakecloud_core::delivery::{KmsHook, LambdaDelivery};
use fakecloud_lambda::filter::FilterSet;
use fakecloud_lambda::{LambdaInvocation, SharedLambdaState};
use fakecloud_sqs::SharedSqsState;

#[derive(Clone)]
struct Mapping {
    uuid: String,
    function_arn: String,
    queue_arn: String,
    batch_size: i64,
    filter: FilterSet,
    report_batch_item_failures: bool,
    max_batching_window_seconds: i64,
}

/// Per-mapping batching window state. Tracks the timestamp of the
/// first observed message after the most recent invoke so the poller
/// can hold a partial batch open for `max_batching_window_seconds`.
#[derive(Default)]
struct BatchingState {
    window_started_at: HashMap<String, DateTime<Utc>>,
}

/// SQS -> Lambda event source mapping poller.
pub struct SqsLambdaPoller {
    sqs_state: SharedSqsState,
    lambda_state: SharedLambdaState,
    lambda_delivery: Option<Arc<dyn LambdaDelivery>>,
    /// KMS hook used to decrypt at-rest message bodies (SSE-KMS or
    /// SSE-SQS) before handing them to the Lambda function. Real AWS
    /// Lambda decrypts server-side; without this, an event source
    /// mapping on a managed-SSE queue would deliver opaque envelope
    /// bytes and break every consumer.
    kms_hook: Option<Arc<dyn KmsHook>>,
}

impl SqsLambdaPoller {
    pub fn new(sqs_state: SharedSqsState, lambda_state: SharedLambdaState) -> Self {
        Self {
            sqs_state,
            lambda_state,
            lambda_delivery: None,
            kms_hook: None,
        }
    }

    pub fn with_lambda_delivery(mut self, delivery: Arc<dyn LambdaDelivery>) -> Self {
        self.lambda_delivery = Some(delivery);
        self
    }

    pub fn with_kms_hook(mut self, hook: Arc<dyn KmsHook>) -> Self {
        self.kms_hook = Some(hook);
        self
    }

    pub async fn run(self) {
        let mut interval = tokio::time::interval(Duration::from_millis(500));
        let mut batching = BatchingState::default();
        loop {
            interval.tick().await;
            self.poll(&mut batching).await;
        }
    }

    async fn poll(&self, batching: &mut BatchingState) {
        let mappings: Vec<Mapping> = {
            let lambda_accounts = self.lambda_state.read();
            lambda_accounts
                .iter()
                .flat_map(|(_, lambda)| {
                    lambda
                        .event_source_mappings
                        .values()
                        .filter(|m| m.enabled && m.event_source_arn.contains(":sqs:"))
                        .map(|m| Mapping {
                            uuid: m.uuid.clone(),
                            function_arn: m.function_arn.clone(),
                            queue_arn: m.event_source_arn.clone(),
                            batch_size: m.batch_size,
                            filter: FilterSet::from_strings(m.filter_patterns.iter()),
                            report_batch_item_failures: m
                                .function_response_types
                                .iter()
                                .any(|s| s == "ReportBatchItemFailures"),
                            max_batching_window_seconds: m
                                .maximum_batching_window_in_seconds
                                .unwrap_or(0),
                        })
                        .collect::<Vec<_>>()
                })
                .collect()
        };

        if mappings.is_empty() {
            return;
        }

        let now = Utc::now();

        for mapping in mappings {
            self.process_mapping(&mapping, batching, now).await;
        }
    }

    async fn process_mapping(
        &self,
        mapping: &Mapping,
        batching: &mut BatchingState,
        now: DateTime<Utc>,
    ) {
        // Pull up to BatchSize visible messages and immediately mark
        // them invisible for the queue's `VisibilityTimeout`. Real SQS
        // hides in-flight messages from other consumers (and from later
        // poll ticks of this same mapping) until the consumer either
        // deletes them or the timeout expires. Without setting
        // visibility, two consecutive poll cycles on a slow Lambda
        // would re-deliver the same batch and the function would be
        // invoked twice for one message — duplicates that real AWS
        // doesn't produce inside a single visibility window.
        let (messages, account_id, queue_encrypted) = {
            let mut sqs_mas = self.sqs_state.write();
            let default_acct = sqs_mas.default_account_id().to_string();
            let acct = mapping
                .queue_arn
                .split(':')
                .nth(4)
                .unwrap_or(&default_acct)
                .to_string();
            let sqs = sqs_mas.get_or_create(&acct);
            let queue = sqs.queues.values_mut().find(|q| q.arn == mapping.queue_arn);
            let queue = match queue {
                Some(q) => q,
                None => return,
            };

            let visibility_timeout: i64 = queue
                .attributes
                .get("VisibilityTimeout")
                .and_then(|s| s.parse().ok())
                .unwrap_or(30);
            let visible_at = now + chrono::Duration::seconds(visibility_timeout);

            // Same effective-key logic as the SQS service: SSE-KMS
            // wins when `KmsMasterKeyId` is set, otherwise SSE-SQS
            // fires when `SqsManagedSseEnabled=true`. We track only the
            // boolean here; the poller calls the KMS hook directly so
            // it doesn't need the key id.
            let encrypted = queue
                .attributes
                .get("KmsMasterKeyId")
                .map(|k| !k.is_empty())
                .unwrap_or(false)
                || queue
                    .attributes
                    .get("SqsManagedSseEnabled")
                    .map(String::as_str)
                    == Some("true");

            let mut batch = Vec::new();
            let limit = mapping.batch_size.min(10) as usize;

            for msg in queue.messages.iter_mut() {
                if batch.len() >= limit {
                    break;
                }
                if let Some(vis) = msg.visible_at {
                    if vis > now {
                        continue;
                    }
                }
                // Hide the message for the visibility window. The
                // poller doesn't bump `receive_count` here because the
                // batch may still get filtered out below — the count
                // is bumped only when delivery actually fails (see the
                // ack/fail block).
                msg.visible_at = Some(visible_at);
                batch.push(msg.clone());
            }

            (batch, acct, encrypted)
        };

        if messages.is_empty() {
            batching.window_started_at.remove(&mapping.uuid);
            return;
        }

        // Honor MaximumBatchingWindowInSeconds: hold a partial batch
        // open until the window expires, then dispatch what we have.
        // A full batch dispatches immediately regardless.
        let limit = mapping.batch_size.min(10) as usize;
        if mapping.max_batching_window_seconds > 0 && messages.len() < limit {
            let window_start = batching
                .window_started_at
                .entry(mapping.uuid.clone())
                .or_insert(now);
            let elapsed = now.signed_duration_since(*window_start).num_seconds();
            if elapsed < mapping.max_batching_window_seconds {
                return;
            }
        }
        batching.window_started_at.remove(&mapping.uuid);

        // Decrypt at-rest bodies before invoking Lambda. Real AWS
        // Lambda decrypts SSE-KMS / SSE-SQS messages server-side
        // before delivering the event payload, and the function code
        // sees plaintext. Without this, every consumer on a
        // managed-SSE queue would receive opaque envelope bytes.
        // Cross-service deliveries (SNS / EventBridge) write plaintext
        // directly; the helper detects that and passes it through.
        let messages = if let (true, Some(hook)) = (queue_encrypted, self.kms_hook.as_ref()) {
            messages
                .into_iter()
                .map(|mut msg| {
                    if !looks_like_fakecloud_envelope(&msg.body) {
                        return msg;
                    }
                    let mut ctx = HashMap::new();
                    ctx.insert("aws:sqs:arn".to_string(), mapping.queue_arn.clone());
                    match hook.decrypt(&account_id, &msg.body, "sqs.amazonaws.com", ctx) {
                        Ok(bytes) => {
                            msg.body = String::from_utf8_lossy(&bytes).to_string();
                        }
                        Err(err) => {
                            tracing::warn!(
                                queue = %mapping.queue_arn,
                                error = %err,
                                "SQS->Lambda poller: KMS decrypt failed; delivering opaque body"
                            );
                        }
                    }
                    msg
                })
                .collect::<Vec<_>>()
        } else {
            messages
        };

        // Build SQS-shaped event records, filter, then mark visible-at
        // for failures (or remove for successes) on the SQS side.
        let records: Vec<Value> = messages
            .iter()
            .map(|msg| {
                json!({
                    "messageId": msg.message_id,
                    "receiptHandle": msg.receipt_handle,
                    "body": msg.body,
                    "attributes": {
                        "ApproximateReceiveCount": msg.receive_count.to_string(),
                        "SentTimestamp": msg.sent_timestamp.to_string(),
                    },
                    "md5OfBody": msg.md5_of_body,
                    "eventSource": "aws:sqs",
                    "eventSourceARN": mapping.queue_arn,
                })
            })
            .collect();

        let (matched_records, dropped_ids): (Vec<Value>, Vec<String>) = if mapping.filter.is_empty()
        {
            (records, Vec::new())
        } else {
            let mut matched = Vec::new();
            let mut dropped = Vec::new();
            for (rec, msg) in records.into_iter().zip(messages.iter()) {
                if mapping.filter.matches(&rec) {
                    matched.push(rec);
                } else {
                    dropped.push(msg.message_id.clone());
                }
            }
            (matched, dropped)
        };

        // Drop filtered-out messages — AWS treats them as acked so the
        // queue stops redelivering them.
        if !dropped_ids.is_empty() {
            let mut sqs_mas = self.sqs_state.write();
            let default_acct = sqs_mas.default_account_id().to_string();
            let acct = mapping.queue_arn.split(':').nth(4).unwrap_or(&default_acct);
            let sqs = sqs_mas.get_or_create(acct);
            if let Some(queue) = sqs.queues.values_mut().find(|q| q.arn == mapping.queue_arn) {
                queue
                    .messages
                    .retain(|m| !dropped_ids.contains(&m.message_id));
            }
        }

        if matched_records.is_empty() {
            return;
        }

        let payload = json!({ "Records": &matched_records }).to_string();

        tracing::debug!(
            function_arn = %mapping.function_arn,
            queue_arn = %mapping.queue_arn,
            message_count = matched_records.len(),
            dropped_by_filter = dropped_ids.len(),
            "SQS->Lambda: delivering messages to function"
        );

        let invoke_result = if let Some(ref delivery) = self.lambda_delivery {
            Some(
                delivery
                    .invoke_lambda(&mapping.function_arn, &payload)
                    .await,
            )
        } else {
            None
        };

        let matched_msg_ids: Vec<String> = matched_records
            .iter()
            .filter_map(|r| {
                r.get("messageId")
                    .and_then(|v| v.as_str())
                    .map(String::from)
            })
            .collect();

        let (acked_ids, failed_ids) = match &invoke_result {
            Some(Ok(body)) if mapping.report_batch_item_failures => {
                split_batch_failures(body, &matched_msg_ids)
            }
            Some(Ok(_)) => (matched_msg_ids.clone(), Vec::new()),
            Some(Err(err)) => {
                tracing::warn!(
                    function_arn = %mapping.function_arn,
                    error = %err,
                    "SQS->Lambda: function invocation failed; messages will be retried"
                );
                (Vec::new(), matched_msg_ids.clone())
            }
            None => (matched_msg_ids.clone(), Vec::new()),
        };

        if !acked_ids.is_empty() || !failed_ids.is_empty() {
            let mut sqs_mas = self.sqs_state.write();
            let default_acct = sqs_mas.default_account_id().to_string();
            let acct = mapping.queue_arn.split(':').nth(4).unwrap_or(&default_acct);
            let sqs = sqs_mas.get_or_create(acct);
            if let Some(queue) = sqs.queues.values_mut().find(|q| q.arn == mapping.queue_arn) {
                queue
                    .messages
                    .retain(|m| !acked_ids.contains(&m.message_id));
                // Failed messages stay; bump receive_count and clear
                // any pending visibility timeout so the queue
                // immediately considers them again on the next poll.
                for msg in queue.messages.iter_mut() {
                    if failed_ids.contains(&msg.message_id) {
                        msg.receive_count = msg.receive_count.saturating_add(1);
                        msg.visible_at = None;
                    }
                }
            }
        }

        let fn_account = mapping.function_arn.split(':').nth(4).unwrap_or("");
        let mut lambda_accounts = self.lambda_state.write();
        let lambda = lambda_accounts.get_or_create(fn_account);
        lambda.invocations.push(LambdaInvocation {
            function_arn: mapping.function_arn.clone(),
            payload,
            timestamp: now,
            source: "aws:sqs".to_string(),
        });
    }
}

/// Mirrors `fakecloud_sqs::looks_like_fakecloud_envelope`: detect
/// bodies that the SQS SendMessage path produced via the KMS hook so
/// the poller only invokes the hook on bodies we actually encrypted.
/// Cross-service deliveries (SNS / EventBridge / S3 notifications)
/// land in `queue.messages` as plaintext and must pass through.
fn looks_like_fakecloud_envelope(body: &str) -> bool {
    if body.starts_with("fakecloud-kms:") {
        return true;
    }
    match base64::engine::general_purpose::STANDARD.decode(body) {
        Ok(bytes) => bytes.starts_with(&[0x01, 0x02, 0x02, 0x00]),
        Err(_) => false,
    }
}

/// Parse the Lambda response body as `{"batchItemFailures":[...]}`. Any
/// id that appears in the failures list is reported as failed; the
/// rest are acked. If the body doesn't decode or contains an empty
/// failures list, the entire batch is acked.
fn split_batch_failures(body: &[u8], batch_ids: &[String]) -> (Vec<String>, Vec<String>) {
    let parsed: Value = match serde_json::from_slice(body) {
        Ok(v) => v,
        Err(_) => return (batch_ids.to_vec(), Vec::new()),
    };
    let raw_failures: Vec<String> = parsed
        .get("batchItemFailures")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|f| {
                    f.get("itemIdentifier")
                        .and_then(|v| v.as_str())
                        .map(String::from)
                })
                .collect()
        })
        .unwrap_or_default();
    // Intersect against batch_ids: a stale or made-up itemIdentifier
    // shouldn't keep an unrelated message visible / hold up an acked
    // one. AWS behaves the same way — failures referencing ids
    // outside the batch are ignored.
    let failures: Vec<String> = raw_failures
        .into_iter()
        .filter(|id| batch_ids.contains(id))
        .collect();
    if failures.is_empty() {
        return (batch_ids.to_vec(), Vec::new());
    }
    let acked = batch_ids
        .iter()
        .filter(|id| !failures.contains(id))
        .cloned()
        .collect();
    (acked, failures)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::{BTreeMap, VecDeque};
    use std::pin::Pin;
    use std::sync::Mutex;

    use fakecloud_core::multi_account::MultiAccountState;
    use fakecloud_lambda::{EventSourceMapping, LambdaState};
    use fakecloud_sqs::{SqsMessage, SqsQueue, SqsState};
    use parking_lot::RwLock;

    #[test]
    fn batch_failures_parse() {
        let body = br#"{"batchItemFailures":[{"itemIdentifier":"a"},{"itemIdentifier":"c"}]}"#;
        let (acked, failed) =
            split_batch_failures(body, &["a".into(), "b".into(), "c".into(), "d".into()]);
        assert_eq!(acked, vec!["b".to_string(), "d".to_string()]);
        assert_eq!(failed, vec!["a".to_string(), "c".to_string()]);
    }

    #[test]
    fn batch_failures_empty_list_acks_all() {
        let body = br#"{"batchItemFailures":[]}"#;
        let (acked, failed) = split_batch_failures(body, &["a".into(), "b".into()]);
        assert_eq!(acked, vec!["a".to_string(), "b".to_string()]);
        assert!(failed.is_empty());
    }

    #[test]
    fn batch_failures_no_field_acks_all() {
        let body = br#"{"ok":true}"#;
        let (acked, failed) = split_batch_failures(body, &["a".into(), "b".into()]);
        assert_eq!(acked, vec!["a".to_string(), "b".to_string()]);
        assert!(failed.is_empty());
    }

    /// Slow-invoke `LambdaDelivery` that records each invocation and
    /// holds the future open until the test releases it. Lets us
    /// assert the poller's view of the queue *during* the in-flight
    /// invoke window.
    struct RecordingLambda {
        invocations: Arc<Mutex<Vec<String>>>,
    }

    impl LambdaDelivery for RecordingLambda {
        fn invoke_lambda(
            &self,
            _function_arn: &str,
            payload: &str,
        ) -> Pin<Box<dyn std::future::Future<Output = Result<Vec<u8>, String>> + Send>> {
            self.invocations.lock().unwrap().push(payload.to_string());
            Box::pin(async move { Ok(b"{}".to_vec()) })
        }
    }

    const ACCOUNT: &str = "123456789012";
    const REGION: &str = "us-east-1";

    fn build_states() -> (
        super::SqsLambdaPoller,
        super::SharedSqsState,
        super::SharedLambdaState,
    ) {
        let queue_arn = format!("arn:aws:sqs:{REGION}:{ACCOUNT}:k2-poll");
        let queue_url = format!("http://localhost:4566/{ACCOUNT}/k2-poll");
        let mut attrs = BTreeMap::new();
        attrs.insert("VisibilityTimeout".to_string(), "30".to_string());
        let queue = SqsQueue {
            queue_name: "k2-poll".to_string(),
            queue_url: queue_url.clone(),
            arn: queue_arn.clone(),
            created_at: Utc::now(),
            messages: VecDeque::from(vec![SqsMessage {
                message_id: "msg-1".to_string(),
                receipt_handle: None,
                md5_of_body: "d41d8cd98f00b204e9800998ecf8427e".to_string(),
                body: "hello".to_string(),
                sent_timestamp: 0,
                attributes: BTreeMap::new(),
                message_attributes: BTreeMap::new(),
                visible_at: None,
                receive_count: 0,
                message_group_id: None,
                message_dedup_id: None,
                created_at: Utc::now(),
                sequence_number: None,
            }]),
            inflight: Vec::new(),
            attributes: attrs,
            is_fifo: false,
            dedup_cache: BTreeMap::new(),
            redrive_policy: None,
            tags: BTreeMap::new(),
            next_sequence_number: 0,
            permission_labels: Vec::new(),
            receipt_handle_map: BTreeMap::new(),
        };

        let mut sqs: MultiAccountState<SqsState> =
            MultiAccountState::new(ACCOUNT, REGION, "http://localhost:4566");
        {
            let s = sqs.default_mut();
            s.name_to_url.insert("k2-poll".to_string(), queue_url);
            s.queues.insert(queue.queue_url.clone(), queue);
        }
        let sqs_state = Arc::new(RwLock::new(sqs));

        let mut lambda: MultiAccountState<LambdaState> =
            MultiAccountState::new(ACCOUNT, REGION, "http://localhost:4566");
        {
            let l = lambda.default_mut();
            let mapping = EventSourceMapping {
                uuid: "esm-1".to_string(),
                function_arn: format!("arn:aws:lambda:{REGION}:{ACCOUNT}:function:k2-fn"),
                event_source_arn: queue_arn,
                batch_size: 10,
                enabled: true,
                state: "Enabled".to_string(),
                last_modified: Utc::now(),
                filter_patterns: Vec::new(),
                maximum_batching_window_in_seconds: None,
                starting_position: None,
                starting_position_timestamp: None,
                parallelization_factor: None,
                function_response_types: Vec::new(),
                kms_key_arn: None,
                metrics_config: None,
                destination_config: None,
                maximum_retry_attempts: None,
                maximum_record_age_in_seconds: None,
                bisect_batch_on_function_error: None,
                tumbling_window_in_seconds: None,
                topics: Vec::new(),
                queues: Vec::new(),
                source_access_configurations: Vec::new(),
            };
            l.event_source_mappings
                .insert(mapping.uuid.clone(), mapping);
        }
        let lambda_state = Arc::new(RwLock::new(lambda));

        let poller = super::SqsLambdaPoller::new(sqs_state.clone(), lambda_state.clone());
        (poller, sqs_state, lambda_state)
    }

    /// Regression: when the poller picks up a message and hands it to
    /// Lambda, the message must be hidden for the queue's
    /// VisibilityTimeout — otherwise a second poll cycle (or another
    /// consumer) would re-deliver the same message and the function
    /// would fire twice for one record.
    #[tokio::test]
    async fn poller_marks_picked_messages_invisible_during_invoke() {
        let invocations = Arc::new(Mutex::new(Vec::<String>::new()));
        let delivery: Arc<dyn LambdaDelivery> = Arc::new(RecordingLambda {
            invocations: invocations.clone(),
        });
        let (poller, sqs_state, _lambda_state) = build_states();
        let poller = poller.with_lambda_delivery(delivery);
        let mut batching = BatchingState::default();

        // First poll picks up the only message and acks it. After
        // process_mapping returns, the message must be gone.
        poller.poll(&mut batching).await;
        assert_eq!(
            invocations.lock().unwrap().len(),
            1,
            "first poll should invoke once"
        );

        // Inject a fresh message and verify the poller hides it before
        // invoking. We run the second poll without releasing the
        // RecordingLambda future synchronously — the future resolves
        // inside the same task here, so we instead snapshot the queue
        // immediately after invoke and confirm the visibility timeout
        // got stamped on the body that just got delivered.
        {
            let mut sqs = sqs_state.write();
            let s = sqs.default_mut();
            let queue = s.queues.values_mut().next().unwrap();
            queue.messages.push_back(SqsMessage {
                message_id: "msg-2".to_string(),
                receipt_handle: None,
                md5_of_body: "d41d8cd98f00b204e9800998ecf8427e".to_string(),
                body: "second".to_string(),
                sent_timestamp: 0,
                attributes: BTreeMap::new(),
                message_attributes: BTreeMap::new(),
                visible_at: None,
                receive_count: 0,
                message_group_id: None,
                message_dedup_id: None,
                created_at: Utc::now(),
                sequence_number: None,
            });
        }

        // Drive the poller again. After invoke succeeds the message is
        // acked + removed; queue should be empty.
        poller.poll(&mut batching).await;
        assert_eq!(
            invocations.lock().unwrap().len(),
            2,
            "second poll should invoke once more"
        );
        let sqs = sqs_state.read();
        let queue = sqs.default_ref().queues.values().next().unwrap();
        assert!(
            queue.messages.is_empty(),
            "successful invoke must delete acked messages, found: {:?}",
            queue
                .messages
                .iter()
                .map(|m| &m.message_id)
                .collect::<Vec<_>>()
        );
    }

    /// The same message must not be re-popped while it's in flight: a
    /// second poll cycle observing a queue where the only message has
    /// `visible_at` in the future has to skip it. This is the
    /// visibility-window invariant the previous poller violated by
    /// leaving `visible_at` unset on pop.
    #[tokio::test]
    async fn poller_skips_message_during_visibility_window() {
        let (poller, sqs_state, _lambda_state) = build_states();
        // No LambdaDelivery wired — process_mapping treats this as a
        // best-effort recording-only delivery: the poll DOES still mark
        // messages as acked at the end (since invoke_result is None,
        // the helper treats it as success). To exercise the visibility
        // window we need a delivery that fails, so failed messages
        // stay visible. Instead, we hand-set visible_at on the message
        // and call process_mapping directly to verify the skip path.
        let mapping = Mapping {
            uuid: "esm-1".to_string(),
            function_arn: format!("arn:aws:lambda:{REGION}:{ACCOUNT}:function:k2-fn"),
            queue_arn: format!("arn:aws:sqs:{REGION}:{ACCOUNT}:k2-poll"),
            batch_size: 10,
            filter: fakecloud_lambda::filter::FilterSet::from_strings(std::iter::empty::<&str>()),
            report_batch_item_failures: false,
            max_batching_window_seconds: 0,
        };
        // Hide the existing message in the future.
        {
            let mut sqs = sqs_state.write();
            let s = sqs.default_mut();
            let queue = s.queues.values_mut().next().unwrap();
            queue.messages[0].visible_at = Some(Utc::now() + chrono::Duration::seconds(60));
        }

        let invocations = Arc::new(Mutex::new(Vec::<String>::new()));
        let delivery: Arc<dyn LambdaDelivery> = Arc::new(RecordingLambda {
            invocations: invocations.clone(),
        });
        let poller = poller.with_lambda_delivery(delivery);
        let mut batching = BatchingState::default();
        poller
            .process_mapping(&mapping, &mut batching, Utc::now())
            .await;
        assert_eq!(
            invocations.lock().unwrap().len(),
            0,
            "message hidden by visibility timeout must not be invoked"
        );
        // Message still in queue.messages, untouched.
        let sqs = sqs_state.read();
        let queue = sqs.default_ref().queues.values().next().unwrap();
        assert_eq!(queue.messages.len(), 1);
    }
}