tarn 0.6.2

CLI-first API testing tool
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
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Source location pointing at a YAML node (a step's `name:` key or an
/// assertion operator key). All fields are 1-based so they line up with
/// what editors and JSON reports already use elsewhere.
///
/// The field name and shape are fixed by the public JSON report schema and
/// consumed by the VS Code extension via `schemaGuards.ts`; do not rename.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Location {
    /// Absolute path of the source file (matches `FileResult.file`).
    pub file: String,
    /// 1-based line number.
    pub line: usize,
    /// 1-based column number.
    pub column: usize,
}

/// Runtime HTTP transport settings shared by run and bench commands.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct HttpTransportConfig {
    pub proxy: Option<String>,
    pub no_proxy: Option<String>,
    pub cacert: Option<String>,
    pub cert: Option<String>,
    pub key: Option<String>,
    pub insecure: bool,
    pub http_version: Option<HttpVersionPreference>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HttpVersionPreference {
    Http1_1,
    Http2,
}

impl HttpTransportConfig {
    /// Merge project defaults with CLI overrides. CLI wins when provided.
    pub fn merge(project: &Self, cli: &Self) -> Self {
        Self {
            proxy: cli.proxy.clone().or_else(|| project.proxy.clone()),
            no_proxy: cli.no_proxy.clone().or_else(|| project.no_proxy.clone()),
            cacert: cli.cacert.clone().or_else(|| project.cacert.clone()),
            cert: cli.cert.clone().or_else(|| project.cert.clone()),
            key: cli.key.clone().or_else(|| project.key.clone()),
            insecure: cli.insecure || project.insecure,
            http_version: cli.http_version.or(project.http_version),
        }
    }

    pub fn has_custom_transport(&self) -> bool {
        self.proxy.is_some()
            || self.no_proxy.is_some()
            || self.cacert.is_some()
            || self.cert.is_some()
            || self.key.is_some()
            || self.insecure
            || self.http_version.is_some()
    }
}

#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct RedactionConfig {
    #[serde(default = "default_redacted_headers")]
    pub headers: Vec<String>,
    #[serde(default = "default_redaction_replacement")]
    pub replacement: String,
    #[serde(default, rename = "env")]
    pub env_vars: Vec<String>,
    #[serde(default)]
    pub captures: Vec<String>,
}

impl Default for RedactionConfig {
    fn default() -> Self {
        Self {
            headers: default_redacted_headers(),
            replacement: default_redaction_replacement(),
            env_vars: Vec::new(),
            captures: Vec::new(),
        }
    }
}

impl RedactionConfig {
    /// Append extra header names to the effective redaction list without
    /// removing any existing entries. All names are normalized to lowercase
    /// so header matching stays case-insensitive, and duplicates (by
    /// lowercase comparison) are skipped so the list stays tidy.
    ///
    /// This is the single merge point used by the `--redact-header` CLI
    /// flag: callers can widen an already-resolved `RedactionConfig`
    /// (from defaults + `tarn.config.yaml` + test file) without mutating
    /// any persisted configuration.
    pub fn merge_headers<I, S>(&mut self, extra: I)
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        for name in extra {
            let trimmed = name.as_ref().trim();
            if trimmed.is_empty() {
                continue;
            }
            let normalized = trimmed.to_ascii_lowercase();
            if !self
                .headers
                .iter()
                .any(|existing| existing.eq_ignore_ascii_case(&normalized))
            {
                self.headers.push(normalized);
            }
        }
    }
}

fn default_redacted_headers() -> Vec<String> {
    vec![
        "authorization".into(),
        "cookie".into(),
        "set-cookie".into(),
        "x-api-key".into(),
        "x-auth-token".into(),
    ]
}

fn default_redaction_replacement() -> String {
    "***".into()
}

/// Top-level test file structure matching .tarn.yaml format.
///
/// Supports two modes:
/// 1. Simple (flat steps): `steps:` at the top level
/// 2. Full (named tests): `tests:` map with named test groups
#[derive(Debug, Deserialize, Clone)]
pub struct TestFile {
    /// Schema version (optional, defaults to "1")
    pub version: Option<String>,

    /// Human-readable name for this test file
    pub name: String,

    /// Optional description
    pub description: Option<String>,

    /// Tags for filtering
    #[serde(default)]
    pub tags: Vec<String>,

    /// Inline environment variables with defaults
    #[serde(default)]
    pub env: HashMap<String, String>,

    /// Report-time redaction settings for sensitive headers
    #[serde(alias = "redact")]
    pub redaction: Option<RedactionConfig>,

