curie-build 0.5.0

The Curie build tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
//! `curie add` and `curie remove` — manage dependencies in `Curie.toml`.
//!
//! # `curie add`
//!
//! ```text
//! curie add <group:artifact>[@version] [--test] [--annotation-processor] [--bom]
//! ```
//!
//! Adds one entry to the appropriate section of `Curie.toml`:
//!
//! | flags                            | section                       |
//! |----------------------------------|-------------------------------|
//! | *(none)*                         | `[dependencies]`              |
//! | `--test`                         | `[test-dependencies]`         |
//! | `--annotation-processor`         | `[annotation-processors]`     |
//! | `--test --annotation-processor`  | `[test-annotation-processors]`|
//! | `--bom`                          | `[bom-imports]`               |
//! | `--test --bom`                   | `[test-bom-imports]`          |
//!
//! **Version selection** (in order of priority):
//!
//! 1. Explicit `@version` in the coordinate string — used as-is.
//! 2. Coordinate is managed by an already-declared BOM (checked by resolving
//!    the project's `[bom-imports]`) → writes `""` (BOM-managed).
//! 3. Otherwise: fetch `maven-metadata.xml` and pick the latest stable
//!    release using the same algorithm as `curie update`.
//!
//! BOM imports (`--bom`) always require an explicit `@version`.
//!
//! # `curie remove`
//!
//! ```text
//! curie remove <group:artifact> [--test] [--annotation-processor] [--bom]
//! ```
//!
//! Removes the entry from the appropriate section.  Errors when the entry
//! is not present.

use crate::build::{central_repos, extra_repos};
use crate::descriptor::{self, Descriptor};
use crate::search_index::IndexHandle;
use crate::update::{
    epoch_ms_to_date, fetch_all_versions, fetch_latest_stable, fetch_version_timestamps,
    resolve_repo_url,
};
use crate::version_ui::{run_version_phase, run_version_ui, show_loading_screen, VersionPick};
use anyhow::{bail, Context, Result};
use crossterm::{cursor, execute, terminal};
use curie_deps::repo::Repository;
use std::collections::HashMap;
use std::io::Write;
use std::path::Path;
use toml_edit::DocumentMut;

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Options for `curie add`.
#[derive(Debug, Clone)]
pub struct AddOptions {
    /// Add to the test-scoped section instead of the production section.
    pub test: bool,
    /// Target `[annotation-processors]` / `[test-annotation-processors]`
    /// instead of `[dependencies]` / `[test-dependencies]`.
    pub annotation_processor: bool,
    /// Target `[bom-imports]` / `[test-bom-imports]`.
    pub bom: bool,
    /// Do not access the network; fail when version must be resolved.
    pub offline: bool,
    /// Force re-download of the local artifact index before opening the search UI.
    pub refresh_index: bool,
    /// Use the local Tantivy index for search (download it if not yet present).
    pub use_index: bool,
    /// Force Maven Central REST API search even when a local index exists.
    pub api: bool,
}

/// Options for `curie remove`.
#[derive(Debug, Clone)]
pub struct RemoveOptions {
    /// Remove from the test-scoped section.
    pub test: bool,
    /// Remove from `[annotation-processors]` / `[test-annotation-processors]`.
    pub annotation_processor: bool,
    /// Remove from `[bom-imports]` / `[test-bom-imports]`.
    pub bom: bool,
}

