harn-hostlib 0.9.21

Opt-in code-intelligence and deterministic-tool host builtins for the Harn VM
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//! End-to-end coverage for the wired-up `ast::*` builtins.
//!
//! Complements `ast_fixtures.rs` (golden-symbol/outline checks): this
//! file drives the full builtin path through the registration table —
//! parameter parsing, `VmValue::Dict` shape, schema field set.
//!
//! Parse-latency characterization lives in `benches/ast_parse.rs`.

use std::path::PathBuf;
use std::sync::Arc;

use harn_hostlib::{ast::AstCapability, BuiltinRegistry, HostlibCapability};
use harn_vm::VmValue;

fn ast_registry() -> BuiltinRegistry {
    let mut registry = BuiltinRegistry::new();
    AstCapability.register_builtins(&mut registry);
    registry
}

fn dict(pairs: &[(&str, VmValue)]) -> VmValue {
    let mut map: harn_vm::value::DictMap = Default::default();
    for (k, v) in pairs {
        map.insert((*k).into(), v.clone());
    }
    VmValue::dict(map)
}

fn fixture_path(rel: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/ast")
        .join(rel)
}

fn structural_diff_fixture_path(rel: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/structural_diff")
        .join(rel)
}

fn invoke(registry: &BuiltinRegistry, name: &str, payload: VmValue) -> VmValue {
    let entry = registry
        .find(name)
        .unwrap_or_else(|| panic!("builtin {name} not registered"));
    (entry.handler)(&[payload]).unwrap_or_else(|err| panic!("{name} failed: {err}"))
}

fn dict_field(value: &VmValue, key: &str) -> VmValue {
    match value {
        VmValue::Dict(d) => d
            .get(key)
            .cloned()
            .unwrap_or_else(|| panic!("missing field `{key}` on {value:?}")),
        other => panic!("expected dict, got {other:?}"),
    }
}

fn string_value(value: &VmValue) -> &str {
    match value {
        VmValue::String(s) => s.as_str(),
        other => panic!("expected string, got {other:?}"),
    }
}

fn list_value(value: &VmValue) -> Arc<Vec<VmValue>> {
    match value {
        VmValue::List(l) => l.clone(),
        other => panic!("expected list, got {other:?}"),
    }
}

fn int_value(value: &VmValue) -> i64 {
    match value {
        VmValue::Int(n) => *n,
        other => panic!("expected int, got {other:?}"),
    }
}

fn bool_value(value: &VmValue) -> bool {
    match value {
        VmValue::Bool(b) => *b,
        other => panic!("expected bool, got {other:?}"),
    }
}

#[test]
fn parse_file_produces_flat_node_list_with_root_id_zero() {
    let registry = ast_registry();
    let path = fixture_path("rust/source.rs");
    let payload = dict(&[(
        "path",
        VmValue::String(arcstr::ArcStr::from(path.to_string_lossy().as_ref())),
    )]);

    let result = invoke(&registry, "hostlib_ast_parse_file", payload);

    assert_eq!(string_value(&dict_field(&result, "language")), "rust");
    assert_eq!(int_value(&dict_field(&result, "root_id")), 0);
    let nodes = list_value(&dict_field(&result, "nodes"));
    assert!(
        nodes.len() > 5,
        "expected non-trivial tree, got {} nodes",
        nodes.len()
    );

    // Root has parent_id = nil; every other node has an integer parent
    // pointing at a smaller id (BFS guarantees this).
    let first = &nodes[0];
    assert!(matches!(dict_field(first, "parent_id"), VmValue::Nil));
    for node in nodes.iter().skip(1) {
        match dict_field(node, "parent_id") {
            VmValue::Int(n) => assert!(
                n >= 0 && (n as usize) < nodes.len(),
                "parent_id out of range: {n}"
            ),
            other => panic!("expected int parent_id, got {other:?}"),
        }
    }
}

#[test]
fn parse_file_rejects_unknown_language() {
    let registry = ast_registry();
    let entry = registry.find("hostlib_ast_parse_file").expect("registered");
    let payload = dict(&[
        ("path", VmValue::String(arcstr::ArcStr::from("foo.unknown"))),
        ("language", VmValue::String(arcstr::ArcStr::from("klingon"))),
    ]);
    let err = (entry.handler)(&[payload]).expect_err("must reject unknown language");
    assert!(
        err.to_string().contains("klingon") || err.to_string().contains("could not infer"),
        "unexpected error message: {err}",
    );
}