    /// Default headers/settings applied to every request
    pub defaults: Option<Defaults>,

    /// Setup steps run once before all tests
    #[serde(default)]
    pub setup: Vec<Step>,

    /// Teardown steps run once after all tests (even on failure)
    #[serde(default)]
    pub teardown: Vec<Step>,

    /// Named test groups (full format)
    #[serde(default)]
    pub tests: IndexMap<String, TestGroup>,

    /// Flat steps (simple format — mutually exclusive with `tests`)
    #[serde(default)]
    pub steps: Vec<Step>,

    /// Cookie handling mode: "auto" (default), "off", or "per-test"
    #[serde(default)]
    pub cookies: Option<CookieMode>,
}

/// File-level cookie handling mode.
///
/// - `Auto` (default) — single file-scoped jar shared across setup, tests, teardown.
/// - `Off` — cookies disabled entirely for the file.
/// - `PerTest` — the default jar is cleared between named tests so subset runs
///   and flaky suites never see session state from a prior test. Setup and
///   teardown still share the file-level jar. Named jars are unaffected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CookieMode {
    #[default]
    Auto,
    Off,
    PerTest,
}

impl<'de> Deserialize<'de> for CookieMode {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        match value.as_str() {
            "auto" => Ok(CookieMode::Auto),
            "off" => Ok(CookieMode::Off),
            "per-test" => Ok(CookieMode::PerTest),
            other => Err(serde::de::Error::custom(format!(
                "cookies must be \"auto\", \"off\", or \"per-test\" (got \"{}\")",
                other
            ))),
        }
    }
}

/// A named group of test steps.
#[derive(Debug, Deserialize, Clone)]
pub struct TestGroup {
    pub description: Option<String>,

    #[serde(default)]
    pub tags: Vec<String>,

    #[serde(default)]
    pub steps: Vec<Step>,
}

/// A single test step: one HTTP request + optional capture + assertions.
#[derive(Debug, Deserialize, Clone)]
pub struct Step {
    pub name: String,
    pub request: Request,

    /// Captures from the response (JSONPath or header with optional regex)
    #[serde(default)]
    pub capture: HashMap<String, CaptureSpec>,

    /// Assertions on the response
    #[serde(rename = "assert")]
    pub assertions: Option<Assertion>,

    /// Number of retries on failure (0 = no retries)
    #[serde(default)]
    pub retries: Option<u32>,

    /// Step-level timeout in milliseconds (overrides defaults)
    pub timeout: Option<u64>,

    /// Step-level connect timeout in milliseconds (overrides defaults)
    #[serde(alias = "connect-timeout")]
    pub connect_timeout: Option<u64>,

    /// Whether this step should follow redirects (overrides defaults)
    #[serde(alias = "follow-redirects")]
    pub follow_redirects: Option<bool>,

    /// Maximum redirects to follow for this step (overrides defaults)
    #[serde(alias = "max-redirs")]
    pub max_redirs: Option<u32>,

    /// Delay before executing this step (e.g., "500ms", "2s")
    pub delay: Option<String>,

    /// Polling configuration: re-execute until condition is met
    pub poll: Option<PollConfig>,

    /// Lua script to run after HTTP response for custom validation
    pub script: Option<String>,

    /// Per-step cookie control:
    /// - omitted or `true`: use the default cookie jar
    /// - `false`: skip cookies entirely for this step
    /// - `"jar-name"`: use a named cookie jar (for multi-user scenarios)
    pub cookies: Option<StepCookies>,

    /// Source location of the step's `name:` node in the original YAML.
    /// Populated by `parser::parse_str` after deserialization so downstream
    /// consumers can anchor runtime results on the exact source range.
    #[serde(skip)]
    pub location: Option<Location>,

    /// Source locations of individual assertion keys, indexed by the same
    /// string used in `AssertionResult::assertion` (e.g. `"status"`,
    /// `"duration"`, `"headers.content-type"`, `"body $.name"`).
    /// Populated by `parser::parse_str` after deserialization.
    #[serde(skip)]
    pub assertion_locations: HashMap<String, Location>,
}

/// Step-level cookie control.
#[derive(Debug, Clone, PartialEq)]
pub enum StepCookies {
    /// Enable (true) or disable (false) the default cookie jar.
    Enabled(bool),
    /// Use a named cookie jar.
    Named(String),
}

