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
use std::fs;
use tempfile::TempDir;
use tracevault_cli::commands::init::ClaudeSettingsTarget;
use tracevault_cli::config::UserContext;

fn tmp_git_repo() -> TempDir {
    let tmp = TempDir::new().unwrap();
    fs::create_dir(tmp.path().join(".git")).unwrap();
    tmp
}

#[tokio::test]
async fn init_fails_without_git() {
    let tmp = TempDir::new().unwrap();
    let result = tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await;
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Not a git repository"));
}

#[tokio::test]
async fn init_creates_tracevault_config() {
    let tmp = tmp_git_repo();
    let config_path = tmp.path().join(".tracevault").join("config.toml");

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    assert!(config_path.exists());
    let content = fs::read_to_string(&config_path).unwrap();
    assert!(content.contains("claude-code"));
}

#[tokio::test]
async fn init_creates_directory_structure() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    assert!(tmp.path().join(".tracevault").exists());
    assert!(tmp.path().join(".tracevault/sessions").exists());
    assert!(tmp.path().join(".tracevault/cache").exists());

    let gitignore = fs::read_to_string(tmp.path().join(".gitignore")).unwrap();
    assert!(gitignore.contains(".tracevault/"));
    assert!(gitignore.contains(".claude/settings.json"));
    // Only the settings file init actually wrote is gitignored. init never
    // touched settings.local.json (Shared target), so it must not be added.
    assert!(!gitignore.contains(".claude/settings.local.json"));
}

#[tokio::test]
async fn init_installs_claude_hooks() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let settings_path = tmp.path().join(".claude/settings.json");
    assert!(settings_path.exists());

    let content = fs::read_to_string(&settings_path).unwrap();
    let settings: serde_json::Value = serde_json::from_str(&content).unwrap();
    let hooks = settings.get("hooks").unwrap();
    assert!(hooks.get("PreToolUse").is_some());
    assert!(hooks.get("PostToolUse").is_some());
    assert!(hooks.get("Notification").is_some());
}

#[tokio::test]
async fn init_installs_session_hooks() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let settings_path = tmp.path().join(".claude/settings.json");
    assert!(settings_path.exists());

    let content = fs::read_to_string(&settings_path).unwrap();
    let settings: serde_json::Value = serde_json::from_str(&content).unwrap();
    let hooks = settings.get("hooks").unwrap();

    // Per-repo init installs SessionStart hook (exports session ID and injects policies)
    assert!(
        hooks.get("SessionStart").is_some(),
        "missing SessionStart hook"
    );
    assert!(
        hooks["SessionStart"].is_array(),
        "SessionStart should be an array"
    );
    let session_start_entry = &hooks["SessionStart"][0];
    let session_start_cmd = session_start_entry
        .get("hooks")
        .and_then(|h| h.get(0))
        .and_then(|h| h.get("command"))
        .and_then(|c| c.as_str());
    assert_eq!(
        session_start_cmd,
        Some("tracevault session-start"),
        "SessionStart hook should run 'tracevault session-start'"
    );

    // Per-repo init installs UserPromptSubmit hook (re-injects policies when repo changes)
    assert!(
        hooks.get("UserPromptSubmit").is_some(),
        "missing UserPromptSubmit hook"
    );
    assert!(
        hooks["UserPromptSubmit"].is_array(),
        "UserPromptSubmit should be an array"
    );
    let user_prompt_entry = &hooks["UserPromptSubmit"][0];
    let user_prompt_cmd = user_prompt_entry
        .get("hooks")
        .and_then(|h| h.get(0))
        .and_then(|h| h.get("command"))
        .and_then(|c| c.as_str());
    assert_eq!(
        user_prompt_cmd,
        Some("tracevault user-prompt"),
        "UserPromptSubmit hook should run 'tracevault user-prompt'"
    );
}

#[tokio::test]
async fn init_merges_into_existing_settings() {
    let tmp = tmp_git_repo();

    // Pre-existing settings.json with other config
    let claude_dir = tmp.path().join(".claude");
    fs::create_dir_all(&claude_dir).unwrap();
    fs::write(claude_dir.join("settings.json"), r#"{"model": "opus"}"#).unwrap();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let content = fs::read_to_string(claude_dir.join("settings.json")).unwrap();
    let settings: serde_json::Value = serde_json::from_str(&content).unwrap();

    // Hooks were added
    assert!(settings.get("hooks").is_some());
    // Existing config preserved
    assert_eq!(settings.get("model").unwrap(), "opus");
}

#[test]
fn tracevault_hooks_has_pre_post_and_notification() {
    let hooks = tracevault_cli::commands::init::tracevault_hooks();
    assert!(hooks.get("PreToolUse").is_some());
    assert!(hooks.get("PostToolUse").is_some());
    assert!(hooks.get("Notification").is_some());
    assert!(hooks.get("SessionStart").is_some());
    assert!(hooks.get("UserPromptSubmit").is_some());
}

#[tokio::test]
async fn init_installs_git_pre_push_hook() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let hook_path = tmp.path().join(".git/hooks/pre-push");
    assert!(hook_path.exists());

    let content = fs::read_to_string(&hook_path).unwrap();
    assert!(content.contains("#!/bin/sh"));
    assert!(content.contains("# tracevault:enforce"));
    assert!(content.contains("tracevault sync"));
    assert!(content.contains("tracevault check"));
    assert!(!content.contains("tracevault push"));
}

