use std::{fs, path::Path, process::Command};
use serde_json::Value;
use tempfile::tempdir;
fn kayro() -> Command {
Command::new(env!("CARGO_BIN_EXE_kayro"))
}
fn fixture(root: &Path, extra_config: &str) {
fs::create_dir_all(root.join("src-kayro")).unwrap();
fs::write(root.join("src-kayro/Cargo.toml"), "[workspace]\n").unwrap();
fs::write(
root.join("kayro.toml"),
format!("[app]\nname = 'Test'\nidentifier = 'dev.test.app'\n{extra_config}"),
)
.unwrap();
}
fn json_lines(bytes: &[u8]) -> Vec<Value> {
String::from_utf8(bytes.to_vec())
.unwrap()
.lines()
.map(|line| serde_json::from_str(line).unwrap())
.collect()
}
#[test]
fn dry_run_is_a_stable_machine_readable_plan() {
let directory = tempdir().unwrap();
fixture(directory.path(), "");
let output = kayro()
.args([
"--json",
"--dry-run",
"build",
"--project",
directory.path().to_str().unwrap(),
"--target",
"aarch64-unknown-linux-gnu",
"--no-bundle",
])
.output()
.unwrap();
assert!(output.status.success());
assert!(output.stderr.is_empty());
let events = json_lines(&output.stdout);
assert_eq!(events.len(), 5);
assert_eq!(events[0]["event"], "project_resolved");
assert_eq!(events[4]["event"], "completed");
for (index, event) in events.iter().enumerate() {
assert_eq!(event["schema_version"], 1);
assert_eq!(event["sequence"], index + 1);
}
let native = events
.iter()
.find(|event| event["phase"] == "native")
.unwrap();
assert!(
native["args"]
.as_array()
.unwrap()
.iter()
.any(|argument| argument == "aarch64-unknown-linux-gnu")
);
}
#[test]
fn dry_run_reports_the_native_default_bundle_as_structured_data() {
let directory = tempdir().unwrap();
fixture(directory.path(), "");
let output = kayro()
.args([
"--json",
"--dry-run",
"build",
"--project",
directory.path().to_str().unwrap(),
])
.output()
.unwrap();
assert!(output.status.success());
let events = json_lines(&output.stdout);
let bundle = events
.iter()
.find(|event| event["event"] == "bundle_planned")
.unwrap();
let expected = if cfg!(target_os = "macos") {
"dmg"
} else if cfg!(target_os = "windows") {
"nsis"
} else {
"appimage"
};
assert_eq!(bundle["formats"], serde_json::json!([expected]));
}
#[test]
fn json_mode_keeps_argument_errors_off_stderr() {
let output = kayro()
.args([
"--json",
"build",
"--arch",
"arm64",
"--target",
"aarch64-apple-darwin",
])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(2));
assert!(output.stderr.is_empty());
let events = json_lines(&output.stdout);
assert_eq!(events.len(), 1);
assert_eq!(events[0]["event"], "error");
assert_eq!(events[0]["code"], "invalid_arguments");
}
#[test]
fn json_mode_applies_to_version_output() {
let output = kayro().args(["--json", "--version"]).output().unwrap();
assert!(output.status.success());
assert!(output.stderr.is_empty());
let events = json_lines(&output.stdout);
assert_eq!(events.len(), 1);
assert_eq!(events[0]["event"], "version");
assert_eq!(events[0]["schema_version"], 1);
assert!(events[0]["text"].as_str().unwrap().starts_with("kayro "));
}
#[test]
fn missing_tools_have_a_distinct_agent_error() {
let directory = tempdir().unwrap();
fixture(
directory.path(),
"\n[build]\nbuild_command = ['kayro-tool-that-does-not-exist']\n",
);
let output = kayro()
.args([
"--json",
"build",
"--project",
directory.path().to_str().unwrap(),
"--frontend-only",
"--skip-types",
])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(3));
assert!(output.stderr.is_empty());
let events = json_lines(&output.stdout);
assert_eq!(events.last().unwrap()["event"], "error");
assert_eq!(events.last().unwrap()["code"], "tool_not_found");
}
#[test]
fn new_creates_a_complete_project_without_installing_when_requested() {
let directory = tempdir().unwrap();
let project = directory.path().join("hello-app");
let output = kayro()
.args([
"new",
"Hello App",
"--directory",
project.to_str().unwrap(),
"--identifier",
"dev.example.hello",
"--no-install",
])
.output()
.unwrap();
assert!(output.status.success());
assert!(project.join("kayro.toml").is_file());
assert!(project.join("src-kayro/src/main.rs").is_file());
assert!(project.join("vite.config.ts").is_file());
assert!(!project.join("src-kayro/Cargo.lock").exists());
let package: Value =
serde_json::from_str(&fs::read_to_string(project.join("package.json")).unwrap()).unwrap();
assert_eq!(package["name"], "hello-app");
assert_eq!(package["scripts"]["dev"], "kayro dev");
let config = fs::read_to_string(project.join("kayro.toml")).unwrap();
assert!(config.contains("[bundle]\nactive = true"));
assert!(config.contains("[permissions.filesystem]"));
assert!(config.contains("read = [\"data\", \"config\", \"cache\"]"));
assert!(config.contains("max_file_size = 67108864"));
assert!(config.contains("[shell]\nmax_concurrent = 4"));
let native = fs::read_to_string(project.join("src-kayro/src/main.rs")).unwrap();
assert!(native.contains("load_or_embedded"));
assert!(native.contains(".manage(commands::AppState::default())"));
assert!(native.contains(".events(kayro::events![commands::GreetingSent])"));
let frontend = fs::read_to_string(project.join("src/main.ts")).unwrap();
assert!(frontend.contains("window.kayro.os.info()"));
let manifest = fs::read_to_string(project.join("src-kayro/Cargo.toml")).unwrap();
assert!(manifest.contains("kayro = \"0.5.0\""));
let second = kayro()
.args([
"new",
"Hello App",
"--directory",
project.to_str().unwrap(),
"--no-install",
])
.output()
.unwrap();
assert_eq!(second.status.code(), Some(2));
}
#[test]
fn init_preserves_an_existing_vite_frontend_byte_for_byte() {
let directory = tempdir().unwrap();
let package = r#"{"name":"existing","scripts":{"dev":"vite","build":"vite build"}}"#;
let main = "document.body.textContent = 'keep me';\n";
let vite_config = "export default {};\n";
fs::write(directory.path().join("package.json"), package).unwrap();
fs::create_dir_all(directory.path().join("src")).unwrap();
fs::write(directory.path().join("src/main.ts"), main).unwrap();
fs::write(directory.path().join("vite.config.js"), vite_config).unwrap();
let output = kayro()
.args(["init", directory.path().to_str().unwrap(), "--no-install"])
.output()
.unwrap();
assert!(output.status.success());
assert_eq!(
fs::read_to_string(directory.path().join("package.json")).unwrap(),
package
);
assert_eq!(
fs::read_to_string(directory.path().join("src/main.ts")).unwrap(),
main
);
assert_eq!(
fs::read_to_string(directory.path().join("vite.config.js")).unwrap(),
vite_config
);
assert!(!directory.path().join("vite.config.ts").exists());
assert!(!directory.path().join("src/style.css").exists());
let config = fs::read_to_string(directory.path().join("kayro.toml")).unwrap();
assert!(!config.contains("[build]"));
let plan = kayro()
.args([
"--json",
"--dry-run",
"dev",
"--project",
directory.path().to_str().unwrap(),
])
.output()
.unwrap();
assert!(plan.status.success());
let events = json_lines(&plan.stdout);
let web = events.iter().find(|event| event["phase"] == "web").unwrap();
assert_eq!(web["args"], serde_json::json!(["run", "dev"]));
}
#[test]
fn scaffold_dry_run_never_creates_the_target() {
let directory = tempdir().unwrap();
let project = directory.path().join("planned");
let output = kayro()
.args([
"--json",
"--dry-run",
"new",
"planned",
"--directory",
project.to_str().unwrap(),
"--no-install",
])
.output()
.unwrap();
assert!(output.status.success());
assert!(!project.exists());
let events = json_lines(&output.stdout);
assert!(events.iter().any(|event| event["event"] == "file_planned"));
assert!(
events
.iter()
.any(|event| event["event"] == "project_planned")
);
}
#[test]
fn new_rejects_vite_unsafe_parent_paths_before_writing() {
let directory = tempdir().unwrap();
let unsafe_parent = directory.path().join("c#-projects");
let project = unsafe_parent.join("app");
let output = kayro()
.args([
"--json",
"new",
"app",
"--directory",
project.to_str().unwrap(),
"--no-install",
])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(2));
assert!(!unsafe_parent.exists());
let events = json_lines(&output.stdout);
assert_eq!(events.len(), 1);
assert_eq!(events[0]["code"], "unsupported_vite_path");
assert!(events[0]["message"].as_str().unwrap().contains("`#`"));
}
#[test]
fn dev_rejects_an_unsafe_existing_path_before_starting_types() {
let directory = tempdir().unwrap();
let project = directory.path().join("c#-projects/app");
fs::create_dir_all(&project).unwrap();
fixture(&project, "");
let output = kayro()
.args([
"--json",
"--dry-run",
"dev",
"--project",
project.to_str().unwrap(),
])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(2));
let events = json_lines(&output.stdout);
assert_eq!(events.len(), 1);
assert_eq!(events[0]["code"], "unsupported_vite_path");
assert!(!events.iter().any(|event| event["phase"] == "types"));
}