tak-cli 0.0.16

Pre-v1 command-line benchmarking and performance tracking
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
//! `tak detect` against a real repository with notes on some of its commits.
//!
//! The step rules are unit-tested in `src/detect.rs`. What only a repository
//! can show is the walk: first-parent order, commits with no note becoming part
//! of a range, the window counting recorded commits, and the exit status a
//! main-branch workflow would see.
//!
//! Notes are written directly rather than measured, so this runs without
//! valgrind and the instruction counts are exactly the ones asserted on.

use std::path::Path;
use std::process::{Command, Output};

fn git(dir: &Path, args: &[&str]) -> String {
    let out = Command::new("git")
        .args(["-c", "user.name=tak-test", "-c", "user.email=t@example.com"])
        .args(args)
        .current_dir(dir)
        .output()
        .unwrap_or_else(|e| panic!("git {args:?} failed to spawn: {e}"));
    assert!(
        out.status.success(),
        "git {args:?} failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8_lossy(&out.stdout).trim_end().to_string()
}

fn tak(dir: &Path, args: &[&str]) -> Output {
    tak_env(dir, args, &[])
}

fn tak_env(dir: &Path, args: &[&str], env: &[(&str, &str)]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_tak"))
        .args(args)
        // No remote exists, so the refresh fails and falls back to local notes,
        // as it does offline. Pointing at a name keeps it from guessing.
        .args(["--remote", "nowhere", "--no-credit"])
        // The default gate and trailer policy, whatever the environment
        // running the tests says.
        .env_remove("TAK_GATE_PCT")
        .env_remove("TAK_ACCEPT_TRAILERS")
        .env_remove("TAK_ALLOW_EMPTY")
        // The warning's shape depends on it, and CI sets it.
        .env_remove("GITHUB_ACTIONS")
        .envs(env.iter().copied())
        .current_dir(dir)
        .output()
        .expect("failed to run tak")
}

fn line(bench: &str, runner: &str, instructions: u64) -> String {
    format!(
        r#"{{"bench":"{bench}","metrics":{{"instructions":{instructions}.0,"wall_min_ms":1.0}},"runner":"{runner}","tool":"self","ts":"2026-01-01T00:00:00Z","v":1}}"#
    )
}

/// c0..c7 on one first-parent line. `startup` on `gha`:
///
/// ```text
/// c0 1000   c1 1000   c2 —   c3 1100   c4 1100   c5 1105   c6 1110   c7 1115
/// ```
///
/// A 10% step somewhere in c2..c3, then a creep of 1.36% in steps of ~0.45%.
/// c7 also carries the first measurement on a second runner.
fn repo() -> (tempfile::TempDir, Vec<String>) {
    let dir = tempfile::Builder::new()
        .prefix("tak-detect-")
        .tempdir()
        .unwrap();
    let d = dir.path();
    git(d, &["init", "--quiet", "-b", "main"]);
    let values = [
        Some(1000),
        Some(1000),
        None,
        Some(1100),
        Some(1100),
        Some(1105),
        Some(1110),
        Some(1115),
    ];
    let mut shas = Vec::new();
    for (i, v) in values.iter().enumerate() {
        git(
            d,
            &["commit", "--quiet", "--allow-empty", "-m", &format!("c{i}")],
        );
        let sha = git(d, &["rev-parse", "HEAD"]);
        if let Some(v) = v {
            let mut body = line("startup", "gha", *v);
            if i == 7 {
                body = format!("{body}\n{}", line("startup", "bigger", 9000));
            }
            git(d, &["notes", "--ref", "refs/notes/tak", "add", "-m", &body]);
        }
        shas.push(sha);
    }
    (dir, shas)
}

fn stdout(o: &Output) -> String {
    String::from_utf8_lossy(&o.stdout).into_owned()
}

fn short(sha: &str) -> &str {
    &sha[..12]
}

/// The run for the commit that introduced a step fails, and names the range
/// it could have come from, because c2 was never measured.
#[test]
fn a_step_onto_the_head_fails_and_names_its_range() {
    let (dir, c) = repo();
    let out = tak(dir.path(), &["detect", &c[3]]);
    let md = stdout(&out);
    assert!(!out.status.success(), "a 10% step must fail: {md}");
    assert!(
        md.contains(&format!("`{}..{}` (2 commits)", short(&c[1]), short(&c[3]))),
        "{md}"
    );
    assert!(md.contains("**+10.00%** ⚠️"), "{md}");
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("stepped up by more than 1%"),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );

    let out = tak(dir.path(), &["detect", &c[3], "--no-gate"]);
    assert!(out.status.success(), "--no-gate reports without failing");
}