#[tokio::test]
async fn init_preserves_existing_pre_push_hook() {
    let tmp = tmp_git_repo();

    // Create existing hook
    let hooks_dir = tmp.path().join(".git/hooks");
    fs::create_dir_all(&hooks_dir).unwrap();
    fs::write(
        hooks_dir.join("pre-push"),
        "#!/bin/sh\necho 'existing hook'\n",
    )
    .unwrap();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let content = fs::read_to_string(hooks_dir.join("pre-push")).unwrap();
    // Existing content preserved
    assert!(content.contains("echo 'existing hook'"));
    // Tracevault appended
    assert!(content.contains("# tracevault:enforce"));
    assert!(content.contains("tracevault check"));
    assert!(!content.contains("tracevault push"));
}

#[tokio::test]
async fn init_does_not_duplicate_hook_on_reinit() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();
    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let content = fs::read_to_string(tmp.path().join(".git/hooks/pre-push")).unwrap();
    let marker_count = content.matches("# tracevault:enforce").count();
    assert_eq!(
        marker_count, 1,
        "Marker should appear exactly once, found {marker_count}"
    );
}

#[tokio::test]
async fn init_installs_post_commit_hook() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let hook_path = tmp.path().join(".git/hooks/post-commit");
    assert!(hook_path.exists());

    let content = fs::read_to_string(&hook_path).unwrap();
    assert!(content.contains("#!/bin/sh"));
    assert!(content.contains("# tracevault:post-commit"));
    assert!(content.contains("tracevault commit-push 2>/dev/null &"));
}

#[tokio::test]
async fn init_does_not_duplicate_post_commit_hook_on_reinit() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();
    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let content = fs::read_to_string(tmp.path().join(".git/hooks/post-commit")).unwrap();
    let marker_count = content.matches("# tracevault:post-commit").count();
    assert_eq!(
        marker_count, 1,
        "Post-commit marker should appear exactly once, found {marker_count}"
    );
}

#[tokio::test]
async fn init_local_target_writes_to_settings_local_json() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Local),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let local_path = tmp.path().join(".claude/settings.local.json");
    let shared_path = tmp.path().join(".claude/settings.json");
    assert!(local_path.exists(), "settings.local.json should exist");
    assert!(
        !shared_path.exists(),
        "settings.json should not be created when local target chosen"
    );

    let content = fs::read_to_string(&local_path).unwrap();
    let settings: serde_json::Value = serde_json::from_str(&content).unwrap();
    assert!(settings.get("hooks").is_some());
}

#[tokio::test]
async fn init_local_target_gitignores_settings_local_json() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Local),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let gitignore = fs::read_to_string(tmp.path().join(".gitignore")).unwrap();
    assert!(gitignore.contains(".claude/settings.local.json"));
    // Only the chosen settings file is gitignored; init didn't touch
    // settings.json (Local target), so it must not be added.
    assert!(!gitignore.contains(".claude/settings.json"));
}

#[tokio::test]
async fn init_local_target_merges_into_existing_settings_local_json() {
    let tmp = tmp_git_repo();

    let claude_dir = tmp.path().join(".claude");
    fs::create_dir_all(&claude_dir).unwrap();
    fs::write(
        claude_dir.join("settings.local.json"),
        r#"{"model": "opus"}"#,
    )
    .unwrap();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Local),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let content = fs::read_to_string(claude_dir.join("settings.local.json")).unwrap();
    let settings: serde_json::Value = serde_json::from_str(&content).unwrap();
    assert!(settings.get("hooks").is_some());
    assert_eq!(settings.get("model").unwrap(), "opus");
}

#[tokio::test]
async fn init_writes_server_url_to_config() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        Some("https://tv.example.com"),
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let config_path = tmp.path().join(".tracevault/config.toml");
    let content = fs::read_to_string(&config_path).unwrap();
    assert!(content.contains("server_url = \"https://tv.example.com\""));
}

#[tokio::test]
async fn init_no_gitignore_skips_gitignore_update() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        true,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    // .gitignore should not exist (tmp_git_repo creates a bare repo without one)
    // or should not contain any tracevault entries if it already existed
    let gitignore_path = tmp.path().join(".gitignore");
    if gitignore_path.exists() {
        let content = fs::read_to_string(&gitignore_path).unwrap();
        assert!(
            !content.contains(".tracevault/"),
            ".gitignore should not have been modified with --no-gitignore"
        );
        assert!(!content.contains(".claude/settings.json"));
    }
    // But the rest of init should still work
    assert!(tmp.path().join(".tracevault").exists());
    assert!(tmp.path().join(".claude/settings.json").exists());
}

#[tokio::test]
async fn init_default_writes_user_context_true() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Toggle(true),
    )
    .await
    .unwrap();

    let config_path = tmp.path().join(".tracevault/config.toml");
    let content = fs::read_to_string(&config_path).unwrap();
    assert!(
        content.contains("user_context = true"),
        "expected user_context = true, got: {content}"
    );
}

#[tokio::test]
async fn init_explicit_user_context_path_written() {
    let tmp = tmp_git_repo();

    tracevault_cli::commands::init::init_in_directory(
        tmp.path(),
        None,
        Some(ClaudeSettingsTarget::Shared),
        false,
        UserContext::Path("/tmp/my-context.json".to_string()),
    )
    .await
    .unwrap();

    let config_path = tmp.path().join(".tracevault/config.toml");
    let content = fs::read_to_string(&config_path).unwrap();
    assert!(
        content.contains("user_context = \"/tmp/my-context.json\""),
        "expected explicit path, got: {content}"
    );
}