leindex 1.8.3

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
//! Cargo install layout tests (VAL-CARGO-001..013).
//!
//! These tests statically verify that the root `Cargo.toml` is configured so
//! `cargo install leindex` (and the `--features onnx` / `--features
//! onnx-migraphx` variants) produces correct results without requiring ORT
//! at build time.
//!
//! Key invariants:
//!   * `cargo install leindex --features onnx` installs BOTH `leindex` and
//!     `leindex-embed` binaries (VAL-CARGO-002/005).
//!   * `cargo install leindex` (default features) installs `leindex`
//!     (VAL-CARGO-001/004).
//!   * The worker binary is gated by `required-features = ["onnx"]` so the
//!     default install is unaffected.
//!   * The `onnx` feature propagates to `leindex-embed/onnx` so the installed
//!     worker has real ONNX inference support (not a dummy fallback).
//!   * No `ORT_LIB_PATH` / `ORT_PREFER_DYNAMIC_LINK` entries remain in
//!     `.cargo/config.toml` (VAL-ORT-003 / VAL-DOCS-008/011).
//!   * No `$ORIGIN` rpath in build scripts (VAL-RELEASE-012).
//!   * No `ort-lib/` directory exists (VAL-ORT-004 / VAL-DOCS-009/010).
//!   * The worker binary exposes `--version` (VAL-CARGO-005 evidence).
//!
//! Because `cargo install` cannot run inside `cargo test`, we verify the
//! Cargo.toml metadata that drives the install layout. The end-to-end
//! install was validated manually during feature development.

#![cfg(test)]

use std::path::PathBuf;

/// Return the absolute path to the repo root.
fn repo_root() -> PathBuf {
    let dir = env!("CARGO_MANIFEST_DIR");
    PathBuf::from(dir)
}

/// Read the root Cargo.toml as a string.
fn root_cargo_toml() -> String {
    let path = repo_root().join("Cargo.toml");
    std::fs::read_to_string(&path)
        .unwrap_or_else(|e| panic!("failed to read {}: {e}", path.display()))
}

/// Read the leindex-embed subcrate Cargo.toml as a string.
fn embed_cargo_toml() -> String {
    let path = repo_root()
        .join("crates")
        .join("leindex-embed")
        .join("Cargo.toml");
    std::fs::read_to_string(&path)
        .unwrap_or_else(|e| panic!("failed to read {}: {e}", path.display()))
}

/// Read the root .cargo/config.toml.
fn cargo_config_toml() -> String {
    let path = repo_root().join(".cargo").join("config.toml");
    std::fs::read_to_string(&path).unwrap_or_default()
}

/// Extract all `[[bin]]` blocks from a Cargo.toml string, correctly
/// stopping at the next `[[...]]` or `[...]` table header. Each returned
/// string contains all key=value lines belonging to one block.
fn bin_blocks(cargo_toml: &str) -> Vec<String> {
    let mut blocks = Vec::new();
    let mut current: Option<Vec<&str>> = None;
    for line in cargo_toml.lines() {
        let t = line.trim();
        if t == "[[bin]]" {
            if let Some(b) = current.take() {
                blocks.push(b.join("\n"));
            }
            current = Some(Vec::new());
        } else if t.starts_with("[[")
            || (t.starts_with('[') && !t.starts_with("[[") && current.is_some())
        {
            // New table header: close current block
            if let Some(b) = current.take() {
                blocks.push(b.join("\n"));
            }
        } else if let Some(ref mut b) = current {
            if !t.is_empty() && !t.starts_with('#') {
                b.push(line);
            }
        }
    }
    if let Some(b) = current {
        blocks.push(b.join("\n"));
    }
    blocks
}

// ============================================================================
// VAL-CARGO-001/002/003: Both main binary and worker binary declared
// ============================================================================

mod binary_targets {
    use super::*;

