capsula 0.12.0

The command line interface for Capsula.
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
//! Integration tests for the capsula CLI tool.
#![expect(
    clippy::unwrap_used,
    reason = "Test code doesn't need production-level error handling"
)]

use assert_cmd::cargo::cargo_bin_cmd;
use predicates::prelude::*;
use serde::Deserialize;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

/// Helper to create a minimal capsula.toml config
fn create_test_config(dir: &TempDir, vault_name: &str) -> PathBuf {
    let config_path = dir.path().join("capsula.toml");
    let config_content = format!(
        r#"
[vault]
name = "{vault_name}"

[[pre-run.hooks]]
id = "capture-cwd"
"#,
    );
    fs::write(&config_path, config_content).unwrap();
    config_path
}

/// Helper to create a capsula.toml config with both pre-run and post-run hooks
fn create_test_config_with_post_run(dir: &TempDir, vault_name: &str) -> PathBuf {
    let config_path = dir.path().join("capsula.toml");
    let config_content = format!(
        r#"
[vault]
name = "{vault_name}"

[[pre-run.hooks]]
id = "capture-cwd"

[[post-run.hooks]]
id = "capture-cwd"
"#,
    );
    fs::write(&config_path, config_content).unwrap();
    config_path
}

#[derive(Deserialize)]
struct TestRunMetadata {
    name: String,
}

fn find_first_run(vault_dir: &std::path::Path) -> (PathBuf, String) {
    let date_dirs: Vec<_> = fs::read_dir(vault_dir)
        .unwrap()
        .filter_map(Result::ok)
        .filter(|e| e.path().is_dir())
        .collect();

    for date_dir in date_dirs {
        let run_dirs: Vec<_> = fs::read_dir(date_dir.path())
            .unwrap()
            .filter_map(Result::ok)
            .filter(|e| e.path().is_dir())
            .collect();

        for run_dir in run_dirs {
            let run_path = run_dir.path();
            let metadata_path = run_path.join("_capsula").join("metadata.json");
            if !metadata_path.exists() {
                continue;
            }

            let content = fs::read_to_string(&metadata_path).unwrap();
            let metadata: TestRunMetadata = serde_json::from_str(&content).unwrap();
            return (run_path, metadata.name);
        }
    }

    panic!("No run directory found");
}

#[test]
fn test_capsula_run_creates_run_directory() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run")
        .arg("echo")
        .arg("hello");

    cmd.assert().success();

    // Check that the vault directory was created
    let vault_dir = temp_dir.path().join(".capsula").join("test-vault");
    assert!(vault_dir.exists(), "Vault directory should exist");

    // Check that at least one run directory exists
    let date_dirs: Vec<_> = fs::read_dir(&vault_dir)
        .unwrap()
        .filter_map(Result::ok)
        .filter(|e| e.path().is_dir())
        .collect();

    assert!(
        !date_dirs.is_empty(),
        "Should have at least one date directory"
    );

    // Find a run directory
    for date_dir in date_dirs {
        let run_dirs: Vec<_> = fs::read_dir(date_dir.path())
            .unwrap()
            .filter_map(Result::ok)
            .filter(|e| e.path().is_dir())
            .collect();

        if !run_dirs.is_empty() {
            let run_dir = run_dirs[0].path();
            let capsula_dir = run_dir.join("_capsula");
            assert!(capsula_dir.exists(), "_capsula directory should exist");
            assert!(
                capsula_dir.join("metadata.json").exists(),
                "metadata.json should exist"
            );
            assert!(
                capsula_dir.join("pre-run.json").exists(),
                "pre-run.json should exist"
            );
            assert!(
                capsula_dir.join("command.json").exists(),
                "command.json should exist"
            );
            return;
        }
    }

    panic!("No run directory found");
}

#[test]
fn test_capsula_run_dir_prints_path() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run")
        .arg("echo")
        .arg("hello");

    cmd.assert().success();

    let vault_dir = temp_dir.path().join(".capsula").join("test-vault");
    let (run_dir, run_name) = find_first_run(&vault_dir);

    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run-dir")
        .arg(&run_name);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains(run_dir.to_string_lossy().as_ref()));
}

#[test]
fn test_capsula_list_shows_runs() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    // First, create a run
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run")
        .arg("echo")
        .arg("test");

    cmd.assert().success();

    // Now list runs
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("list");

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("TIMESTAMP"))
        .stdout(predicate::str::contains("NAME"))
        .stdout(predicate::str::contains("COMMAND"));
}

#[test]
fn test_capsula_show_displays_run_details() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    // Create a run
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run")
        .arg("echo")
        .arg("hello-show");

    cmd.assert().success();

    // Find the run name
    let vault_dir = temp_dir.path().join(".capsula").join("test-vault");
    let (_, run_name) = find_first_run(&vault_dir);

    // Show the run
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("show")
        .arg(&run_name);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains(format!("Run:       {run_name}")))
        .stdout(predicate::str::contains("ID:"))
        .stdout(predicate::str::contains("Timestamp:"))
        .stdout(predicate::str::contains("Result:    exit 0"))
        .stdout(predicate::str::contains("Pre-run hooks:"))
        .stdout(predicate::str::contains("[ok]     capture-cwd"));
}

