harn-cli 0.7.27

CLI for the Harn programming language — run, test, REPL, format, and lint
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
use std::path::{Path, PathBuf};
use std::time::Instant;

use harn_lexer::Lexer;
use harn_parser::{Node, Parser};

pub struct TestResult {
    pub name: String,
    pub file: String,
    pub passed: bool,
    pub error: Option<String>,
    pub duration_ms: u64,
}

pub struct TestSummary {
    pub results: Vec<TestResult>,
    pub passed: usize,
    pub failed: usize,
    pub total: usize,
    pub duration_ms: u64,
}

fn canonicalize_existing_path(path: &Path) -> PathBuf {
    path.canonicalize().unwrap_or_else(|_| path.to_path_buf())
}

fn test_execution_cwd() -> PathBuf {
    std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))
}

/// Run all test_* pipelines in a single source file using the VM.
pub async fn run_test_file(
    path: &Path,
    filter: Option<&str>,
    timeout_ms: u64,
    execution_cwd: Option<&Path>,
) -> Result<Vec<TestResult>, String> {
    let source = std::fs::read_to_string(path)
        .map_err(|e| format!("Failed to read {}: {e}", path.display()))?;

    let mut lexer = Lexer::new(&source);
    let tokens = lexer.tokenize().map_err(|e| format!("{e}"))?;
    let mut parser = Parser::new(tokens);
    let program = parser.parse().map_err(|e| format!("{e}"))?;

    let test_names: Vec<String> = program
        .iter()
        .filter_map(|snode| {
            // Recognize either:
            //  - the legacy naming convention: `pipeline test_*`
            //  - the explicit `@test` attribute on a Pipeline (declarative)
            let (has_test_attr, decl_node) = match &snode.node {
                Node::AttributedDecl { attributes, inner } => {
                    (attributes.iter().any(|a| a.name == "test"), inner.as_ref())
                }
                _ => (false, snode),
            };
            let name = match &decl_node.node {
                Node::Pipeline { name, .. } => name.clone(),
                _ => return None,
            };
            if !(has_test_attr || name.starts_with("test_")) {
                return None;
            }
            if let Some(pattern) = filter {
                if !name.contains(pattern) {
                    return None;
                }
            }
            Some(name)
        })
        .collect();

    let mut results = Vec::new();

    for test_name in &test_names {
        harn_vm::reset_thread_local_state();

        let start = Instant::now();

        let chunk = match harn_vm::Compiler::new().compile_named(&program, test_name) {
            Ok(c) => c,
            Err(e) => {
                results.push(TestResult {
                    name: test_name.clone(),
                    file: path.display().to_string(),
                    passed: false,
                    error: Some(format!("Compile error: {e}")),
                    duration_ms: 0,
                });
                continue;
            }
        };

        let local = tokio::task::LocalSet::new();
        let path_str = path.display().to_string();
        let timeout = std::time::Duration::from_millis(timeout_ms);
        let previous_cwd = if let Some(cwd) = execution_cwd {
            let previous = std::env::current_dir().ok();
            std::env::set_current_dir(cwd).map_err(|error| {
                format!(
                    "Failed to set current directory to {}: {error}",
                    cwd.display()
                )
            })?;
            previous
        } else {
            None
        };
        let result = tokio::time::timeout(
            timeout,
            local.run_until(async {
                let mut vm = harn_vm::Vm::new();
                harn_vm::register_vm_stdlib(&mut vm);
                let source_parent = path.parent().unwrap_or(std::path::Path::new("."));
                let project_root = harn_vm::stdlib::process::find_project_root(source_parent);
                let store_base = project_root.as_deref().unwrap_or(source_parent);
                let execution_cwd = test_execution_cwd();
                let source_dir = source_parent.to_string_lossy().into_owned();
                harn_vm::register_store_builtins(&mut vm, store_base);
                harn_vm::register_metadata_builtins(&mut vm, store_base);
                let pipeline_name = path.file_stem().and_then(|s| s.to_str()).unwrap_or("test");
                harn_vm::register_checkpoint_builtins(&mut vm, store_base, pipeline_name);
                vm.set_source_info(&path_str, &source);
                harn_vm::stdlib::process::set_thread_execution_context(Some(
                    harn_vm::orchestration::RunExecutionRecord {
                        cwd: Some(execution_cwd.to_string_lossy().into_owned()),
                        source_dir: Some(source_dir),
                        env: std::collections::BTreeMap::new(),
                        adapter: None,
                        repo_path: None,
                        worktree_path: None,
                        branch: None,
                        base_ref: None,
                        cleanup: None,
                    },
                ));
                if let Some(ref root) = project_root {
                    vm.set_project_root(root);
                }
                if let Some(parent) = path.parent() {
                    if !parent.as_os_str().is_empty() {
                        vm.set_source_dir(parent);
                    }
                }
                let loaded =
                    crate::skill_loader::load_skills(&crate::skill_loader::SkillLoaderInputs {
                        cli_dirs: Vec::new(),
                        source_path: Some(path.to_path_buf()),
                    });
                crate::skill_loader::emit_loader_warnings(&loaded.loader_warnings);
                crate::skill_loader::install_skills_global(&mut vm, &loaded);
                let extensions = crate::package::load_runtime_extensions(path);
                crate::package::install_runtime_extensions(&extensions);
                crate::package::install_manifest_triggers(&mut vm, &extensions)
                    .await
                    .map_err(|error| format!("failed to install manifest triggers: {error}"))?;
                crate::package::install_manifest_hooks(&mut vm, &extensions)
                    .await
                    .map_err(|error| format!("failed to install manifest hooks: {error}"))?;
                match vm.execute(&chunk).await {
                    Ok(val) => Ok(val),
                    Err(e) => {
                        let formatted = vm.format_runtime_error(&e);
                        Err(formatted)
                    }
                }
            }),
        )
        .await;
        if let Some(previous) = previous_cwd {
            let _ = std::env::set_current_dir(previous);
        }

        let duration = start.elapsed().as_millis() as u64;

        match result {
            Ok(Ok(_)) => {
                results.push(TestResult {
                    name: test_name.clone(),
                    file: path.display().to_string(),
                    passed: true,
                    error: None,
                    duration_ms: duration,
                });
            }
            Ok(Err(e)) => {
                results.push(TestResult {
                    name: test_name.clone(),
                    file: path.display().to_string(),
                    passed: false,
                    error: Some(e),
                    duration_ms: duration,
                });
            }
            Err(_) => {
                results.push(TestResult {
                    name: test_name.clone(),
                    file: path.display().to_string(),
                    passed: false,
                    error: Some(format!("timed out after {timeout_ms}ms")),
                    duration_ms: timeout_ms,
                });
            }
        }
    }

    Ok(results)
}

