sloc-web 1.6.18

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

use std::path::Path;
use std::time::Duration;

use axum::{
    body::Bytes,
    extract::{Query, State},
    http::{HeaderMap, StatusCode},
    response::{IntoResponse, Json, Response},
};
use serde::{Deserialize, Serialize};

use tracing::info;

use sloc_git::{
    ScanSchedule, ScanScheduleKind, ScanScheduleProvider, ScheduleStore, WebhookEvent,
    WebhookProvider, clone_or_fetch, create_worktree, destroy_worktree, get_sha,
    parse_bitbucket_push, parse_github_push, parse_gitlab_push,
    webhook::{verify_bitbucket_sig, verify_github_sig},
};

use sloc_core::AnalysisRun;

use super::{
    AppState, RunArtifacts, git_clone_dest, register_artifacts_in_registry, scan_path_to_artifacts,
};

// ── request types ─────────────────────────────────────────────────────────────

#[derive(Debug, Deserialize, Serialize)]
pub struct CreateScheduleRequest {
    pub label: String,
    pub repo_url: String,
    pub branch: String,
    pub kind: String,
    pub provider: Option<String>,
    pub interval_secs: Option<u64>,
    /// Webhook HMAC secret. If omitted, a random secret is auto-generated.
    pub webhook_secret: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct ScheduleIdQuery {
    pub id: uuid::Uuid,
}

/// Provider-agnostic build-completion payload posted to `/webhooks/ci`.
///
/// Deliberately tiny and forgiving so that even very old CI systems (a Jenkins
/// freestyle job, a shell step in a legacy pipeline) can emit it with nothing
/// more than `curl`. Only `repo_url` + `branch` are required to match a
/// configured scan schedule; everything else is optional but strengthens the
/// guards (`status` gates on success, `upstream.build_id` enables idempotency).
#[derive(Debug, Deserialize)]
pub struct CiBuildCompletePayload {
    pub repo_url: String,
    #[serde(default)]
    pub branch: Option<String>,
    /// Optional exact commit to scan. If omitted, the branch tip is used.
    #[serde(default)]
    pub commit_sha: Option<String>,
    /// Upstream build result. Anything not recognised as success is skipped.
    #[serde(default)]
    pub status: Option<String>,
    #[serde(default)]
    pub upstream: Option<CiUpstream>,
}

/// Provenance of the upstream build. Used for logging and the idempotency key.
#[derive(Debug, Default, Deserialize)]
pub struct CiUpstream {
    #[serde(default)]
    pub system: Option<String>,
    #[serde(default)]
    pub job: Option<String>,
    #[serde(default)]
    pub build_id: Option<String>,
    #[serde(default)]
    pub url: Option<String>,
}

// ── schedule CRUD ─────────────────────────────────────────────────────────────

pub async fn api_list_schedules(State(state): State<AppState>) -> impl IntoResponse {
    let store = state.schedules.lock().await;
    Json(serde_json::json!({ "schedules": store.schedules }))
}

pub async fn api_create_schedule(
    State(state): State<AppState>,
    Json(body): Json<CreateScheduleRequest>,
) -> impl IntoResponse {
    let schedule = build_schedule(body);
    let is_poll = schedule.kind == ScanScheduleKind::Poll;
    {
        let mut store = state.schedules.lock().await;
        store.schedules.push(schedule.clone());
        let _ = store.save(&state.schedules_path);
    }
    if is_poll {
        let interval = schedule.interval_secs.unwrap_or(300);
        let st = state;
        let sc = schedule.clone();
        tokio::spawn(async move { poll_loop(st, sc, interval).await });
    }
    (StatusCode::CREATED, Json(schedule)).into_response()
}

pub async fn api_delete_schedule(
    State(state): State<AppState>,
    Query(q): Query<ScheduleIdQuery>,
) -> impl IntoResponse {
    let mut store = state.schedules.lock().await;
    store.remove(q.id);
    let _ = store.save(&state.schedules_path);
    StatusCode::NO_CONTENT
}

// ── webhook receivers ─────────────────────────────────────────────────────────

/// Validate the X-GitHub-Event header; return a short-circuit `Response` for
/// missing or non-push events, or `None` to continue processing a push.
fn check_github_event_header(event: &str) -> Option<Response> {
    if event == "push" {
        return None;
    }
    let resp = if event.is_empty() {
        info!(
            "github webhook received without X-GitHub-Event header — likely misconfigured sender"
        );
        (StatusCode::BAD_REQUEST, "missing X-GitHub-Event header").into_response()
    } else {
        info!(event = %event, "github webhook: ignoring non-push event");
        StatusCode::OK.into_response()
    };
    Some(resp)
}

/// Validate the X-GitLab-Event header; return a short-circuit `Response` for
/// missing or non-push events, or `None` to continue processing a push.
fn check_gitlab_event_header(event: &str) -> Option<Response> {
    if event == "Push Hook" || event == "Tag Push Hook" {
        return None;
    }
    let resp = if event.is_empty() {
        info!(
            "gitlab webhook received without X-GitLab-Event header — likely misconfigured sender"
        );
        (StatusCode::BAD_REQUEST, "missing X-GitLab-Event header").into_response()
    } else {
        info!(event = %event, "gitlab webhook: ignoring non-push event");
        StatusCode::OK.into_response()
    };
    Some(resp)
}

pub async fn handle_github_webhook(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: Bytes,
) -> Response {
    if let Some(resp) = check_github_event_header(&header_str(&headers, "x-github-event")) {
        return resp;
    }
    let event = match parse_github_push(&body) {
        Ok(e) => e,
        Err(e) => {
            info!(error = %e, "github webhook payload parse failed");
            return (
                StatusCode::BAD_REQUEST,
                format!("invalid github push payload: {e}"),
            )
                .into_response();
        }
    };
    let sig = header_str(&headers, "x-hub-signature-256");
    dispatch_hmac_webhook(state, event, &body, &sig, is_valid_github_sig).await;
    StatusCode::ACCEPTED.into_response()
}

pub async fn handle_gitlab_webhook(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: Bytes,
) -> Response {
    if let Some(resp) = check_gitlab_event_header(&header_str(&headers, "x-gitlab-event")) {
        return resp;
    }
    let event = match parse_gitlab_push(&body) {
        Ok(e) => e,
        Err(e) => {
            info!(error = %e, "gitlab webhook payload parse failed");
            return (
                StatusCode::BAD_REQUEST,
                format!("invalid gitlab push payload: {e}"),
            )
                .into_response();
        }
    };
    let token = header_str(&headers, "x-gitlab-token");
    dispatch_token_webhook(state, event, &token).await;
    StatusCode::ACCEPTED.into_response()
}

pub async fn handle_bitbucket_webhook(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: Bytes,
) -> Response {
    let event = match parse_bitbucket_push(&body) {
        Ok(e) => e,
        Err(e) => {
            info!(error = %e, "bitbucket webhook payload parse failed");
            return (
                StatusCode::BAD_REQUEST,
                format!("invalid bitbucket push payload: {e}"),
            )
                .into_response();
        }
    };
    let sig = header_str(&headers, "x-hub-signature");
    dispatch_hmac_webhook(state, event, &body, &sig, is_valid_bitbucket_sig).await;
    StatusCode::ACCEPTED.into_response()
}

/// Generic build-completion receiver: an upstream CI build finishing triggers a
/// downstream oxide-sloc scan. Unlike the push receivers this is not tied to any
/// provider's payload shape, so any pipeline — however old — can drive it with a
/// single signed `curl`. Three independent guards must all pass before a scan
/// runs: the upstream status must be a success, the body HMAC must validate
/// against a matching schedule's secret, and the upstream build id must not have
/// been processed already (idempotency). Auth outcomes are never leaked back to
/// the caller — a bad signature is indistinguishable from success — but the two
/// non-secret skip reasons (unsuccessful build, duplicate delivery) are reported
/// so operators can debug their wiring.
pub async fn handle_ci_webhook(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: Bytes,
) -> Response {
    let payload: CiBuildCompletePayload = match serde_json::from_slice(&body) {
        Ok(p) => p,
        Err(e) => {
            info!(error = %e, "ci webhook payload parse failed");
            return (StatusCode::BAD_REQUEST, format!("invalid ci payload: {e}")).into_response();
        }
    };

    let repo_url = payload.repo_url.trim().to_owned();
    let branch = payload.branch.as_deref().unwrap_or("").trim().to_owned();
    if repo_url.is_empty() || branch.is_empty() {
        return (
            StatusCode::BAD_REQUEST,
            "ci payload requires non-empty repo_url and branch",
        )
            .into_response();
    }

    // Guard 1 — success gate. A missing status is treated as success so the
    // simplest possible caller ("my build reached its final step") still works;
    // anything explicitly non-success is a normal, non-secret skip.
    let status = payload.status.as_deref().unwrap_or("success");
    if !is_success_status(status) {
        info!(
            repo = %repo_url, branch = %branch, status,
            "ci webhook: upstream build not successful — no scan triggered"
        );
        return skip_response("upstream build not successful");
    }

    let sig = header_str(&headers, "x-sloc-signature");
    let build_key = ci_build_key(payload.upstream.as_ref());
    let upstream_url = payload
        .upstream
        .as_ref()
        .and_then(|u| u.url.clone())
        .unwrap_or_default();

    // Guard 2 — HMAC. Only schedules whose secret validates the exact body bytes
    // are eligible. Guard 3 — idempotency: skip any schedule that already
    // recorded this upstream build id, and optimistically claim the id now so a
    // flaky upstream retrying its notification cannot double-trigger.
    let mut store = state.schedules.lock().await;
    let matching_ids: Vec<uuid::Uuid> = store
        .find_matching(&repo_url, &branch)
        .into_iter()
        .filter(|s| matches_hmac(s, &body, &sig, &is_valid_github_sig))
        .map(|s| s.id)
        .collect();
    let valid_sig_count = matching_ids.len();

    let fresh = claim_fresh_schedules(&mut store, &matching_ids, &build_key);
    let duplicate = valid_sig_count > 0 && fresh.is_empty();
    if !fresh.is_empty() {
        let _ = store.save(&state.schedules_path);
    }
    drop(store);

    info!(
        repo = %repo_url,
        branch = %branch,
        upstream_url = %upstream_url,
        valid_signatures = valid_sig_count,
        triggered = fresh.len(),
        "inbound ci build-complete webhook received"
    );

    if duplicate {
        return skip_response("duplicate upstream build");
    }

    // commit_sha is optional — fall back to the branch tip. `scan_commit` fetches
    // then worktrees this ref, so a branch name resolves fine and keeps pipelines
    // that cannot emit a SHA working.
    let commit_ref = payload
        .commit_sha
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .unwrap_or(&branch)
        .to_owned();

    let event = WebhookEvent {
        provider: WebhookProvider::Ci,
        repo_url,
        branch,
        commit_sha: commit_ref,
        pusher: build_key,
    };
    spawn_scans(state, event, fresh);
    StatusCode::ACCEPTED.into_response()
}

/// Guard 3 — idempotency. From the HMAC-validated `matching_ids`, return the schedules that have
/// not already processed this upstream build, optimistically claiming `build_key` on each so a
/// retried upstream notification cannot double-trigger. A schedule whose `last_ci_build` already
/// equals the key is skipped as a duplicate; with no build key every match is returned (such
/// deliveries cannot be de-duplicated). Mutates `store` in place — the caller persists it.
fn claim_fresh_schedules(
    store: &mut ScheduleStore,
    matching_ids: &[uuid::Uuid],
    build_key: &Option<String>,
) -> Vec<ScanSchedule> {
    let mut fresh: Vec<ScanSchedule> = Vec::new();
    for id in matching_ids {
        let Some(s) = store.by_id_mut(*id) else {
            continue;
        };
        if let Some(key) = build_key {
            if s.last_ci_build.as_deref() == Some(key.as_str()) {
                continue; // duplicate delivery of the same upstream build
            }
            s.last_ci_build = Some(key.clone());
        }
        fresh.push(s.clone());
    }
    fresh
}

/// A non-secret 200 skip: the request was well-formed and (where relevant)
/// correctly signed, but a guard other than authentication declined the scan.
fn skip_response(reason: &str) -> Response {
    (
        StatusCode::OK,
        Json(serde_json::json!({ "status": "skipped", "reason": reason })),
    )
        .into_response()
}

/// Recognised success tokens across CI systems (Jenkins `SUCCESS`, GitLab
/// `success`, a raw exit code `0`, a boolean `true`, etc.). Case-insensitive.
fn is_success_status(status: &str) -> bool {
    matches!(
        status.trim().to_ascii_lowercase().as_str(),
        "success" | "succeeded" | "passed" | "pass" | "ok" | "green" | "completed" | "0" | "true"
    )
}

/// Build the `<system>:<job>:<build_id>` idempotency key. Returns `None` when no
/// build id is supplied — such deliveries cannot be de-duplicated, so callers
/// that want the guard must always send `upstream.build_id`.
fn ci_build_key(upstream: Option<&CiUpstream>) -> Option<String> {
    let u = upstream?;
    let non_empty = |o: &Option<String>| {
        o.as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_owned)
    };
    let build_id = non_empty(&u.build_id)?;
    let system = non_empty(&u.system).unwrap_or_else(|| "ci".to_owned());
    let job = non_empty(&u.job).unwrap_or_else(|| "-".to_owned());
    Some(format!("{system}:{job}:{build_id}"))
}

