gadk-model 0.5.11

The gadk LLM abstraction: the Llm trait, model registry, HTTP seam and REST transport
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
// Copyright 2026 Chris Olson
// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Test doubles for code that sends through an [`HttpClient`] (feature `test-util`), shared
//! by the provider crates' tests:
//!
//! - [`ScriptedClient`]: an in-process [`HttpClient`] that records every request
//!   ([`RecordedRequest`], with the tokio time it arrived) and answers from a handler or a
//!   script of [`FakeResponse`]s;
//! - [`FakeServer`]: the same handlers behind a real HTTP/1 server on `127.0.0.1`, for code
//!   that builds its own `reqwest` client;
//! - [`FakeResponse`]: status, headers, body chunks, pauses, a body error
//!   ([`FakeResponse::fail`], a dropped connection), a body that never ends
//!   ([`FakeResponse::hang`]), a request never answered ([`FakeResponse::never`]) and a
//!   connection error ([`FakeResponse::reset`]);
//! - [`sse_event`] / [`sse_lines`]: SSE transcripts;
//! - [`fixed_header_credentials`] / [`bearer_credentials`]: Google credentials whose headers
//!   are fixed (static token sources).
//!
//! [`ScriptedClient`] works under paused tokio time (`start_paused`); [`FakeServer`] needs a
//! running tokio runtime and real time.

#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use std::collections::VecDeque;
use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context, Poll};
use std::time::Duration;

use bytes::Bytes;
use futures::StreamExt;
use futures::future::BoxFuture;
use hyper::body::{Body, Frame, Incoming, SizeHint};
use parking_lot::Mutex;
use tokio::sync::Notify;
use tokio::time::Instant;

use crate::error::ModelError;
use crate::http::{BodyStream, HttpClient};

/// How long the `wait_for_*` helpers wait before failing the test.
pub const WAIT_TIMEOUT: Duration = Duration::from_secs(10);

// ------------------------------------------------------------------------------------ requests

/// One request as the fake server (or [`ScriptedClient`]) received it.
#[derive(Debug, Clone)]
pub struct RecordedRequest {
    /// HTTP method, upper case (`POST`).
    pub method: String,
    /// Absolute URL: `http://127.0.0.1:<port>/v1/messages?…` for [`FakeServer`], the request
    /// URI as sent for [`ScriptedClient`].
    pub url: String,
    /// URL path, e.g. `/v1/messages`.
    pub path: String,
    /// Raw query without `?`; `None` when the URL has none.
    pub query: Option<String>,
    /// Request headers as received.
    pub headers: http::HeaderMap,
    /// The whole request body.
    pub body: Bytes,
    /// When the request arrived (tokio time, so paused-time tests can measure backoff).
    pub at: Instant,
}

impl RecordedRequest {
    /// `path` plus `?query` when there is one.
    pub fn path_and_query(&self) -> String {
        match &self.query {
            Some(q) => format!("{}?{q}", self.path),
            None => self.path.clone(),
        }
    }

    /// The first value of header `name`, or `""` when absent (case-insensitive name).
    pub fn header(&self, name: &str) -> String {
        self.headers
            .get(name)
            .map(|v| String::from_utf8_lossy(v.as_bytes()).into_owned())
            .unwrap_or_default()
    }

    /// Every value of header `name`, in order.
    pub fn header_values(&self, name: &str) -> Vec<String> {
        self.headers
            .get_all(name)
            .iter()
            .map(|v| String::from_utf8_lossy(v.as_bytes()).into_owned())
            .collect()
    }

    /// The body decoded as JSON; `Value::Null` when it is not JSON.
    pub fn json(&self) -> serde_json::Value {
        serde_json::from_slice(&self.body).unwrap_or(serde_json::Value::Null)
    }

    /// Whether the body is a JSON object whose `stream` is `true`.
    pub fn is_stream(&self) -> bool {
        self.json().get("stream") == Some(&serde_json::Value::Bool(true))
    }

    /// The body as text (lossy UTF-8).
    pub fn body_text(&self) -> String {
        String::from_utf8_lossy(&self.body).into_owned()
    }

    fn from_parts(url: String, parts: &http::request::Parts, body: Bytes) -> Self {
        Self {
            method: parts.method.as_str().to_string(),
            url,
            path: parts.uri.path().to_string(),
            query: parts.uri.query().map(str::to_string),
            headers: parts.headers.clone(),
            body,
            at: Instant::now(),
        }
    }
}

// ----------------------------------------------------------------------------------- responses

/// One step of a scripted response body.
#[derive(Debug, Clone)]
pub enum BodyStep {
    /// Send these bytes as one chunk, flushed immediately.
    Chunk(Bytes),
    /// Wait this long before the next step.
    Delay(Duration),
    /// The body fails here, as a dropped connection does: [`ScriptedClient`] yields
    /// `Err(ModelError::Transport(<message>))` and ends the body; [`FakeServer`] aborts the
    /// connection. Later steps are skipped.
    Fail(String),
}

/// What happens after the last [`BodyStep`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BodyEnd {
    /// The body ends normally.
    Finish,
    /// The body never ends: the connection stays open until the client drops it.
    Hang,
}

