ask-please 0.4.8

Agent Skills Kit — download version-specific library docs for AI coding agents (Rust port of @pleaseai/ask).
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
//! `ask add <spec>` — add a library to `ask.json` and materialize its docs via
//! `run_install`. Rust port of `commands/add.ts` + the bare-`ask add`
//! interactive flow in `interactive.ts`.
//!
//! The real contract is the persisted `ask.json`, NOT the prompt UI. The
//! non-interactive path (`--docs-paths`, `--clear-docs-paths`, cold-cache bare
//! `add`) is what gets byte-parity tested against the TS oracle; the TTY
//! multiselect / text prompts are seamed out so unit tests never need a
//! terminal, and the production defaults use `dialoguer`.

use std::collections::HashSet;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};

use crate::ask_json::{entry_from_spec, AskJson};
use crate::commands::ensure_checkout::EnsureCheckoutDeps;
use crate::discovery::candidates::{
    gather_docs_candidates, CandidateGatheringError, CandidateGroup,
};
use crate::http::HttpClient;
use crate::install::{run_install, RunInstallOptions};
use crate::io::{read_ask_json, write_ask_json};
use crate::registry::api::fetch_registry_entry;
use crate::registry::detect_ecosystem;
use crate::spec::{parse_spec, ParsedSpec};

/// A single choice presented by a multiselect prompt. `value` is what the
/// selection resolves to (an absolute path, or a dependency name); `label` and
/// `hint` are display-only.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PromptOption {
    pub label: String,
    pub value: String,
    pub hint: String,
}

/// Normalize a POSIX path the way Node's `path.posix.normalize` does: collapse
/// `.`/`//`, resolve `..` (leaving leading `..` for relative escapes), and
/// return `.` for an empty result. Used by [`sanitize_docs_path`].
fn posix_normalize(input: &str) -> String {
    let mut stack: Vec<&str> = Vec::new();
    for seg in input.split('/') {
        match seg {
            "" | "." => {}
            ".." => {
                if matches!(stack.last(), Some(&top) if top != "..") {
                    stack.pop();
                } else {
                    stack.push("..");
                }
            }
            other => stack.push(other),
        }
    }
    if stack.is_empty() {
        ".".to_string()
    } else {
        stack.join("/")
    }
}

/// Validate and canonicalize a user-supplied docs path. Returns the
/// POSIX-normalized relative path on success, or `None` when the input is
/// unsafe / empty. Rejects empty/whitespace, absolute paths, and `..` escapes.
/// Parity with `sanitizeDocsPath`.
pub fn sanitize_docs_path(input: &str) -> Option<String> {
    let trimmed = input.trim();
    if trimmed.is_empty() {
        return None;
    }
    // `Path::is_absolute` matches `path.isAbsolute` on POSIX (leading `/`).
    if Path::new(trimmed).is_absolute() {
        return None;
    }
    let normalized = posix_normalize(&trimmed.replace('\\', "/"));
    if normalized.is_empty() {
        return None;
    }
    if normalized == ".." || normalized.starts_with("../") {
        return None;
    }
    Some(normalized)
}

const AMBIGUOUS_SPEC_HINT: &str = "Ambiguous spec '{}'. Use:\n\
     \u{20} • npm:<name>           (e.g. npm:next, npm:@mastra/client-js)\n\
     \u{20} • github:<owner>/<repo>@<ref>  (e.g. github:vercel/next.js@v14.2.3)";

/// True when `input` is an `owner/repo` pair (exactly one slash, both sides
/// non-empty). Equivalent to the `^[^/]+/[^/]+$` regex without a regex.
fn is_owner_repo(input: &str) -> bool {
    let parts: Vec<&str> = input.split('/').collect();
    parts.len() == 2 && !parts[0].is_empty() && !parts[1].is_empty()
}

/// `ask add <spec>` spec normalization: a bare `owner/repo` becomes
/// `github:owner/repo`; anything else without a `:` is ambiguous and errors.
/// Parity with `add.ts`'s `normalizeAddSpec`.
pub fn normalize_add_spec(input: &str) -> anyhow::Result<String> {
    if !input.contains(':') {
        if is_owner_repo(input) {
            return Ok(format!("github:{input}"));
        }
        return Err(anyhow::anyhow!(AMBIGUOUS_SPEC_HINT.replacen("{}", input, 1)));
    }
    Ok(input.to_string())
}

