tldr-cli 0.1.5

CLI binary for TLDR code analysis tool
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
//! L3 Statement-Level Diff Tests (Zhang-Shasha Tree Edit Distance)
//!
//! These tests define the expected behavior for `--granularity statement` in the
//! `tldr diff` command. Statement-level diff works by:
//! 1. Running L4 function matching to pair functions by name
//! 2. For each matched pair, extracting statement subtrees from the AST
//! 3. Building labeled trees and running Zhang-Shasha tree edit distance
//! 4. Converting the edit script into ASTChange records with children
//!
//! Spec: thoughts/shared/plans/multi-level-diff-spec.md, Section 4.5

use std::io::Write;
use std::path::PathBuf;

use tempfile::NamedTempFile;

use tldr_cli::commands::remaining::types::{
    ASTChange, ChangeType, DiffGranularity, DiffReport, DiffSummary, NodeKind,
};
use tldr_cli::commands::remaining::diff::DiffArgs;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Create a temporary Python file from an inline source string.
fn write_temp_py(content: &str) -> NamedTempFile {
    let mut f = NamedTempFile::with_suffix(".py").unwrap();
    write!(f, "{}", content).unwrap();
    f.flush().unwrap();
    f
}

/// Create a temporary TypeScript file from an inline source string.
fn write_temp_ts(content: &str) -> NamedTempFile {
    let mut f = NamedTempFile::with_suffix(".ts").unwrap();
    write!(f, "{}", content).unwrap();
    f.flush().unwrap();
    f
}

/// Run L3 (statement-level) diff and return the report.
fn run_l3_diff(file_a: &NamedTempFile, file_b: &NamedTempFile) -> DiffReport {
    let args = DiffArgs {
        file_a: file_a.path().to_path_buf(),
        file_b: file_b.path().to_path_buf(),
        granularity: DiffGranularity::Statement,
        semantic_only: false,
        output: None,
    };
    args.run_to_report().expect("L3 statement-level diff should succeed")
}

/// Find a change in the report by name and change_type.
fn find_change<'a>(
    changes: &'a [ASTChange],
    name: &str,
    change_type: ChangeType,
) -> Option<&'a ASTChange> {
    changes
        .iter()
        .find(|c| c.change_type == change_type && c.name.as_deref() == Some(name))
}

/// Find a child change within a parent ASTChange by change_type and node_kind.
fn find_child<'a>(
    parent: &'a ASTChange,
    change_type: ChangeType,
    node_kind: NodeKind,
) -> Option<&'a ASTChange> {
    parent.children.as_ref().and_then(|children| {
        children
            .iter()
            .find(|c| c.change_type == change_type && c.node_kind == node_kind)
    })
}

/// Count statement-level children of a given change type within a parent.
fn count_children(parent: &ASTChange, change_type: ChangeType) -> usize {
    parent
        .children
        .as_ref()
        .map(|ch| ch.iter().filter(|c| c.change_type == change_type).count())
        .unwrap_or(0)
}

/// Count all statement-level children.
fn count_all_children(parent: &ASTChange) -> usize {
    parent.children.as_ref().map(|ch| ch.len()).unwrap_or(0)
}

// ===========================================================================
// Test 1: Identical functions -> no statement changes
// ===========================================================================

#[test]
fn test_statement_identical() {
    let source = r#"
def compute(x, y):
    result = x + y
    if result > 10:
        result = 10
    return result

def helper():
    return 42
"#;

    let file_a = write_temp_py(source);
    let file_b = write_temp_py(source);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(report.identical, "Identical files should produce identical=true");
    assert!(
        report.changes.is_empty(),
        "Identical files should have zero changes, got {}",
        report.changes.len()
    );
    assert_eq!(
        report.granularity,
        DiffGranularity::Statement,
        "Report granularity should be Statement"
    );
}

// ===========================================================================
// Test 2: Function gains a new statement -> Insert
// ===========================================================================

#[test]
fn test_statement_added() {
    let source_a = r#"
def compute(x, y):
    result = x + y
    return result
"#;

    let source_b = r#"
def compute(x, y):
    result = x + y
    if result < 0:
        result = 0
    return result
"#;

    let file_a = write_temp_py(source_a);
    let file_b = write_temp_py(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical, "Files should not be identical");

    // Should have an Update change for function "compute" (function-level)
    let compute_change = find_change(&report.changes, "compute", ChangeType::Update)
        .expect("Should have Update change for 'compute'");

    // The function-level change should have statement-level children
    assert!(
        compute_change.children.is_some(),
        "Function update should have statement-level children"
    );

    let children = compute_change.children.as_ref().unwrap();
    assert!(
        !children.is_empty(),
        "Should have at least one statement-level change"
    );

    // There should be an Insert for the if_statement
    let has_insert = children
        .iter()
        .any(|c| c.change_type == ChangeType::Insert && c.node_kind == NodeKind::Statement);
    assert!(
        has_insert,
        "Should have a statement Insert for the new if block"
    );
}

// ===========================================================================
// Test 3: Function loses a statement -> Delete
// ===========================================================================