/// What the server does with a request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
    /// Answer with `status`, `headers` and the body steps.
    Respond,
    /// Never answer: the handler blocks until the client goes away. Counted
    /// as a drop when the client gives up.
    Never,
    /// No response: [`FakeServer`] closes the connection, [`ScriptedClient`] returns
    /// `ModelError::Transport(<message>)`.
    Reset(String),
}

/// A scripted response.
#[derive(Debug, Clone)]
pub struct FakeResponse {
    /// HTTP status code.
    pub status: u16,
    /// Response headers, in order.
    pub headers: Vec<(String, String)>,
    /// Delay before the status line and headers are sent.
    pub header_delay: Option<Duration>,
    /// Body chunks and delays, in order.
    pub steps: Vec<BodyStep>,
    /// What happens after the last step.
    pub end: BodyEnd,
    /// Respond, never respond, or reset.
    pub outcome: Outcome,
}

impl FakeResponse {
    /// `status` with no headers and an empty body.
    pub fn new(status: u16) -> Self {
        Self {
            status,
            headers: Vec::new(),
            header_delay: None,
            steps: Vec::new(),
            end: BodyEnd::Finish,
            outcome: Outcome::Respond,
        }
    }

    /// 200 with `Content-Type: application/json` and `body`.
    pub fn json(body: impl Into<String>) -> Self {
        Self::json_status(200, body)
    }

    /// `status` with `Content-Type: application/json` and `body`.
    pub fn json_status(status: u16, body: impl Into<String>) -> Self {
        Self::new(status)
            .header("Content-Type", "application/json")
            .chunk(body.into())
    }

    /// 200 with `Content-Type: text/event-stream` and the whole SSE `body` as one chunk.
    pub fn sse(body: impl Into<String>) -> Self {
        Self::new(200)
            .header("Content-Type", "text/event-stream")
            .chunk(body.into())
    }

    /// 200 `text/event-stream`, one flushed chunk per item.
    pub fn sse_chunks<I, S>(events: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let mut resp = Self::new(200).header("Content-Type", "text/event-stream");
        for ev in events {
            resp = resp.chunk(ev.into());
        }
        resp
    }

    /// A request that is never answered (see [`Outcome::Never`]).
    pub fn never() -> Self {
        Self {
            outcome: Outcome::Never,
            ..Self::new(200)
        }
    }

    /// A request answered by closing the connection (see [`Outcome::Reset`]).
    pub fn reset(message: impl Into<String>) -> Self {
        Self {
            outcome: Outcome::Reset(message.into()),
            ..Self::new(200)
        }
    }

    /// Replaces the status code.
    pub fn status(mut self, status: u16) -> Self {
        self.status = status;
        self
    }

    /// Appends a response header (call once per name).
    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }

    /// Appends a body chunk.
    pub fn chunk(mut self, data: impl Into<Bytes>) -> Self {
        self.steps.push(BodyStep::Chunk(data.into()));
        self
    }

    /// Appends a pause between body chunks.
    pub fn delay(mut self, d: Duration) -> Self {
        self.steps.push(BodyStep::Delay(d));
        self
    }

    /// Appends a body failure (see [`BodyStep::Fail`]), e.g. `fail("unexpected EOF")`.
    pub fn fail(mut self, message: impl Into<String>) -> Self {
        self.steps.push(BodyStep::Fail(message.into()));
        self
    }

    /// Delays the status line and headers by `d`.
    pub fn delay_headers(mut self, d: Duration) -> Self {
        self.header_delay = Some(d);
        self
    }

    /// Keeps the body open after the last step (see [`BodyEnd::Hang`]).
    pub fn hang(mut self) -> Self {
        self.end = BodyEnd::Hang;
        self
    }

    /// The concatenated body chunks.
    pub fn body_bytes(&self) -> Bytes {
        let mut out = Vec::new();
        for step in &self.steps {
            if let BodyStep::Chunk(b) = step {
                out.extend_from_slice(b);
            }
        }
        Bytes::from(out)
    }
}

/// An async request handler: gets each recorded request, returns the response to send.
pub type Handler = Arc<dyn Fn(RecordedRequest) -> BoxFuture<'static, FakeResponse> + Send + Sync>;

fn sync_handler<F>(f: F) -> Handler
where
    F: Fn(&RecordedRequest) -> FakeResponse + Send + Sync + 'static,
{
    Arc::new(move |req: RecordedRequest| {
        let resp = f(&req);
        Box::pin(async move { resp }) as BoxFuture<'static, FakeResponse>
    })
}

