phora 0.1.2

A git-based artifact package manager and multiplexer for content-addressed file distribution
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
//! The sole producer of user-facing CLI output (`println!`/`format!`).

use std::fmt::Write;

use crate::config::ParsedSource;
use crate::deploy::ArtifactState;
use crate::error::{Error, Result};
use crate::sync::{HookOutcome, HookScope, HookStatus, SyncState};

use super::query::{
    CheckMatchReport, ExplainBody, ExplainReport, OfferAttribution, PreviewPlan, SourceResolution,
    SourceRow, SourceSummary, TakeAttribution, TargetDetail, TargetListing, TargetRow, WhereFilter,
    WhereMatch,
};

#[must_use]
pub(super) fn render_hook_report(outcomes: &[HookOutcome]) -> String {
    let mut out = String::new();
    for outcome in outcomes {
        let scope = match outcome.scope {
            HookScope::PreSync => "pre_sync",
            HookScope::PreDeploy => "pre_deploy",
            HookScope::OnChange => "on_change",
            HookScope::PostSync => "post_sync",
        };
        let status = match outcome.status {
            HookStatus::Success => "ok",
            HookStatus::Failure => "failed",
        };
        let _ = writeln!(
            out,
            "hook {} [{scope}] `{}` {status}",
            outcome.hook_id, outcome.command
        );
    }
    out
}

pub(super) fn print_listings(listings: &[TargetListing]) {
    print!("{}", format_listings(listings));
}

#[must_use]
pub(super) fn format_listings(listings: &[TargetListing]) -> String {
    let mut out = String::new();
    if listings.is_empty() {
        let _ = writeln!(out, "No targets configured.");
        return out;
    }
    for listing in listings {
        let _ = writeln!(out, "{}:", listing.target);
        if listing.artifacts.is_empty() {
            let _ = writeln!(out, "  (nothing deployed — run `phora sync`)");
            continue;
        }
        for artifact in &listing.artifacts {
            let _ = writeln!(
                out,
                "  {}/{}  {}",
                artifact.source, artifact.artifact, artifact.state
            );
        }
    }
    out
}

pub(super) fn print_verify(report: &crate::sync::VerifyReport) {
    use crate::sync::VerifyReason;
    if report.is_clean() {
        println!("all verified");
        return;
    }
    for m in &report.mismatches {
        let reason = match &m.reason {
            VerifyReason::Missing => "missing".to_owned(),
            VerifyReason::ContentMismatch { .. } => "content mismatch".to_owned(),
        };
        println!(
            "{}/{}: {} ({reason})",
            m.key.source,
            m.key.artifact,
            m.path.display()
        );
    }
    for hook in &report.untrusted_hooks {
        println!(
            "{}: untrusted stripped hook `{}` — deployed but not post-processed, artifact may be \
             incomplete; run `phora trust {}` to approve",
            hook.source, hook.hook_id, hook.source
        );
    }
}

pub(super) fn print_where_matches(matches: &[WhereMatch], filter: &WhereFilter) {
    print!("{}", format_where_matches(matches, filter));
}

#[must_use]
pub(super) fn format_where_matches(matches: &[WhereMatch], filter: &WhereFilter) -> String {
    let mut out = String::new();
    if matches.is_empty() {
        match where_filter_description(filter) {
            Some(desc) => {
                let _ = writeln!(out, "No deployed artifacts match {desc}.");
            }
            None => {
                let _ = writeln!(out, "No deployed artifacts yet.");
            }
        }
        let _ = writeln!(
            out,
            "Run `phora sync` to deploy, or `phora preview` to see the plan."
        );
        return out;
    }
    for m in matches {
        let commit = m.commit.get(..8).unwrap_or(&m.commit);
        let _ = writeln!(
            out,
            "Artifact: {}/{} (commit {commit}, digest {})",
            m.source, m.artifact, m.digest
        );
        for target in &m.targets {
            let _ = writeln!(out, "  - {target}");
        }
    }
    out
}

/// A human phrase for the active `where` filter constraints, or `None` when unfiltered.
fn where_filter_description(filter: &WhereFilter) -> Option<String> {
    let parts: Vec<String> = [
        ("source", filter.source.as_deref()),
        ("artifact", filter.artifact.as_deref()),
        ("commit", filter.commit.as_deref()),
        ("digest", filter.digest.as_deref()),
    ]
    .into_iter()
    .filter_map(|(label, value)| value.map(|v| format!("{label} `{v}`")))
    .collect();
    (!parts.is_empty()).then(|| parts.join(", "))
}

