tracevault-cli 0.21.0

CLI tool for Visdom Trace - AI code tracing and attribution
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
use crate::config::{TracevaultConfig, UserContext};
use crate::context::{resolve_context_paths, Context, WorktreeScope};
use std::io;
use std::path::{Path, PathBuf};

/// Walk up from `start` to find the nearest ancestor that contains a
/// `.tracevault/` directory. Returns that ancestor. Errors if none found.
///
/// Kept as a public utility for integration tests and callers that need the
/// classic "must have .tracevault/" guarantee (e.g. legacy hooks, tests).
/// Worktree-aware commands use `resolve_context_paths` directly instead.
pub fn find_project_root(start: &Path) -> Result<PathBuf, Box<dyn std::error::Error>> {
    for ancestor in start.ancestors() {
        if ancestor.join(".tracevault").is_dir() {
            return Ok(ancestor.to_path_buf());
        }
    }
    Err(format!(
        "No .tracevault/ directory found in '{}' or any parent directory. \
         Run `tracevault init` first.",
        start.display()
    )
    .into())
}

/// Parse a `--param` value of the form `key=value`.
/// Rejects values with no `=`.
fn parse_param(raw: &str) -> Result<(String, String), Box<dyn std::error::Error>> {
    let pos = raw
        .find('=')
        .ok_or_else(|| format!("invalid --param '{raw}': expected key=value"))?;
    let key = raw[..pos].to_string();
    let value = raw[pos + 1..].to_string();
    Ok((key, value))
}

/// Split a label argument on commas, trimming whitespace.
fn split_labels(labels: &[String]) -> Vec<String> {
    labels
        .iter()
        .flat_map(|l| l.split(','))
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect()
}

/// Resolve the file backing the project's user-level context source
/// (`user_context` in `config.toml`), regardless of whether it is enabled.
///
/// Used by `--user` on `context set/update/clear` to edit that file directly.
fn user_context_path(cwd: &Path) -> Result<PathBuf, Box<dyn std::error::Error>> {
    let root = find_project_root(cwd)?;
    // A *malformed* config.toml must not silently fall back to the default path:
    // that could route a `--user` edit to the wrong file when the user actually
    // configured a custom path we failed to parse. A *missing* config is fine —
    // there is no configured override, so the default path is correct.
    let config = TracevaultConfig::try_load(&root)
        .map_err(|e| format!("cannot resolve --user path: malformed .tracevault/config.toml: {e}"))?
        .unwrap_or_default();
    Ok(config.user_context.path())
}

/// `tracevault context set` — build a fresh Context from flags, save it.
/// Omitted dimensions are empty. Clears anything not explicitly provided.
///
/// Default scope:
/// - Linked worktree → writes `worktrees/<key>/context.json`.
/// - Primary checkout → writes the global `context.json`.
///
/// `global = true` forces the global file from any worktree.
/// `user = true` writes the resolved user-context file instead (wins over `global`).
pub fn run_set(
    cwd: &Path,
    flow: Option<String>,
    labels: Vec<String>,
    params: Vec<String>,
    global: bool,
    user: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut parsed_params = std::collections::BTreeMap::new();
    for raw in &params {
        let (k, v) = parse_param(raw)?;
        parsed_params.insert(k, Some(v));
    }

    let ctx = Context {
        flow_id: flow,
        labels: split_labels(&labels),
        params: parsed_params,
    };

    if user {
        let path = user_context_path(cwd)?;
        ctx.save_to(&path)
            .map_err(|e: io::Error| format!("failed to save context: {e}"))?;
        println!("Context set.");
        return Ok(());
    }

    let paths = resolve_context_paths(cwd);

    // Require an initialised .tracevault/ before mutating.
    if !paths.tracevault_dir.exists() {
        return Err("no .tracevault/ found — run 'tracevault init' first".into());
    }

    if global || matches!(paths.scope, WorktreeScope::Primary) {
        ctx.save_global(&paths)
    } else {
        ctx.save_worktree(&paths)
    }
    .map_err(|e: io::Error| format!("failed to save context: {e}"))?;

    println!("Context set.");
    Ok(())
}

