loops-so 0.1.0

Unofficial Rust SDK for the Loops.so email platform
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
//! A mock Loops API for testing code that uses this crate.
//!
//! [`LoopsMock`] starts a local [`wiremock`] server that answers every Loops
//! endpoint with a successful default response, and points the process-wide
//! client at it. Your code runs against the real [`LoopsClient`] end to end,
//! but no request leaves the process.
//!
//! Enable the `testing` feature:
//!
//! ```toml
//! [dev-dependencies]
//! loops-so = { version = "*", features = ["testing"] }
//! ```
//!
//! ```no_run
//! # async fn run() -> loops_so::Result<()> {
//! use loops_so::routes::Route;
//! use loops_so::testing::LoopsMock;
//! use loops_so::TransactionalOptions;
//!
//! let mock = LoopsMock::start().await;
//! let client = loops_so::get_client()?;
//! client
//!     .send_transactional_email("tpl_abc", "user@test.com", TransactionalOptions::new())
//!     .await?;
//!
//! // Inspect what was sent over the wire.
//! let body: serde_json::Value = mock.calls(Route::Transactional).await[0].body_json().unwrap();
//! assert_eq!(body["transactionalId"], "tpl_abc");
//! # Ok(())
//! # }
//! ```
//!
//! Simulate a failure by overriding a route before the call. The latest
//! override of a route wins; [`LoopsMock::reset_route`] restores the default:
//!
//! ```no_run
//! # async fn run() {
//! use loops_so::routes::Route;
//! use loops_so::testing::{LoopsMock, wiremock::ResponseTemplate};
//!
//! let mock = LoopsMock::start().await;
//! mock.respond(
//!     Route::Transactional,
//!     ResponseTemplate::new(429)
//!         .insert_header("x-ratelimit-limit", "10")
//!         .insert_header("x-ratelimit-remaining", "0"),
//! );
//! // send_transactional_email now fails with LoopsError::RateLimit.
//! # }
//! ```
//!
//! While a `LoopsMock` is alive it holds a process-wide lock, so tests that
//! use it run one at a time even under the default parallel test runner.
//! Dropping it resets the client singleton and restores the configuration it
//! replaced. Starting a second mock while one is alive therefore waits for
//! the first to drop; in a single test, drop the first one explicitly.

use std::collections::HashMap;
use std::sync::{Arc, Mutex as StdMutex, PoisonError};

use serde_json::{Value, json};
use tokio::sync::{Mutex, MutexGuard};
pub use wiremock;
use wiremock::matchers::{method, path_regex};
use wiremock::{Mock, MockServer, Request, Respond, ResponseTemplate};

use crate::LoopsClient;
use crate::config::{ConfigureOptions, configure, snapshot};
use crate::routes::Route;

/// The API key the mock configures unless told otherwise.
pub const TEST_API_KEY: &str = "test-key";

/// Path prefix the mock serves the API under, mirroring
/// `https://app.loops.so/api`.
const API_PREFIX: &str = "/api";

const ROUTE_PRIORITY: u8 = 1;
const UNMATCHED_PRIORITY: u8 = u8::MAX;

static GLOBAL: Mutex<()> = Mutex::const_new(());

/// Configures a [`LoopsMock`] before it starts.
#[derive(Debug, Clone)]
#[must_use]
pub struct LoopsMockBuilder {
    api_key: String,
    safe_mode: bool,
    safe_mode_allowed_domains: Vec<String>,
}

impl Default for LoopsMockBuilder {
    fn default() -> Self {
        Self {
            api_key: TEST_API_KEY.to_owned(),
            safe_mode: false,
            safe_mode_allowed_domains: Vec::new(),
        }
    }
}

