assay-lua 0.20.7

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
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
//! 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.
//! Webhooks are the one family with no REST route at any version, so they go
//! over the MCP endpoint and are pinned here as such.

#[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"
    );
}

const CREDS: &str = ", email = \"ops@example.test\", password = \"pw\"";

/// The shape the web app's own `/me` answers: the plan is nested two deep, under
/// the user's account, and the credit pools sit beside it on the account rather
/// than on the plan. A trial states no billing cycle anywhere.
fn me_body() -> serde_json::Value {
    json!({
        "user": {
            "id": "usr_xa1", "firstName": "Ada", "lastName": "Person",
            "email": "ops@example.test", "accountId": "acc_xa1",
            "account": {
                "id": "acc_xa1", "name": "Example Ltd",
                "activePlanId": "plan_trial",
                "activePlan": {
                    "id": "plan_trial", "name": "Trial",
                    "activatedLeadsPerMonthLimit": 50, "emailsPerMonthLimit": 100,
                    "validationsPerMonthLimit": 50, "personalizationsPerMonthLimit": 50,
                    "socialActionsPerMonthLimit": 50, "linkedInProfilesLimit": 1,
                },
                "planStartedAt": "2026-08-31T09:12:48.185965Z",
                "subscriptionStatus": "active",
                "freeTrialExpiresAt": "2026-09-14T09:12:48.185965Z",
                "emailCreditsLeft": 100, "leadCreditsLeft": 50,
                "emailValidationCreditsLeft": 50, "personalizationCreditsLeft": 50,
                "socialActionCreditsLeft": 50,
                "StripeCustomerID": serde_json::Value::Null,
            },
        },
    })
}

async fn mount_me(server: &MockServer, status: u16, body: serde_json::Value) {
    Mock::given(method("GET"))
        .and(path("/me"))
        .and(header("authorization", "Bearer tok_x1"))
        .respond_with(ResponseTemplate::new(status).set_body_json(body))
        .mount(server)
        .await;
}

/// The plan and its ceilings live only on the internal `/me`. Every plan,
/// billing, usage and limits path under the public workspace is a flat 404, and
/// the internal subscription route holds nothing for an account that never
/// bought one.
#[tokio::test]
async fn test_the_plan_and_its_limits_come_off_the_internal_me_endpoint() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    mount_me(&server, 200, me_body()).await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), CREDS),
        r#"
        local out = c:costs()
        assert.eq(#out.items, 1)
        assert.eq(out.items[1].kind, "plan")
        assert.eq(out.items[1].unit, "plan")
        assert.eq(out.items[1].ref, "Trial")
        assert.eq(out.items[1].quantity, 1)
        assert.eq(out.items[1].source, "vendor")
        assert.eq(out.meta.plan.id, "plan_trial")
        assert.eq(out.meta.plan.status, "active")
        assert.eq(out.meta.plan.trial_expires_at, "2026-09-14T09:12:48.185965Z")
        assert.eq(out.meta.limits.emails_per_month, 100)
        assert.eq(out.meta.limits.linkedin_profiles, 1)
        assert.eq(out.meta.credits_left.emails, 100)
        assert.eq(out.meta.credits_left.validations, 50)
        "#
    ))
    .await
    .unwrap();
}

/// The vendor names no money anywhere on the plan it says you are on. An absent
/// price read as free would put the sequencer's cost at nothing, so the item
/// carries no price and `meta.priced` says why.
#[tokio::test]
async fn test_the_vendor_names_no_price_so_costs_says_so_rather_than_reading_as_free() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    mount_me(&server, 200, me_body()).await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), CREDS),
        r#"
        local out = c:costs()
        assert.eq(out.items[1].unit_price_cents, nil)
        assert.eq(out.items[1].currency, nil)
        assert.eq(out.meta.priced, false)
        assert.eq(out.meta.currency_known, false)
        "#
    ))
    .await
    .unwrap();
}

