assay-lua 0.20.2

General-purpose enhanced Lua runtime. Batteries-included scripting, automation, and web services.
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
//! assay.salesforge against the shapes probed live on 2026-09-04, anonymised.
//! What is pinned: the public key riding bare in `Authorization` with no Bearer
//! prefix, an empty list arriving as a JSON object rather than an array, paging
//! by limit and offset, the public API carrying no warm-up state while the
//! internal one does, a failed sign-in reading as a typed sign_in error that
//! leaves the public API working, and 401, 402 and 429 reading as themselves.

#[path = "../common/mod.rs"]
mod common;

use common::run_lua;
use serde_json::json;
use wiremock::matchers::{body_string_contains, header, method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

const WS: &str = "wks_xa1";

/// One server stands in for all three endpoints; the paths do not collide.
fn client(uri: &str, extra: &str) -> String {
    format!(
        "local sf = require(\"assay.salesforge\")\n\
         local c = sf.client({{ api_key = \"k\", workspace_id = \"{WS}\",\n\
           base_url = \"{uri}/public/v2\", internal_base_url = \"{uri}\",\n\
           identity_url = \"{uri}/identity\"{extra} }})\n"
    )
}

fn page(rows: serde_json::Value, total: u64) -> ResponseTemplate {
    ResponseTemplate::new(200)
        .set_body_json(json!({ "data": rows, "total": total, "limit": 100, "offset": 0 }))
}

fn public_row() -> serde_json::Value {
    json!({
        "id": "mbx_xa1", "address": "Ada@Example.TEST", "firstName": "Ada", "lastName": "Person",
        "status": "ACTIVE", "dailyEmailLimit": 30, "mailboxProvider": "GOOGLE",
        "trackingDomainStatus": "NOT_SET",
    })
}

fn internal_row(activated: bool) -> serde_json::Value {
    json!({
        "id": "mbx_xa1", "address": "ada@example.test", "status": "active",
        "warmupActivated": activated, "daysUntilWarm": 5, "dailyEmailLimit": 30,
        "mailboxProvider": "GOOGLE", "sentEmailsToday": 4, "workspaceId": WS,
    })
}

async fn mount_public(server: &MockServer, route: &str, rows: serde_json::Value, total: u64) {
    Mock::given(method("GET"))
        // The key is an apiKey scheme, not a bearer one. A client that prefixed
        // it with "Bearer " would never match this mock.
        .and(header("authorization", "k"))
        .and(path(format!("/public/v2{route}")))
        .respond_with(page(rows, total))
        .mount(server)
        .await;
}

async fn mount_sign_in(server: &MockServer, status: u16, body: serde_json::Value) {
    Mock::given(method("POST"))
        .and(path("/identity"))
        .and(query_param(
            "key",
            "AIzaSyCSvPu4xQeXnowWbgt2uRFGwAuMhkbJo-o",
        ))
        .respond_with(ResponseTemplate::new(status).set_body_json(body))
        .mount(server)
        .await;
}

/// The public API lists what the workspace is connected to and carries no
/// warm-up state at all — no `warmupActivated`, no days, no score.
#[tokio::test]
async fn test_the_public_key_rides_bare_in_authorization_with_no_bearer_prefix() {
    let server = MockServer::start().await;
    mount_public(
        &server,
        &format!("/workspaces/{WS}/mailboxes"),
        json!([public_row()]),
        1,
    )
    .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local rows = c:mailboxes()
        assert.eq(#rows, 1)
        assert.eq(rows[1].address, "ada@example.test")
        assert.eq(rows[1].domain, "example.test")
        assert.eq(rows[1].status, "active")
        assert.eq(rows[1].provider_ref, "mbx_xa1")
        assert.eq(rows[1].daily_limit, 30)
        assert.eq(rows[1].connected, true)
        assert.eq(rows[1].warmup, nil)
        "#
    ))
    .await
    .unwrap();
}