impl LoopsMockBuilder {
    /// The API key to configure (default [`TEST_API_KEY`]).
    pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
        self.api_key = api_key.into();
        self
    }

    /// Turn safe mode on. The mock defaults to safe mode off so tests can use
    /// any address.
    pub fn safe_mode(mut self, safe_mode: bool) -> Self {
        self.safe_mode = safe_mode;
        self
    }

    /// The domains allowed in safe mode, each starting with `@`.
    pub fn safe_mode_allowed_domains<I, S>(mut self, domains: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.safe_mode_allowed_domains = domains.into_iter().map(Into::into).collect();
        self
    }

    /// Start the mock server, mount the default routes and configure the
    /// process-wide client to use it.
    pub async fn start(self) -> LoopsMock {
        let guard = GLOBAL.lock().await;
        let restore = snapshot();
        let server = MockServer::start().await;
        let mut overrides = HashMap::new();
        for route in Route::ALL {
            let (status, body) = default_response(*route);
            let mut default = ResponseTemplate::new(status);
            if let Some(body) = body {
                default = default.set_body_json(body);
            }
            let slot = Arc::new(StdMutex::new(None));
            overrides.insert(*route, Arc::clone(&slot));
            Mock::given(method(route.method().as_str()))
                .and(path_regex(route_regex(*route)))
                .respond_with(RouteResponder { default, slot })
                .with_priority(ROUTE_PRIORITY)
                .named(route.name())
                .mount(&server)
                .await;
        }
        Mock::given(wiremock::matchers::any())
            .respond_with(|request: &Request| {
                ResponseTemplate::new(501).set_body_json(json!({
                    "success": false,
                    "message": format!(
                        "loops-so testing: no mock route for {} {}",
                        request.method,
                        request.url.path()
                    ),
                }))
            })
            .with_priority(UNMATCHED_PRIORITY)
            .named("unmatched")
            .mount(&server)
            .await;

        let base_url = format!("{}{API_PREFIX}", server.uri());
        configure(
            ConfigureOptions::new()
                .api_key(&self.api_key)
                .base_url(&base_url)
                .safe_mode(self.safe_mode)
                .safe_mode_allowed_domains(self.safe_mode_allowed_domains.clone()),
        );
        crate::reset_client();
        LoopsMock {
            server,
            base_url,
            settings: self,
            overrides,
            restore: Some(Box::new(restore)),
            _guard: guard,
        }
    }
}

/// A running mock of the Loops API. See the [module docs](self).
pub struct LoopsMock {
    server: MockServer,
    base_url: String,
    settings: LoopsMockBuilder,
    overrides: HashMap<Route, OverrideSlot>,
    restore: Option<Box<dyn FnOnce() + Send>>,
    _guard: MutexGuard<'static, ()>,
}

impl std::fmt::Debug for LoopsMock {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LoopsMock")
            .field("base_url", &self.base_url)
            .finish_non_exhaustive()
    }
}

impl LoopsMock {
    /// Start a mock with the default settings.
    pub async fn start() -> Self {
        Self::builder().start().await
    }

    /// Configure a mock before starting it.
    pub fn builder() -> LoopsMockBuilder {
        LoopsMockBuilder::default()
    }

    /// The base URL the mock serves the API under.
    pub fn base_url(&self) -> &str {
        &self.base_url
    }

    /// The underlying wiremock server, for custom matchers.
    pub fn server(&self) -> &MockServer {
        &self.server
    }

    /// A new client bound to the mock with the mock's settings, independent
    /// of the process-wide singleton.
    ///
    /// # Panics
    ///
    /// Never in practice: the mock's settings always form a valid client.
    pub fn client(&self) -> LoopsClient {
        LoopsClient::builder()
            .api_key(&self.settings.api_key)
            .base_url(&self.base_url)
            .safe_mode(self.settings.safe_mode)
            .safe_mode_allowed_domains(self.settings.safe_mode_allowed_domains.clone())
            .build()
            .expect("the mock's settings form a valid client")
    }

