bddkit 0.2.0

Gherkin acceptance testing for backend services: one binary drives the HTTP API and the resources behind it
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
pub mod api;
pub mod assert;
pub mod db;
pub mod debug;
pub mod help;
pub mod plugin;
pub mod srp;
pub mod vars;

use crate::macros::{MacroCatalog, MacroDef};
use crate::options::{OptionsLayer, PollingOptionsLayer};
use crate::polling::{AttemptError, AttemptResult};
use crate::world::World;
use regex::Regex;
use std::collections::{HashSet, VecDeque};
use std::sync::LazyLock;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StepId {
    // request
    SetRequestHeader,
    AddRequestHeader,
    SetQueryParam,
    SetRequestBody,
    EmptyRequestBody,
    SetFormParams,
    RequestPath,
    RequestPathWithMethod,
    UseApi,
    UsePluginInstance,
    SignNextRequestWithHawk,
    ExpectEventually,
    ExpectWithin,
    ExpectWithinEvery,
    // response checks
    ResponseCode,
    ResponseBodyContainsJson,
    ResponseBodyEqualsJson,
    ResponseArrayLength,
    ResponseHeader,
    JsonNodeExists,
    // variables
    SetVariable,
    SetVariableGlobal,
    ExtractFromJson,
    ExtractFromJsonGlobal,
    ExtractFromCookies,
    ExtractFromCookiesGlobal,
    VariableEquals,
    VariableNotEquals,
    EncryptWithAes,
    // database
    UseConnection,
    DebugOn,
    DebugOff,
    HaveWith,
    HaveWhere,
    HaveMulti,
    Update,
    DeleteWhere,
    DeleteAll,
    ExtractFromDb,
    ShouldHaveWith,
    ShouldHaveTable,
    ShouldNotHaveWith,
    ShouldNotHaveTable,
    CallProcedure,
    CallFunction,
    GetSequence,
    Sleep,
    ShowAllVariables,
    ShowVariable,
    PrintResponseHeaders,
    PrintResponseBody,
    PrintResponseBodyAsPath,
    // SRP
    SrpVerifier,
    SrpVerifierWithSalt,
    SrpStartLogin,
    SrpCompleteLogin,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OptionsSource {
    Global,
    Http,
    Db,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StepKind {
    Action,
    Assertion(OptionsSource),
}

pub struct StepDef {
    pub id: StepId,
    /// The resource this step belongs to, and the heading `bddkit steps list`
    /// prints it under: `api`, `db`, `srp`, `vars`, `debug`, or `general` for
    /// the few steps that belong to no resource at all.
    pub group: &'static str,
    pub pattern: &'static str,
    /// One line, in English, stating the step's effect. English is the source
    /// language and it lives here, beside the pattern it describes;
    /// `locales/steps.<code>.yaml` carries translations of it.
    pub description: &'static str,
    pub kind: StepKind,
}

const fn action(
    id: StepId,
    group: &'static str,
    pattern: &'static str,
    description: &'static str,
) -> StepDef {
    StepDef {
        id,
        group,
        pattern,
        description,
        kind: StepKind::Action,
    }
}

const fn assertion(
    id: StepId,
    group: &'static str,
    pattern: &'static str,
    description: &'static str,
    source: OptionsSource,
) -> StepDef {
    StepDef {
        id,
        group,
        pattern,
        description,
        kind: StepKind::Assertion(source),
    }
}

/// Capture groups are named, and the name is the parameter's whole
/// documentation: `bddkit steps list` renders `(?P<path>[^"]*)` as `<path>`.
/// The `regex` crate keeps a named group addressable by index too, so every
/// dispatch arm below still captures by position.
pub const BUILTIN_STEPS: &[StepDef] = &[
    action(
        StepId::SetRequestHeader,
        "api",
        r#"^the "(?P<name>[^"]*)" request header is "(?P<value>[^"]*)"$"#,
        "sets a header on the request being built, replacing any previous value",
    ),
    action(
        StepId::AddRequestHeader,
        "api",
        r#"^I add "(?P<value>[^"]*)" to the "(?P<name>[^"]*)" request header$"#,
        "appends another value to a request header, keeping what is already there",
    ),
    action(
        StepId::SetQueryParam,
        "api",
        r#"^the query parameter "(?P<name>[^"]*)" is "(?P<value>[^"]*)"$"#,
        "sets a query string parameter on the request being built",
    ),
    action(
        StepId::SetRequestBody,
        "api",
        r#"^the request body is:$"#,
        "sets the request body from the docstring below the step",
    ),
    action(
        StepId::EmptyRequestBody,
        "api",
        r#"^the request body is empty$"#,
        "clears the request body built so far",
    ),
    action(
        StepId::SetFormParams,
        "api",
        r#"^the request form parameters are:$"#,
        "sends the table below as form-encoded parameters instead of a body",
    ),
    action(
        StepId::RequestPathWithMethod,
        "api",
        r#"^I request "(?P<path>[^"]*)" using HTTP (?P<method>GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)$"#,
        "performs the request against the current API with this method",
    ),
    action(
        StepId::RequestPath,
        "api",
        r#"^I request "(?P<path>[^"]*)"$"#,
        "performs a GET request against the current API",
    ),
    action(
        StepId::SignNextRequestWithHawk,
        "api",
        r#"^I sign the next request with Hawk id "(?P<id>[^"]*)" and key "(?P<key>[^"]*)"$"#,
        "signs the next request with these Hawk credentials, and every replay of it",
    ),
    action(
        StepId::ExpectEventually,
        "general",
        r#"^I expect the next assertion to pass eventually$"#,
        "lets the next assertion retry with the configured polling options",
    ),
    action(
        StepId::ExpectWithinEvery,
        "general",
        r#"^I expect the next assertion to pass within "(?P<seconds>\d+)" seconds, checking every "(?P<milliseconds>\d+)" milliseconds$"#,
        "lets the next assertion retry for this long, at this interval",
    ),
    action(
        StepId::ExpectWithin,
        "general",
        r#"^I expect the next assertion to pass within "(?P<seconds>\d+)" seconds$"#,
        "lets the next assertion retry for this long, at the configured interval",
    ),
    assertion(
        StepId::ResponseCode,
        "api",
        r#"^the response code is (?P<code>\d+)$"#,
        "asserts the HTTP status code of the last response",
        OptionsSource::Http,
    ),
    assertion(
        StepId::ResponseBodyContainsJson,
        "api",
        r#"^the response body contains JSON:$"#,
        "asserts the response body contains this JSON — object subsets pass, array order is ignored",
        OptionsSource::Http,
    ),
    assertion(
        StepId::ResponseBodyEqualsJson,
        "api",
        r#"^the response body equals JSON:$"#,
        "asserts the response body equals this JSON exactly, key for key and element for element",
        OptionsSource::Http,
    ),
    assertion(
        StepId::ResponseArrayLength,
        "api",
        r#"^the response body is a JSON array of length (?P<length>\d+)$"#,
        "asserts the response body is a JSON array holding this many elements",
        OptionsSource::Http,
    ),
    assertion(
        StepId::ResponseHeader,
        "api",
        r#"^the "(?P<name>[^"]*)" response header is "(?P<value>[^"]*)"$"#,
        "asserts a response header holds exactly this value",
        OptionsSource::Http,
    ),
    assertion(
        StepId::JsonNodeExists,
        "api",
        r#"^the JSON node "(?P<path>[^"]*)" should exist$"#,
        "asserts the response body has a node at this JSON path",
        OptionsSource::Http,
    ),
    action(
        StepId::SetVariableGlobal,
        "vars",
        r#"^set variable "(?P<name>[^"]*)" to "(?P<value>[^"]*)" global$"#,
        "sets a variable every scenario of this feature file can read",
    ),
    action(
        StepId::SetVariable,
        "vars",
        r#"^set variable "(?P<name>[^"]*)" to "(?P<value>[^"]*)"$"#,
        "sets a variable for the current scenario",
    ),
    action(
        StepId::ExtractFromJsonGlobal,
        "vars",
        r#"^extract "(?P<path>[^"]*)" from JSON as "(?P<name>[^"]*)" global$"#,
        "reads a JSON path of the response into a variable the whole file can see",
    ),
    action(
        StepId::ExtractFromJson,
        "vars",
        r#"^extract "(?P<path>[^"]*)" from JSON as "(?P<name>[^"]*)"$"#,
        "reads a JSON path of the response into a variable",
    ),
    action(
        StepId::ExtractFromCookiesGlobal,
        "vars",
        r#"^extract "(?P<cookie>[^"]*)" from cookies as "(?P<name>[^"]*)" global$"#,
        "reads a response cookie into a variable the whole file can see",
    ),
    action(
        StepId::ExtractFromCookies,
        "vars",
        r#"^extract "(?P<cookie>[^"]*)" from cookies as "(?P<name>[^"]*)"$"#,
        "reads a response cookie into a variable",
    ),
    assertion(
        StepId::VariableNotEquals,
        "vars",
        r#"^variable "(?P<name>[^"]*)" should not be equal to "(?P<value>[^"]*)"$"#,
        "asserts a variable differs from this value",
        OptionsSource::Global,
    ),
    assertion(
        StepId::VariableEquals,
        "vars",
        r#"^variable "(?P<name>[^"]*)" should be equal to "(?P<value>[^"]*)"$"#,
        "asserts a variable holds exactly this value",
        OptionsSource::Global,
    ),
    action(
        StepId::EncryptWithAes,
        "vars",
        r#"^I encrypt "(?P<value>[^"]*)" with AES using key "(?P<key>[^"]*)" as "(?P<name>[^"]*)"$"#,
        "encrypts a value with an AES key and stores the result in a variable",
    ),
    action(
        StepId::UseApi,
        "api",
        r#"^I use "(?P<name>[^"]*)" api$"#,
        "switches to another API resource and its default headers, until the scenario ends",
    ),
    action(
        StepId::UseConnection,
        "db",
        r#"^I use "(?P<name>[^"]*)" connection$"#,
        "switches to another database connection, until the scenario ends",
    ),
    action(
        StepId::DebugOn,
        "debug",
        r#"^I am in debug mode$"#,
        "prints the generated SQL, its binds and plugin exchanges to stderr",
    ),
    action(
        StepId::DebugOff,
        "debug",
        r#"^I am not in debug mode$"#,
        "stops the debug output turned on earlier in the scenario",
    ),
    action(
        StepId::HaveWhere,
        "db",
        r#"^I have "(?P<table>[^"]*)" where:$"#,
        "inserts one row per line of the table below, its header naming the columns",
    ),
    action(
        StepId::HaveWith,
        "db",
        r#"^I have "(?P<table>[^"]*)" with "(?P<pairs>[^"]*)"$"#,
        "inserts one row, filling required columns the pairs omit from the table schema",
    ),
    action(
        StepId::HaveMulti,
        "db",
        r#"^I have:$"#,
        "inserts rows into several tables, the first column of the table below naming each one",
    ),
    action(
        StepId::Update,
        "db",
        r#"^I update "(?P<table>[^"]*)" with "(?P<pairs>[^"]*)" where "(?P<condition>[^"]*)"$"#,
        "updates matching rows and stores the affected count in updated_<table>",
    ),
    action(
        StepId::DeleteAll,
        "db",
        r#"^I delete all "(?P<table>[^"]*)"$"#,
        "wipes the whole table — no WHERE, and no awareness of files running in parallel",
    ),
    action(
        StepId::DeleteWhere,
        "db",
        r#"^I delete "(?P<table>[^"]*)" where "(?P<condition>[^"]*)"$"#,
        "deletes matching rows and stores the affected count in deleted_<table>",
    ),
    action(
        StepId::ExtractFromDb,
        "db",
        r#"^I extract "(?P<column>[^"]*)" from "(?P<table>[^"]*)" with "(?P<condition>[^"]*)" as "(?P<name>[^"]*)"$"#,
        "reads a column of the first matching row into a variable",
    ),
    assertion(
        StepId::ShouldNotHaveTable,
        "db",
        r#"^I should not have "(?P<table>[^"]*)" with:$"#,
        "asserts no row matches any line of the table below",
        OptionsSource::Db,
    ),
    assertion(
        StepId::ShouldNotHaveWith,
        "db",
        r#"^I should not have "(?P<table>[^"]*)" with "(?P<pairs>[^"]*)"$"#,
        "asserts no row matches these column values",
        OptionsSource::Db,
    ),
    assertion(
        StepId::ShouldHaveTable,
        "db",
        r#"^I should have "(?P<table>[^"]*)" with:$"#,
        "asserts a row matches each line of the table below",
        OptionsSource::Db,
    ),
    assertion(
        StepId::ShouldHaveWith,
        "db",
        r#"^I should have "(?P<table>[^"]*)" with "(?P<pairs>[^"]*)"$"#,
        "asserts at least one row matches these column values",
        OptionsSource::Db,
    ),
    action(
        StepId::CallProcedure,
        "db",
        r#"^I call procedure "(?P<name>[^"]*)" with "(?P<arguments>[^"]*)"$"#,
        "calls a stored procedure with these arguments",
    ),
    action(
        StepId::CallFunction,
        "db",
        r#"^I call function "(?P<name>[^"]*)" with "(?P<arguments>[^"]*)" as "(?P<variable>[^"]*)"$"#,
        "calls a function and stores its return value in a variable",
    ),
    action(
        StepId::GetSequence,
        "db",
        r#"^I get next value of sequence "(?P<name>[^"]*)" as "(?P<variable>[^"]*)"$"#,
        "advances a sequence and stores the new value in a variable",
    ),
    action(
        StepId::Sleep,
        "general",
        r#"^I sleep "(?P<seconds>\d+)" seconds$"#,
        "pauses the scenario — a last resort, an eventual assertion is usually what you want",
    ),
    action(
        StepId::ShowAllVariables,
        "debug",
        r#"^Show all variables$"#,
        "prints every variable in scope to stderr",
    ),
    action(
        StepId::ShowVariable,
        "debug",
        r#"^Show "(?P<name>[^"]*)" variable$"#,
        "prints one variable to stderr, failing if it is not set",
    ),
    action(
        StepId::PrintResponseHeaders,
        "debug",
        r#"^Print response headers$"#,
        "prints the headers of the last response to stderr",
    ),
    action(
        StepId::PrintResponseBody,
        "debug",
        r#"^Print response body$"#,
        "prints the body of the last response to stderr, highlighted",
    ),
    action(
        StepId::PrintResponseBodyAsPath,
        "debug",
        r#"^Print response body as "(?P<path>[^"]*)"$"#,
        "prints one JSON path or XPath selection of the last response to stderr",
    ),
    action(
        StepId::SrpVerifierWithSalt,
        "srp",
        r#"^I generate an SRP verifier for "(?P<username>[^"]*)" with password "(?P<password>[^"]*)" and salt "(?P<salt>[^"]*)" as "(?P<prefix>[^"]*)"$"#,
        "computes an SRP verifier from this salt into <prefix>_salt and <prefix>_verifier",
    ),
    action(
        StepId::SrpVerifier,
        "srp",
        r#"^I generate an SRP verifier for "(?P<username>[^"]*)" with password "(?P<password>[^"]*)" as "(?P<prefix>[^"]*)"$"#,
        "computes an SRP verifier from a fresh salt into <prefix>_salt and <prefix>_verifier",
    ),
    action(
        StepId::SrpStartLogin,
        "srp",
        r#"^I start an SRP login as "(?P<prefix>[^"]*)"$"#,
        "starts an SRP login, storing the client values in <prefix>_a and <prefix>_A",
    ),
    action(
        StepId::SrpCompleteLogin,
        "srp",
        r#"^I complete SRP login "(?P<prefix>[^"]*)" for "(?P<username>[^"]*)" with password "(?P<password>[^"]*)" salt "(?P<salt>[^"]*)" and "(?P<server_public>[^"]*)"$"#,
        "answers the server challenge into <prefix>_M1, <prefix>_M2 and <prefix>_sessionKey",
    ),
];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StepTarget {
    Builtin { id: StepId, kind: StepKind },
    Macro(usize),
    /// A step served over FFI. It never becomes a `StepId`, which is what
    /// keeps `dispatch`'s exhaustive match (invariant 4) meaningful.
    Plugin {
        lib: usize,
        step: usize,
        assertion: bool,
    },
}

impl PartialEq<StepId> for StepTarget {
    fn eq(&self, other: &StepId) -> bool {
        matches!(self, Self::Builtin { id, .. } if id == other)
    }
}

#[derive(Debug)]
pub struct Registry {
    entries: Vec<(StepTarget, Regex)>,
    macros: Vec<MacroDef>,
}

impl Registry {
    #[cfg(test)]
    pub fn new() -> Result<Self, String> {
        Self::with_macros(MacroCatalog {
            definitions: Vec::new(),
        })
    }

    /// Only tests build a registry without plugins now: `main` always goes
    /// through `with_macros_and_plugins`, which is a superset.
    #[cfg(test)]
    pub fn with_macros(catalog: MacroCatalog) -> Result<Self, String> {
        Self::with_macros_and_plugins(catalog, &[], &[])
    }

    /// Everything is registered before macros are validated, so a macro body may
    /// name a plugin step. Order inside `entries` is irrelevant — `find` collects
    /// every match and reports ambiguity — but a plugin pattern that is not in the
    /// registry yet is indistinguishable from a typo.
    pub fn with_macros_and_plugins(
        catalog: MacroCatalog,
        plugin_steps: &[(usize, usize, String, bool)],
        plugin_groups: &[String],
    ) -> Result<Self, String> {
        let mut entries = Vec::with_capacity(BUILTIN_STEPS.len());
        for def in BUILTIN_STEPS {
            let re = Regex::new(def.pattern)
                .map_err(|e| format!("invalid step pattern {:?}: {e}", def.pattern))?;
            entries.push((
                StepTarget::Builtin {
                    id: def.id,
                    kind: def.kind,
                },
                re,
            ));
        }
        for (index, definition) in catalog.definitions.iter().enumerate() {
            entries.push((StepTarget::Macro(index), definition.regex.clone()));
        }
        let mut registry = Self {
            entries,
            macros: catalog.definitions,
        };
        for (lib, step, pattern, assertion) in plugin_steps {
            registry.add_plugin_step(*lib, *step, pattern, *assertion)?;
        }
        registry.add_use_group_step(plugin_groups)?;
        registry.validate_macros()?;
        Ok(registry)
    }

    /// Returns `Ok(None)` if the step is unknown, and `Err` if it is ambiguous.
    /// Ambiguity is an error, not "first wins": a silently shadowed step
    /// is more expensive to debug than a failed start.
    pub fn find(&self, text: &str) -> Result<Option<(StepTarget, Vec<String>)>, String> {
        let mut hits: Vec<(StepTarget, Vec<String>)> = Vec::new();
        for (target, re) in &self.entries {
            if let Some(c) = re.captures(text) {
                let caps = c
                    .iter()
                    .skip(1)
                    .map(|g| g.map(|m| m.as_str().to_string()).unwrap_or_default())
                    .collect();
                hits.push((*target, caps));
            }
        }
        match hits.len() {
            0 => Ok(None),
            1 => Ok(Some(hits.remove(0))),
            _ => Err(format!(
                "step {text:?} matches several definitions: {:?}",
                hits.iter().map(|(target, _)| target).collect::<Vec<_>>()
            )),
        }
    }

    pub fn macro_def(&self, index: usize) -> &MacroDef {
        &self.macros[index]
    }

    /// Registers one pattern a plugin declared. Called at startup, before the
    /// first request, so `validate::check` still sees every step (invariant 1).
    ///
    /// ponytail: no static overlap analysis between two arbitrary plugin
    /// regexes — `find` reports ambiguity per step and `validate::check` runs
    /// it over every selected step, which catches every collision that can
    /// actually fire. Upgrade path if a startup-time check is ever wanted:
    /// extend the `patterns_overlap` token model in this file to raw regexes.
    pub fn add_plugin_step(
        &mut self,
        lib: usize,
        step: usize,
        pattern: &str,
        assertion: bool,
    ) -> Result<(), String> {
        let re = Regex::new(pattern)
            .map_err(|e| format!("invalid plugin step pattern {pattern:?}: {e}"))?;
        self.entries
            .push((StepTarget::Plugin { lib, step, assertion }, re));
        Ok(())
    }

    /// Registers `I use "<name>" <group>` over the groups the loaded plugins
    /// actually claim. The alternation is built from the real group names, so
    /// the pattern cannot overlap `I use "<name>" api` or
    /// `I use "<conn>" connection` and no existing .feature changes meaning.
    pub fn add_use_group_step(&mut self, groups: &[String]) -> Result<(), String> {
        if groups.is_empty() {
            return Ok(());
        }
        let alternation = groups
            .iter()
            .map(|g| regex::escape(g))
            .collect::<Vec<_>>()
            .join("|");
        let pattern = format!(r#"^I use "([^"]*)" ({alternation})$"#);
        let re = Regex::new(&pattern)
            .map_err(|e| format!("invalid group-switch pattern {pattern:?}: {e}"))?;
        self.entries.push((
            StepTarget::Builtin {
                id: StepId::UsePluginInstance,
                kind: StepKind::Action,
            },
            re,
        ));
        Ok(())
    }

    fn validate_macros(&self) -> Result<(), String> {
        for (left_index, left) in self.macros.iter().enumerate() {
            for builtin in BUILTIN_STEPS {
                if builtin_patterns(builtin.pattern)
                    .iter()
                    .any(|pattern| patterns_overlap(&macro_pattern(&left.step), pattern))
                {
                    return Err(format!(
                        "macro step {:?} from {}:{} conflicts with builtin step {:?}",
                        left.step,
                        left.source.display(),
                        left.line,
                        builtin.pattern
                    ));
                }
            }
            for right in self.macros.iter().skip(left_index + 1) {
                if patterns_overlap(&macro_pattern(&left.step), &macro_pattern(&right.step)) {
                    return Err(format!(
                        "macro step {:?} from {}:{} conflicts with {:?} from {}:{}",
                        left.step,
                        left.source.display(),
                        left.line,
                        right.step,
                        right.source.display(),
                        right.line,
                    ));
                }
            }
        }

        let mut graph = vec![Vec::new(); self.macros.len()];
        for (index, definition) in self.macros.iter().enumerate() {
            for step in &definition.body {
                match self.find(&step.text)? {
                    Some((StepTarget::Builtin { .. }, _)) => {}
                    Some((StepTarget::Plugin { .. }, _)) => {}
                    Some((StepTarget::Macro(_), _)) if step.docstring.is_some() => {
                        return Err(format!(
                            "macro call {:?} with a doc string inside macro {:?} from {}:{} is not supported",
                            step.text,
                            definition.step,
                            definition.source.display(),
                            definition.line,
                        ));
                    }
                    Some((StepTarget::Macro(target), _)) => graph[index].push(target),
                    None => {
                        return Err(format!(
                            "unknown step {:?} in macro {:?} from {}:{}",
                            step.text,
                            definition.step,
                            definition.source.display(),
                            definition.line,
                        ));
                    }
                }
            }
        }

        let mut visited = vec![false; self.macros.len()];
        let mut visiting = vec![false; self.macros.len()];
        let mut path = Vec::new();
        for index in 0..self.macros.len() {
            self.visit(index, &graph, &mut visited, &mut visiting, &mut path)?;
        }
        let mut depths = vec![None; self.macros.len()];
        for index in 0..self.macros.len() {
            if macro_depth(index, &graph, &mut depths) > 16 {
                return Err(format!(
                    "macro nesting exceeds 16 starting from step {:?}",
                    self.macros[index].step
                ));
            }
        }
        Ok(())
    }

    fn visit(
        &self,
        index: usize,
        graph: &[Vec<usize>],
        visited: &mut [bool],
        visiting: &mut [bool],
        path: &mut Vec<usize>,
    ) -> Result<(), String> {
        if visiting[index] {
            let start = path.iter().position(|item| *item == index).unwrap_or(0);
            let mut cycle: Vec<&str> = path[start..]
                .iter()
                .map(|item| self.macros[*item].step.as_str())
                .collect();
            cycle.push(self.macros[index].step.as_str());
            return Err(format!("cycle in macros: {}", cycle.join(" → ")));
        }
        if visited[index] {
            return Ok(());
        }
        visiting[index] = true;
        path.push(index);
        for child in &graph[index] {
            self.visit(*child, graph, visited, visiting, path)?;
        }
        path.pop();
        visiting[index] = false;
        visited[index] = true;
        Ok(())
    }
}

fn macro_depth(index: usize, graph: &[Vec<usize>], memo: &mut [Option<usize>]) -> usize {
    if let Some(depth) = memo[index] {
        return depth;
    }
    let depth = 1 + graph[index]
        .iter()
        .map(|child| macro_depth(*child, graph, memo))
        .max()
        .unwrap_or(0);
    memo[index] = Some(depth);
    depth
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CharClass {
    Any,
    NonQuote,
    Digit,
    Exact(char),
}

static DIGIT_CHAR: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^\d$").expect("constant digit regex"));

#[derive(Clone, Copy, Debug)]
enum PatternToken {
    One(CharClass),
    Star(CharClass),
}

static MACRO_PARAM: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"\{[A-Za-z_][A-Za-z0-9_]*\}").expect("constant macro parameter regex")
});

fn macro_pattern(template: &str) -> Vec<PatternToken> {
    let mut tokens = Vec::new();
    let mut last = 0;
    for parameter in MACRO_PARAM.find_iter(template) {
        tokens.extend(
            template[last..parameter.start()]
                .chars()
                .map(|char_| PatternToken::One(CharClass::Exact(char_))),
        );
        tokens.push(PatternToken::Star(CharClass::Any));
        last = parameter.end();
    }
    tokens.extend(
        template[last..]
            .chars()
            .map(|char_| PatternToken::One(CharClass::Exact(char_))),
    );
    tokens
}

static GROUP_NAME: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"\?P?<[A-Za-z_][A-Za-z0-9_]*>").expect("constant group-name regex")
});