// ── dispatch helpers ──────────────────────────────────────────────────────────

async fn dispatch_hmac_webhook<F>(
    state: AppState,
    event: WebhookEvent,
    body: &Bytes,
    sig: &str,
    verify: F,
) where
    F: Fn(&[u8], &str, &str) -> bool,
{
    let store = state.schedules.lock().await;
    let candidates = store.find_matching(&event.repo_url, &event.branch);
    let candidate_count = candidates.len();
    let matching: Vec<ScanSchedule> = candidates
        .into_iter()
        .filter(|s| matches_hmac(s, body, sig, &verify))
        .cloned()
        .collect();
    let valid_sig_count = matching.len();
    drop(store);
    info!(
        repo = %event.repo_url,
        branch = %event.branch,
        matched_schedules = candidate_count,
        valid_signatures = valid_sig_count,
        "inbound HMAC webhook received"
    );
    if candidate_count > 0 && valid_sig_count == 0 {
        info!(
            repo = %event.repo_url,
            branch = %event.branch,
            "webhook HMAC verification failed for all matching schedules — no scan triggered"
        );
    }
    spawn_scans(state, event, matching);
}

async fn dispatch_token_webhook(state: AppState, event: WebhookEvent, token: &str) {
    let store = state.schedules.lock().await;
    let candidates = store.find_matching(&event.repo_url, &event.branch);
    let candidate_count = candidates.len();
    let matching: Vec<ScanSchedule> = candidates
        .into_iter()
        .filter(|s| matches_token(s, token))
        .cloned()
        .collect();
    let valid_token_count = matching.len();
    drop(store);
    info!(
        repo = %event.repo_url,
        branch = %event.branch,
        matched_schedules = candidate_count,
        valid_tokens = valid_token_count,
        "inbound token webhook received"
    );
    if candidate_count > 0 && valid_token_count == 0 {
        info!(
            repo = %event.repo_url,
            branch = %event.branch,
            "webhook token verification failed for all matching schedules — no scan triggered"
        );
    }
    spawn_scans(state, event, matching);
}

