thirdshift 0.13.4

Turn a GitHub issue into a ready-for-review pull request, unattended
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
//! Security runs: gate before work, audit origin's Base branch, then record
//! and reproduce the findings privately for the Day shift.

use std::fmt;

use anyhow::Result;

use crate::asks::Flags;
use crate::base_fix::Advice;
use crate::config::UserConfig;
use crate::failed_run::FailedRun;
use crate::github::ListedIssue;
use crate::harness::Choice;
use crate::issue::Repo;
use crate::launch::{self, AlreadyRunning, Launch, Start};
use crate::logs::{self, Pass, Work};
use crate::pass::{LaunchAndGitHub, Outside};

pub mod audit;
pub(crate) mod fixing;
pub mod reproduction;
pub(crate) mod review;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FixAsk {
    Allow,
    Forbid,
}

pub enum Outcome {
    Skipped(Skipped),
    Audited(Ended),
    Fixed {
        ended: crate::run::Ended,
        findings: Vec<RecordedFinding>,
    },
}

/// The Security run's outcome and the private records it reached, including before a failure.
pub struct Ended {
    pub outcome: Result<Recorded, FailedRun>,
    /// Whether an audit session was requested after preparation succeeded.
    pub audit_started: bool,
    /// A failed reproduction's number; its audit and private recording completed.
    pub failed_reproduction: Option<usize>,
    pub findings: Vec<RecordedFinding>,
    pub findings_waiting: bool,
    pub advice: Vec<Advice>,
}

impl From<anyhow::Error> for Ended {
    fn from(error: anyhow::Error) -> Self {
        Self {
            outcome: Err(error.into()),
            audit_started: false,
            failed_reproduction: None,
            findings: Vec::new(),
            findings_waiting: false,
            advice: Vec::new(),
        }
    }
}

pub use crate::github::RecordedFinding;

pub enum Skipped {
    AlreadyRunning(AlreadyRunning),
    ReadyIssue(ListedIssue),
    FindingWaiting,
    FailedFix(crate::issue::IssueUrl),
    UnchangedBase(String),
}

impl fmt::Display for Skipped {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::AlreadyRunning(running) => running.fmt(f),
            Self::ReadyIssue(listed) => write!(
                f,
                "Ready issue #{} \"{}\" goes first: {}",
                listed.issue.number, listed.title, listed.issue.url
            ),
            Self::FailedFix(ticket) => write!(
                f,
                "failed Security fix #{} is still open for the Day shift: {}",
                ticket.number, ticket.url
            ),
            Self::FindingWaiting => f.write_str("a Security finding is waiting for the Day shift"),
            Self::UnchangedBase(base) => write!(
                f,
                "Base branch {base} hasn't changed since the last completed Security audit"
            ),
        }
    }
}

pub struct Recorded {
    pub created: usize,
    pub existing: usize,
}

impl fmt::Display for Recorded {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Security audit recorded {} new finding(s), {} already recorded",
            self.created, self.existing
        )
    }
}

pub fn run(
    base: Option<&str>,
    flags: &Flags,
    config: &UserConfig,
    harness: &mut Choice,
) -> Outcome {
    let offer_command = (flags.security_fix.is_none() && config.security_fix.is_none())
        .then(|| crate::args::retry_with_security_fix(base, flags));
    let Launch {
        directory,
        repo,
        base,
    } = match launch::start(base) {
        Ok(Start::Clear(launch)) => launch,
        Ok(Start::AlreadyRunning(running)) => {
            logs::skipped(Pass::Security, &running.0, &running);
            return Outcome::Skipped(Skipped::AlreadyRunning(running));
        }
        Err(error) => return Outcome::Audited(error.into()),
    };
    run_through(
        &mut LaunchAndGitHub {
            launch: &directory,
            base: &base,
            repo: &repo,
            harness,
            flags,
            config,
            security_review_harness: None,
        },
        &repo,
        base.name(),
        flags.security_fix_allowed(config),
        offer_command.as_deref(),
    )
}