pub(super) fn print_check_match(source: &ParsedSource, path: &str, report: &CheckMatchReport) {
    let artifact = path.split('/').next().unwrap_or(path);
    println!(
        "artifact `{artifact}`: {}",
        allow_label(report.artifact_allowed)
    );
    println!("path `{path}`: {}", allow_label(report.path_allowed));
    println!("include: {:?}", source.includes());
    println!("exclude: {:?}", source.excludes());
}

fn allow_label(allowed: bool) -> &'static str {
    if allowed { "allowed" } else { "excluded" }
}

#[must_use]
pub(crate) fn render_explain(report: &ExplainReport) -> String {
    let mut out = String::new();
    let _ = writeln!(out, "{} under {}", report.source, report.target);
    match &report.body {
        ExplainBody::Path { path, offer, take } => {
            render_offer(&mut out, path, offer);
            if let Some(take) = take {
                render_take(&mut out, take);
            }
        }
        ExplainBody::Summary { leaves } => {
            if leaves.is_empty() {
                let _ = writeln!(out, "  offer is empty");
            }
            for leaf in leaves {
                let _ = write!(out, "  {}: ", leaf.leaf);
                render_take_inline(&mut out, &leaf.take);
            }
        }
    }
    for warning in &report.warnings {
        let _ = writeln!(out, "  warning: {warning}");
    }
    out
}

fn render_offer(out: &mut String, path: &str, offer: &OfferAttribution) {
    match offer {
        OfferAttribution::Allowed { include: Some(inc) } => {
            let _ = writeln!(out, "  offer: `{path}` allowed by include `{inc}`");
        }
        OfferAttribution::Allowed { include: None } => {
            let _ = writeln!(out, "  offer: `{path}` allowed by the implicit-full offer");
        }
        OfferAttribution::Vetoed { exclude } => {
            let _ = writeln!(out, "  offer: `{path}` vetoed by exclude `{exclude}`");
        }
        OfferAttribution::Outside { suggestions } => {
            let _ = writeln!(out, "  offer: `{path}` is outside the offer");
            if !suggestions.is_empty() {
                let _ = writeln!(out, "  did you mean: {}", suggestions.join(", "));
            }
        }
    }
}

fn render_take(out: &mut String, take: &TakeAttribution) {
    let _ = write!(out, "  take: ");
    render_take_inline(out, take);
}

fn render_take_inline(out: &mut String, take: &TakeAttribution) {
    match take {
        TakeAttribution::Identity { dest } => {
            let _ = writeln!(out, "kept at `{dest}`");
        }
        TakeAttribution::Renamed { src, dest } => {
            let _ = writeln!(out, "renamed `{src}` -> `{dest}`");
        }
        TakeAttribution::Collapsed { dir } => {
            let _ = writeln!(out, "collapsed into directory `{dir}`");
        }
        TakeAttribution::Dropped => {
            let _ = writeln!(out, "dropped by a narrowing take (not taken)");
        }
    }
}

pub(super) fn print_source_rows(rows: &[SourceRow]) {
    for row in rows {
        println!("{}  {}  {}", row.name, row.remote, row.refspec);
    }
}

pub(super) fn print_source_summary(summary: &SourceSummary) {
    println!("{}", summary.name);
    println!("  remote: {}", summary.remote);
    println!("  refspec: {}", summary.refspec);
    if summary.targets.is_empty() {
        println!("  deployed to: (none)");
    } else {
        println!("  deployed to: {}", summary.targets.join(", "));
    }
}

pub(super) fn print_target_rows(rows: &[TargetRow]) {
    for row in rows {
        let SourceResolution::Explicit(names) = &row.resolution;
        let sources = names.join(", ");
        println!("{}  {}  [{sources}]", row.name, row.path);
    }
}

pub(super) fn print_target_detail(detail: &TargetDetail) {
    println!("{}", detail.name);
    println!("  path: {}", detail.path);
    println!("  sources: {}", detail.bound_sources.join(", "));
    for artifact in &detail.artifacts {
        println!(
            "  {}/{}  {}",
            artifact.source, artifact.artifact, artifact.state
        );
    }
}

pub(super) fn print_source_removed(name: &str) {
    println!("Removed source '{name}'");
}

pub(super) fn print_target_added(name: &str, path: &str) {
    println!("Added target '{name}': {path}");
}