/// Interactive-mode spec normalization: assumes npm for bare names, treats
/// `@scope/pkg` as npm (not github), never errors. Parity with
/// `interactive.ts`'s `normalizeAddSpec`.
pub fn normalize_add_spec_interactive(input: &str) -> String {
    if !input.contains(':') {
        if input.starts_with('@') {
            return format!("npm:{input}");
        }
        if is_owner_repo(input) {
            return format!("github:{input}");
        }
        return format!("npm:{input}");
    }
    input.to_string()
}

/// Options for [`run_add`].
#[derive(Debug, Clone)]
pub struct RunAddOptions {
    pub project_dir: PathBuf,
    pub spec: String,
    /// Raw CSV from `--docs-paths`. When `Some`, skips the interactive prompt.
    pub docs_paths_arg: Option<String>,
    /// `--clear-docs-paths` — drop any existing `docsPaths` override.
    pub clear_docs_paths: bool,
}

/// What [`run_add`] produced: user-facing messages, collected so the CLI can
/// print them (routing is not parity-critical — the harness checks `ask.json`).
#[derive(Debug, Default, PartialEq, Eq)]
pub struct AddReport {
    pub stdout: Vec<String>,
    pub stderr: Vec<String>,
}

type GatherFn<'a> =
    dyn Fn(&str, &Path) -> Result<Vec<CandidateGroup>, CandidateGatheringError> + 'a;
type PromptFn<'a> = dyn Fn(&str, &[PromptOption]) -> Vec<String> + 'a;
type InstallerFn<'a> = dyn Fn(&Path, &[String]) -> anyhow::Result<()> + 'a;

/// Test/production seams for [`run_add`]. All `None` → real defaults.
#[derive(Default)]
pub struct RunAddDeps<'a> {
    pub gather: Option<&'a GatherFn<'a>>,
    pub prompt: Option<&'a PromptFn<'a>>,
    pub is_tty: Option<&'a (dyn Fn() -> bool + 'a)>,
    pub installer: Option<&'a InstallerFn<'a>>,
}

/// `ask add <spec>` implementation. Persists the entry (bare string or object
/// with a `docsPaths` override per the three policies below) then runs the
/// lazy-first installer for the single spec.
pub fn run_add(
    client: &dyn HttpClient,
    options: &RunAddOptions,
    deps: &RunAddDeps,
) -> anyhow::Result<AddReport> {
    let default_gather = |spec: &str, dir: &Path| {
        gather_docs_candidates(client, spec, dir, &EnsureCheckoutDeps::default())
    };
    let gather: &GatherFn = deps.gather.unwrap_or(&default_gather);

    let default_prompt: &PromptFn = &default_multiselect;
    let prompt: &PromptFn = deps.prompt.unwrap_or(default_prompt);

    let default_is_tty = || std::io::stdout().is_terminal();
    let is_tty: &dyn Fn() -> bool = deps.is_tty.unwrap_or(&default_is_tty);

    let default_installer = |dir: &Path, specs: &[String]| -> anyhow::Result<()> {
        run_install(
            dir,
            &RunInstallOptions {
                only_specs: Some(specs.to_vec()),
            },
        )?;
        Ok(())
    };
    let installer: &InstallerFn = deps.installer.unwrap_or(&default_installer);

    let spec = normalize_add_spec(&options.spec)?;

    if let ParsedSpec::Unknown { .. } = parse_spec(&spec) {
        anyhow::bail!("Invalid spec: {spec}");
    }

    let mut ask_json = read_ask_json(&options.project_dir)?.unwrap_or(AskJson {
        libraries: Vec::new(),
    });
    let existing_idx = ask_json.libraries.iter().position(|e| e.spec() == spec);

    let mut report = AddReport::default();
    let selected = resolve_selected_paths(&spec, options, gather, prompt, is_tty, &mut report);

    let entry = entry_from_spec(spec.clone(), selected.as_deref().unwrap_or(&[]));
    if let Some(idx) = existing_idx {
        ask_json.libraries[idx] = entry;
        report.stdout.push(format!("Updated {spec} in ask.json"));
    } else {
        ask_json.libraries.push(entry);
        report.stdout.push(format!("Added {spec} to ask.json"));
    }
    write_ask_json(&options.project_dir, &ask_json)?;

    installer(&options.project_dir, &[spec])?;
    Ok(report)
}