fn async_handler<F, Fut>(f: F) -> Handler
where
    F: Fn(RecordedRequest) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = FakeResponse> + Send + 'static,
{
    Arc::new(move |req: RecordedRequest| Box::pin(f(req)) as BoxFuture<'static, FakeResponse>)
}

fn sequence_handler(responses: Vec<FakeResponse>) -> Handler {
    assert!(
        !responses.is_empty(),
        "sequence needs at least one response"
    );
    let queue = Mutex::new(VecDeque::from(responses));
    sync_handler(move |_| {
        let mut q = queue.lock();
        if q.len() > 1 {
            q.pop_front().unwrap()
        } else {
            q.front().cloned().unwrap()
        }
    })
}

/// Shared by [`FakeServer`] and [`ScriptedClient`]: the handler, the request log and counters.
struct Shared {
    handler: Handler,
    requests: Mutex<Vec<RecordedRequest>>,
    hits: AtomicUsize,
    drops: AtomicUsize,
    changed: Notify,
}

impl Shared {
    fn new(handler: Handler) -> Arc<Self> {
        Arc::new(Self {
            handler,
            requests: Mutex::new(Vec::new()),
            hits: AtomicUsize::new(0),
            drops: AtomicUsize::new(0),
            changed: Notify::new(),
        })
    }

    fn record(&self, req: RecordedRequest) {
        self.requests.lock().push(req);
        self.hits.fetch_add(1, Ordering::SeqCst);
        self.changed.notify_waiters();
    }

    fn dropped(&self) {
        self.drops.fetch_add(1, Ordering::SeqCst);
        self.changed.notify_waiters();
    }

    fn requests(&self) -> Vec<RecordedRequest> {
        self.requests.lock().clone()
    }

    fn hits(&self) -> usize {
        self.hits.load(Ordering::SeqCst)
    }

    fn drops(&self) -> usize {
        self.drops.load(Ordering::SeqCst)
    }

    async fn wait_until(&self, what: &str, cond: impl Fn(&Self) -> bool) {
        let wait = async {
            loop {
                let notified = self.changed.notified();
                tokio::pin!(notified);
                notified.as_mut().enable();
                if cond(self) {
                    return;
                }
                notified.await;
            }
        };
        if tokio::time::timeout(WAIT_TIMEOUT, wait).await.is_err() {
            panic!(
                "timed out after {WAIT_TIMEOUT:?} waiting for {what} (hits = {}, drops = {})",
                self.hits(),
                self.drops()
            );
        }
    }

    /// Runs the handler, applying `Never` and the header delay. `Err` = reset with message.
    async fn respond(self: &Arc<Self>, req: RecordedRequest) -> Result<FakeResponse, String> {
        let guard = DropGuard::new(self.clone());
        let resp = (self.handler)(req).await;
        match &resp.outcome {
            Outcome::Respond => {}
            Outcome::Never => std::future::pending::<()>().await,
            Outcome::Reset(msg) => {
                guard.disarm();
                return Err(msg.clone());
            }
        }
        if let Some(d) = resp.header_delay {
            tokio::time::sleep(d).await;
        }
        guard.disarm();
        Ok(resp)
    }
}

/// Counts a drop when the request future is dropped before the response head is ready.
struct DropGuard {
    shared: Option<Arc<Shared>>,
}

impl DropGuard {
    fn new(shared: Arc<Shared>) -> Self {
        Self {
            shared: Some(shared),
        }
    }

    fn disarm(mut self) {
        self.shared = None;
    }
}

impl Drop for DropGuard {
    fn drop(&mut self) {
        if let Some(shared) = self.shared.take() {
            shared.dropped();
        }
    }
}

/// A response body that plays [`BodyStep`]s; counts a drop if dropped before it finished.
///
/// The body counts as finished as soon as its last chunk is handed out (or at once when it
/// has no steps) with [`BodyEnd::Finish`], or when a [`BodyStep::Fail`] is played: hyper drops
/// a fixed-length body after writing the declared bytes without polling it to `None`, so
/// waiting for `None` would count every fully sent `Content-Length` response as a drop.
struct ScriptedBody {
    steps: VecDeque<BodyStep>,
    end: BodyEnd,
    sleep: Option<Pin<Box<tokio::time::Sleep>>>,
    shared: Arc<Shared>,
    finished: bool,
    exact_len: Option<u64>,
}

impl ScriptedBody {
    fn new(resp: &FakeResponse, shared: Arc<Shared>) -> Self {
        let only_chunks = resp.end == BodyEnd::Finish
            && resp.steps.iter().all(|s| matches!(s, BodyStep::Chunk(_)));
        Self {
            steps: resp.steps.iter().cloned().collect(),
            end: resp.end,
            sleep: None,
            shared,
            finished: resp.end == BodyEnd::Finish && resp.steps.is_empty(),
            exact_len: only_chunks.then(|| resp.body_bytes().len() as u64),
        }
    }

    fn poll_chunk(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, String>>> {
        loop {
            if let Some(sleep) = self.sleep.as_mut() {
                if sleep.as_mut().poll(cx).is_pending() {
                    return Poll::Pending;
                }
                self.sleep = None;
            }
            match self.steps.pop_front() {
                Some(BodyStep::Chunk(b)) => {
                    if self.steps.is_empty() && self.end == BodyEnd::Finish {
                        self.finished = true;
                    }
                    return Poll::Ready(Some(Ok(b)));
                }
                Some(BodyStep::Delay(d)) => self.sleep = Some(Box::pin(tokio::time::sleep(d))),
                Some(BodyStep::Fail(msg)) => {
                    self.steps.clear();
                    self.end = BodyEnd::Finish;
                    self.finished = true;
                    return Poll::Ready(Some(Err(msg)));
                }
                None => {
                    return match self.end {
                        BodyEnd::Finish => {
                            self.finished = true;
                            Poll::Ready(None)
                        }
                        BodyEnd::Hang => Poll::Pending,
                    };
                }
            }
        }
    }
}

impl Drop for ScriptedBody {
    fn drop(&mut self) {
        if !self.finished {
            self.shared.dropped();
        }
    }
}

impl Body for ScriptedBody {
    type Data = Bytes;
    type Error = std::io::Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Bytes>, std::io::Error>>> {
        self.get_mut()
            .poll_chunk(cx)
            .map(|chunk| chunk.map(|r| r.map(Frame::data).map_err(std::io::Error::other)))
    }

    fn is_end_stream(&self) -> bool {
        self.finished
    }

    fn size_hint(&self) -> SizeHint {
        match self.exact_len {
            Some(n) => SizeHint::with_exact(n),
            None => SizeHint::default(),
        }
    }
}

fn build_response<B>(resp: &FakeResponse, body: B) -> http::Response<B> {
    let mut builder = http::Response::builder().status(resp.status);
    for (k, v) in &resp.headers {
        builder = builder.header(k.as_str(), v.as_str());
    }
    builder.body(body).expect("valid scripted response head")
}

fn transport_error(message: String) -> ModelError {
    ModelError::Transport(Box::new(std::io::Error::other(message)))
}

// --------------------------------------------------------------------------------- fake server

/// A local HTTP/1 server on `127.0.0.1`. Every request is recorded,
/// counted and answered by the handler. Dropping the server stops it and closes its
/// connections.
pub struct FakeServer {
    addr: SocketAddr,
    shared: Arc<Shared>,
    tasks: Arc<Mutex<Vec<tokio::task::AbortHandle>>>,
}

impl FakeServer {
    /// Starts a server answering each request with `handler(&request)`. Needs a tokio runtime.
    pub fn start<F>(handler: F) -> Self
    where
        F: Fn(&RecordedRequest) -> FakeResponse + Send + Sync + 'static,
    {
        Self::with_handler(sync_handler(handler))
    }

    /// Starts a server with an async handler (for handlers that wait on a signal first).
    pub fn start_async<F, Fut>(handler: F) -> Self
    where
        F: Fn(RecordedRequest) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = FakeResponse> + Send + 'static,
    {
        Self::with_handler(async_handler(handler))
    }

    /// Starts a server answering every request with a clone of `resp`.
    pub fn always(resp: FakeResponse) -> Self {
        Self::start(move |_| resp.clone())
    }

    /// Starts a server answering every request with `200 application/json` `body`.
    pub fn json(body: impl Into<String>) -> Self {
        Self::always(FakeResponse::json(body))
    }

    /// Starts a server answering request `i` with `responses[i]`; the last one repeats.
    pub fn sequence(responses: Vec<FakeResponse>) -> Self {
        Self::with_handler(sequence_handler(responses))
    }

    /// Starts a server with a prepared [`Handler`].
    pub fn with_handler(handler: Handler) -> Self {
        let std_listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind 127.0.0.1:0");
        std_listener.set_nonblocking(true).expect("set_nonblocking");
        let addr = std_listener.local_addr().expect("local_addr");
        let listener = tokio::net::TcpListener::from_std(std_listener).expect("tokio listener");
        let shared = Shared::new(handler);
        let tasks: Arc<Mutex<Vec<tokio::task::AbortHandle>>> = Arc::default();
        let base = format!("http://{addr}");

        let (acc_shared, acc_tasks) = (shared.clone(), tasks.clone());
        let accept = tokio::spawn(async move {
            loop {
                let Ok((stream, _)) = listener.accept().await else {
                    tokio::time::sleep(Duration::from_millis(10)).await;
                    continue;
                };
                let (shared, base) = (acc_shared.clone(), base.clone());
                let service = hyper::service::service_fn(move |req| {
                    let (shared, base) = (shared.clone(), base.clone());
                    async move { serve(shared, base, req).await }
                });
                let conn = tokio::spawn(async move {
                    let _ = hyper::server::conn::http1::Builder::new()
                        .serve_connection(hyper_util::rt::TokioIo::new(stream), service)
                        .await;
                });
                acc_tasks.lock().push(conn.abort_handle());
            }
        });
        tasks.lock().push(accept.abort_handle());
        Self {
            addr,
            shared,
            tasks,
        }
    }

    /// The base URL: `http://127.0.0.1:<port>`, no trailing slash.
    pub fn url(&self) -> String {
        format!("http://{}", self.addr)
    }

    /// The listening address.
    pub fn addr(&self) -> SocketAddr {
        self.addr
    }

    /// Every request received so far, in arrival order.
    pub fn requests(&self) -> Vec<RecordedRequest> {
        self.shared.requests()
    }

    /// The most recent request; panics when there is none.
    pub fn last_request(&self) -> RecordedRequest {
        self.requests()
            .pop()
            .expect("the fake server received no request")
    }

    /// Number of requests received.
    pub fn hits(&self) -> usize {
        self.shared.hits()
    }

    /// Number of responses the client abandoned: a [`FakeResponse::never`] request whose
    /// connection closed, or a body dropped before its last chunk was handed out
    /// (drop-cancellation evidence). A fully sent response, including a fixed-length or empty
    /// one, is never a drop.
    pub fn drops(&self) -> usize {
        self.shared.drops()
    }

    /// Waits until at least `n` requests arrived; panics after
    /// [`WAIT_TIMEOUT`].
    pub async fn wait_for_hits(&self, n: usize) {
        self.shared
            .wait_until(&format!("{n} request(s)"), |s| s.hits() >= n)
            .await;
    }

    /// Waits until at least `n` responses were abandoned by the client; panics after
    /// [`WAIT_TIMEOUT`].
    pub async fn wait_for_drops(&self, n: usize) {
        self.shared
            .wait_until(&format!("{n} dropped response(s)"), |s| s.drops() >= n)
            .await;
    }
}

impl Drop for FakeServer {
    fn drop(&mut self) {
        for task in self.tasks.lock().drain(..) {
            task.abort();
        }
    }
}

async fn read_incoming(mut body: Incoming) -> Result<Bytes, hyper::Error> {
    let mut out = Vec::new();
    while let Some(frame) = std::future::poll_fn(|cx| Pin::new(&mut body).poll_frame(cx)).await {
        if let Ok(data) = frame?.into_data() {
            out.extend_from_slice(&data);
        }
    }
    Ok(Bytes::from(out))
}

async fn serve(
    shared: Arc<Shared>,
    base: String,
    req: http::Request<Incoming>,
) -> Result<http::Response<ScriptedBody>, std::io::Error> {
    let (parts, body) = req.into_parts();
    let body = read_incoming(body).await.map_err(std::io::Error::other)?;
    let path_and_query = parts
        .uri
        .path_and_query()
        .map(|p| p.as_str().to_string())
        .unwrap_or_default();
    let recorded = RecordedRequest::from_parts(format!("{base}{path_and_query}"), &parts, body);
    shared.record(recorded.clone());
    let resp = shared
        .respond(recorded)
        .await
        .map_err(std::io::Error::other)?;
    let body = ScriptedBody::new(&resp, shared.clone());
    Ok(build_response(&resp, body))
}

// ------------------------------------------------------------------------------ scripted client

/// An in-process [`HttpClient`]: records each request and answers it
/// from the handler without a socket. [`Outcome::Reset`] becomes
/// `ModelError::Transport(<message>)`; a [`BodyStep::Fail`] becomes a body item
/// `Err(ModelError::Transport(<message>))`. Clone the `Arc` into a provider's `http_client`.
pub struct ScriptedClient {
    shared: Arc<Shared>,
}

impl ScriptedClient {
    /// A client answering each request with `handler(&request)`.
    pub fn new<F>(handler: F) -> Arc<Self>
    where
        F: Fn(&RecordedRequest) -> FakeResponse + Send + Sync + 'static,
    {
        Self::with_handler(sync_handler(handler))
    }

    /// A client with an async handler.
    pub fn new_async<F, Fut>(handler: F) -> Arc<Self>
    where
        F: Fn(RecordedRequest) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = FakeResponse> + Send + 'static,
    {
        Self::with_handler(async_handler(handler))
    }

    /// A client answering every request with a clone of `resp`.
    pub fn always(resp: FakeResponse) -> Arc<Self> {
        Self::new(move |_| resp.clone())
    }

    /// A client answering every request with `200 application/json` `body`.
    pub fn json(body: impl Into<String>) -> Arc<Self> {
        Self::always(FakeResponse::json(body))
    }

    /// A client answering request `i` with `responses[i]`; the last one repeats.
    pub fn sequence(responses: Vec<FakeResponse>) -> Arc<Self> {
        Self::with_handler(sequence_handler(responses))
    }

    /// A client with a prepared [`Handler`].
    pub fn with_handler(handler: Handler) -> Arc<Self> {
        Arc::new(Self {
            shared: Shared::new(handler),
        })
    }

    /// Every request sent so far.
    pub fn requests(&self) -> Vec<RecordedRequest> {
        self.shared.requests()
    }

    /// The most recent request; panics when there is none.
    pub fn last_request(&self) -> RecordedRequest {
        self.requests()
            .pop()
            .expect("the scripted client got no request")
    }

    /// Number of requests sent.
    pub fn hits(&self) -> usize {
        self.shared.hits()
    }

    /// Number of responses abandoned before completion (see [`FakeServer::drops`]).
    pub fn drops(&self) -> usize {
        self.shared.drops()
    }

    /// The tokio time between request `i` and request `i + 1`; panics when either is missing.
    pub fn gap(&self, i: usize) -> Duration {
        let requests = self.shared.requests.lock();
        requests[i + 1].at - requests[i].at
    }

    /// Waits until at least `n` requests were sent.
    pub async fn wait_for_hits(&self, n: usize) {
        self.shared
            .wait_until(&format!("{n} request(s)"), |s| s.hits() >= n)
            .await;
    }

    /// Waits until at least `n` responses were abandoned.
    pub async fn wait_for_drops(&self, n: usize) {
        self.shared
            .wait_until(&format!("{n} dropped response(s)"), |s| s.drops() >= n)
            .await;
    }
}

#[async_trait::async_trait]
impl HttpClient for ScriptedClient {
    async fn send(
        &self,
        req: http::Request<Bytes>,
    ) -> Result<http::Response<BodyStream>, ModelError> {
        let (parts, body) = req.into_parts();
        let recorded = RecordedRequest::from_parts(parts.uri.to_string(), &parts, body);
        self.shared.record(recorded.clone());
        let resp = self
            .shared
            .respond(recorded)
            .await
            .map_err(transport_error)?;
        let mut body = ScriptedBody::new(&resp, self.shared.clone());
        let stream: BodyStream = futures::stream::poll_fn(move |cx| {
            body.poll_chunk(cx)
                .map(|c| c.map(|r| r.map_err(transport_error)))
        })
        .boxed();
        Ok(build_response(&resp, stream))
    }
}

// ------------------------------------------------------------------------------------------ SSE

/// One SSE event: `event: <name>\ndata: <data>\n\n`.
pub fn sse_event(name: &str, data: &str) -> String {
    format!("event: {name}\ndata: {data}\n\n")
}

/// An SSE transcript written line by line, joined with `"\n"`.
pub fn sse_lines(lines: &[&str]) -> String {
    lines.join("\n")
}

// ---------------------------------------------------------------------------------- credentials

/// A credentials provider whose headers are always the same.
#[derive(Debug)]
struct FixedHeaders(http::HeaderMap);

impl google_cloud_auth::credentials::CredentialsProvider for FixedHeaders {
    async fn headers(
        &self,
        _extensions: http::Extensions,
    ) -> Result<
        google_cloud_auth::credentials::CacheableResource<http::HeaderMap>,
        google_cloud_auth::errors::CredentialsError,
    > {
        Ok(google_cloud_auth::credentials::CacheableResource::New {
            entity_tag: google_cloud_auth::credentials::EntityTag::new(),
            data: self.0.clone(),
        })
    }

    async fn universe_domain(&self) -> Option<String> {
        None
    }
}

/// Credentials whose headers are always `headers`.
pub fn fixed_header_credentials(
    headers: http::HeaderMap,
) -> google_cloud_auth::credentials::Credentials {
    google_cloud_auth::credentials::Credentials::from(FixedHeaders(headers))
}

/// Credentials whose headers are always `Authorization: Bearer {token}`. Panics if `token` is
/// not a valid header value.
pub fn bearer_credentials(token: &str) -> google_cloud_auth::credentials::Credentials {
    fixed_header_credentials(http::HeaderMap::from_iter([(
        http::header::AUTHORIZATION,
        http::HeaderValue::from_str(&format!("Bearer {token}")).expect("ASCII token"),
    )]))
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::*;
    use crate::gcp_auth::credential_headers;
    use crate::http::collect_body;

    #[tokio::test]
    async fn bearer_credentials_send_a_fixed_token() {
        let headers = credential_headers(&bearer_credentials("t")).await.unwrap();
        assert_eq!(headers.len(), 1);
        assert_eq!(headers[http::header::AUTHORIZATION], "Bearer t");
        let custom = http::HeaderMap::from_iter([(
            http::header::HeaderName::from_static("x-goog-user-project"),
            http::HeaderValue::from_static("p"),
        )]);
        assert_eq!(
            credential_headers(&fixed_header_credentials(custom.clone()))
                .await
                .unwrap(),
            custom
        );
    }

    const MSG: &str = r#"{"id":"msg_1","text":"ok"}"#;

    #[tokio::test]
    async fn server_records_request_and_answers_json() {
        let srv = FakeServer::json(MSG);
        let resp = reqwest::Client::new()
            .post(format!("{}/v1/messages?beta=true", srv.url()))
            .header("X-Api-Key", "k")
            .header("anthropic-version", "2023-06-01")
            .body(r#"{"model":"m","stream":true}"#)
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), 200);
        assert_eq!(resp.headers()["content-type"], "application/json");
        assert_eq!(resp.text().await.unwrap(), MSG);

        assert_eq!(srv.hits(), 1);
        let req = srv.last_request();
        assert_eq!(req.method, "POST");
        assert_eq!(req.path, "/v1/messages");
        assert_eq!(req.query.as_deref(), Some("beta=true"));
        assert_eq!(req.path_and_query(), "/v1/messages?beta=true");
        assert_eq!(req.url, format!("{}/v1/messages?beta=true", srv.url()));
        assert_eq!(req.header("x-api-key"), "k");
        assert_eq!(req.header("Anthropic-Version"), "2023-06-01");
        assert_eq!(req.header("authorization"), "");
        assert_eq!(req.json()["model"], "m");
        assert!(req.is_stream());
        assert_eq!(req.body_text(), r#"{"model":"m","stream":true}"#);
    }

    #[tokio::test]
    async fn server_streams_chunks_with_delays() {
        let events = [
            sse_event("message_start", r#"{"type":"message_start"}"#),
            sse_event("message_stop", r#"{"type":"message_stop"}"#),
        ];
        let resp = FakeResponse::sse_chunks(events.clone())
            .delay(Duration::from_millis(20))
            .chunk("event: ping\ndata: {}\n\n");
        let srv = FakeServer::always(resp);
        let resp = reqwest::get(srv.url()).await.unwrap();
        assert_eq!(resp.headers()["content-type"], "text/event-stream");
        let mut body = resp.bytes_stream();
        let mut got = Vec::new();
        while let Some(chunk) = body.next().await {
            got.extend_from_slice(&chunk.unwrap());
        }
        assert_eq!(
            String::from_utf8(got).unwrap(),
            format!("{}{}event: ping\ndata: {{}}\n\n", events[0], events[1])
        );
        assert_eq!(srv.drops(), 0);
    }

    #[tokio::test]
    async fn server_fully_read_fixed_length_bodies_are_not_drops() {
        let srv = FakeServer::json(MSG);
        let client = reqwest::Client::new();
        for _ in 0..2 {
            let resp = client.post(srv.url()).body("{}").send().await.unwrap();
            assert!(resp.headers().contains_key("content-length"));
            assert!(resp.text().await.unwrap().contains("msg_1"));
        }
        // hyper releases the body after the response is written; give it time to do so.
        tokio::time::sleep(Duration::from_millis(100)).await;
        assert_eq!((srv.hits(), srv.drops()), (2, 0));
    }

    #[tokio::test]
    async fn server_fully_read_empty_body_is_not_a_drop() {
        let srv = FakeServer::always(FakeResponse::new(204));
        let resp = reqwest::get(srv.url()).await.unwrap();
        assert_eq!(resp.status(), 204);
        assert!(resp.bytes().await.unwrap().is_empty());
        tokio::time::sleep(Duration::from_millis(100)).await;
        assert_eq!((srv.hits(), srv.drops()), (1, 0));
    }

    #[tokio::test]
    async fn server_observes_client_drop_mid_body() {
        let srv = FakeServer::always(
            FakeResponse::sse(sse_event("message_start", "{}"))
                .delay(Duration::from_millis(10))
                .hang(),
        );
        let resp = reqwest::get(srv.url()).await.unwrap();
        let mut body = resp.bytes_stream();
        let first = body.next().await.unwrap().unwrap();
        assert_eq!(&first[..], b"event: message_start\ndata: {}\n\n");
        drop(body);
        srv.wait_for_drops(1).await;
        assert_eq!(srv.drops(), 1);
    }

    #[tokio::test]
    async fn server_observes_drop_of_unanswered_request() {
        let srv = FakeServer::always(FakeResponse::never());
        let url = srv.url();
        let client = tokio::spawn(async move { reqwest::get(url).await });
        srv.wait_for_hits(1).await;
        assert_eq!(srv.drops(), 0);
        client.abort();
        srv.wait_for_drops(1).await;
    }

    #[tokio::test]
    async fn server_async_handler_waits_for_release() {
        let release = Arc::new(Notify::new());
        let gate = release.clone();
        let srv = FakeServer::start_async(move |req| {
            let gate = gate.clone();
            async move {
                gate.notified().await;
                FakeResponse::json(format!(r#"{{"path":"{}"}}"#, req.path))
            }
        });
        let url = format!("{}/v1/messages", srv.url());
        let client = tokio::spawn(async move { reqwest::get(url).await.unwrap().text().await });
        srv.wait_for_hits(1).await;
        release.notify_one();
        assert_eq!(client.await.unwrap().unwrap(), r#"{"path":"/v1/messages"}"#);
    }

    #[tokio::test]
    async fn server_sequence_status_headers_and_reset() {
        let srv = FakeServer::sequence(vec![
            FakeResponse::json_status(529, r#"{"error":"overloaded"}"#)
                .header("Retry-After-Ms", "1"),
            FakeResponse::reset("connection reset by script"),
            FakeResponse::json("{}").delay_headers(Duration::from_millis(5)),
        ]);
        let client = reqwest::Client::new();
        let first = client.get(srv.url()).send().await.unwrap();
        assert_eq!(first.status(), 529);
        assert_eq!(first.headers()["retry-after-ms"], "1");
        assert!(client.get(srv.url()).send().await.is_err());
        for _ in 0..2 {
            let ok = client.get(srv.url()).send().await.unwrap();
            assert_eq!(ok.status(), 200);
            assert_eq!(ok.text().await.unwrap(), "{}");
        }
        assert_eq!(srv.hits(), 4);
    }

    #[tokio::test]
    async fn server_body_failure_aborts_the_body() {
        // The pause lets hyper send the head and the first chunk before the body fails.
        let srv = FakeServer::always(
            FakeResponse::sse("data: a\n\n")
                .delay(Duration::from_millis(20))
                .fail("unexpected EOF"),
        );
        let resp = reqwest::get(srv.url()).await.unwrap();
        let mut body = resp.bytes_stream();
        let mut got = Vec::new();
        let mut failed = false;
        while let Some(chunk) = body.next().await {
            match chunk {
                Ok(c) => got.extend_from_slice(&c),
                Err(_) => failed = true,
            }
        }
        assert!(failed, "the body must fail");
        assert_eq!(&got[..], b"data: a\n\n");
        assert_eq!(srv.drops(), 0);
    }

    fn request(uri: &str, body: &'static [u8]) -> http::Request<Bytes> {
        http::Request::builder()
            .method("POST")
            .uri(uri)
            .header("x-api-key", "k")
            .body(Bytes::from_static(body))
            .unwrap()
    }

    #[tokio::test]
    async fn scripted_client_records_and_streams() {
        let client = ScriptedClient::always(
            FakeResponse::sse_chunks(["a", "b"])
                .delay(Duration::from_millis(5))
                .chunk("c"),
        );
        let resp = client
            .send(request(
                "https://api.anthropic.com/v1/messages",
                br#"{"stream":true}"#,
            ))
            .await
            .unwrap();
        assert_eq!(resp.status(), 200);
        assert_eq!(resp.headers()["content-type"], "text/event-stream");
        let body = collect_body(resp.into_body()).await.unwrap();
        assert_eq!(&body[..], b"abc");
        let rec = client.last_request();
        assert_eq!(rec.url, "https://api.anthropic.com/v1/messages");
        assert_eq!(rec.path, "/v1/messages");
        assert_eq!(rec.header("X-Api-Key"), "k");
        assert!(rec.is_stream());
        assert_eq!((client.hits(), client.drops()), (1, 0));
    }

    #[tokio::test]
    async fn scripted_client_reset_and_drop() {
        let client = ScriptedClient::sequence(vec![
            FakeResponse::reset("boom"),
            FakeResponse::sse("x").hang(),
        ]);
        match client.send(request("http://h/", b"")).await {
            Err(ModelError::Transport(e)) => assert_eq!(e.to_string(), "boom"),
            other => panic!("want transport error, got {:?}", other.map(|r| r.status())),
        }
        let resp = client.send(request("http://h/", b"")).await.unwrap();
        let mut body = resp.into_body();
        assert_eq!(&body.next().await.unwrap().unwrap()[..], b"x");
        drop(body);
        assert_eq!(client.drops(), 1);
        client.wait_for_drops(1).await;
    }

    #[tokio::test]
    async fn scripted_client_body_failure_ends_the_body() {
        let client = ScriptedClient::always(
            FakeResponse::json("{")
                .fail("unexpected EOF")
                .chunk("never sent"),
        );
        let resp = client.send(request("http://h/", b"")).await.unwrap();
        let mut body = resp.into_body();
        assert_eq!(&body.next().await.unwrap().unwrap()[..], b"{");
        match body.next().await {
            Some(Err(ModelError::Transport(e))) => assert_eq!(e.to_string(), "unexpected EOF"),
            other => panic!("want a transport body error, got {other:?}"),
        }
        assert!(body.next().await.is_none());
        drop(body);
        assert_eq!(client.drops(), 0, "a scripted failure is not a client drop");
    }

    #[tokio::test(start_paused = true)]
    async fn scripted_client_never_counts_a_drop_and_gap_measures_tokio_time() {
        let client =
            ScriptedClient::sequence(vec![FakeResponse::never(), FakeResponse::json("{}")]);
        let first = tokio::time::timeout(
            Duration::from_secs(3),
            client.send(request("http://h/", b"")),
        )
        .await;
        assert!(first.is_err(), "never answered");
        assert_eq!(client.drops(), 1);
        tokio::time::sleep(Duration::from_secs(2)).await;
        client.send(request("http://h/", b"")).await.unwrap();
        assert_eq!(client.gap(0), Duration::from_secs(5));
        assert_eq!(client.requests().len(), 2);
    }

    #[test]
    fn sse_builders_match_go_layout() {
        assert_eq!(
            sse_event("message_stop", r#"{"type":"message_stop"}"#),
            "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
        );
        assert_eq!(
            sse_lines(&["event: a", "data: {}", "", "event: b", "data: {}", ""]),
            "event: a\ndata: {}\n\nevent: b\ndata: {}\n"
        );
    }

    #[test]
    fn fake_response_builders() {
        let r = FakeResponse::json_status(429, "{}")
            .header("Retry-After", "1")
            .delay(Duration::from_secs(1))
            .chunk("x")
            .fail("eof")
            .status(503)
            .delay_headers(Duration::from_secs(2))
            .hang();
        assert_eq!(r.status, 503);
        assert_eq!(
            r.headers,
            vec![
                ("Content-Type".to_string(), "application/json".to_string()),
                ("Retry-After".to_string(), "1".to_string()),
            ]
        );
        assert_eq!(r.header_delay, Some(Duration::from_secs(2)));
        assert_eq!(r.end, BodyEnd::Hang);
        assert_eq!(r.outcome, Outcome::Respond);
        assert_eq!(&r.body_bytes()[..], b"{}x");
        assert!(matches!(r.steps[3], BodyStep::Fail(ref m) if m == "eof"));
        assert_eq!(FakeResponse::never().outcome, Outcome::Never);
        assert_eq!(
            FakeResponse::reset("x").outcome,
            Outcome::Reset("x".to_string())
        );
    }
}