/// A workspace with no sequences answers `{"data": {}, "total": 0}` — an object
/// where a list belongs. Read as an error it would look like an outage.
#[tokio::test]
async fn test_an_empty_list_arriving_as_an_object_is_an_empty_list() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path(format!("/public/v2/workspaces/{WS}/sequences")))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": {}, "total": 0, "limit": 100, "offset": 0,
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local rows, meta = c:sequences()
        assert.eq(#rows, 0)
        -- An empty answer is a real one, not a walk that got cut short.
        assert.eq(meta.truncated, false)
        assert.eq(meta.seen, 0)
        "#
    ))
    .await
    .unwrap();
}

/// The vendor pages by limit and offset. A caller that read one page would
/// report a workspace smaller than it is.
#[tokio::test]
async fn test_paging_walks_by_offset_and_stops_on_the_total() {
    let server = MockServer::start().await;
    let full: Vec<serde_json::Value> = (0..100)
        .map(|n| json!({ "id": format!("mbx_x{n}"), "address": format!("p{n}@example.test") }))
        .collect();
    Mock::given(method("GET"))
        .and(path(format!("/public/v2/workspaces/{WS}/mailboxes")))
        .and(query_param("offset", "0"))
        .respond_with(page(json!(full), 101))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path(format!("/public/v2/workspaces/{WS}/mailboxes")))
        .and(query_param("offset", "100"))
        .respond_with(page(
            json!([{ "id": "mbx_xlast", "address": "last@example.test" }]),
            101,
        ))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local rows = c:mailboxes()
        assert.eq(#rows, 101)
        assert.eq(rows[101].address, "last@example.test")
        "#
    ))
    .await
    .unwrap();
}

/// The warm-up state lives only on the web app's own API, reached with a
/// Firebase id token as a bearer. The public key does not open it.
#[tokio::test]
async fn test_the_internal_api_carries_the_warmup_state_behind_a_bearer_id_token() {
    let server = MockServer::start().await;
    mount_sign_in(
        &server,
        200,
        json!({ "idToken": "tok_x1", "expiresIn": "3600" }),
    )
    .await;
    Mock::given(method("GET"))
        .and(path(format!("/workspaces/{WS}/mailboxes")))
        .and(header("authorization", "Bearer tok_x1"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": [internal_row(true), internal_row(false)],
            "pagination": { "currentPage": 1, "pageSize": 50, "totalItems": 2,
                            "totalPages": 1, "next": "", "previous": "" },
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(
            &server.uri(),
            ", email = \"ops@example.test\", password = \"pw\""
        ),
        r#"
        local rows = c:mailboxes_internal()
        assert.eq(#rows, 2)
        assert.eq(rows[1].warmup.activated, true)
        assert.eq(rows[1].warmup.days_until_warm, 5)
        -- No reputationScore on the live row: an absent heat is a reading
        -- nobody has yet, never a heat of zero.
        assert.eq(rows[1].warmup.heat, nil)
        -- The switch is off, so there is no curve to be part-way along.
        assert.eq(rows[2].warmup, nil)
        "#
    ))
    .await
    .unwrap();
}

/// Sign-in happens once for the life of the client, however many internal calls
/// follow it.
#[tokio::test]
async fn test_sign_in_is_memoised_and_the_token_is_never_returned() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    Mock::given(method("GET"))
        .and(path(format!("/workspaces/{WS}/mailboxes")))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": [internal_row(true)],
            "pagination": { "totalPages": 1, "next": "" },
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(
            &server.uri(),
            ", email = \"ops@example.test\", password = \"pw\""
        ),
        r#"
        assert.eq(c:sign_in(), true)
        assert.eq(c:sign_in(), true)
        c:mailboxes_internal()
        c:mailboxes_internal()
        "#
    ))
    .await
    .unwrap();
    let signins = server
        .received_requests()
        .await
        .unwrap()
        .iter()
        .filter(|r| r.url.path() == "/identity")
        .count();
    assert_eq!(signins, 1, "signed in more than once");
}

