indra_db 0.1.10

A content-addressed graph database for versioned thoughts
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
//! CLI Integration Tests
//!
//! These tests verify that the CLI commands work correctly end-to-end.
//! They test the actual binary behavior, not just the library.
//!
//! Run with:
//! ```bash
//! cargo test --test cli_integration
//! ```

use std::path::PathBuf;
use std::process::Command;
use tempfile::tempdir;

/// Get the path to the built binary
fn indra_binary() -> PathBuf {
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    path.push("target");
    path.push("debug");
    path.push("indra");
    path
}

/// Run indra command and return (stdout, stderr, success)
fn run_indra(args: &[&str], db_path: &str) -> (String, String, bool) {
    let output = Command::new(indra_binary())
        .args(["-d", db_path, "-f", "json", "--embedder", "mock"])
        .args(args)
        .output()
        .expect("Failed to execute indra");

    (
        String::from_utf8_lossy(&output.stdout).to_string(),
        String::from_utf8_lossy(&output.stderr).to_string(),
        output.status.success(),
    )
}

// ============================================================================
// Database Initialization Tests
// ============================================================================

#[test]
fn test_cli_init_creates_database() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    let (stdout, _stderr, success) = run_indra(&["init"], db_str);

    assert!(success, "init should succeed");
    assert!(stdout.contains("status"), "should return JSON with status");
    assert!(stdout.contains("ok"), "status should be ok");
    assert!(db_path.exists(), ".indra file should be created");
}

#[test]
fn test_cli_default_path_is_dot_indra() {
    // Verify the help text shows .indra as default
    let output = Command::new(indra_binary())
        .args(["--help"])
        .output()
        .expect("Failed to execute indra");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("[default: .indra]"),
        "Default database path should be .indra, got: {}",
        stdout
    );
}

// ============================================================================
// Thought CRUD Tests
// ============================================================================

#[test]
fn test_cli_create_thought() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    // Init
    run_indra(&["init"], db_str);

    // Create thought
    let (stdout, _stderr, success) = run_indra(&["create", "Hello, world!"], db_str);

    assert!(success, "create should succeed");
    assert!(
        stdout.contains("\"status\":\"ok\""),
        "should return ok status"
    );
    assert!(stdout.contains("\"id\":"), "should return thought ID");
}

#[test]
fn test_cli_create_thought_with_custom_id() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);

    let (stdout, _stderr, success) = run_indra(
        &["create", "My custom thought", "--id", "my-custom-id"],
        db_str,
    );

    assert!(success, "create with custom ID should succeed");
    assert!(
        stdout.contains("\"id\":\"my-custom-id\""),
        "should use custom ID"
    );
}

#[test]
fn test_cli_get_thought() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["create", "Test content", "--id", "test-id"], db_str);

    let (stdout, _stderr, success) = run_indra(&["get", "test-id"], db_str);

    assert!(success, "get should succeed");
    assert!(
        stdout.contains("\"content\":\"Test content\""),
        "should return correct content"
    );
    assert!(
        stdout.contains("\"id\":\"test-id\""),
        "should return correct ID"
    );
}

#[test]
fn test_cli_get_nonexistent_thought() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);

    let (_stdout, _stderr, success) = run_indra(&["get", "nonexistent"], db_str);

    assert!(!success, "get nonexistent should fail");
}

#[test]
fn test_cli_list_thoughts() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["create", "First thought", "--id", "first"], db_str);
    run_indra(&["create", "Second thought", "--id", "second"], db_str);

    let (stdout, _stderr, success) = run_indra(&["list"], db_str);

    assert!(success, "list should succeed");
    assert!(stdout.contains("\"count\":2"), "should have 2 thoughts");
    assert!(
        stdout.contains("First thought"),
        "should contain first thought"
    );
    assert!(
        stdout.contains("Second thought"),
        "should contain second thought"
    );
}

#[test]
fn test_cli_update_thought() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(
        &["create", "Original content", "--id", "update-test"],
        db_str,
    );

    let (stdout, _stderr, success) =
        run_indra(&["update", "update-test", "Updated content"], db_str);

    assert!(success, "update should succeed");
    assert!(stdout.contains("\"status\":\"ok\""), "should return ok");

    // Verify the update
    let (stdout, _, _) = run_indra(&["get", "update-test"], db_str);
    assert!(
        stdout.contains("Updated content"),
        "content should be updated"
    );
}

#[test]
fn test_cli_delete_thought() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["create", "To be deleted", "--id", "delete-me"], db_str);

    // Verify it exists
    let (_stdout, _, success) = run_indra(&["get", "delete-me"], db_str);
    assert!(success, "thought should exist before delete");

    // Delete
    let (stdout, _stderr, success) = run_indra(&["delete", "delete-me"], db_str);
    assert!(success, "delete should succeed");
    assert!(stdout.contains("\"status\":\"ok\""), "should return ok");

    // Verify it's gone
    let (_, _, success) = run_indra(&["get", "delete-me"], db_str);
    assert!(!success, "thought should not exist after delete");
}