pub(super) fn print_target_removed(name: &str) {
    println!("Removed target '{name}'");
}

pub(super) fn print_added_to_default(name: &str, description: &str) {
    println!("Added source '{name}': {description}");
    println!("  bound to default");
}

pub(super) fn print_added_declared(name: &str, description: &str) {
    println!("Added source '{name}': {description}");
    println!("  declared only; bind it with `phora bind {name} --to <target>`");
}

pub(super) fn print_added_and_bound(name: &str, description: &str, targets: &[String]) {
    println!("Added source '{name}': {description}");
    println!("  bound to {}", targets.join(", "));
}

pub(super) fn print_bound(sources: &[String], target: &str) {
    println!("Bound {} to '{target}'", sources.join(", "));
}

pub(super) fn print_bind_unchanged(sources: &[String], target: &str) {
    println!(
        "Bindings in '{target}' already up to date: {}",
        sources.join(", ")
    );
}

pub(super) fn print_unbound(sources: &[String], target: &str) {
    println!("Unbound {} from '{target}'", sources.join(", "));
}

pub(super) fn warn_target_rm_deployed(name: &str) {
    eprintln!(
        "phora: target `{name}` still has deployed artifacts registered; \
         run `phora sync --prune` to remove them"
    );
}

pub(super) fn warn_unbind_tombstone(target: &str) {
    eprintln!("phora: {}", super::bind::unbind_tombstone_warning(target));
}

/// Renders the preview plan as an indented per-target tree for terminal output.
#[must_use]
pub(crate) fn render_preview_tree(plan: &PreviewPlan) -> String {
    let mut out = String::new();
    for tp in &plan.targets {
        let _ = writeln!(out, "{}", tp.target);
        for entry in &tp.entries {
            match entry.state {
                SyncState::Synced => render_synced_entry(&mut out, entry),
                SyncState::NotLocked => {
                    let _ = writeln!(out, "  {} — not locked", entry.identity);
                }
                SyncState::NeedsSync => {
                    let _ = writeln!(out, "  {} — needs sync", entry.identity);
                }
                SyncState::LinkWorkingTreeGone => {
                    let _ = writeln!(out, "  {} — link working tree gone", entry.identity);
                }
            }
        }
        for collision in &tp.collisions {
            let _ = writeln!(
                out,
                "  collision: {} from {}",
                collision.artifact,
                collision.sources.join(", ")
            );
        }
        for group in &tp.warnings {
            render_binding_warnings(&mut out, group);
        }
    }
    out
}

fn render_synced_entry(out: &mut String, entry: &crate::sync::PreviewEntry) {
    let line = if entry.commit == "link" {
        format!("{}@link", entry.identity)
    } else {
        let short = entry.commit.get(..8).unwrap_or(&entry.commit);
        format!("{}@{short}", entry.identity)
    };
    let artifact = if let Some(src) = &entry.rename {
        format!("{src} -> {}", entry.artifact)
    } else if entry.collapsed {
        format!("{}/", entry.artifact)
    } else {
        entry.artifact.clone()
    };
    let _ = writeln!(
        out,
        "  {line} {artifact} -> {}",
        entry.destination.display()
    );
    for file in &entry.files {
        let suffix = if file.templated { " (templated)" } else { "" };
        let _ = writeln!(out, "    {}{suffix}", file.path.display());
    }
}

fn render_binding_warnings(out: &mut String, group: &crate::sync::BindingWarnings) {
    use crate::diagnostic::DID_YOU_MEAN;
    use crate::sync::PreviewWarning;
    for warning in &group.warnings {
        match warning {
            PreviewWarning::TakeNoMatch {
                pattern,
                suggestions,
            } => {
                let _ = writeln!(
                    out,
                    "  warning: {} take `{pattern}` matched no offered leaf",
                    group.identity
                );
                if !suggestions.is_empty() {
                    let _ = writeln!(out, "    {DID_YOU_MEAN} {}", suggestions.join(", "));
                }
            }
            PreviewWarning::CollapseBlocked { dir } => {
                let _ = writeln!(
                    out,
                    "  warning: {} `{dir}` could not collapse: a within-dir exclude forced \
                     per-leaf links",
                    group.identity
                );
            }
        }
    }
}

/// Returns the preview plan as pretty-printed JSON.
///
/// # Errors
/// Errors if serialization fails.
pub(crate) fn render_preview_json(plan: &PreviewPlan) -> Result<String> {
    serde_json::to_string_pretty(&PreviewPlanJson {
        targets: &plan.targets,
    })
    .map_err(|e| Error::Sync(format!("serialize preview json: {e}")))
}