/// Strip `root` from `abs`, yielding the relative POSIX-ish tail (empty when
/// equal). Candidates are always descendants of their group root.
fn relative(root: &Path, abs: &Path) -> String {
    abs.strip_prefix(root)
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_else(|_| abs.to_string_lossy().into_owned())
}

fn basename(root: &Path) -> String {
    root.file_name()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_default()
}

/// Resolve which docs paths (if any) to persist. Returns `None` for the
/// canonical bare-string form; `Some(paths)` for an object override.
fn resolve_selected_paths(
    spec: &str,
    options: &RunAddOptions,
    gather: &GatherFn,
    prompt: &PromptFn,
    is_tty: &dyn Fn() -> bool,
    report: &mut AddReport,
) -> Option<Vec<String>> {
    // 1. `--clear-docs-paths` → force canonical string form (downgrade).
    if options.clear_docs_paths {
        return None;
    }

    // 2. `--docs-paths a,b,c` → store the supplied CSV, no prompt.
    if let Some(csv) = &options.docs_paths_arg {
        let mut parsed = Vec::new();
        for raw in csv.split(',') {
            match sanitize_docs_path(raw) {
                Some(safe) => parsed.push(safe),
                None => {
                    let trimmed = raw.trim();
                    if !trimmed.is_empty() {
                        report.stderr.push(format!(
                            "Ignoring unsafe docs-path entry {trimmed:?} (must be a relative path that stays inside its root)."
                        ));
                    }
                }
            }
        }
        return if parsed.is_empty() {
            None
        } else {
            Some(parsed)
        };
    }

    // 3. Probe candidates offline; prompt only on a TTY with a real choice.
    let groups = match gather(spec, &options.project_dir) {
        Ok(g) => g,
        Err(err) => {
            report.stderr.push(format!(
                "Could not probe docs candidates for {spec}: {}. Recording the spec without a docs-path override.",
                err.cause
            ));
            return None;
        }
    };

    let all_candidates: Vec<(&CandidateGroup, &PathBuf)> = groups
        .iter()
        .flat_map(|g| g.paths.iter().map(move |p| (g, p)))
        .collect();
    let all_are_root_only = groups
        .iter()
        .all(|g| g.paths.len() == 1 && g.paths[0] == g.root);

    if !is_tty() || all_candidates.len() <= 1 || all_are_root_only {
        return None;
    }

    let prompt_options: Vec<PromptOption> = all_candidates
        .iter()
        .map(|(g, abs)| {
            let rel = relative(&g.root, abs);
            PromptOption {
                label: if rel.is_empty() { ".".to_string() } else { rel },
                value: abs.to_string_lossy().into_owned(),
                hint: basename(&g.root),
            }
        })
        .collect();

    let picked = prompt(
        &format!("Select docs paths to keep for {spec} (space to toggle, enter to confirm):"),
        &prompt_options,
    );
    if picked.is_empty() {
        return None;
    }

    let mut selected = Vec::new();
    for abs in &picked {
        if let Some((g, abs_path)) = all_candidates
            .iter()
            .find(|(_, p)| p.to_string_lossy() == *abs)
        {
            // Root selection resolves to `.` so the min(1) override schema
            // still accepts it and the read side re-resolves to the root.
            let rel = relative(&g.root, abs_path);
            let rel = if rel.is_empty() { ".".to_string() } else { rel };
            if let Some(safe) = sanitize_docs_path(&rel) {
                selected.push(safe);
            }
        }
    }
    if selected.is_empty() {
        None
    } else {
        Some(selected)
    }
}

// ---------------------------------------------------------------------------
// Interactive flow (bare `ask add`) — TTY-only, not byte-parity tested.
// ---------------------------------------------------------------------------