impl<'de> Deserialize<'de> for StepCookies {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = serde_yaml::Value::deserialize(deserializer)?;
        match value {
            serde_yaml::Value::Bool(b) => Ok(StepCookies::Enabled(b)),
            serde_yaml::Value::String(s) => Ok(StepCookies::Named(s)),
            _ => Err(serde::de::Error::custom(
                "cookies must be true, false, or a jar name string",
            )),
        }
    }
}

/// Capture specification: either a simple JSONPath string or an extended capture.
#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
pub enum CaptureSpec {
    /// Simple JSONPath: "$.token"
    JsonPath(String),
    /// Extended capture: from header or JSONPath with optional regex
    Extended(ExtendedCapture),
}

/// Extended capture specification supporting multiple response sources with optional regex extraction.
#[derive(Debug, Deserialize, Clone)]
pub struct ExtendedCapture {
    /// Capture from a response header (case-insensitive lookup)
    pub header: Option<String>,
    /// Capture from a response cookie by cookie name
    pub cookie: Option<String>,
    /// Capture from body via JSONPath (explicit form)
    pub jsonpath: Option<String>,
    /// Capture from the whole response body string
    pub body: Option<bool>,
    /// Capture from the HTTP response status code
    pub status: Option<bool>,
    /// Capture from the final response URL after redirects
    pub url: Option<bool>,
    /// Optional regex to extract a sub-match (capture group 1)
    pub regex: Option<String>,
}

/// Polling configuration: re-execute step until a condition is met.
#[derive(Debug, Deserialize, Clone)]
pub struct PollConfig {
    /// Assertions that must pass for polling to stop
    pub until: Assertion,
    /// Time between attempts (e.g., "2s", "500ms")
    pub interval: String,
    /// Maximum number of polling attempts
    pub max_attempts: u32,
}

/// HTTP request definition.
#[derive(Debug, Deserialize, Clone)]
pub struct Request {
    pub method: String,
    pub url: String,

    #[serde(default)]
    pub headers: HashMap<String, String>,

    /// Optional auth helper. Explicit Authorization headers still win.
    pub auth: Option<AuthConfig>,

    /// Request body — can be any JSON-compatible value
    pub body: Option<serde_json::Value>,

    /// URL-encoded form body sent as application/x-www-form-urlencoded
    #[serde(default)]
    pub form: Option<IndexMap<String, String>>,

    /// GraphQL query (syntactic sugar; translates to JSON POST body)
    pub graphql: Option<GraphqlRequest>,

    /// Multipart form data for file uploads
    pub multipart: Option<MultipartBody>,
}

/// First-class auth helper for common bearer/basic cases.
#[derive(Debug, Deserialize, Clone)]
pub struct AuthConfig {
    /// Bearer token value (without the `Bearer ` prefix)
    pub bearer: Option<String>,
    /// Basic auth credentials
    pub basic: Option<BasicAuthConfig>,
}

/// Basic auth credentials.
#[derive(Debug, Deserialize, Clone)]
pub struct BasicAuthConfig {
    pub username: String,
    pub password: String,
}

/// GraphQL query definition.
#[derive(Debug, Deserialize, Clone)]
pub struct GraphqlRequest {
    /// The GraphQL query or mutation string
    pub query: String,
    /// Variables to pass to the query
    #[serde(default)]
    pub variables: Option<serde_json::Value>,
    /// Operation name (when query contains multiple operations)
    pub operation_name: Option<String>,
}

/// Multipart form data body for file uploads.
#[derive(Debug, Deserialize, Clone)]
pub struct MultipartBody {
    /// Text form fields
    #[serde(default)]
    pub fields: Vec<FormField>,
    /// File upload fields
    #[serde(default)]
    pub files: Vec<FileField>,
}

/// A text field in a multipart form.
#[derive(Debug, Deserialize, Clone)]
pub struct FormField {
    pub name: String,
    pub value: String,
}

/// A file field in a multipart form.
#[derive(Debug, Deserialize, Clone)]
pub struct FileField {
    /// Form field name
    pub name: String,
    /// Path to the file (relative to test file)
    pub path: String,
    /// MIME content type (e.g., "image/jpeg")
    pub content_type: Option<String>,
    /// Override filename sent in the form
    pub filename: Option<String>,
}

/// Default settings applied to every request in a file.
#[derive(Debug, Deserialize, Clone)]
pub struct Defaults {
    #[serde(default)]
    pub headers: HashMap<String, String>,

    /// Default auth helper applied when a step does not set request.auth.
    pub auth: Option<AuthConfig>,

    /// Default timeout in milliseconds
    pub timeout: Option<u64>,

