amont 1.6.2

Opinionated git hooks that judge what you are committing, not what is on disk
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
//! pre-push-run-tests-js. git feeds pre-push one line per ref on stdin, so the
//! cases drive the binary the same way.

mod common;
use common::{missing, Repo};
use std::io::Write;
use std::process::{Command, Stdio};

/// Feed the hook a pre-push line for the range `from..to`.
///
/// A REAL range matters: passing a zero remote-oid means "new branch", and the
/// range becomes the single root commit — on which `git diff-tree` reports
/// nothing without `--root`. So no files look changed, no gate runs, and every
/// case passes for the wrong reason. Two commits and an explicit range is what
/// the zsh suite used.
fn push_range(r: &Repo, from: &str, to: &str) -> i32 {
    let line = format!("refs/heads/main {to} refs/heads/main {from}\n");
    let mut child = Command::new(env!("CARGO_BIN_EXE_amont"))
        .arg("--hooks-dir")
        .arg(r.path(".git/hooks"))
        .arg("pre-push-run-tests-js")
        .current_dir(&r.dir)
        .stdin(Stdio::piped())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("spawn");
    child
        .stdin
        .as_mut()
        .unwrap()
        .write_all(line.as_bytes())
        .unwrap();
    child.wait().expect("wait").code().unwrap_or(-1)
}

fn head(r: &Repo) -> String {
    String::from_utf8_lossy(&r.git(&["rev-parse", "HEAD"]).stdout)
        .trim()
        .to_string()
}

#[test]
fn runs_a_packages_gate_and_passes_when_it_succeeds() {
    if missing("npm") {
        return;
    }
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"test":"exit 0"}}"#,
    );
    r.commit("chore: base");
    let base = head(&r);
    r.stage("src/a.js", "const x = 1;\n");
    r.commit("feat: a");
    assert_eq!(push_range(&r, &base, &head(&r)), 0);
}

#[test]
fn fails_when_the_gate_fails() {
    if missing("npm") {
        return;
    }
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"test":"exit 1"}}"#,
    );
    r.commit("chore: base");
    let base = head(&r);
    r.stage("src/a.js", "const x = 1;\n");
    r.commit("feat: a");
    assert_ne!(push_range(&r, &base, &head(&r)), 0);
}

/// typecheck → test:unit → test, cheapest first, so a type error costs seconds
/// rather than a full suite. `lint` is deliberately absent — pre-commit-lint-js
/// already lints staged files.
///
/// The recording scripts avoid `sh -c '...'`: npm runs scripts through cmd.exe
/// on Windows, where `'` does not quote. cmd would eat the `>>` redirect itself
/// and run `sh -c 'echo typecheck`, creating ran.txt EMPTY — the gate looks like
/// it ran nothing while exiting 0. `echo x>>ran.txt` means the same thing to sh
/// and to cmd. No space before `>>`: cmd would include it in the written line.
#[test]
fn runs_the_gate_in_order_and_never_lint() {
    if missing("npm") {
        return;
    }
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"typecheck":"echo typecheck>>ran.txt","lint":"echo lint>>ran.txt","test:unit":"echo unit>>ran.txt","test":"echo test>>ran.txt"}}"#,
    );
    r.commit("chore: base");
    let base = head(&r);
    r.stage("src/a.js", "const x = 1;\n");
    r.commit("feat: a");
    assert_eq!(push_range(&r, &base, &head(&r)), 0);
    let ran = std::fs::read_to_string(r.path("ran.txt")).unwrap_or_default();
    assert_eq!(
        ran.lines().map(str::trim).collect::<Vec<_>>(),
        vec!["typecheck", "unit", "test"]
    );
    assert!(
        !ran.contains("lint"),
        "lint belongs to pre-commit, not here"
    );
}

#[test]
fn a_failure_skips_the_rest_of_the_gate() {
    if missing("npm") {
        return;
    }
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"typecheck":"exit 1","test:unit":"echo unit>>ran.txt","test":"echo test>>ran.txt"}}"#,
    );
    r.commit("chore: base");
    let base = head(&r);
    r.stage("src/a.js", "const x = 1;\n");
    r.commit("feat: a");
    assert_ne!(push_range(&r, &base, &head(&r)), 0);
    assert!(
        !r.path("ran.txt").exists(),
        "nothing after the failure should run"
    );
}