/// Discover and run tests in a file or directory.
pub async fn run_tests(
    path: &Path,
    filter: Option<&str>,
    timeout_ms: u64,
    parallel: bool,
) -> TestSummary {
    // Default LLM provider to "mock" in test mode unless caller overrides.
    let prev_provider = std::env::var("HARN_LLM_PROVIDER").ok();
    if prev_provider.is_none() {
        std::env::set_var("HARN_LLM_PROVIDER", "mock");
    }

    let start = Instant::now();
    let mut all_results = Vec::new();

    let canonical_target = canonicalize_existing_path(path);
    let files = if canonical_target.is_dir() {
        discover_test_files(&canonical_target)
    } else {
        vec![canonical_target]
    };

    if parallel {
        let local = tokio::task::LocalSet::new();
        let results = local
            .run_until(async {
                let mut handles = Vec::new();
                for file in files {
                    let filter = filter.map(|s| s.to_string());
                    handles.push(tokio::task::spawn_local(async move {
                        run_test_file(&file, filter.as_deref(), timeout_ms, None).await
                    }));
                }
                let mut results = Vec::new();
                for handle in handles {
                    match handle.await {
                        Ok(Ok(r)) => results.extend(r),
                        Ok(Err(e)) => results.push(TestResult {
                            name: "<file error>".to_string(),
                            file: String::new(),
                            passed: false,
                            error: Some(e),
                            duration_ms: 0,
                        }),
                        Err(e) => results.push(TestResult {
                            name: "<join error>".to_string(),
                            file: String::new(),
                            passed: false,
                            error: Some(format!("{e}")),
                            duration_ms: 0,
                        }),
                    }
                }
                results
            })
            .await;
        all_results = results;
    } else {
        for file in &files {
            let execution_cwd = file
                .parent()
                .filter(|parent| !parent.as_os_str().is_empty());
            match run_test_file(file, filter, timeout_ms, execution_cwd).await {
                Ok(results) => all_results.extend(results),
                Err(e) => {
                    all_results.push(TestResult {
                        name: "<file error>".to_string(),
                        file: file.display().to_string(),
                        passed: false,
                        error: Some(e),
                        duration_ms: 0,
                    });
                }
            }
        }
    }

    match prev_provider {
        Some(val) => std::env::set_var("HARN_LLM_PROVIDER", val),
        None => std::env::remove_var("HARN_LLM_PROVIDER"),
    }

    let passed = all_results.iter().filter(|r| r.passed).count();
    let failed = all_results.iter().filter(|r| !r.passed).count();
    let total = all_results.len();

    TestSummary {
        results: all_results,
        passed,
        failed,
        total,
        duration_ms: start.elapsed().as_millis() as u64,
    }
}