    /// Default connect timeout in milliseconds
    #[serde(alias = "connect-timeout")]
    pub connect_timeout: Option<u64>,

    /// Default redirect-following behavior
    #[serde(alias = "follow-redirects")]
    pub follow_redirects: Option<bool>,

    /// Default maximum redirects to follow
    #[serde(alias = "max-redirs")]
    pub max_redirs: Option<u32>,

    /// Default retries for all steps
    pub retries: Option<u32>,

    /// Default delay before each request (e.g., "100ms", "1s")
    pub delay: Option<String>,
}

/// Assertion block for a step.
#[derive(Debug, Deserialize, Clone)]
pub struct Assertion {
    /// Expected HTTP status code (exact, shorthand range, or complex)
    pub status: Option<StatusAssertion>,

    /// Response time assertion (e.g., "< 500ms")
    pub duration: Option<String>,

    /// Redirect assertions against the final response URL and redirect count
    pub redirect: Option<RedirectAssertion>,

    /// Header assertions
    pub headers: Option<HashMap<String, String>>,

    /// Body assertions via JSONPath
    pub body: Option<IndexMap<String, serde_yaml::Value>>,
}

/// Redirect assertions for a followed response chain.
#[derive(Debug, Deserialize, Clone)]
pub struct RedirectAssertion {
    /// Expected final URL after redirects
    pub url: Option<String>,
    /// Expected number of followed redirects
    pub count: Option<u32>,
}

/// Status code assertion: exact match, shorthand range, or complex spec.
#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
pub enum StatusAssertion {
    /// Exact match: `status: 200`
    Exact(u16),
    /// Shorthand range: `status: "2xx"`, `status: "4xx"`
    Shorthand(String),
    /// Complex: `status: { in: [200, 201] }` or `status: { gte: 400, lt: 500 }`
    Complex(StatusSpec),
}