#[test]
fn test_capsula_show_json_output() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    // Create a run
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run")
        .arg("echo")
        .arg("hello-json");

    cmd.assert().success();

    let vault_dir = temp_dir.path().join(".capsula").join("test-vault");
    let (_, run_name) = find_first_run(&vault_dir);

    // Show the run with --json
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("show")
        .arg(&run_name)
        .arg("--json");

    let output = cmd.assert().success();
    let stdout = String::from_utf8(output.get_output().stdout.clone()).unwrap();

    // Verify it's valid JSON with expected structure
    let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert!(parsed.get("metadata").is_some(), "should have metadata key");
    assert!(parsed.get("pre_run").is_some(), "should have pre_run key");
    assert!(parsed.get("command").is_some(), "should have command key");
    assert_eq!(
        parsed["metadata"]["name"].as_str().unwrap(),
        run_name,
        "metadata.name should match run name"
    );
    assert_eq!(
        parsed["command"]["exit_code"].as_i64().unwrap(),
        0,
        "exit code should be 0"
    );
}

#[test]
fn test_capsula_push_requires_server_url() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    // First, create a run
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run")
        .arg("echo")
        .arg("test");

    cmd.assert().success();

    // Try to push without server URL (should fail)
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("push")
        .arg("01234567890123456789012345"); // Some fake ID

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("Server URL not specified"));
}

#[test]
fn test_capsula_vaults_list_requires_server_url() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    // Try to list vaults without server URL (should fail)
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("vaults")
        .arg("list");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("Server URL not specified"));
}

#[test]
fn test_capsula_run_with_nonexistent_config() {
    let temp_dir = TempDir::new().unwrap();

    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg("nonexistent.toml")
        .arg("run")
        .arg("echo")
        .arg("test");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("Configuration file not found"));
}

#[test]
fn test_capsula_run_without_command() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run");

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("No command specified"));
}

#[test]
fn test_capsula_config_with_server_url() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("capsula.toml");
    let config_content = r#"
[vault]
name = "test-vault"

server = "http://localhost:8500"

[[pre-run.hooks]]
id = "capture-cwd"
"#;
    fs::write(&config_path, config_content).unwrap();

    // Create a run to ensure config is valid
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run")
        .arg("echo")
        .arg("test");

    cmd.assert().success();
}

#[test]
fn test_run_start_creates_directory_and_pre_run() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config_with_post_run(&temp_dir, "test-vault");

    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run-start");

    let output = cmd.assert().success();
    let stdout = String::from_utf8(output.get_output().stdout.clone()).unwrap();
    let run_name = stdout.trim();
    assert!(
        !run_name.is_empty(),
        "run-start should print run name to stdout"
    );

    let vault_dir = temp_dir.path().join(".capsula").join("test-vault");
    assert!(vault_dir.exists(), "Vault directory should exist");

    let (run_dir, found_name) = find_first_run(&vault_dir);
    assert_eq!(found_name, run_name);

    let capsula_dir = run_dir.join("_capsula");
    assert!(
        capsula_dir.join("metadata.json").exists(),
        "metadata.json should exist"
    );
    assert!(
        capsula_dir.join("pre-run.json").exists(),
        "pre-run.json should exist"
    );
    assert!(
        !capsula_dir.join("command.json").exists(),
        "command.json should NOT exist"
    );
    assert!(
        !capsula_dir.join("post-run.json").exists(),
        "post-run.json should NOT exist"
    );
}

#[test]
fn test_run_start_prints_name_to_stdout() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run-start");

    let output = cmd.assert().success();
    let stdout = String::from_utf8(output.get_output().stdout.clone()).unwrap();
    let lines: Vec<&str> = stdout.trim().lines().collect();
    assert_eq!(
        lines.len(),
        1,
        "stdout should contain exactly one line (the run name)"
    );
    // Run names from the `names` crate are two hyphenated words
    assert!(
        lines[0].contains('-'),
        "Run name should be hyphenated (e.g., 'happy-river')"
    );
}

#[test]
fn test_run_end_creates_post_run() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config_with_post_run(&temp_dir, "test-vault");

    // First, start a run
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run-start");

    let output = cmd.assert().success();
    let run_name = String::from_utf8(output.get_output().stdout.clone())
        .unwrap()
        .trim()
        .to_string();

    // Now end the run
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run-end")
        .arg(&run_name);

    cmd.assert().success();

    let vault_dir = temp_dir.path().join(".capsula").join("test-vault");
    let (run_dir, _) = find_first_run(&vault_dir);
    let capsula_dir = run_dir.join("_capsula");
    assert!(
        capsula_dir.join("post-run.json").exists(),
        "post-run.json should exist after run-end"
    );
}

#[test]
fn test_run_end_nonexistent_name_fails() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config(&temp_dir, "test-vault");

    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run-end")
        .arg("nonexistent-run");

    cmd.assert().failure();
}

#[test]
fn test_run_end_already_finalized_fails() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = create_test_config_with_post_run(&temp_dir, "test-vault");

    // Start a run
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run-start");

    let output = cmd.assert().success();
    let run_name = String::from_utf8(output.get_output().stdout.clone())
        .unwrap()
        .trim()
        .to_string();

    // End the run (first time - should succeed)
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run-end")
        .arg(&run_name);

    cmd.assert().success();

    // End the run again (should fail)
    let mut cmd = cargo_bin_cmd!("capsula");
    cmd.current_dir(temp_dir.path())
        .arg("--config")
        .arg(&config_path)
        .arg("run-end")
        .arg(&run_name);

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("already been finalized"));
}