/// The gate comes from the working tree's `tak.toml`, per benchmark, exactly
/// as for `tak compare`: a report-only benchmark's 10% step is reported and
/// does not fail, and a loosened gate lets it through as a pass.
#[test]
fn a_benchmark_gate_in_tak_toml_applies() {
    let (dir, c) = repo();
    let toml = dir.path().join("tak.toml");

    std::fs::write(
        &toml,
        "[bench.startup]\ncmd = [\"true\"]\ngate = { enabled = false }\n",
    )
    .unwrap();
    let out = tak(dir.path(), &["detect", &c[3]]);
    let md = stdout(&out);
    assert!(out.status.success(), "report-only never fails: {md}");
    assert!(md.contains("**+10.00%** (not gated)"), "{md}");
    assert!(md.contains("Every benchmark here is report-only"), "{md}");

    std::fs::write(
        &toml,
        "[bench.startup]\ncmd = [\"true\"]\ngate = { pct = 20.0 }\n",
    )
    .unwrap();
    let out = tak(dir.path(), &["detect", &c[3]]);
    let md = stdout(&out);
    assert!(out.status.success(), "10% is within a 20% gate: {md}");
    assert!(md.contains("| 20% |"), "{md}");

    std::fs::write(
        &toml,
        "[bench.startup]\ncmd = [\"true\"]\ngate = { pct = 5.0 }\n",
    )
    .unwrap();
    let out = tak(dir.path(), &["detect", &c[3]]);
    assert!(!out.status.success(), "10% is past a 5% gate");
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("stepped beyond their gate"),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
}

/// The next commit passes: a main workflow fails once, on the commit that
/// caused the step, not on every push after it.
#[test]
fn the_commit_after_a_step_passes_and_still_reports_it() {
    let (dir, c) = repo();
    let out = tak(dir.path(), &["detect", &c[4]]);
    let md = stdout(&out);
    assert!(out.status.success(), "{md}");
    assert!(md.contains("Earlier steps above the 1% gate"), "{md}");
    assert!(
        md.contains(&format!("`{}..{}`", short(&c[1]), short(&c[3]))),
        "{md}"
    );
    assert!(md.contains(&format!("| `{}` |", short(&c[4]))), "{md}");
}

/// Creep made of sub-threshold steps is reported from after the last step,
/// and never fails. The second runner is a new series, not a 700% step.
#[test]
fn drift_and_runner_changes_are_reported_not_failed() {
    let (dir, c) = repo();
    let out = tak(dir.path(), &["detect"]);
    let md = stdout(&out);
    assert!(out.status.success(), "{md}");
    assert!(md.contains("Sustained drift"), "{md}");
    assert!(
        md.contains(&format!(
            "+1.36% (1,100 → 1,115) over 5 recorded commits since `{}`",
            short(&c[3])
        )),
        "{md}"
    );
    assert!(
        md.contains("nothing to compare against: `startup` on `bigger`"),
        "{md}"
    );
    assert!(md.contains("8 first-parent commit(s)"), "{md}");
}

/// The window counts recorded commits. Two of them hold one step and no room
/// for drift or for the step further back.
#[test]
fn the_window_bounds_what_is_examined() {
    let (dir, _) = repo();
    let md = stdout(&tak(dir.path(), &["detect", "--window", "2"]));
    assert!(md.contains("2 first-parent commit(s)"), "{md}");
    assert!(!md.contains("Sustained drift"), "{md}");
    assert!(!md.contains("Earlier steps"), "{md}");

    let out = tak(dir.path(), &["detect", "--window", "1"]);
    assert!(!out.status.success(), "a window of one cannot hold a step");
}