/// `tracevault context update` — load existing, apply mutations, save.
///
/// Default scope:
/// - Linked worktree → reads/writes `worktrees/<key>/context.json`.
/// - Primary checkout → reads/writes the global `context.json`.
///
/// `global = true` forces the global file from any worktree.
/// `user = true` reads/writes the resolved user-context file instead (wins over `global`).
#[allow(clippy::too_many_arguments)]
pub fn run_update(
    cwd: &Path,
    flow: Option<String>,
    labels: Vec<String>,
    params: Vec<String>,
    remove_labels: Vec<String>,
    remove_params: Vec<String>,
    global: bool,
    user: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    if user {
        let path = user_context_path(cwd)?;
        let mut ctx = Context::load_from(&path);
        apply_update_mutations(
            &mut ctx,
            flow,
            &labels,
            &params,
            &remove_labels,
            &remove_params,
        )?;
        ctx.save_to(&path)
            .map_err(|e: io::Error| format!("failed to save context: {e}"))?;
        println!("Context updated.");
        return Ok(());
    }

    let paths = resolve_context_paths(cwd);

    // Require an initialised .tracevault/ before mutating.
    if !paths.tracevault_dir.exists() {
        return Err("no .tracevault/ found — run 'tracevault init' first".into());
    }

    let use_global = global || matches!(paths.scope, WorktreeScope::Primary);

    let mut ctx = if use_global {
        Context::load_global(&paths)
    } else {
        Context::load_worktree(&paths).unwrap_or_default()
    };

    apply_update_mutations(
        &mut ctx,
        flow,
        &labels,
        &params,
        &remove_labels,
        &remove_params,
    )?;

    if use_global {
        ctx.save_global(&paths)
    } else {
        ctx.save_worktree(&paths)
    }
    .map_err(|e: io::Error| format!("failed to save context: {e}"))?;

    println!("Context updated.");
    Ok(())
}

/// Apply the `update` flow's mutations (set flow, union labels, upsert params,
/// remove labels/params) to an already-loaded `ctx`. Shared by the
/// repo/worktree/global path and the `--user` path so both branches stay in sync.
fn apply_update_mutations(
    ctx: &mut Context,
    flow: Option<String>,
    labels: &[String],
    params: &[String],
    remove_labels: &[String],
    remove_params: &[String],
) -> Result<(), Box<dyn std::error::Error>> {
    // Set flow if provided
    if let Some(f) = flow {
        ctx.flow_id = Some(f);
    }

    // Union-add labels (dedup guarded inline by the contains check below; save also normalises)
    let new_labels = split_labels(labels);
    for l in new_labels {
        if !ctx.labels.contains(&l) {
            ctx.labels.push(l);
        }
    }

    // Insert/overwrite params
    for raw in params {
        let (k, v) = parse_param(raw)?;
        ctx.params.insert(k, Some(v));
    }

    // Remove labels
    let remove_set: std::collections::HashSet<String> =
        split_labels(remove_labels).into_iter().collect();
    ctx.labels.retain(|l| !remove_set.contains(l));

    // Remove params: insert a `None` tombstone so the removal propagates through
    // the merge and drops the key even when a lower-precedence layer sets it.
    for k in remove_params {
        ctx.params.insert(k.clone(), None);
    }

    Ok(())
}