    /// Every request received on `route`, oldest first.
    pub async fn calls(&self, route: Route) -> Vec<Request> {
        self.server
            .received_requests()
            .await
            .unwrap_or_default()
            .into_iter()
            .filter(|request| {
                request.method.as_str() == route.method().as_str()
                    && matches_route(route, request.url.path())
            })
            .collect()
    }

    /// Whether `route` received at least one request.
    pub async fn called(&self, route: Route) -> bool {
        !self.calls(route).await.is_empty()
    }

    /// Answer `route` with `response` from now on. The latest override of a
    /// route wins.
    pub fn respond(&self, route: Route, response: ResponseTemplate) {
        *self.overrides[&route]
            .lock()
            .unwrap_or_else(PoisonError::into_inner) = Some(response);
    }

    /// Go back to the default response for `route`.
    pub fn reset_route(&self, route: Route) {
        self.overrides[&route]
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .take();
    }

    /// Answer `route` with a JSON body and status from now on.
    pub fn respond_json(&self, route: Route, status: u16, body: Value) {
        self.respond(route, ResponseTemplate::new(status).set_body_json(body));
    }
}

impl Drop for LoopsMock {
    fn drop(&mut self) {
        crate::reset_client();
        if let Some(restore) = self.restore.take() {
            restore();
        }
    }
}

type OverrideSlot = Arc<StdMutex<Option<ResponseTemplate>>>;

/// Answers one route: the latest override when there is one, else the
/// default response.
struct RouteResponder {
    default: ResponseTemplate,
    slot: OverrideSlot,
}

impl Respond for RouteResponder {
    fn respond(&self, _request: &Request) -> ResponseTemplate {
        self.slot
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clone()
            .unwrap_or_else(|| self.default.clone())
    }
}

/// Whether [`get_client`](crate::get_client) currently holds a client.
pub fn client_is_cached() -> bool {
    crate::client::has_client()
}

/// An anchored regex matching the route's path under [`API_PREFIX`], with
/// each `{placeholder}` matching one path segment.
fn route_regex(route: Route) -> String {
    let mut pattern = format!("^{API_PREFIX}");
    for segment in route.path().split('/').filter(|s| !s.is_empty()) {
        pattern.push('/');
        if segment.starts_with('{') {
            pattern.push_str("[^/]+");
        } else {
            pattern.push_str(&escape(segment));
        }
    }
    pattern.push('$');
    pattern
}

fn escape(segment: &str) -> String {
    segment
        .chars()
        .flat_map(|c| {
            let escaped = matches!(
                c,
                '.' | '+' | '*' | '?' | '(' | ')' | '[' | ']' | '{' | '}' | '|' | '^' | '$' | '\\'
            );
            escaped
                .then_some('\\')
                .into_iter()
                .chain(std::iter::once(c))
        })
        .collect()
}

/// Whether `path` is a request path for `route`, the same test the mounted
/// [`route_regex`] performs.
fn matches_route(route: Route, path: &str) -> bool {
    let Some(rest) = path.strip_prefix(API_PREFIX) else {
        return false;
    };
    let expected = route.path().split('/').filter(|s| !s.is_empty());
    let actual = rest.split('/').filter(|s| !s.is_empty());
    expected.clone().count() == actual.clone().count()
        && expected
            .zip(actual)
            .all(|(segment, part)| segment.starts_with('{') || segment == part)
}

const TIMESTAMP: &str = "2024-01-01T00:00:00.000Z";

fn pagination() -> Value {
    json!({
        "totalResults": 0,
        "returnedResults": 0,
        "perPage": 20,
        "totalPages": 0,
        "nextCursor": null,
        "nextPage": null,
    })
}

fn empty_page() -> Value {
    json!({ "pagination": pagination(), "data": [] })
}

fn campaign(name: &str, updated_at: &str, email_message_id: Option<&str>) -> Value {
    json!({
        "id": "mock-campaign-id",
        "url": "https://app.loops.so/campaigns/mock-campaign-id",
        "name": name,
        "status": "Draft",
        "createdAt": TIMESTAMP,
        "updatedAt": updated_at,
        "emailMessageId": email_message_id,
        "campaignGroupId": null,
        "mailingListId": null,
        "audienceSegmentId": null,
        "audienceFilter": null,
        "scheduling": { "method": "now", "timestamp": null },
    })
}

