alef 0.79.4

Opinionated polyglot binding generator for Rust libraries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
//! Coverage for the generated-manifest / committed-lock disagreement check.
//!
//! Every fixture reproduces the reported shape structurally: an alef-generated Rust e2e crate
//! that is its own workspace root and reaches its real registry requirements through a *path*
//! dependency it does not own. That indirection is the whole point — it is why the pre-existing
//! `relock_lockfiles_beside_changed_manifests` hook (keyed on "did alef rewrite this manifest")
//! cannot see the breakage, and why nothing observed lock freshness at all before this module.
//!
//! No cargo invocation anywhere: the fixtures are plain files and the check is pure.

use super::*;

const E2E_RELATIVE_DIR: &str = "e2e/rust";

/// Package name of the crate under test, and of the lock entry the fixtures move.
const REGISTRY_DEPENDENCY: &str = "sample-json";

/// The lock pins this; the manifests below require one minor above it in the stale fixtures.
const STALE_PIN: &str = "1.25.0";
const FRESH_PIN: &str = "1.26.0";
const REQUIREMENT: &str = "1.26";

/// Root workspace crate that the generated e2e crate depends on by path.
fn write_root_manifest(root: &Path, dependencies: &str) {
    std::fs::write(
        root.join("Cargo.toml"),
        format!("[package]\nname = \"sample-core\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n{dependencies}"),
    )
    .expect("write root Cargo.toml");
}

/// The alef-generated e2e crate: its own workspace root, depending on the crate under test by
/// path exactly as `crate::e2e::codegen::rust::cargo_toml` emits it.
fn write_generated_e2e_manifest(root: &Path) -> PathBuf {
    let dir = root.join(E2E_RELATIVE_DIR);
    std::fs::create_dir_all(&dir).expect("create e2e dir");
    let manifest = dir.join("Cargo.toml");
    std::fs::write(
        &manifest,
        "[workspace]\n\n[package]\nname = \"sample-core-e2e-rust\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n\
         [dependencies]\nsample_core = { package = \"sample-core\", path = \"../..\" }\n",
    )
    .expect("write generated e2e Cargo.toml");
    manifest
}

/// A committed lock beside the generated manifest, pinning the registry dependency at `pin`.
fn write_lock(root: &Path, pin: &str) {
    std::fs::write(
        root.join(E2E_RELATIVE_DIR).join("Cargo.lock"),
        format!(
            "version = 4\n\n\
             [[package]]\nname = \"sample-core-e2e-rust\"\nversion = \"0.1.0\"\n\n\
             [[package]]\nname = \"sample-core\"\nversion = \"0.1.0\"\n\n\
             [[package]]\nname = \"{REGISTRY_DEPENDENCY}\"\nversion = \"{pin}\"\n"
        ),
    )
    .expect("write Cargo.lock");
}

fn e2e_dir(root: &Path) -> PathBuf {
    root.join(E2E_RELATIVE_DIR)
}

/// The regression: the generated manifest is byte-identical to what alef would emit, nothing
/// alef owns changed, and the lock still cannot satisfy a requirement the path dependency
/// declares. `cargo metadata --locked` fails here; before this module alef exited 0.
#[test]
fn stale_lock_findings_reports_a_requirement_no_locked_version_satisfies() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    write_root_manifest(
        root,
        &format!("[dependencies]\n{REGISTRY_DEPENDENCY} = \"{REQUIREMENT}\"\n"),
    );
    write_generated_e2e_manifest(root);
    write_lock(root, STALE_PIN);

    let findings = stale_lock_findings(&e2e_dir(root));

    assert_eq!(findings.len(), 1, "expected exactly one finding, got: {findings:?}");
    let finding = &findings[0];
    assert_eq!(finding.dependency, REGISTRY_DEPENDENCY);
    assert_eq!(finding.requirement, REQUIREMENT);
    assert_eq!(finding.locked_versions, vec![STALE_PIN.to_string()]);
    assert_eq!(finding.lock, e2e_dir(root).join("Cargo.lock"));
    assert_eq!(
        finding.declared_in,
        root.join("Cargo.toml"),
        "the requirement is declared in the path dependency, not in the manifest alef generated"
    );
}