    /// VAL-CARGO-002/005: The root package declares a `leindex-embed`
    /// `[[bin]]` target so `cargo install leindex --features onnx` installs
    /// both binaries. Without this, `cargo install` only installs binaries
    /// from the root crate's own `[[bin]]` targets (not from subcrates).
    #[test]
    fn root_declares_leindex_embed_bin_target() {
        let toml = root_cargo_toml();
        let bins = bin_blocks(&toml);

        let embed_bin = bins.iter().find(|b| b.contains("name = \"leindex-embed\""));
        assert!(
            embed_bin.is_some(),
            "Root Cargo.toml MUST declare a [[bin]] target named 'leindex-embed' \
             so cargo install co-installs the worker. Found bin blocks: {:?}",
            bins.iter()
                .map(|b| b.lines().next().unwrap_or(""))
                .collect::<Vec<_>>()
        );

        let embed = embed_bin.unwrap();
        // The path should point to the root src/bin/ wrapper
        assert!(
            embed.contains("path = \"src/bin/leindex-embed.rs\""),
            "leindex-embed bin target should point to src/bin/leindex-embed.rs, got: {}",
            embed
        );
        // Must be gated by required-features = ["onnx"]
        assert!(
            embed.contains("required-features = [\"onnx\"]"),
            "leindex-embed bin target must have required-features = [\"onnx\"], got: {}",
            embed
        );
    }

    /// VAL-CARGO-001/004: The root package also declares the `leindex`
    /// main binary.
    #[test]
    fn root_declares_leindex_bin_target() {
        let toml = root_cargo_toml();
        let bins = bin_blocks(&toml);

        let main_bin = bins.iter().find(|b| b.contains("name = \"leindex\""));
        assert!(
            main_bin.is_some(),
            "Root Cargo.toml MUST declare [[bin]] 'leindex'. Found: {:?}",
            bins.iter()
                .map(|b| b.lines().next().unwrap_or(""))
                .collect::<Vec<_>>()
        );
    }

    /// VAL-CARGO-005: There should be exactly two bin targets in the root.
    #[test]
    fn root_has_exactly_two_bin_targets() {
        let toml = root_cargo_toml();
        let bins = bin_blocks(&toml);
        assert_eq!(
            bins.len(),
            2,
            "Expected exactly 2 [[bin]] targets (leindex, leindex-embed), got {}: {:?}",
            bins.len(),
            bins.iter()
                .map(|b| b.lines().next().unwrap_or(""))
                .collect::<Vec<_>>()
        );
    }

    /// VAL-CARGO-002: The root wrapper source exists and calls the shared run fn.
    #[test]
    fn root_embed_wrapper_source_exists() {
        let path = repo_root().join("src").join("bin").join("leindex-embed.rs");
        assert!(path.exists(), "src/bin/leindex-embed.rs must exist");
        let src = std::fs::read_to_string(&path)
            .unwrap_or_else(|e| panic!("failed to read {}: {e}", path.display()));
        assert!(
            src.contains("leindex_embed::worker_main::run"),
            "Wrapper must call leindex_embed::worker_main::run()"
        );
    }

    /// VAL-CARGO-005: The shared worker_main module exists in leindex-embed.
    #[test]
    fn worker_main_module_exists() {
        let path = repo_root()
            .join("crates")
            .join("leindex-embed")
            .join("src")
            .join("worker_main.rs");
        assert!(
            path.exists(),
            "crates/leindex-embed/src/worker_main.rs must exist"
        );
        let src = std::fs::read_to_string(&path)
            .unwrap_or_else(|e| panic!("failed to read {}: {e}", path.display()));
        assert!(src.contains("pub fn run()"));
        assert!(
            src.contains("--version"),
            "worker_main must handle --version"
        );
    }

    /// VAL-CARGO-005: The lib.rs exports worker_main.
    #[test]
    fn lib_exports_worker_main() {
        let path = repo_root()
            .join("crates")
            .join("leindex-embed")
            .join("src")
            .join("lib.rs");
        let src = std::fs::read_to_string(&path)
            .unwrap_or_else(|e| panic!("failed to read {}: {e}", path.display()));
        assert!(
            src.contains("pub mod worker_main"),
            "lib.rs must export worker_main module"
        );
    }
}