// ============================================================================
// Auto-commit Tests (Critical for MCP integration)
// ============================================================================

#[test]
fn test_cli_auto_commits_by_default() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(
        &["create", "Auto-committed thought", "--id", "auto-test"],
        db_str,
    );

    // Check that a commit was created
    let (stdout, _stderr, success) = run_indra(&["log"], db_str);

    assert!(success, "log should succeed");
    assert!(stdout.contains("\"count\":1"), "should have 1 commit");
    assert!(
        stdout.contains("Auto-commit"),
        "commit message should indicate auto-commit"
    );
}

#[test]
fn test_cli_no_auto_commit_flag() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    // Init first (without no-auto-commit since init doesn't support it)
    run_indra(&["init"], db_str);

    // Create with --no-auto-commit
    let output = Command::new(indra_binary())
        .args([
            "-d",
            db_str,
            "-f",
            "json",
            "--embedder",
            "mock",
            "--no-auto-commit",
        ])
        .args(["create", "Not auto-committed"])
        .output()
        .expect("Failed to execute indra");

    assert!(output.status.success(), "create should succeed");

    // Check that no commit was created (only the init creates the file structure)
    let (stdout, _stderr, _) = run_indra(&["log"], db_str);

    // With no-auto-commit, there should be 0 commits
    assert!(
        stdout.contains("\"count\":0") || stdout.contains("\"commits\":[]"),
        "should have 0 commits when using --no-auto-commit, got: {}",
        stdout
    );
}

// ============================================================================
// Persistence Tests (Critical - this was the bug!)
// ============================================================================

#[test]
fn test_cli_data_persists_across_invocations() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    // First session: create data
    run_indra(&["init"], db_str);
    run_indra(
        &["create", "Persistent thought 1", "--id", "persist-1"],
        db_str,
    );
    run_indra(
        &["create", "Persistent thought 2", "--id", "persist-2"],
        db_str,
    );

    // Simulate "new session" by just running more commands
    // (In reality, the MCP spawns a new process for each command)

    // Second "session": verify data exists
    let (stdout, _stderr, success) = run_indra(&["list"], db_str);

    assert!(success, "list should succeed in second session");
    assert!(
        stdout.contains("\"count\":2"),
        "should still have 2 thoughts"
    );
    assert!(stdout.contains("persist-1"), "should have first thought");
    assert!(stdout.contains("persist-2"), "should have second thought");
}

#[test]
fn test_cli_commits_persist_across_invocations() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    // Create multiple commits
    run_indra(&["init"], db_str);
    run_indra(&["create", "First", "--id", "first"], db_str);
    run_indra(&["create", "Second", "--id", "second"], db_str);
    run_indra(&["create", "Third", "--id", "third"], db_str);

    // Verify commit history
    let (stdout, _stderr, success) = run_indra(&["log"], db_str);

    assert!(success, "log should succeed");
    assert!(
        stdout.contains("\"count\":3"),
        "should have 3 commits (one per create)"
    );
}

// ============================================================================
// Search Tests
// ============================================================================

#[test]
fn test_cli_search_basic() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["create", "The cat sat on the mat", "--id", "cat"], db_str);
    run_indra(
        &["create", "Dogs love to play fetch", "--id", "dog"],
        db_str,
    );

    // Note: Without HF embeddings, search uses mock embedder which does keyword matching
    let (stdout, _stderr, success) = run_indra(&["search", "cat"], db_str);

    assert!(success, "search should succeed");
    assert!(stdout.contains("\"count\":"), "should return count");
}

// ============================================================================
// Status Tests
// ============================================================================

#[test]
fn test_cli_status() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["create", "A thought"], db_str);

    let (stdout, _stderr, success) = run_indra(&["status"], db_str);

    assert!(success, "status should succeed");
    assert!(
        stdout.contains("\"branch\":\"main\""),
        "should be on main branch"
    );
    assert!(
        stdout.contains("\"dirty\":false"),
        "should not be dirty after auto-commit"
    );
}

// ============================================================================
// Branch Tests
// ============================================================================

#[test]
fn test_cli_branches() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["create", "Initial thought"], db_str);

    // Create branch
    let (_stdout, _stderr, success) = run_indra(&["branch", "feature"], db_str);
    assert!(success, "branch creation should succeed");

    // List branches
    let (stdout, _stderr, success) = run_indra(&["branches"], db_str);
    assert!(success, "branches list should succeed");
    assert!(stdout.contains("main"), "should have main branch");
    assert!(stdout.contains("feature"), "should have feature branch");
}

// ============================================================================
// Edge Cases
// ============================================================================

#[test]
fn test_cli_empty_content() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);

    // Empty content should still work
    let (stdout, _stderr, success) = run_indra(&["create", ""], db_str);
    assert!(success, "empty content should be allowed");
    assert!(stdout.contains("\"status\":\"ok\""));
}

#[test]
fn test_cli_special_characters_in_content() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);

    let special_content = r#"Special chars: "quotes", 'apostrophes', \backslash, emoji 🎉"#;
    let (stdout, _stderr, success) = run_indra(&["create", special_content], db_str);

    assert!(success, "special characters should be handled");
    assert!(stdout.contains("\"status\":\"ok\""));
}