/// A trial states no billing cycle in any field. A period of "month" here would
/// be this module's guess wearing the vendor's name, so there is none — the
/// same way the absent price is absent rather than zero.
#[tokio::test]
async fn test_a_plan_whose_cycle_the_vendor_never_states_carries_no_period() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    mount_me(&server, 200, me_body()).await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), CREDS),
        r#"
        local out = c:costs()
        assert.eq(out.items[1].period, nil)
        -- The ceilings stay monthly whatever the plan bills on; that is the
        -- vendor's own field naming, not a cycle.
        assert.eq(out.meta.limits.emails_per_month, 100)
        "#
    ))
    .await
    .unwrap();
}

/// An annual plan reads as a year. The vendor writes the cycle on the plan on
/// some accounts and on the account on others, so all three fields are read.
#[tokio::test]
async fn test_an_annual_plan_reads_as_a_year_wherever_the_vendor_states_it() {
    let cases = [
        json!({ "activePlanId": "plan_growth",
                "activePlan": { "name": "Growth", "interval": "year" } }),
        json!({ "activePlanId": "plan_growth",
                "activePlan": { "name": "Growth", "billingPeriod": "YEARLY" } }),
        json!({ "activePlanId": "plan_growth", "billingCycle": "ANNUAL",
                "activePlan": { "name": "Growth" } }),
    ];
    for account in cases {
        let server = MockServer::start().await;
        mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
        mount_me(&server, 200, json!({ "user": { "account": account } })).await;
        run_lua(&format!(
            "{}{}",
            client(&server.uri(), CREDS),
            r#"
            local out = c:costs()
            assert.eq(out.items[1].period, "year")
            assert.eq(out.items[1].ref, "Growth")
            "#
        ))
        .await
        .unwrap();
    }
}

/// A monthly plan reads as a month when the vendor is the one saying so.
#[tokio::test]
async fn test_a_monthly_plan_reads_as_a_month_when_the_vendor_states_it() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    mount_me(
        &server,
        200,
        json!({ "user": { "account": {
            "activePlanId": "plan_growth", "billingCycle": "MONTHLY",
            "activePlan": { "name": "Growth" },
        } } }),
    )
    .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), CREDS),
        r#"
        local out = c:costs()
        assert.eq(out.items[1].period, "month")
        "#
    ))
    .await
    .unwrap();
}

/// A `/me` the vendor refuses reads as a refusal. Answering a plan of "unknown"
/// with empty limits would look like an account entitled to nothing.
#[tokio::test]
async fn test_a_refused_me_reads_as_an_error_not_as_an_account_entitled_to_nothing() {
    for (status, code) in [
        (401u16, "auth"),
        (402, "plan"),
        (429, "rate_limit"),
        (500, "server"),
    ] {
        let server = MockServer::start().await;
        mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
        mount_me(&server, status, json!({ "message": "no" })).await;
        let body = format!(
            r#"
            local out, err = c:costs()
            assert.eq(out, nil)
            assert.eq(err.code, "{code}")
            assert.eq(err.status, {status})
            "#
        );
        run_lua(&format!("{}{}", client(&server.uri(), CREDS), body))
            .await
            .unwrap();
    }
}

/// A 200 carrying nothing, a bare JSON scalar and a JSON `null` all arrive as
/// something that is not an account. Indexed they crash the caller; read as an
/// empty account they report a workspace entitled to nothing, which is a plan
/// downgrade that never happened.
#[tokio::test]
async fn test_a_me_that_is_not_an_account_object_is_a_typed_read_error() {
    let bodies = ["", "true", "null", "\"nope\"", "[]"];
    for raw in bodies {
        let server = MockServer::start().await;
        mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
        Mock::given(method("GET"))
            .and(path("/me"))
            .respond_with(ResponseTemplate::new(200).set_body_string(raw))
            .mount(&server)
            .await;
        run_lua(&format!(
            "{}{}",
            client(&server.uri(), CREDS),
            r#"
            local out, err = c:costs()
            assert.eq(out, nil)
            assert.eq(type(err), "table")
            assert.contains(tostring(err), "salesforge: ")
            "#
        ))
        .await
        .unwrap();
    }
}

