blz-cli 1.5.5

CLI for blz – fast local llms.txt search
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
#![allow(
    missing_docs,
    clippy::expect_used,
    clippy::unwrap_used,
    clippy::needless_raw_string_hashes,
    clippy::uninlined_format_args,
    clippy::len_zero
)]

mod common;

use common::blz_cmd;
use serde_json::Value;
use tempfile::tempdir;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

/// Seed a test source with sample documentation
async fn seed_source(
    tmp: &tempfile::TempDir,
    server: &MockServer,
    alias: &str,
    doc: &str,
) -> anyhow::Result<()> {
    let url = format!("{}/llms.txt", server.uri());

    Mock::given(method("HEAD"))
        .and(path("/llms.txt"))
        .respond_with(
            ResponseTemplate::new(200).insert_header("content-length", doc.len().to_string()),
        )
        .mount(server)
        .await;

    Mock::given(method("GET"))
        .and(path("/llms.txt"))
        .respond_with(ResponseTemplate::new(200).set_body_string(doc))
        .mount(server)
        .await;

    blz_cmd()
        .env("BLZ_DATA_DIR", tmp.path())
        .args(["add", alias, url.as_str(), "-y"])
        .assert()
        .success();

    Ok(())
}

/// Run a find command and parse JSON output
fn run_find_json(tmp: &tempfile::TempDir, args: &[&str]) -> anyhow::Result<Value> {
    let stdout = blz_cmd()
        .env("BLZ_DATA_DIR", tmp.path())
        .args(args)
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    Ok(serde_json::from_slice(&stdout)?)
}

// Sample documentation for tests
const SAMPLE_DOC: &str = r#"# Documentation

## Introduction
Welcome to our documentation.

## Getting Started
This section covers installation.

### Installation
Run the installer script.

### Configuration
Edit the config file.

## Advanced Topics
Deep dive into advanced features.

### Performance Tuning
Optimize for speed.

## API Reference
Complete API documentation.
"#;

#[tokio::test]
async fn find_with_query_routes_to_search_mode() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Query string should route to search mode
    let payload = run_find_json(
        &tmp,
        &["find", "installation", "--source", "docs", "-f", "json"],
    )?;

    // Verify search response structure
    assert!(payload.get("query").is_some(), "should have query field");
    assert!(
        payload.get("results").is_some(),
        "should have results array"
    );
    assert_eq!(
        payload["query"].as_str().unwrap(),
        "installation",
        "query should match input"
    );

    // Verify we got search results
    let results = payload["results"].as_array().unwrap();
    assert!(
        !results.is_empty(),
        "should find matches for 'installation'"
    );

    Ok(())
}

#[tokio::test]
async fn find_with_citation_routes_to_retrieve_mode() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Citation pattern should route to retrieve mode
    let payload = run_find_json(&tmp, &["find", "docs:1-5", "-f", "json"])?;

    // Verify get/retrieve response structure
    assert!(
        payload.get("requests").is_some(),
        "should have requests field from get mode"
    );

    let requests = payload["requests"].as_array().unwrap();
    assert_eq!(requests.len(), 1, "should have one request");

    let request = &requests[0];
    assert_eq!(request["alias"].as_str().unwrap(), "docs");
    assert!(
        request.get("snippet").is_some(),
        "should have snippet content"
    );

    Ok(())
}

#[tokio::test]
async fn find_with_multirange_citation_routes_to_retrieve() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Multi-range citation should also route to retrieve mode
    let payload = run_find_json(&tmp, &["find", "docs:1-3,7-9", "-f", "json"])?;

    assert!(
        payload.get("requests").is_some(),
        "multi-range should use retrieve mode"
    );

    let requests = payload["requests"].as_array().unwrap();
    assert_eq!(requests.len(), 1, "should have one request for multi-range");

    Ok(())
}

#[tokio::test]
async fn find_heading_level_filter_applies_in_search_mode() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Search without filter
    let all_results = run_find_json(&tmp, &["find", "section", "--source", "docs", "-f", "json"])?;
    let all_count = all_results["results"].as_array().unwrap().len();

    // Search with heading level filter (only level 2 headings)
    let filtered_results = run_find_json(
        &tmp,
        &[
            "find", "section", "--source", "docs", "-H", "=2", "-f", "json",
        ],
    )?;
    let filtered_count = filtered_results["results"].as_array().unwrap().len();

    // Filtering should reduce results
    assert!(
        filtered_count <= all_count,
        "heading filter should reduce or maintain result count"
    );

    // Verify all results have level 2
    for result in filtered_results["results"].as_array().unwrap() {
        let level = result.get("level").and_then(Value::as_u64);
        assert_eq!(level, Some(2), "all filtered results should be level 2");
    }

    Ok(())
}

#[tokio::test]
async fn find_heading_level_filter_with_less_than_operator() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Filter for headings level 2 or less (h1 and h2)
    let filtered = run_find_json(
        &tmp,
        &[
            "find",
            "documentation",
            "--source",
            "docs",
            "-H",
            "<=2",
            "-f",
            "json",
        ],
    )?;

    // All results should have level <= 2
    for result in filtered["results"].as_array().unwrap() {
        let level = result.get("level").and_then(Value::as_u64).unwrap();
        assert!(
            level <= 2,
            "filtered result should have level <= 2, got {}",
            level
        );
    }

    Ok(())
}