#[test]
fn symbols_filters_by_kind() {
    let registry = ast_registry();
    let path = fixture_path("rust/source.rs");
    let kinds = VmValue::List(Arc::new(vec![VmValue::String(arcstr::ArcStr::from(
        "function",
    ))]));
    let payload = dict(&[
        (
            "path",
            VmValue::String(arcstr::ArcStr::from(path.to_string_lossy().as_ref())),
        ),
        ("kinds", kinds),
    ]);
    let result = invoke(&registry, "hostlib_ast_symbols", payload);
    let symbols = list_value(&dict_field(&result, "symbols"));
    assert!(!symbols.is_empty());
    for sym in symbols.iter() {
        assert_eq!(string_value(&dict_field(sym, "kind")), "function");
    }
}

#[test]
fn outline_caps_depth_when_max_depth_supplied() {
    let registry = ast_registry();
    let path = fixture_path("python/source.py");
    let payload = dict(&[
        (
            "path",
            VmValue::String(arcstr::ArcStr::from(path.to_string_lossy().as_ref())),
        ),
        ("max_depth", VmValue::Int(1)),
    ]);
    let result = invoke(&registry, "hostlib_ast_outline", payload);
    let items = list_value(&dict_field(&result, "items"));
    assert!(!items.is_empty());
    for item in items.iter() {
        let children = list_value(&dict_field(item, "children"));
        assert!(
            children.is_empty(),
            "max_depth=1 must cap to root level, got children {children:?}"
        );
    }
}

// Issue #564's 20ms parse-time target moved to a Criterion benchmark
// (`benches/ast_parse.rs`). Wall-clock perf budgets in tests flake on
// shared CI runners; Criterion's statistical sampling tracks the same
// signal without that flake.

// ---------------------------------------------------------------------------
// Mutation + bracket-balance builtins (issue #775)
// ---------------------------------------------------------------------------

fn vm_string(s: &str) -> VmValue {
    VmValue::String(arcstr::ArcStr::from(s))
}

#[test]
fn symbol_extract_returns_one_based_inclusive_lines() {
    let registry = ast_registry();
    let source = "fn alpha() {}\nfn beta() {}\nfn gamma() {}\n";
    let payload = dict(&[
        ("source", vm_string(source)),
        ("language", vm_string("rust")),
        ("symbol_name", vm_string("beta")),
    ]);
    let result = invoke(&registry, "hostlib_ast_symbol_extract", payload);
    assert_eq!(string_value(&dict_field(&result, "result")), "extracted");
    assert_eq!(int_value(&dict_field(&result, "start_line")), 2);
    assert_eq!(int_value(&dict_field(&result, "end_line")), 2);
    assert_eq!(string_value(&dict_field(&result, "text")), "fn beta() {}");
}

#[test]
fn symbol_delete_removes_function_and_keeps_syntax_valid() {
    let registry = ast_registry();
    let source = "fn alpha() {}\nfn beta() {}\nfn gamma() {}\n";
    let payload = dict(&[
        ("source", vm_string(source)),
        ("language", vm_string("rust")),
        ("symbol_name", vm_string("beta")),
    ]);
    let result = invoke(&registry, "hostlib_ast_symbol_delete", payload);
    assert_eq!(string_value(&dict_field(&result, "result")), "removed");
    let new_source = string_value(&dict_field(&result, "source")).to_string();
    assert!(!new_source.contains("beta"));
    assert!(new_source.contains("alpha"));
    assert!(new_source.contains("gamma"));
}

#[test]
fn symbol_replace_swaps_in_caller_text() {
    let registry = ast_registry();
    let source = "fn alpha() {}\nfn beta() -> i32 { 0 }\n";
    let payload = dict(&[
        ("source", vm_string(source)),
        ("language", vm_string("rust")),
        ("symbol_name", vm_string("beta")),
        ("new_text", vm_string("fn beta() -> i32 { 42 }")),
    ]);
    let result = invoke(&registry, "hostlib_ast_symbol_replace", payload);
    assert_eq!(string_value(&dict_field(&result, "result")), "replaced");
    assert!(string_value(&dict_field(&result, "source")).contains("42"));
}