fn builtin_patterns(pattern: &str) -> Vec<Vec<PatternToken>> {
    // A group name is display metadata for `bddkit steps list`, and the
    // tokenizer below recognizes `([^"]*)`, `(\d+)` and the method alternation
    // by their literal text. An unrecognized construct does not fail here — it
    // degrades into literal characters, which would quietly stop a macro from
    // conflicting with the builtin it shadows. Stripping the names first leaves
    // the token stream exactly as it was before the table was annotated.
    let pattern = &*GROUP_NAME.replace_all(pattern, "");
    const METHODS: &str = "(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)";
    let variants: Vec<String> = if pattern.contains(METHODS) {
        ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"]
            .iter()
            .map(|method| pattern.replace(METHODS, method))
            .collect()
    } else {
        vec![pattern.to_string()]
    };
    variants
        .iter()
        .map(|variant| {
            let source = variant.trim_start_matches('^').trim_end_matches('$');
            let mut tokens = Vec::new();
            let mut rest = source;
            while !rest.is_empty() {
                if let Some(tail) = rest.strip_prefix(r#"([^"]*)"#) {
                    tokens.push(PatternToken::Star(CharClass::NonQuote));
                    rest = tail;
                } else if let Some(tail) = rest.strip_prefix(r"(\d+)") {
                    tokens.push(PatternToken::One(CharClass::Digit));
                    tokens.push(PatternToken::Star(CharClass::Digit));
                    rest = tail;
                } else {
                    let char_ = rest.chars().next().expect("string is not empty");
                    tokens.push(PatternToken::One(CharClass::Exact(char_)));
                    rest = &rest[char_.len_utf8()..];
                }
            }
            tokens
        })
        .collect()
}

