claudix 0.2.0

Local semantic search plugin for Claude Code
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
mod common {
    pub mod fixture {
        include!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/common/fixture.rs"
        ));
    }

    pub mod config_support {
        use claudix;

        include!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/tests/common/config_support.rs"
        ));
    }
}

use std::sync::Arc;

use claudix::search::SearchQuery;
use claudix::types::{Language, RelativePath};
use claudix::{Claudix, ClaudixError, IndexStats};
use common::config_support::{stub_config, stub_config_with_model};
use common::fixture::TestFixture;

// ── a ──────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn full_index_enumerates_and_persists_chunks() {
    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());

    let claudix = Claudix::new(fixture.root().to_path_buf(), Arc::new(stub_config())).await;
    assert!(claudix.is_ok(), "Claudix::new failed");
    let claudix = claudix.ok().unwrap_or_else(|| unreachable!());

    let stats = claudix.index_full(&mut ()).await;
    assert!(stats.is_ok(), "index_full failed: {stats:?}");
    let stats = stats.ok().unwrap_or_else(|| unreachable!());

    assert_eq!(
        stats,
        IndexStats {
            file_count: 2,
            chunk_count: 3,
        },
        "unexpected index stats: {stats:?}"
    );
}

// ── b ──────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn full_reindex_after_file_edit_reflects_changes() {
    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());

    let claudix = Claudix::new(fixture.root().to_path_buf(), Arc::new(stub_config())).await;
    assert!(claudix.is_ok(), "Claudix::new failed");
    let claudix = claudix.ok().unwrap_or_else(|| unreachable!());

    let first = claudix.index_full(&mut ()).await;
    assert!(first.is_ok(), "initial index_full failed: {first:?}");

    let write = std::fs::write(
        fixture.root().join("src/math.rs"),
        "pub fn square(x: i32) -> i32 { x * x }\n",
    );
    assert!(write.is_ok(), "write math.rs failed");

    let second = claudix.index_full(&mut ()).await;
    assert!(second.is_ok(), "reindex failed: {second:?}");

    let results = claudix
        .search(SearchQuery {
            query: "square".to_owned(),
            top_k: 10,
            language_filter: None,
            path_prefix: None,
            repos: Vec::new(),
        })
        .await;
    assert!(results.is_ok(), "search failed: {results:?}");
    let results = results.ok().unwrap_or_else(|| unreachable!()).results;

    let found = results
        .iter()
        .any(|r| r.chunk.name.as_deref() == Some("square"));
    assert!(
        found,
        "expected chunk named 'square' in results: {results:?}"
    );
}

// ── c ──────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn reindex_file_updates_target_preserves_others() {
    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());

    let claudix = Claudix::new(fixture.root().to_path_buf(), Arc::new(stub_config())).await;
    assert!(claudix.is_ok(), "Claudix::new failed");
    let claudix = claudix.ok().unwrap_or_else(|| unreachable!());

    let first = claudix.index_full(&mut ()).await;
    assert!(first.is_ok(), "initial index_full failed: {first:?}");

    let write = std::fs::write(
        fixture.root().join("src/math.rs"),
        "pub fn square(x: i32) -> i32 { x * x }\n",
    );
    assert!(write.is_ok(), "write math.rs failed");

    let reindex = claudix
        .reindex_file(&fixture.root().join("src/math.rs"))
        .await;
    assert!(reindex.is_ok(), "reindex_file failed: {reindex:?}");

    let greet_results = claudix
        .search(SearchQuery {
            query: "greet".to_owned(),
            top_k: 10,
            language_filter: None,
            path_prefix: None,
            repos: Vec::new(),
        })
        .await;
    assert!(
        greet_results.is_ok(),
        "search(greet) failed: {greet_results:?}"
    );
    let greet_results = greet_results.ok().unwrap_or_else(|| unreachable!()).results;
    assert!(
        !greet_results.is_empty(),
        "expected greet to still be found after reindex_file"
    );

    let square_results = claudix
        .search(SearchQuery {
            query: "square".to_owned(),
            top_k: 10,
            language_filter: None,
            path_prefix: None,
            repos: Vec::new(),
        })
        .await;
    assert!(
        square_results.is_ok(),
        "search(square) failed: {square_results:?}"
    );
    let square_results = square_results
        .ok()
        .unwrap_or_else(|| unreachable!())
        .results;
    assert!(
        square_results
            .iter()
            .any(|r| r.chunk.name.as_deref() == Some("square")),
        "expected 'square' chunk after reindex_file: {square_results:?}"
    );
}