#[test]
fn symbol_replace_flags_post_edit_syntax_error() {
    let registry = ast_registry();
    let source = "fn alpha() {}\nfn beta() {}\n";
    let payload = dict(&[
        ("source", vm_string(source)),
        ("language", vm_string("rust")),
        ("symbol_name", vm_string("beta")),
        // Unclosed paren intentionally breaks the edit.
        ("new_text", vm_string("fn beta( {")),
    ]);
    let result = invoke(&registry, "hostlib_ast_symbol_replace", payload);
    assert_eq!(
        string_value(&dict_field(&result, "result")),
        "syntax_error_after_edit"
    );
    assert!(!string_value(&dict_field(&result, "details")).is_empty());
}

#[test]
fn symbol_lookup_reports_ambiguity_with_match_count() {
    let registry = ast_registry();
    let source = "class A:\n    def greet(self): pass\nclass B:\n    def greet(self): pass\n";
    let payload = dict(&[
        ("source", vm_string(source)),
        ("language", vm_string("python")),
        ("symbol_name", vm_string("greet")),
    ]);
    let result = invoke(&registry, "hostlib_ast_symbol_extract", payload);
    assert_eq!(string_value(&dict_field(&result, "result")), "ambiguous");
    assert!(int_value(&dict_field(&result, "match_count")) >= 2);
}

#[test]
fn symbol_lookup_qualified_name_disambiguates() {
    let registry = ast_registry();
    let source = "class A:\n    def greet(self): pass\nclass B:\n    def greet(self): pass\n";
    let payload = dict(&[
        ("source", vm_string(source)),
        ("language", vm_string("python")),
        ("symbol_name", vm_string("B.greet")),
    ]);
    let result = invoke(&registry, "hostlib_ast_symbol_extract", payload);
    assert_eq!(string_value(&dict_field(&result, "result")), "extracted");
    assert!(string_value(&dict_field(&result, "text")).contains("greet"));
    assert_eq!(int_value(&dict_field(&result, "start_line")), 4);
}

#[test]
fn symbol_lookup_emits_suggestions_when_typo_misses() {
    let registry = ast_registry();
    let source = "fn parse_query() {}\nfn parse_other() {}\n";
    let payload = dict(&[
        ("source", vm_string(source)),
        ("language", vm_string("rust")),
        ("symbol_name", vm_string("parse_qury")),
    ]);
    let result = invoke(&registry, "hostlib_ast_symbol_extract", payload);
    assert_eq!(string_value(&dict_field(&result, "result")), "not_found");
    let suggestions = list_value(&dict_field(&result, "suggestions"));
    assert!(!suggestions.is_empty());
    let names: Vec<&str> = suggestions.iter().map(string_value).collect();
    assert!(names.contains(&"parse_query"));
}

#[test]
fn unsupported_language_short_circuits_on_mutation_builtins() {
    let registry = ast_registry();
    for builtin in &[
        "hostlib_ast_symbol_extract",
        "hostlib_ast_symbol_delete",
        "hostlib_ast_symbol_replace",
    ] {
        let mut entries = vec![
            ("source", vm_string("hello")),
            ("language", vm_string("klingon")),
            ("symbol_name", vm_string("greet")),
        ];
        if *builtin == "hostlib_ast_symbol_replace" {
            entries.push(("new_text", vm_string("replacement")));
        }
        let result = invoke(&registry, builtin, dict(&entries));
        assert_eq!(
            string_value(&dict_field(&result, "result")),
            "unsupported_language",
            "{builtin} did not short-circuit on klingon",
        );
    }
}

#[test]
fn bracket_balance_counts_unmatched_opener() {
    let registry = ast_registry();
    let payload = dict(&[
        ("source", vm_string("fn foo() {")),
        ("language", vm_string("rust")),
    ]);
    let result = invoke(&registry, "hostlib_ast_bracket_balance", payload);
    assert_eq!(int_value(&dict_field(&result, "parens")), 0);
    assert_eq!(int_value(&dict_field(&result, "brackets")), 0);
    assert_eq!(int_value(&dict_field(&result, "braces")), 1);
}