#[derive(serde::Serialize)]
struct PreviewPlanJson<'a> {
    targets: &'a [crate::sync::PreviewTargetPlan],
}

pub(super) fn state_label(state: &ArtifactState) -> &'static str {
    match state {
        ArtifactState::Clean | ArtifactState::Revalidated { .. } => "✓ clean",
        ArtifactState::Outdated => "outdated",
        ArtifactState::Modified { .. } => "modified",
        ArtifactState::Foreign => "foreign",
        ArtifactState::Missing => "missing",
        ArtifactState::Ejected => "ejected",
        ArtifactState::Linked => "linked",
    }
}

/// Summarizes what a dep's root `phora.toml` would contribute if imported: its
/// targets (with relative paths), any stripped/inert hooks, and the `imports` +
/// `phora trust` opt-in. Empty for a manifest declaring neither targets nor sources.
#[must_use]
pub(super) fn render_add_contribution(
    name: &str,
    manifest: &crate::config::transitive::TransitiveManifest,
) -> String {
    if manifest.targets.is_empty() && manifest.sources.is_empty() {
        return String::new();
    }

    let hooked: std::collections::BTreeSet<&str> = manifest
        .hooks()
        .and_then(toml::Value::as_table)
        .map(|t| t.keys().map(String::as_str).collect())
        .unwrap_or_default();

    let mut out = String::new();
    let _ = writeln!(
        out,
        "Source '{name}' ships a phora.toml that would contribute:"
    );
    for (target, config) in &manifest.targets {
        let _ = writeln!(out, "  [targets.{target}] -> {}", config.path.display());
        if hooked.contains(target.as_str()) {
            let _ = writeln!(
                out,
                "    note: carries a hook, stripped and inert until you `phora trust` it"
            );
        }
    }
    let _ = writeln!(
        out,
        "Opt in with `imports = [\"{name}\"]`, then `phora trust` to admit any hooks."
    );
    out
}

#[cfg(test)]
mod preview_render_tests {
    use std::path::PathBuf;

    use super::super::query::PreviewPlan;
    use super::{render_preview_json, render_preview_tree};
    use crate::sync::{
        BindingWarnings, PreviewEntry, PreviewFile, PreviewTargetPlan, PreviewWarning, SyncState,
    };

    fn synced(identity: &str, artifact: &str, dest: &str) -> PreviewEntry {
        PreviewEntry {
            identity: identity.to_string(),
            source: identity.to_string(),
            artifact: artifact.to_string(),
            commit: "c0ffeeba".to_string(),
            destination: PathBuf::from(dest),
            state: SyncState::Synced,
            files: Vec::new(),
            rename: None,
            collapsed: false,
        }
    }

    fn plan(target: PreviewTargetPlan) -> PreviewPlan {
        PreviewPlan {
            targets: vec![target],
        }
    }

    #[test]
    fn renamed_leaf_renders_src_to_dest_legibly() {
        let mut entry = synced("dots", "renamed.md", "/dst/renamed.md");
        entry.rename = Some("x.md".to_string());
        let tp = PreviewTargetPlan {
            target: "home".to_string(),
            entries: vec![entry],
            collisions: Vec::new(),
            warnings: Vec::new(),
            untrusted_stripped_hook: false,
        };
        let rendered = render_preview_tree(&plan(tp));
        assert!(
            rendered.contains("x.md -> renamed.md"),
            "a renamed leaf must render `src -> dest` so the rename is legible; got:\n{rendered}"
        );
    }

    #[test]
    fn collapsed_dir_is_marked_as_a_collapsed_artifact() {
        let mut entry = synced("dots", "d", "/dst/d");
        entry.collapsed = true;
        entry.files = vec![PreviewFile {
            path: PathBuf::from("a.md"),
            templated: false,
        }];
        let tp = PreviewTargetPlan {
            target: "home".to_string(),
            entries: vec![entry],
            collisions: Vec::new(),
            warnings: Vec::new(),
            untrusted_stripped_hook: false,
        };
        let rendered = render_preview_tree(&plan(tp));
        assert!(
            rendered.contains("d/"),
            "a collapsed dir must be legible as a directory artifact (`d/`); got:\n{rendered}"
        );
        assert!(
            rendered.contains("a.md"),
            "the `--files` enrichment must still list the collapsed dir's children; got:\n{rendered}"
        );
    }