/// The two surfaces authenticate differently, so an account that cannot sign in
/// must not take the public API down with it.
#[tokio::test]
async fn test_a_failed_sign_in_is_typed_and_leaves_the_public_api_working() {
    let server = MockServer::start().await;
    mount_sign_in(
        &server,
        400,
        json!({ "error": { "code": 400, "message": "INVALID_LOGIN_CREDENTIALS" } }),
    )
    .await;
    mount_public(
        &server,
        &format!("/workspaces/{WS}/mailboxes"),
        json!([public_row()]),
        1,
    )
    .await;
    run_lua(&format!(
        "{}{}",
        client(
            &server.uri(),
            ", email = \"ops@example.test\", password = \"wrong\""
        ),
        r#"
        local rows, err = c:mailboxes_internal()
        assert.eq(rows, nil)
        assert.eq(err.code, "sign_in")
        assert.eq(err.status, 400)
        assert.contains(tostring(err), "salesforge: ")
        -- The public key is untouched by the account's failure.
        assert.eq(#c:mailboxes(), 1)
        "#
    ))
    .await
    .unwrap();
}

/// A client with no account at all reports the same typed error rather than
/// reaching for a credential nobody supplied.
#[tokio::test]
async fn test_an_absent_account_is_a_sign_in_error_not_a_crash() {
    let server = MockServer::start().await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ", email = \"\", password = \"\""),
        r#"
        local rows, err = c:mailboxes_internal()
        assert.eq(rows, nil)
        assert.eq(err.code, "sign_in")
        "#
    ))
    .await
    .unwrap();
}

/// A refused key is not an empty workspace, and the plan gate is not a bad
/// request: the public API is Growth-only and answers 402 when it is not.
#[tokio::test]
async fn test_auth_plan_and_rate_limits_read_as_themselves() {
    // Every verb the client uses, not just the reads.
    //
    // A refused write is the case worth driving: if the HTTP builtin threw on a
    // 4xx PUT instead of answering with one, the client's `pcall` would catch
    // it and report `code = "transport"` with no status at all. Asserting the
    // status-derived code and the status itself is what tells those two apart,
    // so a vendor saying "no" stays legible as the "no" it is.
    let calls: [(&str, String, &str); 4] = [
        (
            "GET",
            format!("/public/v2/workspaces/{WS}/mailboxes"),
            "c:mailboxes()",
        ),
        (
            "PUT",
            format!("/public/v2/workspaces/{WS}/sequences/seq_xa1/mailboxes"),
            r#"c:set_rotation("seq_xa1", { "mbx_xa1" })"#,
        ),
        (
            "PUT",
            format!("/public/v2/workspaces/{WS}/sequences/seq_xa1/status"),
            r#"c:set_sequence_status("seq_xa1", "paused")"#,
        ),
        (
            "PUT",
            format!("/public/v2/workspaces/{WS}/sequences/seq_xa1/contacts"),
            r#"c:enrol("seq_xa1", { "con_xa1" })"#,
        ),
    ];
    for (status, code) in [
        (401u16, "auth"),
        (403, "auth"),
        (402, "plan"),
        (429, "rate_limit"),
        (500, "server"),
    ] {
        for (verb, route, call) in &calls {
            let server = MockServer::start().await;
            Mock::given(method(*verb))
                .and(path(route.clone()))
                .respond_with(ResponseTemplate::new(status))
                .mount(&server)
                .await;
            let check = format!(
                r#"
                local out, err = {call}
                assert.eq(out, nil)
                assert.eq(err.code, "{code}")
                assert.eq(err.status, {status})
                assert.contains(tostring(err), "salesforge: ")
                "#
            );
            run_lua(&format!("{}{check}", client(&server.uri(), "")))
                .await
                .unwrap_or_else(|e| panic!("{verb} {route} on {status}: {e}"));
        }
    }
}