fn discover_test_files(dir: &Path) -> Vec<PathBuf> {
    let mut files = Vec::new();
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                files.extend(discover_test_files(&path));
            } else if path.extension().is_some_and(|e| e == "harn") {
                if let Ok(content) = std::fs::read_to_string(&path) {
                    if content.contains("test_") {
                        files.push(canonicalize_existing_path(&path));
                    }
                }
            }
        }
    }
    files.sort();
    files
}

#[cfg(test)]
mod tests {
    use super::{discover_test_files, run_tests};
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::time::{SystemTime, UNIX_EPOCH};

    struct TempTestDir {
        path: PathBuf,
    }

    impl TempTestDir {
        fn new() -> Self {
            let unique = format!(
                "harn-test-runner-{}-{}",
                std::process::id(),
                SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap()
                    .as_nanos()
            );
            let path = std::env::temp_dir().join(unique);
            fs::create_dir_all(&path).unwrap();
            Self { path }
        }

        fn write(&self, relative: &str, contents: &str) {
            let path = self.path.join(relative);
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent).unwrap();
            }
            fs::write(path, contents).unwrap();
        }

        fn path(&self) -> &Path {
            &self.path
        }
    }

    impl Drop for TempTestDir {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.path);
        }
    }

    #[test]
    fn discover_test_files_returns_canonical_absolute_paths() {
        let temp = TempTestDir::new();
        temp.write("suite/test_alpha.harn", "pipeline test_alpha(task) {}");
        temp.write("suite/nested/test_beta.harn", "pipeline test_beta(task) {}");
        temp.write("suite/ignore.harn", "pipeline build(task) {}");

        // Pass an absolute path rather than mutating process-wide cwd — the
        // other test_runner test asserts cwd preservation, and mutating it
        // from two tests concurrently causes cross-test flakiness.
        let files = discover_test_files(&temp.path().join("suite"));

        assert_eq!(files.len(), 2);
        assert!(files.iter().all(|path| path.is_absolute()));
        assert!(files
            .iter()
            .any(|path| path.ends_with("suite/test_alpha.harn")));
        assert!(files
            .iter()
            .any(|path| path.ends_with("suite/nested/test_beta.harn")));
    }

    #[tokio::test]
    async fn run_tests_uses_file_parent_as_execution_cwd_and_restores_shell_cwd() {
        let _cwd_guard = crate::tests::common::cwd_lock::lock_cwd_async().await;
        let _env_guard = crate::tests::common::env_lock::lock_env().lock().await;
        let temp = TempTestDir::new();
        temp.write(
            "suite/test_cwd.harn",
            r#"
pipeline test_current_dir(task) {
  assert_eq(cwd(), source_dir())
}
"#,
        );

        let original_cwd = std::env::current_dir().unwrap();
        let summary = run_tests(&temp.path().join("suite"), None, 1_000, false).await;
        let restored_cwd = std::env::current_dir().unwrap();

        assert_eq!(summary.failed, 0);
        assert_eq!(summary.passed, 1);
        assert_eq!(
            fs::canonicalize(restored_cwd).unwrap(),
            fs::canonicalize(original_cwd).unwrap()
        );
    }
}