rsconstruct 0.9.143

Rust based fast build system
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
use crate::common::{run_rsconstruct_with_env, setup_project_with_config, setup_test_project};
use serde_json::Value;

#[test]
fn tools_list_shows_all_registry_tools() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // `tool list` shows the central registry regardless of config, like
    // `processor list`. It lists tools no processor in this project needs.
    let output = run_rsconstruct_with_env(project_path, &["tool", "list"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "tool list failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        !stdout.is_empty(),
        "tool list should show at least one tool"
    );
    // The registry view is not processor-scoped, so it has no "(...)" column.
    assert!(
        !stdout.contains("("),
        "registry list should not show processor names in parentheses"
    );
    // It includes tools the minimal test project does not require.
    assert!(
        stdout.contains("clojure"),
        "registry list should include all known tools, e.g. clojure"
    );
}

#[test]
fn tools_list_shows_configured_tools() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path,
        &["tool", "list-configured"],
        &[("NO_COLOR", "1")],
    );
    assert!(
        output.status.success(),
        "tool list-configured failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    // Template processor requires python3, so list-configured always has output.
    assert!(
        !stdout.is_empty(),
        "tool list-configured should show at least one tool"
    );
    assert!(
        stdout.contains("("),
        "Expected processor name in parentheses for each tool"
    );
}

/// A tera stanza plus a ruff stanza with `enabled = false`. ruff is a
/// pip-installed registry tool, so it is exactly what a disabled stanza
/// must keep out of the lock, the install set and the tool listings.
const TERA_AND_DISABLED_RUFF: &str = "[processor.generator.tera]\nsrc_dirs = [\"tera.templates\"]\n\n[processor.checker.ruff]\nsrc_dirs = [\"src\"]\nenabled = false\n";

/// `list-configured` shows the tools of the enabled processors; `--all` is
/// the one way to see a disabled stanza's tool. Before the enabled filter
/// was real, both forms listed everything and `--all` was a no-op.
#[test]
fn tools_list_configured_hides_disabled_processors_unless_all() {
    let temp_dir = setup_project_with_config(TERA_AND_DISABLED_RUFF);
    let project_path = temp_dir.path();
    std::fs::create_dir_all(project_path.join("tera.templates")).unwrap();

    let output_default = run_rsconstruct_with_env(
        project_path,
        &["tool", "list-configured"],
        &[("NO_COLOR", "1")],
    );
    let output_all = run_rsconstruct_with_env(
        project_path,
        &["tool", "list-configured", "-a"],
        &[("NO_COLOR", "1")],
    );
    assert!(output_default.status.success());
    assert!(output_all.status.success());

    let stdout_default = String::from_utf8_lossy(&output_default.stdout);
    let stdout_all = String::from_utf8_lossy(&output_all.stdout);
    assert!(
        !stdout_default.contains("ruff"),
        "a disabled processor's tool was listed without --all: {stdout_default}"
    );
    assert!(
        stdout_all.contains("ruff") && stdout_all.contains("processor.checker.ruff"),
        "--all must list the disabled processor's tool: {stdout_all}"
    );

    // The JSON form follows the same rule.
    let json_default = run_rsconstruct_with_env(
        project_path,
        &["--json", "tool", "list-configured"],
        &[("NO_COLOR", "1")],
    );
    let entries: Vec<Value> = serde_json::from_slice(&json_default.stdout).unwrap();
    assert!(!entries.iter().any(|e| e["tool"] == "ruff"), "{entries:?}");
    let json_all = run_rsconstruct_with_env(
        project_path,
        &["--json", "tool", "list-configured", "--all"],
        &[("NO_COLOR", "1")],
    );
    let entries: Vec<Value> = serde_json::from_slice(&json_all.stdout).unwrap();
    assert!(entries.iter().any(|e| e["tool"] == "ruff"), "{entries:?}");
}