/// Add a dependency to `Curie.toml`.
///
/// `coord_arg` is `"group:artifact"` or `"group:artifact@version"`, or `None`
/// to open the interactive artifact search + version picker.
pub fn run_add(project_root: &Path, coord_arg: Option<&str>, opts: AddOptions) -> Result<()> {
    let desc = descriptor::load(project_root)?;
    let section = section_name(&opts);

    let (coord, version) = if coord_arg.is_none() {
        // ── Interactive path: unified search → version picker in one alt-screen ──
        // Prepare data source BEFORE entering the alt screen (may download index).
        let source = prepare_search_source(&opts)?;
        match run_interactive_flow(&opts, &desc, source)? {
            Some(pair) => pair,
            None => return Ok(()), // user cancelled
        }
    } else {
        // ── CLI path: coord (possibly with explicit version) given on command line ──
        let (coord, explicit_version) = parse_coord_arg(coord_arg.unwrap())?;
        if opts.bom && explicit_version.is_none() {
            bail!(
                "BOM imports require an explicit version.\n\
                 Use:  curie add --bom {}@<version>",
                coord
            );
        }
        let version = match explicit_version {
            Some(v) => v,
            None => resolve_version(&coord, &desc, &opts)?,
        };
        (coord, version)
    };

    // -- guard: already present ----------------------------------------------
    if coord_exists_in_section(&desc, section, &coord) {
        bail!(
            "\"{}\" is already present in [{}].\n\
             Run `curie update` to upgrade it.",
            coord, section
        );
    }

    // -- rewrite Curie.toml --------------------------------------------------
    rewrite_toml(project_root, |doc| {
        insert_entry(doc, section, &coord, &version);
        Ok(())
    })?;

    // -- report --------------------------------------------------------------
    if version.is_empty() {
        println!("  Added \"{}\" = \"\" (BOM-managed) to [{}]", coord, section);
    } else {
        println!("  Added \"{}\" = \"{}\" to [{}]", coord, version, section);
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Unified interactive flow (search → version picker, single alt-screen session)
// ---------------------------------------------------------------------------

/// Discriminates which data source backs the artifact search.
enum SearchSource {
    Tantivy(IndexHandle),
    Api,
}

/// Prepare the search source BEFORE entering the alternate screen.
/// Index downloads (if needed) happen here, with normal terminal output.
fn prepare_search_source(opts: &AddOptions) -> Result<SearchSource> {
    if opts.refresh_index {
        let db = crate::search_index::ensure_index(true, opts.offline)?;
        return Ok(SearchSource::Tantivy(db));
    }
    if opts.use_index {
        let db = crate::search_index::ensure_index(false, opts.offline)?;
        return Ok(SearchSource::Tantivy(db));
    }
    if crate::search_index::index_exists() && !opts.api {
        let db = crate::search_index::open_index()?;
        return Ok(SearchSource::Tantivy(db));
    }
    if opts.offline {
        anyhow::bail!(
            "no local artifact index; pass --use-index to download it, or remove --offline"
        );
    }
    Ok(SearchSource::Api)
}

/// Open the full interactive add flow — artifact search, then version picker —
/// in a single alternate-screen session.  Esc on the version picker returns to
/// the artifact search rather than cancelling the whole command.
fn run_interactive_flow(
    opts: &AddOptions,
    desc: &Descriptor,
    source: SearchSource,
) -> Result<Option<(String, String)>> {
    let mut stdout = std::io::stdout();
    terminal::enable_raw_mode()?;
    execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;

    let result = interactive_flow_inner(opts, desc, &source, &mut stdout);

    let _ = execute!(stdout, terminal::LeaveAlternateScreen, cursor::Show);
    let _ = terminal::disable_raw_mode();

    result
}

fn interactive_flow_inner(
    opts: &AddOptions,
    desc: &Descriptor,
    source: &SearchSource,
    stdout: &mut impl Write,
) -> Result<Option<(String, String)>> {
    // Preserve search state across the version picker so the user can
    // return to the same query + selection after pressing Esc.
    let mut tantivy_state: Option<crate::search_ui::UiState>     = None;
    let mut api_state:     Option<crate::api_search_ui::UiState> = None;

    loop {
        // ── Phase 1: artifact selection ────────────────────────────────────
        let coord = match source {
            SearchSource::Tantivy(db) => {
                let (coord, st) = crate::search_ui::run_ui_inner(db, stdout, tantivy_state.take())?;
                tantivy_state = Some(st);
                coord
            }
            SearchSource::Api => {
                let (coord, st) = crate::api_search_ui::run_ui_inner(stdout, api_state.take())?;
                api_state = Some(st);
                coord
            }
        };
        let coord = match coord {
            Some(c) => c,
            None    => return Ok(None), // Esc at search → cancel add
        };

        // ── Loading placeholder while we fetch version data ────────────────
        show_loading_screen(stdout, &coord)?;

        let bom_version   = bom_managed_version(&coord, desc, opts);
        let all_versions  = fetch_coord_versions(&coord, desc, opts);
        let version_dates = fetch_coord_version_dates(&coord, opts);

        // ── Phase 2: version selection ─────────────────────────────────────
        let pick = run_version_phase(
            &coord, &all_versions, bom_version.as_deref(), &version_dates, stdout,
        )?;

        match pick {
            Some(VersionPick::BomManaged)   => return Ok(Some((coord, String::new()))),
            Some(VersionPick::Explicit(v))  => return Ok(Some((coord, v))),
            // Esc on version picker → go back to artifact search
            None => continue,
        }
    }
}

/// Fetch release dates for all versions of `coord` from the Maven Central GAV API.
///
/// Returns a map of `version → "YYYY-MM-DD"`.  Returns an empty map when
/// offline or on any network/parse failure (graceful degradation).
fn fetch_coord_version_dates(coord: &str, opts: &AddOptions) -> HashMap<String, String> {
    if opts.offline { return HashMap::new(); }
    let Ok(client) = reqwest::blocking::Client::builder()
        .user_agent("curie-add/0.1")
        .timeout(std::time::Duration::from_secs(15))
        .build() else { return HashMap::new(); };
    fetch_version_timestamps(&client, coord)
        .into_iter()
        .filter_map(|(v, ms)| {
            let date = epoch_ms_to_date(ms);
            if date.is_empty() { None } else { Some((v, date)) }
        })
        .collect()
}

/// Fetch all versions from Maven Central for `coord`.  Returns an empty vec on
/// network failure or when offline (the version picker still shows BOM entry).
fn fetch_coord_versions(coord: &str, desc: &Descriptor, opts: &AddOptions) -> Vec<String> {
    if opts.offline {
        return vec![];
    }
    let default_repos = central_repos();
    let named_repos   = extra_repos(desc);
    let repo_url      = resolve_repo_url(&None, &named_repos, &default_repos);
    let Ok(client) = reqwest::blocking::Client::builder()
        .user_agent("curie-add/0.1")
        .timeout(std::time::Duration::from_secs(15))
        .build() else { return vec![]; };
    fetch_all_versions(&client, &repo_url, coord).unwrap_or_default()
}

/// Remove a dependency from `Curie.toml`.
///
/// `coord_arg` is `"group:artifact"` (a trailing `@version` is tolerated but
/// ignored — only the coordinate is needed to identify the entry).
pub fn run_remove(project_root: &Path, coord_arg: &str, opts: RemoveOptions) -> Result<()> {
    // strip optional @version so callers can pass the same string to both commands
    let (coord, _) = parse_coord_arg(coord_arg)?;
    let section = section_name_remove(&opts);

    // -- rewrite Curie.toml --------------------------------------------------
    rewrite_toml(project_root, |doc| {
        // Verify entry exists before mutating.
        let entry_present = doc
            .get(section)
            .and_then(|v| v.as_table())
            .map(|t| t.contains_key(&coord))
            .unwrap_or(false);

        if !entry_present {
            anyhow::bail!("\"{}\" not found in [{}]", coord, section);
        }

        doc.get_mut(section)
            .and_then(|v| v.as_table_mut())
            .expect("table existence checked above")
            .remove(&coord);
        Ok(())
    })?;

    println!("  Removed \"{}\" from [{}]", coord, section);
    Ok(())
}

// ---------------------------------------------------------------------------
// TOML rewrite helper
// ---------------------------------------------------------------------------

/// Read `Curie.toml`, parse it, call `f` to mutate the document, then write
/// it back.  All three I/O operations carry the file path in their error
/// context.  `f` may return an error to abort the write.
fn rewrite_toml(project_root: &Path, f: impl FnOnce(&mut DocumentMut) -> Result<()>) -> Result<()> {
    let toml_path = project_root.join("Curie.toml");
    let content = std::fs::read_to_string(&toml_path)
        .with_context(|| format!("failed to read {}", toml_path.display()))?;
    let mut doc: DocumentMut = content
        .parse()
        .with_context(|| format!("failed to parse {} as TOML", toml_path.display()))?;
    f(&mut doc)?;
    std::fs::write(&toml_path, doc.to_string())
        .with_context(|| format!("failed to write {}", toml_path.display()))
}

// ---------------------------------------------------------------------------
// Section routing
// ---------------------------------------------------------------------------

/// Map the three section-routing flags to the Curie.toml table name.
fn section_for(bom: bool, annotation_processor: bool, test: bool) -> &'static str {
    match (bom, annotation_processor, test) {
        (true,  _,     false) => "bom-imports",
        (true,  _,     true)  => "test-bom-imports",
        (false, true,  false) => "annotation-processors",
        (false, true,  true)  => "test-annotation-processors",
        (false, false, false) => "dependencies",
        (false, false, true)  => "test-dependencies",
    }
}

fn section_name(opts: &AddOptions) -> &'static str {
    section_for(opts.bom, opts.annotation_processor, opts.test)
}