/// Complex status code specification with ranges and sets.
#[derive(Debug, Deserialize, Clone)]
pub struct StatusSpec {
    /// Set of allowed status codes
    #[serde(rename = "in")]
    pub in_set: Option<Vec<u16>>,
    /// Greater than or equal
    pub gte: Option<u16>,
    /// Greater than
    pub gt: Option<u16>,
    /// Less than or equal
    pub lte: Option<u16>,
    /// Less than
    pub lt: Option<u16>,
}

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

    #[test]
    fn deserialize_minimal_test_file() {
        let yaml = r#"
name: Health check
steps:
  - name: GET /health
    request:
      method: GET
      url: "http://localhost:3000/health"
    assert:
      status: 200
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.name, "Health check");
        assert_eq!(tf.steps.len(), 1);
        assert_eq!(tf.steps[0].name, "GET /health");
        assert_eq!(tf.steps[0].request.method, "GET");
        assert_eq!(tf.steps[0].request.url, "http://localhost:3000/health");
        assert!(matches!(
            tf.steps[0].assertions.as_ref().unwrap().status,
            Some(StatusAssertion::Exact(200))
        ));
    }

    #[test]
    fn deserialize_full_test_file() {
        let yaml = r#"
version: "1"
name: "User CRUD"
description: "Tests CRUD lifecycle"
tags: [crud, users]
env:
  base_url: "http://localhost:3000"
defaults:
  headers:
    Content-Type: "application/json"
  timeout: 5000
setup:
  - name: Auth
    request:
      method: POST
      url: "http://localhost:3000/auth"
      body:
        email: "admin@test.com"
    capture:
      token: "$.token"
    assert:
      status: 200
teardown:
  - name: Cleanup
    request:
      method: POST
      url: "http://localhost:3000/cleanup"
tests:
  create_user:
    description: "Create a user"
    tags: [smoke]
    steps:
      - name: Create
        request:
          method: POST
          url: "http://localhost:3000/users"
          headers:
            Authorization: "Bearer token"
          body:
            name: "Jane"
        capture:
          user_id: "$.id"
        assert:
          status: 201
          duration: "< 500ms"
          headers:
            content-type: contains "application/json"
          body:
            "$.name": "Jane"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.version, Some("1".into()));
        assert_eq!(tf.name, "User CRUD");
        assert_eq!(tf.description, Some("Tests CRUD lifecycle".into()));
        assert_eq!(tf.tags, vec!["crud", "users"]);
        assert_eq!(tf.env.get("base_url").unwrap(), "http://localhost:3000");

        // Defaults
        let defaults = tf.defaults.as_ref().unwrap();
        assert_eq!(
            defaults.headers.get("Content-Type").unwrap(),
            "application/json"
        );
        assert_eq!(defaults.timeout, Some(5000));

        // Setup
        assert_eq!(tf.setup.len(), 1);
        assert_eq!(tf.setup[0].name, "Auth");
        assert!(matches!(
            tf.setup[0].capture.get("token"),
            Some(CaptureSpec::JsonPath(p)) if p == "$.token"
        ));

        // Teardown
        assert_eq!(tf.teardown.len(), 1);

        // Tests
        assert_eq!(tf.tests.len(), 1);
        let test = tf.tests.get("create_user").unwrap();
        assert_eq!(test.description, Some("Create a user".into()));
        assert_eq!(test.tags, vec!["smoke"]);
        assert_eq!(test.steps.len(), 1);

        let step = &test.steps[0];
        assert_eq!(step.name, "Create");
        assert_eq!(step.request.method, "POST");
        assert!(step.request.body.is_some());
        assert!(matches!(
            step.capture.get("user_id"),
            Some(CaptureSpec::JsonPath(p)) if p == "$.id"
        ));

        let assertions = step.assertions.as_ref().unwrap();
        assert!(matches!(
            assertions.status,
            Some(StatusAssertion::Exact(201))
        ));
        assert_eq!(assertions.duration, Some("< 500ms".into()));
        assert!(assertions.headers.is_some());
        assert!(assertions.body.is_some());
    }

    #[test]
    fn deserialize_step_without_assertions() {
        let yaml = r#"
name: Fire and forget
steps:
  - name: Trigger webhook
    request:
      method: POST
      url: "http://localhost:3000/webhook"
      body:
        event: "deploy"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.steps.len(), 1);
        assert!(tf.steps[0].assertions.is_none());
    }

    #[test]
    fn deserialize_redirect_assertion() {
        let yaml = r#"
name: Redirect assertions
steps:
  - name: Follow chain
    request:
      method: GET
      url: "http://localhost:3000/redirect"
    assert:
      redirect:
        url: "http://localhost:3000/final"
        count: 2
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let redirect = tf.steps[0]
            .assertions
            .as_ref()
            .and_then(|a| a.redirect.as_ref())
            .unwrap();
        assert_eq!(redirect.url.as_deref(), Some("http://localhost:3000/final"));
        assert_eq!(redirect.count, Some(2));
    }

    #[test]
    fn deserialize_empty_optional_fields() {
        let yaml = r#"
name: Minimal
steps:
  - name: Simple GET
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert!(tf.version.is_none());
        assert!(tf.description.is_none());
        assert!(tf.tags.is_empty());
        assert!(tf.env.is_empty());
        assert!(tf.defaults.is_none());
        assert!(tf.setup.is_empty());
        assert!(tf.teardown.is_empty());
        assert!(tf.tests.is_empty());
    }

    #[test]
    fn deserialize_request_with_headers_and_body() {
        let yaml = r#"
name: test
steps:
  - name: POST with JSON body
    request:
      method: POST
      url: "http://localhost:3000/users"
      headers:
        Authorization: "Bearer xyz"
        X-Custom: "hello"
      body:
        name: "Alice"
        tags: ["a", "b"]
        nested:
          key: "value"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let req = &tf.steps[0].request;
        assert_eq!(req.headers.get("Authorization").unwrap(), "Bearer xyz");
        assert_eq!(req.headers.get("X-Custom").unwrap(), "hello");

        let body = req.body.as_ref().unwrap();
        assert_eq!(body["name"], "Alice");
        assert_eq!(body["tags"][0], "a");
        assert_eq!(body["nested"]["key"], "value");
    }

    #[test]
    fn deserialize_request_with_auth_helper() {
        let yaml = r#"
name: auth
steps:
  - name: GET
    request:
      method: GET
      url: "http://localhost:3000/me"
      auth:
        bearer: "{{ env.token }}"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let auth = tf.steps[0].request.auth.as_ref().unwrap();
        assert_eq!(auth.bearer.as_deref(), Some("{{ env.token }}"));
        assert!(auth.basic.is_none());
    }

    #[test]
    fn deserialize_defaults_with_basic_auth_helper() {
        let yaml = r#"
name: auth
defaults:
  auth:
    basic:
      username: "demo"
      password: "{{ env.password }}"
steps:
  - name: GET
    request:
      method: GET
      url: "http://localhost:3000/me"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let auth = tf.defaults.as_ref().unwrap().auth.as_ref().unwrap();
        let basic = auth.basic.as_ref().unwrap();
        assert_eq!(basic.username, "demo");
        assert_eq!(basic.password, "{{ env.password }}");
    }

    #[test]
    fn tests_preserve_insertion_order() {
        let yaml = r#"
name: Order test
tests:
  third_test:
    steps:
      - name: step
        request:
          method: GET
          url: "http://localhost:3000"
  first_test:
    steps:
      - name: step
        request:
          method: GET
          url: "http://localhost:3000"
  second_test:
    steps:
      - name: step
        request:
          method: GET
          url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let keys: Vec<&String> = tf.tests.keys().collect();
        assert_eq!(keys, vec!["third_test", "first_test", "second_test"]);
    }

    #[test]
    fn deserialize_body_assertions_with_various_types() {
        let yaml = r#"
name: Assertion types
steps:
  - name: Check types
    request:
      method: GET
      url: "http://localhost:3000"
    assert:
      status: 200
      body:
        "$.string": "hello"
        "$.number": 42
        "$.bool": true
        "$.null_field": null
        "$.complex":
          type: string
          contains: "sub"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let body = tf.steps[0]
            .assertions
            .as_ref()
            .unwrap()
            .body
            .as_ref()
            .unwrap();
        assert_eq!(body.len(), 5);
        assert!(body.contains_key("$.string"));
        assert!(body.contains_key("$.number"));
        assert!(body.contains_key("$.bool"));
        assert!(body.contains_key("$.null_field"));
        assert!(body.contains_key("$.complex"));
    }

    // --- New tests for extended captures ---

    #[test]
    fn deserialize_header_capture() {
        let yaml = r#"
name: Header capture test
steps:
  - name: Login
    request:
      method: POST
      url: "http://localhost:3000/login"
    capture:
      session_token:
        header: "set-cookie"
        regex: "session_token=([^;]+)"
      user_id: "$.id"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let cap = &tf.steps[0].capture;
        assert!(matches!(cap.get("user_id"), Some(CaptureSpec::JsonPath(p)) if p == "$.id"));
        match cap.get("session_token") {
            Some(CaptureSpec::Extended(ext)) => {
                assert_eq!(ext.header.as_deref(), Some("set-cookie"));
                assert_eq!(ext.cookie, None);
                assert_eq!(ext.body, None);
                assert_eq!(ext.status, None);
                assert_eq!(ext.url, None);
                assert_eq!(ext.regex.as_deref(), Some("session_token=([^;]+)"));
            }
            other => panic!("Expected Extended capture, got {:?}", other),
        }
    }

    #[test]
    fn deserialize_status_capture() {
        let yaml = r#"
name: Status capture test
steps:
  - name: Health
    request:
      method: GET
      url: "http://localhost:3000/health"
    capture:
      status_code:
        status: true
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let cap = &tf.steps[0].capture;
        match cap.get("status_code") {
            Some(CaptureSpec::Extended(ext)) => {
                assert_eq!(ext.header, None);
                assert_eq!(ext.cookie, None);
                assert_eq!(ext.jsonpath, None);
                assert_eq!(ext.body, None);
                assert_eq!(ext.status, Some(true));
                assert_eq!(ext.url, None);
                assert_eq!(ext.regex, None);
            }
            other => panic!("Expected Extended capture, got {:?}", other),
        }
    }

    #[test]
    fn deserialize_url_capture() {
        let yaml = r#"
name: URL capture test
steps:
  - name: Follow redirect
    request:
      method: GET
      url: "http://localhost:3000/redirect"
    capture:
      final_url:
        url: true
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let cap = &tf.steps[0].capture;
        match cap.get("final_url") {
            Some(CaptureSpec::Extended(ext)) => {
                assert_eq!(ext.header, None);
                assert_eq!(ext.cookie, None);
                assert_eq!(ext.jsonpath, None);
                assert_eq!(ext.body, None);
                assert_eq!(ext.status, None);
                assert_eq!(ext.url, Some(true));
                assert_eq!(ext.regex, None);
            }
            other => panic!("Expected Extended capture, got {:?}", other),
        }
    }

    #[test]
    fn deserialize_cookie_and_body_capture() {
        let yaml = r#"
name: Cookie capture test
steps:
  - name: Cookies
    request:
      method: GET
      url: "http://localhost:3000/cookies"
    capture:
      session_cookie:
        cookie: "session"
      body_word:
        body: true
        regex: "plain (text)"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let cap = &tf.steps[0].capture;
        match cap.get("session_cookie") {
            Some(CaptureSpec::Extended(ext)) => {
                assert_eq!(ext.header, None);
                assert_eq!(ext.cookie.as_deref(), Some("session"));
                assert_eq!(ext.jsonpath, None);
                assert_eq!(ext.body, None);
                assert_eq!(ext.status, None);
                assert_eq!(ext.url, None);
                assert_eq!(ext.regex, None);
            }
            other => panic!("Expected cookie Extended capture, got {:?}", other),
        }
        match cap.get("body_word") {
            Some(CaptureSpec::Extended(ext)) => {
                assert_eq!(ext.header, None);
                assert_eq!(ext.cookie, None);
                assert_eq!(ext.jsonpath, None);
                assert_eq!(ext.body, Some(true));
                assert_eq!(ext.status, None);
                assert_eq!(ext.url, None);
                assert_eq!(ext.regex.as_deref(), Some("plain (text)"));
            }
            other => panic!("Expected body Extended capture, got {:?}", other),
        }
    }

    // --- New tests for status assertion variants ---

    #[test]
    fn deserialize_status_shorthand() {
        let yaml = r#"
name: Status range
steps:
  - name: Check 2xx
    request:
      method: GET
      url: "http://localhost:3000"
    assert:
      status: "2xx"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert!(matches!(
            tf.steps[0].assertions.as_ref().unwrap().status,
            Some(StatusAssertion::Shorthand(ref s)) if s == "2xx"
        ));
    }

    #[test]
    fn deserialize_status_complex_in() {
        let yaml = r#"
name: Status set
steps:
  - name: Check set
    request:
      method: GET
      url: "http://localhost:3000"
    assert:
      status:
        in: [200, 201, 204]
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        match &tf.steps[0].assertions.as_ref().unwrap().status {
            Some(StatusAssertion::Complex(spec)) => {
                assert_eq!(spec.in_set.as_ref().unwrap(), &vec![200, 201, 204]);
            }
            other => panic!("Expected Complex status, got {:?}", other),
        }
    }

    #[test]
    fn deserialize_status_complex_range() {
        let yaml = r#"
name: Status range
steps:
  - name: Check 4xx range
    request:
      method: GET
      url: "http://localhost:3000"
    assert:
      status:
        gte: 400
        lt: 500
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        match &tf.steps[0].assertions.as_ref().unwrap().status {
            Some(StatusAssertion::Complex(spec)) => {
                assert_eq!(spec.gte, Some(400));
                assert_eq!(spec.lt, Some(500));
            }
            other => panic!("Expected Complex status, got {:?}", other),
        }
    }

    // --- Multipart ---

    #[test]
    fn deserialize_multipart_request() {
        let yaml = r#"
name: Upload test
steps:
  - name: Upload photo
    request:
      method: POST
      url: "http://localhost:3000/upload"
      multipart:
        fields:
          - name: "title"
            value: "My Photo"
        files:
          - name: "photo"
            path: "./fixtures/test.jpg"
            content_type: "image/jpeg"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let mp = tf.steps[0].request.multipart.as_ref().unwrap();
        assert_eq!(mp.fields.len(), 1);
        assert_eq!(mp.fields[0].name, "title");
        assert_eq!(mp.fields[0].value, "My Photo");
        assert_eq!(mp.files.len(), 1);
        assert_eq!(mp.files[0].name, "photo");
        assert_eq!(mp.files[0].path, "./fixtures/test.jpg");
        assert_eq!(mp.files[0].content_type.as_deref(), Some("image/jpeg"));
    }

    #[test]
    fn deserialize_form_request() {
        let yaml = r#"
name: Form test
steps:
  - name: Submit form
    request:
      method: POST
      url: "http://localhost:3000/login"
      form:
        email: "user@example.com"
        password: "secret"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let form = tf.steps[0].request.form.as_ref().unwrap();
        assert_eq!(
            form.get("email").map(String::as_str),
            Some("user@example.com")
        );
        assert_eq!(form.get("password").map(String::as_str), Some("secret"));
    }

    // --- Default delay ---

    #[test]
    fn deserialize_defaults_with_delay() {
        let yaml = r#"
name: Delay test
defaults:
  delay: "100ms"
  timeout: 5000
steps:
  - name: test
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(
            tf.defaults.as_ref().unwrap().delay.as_deref(),
            Some("100ms")
        );
    }

    // --- Cookies ---

    #[test]
    fn deserialize_step_cookies_false() {
        let yaml = r#"
name: Step cookies test
steps:
  - name: No cookies step
    cookies: false
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.steps[0].cookies, Some(StepCookies::Enabled(false)));
    }

    #[test]
    fn deserialize_step_cookies_true() {
        let yaml = r#"
name: Step cookies test
steps:
  - name: With cookies
    cookies: true
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.steps[0].cookies, Some(StepCookies::Enabled(true)));
    }

    #[test]
    fn deserialize_step_cookies_named_jar() {
        let yaml = r#"
name: Step cookies test
steps:
  - name: Admin step
    cookies: "admin"
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(
            tf.steps[0].cookies,
            Some(StepCookies::Named("admin".to_string()))
        );
    }

    #[test]
    fn deserialize_step_cookies_default_none() {
        let yaml = r#"
name: Step cookies test
steps:
  - name: Default step
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.steps[0].cookies, None);
    }

    #[test]
    fn deserialize_cookies_off() {
        let yaml = r#"
name: No cookies
cookies: "off"
steps:
  - name: test
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.cookies, Some(CookieMode::Off));
    }

    #[test]
    fn deserialize_cookies_auto() {
        let yaml = r#"
name: Auto cookies
cookies: "auto"
steps:
  - name: test
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.cookies, Some(CookieMode::Auto));
    }

    #[test]
    fn deserialize_cookies_per_test() {
        let yaml = r#"
name: Per-test cookies
cookies: "per-test"
tests:
  login:
    steps:
      - name: test
        request:
          method: GET
          url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.cookies, Some(CookieMode::PerTest));
    }

    #[test]
    fn deserialize_cookies_invalid_value_is_rejected() {
        let yaml = r#"
name: Bad cookies
cookies: "sometimes"
steps:
  - name: test
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let err = serde_yaml::from_str::<TestFile>(yaml).unwrap_err();
        assert!(
            err.to_string().contains("per-test"),
            "error should mention the valid options, got: {err}"
        );
    }

    #[test]
    fn deserialize_cookies_default_is_none() {
        let yaml = r#"
name: Default cookies
steps:
  - name: test
    request:
      method: GET
      url: "http://localhost:3000"
"#;
        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(tf.cookies, None);
    }

    #[test]
    fn deserialize_redaction_config() {
        let yaml = r#"
name: Redaction config
redaction:
  headers:
    - authorization
    - x-session-token
  env:
    - api_token
  captures:
    - session
  replacement: "[redacted]"
steps:
  - name: test
    request:
      method: GET
      url: "http://localhost:3000"
"#;

        let tf: TestFile = serde_yaml::from_str(yaml).unwrap();
        let redaction = tf.redaction.unwrap();
        assert_eq!(redaction.headers, vec!["authorization", "x-session-token"]);
        assert_eq!(redaction.env_vars, vec!["api_token"]);
        assert_eq!(redaction.captures, vec!["session"]);
        assert_eq!(redaction.replacement, "[redacted]");
    }

    #[test]
    fn merge_headers_widens_list_case_insensitively() {
        let mut redaction = RedactionConfig {
            headers: vec!["authorization".into()],
            ..RedactionConfig::default()
        };
        redaction.merge_headers(["X-Custom-Token", "x-debug"]);
        assert_eq!(
            redaction.headers,
            vec!["authorization", "x-custom-token", "x-debug"]
        );
    }

    #[test]
    fn merge_headers_skips_duplicates_ignoring_case() {
        let mut redaction = RedactionConfig {
            headers: vec!["authorization".into(), "x-api-key".into()],
            ..RedactionConfig::default()
        };
        redaction.merge_headers(["Authorization", "X-API-KEY", "x-new"]);
        assert_eq!(
            redaction.headers,
            vec!["authorization", "x-api-key", "x-new"]
        );
    }

    #[test]
    fn merge_headers_trims_and_drops_empty_entries() {
        let mut redaction = RedactionConfig::default();
        let baseline_len = redaction.headers.len();
        redaction.merge_headers(["", "   ", "  X-Trim  "]);
        assert_eq!(redaction.headers.len(), baseline_len + 1);
        assert!(redaction.headers.iter().any(|h| h == "x-trim"));
    }

    #[test]
    fn merge_headers_never_narrows_existing_list() {
        let mut redaction = RedactionConfig {
            headers: vec!["authorization".into(), "cookie".into()],
            ..RedactionConfig::default()
        };
        // Empty merge must not drop anything.
        redaction.merge_headers(std::iter::empty::<String>());
        assert_eq!(redaction.headers, vec!["authorization", "cookie"]);
    }
}