#[test]
fn test_cli_unicode_content() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);

    let unicode_content = "Unicode: 日本語 中文 한국어 العربية";
    let (_stdout, _stderr, success) =
        run_indra(&["create", unicode_content, "--id", "unicode"], db_str);

    assert!(success, "unicode should be handled");

    // Verify retrieval
    let (stdout, _, success) = run_indra(&["get", "unicode"], db_str);
    assert!(success);
    assert!(stdout.contains("日本語"), "unicode should be preserved");
}

#[test]
fn test_cli_very_long_content() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);

    // Create a long string (10KB)
    let long_content = "x".repeat(10_000);
    let (stdout, _stderr, success) = run_indra(&["create", &long_content], db_str);

    assert!(success, "long content should be handled");
    assert!(stdout.contains("\"status\":\"ok\""));
}

// ============================================================================
// Remote Management Tests
// ============================================================================

#[test]
fn test_cli_remote_add_and_list() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);

    // Add a remote
    let (stdout, _stderr, success) = run_indra(&["remote", "add", "origin", "user/repo"], db_str);
    assert!(success, "remote add should succeed");
    assert!(stdout.contains("\"status\":\"ok\""));

    // List remotes
    let (stdout, _stderr, success) = run_indra(&["remote", "list"], db_str);
    assert!(success, "remote list should succeed");
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(json["count"], 1);
    assert_eq!(json["default"], "origin");
    assert_eq!(json["remotes"][0]["name"], "origin");
    assert_eq!(json["remotes"][0]["url"], "user/repo");
}

#[test]
fn test_cli_remote_show() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["remote", "add", "origin", "kojinglick/my-db"], db_str);

    let (stdout, _stderr, success) = run_indra(&["remote", "show", "origin"], db_str);
    assert!(success, "remote show should succeed");

    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(json["name"], "origin");
    assert_eq!(json["url"], "kojinglick/my-db");
    assert_eq!(json["owner"], "kojinglick");
    assert_eq!(json["repo"], "my-db");
}

#[test]
fn test_cli_remote_set_url() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["remote", "add", "origin", "user/old-repo"], db_str);

    // Update URL
    let (stdout, _stderr, success) =
        run_indra(&["remote", "set-url", "origin", "user/new-repo"], db_str);
    assert!(success, "remote set-url should succeed");
    assert!(stdout.contains("\"status\":\"ok\""));

    // Verify change
    let (stdout, _stderr, success) = run_indra(&["remote", "show", "origin"], db_str);
    assert!(success);
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(json["url"], "user/new-repo");
}

#[test]
fn test_cli_remote_remove() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["remote", "add", "origin", "user/repo"], db_str);
    run_indra(&["remote", "add", "upstream", "other/repo"], db_str);

    // Remove origin
    let (stdout, _stderr, success) = run_indra(&["remote", "remove", "origin"], db_str);
    assert!(success, "remote remove should succeed");
    assert!(stdout.contains("\"status\":\"ok\""));

    // Verify removal
    let (stdout, _stderr, success) = run_indra(&["remote", "list"], db_str);
    assert!(success);
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(json["count"], 1);
    assert_eq!(json["remotes"][0]["name"], "upstream");
}

#[test]
fn test_cli_remote_duplicate_error() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["remote", "add", "origin", "user/repo"], db_str);

    // Try to add duplicate
    let (_stdout, _stderr, success) = run_indra(&["remote", "add", "origin", "other/repo"], db_str);
    assert!(!success, "adding duplicate remote should fail");
}

#[test]
fn test_cli_push_requires_remote() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);

    // Push without remote should fail
    let (_stdout, _stderr, success) = run_indra(&["push"], db_str);
    assert!(!success, "push without remote should fail");
}

#[test]
fn test_cli_push_with_remote() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["remote", "add", "origin", "user/repo"], db_str);
    run_indra(&["create", "test thought"], db_str);

    // Push will attempt to connect to API and fail (no real endpoint)
    // This is expected behavior - the command runs but network fails
    let (stdout, _stderr, _success) = run_indra(&["push"], db_str);
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    // Should have remote info in the response regardless of success/failure
    assert_eq!(json["remote"], "origin");
    // Status will be "error" since there's no real API to push to
    assert!(
        json["status"] == "pending" || json["status"] == "error",
        "Expected 'pending' or 'error' status, got: {}",
        json["status"]
    );
}

#[test]
fn test_cli_status_shows_remotes() {
    let dir = tempdir().unwrap();
    let db_path = dir.path().join(".indra");
    let db_str = db_path.to_str().unwrap();

    run_indra(&["init"], db_str);
    run_indra(&["remote", "add", "origin", "user/repo"], db_str);

    let (stdout, _stderr, success) = run_indra(&["status"], db_str);
    assert!(success, "status should succeed");

    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(json["remotes"].as_array().unwrap().len(), 1);
    assert_eq!(json["remotes"][0]["name"], "origin");
}