/// Feed the hook a NEW-BRANCH pre-push line: the remote oid is all zeroes.
fn push_new_branch(r: &Repo, branch: &str, tip: &str) -> i32 {
    let zero = "0".repeat(tip.len());
    let line = format!("refs/heads/{branch} {tip} refs/heads/{branch} {zero}\n");
    let mut child = Command::new(env!("CARGO_BIN_EXE_amont"))
        .arg("--hooks-dir")
        .arg(r.path(".git/hooks"))
        .arg("pre-push-run-tests-js")
        .current_dir(&r.dir)
        .stdin(Stdio::piped())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("spawn");
    child
        .stdin
        .as_mut()
        .unwrap()
        .write_all(line.as_bytes())
        .unwrap();
    child.wait().expect("wait").code().unwrap_or(-1)
}

/// A repo with a REAL bare origin holding `main`, so `--not --remotes` has a
/// remote-tracking ref to exclude against — without one, a new branch's whole
/// history is "new" and the case proves nothing about commit selection.
fn with_origin(r: &Repo) {
    let origin = r.path(".git/test-origin.git");
    r.git(&["init", "-q", "--bare", origin.to_str().expect("utf8")]);
    r.git(&["remote", "add", "origin", origin.to_str().expect("utf8")]);
    r.git(&["push", "-q", "--no-verify", "origin", "main"]);
}

/// A new branch carrying several commits must be judged by ALL of them.
///
/// The check used to build its own range: `remote_oid == zero` became the
/// single string `<tip>`, and `diff-tree <tip>` reports only what the TIP
/// changed against its parent. So a multi-commit new branch whose FIRST commit
/// breaks the JS package, with a docs commit on top, selected no package at all
/// and the push proceeded green with the suite never having run. This case
/// passes VACUOUSLY under the old code, which is exactly the bug.
#[test]
fn a_new_branch_is_judged_by_every_commit_not_just_its_tip() {
    if missing("npm") {
        return;
    }
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"test":"exit 1"}}"#,
    );
    r.commit("chore: base");
    with_origin(&r);

    r.git(&["checkout", "-q", "-b", "feat/x"]);
    r.stage("src/a.js", "const x = 1;\n");
    r.commit("feat: the js change");
    r.stage("README.md", "docs\n");
    r.commit("docs: on top of it");

    assert_ne!(
        push_new_branch(&r, "feat/x", &head(&r)),
        0,
        "the failing package was two commits back and the gate never ran"
    );
}

/// A file changed and reverted within one push still selects its package.
///
/// `diff-tree A..B` is a two-TREE compare, not a commit walk — unlike `log`,
/// where the identical syntax means the opposite. A `.js` file edited by one
/// commit and restored by a later one in the same push nets to "unchanged"
/// between the endpoints, so the package was never selected and its suite never
/// ran, even though the push plainly carries a commit that touched it.
#[test]
fn a_file_reverted_later_in_the_same_push_still_selects_its_package() {
    if missing("npm") {
        return;
    }
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"test":"exit 1"}}"#,
    );
    r.stage("src/a.js", "const x = 1;\n");
    r.commit("chore: base");
    let base = head(&r);

    r.stage("src/a.js", "const x = 2;\n");
    r.commit("feat: touch the js");
    r.stage("src/a.js", "const x = 1;\n");
    r.commit("revert: put it back");

    assert_ne!(
        push_range(&r, &base, &head(&r)),
        0,
        "the endpoints' trees are identical, but a pushed commit touched .js"
    );
}

/// A change with no JS/TS files runs nothing at all.
#[test]
fn a_non_js_change_runs_no_gate() {
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"test":"exit 1"}}"#,
    );
    r.commit("chore: seed");
    let base = head(&r);
    r.stage("README.md", "docs\n");
    r.commit("docs: readme");
    assert_eq!(push_range(&r, &base, &head(&r)), 0);
}