fn matches_hmac<F: Fn(&[u8], &str, &str) -> bool>(
    s: &ScanSchedule,
    body: &[u8],
    sig: &str,
    verify: &F,
) -> bool {
    // A schedule without a secret never matches — callers must configure a secret.
    // Using is_none_or here would allow unauthenticated webhook triggering.
    match s.webhook_secret.as_deref() {
        None | Some("") => false,
        Some(secret) => verify(body, sig, secret),
    }
}

fn matches_token(s: &ScanSchedule, token: &str) -> bool {
    // A schedule without a secret never matches — token must be configured.
    match s.webhook_secret.as_deref() {
        None | Some("") => false,
        Some(secret) => ct_eq(secret, token),
    }
}

fn is_valid_github_sig(body: &[u8], sig: &str, secret: &str) -> bool {
    verify_github_sig(body, sig, secret)
}

fn is_valid_bitbucket_sig(body: &[u8], sig: &str, secret: &str) -> bool {
    verify_bitbucket_sig(body, sig, secret)
}

// Takes owned values so each iteration can move into the async task after cloning.
#[allow(clippy::needless_pass_by_value)]
fn spawn_scans(state: AppState, event: WebhookEvent, schedules: Vec<ScanSchedule>) {
    for schedule in schedules {
        let st = state.clone();
        let ev = event.clone();
        let sc = schedule.clone();
        tokio::spawn(async move { run_scheduled_scan(st, ev, sc).await });
    }
}