/// Dependency names (from `package.json` deps + devDeps) not already declared
/// in `ask.json`, sorted. Parity with `readProjectDeps`.
pub fn read_project_deps(
    package_json: &serde_json::Value,
    existing_specs: &[String],
) -> Vec<String> {
    let mut names: Vec<String> = Vec::new();
    let mut seen: HashSet<String> = HashSet::new();
    for key in ["dependencies", "devDependencies"] {
        if let Some(obj) = package_json.get(key).and_then(|v| v.as_object()) {
            for k in obj.keys() {
                if seen.insert(k.clone()) {
                    names.push(k.clone());
                }
            }
        }
    }
    if names.is_empty() {
        return Vec::new();
    }
    let registered: HashSet<String> = existing_specs
        .iter()
        .filter_map(|s| match parse_spec(s) {
            ParsedSpec::Npm { pkg, .. } => Some(pkg),
            _ => None,
        })
        .collect();
    let mut out: Vec<String> = names
        .into_iter()
        .filter(|n| !registered.contains(n))
        .collect();
    out.sort();
    out
}

/// Which of `deps` have a registry entry. Parity with `checkRegistryBatch`
/// (sequential here — result set, not concurrency, is the contract).
#[derive(Debug, Default, PartialEq, Eq)]
pub struct RegistryCheckResult {
    pub registered: Vec<String>,
    pub unregistered: Vec<String>,
}

pub fn check_registry_batch(
    client: &dyn HttpClient,
    ecosystem: &str,
    deps: &[String],
) -> RegistryCheckResult {
    let mut result = RegistryCheckResult::default();
    for name in deps {
        if fetch_registry_entry(client, ecosystem, name).is_some() {
            result.registered.push(name.clone());
        } else {
            result.unregistered.push(name.clone());
        }
    }
    result
}

/// Report for the interactive flow, including an exit code (TTY-required guard
/// exits 1, mirroring `process.exit(1)`).
#[derive(Debug, Default, PartialEq, Eq)]
pub struct InteractiveReport {
    pub stdout: Vec<String>,
    pub stderr: Vec<String>,
    pub exit_code: i32,
}

type TextFn<'a> = dyn Fn(&str, &str) -> String + 'a;

/// Seams for [`run_interactive_add`].
#[derive(Default)]
pub struct RunInteractiveDeps<'a> {
    pub is_tty: Option<&'a (dyn Fn() -> bool + 'a)>,
    pub multiselect: Option<&'a PromptFn<'a>>,
    pub text: Option<&'a TextFn<'a>>,
    pub installer: Option<&'a InstallerFn<'a>>,
}