/// `tracevault context show` — print four labelled sections:
///   1. User — the cross-repo user-level context (its contents are shown only
///      when `user_context` is enabled in `config.toml`; the header shows the
///      resolved file path when enabled and `disabled` otherwise).
///   2. Global — the repo-wide `context.json`.
///   3. This worktree — the per-worktree context (Linked only; "(none)" when missing).
///   4. Effective — the merged result that the hook stamps on every event, with
///      each label/param annotated with the layer it came from.
///
/// Prints a hint if no `.tracevault/` directory is found (i.e. `tracevault init` has
/// not been run), using `find_project_root` for the diagnostic.
pub fn run_show(cwd: &Path) -> Result<(), Box<dyn std::error::Error>> {
    // Resolve the user-level context source from config.toml (if any project
    // root/config exists yet — tolerate neither being present, same as
    // `user_context_path`).
    let config = find_project_root(cwd)
        .ok()
        .and_then(|root| TracevaultConfig::load(&root))
        .unwrap_or_default();
    let user_path = config.user_context.resolve();
    let user_enabled = user_path.is_some();

    let (paths, user, global, worktree, effective) =
        Context::effective_with_parts(cwd, user_path.as_deref());

    // If the tracevault directory doesn't exist yet, hint that init is needed.
    // find_project_root gives the classic walk-up path; if it also fails, the
    // user has not run `tracevault init` yet.
    if !paths.tracevault_dir.exists() && find_project_root(cwd).is_err() {
        eprintln!("hint: No .tracevault/ directory found. Run `tracevault init` first.");
    }

    let is_linked = matches!(paths.scope, WorktreeScope::Linked { .. });

    print!(
        "{}",
        show_report(
            user.as_ref(),
            user_enabled,
            user_path.as_deref(),
            &global,
            worktree.as_ref(),
            is_linked,
            &effective,
        )
    );

    Ok(())
}

/// Layer names used in provenance annotations, precedence low→high.
const LAYER_USER: &str = "user";
const LAYER_REPO: &str = "repo";
const LAYER_WORKTREE: &str = "worktree";

/// Highest-precedence layer (`worktree` > `repo` > `user`) whose stored
/// `params` map has `key` set to `Some(_)`. `None` tombstones are skipped:
/// per `Context::merge_layers`, a key only survives into the effective
/// params at all if the highest layer that *touches* it sets `Some`, so the
/// highest `Some` always identifies the correct source.
fn param_source(
    user: Option<&Context>,
    global: &Context,
    worktree: Option<&Context>,
    key: &str,
) -> &'static str {
    if let Some(w) = worktree {
        if matches!(w.params.get(key), Some(Some(_))) {
            return LAYER_WORKTREE;
        }
    }
    if matches!(global.params.get(key), Some(Some(_))) {
        return LAYER_REPO;
    }
    if let Some(u) = user {
        if matches!(u.params.get(key), Some(Some(_))) {
            return LAYER_USER;
        }
    }
    "?"
}

/// First (lowest-precedence) layer whose `labels` contains `label` — labels
/// are a union, so the *introducing* layer (not the overriding one) is the
/// meaningful provenance.
fn label_source(
    user: Option<&Context>,
    global: &Context,
    worktree: Option<&Context>,
    label: &str,
) -> &'static str {
    if let Some(u) = user {
        if u.labels.iter().any(|l| l == label) {
            return LAYER_USER;
        }
    }
    if global.labels.iter().any(|l| l == label) {
        return LAYER_REPO;
    }
    if let Some(w) = worktree {
        if w.labels.iter().any(|l| l == label) {
            return LAYER_WORKTREE;
        }
    }
    "?"
}

/// Highest-precedence layer whose `flow_id` is `Some(_)` — same "highest
/// wins" rule `merge_layers` uses for `flow_id`.
fn flow_source(
    user: Option<&Context>,
    global: &Context,
    worktree: Option<&Context>,
) -> &'static str {
    if let Some(w) = worktree {
        if w.flow_id.is_some() {
            return LAYER_WORKTREE;
        }
    }
    if global.flow_id.is_some() {
        return LAYER_REPO;
    }
    if let Some(u) = user {
        if u.flow_id.is_some() {
            return LAYER_USER;
        }
    }
    "?"
}