fn patterns_overlap(left: &[PatternToken], right: &[PatternToken]) -> bool {
    let mut queue = VecDeque::from([(0usize, 0usize)]);
    let mut visited = HashSet::new();
    while let Some((left_pos, right_pos)) = queue.pop_front() {
        if !visited.insert((left_pos, right_pos)) {
            continue;
        }
        if left_pos == left.len() && right_pos == right.len() {
            return true;
        }
        if matches!(left.get(left_pos), Some(PatternToken::Star(_))) {
            queue.push_back((left_pos + 1, right_pos));
        }
        if matches!(right.get(right_pos), Some(PatternToken::Star(_))) {
            queue.push_back((left_pos, right_pos + 1));
        }
        let Some(left_token) = left.get(left_pos) else {
            continue;
        };
        let Some(right_token) = right.get(right_pos) else {
            continue;
        };
        let (left_class, left_next) = consumed(*left_token, left_pos);
        let (right_class, right_next) = consumed(*right_token, right_pos);
        if classes_overlap(left_class, right_class) {
            queue.push_back((left_next, right_next));
        }
    }
    false
}

fn consumed(token: PatternToken, position: usize) -> (CharClass, usize) {
    match token {
        PatternToken::One(class) => (class, position + 1),
        PatternToken::Star(class) => (class, position),
    }
}