fn component(affected: Option<u64>) -> Value {
    let mut value = json!({
        "id": "mock-component-id",
        "name": "Mock Component",
        "lmx": "<Text>Hello</Text>",
    });
    if let Some(count) = affected {
        value["affectedEmailCount"] = json!(count);
    }
    value
}

fn theme(updated_at: &str, affected: Option<u64>) -> Value {
    let mut value = json!({
        "id": "mock-theme-id",
        "name": "Mock Theme",
        "styles": {},
        "isDefault": false,
        "createdAt": TIMESTAMP,
        "updatedAt": updated_at,
    });
    if let Some(count) = affected {
        value["affectedEmailCount"] = json!(count);
    }
    value
}

fn email_message() -> Value {
    json!({
        "id": "mock-email-message-id",
        "campaignId": null,
        "subject": "Mock Subject",
        "previewText": "",
        "fromName": "Test",
        "fromEmail": "test",
        "replyToEmail": "",
        "emailFormat": "styled",
        "lmx": "<Text>Hello</Text>",
        "contentRevisionId": "rev-1",
        "updatedAt": TIMESTAMP,
    })
}

fn transactional_resource() -> Value {
    json!({
        "id": "mock-transactional-id",
        "url": "https://app.loops.so/transactional/mock-transactional-id",
        "name": "Mock Transactional",
        "draftEmailMessageId": null,
        "publishedEmailMessageId": null,
        "transactionalGroupId": null,
        "createdAt": TIMESTAMP,
        "updatedAt": TIMESTAMP,
        "dataVariables": [],
    })
}

fn transactional_draft() -> Value {
    json!({
        "id": "mock-transactional-id",
        "url": "https://app.loops.so/transactional/mock-transactional-id",
        "name": "Mock Transactional",
        "draftEmailMessageId": null,
        "draftEmailMessageContentRevisionId": null,
        "publishedEmailMessageId": null,
        "transactionalGroupId": null,
        "createdAt": TIMESTAMP,
        "updatedAt": TIMESTAMP,
        "dataVariables": [],
    })
}

fn workflow() -> Value {
    json!({
        "id": "mock-workflow-id",
        "url": "https://app.loops.so/workflows/mock-workflow-id",
        "workflowRevisionId": "mock-revision-id",
        "status": "Draft",
        "name": "Mock Workflow",
        "description": null,
        "mailingListId": null,
        "rootNodeId": "mock-node-id",
        "nodes": {},
    })
}

fn mutation_node() -> Value {
    json!({
        "id": "mock-node-id",
        "typeName": "SignupTrigger",
        "nextNodeIds": [],
        "workflowRevisionId": "mock-revision-id",
    })
}

fn node_with_workflow() -> Value {
    let mut node = mutation_node();
    node["workflow"] = workflow();
    node
}

fn deleted_nodes() -> Value {
    json!({
        "status": "deleted",
        "nodeIds": ["mock-node-id"],
        "workflowRevisionId": "mock-revision-id",
        "queuedContactCount": 0,
        "queuedContactLimitReached": false,
        "workflow": workflow(),
    })
}

fn audience_segment(description: Option<&str>) -> Value {
    json!({
        "id": "mock-segment-id",
        "name": "Mock Segment",
        "description": description,
        "createdAt": TIMESTAMP,
        "updatedAt": TIMESTAMP,
        "filter": null,
    })
}

fn group() -> Value {
    json!({
        "id": "mock-group-id",
        "name": "Mock Group",
        "description": "A mock group",
        "createdAt": TIMESTAMP,
        "updatedAt": TIMESTAMP,
    })
}

