agent-first-http 0.4.2

Persistent HTTP client for AI agents — one request, one JSON line
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
#!/usr/bin/env python3
"""
CLI mode correctness tests for afhttp.
Starts a local test server, runs afhttp CLI commands, validates JSON output.
"""

import json
import os
import subprocess
import sys
import time
import base64
import tempfile

sys.path.insert(0, os.path.dirname(__file__))
from server import start_server

AFHTTP = os.environ.get("AFHTTP_BIN") or os.path.join(os.path.dirname(__file__), "..", "target", "debug", "afhttp")
HTTP_PORT = int(os.environ.get("AFH_TEST_HTTP_PORT", "18080"))
BASE = f"http://127.0.0.1:{HTTP_PORT}"
_AFH_VERSION = os.environ.get("AFH_VERSION")

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def run_cli(args: list[str], timeout_s=30, stdin_data=None) -> tuple[list[dict], int]:
    """Run afhttp with CLI args, return (parsed_output_lines, exit_code)."""
    proc = subprocess.run(
        [AFHTTP] + args,
        input=stdin_data,
        capture_output=True,
        text=True,
        timeout=timeout_s,
    )
    if proc.stderr.strip():
        raise RuntimeError(f"afhttp wrote to stderr: {proc.stderr[:800]}")
    lines = []
    for line in proc.stdout.strip().split("\n"):
        line = line.strip()
        if line:
            try:
                lines.append(json.loads(line))
            except json.JSONDecodeError:
                lines.append({"_raw": line})
    return lines, proc.returncode


def afh_version() -> str:
    global _AFH_VERSION
    if _AFH_VERSION:
        return _AFH_VERSION
    proc = subprocess.run([AFHTTP, "--version"], capture_output=True, text=True, timeout=5)
    if proc.returncode != 0:
        raise RuntimeError(f"afhttp --version failed: {proc.stderr.strip()}")
    tokens = proc.stdout.strip().split()
    if not tokens:
        raise RuntimeError("afhttp --version returned empty output")
    _AFH_VERSION = tokens[-1]
    return _AFH_VERSION


def find_by_code(outputs, code):
    return [o for o in outputs if o.get("code") == code]


def find_log_events(outputs, event):
    """Find log events by event type (code=log, event=<event>)."""
    return [o for o in outputs if o.get("code") == "log" and o.get("event") == event]


def get_header_ci(headers_dict, name):
    """Case-insensitive header lookup."""
    name_lower = name.lower()
    for k, v in headers_dict.items():
        if k.lower() == name_lower:
            return v
    return None


def temp_path(prefix: str, suffix: str) -> str:
    return os.path.join(
        tempfile.gettempdir(),
        f"{prefix}-{os.getpid()}-{time.time_ns()}{suffix}",
    )


passed = 0
failed = 0
errors = []


def test(name):
    def decorator(fn):
        def wrapper():
            global passed, failed
            try:
                fn()
                passed += 1
                print(f"  \033[32mPASS\033[0m {name}")
            except Exception as e:
                failed += 1
                errors.append((name, str(e)))
                print(f"  \033[31mFAIL\033[0m {name}: {e}")
        wrapper.__name__ = name
        return wrapper
    return decorator


# ---------------------------------------------------------------------------
# Structural tests
# ---------------------------------------------------------------------------

@test("default output has no startup log")
def test_no_startup_default():
    out, _ = run_cli(["GET", f"{BASE}/fast"])
    codes = [o["code"] for o in out]
    assert codes == ["response"], f"codes: {codes}"


@test("--log startup enables startup log event")
def test_log_startup():
    out, _ = run_cli(["GET", f"{BASE}/fast", "--log", "startup"])
    startups = find_log_events(out, "startup")
    assert startups, f"no startup log, got: {out}"
    s = startups[0]
    assert s["version"] == afh_version()
    assert isinstance(s["argv"], list)
    assert "GET" in s["argv"]
    assert s["config"]["timeout_connect_s"] == 10
    assert s["config"]["defaults"]["timeout_idle_s"] == 30


@test("--verbose enables startup and all log categories")
def test_verbose():
    out, _ = run_cli(["GET", f"{BASE}/fast", "--verbose"])
    events = [o.get("event") for o in out if o.get("code") == "log"]
    assert "startup" in events, f"no startup with --verbose: {events}"


@test("no close message in CLI mode")
def test_no_close():
    out, code = run_cli(["GET", f"{BASE}/fast"])
    codes = [o["code"] for o in out]
    assert "close" not in codes, f"close present in CLI output: {codes}"


@test("id and tag fields absent in CLI output")
def test_no_id_tag():
    out, _ = run_cli(["GET", f"{BASE}/fast", "--log", "startup"])
    for line in out:
        assert "id" not in line, f"id present in {line['code']}: {line.get('id')}"
        assert "tag" not in line, f"tag present in {line['code']}: {line.get('tag')}"


@test("default output is response only")
def test_output_order():
    out, _ = run_cli(["GET", f"{BASE}/fast"])
    codes = [o["code"] for o in out]
    assert codes == ["response"], f"codes: {codes}"


