calmlens 0.34.0

Rust SDK for CalmLens moderation, storage and CDN delivery
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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//! An offline, programmable client for tests (the `testing` feature).
//!
//! [`MockServerClient`] records every request and answers it from a FIFO queue of scripted
//! replies. It uses the real client's validation, request encoding, retries and error mapping;
//! only the network is replaced, and no HTTP client is created. [`MockServerClient::in_memory`]
//! adds an in-memory API behind the queue: uploads create assets that a `moderate` callback
//! ([`MockOptions::moderate`]) decides with a [`MockDecision`], and assets, folders, hash lists,
//! events and CDN downloads behave like the API's. Against a real project, signed test inputs
//! ([`generate_test_text_code`], [`paint_test_image`], [`generate_media_test_code`]) make
//! CalmLens decide an input as a chosen [`TestInput`] scenario. Enable the feature in
//! `[dev-dependencies]`:
//!
//! ```toml
//! [dev-dependencies]
//! calmlens = { version = "0.28", features = ["testing"] }
//! ```
// Generated (packages/sdk-contract/mockAssetFilters.ts).
mod asset_filters;
// Generated, one line per list (packages/sdk-contract/mockClassification.ts).
#[rustfmt::skip]
mod classification;
mod decision;
mod image_grid;
mod inputs;
mod memory;
mod placeholder;
mod url_asset_types;

pub use decision::{
    IntoMockDecision, MockDecision, MockModerationContent, MockModerationInput,
    MockModerationTrigger, MockOutcome,
};
pub use inputs::{
    generate_media_test_code, generate_test_text_code, paint_test_image, test_input_for_signal,
    TestAgeRange, TestDetectionList, TestDetections, TestFace, TestFaceAge, TestImage, TestInput,
    TestInputInstance, TestInputKind, TestInputPreset, TestPerson, TestRegionalAction,
    TestScenario, MEDIA_TEST_SIGNALS, TEST_INPUT_SIGNALS,
};
pub use memory::{
    create_mock_api_key, MockApiKeyGrant, MockFailure, MockFailureOptions, MockOptions,
    MockServedUrl, MockStorage, StoredContent, MOCK_API_KEY_ID,
};

use crate::error::ErrorCode;
use crate::models::{Actor, ActorType, Asset, AssetModerationDecision, Event, EventResourceKind};
use crate::transport::{Attempt, AttemptBody, Exchange, ResponseBody};
use crate::{Error, Result, ServerClient, ServerClientBuilder};
use bytes::Bytes;
use http::{HeaderMap, HeaderName, HeaderValue};
use serde_json::Value;
use std::{
    collections::{HashMap, VecDeque},
    io,
    ops::Deref,
    sync::{Arc, Mutex},
    time::Duration,
};

/// A request captured by [`MockServerClient`], decoded so tests need not parse URLs by hand.
#[derive(Clone)]
#[non_exhaustive]
pub struct Request {
    /// The HTTP method, e.g. `POST`.
    pub method: String,
    /// Full request URL, including the configured base URL and query string.
    pub url: String,
    /// The path relative to the API (or CDN) base URL, each segment decoded, e.g.
    /// `/projects/project-1/assets` or `/assets/asset_e5Gm2Rk8XqT3wLp9VbN4`. A request to
    /// neither base URL keeps its whole decoded URL path.
    pub path: String,
    /// Decoded query parameters in request order.
    pub query: Vec<(String, String)>,
    /// Header names are lowercase; repeated headers keep their last value.
    pub headers: HashMap<String, String>,
    /// The request body, including streamed uploads.
    pub body: Vec<u8>,
    /// The HTTP status the mock answered with, or `None` when the request failed without a
    /// response (a transport error or timeout).
    pub response_status: Option<u16>,
}
impl std::fmt::Debug for Request {
    /// Everything but the API key, which a mock built with [`MockServerClient::from_builder`] may
    /// have been given for real.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut headers = self.headers.clone();
        if let Some(api_key) = headers.get_mut("x-api-key") {
            *api_key = "[redacted]".to_owned();
        }
        f.debug_struct("Request")
            .field("method", &self.method)
            .field("url", &self.url)
            .field("path", &self.path)
            .field("query", &self.query)
            .field("headers", &headers)
            .field("body", &self.body)
            .field("response_status", &self.response_status)
            .finish()
    }
}
impl Request {
    /// The first value of query parameter `name`.
    pub fn query_param(&self, name: &str) -> Option<&str> {
        self.query
            .iter()
            .find(|(key, _)| key == name)
            .map(|(_, value)| value.as_str())
    }
    /// The value of header `name` (case-insensitive).
    pub fn header(&self, name: &str) -> Option<&str> {
        self.headers
            .get(&name.to_ascii_lowercase())
            .map(String::as_str)
    }
    /// The body parsed as JSON, or `None` when it is empty or not JSON.
    pub fn json(&self) -> Option<Value> {
        serde_json::from_slice(&self.body).ok()
    }
}