fn classes_overlap(left: CharClass, right: CharClass) -> bool {
    use CharClass::{Any, Digit, Exact, NonQuote};
    match (left, right) {
        (Any, _)
        | (_, Any)
        | (NonQuote, NonQuote)
        | (NonQuote, Digit)
        | (Digit, NonQuote)
        | (Digit, Digit) => true,
        (NonQuote, Exact(char_)) | (Exact(char_), NonQuote) => char_ != '"',
        (Digit, Exact(char_)) | (Exact(char_), Digit) => DIGIT_CHAR.is_match(&char_.to_string()),
        (Exact(left), Exact(right)) => left == right,
    }
}

/// Step arguments after interpolation.
pub struct Args {
    pub caps: Vec<String>,
    pub docstring: Option<String>,
    pub table: Option<Vec<Vec<String>>>,
}

impl Args {
    fn cap(&self, i: usize) -> &str {
        self.caps.get(i).map(String::as_str).unwrap_or("")
    }
}

/// One `match` instead of boxed futures and trait objects.
pub async fn dispatch(w: &mut World, id: StepId, a: &Args, attempt: u64) -> AttemptResult {
    let result: Result<(), String> = match id {
        StepId::SetRequestHeader => api::set_header(w, a.cap(0), a.cap(1)),
        // The step names the value before the name: `I add "V" to the "H" request header`.
        StepId::AddRequestHeader => api::add_header(w, a.cap(1), a.cap(0)),
        StepId::SetQueryParam => api::set_query(w, a.cap(0), a.cap(1)),
        StepId::SetRequestBody => api::set_body(w, a.docstring.as_ref()),
        StepId::EmptyRequestBody => api::clear_body(w),
        StepId::SetFormParams => api::set_form(w, a.table.as_ref()),
        StepId::SignNextRequestWithHawk => api::sign_next_request_with_hawk(w, a.cap(0), a.cap(1)),
        StepId::RequestPath => api::request(w, a.cap(0), "GET").await,
        StepId::RequestPathWithMethod => {
            let (p, m) = (a.cap(0).to_string(), a.cap(1).to_string());
            api::request(w, &p, &m).await
        }
        StepId::ExpectEventually => {
            w.arm_options(OptionsLayer::default());
            Ok(())
        }
        StepId::ExpectWithin => {
            let timeout_secs = parse_positive(a.cap(0))?;
            w.arm_options(OptionsLayer {
                polling: Some(PollingOptionsLayer {
                    timeout_secs: Some(timeout_secs),
                    interval_ms: None,
                }),
            });
            Ok(())
        }
        StepId::ExpectWithinEvery => {
            let timeout_secs = parse_positive(a.cap(0))?;
            let interval_ms = parse_positive(a.cap(1))?;
            if interval_ms > timeout_secs.saturating_mul(1000) {
                Err("polling interval_ms must not exceed timeout_secs".to_string())
            } else {
                w.arm_options(OptionsLayer {
                    polling: Some(PollingOptionsLayer {
                        timeout_secs: Some(timeout_secs),
                        interval_ms: Some(interval_ms),
                    }),
                });
                Ok(())
            }
        }
        StepId::ResponseCode => {
            assert::replay_response(w, attempt).await?;
            return assert::response_code(w, a.cap(0));
        }
        StepId::ResponseHeader => {
            assert::replay_response(w, attempt).await?;
            return assert::response_header(w, a.cap(0), a.cap(1));
        }
        StepId::ResponseBodyContainsJson => {
            assert::replay_response(w, attempt).await?;
            return assert::body_contains_json(w, a.docstring.as_ref());
        }
        StepId::ResponseBodyEqualsJson => {
            assert::replay_response(w, attempt).await?;
            return assert::body_equals_json(w, a.docstring.as_ref());
        }
        StepId::ResponseArrayLength => {
            assert::replay_response(w, attempt).await?;
            return assert::array_length(w, a.cap(0));
        }
        StepId::JsonNodeExists => {
            assert::replay_response(w, attempt).await?;
            return assert::json_node_exists(w, a.cap(0));
        }
        StepId::SetVariable => vars::set_variable(w, a.cap(0), a.cap(1), false),
        StepId::SetVariableGlobal => vars::set_variable(w, a.cap(0), a.cap(1), true),
        StepId::ExtractFromJson => vars::extract_from_json(w, a.cap(0), a.cap(1), false),
        StepId::ExtractFromJsonGlobal => vars::extract_from_json(w, a.cap(0), a.cap(1), true),
        StepId::ExtractFromCookies => vars::extract_from_cookies(w, a.cap(0), a.cap(1), false),
        StepId::ExtractFromCookiesGlobal => vars::extract_from_cookies(w, a.cap(0), a.cap(1), true),
        StepId::VariableEquals => {
            return vars::variable_equals(w, a.cap(0), a.cap(1), false);
        }
        StepId::VariableNotEquals => {
            return vars::variable_equals(w, a.cap(0), a.cap(1), true);
        }
        StepId::EncryptWithAes => vars::encrypt_with_aes(w, a.cap(0), a.cap(1), a.cap(2)),
        StepId::UseApi => api::use_api(w, a.cap(0)),
        StepId::UsePluginInstance => plugin::use_instance(w, a.cap(1), a.cap(0)),
        StepId::UseConnection => db::use_connection(w, a.cap(0)),
        StepId::DebugOn => db::debug_on(w),
        StepId::DebugOff => db::debug_off(w),
        StepId::HaveWith => db::have_with(w, a.cap(0), a.cap(1)).await,
        StepId::HaveWhere => db::have_where(w, a.cap(0), a.table.as_ref()).await,
        StepId::HaveMulti => db::have_multi(w, a.table.as_ref()).await,
        StepId::Update => db::update(w, a.cap(0), a.cap(1), a.cap(2)).await,
        StepId::DeleteWhere => db::delete_where(w, a.cap(0), a.cap(1)).await,
        StepId::DeleteAll => db::delete_all(w, a.cap(0)).await,
        StepId::ExtractFromDb => {
            db::extract_from_db(w, a.cap(0), a.cap(1), a.cap(2), a.cap(3)).await
        }
        StepId::ShouldHaveWith => return db::should_have(w, a.cap(0), a.cap(1)).await,
        StepId::ShouldHaveTable => {
            return db::should_have_table(w, a.cap(0), a.table.as_ref()).await;
        }
        StepId::ShouldNotHaveWith => return db::should_not_have(w, a.cap(0), a.cap(1)).await,
        StepId::ShouldNotHaveTable => {
            return db::should_not_have_table(w, a.cap(0), a.table.as_ref()).await;
        }
        StepId::CallProcedure => db::call_procedure(w, a.cap(0), a.cap(1)).await,
        StepId::CallFunction => db::call_function(w, a.cap(0), a.cap(1), a.cap(2)).await,
        StepId::GetSequence => db::get_sequence(w, a.cap(0), a.cap(1)).await,
        StepId::Sleep => sleep(a.cap(0)).await,
        StepId::ShowAllVariables => show_all_variables(w),
        StepId::ShowVariable => show_variable(w, a.cap(0)),
        StepId::PrintResponseHeaders => debug::print_headers(w),
        StepId::PrintResponseBody => debug::print_body(w),
        StepId::PrintResponseBodyAsPath => debug::print_body_as(w, a.cap(0)),
        StepId::SrpVerifier => srp::generate_verifier(w, a.cap(0), a.cap(1), None, a.cap(2)),
        StepId::SrpVerifierWithSalt => {
            srp::generate_verifier(w, a.cap(0), a.cap(1), Some(a.cap(2)), a.cap(3))
        }
        StepId::SrpStartLogin => srp::start_login(w, a.cap(0)),
        StepId::SrpCompleteLogin => {
            srp::complete_login(w, a.cap(0), a.cap(1), a.cap(2), a.cap(3), a.cap(4))
        }
    };
    result.map_err(AttemptError::Fatal)
}