/// Enrolling, do-not-contact and replying answer 204 with no body at all. A
/// blanket parse would turn every success into a read error.
#[tokio::test]
async fn test_the_write_calls_send_the_vendors_own_field_names_and_accept_a_204() {
    let server = MockServer::start().await;
    Mock::given(method("PUT"))
        .and(path(format!(
            "/public/v2/workspaces/{WS}/sequences/seq_xa1/contacts"
        )))
        .and(body_string_contains("contactIds"))
        .respond_with(ResponseTemplate::new(204))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/public/v2/workspaces/{WS}/dnc/bulk")))
        .and(body_string_contains("dncs"))
        .respond_with(ResponseTemplate::new(201))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!(
            "/public/v2/workspaces/{WS}/mailboxes/mbx_xa1/emails/em_xa9/reply"
        )))
        .and(body_string_contains("includeHistory"))
        .respond_with(ResponseTemplate::new(204))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/public/v2/workspaces/{WS}/contacts")))
        .respond_with(ResponseTemplate::new(201).set_body_json(json!({
            "id": "con_xa1", "email": "ada@example.test",
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        assert.eq(c:enrol("seq_xa1", { "con_xa1" }), true)
        assert.eq(c:dnc({ "ada@example.test" }), true)
        assert.eq(c:reply("mbx_xa1", "em_xa9", "thanks"), true)
        assert.eq(c:create_contact({ firstName = "Ada", email = "ada@example.test" }).id, "con_xa1")
        "#
    ))
    .await
    .unwrap();
}

/// An empty list would encode as a JSON object rather than an empty array and
/// reach the vendor as a malformed body, so it never leaves this process.
#[tokio::test]
async fn test_an_empty_write_is_refused_here_rather_than_sent_malformed() {
    let server = MockServer::start().await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local ok, err = c:enrol("seq_xa1", {})
        assert.eq(ok, nil)
        assert.eq(err.code, "config")
        local ok2, err2 = c:dnc({})
        assert.eq(ok2, nil)
        assert.eq(err2.code, "config")
        local ok3, err3 = c:create_contact({ email = "ada@example.test" })
        assert.eq(ok3, nil)
        assert.eq(err3.code, "config")
        "#
    ))
    .await
    .unwrap();
    assert!(
        server.received_requests().await.unwrap().is_empty(),
        "a malformed write reached the vendor"
    );
}

/// A client with no key or no workspace is a programming error, not a vendor
/// answer.
#[tokio::test]
async fn test_a_client_refuses_to_build_without_a_key_or_a_workspace() {
    for args in [r#"{ workspace_id = "wks_xa1" }"#, r#"{ api_key = "k" }"#] {
        let err = run_lua(&format!(
            "local sf = require(\"assay.salesforge\")\nsf.client({args})"
        ))
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("required"), "gave {err}");
    }
}

/// A walk the vendor ended by running out of rows has the whole workspace.
#[tokio::test]
async fn test_a_walk_the_vendor_ended_is_not_truncated() {
    let server = MockServer::start().await;
    mount_public(
        &server,
        &format!("/workspaces/{WS}/mailboxes"),
        json!([public_row()]),
        1,
    )
    .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local rows, meta = c:mailboxes()
        assert.eq(#rows, 1)
        assert.eq(meta.truncated, false)
        assert.eq(meta.cap, 2000)
        assert.eq(meta.seen, 1)
        "#
    ))
    .await
    .unwrap();
}

/// A vendor answering full pages against a total it never reaches walks into
/// the page cap. Read without `meta` that is a workspace two thousand mailboxes
/// wide reported as if it were the whole thing.
#[tokio::test]
async fn test_a_public_walk_stopped_by_the_page_cap_says_it_is_truncated() {
    let server = MockServer::start().await;
    let full: Vec<serde_json::Value> = (0..100)
        .map(|n| json!({ "id": format!("mbx_x{n}"), "address": format!("p{n}@example.test") }))
        .collect();
    Mock::given(method("GET"))
        .and(path(format!("/public/v2/workspaces/{WS}/mailboxes")))
        .respond_with(page(json!(full), 999_999))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local rows, meta = c:mailboxes()
        assert.eq(meta.truncated, true)
        assert.eq(meta.cap, 2000)
        assert.eq(meta.seen, 2000)
        assert.eq(#rows, 2000)
        "#
    ))
    .await
    .unwrap();
}