/// A queued response with a status, headers and body.
#[derive(Clone, Debug)]
pub struct MockResponse {
    status: u16,
    headers: HeaderMap,
    body: Bytes,
    /// An API error body, which gets the response's request ID as `requestId`.
    api_error: bool,
}
impl MockResponse {
    /// An empty response with `status`.
    pub fn new(status: u16) -> Self {
        Self {
            status,
            headers: HeaderMap::new(),
            body: Bytes::new(),
            api_error: false,
        }
    }
    /// A JSON response serialized from a model or `serde_json::Value`.
    pub fn json(status: u16, body: impl serde::Serialize) -> Self {
        Self::new(status)
            .header("content-type", "application/json")
            .body(serde_json::to_vec(&body).expect("serializable mock response"))
    }
    /// The API's error response for `code`, with the status the API uses for it (400 for
    /// codes without one) and the same body shape: `{code, message, requestId, details}`, where
    /// `requestId` is the `x-request-id` header the mock adds.
    pub fn api_error(code: impl Into<ErrorCode>, message: impl Into<String>) -> Self {
        let code = code.into();
        let status = code.http_status().map_or(400, |status| status.as_u16());
        Self::api_error_body(status, code.as_str(), message.into(), serde_json::json!({}))
    }
    /// An API error response with `status` and the body `{code, message, requestId, details}`.
    pub(crate) fn api_error_body(status: u16, code: &str, message: String, details: Value) -> Self {
        let mut response = Self::json(
            status,
            serde_json::json!({"code": code, "message": message, "details": details}),
        );
        response.api_error = true;
        response
    }
    /// Adds a header, e.g. `content-type`, `content-range` or `retry-after`.
    pub fn header(mut self, name: &str, value: &str) -> Self {
        self.headers.append(
            HeaderName::from_bytes(name.as_bytes()).expect("valid header name"),
            HeaderValue::from_str(value).expect("valid header value"),
        );
        self
    }
    /// Sets the body.
    pub fn body(mut self, body: impl Into<Bytes>) -> Self {
        self.body = body.into();
        self
    }
}