    #[test]
    fn take_no_match_warning_renders_with_a_did_you_mean_suggestion() {
        let tp = PreviewTargetPlan {
            target: "home".to_string(),
            entries: Vec::new(),
            collisions: Vec::new(),
            warnings: vec![BindingWarnings {
                identity: "dots".to_string(),
                source: "dots".to_string(),
                warnings: vec![PreviewWarning::TakeNoMatch {
                    pattern: "init.lus".to_string(),
                    suggestions: vec!["init.lua".to_string()],
                }],
            }],
            untrusted_stripped_hook: false,
        };
        let rendered = render_preview_tree(&plan(tp));
        assert!(
            rendered.contains("warning") && rendered.contains("init.lus"),
            "a no-match-glob take must render a warning naming the unmatched pattern; got:\n{rendered}"
        );
        assert!(
            rendered.contains(crate::diagnostic::DID_YOU_MEAN) && rendered.contains("init.lua"),
            "the warning must render a `did you mean:` suggestion naming the nearest leaf; got:\n{rendered}"
        );
    }

    #[test]
    fn collapse_blocked_warning_renders_per_binding() {
        let tp = PreviewTargetPlan {
            target: "home".to_string(),
            entries: Vec::new(),
            collisions: Vec::new(),
            warnings: vec![BindingWarnings {
                identity: "dots".to_string(),
                source: "dots".to_string(),
                warnings: vec![PreviewWarning::CollapseBlocked {
                    dir: "d".to_string(),
                }],
            }],
            untrusted_stripped_hook: false,
        };
        let rendered = render_preview_tree(&plan(tp));
        assert!(
            rendered.contains("warning") && rendered.contains("`d`"),
            "a blocked collapse must render a clear per-binding warning naming the dir; got:\n{rendered}"
        );
    }

    #[test]
    fn json_carries_warnings_in_a_structured_form() {
        let tp = PreviewTargetPlan {
            target: "home".to_string(),
            entries: Vec::new(),
            collisions: Vec::new(),
            warnings: vec![BindingWarnings {
                identity: "dots".to_string(),
                source: "dots".to_string(),
                warnings: vec![PreviewWarning::TakeNoMatch {
                    pattern: "init.lus".to_string(),
                    suggestions: vec!["init.lua".to_string()],
                }],
            }],
            untrusted_stripped_hook: false,
        };
        let json = render_preview_json(&plan(tp)).expect("preview json serializes");
        let value: serde_json::Value = serde_json::from_str(&json).expect("json parses");
        let warning = &value["targets"][0]["warnings"][0];
        assert_eq!(
            warning["identity"], "dots",
            "the warning group must carry its binding identity in JSON; got:\n{json}"
        );
        let take = &warning["warnings"][0]["TakeNoMatch"];
        assert_eq!(
            take["pattern"], "init.lus",
            "the structured JSON must carry the unmatched pattern, not pre-rendered prose; got:\n{json}"
        );
        assert_eq!(
            take["suggestions"][0], "init.lua",
            "the structured JSON must carry the suggestions array; got:\n{json}"
        );
    }
}

#[cfg(test)]
mod explain_render_tests {
    use super::super::query::{ExplainBody, ExplainReport, OfferAttribution, TakeAttribution};
    use super::render_explain;

    fn report(body: ExplainBody, warnings: Vec<String>) -> ExplainReport {
        ExplainReport {
            target: "home".to_string(),
            source: "dots".to_string(),
            body,
            warnings,
        }
    }

    #[test]
    fn allowed_path_names_target_source_include_and_identity_take() {
        let rendered = render_explain(&report(
            ExplainBody::Path {
                path: "init.lua".to_string(),
                offer: OfferAttribution::Allowed {
                    include: Some("*.lua".to_string()),
                },
                take: Some(TakeAttribution::Identity {
                    dest: "init.lua".to_string(),
                }),
            },
            Vec::new(),
        ));
        for needle in ["dots", "home", "init.lua", "*.lua", "allowed", "kept"] {
            assert!(
                rendered.contains(needle),
                "the rendering must surface `{needle}`; got:\n{rendered}"
            );
        }
    }