// ── d ──────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn search_language_filter_excludes_other_languages() {
    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());

    let claudix = Claudix::new(fixture.root().to_path_buf(), Arc::new(stub_config())).await;
    assert!(claudix.is_ok(), "Claudix::new failed");
    let claudix = claudix.ok().unwrap_or_else(|| unreachable!());

    let index = claudix.index_full(&mut ()).await;
    assert!(index.is_ok(), "index_full failed: {index:?}");

    let results = claudix
        .search(SearchQuery {
            query: "greet add".to_owned(),
            top_k: 10,
            language_filter: Some(vec![Language::Rust]),
            path_prefix: None,
            repos: Vec::new(),
        })
        .await;
    assert!(
        results.is_ok(),
        "search with language filter failed: {results:?}"
    );
    let results = results.ok().unwrap_or_else(|| unreachable!()).results;

    for result in &results {
        assert_eq!(
            result.chunk.language,
            Language::Rust,
            "non-Rust chunk leaked through language filter: {:?}",
            result.chunk
        );
    }
}

// ── e ──────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn search_path_prefix_restricts_results() {
    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());

    let claudix = Claudix::new(fixture.root().to_path_buf(), Arc::new(stub_config())).await;
    assert!(claudix.is_ok(), "Claudix::new failed");
    let claudix = claudix.ok().unwrap_or_else(|| unreachable!());

    let index = claudix.index_full(&mut ()).await;
    assert!(index.is_ok(), "index_full failed: {index:?}");

    let results = claudix
        .search(SearchQuery {
            query: "add".to_owned(),
            top_k: 10,
            language_filter: None,
            path_prefix: Some(RelativePath::new("src/math")),
            repos: Vec::new(),
        })
        .await;
    assert!(
        results.is_ok(),
        "search with path prefix failed: {results:?}"
    );
    let results = results.ok().unwrap_or_else(|| unreachable!()).results;

    for result in &results {
        assert!(
            result
                .chunk
                .file_path
                .starts_with(&RelativePath::new("src/math")),
            "result outside path prefix: {:?}",
            result.chunk.file_path
        );
    }
}

// ── f ──────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn indexignore_excludes_skip_indexinclude_reinstates_reinclude() {
    let fixture = TestFixture::new("ignore_overrides");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());

    let claudix = Claudix::new(fixture.root().to_path_buf(), Arc::new(stub_config())).await;
    assert!(claudix.is_ok(), "Claudix::new failed");
    let claudix = claudix.ok().unwrap_or_else(|| unreachable!());

    let stats = claudix.index_full(&mut ()).await;
    assert!(stats.is_ok(), "index_full failed: {stats:?}");
    let stats = stats.ok().unwrap_or_else(|| unreachable!());

    assert_eq!(
        stats.file_count, 2,
        "expected 2 files (keep.rs + reinclude.rs), got {}: {stats:?}",
        stats.file_count
    );

    let results = claudix
        .search(SearchQuery {
            query: "reinclude".to_owned(),
            top_k: 10,
            language_filter: None,
            path_prefix: None,
            repos: Vec::new(),
        })
        .await;
    assert!(results.is_ok(), "search(reinclude) failed: {results:?}");
    let results = results.ok().unwrap_or_else(|| unreachable!()).results;

    let from_reinclude = results
        .iter()
        .any(|r| r.chunk.file_path.as_str().contains("reinclude"));
    assert!(
        from_reinclude,
        "expected at least one result from reinclude.rs: {results:?}"
    );
}

// ── f2 ─────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn indexinclude_reincludes_gitignored_docs_end_to_end() {
    let fixture = TestFixture::new("gitignored_docs");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());

    let claudix = Claudix::new(fixture.root().to_path_buf(), Arc::new(stub_config())).await;
    assert!(claudix.is_ok(), "Claudix::new failed");
    let claudix = claudix.ok().unwrap_or_else(|| unreachable!());

    let stats = claudix.index_full(&mut ()).await;
    assert!(stats.is_ok(), "index_full failed: {stats:?}");
    let stats = stats.ok().unwrap_or_else(|| unreachable!());
    // src/lib.rs + both gitignored docs, all chunked (docs as prose text).
    assert_eq!(
        stats.file_count, 3,
        "expected src/lib.rs + 2 re-included docs, got {}: {stats:?}",
        stats.file_count
    );
    assert!(stats.chunk_count >= 3, "docs produced no chunks: {stats:?}");

    let results = claudix
        .search(SearchQuery {
            query: "user guide documentation".to_owned(),
            top_k: 10,
            language_filter: None,
            path_prefix: None,
            repos: Vec::new(),
        })
        .await;
    assert!(results.is_ok(), "search failed: {results:?}");
    let results = results.ok().unwrap_or_else(|| unreachable!()).results;

    let from_docs = results
        .iter()
        .any(|r| r.chunk.file_path.as_str().starts_with("docs/"));
    assert!(from_docs, "expected a hit from the docs/ tree: {results:?}");
}