// ── scan execution ────────────────────────────────────────────────────────────

async fn run_scheduled_scan(state: AppState, event: WebhookEvent, schedule: ScanSchedule) {
    let repo = event.repo_url.clone();
    let sha = event.commit_sha.clone();
    let sha_for_record = sha.clone();
    let clones_dir = state.git_clones_dir.clone();
    let config = state.base_config.clone();
    let label = schedule.label.clone();
    let label_for_closure = label.clone();
    let sched_id = schedule.id;

    let result = tokio::task::spawn_blocking(move || {
        scan_commit(&repo, &sha, &clones_dir, &config, &label_for_closure)
    })
    .await;

    match result {
        Ok(Ok((run_id, artifacts, run))) => {
            register_artifacts_in_registry(&state, &label, &run, &artifacts).await;
            record_scan_result(&state, sched_id, &sha_for_record, &run_id).await;
            crate::confluence::maybe_auto_post_confluence(&state, sched_id, &run, &run_id).await;
        }
        Ok(Err(e)) => eprintln!("[sloc-webhook] scan failed '{}': {e:#}", schedule.label),
        Err(e) => eprintln!("[sloc-webhook] task panicked: {e}"),
    }
}

async fn record_scan_result(state: &AppState, id: uuid::Uuid, sha: &str, run_id: &str) {
    let mut store = state.schedules.lock().await;
    if let Some(s) = store.by_id_mut(id) {
        s.last_scan_sha = Some(sha.to_owned());
        s.last_scan_at = Some(chrono::Utc::now());
        s.last_run_id = Some(run_id.to_owned());
    }
    let _ = store.save(&state.schedules_path);
}

fn scan_commit(
    repo: &str,
    sha: &str,
    clones_dir: &Path,
    config: &sloc_config::AppConfig,
    label: &str,
) -> anyhow::Result<(String, RunArtifacts, AnalysisRun)> {
    let dest = git_clone_dest(repo, clones_dir);
    clone_or_fetch(repo, &dest)?;
    let wt_path = clones_dir.join(format!("wt-{}", uuid::Uuid::new_v4().simple()));
    create_worktree(&dest, sha, &wt_path)?;
    let result = scan_path_to_artifacts(&wt_path, config, label);
    let _ = destroy_worktree(&dest, &wt_path);
    result
}

// ── polling ───────────────────────────────────────────────────────────────────

pub async fn poll_loop(state: AppState, mut schedule: ScanSchedule, interval_secs: u64) {
    let mut ticker = tokio::time::interval(Duration::from_secs(interval_secs));
    ticker.tick().await;
    loop {
        ticker.tick().await;
        if let Err(e) = poll_once(&state, &mut schedule).await {
            eprintln!("[sloc-poll] '{}': {e:#}", schedule.label);
        }
    }
}

async fn poll_once(state: &AppState, schedule: &mut ScanSchedule) -> anyhow::Result<()> {
    let repo = schedule.repo_url.clone();
    let branch = schedule.branch.clone();
    let clones_dir = state.git_clones_dir.clone();
    let last_sha = schedule.last_scan_sha.clone().unwrap_or_default();

    let current_sha =
        tokio::task::spawn_blocking(move || fetch_and_resolve_sha(&repo, &branch, &clones_dir))
            .await??;

    if current_sha == last_sha {
        return Ok(());
    }

    let label = schedule.label.clone();
    let config = state.base_config.clone();
    let repo2 = schedule.repo_url.clone();
    let sha = current_sha.clone();
    let clones2 = state.git_clones_dir.clone();

    let (run_id, artifacts, run) =
        tokio::task::spawn_blocking(move || scan_commit(&repo2, &sha, &clones2, &config, &label))
            .await??;

    register_artifacts_in_registry(state, &schedule.label, &run, &artifacts).await;
    schedule.last_scan_sha = Some(current_sha.clone());
    schedule.last_scan_at = Some(chrono::Utc::now());
    schedule.last_run_id = Some(run_id.clone());
    record_scan_result(state, schedule.id, &current_sha, &run_id).await;
    crate::confluence::maybe_auto_post_confluence(state, schedule.id, &run, &run_id).await;
    Ok(())
}

fn fetch_and_resolve_sha(repo: &str, branch: &str, clones_dir: &Path) -> anyhow::Result<String> {
    let dest = git_clone_dest(repo, clones_dir);
    clone_or_fetch(repo, &dest)?;
    get_sha(&dest, &format!("origin/{branch}"))
}

// ── small helpers ─────────────────────────────────────────────────────────────

fn build_schedule(req: CreateScheduleRequest) -> ScanSchedule {
    if req.kind == "poll" {
        ScanSchedule::new_poll(
            req.repo_url,
            req.branch,
            req.interval_secs.unwrap_or(300),
            req.label,
        )
    } else {
        let provider = match req.provider.as_deref() {
            Some("github") => ScanScheduleProvider::GitHub,
            Some("gitlab") => ScanScheduleProvider::GitLab,
            Some("bitbucket") => ScanScheduleProvider::Bitbucket,
            _ => ScanScheduleProvider::Any,
        };
        ScanSchedule::new_webhook(
            req.repo_url,
            req.branch,
            provider,
            req.label,
            req.webhook_secret,
        )
    }
}

fn header_str(headers: &HeaderMap, name: &str) -> String {
    headers
        .get(name)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("")
        .to_owned()
}

fn ct_eq(a: &str, b: &str) -> bool {
    use subtle::ConstantTimeEq;
    a.as_bytes().ct_eq(b.as_bytes()).into()
}