enum Reply {
    Response(MockResponse),
    Error(Error),
    /// No response until the attempt's timeout.
    Hang,
}
#[derive(Default)]
struct State {
    replies: VecDeque<Reply>,
    requests: Vec<Request>,
    backend: Option<memory::Backend>,
    /// Virtual time the client's waits and timeouts have advanced.
    elapsed: Duration,
    /// Each wait between attempts and polls, in order.
    sleeps: Vec<Duration>,
    /// The API and CDN base URLs, which recorded paths are relative to.
    base_urls: [String; 2],
}
pub(crate) struct MockTransport {
    state: Mutex<State>,
    started: tokio::time::Instant,
}
impl MockTransport {
    fn new(state: State) -> Self {
        Self {
            state: Mutex::new(state),
            started: tokio::time::Instant::now(),
        }
    }
    fn state(&self) -> std::sync::MutexGuard<'_, State> {
        self.state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }
    /// Whether an in-memory API answers requests, which then makes the multipart size checks.
    pub(crate) fn defers_upload_checks(&self) -> bool {
        self.state().backend.is_some()
    }
    /// The virtual clock: the time the mock was created plus the waits since.
    pub(crate) fn now(&self) -> tokio::time::Instant {
        self.started + self.state().elapsed
    }
    /// Advances the virtual clock instead of waiting, then yields once to the runtime.
    pub(crate) async fn sleep(&self, duration: Duration) {
        {
            let mut state = self.state();
            state.elapsed = state.elapsed.saturating_add(duration);
            state.sleeps.push(duration);
        }
        tokio::task::yield_now().await;
    }
    pub(crate) async fn exchange(
        &self,
        attempt: Attempt,
        timeout: Option<Duration>,
    ) -> Result<Exchange> {
        let body = match attempt.body {
            AttemptBody::Empty => Vec::new(),
            AttemptBody::Bytes(bytes) => bytes.to_vec(),
            AttemptBody::Stream(stream, size) => {
                let body = crate::transport::collect(stream).await?;
                if let Some(size) = size.filter(|size| body.len() as u64 != *size) {
                    return Err(Error::new(
                        ErrorCode::TransportError,
                        format!("Upload stream yielded {} bytes, not {size}", body.len()),
                    ));
                }
                body
            }
        };
        let url = &attempt.url;
        let mut state = self.state();
        let number = state.requests.len() + 1;
        let reply = match state.replies.pop_front() {
            Some(reply) => Some(reply),
            None => state.backend.as_mut().map(|backend| {
                let Some(queued) =
                    backend.take_failure(&attempt.method, url, &attempt.headers, &body)
                else {
                    return match backend.handle(&attempt.method, url, &attempt.headers, &body) {
                        Ok(response) => Reply::Response(response),
                        Err(error) => Reply::Error(error),
                    };
                };
                match queued.failure {
                    MockFailure::TransportError => Reply::Error(io_failure(
                        ErrorCode::TransportError,
                        io::ErrorKind::ConnectionReset,
                        "connection reset",
                    )),
                    MockFailure::Timeout => Reply::Error(io_failure(
                        ErrorCode::Timeout,
                        io::ErrorKind::TimedOut,
                        "operation timed out",
                    )),
                    MockFailure::Api(code) => {
                        let message = format!("Injected {code} failure");
                        let status = code.http_status().map_or(400, |status| status.as_u16());
                        let details = Value::Object(queued.options.details.unwrap_or_default());
                        let mut response =
                            MockResponse::api_error_body(status, code.as_str(), message, details);
                        if let Some(seconds) = queued.options.retry_after {
                            response = response.header("retry-after", &seconds.to_string());
                        }
                        Reply::Response(response)
                    }
                }
            }),
        };
        let path = relative_path(url, &state.base_urls);
        state.requests.push(Request {
            method: attempt.method.to_string(),
            url: url.to_string(),
            path,
            query: url.query_pairs().into_owned().collect(),
            headers: attempt
                .headers
                .iter()
                .map(|(name, value)| (name.to_string(), value.to_str().unwrap_or("").to_string()))
                .collect(),
            body,
            response_status: match &reply {
                Some(Reply::Response(response)) => Some(response.status),
                _ => None,
            },
        });
        match reply {
            Some(Reply::Response(mut response)) => {
                // API errors the mock builds carry a request ID, in the header and the body;
                // responses a test scripts are sent exactly as scripted.
                if response.api_error && !response.headers.contains_key("x-request-id") {
                    let request_id = format!("mock-request-{number}");
                    if let Ok(Value::Object(mut error)) =
                        serde_json::from_slice::<Value>(&response.body)
                    {
                        error.insert("requestId".into(), request_id.clone().into());
                        response.body = serde_json::to_vec(&error)
                            .expect("serializable mock response")
                            .into();
                    }
                    response.headers.insert(
                        "x-request-id",
                        HeaderValue::from_str(&request_id).expect("valid header value"),
                    );
                }
                Ok(Exchange {
                    status: response.status,
                    headers: response.headers,
                    body: ResponseBody::Mock(response.body),
                })
            }
            Some(Reply::Error(error)) => Err(error),
            Some(Reply::Hang) => {
                // The attempt's deadline passes on the virtual clock; without one it would
                // wait forever, so it fails at once.
                if let Some(timeout) = timeout {
                    state.elapsed = state.elapsed.saturating_add(timeout);
                }
                Err(io_failure(
                    ErrorCode::Timeout,
                    io::ErrorKind::TimedOut,
                    "operation timed out",
                ))
            }
            None => Err(Error::new(
                ErrorCode::TransportError,
                "No queued mock response",
            )),
        }
    }
}