fn section_name_remove(opts: &RemoveOptions) -> &'static str {
    section_for(opts.bom, opts.annotation_processor, opts.test)
}

// ---------------------------------------------------------------------------
// Coordinate parsing
// ---------------------------------------------------------------------------

/// Parse `"group:artifact"` or `"group:artifact@version"`.
///
/// Returns `(coord, Option<version>)`.  Errors when the coordinate does not
/// contain exactly one `:` (before the `@`).
fn parse_coord_arg(arg: &str) -> Result<(String, Option<String>)> {
    let (coord_part, version_part) = match arg.split_once('@') {
        Some((c, v)) => (c, Some(v.to_string())),
        None => (arg, None),
    };

    if coord_part.split(':').count() != 2 {
        bail!(
            "invalid coordinate \"{}\": expected \"group:artifact\" or \
             \"group:artifact@version\"",
            arg
        );
    }
    // Validate neither part is empty
    let mut parts = coord_part.splitn(2, ':');
    let group = parts.next().unwrap_or("").trim();
    let artifact = parts.next().unwrap_or("").trim();
    if group.is_empty() || artifact.is_empty() {
        bail!("invalid coordinate \"{}\": group and artifact must be non-empty", arg);
    }

    Ok((coord_part.to_string(), version_part))
}