/// The control that stops "always fail" from satisfying this suite: the identical fixture with a
/// lock that does satisfy the requirement must produce nothing at all.
#[test]
fn stale_lock_findings_accepts_a_lock_that_satisfies_every_requirement() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    write_root_manifest(
        root,
        &format!("[dependencies]\n{REGISTRY_DEPENDENCY} = \"{REQUIREMENT}\"\n"),
    );
    write_generated_e2e_manifest(root);
    write_lock(root, FRESH_PIN);

    let findings = stale_lock_findings(&e2e_dir(root));

    assert!(
        findings.is_empty(),
        "a lock that resolves must be reported clean: {findings:?}"
    );
}

/// The one-sided rule: a requirement whose package is not in the lock at all is never reported.
/// Absence is ambiguous (trimmed dev-dependencies, `[patch]`, platform gating) and reporting it
/// would turn healthy trees red; only a contradiction cargo itself would reject is a finding.
#[test]
fn stale_lock_findings_ignores_a_dependency_absent_from_the_lock() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    write_root_manifest(root, "[dependencies]\nsample-absent = \"2\"\n");
    write_generated_e2e_manifest(root);
    write_lock(root, STALE_PIN);

    let findings = stale_lock_findings(&e2e_dir(root));

    assert!(
        findings.is_empty(),
        "a package missing from the lock is not evidence of staleness: {findings:?}"
    );
}

/// The common real-world spelling: the path dependency inherits its requirement from the
/// workspace root it is itself the root of. Resolving inheritance is what keeps this check from
/// being blind on most consumer repos.
#[test]
fn stale_lock_findings_resolves_a_workspace_inherited_requirement() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    write_root_manifest(
        root,
        &format!(
            "[workspace]\nmembers = []\n\n[workspace.dependencies]\n{REGISTRY_DEPENDENCY} = \"{REQUIREMENT}\"\n\n\
             [dependencies]\n{REGISTRY_DEPENDENCY} = {{ workspace = true }}\n"
        ),
    );
    write_generated_e2e_manifest(root);
    write_lock(root, STALE_PIN);

    let findings = stale_lock_findings(&e2e_dir(root));

    assert_eq!(
        findings.len(),
        1,
        "expected the inherited requirement, got: {findings:?}"
    );
    assert_eq!(findings[0].dependency, REGISTRY_DEPENDENCY);
    assert_eq!(findings[0].requirement, REQUIREMENT);
}

/// A git dependency is locked by revision; the `version` field beside it is not a registry
/// requirement the lock's pinned version has to satisfy.
#[test]
fn stale_lock_findings_ignores_a_git_dependency() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    write_root_manifest(
        root,
        &format!(
            "[dependencies]\n{REGISTRY_DEPENDENCY} = {{ git = \"https://example.invalid/sample.git\", version = \
             \"{REQUIREMENT}\" }}\n"
        ),
    );
    write_generated_e2e_manifest(root);
    write_lock(root, STALE_PIN);

    let findings = stale_lock_findings(&e2e_dir(root));

    assert!(
        findings.is_empty(),
        "a git dependency carries no registry pin: {findings:?}"
    );
}

/// Alef never authors a lockfile. A generated crate without one is a consumer choice, not a
/// defect, and must not fail the run.
#[test]
fn stale_lock_findings_skips_a_directory_with_no_committed_lock() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    write_root_manifest(
        root,
        &format!("[dependencies]\n{REGISTRY_DEPENDENCY} = \"{REQUIREMENT}\"\n"),
    );
    write_generated_e2e_manifest(root);

    let findings = stale_lock_findings(&e2e_dir(root));

    assert!(
        findings.is_empty(),
        "a directory with no lock has nothing to check: {findings:?}"
    );
}

