runnerd-core 0.1.0

Config, domain logic and API types shared by runnerd and its clients
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
//! The GitHub client against a fake GitHub on a loopback port: queued jobs, listings over several pages,
//! conditional requests and how much of them is kept, rate limits, token refresh, runner registration and deletion, who started a run and opened a pull request, and that the token
//! never shows in `Debug` output or errors.

use std::fmt;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};

use axum::Router;
use axum::http::{HeaderMap, HeaderValue, Method, StatusCode, Uri};
use axum::response::{IntoResponse, Response};

use super::{Class, Conditional, Deleted, Error, Github, GithubApi, QueuedJob, Token};
use crate::sync::lock;

const TOKEN: &str = "ghp_sentinelTokenValue0123456789";

/// A request the fake GitHub received.
#[derive(Debug, Clone)]
struct Seen {
    method: Method,
    uri: String,
    headers: HeaderMap,
}

type Handler = dyn Fn(&Seen) -> Response + Send + Sync;

/// Serves `handler` on a loopback port in place of api.github.com; returns its URL and what it received.
async fn fake_github(
    handler: impl Fn(&Seen) -> Response + Send + Sync + 'static,
) -> (String, Arc<Mutex<Vec<Seen>>>) {
    let seen = Arc::new(Mutex::new(Vec::new()));
    let handler: Arc<Handler> = Arc::new(handler);
    let log = seen.clone();
    let app = Router::new().fallback(move |method: Method, uri: Uri, headers: HeaderMap| {
        let request = Seen {
            method,
            uri: uri.to_string(),
            headers,
        };
        log.lock().unwrap().push(request.clone());
        let response = handler(&request);
        async move { response }
    });
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let url = format!("http://{}", listener.local_addr().unwrap());
    tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
    (url, seen)
}

fn client(url: &str) -> Github {
    Github::new(url, Box::new(|| Ok(Token(TOKEN.to_owned())))).unwrap()
}

fn json(body: &str) -> Response {
    ([("content-type", "application/json")], body.to_owned()).into_response()
}

#[tokio::test]
async fn queued_jobs_come_from_queued_and_started_runs() {
    let (url, _) = fake_github(|request| match request.uri.as_str() {
        "/repos/o/r/actions/runs?status=queued&per_page=100" => {
            json(r#"{"total_count":1,"workflow_runs":[{"id":10}]}"#)
        }
        "/repos/o/r/actions/runs?status=in_progress&per_page=100" => {
            json(r#"{"total_count":1,"workflow_runs":[{"id":20}]}"#)
        }
        "/repos/o/r/actions/runs/10/jobs?filter=latest&per_page=100" => json(
            r#"{"jobs":[{"id":1,"run_id":10,"name":"smoke","status":"queued","labels":["self-hosted","sandbox"]}]}"#,
        ),
        // A matrix run: one job started, one still waiting for a runner.
        "/repos/o/r/actions/runs/20/jobs?filter=latest&per_page=100" => json(
            r#"{"jobs":[{"id":2,"run_id":20,"name":"a","status":"in_progress","labels":["self-hosted"]},
                       {"id":3,"run_id":20,"name":"b","status":"queued","labels":["self-hosted"]}]}"#,
        ),
        _ => StatusCode::NOT_FOUND.into_response(),
    })
    .await;
    let jobs = client(&url).queued_jobs("o/r").await.unwrap();
    assert_eq!(
        jobs,
        [
            QueuedJob {
                id: 1,
                run_id: 10,
                name: "smoke".to_owned(),
                labels: vec!["self-hosted".to_owned(), "sandbox".to_owned()],
                run_attempt: 1,
                ..QueuedJob::default()
            },
            QueuedJob {
                id: 3,
                run_id: 20,
                name: "b".to_owned(),
                labels: vec!["self-hosted".to_owned()],
                run_attempt: 1,
                ..QueuedJob::default()
            },
        ]
    );
}