// ============================================================================
// VAL-CARGO-002: The onnx feature propagates to leindex-embed/onnx
// ============================================================================

mod feature_propagation {
    use super::*;

    /// VAL-CARGO-002: The root `onnx` feature MUST enable `leindex-embed/onnx`
    /// so the installed worker binary has real ONNX inference support.
    /// Without this propagation, the worker would compile without onnx and
    /// produce dummy zeros instead of real embeddings.
    #[test]
    fn onnx_feature_propagates_to_leindex_embed() {
        let toml = root_cargo_toml();

        // Find the onnx feature line
        let onnx_line = toml
            .lines()
            .find(|l| l.trim_start().starts_with("onnx = ["))
            .expect("root Cargo.toml must define an 'onnx' feature");

        assert!(
            onnx_line.contains("leindex-embed/onnx"),
            "The root 'onnx' feature MUST include 'leindex-embed/onnx' so the worker \
             binary gets ONNX support. Got: {}",
            onnx_line
        );
    }

    /// VAL-CARGO-003: onnx-migraphx builds on onnx and adds migraphx.
    #[test]
    fn onnx_migraphx_feature_includes_migraphx() {
        let toml = root_cargo_toml();
        let migraphx_line = toml
            .lines()
            .find(|l| l.trim_start().starts_with("onnx-migraphx = ["))
            .expect("root Cargo.toml must define 'onnx-migraphx' feature");

        assert!(migraphx_line.contains("leindex-embed/onnx-migraphx"));
    }

    /// VAL-ORT-001/002: The ort crate uses load-dynamic (not download-binaries).
    /// We check only the active `ort = ...` dependency line, ignoring comments.
    #[test]
    fn ort_uses_load_dynamic() {
        let toml = embed_cargo_toml();

        // Find the ort dependency line (not a comment)
        let ort_line = toml
            .lines()
            .find(|l| {
                let t = l.trim();
                t.starts_with("ort = ") || t.starts_with("ort = {")
            })
            .expect("leindex-embed must declare an ort dependency");

        assert!(
            ort_line.contains("load-dynamic"),
            "leindex-embed ort dependency must use load-dynamic feature. Got: {}",
            ort_line
        );
        assert!(
            !ort_line.contains("download-binaries"),
            "leindex-embed ort dependency must NOT use download-binaries. Got: {}",
            ort_line
        );
        assert!(
            !ort_line.contains("copy-dylibs"),
            "leindex-embed ort dependency must NOT use copy-dylibs. Got: {}",
            ort_line
        );
    }
}

// ============================================================================
// VAL-CARGO-006: leindex setup subcommand is declared
// ============================================================================

mod setup_command {
    use super::*;

    /// VAL-CARGO-006: `leindex --help` lists `setup` and `leindex setup --help`
    /// exits 0. We statically verify the Setup variant is declared in cli.rs.
    #[test]
    fn setup_command_declared_in_cli() {
        let cli_path = repo_root().join("src").join("cli").join("cli.rs");
        let src = std::fs::read_to_string(&cli_path)
            .unwrap_or_else(|e| panic!("failed to read {}: {e}", cli_path.display()));

        assert!(
            src.contains("Setup {") || src.contains("Setup{"),
            "cli.rs must declare a Setup subcommand variant"
        );

        // VAL-CARGO-006 evidence: flags must include --neural, --cpu, --gpu, --no-neural, --check
        for flag in ["--neural", "--cpu", "--gpu", "--no-neural", "--check"] {
            let as_long = format!("long = \"{}\"", flag.trim_start_matches("--"));
            assert!(
                src.contains(&as_long),
                "cli.rs setup must declare flag '{}' (as '{}')",
                flag,
                as_long
            );
        }
    }
}

// ============================================================================
// VAL-CARGO-011: No ORT vendored into ~/.cargo/bin/
// (Static check: no build mechanism copies .so files into install root)
// ============================================================================

