apify-client 0.14.0

An official, but experimental, AI-generated and AI-maintained Rust client for the Apify API (https://apify.com).
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
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
//! Generic lazy pagination shared by every collection client.
//!
//! The reference JavaScript client returns an `AsyncIterable` from every collection `list()`
//! (via its base `_listPaginatedFromCallback`), so callers can iterate across all pages without
//! manually tracking offsets. Rust cannot return a value that is simultaneously a `Future` and a
//! `Stream`, so the idiomatic equivalent here is a dedicated `iterate()` method on each collection
//! client that returns a [`ListIterator`]. The paging logic lives here once so every client stays
//! a thin wrapper over it (don't-repeat-yourself).

use std::collections::VecDeque;
use std::future::Future;
use std::pin::Pin;

use crate::common::PaginationList;
use crate::error::ApifyClientResult;

/// A boxed future yielding one page of results. Boxed so [`ListIterator`] can hold a fetcher
/// for any concrete collection client without being generic over its (unnameable) future type.
type PageFuture<T> = Pin<Box<dyn Future<Output = ApifyClientResult<PaginationList<T>>> + Send>>;

/// Fetches one page: the arguments are the absolute `offset` to start at and the per-page `limit`
/// to request (`None` = let the API pick its default page size). Implementations capture a clone of
/// the collection client and the caller's list options, overriding only the offset and limit per
/// page.
type PageFetcher<T> = Box<dyn Fn(i64, Option<i64>) -> PageFuture<T> + Send + Sync>;

/// Generates the body of a collection client's `iterate()` method for the common shape: an
/// options struct with `offset`/`limit` fields plus a `list(options)` method. It clones the
/// client, reads the caller's start offset and total-item cap from the options, and builds a
/// [`ListIterator`] whose per-page fetcher overrides only `offset`/`limit` before calling
/// `list`. This removes the ~15 lines of identical closure boilerplate that would otherwise be
/// copied into every collection client (the don't-repeat-yourself goal of this module).
///
/// Collections whose listing does not fit this shape build their iterator directly instead:
/// the run listing takes a separate `filter` argument, dataset items use `list_items::<T>`, and
/// the listings that are not paginated (an Actor's versions and a version's env vars) use
/// `single_page_iterator!`.
macro_rules! list_iterator {
    ($self:expr, $options:expr, $list:ident) => {{
        let client = $self.clone();
        let options = $options;
        let start = options.offset.unwrap_or(0);
        let total_limit = options.limit;
        $crate::clients::pagination::ListIterator::new(
            start,
            total_limit,
            Box::new(move |offset, page_limit| {
                let client = client.clone();
                let mut options = options.clone();
                options.offset = Some(offset);
                options.limit = page_limit;
                Box::pin(async move { client.$list(options).await })
            }),
        )
    }};
}
pub(crate) use list_iterator;

/// Generates the body of the `iterate()` method of a collection whose endpoint is not
/// offset-paginated: it has a no-argument `list()` method and returns every item in one response.
/// The resulting [`ListIterator`] calls `list` exactly once and then completes.
macro_rules! single_page_iterator {
    ($self:expr) => {{
        let client = $self.clone();
        $crate::clients::pagination::ListIterator::new_single_page(Box::new(
            move |_offset, _page_limit| {
                let client = client.clone();
                Box::pin(async move { client.list().await })
            },
        ))
    }};
}
pub(crate) use single_page_iterator;

/// Returns the smaller of two optional positive limits, treating a non-positive value as "no
/// limit" (`None`). Mirrors the reference client's `minForLimitParam`, where the API treats `0`
/// as an absent limit.
fn min_positive_limit(a: Option<i64>, b: Option<i64>) -> Option<i64> {
    let a = a.filter(|&x| x > 0);
    let b = b.filter(|&x| x > 0);
    match (a, b) {
        (Some(x), Some(y)) => Some(x.min(y)),
        (Some(x), None) | (None, Some(x)) => Some(x),
        (None, None) => None,
    }
}

/// A lazy, page-fetching async iterator over an offset/limit-paginated list endpoint.
///
/// Created by a collection client's `iterate()` method. Each call to [`next`](Self::next)
/// returns the next item, transparently fetching the following page from the API once the
/// local buffer drains, until the listing is exhausted (or the caller's total-item cap is hit).
///
/// # `limit` vs. page size
///
/// The caller's `limit` (from the list options passed to `iterate()`) is a **cap on the total
/// number of items the iterator yields**, matching the reference JavaScript client's
/// `_listPaginatedFromCallback`, where `options.limit` bounds the whole async-iterable and a
/// separate `chunkSize` controls page size. Leaving `limit` unset (or `0`) iterates the entire
/// listing. The page size is a distinct concern: set it with [`with_chunk_size`](Self::with_chunk_size);
/// when unset, the API's default page size is used. So `iterate(opts{ limit: 10 })` yields at most
/// 10 items, and `iterate(opts).with_chunk_size(50)` fetches 50 per request while yielding
/// everything.
///
/// **Large caps and the first page.** When a total cap is set but no page size is, the first page
/// requests `limit == cap` (the reference client does the same, via
/// `minForLimitParam(options.limit, options.chunkSize)`). If you set a very large cap — larger
/// than the endpoint's maximum `limit` — also call [`with_chunk_size`](Self::with_chunk_size) with
/// a value at or below that maximum, so the first request stays within the endpoint's accepted
/// range rather than asking for the whole cap up front.
///
/// # Example
/// ```no_run
/// use apify_client::ApifyClient;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let client = ApifyClient::new("my-api-token");
/// let mut it = client.actors().iterate(Default::default());
/// while let Some(actor) = it.next().await? {
///     println!("{}", actor.id);
/// }
/// # Ok(())
/// # }
/// ```
pub struct ListIterator<T> {
    fetch: PageFetcher<T>,
    buffer: VecDeque<T>,
    /// Absolute offset of the next page to request.
    next_offset: i64,
    /// Number of items still allowed under the caller's total-item cap; `None` = uncapped.
    /// Decremented by each page's item count as pages are fetched.
    remaining: Option<i64>,
    /// Page size to request per fetch (the reference client's `chunkSize`); `None` = let the API
    /// choose its default page size.
    chunk_size: Option<i64>,
    /// When `true`, the underlying endpoint is not offset-paginated: the first fetch returns the
    /// whole result set, so the iterator stops after it rather than requesting a second page.
    single_page: bool,
    /// Set once the listing has been fully consumed.
    exhausted: bool,
}

impl<T> ListIterator<T> {
    /// Builds an iterator that starts at `start_offset`, yields at most `total_limit` items across
    /// all pages (`None`/`0` = uncapped), and fetches offset-paginated pages via `fetch`.
    pub(crate) fn new(start_offset: i64, total_limit: Option<i64>, fetch: PageFetcher<T>) -> Self {
        let cap = total_limit.filter(|&l| l > 0);
        Self {
            fetch,
            buffer: VecDeque::new(),
            next_offset: start_offset,
            remaining: cap,
            chunk_size: None,
            single_page: false,
            exhausted: false,
        }
    }

    /// Builds an iterator over an endpoint that is **not** offset-paginated (it returns every
    /// item in one response, e.g. an Actor version's environment variables). `fetch` is called
    /// exactly once; the iterator does not attempt a second page, so it does not depend on the
    /// endpoint reporting a page `limit` and cannot refetch the same items.
    pub(crate) fn new_single_page(fetch: PageFetcher<T>) -> Self {
        Self {
            single_page: true,
            ..Self::new(0, None, fetch)
        }
    }

    /// Sets the page size (items requested per API call) for this iteration — the reference
    /// client's `chunkSize`. This controls only how many items each page fetch requests, never how
    /// many the iterator yields in total (that is the caller's `limit`; see the type docs). A
    /// non-positive value lets the API choose its default page size.
    pub fn with_chunk_size(mut self, chunk_size: i64) -> Self {
        self.chunk_size = (chunk_size > 0).then_some(chunk_size);
        self
    }

    /// Returns the next item, or `None` when the listing is exhausted. Fetches another page from
    /// the API when the local buffer is empty.
    pub async fn next(&mut self) -> ApifyClientResult<Option<T>> {
        if let Some(item) = self.buffer.pop_front() {
            return Ok(Some(item));
        }
        if self.exhausted {
            return Ok(None);
        }

        // Request the smaller of the items still allowed under the caller's cap (`remaining`) and
        // the configured page size (`chunk_size`); `None` lets the API pick its default page size.
        // On the first page `remaining` is the full cap, matching the reference client's initial
        // `minForLimitParam(options.limit, options.chunkSize)`.
        let page_limit = min_positive_limit(self.remaining, self.chunk_size);
        let page = (self.fetch)(self.next_offset, page_limit).await?;
        let received = page.items.len() as i64;

        // The number of rows this page *scanned*, used to advance the offset and decide when the
        // listing is exhausted. For every endpoint except dataset items this equals `received`
        // (`page.scanned` is `None`); dataset items can report it separately (see
        // `PaginationList::scanned`), since a filter can scan more rows than it returns and
        // `unwind` can return more items than rows scanned. It is capped at the page size actually
        // requested so an unwound page never advances past rows the next request would then skip.
        let scanned = page.scanned.unwrap_or(received).max(received);
        let scanned = match page_limit {
            Some(limit) if limit > 0 => scanned.min(limit),
            _ => scanned,
        };

        // Enforce the caller's total-item cap exactly, even if the API returns more than the
        // requested page limit.
        let mut items = page.items;
        if let Some(rem) = self.remaining {
            if received > rem {
                items.truncate(rem.max(0) as usize);
            }
        }

        self.next_offset += scanned;
        if let Some(rem) = self.remaining.as_mut() {
            *rem -= scanned;
        }

        // Decide whether more pages remain. `total` is deliberately never consulted: it changes
        // as the listed resource grows (e.g. a dataset a still-running Actor keeps pushing to),
        // so stopping at the `total` a page happened to report can end the walk before rows added
        // later are ever read. A short page is not treated as terminal either (server-side filters
        // can legitimately make a full, non-final window come back short) — only a page that
        // scanned nothing, or the caller's own cap, ends the walk. This matches the reference
        // client, which pages until a request scans zero rows and so always sends one extra
        // request at the end to find that empty page.
        if self.single_page {
            // Non-paginated endpoint: everything came back in one response.
            self.exhausted = true;
        } else if scanned == 0 {
            self.exhausted = true;
        } else if matches!(self.remaining, Some(r) if r <= 0) {
            // Reached the caller's total-item cap.
            self.exhausted = true;
        }

        self.buffer.extend(items);
        Ok(self.buffer.pop_front())
    }

    /// Eagerly drains the iterator into a single `Vec`, fetching every remaining page.
    ///
    /// Convenience for callers that want all items at once; prefer [`next`](Self::next) to
    /// process items as they stream in without buffering the whole result set.
    pub async fn collect_all(mut self) -> ApifyClientResult<Vec<T>> {
        let mut out = Vec::new();
        while let Some(item) = self.next().await? {
            out.push(item);
        }
        Ok(out)
    }
}

#[cfg(test)]
mod tests {
    use super::{ListIterator, PageFetcher};
    use crate::common::PaginationList;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    /// Builds a fetcher that serves `all` as offset-paginated pages, honouring the per-page `limit`
    /// requested by the iterator (falling back to `default_page` when the iterator requests none).
    /// Reports `total = all.len()` only when `report_total`, and echoes the effective page size back
    /// as the response `limit`. Counts how many times it is called so tests can assert page-request
    /// behaviour.
    fn slicing_fetcher(
        all: Vec<i64>,
        default_page: i64,
        report_total: bool,
        calls: Arc<AtomicUsize>,
    ) -> PageFetcher<i64> {
        let all = Arc::new(all);
        Box::new(move |offset, limit| {
            calls.fetch_add(1, Ordering::SeqCst);
            let all = all.clone();
            let page_size = limit.filter(|&l| l > 0).unwrap_or(default_page);
            Box::pin(async move {
                let start = (offset.max(0) as usize).min(all.len());
                let end = (start + page_size.max(0) as usize).min(all.len());
                let items = all[start..end].to_vec();
                Ok(PaginationList {
                    total: if report_total { all.len() as i64 } else { 0 },
                    offset,
                    limit: page_size,
                    count: items.len() as i64,
                    desc: false,
                    items,
                    scanned: None,
                })
            })
        })
    }

    #[tokio::test]
    async fn ignores_reported_total_and_pages_to_an_empty_page() {
        let calls = Arc::new(AtomicUsize::new(0));
        let iter = ListIterator::new(
            0,
            None,
            slicing_fetcher((0..5).collect(), 2, true, calls.clone()),
        )
        .with_chunk_size(2);
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 1, 2, 3, 4]);
        // Pages: [0,1] [2,3] [4] [] — the third page is short (1 < 2) but not terminal, and
        // `total` (reported as 5, reached by offset after the third page) is not consulted
        // either, so a fourth, empty request is what actually ends the walk.
        assert_eq!(calls.load(Ordering::SeqCst), 4);
    }

    #[tokio::test]
    async fn walks_all_pages_when_total_is_zero() {
        // Emulates an endpoint that does not report a usable total: the trailing empty page ends it.
        let calls = Arc::new(AtomicUsize::new(0));
        let iter = ListIterator::new(0, None, slicing_fetcher((0..5).collect(), 2, false, calls))
            .with_chunk_size(2);
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 1, 2, 3, 4]);
    }

    #[tokio::test]
    async fn total_zero_exact_multiple_terminates_on_empty_page() {
        // 4 items, page size 2, no usable total: pages [0,1] [2,3] [] — the empty page ends it.
        let calls = Arc::new(AtomicUsize::new(0));
        let iter = ListIterator::new(
            0,
            None,
            slicing_fetcher((0..4).collect(), 2, false, calls.clone()),
        )
        .with_chunk_size(2);
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 1, 2, 3]);
        assert_eq!(calls.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn reported_total_does_not_skip_the_trailing_empty_request() {
        // 4 items, page size 2, total known and an exact multiple of the page size: even though
        // offset reaches `total` right after the second (full) page, that is not consulted, so a
        // third, empty request is still made before the walk ends.
        let calls = Arc::new(AtomicUsize::new(0));
        let iter = ListIterator::new(
            0,
            None,
            slicing_fetcher((0..4).collect(), 2, true, calls.clone()),
        )
        .with_chunk_size(2);
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 1, 2, 3]);
        assert_eq!(calls.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn keeps_paging_past_the_first_pages_total_as_the_listing_grows() {
        // The listing holds 2 items when the first page is read and 4 from then on (e.g. a
        // dataset a still-running Actor keeps pushing to). Each response reports the *current*
        // total, which would wrongly look "complete" at the end of page one if total were
        // consulted; ignoring it lets the walk pick up the rows added afterwards.
        let calls = Arc::new(AtomicUsize::new(0));
        let counter = calls.clone();
        let fetch: PageFetcher<i64> = Box::new(move |offset, limit| {
            let call_number = counter.fetch_add(1, Ordering::SeqCst);
            Box::pin(async move {
                let total_rows: i64 = if call_number == 0 { 2 } else { 4 };
                let page_size = limit.unwrap_or(2);
                let start = (offset.max(0)).min(total_rows);
                let end = (start + page_size).min(total_rows);
                let items: Vec<i64> = (start..end).collect();
                Ok(PaginationList {
                    total: total_rows,
                    offset,
                    limit: page_size,
                    count: items.len() as i64,
                    desc: false,
                    items,
                    scanned: None,
                })
            })
        });
        let iter = ListIterator::new(0, None, fetch).with_chunk_size(2);
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 1, 2, 3]);
        // Pages: [0,1] (total looked like 2) [2,3] (total now 4) [] — three requests.
        assert_eq!(calls.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn advances_by_the_scanned_count_when_it_exceeds_items_returned() {
        // Models a dataset `listItems` filter: each page scans 3 rows (`x-apify-pagination-count`)
        // but a `skip_empty`-style filter leaves only 1 item per page. Advancing by `items.len()`
        // alone would re-request the same rows forever; advancing by the scanned count moves past
        // them.
        let calls = Arc::new(AtomicUsize::new(0));
        let counter = calls.clone();
        let fetch: PageFetcher<i64> = Box::new(move |offset, _limit| {
            counter.fetch_add(1, Ordering::SeqCst);
            Box::pin(async move {
                // 9 raw rows in windows of 3, only the first of each window "survives" the filter.
                let window_start = (offset.max(0) / 3) * 3;
                let items = if window_start < 9 {
                    vec![window_start]
                } else {
                    vec![]
                };
                let scanned = if window_start < 9 { 3 } else { 0 };
                Ok(PaginationList {
                    total: 0,
                    offset,
                    limit: 3,
                    count: items.len() as i64,
                    desc: false,
                    items,
                    scanned: Some(scanned),
                })
            })
        });
        let iter = ListIterator::new(0, None, fetch).with_chunk_size(3);
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 3, 6]);
        // Offsets requested: 0, 3, 6, 9 (empty) — four calls, each advancing by the scanned 3
        // rows rather than the 1 surviving item.
        assert_eq!(calls.load(Ordering::SeqCst), 4);
    }

    #[tokio::test]
    async fn scanned_count_is_capped_at_the_requested_page_size() {
        // A lagging `x-apify-pagination-count` header can occasionally overshoot what was asked
        // for (e.g. counting rows added by a concurrent push); the cap keeps the offset from
        // jumping past rows the next request would then never read.
        let calls = Arc::new(AtomicUsize::new(0));
        let counter = calls.clone();
        let fetch: PageFetcher<i64> = Box::new(move |offset, limit| {
            counter.fetch_add(1, Ordering::SeqCst);
            Box::pin(async move {
                let page_size = limit.unwrap_or(2);
                let start = offset.max(0);
                let end = (start + page_size).min(6);
                let items: Vec<i64> = (start..end).collect();
                // Reports scanning more rows than were actually requested/returned, but only
                // while there is anything left to scan (a genuinely empty page reports 0, same
                // as `items.len()`, so the walk can still terminate).
                let scanned = if items.is_empty() { 0 } else { page_size + 5 };
                Ok(PaginationList {
                    total: 0,
                    offset,
                    limit: page_size,
                    count: items.len() as i64,
                    desc: false,
                    items,
                    scanned: Some(scanned),
                })
            })
        });
        let iter = ListIterator::new(0, None, fetch).with_chunk_size(2);
        // Without the cap this would skip straight past offset 6 and lose items [2..6).
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 1, 2, 3, 4, 5]);
        assert_eq!(calls.load(Ordering::SeqCst), 4);
    }

    #[tokio::test]
    async fn non_final_short_page_with_reported_total_does_not_truncate() {
        // Guards the termination logic, NOT filter de-duplication: every page is "short" (it
        // returns fewer items than the page size the API reports), and the endpoint also reports
        // a large `total`. Neither is consulted, so the walk must keep going and yield every item,
        // ending only on the empty page. (This fetcher advances offset by the page size and never
        // overlaps windows, so it does not model the sparse-window duplicate behaviour
        // documented on `iterate_items`; it only proves a short page is not treated as terminal.)
        let all: Vec<i64> = (0..6).collect();
        let calls = Arc::new(AtomicUsize::new(0));
        let counter = calls.clone();
        let all = Arc::new(all);
        let fetch: PageFetcher<i64> = Box::new(move |offset, _limit| {
            counter.fetch_add(1, Ordering::SeqCst);
            let all = all.clone();
            Box::pin(async move {
                // Serve at most 2 items per page but report a page limit of 4, so every non-final
                // page is strictly short (received 2 < reported limit 4), and `total` is reported
                // large (100) — neither is consulted, so termination can only come from the empty
                // page, never a short page or a reported total.
                let start = (offset.max(0) as usize).min(all.len());
                let end = (start + 2).min(all.len());
                let items = all[start..end].to_vec();
                Ok(PaginationList {
                    total: 100,
                    offset,
                    limit: 4,
                    count: items.len() as i64,
                    desc: false,
                    items,
                    scanned: None,
                })
            })
        });
        let iter = ListIterator::new(0, None, fetch).with_chunk_size(4);
        let got = iter.collect_all().await.unwrap();
        // Every item must be yielded despite each page being short.
        assert_eq!(got, vec![0, 1, 2, 3, 4, 5]);
        // Pages: [0,1] [2,3] [4,5] [] — four calls, none terminated early by short-page detection.
        assert_eq!(calls.load(Ordering::SeqCst), 4);
    }

    #[tokio::test]
    async fn total_limit_caps_items_yielded() {
        // `limit` is a total-item cap: with 100 items available, iterate should yield exactly 3.
        let calls = Arc::new(AtomicUsize::new(0));
        let iter = ListIterator::new(
            0,
            Some(3),
            slicing_fetcher((0..100).collect(), 1000, true, calls.clone()),
        );
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 1, 2]);
        // Requesting limit=3 up front means a single page suffices.
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn total_limit_with_smaller_chunk_size_pages_and_caps() {
        // limit=5 total cap, chunk_size=2 page size: pages of 2 until 5 items are yielded.
        let calls = Arc::new(AtomicUsize::new(0));
        let iter = ListIterator::new(
            0,
            Some(5),
            slicing_fetcher((0..100).collect(), 1000, true, calls.clone()),
        )
        .with_chunk_size(2);
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 1, 2, 3, 4]);
        // Pages: [0,1] [2,3] [4] — the last page is trimmed to the remaining budget of 1.
        assert_eq!(calls.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn cap_truncates_page_that_exceeds_remaining_budget() {
        // Exercises the cap-truncation branch (`received > rem`): the endpoint ignores the
        // requested page limit and returns MORE items than the caller's total cap allows. The
        // iterator must trim the over-long page to the remaining budget and yield exactly `limit`
        // items, fetching only once.
        let calls = Arc::new(AtomicUsize::new(0));
        let counter = calls.clone();
        let fetch: PageFetcher<i64> = Box::new(move |offset, _limit| {
            counter.fetch_add(1, Ordering::SeqCst);
            Box::pin(async move {
                // Always return 5 items regardless of the requested limit.
                Ok(PaginationList {
                    total: 100,
                    offset,
                    limit: 5,
                    count: 5,
                    desc: false,
                    items: vec![0, 1, 2, 3, 4],
                    scanned: None,
                })
            })
        });
        // Total cap of 3, but the page delivers 5 → must be truncated to [0, 1, 2].
        let iter = ListIterator::new(0, Some(3), fetch);
        assert_eq!(iter.collect_all().await.unwrap(), vec![0, 1, 2]);
        assert_eq!(
            calls.load(Ordering::SeqCst),
            1,
            "cap reached on the first (over-long) page, so no second fetch"
        );
    }

    #[tokio::test]
    async fn honours_caller_start_offset() {
        let calls = Arc::new(AtomicUsize::new(0));
        let iter = ListIterator::new(2, None, slicing_fetcher((0..5).collect(), 10, true, calls));
        assert_eq!(iter.collect_all().await.unwrap(), vec![2, 3, 4]);
    }

    #[tokio::test]
    async fn single_page_fetches_once_and_stops() {
        // A non-paginated endpoint: the fetcher ignores offset and returns everything every call,
        // reporting limit == count. Multi-page mode would loop forever here; single-page must not.
        let calls = Arc::new(AtomicUsize::new(0));
        let counter = calls.clone();
        let fetch: PageFetcher<i64> = Box::new(move |offset, _limit| {
            counter.fetch_add(1, Ordering::SeqCst);
            Box::pin(async move {
                Ok(PaginationList {
                    total: 0,
                    offset,
                    limit: 3,
                    count: 3,
                    desc: false,
                    items: vec![10, 20, 30],
                    scanned: None,
                })
            })
        });
        let iter = ListIterator::new_single_page(fetch);
        assert_eq!(iter.collect_all().await.unwrap(), vec![10, 20, 30]);
        assert_eq!(
            calls.load(Ordering::SeqCst),
            1,
            "single-page must fetch exactly once"
        );
    }

    #[tokio::test]
    async fn empty_first_page_yields_nothing() {
        let calls = Arc::new(AtomicUsize::new(0));
        let mut iter = ListIterator::new(0, None, slicing_fetcher(vec![], 5, true, calls));
        assert!(iter.next().await.unwrap().is_none());
    }
}