/// A head with nothing recorded compares nothing. That fails, and says why,
/// rather than reading like a pass.
#[test]
fn an_unrecorded_head_fails_as_nothing_compared() {
    let (dir, c) = repo();
    let out = tak(dir.path(), &["detect", &c[2]]);
    let md = stdout(&out);
    assert!(!out.status.success(), "{md}");
    assert!(
        md.contains("**Nothing was compared at `") && md.contains("so this check fails.**"),
        "{md}"
    );
    assert!(md.contains("No instruction counts are recorded"), "{md}");
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("pass --allow-empty"),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
}

/// The first recording has nothing before it. It fails by default and passes
/// with `--allow-empty` or with `--no-gate`, which never fails.
#[test]
fn a_first_recording_needs_allow_empty() {
    let (dir, c) = repo();
    let out = tak(dir.path(), &["detect", &c[0]]);
    let md = stdout(&out);
    assert!(!out.status.success(), "{md}");
    assert!(
        md.contains("has an earlier recorded point in the window"),
        "{md}"
    );
    assert!(md.contains("pass `--allow-empty`"), "{md}");

    let out = tak(dir.path(), &["detect", &c[0], "--no-gate"]);
    let md = stdout(&out);
    assert!(out.status.success(), "--no-gate never fails: {md}");
    assert!(md.contains("nothing was gated** (`--no-gate`)"), "{md}");

    let out = tak(dir.path(), &["detect", &c[0], "--allow-empty"]);
    let md = stdout(&out);
    assert!(out.status.success(), "{md}");
    assert!(md.contains("nothing was gated** (`allow_empty`)"), "{md}");
    assert_eq!(
        stderr(&out),
        format!(
            "warning: nothing was compared at {}; passing because `allow_empty` is on\n",
            short(&c[0])
        )
    );

    // It waives only the empty case: a real step still fails.
    let out = tak(dir.path(), &["detect", &c[3], "--allow-empty"]);
    assert!(!out.status.success(), "{}", stdout(&out));
}

fn stderr(o: &Output) -> String {
    String::from_utf8_lossy(&o.stderr).into_owned()
}

/// `[gate] allow_empty = true` in the working tree's `tak.toml` does what
/// `--allow-empty` does, and the report and warning cannot tell them apart:
/// the flag is one spelling of the setting. The environment overrides the
/// file in both directions, and the flag overrides the environment.
#[test]
fn allow_empty_from_tak_toml_passes_with_a_warning() {
    let (dir, c) = repo();
    std::fs::write(dir.path().join("tak.toml"), "[gate]\nallow_empty = true\n").unwrap();

    let by_setting = tak(dir.path(), &["detect", &c[0]]);
    assert!(by_setting.status.success(), "{}", stdout(&by_setting));
    let by_flag = tak(dir.path(), &["detect", &c[0], "--allow-empty"]);
    assert_eq!(stdout(&by_setting), stdout(&by_flag));
    assert_eq!(stderr(&by_setting), stderr(&by_flag));
    assert!(stderr(&by_setting).starts_with("warning: nothing was compared at "));
    assert!(
        stdout(&by_setting).contains("passes with a warning instead of failing"),
        "{}",
        stdout(&by_setting)
    );

    let off = tak_env(dir.path(), &["detect", &c[0]], &[("TAK_ALLOW_EMPTY", "0")]);
    assert!(!off.status.success(), "TAK_ALLOW_EMPTY=0 beats the file");
    assert!(
        stdout(&off).contains("so this check fails.**"),
        "{}",
        stdout(&off)
    );

    let flag = tak_env(
        dir.path(),
        &["detect", &c[0], "--allow-empty"],
        &[("TAK_ALLOW_EMPTY", "0")],
    );
    assert!(flag.status.success(), "the flag beats the environment");

    // A step onto the head still fails with the setting on.
    let step = tak(dir.path(), &["detect", &c[3]]);
    assert!(!step.status.success(), "{}", stdout(&step));
    assert!(!stderr(&step).contains("allow_empty"), "{}", stderr(&step));
}

/// Under GitHub Actions the one warning line is a workflow command, on
/// stderr, so it becomes an annotation without touching the report on stdout.
#[test]
fn allow_empty_warns_as_an_annotation_under_github_actions() {
    let (dir, c) = repo();
    let out = tak_env(
        dir.path(),
        &["detect", &c[0], "--allow-empty"],
        &[("GITHUB_ACTIONS", "true")],
    );
    assert!(out.status.success(), "{}", stdout(&out));
    assert_eq!(
        stderr(&out),
        format!(
            "::warning::tak: nothing was compared at {}; passing because `allow_empty` is on\n",
            short(&c[0])
        )
    );
    assert!(!stdout(&out).contains("::warning"), "{}", stdout(&out));
}