// ---------------------------------------------------------------------------
// Guard: duplicate detection against the parsed descriptor
// ---------------------------------------------------------------------------

fn coord_exists_in_section(desc: &Descriptor, section: &str, coord: &str) -> bool {
    match section {
        "dependencies"              => desc.dependencies.contains_key(coord),
        "test-dependencies"         => desc.test_dependencies.contains_key(coord),
        "bom-imports"               => desc.bom_imports.contains_key(coord),
        "test-bom-imports"          => desc.test_bom_imports.contains_key(coord),
        "annotation-processors"     => desc.annotation_processors.contains_key(coord),
        "test-annotation-processors" => desc.test_annotation_processors.contains_key(coord),
        _ => false,
    }
}

// ---------------------------------------------------------------------------
// Version resolution
// ---------------------------------------------------------------------------

/// Determine the version string to write when none was given on the CLI.
///
/// * Offline: auto-accept BOM if available, otherwise fail.
/// * Online:  open an interactive version picker (pre-selecting the BOM version,
///   the latest stable, or the newest available — in that priority order).
fn resolve_version(
    coord: &str,
    desc: &Descriptor,
    opts: &AddOptions,
) -> Result<String> {
    let bom_version = bom_managed_version(coord, desc, opts);

    // Offline path: no TUI, no network.
    if opts.offline {
        if bom_version.is_some() {
            return Ok(String::new()); // BOM-managed
        }
        bail!(
            "\"{}\" is not managed by any declared BOM and no version was \
             specified.\nRe-run without --offline or pass an explicit version:\n  \
             curie add {}@<version>",
            coord, coord
        );
    }

    let default_repos = central_repos();
    let named_repos = extra_repos(desc);
    let repo_url = resolve_repo_url(&None, &named_repos, &default_repos);

    let client = reqwest::blocking::Client::builder()
        .user_agent("curie-add/0.1")
        .timeout(std::time::Duration::from_secs(15))
        .build()
        .context("failed to build HTTP client")?;

    // Fetch all versions from maven-metadata.xml; open the picker if successful.
    if let Some(versions) = fetch_all_versions(&client, &repo_url, coord) {
        let version_dates = fetch_coord_version_dates(coord, opts);
        let pick = run_version_ui(coord, &versions, bom_version.as_deref(), &version_dates)?;
        return match pick {
            Some(VersionPick::BomManaged)    => Ok(String::new()),
            Some(VersionPick::Explicit(v))   => Ok(v),
            None                             => bail!("add cancelled"),
        };
    }

    // Fallback when version fetch fails (network error, artifact absent).
    if bom_version.is_some() {
        return Ok(String::new());
    }
    match fetch_latest_stable(&client, &repo_url, coord) {
        Some(v) => Ok(v),
        None => bail!(
            "could not find any release of \"{}\" in {}.\n\
             Specify a version explicitly:  curie add {}@<version>",
            coord, repo_url, coord
        ),
    }
}