#[cfg(test)]
mod tests {
    use super::*;
    use sloc_git::{ScanScheduleKind, ScanScheduleProvider};

    // ── /webhooks/ci guards ───────────────────────────────────────────────────

    #[test]
    fn success_status_accepts_common_tokens() {
        for s in [
            "success",
            "SUCCESS",
            " Success ",
            "passed",
            "pass",
            "ok",
            "green",
            "completed",
            "0",
            "true",
        ] {
            assert!(is_success_status(s), "{s:?} should be a success");
        }
    }

    #[test]
    fn success_status_rejects_failures_and_unknowns() {
        for s in [
            "failure", "failed", "1", "error", "aborted", "unstable", "canceled", "false", "",
            "running",
        ] {
            assert!(!is_success_status(s), "{s:?} should not be a success");
        }
    }

    #[test]
    fn ci_build_key_requires_build_id() {
        // No upstream at all → no key (dedup disabled).
        assert_eq!(ci_build_key(None), None);
        // Upstream present but no build id → still no key.
        let u = CiUpstream {
            system: Some("jenkins".into()),
            job: Some("api".into()),
            build_id: None,
            url: None,
        };
        assert_eq!(ci_build_key(Some(&u)), None);
    }

    #[test]
    fn ci_build_key_composes_stable_and_fills_defaults() {
        let full = CiUpstream {
            system: Some("jenkins".into()),
            job: Some("api-pipeline".into()),
            build_id: Some("1234".into()),
            url: None,
        };
        assert_eq!(
            ci_build_key(Some(&full)).as_deref(),
            Some("jenkins:api-pipeline:1234")
        );
        // Missing system/job fall back to fixed placeholders so the key is stable.
        let sparse = CiUpstream {
            system: None,
            job: None,
            build_id: Some("42".into()),
            url: None,
        };
        assert_eq!(ci_build_key(Some(&sparse)).as_deref(), Some("ci:-:42"));
    }

    #[test]
    fn ci_signature_roundtrips_against_schedule_secret() {
        // What a trigger script computes must be what a matching schedule accepts.
        let secret = "s3cr3t-shared-key";
        let body = br#"{"repo_url":"https://x/y.git","branch":"main","status":"success"}"#;
        let sig = format!(
            "sha256={}",
            sloc_git::webhook::hmac_sha256_hex(secret.as_bytes(), body)
        );

        let sched = ScanSchedule::new_webhook(
            "https://x/y.git".into(),
            "main".into(),
            ScanScheduleProvider::Any,
            "ci".into(),
            Some(secret.into()),
        );
        assert!(matches_hmac(&sched, body, &sig, &is_valid_github_sig));
        // A tampered body must not validate under the same signature.
        let tampered = br#"{"repo_url":"https://x/y.git","branch":"main","status":"SUCCESS "}"#;
        assert!(!matches_hmac(&sched, tampered, &sig, &is_valid_github_sig));
    }

    #[test]
    fn ci_payload_parses_minimal_and_full() {
        let minimal: CiBuildCompletePayload =
            serde_json::from_slice(br#"{"repo_url":"https://x/y.git","branch":"main"}"#).unwrap();
        assert_eq!(minimal.repo_url, "https://x/y.git");
        assert!(minimal.commit_sha.is_none() && minimal.upstream.is_none());

        let full: CiBuildCompletePayload = serde_json::from_slice(
            br#"{"repo_url":"https://x/y.git","branch":"main","commit_sha":"abc",
                 "status":"success","upstream":{"system":"gitlab","job":"build",
                 "build_id":"9","url":"https://ci/9"}}"#,
        )
        .unwrap();
        assert_eq!(full.commit_sha.as_deref(), Some("abc"));
        assert_eq!(full.upstream.unwrap().build_id.as_deref(), Some("9"));
    }

    // ── claim_fresh_schedules (Guard 3 idempotency) ───────────────────────────

    fn webhook_sched() -> ScanSchedule {
        ScanSchedule::new_webhook(
            "https://x/y.git".into(),
            "main".into(),
            ScanScheduleProvider::Any,
            "ci".into(),
            Some("s3cr3t".into()),
        )
    }

    #[test]
    fn claim_fresh_schedules_dedups_by_build_key() {
        let mut store = ScheduleStore::default();
        store.schedules.push(webhook_sched());
        store.schedules.push(webhook_sched());
        let ids: Vec<uuid::Uuid> = store.schedules.iter().map(|s| s.id).collect();
        let key = Some("jenkins:api:1".to_string());

        // First delivery: both schedules are fresh and get the build id claimed.
        let fresh = claim_fresh_schedules(&mut store, &ids, &key);
        assert_eq!(fresh.len(), 2);
        assert!(
            store
                .schedules
                .iter()
                .all(|s| s.last_ci_build.as_deref() == Some("jenkins:api:1"))
        );

        // Re-delivery of the same build id: both are now duplicates.
        assert!(claim_fresh_schedules(&mut store, &ids, &key).is_empty());

        // A new build id claims them again.
        let key2 = Some("jenkins:api:2".to_string());
        assert_eq!(claim_fresh_schedules(&mut store, &ids, &key2).len(), 2);
    }

    #[test]
    fn claim_fresh_schedules_without_key_returns_all_and_skips_unknown_ids() {
        let mut store = ScheduleStore::default();
        store.schedules.push(webhook_sched());
        let real = store.schedules[0].id;
        let bogus = uuid::Uuid::new_v4();
        // No build key ⇒ dedup disabled ⇒ every existing match returned; unknown ids skipped.
        let fresh = claim_fresh_schedules(&mut store, &[real, bogus], &None);
        assert_eq!(fresh.len(), 1);
        assert_eq!(fresh[0].id, real);
    }