/// The run-level entry point: it must select manifests out of the generated path set, and the
/// error it returns must name the dependency, the lock, and the command that fixes it.
#[test]
fn check_generated_lock_freshness_names_the_dependency_and_the_remedy() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    write_root_manifest(
        root,
        &format!("[dependencies]\n{REGISTRY_DEPENDENCY} = \"{REQUIREMENT}\"\n"),
    );
    let manifest = write_generated_e2e_manifest(root);
    write_lock(root, STALE_PIN);
    let generated: HashSet<PathBuf> = [manifest, root.join(E2E_RELATIVE_DIR).join("tests/basic_test.rs")]
        .into_iter()
        .collect();

    let error = check_generated_lock_freshness(&generated).expect("a stale lock must fail the run");
    let message = format!("{error:#}");

    assert!(
        message.contains(REGISTRY_DEPENDENCY),
        "message must name the dependency: {message}"
    );
    assert!(
        message.contains(STALE_PIN),
        "message must name the stale pin: {message}"
    );
    assert!(
        message.contains(REQUIREMENT),
        "message must name the requirement: {message}"
    );
    assert!(
        message.contains("cargo update"),
        "message must name the remedy: {message}"
    );
    assert!(
        message.contains(&e2e_dir(root).join("Cargo.lock").display().to_string()),
        "message must name the lock: {message}"
    );
}

/// Control for the entry point, matching the `stale_lock_findings` control above: a resolvable
/// lock must return `None` so the run keeps its zero exit.
#[test]
fn check_generated_lock_freshness_passes_a_resolvable_lock() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    write_root_manifest(
        root,
        &format!("[dependencies]\n{REGISTRY_DEPENDENCY} = \"{REQUIREMENT}\"\n"),
    );
    let manifest = write_generated_e2e_manifest(root);
    write_lock(root, FRESH_PIN);
    let generated: HashSet<PathBuf> = [manifest].into_iter().collect();

    assert!(
        check_generated_lock_freshness(&generated).is_none(),
        "a resolvable lock must not fail the run"
    );
}

/// A generated path set containing no Rust manifest at all must not walk anything.
#[test]
fn check_generated_lock_freshness_ignores_non_manifest_paths() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    write_root_manifest(
        root,
        &format!("[dependencies]\n{REGISTRY_DEPENDENCY} = \"{REQUIREMENT}\"\n"),
    );
    write_generated_e2e_manifest(root);
    write_lock(root, STALE_PIN);
    let generated: HashSet<PathBuf> = [root.join(E2E_RELATIVE_DIR).join("tests/basic_test.rs")]
        .into_iter()
        .collect();

    assert!(check_generated_lock_freshness(&generated).is_none());
}

/// Coverage for [`check_generated_node_lock_freshness`] / [`stale_node_lock_findings`], the pnpm
/// sibling of the checks above. Unlike the Rust fixtures, there is no path-dependency indirection
/// to reproduce: the specifiers being compared live in the one `package.json` alef generated, so
/// the fixtures below only need that file and a `pnpm-lock.yaml` beside it.
mod node {
    use super::*;

    const NODE_DIR_RELATIVE: &str = "e2e/typescript";
    const NODE_DEPENDENCY: &str = "sample-pkg";
    const NODE_STALE_SPEC: &str = "1.3.0";
    const NODE_FRESH_SPEC: &str = "1.2.3";

    fn node_dir(root: &Path) -> PathBuf {
        root.join(NODE_DIR_RELATIVE)
    }

    /// The alef-generated e2e `package.json`, matching the shape
    /// `crate::e2e::codegen::typescript::config::render_package_json` emits: the dependency under
    /// test sits in `devDependencies`.
    fn write_package_json(root: &Path, specifier: &str) -> PathBuf {
        let dir = node_dir(root);
        std::fs::create_dir_all(&dir).expect("create node dir");
        let manifest = dir.join("package.json");
        std::fs::write(
            &manifest,
            format!(
                "{{\n  \"name\": \"sample-pkg-e2e-typescript\",\n  \"version\": \"0.1.0\",\n  \"private\": \
                 true,\n  \"devDependencies\": {{\n    \"{NODE_DEPENDENCY}\": \"{specifier}\"\n  }}\n}}\n"
            ),
        )
        .expect("write package.json");
        manifest
    }