fn event_pattern() -> Value {
    json!({
        "id": "mock-event-pattern-id",
        "eventName": "signup",
        "eventProperties": [],
        "incomingWebhookPlatform": null,
    })
}

fn email_metrics() -> Value {
    json!({
        "sends": 0,
        "opens": 0,
        "clicks": 0,
        "unsubscribes": 0,
        "spamReports": 0,
        "hardBounces": 0,
        "softBounces": 0,
    })
}

/// The status and JSON body each route answers with by default.
#[allow(clippy::too_many_lines)]
fn default_response(route: Route) -> (u16, Option<Value>) {
    let ok = |body: Value| (200, Some(body));
    let created = |body: Value| (201, Some(body));
    match route {
        Route::Health => ok(json!({ "success": true, "teamName": "Test" })),
        Route::Transactional | Route::SendEvent => ok(json!({ "success": true })),
        Route::ListTransactional => ok(json!({
            "pagination": { "totalResults": 0, "returnedResults": 0, "perPage": 20, "totalPages": 0 },
            "data": [],
        })),
        Route::CreateContact | Route::UpsertContact => {
            ok(json!({ "success": true, "id": "mock-contact-id" }))
        }
        Route::FindContact => ok(json!([{
            "id": "mock-contact-id",
            "email": "user@test.com",
            "firstName": "Test",
            "lastName": "User",
            "source": "API",
            "subscribed": true,
            "userGroup": "",
            "userId": null,
            "mailingLists": {},
            "optInStatus": null,
        }])),
        Route::DeleteContact => ok(json!({ "success": true, "message": "Contact deleted." })),
        Route::ListContactProperties | Route::ListMailingLists | Route::ListSendingIps => {
            ok(json!([]))
        }
        Route::CreateContactProperty => ok(json!({
            "success": true,
            "key": "custom_prop",
            "label": "Custom Prop",
            "type": "string",
        })),
        Route::GetContactSuppression => ok(json!({
            "contact": { "id": "mock-contact-id", "email": "user@test.com", "userId": null },
            "isSuppressed": false,
            "removalQuota": { "limit": 3, "used": 0, "remaining": 3 },
        })),
        Route::RemoveContactSuppression => ok(json!({
            "success": true,
            "message": "Contact removed from suppression list.",
            "removalQuota": { "limit": 3, "used": 1, "remaining": 2 },
        })),
        Route::ListCampaigns
        | Route::ListComponents
        | Route::ListThemes
        | Route::ListTransactionalTemplates
        | Route::ListWorkflows
        | Route::ListAudienceSegments
        | Route::ListCampaignGroups
        | Route::ListTransactionalGroups
        | Route::ListEventPatterns => ok(empty_page()),
        Route::GetCampaign => ok(campaign("Mock Campaign", TIMESTAMP, None)),
        Route::CreateCampaign => {
            let mut body = campaign("Mock Campaign", TIMESTAMP, Some("mock-email-message-id"));
            body["emailMessageContentRevisionId"] = Value::Null;
            created(body)
        }
        Route::UpdateCampaign => ok(campaign(
            "Updated Campaign",
            "2024-01-02T00:00:00.000Z",
            None,
        )),
        Route::GetCampaignMetrics | Route::GetWorkflowNodeMetrics => ok(email_metrics()),
        Route::GetComponent => ok(component(None)),
        Route::CreateComponent => created(component(None)),
        Route::UpdateComponent => ok(component(Some(0))),
        Route::GetTheme => ok(theme(TIMESTAMP, None)),
        Route::CreateTheme => created(theme(TIMESTAMP, None)),
        Route::UpdateTheme => ok(theme("2024-01-02T00:00:00.000Z", Some(0))),
        Route::GetEmailMessage | Route::UpdateEmailMessage => ok(email_message()),
        Route::GetEmailMessageGuardian => ok(json!({ "errors": [], "warnings": [] })),
        Route::PreviewEmailMessage => ok(json!({ "id": "mock-preview-id" })),
        Route::GetTransactionalTemplate
        | Route::UpdateTransactionalTemplate
        | Route::PublishTransactionalTemplate => ok(transactional_resource()),
        Route::CreateTransactionalTemplate => created(transactional_draft()),
        Route::DraftTransactionalTemplate => ok(transactional_draft()),
        Route::GetTransactionalTemplateMetrics => ok(json!({
            "sends": 0,
            "deliveries": 0,
            "spamReports": 0,
            "hardBounces": 0,
            "softBounces": 0,
        })),
        Route::CreateUpload => ok(json!({
            "emailAssetId": "mock-asset-id",
            "presignedUrl": "https://example.com/upload",
        })),
        Route::CompleteUpload => ok(json!({
            "emailAssetId": "mock-asset-id",
            "finalUrl": "https://example.com/final.png",
        })),
        Route::GetWorkflow | Route::CreateWorkflow | Route::UpdateWorkflow => ok(workflow()),
        Route::GetWorkflowNode => ok(json!({
            "id": "mock-node-id",
            "workflowId": "mock-workflow-id",
            "typeName": "ExitAction",
            "nextNodeIds": [],
            "workflowRevisionId": "mock-revision-id",
        })),
        Route::ChangeWorkflowMailingList => ok(json!({
            "status": "updated",
            "mailingListId": "mock-list-id",
            "workflowRevisionId": "mock-revision-id",
            "queuedContactCount": 0,
            "queuedContactLimitReached": false,
            "workflow": workflow(),
        })),
        Route::DeleteWorkflow => (204, None),
        Route::CreateWorkflowNode | Route::AddWorkflowBranch => {
            ok(json!({ "node": mutation_node(), "workflow": workflow() }))
        }
        Route::UpdateWorkflowNode | Route::RerouteNodeConnection => ok(node_with_workflow()),
        Route::DeleteWorkflowNode | Route::DeleteWorkflowNodeRecursively => ok(deleted_nodes()),
        Route::GetAudienceSegment => ok(audience_segment(Some("A mock audience segment"))),
        Route::CreateAudienceSegment => ok(audience_segment(None)),
        Route::GetCampaignGroup
        | Route::CreateCampaignGroup
        | Route::UpdateCampaignGroup
        | Route::GetTransactionalGroup
        | Route::CreateTransactionalGroup
        | Route::UpdateTransactionalGroup => ok(group()),
        Route::GetEventPattern | Route::GetEventPatternByName => ok(event_pattern()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::get_config;

    // The only test in the library's test binary that touches the
    // process-wide configuration, so reading it outside the mock is safe.
    #[tokio::test]
    async fn drop_resets_singleton_and_restores_config() {
        configure(
            ConfigureOptions::new()
                .api_key("before-key")
                .base_url("https://before.example/api"),
        );
        {
            let mock = LoopsMock::start().await;
            assert_eq!(get_config().base_url, mock.base_url());
            configure(ConfigureOptions::new().api_key("changed-inside"));
            crate::get_client().unwrap();
            assert!(client_is_cached());
        }
        assert!(!client_is_cached());
        let config = get_config();
        assert_eq!(config.api_key.as_deref(), Some("before-key"));
        assert_eq!(config.base_url, "https://before.example/api");
    }

    #[test]
    fn route_patterns_match_their_own_paths_only() {
        for route in Route::ALL {
            let sample = format!(
                "{API_PREFIX}{}",
                route
                    .path()
                    .replace("{id}", "x1")
                    .replace("{node_id}", "n1")
                    .replace("{name}", "e1")
            );
            assert!(
                matches_route(*route, &sample),
                "{route:?} does not match {sample}"
            );
            for other in Route::ALL
                .iter()
                .filter(|o| **o != *route && o.method() == route.method())
            {
                assert!(
                    !matches_route(*other, &sample),
                    "{other:?} also matches {sample}, which belongs to {route:?}"
                );
            }
        }
    }
}