/// Pure formatter for `tracevault context show`. Extracted out of `run_show`
/// so the provenance logic is unit-testable without capturing stdout.
///
/// `user_enabled`/`user_path` carry the `user_context` config state needed
/// for the `User` header (a `Context`/`EffectiveContext` alone can't tell
/// "disabled" apart from "enabled but file missing"). `is_linked` likewise
/// carries `ContextPaths::scope`: a bare `worktree: Option<&Context>` can't
/// distinguish "primary checkout, no per-worktree section at all" from
/// "linked worktree with no per-worktree file yet ('(none)')".
#[allow(clippy::too_many_arguments)]
fn show_report(
    user: Option<&Context>,
    user_enabled: bool,
    user_path: Option<&Path>,
    global: &Context,
    worktree: Option<&Context>,
    is_linked: bool,
    eff: &crate::context::EffectiveContext,
) -> String {
    use std::fmt::Write;
    let mut out = String::new();

    let file_desc = match user_path {
        Some(p) if user_enabled => p.display().to_string(),
        _ => "disabled".to_string(),
    };
    let _ = writeln!(
        out,
        "=== User (enabled: {user_enabled}, file: {file_desc}) ==="
    );
    match user {
        Some(u) => {
            let _ = writeln!(
                out,
                "{}",
                serde_json::to_string_pretty(u).unwrap_or_default()
            );
        }
        None => {
            let _ = writeln!(out, "(none)");
        }
    }

    let _ = writeln!(out, "\n=== Global ===");
    let _ = writeln!(
        out,
        "{}",
        serde_json::to_string_pretty(global).unwrap_or_default()
    );

    if is_linked {
        let _ = writeln!(out, "\n=== This worktree ===");
        match worktree {
            Some(wt) => {
                let _ = writeln!(
                    out,
                    "{}",
                    serde_json::to_string_pretty(wt).unwrap_or_default()
                );
            }
            None => {
                let _ = writeln!(out, "(none)");
            }
        }
    }

    let _ = writeln!(out, "\n=== Effective ===");
    match &eff.flow_id {
        Some(f) => {
            let source = flow_source(user, global, worktree);
            let _ = writeln!(out, "flow_id = {f}   [from {source}]");
        }
        None => {
            let _ = writeln!(out, "flow_id = (none)");
        }
    }

    let _ = writeln!(out, "labels:");
    if eff.labels.is_empty() {
        let _ = writeln!(out, "  (none)");
    } else {
        for label in &eff.labels {
            let source = label_source(user, global, worktree, label);
            let _ = writeln!(out, "  {label}   [from {source}]");
        }
    }

    let _ = writeln!(out, "params:");
    if eff.params.is_empty() {
        let _ = writeln!(out, "  (none)");
    } else {
        for (k, v) in &eff.params {
            let source = param_source(user, global, worktree, k);
            let _ = writeln!(out, "  {k} = {v}   [from {source}]");
        }
    }

    out
}

/// `tracevault context clear` — remove/reset the context for the chosen scope.
///
/// Default scope:
/// - Linked worktree → DELETE `worktrees/<key>/context.json` if it exists.
///   A missing file means "no per-worktree context" (`load_worktree` returns
///   `None`, `show` prints "(none)"), so deleting preserves the absent-vs-empty
///   distinction that the rest of the design relies on.
/// - Primary checkout → write an empty (default) `context.json`.
///   The global file is the canonical one; there is no absent-vs-empty
///   distinction to preserve, and `show`/`effective` always read it.
///
/// `global = true` forces clearing the global file from any worktree.
/// `user = true` clears the resolved user-context file instead (wins over `global`);
/// like the global file, the user file is canonical, so it is overwritten with an
/// empty context rather than deleted.
pub fn run_clear(cwd: &Path, global: bool, user: bool) -> Result<(), Box<dyn std::error::Error>> {
    if user {
        let path = user_context_path(cwd)?;
        Context::default()
            .save_to(&path)
            .map_err(|e: io::Error| format!("failed to clear context: {e}"))?;
        println!("Context cleared.");
        return Ok(());
    }

    let paths = resolve_context_paths(cwd);

    // Require an initialised .tracevault/ before mutating.
    if !paths.tracevault_dir.exists() {
        return Err("no .tracevault/ found — run 'tracevault init' first".into());
    }

    let use_global = global || matches!(paths.scope, WorktreeScope::Primary);

    if use_global {
        // Global scope: overwrite with an empty context so `show`/`effective`
        // continue to find a well-formed file.
        Context::default()
            .save_global(&paths)
            .map_err(|e: io::Error| format!("failed to clear context: {e}"))?;
    } else {
        // Per-worktree scope: delete the file so that `load_worktree` returns
        // `None` and `show` prints "(none)" — preserving the absent-vs-empty
        // distinction.
        if let Some(wt_path) = paths.worktree_path() {
            match std::fs::remove_file(&wt_path) {
                Ok(()) => {}
                Err(e) if e.kind() == io::ErrorKind::NotFound => {} // already absent — fine
                Err(e) => return Err(format!("failed to clear context: {e}").into()),
            }
        }
    }

    println!("Context cleared.");
    Ok(())
}