// --- what a repository has already gated at COMMIT time ---------------------
//
// `typecheck` is in GATE because nothing checks it earlier. A repository that
// moves it earlier — so a type error is heard at the commit that caused it,
// not an hour later at push — must not then be charged for it twice. These
// pin both halves: that a real declaration removes it, and that a declaration
// which would NOT run leaves it exactly where it was.
//
// The signal throughout is `"typecheck": "exit 1"`. If it runs, the push
// fails; if it is correctly skipped, `test` still runs and the push passes.
// That cannot be satisfied by accident in either direction.

/// Same as `push_range`, but keeps stdout so the message can be read.
fn push_range_out(r: &Repo, from: &str, to: &str) -> (i32, String) {
    let line = format!("refs/heads/main {to} refs/heads/main {from}\n");
    let mut child = Command::new(env!("CARGO_BIN_EXE_amont"))
        .arg("--hooks-dir")
        .arg(r.path(".git/hooks"))
        .arg("pre-push-run-tests-js")
        .current_dir(&r.dir)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn");
    child
        .stdin
        .as_mut()
        .unwrap()
        .write_all(line.as_bytes())
        .unwrap();
    let out = child.wait_with_output().expect("wait");
    (
        out.status.code().unwrap_or(-1),
        format!(
            "{}{}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr)
        ),
    )
}

/// A repo whose `typecheck` script would FAIL, plus a passing `test`, with one
/// JS commit to push. Returns the base to push from.
fn repo_with_failing_typecheck() -> (Repo, String) {
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"typecheck":"exit 1","test":"exit 0"}}"#,
    );
    r.commit("chore: base");
    let base = head(&r);
    r.stage("src/a.ts", "export const x = 1\n");
    r.commit("feat: add a ts file");
    (r, base)
}

fn trust_manifest(r: &Repo) {
    let out = Command::new(env!("CARGO_BIN_EXE_amont"))
        .arg("trust")
        .current_dir(&r.dir)
        .output()
        .expect("amont trust");
    assert!(
        out.status.success(),
        "could not trust the manifest: {}",
        String::from_utf8_lossy(&out.stderr)
    );
}

/// Wire the real hooks into the fixture, so a commit stamps.
///
/// Suppression is no longer paper-based: the push gate defers to a
/// commit-time declaration only for commits that CARRY its stamp, and the
/// stamp comes from pre-commit + post-commit actually running.
fn install_hooks(r: &Repo) {
    let out = Command::new(env!("CARGO_BIN_EXE_amont"))
        .arg("init")
        .current_dir(&r.dir)
        .stdin(Stdio::null())
        .output()
        .expect("amont init");
    assert!(
        out.status.success(),
        "could not install hooks: {}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
}

/// A commit that RUNS the hooks — unlike `Repo::commit`, which passes
/// `--no-verify` precisely so ordinary fixtures stay hook-free.
fn verified_commit(r: &Repo, msg: &str) {
    let out = r.git(&["commit", "-q", "-m", msg]);
    assert!(
        out.status.success(),
        "verified commit failed: {}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
}

/// A repo whose `typecheck` LOGS each run and passes, with a trusted
/// declaration and real hooks. The log length is the observable: it says
/// exactly how many times the script executed, wherever from.
///
/// The declared command is `node tc.js`, NOT `npm run typecheck`: a declared
/// external spawns its literal program, and on Windows `npm` is `npm.cmd`,
/// which `Command::new("npm")` cannot start — the check would report
/// Unavailable at commit time and nothing would ever stamp. `node` is a real
/// .exe everywhere. The push-time gate still goes through `npm run`, so both
/// spellings of "the same check" are exercised — which is exactly the
/// name-is-the-contract argument in `gated_at_commit`'s doc.
fn stamped_repo() -> (Repo, String) {
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"typecheck":"node tc.js","test":"exit 0"}}"#,
    );
    r.stage("tc.js", "require('fs').appendFileSync('tc.log','x')\n");
    r.commit("chore: base");
    let base = head(&r);
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  block  node tc.js\n",
    );
    r.commit("chore: typecheck at commit time");
    trust_manifest(&r);
    install_hooks(&r);
    (r, base)
}