/// An offline client that records requests and answers them from a FIFO queue of scripted
/// replies, then, when created with [`in_memory`](Self::in_memory), from an in-memory API.
/// Dereferences to [`ServerClient`].
///
/// Waits take no real time: retry backoff and `wait_for_decision` advance a virtual clock
/// ([`elapsed`](Self::elapsed)), so a decision that never comes times out at once.
///
/// ```
/// # async fn run() -> calmlens::Result<()> {
/// use calmlens::{models::*, testing::MockServerClient};
/// let mock = MockServerClient::new()?;
/// mock.enqueue(
///     200,
///     Asset::new("asset-1", AssetClassificationStatus::Completed, Some(AssetModerationDecision::Allowed)),
/// );
/// let asset = mock.assets().classify(SubmitAsset::text("hello")).await?;
/// assert_eq!(asset.id, "asset-1");
/// assert_eq!(mock.last_request().unwrap().body, b"hello");
/// # Ok(()) }
/// ```
pub struct MockServerClient {
    client: ServerClient,
    #[cfg_attr(not(feature = "blocking"), allow(dead_code))]
    builder: ServerClientBuilder,
    transport: Arc<MockTransport>,
}
impl std::fmt::Debug for MockServerClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MockServerClient")
            .field("client", &self.client)
            .field("remaining_responses", &self.remaining_responses())
            .finish()
    }
}
impl MockServerClient {
    /// A mock for project `project-1` with placeholder base URLs. Retries are off, so each
    /// call consumes exactly one queued reply; use [`MockServerClient::from_builder`] to test them.
    pub fn new() -> Result<Self> {
        Self::from_builder(
            ServerClient::builder("mock-key", "project-1")
                .base_url(memory::API_BASE_URL)
                .cdn_base_url(memory::CDN_BASE_URL)
                .max_retries(0),
        )
    }
    /// A mock backed by an in-memory API, configured by `options`. Queued replies are still
    /// used first, in order; once the queue is empty, requests reach the in-memory API.
    ///
    /// Uploads create assets, which [`MockOptions::moderate`] decides (by default queued
    /// uploads, from `assets().create` or a queued multipart upload, stay `pending` until
    /// [`complete_processing`](Self::complete_processing), and the rest are `allowed`). A decided
    /// asset gets a full report, and the events feed records `asset.uploaded`, then
    /// `asset.classified` and the automated `asset.<status>`, or `asset.failed`. Finished
    /// processing removes the content unless the upload set `retain_content`;
    /// `purged` removes it with reason `policy`.
    /// Retrieve, list (RSQL filter, ordering, folder, cursor), children, update, move, delete,
    /// delete content, retry, keyword search, signed URLs, folders, hash lists, lenses, upload
    /// preset definitions, project settings, metadata fields and the events feed read and change
    /// that storage, with the API's error codes, statuses and idempotent
    /// replays. `wait_for_decision` returns at once for a settled asset (processing `completed`
    /// or `failed`); a queued or processing one waits for [`decide`](Self::decide),
    /// [`complete_processing`](Self::complete_processing) or the timeout. An API-key upload naming
    /// a preset takes its folder, visibility and processing options and keeps to its limits. The
    /// CDN serves stored bytes, refusing
    /// blocked and purged assets, age-restricted ones requested without
    /// `ViewerClaims::age_confirmed` set to `true` whatever the key, and private ones unless the
    /// API key has an `assets:download` grant ([`create_mock_api_key`]). Without a grant or an API
    /// key, a private asset answers `asset/not-found` whatever its state, including
    /// its placeholder and info. Served content has its stored `content-type` and
    /// the CDN's `cache-control`: `public` only when anyone may see the asset, otherwise
    /// `private` according to access. Public files are fresh for a day; signed files are capped by the
    /// grant expiry, and unsigned private files must revalidate.
    /// Content is never classified or transformed.
    ///
    /// ```
    /// # async fn run() -> calmlens::Result<()> {
    /// use calmlens::{models::*, testing::*};
    /// let mock = MockServerClient::in_memory(MockOptions::new())?;
    /// let asset = mock.assets().create(SubmitAsset::text("hello")).await?;
    /// assert_eq!(asset.classification.moderation.decision, None);
    /// mock.complete_processing(&asset.id, Some(MockOutcome::Blocked.into()))?;
    /// let asset = mock.assets().retrieve(AssetId::new(&asset.id)).await?;
    /// assert_eq!(asset.classification.moderation.decision, Some(AssetModerationDecision::Blocked));
    /// # Ok(()) }
    /// ```
    pub fn in_memory(options: MockOptions) -> Result<Self> {
        let mut builder =
            ServerClient::builder(options.api_key.clone(), options.project_id.clone())
                .base_url(&options.api_base_url)
                .cdn_base_url(&options.cdn_base_url)
                .max_retries(options.max_retries);
        if let Some(value) = options.upload_timeout {
            builder = builder.upload_timeout(value);
        }
        if let Some(value) = options.max_response_bytes {
            builder = builder.max_response_bytes(value);
        }
        if let Some(value) = options.part_size_bytes {
            builder = builder.part_size_bytes(value);
        }
        if let Some((name, version, url)) = &options.app_info {
            builder = builder.app_info(name, version.as_deref(), url.as_deref());
        }
        if let Some(hook) = &options.on_attempt {
            let hook = hook.clone();
            builder = builder.on_attempt(move |event| hook(event));
        }
        let transport = Arc::new(MockTransport::new(State {
            backend: Some(memory::Backend::new(options)),
            base_urls: builder.base_urls(),
            ..State::default()
        }));
        let client = builder.clone().build_mock(transport.clone())?;
        Ok(Self {
            client,
            builder,
            transport,
        })
    }
    fn backend<R>(&self, f: impl FnOnce(&mut memory::Backend) -> R) -> R {
        let mut state = self.transport.state();
        let backend = state
            .backend
            .as_mut()
            .expect("this MockServerClient was not created with MockServerClient::in_memory");
        f(backend)
    }
    /// Runs `f` on the in-memory storage, e.g. to seed assets or inspect stored content.
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn with_storage<R>(&self, f: impl FnOnce(&mut MockStorage) -> R) -> R {
        self.backend(|backend| f(&mut backend.storage))
    }
    /// Applies a reviewer decision to a stored asset, as the portal would: the moderation
    /// status changes (`purged` removes the content), the review is added to `manual_reviews`,
    /// and an `asset.<status>` event whose `actor` is a `user` joins the events feed (after an
    /// `asset.content-removed` event for a purge).
    /// The report stays as moderation left it, but for any
    /// [`pending_review`](MockDecision::pending_review), which this decision settles. Fails with `sdk/invalid-argument` for a decision
    /// that is not `allowed`, `limited`, `age-restricted`, `blocked` or `purged`, and with
    /// `asset/not-found` (status 404) for an unknown asset.
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn decide(&self, asset_id: &str, decision: AssetModerationDecision) -> Result<Asset> {
        if !decision.is_known() {
            return Err(Error::new(
                ErrorCode::InvalidArgument,
                "decision must be one of allowed, limited, age-restricted, blocked, purged",
            ));
        }
        let asset = self
            .backend(|backend| backend.decide(asset_id, decision.as_str()))
            .ok_or_else(|| {
                Error::new(ErrorCode::AssetNotFound, "Asset not found").with_http_status(404)
            })?;
        decode_asset(asset)
    }
    /// Finishes processing a stored asset as the pipeline would and returns it: with
    /// `decision`, or else what [`MockOptions::moderate`] returns for the
    /// [`Complete`](MockModerationTrigger::Complete) trigger (by default `allowed`). It records
    /// the processing events and no manual review; a `pending` decision leaves the asset
    /// processing. Fails with `asset/not-found`
    /// (status 404) for an unknown asset and with `sdk/invalid-argument` for one that isn't
    /// processing.
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn complete_processing(
        &self,
        asset_id: &str,
        decision: Option<MockDecision>,
    ) -> Result<Asset> {
        let asset = self.backend(|backend| backend.complete_processing(asset_id, decision))?;
        decode_asset(asset)
    }
    /// Replaces the grants of the mock's API key, as editing the key in the portal would: later
    /// requests, such as CDN downloads, use them. The key itself stays the same, so its
    /// idempotency keys still replay.
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn set_api_key_grants(&self, grants: &[MockApiKeyGrant]) {
        self.backend(|backend| backend.set_grants(grants));
    }
    /// Makes the next request (or, with [`MockFailureOptions::operation`], the next request of
    /// that operation), or the next [`times`](MockFailureOptions::times) of them, fail with
    /// `failure` instead of reaching the in-memory API, which it leaves unchanged. An API error
    /// carries the options' `details` and, with `retry_after`, a `Retry-After` header. Failures
    /// queue in order and a request takes the oldest one that matches it, using up one of its
    /// times, so a client retry takes the same failure again until it is used up, then the next
    /// queued failure or the API.
    ///
    /// The operation is the client method as a call spells it, such as `assets().create`,
    /// `assets().upload_part`, `cdn().serve` or `ensure_lenses`; a convenience fails with the
    /// request it sends (`assets().wait_for_decision` and `events().retrieve_asset` with
    /// `assets().retrieve`, `ensure_lens` with `ensure_lenses`, iterators and
    /// `events().pages` with their list). A text upload is `text().classify`'s request alone,
    /// and a file or URL upload `assets().classify`'s. Fails with `sdk/invalid-argument`,
    /// queueing nothing, for an unknown operation, `times` of 0, or `details` or `retry_after`
    /// with a transport error or timeout.
    ///
    /// ```
    /// # async fn run() -> calmlens::Result<()> {
    /// use calmlens::{models::*, testing::*};
    /// let mock = MockServerClient::in_memory(MockOptions::new())?;
    /// use calmlens::ErrorCode;
    /// mock.fail_next_request(
    ///     MockFailure::TransportError,
    ///     MockFailureOptions::new().operation("assets().retrieve").times(2),
    /// )?;
    /// for _ in 0..2 {
    ///     let error = mock.assets().retrieve(AssetId::new("asset-1")).await.unwrap_err();
    ///     assert_eq!(error.code(), &ErrorCode::TransportError);
    /// }
    /// // The third request reaches the in-memory API.
    /// let error = mock.assets().retrieve(AssetId::new("asset-1")).await.unwrap_err();
    /// assert_eq!(error.code(), &ErrorCode::AssetNotFound);
    /// # Ok(()) }
    /// ```
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn fail_next_request(
        &self,
        failure: MockFailure,
        options: MockFailureOptions,
    ) -> Result<()> {
        if options.times == Some(0) {
            return Err(Error::new(
                ErrorCode::InvalidArgument,
                "times must be at least 1",
            ));
        }
        let remaining = Some(options.times.unwrap_or(1));
        self.queue_failure(failure, options, remaining)
    }
    /// Makes every request (or every request of [`MockFailureOptions::operation`]) fail as
    /// [`fail_next_request`](Self::fail_next_request) would, until
    /// [`clear_failures`](Self::clear_failures). Queued after other failures, it fails the
    /// requests they don't. Fails with `sdk/invalid-argument` for options with `times`, and as
    /// `fail_next_request` does.
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn fail_requests(&self, failure: MockFailure, options: MockFailureOptions) -> Result<()> {
        if options.times.is_some() {
            return Err(Error::new(
                ErrorCode::InvalidArgument,
                "fail_requests fails every request: use fail_next_request for times",
            ));
        }
        self.queue_failure(failure, options, None)
    }
    /// Makes `url` answer with `served`'s content and content type when a URL upload fetches it,
    /// as a site would; registering it again replaces them. A URL upload of it then stores the
    /// content (`content.format` is the content type, `content.size_in_bytes` its length, the
    /// CDN's original serves it and `moderate` receives it) and is typed by the content type as
    /// an upload of the bytes would be: `image/png` is an image, while `text/html` and
    /// `application/xhtml+xml` are a website, which stores nothing. A URL not served this way is
    /// typed by its path's extension, and has no content. URLs are compared after parsing. Fails
    /// with `sdk/invalid-argument` for a relative URL or an empty content type.
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn serve_url(&self, url: &str, served: MockServedUrl) -> Result<()> {
        self.backend(|backend| backend.serve_url(url, served))
    }
    /// Makes the mock hash an image upload of exactly `content` as `hash`, a PDQ hash of 64
    /// hexadecimal characters, with `quality` (0 to 100), instead of the SHA-256 of its bytes.
    /// Hash list entries match hashes up to 31 bits away, as in CalmLens, so a test can make a
    /// resized or recompressed copy a near match of a listed image: declare the copy's hash a few
    /// bits from the entry's. Fails with `sdk/invalid-argument` for an invalid hash or quality.
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn set_perceptual_hash(
        &self,
        content: impl Into<Vec<u8>>,
        hash: &str,
        quality: u8,
    ) -> Result<()> {
        let content = content.into();
        self.backend(|backend| backend.set_perceptual_hash(content, hash, quality))
    }
    /// Drops every failure [`fail_next_request`](Self::fail_next_request) and
    /// [`fail_requests`](Self::fail_requests) queued, so requests reach the in-memory API again.
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn clear_failures(&self) {
        self.backend(|backend| backend.clear_failures());
    }
    fn queue_failure(
        &self,
        failure: MockFailure,
        options: MockFailureOptions,
        remaining: Option<u32>,
    ) -> Result<()> {
        if let Some(operation) = &options.operation {
            if !memory::OPERATIONS.contains(&operation.as_str()) {
                return Err(Error::new(
                    ErrorCode::InvalidArgument,
                    format!("{operation:?} is not a client method that sends requests"),
                ));
            }
        }
        if !matches!(failure, MockFailure::Api(_))
            && (options.details.is_some() || options.retry_after.is_some())
        {
            return Err(Error::new(
                ErrorCode::InvalidArgument,
                "details and retry_after apply only to an API error code",
            ));
        }
        self.backend(|backend| {
            backend.queue_failure(memory::QueuedFailure {
                failure,
                options,
                remaining,
            });
        });
        Ok(())
    }
    /// Appends `event` to the in-memory events feed and returns it. An empty ID or project ID,
    /// or a zero creation time, is filled in, and an event without an actor is the mock API
    /// key's (`api-key` with the ID [`MOCK_API_KEY_ID`]); an event about an asset (a `resource` such as
    /// [`EventResource::asset`](crate::models::EventResource::asset)) takes that asset's next `resource.sequence` like the API's (any
    /// given one is ignored); other resources are kept as given, and an event with no
    /// `resource` is about none. `events().list` returns it to cursors taken
    /// before it was added.
    ///
    /// # Panics
    ///
    /// When the mock was not created with [`MockServerClient::in_memory`].
    pub fn enqueue_event(&self, mut event: Event) -> Event {
        self.backend(|backend| {
            if event.id.is_empty() {
                event.id = backend.create_id("evt");
            }
            if event.project_id.is_empty() {
                event.project_id = backend.project_id().to_owned();
            }
            if event.created_at == 0 {
                event.created_at = backend.now();
            }
            // The mock's API key, unless the event names someone else.
            if event.actor.actor_type.as_str().is_empty() {
                event.actor = Actor::new(ActorType::ApiKey, Some(MOCK_API_KEY_ID.to_owned()));
            }
            if let Some(resource) = event
                .resource
                .as_mut()
                .filter(|resource| resource.kind == EventResourceKind::Asset)
            {
                resource.sequence = backend.next_asset_sequence(&resource.id);
            }
            let stored = serde_json::to_value(&event).expect("serializable event");
            backend.storage.events.push(stored);
            event
        })
    }
    /// A mock configured exactly like a real client, e.g.
    /// `MockServerClient::from_builder(ServerClient::builder(key, project).base_url(url).max_retries(2))`.
    /// The configuration is validated the same way; no network requests are made.
    pub fn from_builder(builder: ServerClientBuilder) -> Result<Self> {
        let transport = Arc::new(MockTransport::new(State {
            base_urls: builder.base_urls(),
            ..State::default()
        }));
        let client = builder.clone().build_mock(transport.clone())?;
        Ok(Self {
            client,
            builder,
            transport,
        })
    }
    /// A [`ServerClient`] answered by this mock's queue and recorded in its
    /// [`requests`](Self::requests), for code under test that owns its client.
    pub fn client(&self) -> ServerClient {
        self.client.clone()
    }
    /// Fetches a CDN URL as a browser would, with a GET and without the API key: a signed asset
    /// URL from `cdn().sign_asset_url`, or a public one from
    /// `cdn().get_asset_url`. A private asset's plain URL answers `asset/not-found`, whatever the
    /// asset's state, including its placeholder and info. The request is recorded like any other, and the response is returned
    /// whatever its status.
    pub async fn fetch_cdn(&self, url: &str) -> Result<crate::CdnResponse> {
        self.client.cdn().fetch_unauthenticated(url).await
    }
    /// A [`blocking::ServerClient`](crate::blocking::ServerClient) answered by this mock's queue.
    #[cfg(feature = "blocking")]
    pub fn blocking(&self) -> Result<crate::blocking::ServerClient> {
        crate::blocking::ServerClient::from_async(
            self.builder.clone().build_mock(self.transport.clone())?,
        )
    }
    /// Queues a JSON response serialized from a native model or `serde_json::Value`.
    pub fn enqueue<T: serde::Serialize>(&self, status: u16, body: T) {
        self.enqueue_response(MockResponse::json(status, body));
    }
    /// Queues a response with a raw body and no headers.
    pub fn enqueue_raw(&self, status: u16, body: impl Into<Bytes>) {
        self.enqueue_response(MockResponse::new(status).body(body));
    }
    /// Queues a response with headers, e.g. a CDN response with `content-type`.
    pub fn enqueue_response(&self, response: MockResponse) {
        self.transport
            .state()
            .replies
            .push_back(Reply::Response(response));
    }
    /// Queues the API's error response for `code` with its real HTTP status, so the client
    /// returns the same [`Error`] as against the API.
    pub fn enqueue_api_error(&self, code: impl Into<ErrorCode>, message: impl Into<String>) {
        self.enqueue_response(MockResponse::api_error(code, message));
    }
    /// Queues a failure the next request returns as-is, after the request is captured.
    pub fn enqueue_error(&self, error: Error) {
        self.transport
            .state()
            .replies
            .push_back(Reply::Error(error));
    }
    /// Queues an `sdk/transport-error` (connection reset) whose `source()` is an `io::Error`.
    pub fn enqueue_transport_error(&self, message: &str) {
        self.enqueue_error(io_failure(
            ErrorCode::TransportError,
            io::ErrorKind::ConnectionReset,
            message,
        ));
    }
    /// Queues a request that gets no response: it fails with `sdk/timeout` once the attempt's
    /// timeout passes on the virtual clock (at once for an attempt without one).
    pub fn enqueue_hang(&self) {
        self.transport.state().replies.push_back(Reply::Hang);
    }
    /// Queues an `sdk/timeout` whose `source()` is an `io::Error` of kind `TimedOut`.
    pub fn enqueue_timeout(&self) {
        self.enqueue_error(io_failure(
            ErrorCode::Timeout,
            io::ErrorKind::TimedOut,
            "operation timed out",
        ));
    }
    /// Captured requests, oldest first.
    pub fn requests(&self) -> Vec<Request> {
        self.transport.state().requests.clone()
    }
    /// Forgets the requests captured so far.
    pub fn clear_requests(&self) {
        self.transport.state().requests.clear();
    }
    /// The most recent captured request.
    pub fn last_request(&self) -> Option<Request> {
        self.transport.state().requests.last().cloned()
    }
    /// The number of queued replies not yet used.
    pub fn remaining_responses(&self) -> usize {
        self.transport.state().replies.len()
    }
    /// The virtual time this mock's clients have waited: retry backoff, `wait_for_decision`
    /// and the timeouts of [`enqueue_hang`](Self::enqueue_hang) advance it instead of sleeping.
    pub fn elapsed(&self) -> Duration {
        self.transport.state().elapsed
    }
    /// Each wait the clients made between attempts (retry backoff, `Retry-After`) and
    /// between `wait_for_decision` checks, in order.
    pub fn sleeps(&self) -> Vec<Duration> {
        self.transport.state().sleeps.clone()
    }
}
/// Signs an upload token exactly as
/// [`UploadTokensClient::sign`](crate::UploadTokensClient::sign) does, with the same
/// validation, but at `now` (milliseconds since the Unix epoch) and with `nonce`, so a test can
/// compare the whole token.
pub fn sign_upload_token(
    api_key: &str,
    options: crate::models::SignUploadToken,
    now: u64,
    nonce: &str,
) -> Result<crate::models::SignedUploadToken> {
    crate::client::sign_upload_token(api_key, options, now, nonce)
}