/// Interactive add — scans `package.json` deps, checks them against the ASK
/// registry, and lets the user multiselect + free-type specs to add.
pub fn run_interactive_add(
    client: &dyn HttpClient,
    project_dir: &Path,
    deps: &RunInteractiveDeps,
) -> anyhow::Result<InteractiveReport> {
    let default_is_tty = || std::io::stdout().is_terminal();
    let is_tty: &dyn Fn() -> bool = deps.is_tty.unwrap_or(&default_is_tty);
    let default_multi: &PromptFn = &default_multiselect;
    let multiselect: &PromptFn = deps.multiselect.unwrap_or(default_multi);
    let default_text_fn: &TextFn = &default_text;
    let text: &TextFn = deps.text.unwrap_or(default_text_fn);
    let default_installer = |dir: &Path, specs: &[String]| -> anyhow::Result<()> {
        run_install(
            dir,
            &RunInstallOptions {
                only_specs: Some(specs.to_vec()),
            },
        )?;
        Ok(())
    };
    let installer: &InstallerFn = deps.installer.unwrap_or(&default_installer);

    let mut report = InteractiveReport::default();

    if !is_tty() {
        report.stderr.push(
            "Interactive mode requires a TTY. Use `ask add <spec>` to add a library non-interactively."
                .to_string(),
        );
        report.exit_code = 1;
        return Ok(report);
    }

    let ecosystem = detect_ecosystem(project_dir);
    report
        .stdout
        .push(format!("Detected ecosystem: {ecosystem}"));

    let pkg_path = project_dir.join("package.json");
    if !pkg_path.exists() {
        report
            .stderr
            .push("No package.json found. Cannot scan dependencies.".to_string());
        report
            .stdout
            .push("Use `ask add <spec>` to add a library directly.".to_string());
        return Ok(report);
    }

    let package_json: serde_json::Value = match std::fs::read_to_string(&pkg_path)
        .ok()
        .and_then(|s| serde_json::from_str(&s).ok())
    {
        Some(v) => v,
        None => {
            report
                .stderr
                .push("Failed to parse package.json. Check for syntax errors.".to_string());
            return Ok(report);
        }
    };

    let mut ask_json = read_ask_json(project_dir)?.unwrap_or(AskJson {
        libraries: Vec::new(),
    });
    let existing_specs: Vec<String> = ask_json
        .libraries
        .iter()
        .map(|e| e.spec().to_string())
        .collect();
    let dep_names = read_project_deps(&package_json, &existing_specs);

    if dep_names.is_empty() {
        report
            .stdout
            .push("All project dependencies are already registered in ask.json.".to_string());
        return Ok(report);
    }

    report.stdout.push(format!(
        "Checking {} dependencies against ASK registry...",
        dep_names.len()
    ));
    let RegistryCheckResult {
        registered,
        unregistered,
    } = check_registry_batch(client, ecosystem, &dep_names);

    let mut choices: Vec<PromptOption> = Vec::new();
    for name in &registered {
        choices.push(PromptOption {
            label: name.clone(),
            value: name.clone(),
            hint: "registry".to_string(),
        });
    }
    for name in &unregistered {
        choices.push(PromptOption {
            label: name.clone(),
            value: name.clone(),
            hint: "not in registry".to_string(),
        });
    }

    if choices.is_empty() {
        report
            .stdout
            .push("No new dependencies to add.".to_string());
        return Ok(report);
    }

    report.stdout.push(format!(
        "Found {} registered, {} unregistered in ASK registry.",
        registered.len(),
        unregistered.len()
    ));

    let selected = multiselect(
        "Select libraries to add (space to toggle, enter to confirm):",
        &choices,
    );

    let placeholder = "npm:lodash, github:owner/repo@v1";
    let specs: Vec<String> = if selected.is_empty() {
        let manual = text(
            "Enter a spec manually (or press enter to skip):",
            placeholder,
        );
        if manual.trim().is_empty() {
            report.stdout.push("No libraries selected.".to_string());
            return Ok(report);
        }
        manual
            .split(',')
            .map(|s| normalize_add_spec_interactive(s.trim()))
            .filter(|s| !s.is_empty())
            .collect()
    } else {
        // Selected names always come from package.json → npm ecosystem.
        let mut specs: Vec<String> = selected.iter().map(|n| format!("npm:{n}")).collect();
        let manual = text(
            "Any additional specs to add manually? (press enter to skip):",
            placeholder,
        );
        if !manual.trim().is_empty() {
            specs.extend(
                manual
                    .split(',')
                    .map(|s| normalize_add_spec_interactive(s.trim()))
                    .filter(|s| !s.is_empty()),
            );
        }
        specs
    };

    add_specs(project_dir, &mut ask_json, &specs, installer, &mut report)?;
    Ok(report)
}

fn add_specs(
    project_dir: &Path,
    ask_json: &mut AskJson,
    specs: &[String],
    installer: &InstallerFn,
    report: &mut InteractiveReport,
) -> anyhow::Result<()> {
    let mut added: Vec<String> = Vec::new();
    for spec in specs {
        if let ParsedSpec::Unknown { .. } = parse_spec(spec) {
            report.stderr.push(format!("Skipping invalid spec: {spec}"));
            continue;
        }
        if ask_json.libraries.iter().any(|e| e.spec() == spec) {
            report.stdout.push(format!("{spec} already in ask.json"));
            continue;
        }
        ask_json.libraries.push(entry_from_spec(spec.clone(), &[]));
        added.push(spec.clone());
    }

    if added.is_empty() {
        report.stdout.push("No new libraries to add.".to_string());
        return Ok(());
    }

    write_ask_json(project_dir, ask_json)?;
    let plural = if added.len() == 1 { "y" } else { "ies" };
    report.stdout.push(format!(
        "Added {} librar{plural} to ask.json: {}",
        added.len(),
        added.join(", ")
    ));

    installer(project_dir, &added)?;
    Ok(())
}

/// Production multiselect: `dialoguer::MultiSelect`. On any I/O error (no TTY,
/// interrupted) returns an empty selection, matching the "nothing picked" path.
fn default_multiselect(msg: &str, opts: &[PromptOption]) -> Vec<String> {
    use dialoguer::MultiSelect;
    let items: Vec<String> = opts
        .iter()
        .map(|o| {
            if o.hint.is_empty() {
                o.label.clone()
            } else {
                format!("{}  ({})", o.label, o.hint)
            }
        })
        .collect();
    match MultiSelect::new().with_prompt(msg).items(&items).interact() {
        Ok(idxs) => idxs
            .into_iter()
            .filter_map(|i| opts.get(i).map(|o| o.value.clone()))
            .collect(),
        Err(_) => Vec::new(),
    }
}