    /// `lockfileVersion` 9's workspace-aware shape: dependency tables nest under `importers.".".*`.
    fn write_pnpm_lock_v9(root: &Path, locked_specifier: &str) {
        std::fs::write(
            node_dir(root).join("pnpm-lock.yaml"),
            format!(
                "lockfileVersion: '9.0'\n\nimporters:\n  .:\n    devDependencies:\n      \
                 {NODE_DEPENDENCY}:\n        specifier: {locked_specifier}\n        version: \
                 {locked_specifier}\n"
            ),
        )
        .expect("write pnpm-lock.yaml");
    }

    /// `lockfileVersion` 6's flat, non-workspace shape: dependency tables sit at the document root
    /// with no `importers` wrapper at all -- the fallback `locked_node_specifiers` must also read.
    fn write_pnpm_lock_v6(root: &Path, locked_specifier: &str) {
        std::fs::write(
            node_dir(root).join("pnpm-lock.yaml"),
            format!(
                "lockfileVersion: '6.0'\n\ndevDependencies:\n  {NODE_DEPENDENCY}:\n    specifier: \
                 {locked_specifier}\n    version: {locked_specifier}\n"
            ),
        )
        .expect("write pnpm-lock.yaml");
    }

    /// The regression: `package.json` was regenerated with a specifier the committed
    /// `pnpm-lock.yaml` does not record, exactly the shape that fails `pnpm install` under the
    /// default frozen lockfile in CI. Before this module alef reported nothing and exited 0.
    #[test]
    fn stale_node_lock_findings_reports_a_specifier_mismatch() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_package_json(root, NODE_STALE_SPEC);
        write_pnpm_lock_v9(root, NODE_FRESH_SPEC);

        let findings = stale_node_lock_findings(&node_dir(root));