#[tokio::test]
async fn an_unchanged_answer_is_served_from_the_last_response() {
    let (url, seen) = fake_github(|request| {
        if request
            .headers
            .get("if-none-match")
            .map(HeaderValue::as_bytes)
            == Some(b"\"v1\"")
        {
            return StatusCode::NOT_MODIFIED.into_response();
        }
        (
            [("etag", "\"v1\"")],
            json(r#"{"runners":[{"id":7,"name":"runnerd-a","status":"online","busy":true}]}"#),
        )
            .into_response()
    })
    .await;
    let github = client(&url);
    let first = github.runners("o/r").await.unwrap();
    let second = github.runners("o/r").await.unwrap();
    assert_eq!(first, second);
    assert_eq!(second[0].id, 7);
    assert!(
        seen.lock().unwrap()[1]
            .headers
            .contains_key("if-none-match")
    );
}

/// `body`, with a `Link` to a next page if `next`, tagged by its URI: a request conditional on that tag gets a 304.
fn page(request: &Seen, body: &str, next: bool) -> Response {
    let etag = format!("\"{}\"", request.uri);
    if request
        .headers
        .get("if-none-match")
        .is_some_and(|sent| sent.as_bytes() == etag.as_bytes())
    {
        return StatusCode::NOT_MODIFIED.into_response();
    }
    let mut response = ([("etag", etag)], json(body)).into_response();
    if next {
        // GitHub links pages by repository ID; the client builds its own next address.
        let link = r#"<https://api.github.com/repositories/1/elsewhere?page=2>; rel="next", <https://api.github.com/repositories/1/elsewhere?page=9>; rel="last""#;
        response
            .headers_mut()
            .insert("link", HeaderValue::from_static(link));
    }
    response
}

fn uris(seen: &Mutex<Vec<Seen>>) -> Vec<String> {
    seen.lock()
        .unwrap()
        .iter()
        .map(|request| request.uri.clone())
        .collect()
}

#[tokio::test]
async fn a_queued_job_on_a_later_page_of_runs_and_jobs_is_found() {
    let (url, seen) = fake_github(|request| match request.uri.as_str() {
        "/repos/o/r/actions/runs?status=queued&per_page=100" => {
            page(request, r#"{"workflow_runs":[{"id":10}]}"#, true)
        }
        // Run 10 moved down a page while the first was read.
        "/repos/o/r/actions/runs?status=queued&per_page=100&page=2" => {
            page(request, r#"{"workflow_runs":[{"id":10},{"id":20}]}"#, false)
        }
        "/repos/o/r/actions/runs?status=in_progress&per_page=100" => {
            page(request, r#"{"workflow_runs":[]}"#, false)
        }
        "/repos/o/r/actions/runs/10/jobs?filter=latest&per_page=100" => page(
            request,
            r#"{"jobs":[{"id":1,"run_id":10,"name":"a","status":"completed","labels":[]}]}"#,
            false,
        ),
        // A matrix whose first page is done; the only job still waiting is on the second.
        "/repos/o/r/actions/runs/20/jobs?filter=latest&per_page=100" => page(
            request,
            r#"{"jobs":[{"id":2,"run_id":20,"name":"m (1)","status":"completed","labels":[]}]}"#,
            true,
        ),
        "/repos/o/r/actions/runs/20/jobs?filter=latest&per_page=100&page=2" => page(
            request,
            r#"{"jobs":[{"id":3,"run_id":20,"name":"m (101)","status":"queued","labels":["self-hosted"]}]}"#,
            false,
        ),
        _ => StatusCode::NOT_FOUND.into_response(),
    })
    .await;
    let github = client(&url);
    let first = github.queued_jobs("o/r").await.unwrap();
    let sent = seen.lock().unwrap().len();
    let second = github.queued_jobs("o/r").await.unwrap();
    for jobs in [&first, &second] {
        assert_eq!(
            jobs.iter().map(|job| job.id).collect::<Vec<_>>(),
            [3],
            "the queued job on page 2 of run 20, found again from 304s"
        );
    }
    assert_eq!(
        uris(&seen)
            .iter()
            .filter(|uri| uri.contains("/runs/10/jobs"))
            .count(),
        2,
        "run 10 is read once a poll though it showed on two pages"
    );
    assert!(
        seen.lock().unwrap()[sent..]
            .iter()
            .all(|request| request.headers.contains_key("if-none-match")),
        "every page of the second poll is conditional"
    );
}

#[tokio::test]
async fn an_orphan_runner_on_a_later_page_is_listed() {
    let (url, _) = fake_github(|request| match request.uri.as_str() {
        "/repos/o/r/actions/runners?per_page=100" => page(
            request,
            r#"{"runners":[{"id":1,"name":"runnerd-h-p-1","status":"online","busy":true}]}"#,
            true,
        ),
        "/repos/o/r/actions/runners?per_page=100&page=2" => page(
            request,
            r#"{"runners":[{"id":1,"name":"runnerd-h-p-1","status":"online","busy":true},
                           {"id":2,"name":"runnerd-h-p-2","status":"offline","busy":false}]}"#,
            false,
        ),
        _ => StatusCode::NOT_FOUND.into_response(),
    })
    .await;
    let runners = client(&url).runners("o/r").await.unwrap();
    assert_eq!(
        runners.iter().map(|runner| runner.id).collect::<Vec<_>>(),
        [1, 2]
    );
}

#[tokio::test]
async fn a_listing_that_never_ends_is_read_ten_pages_deep() {
    let (url, seen) = fake_github(|request| page(request, r#"{"runners":[]}"#, true)).await;
    client(&url).runners("o/r").await.unwrap();
    assert_eq!(seen.lock().unwrap().len(), 10);
}

/// A listing of runs `ids` split into pages of 100, answering for page `number`.
fn runs_page(request: &Seen, ids: &[u64], number: usize) -> Response {
    let pages: Vec<&[u64]> = ids.chunks(100).collect();
    let runs: Vec<String> = pages
        .get(number - 1)
        .map_or(&[][..], |page| *page)
        .iter()
        .map(|id| format!(r#"{{"id":{id}}}"#))
        .collect();
    page(
        request,
        &format!(r#"{{"workflow_runs":[{}]}}"#, runs.join(",")),
        number < pages.len(),
    )
}

#[tokio::test]
async fn a_busy_repository_is_polled_queued_runs_first_then_the_oldest_within_the_old_run_count() {
    // 250 runs in progress on GitHub's own runners, and 150 newer queued runs, each listed newest first.
    let (url, seen) = fake_github(|request| {
        let uri = request.uri.as_str();
        let number = |rest: &str| {
            rest.strip_prefix("&page=")
                .map_or(1, |number| number.parse().unwrap())
        };
        if let Some(rest) = uri.strip_prefix("/repos/o/r/actions/runs?status=queued&per_page=100") {
            return runs_page(request, &(301..=450).rev().collect::<Vec<_>>(), number(rest));
        }
        if let Some(rest) =
            uri.strip_prefix("/repos/o/r/actions/runs?status=in_progress&per_page=100")
        {
            return runs_page(request, &(1..=250).rev().collect::<Vec<_>>(), number(rest));
        }
        let (run, rest) = uri
            .trim_start_matches("/repos/o/r/actions/runs/")
            .split_once("/jobs?")
            .unwrap();
        let run: u64 = run.parse().unwrap();
        let job = |id: u64, status: &str| {
            format!(r#"{{"jobs":[{{"id":{id},"run_id":{run},"name":"j","status":"{status}","labels":[]}}]}}"#)
        };
        // Run 50, the last read, has its queued job on a second page.
        match (run, rest.ends_with("&page=2")) {
            (50, false) => page(request, &job(5000, "completed"), true),
            _ => page(request, &job(run, "queued"), false),
        }
    })
    .await;
    let jobs = client(&url).queued_jobs("o/r").await.unwrap();
    assert_eq!(
        jobs.iter().map(|job| job.id).collect::<Vec<_>>(),
        (1..=50).chain(301..=450).collect::<Vec<_>>(),
        "every queued run, then the oldest runs in progress up to 200 runs, the last read to its end"
    );
    // Five pages of runs, 200 runs' jobs, and run 50's second page.
    assert_eq!(seen.lock().unwrap().len(), 206);
}

/// Serves job `id` as completed when `id` is in `completed`, otherwise in progress with a name `pad` long.
async fn fake_jobs(
    completed: std::ops::RangeInclusive<u64>,
    pad: usize,
) -> (String, Arc<Mutex<Vec<Seen>>>) {
    fake_github(move |request| {
        let id: u64 = request
            .uri
            .trim_start_matches("/repos/o/r/actions/jobs/")
            .parse()
            .unwrap();
        let (status, conclusion) = if completed.contains(&id) {
            ("completed", r#""success""#)
        } else {
            ("in_progress", "null")
        };
        let name = "n".repeat(pad);
        page(
            request,
            &format!(
                r#"{{"id":{id},"run_id":1,"name":"{name}","status":"{status}","conclusion":{conclusion}}}"#
            ),
            false,
        )
    })
    .await
}

fn conditional(seen: &Mutex<Vec<Seen>>, uri: &str) -> Vec<bool> {
    seen.lock()
        .unwrap()
        .iter()
        .filter(|request| request.uri == uri)
        .map(|request| request.headers.contains_key("if-none-match"))
        .collect()
}

#[tokio::test]
async fn completed_jobs_leave_the_cache_while_an_active_job_stays_conditional() {
    let (url, seen) = fake_jobs(2..=200, 0).await;
    let github = client(&url);
    github.job("o/r", 1).await.unwrap();
    for id in 2..=200 {
        assert_eq!(github.job("o/r", id).await.unwrap().status, "completed");
    }
    let active = github.job("o/r", 1).await.unwrap();
    assert_eq!(
        active.status, "in_progress",
        "read from the 304's kept answer"
    );
    assert_eq!(
        conditional(&seen, "/repos/o/r/actions/jobs/1"),
        [false, true]
    );
    assert_eq!(
        lock(&github.etags).entries.len(),
        1,
        "only the active job's answer is kept"
    );
}

#[tokio::test]
async fn kept_answers_stay_within_their_byte_limit_and_the_least_recently_used_go() {
    let (url, seen) = fake_jobs(0..=0, 400).await;
    let mut github = client(&url);
    let limit = 2_000;
    // Every answer may go at once: least recently used first.
    *github.etags.get_mut().unwrap() = Conditional::new(limit, Duration::ZERO);
    for id in 2..=20 {
        github.job("o/r", id).await.unwrap();
        // Job 1 is asked between every other: the most recently used.
        github.job("o/r", 1).await.unwrap();
        assert!(
            lock(&github.etags).bytes <= limit,
            "within {limit} bytes after job {id}"
        );
    }
    github.job("o/r", 2).await.unwrap();
    assert!(
        conditional(&seen, "/repos/o/r/actions/jobs/1")[1..]
            .iter()
            .all(|sent| *sent),
        "job 1 stays conditional"
    );
    assert_eq!(
        conditional(&seen, "/repos/o/r/actions/jobs/2"),
        [false, false],
        "job 2, unused the longest, was dropped"
    );
}

#[tokio::test]
async fn polls_that_ask_more_than_the_cache_holds_keep_the_answers_that_fit_conditional() {
    let (url, seen) = fake_jobs(0..=0, 400).await;
    let mut github = client(&url);
    let limit = 2_000;
    *github.etags.get_mut().unwrap() = Conditional::new(limit, Duration::from_secs(600));
    // Two polls asking the same ten jobs in the same order, five times what the cache holds.
    for _ in 0..2 {
        for id in 1..=10 {
            github.job("o/r", id).await.unwrap();
        }
    }
    assert!(lock(&github.etags).bytes <= limit, "within {limit} bytes");
    for id in 1..=2 {
        assert_eq!(
            conditional(&seen, &format!("/repos/o/r/actions/jobs/{id}")),
            [false, true],
            "job {id}, kept from the first poll, is conditional in the second"
        );
    }
}

#[tokio::test]
async fn a_used_up_rate_limit_delays_requests_until_its_reset() {
    let reset = SystemTime::now() + Duration::from_secs(3600);
    let reset_secs = reset
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap()
        .as_secs();
    let (url, seen) = fake_github(move |_| {
        (
            StatusCode::FORBIDDEN,
            [
                ("x-ratelimit-remaining", "0".to_owned()),
                ("x-ratelimit-reset", reset_secs.to_string()),
            ],
            r#"{"message":"API rate limit exceeded"}"#,
        )
            .into_response()
    })
    .await;
    let github = client(&url);
    let expected = SystemTime::UNIX_EPOCH + Duration::from_secs(reset_secs);
    assert!(matches!(
        github.runners("o/r").await,
        Err(Error::RateLimited { until }) if until == expected
    ));
    assert_eq!(github.blocked_until(), Some(expected));
    // The next poll is not sent at all before the reset.
    assert!(matches!(
        github.queued_jobs("o/r").await,
        Err(Error::RateLimited { .. })
    ));
    assert_eq!(seen.lock().unwrap().len(), 1);
}

#[tokio::test]
async fn a_secondary_rate_limit_waits_for_retry_after() {
    let (url, _) =
        fake_github(|_| (StatusCode::TOO_MANY_REQUESTS, [("retry-after", "60")]).into_response())
            .await;
    let github = client(&url);
    let before = SystemTime::now();
    assert!(matches!(
        github.runners("o/r").await,
        Err(Error::RateLimited { .. })
    ));
    let until = github.blocked_until().unwrap();
    assert!(until >= before + Duration::from_secs(60));
}

#[tokio::test]
async fn a_rejected_token_is_read_again_once() {
    let (url, seen) = fake_github(|request| {
        if request
            .headers
            .get("authorization")
            .map(HeaderValue::as_bytes)
            == Some(b"Bearer new")
        {
            json(r#"{"runners":[]}"#)
        } else {
            StatusCode::UNAUTHORIZED.into_response()
        }
    })
    .await;
    let reads = Arc::new(Mutex::new(0));
    let counter = reads.clone();
    let github = Github::new(
        &url,
        Box::new(move || {
            let mut reads = counter.lock().unwrap();
            *reads += 1;
            Ok(Token(if *reads == 1 { "old" } else { "new" }.to_owned()))
        }),
    )
    .unwrap();
    assert_eq!(github.runners("o/r").await.unwrap(), []);
    assert_eq!(*reads.lock().unwrap(), 2);
    assert_eq!(seen.lock().unwrap().len(), 2);
}

/// A client whose token source counts its reads and gives `old` first, `new` after.
fn rotating_client(url: &str, reads: &Arc<Mutex<u32>>, wait: Duration) -> Github {
    let counter = reads.clone();
    Github::new(
        url,
        Box::new(move || {
            std::thread::sleep(wait);
            let mut reads = counter.lock().unwrap();
            *reads += 1;
            Ok(Token(if *reads == 1 { "old" } else { "new" }.to_owned()))
        }),
    )
    .unwrap()
}

async fn new_token_only() -> String {
    let (url, _) = fake_github(|request| {
        if request
            .headers
            .get("authorization")
            .map(HeaderValue::as_bytes)
            == Some(b"Bearer new")
        {
            json(r#"{"runners":[]}"#)
        } else {
            StatusCode::UNAUTHORIZED.into_response()
        }
    })
    .await;
    url
}

#[tokio::test]
async fn requests_refused_together_read_the_token_again_once() {
    let url = new_token_only().await;
    let reads = Arc::new(Mutex::new(0));
    let github = rotating_client(&url, &reads, Duration::ZERO);
    let (a, b, c, d) = tokio::join!(
        github.runners("o/a"),
        github.runners("o/b"),
        github.runners("o/c"),
        github.runners("o/d")
    );
    for answer in [a, b, c, d] {
        assert_eq!(answer.unwrap(), []);
    }
    assert_eq!(*reads.lock().unwrap(), 2);
}

#[tokio::test]
async fn a_slow_token_source_does_not_hold_up_other_tasks() {
    let url = new_token_only().await;
    let reads = Arc::new(Mutex::new(0));
    let github = rotating_client(&url, &reads, Duration::from_millis(300));
    let ticks = Arc::new(Mutex::new(0));
    let ticker = tokio::spawn({
        let ticks = ticks.clone();
        async move {
            loop {
                tokio::time::sleep(Duration::from_millis(10)).await;
                *ticks.lock().unwrap() += 1;
            }
        }
    });
    assert_eq!(github.runners("o/r").await.unwrap(), []);
    ticker.abort();
    // Two reads of 300 ms each: a source run on this thread would leave the ticker none.
    assert!(*ticks.lock().unwrap() > 20, "other tasks ran meanwhile");
}

#[tokio::test]
async fn deleting_a_runner_tells_gone_and_busy_apart() {
    let (url, _) = fake_github(|request| match request.uri.as_str() {
        "/repos/o/r/actions/runners/1" => StatusCode::NO_CONTENT.into_response(),
        "/repos/o/r/actions/runners/2" => StatusCode::NOT_FOUND.into_response(),
        "/repos/o/r/actions/runners/3" => (
            StatusCode::UNPROCESSABLE_ENTITY,
            r#"{"message":"Runner is still running a job"}"#,
        )
            .into_response(),
        _ => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
    })
    .await;
    let github = client(&url);
    assert_eq!(
        github.delete_runner("o/r", 1).await.unwrap(),
        Deleted::Deleted
    );
    assert_eq!(
        github.delete_runner("o/r", 2).await.unwrap(),
        Deleted::NotFound
    );
    assert_eq!(github.delete_runner("o/r", 3).await.unwrap(), Deleted::Busy);
    assert!(github.delete_runner("o/r", 4).await.is_err());
}

#[tokio::test]
async fn cancelling_a_run_that_already_completed_is_not_an_error() {
    let (url, seen) = fake_github(|request| match request.uri.as_str() {
        "/repos/o/r/actions/runs/10/cancel" => StatusCode::ACCEPTED.into_response(),
        "/repos/o/r/actions/runs/20/cancel" => (
            StatusCode::CONFLICT,
            r#"{"message":"Cannot cancel a workflow run that is completed."}"#,
        )
            .into_response(),
        _ => (
            StatusCode::FORBIDDEN,
            r#"{"message":"Resource not accessible"}"#,
        )
            .into_response(),
    })
    .await;
    let github = client(&url);
    github.cancel_run("o/r", 10).await.unwrap();
    github.cancel_run("o/r", 20).await.unwrap();
    let error = github.cancel_run("o/r", 30).await.unwrap_err();
    assert!(
        error.to_string().contains("Resource not accessible"),
        "{error}"
    );
    assert!(
        seen.lock()
            .unwrap()
            .iter()
            .all(|seen| seen.method == Method::POST)
    );
}

#[tokio::test]
async fn a_jit_config_registers_the_runner_with_the_repository_labels() {
    let (url, seen) = fake_github(|request| {
        assert_eq!(request.method, Method::POST);
        (
            StatusCode::CREATED,
            json(r#"{"runner":{"id":42,"name":"runnerd-h-p-1","status":"offline","busy":false},"encoded_jit_config":"Y29uZmln"}"#),
        )
            .into_response()
    })
    .await;
    let labels = ["self-hosted".to_owned(), "sandbox".to_owned()];
    let (runner, config) = client(&url)
        .jit_config("o/r", "runnerd-h-p-1", &labels)
        .await
        .unwrap();
    assert_eq!(runner.id, 42);
    assert_eq!(config.0, "Y29uZmln");
    assert_eq!(
        seen.lock().unwrap()[0].uri,
        "/repos/o/r/actions/runners/generate-jitconfig"
    );
}

#[tokio::test]
async fn the_token_never_shows_in_debug_output_or_errors() {
    let (url, seen) = fake_github(|request| {
        if request.uri.contains("runners/9") {
            (StatusCode::INTERNAL_SERVER_ERROR, "boom").into_response()
        } else {
            StatusCode::UNAUTHORIZED.into_response()
        }
    })
    .await;
    let github = client(&url);
    let unauthorized = github.runners("o/r").await.unwrap_err();
    let server_error = github.delete_runner("o/r", 9).await.unwrap_err();
    // The token did go out, so its absence below is not for lack of use.
    let sent = seen.lock().unwrap()[0].headers["authorization"].clone();
    assert_eq!(sent, format!("Bearer {TOKEN}").as_str());
    let unreachable = client("http://127.0.0.1:1")
        .runners("o/r")
        .await
        .unwrap_err();
    for text in [
        format!("{github:?}"),
        format!("{:?}", Token(TOKEN.to_owned())),
        format!("{unauthorized:?} {unauthorized}"),
        format!("{server_error:?} {server_error}"),
        format!("{unreachable:?} {unreachable}"),
    ] {
        assert!(!text.contains(TOKEN), "{text}");
    }
}

#[tokio::test]
async fn a_run_that_left_the_queue_is_no_longer_cached() {
    let polls = Arc::new(Mutex::new(0));
    let counter = polls.clone();
    let (url, seen) = fake_github(move |request| match request.uri.as_str() {
        "/repos/o/r/actions/runs?status=queued&per_page=100" => {
            let mut polls = counter.lock().unwrap();
            *polls += 1;
            // Listed on the first and third poll, gone on the second.
            if *polls == 2 {
                json(r#"{"workflow_runs":[]}"#)
            } else {
                json(r#"{"workflow_runs":[{"id":10}]}"#)
            }
        }
        "/repos/o/r/actions/runs?status=in_progress&per_page=100" => {
            json(r#"{"workflow_runs":[]}"#)
        }
        _ => (
            [("etag", "\"jobs\"")],
            json(r#"{"jobs":[{"id":1,"run_id":10,"name":"a","status":"queued","labels":[]}]}"#),
        )
            .into_response(),
    })
    .await;
    let github = client(&url);
    for _ in 0..3 {
        github.queued_jobs("o/r").await.unwrap();
    }
    let jobs_requests: Vec<Seen> = seen
        .lock()
        .unwrap()
        .iter()
        .filter(|request| request.uri.contains("/jobs?"))
        .cloned()
        .collect();
    assert_eq!(jobs_requests.len(), 2);
    assert!(!jobs_requests[1].headers.contains_key("if-none-match"));
}

/// A repository's history as the fake GitHub's git sees it: each commit's parents, and its branches and tags.
struct History {
    parents: &'static [(&'static str, &'static [&'static str])],
    branches: &'static [(&'static str, &'static str)],
    tags: &'static [(&'static str, &'static str)],
}

impl History {
    /// What git takes a name in a compare for: a commit, else a tag, else a branch.
    fn resolve(&self, name: &str) -> Option<&'static str> {
        let known = |refs: &[(&str, &'static str)]| {
            refs.iter()
                .find(|(other, _)| *other == name)
                .map(|(_, sha)| *sha)
        };
        let commit = self.parents.iter().find(|(sha, _)| *sha == name);
        commit
            .map(|(sha, _)| *sha)
            .or_else(|| known(self.tags))
            .or_else(|| known(self.branches))
    }

    fn ancestor(&self, older: &str, newer: &str) -> bool {
        let parents = self
            .parents
            .iter()
            .find(|(sha, _)| *sha == newer)
            .map_or(&[][..], |(_, parents)| *parents);
        parents
            .iter()
            .any(|parent| *parent == older || self.ancestor(older, parent))
    }

    fn answer(&self, uri: &str) -> Response {
        let path = uri.trim_start_matches("/repos/o/r/");
        if let Some(branch) = path.strip_prefix("branches/") {
            return match self.branches.iter().find(|(name, _)| *name == branch) {
                Some((_, sha)) => json(&format!(r#"{{"commit":{{"sha":"{sha}"}}}}"#)),
                None => StatusCode::NOT_FOUND.into_response(),
            };
        }
        let Some(range) = path
            .strip_prefix("compare/")
            .and_then(|rest| rest.strip_suffix("?per_page=1"))
        else {
            return StatusCode::NOT_FOUND.into_response();
        };
        let Some((base, head)) = range.split_once("...") else {
            return StatusCode::NOT_FOUND.into_response();
        };
        let (Some(base), Some(head)) = (self.resolve(base), self.resolve(head)) else {
            return StatusCode::NOT_FOUND.into_response();
        };
        let status = if base == head {
            "identical"
        } else if self.ancestor(head, base) {
            "behind"
        } else if self.ancestor(base, head) {
            "ahead"
        } else {
            "diverged"
        };
        json(&format!(r#"{{"status":"{status}"}}"#))
    }
}

#[tokio::test]
async fn a_commit_is_on_a_branch_only_as_its_head_or_an_ancestor() {
    // main is a0, a1, a2; someone tagged their own commit x9 `main`; release/1#1 forked at a0.
    let history = History {
        parents: &[
            ("a0", &[]),
            ("a1", &["a0"]),
            ("a2", &["a1"]),
            ("x9", &["a1"]),
            ("c1", &["a0"]),
        ],
        branches: &[("main", "a2"), ("release/1%231", "c1")],
        tags: &[("main", "x9")],
    };
    let (url, seen) = fake_github(move |request| history.answer(&request.uri)).await;
    let github = client(&url);
    assert!(
        github.on_branch("o/r", "main", "a2").await.unwrap(),
        "main's head"
    );
    assert!(
        github.on_branch("o/r", "main", "a0").await.unwrap(),
        "an ancestor"
    );
    assert!(
        !github.on_branch("o/r", "main", "x9").await.unwrap(),
        "the commit of a tag named main, which main never had"
    );
    assert!(
        github.on_branch("o/r", "release/1#1", "c1").await.unwrap(),
        "an escaped name"
    );
    assert!(
        !github.on_branch("o/r", "release/1#1", "a2").await.unwrap(),
        "diverged"
    );
    assert!(
        github.on_branch("o/r", "gone", "a2").await.is_err(),
        "no such branch"
    );
    assert!(
        github.on_branch("o/r", "main", "ee").await.is_err(),
        "unknown commit"
    );
    let asked = seen.lock().unwrap().len();
    assert!(
        !github.on_branch("o/r", "main", "../x").await.unwrap(),
        "not a commit"
    );
    assert_eq!(
        seen.lock().unwrap().len(),
        asked,
        "an invalid commit is never asked"
    );
}

#[tokio::test]
async fn a_run_and_its_pull_requests_say_whether_people_or_bots_started_them() {
    let person = r#"{"login":"owner","type":"User"}"#;
    let bot = r#"{"login":"renovate[bot]","type":"Bot"}"#;
    let (url, _) = fake_github(move |request| match request.uri.as_str() {
        "/repos/o/r/actions/runs/1" => json(&format!(
            r#"{{"event":"pull_request","head_branch":"feature","head_sha":"a1",
                "head_repository":{{"full_name":"o/r"}},"actor":{person},"triggering_actor":{person},
                "pull_requests":[
                    {{"number":7,"head":{{"ref":"feature","sha":"a1"}},
                      "base":{{"repo":{{"url":"https://api.github.com/repos/O/R"}}}}}},
                    {{"number":3,"head":{{"ref":"feature","sha":"a1"}},
                      "base":{{"repo":{{"url":"https://api.github.com/repos/upstream/r"}}}}}},
                    {{"number":4,"head":{{"ref":"feature","sha":"a1"}},
                      "base":{{"repo":{{"url":"https://api.github.com/repos/o/r2"}}}}}}
                ]}}"#
        )),
        // A GitHub App re-ran a run the owner's push started.
        "/repos/o/r/actions/runs/3" => json(&format!(
            r#"{{"event":"pull_request","head_branch":"feature","head_sha":"a1",
                "head_repository":{{"full_name":"o/r"}},"actor":{person},"triggering_actor":{bot},
                "pull_requests":[]}}"#
        )),
        // The owner re-ran a run Renovate's push started.
        "/repos/o/r/actions/runs/2" => json(&format!(
            r#"{{"event":"pull_request","head_branch":"renovate/x","head_sha":"b1",
                "head_repository":{{"full_name":"o/r"}},"actor":{bot},"triggering_actor":{person},
                "pull_requests":[]}}"#
        )),
        "/repos/o/r/pulls/7" => json(&format!(
            r#"{{"number":7,"user":{person},"head":{{"ref":"feature","sha":"a1","repo":{{"full_name":"o/r"}}}},
                "base":{{"ref":"main"}}}}"#
        )),
        // A bot's pull request from a fork that was deleted since.
        "/repos/o/r/pulls/8" => json(&format!(
            r#"{{"number":8,"user":{bot},"head":{{"ref":"patch-1","sha":"c1","repo":null}},
                "base":{{"ref":"main"}}}}"#
        )),
        _ => StatusCode::NOT_FOUND.into_response(),
    })
    .await;
    let github = client(&url);
    let run = github.run_trigger("o/r", 1).await.unwrap();
    assert!(run.by_user, "the owner pushed and started run 1");
    assert_eq!(
        run.pull_requests,
        [7],
        "pull requests into other repositories of the network are left out"
    );
    let rerun = github.run_trigger("o/r", 2).await.unwrap();
    assert!(!rerun.by_user, "Renovate started run 2");
    assert_eq!(
        rerun.pull_requests,
        Vec::<u64>::new(),
        "GitHub listed no pull request"
    );
    let app_rerun = github.run_trigger("o/r", 3).await.unwrap();
    assert!(!app_rerun.by_user, "a GitHub App re-ran run 3");
    let pull = github.pull_request("o/r", 7).await.unwrap();
    assert_eq!(
        pull,
        super::PullRequest {
            by_user: true,
            head_repository: Some("o/r".to_owned()),
            head_branch: "feature".to_owned(),
            base_branch: "main".to_owned(),
        }
    );
    let fork = github.pull_request("o/r", 8).await.unwrap();
    assert!(!fork.by_user, "a bot opened pull request 8");
    assert_eq!(fork.head_repository, None);
    assert!(
        github.pull_request("o/r", 9).await.is_err(),
        "no such pull request"
    );
}

#[tokio::test]
async fn a_repository_says_its_default_branch_and_whether_it_is_public() {
    let (url, _) = fake_github(|request| match request.uri.as_str() {
        "/repos/o/r" => {
            json(r#"{"default_branch":"trunk","private":true,"visibility":"internal"}"#)
        }
        "/repos/o/site" => {
            json(r#"{"default_branch":"main","private":false,"visibility":"public"}"#)
        }
        _ => StatusCode::NOT_FOUND.into_response(),
    })
    .await;
    let github = client(&url);
    let details = github.repository("o/r").await.unwrap();
    assert_eq!(
        (details.default_branch.as_str(), details.visibility),
        ("trunk", crate::api::Visibility::Internal)
    );
    assert_eq!(
        github.repository("o/site").await.unwrap().visibility,
        crate::api::Visibility::Public
    );
    assert!(
        github.repository("o/gone").await.is_err(),
        "a deleted repository is an error, not a visibility"
    );
}

/// A fake GitHub that tags every answer with the limit's reading, `remaining` lower with each one, and answers a
/// request conditional on the tag of its URI with a 304.
fn counting_fake() -> impl Fn(&Seen) -> Response + Send + Sync + 'static {
    let left = Arc::new(Mutex::new(5000_u64));
    move |request| {
        let remaining = {
            let mut left = left.lock().unwrap();
            *left -= 1;
            *left
        };
        let mut response = match (request.method.as_str(), request.uri.as_str()) {
            ("GET", "/repos/o/r/actions/runs?status=queued&per_page=100") => {
                page(request, r#"{"total_count":1,"workflow_runs":[{"id":10}]}"#, false)
            }
            ("GET", "/repos/o/r/actions/runs?status=in_progress&per_page=100") => {
                page(request, r#"{"total_count":0,"workflow_runs":[]}"#, false)
            }
            ("GET", "/repos/o/r/actions/runs/10/jobs?filter=latest&per_page=100") => {
                page(request, r#"{"jobs":[]}"#, false)
            }
            ("GET", "/repos/o/r/actions/runners?per_page=100") => {
                page(request, r#"{"runners":[]}"#, false)
            }
            ("GET", "/repos/o/r/actions/jobs/5") => page(
                request,
                r#"{"id":5,"run_id":10,"name":"smoke","status":"in_progress"}"#,
                false,
            ),
            ("GET", "/repos/o/r") => json(r#"{"default_branch":"main","visibility":"public"}"#),
            ("POST", "/repos/o/r/actions/runners/generate-jitconfig") => (
                StatusCode::CREATED,
                json(r#"{"runner":{"id":42,"name":"n","status":"offline","busy":false},"encoded_jit_config":"Yw=="}"#),
            )
                .into_response(),
            ("DELETE", "/repos/o/r/actions/runners/1") => StatusCode::NO_CONTENT.into_response(),
            ("DELETE", "/repos/o/r/actions/runners/2") => StatusCode::NOT_FOUND.into_response(),
            ("POST", "/repos/o/r/actions/runs/10/cancel") => StatusCode::FORBIDDEN.into_response(),
            _ => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
        };
        let headers = response.headers_mut();
        headers.insert("x-ratelimit-limit", HeaderValue::from_static("5000"));
        headers.insert(
            "x-ratelimit-remaining",
            HeaderValue::from_str(&remaining.to_string()).unwrap(),
        );
        headers.insert("x-ratelimit-reset", HeaderValue::from_static("4000000000"));
        response
    }
}

/// How many requests of each class got a fresh answer, a 304 and a refusal, in `Class::ALL`'s order.
fn tallies(github: &Github) -> Vec<[u64; 3]> {
    let report = github.requests();
    Class::ALL
        .iter()
        .map(|class| {
            let tally = report.tally(*class);
            [tally.fresh, tally.unchanged, tally.refused]
        })
        .collect()
}

/// The bugs it catches: a class counted under another (a jobs page as a runs listing, a job read as `other`), a 304
/// counted as a fresh answer, a refusal not counted, and a request retried after a 401 counted once.
#[tokio::test]
async fn requests_are_counted_by_class_and_by_what_github_answered() {
    let (url, _) = fake_github(counting_fake()).await;
    let github = client(&url);
    // A poll of the repository, twice: the second poll's three answers are 304s.
    github.queued_jobs("o/r").await.unwrap();
    github.queued_jobs("o/r").await.unwrap();
    github.runners("o/r").await.unwrap();
    github.job("o/r", 5).await.unwrap();
    github.job("o/r", 5).await.unwrap();
    let labels = ["self-hosted".to_owned()];
    github.jit_config("o/r", "n", &labels).await.unwrap();
    github.delete_runner("o/r", 1).await.unwrap();
    assert_eq!(
        github.delete_runner("o/r", 2).await.unwrap(),
        Deleted::NotFound
    );
    github.repository("o/r").await.unwrap();
    assert!(github.cancel_run("o/r", 10).await.is_err());

    assert_eq!(
        tallies(&github),
        [
            [2, 2, 0], // runs: queued and in progress, twice
            [1, 1, 0], // jobs of run 10
            [1, 0, 0], // runners
            [1, 1, 0], // job reads
            [1, 0, 0], // registrations
            [2, 0, 0], // deletes
            [1, 0, 1], // other: the repository, the refused cancel
        ]
    );
    // What the pace is read by counts the registration and both deletes, whatever GitHub answered.
    assert_eq!(github.spend().mutations, 3);
}

#[tokio::test]
async fn a_request_the_token_was_refused_for_counts_each_time_it_was_sent() {
    let sent = Arc::new(Mutex::new(0));
    let counter = sent.clone();
    let (url, _) = fake_github(move |_| {
        let mut sent = counter.lock().unwrap();
        *sent += 1;
        if *sent == 1 {
            StatusCode::UNAUTHORIZED.into_response()
        } else {
            json(r#"{"runners":[]}"#)
        }
    })
    .await;
    let github = client(&url);
    github.runners("o/r").await.unwrap();
    assert_eq!(github.requests().tally(Class::Runners).total(), 2);
}

/// The bugs it catches: the limit read only from answers that carry a body, so a run of 304s leaves a stale count for
/// a governor to act on; and a reading of the search or GraphQL resource taken for the core one.
#[tokio::test]
async fn the_rate_limit_is_read_from_every_answer_of_the_core_resource() {
    let calls = Arc::new(Mutex::new(0));
    let counter = calls.clone();
    let (url, _) = fake_github(move |request| {
        let call = {
            let mut calls = counter.lock().unwrap();
            *calls += 1;
            *calls
        };
        let mut response = if call == 3 {
            json(r#"{"runners":[]}"#)
        } else {
            page(request, r#"{"runners":[]}"#, false)
        };
        let headers = response.headers_mut();
        headers.insert("x-ratelimit-limit", HeaderValue::from_static("5000"));
        headers.insert(
            "x-ratelimit-remaining",
            HeaderValue::from_str(&(5000 - call * 10).to_string()).unwrap(),
        );
        headers.insert("x-ratelimit-reset", HeaderValue::from_static("4000000000"));
        if call == 3 {
            headers.insert("x-ratelimit-resource", HeaderValue::from_static("search"));
        } else {
            headers.insert("x-ratelimit-resource", HeaderValue::from_static("core"));
        }
        response
    })
    .await;
    let github = client(&url);
    assert_eq!(github.requests().rate_limit(), None);
    github.runners("o/r").await.unwrap();
    github.runners("o/r").await.unwrap();
    // The second answer was a 304.
    let limit = github.requests().rate_limit().unwrap();
    assert_eq!(
        (limit.limit, limit.remaining, limit.reset),
        (
            5000,
            4980,
            SystemTime::UNIX_EPOCH + Duration::from_secs(4_000_000_000)
        )
    );
    github.runners("o/r").await.unwrap();
    assert_eq!(github.requests().rate_limit().unwrap().remaining, 4980);
}

fn no_headers() -> HeaderMap {
    HeaderMap::new()
}

/// The bugs it catches: a window that ends early or late, a new one that keeps the old one's counts, and the limit
/// lost with the window: the next window's line would say it was never read.
#[test]
fn a_window_reports_after_an_hour_and_the_next_starts_empty_with_the_limit_kept() {
    use super::requests::{Counter, WINDOW};
    let began = std::time::Instant::now();
    let mut counter = Counter::new(began, WINDOW);
    let mut headers = no_headers();
    headers.insert("x-ratelimit-limit", HeaderValue::from_static("5000"));
    headers.insert("x-ratelimit-remaining", HeaderValue::from_static("4200"));
    headers.insert("x-ratelimit-reset", HeaderValue::from_static("4000000000"));
    counter.record(Class::Runs, StatusCode::OK, &headers);
    counter.record(Class::Runs, StatusCode::NOT_MODIFIED, &no_headers());
    counter.record(Class::Delete, StatusCode::TOO_MANY_REQUESTS, &no_headers());
    counter.record(Class::Registration, StatusCode::OK, &no_headers());
    counter.record(Class::Other, StatusCode::OK, &no_headers());
    assert!(counter.due(began + Duration::from_secs(3599)).is_none());

    let report = counter.due(began + WINDOW).unwrap();
    assert_eq!(report.window(), WINDOW);
    assert_eq!(report.tally(Class::Runs).total(), 2);
    assert_eq!(report.tally(Class::Delete).refused, 1);
    assert_eq!(report.rate_limit().unwrap().remaining, 4200);
    // The registrations and deletes the pace reads go on counting across windows: a window's end is not a drop.
    assert_eq!(counter.spend().mutations, 2);

    counter.record(Class::Jobs, StatusCode::OK, &no_headers());
    let next = counter.peek(began + WINDOW + Duration::from_secs(5));
    assert_eq!(next.tally(Class::Runs).total(), 0);
    assert_eq!(next.tally(Class::Jobs).total(), 1);
    assert_eq!(next.window(), Duration::from_secs(5));
    assert_eq!(next.rate_limit().unwrap().remaining, 4200);
    assert_eq!(counter.spend().mutations, 2);
    assert!(
        counter
            .due(began + WINDOW + Duration::from_secs(3599))
            .is_none()
    );
}

/// The line the owner reads, in full: every class, a refusal only where there was one, and when the count starts
/// over. The bug it catches: a class missing from the line, or a figure under another's name.
#[test]
fn the_line_names_every_class_and_the_limit() {
    use super::requests::{Counter, WINDOW};
    let began = std::time::Instant::now();
    let mut counter = Counter::new(began, WINDOW);
    let mut headers = no_headers();
    headers.insert("x-ratelimit-limit", HeaderValue::from_static("5000"));
    headers.insert("x-ratelimit-remaining", HeaderValue::from_static("4200"));
    headers.insert("x-ratelimit-reset", HeaderValue::from_static("1800"));
    for (class, status) in [
        (Class::Runs, StatusCode::OK),
        (Class::Runs, StatusCode::NOT_MODIFIED),
        (Class::Runs, StatusCode::NOT_MODIFIED),
        (Class::Jobs, StatusCode::NOT_MODIFIED),
        (Class::Runners, StatusCode::OK),
        (Class::JobRead, StatusCode::OK),
        (Class::Registration, StatusCode::CREATED),
        (Class::Delete, StatusCode::NO_CONTENT),
        (Class::Other, StatusCode::FORBIDDEN),
    ] {
        counter.record(class, status, &headers);
    }
    let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1800 - 90);
    assert_eq!(
        counter.take(began + Duration::from_secs(3600)).line(now),
        "GitHub requests in the last 1h 00m: runs 3 (1 fresh, 2 unchanged), jobs 1 (0 fresh, 1 unchanged), \
         runners 1 (1 fresh, 0 unchanged), job reads 1 (1 fresh, 0 unchanged), registrations 1 (1 fresh, 0 unchanged), \
         deletes 1 (1 fresh, 0 unchanged), other 1 (0 fresh, 0 unchanged, 1 refused); \
         rate limit 4200 of 5000 left, resets in 1m 30s"
    );
}

/// The messages logged on this thread while installed with `tracing::subscriber::set_default`.
#[derive(Clone, Default)]
struct LogLines(Arc<Mutex<Vec<String>>>);

impl tracing::Subscriber for LogLines {
    fn enabled(&self, _: &tracing::Metadata<'_>) -> bool {
        true
    }

    fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id {
        tracing::span::Id::from_u64(1)
    }

    fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {}

    fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {}

    fn event(&self, event: &tracing::Event<'_>) {
        struct Message<'lines>(&'lines Mutex<Vec<String>>);
        impl tracing::field::Visit for Message<'_> {
            fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn fmt::Debug) {
                if field.name() == "message" {
                    self.0.lock().unwrap().push(format!("{value:?}"));
                }
            }
        }
        event.record(&mut Message(&self.0));
    }

    fn enter(&self, _: &tracing::span::Id) {}

    fn exit(&self, _: &tracing::span::Id) {}
}

/// A client that asked GitHub something says so as it goes, and one that asked nothing stays quiet. The bug it
/// catches: the figures of the last window lost with the daemon, which is the window the e2e reads.
#[tokio::test]
async fn a_client_logs_what_it_asked_as_it_is_dropped() {
    // A second live dispatcher: with only one, `tracing-core` computes a callsite's interest from the thread that
    // reaches it first, and a parallel test with no subscriber caches "never".
    static SECOND: std::sync::LazyLock<tracing::Dispatch> =
        std::sync::LazyLock::new(|| tracing::Dispatch::new(LogLines::default()));
    std::sync::LazyLock::force(&SECOND);
    let lines = LogLines::default();
    let _installed = tracing::subscriber::set_default(lines.clone());
    let (url, _) = fake_github(counting_fake()).await;
    drop(client(&url));
    let ours = || -> Vec<String> {
        let lines = lines.0.lock().unwrap();
        lines
            .iter()
            .filter(|line| line.starts_with("GitHub requests"))
            .cloned()
            .collect()
    };
    assert!(ours().is_empty(), "nothing was asked");

    let github = client(&url);
    github.runners("o/r").await.unwrap();
    assert!(ours().is_empty(), "the hour is not over");
    drop(github);
    let logged = ours();
    assert_eq!(logged.len(), 1, "{logged:?}");
    assert!(
        logged[0].starts_with("GitHub requests in the last 0s: runs 0 (0 fresh, 0 unchanged), jobs 0 (0 fresh, 0 unchanged), runners 1 (1 fresh, 0 unchanged)"),
        "{logged:?}"
    );
    assert!(!logged[0].contains(TOKEN));

    // A window that is over as the request is counted is logged with it, and the stop adds nothing for it.
    let github = client(&url);
    *lock(&github.requests) =
        super::requests::Counter::new(std::time::Instant::now(), Duration::ZERO);
    github.runners("o/r").await.unwrap();
    assert_eq!(ours().len(), 2, "{:?}", ours());
    drop(github);
    assert_eq!(
        ours().len(),
        2,
        "the stop repeated the hour's line: {:?}",
        ours()
    );
}

/// GitHub answers a token it refuses with the unauthenticated reading of the limit, 60 an hour. The bug it catches:
/// that reading replacing the account's, so a revoked token leaves a limit of 60 for a governor to pace by.
#[tokio::test]
async fn a_refused_token_does_not_change_the_rate_limit_reading() {
    let calls = Arc::new(Mutex::new(0));
    let counter = calls.clone();
    let (url, _) = fake_github(move |_| {
        let call = {
            let mut calls = counter.lock().unwrap();
            *calls += 1;
            *calls
        };
        let (status, limit, remaining) = if call == 1 {
            (StatusCode::OK, "5000", "4990")
        } else {
            (StatusCode::UNAUTHORIZED, "60", "58")
        };
        (
            status,
            [
                ("x-ratelimit-limit", limit),
                ("x-ratelimit-remaining", remaining),
                ("x-ratelimit-reset", "4000000000"),
            ],
            r#"{"runners":[]}"#,
        )
            .into_response()
    })
    .await;
    let github = client(&url);
    github.runners("o/r").await.unwrap();
    assert!(github.runners("o/r").await.is_err());
    let report = github.requests();
    assert_eq!(
        report.tally(Class::Runners).total(),
        3,
        "both sends of the refused request count"
    );
    assert_eq!(report.rate_limit().unwrap().limit, 5000);
}