/// Look up `coord` in the BOM-managed version map for the project.
///
/// Uses the *same* BOM resolution the build itself would use:
/// - For production deps/annotation-processors/bom-imports: `prod_bom_gavs()`.
/// - For test-scoped: `test_bom_gavs()` (superset of prod).
///
/// Returns `Some(managed_version)` when the coord is found, `None` otherwise.
///
/// This is intentionally a *best-effort* check: if BOM resolution fails
/// (network error, malformed POM), we fall through to the Maven-metadata
/// fetch rather than aborting the `add` command.
fn bom_managed_version(coord: &str, desc: &Descriptor, opts: &AddOptions) -> Option<String> {
    use curie_deps::resolver::resolve_boms;

    let bom_gavs = if opts.test {
        desc.test_bom_gavs().ok()?
    } else {
        desc.prod_bom_gavs().ok()?
    };

    if bom_gavs.is_empty() {
        return None;
    }

    let default_repos = central_repos();
    let named_repos = extra_repos(desc);
    let all_repos: Vec<Repository> = default_repos
        .into_iter()
        .chain(named_repos)
        .collect();

    let client = reqwest::blocking::Client::builder()
        .user_agent("curie-add/0.1")
        .timeout(std::time::Duration::from_secs(15))
        .build()
        .ok()?;

    let managed = resolve_boms(&bom_gavs, &all_repos, &client, false).ok()?;
    managed.get(coord).cloned()
}

// ---------------------------------------------------------------------------
// TOML insert
// ---------------------------------------------------------------------------

