harn-hostlib 0.7.50

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
//! Integration tests for `hostlib_tools_git` against a real fixture repo.
//!
//! These tests require `git` on `$PATH`. CI installs it; local dev usually
//! has it. If it's missing the tests are skipped via [`ensure_git`].

use std::collections::BTreeMap;
use std::path::Path;
use std::process::Command;
use std::rc::Rc;

use harn_hostlib::tools::permissions;
use harn_hostlib::{tools::ToolsCapability, BuiltinRegistry, HostlibCapability, HostlibError};
use harn_vm::VmValue;
use tempfile::TempDir;

fn registry() -> BuiltinRegistry {
    permissions::reset();
    permissions::enable_for_test();
    let mut registry = BuiltinRegistry::new();
    ToolsCapability.register_builtins(&mut registry);
    registry
}

fn dict_arg(entries: &[(&str, VmValue)]) -> Vec<VmValue> {
    let mut map: BTreeMap<String, VmValue> = BTreeMap::new();
    for (k, v) in entries {
        map.insert(k.to_string(), v.clone());
    }
    vec![VmValue::Dict(Rc::new(map))]
}

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

fn ensure_git() -> bool {
    Command::new("git").arg("--version").output().is_ok()
}

/// Initialize a tiny git repo with two commits, configured locally so the
/// test never reads global git config.
fn fixture_repo() -> TempDir {
    let dir = TempDir::new().unwrap();
    run_git(dir.path(), &["init", "-q", "-b", "main"]);
    run_git(dir.path(), &["config", "user.email", "tester@example.com"]);
    run_git(dir.path(), &["config", "user.name", "Tester"]);
    run_git(dir.path(), &["config", "commit.gpgsign", "false"]);

    std::fs::write(dir.path().join("a.txt"), "first\n").unwrap();
    run_git(dir.path(), &["add", "a.txt"]);
    run_git(dir.path(), &["commit", "-q", "-m", "first commit"]);

    std::fs::write(dir.path().join("a.txt"), "first\nsecond\n").unwrap();
    std::fs::write(dir.path().join("b.txt"), "new file\n").unwrap();
    run_git(dir.path(), &["add", "a.txt", "b.txt"]);
    run_git(dir.path(), &["commit", "-q", "-m", "second commit"]);

    run_git(
        dir.path(),
        &[
            "remote",
            "add",
            "origin",
            "https://example.invalid/repo.git",
        ],
    );

    dir
}

fn run_git(repo: &Path, args: &[&str]) {
    let mut cmd = Command::new("git");
    // Drop ambient GIT_* env vars so the test isn't perturbed by an
    // outer `git push` hook (which would otherwise leak GIT_DIR etc.
    // into the subprocess and silently break our tempdir isolation).
    for (key, _) in std::env::vars() {
        if key.starts_with("GIT_") {
            cmd.env_remove(&key);
        }
    }
    let output = cmd
        .arg("-C")
        .arg(repo)
        .args(args)
        .output()
        .unwrap_or_else(|err| {
            panic!(
                "git {} {} failed: {err}",
                args.first().unwrap_or(&""),
                repo.display()
            )
        });
    assert!(
        output.status.success(),
        "git {} (cwd={}) status={:?} stderr={}",
        args.join(" "),
        repo.display(),
        output.status,
        String::from_utf8_lossy(&output.stderr)
    );
}

fn dict_get<'a>(value: &'a VmValue, key: &str) -> &'a VmValue {
    match value {
        VmValue::Dict(d) => d.get(key).expect("key present"),
        other => panic!("not a dict: {other:?}"),
    }
}

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

#[test]
fn git_log_returns_structured_entries() {
    if !ensure_git() {
        eprintln!("skipping: git not installed");
        return;
    }
    let repo = fixture_repo();
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let result = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("log")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
    ]))
    .unwrap();
    let data = dict_get(&result, "data");
    let commits = list_of(data);
    assert_eq!(commits.len(), 2);
    if let VmValue::Dict(latest) = &commits[0] {
        if let Some(VmValue::String(s)) = latest.get("subject") {
            assert_eq!(s.as_ref(), "second commit");
        }
        if let Some(VmValue::String(sha)) = latest.get("sha") {
            assert_eq!(sha.len(), 40);
        }
    }
}

#[test]
fn git_log_max_count_limits_results() {
    if !ensure_git() {
        return;
    }
    let repo = fixture_repo();
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let result = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("log")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
        ("max_count", VmValue::Int(1)),
    ]))
    .unwrap();
    let data = dict_get(&result, "data");
    assert_eq!(list_of(data).len(), 1);
}

#[test]
fn git_status_reports_dirty_paths() {
    if !ensure_git() {
        return;
    }
    let repo = fixture_repo();
    std::fs::write(repo.path().join("c.txt"), "untracked\n").unwrap();

    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let result = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("status")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
    ]))
    .unwrap();
    let data = dict_get(&result, "data");
    let entries = list_of(data);
    assert!(entries.iter().any(|e| match dict_get(e, "path") {
        VmValue::String(s) => s.as_ref() == "c.txt",
        _ => false,
    }));
}

#[test]
fn git_current_branch_returns_main() {
    if !ensure_git() {
        return;
    }
    let repo = fixture_repo();
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let result = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("current_branch")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
    ]))
    .unwrap();
    let data = dict_get(&result, "data");
    if let VmValue::String(s) = data {
        assert_eq!(s.as_ref(), "main");
    } else {
        panic!("expected string branch name, got {data:?}");
    }
}