        assert_eq!(findings.len(), 1, "expected exactly one finding, got: {findings:?}");
        let finding = &findings[0];
        assert_eq!(finding.dependency, NODE_DEPENDENCY);
        assert_eq!(finding.bucket, "devDependencies");
        assert_eq!(finding.requirement, NODE_STALE_SPEC);
        assert_eq!(finding.locked_requirement, NODE_FRESH_SPEC);
        assert_eq!(finding.lock, node_dir(root).join("pnpm-lock.yaml"));
        assert_eq!(finding.declared_in, node_dir(root).join("package.json"));
    }

    /// The control that stops "always fail" from satisfying this suite: the identical fixture
    /// with a lock that already records the same specifier must produce nothing at all. This is
    /// the one that would NOT fail if `stale_node_lock_findings` were reverted to always compare
    /// unconditionally correctly -- it instead catches a reversion that made the comparison
    /// unconditionally report (e.g. dropping the equality check).
    #[test]
    fn stale_node_lock_findings_accepts_a_lock_that_matches() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_package_json(root, NODE_FRESH_SPEC);
        write_pnpm_lock_v9(root, NODE_FRESH_SPEC);

        let findings = stale_node_lock_findings(&node_dir(root));

        assert!(
            findings.is_empty(),
            "a lock matching package.json must be reported clean: {findings:?}"
        );
    }

    /// `lockfileVersion` 6's flat shape (no `importers` wrapper) must be read too, not only 9's --
    /// this is the one that would fail if the `importers.".".*` fallback in
    /// `locked_node_specifiers` were the only shape read.
    #[test]
    fn stale_node_lock_findings_reports_a_mismatch_in_the_flat_lockfile_v6_shape() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_package_json(root, NODE_STALE_SPEC);
        write_pnpm_lock_v6(root, NODE_FRESH_SPEC);

        let findings = stale_node_lock_findings(&node_dir(root));

        assert_eq!(
            findings.len(),
            1,
            "expected the flat lockfileVersion 6 shape to be read too, got: {findings:?}"
        );
        assert_eq!(findings[0].locked_requirement, NODE_FRESH_SPEC);
    }

    /// The one-sided rule, matching the cargo check's absence rule: a dependency package.json
    /// declares but the lock's own bucket never mentions is not reported. The lock here is
    /// non-empty (it pins an unrelated package) so this exercises the per-name lookup missing,
    /// not merely an empty bucket short-circuiting earlier.
    #[test]
    fn stale_node_lock_findings_ignores_a_dependency_absent_from_the_lock() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_package_json(root, NODE_STALE_SPEC);
        let dir = node_dir(root);
        std::fs::write(
            dir.join("pnpm-lock.yaml"),
            "lockfileVersion: '9.0'\n\nimporters:\n  .:\n    devDependencies:\n      other-pkg:\n        \
             specifier: 2.0.0\n        version: 2.0.0\n",
        )
        .expect("write pnpm-lock.yaml");

        let findings = stale_node_lock_findings(&dir);

        assert!(
            findings.is_empty(),
            "a package missing from the lock's bucket is not evidence of drift: {findings:?}"
        );
    }

    /// `workspace:` specifiers are resolved through a workspace root this check never reads, so a
    /// text mismatch against the lock's recorded specifier must not be reported.
    #[test]
    fn stale_node_lock_findings_ignores_a_workspace_specifier() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_package_json(root, "workspace:*");
        write_pnpm_lock_v9(root, "workspace:^1.0.0");

        let findings = stale_node_lock_findings(&node_dir(root));

        assert!(
            findings.is_empty(),
            "workspace: specifiers are not directly comparable: {findings:?}"
        );
    }

    /// `file:` specifiers (the wasm e2e app's local dependency mode) are excluded for the same
    /// reason `fingerprint.rs` excludes `node_modules`/`vendor` from its own hash: a locally
    /// linked dependency's content, and potentially the text pnpm records for it, can move for
    /// reasons a text diff here cannot verify.
    #[test]
    fn stale_node_lock_findings_ignores_a_file_specifier() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_package_json(root, "file:../../..");
        write_pnpm_lock_v9(root, "file:../../../dist");

        let findings = stale_node_lock_findings(&node_dir(root));

        assert!(
            findings.is_empty(),
            "file: specifiers are not directly comparable: {findings:?}"
        );
    }

    /// The run-level entry point: it must select `package.json` out of the generated path set,
    /// and the error it returns must name the dependency, both specifiers, the lock, and the
    /// remedy.
    #[test]
    fn check_generated_node_lock_freshness_names_the_dependency_and_the_remedy() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        let manifest = write_package_json(root, NODE_STALE_SPEC);
        write_pnpm_lock_v9(root, NODE_FRESH_SPEC);
        let generated: HashSet<PathBuf> = [manifest].into_iter().collect();

        let error = check_generated_node_lock_freshness(&generated, root).expect("a stale lock must fail the run");
        let message = format!("{error:#}");

        assert!(
            message.contains(NODE_DEPENDENCY),
            "message must name the dependency: {message}"
        );
        assert!(
            message.contains(NODE_STALE_SPEC),
            "message must name the package.json specifier: {message}"
        );
        assert!(
            message.contains(NODE_FRESH_SPEC),
            "message must name the locked specifier: {message}"
        );
        assert!(
            message.contains("pnpm install"),
            "message must name the remedy: {message}"
        );
        assert!(
            message.contains(&node_dir(root).join("pnpm-lock.yaml").display().to_string()),
            "message must name the lock: {message}"
        );
    }

    /// Control for the entry point, matching the pattern above: a lock whose specifier already
    /// matches must return `None` so the run keeps its zero exit. This is the assertion that
    /// would catch a regression turning this check into an unconditional failure.
    #[test]
    fn check_generated_node_lock_freshness_passes_a_matching_lock() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        let manifest = write_package_json(root, NODE_FRESH_SPEC);
        write_pnpm_lock_v9(root, NODE_FRESH_SPEC);
        let generated: HashSet<PathBuf> = [manifest].into_iter().collect();

        assert!(
            check_generated_node_lock_freshness(&generated, root).is_none(),
            "a matching lock must not fail the run"
        );
    }

    /// A generated path set containing no `package.json` at all must not walk anything.
    #[test]
    fn check_generated_node_lock_freshness_ignores_non_manifest_paths() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_package_json(root, NODE_STALE_SPEC);
        write_pnpm_lock_v9(root, NODE_FRESH_SPEC);
        let generated: HashSet<PathBuf> = [node_dir(root).join("src/index.ts")].into_iter().collect();

        assert!(check_generated_node_lock_freshness(&generated, root).is_none());
    }
}

