#![cfg(feature = "localfs")]
#![allow(clippy::unwrap_used, clippy::expect_used)]
use kaish_kernel::{Kernel, KernelConfig};
async fn run(script: &str) -> (i64, String) {
let kernel = Kernel::new(KernelConfig::repl()).expect("kernel");
let r = kernel.execute(script).await.expect("execute");
(r.code, r.text_out().trim().to_string())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn seq_into_jq_uses_structured_data() {
for i in 0..200 {
let (code, out) = run("seq 1 3 | jq -c .").await;
assert_eq!(code, 0, "iter {i}: seq | jq should succeed, got out={out:?}");
assert_eq!(out, "[1,2,3]", "iter {i}: jq should see the structured array, not raw text");
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn cut_into_jq_uses_structured_data() {
for i in 0..200 {
let (code, out) = run("seq 1 3 | cut -f1 | jq -c 'length'").await;
assert_eq!(code, 0, "iter {i}: out={out:?}");
assert_eq!(out, "3", "iter {i}");
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn text_json_into_jq_still_parses() {
for i in 0..50 {
let (code, out) = run("echo '{\"a\":1}' | jq -c '.a'").await;
assert_eq!(code, 0, "iter {i}: out={out:?}");
assert_eq!(out, "1", "iter {i}");
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn seq_into_scatter_sees_structured_items() {
for i in 0..100 {
let kernel = Kernel::new(KernelConfig::repl().with_skip_validation(true)).expect("kernel");
let r = kernel.execute("seq 1 3 | scatter").await.expect("execute");
assert_eq!(r.code, 0, "iter {i}: out={:?} err={:?}", r.text_out(), r.err);
assert!(
r.text_out().contains("3 items"),
"iter {i}: expected 3 items, got {:?}",
r.text_out()
);
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_substitution_in_the_consumer_does_not_eat_the_structured_value() {
let (code, out) = run("seq 1 3 | jq -c $(echo .)").await;
assert_eq!(code, 0, "seq | jq $(echo .) should succeed, got out={out:?}");
assert_eq!(
out, "[1,2,3]",
"a `$()` in the consumer's argv must not cost it the structured value"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_quoted_substitution_in_the_consumer_is_the_same_case() {
let (code, out) = run("seq 1 3 | jq -c \"$(echo .)\"").await;
assert_eq!(code, 0, "quoted form should succeed, got out={out:?}");
assert_eq!(out, "[1,2,3]");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_function_body_consumer_keeps_the_structured_value() {
let (code, out) = run("f() { jq -c .; }; seq 1 3 | f").await;
assert_eq!(code, 0, "function-body consumer should succeed, got out={out:?}");
assert_eq!(out, "[1,2,3]");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_text_consumer_was_never_affected() {
let (code, out) = run("seq 1 3 | wc -l").await;
assert_eq!(code, 0);
assert_eq!(out, "3");
}