#[test]
fn bracket_balance_ignores_brackets_inside_strings() {
    let registry = ast_registry();
    let payload = dict(&[
        ("source", vm_string(r#"let s = "}{)";"#)),
        ("language", vm_string("rust")),
    ]);
    let result = invoke(&registry, "hostlib_ast_bracket_balance", payload);
    assert_eq!(int_value(&dict_field(&result, "parens")), 0);
    assert_eq!(int_value(&dict_field(&result, "braces")), 0);
}

#[test]
fn bracket_balance_python_uses_hash_comments() {
    let registry = ast_registry();
    let payload = dict(&[
        // Python `//` is integer division; must not be parsed as a comment.
        ("source", vm_string("x = 5 // 2  # cmt with [\n")),
        ("language", vm_string("python")),
    ]);
    let result = invoke(&registry, "hostlib_ast_bracket_balance", payload);
    assert_eq!(int_value(&dict_field(&result, "brackets")), 0);
    assert_eq!(int_value(&dict_field(&result, "parens")), 0);
}

#[test]
fn parse_errors_returns_clean_payload_for_valid_python() {
    let registry = ast_registry();
    let payload = dict(&[
        ("content", VmValue::String(arcstr::ArcStr::from("x = 1\n"))),
        ("language", VmValue::String(arcstr::ArcStr::from("python"))),
    ]);
    let result = invoke(&registry, "hostlib_ast_parse_errors", payload);

    assert_eq!(string_value(&dict_field(&result, "language")), "python");
    let errors = list_value(&dict_field(&result, "errors"));
    assert!(errors.is_empty(), "valid Python should have no errors");
    let supported = match dict_field(&result, "supported") {
        VmValue::Bool(b) => b,
        other => panic!("expected bool, got {other:?}"),
    };
    assert!(supported);
}

#[test]
fn parse_errors_flags_typescript_syntax_error() {
    let registry = ast_registry();
    let payload = dict(&[
        // Mismatched paren — tree-sitter will emit at least one ERROR or
        // MISSING node here.
        (
            "content",
            VmValue::String(arcstr::ArcStr::from("function foo(\n  return 1;\n}")),
        ),
        (
            "language",
            VmValue::String(arcstr::ArcStr::from("typescript")),
        ),
    ]);
    let result = invoke(&registry, "hostlib_ast_parse_errors", payload);
    let errors = list_value(&dict_field(&result, "errors"));
    assert!(!errors.is_empty(), "expected errors, got {errors:?}");

    // Each error has the documented field set.
    for entry in errors.iter() {
        for field in [
            "start_row",
            "start_col",
            "end_row",
            "end_col",
            "start_byte",
            "end_byte",
            "message",
            "snippet",
            "missing",
        ] {
            let _ = dict_field(entry, field);
        }
    }
}

#[test]
fn parse_errors_top_level_decl_count_matches_swift_profile() {
    // Covers top-level declaration counts for a couple of canonical
    // language profiles. Drift in this number means our declaration map
    // changed.
    let registry = ast_registry();

    let cases = [
        ("rust", "fn a() {}\nfn b() {}\nstruct C;\n", 3),
        ("python", "def a():\n    pass\nclass B:\n    pass\n", 2),
        // TS lists `export_statement` as both a declaration and a
        // wrapper, so each `export X` contributes 2 (the export itself
        // plus the wrapped decl).
        (
            "typescript",
            "export function a() {}\nexport const b = 1;\nfunction c() {}\n",
            5,
        ),
    ];
    for (lang, src, want) in cases {
        let payload = dict(&[
            ("content", VmValue::String(arcstr::ArcStr::from(src))),
            ("language", VmValue::String(arcstr::ArcStr::from(lang))),
        ]);
        let result = invoke(&registry, "hostlib_ast_parse_errors", payload);
        let count = int_value(&dict_field(&result, "top_level_decl_count"));
        assert_eq!(count, want, "{lang} top_level_decl_count");
    }
}

#[test]
fn undefined_names_python_returns_dedup_diagnostics() {
    let registry = ast_registry();
    let payload = dict(&[
        (
            "content",
            VmValue::String(arcstr::ArcStr::from(
                "import os\n\
                 def foo(x):\n    \
                     return x + missing()\n\
                 missing()\n\
                 missing()\n",
            )),
        ),
        ("language", VmValue::String(arcstr::ArcStr::from("python"))),
    ]);
    let result = invoke(&registry, "hostlib_ast_undefined_names", payload);
    let supported = match dict_field(&result, "supported") {
        VmValue::Bool(b) => b,
        other => panic!("expected bool, got {other:?}"),
    };
    assert!(supported);

    let diagnostics = list_value(&dict_field(&result, "diagnostics"));
    let names: Vec<String> = diagnostics
        .iter()
        .map(|d| string_value(&dict_field(d, "name")).to_string())
        .collect();
    assert_eq!(
        names,
        vec!["missing".to_string()],
        "expected single dedup'd 'missing', got {names:?}"
    );

    // Each diagnostic carries the documented fields.
    for d in diagnostics.iter() {
        let kind_value = dict_field(d, "kind");
        let kind = string_value(&kind_value).to_string();
        assert!(kind == "identifier" || kind == "type", "kind = {kind}");
        let msg_value = dict_field(d, "message");
        let message = string_value(&msg_value).to_string();
        assert!(message.contains("undefined name"), "message = {message}");
    }
}

#[test]
fn undefined_names_marks_unsupported_languages() {
    let registry = ast_registry();
    let payload = dict(&[
        (
            "content",
            VmValue::String(arcstr::ArcStr::from("fn main() {}\n")),
        ),
        ("language", VmValue::String(arcstr::ArcStr::from("rust"))),
    ]);
    let result = invoke(&registry, "hostlib_ast_undefined_names", payload);
    let supported = match dict_field(&result, "supported") {
        VmValue::Bool(b) => b,
        other => panic!("expected bool, got {other:?}"),
    };
    assert!(
        !supported,
        "rust isn't in the undefined-name profile set; must report supported = false"
    );
    let diagnostics = list_value(&dict_field(&result, "diagnostics"));
    assert!(diagnostics.is_empty());
}

fn structural_diff_payload(before: &str, after: &str, extra: &[(&str, VmValue)]) -> VmValue {
    let before_path = structural_diff_fixture_path(before);
    let after_path = structural_diff_fixture_path(after);
    let mut pairs = vec![
        (
            "path_a",
            VmValue::String(arcstr::ArcStr::from(before_path.to_string_lossy().as_ref())),
        ),
        (
            "path_b",
            VmValue::String(arcstr::ArcStr::from(after_path.to_string_lossy().as_ref())),
        ),
        ("language", VmValue::String(arcstr::ArcStr::from("rust"))),
    ];
    pairs.extend_from_slice(extra);
    dict(&pairs)
}

fn structural_summary(result: &VmValue) -> VmValue {
    dict_field(result, "summary")
}

#[test]
fn structural_diff_ignores_reformat_only_changes() {
    let registry = ast_registry();
    let payload = structural_diff_payload("reformat_before.rs", "reformat_after.rs", &[]);
    let result = invoke(&registry, "hostlib_ast_structural_diff", payload);

    assert_eq!(string_value(&dict_field(&result, "result")), "ok");
    assert_eq!(string_value(&dict_field(&result, "mode")), "structural");
    assert!(!bool_value(&dict_field(&result, "changed")));
    let changes = list_value(&dict_field(&result, "changes"));
    assert!(changes.is_empty());
    assert_eq!(
        int_value(&dict_field(&structural_summary(&result), "change_count")),
        0
    );
}

#[test]
fn structural_diff_reports_rename_as_identifier_replacement() {
    let registry = ast_registry();
    let payload = structural_diff_payload("rename_before.rs", "rename_after.rs", &[]);
    let result = invoke(&registry, "hostlib_ast_structural_diff", payload);

    assert_eq!(string_value(&dict_field(&result, "mode")), "structural");
    let changes = list_value(&dict_field(&result, "changes"));
    assert_eq!(
        changes.len(),
        1,
        "rename should produce one syntax change: {changes:?}"
    );
    assert_eq!(string_value(&dict_field(&changes[0], "kind")), "replace");
    assert_eq!(
        string_value(&dict_field(&changes[0], "node_kind")),
        "identifier"
    );
    assert_eq!(
        string_value(&dict_field(&changes[0], "before_text")),
        "value"
    );
    assert_eq!(
        string_value(&dict_field(&changes[0], "after_text")),
        "total"
    );
}

#[test]
fn structural_diff_reports_moved_nodes_without_line_patch_noise() {
    let registry = ast_registry();
    let payload = structural_diff_payload("move_before.rs", "move_after.rs", &[]);
    let result = invoke(&registry, "hostlib_ast_structural_diff", payload);

    assert_eq!(string_value(&dict_field(&result, "mode")), "structural");
    let summary = structural_summary(&result);
    assert!(int_value(&dict_field(&summary, "moves")) >= 1);
    let changes = list_value(&dict_field(&result, "changes"));
    assert_eq!(string_value(&dict_field(&changes[0], "kind")), "move");
    assert_eq!(
        string_value(&dict_field(&changes[0], "node_kind")),
        "function_item"
    );
    assert!(matches!(dict_field(&result, "line_diff"), VmValue::Nil));
}

#[test]
fn structural_diff_reports_logic_change_as_literal_replacement() {
    let registry = ast_registry();
    let payload = structural_diff_payload("logic_before.rs", "logic_after.rs", &[]);
    let result = invoke(&registry, "hostlib_ast_structural_diff", payload);

    assert_eq!(string_value(&dict_field(&result, "mode")), "structural");
    let changes = list_value(&dict_field(&result, "changes"));
    assert_eq!(
        changes.len(),
        1,
        "logic change should stay focused: {changes:?}"
    );
    assert_eq!(string_value(&dict_field(&changes[0], "kind")), "replace");
    assert_eq!(
        string_value(&dict_field(&changes[0], "node_kind")),
        "integer_literal"
    );
    assert_eq!(string_value(&dict_field(&changes[0], "before_text")), "41");
    assert_eq!(string_value(&dict_field(&changes[0], "after_text")), "42");
}

#[test]
fn structural_diff_falls_back_to_line_diff_when_limit_is_exceeded() {
    let registry = ast_registry();
    let payload = structural_diff_payload(
        "logic_before.rs",
        "logic_after.rs",
        &[("max_bytes", VmValue::Int(8))],
    );
    let result = invoke(&registry, "hostlib_ast_structural_diff", payload);

    assert_eq!(string_value(&dict_field(&result, "result")), "fallback");
    assert_eq!(string_value(&dict_field(&result, "mode")), "line");
    assert!(string_value(&dict_field(&result, "fallback_reason")).contains("byte_limit_exceeded"));
    let line_diff = dict_field(&result, "line_diff");
    assert!(string_value(&dict_field(&line_diff, "diff")).contains("-    41"));
    assert!(string_value(&dict_field(&line_diff, "diff")).contains("+    42"));
}

#[test]
fn structural_diff_enforces_node_limit_before_descent() {
    let registry = ast_registry();
    let payload = structural_diff_payload(
        "logic_before.rs",
        "logic_after.rs",
        &[("max_nodes", VmValue::Int(1))],
    );
    let result = invoke(&registry, "hostlib_ast_structural_diff", payload);

    assert_eq!(string_value(&dict_field(&result, "result")), "fallback");
    assert_eq!(string_value(&dict_field(&result, "mode")), "line");
    assert!(string_value(&dict_field(&result, "fallback_reason")).contains("node_limit_exceeded"));
    assert_eq!(
        int_value(&dict_field(&structural_summary(&result), "nodes_before")),
        0
    );
}

#[test]
fn structural_diff_falls_back_to_line_diff_on_parse_error() {
    let registry = ast_registry();
    let payload = structural_diff_payload("logic_before.rs", "parse_error_after.rs", &[]);
    let result = invoke(&registry, "hostlib_ast_structural_diff", payload);

    assert_eq!(string_value(&dict_field(&result, "result")), "fallback");
    assert_eq!(string_value(&dict_field(&result, "mode")), "line");
    assert_eq!(
        string_value(&dict_field(&result, "fallback_reason")),
        "parse_error"
    );
    let line_diff = dict_field(&result, "line_diff");
    assert!(!string_value(&dict_field(&line_diff, "diff")).is_empty());
}

// `perf_smoke_against_external_file_when_available` (env-gated maintainer
// perf check, never run in CI) was removed alongside the in-tree perf
// budget assertion. Use `cargo bench --bench ast_parse` for parse latency
// characterization; it runs against the in-tree fixture without depending
// on a maintainer-local file.