/// The internal API pages by a `next` link, so a vendor that always offers one
/// walks into the same cap. Its warm-up list can be short for the same reason.
#[tokio::test]
async fn test_an_internal_walk_stopped_by_the_page_cap_says_it_is_truncated() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    let full: Vec<serde_json::Value> = (0..50)
        .map(|n| {
            json!({ "id": format!("mbx_x{n}"), "address": format!("p{n}@example.test"),
                    "warmupActivated": true, "daysUntilWarm": 5 })
        })
        .collect();
    Mock::given(method("GET"))
        .and(path(format!("/workspaces/{WS}/mailboxes")))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": full,
            "pagination": { "currentPage": 1, "pageSize": 50, "next": "?page=2&size=50" },
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(
            &server.uri(),
            ", email = \"ops@example.test\", password = \"pw\""
        ),
        r#"
        local rows, meta = c:mailboxes_internal()
        assert.eq(meta.truncated, true)
        assert.eq(meta.cap, 1000)
        assert.eq(meta.seen, 1000)
        "#
    ))
    .await
    .unwrap();
}

/// Every environment variable a module reads has to reach `assay context`, and
/// a quickref only gets there if it parses.
///
/// `parse_quickref` wants `signature -> return_hint | description` and silently
/// drops anything else, so a malformed line is not a formatting nit: the method
/// or the variable it documents simply never appears. Two lines were lost that
/// way before this test existed.
#[tokio::test]
async fn test_every_quickref_parses_and_the_env_vars_are_among_them() {
    let modules = ["clayinbox", "forge", "salesforge"];
    let mut salesforge_refs = String::new();
    for name in modules {
        let path = format!("{}/stdlib/{name}.lua", env!("CARGO_MANIFEST_DIR"));
        let source = std::fs::read_to_string(&path).unwrap();
        let mut parsed = 0;
        for line in source.lines().take_while(|l| l.starts_with("---")) {
            let Some(value) = line.strip_prefix("--- @quickref ") else {
                continue;
            };
            // The parser's own rule, mirrored: split on " -> ", then on " | ".
            let rest = value
                .split_once(" -> ")
                .unwrap_or_else(|| panic!("{name}: quickref has no ` -> `: {value}"))
                .1;
            rest.split_once(" | ")
                .unwrap_or_else(|| panic!("{name}: quickref has no ` | `: {value}"));
            parsed += 1;
            if name == "salesforge" {
                salesforge_refs.push_str(value);
                salesforge_refs.push('\n');
            }
        }
        assert!(parsed > 0, "{name} declares no quickrefs at all");
    }
    for var in [
        "SALESFORGE_API_KEY",
        "SALESFORGE_EMAIL",
        "SALESFORGE_PASSWORD",
    ] {
        assert!(
            salesforge_refs.contains(var),
            "{var} is read but no parsing quickref names it, so `assay context` never shows it"
        );
    }
}

/// The rotation is replaced wholesale: a caller taking one domain out sends
/// back the ids it means to keep, under the vendor's own `mailboxIds` key.
#[tokio::test]
async fn test_setting_a_rotation_sends_the_vendors_own_key_and_accepts_a_204() {
    let server = MockServer::start().await;
    Mock::given(method("PUT"))
        .and(header("authorization", "k"))
        .and(path(format!(
            "/public/v2/workspaces/{WS}/sequences/seq_xa1/mailboxes"
        )))
        .and(body_string_contains("mailboxIds"))
        .respond_with(ResponseTemplate::new(204))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        assert.eq(c:set_rotation("seq_xa1", { "mbx_xa1", "mbx_xa2" }), true)
        "#
    ))
    .await
    .unwrap();
    let sent = &server.received_requests().await.unwrap()[0];
    let body = String::from_utf8(sent.body.clone()).unwrap();
    assert_eq!(
        body, r#"{"mailboxIds":["mbx_xa1","mbx_xa2"]}"#,
        "rotation body was {body}"
    );
}