/// Costing needs the internal API, so a sign-in that fails stops it with the
/// sign-in error rather than a half-read plan.
#[tokio::test]
async fn test_costs_without_a_sign_in_is_a_typed_sign_in_error() {
    let server = MockServer::start().await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local out, err = c:costs()
        assert.eq(out, nil)
        assert.eq(err.code, "sign_in")
        "#
    ))
    .await
    .unwrap();
}

/// An account the vendor answers without a plan object is an account whose plan
/// this module could not read. It names the plan id it does have rather than
/// inventing limits.
#[tokio::test]
async fn test_an_account_with_no_plan_object_falls_back_to_the_plan_id() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    mount_me(
        &server,
        200,
        json!({ "user": { "account": { "activePlanId": "plan_growth" } } }),
    )
    .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), CREDS),
        r#"
        local out = c:costs()
        assert.eq(out.items[1].ref, "plan_growth")
        assert.eq(out.items[1].period, nil)
        assert.eq(out.meta.limits.emails_per_month, nil)
        assert.eq(out.meta.credits_left.emails, nil)
        "#
    ))
    .await
    .unwrap();
}

// ─── connecting a mailbox, and switching its warm-up on ────────────────────

const INTERNAL_AUTH: &str = "Bearer tok_x1";

async fn mount_internal_listing(server: &MockServer, rows: serde_json::Value) {
    Mock::given(method("GET"))
        .and(path(format!("/workspaces/{WS}/mailboxes")))
        .and(header("authorization", INTERNAL_AUTH))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": rows,
            "pagination": { "totalPages": 1, "next": "" },
        })))
        .mount(server)
        .await;
}

fn account(uri: &str) -> String {
    client(uri, ", email = \"ops@example.test\", password = \"pw\"")
}