/// `tracevault context source` — enable/disable or point the project's
/// user-level context source (`user_context` in `config.toml`).
///
/// The mode flags (`--enable`, `--disable`, `--path <p>`, `--default`) are
/// mutually exclusive — enforced at the CLI layer — so exactly one mode is set:
/// `--path` points at an explicit file; `--enable`/`--default` both mean
/// "enabled at the default path"; `--disable` turns it off. If none are given,
/// errors rather than silently no-op'ing.
pub fn run_source(
    cwd: &Path,
    enable: bool,
    disable: bool,
    path: Option<String>,
    default: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    let root = find_project_root(cwd)?;
    // Editing `user_context`, so distinguish a missing config from a malformed
    // one (same as `run_stream` / `user_context_path`) rather than reporting a
    // malformed file as "not found".
    let mut config = match TracevaultConfig::try_load(&root) {
        Ok(Some(cfg)) => cfg,
        Ok(None) => {
            return Err("no .tracevault/config.toml found — run 'tracevault init' first".into())
        }
        Err(e) => return Err(format!("malformed .tracevault/config.toml: {e}").into()),
    };

    config.user_context = if disable {
        UserContext::Toggle(false)
    } else if let Some(p) = path {
        UserContext::Path(p)
    } else if default || enable {
        UserContext::Toggle(true)
    } else {
        return Err("specify one of --enable, --disable, --path <file>, --default".into());
    };

    std::fs::write(TracevaultConfig::config_path(&root), config.to_toml())?;
    println!("Updated user_context.");
    Ok(())
}

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

    #[test]
    fn show_report_includes_user_section_and_repo_provenance() {
        let user = Context {
            flow_id: None,
            labels: vec![],
            params: BTreeMap::from([("owner".to_string(), Some("alice".to_string()))]),
        };
        let global = Context {
            flow_id: None,
            labels: vec![],
            params: BTreeMap::from([("owner".to_string(), Some("bob".to_string()))]),
        };
        let eff = Context::merge_layers(&[&user, &global]);

        let report = show_report(
            Some(&user),
            true,
            Some(Path::new("/tmp/user.json")),
            &global,
            None,
            false,
            &eff,
        );

        assert!(
            report.contains("=== User"),
            "missing User section: {report}"
        );
        assert!(
            report.contains("owner = bob   [from repo]"),
            "missing repo provenance for overridden param: {report}"
        );
    }

    /// Create a temp project with a `.tracevault/config.toml` for `run_source` tests.
    fn temp_project() -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        let tv_dir = dir.path().join(".tracevault");
        fs::create_dir_all(&tv_dir).unwrap();
        fs::write(
            tv_dir.join("config.toml"),
            TracevaultConfig::default().to_toml(),
        )
        .unwrap();
        dir
    }

    #[test]
    fn source_enable_disable_and_path() {
        let dir = temp_project();

        run_source(dir.path(), true, false, None, false).unwrap();
        let cfg = TracevaultConfig::load(dir.path()).unwrap();
        assert!(cfg.user_context.resolve().is_some());

        run_source(dir.path(), false, true, None, false).unwrap();
        let cfg = TracevaultConfig::load(dir.path()).unwrap();
        assert!(cfg.user_context.resolve().is_none());

        run_source(dir.path(), false, false, Some("/p".to_string()), false).unwrap();
        let cfg = TracevaultConfig::load(dir.path()).unwrap();
        assert_eq!(
            cfg.user_context.resolve(),
            Some(std::path::PathBuf::from("/p"))
        );
    }

    #[test]
    fn source_default_flag_enables() {
        let dir = temp_project();
        run_source(dir.path(), false, false, None, true).unwrap();
        let cfg = TracevaultConfig::load(dir.path()).unwrap();
        assert!(cfg.user_context.resolve().is_some());
    }

    #[test]
    fn source_no_flags_errors() {
        let dir = temp_project();
        let err = run_source(dir.path(), false, false, None, false).unwrap_err();
        assert!(err.to_string().contains("--enable"));
    }

    #[test]
    fn parse_param_valid() {
        assert_eq!(
            parse_param("key=value").unwrap(),
            ("key".to_string(), "value".to_string())
        );
    }

    #[test]
    fn parse_param_value_contains_equals() {
        // Only split on the FIRST =
        assert_eq!(
            parse_param("key=a=b").unwrap(),
            ("key".to_string(), "a=b".to_string())
        );
    }

    #[test]
    fn parse_param_no_equals_errors() {
        let err = parse_param("noequals").unwrap_err();
        assert!(err.to_string().contains("expected key=value"));
        assert!(err.to_string().contains("noequals"));
    }

    #[test]
    fn split_labels_comma_separated() {
        let result = split_labels(&["a,b,c".to_string(), "d".to_string()]);
        assert_eq!(result, vec!["a", "b", "c", "d"]);
    }

    /// Create a temp project whose `config.toml` points `user_context` at an
    /// explicit path outside the project (`uc_path`).
    fn temp_project_with_user_context(uc_path: &Path) -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        let tv_dir = dir.path().join(".tracevault");
        fs::create_dir_all(&tv_dir).unwrap();
        let config = TracevaultConfig {
            user_context: UserContext::Path(uc_path.display().to_string()),
            ..TracevaultConfig::default()
        };
        fs::write(tv_dir.join("config.toml"), config.to_toml()).unwrap();
        dir
    }

    #[test]
    fn set_user_writes_resolved_user_context_file() {
        let uc_dir = tempfile::tempdir().unwrap();
        let uc_path = uc_dir.path().join("uc.json");
        let project = temp_project_with_user_context(&uc_path);

        run_set(
            project.path(),
            None,
            vec!["team".to_string()],
            vec!["env=prod".to_string()],
            false,
            true,
        )
        .unwrap();

        let ctx = Context::load_from(&uc_path);
        assert_eq!(ctx.labels, vec!["team".to_string()]);
        assert_eq!(ctx.params.get("env"), Some(&Some("prod".to_string())));
    }

    #[test]
    fn update_user_merges_into_resolved_user_context_file() {
        let uc_dir = tempfile::tempdir().unwrap();
        let uc_path = uc_dir.path().join("uc.json");
        let project = temp_project_with_user_context(&uc_path);

        run_set(
            project.path(),
            None,
            vec!["team".to_string()],
            vec!["env=prod".to_string()],
            false,
            true,
        )
        .unwrap();

        run_update(
            project.path(),
            None,
            vec!["extra".to_string()],
            vec![],
            vec![],
            vec!["env".to_string()],
            false,
            true,
        )
        .unwrap();

        let ctx = Context::load_from(&uc_path);
        assert_eq!(ctx.labels, vec!["team".to_string(), "extra".to_string()]);
        assert_eq!(ctx.params.get("env"), Some(&None));
    }

    #[test]
    fn clear_user_resets_resolved_user_context_file() {
        let uc_dir = tempfile::tempdir().unwrap();
        let uc_path = uc_dir.path().join("uc.json");
        let project = temp_project_with_user_context(&uc_path);

        run_set(
            project.path(),
            None,
            vec!["team".to_string()],
            vec![],
            false,
            true,
        )
        .unwrap();

        run_clear(project.path(), false, true).unwrap();

        let ctx = Context::load_from(&uc_path);
        assert_eq!(ctx, Context::default());
    }
}