#[path = "lock_freshness_registered_manifest_tests.rs"]
mod registered_unmarkable_manifest_gap;

/// Coverage for [`check_generated_uv_lock_freshness`] / [`stale_uv_lock_findings`], the uv/Python
/// sibling of the checks above. Like the node fixtures there is no path-dependency indirection: the
/// specifiers being compared live in the one `pyproject.toml` alef generated, so the fixtures below
/// only need that file and a `uv.lock` beside it. `requires_dist_map`'s marker/extra filtering and
/// `parse_pep508_requirement`/`normalize_pep503_name` are exercised indirectly through every fixture
/// here rather than unit-tested in isolation, matching how `locked_node_specifiers` is covered above.
mod uv {
    use super::*;

    const UV_DIR_RELATIVE: &str = "e2e/python";
    const UV_DEPENDENCY: &str = "sample-pkg";
    const UV_STALE_SPEC: &str = ">=1.1.1";
    const UV_FRESH_SPEC: &str = ">=1.2.0";

    fn uv_dir(root: &Path) -> PathBuf {
        root.join(UV_DIR_RELATIVE)
    }

    /// The alef-generated e2e `pyproject.toml`, matching the shape
    /// `crate::e2e::codegen::python::config::render_pyproject` emits: a `[project.dependencies]`
    /// array holding a plain PEP 508 requirement string.
    fn write_pyproject(root: &Path, specifier: &str) -> PathBuf {
        let dir = uv_dir(root);
        std::fs::create_dir_all(&dir).expect("create uv dir");
        let manifest = dir.join("pyproject.toml");
        std::fs::write(
            &manifest,
            format!(
                "[project]\nname = \"sample-pkg-e2e\"\nversion = \"0.0.0\"\ndependencies = \
                 [\"{UV_DEPENDENCY}{specifier}\"]\n"
            ),
        )
        .expect("write pyproject.toml");
        manifest
    }

    /// The project-lock shape: the project's own `[[package]]` entry carries `[package.metadata]
    /// requires-dist`, keyed by name off `pyproject.toml`'s own `[project.name]`.
    fn write_uv_lock_project_shape(root: &Path, locked_specifier: &str) {
        std::fs::write(
            uv_dir(root).join("uv.lock"),
            format!(
                "version = 1\nrequires-python = \">=3.10\"\n\n\
                 [[package]]\nname = \"sample-pkg-e2e\"\nversion = \"0.0.0\"\nsource = {{ virtual = \".\" }}\n\
                 dependencies = [\n  {{ name = \"{UV_DEPENDENCY}\" }},\n]\n\n\
                 [package.metadata]\nrequires-dist = [{{ name = \"{UV_DEPENDENCY}\", specifier = \
                 \"{locked_specifier}\" }}]\n"
            ),
        )
        .expect("write uv.lock");
    }

    /// The standalone-script-lock shape: no per-package `[[package]]` entry for the project at all
    /// (there is no project -- a script lock has nothing to attach `[package.metadata]` to), only a
    /// top-level `[manifest] requirements` array carrying the same `{ name, specifier }` shape. This
    /// is the fallback `locked_uv_requirements` must also read.
    fn write_uv_lock_manifest_shape(root: &Path, locked_specifier: &str) {
        std::fs::write(
            uv_dir(root).join("uv.lock"),
            format!(
                "version = 1\nrequires-python = \">=3.10\"\n\n\
                 [manifest]\nrequirements = [{{ name = \"{UV_DEPENDENCY}\", specifier = \
                 \"{locked_specifier}\" }}]\n"
            ),
        )
        .expect("write uv.lock");
    }

    /// The regression: `pyproject.toml` was regenerated with a specifier the committed `uv.lock`
    /// does not record, exactly the shape that fails `uv sync --locked` under the default frozen
    /// lockfile in CI. Before this module alef reported nothing and exited 0.
    #[test]
    fn stale_uv_lock_findings_reports_a_specifier_mismatch() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_pyproject(root, UV_STALE_SPEC);
        write_uv_lock_project_shape(root, UV_FRESH_SPEC);