/// The vendor takes two statuses. Constraining them here makes a typo a config
/// error the caller can read rather than a 400 it has to interpret.
#[tokio::test]
async fn test_a_sequence_status_is_one_of_two_words_and_a_typo_never_leaves_the_process() {
    let server = MockServer::start().await;
    for state in ["paused", "active"] {
        Mock::given(method("PUT"))
            .and(header("authorization", "k"))
            .and(path(format!(
                "/public/v2/workspaces/{WS}/sequences/seq_xa1/status"
            )))
            .and(body_string_contains(state))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;
    }
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        assert.eq(c:set_sequence_status("seq_xa1", "paused"), true)
        assert.eq(c:set_sequence_status("seq_xa1", "active"), true)
        -- Case is the caller's business, not the vendor's.
        assert.eq(c:set_sequence_status("seq_xa1", "PAUSED"), true)
        local ok, err = c:set_sequence_status("seq_xa1", "pasued")
        assert.eq(ok, nil)
        assert.eq(err.code, "config")
        assert.contains(err.message, "paused")
        local ok2, err2 = c:set_sequence_status("seq_xa1", nil)
        assert.eq(ok2, nil)
        assert.eq(err2.code, "config")
        "#
    ))
    .await
    .unwrap();
    // Three good calls reached the vendor; neither bad one did.
    assert_eq!(server.received_requests().await.unwrap().len(), 3);
}

/// Clearing a rotation is a real instruction, not a malformed one.
///
/// Pulling a paused domain's boxes can legitimately leave a sequence with none,
/// and that is the truthful state — it then cannot send, which is what
/// `set_sequence_status` is for. The caller must be able to say it rather than
/// being forced to leave a stale mailbox in the rotation.
///
/// The body is the whole point: a bare empty Lua table encodes as `{}`, which
/// the vendor reads as a malformed object, so the ids ride a table marked as a
/// JSON array and the wire form is `[]`.
#[tokio::test]
async fn test_an_empty_rotation_clears_it_and_sends_an_array_not_an_object() {
    let server = MockServer::start().await;
    Mock::given(method("PUT"))
        .and(path(format!(
            "/public/v2/workspaces/{WS}/sequences/seq_xa1/mailboxes"
        )))
        .respond_with(ResponseTemplate::new(204))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        assert.eq(c:set_rotation("seq_xa1", {}), true)
        "#
    ))
    .await
    .unwrap();
    let sent = &server.received_requests().await.unwrap()[0];
    let body = String::from_utf8(sent.body.clone()).unwrap();
    assert_eq!(
        body, r#"{"mailboxIds":[]}"#,
        "empty rotation body was {body}"
    );
}

/// The caller's own table must come back unmarked: the array marker rides a
/// copy, so passing the same list to something else afterwards is unaffected.
#[tokio::test]
async fn test_the_array_marker_does_not_touch_the_callers_table() {
    let server = MockServer::start().await;
    Mock::given(method("PUT"))
        .and(path(format!(
            "/public/v2/workspaces/{WS}/sequences/seq_xa1/mailboxes"
        )))
        .respond_with(ResponseTemplate::new(204))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local ids = {}
        assert.eq(c:set_rotation("seq_xa1", ids), true)
        assert.eq(getmetatable(ids), nil)
        "#
    ))
    .await
    .unwrap();
}

/// A blank sequence id would address the workspace itself, and a non-table is
/// not a list at all. Neither leaves this process.
#[tokio::test]
async fn test_a_blank_id_or_a_non_list_is_refused_before_any_request() {
    let server = MockServer::start().await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local ok, err = c:set_rotation("", { "mbx_xa1" })
        assert.eq(ok, nil)
        assert.eq(err.code, "config")
        local ok2, err2 = c:set_rotation("seq_xa1", "mbx_xa1")
        assert.eq(ok2, nil)
        assert.eq(err2.code, "config")
        local ok3, err3 = c:set_rotation("seq_xa1", nil)
        assert.eq(ok3, nil)
        assert.eq(err3.code, "config")
        local ok4, err4 = c:set_sequence_status("", "paused")
        assert.eq(ok4, nil)
        assert.eq(err4.code, "config")
        "#
    ))
    .await
    .unwrap();
    assert!(
        server.received_requests().await.unwrap().is_empty(),
        "a malformed sequence write reached the vendor"
    );
}