/// Production text input: `dialoguer::Input`, empty allowed (enter to skip).
fn default_text(msg: &str, _placeholder: &str) -> String {
    use dialoguer::Input;
    Input::<String>::new()
        .with_prompt(msg)
        .allow_empty(true)
        .interact_text()
        .unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::http::mock::MockClient;

    #[test]
    fn posix_normalize_matches_node() {
        assert_eq!(posix_normalize("docs/./api"), "docs/api");
        assert_eq!(posix_normalize("docs//api"), "docs/api");
        assert_eq!(posix_normalize("a/b/.."), "a");
        assert_eq!(posix_normalize("a/../.."), "..");
        assert_eq!(posix_normalize("."), ".");
        assert_eq!(posix_normalize("./docs"), "docs");
    }

    #[test]
    fn sanitize_rejects_unsafe_accepts_relative() {
        assert_eq!(sanitize_docs_path("docs"), Some("docs".to_string()));
        assert_eq!(
            sanitize_docs_path("  docs/api "),
            Some("docs/api".to_string())
        );
        assert_eq!(
            sanitize_docs_path("docs\\api"),
            Some("docs/api".to_string())
        );
        assert_eq!(sanitize_docs_path("."), Some(".".to_string()));
        assert_eq!(sanitize_docs_path(""), None);
        assert_eq!(sanitize_docs_path("   "), None);
        assert_eq!(sanitize_docs_path("/abs"), None);
        assert_eq!(sanitize_docs_path(".."), None);
        assert_eq!(sanitize_docs_path("../escape"), None);
        assert_eq!(sanitize_docs_path("a/../../b"), None);
    }

    #[test]
    fn normalize_add_spec_owner_repo_and_ambiguous() {
        assert_eq!(
            normalize_add_spec("vercel/next.js").unwrap(),
            "github:vercel/next.js"
        );
        assert_eq!(normalize_add_spec("npm:next").unwrap(), "npm:next");
        assert!(normalize_add_spec("lodash").is_err());
    }

    #[test]
    fn normalize_interactive_assumes_npm() {
        assert_eq!(normalize_add_spec_interactive("lodash"), "npm:lodash");
        assert_eq!(
            normalize_add_spec_interactive("@mastra/client-js"),
            "npm:@mastra/client-js"
        );
        assert_eq!(
            normalize_add_spec_interactive("vercel/next.js"),
            "github:vercel/next.js"
        );
        assert_eq!(
            normalize_add_spec_interactive("github:o/r@v1"),
            "github:o/r@v1"
        );
    }

    #[test]
    fn read_project_deps_dedups_sorts_excludes_registered() {
        let pkg = serde_json::json!({
            "dependencies": { "react": "18", "zod": "3" },
            "devDependencies": { "vite": "5", "react": "18" }
        });
        let existing = vec!["npm:react".to_string()];
        assert_eq!(read_project_deps(&pkg, &existing), vec!["vite", "zod"]);
    }

    #[test]
    fn read_project_deps_empty() {
        let pkg = serde_json::json!({});
        assert!(read_project_deps(&pkg, &[]).is_empty());
    }

    fn write_ask_json_raw(dir: &Path, body: &str) {
        std::fs::write(dir.join("ask.json"), body).unwrap();
    }

    #[test]
    fn run_add_clear_docs_paths_writes_bare_string() {
        let proj = tempfile::tempdir().unwrap();
        write_ask_json_raw(
            proj.path(),
            r#"{"libraries":[{"spec":"npm:react","docsPaths":["docs"]}]}"#,
        );
        let no_install = |_: &Path, _: &[String]| -> anyhow::Result<()> { Ok(()) };
        let deps = RunAddDeps {
            installer: Some(&no_install),
            ..Default::default()
        };
        let opts = RunAddOptions {
            project_dir: proj.path().to_path_buf(),
            spec: "npm:react".into(),
            docs_paths_arg: None,
            clear_docs_paths: true,
        };
        run_add(&MockClient::new(), &opts, &deps).unwrap();
        let written = std::fs::read_to_string(proj.path().join("ask.json")).unwrap();
        assert!(written.contains("\"npm:react\""));
        assert!(!written.contains("docsPaths"));
    }

    #[test]
    fn run_add_docs_paths_csv_persists_object_and_warns_unsafe() {
        let proj = tempfile::tempdir().unwrap();
        let no_install = |_: &Path, _: &[String]| -> anyhow::Result<()> { Ok(()) };
        let deps = RunAddDeps {
            installer: Some(&no_install),
            ..Default::default()
        };
        let opts = RunAddOptions {
            project_dir: proj.path().to_path_buf(),
            spec: "npm:react".into(),
            docs_paths_arg: Some("docs, ../evil ,api".into()),
            clear_docs_paths: false,
        };
        let report = run_add(&MockClient::new(), &opts, &deps).unwrap();
        let written = std::fs::read_to_string(proj.path().join("ask.json")).unwrap();
        assert!(written.contains("docsPaths"));
        assert!(written.contains("\"docs\""));
        assert!(written.contains("\"api\""));
        assert!(!written.contains("evil"));
        assert!(report.stderr.iter().any(|m| m.contains("unsafe docs-path")));
    }

    #[test]
    fn run_add_bare_owner_repo_normalizes_to_github() {
        let proj = tempfile::tempdir().unwrap();
        let captured = std::cell::RefCell::new(Vec::new());
        let capture = |_: &Path, specs: &[String]| -> anyhow::Result<()> {
            captured.borrow_mut().extend_from_slice(specs);
            Ok(())
        };
        let deps = RunAddDeps {
            installer: Some(&capture),
            ..Default::default()
        };
        let opts = RunAddOptions {
            project_dir: proj.path().to_path_buf(),
            spec: "vercel/next.js".into(),
            docs_paths_arg: None,
            clear_docs_paths: false,
        };
        run_add(&MockClient::new(), &opts, &deps).unwrap();
        assert_eq!(captured.borrow().as_slice(), ["github:vercel/next.js"]);
        let written = std::fs::read_to_string(proj.path().join("ask.json")).unwrap();
        assert!(written.contains("github:vercel/next.js"));
    }

    #[test]
    fn interactive_requires_tty() {
        let proj = tempfile::tempdir().unwrap();
        let no_tty = || false;
        let deps = RunInteractiveDeps {
            is_tty: Some(&no_tty),
            ..Default::default()
        };
        let report = run_interactive_add(&MockClient::new(), proj.path(), &deps).unwrap();
        assert_eq!(report.exit_code, 1);
        assert!(report.stderr[0].contains("requires a TTY"));
    }

    #[test]
    fn interactive_no_package_json() {
        let proj = tempfile::tempdir().unwrap();
        let tty = || true;
        let deps = RunInteractiveDeps {
            is_tty: Some(&tty),
            ..Default::default()
        };
        let report = run_interactive_add(&MockClient::new(), proj.path(), &deps).unwrap();
        assert_eq!(report.exit_code, 0);
        assert!(report.stderr.iter().any(|m| m.contains("No package.json")));
    }

    #[test]
    fn interactive_selects_and_installs_npm_specs() {
        let proj = tempfile::tempdir().unwrap();
        std::fs::write(
            proj.path().join("package.json"),
            r#"{"dependencies":{"lodash":"4"}}"#,
        )
        .unwrap();
        let tty = || true;
        // MockClient returns nothing → registry lookups miss → all unregistered.
        let pick = |_: &str, opts: &[PromptOption]| -> Vec<String> {
            opts.iter().map(|o| o.value.clone()).collect()
        };
        let no_text = |_: &str, _: &str| -> String { String::new() };
        let captured = std::cell::RefCell::new(Vec::new());
        let capture = |_: &Path, specs: &[String]| -> anyhow::Result<()> {
            captured.borrow_mut().extend_from_slice(specs);
            Ok(())
        };
        let deps = RunInteractiveDeps {
            is_tty: Some(&tty),
            multiselect: Some(&pick),
            text: Some(&no_text),
            installer: Some(&capture),
        };
        let report = run_interactive_add(&MockClient::new(), proj.path(), &deps).unwrap();
        assert_eq!(report.exit_code, 0);
        assert_eq!(captured.borrow().as_slice(), ["npm:lodash"]);
        let written = std::fs::read_to_string(proj.path().join("ask.json")).unwrap();
        assert!(written.contains("npm:lodash"));
    }
}