/// The body the vendor documents, and the only shape it accepts: one password
/// carried into both transport blocks, the address as the username on each.
#[tokio::test]
async fn test_connect_smtp_sends_the_transport_blocks_the_vendor_asks_for() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path(format!("/public/v2/workspaces/{WS}/mailboxes")))
        .and(header("authorization", "k"))
        .and(body_string_contains("\"address\":\"ada@example.test\""))
        .and(body_string_contains("\"host\":\"smtp.gmail.com\""))
        .and(body_string_contains("\"port\":587"))
        .and(body_string_contains("\"host\":\"imap.gmail.com\""))
        .and(body_string_contains("\"port\":993"))
        .and(body_string_contains("\"username\":\"ada@example.test\""))
        .and(body_string_contains("\"password\":\"app-pw\""))
        .and(body_string_contains("\"firstName\":\"Ada\""))
        .respond_with(ResponseTemplate::new(201).set_body_json(json!({
            "id": "mbx_new", "address": "ada@example.test", "status": "PENDING",
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local box = c:connect_smtp("Ada@Example.TEST", "app-pw", { first = "Ada", last = "Person" })
        assert.eq(box.address, "ada@example.test")
        assert.eq(box.provider_ref, "mbx_new")
        assert.eq(box.status, "pending")
        -- The vendor verifies the credentials afterwards, so a box it has only
        -- accepted is not a box anything can send from yet.
        assert.eq(box.connected, false)
        "#
    ))
    .await
    .unwrap();
}

/// The password this call sends must never come back out of it.
///
/// `raw` is handed back whole so a caller can read metadata this module does
/// not map. On the connect path that whole is the answer to a body containing
/// the mailbox password, so a vendor that echoed the transport blocks — which
/// it does not today — would put a plaintext credential into every caller that
/// prints a row. The keys come off rather than being trusted not to appear.
#[tokio::test]
async fn test_a_create_response_that_echoes_the_transport_blocks_carries_no_password_out() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path(format!("/public/v2/workspaces/{WS}/mailboxes")))
        .respond_with(ResponseTemplate::new(201).set_body_json(json!({
            "id": "mbx_new",
            "address": "ada@example.test",
            "status": "ACTIVE",
            "dailyEmailLimit": 30,
            // The hypothetical: the vendor hands the submitted blocks back.
            "smtp": { "host": "smtp.gmail.com", "port": 587,
                      "username": "ada@example.test", "password": "app-pw-secret" },
            "imap": { "host": "imap.gmail.com", "port": 993,
                      "username": "ada@example.test", "password": "app-pw-secret" },
            "password": "app-pw-secret",
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local box = c:connect_smtp("ada@example.test", "app-pw-secret")
        assert.eq(box.address, "ada@example.test")
        assert.eq(box.connected, true)
        -- Nothing anywhere in the returned table, however a caller walks it.
        assert.eq(box.raw.smtp, nil)
        assert.eq(box.raw.imap, nil)
        assert.eq(box.raw.password, nil)
        assert.contains(json.encode(box), "mbx_new")
        assert.eq(json.encode(box):find("app-pw-secret", 1, true), nil)
        -- The metadata `raw` exists for is still there.
        assert.eq(box.raw.dailyEmailLimit, 30)
        "#
    ))
    .await
    .unwrap();
}

/// A vendor that already says `active` is the one case where the box IS wired.
#[tokio::test]
async fn test_connect_smtp_reports_connected_only_when_the_vendor_already_says_active() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path(format!("/public/v2/workspaces/{WS}/mailboxes")))
        .respond_with(ResponseTemplate::new(201).set_body_json(json!({
            "id": "mbx_new", "address": "ada@example.test", "status": "ACTIVE",
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local box = c:connect_smtp("ada@example.test", "app-pw")
        assert.eq(box.status, "active")
        assert.eq(box.connected, true)
        -- No name given: the local part beats a refused request, and the vendor
        -- rejects a body with no first name at all.
        "#
    ))
    .await
    .unwrap();
}

/// The vendor answers 2xx with its own refusal in the body when the credentials
/// do not verify. Read as a created mailbox that is a box nothing can send
/// from, reported as connected.
#[tokio::test]
async fn test_a_two_hundred_carrying_a_refusal_is_an_error_and_never_a_mailbox() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path(format!("/public/v2/workspaces/{WS}/mailboxes")))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "message": "failed to verify mailbox credentials",
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local box, err = c:connect_smtp("ada@example.test", "dead-pw")
        assert.eq(box, nil)
        assert.eq(err.code, "refused")
        assert.contains(err.message, "failed to verify mailbox credentials")
        "#
    ))
    .await
    .unwrap();
}

/// Refused here rather than at the vendor: a call with no password would reach
/// the API as a mailbox connected to nothing.
#[tokio::test]
async fn test_connect_smtp_refuses_a_bad_address_or_an_empty_password_before_calling() {
    let server = MockServer::start().await;
    run_lua(&format!(
        "{}{}",
        client(&server.uri(), ""),
        r#"
        local _, no_at = c:connect_smtp("not-an-address", "pw")
        assert.eq(no_at.code, "config")
        local _, no_pw = c:connect_smtp("ada@example.test", "   ")
        assert.eq(no_pw.code, "config")
        "#
    ))
    .await
    .unwrap();
    // Nothing was sent: an unmounted server answers 404, and either call would
    // have come back as an http error rather than a config one.
    assert!(server.received_requests().await.unwrap().is_empty());
}