fn run_through(
    outside: &mut impl Outside,
    repo: &Repo,
    base: &str,
    fixing: bool,
    offer_command: Option<&str>,
) -> Outcome {
    match outside.ready_issue() {
        Ok(Some(ready)) => {
            let skipped = Skipped::ReadyIssue(ready.listed);
            outside.skipped(Pass::Security, &skipped);
            return Outcome::Skipped(skipped);
        }
        Ok(None) => {}
        Err(error) => return Outcome::Audited(error.into()),
    }
    let records = match outside.security_records() {
        Ok(records) => records,
        Err(error) => return Outcome::Audited(error.into()),
    };
    match records.failed_fix() {
        Ok(Some(ticket)) => {
            let skipped = Skipped::FailedFix(ticket);
            outside.skipped(Pass::Security, &skipped);
            return Outcome::Skipped(skipped);
        }
        Ok(None) => {}
        Err(error) => return Outcome::Audited(error.into()),
    }
    if fixing {
        match records.next_fix() {
            Ok(Some((record, metadata))) => return fix(outside, repo, base, record, metadata),
            Ok(None) => {}
            Err(error) => return Outcome::Audited(error.into()),
        }
    }
    if records.waiting_for_day_shift(fixing) {
        let skipped = Skipped::FindingWaiting;
        outside.skipped(Pass::Security, &skipped);
        return Outcome::Skipped(skipped);
    }
    match outside.base_unchanged_since_security_audit() {
        Ok(true) => {
            let skipped = Skipped::UnchangedBase(base.to_string());
            outside.skipped(Pass::Security, &skipped);
            return Outcome::Skipped(skipped);
        }
        Ok(false) => {}
        Err(error) => return Outcome::Audited(error.into()),
    }
    let audited = audit_and_record(outside, repo, base, offer_command);
    if fixing && audited.outcome.is_ok() {
        match outside
            .security_records()
            .and_then(|records| records.next_fix())
        {
            Ok(Some((record, metadata))) => {
                let outcome = fix(outside, repo, base, record, metadata);
                if let Outcome::Fixed { ended, .. } = outcome {
                    return Outcome::Fixed {
                        ended,
                        findings: audited.findings,
                    };
                }
                return outcome;
            }
            Ok(None) => {}
            Err(error) => {
                return Outcome::Audited(Ended {
                    outcome: Err(error.into()),
                    ..audited
                });
            }
        }
    }
    Outcome::Audited(audited)
}

fn fix(
    outside: &mut impl Outside,
    repo: &Repo,
    base: &str,
    record: crate::github::SecurityRecord,
    metadata: RecordedFinding,
) -> Outcome {
    let mut log = None;
    let ended = (|| -> Result<crate::run::Ended> {
        outside.check_harness(true)?;
        outside.started(Work::SecurityRun(repo));
        let (published, session_log) = outside.publish_security_fix(base, &record, &metadata.url);
        log = session_log;
        let issue = published?;
        outside.link_security_fix(&record, &issue)?;
        let mut ended = outside.dispatch(crate::pass::Dispatch::SecurityFix {
            issue: &issue,
            is_spec: record.fix_size()? == reproduction::FixSize::Spec,
            base,
        });
        if let Err(error) =
            outside.record_security_fix_ending(&record, &issue, ended.outcome.is_ok())
        {
            ended.outcome = Err(match ended.outcome {
                Err(mut failed) => {
                    let cause =
                        format!("could not record the failed Security fix's ending: {error:#}");
                    failed.error = std::mem::replace(&mut failed.error, error).context(cause);
                    failed
                }
                Ok(reached) => FailedRun {
                    log: reached.log,
                    pr_url: Some(reached.pr_url),
                    ticket_lines: reached.ticket_lines,
                    ..error
                        .context("could not record the successful Security fix's ending")
                        .into()
                },
            });
        }
        Ok(ended)
    })();
    Outcome::Fixed {
        ended: ended.unwrap_or_else(|error| crate::run::Ended {
            outcome: Err(FailedRun {
                log,
                ..error.into()
            }),
            base_fix: None,
            advice: Vec::new(),
        }),
        findings: vec![metadata],
    }
}