fn typecheck_runs(r: &Repo) -> usize {
    std::fs::read_to_string(r.dir.join("tc.log"))
        .map(|s| s.len())
        .unwrap_or(0)
}

#[test]
fn a_declared_pre_commit_check_removes_that_script_from_the_push_gate() {
    if missing("npm") {
        return;
    }
    let (r, base) = stamped_repo();
    r.stage("src/a.ts", "export const x = 1\n");
    verified_commit(&r, "feat: add a ts file");
    assert_eq!(
        typecheck_runs(&r),
        1,
        "the commit itself should have run typecheck exactly once"
    );

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_eq!(
        code, 0,
        "the push failed despite a stamped, gated typecheck:\n{out}"
    );
    // And it SAYS so. A check that stops running silently is the failure this
    // whole project is arranged against.
    assert!(
        out.contains("typecheck") && out.contains("gated at commit"),
        "the push did not say typecheck had moved:\n{out}"
    );
    assert_eq!(
        typecheck_runs(&r),
        1,
        "the push re-ran a script the commit already stamped:\n{out}"
    );
}

/// THE hole the fifth shim exists to close: `--no-verify` skips pre-commit,
/// so the check never ran — and the push gate must notice, run the script
/// itself, and say why, instead of trusting the declaration's word for it.
#[test]
fn a_no_verify_commit_brings_the_push_gate_back() {
    if missing("npm") {
        return;
    }
    let (r, base) = stamped_repo();
    r.stage("src/a.ts", "export const x = 1\n");
    r.commit("feat: dodge the hooks"); // Repo::commit is --no-verify

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_eq!(
        code, 0,
        "typecheck passes when run — only WHO runs it moved:\n{out}"
    );
    assert!(
        out.contains("no record of it"),
        "the push must say why the gate is running:\n{out}"
    );
    assert!(
        !out.contains("gated at commit"),
        "claimed commit-time cover for a commit that dodged the hooks:\n{out}"
    );
    assert_eq!(
        typecheck_runs(&r),
        1,
        "the gate did not run typecheck for the unchecked commit:\n{out}"
    );
}

/// One unchecked commit poisons the push: the stamped commit next to it does
/// not vouch for it, so the gate runs for the ref.
#[test]
fn one_unstamped_commit_re_runs_the_gate_for_the_push() {
    if missing("npm") {
        return;
    }
    let (r, base) = stamped_repo();
    r.stage("src/a.ts", "export const x = 1\n");
    verified_commit(&r, "feat: checked");
    r.stage("src/b.ts", "export const y = 2\n");
    r.commit("feat: unchecked"); // --no-verify

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_eq!(code, 0, "{out}");
    assert!(
        out.contains("no record of it"),
        "one unstamped commit must bring the gate back:\n{out}"
    );
    assert_eq!(
        typecheck_runs(&r),
        2,
        "commit-time once, push-time once for the unchecked commit:\n{out}"
    );
}

/// The safety half, and the one worth getting wrong only once.
///
/// An untrusted manifest does not run — so if the push gate deferred to it, a
/// repository would have types checked at NEITHER end while both ends reported
/// green. `manifest::gate` turns untrusted declarations into `Unusable`, and
/// `gated_at_commit` counts only `Runnable`; this is what holds that together.
#[test]
fn an_untrusted_declaration_leaves_the_push_gate_alone() {
    if missing("npm") {
        return;
    }
    let (r, base) = repo_with_failing_typecheck();
    // Written and committed, deliberately NOT trusted — the cloned-repo case.
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  block  npm run typecheck\n",
    );
    r.commit("chore: declare typecheck, untrusted");

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_ne!(
        code, 0,
        "an untrusted declaration removed the push gate — nothing typechecks now:\n{out}"
    );
}

/// Same rule, one layer up: a declaration the author switched off is not a
/// check, so it cannot excuse the push gate either.
#[test]
fn a_skipped_declaration_leaves_the_push_gate_alone() {
    if missing("npm") {
        return;
    }
    let (r, base) = repo_with_failing_typecheck();
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  block  npm run typecheck\n",
    );
    r.commit("chore: typecheck at commit time");
    trust_manifest(&r);
    r.git(&["config", "--add", "hook.skip", "pre-commit-typecheck"]);

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_ne!(
        code, 0,
        "a skipped declaration removed the push gate — nothing typechecks now:\n{out}"
    );
}