/// The read-back is the whole point. The PUT answers exactly what it was asked
/// for; only the GET afterwards says what the vendor actually holds.
#[tokio::test]
async fn test_set_warmup_reports_what_the_vendor_holds_and_not_what_it_was_asked_for() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    Mock::given(method("PUT"))
        .and(path(format!("/workspaces/{WS}/mailboxes/mbx_xa1")))
        .and(header("authorization", INTERNAL_AUTH))
        .and(body_string_contains("\"warmupActivated\":true"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": "mbx_xa1", "address": "ada@example.test", "warmupActivated": true,
        })))
        .mount(&server)
        .await;
    // …and the box the vendor then hands back still has it off.
    Mock::given(method("GET"))
        .and(path(format!("/workspaces/{WS}/mailboxes/mbx_xa1")))
        .and(header("authorization", INTERNAL_AUTH))
        .respond_with(ResponseTemplate::new(200).set_body_json(internal_row(false)))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        account(&server.uri()),
        r#"
        local box = c:set_warmup("mbx_xa1", true)
        assert.eq(box.address, "ada@example.test")
        -- Not `warmup.activated == true`: the PUT said so and the box does not.
        assert.eq(box.warmup, nil)
        "#
    ))
    .await
    .unwrap();
}

/// The switch on, read back on: days and heat come off the row the GET returned.
#[tokio::test]
async fn test_set_warmup_on_reads_the_curve_back_off_the_vendors_own_row() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    Mock::given(method("PUT"))
        .and(path(format!("/workspaces/{WS}/mailboxes/mbx_xa1")))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "id": "mbx_xa1" })))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path(format!("/workspaces/{WS}/mailboxes/mbx_xa1")))
        // Some of these routes wrap the object; both shapes read the same.
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": internal_row(true),
        })))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        account(&server.uri()),
        r#"
        local box = c:set_warmup("mbx_xa1", true)
        assert.eq(box.warmup.activated, true)
        assert.eq(box.warmup.days_until_warm, 5)
        "#
    ))
    .await
    .unwrap();
}

/// An operator holds addresses; these endpoints take ids. The address is
/// resolved on the internal listing, which is also the only one that could have
/// answered for the warm-up.
#[tokio::test]
async fn test_set_warmup_takes_the_address_an_operator_actually_has() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    mount_internal_listing(&server, json!([internal_row(false)])).await;
    Mock::given(method("PUT"))
        .and(path(format!("/workspaces/{WS}/mailboxes/mbx_xa1")))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "id": "mbx_xa1" })))
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path(format!("/workspaces/{WS}/mailboxes/mbx_xa1")))
        .respond_with(ResponseTemplate::new(200).set_body_json(internal_row(true)))
        .mount(&server)
        .await;
    run_lua(&format!(
        "{}{}",
        account(&server.uri()),
        r#"
        assert.eq(c:mailbox_id("mbx_xa1"), "mbx_xa1")
        local box = c:set_warmup("Ada@Example.TEST", true)
        assert.eq(box.warmup.activated, true)
        "#
    ))
    .await
    .unwrap();
}

/// A box this workspace does not hold is a named refusal, never a PUT against
/// an address the vendor would read as an id.
#[tokio::test]
async fn test_an_address_the_workspace_does_not_hold_is_refused_by_name() {
    let server = MockServer::start().await;
    mount_sign_in(&server, 200, json!({ "idToken": "tok_x1" })).await;
    mount_internal_listing(&server, json!([internal_row(true)])).await;
    run_lua(&format!(
        "{}{}",
        account(&server.uri()),
        r#"
        local box, err = c:set_warmup("stranger@example.test", true)
        assert.eq(box, nil)
        assert.eq(err.code, "not_found")
        assert.contains(err.message, "stranger@example.test")
        local _, bad = c:set_warmup("mbx_xa1", "yes")
        assert.eq(bad.code, "config")
        "#
    ))
    .await
    .unwrap();
}

// ---------------------------------------------------------------------------
// Webhooks. The vendor's REST API answers 404 for /webhooks at every version,
// so these three ride the MCP endpoint with the Salesforge key header. What is
// pinned: the header, one event per hook, the signing secret coming back from a
// create and never from a read, and a tool-level refusal reading as an error.
// ---------------------------------------------------------------------------

/// The tool's payload arrives as a JSON string nested in the reply's text part.
fn mcp_reply(payload: serde_json::Value) -> ResponseTemplate {
    ResponseTemplate::new(200).set_body_json(json!({
        "jsonrpc": "2.0",
        "id": 1,
        "result": { "content": [{ "type": "text", "text": payload.to_string() }] },
    }))
}