        let findings = stale_uv_lock_findings(&uv_dir(root));

        assert_eq!(findings.len(), 1, "expected exactly one finding, got: {findings:?}");
        let finding = &findings[0];
        assert_eq!(finding.dependency, UV_DEPENDENCY);
        assert_eq!(finding.requirement, UV_STALE_SPEC);
        assert_eq!(finding.locked_requirement, UV_FRESH_SPEC);
        assert_eq!(finding.lock, uv_dir(root).join("uv.lock"));
        assert_eq!(finding.declared_in, uv_dir(root).join("pyproject.toml"));
    }

    /// The control that stops "always fail" from satisfying this suite: the identical fixture with
    /// a lock that already records the same specifier must produce nothing at all. This is the one
    /// that would NOT fail if `stale_uv_lock_findings` were reverted to always compare unconditionally
    /// -- it instead catches a reversion that made the comparison unconditionally report.
    #[test]
    fn stale_uv_lock_findings_accepts_a_lock_that_matches() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_pyproject(root, UV_FRESH_SPEC);
        write_uv_lock_project_shape(root, UV_FRESH_SPEC);

        let findings = stale_uv_lock_findings(&uv_dir(root));

        assert!(
            findings.is_empty(),
            "a lock matching pyproject.toml must be reported clean: {findings:?}"
        );
    }

    /// The one-sided rule, matching the cargo and node checks' absence rule: a dependency
    /// `pyproject.toml` declares but the lock's recorded copy never mentions is not reported. The
    /// lock here is non-empty (it pins an unrelated package) so this exercises the per-name lookup
    /// missing, not merely an empty map short-circuiting earlier.
    #[test]
    fn stale_uv_lock_findings_ignores_a_dependency_absent_from_the_lock() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_pyproject(root, UV_STALE_SPEC);
        std::fs::write(
            uv_dir(root).join("uv.lock"),
            "version = 1\nrequires-python = \">=3.10\"\n\n\
             [[package]]\nname = \"sample-pkg-e2e\"\nversion = \"0.0.0\"\nsource = { virtual = \".\" }\n\n\
             [package.metadata]\nrequires-dist = [{ name = \"other-pkg\", specifier = \">=2.0\" }]\n",
        )
        .expect("write uv.lock");

        let findings = stale_uv_lock_findings(&uv_dir(root));

        assert!(
            findings.is_empty(),
            "a package missing from the lock's recorded copy is not evidence of drift: {findings:?}"
        );
    }

    /// The standalone-script-lock fallback shape must be read too, not only the project shape --
    /// this is the one that would fail if the `[manifest] requirements` fallback in
    /// `locked_uv_requirements` were never reached (e.g. dropped, or only tried when the project
    /// shape's own array is literally absent rather than merely mapping to nothing).
    #[test]
    fn stale_uv_lock_findings_reports_a_mismatch_in_the_manifest_requirements_fallback_shape() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_pyproject(root, UV_STALE_SPEC);
        write_uv_lock_manifest_shape(root, UV_FRESH_SPEC);

        let findings = stale_uv_lock_findings(&uv_dir(root));

        assert_eq!(
            findings.len(),
            1,
            "expected the manifest.requirements fallback shape to be read too, got: {findings:?}"
        );
        assert_eq!(findings[0].locked_requirement, UV_FRESH_SPEC);
    }

    /// A dependency declared with an environment marker is conditional on something this reader
    /// does not evaluate; comparing its specifier text against the lock's recorded copy would not
    /// be reliable evidence of drift, so it must not be reported even when the texts do differ.
    #[test]
    fn stale_uv_lock_findings_ignores_a_marker_conditional_dependency() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        let dir = uv_dir(root);
        std::fs::create_dir_all(&dir).expect("create uv dir");
        std::fs::write(
            dir.join("pyproject.toml"),
            format!(
                "[project]\nname = \"sample-pkg-e2e\"\nversion = \"0.0.0\"\ndependencies = \
                 [\"{UV_DEPENDENCY}{UV_STALE_SPEC}; python_version < '3.11'\"]\n"
            ),
        )
        .expect("write pyproject.toml");
        write_uv_lock_project_shape(root, UV_FRESH_SPEC);

        let findings = stale_uv_lock_findings(&uv_dir(root));

        assert!(
            findings.is_empty(),
            "a marker-conditional requirement is not directly comparable: {findings:?}"
        );
    }

    /// A name declared in `[tool.uv.sources]` has its resolution overridden (path, git, URL,
    /// workspace, or an alternate index) -- exactly `render_pyproject`'s own `Local` dependency
    /// mode, which writes the bare unconstrained name here and the real source in that table.
    /// Comparing it against the lock's registry-shaped specifier text would be a false positive.
    #[test]
    fn stale_uv_lock_findings_ignores_a_source_overridden_dependency() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        let dir = uv_dir(root);
        std::fs::create_dir_all(&dir).expect("create uv dir");
        std::fs::write(
            dir.join("pyproject.toml"),
            format!(
                "[project]\nname = \"sample-pkg-e2e\"\nversion = \"0.0.0\"\ndependencies = \
                 [\"{UV_DEPENDENCY}\"]\n\n[tool.uv]\nsources.{UV_DEPENDENCY} = {{ path = \"../..\" }}\n"
            ),
        )
        .expect("write pyproject.toml");
        write_uv_lock_project_shape(root, UV_FRESH_SPEC);

        let findings = stale_uv_lock_findings(&uv_dir(root));

        assert!(
            findings.is_empty(),
            "a [tool.uv.sources]-overridden dependency is not directly comparable: {findings:?}"
        );
    }

    /// The run-level entry point: it must select `pyproject.toml` out of the generated path set,
    /// and the error it returns must name the dependency, both specifiers, the lock, and the
    /// remedy.
    #[test]
    fn check_generated_uv_lock_freshness_names_the_dependency_and_the_remedy() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        let manifest = write_pyproject(root, UV_STALE_SPEC);
        write_uv_lock_project_shape(root, UV_FRESH_SPEC);
        let generated: HashSet<PathBuf> = [manifest].into_iter().collect();

        let error = check_generated_uv_lock_freshness(&generated).expect("a stale lock must fail the run");
        let message = format!("{error:#}");

        assert!(
            message.contains(UV_DEPENDENCY),
            "message must name the dependency: {message}"
        );
        assert!(
            message.contains(UV_STALE_SPEC),
            "message must name the pyproject.toml specifier: {message}"
        );
        assert!(
            message.contains(UV_FRESH_SPEC),
            "message must name the locked specifier: {message}"
        );
        assert!(message.contains("uv lock"), "message must name the remedy: {message}");
        assert!(
            message.contains(&uv_dir(root).join("uv.lock").display().to_string()),
            "message must name the lock: {message}"
        );
    }

    /// Control for the entry point, matching the pattern above: a lock whose specifier already
    /// matches must return `None` so the run keeps its zero exit. This is the assertion that would
    /// catch a regression turning this check into an unconditional failure.
    #[test]
    fn check_generated_uv_lock_freshness_passes_a_matching_lock() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        let manifest = write_pyproject(root, UV_FRESH_SPEC);
        write_uv_lock_project_shape(root, UV_FRESH_SPEC);
        let generated: HashSet<PathBuf> = [manifest].into_iter().collect();

        assert!(
            check_generated_uv_lock_freshness(&generated).is_none(),
            "a matching lock must not fail the run"
        );
    }

    /// A generated path set containing no `pyproject.toml` at all must not walk anything.
    #[test]
    fn check_generated_uv_lock_freshness_ignores_non_manifest_paths() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        write_pyproject(root, UV_STALE_SPEC);
        write_uv_lock_project_shape(root, UV_FRESH_SPEC);
        let generated: HashSet<PathBuf> = [uv_dir(root).join("tests/test_basic.py")].into_iter().collect();

        assert!(check_generated_uv_lock_freshness(&generated).is_none());
    }
}