/// `url`'s path relative to the first base URL it is under, as `/` and its decoded segments.
fn relative_path(url: &reqwest::Url, base_urls: &[String; 2]) -> String {
    let rest = base_urls
        .iter()
        .find_map(|base| memory::under(url, base))
        .unwrap_or(url.path());
    let segments: Vec<String> = rest
        .split('/')
        .filter(|segment| !segment.is_empty())
        .map(memory::percent_decode)
        .collect();
    format!("/{}", segments.join("/"))
}

fn decode_asset(asset: Value) -> Result<Asset> {
    serde_json::from_value(asset)
        .map_err(|cause| Error::new(ErrorCode::InvalidResponse, cause.to_string()))
}

fn io_failure(code: ErrorCode, kind: io::ErrorKind, message: &str) -> Error {
    Error::new(code, message).with_source(io::Error::new(kind, message.to_string()))
}
impl Deref for MockServerClient {
    type Target = ServerClient;
    fn deref(&self) -> &ServerClient {
        &self.client
    }
}

#[cfg(test)]
mod tests {
    use super::classification::{CATEGORIES, QUALIFIERS};
    use serde_json::{json, Value};

    /// The generated category and qualifier lists the decision builder checks match the
    /// contract's `classification` in `testdata/surface.json`.
    #[test]
    fn classification_lists_match_the_surface() {
        let path = concat!(env!("CARGO_MANIFEST_DIR"), "/testdata/surface.json");
        let surface: Value = serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
        let qualifiers: serde_json::Map<String, Value> = QUALIFIERS
            .iter()
            .map(|(category, qualifiers)| ((*category).to_owned(), json!(qualifiers)))
            .collect();
        assert_eq!(
            json!({"categories": CATEGORIES, "qualifiers": qualifiers}),
            surface["classification"]
        );
    }
}