# ---------------------------------------------------------------------------
# Exit codes
# ---------------------------------------------------------------------------

@test("exit 0 on successful response")
def test_exit_0():
    _, code = run_cli(["GET", f"{BASE}/fast"])
    assert code == 0, f"exit code: {code}"


@test("exit 0 on 4xx/5xx (HTTP error is still a response)")
def test_exit_0_on_4xx():
    _, code = run_cli(["GET", f"{BASE}/status/404"])
    assert code == 0, f"exit code: {code}"


@test("exit 1 on transport error")
def test_exit_1():
    _, code = run_cli(["GET", "http://127.0.0.1:19999/fail"])
    assert code == 1, f"exit code: {code}"


@test("exit 2 on no arguments")
def test_exit_2_no_args():
    proc = subprocess.run([AFHTTP], capture_output=True, text=True, timeout=5)
    assert proc.returncode == 2, f"exit code: {proc.returncode}"


@test("exit 2 on missing URL")
def test_exit_2_no_url():
    proc = subprocess.run([AFHTTP, "GET"], capture_output=True, text=True, timeout=5)
    assert proc.returncode == 2, f"exit code: {proc.returncode}"


# ---------------------------------------------------------------------------
# Basic requests
# ---------------------------------------------------------------------------

@test("GET returns parsed JSON body")
def test_get_json():
    out, _ = run_cli(["GET", f"{BASE}/fast"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 200
    assert r[0]["body"]["ok"] is True


@test("GET returns text body as string")
def test_get_text():
    out, _ = run_cli(["GET", f"{BASE}/text/100"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["body"] == "A" * 100


@test("GET returns binary as body_base64")
def test_get_binary():
    out, _ = run_cli(["GET", f"{BASE}/binary/50"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert "body_base64" in r[0] and r[0]["body_base64"]
    decoded = base64.b64decode(r[0]["body_base64"])
    assert len(decoded) == 50


@test("HEAD returns no body")
def test_head():
    out, _ = run_cli(["HEAD", f"{BASE}/head-test"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 200
    assert "body" not in r[0] or r[0]["body"] is None


@test("204 returns no body")
def test_204():
    out, _ = run_cli(["GET", f"{BASE}/empty"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 204


@test("4xx status returned as response")
def test_4xx():
    out, _ = run_cli(["GET", f"{BASE}/status/404"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 404


@test("5xx status returned as response")
def test_5xx():
    out, _ = run_cli(["GET", f"{BASE}/status/500"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 500


# ---------------------------------------------------------------------------
# Headers
# ---------------------------------------------------------------------------

@test("default User-Agent header sent")
def test_user_agent():
    out, _ = run_cli(["GET", f"{BASE}/headers"])
    r = find_by_code(out, "response")
    assert r, "no response"
    ua = get_header_ci(r[0]["body"], "User-Agent")
    assert ua and ua.startswith("afhttp/"), f"User-Agent: {ua}"


@test("-H adds custom header")
def test_custom_header():
    out, _ = run_cli(["GET", f"{BASE}/headers", "--header", "X-Custom: test-value"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert get_header_ci(r[0]["body"], "X-Custom") == "test-value"


@test("multiple -H flags")
def test_multi_header():
    out, _ = run_cli(["GET", f"{BASE}/headers",
                       "--header", "X-A: one", "--header", "X-B: two"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert get_header_ci(r[0]["body"], "X-A") == "one"
    assert get_header_ci(r[0]["body"], "X-B") == "two"


@test("-H with empty value removes default header")
def test_remove_header():
    out, _ = run_cli(["GET", f"{BASE}/headers", "--header", "User-Agent:"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert get_header_ci(r[0]["body"], "User-Agent") is None, \
        f"User-Agent should be removed"


# ---------------------------------------------------------------------------
# Body
# ---------------------------------------------------------------------------

@test("-b with JSON auto-detected")
def test_body_json():
    out, _ = run_cli(["POST", f"{BASE}/echo", "--body", '{"key":"value"}'])
    r = find_by_code(out, "response")
    assert r, "no response"
    echo = r[0]["body"]
    assert "application/json" in echo["content_type"]
    assert json.loads(echo["body"]) == {"key": "value"}


@test("-b with plain text sends raw bytes (no implicit Content-Type)")
def test_body_text():
    out, _ = run_cli(["POST", f"{BASE}/echo", "--body", "hello world"])
    r = find_by_code(out, "response")
    assert r, "no response"
    echo = r[0]["body"]
    # No implicit Content-Type — caller must specify if needed
    assert echo["content_type"] == "", f"expected no Content-Type for string body, got: {echo['content_type']!r}"
    assert echo["body"] == "hello world"


@test("-b @path reads body from file")
def test_body_at_file():
    with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
        f.write('{"from_file":true}')
        tmp = f.name
    try:
        out, _ = run_cli(["POST", f"{BASE}/echo", "--body", f"@{tmp}",
                           "--header", "Content-Type: application/json"])
        r = find_by_code(out, "response")
        assert r, "no response"
        assert json.loads(r[0]["body"]["body"]) == {"from_file": True}
    finally:
        os.unlink(tmp)


@test("--body-file reads body from file")
def test_body_file_flag():
    with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
        f.write("file content")
        tmp = f.name
    try:
        out, _ = run_cli(["POST", f"{BASE}/echo", "--body-file", tmp])
        r = find_by_code(out, "response")
        assert r, "no response"
        assert r[0]["body"]["body_length"] == len("file content")
    finally:
        os.unlink(tmp)


@test("--body-base64 sends binary body")
def test_body_base64():
    data = bytes(range(64))
    b64 = base64.b64encode(data).decode()
    out, _ = run_cli(["POST", f"{BASE}/echo", "--body-base64", b64])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["body"]["body_length"] == 64


@test("--body-multipart sends multipart")
def test_body_multipart():
    out, _ = run_cli(["POST", f"{BASE}/echo-multipart",
                       "--body-multipart", "field1=hello",
                       "--body-multipart", "field2=world"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["body"]["has_multipart"] is True


@test("--body-multipart with file upload")
def test_body_multipart_file():
    with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
        f.write("upload data")
        tmp = f.name
    try:
        out, _ = run_cli(["POST", f"{BASE}/echo-multipart",
                           "--body-multipart", f"file=@{tmp};filename=test.txt;type=text/plain"])
        r = find_by_code(out, "response")
        assert r, "no response"
        assert r[0]["body"]["has_multipart"] is True
    finally:
        os.unlink(tmp)


@test("--body-urlencoded sends correct content-type and fields")
def test_body_urlencoded():
    out, _ = run_cli(["POST", f"{BASE}/echo-urlencoded",
                      "--body-urlencoded", "grant_type=authorization_code",
                      "--body-urlencoded", "code=abc123"])
    r = find_by_code(out, "response")
    assert r, "no response"
    ct = r[0]["body"]["content_type"]
    assert "application/x-www-form-urlencoded" in ct, f"unexpected Content-Type: {ct}"
    fields = r[0]["body"]["fields"]
    assert fields[0] == {"name": "grant_type", "value": "authorization_code"}
    assert fields[1] == {"name": "code", "value": "abc123"}


@test("--body-urlencoded percent-encodes special characters")
def test_body_urlencoded_special_chars():
    out, _ = run_cli(["POST", f"{BASE}/echo-urlencoded",
                      "--body-urlencoded", "note=hello world",
                      "--body-urlencoded", "redirect_uri=https://app.example.com/cb?x=1&y=2"])
    r = find_by_code(out, "response")
    assert r, "no response"
    fields = r[0]["body"]["fields"]
    assert fields[0] == {"name": "note", "value": "hello world"}
    assert fields[1] == {"name": "redirect_uri", "value": "https://app.example.com/cb?x=1&y=2"}


@test("--body-urlencoded supports duplicate keys")
def test_body_urlencoded_duplicate_keys():
    out, _ = run_cli(["POST", f"{BASE}/echo-urlencoded",
                      "--body-urlencoded", "tag=rust",
                      "--body-urlencoded", "tag=async",
                      "--body-urlencoded", "tag=web"])
    r = find_by_code(out, "response")
    assert r, "no response"
    fields = r[0]["body"]["fields"]
    assert len(fields) == 3
    assert all(f["name"] == "tag" for f in fields)
    assert [f["value"] for f in fields] == ["rust", "async", "web"]


@test("empty POST body")
def test_empty_post():
    out, _ = run_cli(["POST", f"{BASE}/echo"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["body"]["body_length"] == 0


# ---------------------------------------------------------------------------
# Options
# ---------------------------------------------------------------------------

@test("--response-parse-json false returns JSON as string")
def test_parse_json_false():
    out, _ = run_cli(["GET", f"{BASE}/fast", "--response-parse-json", "false"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert isinstance(r[0]["body"], str), f"expected string, got {type(r[0]['body'])}"


@test("--response-redirect 0 returns redirect as-is")
def test_redirect_disabled():
    out, _ = run_cli(["GET", f"{BASE}/redirect/3", "--response-redirect", "0"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 302


@test("redirects followed by default")
def test_redirect_default():
    out, _ = run_cli(["GET", f"{BASE}/redirect/3"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 200
    assert r[0]["body"]["redirected"] is True


@test("--timeout-idle-s triggers timeout on slow server")
def test_timeout_idle():
    out, code = run_cli(["GET", f"{BASE}/hang", "--timeout-idle-s", "1"])
    assert code == 1, f"exit code: {code}"
    e = find_by_code(out, "error")
    assert e, f"no error, codes: {[o.get('code') for o in out]}"
    assert e[0]["error_code"] == "request_timeout"


@test("--retry retries on transport error")
def test_retry():
    t0 = time.time()
    out, code = run_cli(["GET", "http://127.0.0.1:19999/fail", "--retry", "2"])
    elapsed = time.time() - t0
    assert code == 1
    e = find_by_code(out, "error")
    assert e, "no error"
    # With 2 retries and backoff, should take > 0.1s
    assert elapsed > 0.1, f"too fast: {elapsed:.2f}s — retry may not have happened"


@test("--retry-on-status retries specific status codes")
def test_retry_on_status():
    key = f"cli-{os.getpid()}-{time.time()}"
    out, code = run_cli(["GET", f"{BASE}/retry-succeed/{key}/2",
                          "--retry", "3", "--retry-on-status", "429"])
    assert code == 0, f"exit code: {code}"
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 200
    assert r[0]["body"]["attempts"] == 3


@test("--response-save-above-bytes triggers auto-save for large response")
def test_max_inline_bytes():
    out, code = run_cli(["GET", f"{BASE}/size/5000",
                          "--response-save-above-bytes", "1000"])
    assert code == 0, f"exit code: {code}"
    r = find_by_code(out, "response")
    assert r, f"no response, codes: {[o.get('code') for o in out]}"
    assert "body_file" in r[0], f"expected body_file for large response, got keys: {list(r[0].keys())}"
    path = r[0]["body_file"]
    assert os.path.exists(path), f"body_file does not exist: {path}"
    assert os.path.getsize(path) == 5000
    os.unlink(path)


@test("--response-save-dir overrides auto-save directory")
def test_response_save_dir():
    save_dir = tempfile.mkdtemp(prefix="afhttp-test-savedir-")
    try:
        out, code = run_cli(["GET", f"{BASE}/size/5000",
                              "--response-save-above-bytes", "1000",
                              "--response-save-dir", save_dir])
        assert code == 0, f"exit code: {code}"
        r = find_by_code(out, "response")
        assert r, f"no response, codes: {[o.get('code') for o in out]}"
        assert "body_file" in r[0], f"expected body_file, got keys: {list(r[0].keys())}"
        path = r[0]["body_file"]
        assert path.startswith(save_dir), f"body_file {path} not in save_dir {save_dir}"
        assert os.path.exists(path), f"body_file does not exist: {path}"
        assert os.path.getsize(path) == 5000
    finally:
        import shutil
        shutil.rmtree(save_dir, ignore_errors=True)


@test("--retry-base-delay-ms controls retry backoff")
def test_retry_base_delay():
    t0 = time.time()
    out, code = run_cli(["GET", "http://127.0.0.1:19999/fail",
                          "--retry", "2", "--retry-base-delay-ms", "500"])
    elapsed = time.time() - t0
    assert code == 1
    e = find_by_code(out, "error")
    assert e, "no error"
    # With base delay 500ms and 2 retries: 500ms + 1000ms = 1.5s minimum
    assert elapsed > 1.0, f"too fast ({elapsed:.2f}s), retry-base-delay-ms may not work"


@test("--response-max-bytes triggers error on large response")
def test_max_bytes():
    out, code = run_cli(["GET", f"{BASE}/size/10000", "--response-max-bytes", "5000"])
    assert code == 1, f"exit code: {code}"
    e = find_by_code(out, "error")
    assert e, "no error"
    assert e[0]["error_code"] == "response_too_large"


# ---------------------------------------------------------------------------
# Streaming
# ---------------------------------------------------------------------------

@test("--chunked streams NDJSON")
def test_chunked_ndjson():
    out, _ = run_cli(["GET", f"{BASE}/stream/ndjson/5/5", "--chunked"])
    cs = find_by_code(out, "chunk_start")
    assert cs, "no chunk_start"
    chunks = find_by_code(out, "chunk_data")
    assert len(chunks) == 5, f"expected 5 chunks, got {len(chunks)}"
    ce = find_by_code(out, "chunk_end")
    assert ce, "no chunk_end"


@test("--chunked-delimiter '\\n\\n' streams SSE")
def test_chunked_sse():
    out, _ = run_cli(["GET", f"{BASE}/stream/sse/3/5", "--chunked-delimiter", "\\n\\n"])
    cs = find_by_code(out, "chunk_start")
    assert cs, "no chunk_start"
    chunks = find_by_code(out, "chunk_data")
    assert len(chunks) == 3, f"expected 3 chunks, got {len(chunks)}"


@test("--chunked-delimiter-raw streams binary chunks")
def test_chunked_raw():
    out, _ = run_cli(["GET", f"{BASE}/binary/100", "--chunked-delimiter-raw"])
    chunks = find_by_code(out, "chunk_data")
    assert len(chunks) >= 1, "no chunk_data"
    for c in chunks:
        assert c.get("data_base64"), "expected data_base64 in raw mode"


@test("chunked output order: chunk_start, chunk_data..., chunk_end")
def test_chunked_order():
    out, _ = run_cli(["GET", f"{BASE}/stream/ndjson/3/5", "--chunked"])
    codes = [o["code"] for o in out]
    assert codes[0] == "chunk_start"
    assert codes[-1] == "chunk_end"
    assert all(c == "chunk_data" for c in codes[1:-1])


# ---------------------------------------------------------------------------
# File download
# ---------------------------------------------------------------------------

@test("--response-save-file saves to file")
def test_save_to():
    save_path = temp_path("afhttp-cli-test", ".bin")
    try:
        out, code = run_cli(["GET", f"{BASE}/size/5000",
                              "--response-save-file", save_path])
        assert code == 0, f"exit code: {code}"
        ce = find_by_code(out, "chunk_end")
        assert ce, f"no chunk_end, codes: {[o.get('code') for o in out]}"
        assert ce[0]["body_file"] == save_path
        assert os.path.exists(save_path)
        assert os.path.getsize(save_path) == 5000
        # No progress without --log progress
        progs = find_log_events(out, "progress")
        assert len(progs) == 0, f"unexpected progress without --log progress: {progs}"
    finally:
        if os.path.exists(save_path):
            os.unlink(save_path)


@test("--log progress with --progress-bytes emits progress log events")
def test_download_progress():
    save_path = temp_path("afhttp-cli-progress", ".bin")
    try:
        out, code = run_cli(["GET", f"{BASE}/size/10000",
                              "--response-save-file", save_path,
                              "--log", "progress",
                              "--progress-bytes", "2000"])
        assert code == 0
        ce = find_by_code(out, "chunk_end")
        assert ce, "no chunk_end"
        assert ce[0]["trace"]["received_bytes"] == 10000
        progs = find_log_events(out, "progress")
        assert len(progs) >= 1, f"no progress events with --log progress: {[o.get('code') for o in out]}"
        assert "received_bytes" in progs[0], f"progress missing received_bytes: {progs[0]}"
    finally:
        if os.path.exists(save_path):
            os.unlink(save_path)


# ---------------------------------------------------------------------------
# Error handling
# ---------------------------------------------------------------------------

@test("DNS failure returns error JSON")
def test_dns_error():
    out, code = run_cli(["GET", "http://nonexistent.invalid.tld/"])
    assert code == 1
    e = find_by_code(out, "error")
    assert e, "no error"
    assert e[0]["error_code"] in ("dns_failed", "connect_refused")
    assert e[0]["retryable"] is True


@test("connection refused returns error JSON")
def test_connect_refused():
    out, code = run_cli(["GET", "http://127.0.0.1:19999/fail"])
    assert code == 1
    e = find_by_code(out, "error")
    assert e, "no error"
    assert e[0]["error_code"] in ("connect_refused", "connect_timeout")
    assert e[0]["retryable"] is True


@test("error has structured Agent-First Data fields")
def test_error_structure():
    out, _ = run_cli(["GET", "http://127.0.0.1:19999/fail"])
    e = find_by_code(out, "error")
    assert e, "no error"
    e = e[0]
    assert "error_code" in e
    assert "error" in e
    assert "retryable" in e
    assert "trace" in e
    assert isinstance(e["error_code"], str)
    assert isinstance(e["error"], str)
    assert isinstance(e["retryable"], bool)


# ---------------------------------------------------------------------------
# TLS flags
# ---------------------------------------------------------------------------

@test("--tls-insecure accepted (no crash)")
def test_tls_insecure():
    out, code = run_cli(["GET", f"{BASE}/fast", "--tls-insecure"])
    assert code == 0
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 200


# ---------------------------------------------------------------------------
# Trace
# ---------------------------------------------------------------------------

@test("trace has duration_ms in response")
def test_trace_duration():
    out, _ = run_cli(["GET", f"{BASE}/fast"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert "trace" in r[0]
    assert r[0]["trace"]["duration_ms"] >= 0


@test("trace has received_bytes")
def test_trace_received():
    out, _ = run_cli(["GET", f"{BASE}/text/500"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["trace"]["received_bytes"] == 500


# ---------------------------------------------------------------------------
# Method case insensitivity
# ---------------------------------------------------------------------------

@test("method is case-insensitive")
def test_method_case():
    out, code = run_cli(["get", f"{BASE}/fast"])
    assert code == 0
    r = find_by_code(out, "response")
    assert r, "no response"
    assert r[0]["status"] == 200


# ---------------------------------------------------------------------------
# Help and version
# ---------------------------------------------------------------------------

@test("--help prints help and exits 0")
def test_help():
    proc = subprocess.run([AFHTTP, "--help"], capture_output=True, text=True, timeout=5)
    assert proc.returncode == 0
    assert "Agent-First HTTP" in proc.stdout


@test("--version prints version and exits 0")
def test_version():
    proc = subprocess.run([AFHTTP, "--version"], capture_output=True, text=True, timeout=5)
    assert proc.returncode == 0
    assert afh_version() in proc.stdout


# ---------------------------------------------------------------------------
# Unicode
# ---------------------------------------------------------------------------

@test("unicode in response preserved")
def test_unicode():
    out, _ = run_cli(["GET", f"{BASE}/unicode"])
    r = find_by_code(out, "response")
    assert r, "no response"
    assert "你好世界" in r[0]["body"]["text"]


# ---------------------------------------------------------------------------
# Resume download
# ---------------------------------------------------------------------------

@test("--response-save-resume without --response-save-file returns error")
def test_response_save_resume_no_file():
    out, code = run_cli(["GET", f"{BASE}/fast", "--response-save-resume"])
    assert code == 1, f"expected exit code 1, got {code}"
    errs = find_by_code(out, "error")
    assert errs, "no error output"
    assert "response_save_resume requires response_save_file" in errs[0]["error"]


@test("--response-save-resume first download (file absent) fetches full file, no Range")
def test_response_save_resume_new_file():
    import tempfile, os
    tmp = tempfile.mktemp(suffix=".bin")
    try:
        out, code = run_cli(["GET", f"{BASE}/range-file/100",
                              "--response-save-file", tmp,
                              "--response-save-resume",
                              "--log", "request"])
        assert code == 0, f"exit code: {code}"
        # No Range header when file doesn't exist
        logs = find_log_events(out, "request")
        if logs:
            ih = logs[0].get("implicit_headers", {})
            assert "Range" not in ih, f"unexpected Range header on first download: {ih}"
        with open(tmp, "rb") as f:
            data = f.read()
        assert len(data) == 100, f"expected full 100 bytes, got {len(data)}"
    finally:
        if os.path.exists(tmp):
            os.unlink(tmp)


@test("--response-save-resume sends Range header and appends to file")
def test_response_save_resume():
    import tempfile, os
    with tempfile.NamedTemporaryFile(delete=False, suffix=".bin") as f:
        f.write(b"X" * 50)  # pre-existing 50 bytes
        tmp = f.name
    try:
        out, code = run_cli(["GET", f"{BASE}/range-file/100",
                              "--response-save-file", tmp,
                              "--response-save-resume"])
        assert code == 0, f"exit code: {code}"
        with open(tmp, "rb") as f:
            data = f.read()
        assert len(data) == 100, f"expected 100 bytes after resume, got {len(data)}"
        assert data[:50] == b"X" * 50, "first half from pre-existing file"
        assert data[50:] == b"X" * 50, "second half appended from server"
    finally:
        os.unlink(tmp)


@test("--response-save-resume logs Range header as implicit header")
def test_response_save_resume_log():
    import tempfile, os
    with tempfile.NamedTemporaryFile(delete=False, suffix=".bin") as f:
        f.write(b"X" * 50)
        tmp = f.name
    try:
        out, _ = run_cli(["GET", f"{BASE}/range-file/100",
                           "--response-save-file", tmp,
                           "--response-save-resume",
                           "--log", "request"])
        logs = find_log_events(out, "request")
        assert logs, "expected request log event"
        ih = logs[0].get("implicit_headers", {})
        assert "Range" in ih, f"Range missing from implicit_headers: {ih}"
        assert ih["Range"] == "bytes=50-", f"unexpected Range value: {ih['Range']}"
    finally:
        os.unlink(tmp)


@test("--response-save-resume on empty file fetches full file (no Range)")
def test_response_save_resume_empty_file():
    import tempfile, os
    with tempfile.NamedTemporaryFile(delete=False, suffix=".bin") as f:
        tmp = f.name  # empty file
    try:
        out, code = run_cli(["GET", f"{BASE}/range-file/100",
                              "--response-save-file", tmp,
                              "--response-save-resume",
                              "--log", "request"])
        assert code == 0, f"exit code: {code}"
        logs = find_log_events(out, "request")
        if logs:
            ih = logs[0].get("implicit_headers", {})
            assert "Range" not in ih, f"unexpected Range on empty file: {ih}"
        with open(tmp, "rb") as f:
            data = f.read()
        assert len(data) == 100, f"expected 100 bytes, got {len(data)}"
    finally:
        os.unlink(tmp)


# ---------------------------------------------------------------------------
# Invalid UTF-8 / binary safety
# ---------------------------------------------------------------------------

@test("invalid UTF-8 text response returns body_base64 (not corrupted string)")
def test_invalid_utf8_text():
    out, code = run_cli(["GET", f"{BASE}/invalid-utf8/text"])
    assert code == 0, f"exit code: {code}"
    r = find_by_code(out, "response")
    assert r, "no response"
    r = r[0]
    assert "body" not in r or r["body"] is None, \
        f"expected no body string for invalid UTF-8, got: {r.get('body')!r}"
    assert r.get("body_base64"), f"expected body_base64 for invalid UTF-8, keys: {list(r.keys())}"
    decoded = base64.b64decode(r["body_base64"])
    assert decoded == b"caf\xe9 r\xe9sum\xe9", f"decoded bytes mismatch: {decoded!r}"


@test("invalid UTF-8 JSON response returns body_base64 (not corrupted string)")
def test_invalid_utf8_json():
    out, code = run_cli(["GET", f"{BASE}/invalid-utf8/json"])
    assert code == 0, f"exit code: {code}"
    r = find_by_code(out, "response")
    assert r, "no response"
    r = r[0]
    assert "body" not in r or r["body"] is None, \
        f"expected no body for invalid UTF-8 JSON, got: {r.get('body')!r}"
    assert r.get("body_base64"), f"expected body_base64, keys: {list(r.keys())}"
    decoded = base64.b64decode(r["body_base64"])
    assert decoded.startswith(b"\xff\xfe"), f"original bytes not preserved: {decoded[:4]!r}"


@test("chunked delimiter mode with invalid UTF-8 returns data_base64")
def test_chunked_invalid_utf8():
    out, code = run_cli(["GET", f"{BASE}/stream/invalid-utf8",
                          "--chunked-delimiter", "\\n"])
    assert code == 0, f"exit code: {code}"
    chunks = find_by_code(out, "chunk_data")
    assert len(chunks) >= 1, f"no chunk_data, codes: {[o.get('code') for o in out]}"
    for c in chunks:
        assert c.get("data_base64") is not None, \
            f"expected data_base64 for invalid UTF-8 chunk, got data: {c.get('data')!r}"
        assert c.get("data") is None, \
            f"expected no data string for invalid UTF-8 chunk, got: {c.get('data')!r}"
        decoded = base64.b64decode(c["data_base64"])
        assert b"\xff" in decoded or b"\xfe" in decoded or b"\xfd" in decoded or b"\xe9" in decoded, \
            f"decoded bytes don't contain expected invalid UTF-8: {decoded!r}"


# ---------------------------------------------------------------------------
# Output format
# ---------------------------------------------------------------------------

@test("--output json is default (valid JSON)")
def test_output_json():
    out, code = run_cli(["GET", f"{BASE}/fast"])
    assert code == 0
    assert len(out) >= 1
    assert out[0]["code"] == "response"

@test("--output yaml produces YAML output")
def test_output_yaml():
    proc = subprocess.run(
        [AFHTTP, "GET", f"{BASE}/fast", "--output", "yaml"],
        capture_output=True, text=True, timeout=10,
    )
    assert proc.returncode == 0
    assert "---" in proc.stdout, f"no YAML header: {proc.stdout[:200]}"
    assert "code" in proc.stdout or "response" in proc.stdout

@test("--output plain produces logfmt output")
def test_output_plain():
    proc = subprocess.run(
        [AFHTTP, "GET", f"{BASE}/fast", "--output", "plain"],
        capture_output=True, text=True, timeout=10,
    )
    assert proc.returncode == 0
    # logfmt: key=value pairs, should contain code=response
    assert "code=response" in proc.stdout, f"no code=response in plain output: {proc.stdout[:200]}"

@test("--output yaml preserves server body")
def test_output_yaml_body():
    proc = subprocess.run(
        [AFHTTP, "GET", f"{BASE}/fast", "--output", "yaml"],
        capture_output=True, text=True, timeout=10,
    )
    assert proc.returncode == 0
    # Server JSON body should appear as a JSON string, not decomposed into YAML keys
    assert "ok" in proc.stdout, f"body content missing: {proc.stdout[:300]}"

@test("--output invalid rejected")
def test_output_invalid():
    proc = subprocess.run(
        [AFHTTP, "GET", f"{BASE}/fast", "--output", "xml"],
        capture_output=True, text=True, timeout=10,
    )
    assert proc.returncode == 2, f"expected exit 2, got {proc.returncode}"

@test("gzip response auto-decompressed")
def test_gzip_decompress():
    out, code = run_cli(["GET", f"{BASE}/gzip"])
    assert code == 0, f"exit code: {code}"
    r = find_by_code(out, "response")
    assert r, "no response"
    body = r[0].get("body")
    assert body is not None, f"no body, keys: {list(r[0].keys())}"
    assert body.get("compressed") is True, f"body not parsed: {body}"
    assert body.get("message") == "hello from gzip"

@test("--response-decompress false returns raw bytes")
def test_decompress_false():
    out, code = run_cli(["GET", f"{BASE}/gzip", "--response-decompress", "false"])
    assert code == 0, f"exit code: {code}"
    r = find_by_code(out, "response")
    assert r, "no response"
    # With decompress=false + Accept-Encoding: identity, server may still gzip.
    # If it does, body should be base64 (binary), not parsed JSON.
    # If it doesn't, body could be parsed JSON. Either is acceptable.
    # The key test is that we don't crash and return a valid response.
    assert r[0]["status"] == 200

@test("bare --verbose flag (no value)")
def test_verbose_bare():
    out, _ = run_cli(["GET", f"{BASE}/fast", "--verbose"])
    events = [o.get("event") for o in out if o.get("code") == "log"]
    assert "startup" in events, f"--verbose bare flag failed: {events}"

@test("bare --chunked flag (no value)")
def test_chunked_bare():
    out, _ = run_cli(["GET", f"{BASE}/stream/ndjson/3/5", "--chunked"])
    cs = find_by_code(out, "chunk_start")
    assert cs, "no chunk_start with bare --chunked"

@test("--log request logs implicit Content-Type for JSON body")
def test_log_request_content_type():
    out, _ = run_cli(["POST", f"{BASE}/echo", "--log", "request", "--body", '{"key":"val"}'])
    logs = find_log_events(out, "request")
    assert len(logs) == 1, f"expected 1 request log, got {len(logs)}: {logs}"
    ih = logs[0].get("implicit_headers", {})
    assert "Content-Type" in ih, f"missing Content-Type in implicit_headers: {ih}"
    assert "application/json" in ih["Content-Type"], f"unexpected CT: {ih['Content-Type']}"

@test("--log request logs implicit Accept-Encoding when decompress=true")
def test_log_request_accept_encoding():
    out, _ = run_cli(["GET", f"{BASE}/fast", "--log", "request"])
    logs = find_log_events(out, "request")
    assert len(logs) == 1, f"expected 1 request log, got {len(logs)}: {logs}"
    ih = logs[0].get("implicit_headers", {})
    assert "Accept-Encoding" in ih, f"missing Accept-Encoding: {ih}"
    assert "gzip" in ih["Accept-Encoding"], f"unexpected AE: {ih['Accept-Encoding']}"

@test("--log request logs identity Accept-Encoding when decompress=false")
def test_log_request_decompress_false():
    out, _ = run_cli(["GET", f"{BASE}/fast", "--log", "request", "--response-decompress", "false"])
    logs = find_log_events(out, "request")
    assert len(logs) == 1, f"expected 1 request log, got {len(logs)}: {logs}"
    ih = logs[0].get("implicit_headers", {})
    assert ih.get("Accept-Encoding") == "identity", f"expected identity: {ih}"

@test("no request log when --log does not include request")
def test_log_request_not_enabled():
    out, _ = run_cli(["POST", f"{BASE}/echo", "--log", "startup", "--body", '{"key":"val"}'])
    logs = find_log_events(out, "request")
    assert len(logs) == 0, f"unexpected request log: {logs}"

@test("no request log when no implicit headers added")
def test_log_request_no_implicit():
    # Explicit Accept-Encoding → no implicit AE. GET has no body → no implicit CT.
    out, _ = run_cli([
        "GET", f"{BASE}/fast", "--log", "request",
        "--header", "Accept-Encoding: identity",
    ])
    logs = find_log_events(out, "request")
    assert len(logs) == 0, f"unexpected request log when headers are explicit: {logs}"


# ---------------------------------------------------------------------------
# Run
# ---------------------------------------------------------------------------

def main():
    global passed, failed

    print(f"Starting test server on :{HTTP_PORT}...")
    server = start_server(HTTP_PORT)
    time.sleep(0.3)

    import urllib.request
    try:
        urllib.request.urlopen(f"{BASE}/fast", timeout=2)
    except Exception as e:
        print(f"FATAL: test server not responding: {e}")
        sys.exit(1)
    print("Test server ready.\n")

    if not os.path.exists(AFHTTP):
        print(f"FATAL: afhttp binary not found at {AFHTTP}")
        print("Run: cargo build")
        sys.exit(1)

    tests = [
        # Structural
        test_no_startup_default,
        test_log_startup,
        test_verbose,
        test_no_close,
        test_no_id_tag,
        test_output_order,
        # Exit codes
        test_exit_0,
        test_exit_0_on_4xx,
        test_exit_1,
        test_exit_2_no_args,
        test_exit_2_no_url,
        # Basic requests
        test_get_json,
        test_get_text,
        test_get_binary,
        test_head,
        test_204,
        test_4xx,
        test_5xx,
        # Headers
        test_user_agent,
        test_custom_header,
        test_multi_header,
        test_remove_header,
        # Body
        test_body_json,
        test_body_text,
        test_body_at_file,
        test_body_file_flag,
        test_body_base64,
        test_body_multipart,
        test_body_multipart_file,
        test_body_urlencoded,
        test_body_urlencoded_special_chars,
        test_body_urlencoded_duplicate_keys,
        test_empty_post,
        # Options
        test_parse_json_false,
        test_redirect_disabled,
        test_redirect_default,
        test_timeout_idle,
        test_retry,
        test_retry_on_status,
        test_max_inline_bytes,
        test_response_save_dir,
        test_retry_base_delay,
        test_max_bytes,
        # Streaming
        test_chunked_ndjson,
        test_chunked_sse,
        test_chunked_raw,
        test_chunked_order,
        # Download
        test_save_to,
        test_download_progress,
        # Errors
        test_dns_error,
        test_connect_refused,
        test_error_structure,
        # TLS
        test_tls_insecure,
        # Trace
        test_trace_duration,
        test_trace_received,
        # Other
        test_method_case,
        test_help,
        test_version,
        test_unicode,
        # Output format
        test_output_json,
        test_output_yaml,
        test_output_plain,
        test_output_yaml_body,
        test_output_invalid,
        test_gzip_decompress,
        test_decompress_false,
        test_verbose_bare,
        test_chunked_bare,
        # Request log
        test_log_request_content_type,
        test_log_request_accept_encoding,
        test_log_request_decompress_false,
        test_log_request_not_enabled,
        test_log_request_no_implicit,
        # Resume download
        test_response_save_resume_no_file,
        test_response_save_resume_new_file,
        test_response_save_resume,
        test_response_save_resume_log,
        test_response_save_resume_empty_file,
        # Invalid UTF-8 safety
        test_invalid_utf8_text,
        test_invalid_utf8_json,
        test_chunked_invalid_utf8,
    ]

    print(f"Running {len(tests)} CLI mode tests...\n")
    for t in tests:
        t()

    print(f"\n{'='*60}")
    print(f"  {passed} passed, {failed} failed, {passed+failed} total")
    if errors:
        print(f"\n  Failures:")
        for name, msg in errors:
            print(f"    {name}: {msg}")
    print(f"{'='*60}")

    server.shutdown()
    sys.exit(1 if failed else 0)


if __name__ == "__main__":
    main()