#[tokio::test]
async fn nested_indexinclude_reincludes_gitignored_docs_end_to_end() {
    let fixture = TestFixture::new("nested_indexinclude");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());

    let claudix = Claudix::new(fixture.root().to_path_buf(), Arc::new(stub_config())).await;
    assert!(claudix.is_ok(), "Claudix::new failed");
    let claudix = claudix.ok().unwrap_or_else(|| unreachable!());

    let stats = claudix.index_full(&mut ()).await;
    assert!(stats.is_ok(), "index_full failed: {stats:?}");
    let stats = stats.ok().unwrap_or_else(|| unreachable!());
    assert!(
        stats.chunk_count >= 3,
        "nested docs produced no chunks: {stats:?}"
    );

    let results = claudix
        .search(SearchQuery {
            query: "user guide documentation".to_owned(),
            top_k: 10,
            language_filter: None,
            path_prefix: None,
            repos: Vec::new(),
        })
        .await;
    assert!(results.is_ok(), "search failed: {results:?}");
    let results = results.ok().unwrap_or_else(|| unreachable!()).results;

    let from_docs = results
        .iter()
        .any(|r| r.chunk.file_path.as_str().starts_with("docs/"));
    assert!(
        from_docs,
        "expected a hit from the nested docs/ tree: {results:?}"
    );
}

// ── g ──────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn schema_model_mismatch_errors_on_open() {
    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());

    let claudix_v1 = Claudix::new(fixture.root().to_path_buf(), Arc::new(stub_config())).await;
    assert!(claudix_v1.is_ok(), "Claudix::new (v1) failed");
    let claudix_v1 = claudix_v1.ok().unwrap_or_else(|| unreachable!());

    let index = claudix_v1.index_full(&mut ()).await;
    assert!(index.is_ok(), "index_full failed: {index:?}");

    drop(claudix_v1);

    let claudix_v2 = Claudix::new(
        fixture.root().to_path_buf(),
        Arc::new(stub_config_with_model("stub-v2")),
    )
    .await;
    assert!(
        claudix_v2.is_err(),
        "expected EmbeddingModelMismatch error, but Claudix::new succeeded"
    );

    let error = claudix_v2.err().unwrap_or_else(|| unreachable!());
    assert!(
        matches!(error, ClaudixError::EmbeddingModelMismatch { .. }),
        "expected EmbeddingModelMismatch, got: {error:?}"
    );
}

// ── h ──────────────────────────────────────────────────────────────────────

#[test]
#[ignore = "spawns the compiled binary"]
fn hook_exits_zero_with_corrupt_manifest() {
    use assert_cmd::cargo::cargo_bin;
    use std::process::{Command, Stdio};

    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());
    let root = fixture.root();

    let config_contents = "[embedding]\nmodel = \"stub-v1\"\ndimensions = 8\n";
    let config_path = root.join("test-config.toml");
    let write_cfg = std::fs::write(&config_path, config_contents);
    assert!(write_cfg.is_ok(), "write test config failed");

    let mkdir = std::fs::create_dir_all(root.join(".claudix/index"));
    assert!(mkdir.is_ok(), "create index dir failed");

    // Manifest lives at <state_dir>/manifest.json (default: .claudix/manifest.json)
    let write_manifest = std::fs::write(root.join(".claudix/manifest.json"), "not valid json {{{{");
    assert!(write_manifest.is_ok(), "write corrupt manifest failed");

    let output = Command::new(cargo_bin("claudix"))
        .current_dir(root)
        .env("CLAUDE_PROJECT_DIR", root)
        .env("CIRRUS_CONFIG", &config_path)
        .args(["hook", "SessionStart"])
        .stdin(Stdio::null())
        .output();
    assert!(output.is_ok(), "failed to spawn claudix binary");
    let output = output.ok().unwrap_or_else(|| unreachable!());

    assert!(
        output.status.success(),
        "hook must exit 0 even with corrupt store, got: {}",
        output.status
    );
}