#[test]
fn test_statement_removed() {
    let source_a = r#"
def compute(x, y):
    result = x + y
    if result < 0:
        result = 0
    return result
"#;

    let source_b = r#"
def compute(x, y):
    result = x + y
    return result
"#;

    let file_a = write_temp_py(source_a);
    let file_b = write_temp_py(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical, "Files should not be identical");

    let compute_change = find_change(&report.changes, "compute", ChangeType::Update)
        .expect("Should have Update change for 'compute'");

    assert!(
        compute_change.children.is_some(),
        "Function update should have statement-level children"
    );

    // There should be a Delete for the if_statement
    let has_delete = compute_change
        .children
        .as_ref()
        .unwrap()
        .iter()
        .any(|c| c.change_type == ChangeType::Delete && c.node_kind == NodeKind::Statement);
    assert!(
        has_delete,
        "Should have a statement Delete for the removed if block"
    );
}

// ===========================================================================
// Test 4: Statement changed (e.g., return value) -> Update (Relabel)
// ===========================================================================

#[test]
fn test_statement_modified() {
    let source_a = r#"
def compute(x):
    result = x * 2
    return result
"#;

    let source_b = r#"
def compute(x):
    result = x * 3
    return result
"#;

    let file_a = write_temp_py(source_a);
    let file_b = write_temp_py(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical, "Files should not be identical");

    let compute_change = find_change(&report.changes, "compute", ChangeType::Update)
        .expect("Should have Update change for 'compute'");

    assert!(
        compute_change.children.is_some(),
        "Function update should have statement-level children"
    );

    // The assignment "result = x * 2" -> "result = x * 3" should be an Update
    let has_update = compute_change
        .children
        .as_ref()
        .unwrap()
        .iter()
        .any(|c| c.change_type == ChangeType::Update && c.node_kind == NodeKind::Statement);
    assert!(
        has_update,
        "Should have a statement Update for the modified assignment"
    );
}

// ===========================================================================
// Test 5: Statements reordered -> Delete + Insert
// ===========================================================================

#[test]
fn test_statement_reordered() {
    let source_a = r#"
def process(items):
    items.sort()
    items.reverse()
    return items
"#;

    let source_b = r#"
def process(items):
    items.reverse()
    items.sort()
    return items
"#;

    let file_a = write_temp_py(source_a);
    let file_b = write_temp_py(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical, "Files should not be identical");

    let process_change = find_change(&report.changes, "process", ChangeType::Update)
        .expect("Should have Update change for 'process'");

    assert!(
        process_change.children.is_some(),
        "Function update should have statement-level children"
    );

    // Reordering should produce changes (Update/Delete+Insert depending on algorithm)
    let children = process_change.children.as_ref().unwrap();
    assert!(
        !children.is_empty(),
        "Should detect statement reordering as changes"
    );
}

// ===========================================================================
// Test 6: Nested if-block body changed
// ===========================================================================

#[test]
fn test_nested_if_changed() {
    let source_a = r#"
def check(x):
    if x > 0:
        print("positive")
        return True
    return False
"#;

    let source_b = r#"
def check(x):
    if x > 0:
        print("non-negative")
        return True
    return False
"#;

    let file_a = write_temp_py(source_a);
    let file_b = write_temp_py(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical, "Files should not be identical");

    let check_change = find_change(&report.changes, "check", ChangeType::Update)
        .expect("Should have Update change for 'check'");

    assert!(
        check_change.children.is_some(),
        "Function update should have statement-level children"
    );

    // Should detect the change within the if block
    let children = check_change.children.as_ref().unwrap();
    assert!(
        !children.is_empty(),
        "Should detect changes within nested if block"
    );
}

// ===========================================================================
// Test 7: New function added -> function-level Insert (not statement)
// ===========================================================================

#[test]
fn test_unmatched_function() {
    let source_a = r#"
def existing():
    return 42
"#;

    let source_b = r#"
def existing():
    return 42

def brand_new(x):
    if x > 0:
        return x
    return 0
"#;

    let file_a = write_temp_py(source_a);
    let file_b = write_temp_py(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical, "Files should not be identical");

    // "existing" should not have any changes (identical)
    let existing_change = find_change(&report.changes, "existing", ChangeType::Update);
    assert!(
        existing_change.is_none(),
        "Identical function 'existing' should have no Update change"
    );

    // "brand_new" should be a function-level Insert, NOT statement-level
    let new_fn = find_change(&report.changes, "brand_new", ChangeType::Insert)
        .expect("Should have Insert for new function 'brand_new'");

    assert_eq!(
        new_fn.node_kind,
        NodeKind::Function,
        "New function should be reported as NodeKind::Function, not Statement"
    );

    // Function-level insert should NOT have statement children
    // (no point in statement-diffing a brand new function)
    assert!(
        new_fn.children.is_none() || new_fn.children.as_ref().unwrap().is_empty(),
        "Function-level Insert should not have statement children"
    );
}

// ===========================================================================
// Test 8: Multiple functions, one with statement changes, one identical
// ===========================================================================