/// First-parent only: a merged side branch's measurements are not trunk
/// points, and a step on the branch is not a step on main.
#[test]
fn merged_branches_are_not_walked() {
    let (dir, _) = repo();
    let d = dir.path();
    git(d, &["checkout", "--quiet", "-b", "side"]);
    git(d, &["commit", "--quiet", "--allow-empty", "-m", "side"]);
    git(
        d,
        &[
            "notes",
            "--ref",
            "refs/notes/tak",
            "add",
            "-m",
            &line("startup", "gha", 50_000),
        ],
    );
    git(d, &["checkout", "--quiet", "main"]);
    git(
        d,
        &["merge", "--quiet", "--no-ff", "-m", "merge side", "side"],
    );
    git(
        d,
        &[
            "notes",
            "--ref",
            "refs/notes/tak",
            "add",
            "-m",
            &line("startup", "gha", 1115),
        ],
    );
    let out = tak(d, &["detect"]);
    let md = stdout(&out);
    assert!(out.status.success(), "{md}");
    assert!(!md.contains("50,000"), "the side branch leaked in: {md}");
}

/// `actions/checkout` fetches one commit by default. The walk then finds
/// nothing earlier, which on its own reads exactly like a first recording; the
/// report has to say the checkout was the reason.
#[test]
fn a_shallow_checkout_is_named_as_the_reason() {
    let (dir, _) = repo();
    let d = dir.path();
    let url = format!("file://{}", d.display());
    git(d, &["clone", "--quiet", "--depth", "2", &url, "shallow"]);
    let shallow = d.join("shallow");
    git(
        &shallow,
        &[
            "fetch",
            "--quiet",
            "origin",
            "+refs/notes/tak:refs/notes/tak",
        ],
    );
    let md = stdout(&tak(&shallow, &["detect"]));
    assert!(md.contains("This checkout is shallow"), "{md}");

    // A full clone is not warned about.
    let md = stdout(&tak(d, &["detect"]));
    assert!(!md.contains("shallow"), "{md}");
}

/// tak refreshes notes with a depth-1 fetch, and git records that as a
/// shallow boundary *in the notes history* — `.git/shallow` appears and
/// `--is-shallow-repository` says true in what is a full clone of the project.
/// A walk that reached the real root must not blame a shallow checkout.
#[test]
fn a_shallow_notes_fetch_is_not_a_shallow_checkout() {
    let (dir, _) = repo();
    let d = dir.path();
    // A second notes commit, so the depth-1 fetch has a parent to cut off.
    git(
        d,
        &[
            "notes",
            "--ref",
            "refs/notes/tak",
            "append",
            "-m",
            &line("other", "gha", 1),
            "HEAD~1",
        ],
    );
    let url = format!("file://{}", d.display());
    git(d, &["clone", "--quiet", &url, "full"]);
    let full = d.join("full");
    git(
        &full,
        &[
            "fetch",
            "--quiet",
            "--depth",
            "1",
            "origin",
            "+refs/notes/tak:refs/notes/tak",
        ],
    );
    assert_eq!(
        git(&full, &["rev-parse", "--is-shallow-repository"]),
        "true",
        "the premise: git now calls this clone shallow"
    );

    let md = stdout(&tak(&full, &["detect"]));
    assert!(md.contains("Sustained drift"), "the walk ran: {md}");
    assert!(!md.contains("This checkout is shallow"), "{md}");
}