#[test]
fn index_progress_writes_status_to_stderr() {
    use assert_cmd::cargo::cargo_bin;
    use std::process::{Command, Stdio};

    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());
    let root = fixture.root();

    let config_path = root.join("test-config.toml");
    let write_cfg = std::fs::write(
        &config_path,
        "[embedding]\nmodel = \"stub-v1\"\ndimensions = 8\n",
    );
    assert!(write_cfg.is_ok(), "write test config failed");

    let output = Command::new(cargo_bin("claudix"))
        .current_dir(root)
        .env("CLAUDE_PROJECT_DIR", root)
        .env("CIRRUS_CONFIG", &config_path)
        .args(["index", "--progress"])
        .stdin(Stdio::null())
        .output();
    assert!(output.is_ok(), "failed to spawn claudix binary");
    let output = output.ok().unwrap_or_else(|| unreachable!());

    assert!(
        output.status.success(),
        "index --progress failed: {}",
        output.status
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(
        stdout.contains("indexed 2 files into 3 chunks"),
        "expected final summary on stdout, got: {stdout}"
    );
    assert!(
        stderr.contains("indexed src/lib.rs") && stderr.contains("indexed src/math.rs"),
        "expected indexed files on stderr, got: {stderr}"
    );
    assert!(
        stderr.contains("skipped test-config.toml: no indexable chunks"),
        "expected skipped reason on stderr, got: {stderr}"
    );
    assert!(
        !stdout.contains("indexed src/lib.rs"),
        "progress status leaked to stdout: {stdout}"
    );
}

#[test]
fn index_progress_reports_verified_files_after_reindex() {
    use assert_cmd::cargo::cargo_bin;
    use std::process::{Command, Stdio};

    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());
    let root = fixture.root();

    let config_path = root.join("test-config.toml");
    let write_cfg = std::fs::write(
        &config_path,
        "[embedding]\nmodel = \"stub-v1\"\ndimensions = 8\n",
    );
    assert!(write_cfg.is_ok(), "write test config failed");

    let first = Command::new(cargo_bin("claudix"))
        .current_dir(root)
        .env("CLAUDE_PROJECT_DIR", root)
        .env("CIRRUS_CONFIG", &config_path)
        .arg("index")
        .stdin(Stdio::null())
        .status();
    assert!(first.is_ok(), "failed to spawn claudix binary");
    assert!(first.ok().unwrap_or_else(|| unreachable!()).success());

    let output = Command::new(cargo_bin("claudix"))
        .current_dir(root)
        .env("CLAUDE_PROJECT_DIR", root)
        .env("CIRRUS_CONFIG", &config_path)
        .args(["index", "--progress"])
        .stdin(Stdio::null())
        .output();
    assert!(output.is_ok(), "failed to spawn claudix binary");
    let output = output.ok().unwrap_or_else(|| unreachable!());

    assert!(output.status.success(), "reindex failed: {}", output.status);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("verified src/lib.rs") && stderr.contains("verified src/math.rs"),
        "expected verified files on stderr, got: {stderr}"
    );
}

#[test]
fn index_without_progress_does_not_write_status() {
    use assert_cmd::cargo::cargo_bin;
    use std::process::{Command, Stdio};

    let fixture = TestFixture::new("small_rust");
    assert!(fixture.is_ok(), "fixture setup failed");
    let fixture = fixture.ok().unwrap_or_else(|| unreachable!());
    let root = fixture.root();

    let config_path = root.join("test-config.toml");
    let write_cfg = std::fs::write(
        &config_path,
        "[embedding]\nmodel = \"stub-v1\"\ndimensions = 8\n",
    );
    assert!(write_cfg.is_ok(), "write test config failed");

    let output = Command::new(cargo_bin("claudix"))
        .current_dir(root)
        .env("CLAUDE_PROJECT_DIR", root)
        .env("CIRRUS_CONFIG", &config_path)
        .arg("index")
        .stdin(Stdio::null())
        .output();
    assert!(output.is_ok(), "failed to spawn claudix binary");
    let output = output.ok().unwrap_or_else(|| unreachable!());

    assert!(output.status.success(), "index failed: {}", output.status);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(
        stdout.contains("indexed 2 files into 3 chunks"),
        "expected final summary on stdout, got: {stdout}"
    );
    assert!(
        !stderr.contains("indexed src/lib.rs"),
        "unexpected progress status on stderr: {stderr}"
    );
}