    // ── ct_eq ─────────────────────────────────────────────────────────────────

    #[test]
    fn ct_eq_equal_strings() {
        assert!(ct_eq("hello", "hello"));
    }

    #[test]
    fn ct_eq_unequal_strings() {
        assert!(!ct_eq("hello", "world"));
    }

    #[test]
    fn ct_eq_empty_strings_equal() {
        assert!(ct_eq("", ""));
    }

    #[test]
    fn ct_eq_different_lengths_unequal() {
        assert!(!ct_eq("abc", "abcd"));
        assert!(!ct_eq("abcd", "abc"));
    }

    #[test]
    fn ct_eq_one_empty_unequal() {
        assert!(!ct_eq("", "nonempty"));
        assert!(!ct_eq("nonempty", ""));
    }

    #[test]
    fn ct_eq_case_sensitive() {
        assert!(!ct_eq("Secret", "secret"));
    }

    // ── check_github_event_header ─────────────────────────────────────────────

    #[test]
    fn github_push_event_returns_none() {
        assert!(check_github_event_header("push").is_none());
    }

    #[test]
    fn github_ping_event_returns_200() {
        let resp = check_github_event_header("ping");
        assert!(resp.is_some());
    }

    #[test]
    fn github_empty_event_returns_400() {
        let resp = check_github_event_header("");
        assert!(resp.is_some());
    }

    #[test]
    fn github_issues_event_returns_200() {
        let resp = check_github_event_header("issues");
        assert!(resp.is_some());
    }

    // ── check_gitlab_event_header ─────────────────────────────────────────────

    #[test]
    fn gitlab_push_hook_returns_none() {
        assert!(check_gitlab_event_header("Push Hook").is_none());
    }

    #[test]
    fn gitlab_tag_push_hook_returns_none() {
        assert!(check_gitlab_event_header("Tag Push Hook").is_none());
    }

    #[test]
    fn gitlab_merge_request_hook_returns_200() {
        let resp = check_gitlab_event_header("Merge Request Hook");
        assert!(resp.is_some());
    }

    #[test]
    fn gitlab_empty_event_returns_400() {
        let resp = check_gitlab_event_header("");
        assert!(resp.is_some());
    }

    // ── matches_token ─────────────────────────────────────────────────────────

    #[test]
    fn matches_token_empty_secret_never_matches() {
        let mut s = ScanSchedule::new_webhook(
            "https://gitlab.com/org/repo.git".into(),
            "main".into(),
            ScanScheduleProvider::GitLab,
            "test".into(),
            Some(String::new()),
        );
        s.webhook_secret = Some(String::new());
        assert!(!matches_token(&s, "any-token"));
    }

    #[test]
    fn matches_token_no_secret_never_matches() {
        let mut s = ScanSchedule::new_webhook(
            "https://gitlab.com/org/repo.git".into(),
            "main".into(),
            ScanScheduleProvider::GitLab,
            "test".into(),
            None,
        );
        s.webhook_secret = None;
        assert!(!matches_token(&s, "any-token"));
    }

    #[test]
    fn matches_token_correct_token_matches() {
        let secret = "my-gitlab-secret";
        let mut s = ScanSchedule::new_webhook(
            "https://gitlab.com/org/repo.git".into(),
            "main".into(),
            ScanScheduleProvider::GitLab,
            "test".into(),
            Some(secret.into()),
        );
        s.webhook_secret = Some(secret.into());
        assert!(matches_token(&s, secret));
    }

    #[test]
    fn matches_token_wrong_token_no_match() {
        let mut s = ScanSchedule::new_webhook(
            "https://gitlab.com/org/repo.git".into(),
            "main".into(),
            ScanScheduleProvider::GitLab,
            "test".into(),
            Some("correct-secret".into()),
        );
        s.webhook_secret = Some("correct-secret".into());
        assert!(!matches_token(&s, "wrong-secret"));
    }

    // ── matches_hmac ──────────────────────────────────────────────────────────

    #[test]
    fn matches_hmac_no_secret_always_false() {
        let mut s = ScanSchedule::new_webhook(
            "https://github.com/org/repo.git".into(),
            "main".into(),
            ScanScheduleProvider::GitHub,
            "test".into(),
            None,
        );
        s.webhook_secret = None;
        let body = b"payload";
        let verify = |_: &[u8], _: &str, _: &str| true;
        assert!(!matches_hmac(&s, body, "sig", &verify));
    }

    #[test]
    fn matches_hmac_empty_secret_always_false() {
        let mut s = ScanSchedule::new_webhook(
            "https://github.com/org/repo.git".into(),
            "main".into(),
            ScanScheduleProvider::GitHub,
            "test".into(),
            Some(String::new()),
        );
        s.webhook_secret = Some(String::new());
        let body = b"payload";
        let verify = |_: &[u8], _: &str, _: &str| true;
        assert!(!matches_hmac(&s, body, "sig", &verify));
    }

    #[test]
    fn matches_hmac_verify_fn_determines_result() {
        let secret = "webhook-secret";
        let mut s = ScanSchedule::new_webhook(
            "https://github.com/org/repo.git".into(),
            "main".into(),
            ScanScheduleProvider::GitHub,
            "test".into(),
            Some(secret.into()),
        );
        s.webhook_secret = Some(secret.into());
        let body = b"payload";
        assert!(matches_hmac(&s, body, "sig", &|_, _, _| true));
        assert!(!matches_hmac(&s, body, "sig", &|_, _, _| false));
    }

    // ── build_schedule ────────────────────────────────────────────────────────