    #[test]
    fn renamed_take_shows_src_to_dest_arrow() {
        let rendered = render_explain(&report(
            ExplainBody::Path {
                path: "x.md".to_string(),
                offer: OfferAttribution::Allowed { include: None },
                take: Some(TakeAttribution::Renamed {
                    src: "x.md".to_string(),
                    dest: "renamed.md".to_string(),
                }),
            },
            Vec::new(),
        ));
        assert!(
            rendered.contains("x.md") && rendered.contains("renamed.md") && rendered.contains("->"),
            "a rename must render `src -> dest`; got:\n{rendered}"
        );
    }

    #[test]
    fn outside_path_surfaces_did_you_mean_suggestion() {
        let rendered = render_explain(&report(
            ExplainBody::Path {
                path: "init.lus".to_string(),
                offer: OfferAttribution::Outside {
                    suggestions: vec!["init.lua".to_string()],
                },
                take: None,
            },
            Vec::new(),
        ));
        assert!(
            rendered.contains("outside") && rendered.contains("init.lua"),
            "an outside path must be reported with its nearest suggestion; got:\n{rendered}"
        );
    }

    #[test]
    fn warnings_are_surfaced() {
        let rendered = render_explain(&report(
            ExplainBody::Summary { leaves: Vec::new() },
            vec!["take glob `none/**` matched no offered leaf".to_string()],
        ));
        assert!(
            rendered.contains("warning") && rendered.contains("none/**"),
            "a carried plan warning must render; got:\n{rendered}"
        );
    }
}

#[cfg(test)]
mod contribution_tests {
    use crate::config::transitive::TransitiveManifest;

    fn manifest(text: &str) -> TransitiveManifest {
        TransitiveManifest::parse(text).expect("dep manifest parses")
    }

    #[test]
    fn contribution_summary_lists_targets_with_relative_paths() {
        let dep = manifest(
            "version = 1\n\n\
             [sources.nvim]\ngit = \"https://github.com/dep/nvim.git\"\n\n\
             [targets.editor]\npath = \"config/nvim\"\n\n\
             [targets.shell]\npath = \"config/zsh\"\n",
        );

        let summary = super::render_add_contribution("dots", &dep);

        assert!(
            summary.contains("editor") && summary.contains("config/nvim"),
            "the summary must list target `editor` with its relative path `config/nvim`, got:\n{summary}"
        );
        assert!(
            summary.contains("shell") && summary.contains("config/zsh"),
            "the summary must list target `shell` with its relative path `config/zsh`, got:\n{summary}"
        );
    }

    #[test]
    fn contribution_summary_strips_hooks_and_notes_they_are_inert() {
        let dep = manifest(
            "version = 1\n\n\
             [sources.nvim]\ngit = \"https://github.com/dep/nvim.git\"\n\n\
             [targets.editor]\npath = \"config/nvim\"\n\n\
             [targets.editor.hooks]\non_change = \"./install.sh\"\n",
        );

        let summary = super::render_add_contribution("dots", &dep);

        assert!(
            summary.to_lowercase().contains("hook"),
            "a dep declaring a per-target hook must be surfaced as carrying a stripped hook, got:\n{summary}"
        );
        assert!(
            summary.to_lowercase().contains("inert")
                || summary.to_lowercase().contains("trust")
                || summary.to_lowercase().contains("stripped"),
            "the summary must note the hook is stripped/inert until trusted, got:\n{summary}"
        );
        assert!(
            !summary.contains("./install.sh"),
            "the contribution summary must NOT echo the hook command verbatim (it stays opaque/inert), got:\n{summary}"
        );
    }

    #[test]
    fn contribution_summary_suggests_the_imports_and_trust_opt_in() {
        let dep = manifest(
            "version = 1\n\n\
             [sources.nvim]\ngit = \"https://github.com/dep/nvim.git\"\n\n\
             [targets.editor]\npath = \"config/nvim\"\n",
        );

        let summary = super::render_add_contribution("dots", &dep);

        assert!(
            summary.contains("imports"),
            "the summary must point at the `imports = [...]` opt-in path, got:\n{summary}"
        );
        assert!(
            summary.contains("trust"),
            "the summary must point at `phora trust` as the explicit opt-in, got:\n{summary}"
        );
    }

    #[test]
    fn contribution_summary_is_empty_for_a_manifest_with_no_targets_or_sources() {
        let dep = manifest("version = 1\n");

        let summary = super::render_add_contribution("dots", &dep);

        assert!(
            !summary.contains("imports") && !summary.to_lowercase().contains("trust"),
            "a non-transitive dep (no targets/sources) must NOT produce a contribution/opt-in summary, got:\n{summary}"
        );
    }
}