#[test]
fn test_multiple_functions() {
    let source_a = r#"
def unchanged():
    return 42

def modified(x):
    result = x + 1
    return result
"#;

    let source_b = r#"
def unchanged():
    return 42

def modified(x):
    result = x + 1
    if result > 100:
        result = 100
    return result
"#;

    let file_a = write_temp_py(source_a);
    let file_b = write_temp_py(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical, "Files should not be identical");

    // "unchanged" should not appear in changes
    let unchanged = find_change(&report.changes, "unchanged", ChangeType::Update);
    assert!(
        unchanged.is_none(),
        "'unchanged' function should not have any changes"
    );

    // "modified" should have an Update with statement-level children
    let modified_change = find_change(&report.changes, "modified", ChangeType::Update)
        .expect("Should have Update change for 'modified'");

    assert!(
        modified_change.children.is_some(),
        "Modified function update should have statement-level children"
    );

    let children = modified_change.children.as_ref().unwrap();
    assert!(
        !children.is_empty(),
        "Modified function should have statement-level changes"
    );
}

// ===========================================================================
// Test 9: Large function fallback (>200 statements -> no statement children)
// ===========================================================================

#[test]
fn test_large_function_fallback() {
    // Generate a function with >200 statements
    let mut lines_a = vec!["def big_func():".to_string()];
    let mut lines_b = vec!["def big_func():".to_string()];
    for i in 0..210 {
        lines_a.push(format!("    x_{} = {}", i, i));
        // Change the last few to ensure it's detected as Update
        if i >= 205 {
            lines_b.push(format!("    x_{} = {}", i, i + 1000));
        } else {
            lines_b.push(format!("    x_{} = {}", i, i));
        }
    }
    lines_a.push("    return x_0".to_string());
    lines_b.push("    return x_0".to_string());

    let source_a = lines_a.join("\n");
    let source_b = lines_b.join("\n");

    let file_a = write_temp_py(&source_a);
    let file_b = write_temp_py(&source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical, "Files should not be identical");

    // Should have an Update for big_func
    let big_fn = find_change(&report.changes, "big_func", ChangeType::Update)
        .expect("Should have Update for 'big_func'");

    // With >200 statements, should fall back to L4-style (no statement children)
    // OR have children but computed via fallback. Either way, the function
    // should be detected as changed.
    assert_eq!(
        big_fn.node_kind,
        NodeKind::Function,
        "big_func should be reported as a function-level update"
    );
}

// ===========================================================================
// Test 10: TypeScript statement diff (multi-language support)
// ===========================================================================

#[test]
fn test_statement_typescript() {
    let source_a = r#"
function calculate(x: number): number {
    const result = x * 2;
    return result;
}
"#;

    let source_b = r#"
function calculate(x: number): number {
    const result = x * 2;
    if (result > 100) {
        return 100;
    }
    return result;
}
"#;

    let file_a = write_temp_ts(source_a);
    let file_b = write_temp_ts(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical, "Files should not be identical");
    assert_eq!(report.granularity, DiffGranularity::Statement);

    // Should detect the function as modified
    let calc_change = find_change(&report.changes, "calculate", ChangeType::Update)
        .expect("Should have Update change for 'calculate'");

    // Should have statement-level children
    assert!(
        calc_change.children.is_some() && !calc_change.children.as_ref().unwrap().is_empty(),
        "TypeScript function update should have statement-level children"
    );
}

// ===========================================================================
// Test 11: Function deleted should be function-level Delete
// ===========================================================================

#[test]
fn test_function_deleted() {
    let source_a = r#"
def keep_me():
    return 1

def remove_me():
    x = 42
    return x
"#;

    let source_b = r#"
def keep_me():
    return 1
"#;

    let file_a = write_temp_py(source_a);
    let file_b = write_temp_py(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    assert!(!report.identical);

    let del = find_change(&report.changes, "remove_me", ChangeType::Delete)
        .expect("Should have Delete for removed function");

    assert_eq!(
        del.node_kind,
        NodeKind::Function,
        "Removed function should be NodeKind::Function"
    );

    // Function-level deletes should not have statement children
    assert!(
        del.children.is_none() || del.children.as_ref().unwrap().is_empty(),
        "Function-level Delete should not have statement children"
    );
}

// ===========================================================================
// Test 12: Summary statistics should be correct
// ===========================================================================

#[test]
fn test_statement_summary() {
    let source_a = r#"
def func_a():
    return 1

def func_b(x):
    result = x + 1
    return result
"#;

    let source_b = r#"
def func_a():
    return 1

def func_b(x):
    result = x + 1
    if result > 10:
        result = 10
    return result
"#;

    let file_a = write_temp_py(source_a);
    let file_b = write_temp_py(source_b);
    let report = run_l3_diff(&file_a, &file_b);

    let summary = report.summary.expect("Report should have summary");
    assert!(
        summary.total_changes >= 1,
        "Should have at least 1 function-level change"
    );
    assert!(
        summary.updates >= 1,
        "Should have at least 1 update (for func_b)"
    );
}