/// `tool lock` and `tool check` consider the enabled processors only: the
/// lock never gains an entry for a disabled stanza's tool, and the exact
/// check in CI does not ask for one. (With the filter broken, `tool lock`
/// would resolve ruff on PyPI and write a `[pip] ruff` entry.)
#[test]
fn tool_lock_and_check_skip_disabled_processors() {
    let temp_dir = setup_project_with_config(TERA_AND_DISABLED_RUFF);
    let project_path = temp_dir.path();
    std::fs::create_dir_all(project_path.join("tera.templates")).unwrap();

    let output = run_rsconstruct_with_env(project_path, &["tool", "lock"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "tool lock failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let lock = std::fs::read_to_string(project_path.join("rsconstruct.lock")).unwrap();
    assert!(
        !lock.contains("ruff"),
        "the lock gained an entry for a disabled processor's tool:\n{lock}"
    );

    let output = run_rsconstruct_with_env(
        project_path,
        &["tool", "check"],
        &[("NO_COLOR", "1"), ("CI", "true")],
    );
    assert!(
        output.status.success(),
        "tool check asked for a disabled processor's tool: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// The lock check a CI build runs sees what the missing-tool preflight
/// sees: the processors with products in the graph. A disabled stanza whose
/// tool is neither locked nor installed, a processor that matched no file,
/// and one excluded with `-x` must not fail the build; the same stanza,
/// enabled and with a file to check, must.
#[test]
fn ci_build_checks_the_lock_for_the_processors_that_build_only() {
    let temp_dir = setup_project_with_config(TERA_AND_DISABLED_RUFF);
    let project_path = temp_dir.path();
    std::fs::create_dir_all(project_path.join("tera.templates")).unwrap();
    std::fs::create_dir_all(project_path.join("src")).unwrap();
    std::fs::write(project_path.join("src/a.py"), "x = 1\n").unwrap();
    // An empty lock: ruff is not in it.
    let lock = "version = 1\n";
    std::fs::write(project_path.join("rsconstruct.lock"), lock).unwrap();
    let ci = &[("NO_COLOR", "1"), ("CI", "true")];

    let output = run_rsconstruct_with_env(project_path, &["build"], ci);
    assert!(
        output.status.success(),
        "CI build asked for a disabled processor's tool: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Control: enabled and with a .py file to check, ruff is needed, and
    // the build fails on it — on the preflight when ruff is not installed,
    // on the lock when it is (no `[pip] ruff` entry).
    let enabled = TERA_AND_DISABLED_RUFF.replace("enabled = false", "enabled = true");
    std::fs::write(project_path.join("rsconstruct.toml"), &enabled).unwrap();
    let output = run_rsconstruct_with_env(project_path, &["build"], ci);
    assert!(
        !output.status.success(),
        "an enabled processor with an unlocked tool passed the CI build"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("ruff"), "{stderr}");

    // Excluded with -x, ruff has no products in the graph and is not asked for.
    let output =
        run_rsconstruct_with_env(project_path, &["build", "-x", "processor.checker.ruff"], ci);
    assert!(
        output.status.success(),
        "CI build asked for an excluded processor's tool: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Matching no file, the enabled stanza needs no tool either.
    std::fs::remove_file(project_path.join("src/a.py")).unwrap();
    let output = run_rsconstruct_with_env(project_path, &["build"], ci);
    assert!(
        output.status.success(),
        "CI build asked for the tool of a processor with no products: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let after = std::fs::read_to_string(project_path.join("rsconstruct.lock")).unwrap();
    assert_eq!(after, lock, "CI must not rewrite the lock");
}

#[test]
fn tools_list_json() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path,
        &["--json", "tool", "list"],
        &[("NO_COLOR", "1")],
    );
    assert!(
        output.status.success(),
        "tool list --json failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let entries: Vec<serde_json::Value> =
        serde_json::from_str(&stdout).expect("Expected valid JSON array");

    // Check that every entry has the expected fields
    for entry in &entries {
        assert!(
            entry.get("tool").is_some(),
            "Entry should have 'tool' field"
        );
        assert!(
            entry.get("processor").is_some(),
            "Entry should have 'processor' field"
        );
        assert!(
            entry["processor"].is_array(),
            "'processor' should be an array"
        );
    }
}

#[test]
fn tools_check_succeeds() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // First create the lock file so check has something to verify against
    let lock_output =
        run_rsconstruct_with_env(project_path, &["tool", "lock"], &[("NO_COLOR", "1")]);
    assert!(
        lock_output.status.success(),
        "tool lock failed: {}",
        String::from_utf8_lossy(&lock_output.stderr)
    );

    // Now check should succeed since versions match the just-created lock file
    let output = run_rsconstruct_with_env(project_path, &["tool", "check"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "tool check failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// `tool lock` writes rsconstruct.lock (here with no entries: the tera
/// processor's tools are apt packages, which are not locked), and a second
/// run changes nothing. rsconstruct itself is not recorded.
#[test]
fn tool_lock_writes_rsconstruct_lock_once() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let first = run_rsconstruct_with_env(project_path, &["tool", "lock"], &[("NO_COLOR", "1")]);
    assert!(
        first.status.success(),
        "tool lock failed: {}",
        String::from_utf8_lossy(&first.stderr)
    );
    let stdout = String::from_utf8_lossy(&first.stdout);
    assert!(stdout.contains("Created rsconstruct.lock"), "{stdout}");
    let lock = std::fs::read_to_string(project_path.join("rsconstruct.lock")).unwrap();
    assert!(lock.contains("version = 1\n"), "{lock}");
    assert!(!lock.contains("[rsconstruct]"), "{lock}");

    let second = run_rsconstruct_with_env(project_path, &["tool", "lock"], &[("NO_COLOR", "1")]);
    assert!(second.status.success());
    let stdout = String::from_utf8_lossy(&second.stdout);
    assert!(stdout.contains("up to date"), "{stdout}");
    let again = std::fs::read_to_string(project_path.join("rsconstruct.lock")).unwrap();
    assert_eq!(lock, again);
}

/// In CI a build without rsconstruct.lock fails; with one it proceeds.
#[test]
fn ci_build_requires_the_lock() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path,
        &["build"],
        &[("NO_COLOR", "1"), ("CI", "true")],
    );
    assert!(
        !output.status.success(),
        "build without a lock passed in CI"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("No rsconstruct.lock found"), "{stderr}");

    let lock = run_rsconstruct_with_env(project_path, &["tool", "lock"], &[("NO_COLOR", "1")]);
    assert!(lock.status.success());
    let output = run_rsconstruct_with_env(
        project_path,
        &["build"],
        &[("NO_COLOR", "1"), ("CI", "true")],
    );
    assert!(
        output.status.success(),
        "CI build with a lock failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// A lock written before 2026-10-26 carries an `[rsconstruct]` table (the
/// release `tool lock` ran as, back when CI downloaded that release). It is
/// accepted and ignored: in CI such a lock passes untouched, whatever
/// version it names, and outside CI the next `tool lock` drops the table
/// and reports doing so.
#[test]
fn legacy_rsconstruct_table_is_accepted_and_dropped_on_rewrite() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();
    let lock = "version = 1\n\n[rsconstruct]\nversion = \"0.0.1\"\n";
    std::fs::write(project_path.join("rsconstruct.lock"), lock).unwrap();

    for args in [
        &["tool", "install-deps"][..],
        &["tool", "check"],
        &["build"],
    ] {
        let output =
            run_rsconstruct_with_env(project_path, args, &[("NO_COLOR", "1"), ("CI", "true")]);
        assert!(
            output.status.success(),
            "{args:?} failed in CI: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }
    let after = std::fs::read_to_string(project_path.join("rsconstruct.lock")).unwrap();
    assert_eq!(after, lock, "CI must not rewrite the lock");

    let output = run_rsconstruct_with_env(project_path, &["tool", "lock"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("- rsconstruct 0.0.1"), "{stdout}");
    assert!(stdout.contains("Updated rsconstruct.lock"), "{stdout}");
    let rewritten = std::fs::read_to_string(project_path.join("rsconstruct.lock")).unwrap();
    assert!(!rewritten.contains("[rsconstruct]"), "{rewritten}");
}

/// In CI a lock that does not match the project is an error, never
/// silently rewritten: here it pins a crate nothing needs.
#[test]
fn ci_rejects_a_lock_that_does_not_match() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();
    let stale = "version = 1\n\n[cargo]\ncargo-unused = \"1.0.0\"\n";
    std::fs::write(project_path.join("rsconstruct.lock"), stale).unwrap();

    let output = run_rsconstruct_with_env(
        project_path,
        &["tool", "install-deps"],
        &[("NO_COLOR", "1"), ("CI", "true")],
    );
    assert!(
        !output.status.success(),
        "install-deps accepted a stale lock in CI"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("cargo cargo-unused"), "{stderr}");
    let after = std::fs::read_to_string(project_path.join("rsconstruct.lock")).unwrap();
    assert_eq!(after, stale, "CI must not rewrite the lock");

    // Outside CI the same lock is repaired: the unused entry goes.
    let output = run_rsconstruct_with_env(project_path, &["tool", "lock"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let repaired = std::fs::read_to_string(project_path.join("rsconstruct.lock")).unwrap();
    assert!(!repaired.contains("cargo-unused"), "{repaired}");
}

/// `--upgrade <name>` takes only names the project locks. An unknown name
/// is an error that leaves the lock alone, and `rsconstruct` is one:
/// rsconstruct itself is not locked (CI downloads the latest release).
#[test]
fn tool_lock_upgrade_named_rejects_names_the_project_does_not_lock() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();
    let old = "version = 1\n";
    std::fs::write(project_path.join("rsconstruct.lock"), old).unwrap();

    for name in ["nosuch", "rsconstruct"] {
        let output = run_rsconstruct_with_env(
            project_path,
            &["tool", "lock", "--upgrade", name],
            &[("NO_COLOR", "1")],
        );
        assert!(!output.status.success(), "{name} was accepted");
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains(&format!("not a tool this project locks: {name}")),
            "{stderr}"
        );
        let unchanged = std::fs::read_to_string(project_path.join("rsconstruct.lock")).unwrap();
        assert_eq!(unchanged, old);
    }
}

/// A leftover .tools.versions is an error that says what to do.
#[test]
fn legacy_tools_versions_is_rejected() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();
    std::fs::write(project_path.join(".tools.versions"), "{}").unwrap();

    let output = run_rsconstruct_with_env(project_path, &["tool", "lock"], &[("NO_COLOR", "1")]);
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("Delete .tools.versions"), "{stderr}");
}

#[test]
fn tools_stats_shows_summary() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(project_path, &["tool", "stats"], &[("NO_COLOR", "1")]);
    assert!(
        output.status.success(),
        "tool stats failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Tool"), "Expected 'Tool' table header");
    assert!(
        stdout.contains("Runtime summary:"),
        "Expected 'Runtime summary:' section"
    );
    assert!(stdout.contains("Total:"), "Expected 'Total:' summary line");
    assert!(stdout.contains("installed"), "Expected 'installed' count");
}

#[test]
fn tools_stats_json() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path,
        &["--json", "tool", "stats"],
        &[("NO_COLOR", "1")],
    );
    assert!(
        output.status.success(),
        "tool stats --json failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: Value = serde_json::from_str(&stdout).expect("Expected valid JSON");

    // Verify top-level structure
    assert!(parsed.get("tools").is_some(), "Expected 'tools' field");
    assert!(
        parsed.get("runtimes").is_some(),
        "Expected 'runtimes' field"
    );
    assert!(parsed.get("summary").is_some(), "Expected 'summary' field");

    // Verify tools array entries
    let tools = parsed["tools"]
        .as_array()
        .expect("'tools' should be an array");
    assert!(!tools.is_empty(), "tools array should not be empty");
    for tool in tools {
        assert!(tool.get("name").is_some(), "Tool entry should have 'name'");
        assert!(
            tool.get("installed").is_some(),
            "Tool entry should have 'installed'"
        );
        assert!(
            tool.get("runtime").is_some(),
            "Tool entry should have 'runtime'"
        );
        assert!(
            tool.get("processor").is_some(),
            "Tool entry should have 'processors'"
        );
    }

    // Verify runtimes array entries
    let runtimes = parsed["runtimes"]
        .as_array()
        .expect("'runtimes' should be an array");
    for rt in runtimes {
        assert!(
            rt.get("runtime").is_some(),
            "Runtime entry should have 'runtime'"
        );
        assert!(
            rt.get("total").is_some(),
            "Runtime entry should have 'total'"
        );
        assert!(
            rt.get("installed").is_some(),
            "Runtime entry should have 'installed'"
        );
        assert!(
            rt.get("missing").is_some(),
            "Runtime entry should have 'missing'"
        );
    }

    // Verify summary
    let summary = &parsed["summary"];
    assert!(
        summary.get("total_tools").is_some(),
        "Summary should have 'total_tools'"
    );
    assert!(
        summary.get("installed").is_some(),
        "Summary should have 'installed'"
    );
    assert!(
        summary.get("missing").is_some(),
        "Summary should have 'missing'"
    );

    // Verify consistency: total_tools == tools.len()
    let total_tools = summary["total_tools"].as_u64().unwrap();
    assert_eq!(
        total_tools as usize,
        tools.len(),
        "summary.total_tools should match tools array length"
    );

    // Verify consistency: installed + missing == total_tools
    let installed = summary["installed"].as_u64().unwrap();
    let missing = summary["missing"].as_u64().unwrap();
    assert_eq!(
        installed + missing,
        total_tools,
        "installed + missing should equal total_tools"
    );
}

/// Every install method named in the registry must be one that `install`
/// actually implements. A method string with no arm in `tools::run`
/// (there used to be a bogus "system") is not a config error the user can
/// see — it surfaces only when someone tries to install that tool, as an
/// "unknown install method" failure at the worst possible moment.
#[test]
fn tools_list_uses_only_implemented_install_methods() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path,
        &["--json", "tool", "list"],
        &[("NO_COLOR", "1")],
    );
    assert!(
        output.status.success(),
        "tool list --json failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Kept in sync with the `match method` arms in tools::run().
    const IMPLEMENTED: &[&str] = &[
        "apt",
        "dnf",
        "pacman",
        "brew",
        "snap",
        "pip",
        "npm",
        "npm-ci",
        "npm-install",
        "cargo",
        "gem",
        "binary",
        "manual",
    ];

    let parsed: Value =
        serde_json::from_slice(&output.stdout).expect("tool list --json should emit valid JSON");
    let tools = parsed
        .as_array()
        .expect("tool list --json should be an array");
    assert!(!tools.is_empty(), "registry should not be empty");

    for tool in tools {
        let name = tool["tool"].as_str().unwrap_or("<unnamed>");
        let methods = tool["install_methods"]
            .as_array()
            .expect("tool should have install_methods");
        assert!(
            !methods.is_empty(),
            "tool '{name}' has no install method at all"
        );
        for m in methods {
            let method = m["method"]
                .as_str()
                .expect("install method should have a 'method' string");
            assert!(
                IMPLEMENTED.contains(&method),
                "tool '{name}' declares install method '{method}', which tools::run() does not implement",
            );
        }
    }
}

/// Registry names are detection keys handed straight to `which::which`, which
/// only searches `$PATH` for names with no path separator. A name containing
/// `/` is resolved relative to the current working directory instead, so it
/// reports `missing` unless rsconstruct happens to run from the one directory
/// with that subtree beneath it — including for users who have the tool
/// installed and on `$PATH`. The registry once carried `gems/bin/mdl` and
/// `node_modules/.bin/markdownlint` for exactly this bug. Vendored paths belong
/// in the per-processor `command` config field, not here.
#[test]
fn tools_list_names_are_bare_binaries_not_paths() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path,
        &["--json", "tool", "list"],
        &[("NO_COLOR", "1")],
    );
    assert!(
        output.status.success(),
        "tool list --json failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let parsed: Value =
        serde_json::from_slice(&output.stdout).expect("tool list --json should emit valid JSON");
    let tools = parsed
        .as_array()
        .expect("tool list --json should be an array");
    assert!(!tools.is_empty(), "registry should not be empty");

    for tool in tools {
        let name = tool["tool"]
            .as_str()
            .expect("tool should have a 'tool' name string");
        assert!(
            !name.contains('/'),
            "tool '{name}' is a path, not a bare binary name; which() would resolve it \
             relative to the cwd and report it missing from anywhere else. Use the bare \
             binary name here and put the vendored path in the processor's `command` config.",
        );
    }
}

/// `tool install --all` must be able to install every registry entry.
/// A manual-only entry makes `--all` a hard error, which would break CI
/// provisioning, so the registry must not contain one.
#[test]
fn tools_install_all_has_no_manual_only_entries() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path,
        &["--json", "tool", "list"],
        &[("NO_COLOR", "1")],
    );
    assert!(
        output.status.success(),
        "tool list --json failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let parsed: Value =
        serde_json::from_slice(&output.stdout).expect("tool list --json should emit valid JSON");
    let manual_only: Vec<&str> = parsed
        .as_array()
        .expect("array")
        .iter()
        .filter(|tool| {
            tool["install_methods"].as_array().is_some_and(|ms| {
                !ms.is_empty() && ms.iter().all(|m| m["method"].as_str() == Some("manual"))
            })
        })
        .map(|tool| tool["tool"].as_str().unwrap_or("<unnamed>"))
        .collect();

    assert!(
        manual_only.is_empty(),
        "these tools have only a manual install method, so `tool install --all` cannot provision them: {manual_only:?}",
    );
}

/// `--all` walks the registry instead of the config, so it must reject a
/// tool name rather than silently ignoring one of the two.
#[test]
fn tools_install_all_conflicts_with_tool_name() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path,
        &["tool", "install", "--all", "ruff"],
        &[("NO_COLOR", "1")],
    );
    assert!(
        !output.status.success(),
        "`tool install --all ruff` should be rejected"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cannot be used with"),
        "expected a clap conflict error, got: {stderr}",
    );
}