/// A declaration on the OTHER stage means nothing here: `pre-push typecheck`
/// does not gate anything at commit time, so the entry stays.
#[test]
fn a_pre_push_declaration_does_not_count_as_commit_time() {
    if missing("npm") {
        return;
    }
    let (r, base) = repo_with_failing_typecheck();
    r.stage(
        "amont.conf",
        "pre-push  typecheck  *.ts,*.tsx  block  npm run typecheck\n",
    );
    r.commit("chore: declare on the wrong stage");
    trust_manifest(&r);

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_ne!(
        code, 0,
        "a pre-push declaration was treated as commit-time cover:\n{out}"
    );
}

/// Nothing declared is the overwhelmingly common case, and it must be exactly
/// what it was before any of this existed.
#[test]
fn with_no_declaration_the_gate_is_unchanged() {
    if missing("npm") {
        return;
    }
    let (r, base) = repo_with_failing_typecheck();
    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_ne!(code, 0, "typecheck did not run:\n{out}");
    assert!(
        !out.contains("gated at commit"),
        "said something about commit-time gating with nothing declared:\n{out}"
    );
}

/// A `warn` declaration lets a failing commit through — it cannot stand in for
/// a push check that blocks. Severity is part of "would actually run count".
#[test]
fn a_warn_declaration_leaves_the_push_gate_alone() {
    if missing("npm") {
        return;
    }
    let (r, base) = repo_with_failing_typecheck();
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  warn  npm run typecheck\n",
    );
    r.commit("chore: typecheck at commit time, warn only");
    trust_manifest(&r);

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_ne!(
        code, 0,
        "a warn declaration removed the push gate — a failing commit and a green push:\n{out}"
    );
    assert!(
        !out.contains("gated at commit"),
        "claimed commit-time cover a warn declaration cannot give:\n{out}"
    );
}

/// The same downgrade arrived at sideways: the declaration says `block`, an
/// `amont.severity.*` override says `warn`. The EFFECTIVE severity is what a
/// commit actually experiences, so it is what the push gate must consult.
#[test]
fn a_severity_override_to_warn_leaves_the_push_gate_alone() {
    if missing("npm") {
        return;
    }
    let (r, base) = repo_with_failing_typecheck();
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  block  npm run typecheck\n",
    );
    r.commit("chore: typecheck at commit time");
    trust_manifest(&r);
    r.git(&["config", "amont.severity.pre-commit-typecheck", "warn"]);

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_ne!(
        code, 0,
        "a severity override downgraded the commit check and the push gate still deferred to it:\n{out}"
    );
}

/// A `*.ts,*.tsx` declaration says nothing about a `.js` change. A push whose
/// JS changes fall outside the declared scope carries commits the commit-time
/// check never judged, so the full gate runs for that ref.
#[test]
fn a_push_outside_the_declared_scope_runs_the_full_gate() {
    if missing("npm") {
        return;
    }
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"typecheck":"exit 1","test":"exit 0"}}"#,
    );
    r.commit("chore: base");
    let base = head(&r);
    r.stage("src/legacy.js", "const x = 1;\n");
    r.commit("feat: a js change the ts scope never saw");
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  block  npm run typecheck\n",
    );
    r.commit("chore: typecheck at commit time");
    trust_manifest(&r);

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_ne!(
        code, 0,
        "a .js push was excused by a .ts-scoped declaration — checked at neither end:\n{out}"
    );
    assert!(
        !out.contains("gated at commit"),
        "claimed commit-time cover for files outside the declared scope:\n{out}"
    );
}