#[test]
fn git_remote_list_returns_origin() {
    if !ensure_git() {
        return;
    }
    let repo = fixture_repo();
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let result = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("remote_list")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
    ]))
    .unwrap();
    let data = dict_get(&result, "data");
    let remotes = list_of(data);
    assert_eq!(remotes.len(), 1);
    if let VmValue::Dict(d) = &remotes[0] {
        assert!(matches!(d.get("name"), Some(VmValue::String(s)) if s.as_ref() == "origin"));
        assert!(matches!(
            d.get("url"),
            Some(VmValue::String(s)) if s.as_ref() == "https://example.invalid/repo.git"
        ));
    }
}

#[test]
fn git_blame_returns_authors_per_line() {
    if !ensure_git() {
        return;
    }
    let repo = fixture_repo();
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let result = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("blame")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
        ("path", vm_string("a.txt")),
    ]))
    .unwrap();
    let data = dict_get(&result, "data");
    let lines = list_of(data);
    assert_eq!(lines.len(), 2);
    if let VmValue::Dict(first) = &lines[0] {
        assert!(matches!(
            first.get("author"),
            Some(VmValue::String(s)) if s.as_ref() == "Tester"
        ));
        assert!(matches!(first.get("line"), Some(VmValue::Int(1))));
    }
}

#[test]
fn git_show_emits_patch_text() {
    if !ensure_git() {
        return;
    }
    let repo = fixture_repo();
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let result = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("show")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
        ("rev", vm_string("HEAD")),
    ]))
    .unwrap();
    let data = dict_get(&result, "data");
    if let VmValue::String(s) = data {
        assert!(s.contains("second commit"));
        assert!(s.contains("b.txt"));
    } else {
        panic!("expected string");
    }
}

#[test]
fn git_diff_handles_clean_repo() {
    if !ensure_git() {
        return;
    }
    let repo = fixture_repo();
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let result = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("diff")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
    ]))
    .unwrap();
    let data = dict_get(&result, "data");
    if let VmValue::String(s) = data {
        assert!(s.is_empty(), "expected empty diff, got `{s}`");
    }
}

#[test]
fn git_branch_list_returns_main() {
    if !ensure_git() {
        return;
    }
    let repo = fixture_repo();
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let result = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("branch_list")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
    ]))
    .unwrap();
    let data = dict_get(&result, "data");
    let branches = list_of(data);
    assert_eq!(branches.len(), 1);
    if let VmValue::Dict(d) = &branches[0] {
        assert!(matches!(d.get("name"), Some(VmValue::String(s)) if s.as_ref() == "main"));
    }
}

#[test]
fn git_rejects_flag_lookalike_revs() {
    if !ensure_git() {
        return;
    }
    let repo = fixture_repo();
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let err = (entry.handler)(&dict_arg(&[
        ("operation", vm_string("show")),
        ("repo", vm_string(&repo.path().to_string_lossy())),
        ("rev", vm_string("--exec=rm")),
    ]))
    .unwrap_err();
    assert!(matches!(
        err,
        HostlibError::InvalidParameter { param: "rev", .. }
    ));
}

#[test]
fn git_rejects_unknown_operation() {
    let reg = registry();
    let entry = reg.find("hostlib_tools_git").unwrap();
    let err = (entry.handler)(&dict_arg(&[("operation", vm_string("rm-rf"))])).unwrap_err();
    assert!(matches!(
        err,
        HostlibError::InvalidParameter {
            param: "operation",
            ..
        }
    ));
}

#[test]
fn enable_builtin_flips_the_gate() {
    permissions::reset();
    let mut reg = BuiltinRegistry::new();
    ToolsCapability.register_builtins(&mut reg);

    // Pre-enable: search must refuse.
    let search = reg.find("hostlib_tools_search").unwrap();
    let err = (search.handler)(&dict_arg(&[("pattern", vm_string("foo"))])).unwrap_err();
    assert!(matches!(err, HostlibError::Backend { .. }));

    // hostlib_enable can be called with either a bare string or a dict.
    let enable = reg.find("hostlib_enable").unwrap();
    let result = (enable.handler)(&[vm_string("tools:deterministic")]).unwrap();
    if let VmValue::Dict(d) = &result {
        assert!(matches!(d.get("enabled"), Some(VmValue::Bool(true))));
        assert!(matches!(d.get("newly_enabled"), Some(VmValue::Bool(true))));
    } else {
        panic!("expected dict");
    }

    // After enable: search returns Ok (with no matches in a tmpdir).
    let dir = tempfile::TempDir::new().unwrap();
    let result = (search.handler)(&dict_arg(&[
        ("pattern", vm_string("foo")),
        ("path", vm_string(&dir.path().to_string_lossy())),
    ]))
    .unwrap();
    assert!(matches!(&result, VmValue::Dict(_)));

    // Calling enable a second time reports the feature as already on.
    let again = (enable.handler)(&[vm_string("tools:deterministic")]).unwrap();
    if let VmValue::Dict(d) = &again {
        assert!(matches!(d.get("newly_enabled"), Some(VmValue::Bool(false))));
    }
}

#[test]
fn enable_builtin_rejects_unknown_features() {
    let mut reg = BuiltinRegistry::new();
    ToolsCapability.register_builtins(&mut reg);
    let enable = reg.find("hostlib_enable").unwrap();
    let err = (enable.handler)(&[vm_string("tools:exec")]).unwrap_err();
    assert!(matches!(
        err,
        HostlibError::InvalidParameter {
            param: "feature",
            ..
        }
    ));
}