    #[test]
    fn build_schedule_webhook_kind() {
        let req = CreateScheduleRequest {
            label: "My webhook".into(),
            repo_url: "https://github.com/org/repo.git".into(),
            branch: "main".into(),
            kind: "webhook".into(),
            provider: Some("github".into()),
            interval_secs: None,
            webhook_secret: Some("secret123".into()),
        };
        let s = build_schedule(req);
        assert_eq!(s.kind, ScanScheduleKind::Webhook);
        assert_eq!(s.repo_url, "https://github.com/org/repo.git");
        assert_eq!(s.branch, "main");
        assert_eq!(s.label, "My webhook");
        assert_eq!(s.webhook_secret.as_deref(), Some("secret123"));
    }

    #[test]
    fn build_schedule_poll_kind() {
        let req = CreateScheduleRequest {
            label: "My poll".into(),
            repo_url: "https://github.com/org/repo.git".into(),
            branch: "develop".into(),
            kind: "poll".into(),
            provider: None,
            interval_secs: Some(600),
            webhook_secret: None,
        };
        let s = build_schedule(req);
        assert_eq!(s.kind, ScanScheduleKind::Poll);
        assert_eq!(s.interval_secs, Some(600));
    }

    #[test]
    fn build_schedule_poll_defaults_interval_to_300() {
        let req = CreateScheduleRequest {
            label: "Poll no interval".into(),
            repo_url: "https://github.com/org/repo.git".into(),
            branch: "main".into(),
            kind: "poll".into(),
            provider: None,
            interval_secs: None,
            webhook_secret: None,
        };
        let s = build_schedule(req);
        assert_eq!(s.kind, ScanScheduleKind::Poll);
        assert_eq!(s.interval_secs, Some(300));
    }

    #[test]
    fn build_schedule_github_provider() {
        let req = CreateScheduleRequest {
            label: "GH".into(),
            repo_url: "https://github.com/org/repo.git".into(),
            branch: "main".into(),
            kind: "webhook".into(),
            provider: Some("github".into()),
            interval_secs: None,
            webhook_secret: None,
        };
        let s = build_schedule(req);
        assert_eq!(s.provider, ScanScheduleProvider::GitHub);
    }

    #[test]
    fn build_schedule_gitlab_provider() {
        let req = CreateScheduleRequest {
            label: "GL".into(),
            repo_url: "https://gitlab.com/org/repo.git".into(),
            branch: "main".into(),
            kind: "webhook".into(),
            provider: Some("gitlab".into()),
            interval_secs: None,
            webhook_secret: None,
        };
        let s = build_schedule(req);
        assert_eq!(s.provider, ScanScheduleProvider::GitLab);
    }

    #[test]
    fn build_schedule_bitbucket_provider() {
        let req = CreateScheduleRequest {
            label: "BB".into(),
            repo_url: "https://bitbucket.org/ws/repo.git".into(),
            branch: "main".into(),
            kind: "webhook".into(),
            provider: Some("bitbucket".into()),
            interval_secs: None,
            webhook_secret: None,
        };
        let s = build_schedule(req);
        assert_eq!(s.provider, ScanScheduleProvider::Bitbucket);
    }

    #[test]
    fn build_schedule_unknown_provider_defaults_to_any() {
        let req = CreateScheduleRequest {
            label: "Any".into(),
            repo_url: "https://custom.git.server/repo.git".into(),
            branch: "main".into(),
            kind: "webhook".into(),
            provider: Some("unknown".into()),
            interval_secs: None,
            webhook_secret: None,
        };
        let s = build_schedule(req);
        assert_eq!(s.provider, ScanScheduleProvider::Any);
    }

    // ── header_str ────────────────────────────────────────────────────────────

    #[test]
    fn header_str_missing_header_returns_empty() {
        let headers = HeaderMap::new();
        assert_eq!(header_str(&headers, "x-github-event"), "");
    }

    #[test]
    fn header_str_present_header_returned() {
        let mut headers = HeaderMap::new();
        headers.insert("x-github-event", "push".parse().unwrap());
        assert_eq!(header_str(&headers, "x-github-event"), "push");
    }

    #[test]
    fn header_str_case_insensitive_lookup() {
        let mut headers = HeaderMap::new();
        headers.insert("x-github-event", "ping".parse().unwrap());
        // HeaderMap normalises header names to lowercase
        assert_eq!(header_str(&headers, "X-GitHub-Event"), "ping");
    }

    // ── build_schedule — edge cases ───────────────────────────────────────────

    #[test]
    fn build_schedule_no_provider_defaults_to_any() {
        let req = CreateScheduleRequest {
            label: "No provider".into(),
            repo_url: "https://git.example.com/repo.git".into(),
            branch: "main".into(),
            kind: "webhook".into(),
            provider: None,
            interval_secs: None,
            webhook_secret: None,
        };
        let s = build_schedule(req);
        assert_eq!(s.provider, ScanScheduleProvider::Any);
        assert_eq!(s.kind, ScanScheduleKind::Webhook);
    }

    #[test]
    fn build_schedule_webhook_no_secret_sets_none_or_generated() {
        let req = CreateScheduleRequest {
            label: "No secret webhook".into(),
            repo_url: "https://github.com/org/repo.git".into(),
            branch: "main".into(),
            kind: "webhook".into(),
            provider: Some("github".into()),
            interval_secs: None,
            webhook_secret: None,
        };
        let s = build_schedule(req);
        // When no secret is provided, the schedule either has None or an
        // auto-generated secret (implementation detail). Either is valid.
        assert_eq!(s.kind, ScanScheduleKind::Webhook);
    }