/// Insert `"coord" = "version"` into `section`, creating the table when absent.
///
/// The new entry is always appended at the end of the section so it doesn't
/// disrupt existing comment/whitespace layout.
fn insert_entry(doc: &mut DocumentMut, section: &str, coord: &str, version: &str) {
    // Ensure the section table exists.
    if doc.get(section).is_none() {
        let mut tbl = toml_edit::Table::new();
        tbl.set_implicit(false);
        doc.insert(section, toml_edit::Item::Table(tbl));
    }

    let table = doc
        .get_mut(section)
        .and_then(|v| v.as_table_mut())
        .expect("section was just created");

    table.insert(coord, toml_edit::value(version));
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    /// Write a minimal `Curie.toml` in a temp dir and return the dir handle.
    fn make_project(content: &str) -> TempDir {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("Curie.toml"), content).unwrap();
        dir
    }

    fn read_toml(dir: &TempDir) -> String {
        fs::read_to_string(dir.path().join("Curie.toml")).unwrap()
    }

    // -----------------------------------------------------------------------
    // parse_coord_arg
    // -----------------------------------------------------------------------

    #[test]
    fn parse_coord_without_version() {
        let (coord, ver) = parse_coord_arg("com.example:foo").unwrap();
        assert_eq!(coord, "com.example:foo");
        assert!(ver.is_none());
    }

    #[test]
    fn parse_coord_with_version() {
        let (coord, ver) = parse_coord_arg("com.example:foo@1.2.3").unwrap();
        assert_eq!(coord, "com.example:foo");
        assert_eq!(ver.as_deref(), Some("1.2.3"));
    }

    #[test]
    fn parse_coord_missing_colon_errors() {
        assert!(parse_coord_arg("invalid").is_err());
    }

    #[test]
    fn parse_coord_empty_group_errors() {
        assert!(parse_coord_arg(":artifact").is_err());
    }

    #[test]
    fn parse_coord_empty_artifact_errors() {
        assert!(parse_coord_arg("group:").is_err());
    }

    // -----------------------------------------------------------------------
    // section routing
    // -----------------------------------------------------------------------

    #[test]
    fn section_defaults_to_dependencies() {
        let opts = AddOptions { test: false, annotation_processor: false, bom: false, offline: false, refresh_index: false, use_index: false, api: false };
        assert_eq!(section_name(&opts), "dependencies");
    }

    #[test]
    fn section_test_flag() {
        let opts = AddOptions { test: true, annotation_processor: false, bom: false, offline: false, refresh_index: false, use_index: false, api: false };
        assert_eq!(section_name(&opts), "test-dependencies");
    }

    #[test]
    fn section_annotation_processor() {
        let opts = AddOptions { test: false, annotation_processor: true, bom: false, offline: false, refresh_index: false, use_index: false, api: false };
        assert_eq!(section_name(&opts), "annotation-processors");
    }

    #[test]
    fn section_test_annotation_processor() {
        let opts = AddOptions { test: true, annotation_processor: true, bom: false, offline: false, refresh_index: false, use_index: false, api: false };
        assert_eq!(section_name(&opts), "test-annotation-processors");
    }

    #[test]
    fn section_bom() {
        let opts = AddOptions { test: false, annotation_processor: false, bom: true, offline: false, refresh_index: false, use_index: false, api: false };
        assert_eq!(section_name(&opts), "bom-imports");
    }

    #[test]
    fn section_test_bom() {
        let opts = AddOptions { test: true, annotation_processor: false, bom: true, offline: false, refresh_index: false, use_index: false, api: false };
        assert_eq!(section_name(&opts), "test-bom-imports");
    }

    // -----------------------------------------------------------------------
    // insert_entry
    // -----------------------------------------------------------------------

    #[test]
    fn insert_entry_creates_section_if_absent() {
        let toml = "[application]\nname = \"demo\"\n";
        let mut doc: DocumentMut = toml.parse().unwrap();
        insert_entry(&mut doc, "dependencies", "com.example:foo", "1.0.0");
        let out = doc.to_string();
        assert!(out.contains("[dependencies]"), "got: {}", out);
        assert!(out.contains("\"com.example:foo\" = \"1.0.0\""), "got: {}", out);
    }

    #[test]
    fn insert_entry_appends_to_existing_section() {
        let toml = "[dependencies]\n\"org.existing:lib\" = \"2.0\"\n";
        let mut doc: DocumentMut = toml.parse().unwrap();
        insert_entry(&mut doc, "dependencies", "com.example:foo", "1.5.0");
        let out = doc.to_string();
        assert!(out.contains("\"org.existing:lib\" = \"2.0\""), "got: {}", out);
        assert!(out.contains("\"com.example:foo\" = \"1.5.0\""), "got: {}", out);
    }

    #[test]
    fn insert_entry_preserves_comments() {
        let toml = "# header\n[dependencies]\n# keep this\n\"x:y\" = \"1.0\"\n";
        let mut doc: DocumentMut = toml.parse().unwrap();
        insert_entry(&mut doc, "dependencies", "a:b", "2.0");
        let out = doc.to_string();
        assert!(out.contains("# header"), "got: {}", out);
        assert!(out.contains("# keep this"), "got: {}", out);
    }

    // -----------------------------------------------------------------------
    // run_add — file-level integration (no network)
    // -----------------------------------------------------------------------

    fn minimal_app_toml() -> &'static str {
        "[application]\nname = \"demo\"\nversion = \"0.1.0\"\n"
    }

    #[test]
    fn add_with_explicit_version_inserts_entry() {
        let dir = make_project(minimal_app_toml());
        run_add(
            dir.path(),
            Some("com.google.guava:guava@33.0.0-jre"),
            AddOptions { test: false, annotation_processor: false, bom: false, offline: false, refresh_index: false, use_index: false, api: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(out.contains("[dependencies]"), "got: {}", out);
        assert!(out.contains("\"com.google.guava:guava\" = \"33.0.0-jre\""), "got: {}", out);
    }

    #[test]
    fn add_to_test_section_with_explicit_version() {
        let dir = make_project(minimal_app_toml());
        run_add(
            dir.path(),
            Some("org.junit.jupiter:junit-jupiter@5.10.0"),
            AddOptions { test: true, annotation_processor: false, bom: false, offline: false, refresh_index: false, use_index: false, api: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(out.contains("[test-dependencies]"), "got: {}", out);
        assert!(out.contains("\"org.junit.jupiter:junit-jupiter\" = \"5.10.0\""), "got: {}", out);
        assert!(!out.contains("[dependencies]"), "should not have prod deps section, got: {}", out);
    }

    #[test]
    fn add_annotation_processor_with_explicit_version() {
        let dir = make_project(minimal_app_toml());
        run_add(
            dir.path(),
            Some("org.projectlombok:lombok@1.18.30"),
            AddOptions { test: false, annotation_processor: true, bom: false, offline: false, refresh_index: false, use_index: false, api: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(out.contains("[annotation-processors]"), "got: {}", out);
        assert!(out.contains("\"org.projectlombok:lombok\" = \"1.18.30\""), "got: {}", out);
    }

    #[test]
    fn add_test_annotation_processor_with_explicit_version() {
        let dir = make_project(minimal_app_toml());
        run_add(
            dir.path(),
            Some("com.example:proc@2.0.0"),
            AddOptions { test: true, annotation_processor: true, bom: false, offline: false, refresh_index: false, use_index: false, api: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(out.contains("[test-annotation-processors]"), "got: {}", out);
        assert!(out.contains("\"com.example:proc\" = \"2.0.0\""), "got: {}", out);
    }

    #[test]
    fn add_bom_with_explicit_version() {
        let dir = make_project(minimal_app_toml());
        run_add(
            dir.path(),
            Some("org.springframework.boot:spring-boot-dependencies@3.2.0"),
            AddOptions { test: false, annotation_processor: false, bom: true, offline: false, refresh_index: false, use_index: false, api: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(out.contains("[bom-imports]"), "got: {}", out);
        assert!(
            out.contains("\"org.springframework.boot:spring-boot-dependencies\" = \"3.2.0\""),
            "got: {}", out
        );
    }

    #[test]
    fn add_test_bom_with_explicit_version() {
        let dir = make_project(minimal_app_toml());
        run_add(
            dir.path(),
            Some("com.example:test-bom@1.0.0"),
            AddOptions { test: true, annotation_processor: false, bom: true, offline: false, refresh_index: false, use_index: false, api: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(out.contains("[test-bom-imports]"), "got: {}", out);
    }

    #[test]
    fn add_bom_without_version_errors() {
        let dir = make_project(minimal_app_toml());
        let result = run_add(
            dir.path(),
            Some("org.springframework.boot:spring-boot-dependencies"),
            AddOptions { test: false, annotation_processor: false, bom: true, offline: false, refresh_index: false, use_index: false, api: false },
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("require an explicit version"));
    }

    #[test]
    fn add_duplicate_coord_errors() {
        let toml = "[application]\nname=\"demo\"\nversion=\"0.1.0\"\n\
                    [dependencies]\n\"com.example:foo\" = \"1.0.0\"\n";
        let dir = make_project(toml);
        let result = run_add(
            dir.path(),
            Some("com.example:foo@2.0.0"),
            AddOptions { test: false, annotation_processor: false, bom: false, offline: false, refresh_index: false, use_index: false, api: false },
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("already present"));
    }

    #[test]
    fn add_without_version_offline_errors() {
        let dir = make_project(minimal_app_toml());
        let result = run_add(
            dir.path(),
            Some("com.example:foo"),
            AddOptions { test: false, annotation_processor: false, bom: false, offline: true, refresh_index: false, use_index: false, api: false },
        );
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(msg.contains("offline") || msg.contains("--offline"), "got: {}", msg);
    }

    #[test]
    fn add_with_bom_managed_dep_writes_empty_version() {
        // We test the *TOML-rewrite* side only (no network): simulate the
        // BOM-managed path by calling insert_entry with an empty version.
        let dir = make_project(minimal_app_toml());
        let toml_path = dir.path().join("Curie.toml");
        let content = std::fs::read_to_string(&toml_path).unwrap();
        let mut doc: DocumentMut = content.parse().unwrap();
        insert_entry(&mut doc, "dependencies", "com.fasterxml.jackson.core:jackson-databind", "");
        std::fs::write(&toml_path, doc.to_string()).unwrap();

        let out = read_toml(&dir);
        assert!(
            out.contains("\"com.fasterxml.jackson.core:jackson-databind\" = \"\""),
            "got: {}", out
        );
    }

    // -----------------------------------------------------------------------
    // run_remove
    // -----------------------------------------------------------------------

    #[test]
    fn remove_existing_dep_succeeds() {
        let toml = "[application]\nname=\"demo\"\nversion=\"0.1.0\"\n\
                    [dependencies]\n\"com.example:foo\" = \"1.0.0\"\n\
                    \"com.example:bar\" = \"2.0.0\"\n";
        let dir = make_project(toml);
        run_remove(
            dir.path(),
            "com.example:foo",
            RemoveOptions { test: false, annotation_processor: false, bom: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(!out.contains("\"com.example:foo\""), "got: {}", out);
        assert!(out.contains("\"com.example:bar\""), "got: {}", out);
    }

    #[test]
    fn remove_absent_dep_errors() {
        let dir = make_project(minimal_app_toml());
        let result = run_remove(
            dir.path(),
            "com.example:nonexistent",
            RemoveOptions { test: false, annotation_processor: false, bom: false },
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[test]
    fn remove_from_test_section() {
        let toml = "[application]\nname=\"demo\"\nversion=\"0.1.0\"\n\
                    [test-dependencies]\n\"org.junit.jupiter:junit-jupiter\" = \"5.10.0\"\n";
        let dir = make_project(toml);
        run_remove(
            dir.path(),
            "org.junit.jupiter:junit-jupiter",
            RemoveOptions { test: true, annotation_processor: false, bom: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(!out.contains("junit-jupiter"), "got: {}", out);
    }

    #[test]
    fn remove_from_bom_imports() {
        let toml = "[application]\nname=\"demo\"\nversion=\"0.1.0\"\n\
                    [bom-imports]\n\"org.springframework.boot:spring-boot-dependencies\" = \"3.2.0\"\n";
        let dir = make_project(toml);
        run_remove(
            dir.path(),
            "org.springframework.boot:spring-boot-dependencies",
            RemoveOptions { test: false, annotation_processor: false, bom: true },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(!out.contains("spring-boot-dependencies"), "got: {}", out);
    }

    #[test]
    fn remove_from_annotation_processors() {
        let toml = "[application]\nname=\"demo\"\nversion=\"0.1.0\"\n\
                    [annotation-processors]\n\"org.projectlombok:lombok\" = \"1.18.30\"\n";
        let dir = make_project(toml);
        run_remove(
            dir.path(),
            "org.projectlombok:lombok",
            RemoveOptions { test: false, annotation_processor: true, bom: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(!out.contains("lombok"), "got: {}", out);
    }

    #[test]
    fn remove_wrong_section_errors() {
        // coord present in [dependencies] but user passes --test
        let toml = "[application]\nname=\"demo\"\nversion=\"0.1.0\"\n\
                    [dependencies]\n\"com.example:foo\" = \"1.0.0\"\n";
        let dir = make_project(toml);
        let result = run_remove(
            dir.path(),
            "com.example:foo",
            RemoveOptions { test: true, annotation_processor: false, bom: false },
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[test]
    fn remove_ignores_version_suffix_in_coord() {
        let toml = "[application]\nname=\"demo\"\nversion=\"0.1.0\"\n\
                    [dependencies]\n\"com.example:foo\" = \"1.0.0\"\n";
        let dir = make_project(toml);
        // Passing com.example:foo@1.0.0 should strip the @1.0.0 part
        run_remove(
            dir.path(),
            "com.example:foo@1.0.0",
            RemoveOptions { test: false, annotation_processor: false, bom: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(!out.contains("\"com.example:foo\""), "got: {}", out);
    }

    #[test]
    fn remove_preserves_sibling_entries_and_comments() {
        let toml = "# top\n[application]\nname=\"demo\"\nversion=\"0.1.0\"\n\
                    [dependencies]\n# keep\n\"a:b\" = \"1.0\"\n\"c:d\" = \"2.0\"\n";
        let dir = make_project(toml);
        run_remove(
            dir.path(),
            "c:d",
            RemoveOptions { test: false, annotation_processor: false, bom: false },
        ).unwrap();
        let out = read_toml(&dir);
        assert!(out.contains("# top"), "got: {}", out);
        assert!(out.contains("# keep"), "got: {}", out);
        assert!(out.contains("\"a:b\""), "got: {}", out);
        assert!(!out.contains("\"c:d\""), "got: {}", out);
    }
}