/// The declared command runs at the repo ROOT. A monorepo sub-package's gate
/// is a different command in a different directory, and no root declaration
/// has judged it — so it is never skipped on the root's account.
#[test]
fn a_root_declaration_does_not_excuse_a_sub_package() {
    if missing("npm") {
        return;
    }
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"root","scripts":{"typecheck":"exit 0","test":"exit 0"}}"#,
    );
    r.stage(
        "packages/b/package.json",
        r#"{"name":"b","scripts":{"typecheck":"exit 1"}}"#,
    );
    r.commit("chore: base");
    let base = head(&r);
    r.stage("packages/b/src/a.ts", "export const x = 1\n");
    r.commit("feat: change the sub-package");
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  block  npm run typecheck\n",
    );
    r.commit("chore: typecheck at commit time");
    trust_manifest(&r);

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_ne!(
        code, 0,
        "the root declaration excused packages/b's own typecheck:\n{out}"
    );
}

/// A BLOCKED commit attempt must not leave a marker that vouches for the
/// `--no-verify` retry of the same tree. pre_commit records an empty gate
/// list on a Block verdict — the design property the dispatcher's comment
/// states, pinned end to end here: gated typecheck PASSES while a sibling
/// check blocks, the same tree is then committed with --no-verify, and the
/// push must re-run the gate.
#[cfg(unix)]
#[test]
fn a_blocked_attempt_cannot_vouch_for_a_no_verify_retry() {
    if missing("npm") {
        return;
    }
    let r = Repo::new();
    r.stage(
        "package.json",
        r#"{"name":"t","scripts":{"typecheck":"node tc.js","test":"exit 0"}}"#,
    );
    r.stage("tc.js", "require('fs').appendFileSync('tc.log','x')\n");
    r.stage("deny.sh", "#!/bin/sh\nexit 1\n");
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(r.path("deny.sh"), std::fs::Permissions::from_mode(0o755))
        .expect("chmod");
    r.git(&["add", "deny.sh"]);
    r.commit("chore: base");
    let base = head(&r);
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  block  node tc.js\n\
         pre-commit  deny  *  block  ./deny.sh\n",
    );
    r.commit("chore: declare");
    trust_manifest(&r);
    install_hooks(&r);

    r.stage("src/a.ts", "export const x = 1\n");
    let attempt = r.git(&["commit", "-q", "-m", "feat: blocked attempt"]);
    assert!(
        !attempt.status.success(),
        "the deny check should have blocked the commit"
    );
    assert_eq!(typecheck_runs(&r), 1, "typecheck itself ran and passed");

    r.commit("feat: same tree, hooks dodged"); // --no-verify
    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_eq!(code, 0, "{out}");
    assert!(
        out.contains("no record of it"),
        "a blocked attempt's marker vouched for the retry:\n{out}"
    );
    assert_eq!(
        typecheck_runs(&r),
        2,
        "the push gate must re-run typecheck for the unstamped commit:\n{out}"
    );
}