async fn mount_mcp(server: &MockServer, tool: &str, payload: serde_json::Value) {
    Mock::given(method("POST"))
        .and(path("/mcp"))
        // The key rides its own product header, which is the whole of what
        // tells this endpoint which forge is calling.
        .and(header("x-salesforge-key", "k"))
        .and(body_string_contains(tool))
        .respond_with(mcp_reply(payload))
        .mount(server)
        .await;
}

fn mcp_client(uri: &str) -> String {
    client(uri, &format!(", mcp_base_url = \"{uri}/mcp\""))
}

#[tokio::test]
async fn test_webhooks_are_listed_from_the_mcp_endpoint() {
    let server = MockServer::start().await;
    mount_mcp(
        &server,
        "list_webhooks",
        json!({ "total": 1, "data": [{
            "id": "rwh_xa1", "name": "replies", "type": "email_replied",
            "url": "https://x.test/api/hooks/salesforge?token=t", "sentCount": 0,
        }] }),
    )
    .await;

    let script = format!(
        "{}local hooks = c:webhooks()\n\
         assert.eq(#hooks, 1)\n\
         assert.eq(hooks[1].id, \"rwh_xa1\")\n\
         assert.eq(hooks[1].type, \"email_replied\")",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}

/// An empty list is a list. A workspace that has never registered one is the
/// normal case, and it must not read as a failure.
#[tokio::test]
async fn test_no_webhooks_is_an_empty_list_and_not_an_error() {
    let server = MockServer::start().await;
    mount_mcp(&server, "list_webhooks", json!({ "total": 0, "data": [] })).await;
    let script = format!(
        "{}local hooks, meta = c:webhooks()\n\
         assert.not_nil(hooks)\n\
         assert.eq(#hooks, 0)\n\
         assert.eq(meta.truncated, false)",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}

/// The signing secret is returned once, by the create, and by nothing else. A
/// caller that does not store it here can never verify a delivery.
#[tokio::test]
async fn test_creating_a_webhook_returns_the_signing_secret_once() {
    let server = MockServer::start().await;
    mount_mcp(
        &server,
        "create_webhook",
        json!({
            "id": "rwh_xa2", "name": "replies", "type": "email_replied",
            "url": "https://x.test/api/hooks/salesforge?token=t",
            "sentCount": 0, "signingSecret": "whsec_abc",
        }),
    )
    .await;

    let script = format!(
        "{}local hook = c:create_webhook({{\n\
           url = \"https://x.test/api/hooks/salesforge?token=t\",\n\
           event = \"email_replied\", name = \"replies\",\n\
         }})\n\
         assert.eq(hook.id, \"rwh_xa2\")\n\
         assert.eq(hook.signingSecret, \"whsec_abc\")",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}

/// A read never carries the secret back, which is why the create is the only
/// chance to keep it.
#[tokio::test]
async fn test_reading_a_webhook_never_carries_the_secret() {
    let server = MockServer::start().await;
    mount_mcp(
        &server,
        "get_webhook",
        json!({
            "id": "rwh_xa2", "name": "replies", "type": "email_replied",
            "url": "https://x.test/api/hooks/salesforge?token=t", "sentCount": 3,
        }),
    )
    .await;

    let script = format!(
        "{}local hook = c:webhook(\"rwh_xa2\")\n\
         assert.eq(hook.sentCount, 3)\n\
         assert.eq(hook.signingSecret, nil)",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}

/// Both are the caller's mistake and neither reaches the vendor.
/// The tool answers ten and ignores limit and offset, so a workspace past ten
/// loses rows with nothing to say it did. A full window is the only signal
/// there is, and it reads as truncated rather than as a complete list of ten.
#[tokio::test]
async fn test_a_full_window_of_webhooks_reads_as_truncated() {
    let server = MockServer::start().await;
    let rows: Vec<serde_json::Value> = (0..10)
        .map(|i| {
            json!({
                "id": format!("rwh_{i}"), "name": "replies", "type": "email_replied",
                "url": "https://x.test/h?token=t", "sentCount": 0,
            })
        })
        .collect();
    mount_mcp(
        &server,
        "list_webhooks",
        json!({ "total": 10, "data": rows }),
    )
    .await;

    let script = format!(
        "{}local hooks, meta = c:webhooks()
         assert.eq(#hooks, 10)
         assert.eq(meta.truncated, true)
         assert.eq(meta.cap, 10)
         assert.eq(meta.seen, 10)",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}

/// Short of the cap the vendor has shown everything it holds.
#[tokio::test]
async fn test_a_short_window_of_webhooks_is_the_whole_list() {
    let server = MockServer::start().await;
    mount_mcp(
        &server,
        "list_webhooks",
        json!({ "total": 2, "data": [
            { "id": "rwh_1", "type": "email_replied", "url": "https://x.test/h" },
            { "id": "rwh_2", "type": "email_bounced", "url": "https://x.test/h" },
        ] }),
    )
    .await;

    let script = format!(
        "{}local hooks, meta = c:webhooks()
         assert.eq(#hooks, 2)
         assert.eq(meta.truncated, false)
         assert.eq(meta.seen, 2)",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}

/// An empty list still carries its meta, so a caller reading `truncated` on
/// every answer never finds it nil.
#[tokio::test]
async fn test_an_empty_webhook_list_still_carries_meta() {
    let server = MockServer::start().await;
    mount_mcp(&server, "list_webhooks", json!({ "total": 0, "data": [] })).await;
    let script = format!(
        "{}local hooks, meta = c:webhooks()
         assert.eq(#hooks, 0)
         assert.eq(meta.truncated, false)
         assert.eq(meta.cap, 10)",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}

/// `tostring` on a table sends the vendor "table: 0x…" as an event name, and
/// its refusal arrives seconds later in words nobody can act on.
#[tokio::test]
async fn test_a_url_or_event_that_is_not_a_string_is_refused_here() {
    let server = MockServer::start().await;
    let script = format!(
        "{}local a, e1 = c:create_webhook({{ url = \"https://x.test/h\", event = {{}} }})\n\
         assert.eq(a, nil)\n\
         assert.eq(e1.code, \"config\")\n\
         assert.contains(e1.message, \"string\")\n\
         local b, e2 = c:create_webhook({{ url = {{}}, event = \"email_replied\" }})\n\
         assert.eq(b, nil)\n\
         assert.eq(e2.code, \"config\")",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}

#[tokio::test]
async fn test_a_webhook_needs_a_url_and_an_event() {
    let server = MockServer::start().await;
    let script = format!(
        "{}local a, e1 = c:create_webhook({{ event = \"email_replied\" }})\n\
         assert.eq(a, nil)\n\
         assert.eq(e1.code, \"config\")\n\
         local b, e2 = c:create_webhook({{ url = \"https://x.test/h\" }})\n\
         assert.eq(b, nil)\n\
         assert.eq(e2.code, \"config\")",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}

/// The vendor refuses inside a 200, in an ordinary-looking result. Read as a
/// payload it becomes a success carrying the word "Error", which is how a
/// caller ends up acting on a call that did nothing.
#[tokio::test]
async fn test_a_refusal_the_tool_made_reads_as_an_error() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/mcp"))
        .and(header("x-salesforge-key", "k"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "jsonrpc": "2.0",
            "id": 1,
            "result": {
                "isError": true,
                "content": [{ "type": "text", "text": "Error: Salesforge API error 400: taken" }],
            },
        })))
        .mount(&server)
        .await;

    let script = format!(
        "{}local hook, err = c:create_webhook({{ url = \"https://x.test/h\", event = \"email_replied\" }})\n\
         assert.eq(hook, nil)\n\
         assert.eq(err.code, \"tool\")\n\
         assert.contains(err.message, \"400\")",
        mcp_client(&server.uri())
    );
    run_lua(&script).await.unwrap();
}