mod no_ort_vendoring {
    use super::*;

    /// VAL-CYARGO-011 / VAL-DOCS-008: No ORT_LIB_PATH or ORT_PREFER_DYNAMIC_LINK
    /// remain in .cargo/config.toml.
    #[test]
    fn cargo_config_has_no_ort_env_entries() {
        let cfg = cargo_config_toml();
        assert!(
            !cfg.contains("ORT_LIB_PATH"),
            ".cargo/config.toml must not set ORT_LIB_PATH (obsolete under load-dynamic)"
        );
        assert!(
            !cfg.contains("ORT_PREFER_DYNAMIC_LINK"),
            ".cargo/config.toml must not set ORT_PREFER_DYNAMIC_LINK"
        );
    }

    /// VAL-DOCS-009 / VAL-ORT-004: The ort-lib/ directory must not exist.
    #[test]
    fn ort_lib_directory_removed() {
        let ort_lib = repo_root().join("ort-lib");
        assert!(
            !ort_lib.exists(),
            "ort-lib/ directory must not exist (it was the old build-time ORT cache)"
        );
    }

    /// VAL-DOCS-010: No source references to ort-lib/ remain.
    #[test]
    fn no_source_references_to_ort_lib() {
        // Check build scripts
        for rel in ["build.rs", "crates/leindex-embed/build.rs"] {
            let path = repo_root().join(rel);
            if let Ok(src) = std::fs::read_to_string(&path) {
                // Allow references in comments explaining what was removed
                for line in src.lines() {
                    let trimmed = line.trim();
                    if trimmed.starts_with("//") || trimmed.starts_with("#") {
                        continue;
                    }
                    assert!(
                        !trimmed.contains("ort-lib"),
                        "Build script {} references ort-lib/ in code: {}",
                        rel,
                        trimmed
                    );
                }
            }
        }
    }

    /// VAL-RELEASE-012: No $ORIGIN rpath in build scripts.
    #[test]
    fn build_scripts_have_no_origin_rpath() {
        for rel in ["build.rs", "crates/leindex-embed/build.rs"] {
            let path = repo_root().join(rel);
            if let Ok(src) = std::fs::read_to_string(&path) {
                for line in src.lines() {
                    let trimmed = line.trim();
                    if trimmed.starts_with("//") {
                        continue;
                    }
                    assert!(
                        !trimmed.contains("$ORIGIN"),
                        "Build script {} still uses $ORIGIN rpath: {}",
                        rel,
                        trimmed
                    );
                }
            }
        }
    }
}

// ============================================================================
// VAL-CARGO-012/013: Version parity
// ============================================================================

mod version_parity {
    use super::*;

    /// Extract the version = "..." line from a TOML string.
    fn extract_version(toml: &str) -> Option<String> {
        for line in toml.lines() {
            let trimmed = line.trim();
            if let Some(rest) = trimmed.strip_prefix("version = ") {
                let v = rest.trim().trim_matches('"');
                if !v.is_empty() {
                    return Some(v.to_string());
                }
            }
        }
        None
    }

    /// VAL-DOCS-005 / VAL-CARGO-012: Root and subcrate versions must match.
    #[test]
    fn versions_match_across_crates() {
        let root = root_cargo_toml();
        let embed = embed_cargo_toml();
        let root_v = extract_version(&root).expect("root Cargo.toml missing version");
        let embed_v = extract_version(&embed).expect("embed Cargo.toml missing version");
        assert_eq!(
            root_v, embed_v,
            "Version mismatch: root={} embed={}",
            root_v, embed_v
        );
    }
}

// ============================================================================
// Neural setup docs packaging
// ============================================================================

mod docs_packaging {
    use super::*;

    #[test]
    fn crate_package_includes_neural_setup_doc() {
        let cargo = root_cargo_toml();
        assert!(
            cargo.contains("/docs/NEURAL_SETUP.md"),
            "crate package include list must contain docs/NEURAL_SETUP.md because README points to it"
        );
    }
}