fn parse_positive(value: &str) -> Result<u64, String> {
    let parsed = value
        .parse::<u64>()
        .map_err(|_| format!("polling value must be a positive integer: {value:?}"))?;
    if parsed == 0 {
        Err("polling value must be positive".to_string())
    } else {
        Ok(parsed)
    }
}

async fn sleep(secs: &str) -> Result<(), String> {
    let n = secs
        .parse::<u64>()
        .map_err(|_| format!("not a number: {secs}"))?;
    tokio::time::sleep(std::time::Duration::from_secs(n)).await;
    Ok(())
}

fn show_all_variables(w: &World) -> Result<(), String> {
    eprintln!("=== All variables ===");
    for (name, value) in w.vars.all_vars() {
        eprintln!("{}: {}", name, value);
    }
    Ok(())
}

fn show_variable(w: &World, name: &str) -> Result<(), String> {
    match w.vars.get(name) {
        Some(v) => {
            eprintln!("{}: {}", name, v);
            Ok(())
        }
        None => Err(format!("variable not found: {name}")),
    }
}

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

    fn reg() -> Registry {
        Registry::new().unwrap()
    }

    #[test]
    fn all_builtin_patterns_compile() {
        assert!(Registry::new().is_ok());
    }

    #[test]
    fn a_plugin_step_is_found_like_any_other() {
        let mut reg = Registry::new().expect("registry");
        reg.add_plugin_step(0, 1, r#"^I upload file "([^"]*)" to "([^"]*)"$"#, false)
            .expect("valid pattern");
        let (target, caps) = reg
            .find(r#"I upload file "report.pdf" to "backups""#)
            .expect("no ambiguity")
            .expect("matched");
        assert_eq!(
            target,
            StepTarget::Plugin { lib: 0, step: 1, assertion: false }
        );
        assert_eq!(caps, vec!["report.pdf".to_string(), "backups".to_string()]);
    }

    #[test]
    fn an_invalid_plugin_pattern_is_rejected_at_registration() {
        let mut reg = Registry::new().expect("registry");
        let error = reg
            .add_plugin_step(0, 0, "^I upload file \"([^\"]*$", false)
            .expect_err("unbalanced group");
        assert!(error.contains("plugin"), "{error}");
    }

    #[test]
    fn a_plugin_pattern_colliding_with_a_builtin_is_an_ambiguity_error() {
        // Cross-layer ambiguity needs no separate checker: `find` already reports
        // every match, and validate::check runs `find` over every selected step
        // before the first request.
        let mut reg = Registry::new().expect("registry");
        reg.add_plugin_step(0, 0, r#"^the response code is (\d+)$"#, true)
            .expect("valid pattern");
        let error = reg
            .find("the response code is 200")
            .expect_err("two definitions match");
        assert!(error.contains("several definitions"), "{error}");
    }

    #[test]
    fn the_group_switch_step_does_not_shadow_api_or_connection() {
        let reg = Registry::with_macros_and_plugins(
            MacroCatalog { definitions: Vec::new() },
            &[],
            &["widget".to_string(), "browser".to_string()],
        )
        .expect("registry");

        let (api_target, _) = reg
            .find(r#"I use "main" api"#)
            .expect("no ambiguity")
            .expect("matched");
        assert_eq!(api_target, StepTarget::Builtin { id: StepId::UseApi, kind: StepKind::Action });

        let (conn_target, _) = reg
            .find(r#"I use "pg" connection"#)
            .expect("no ambiguity")
            .expect("matched");
        assert_eq!(
            conn_target,
            StepTarget::Builtin { id: StepId::UseConnection, kind: StepKind::Action }
        );

        let (group_target, caps) = reg
            .find(r#"I use "bucket-a" widget"#)
            .expect("no ambiguity")
            .expect("matched");
        assert_eq!(
            group_target,
            StepTarget::Builtin { id: StepId::UsePluginInstance, kind: StepKind::Action }
        );
        assert_eq!(caps, vec!["bucket-a".to_string(), "widget".to_string()], "name before group");
    }

    #[test]
    fn a_group_name_with_regex_metacharacters_is_escaped_not_interpreted() {
        let reg = Registry::with_macros_and_plugins(
            MacroCatalog { definitions: Vec::new() },
            &[],
            &["widget.beta".to_string()],
        )
        .expect("registry");

        // If the dot were left as a regex metachar it would match any single
        // character here too, not just a literal dot.
        assert!(
            reg.find(r#"I use "x" widgetXbeta"#).expect("no ambiguity").is_none(),
            "an unescaped '.' would wrongly match any character"
        );
        assert!(
            reg.find(r#"I use "x" widget.beta"#).expect("no ambiguity").is_some(),
            "the literal group name must still match"
        );
    }

    #[test]
    fn every_builtin_declares_a_known_group_and_a_description() {
        // The group is what `bddkit steps list` prints and filters on: a typo
        // there hides a whole step from discovery without failing anything else.
        const GROUPS: &[&str] = &["api", "db", "srp", "vars", "debug", "general"];
        for def in BUILTIN_STEPS {
            assert!(
                GROUPS.contains(&def.group),
                "{:?}: unknown group {:?}",
                def.id,
                def.group
            );
            assert!(
                !def.description.is_empty(),
                "{:?} has no description",
                def.id
            );
        }
    }

    #[test]
    fn a_macro_conflicting_with_a_named_builtin_is_still_rejected() {
        // `builtin_patterns` recognizes `([^"]*)` by its literal text, and an
        // unrecognized construct degrades into literal characters rather than
        // failing. Naming the group without normalizing it first would silently
        // stop this collision from being seen at all.
        //
        // The macro is deliberately parameterless: a `{param}` becomes a
        // wildcard that swallows even unrecognized literals, so it would report
        // a conflict either way. Here the BUILTIN's capture is the side that has
        // to expand, which it can only do once it is recognized as a capture.
        let path = std::env::temp_dir().join(format!(
            "bddkit-steps-named-group-conflict-{}.yaml",
            std::process::id()
        ));
        std::fs::write(
            &path,
            "- step: I request \"/ping\"\n  do: [Show all variables]\n",
        )
        .expect("write macro file");
        let catalog = MacroCatalog::load(std::slice::from_ref(&path)).expect("macro loads");
        std::fs::remove_file(&path).ok();

        let error = Registry::with_macros(catalog).expect_err("the macro shadows a builtin");
        assert!(error.contains("conflicts with builtin"), "{error}");
    }

    #[test]
    fn a_macro_body_may_call_a_plugin_step() {
        // Macros exist to compose steps and plugins exist to add steps.
        // Validating macros before the plugin patterns are registered made a
        // plugin step in a macro body indistinguishable from a typo.
        let path = std::env::temp_dir().join(format!(
            "bddkit-steps-macro-calls-plugin-{}.yaml",
            std::process::id()
        ));
        std::fs::write(
            &path,
            "- step: I do the plugin thing\n  do: [I upload file \"a\" to \"b\"]\n",
        )
        .expect("write macro file");
        let catalog = MacroCatalog::load(std::slice::from_ref(&path)).expect("macro loads");
        std::fs::remove_file(&path).ok();

        let reg = Registry::with_macros_and_plugins(
            catalog,
            &[(0, 1, r#"^I upload file "([^"]*)" to "([^"]*)"$"#.to_string(), false)],
            &[],
        )
        .expect("a plugin step in a macro body is not a typo");

        let (target, _) = reg
            .find("I do the plugin thing")
            .expect("no ambiguity")
            .expect("matched");
        assert_eq!(target, StepTarget::Macro(0));
    }

    #[test]
    fn a_macro_body_calling_a_truly_unknown_step_still_fails() {
        let path = std::env::temp_dir().join(format!(
            "bddkit-steps-macro-calls-unknown-{}.yaml",
            std::process::id()
        ));
        std::fs::write(
            &path,
            "- step: I do the mystery thing\n  do: [I have never heard of this step]\n",
        )
        .expect("write macro file");
        let catalog = MacroCatalog::load(std::slice::from_ref(&path)).expect("macro loads");
        std::fs::remove_file(&path).ok();

        let error = Registry::with_macros_and_plugins(catalog, &[], &[])
            .expect_err("no builtin, macro, or plugin step matches");
        assert!(error.contains("unknown step"), "{error}");
    }

    #[test]
    fn eventual_modifiers_are_registered_as_actions() {
        let cases = [
            (
                "I expect the next assertion to pass eventually",
                StepId::ExpectEventually,
                vec![],
            ),
            (
                r#"I expect the next assertion to pass within "10" seconds"#,
                StepId::ExpectWithin,
                vec!["10".to_string()],
            ),
            (
                r#"I expect the next assertion to pass within "10" seconds, checking every "100" milliseconds"#,
                StepId::ExpectWithinEvery,
                vec!["10".to_string(), "100".to_string()],
            ),
        ];

        for (text, expected_id, expected_caps) in cases {
            let (target, caps) = reg().find(text).unwrap().expect("step is registered");
            let StepTarget::Builtin { id, kind } = target else {
                panic!("modifier resolved to a macro");
            };
            assert_eq!(id, expected_id);
            assert_eq!(kind, StepKind::Action);
            assert_eq!(caps, expected_caps);
        }
    }

    #[test]
    fn assertions_keep_their_option_source_in_registry_metadata() {
        let cases = [
            ("the response code is 200", OptionsSource::Http),
            (
                r#"variable "state" should be equal to "ready""#,
                OptionsSource::Global,
            ),
            (
                r#"I should have "users" with "state: ready""#,
                OptionsSource::Db,
            ),
        ];

        for (text, source) in cases {
            let (target, _) = reg().find(text).unwrap().expect("assertion is registered");
            let StepTarget::Builtin { kind, .. } = target else {
                panic!("assertion resolved to a macro");
            };
            assert_eq!(kind, StepKind::Assertion(source));
        }
    }

    #[test]
    fn no_two_builtin_steps_are_ambiguous() {
        // Every pattern is checked against all the others on concrete examples.
        let samples = [
            r#"the "Accept" request header is "application/json""#,
            r#"I add "b" to the "Accept" request header"#,
            r#"the query parameter "q" is "1""#,
            "the request body is:",
            "the request body is empty",
            "the request form parameters are:",
            r#"I request "/ping" using HTTP POST"#,
            r#"I request "/ping""#,
            "the response code is 200",
            "the response body contains JSON:",
            "the response body equals JSON:",
            "the response body is a JSON array of length 3",
            r#"the "X-Trace" response header is "abc""#,
            r#"the JSON node "data.id" should exist"#,
            r#"set variable "a" to "1""#,
            r#"set variable "a" to "1" global"#,
            r#"extract "id" from JSON as "userId""#,
            r#"extract "id" from JSON as "userId" global"#,
            r#"extract "jwt" from cookies as "t""#,
            r#"extract "jwt" from cookies as "t" global"#,
            r#"variable "a" should be equal to "1""#,
            r#"variable "a" should not be equal to "1""#,
            r#"I encrypt "555555" with AES using key "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" as "otp""#,
            r#"I use "main" connection"#,
            r#"I use "billing" api"#,
            "I am in debug mode",
            "I am not in debug mode",
            r#"I have "users" where:"#,
            r#"I have "users" with "email: a@b.net""#,
            "I have:",
            r#"I update "companies" with "name: x" where "id: 1""#,
            r#"I delete "companies" where "slug: x""#,
            r#"I delete all "companies""#,
            r#"I extract "id" from "companies" with "slug: x" as "cid""#,
            r#"I call procedure "p" with "a: 1""#,
            r#"I call function "f" with "a: 1" as "r""#,
            r#"I get next value of sequence "s" as "n""#,
            r#"I sleep "5" seconds"#,
            "Show all variables",
            r#"Show "userId" variable"#,
            "Print response headers",
            "Print response body",
            r#"Print response body as "status""#,
            r#"I generate an SRP verifier for "u@example.test" with password "p" as "reg""#,
            r#"I generate an SRP verifier for "u@example.test" with password "p" and salt "ab" as "reg""#,
            r#"I start an SRP login as "srp""#,
            r#"I complete SRP login "srp" for "u@example.test" with password "p" salt "ab" and "cd""#,
            r#"I sign the next request with Hawk id "session-1" and key "abc""#,
        ];
        let r = reg();
        for s in samples {
            match r.find(s) {
                Ok(Some(_)) => {}
                Ok(None) => panic!("step not recognized: {s}"),
                Err(e) => panic!("ambiguity: {e}"),
            }
        }
    }

    #[test]
    fn the_use_api_step_resolves_to_its_builtin() {
        let (target, caps) = reg()
            .find(r#"I use "billing" api"#)
            .expect("pattern is not ambiguous")
            .expect("step is declared");
        assert!(target == StepId::UseApi, "the step must resolve to UseApi");
        assert_eq!(caps, vec!["billing".to_string()]);
    }

    #[test]
    fn the_aes_encryption_step_resolves_to_its_builtin() {
        let (target, caps) = reg()
            .find(
                r#"I encrypt "555555" with AES using key "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" as "otp""#,
            )
            .expect("pattern is unambiguous")
            .expect("step is declared");

        assert!(target == StepId::EncryptWithAes);
        assert_eq!(
            caps,
            vec![
                "555555",
                "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
                "otp",
            ]
        );
    }

    #[test]
    fn the_sign_next_request_with_hawk_step_resolves_to_its_builtin() {
        let (target, caps) = reg()
            .find(r#"I sign the next request with Hawk id "session-1" and key "abc""#)
            .expect("pattern is not ambiguous")
            .expect("step is declared");
        assert!(
            target == StepId::SignNextRequestWithHawk,
            "the step must resolve to SignNextRequestWithHawk"
        );
        assert_eq!(caps, vec!["session-1".to_string(), "abc".to_string()]);
    }

    #[test]
    fn captures_path_and_method() {
        let (id, caps) = reg()
            .find(r#"I request "/api/v1/x" using HTTP PATCH"#)
            .unwrap()
            .unwrap();
        assert_eq!(id, StepId::RequestPathWithMethod);
        assert_eq!(caps, vec!["/api/v1/x".to_string(), "PATCH".to_string()]);
    }

    #[test]
    fn bare_request_defaults_to_get_variant() {
        let (id, caps) = reg().find(r#"I request "/ping""#).unwrap().unwrap();
        assert_eq!(id, StepId::RequestPath);
        assert_eq!(caps, vec!["/ping".to_string()]);
    }

    #[test]
    fn global_suffix_picks_the_global_variant() {
        let (id, _) = reg()
            .find(r#"set variable "a" to "1" global"#)
            .unwrap()
            .unwrap();
        assert_eq!(id, StepId::SetVariableGlobal);
        let (id, _) = reg().find(r#"set variable "a" to "1""#).unwrap().unwrap();
        assert_eq!(id, StepId::SetVariable);
    }

    #[test]
    fn unknown_method_is_not_a_known_step() {
        assert!(
            reg()
                .find(r#"I request "/ping" using HTTP POSTT"#)
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn unknown_step_returns_none() {
        assert!(reg().find("I refund the order").unwrap().is_none());
    }
}