fn audit_and_record(
    outside: &mut impl Outside,
    repo: &Repo,
    base: &str,
    offer_command: Option<&str>,
) -> Ended {
    let mut advice = Vec::new();
    let mut findings = Vec::new();
    let mut findings_waiting = false;
    let mut log = None;
    let mut reproduction = None;
    let mut audit_started = false;
    let recorded = (|| -> Result<Recorded> {
        outside.check_harness(false)?;
        outside.check_node()?;
        outside.started(Work::SecurityRun(repo));
        let attempt = outside.audit(base);
        audit_started = attempt.started;
        log = attempt.log;
        let audited = attempt.outcome?;
        let mut recorded = Recorded {
            created: 0,
            existing: 0,
        };
        if audited.findings.is_empty() {
            return Ok(recorded);
        }
        let mut known = outside.security_records()?;
        let mut records = Vec::new();
        let mut seen = std::collections::HashSet::new();
        for finding in audited.findings {
            let resolved = known.record_or_reuse(&finding, |records, draft| {
                outside.create_security_record(records, draft)
            })?;
            if resolved.created {
                recorded.created += 1;
                outside.step("recorded a Security finding privately".to_string());
            } else {
                recorded.existing += 1;
            }
            let record = resolved.record;
            findings_waiting |= record.untriaged();
            let metadata = record.metadata().clone();
            let url = metadata.url.clone();
            findings.push(metadata);
            if record.untriaged() && seen.insert(finding.fingerprint) {
                records.push((record, url));
            } else if !record.untriaged() {
                outside.step(format!(
                    "keeping the Day shift's grade for {}",
                    record.name()
                ));
            }
        }
        for (index, (record, url)) in records.iter().enumerate() {
            let number = index + 1;
            outside.step(format!(
                "starting Security reproduction {number} of {}",
                record.name()
            ));
            reproduction = Some(number);
            let (reproduced, session_log) = outside.reproduce(record, number);
            if session_log.is_some() {
                log = session_log;
            }
            let reproduced = reproduced?;
            outside.update_security_record(record, &reproduced)?;
            if reproduced.severity().is_some()
                && let Some(command) = offer_command
            {
                advice = vec![
                    Advice {
                        label: "Allow fixing",
                        value: command.to_string(),
                    },
                    Advice {
                        label: "Or set",
                        value: "fix = true under [security] in ~/.thirdshift/config.toml"
                            .to_string(),
                    },
                ];
            }
            for finding in findings.iter_mut().filter(|finding| finding.url == *url) {
                finding.severity = reproduced
                    .severity()
                    .map(|severity| severity.name().to_string());
            }
            outside.step(format!(
                "Security reproduction {number}: {}",
                reproduced.outcome
            ));
        }
        Ok(recorded)
    })();
    let failed_reproduction = if recorded.is_err() {
        reproduction
    } else {
        None
    };
    Ended {
        outcome: recorded.map_err(|error| FailedRun {
            log,
            ..error.into()
        }),
        audit_started,
        failed_reproduction,
        findings,
        findings_waiting,
        advice,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pass::{Call, InMemory, widgets};

    #[test]
    fn a_reproduced_fix_precedes_waiting_and_unchanged_but_yields_to_a_ready_issue() {
        for ready in [false, true] {
            let mut outside = InMemory::default().advisories(vec![
                serde_json::json!({"state":"draft", "severity":null}),
                serde_json::json!({
                    "state":"draft", "severity":"critical", "ghsa_id":"GHSA-first",
                    "summary":"Bound input", "html_url":"https://github.com/acme/widgets/security/advisories/GHSA-first",
                    "description":"Private record\n<!-- thirdshift:security-reproduction -->\nOutcome: reproduced critical single\n"
                }),
                serde_json::json!({
                    "state":"draft", "severity":"critical", "ghsa_id":"GHSA-second",
                    "summary":"Other bound", "html_url":"https://github.com/acme/widgets/security/advisories/GHSA-second",
                    "description":"Private record\n<!-- thirdshift:security-reproduction -->\nOutcome: reproduced critical single\n"
                }),
            ]).unchanged_base().dispatched_ending(crate::pass::ready_for_review());
            if ready {
                outside = outside.ready(7, false);
            }
            let outcome = run_through(&mut outside, &widgets(), "main", true, None);
            if ready {
                assert!(matches!(outcome, Outcome::Skipped(Skipped::ReadyIssue(_))));
                assert!(matches!(
                    &outside.calls[..],
                    [Call::ReadySearch, Call::Skipped(_)]
                ));
            } else {
                assert!(matches!(
                    outcome,
                    Outcome::Fixed {
                        ended: crate::run::Ended { outcome: Ok(_), .. },
                        ..
                    }
                ));
                assert!(
                    outside
                        .calls
                        .contains(&Call::PublishSecurityFix("GHSA-first".into()))
                );
                assert!(outside.calls.contains(&Call::DispatchSecurityFix {
                    issue: 8,
                    is_spec: false,
                    base: "main".into()
                }));
                assert!(!outside.calls.contains(&Call::AuditHistory));
                assert!(!outside.calls.contains(&Call::NodeCheck));
            }
        }
    }

    #[test]
    fn a_draft_without_severity_waits_before_prerequisites_or_audit() {
        let mut outside = InMemory::default()
            .advisories(vec![serde_json::json!({
                "state": "draft", "severity": null,
                "ghsa_id": "GHSA-test", "summary": "Unchecked input size"
            })])
            .harness_failing();
        let outcome = run_through(&mut outside, &widgets(), "main", false, None);
        assert!(matches!(outcome, Outcome::Skipped(_)));
        assert_eq!(
            outside.calls,
            vec![
                Call::ReadySearch,
                Call::AdvisoryList,
                Call::Skipped("a Security finding is waiting for the Day shift".into()),
            ]
        );
    }

    #[test]
    fn advisory_triage_ends_the_wait() {
        for (state, severity, waits) in [
            ("draft", None, true),
            ("draft", Some("low"), false),
            ("draft", Some("high"), false),
            ("published", None, false),
            ("closed", None, false),
            ("triage", None, false),
        ] {
            let mut outside = InMemory::default().advisories(vec![serde_json::json!({
                "state": state, "severity": severity
            })]);
            let outcome = run_through(&mut outside, &widgets(), "main", false, None);
            assert_eq!(
                matches!(outcome, Outcome::Skipped(_)),
                waits,
                "{state}: {severity:?}"
            );
            assert_eq!(
                outside.calls.contains(&Call::SecurityAudit("main".into())),
                !waits
            );
        }
    }

    #[test]
    fn only_open_private_findings_still_needing_triage_wait() {
        for (state, labels, waits) in [
            ("open", vec!["security-finding", "needs-triage"], true),
            ("OPEN", vec!["security-finding", "Needs-Triage"], true),
            ("closed", vec!["security-finding", "needs-triage"], false),
            ("open", vec!["security-finding"], false),
            ("open", vec!["security-finding", "wontfix"], false),
        ] {
            let mut outside = InMemory::default().finding_issues(vec![serde_json::json!({
                "state": state,
                "labels": labels.iter().map(|name| serde_json::json!({"name":name})).collect::<Vec<_>>()
            })]);
            let outcome = run_through(&mut outside, &widgets(), "main", false, None);
            assert_eq!(
                matches!(outcome, Outcome::Skipped(_)),
                waits,
                "{state}: {labels:?}"
            );
            assert_eq!(
                outside.calls.contains(&Call::SecurityAudit("main".into())),
                !waits
            );
        }
    }

    #[test]
    fn checks_prerequisites_then_audits_without_pulling() {
        let mut outside = InMemory::default();
        let outcome = run_through(&mut outside, &widgets(), "main", false, None);
        assert!(matches!(
            outcome,
            Outcome::Audited(Ended { outcome: Ok(_), .. })
        ));
        assert_eq!(
            outside.calls,
            vec![
                Call::ReadySearch,
                Call::AdvisoryList,
                Call::AuditHistory,
                Call::HarnessCheck,
                Call::NodeCheck,
                Call::Started(None),
                Call::SecurityAudit("main".to_string())
            ]
        );
    }
    #[test]
    fn a_ready_issue_precedes_every_prerequisite_and_audit() {
        let mut outside = InMemory::default()
            .ready(7, false)
            .advisories(vec![serde_json::json!({"state":"draft", "severity":null})])
            .unchanged_base()
            .node_failing()
            .harness_failing();
        assert!(matches!(
            run_through(&mut outside, &widgets(), "main", false, None),
            Outcome::Skipped(Skipped::ReadyIssue(_))
        ));
        assert!(matches!(
            &outside.calls[..],
            [Call::ReadySearch, Call::Skipped(_)]
        ));
    }

    #[test]
    fn security_gates_choose_ready_then_waiting_then_unchanged_before_work() {
        for ready in [false, true] {
            for waiting in [false, true] {
                for unchanged in [false, true] {
                    let mut outside = InMemory::default();
                    if ready {
                        outside = outside.ready(7, false);
                    }
                    if waiting {
                        outside = outside.advisories(vec![
                            serde_json::json!({"state":"draft", "severity":null}),
                        ]);
                    }
                    if unchanged {
                        outside = outside.unchanged_base();
                    }
                    let outcome = run_through(&mut outside, &widgets(), "main", false, None);
                    let expected = if ready {
                        "Ready issue #7"
                    } else if waiting {
                        "a Security finding is waiting for the Day shift"
                    } else if unchanged {
                        "Base branch main hasn't changed since the last completed Security audit"
                    } else {
                        assert!(matches!(
                            outcome,
                            Outcome::Audited(Ended { outcome: Ok(_), .. })
                        ));
                        assert!(outside.calls.contains(&Call::SecurityAudit("main".into())));
                        continue;
                    };
                    let Outcome::Skipped(skipped) = outcome else {
                        panic!("Security run should skip");
                    };
                    assert!(skipped.to_string().starts_with(expected));
                    assert!(!outside.calls.contains(&Call::HarnessCheck));
                    assert_eq!(outside.calls.contains(&Call::AdvisoryList), !ready);
                    assert_eq!(
                        outside.calls.contains(&Call::AuditHistory),
                        !ready && !waiting
                    );
                }
            }
        }
    }

    #[test]
    fn a_failed_fix_yields_to_ready_work_but_precedes_waiting_and_audit_history() {
        for private in [false, true] {
            for ready in [false, true] {
                for closed in [false, true] {
                    for fixing in [false, true] {
                        let body = "Private record\n<!-- thirdshift:security-fix -->\nFix Ticket: https://github.com/acme/widgets/issues/8\nFix Run: failed\n";
                        let values = vec![
                            serde_json::json!({
                                "state": if private { "open" } else { "draft" },
                                "severity": null, "description": body, "body": body,
                                "security_fix_closed": closed,
                            }),
                            serde_json::json!({
                                "state": if private { "open" } else { "draft" },
                                "severity": null, "labels": [{"name":"needs-triage"}],
                            }),
                        ];
                        let mut outside = if private {
                            InMemory::default().finding_issues(values)
                        } else {
                            InMemory::default().advisories(values)
                        }
                        .unchanged_base();
                        if ready {
                            outside = outside.ready(7, false);
                        }
                        let Outcome::Skipped(skipped) =
                            run_through(&mut outside, &widgets(), "main", fixing, None)
                        else {
                            panic!("Security run should skip");
                        };
                        let expected = if ready {
                            "Ready issue #7"
                        } else if !closed {
                            "failed Security fix #8 is still open"
                        } else {
                            "a Security finding is waiting for the Day shift"
                        };
                        assert!(skipped.to_string().starts_with(expected), "{skipped}");
                        assert!(!outside.calls.contains(&Call::HarnessCheck));
                        assert!(!outside.calls.contains(&Call::AuditHistory));
                        assert_eq!(outside.calls.contains(&Call::AdvisoryList), !ready);
                    }
                }
            }
        }
    }

    #[test]
    fn each_failed_prerequisite_prevents_later_operations() {
        for (mut outside, expected) in [
            (
                InMemory::default().harness_failing(),
                vec![
                    Call::ReadySearch,
                    Call::AdvisoryList,
                    Call::AuditHistory,
                    Call::HarnessCheck,
                ],
            ),
            (
                InMemory::default().node_failing(),
                vec![
                    Call::ReadySearch,
                    Call::AdvisoryList,
                    Call::AuditHistory,
                    Call::HarnessCheck,
                    Call::NodeCheck,
                ],
            ),
        ] {
            assert!(matches!(
                run_through(&mut outside, &widgets(), "main", false, None),
                Outcome::Audited(Ended {
                    outcome: Err(_),
                    ..
                })
            ));
            assert_eq!(outside.calls, expected);
        }
    }

    #[test]
    fn a_failed_audit_records_nothing_and_keeps_its_session_log() {
        let mut outside = InMemory::default()
            .audit_failing("invalid report")
            .session_log("audit.jsonl");
        let Outcome::Audited(Ended {
            outcome: Err(failed),
            ..
        }) = run_through(&mut outside, &widgets(), "main", false, None)
        else {
            panic!("audit should fail");
        };
        assert_eq!(failed.log, Some(std::path::PathBuf::from("audit.jsonl")));
        assert_eq!(
            outside
                .calls
                .iter()
                .filter(|call| **call == Call::AdvisoryList)
                .count(),
            1
        );
        assert!(
            !outside
                .calls
                .iter()
                .any(|call| matches!(call, Call::CreateAdvisory(_)))
        );
    }

    #[test]
    fn repeated_fingerprints_preserve_counts_metadata_order_and_reproduce_once() {
        use crate::github::{DraftAdvisory, Package};
        use serde_json::json;
        let draft = |fingerprint: &str| DraftAdvisory {
            fingerprint: fingerprint.into(),
            summary: "Candidate".into(),
            description: format!("Fingerprint: `{fingerprint}`"),
            package: Package {
                ecosystem: "other".into(),
                name: None,
            },
        };
        let mut outside = InMemory::default()
            .audited(vec![draft("old"), draft("new"), draft("new")])
            .advisories(vec![
                json!({"ghsa_id":"old", "state":"closed", "description":"Fingerprint: `old`", "summary":"Candidate", "html_url":"https://github.com/acme/widgets/security/advisories/GHSA-old"}),
            ]);
        let Outcome::Audited(Ended {
            outcome: Ok(recorded),
            findings,
            ..
        }) = run_through(&mut outside, &widgets(), "main", false, None)
        else {
            panic!("audit should succeed");
        };
        assert_eq!((recorded.created, recorded.existing), (1, 2));
        assert_eq!(
            findings
                .iter()
                .map(|finding| finding.url.as_str())
                .collect::<Vec<_>>(),
            vec![
                "https://github.com/acme/widgets/security/advisories/GHSA-old",
                "https://github.com/acme/widgets/security/advisories/new",
                "https://github.com/acme/widgets/security/advisories/new",
            ]
        );
        assert_eq!(
            outside
                .calls
                .iter()
                .filter_map(|call| match call {
                    Call::CreateAdvisory(fingerprint) => Some(fingerprint.as_str()),
                    _ => None,
                })
                .collect::<Vec<_>>(),
            vec!["new"]
        );
        assert_eq!(
            outside
                .calls
                .iter()
                .filter_map(|call| match call {
                    Call::Reproduce(record) => Some(record.as_str()),
                    _ => None,
                })
                .collect::<Vec<_>>(),
            vec!["new"]
        );
    }
}