    #[test]
    fn build_schedule_poll_stores_interval() {
        let req = CreateScheduleRequest {
            label: "Fast poll".into(),
            repo_url: "https://github.com/org/repo.git".into(),
            branch: "main".into(),
            kind: "poll".into(),
            provider: None,
            interval_secs: Some(60),
            webhook_secret: None,
        };
        let s = build_schedule(req);
        assert_eq!(s.kind, ScanScheduleKind::Poll);
        assert_eq!(s.interval_secs, Some(60));
    }

    // ── check_github_event_header — additional event types ────────────────────

    #[test]
    fn github_pull_request_event_returns_200() {
        let resp = check_github_event_header("pull_request");
        assert!(
            resp.is_some(),
            "pull_request must return a response (not push)"
        );
    }

    #[test]
    fn github_star_event_returns_200() {
        let resp = check_github_event_header("star");
        assert!(
            resp.is_some(),
            "star event must return a response (not push)"
        );
    }

    // ── check_gitlab_event_header — additional event types ────────────────────

    #[test]
    fn gitlab_pipeline_hook_returns_200() {
        let resp = check_gitlab_event_header("Pipeline Hook");
        assert!(resp.is_some(), "Pipeline Hook must return a response");
    }

    #[test]
    fn gitlab_issue_hook_returns_200() {
        let resp = check_gitlab_event_header("Issue Hook");
        assert!(resp.is_some(), "Issue Hook must return a response");
    }

    // ── is_valid_github_sig / is_valid_bitbucket_sig ─────────────────────────

    #[test]
    fn is_valid_github_sig_empty_sig_always_false() {
        // An empty signature string cannot match any valid HMAC
        assert!(!is_valid_github_sig(b"test-body", "", "some-secret"));
    }

    #[test]
    fn is_valid_github_sig_wrong_format_returns_false() {
        // GitHub sig must be "sha256=<hex>" — anything else is invalid
        assert!(!is_valid_github_sig(
            b"test-body",
            "not-a-valid-sig",
            "some-secret"
        ));
    }

    #[test]
    fn is_valid_bitbucket_sig_empty_sig_returns_false() {
        assert!(!is_valid_bitbucket_sig(b"test-body", "", "some-secret"));
    }

    #[test]
    fn is_valid_bitbucket_sig_wrong_sig_returns_false() {
        assert!(!is_valid_bitbucket_sig(
            b"test-body",
            "sha256=wronghex",
            "some-secret"
        ));
    }

    // ── matches_hmac — verify fn called with correct arguments ────────────────

    #[test]
    fn matches_hmac_passes_body_to_verify() {
        let secret = "my-secret";
        let s = ScanSchedule::new_webhook(
            "https://github.com/org/repo.git".into(),
            "main".into(),
            ScanScheduleProvider::GitHub,
            "test".into(),
            Some(secret.into()),
        );
        let body = b"test-payload";
        let sig = "sha256=abc123";

        // Use a stateless verify fn that checks the secret matches
        let verify = |_body: &[u8], _sig: &str, sec: &str| sec == "my-secret";
        assert!(matches_hmac(&s, body, sig, &verify));

        // A fn that always returns false
        let reject = |_body: &[u8], _sig: &str, _sec: &str| false;
        assert!(!matches_hmac(&s, body, sig, &reject));
    }

    // ── header_str — additional headers ──────────────────────────────────────

    #[test]
    fn header_str_x_hub_signature_256() {
        let mut headers = HeaderMap::new();
        headers.insert("x-hub-signature-256", "sha256=abc123".parse().unwrap());
        assert_eq!(header_str(&headers, "x-hub-signature-256"), "sha256=abc123");
    }

    #[test]
    fn header_str_x_gitlab_token() {
        let mut headers = HeaderMap::new();
        headers.insert("x-gitlab-token", "my-secret-token".parse().unwrap());
        assert_eq!(header_str(&headers, "x-gitlab-token"), "my-secret-token");
    }

    #[test]
    fn header_str_x_hub_signature() {
        let mut headers = HeaderMap::new();
        headers.insert("x-hub-signature", "sha256=bitbucket-sig".parse().unwrap());
        assert_eq!(
            header_str(&headers, "x-hub-signature"),
            "sha256=bitbucket-sig"
        );
    }

    // ── ct_eq — unicode and binary-safe boundary tests ────────────────────────

    #[test]
    fn ct_eq_spaces_in_secret() {
        assert!(ct_eq("my secret", "my secret"));
        assert!(!ct_eq("my secret", "my-secret"));
    }

    #[test]
    fn ct_eq_long_strings() {
        let a = "x".repeat(1024);
        let b = "x".repeat(1024);
        assert!(ct_eq(&a, &b));
        let mut c = "x".repeat(1023);
        c.push('y');
        assert!(!ct_eq(&a, &c));
    }

    // ── CreateScheduleRequest — serialization ─────────────────────────────────

    #[test]
    fn create_schedule_request_serializes_and_deserializes() {
        let req = CreateScheduleRequest {
            label: "Test Label".into(),
            repo_url: "https://github.com/org/repo.git".into(),
            branch: "main".into(),
            kind: "webhook".into(),
            provider: Some("github".into()),
            interval_secs: Some(300),
            webhook_secret: Some("sec".into()),
        };
        let json = serde_json::to_string(&req).expect("must serialize");
        let deser: CreateScheduleRequest = serde_json::from_str(&json).expect("must deserialize");
        assert_eq!(deser.label, "Test Label");
        assert_eq!(deser.repo_url, "https://github.com/org/repo.git");
        assert_eq!(deser.branch, "main");
        assert_eq!(deser.kind, "webhook");
        assert_eq!(deser.provider.as_deref(), Some("github"));
        assert_eq!(deser.interval_secs, Some(300));
        assert_eq!(deser.webhook_secret.as_deref(), Some("sec"));
    }
}