#[tokio::test]
async fn find_heading_level_filter_ignored_in_retrieve_mode() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Use citation with heading level filter - should be ignored
    let result = blz_cmd()
        .env("BLZ_DATA_DIR", tmp.path())
        .args(["find", "docs:1-5", "-H", "=2", "-f", "json"])
        .assert()
        .success();

    let payload: Value = serde_json::from_slice(&result.get_output().stdout)?;

    // Should have retrieve mode structure (requests field)
    assert!(
        payload.get("requests").is_some(),
        "should use retrieve mode despite -H flag"
    );

    // Should return the requested lines regardless of heading level
    let requests = payload["requests"].as_array().unwrap();
    assert_eq!(requests.len(), 1);
    assert!(
        requests[0].get("snippet").is_some(),
        "should return content despite heading filter"
    );

    Ok(())
}

#[tokio::test]
async fn find_bare_command_with_query() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Bare command without explicit "find" subcommand
    // Note: This tests handle_default routing through find
    let payload = run_find_json(&tmp, &["find", "performance", "-f", "json"])?;

    assert!(
        payload.get("query").is_some(),
        "bare query should use search mode"
    );
    assert_eq!(payload["query"].as_str().unwrap(), "performance");

    Ok(())
}

#[tokio::test]
async fn find_bare_command_with_citation() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Bare command with citation pattern
    let payload = run_find_json(&tmp, &["find", "docs:10-15", "-f", "json"])?;

    assert!(
        payload.get("requests").is_some(),
        "bare citation should use retrieve mode"
    );

    Ok(())
}

#[tokio::test]
async fn find_distinguishes_query_from_citation_like_pattern() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // These look like citations but aren't (should be treated as queries)
    let test_cases = vec![
        "getting:started", // Not a valid citation (no digit-digit after colon)
        "docs:",           // Missing range
        "docs:10",         // Missing end of range
        "DOCS:10-20",      // Uppercase not allowed
        "docs 10-20",      // Space instead of colon
    ];

    for query in test_cases {
        let payload = run_find_json(&tmp, &["find", query, "-f", "json"])?;

        // Should be treated as search query, not citation
        assert!(
            payload.get("query").is_some(),
            "pattern '{}' should be treated as query",
            query
        );
        assert_eq!(
            payload["query"].as_str().unwrap(),
            query,
            "query field should match input '{}'",
            query
        );
    }

    Ok(())
}

#[tokio::test]
async fn find_with_context_in_search_mode() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Search with context flag
    let payload = run_find_json(
        &tmp,
        &[
            "find",
            "installation",
            "--source",
            "docs",
            "-C",
            "2",
            "-f",
            "json",
        ],
    )?;

    // Should be in search mode
    assert!(payload.get("query").is_some());

    // Results should have context
    let results = payload["results"].as_array().unwrap();
    if !results.is_empty() {
        // Check if first result has context (when matching)
        let first = &results[0];
        if first.get("context").is_some() {
            assert!(
                first["context"].get("lines").is_some(),
                "context should include line range"
            );
        }
    }

    Ok(())
}

#[tokio::test]
async fn find_with_block_context_in_retrieve_mode() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Retrieve with --context all (block mode)
    let payload = run_find_json(
        &tmp,
        &["find", "docs:5-7", "--context", "all", "-f", "json"],
    )?;

    // Should be in retrieve mode
    assert!(payload.get("requests").is_some());

    // Should expand to full block
    let requests = payload["requests"].as_array().unwrap();
    let snippet = requests[0]["snippet"].as_str().unwrap();
    assert!(!snippet.is_empty(), "should have expanded block content");

    Ok(())
}

#[tokio::test]
async fn find_with_copy_flag_in_search_mode() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Search with --copy flag (should still succeed even if clipboard fails)
    let result = blz_cmd()
        .env("BLZ_DATA_DIR", tmp.path())
        .args(["find", "api", "--source", "docs", "--copy", "-f", "json"])
        .assert()
        .success();

    let payload: Value = serde_json::from_slice(&result.get_output().stdout)?;

    // Should be in search mode
    assert!(payload.get("query").is_some());
    assert_eq!(payload["query"].as_str().unwrap(), "api");

    Ok(())
}

#[tokio::test]
async fn find_with_multiple_sources_in_search_mode() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    // Seed multiple sources
    seed_source(&tmp, &server, "docs1", SAMPLE_DOC).await?;
    seed_source(&tmp, &server, "docs2", SAMPLE_DOC).await?;

    let payload = run_find_json(
        &tmp,
        &[
            "find",
            "documentation",
            "--source",
            "docs1",
            "--source",
            "docs2",
            "-f",
            "json",
        ],
    )?;

    // Should search both sources
    assert!(payload.get("sources").is_some());
    let sources = payload["sources"].as_array().unwrap();
    assert!(sources.len() >= 1, "should search at least one source");

    Ok(())
}

#[tokio::test]
async fn find_heading_level_filter_with_range_syntax() -> anyhow::Result<()> {
    let tmp = tempdir()?;
    let server = MockServer::start().await;

    seed_source(&tmp, &server, "docs", SAMPLE_DOC).await?;

    // Filter for headings level 2-3 (h2 and h3)
    let filtered = run_find_json(
        &tmp,
        &[
            "find", "section", "--source", "docs", "-H", "2-3", "-f", "json",
        ],
    )?;

    // All results should have level between 2 and 3 (inclusive)
    for result in filtered["results"].as_array().unwrap() {
        let level = result.get("level").and_then(Value::as_u64).unwrap();
        assert!(
            (2..=3).contains(&level),
            "filtered result should have level 2-3, got {}",
            level
        );
    }

    Ok(())
}