/// `git commit -a` builds a TEMPORARY index and exports GIT_INDEX_FILE to
/// the hooks. gate_stamp::record's `git write-tree` claims (in a comment) to
/// inherit it and so bind the stamp to the tree that commit actually seals —
/// this is the claim, pinned.
#[test]
fn a_commit_dash_a_still_earns_its_stamp() {
    if missing("npm") {
        return;
    }
    let (r, base) = stamped_repo();
    r.stage("src/a.ts", "export const x = 1\n");
    verified_commit(&r, "feat: tracked first");
    r.write("src/a.ts", "export const x = 2\n"); // unstaged edit…
    let out = r.git(&["commit", "-q", "-am", "feat: via commit -a"]);
    assert!(
        out.status.success(),
        "commit -a failed: {}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );

    let (code, out) = push_range_out(&r, &base, &head(&r));
    assert_eq!(code, 0, "{out}");
    assert!(
        out.contains("gated at commit") && !out.contains("no record of it"),
        "a commit -a lost its stamp — write-tree read the wrong index:\n{out}"
    );
    assert_eq!(
        typecheck_runs(&r),
        2,
        "typecheck at each commit, never re-run at push:\n{out}"
    );
}

/// The marker lives in the worktree-PRIVATE gitdir and the stamps in the
/// shared notes ref — the code's claim about linked worktrees, pinned: a
/// commit made in a linked worktree must suppress the push gate exactly as a
/// main-checkout commit does.
#[test]
fn a_linked_worktree_commit_earns_a_stamp_the_push_sees() {
    if missing("npm") {
        return;
    }
    let (r, base) = stamped_repo();
    r.stage("src/a.ts", "export const x = 1\n");
    verified_commit(&r, "feat: main checkout");

    let wt = r
        .dir
        .parent()
        .expect("parent")
        .join(format!("stamp-wt-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&wt);
    let out = r.git(&[
        "worktree",
        "add",
        "-b",
        "feat/wt",
        wt.to_str().expect("utf8"),
    ]);
    assert!(out.status.success(), "worktree add failed");

    std::fs::create_dir_all(wt.join("src")).expect("mkdir");
    std::fs::write(wt.join("src/b.ts"), "export const y = 2\n").expect("write");
    let wt_git = |args: &[&str]| {
        let mut cmd = Command::new("git");
        cmd.arg("-C").arg(&wt).args(args);
        common::Repo::strip_git_env_impl(&mut cmd);
        cmd.output().expect("git in worktree")
    };
    assert!(wt_git(&["add", "src/b.ts"]).status.success());
    let out = wt_git(&["commit", "-q", "-m", "feat: from the worktree"]);
    assert!(
        out.status.success(),
        "worktree commit failed: {}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );

    // Push the worktree's branch, from the worktree, through the shared hooks.
    let tip = String::from_utf8_lossy(&wt_git(&["rev-parse", "HEAD"]).stdout)
        .trim()
        .to_string();
    let line = format!("refs/heads/feat/wt {tip} refs/heads/feat/wt {base}\n");
    let mut child = Command::new(env!("CARGO_BIN_EXE_amont"))
        .arg("--hooks-dir")
        .arg(r.path(".git/hooks"))
        .arg("pre-push-run-tests-js")
        .current_dir(&wt)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn");
    child
        .stdin
        .as_mut()
        .expect("stdin")
        .write_all(line.as_bytes())
        .expect("write");
    let out = child.wait_with_output().expect("wait");
    let text = format!(
        "{}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    let _ = r.git(&["worktree", "remove", "--force", wt.to_str().expect("utf8")]);
    assert_eq!(out.status.code(), Some(0), "{text}");
    assert!(
        text.contains("gated at commit") && !text.contains("no record of it"),
        "a worktree commit's stamp was invisible to the push:\n{text}"
    );
}

// --- the bypass ledger -----------------------------------------------------
//
// The stamp's silent sibling: a commit a gate declaration covered, created
// without that gate having run, appends one line per dodged script to
// `.git/amont-bypasses`. These tests pin what counts and — just as load-
// bearing — what does not: the fixture's own setup commits, out-of-scope
// commits, and repositories that declare no gate must all leave NO file.

fn ledger(r: &Repo) -> Option<String> {
    std::fs::read_to_string(r.path(".git/amont-bypasses")).ok()
}

#[test]
fn a_stamped_repo_starts_with_an_empty_ledger() {
    let (r, _base) = stamped_repo();
    assert_eq!(
        ledger(&r),
        None,
        "the fixture's pre-install commits must not register"
    );
}

#[test]
fn a_no_verify_commit_a_gate_covers_is_recorded_as_unverified() {
    let (r, _base) = stamped_repo();
    r.stage("src/b.ts", "export const y = 2\n");
    r.commit("feat: dodge the gate"); // Repo::commit IS --no-verify
    let text = ledger(&r).expect("a bypassed gate leaves a ledger");
    assert!(text.starts_with("amont-bypass-v1"), "{text:?}");
    assert!(text.contains(" typecheck"), "{text:?}");
    assert_eq!(text.lines().count(), 2, "one header, one event: {text:?}");
}

#[test]
fn a_verified_commit_records_nothing() {
    if missing("node") {
        return;
    }
    let (r, _base) = stamped_repo();
    r.stage("src/b.ts", "export const y = 2\n");
    verified_commit(&r, "feat: through the gate");
    assert_eq!(ledger(&r), None, "a stamped commit is not a bypass");
}

#[test]
fn a_commit_outside_the_declarations_scope_records_nothing() {
    let (r, _base) = stamped_repo();
    r.stage("README.md", "docs only\n");
    r.commit("docs: no ts touched");
    assert_eq!(
        ledger(&r),
        None,
        "the gate never covered this commit, so nothing was dodged"
    );
}

#[test]
fn a_repo_that_declares_no_gate_records_nothing() {
    let r = Repo::new();
    r.stage("a.txt", "hello\n");
    r.commit("chore: base");
    install_hooks(&r);
    r.stage("b.txt", "more\n");
    r.commit("chore: more");
    assert_eq!(ledger(&r), None, "no declaration, no bypass to count");
}

#[test]
fn a_blocked_commit_retried_with_no_verify_is_recorded() {
    if missing("node") {
        return;
    }
    let r = Repo::new();
    r.stage("tc.js", "process.exit(1)\n");
    r.commit("chore: base");
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  block  node tc.js\n",
    );
    r.commit("chore: gate at commit time");
    trust_manifest(&r);
    install_hooks(&r);
    r.stage("src/a.ts", "export const x = 1\n");
    let attempt = r.git(&["commit", "-q", "-m", "feat: blocked"]);
    assert!(!attempt.status.success(), "the gate must block the attempt");
    r.commit("feat: bypassed after the block");
    let text = ledger(&r).expect("the retry dodged a gate that said no");
    assert!(text.contains(" typecheck"), "{text:?}");
}

#[test]
fn a_partially_stamped_commit_records_only_the_missing_script() {
    if missing("node") {
        return;
    }
    let (r, _base) = stamped_repo();
    // A second gate whose program does not exist: it reports Unavailable at
    // commit time (warn, never blocks), earns no stamp, and is exactly the
    // case the discard-site design missed — the commit lands stamped for
    // typecheck and unverified for test.
    r.stage(
        "amont.conf",
        "pre-commit  typecheck  *.ts,*.tsx  block  node tc.js\n\
         pre-commit  test       *.ts,*.tsx  block  amont-no-such-tool-xyz\n",
    );
    r.stage("src/b.ts", "export const y = 2\n");
    trust_manifest(&r);
    verified_commit(&r, "feat: half a gate");
    let text = ledger(&r).expect("the unavailable half goes unverified");
    assert!(text.contains(" test"), "{text:?}");
    assert!(
        !text.contains(" typecheck"),
        "the stamped half must not be counted: {text:?}"
    );
}

#[test]
fn amont_list_json_reports_the_bypass_ledger() {
    let (r, _base) = stamped_repo();
    r.stage("src/b.ts", "export const y = 2\n");
    r.commit("feat: dodge the gate");
    let out = r.run(&["list", "--json"]);
    assert_eq!(out.code, 0, "{}", out.output());
    assert!(
        out.stdout.contains("\"bypasses\":{\"total\":1"),
        "{}",
        out.stdout
    );
    assert!(
        out.stdout.contains("\"script\":\"typecheck\",\"count\":1"),
        "{}",
        out.stdout
    );
}

#[test]
fn amont_list_text_names_the_dodged_script() {
    let (r, _base) = stamped_repo();
    r.stage("src/b.ts", "export const y = 2\n");
    r.commit("feat: dodge the gate");
    let out = r.run(&["list"]);
    assert!(out.stdout.contains("unverified commits"), "{}", out.stdout);
    assert!(out.stdout.contains("typecheck"), "{}", out.stdout);
}

#[test]
fn amont_uninstall_forgets_the_bypass_ledger() {
    let (r, _base) = stamped_repo();
    r.stage("src/b.ts", "export const y = 2\n");
    r.commit("feat: dodge the gate");
    assert!(ledger(&r).is_some());
    let out = r.run(&["uninstall"]);
    assert_eq!(out.code, 0, "{}", out.output());
    assert_eq!(ledger(&r), None, "the ledger is OUR bookkeeping — gone");
}

#[test]
fn amont_record_bypasses_false_records_nothing() {
    let (r, _base) = stamped_repo();
    r.git(&["config", "amont.recordBypasses", "false"]);
    r.stage("src/b.ts", "export const y = 2\n");
    r.commit("feat: dodge the gate, off the record");
    assert_eq!(ledger(&r), None, "the documented switch must be honoured");
}