/// A single first-parent line of `total` empty commits, with `startup` on
/// `gha` recorded at the root and at the tip.
///
/// fast-import builds them in one process; ten thousand `git commit` calls
/// would not.
fn linear_history(prefix: &str, total: usize, root: u64, tip: u64) -> tempfile::TempDir {
    use std::io::Write;

    let dir = tempfile::Builder::new().prefix(prefix).tempdir().unwrap();
    let d = dir.path();
    git(d, &["init", "--quiet", "-b", "main"]);
    let mut stream = String::new();
    for i in 1..=total {
        stream.push_str(&format!(
            "commit refs/heads/main\nmark :{i}\ncommitter t <t@example.com> {i} +0000\ndata 2\nc\n"
        ));
        if i > 1 {
            stream.push_str(&format!("from :{}\n", i - 1));
        }
        stream.push('\n');
    }
    let mut child = Command::new("git")
        .args(["fast-import", "--quiet"])
        .current_dir(d)
        .stdin(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    child
        .stdin
        .take()
        .unwrap()
        .write_all(stream.as_bytes())
        .unwrap();
    assert!(child.wait().unwrap().success(), "fast-import failed");
    git(d, &["reset", "--quiet", "--hard", "main"]);

    let first = git(d, &["rev-list", "--max-parents=0", "HEAD"]);
    for (rev, value) in [(first.as_str(), root), ("HEAD", tip)] {
        git(
            d,
            &[
                "notes",
                "--ref",
                "refs/notes/tak",
                "add",
                "-m",
                &line("startup", "gha", value),
                rev,
            ],
        );
    }
    dir
}

/// A previous recording further back than the scan limit is never reached.
/// The walk must say it stopped at the limit rather than report the head's
/// series as brand new, and the empty comparison must still fail. One commit
/// past the limit is the tightest case: the root is the only one left out.
#[test]
fn hitting_the_scan_limit_is_named_as_the_reason() {
    let total = tak_cli::detect::SCAN_LIMIT + 1;
    let dir = linear_history("tak-detect-over-", total, 1000, 2000);

    let out = tak(dir.path(), &["detect"]);
    let md = stdout(&out);
    assert!(!out.status.success(), "nothing was compared: {md}");
    assert!(md.contains("limit of 10,000 first-parent commits"), "{md}");
    assert!(!md.contains("This checkout is shallow"), "{md}");
}

/// A history of exactly the limit is complete: the walk reaches the root, the
/// root's recording is compared, and nothing claims older ones were missed.
#[test]
fn a_history_of_exactly_the_limit_is_not_cut_short() {
    let total = tak_cli::detect::SCAN_LIMIT;
    let dir = linear_history("tak-detect-exact-", total, 1000, 1000);

    let out = tak(dir.path(), &["detect"]);
    let md = stdout(&out);
    assert!(out.status.success(), "{md}");
    assert!(
        md.contains(&format!("Walked {total} first-parent commit(s)")),
        "{md}"
    );
    assert!(!md.contains("limit of"), "{md}");
    assert!(!md.contains("Nothing was compared"), "{md}");
}

/// c0 recorded at 1000, c1 unrecorded with a `Tak-Accept: startup` trailer,
/// c2 recorded at 1100: a 10% step whose range, c0..c2, holds the trailer.
fn accepted_repo() -> (tempfile::TempDir, Vec<String>) {
    accepted_repo_with("Tak-Accept: startup")
}

/// As [`accepted_repo`], with `trailer` as c1's trailer paragraph.
fn accepted_repo_with(trailer: &str) -> (tempfile::TempDir, Vec<String>) {
    let c1 = format!("c1\n\n{trailer}");
    let dir = tempfile::Builder::new()
        .prefix("tak-detect-accept-")
        .tempdir()
        .unwrap();
    let d = dir.path();
    git(d, &["init", "--quiet", "-b", "main"]);
    let mut shas = Vec::new();
    for (msg, value) in [("c0", Some(1000)), (c1.as_str(), None), ("c2", Some(1100))] {
        git(d, &["commit", "--quiet", "--allow-empty", "-m", msg]);
        if let Some(v) = value {
            git(
                d,
                &[
                    "notes",
                    "--ref",
                    "refs/notes/tak",
                    "add",
                    "-m",
                    &line("startup", "gha", v),
                ],
            );
        }
        shas.push(git(d, &["rev-parse", "HEAD"]));
    }
    (dir, shas)
}

/// Trailers are ignored unless `accept_trailers` is on, and the report says
/// one was found. On, the trailer in the step's range accepts it.
#[test]
fn a_trailer_in_the_steps_range_accepts_it_when_enabled() {
    let (dir, c) = accepted_repo();

    let out = tak(dir.path(), &["detect"]);
    let md = stdout(&out);
    assert!(!out.status.success(), "trailers are off by default: {md}");
    assert!(md.contains("were found but not honoured"), "{md}");

    let out = tak_env(dir.path(), &["detect"], &[("TAK_ACCEPT_TRAILERS", "1")]);
    let md = stdout(&out);
    assert!(out.status.success(), "{md}");
    assert!(md.contains("**+10.00%** (accepted)"), "{md}");
    assert!(
        md.contains(&format!("(accepted by `Tak-Accept` in `{}`)", short(&c[1]))),
        "{md}"
    );
}

/// One trailer names one benchmark, commas included, as in `tak compare`:
/// `Tak-Accept: startup,other` does not accept `startup`, and the step fails.
/// Repeated trailers are how to name several.
#[test]
fn a_comma_trailer_does_not_split_into_names() {
    let (dir, _) = accepted_repo_with("Tak-Accept: startup,other");
    let out = tak_env(dir.path(), &["detect"], &[("TAK_ACCEPT_TRAILERS", "1")]);
    let md = stdout(&out);
    assert!(!out.status.success(), "`startup` was not named: {md}");
    assert!(!md.contains("(accepted"), "{md}");
    assert!(
        md.contains("so nothing was accepted: `startup,other`"),
        "{md}"
    );

    let (dir, _) = accepted_repo_with("Tak-Accept: other\nTak-Accept: startup");
    let out = tak_env(dir.path(), &["detect"], &[("TAK_ACCEPT_TRAILERS", "1")]);
    let md = stdout(&out);
    assert!(out.status.success(), "{md}");
    assert!(md.contains("**+10.00%** (accepted)"), "{md}");
}

/// On the nothing-compared path, a misspelt acceptance is still named, so
/// `--allow-empty` cannot hide it.
#[test]
fn an_unused_acceptance_is_named_even_when_nothing_was_compared() {
    let (dir, c) = accepted_repo();
    let out = tak(
        dir.path(),
        &["detect", &c[0], "--accept", "startupp", "--allow-empty"],
    );
    let md = stdout(&out);
    assert!(out.status.success(), "{md}");
    assert!(md.contains("Nothing was compared"), "{md}");
    assert!(
        md.contains("so nothing was accepted: `startupp` (`--accept`)"),
        "{md}"
    );
}

/// `--accept` works whatever the setting says: the escape for a manual rerun
/// of a main-branch job whose step was accepted on its pull request.
#[test]
fn accept_flag_waives_a_step_onto_the_head() {
    let (dir, _) = accepted_repo();
    let out = tak(dir.path(), &["detect", "--accept", "startup"]);
    let md = stdout(&out);
    assert!(out.status.success(), "{md}");
    assert!(md.contains("(accepted by `--accept`)"), "{md}");

    // Exact names only: another benchmark's acceptance changes nothing.
    let out = tak(dir.path(), &["detect", "--accept", "other"]);
    assert!(!out.status.success());
    // And an empty one is an error before anything is walked.
    let out = tak(dir.path(), &["detect", "--accept", ""]);
    assert!(!out.status.success());
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("empty"),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
}

/// The range is first-parent. A trailer on a branch commit behind a merge's
/// second parent did not land on main as its own commit, so it does not
/// accept the step.
#[test]
fn only_first_parent_trailers_in_the_range_count() {
    let (dir, _) = repo();
    let d = dir.path();
    // A side branch whose commit carries the trailer, merged with a plain
    // merge commit that is then recorded well above c7.
    git(d, &["checkout", "--quiet", "-b", "side"]);
    git(
        d,
        &[
            "commit",
            "--quiet",
            "--allow-empty",
            "-m",
            "side\n\nTak-Accept: startup",
        ],
    );
    git(d, &["checkout", "--quiet", "main"]);
    git(
        d,
        &["merge", "--quiet", "--no-ff", "-m", "merge side", "side"],
    );
    git(
        d,
        &[
            "notes",
            "--ref",
            "refs/notes/tak",
            "add",
            "-m",
            &line("startup", "gha", 1300),
        ],
    );
    let out = tak_env(d, &["detect"], &[("TAK_ACCEPT_TRAILERS", "1")]);
    let md = stdout(&out);
    assert!(
        !out.status.success(),
        "the side commit is not first-parent: {md}"
    );
    assert!(!md.contains("(accepted)"), "{md}");
}