#![allow(clippy::expect_used)]
use polyplug_codegen::{GenerateConfig, Lang, Side};
use polyplugc::generate;
use std::path::Path;
use std::path::PathBuf;
fn workspace_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("parent of crates/polyplug_codegen")
.parent()
.expect("workspace root")
.to_path_buf()
}
fn generate_js_quickjs_bindings(api_toml: &Path, out_dir: &Path) {
let config = GenerateConfig {
api_toml: api_toml.to_path_buf(),
lang: Lang::JsQuickJs,
side: Side::Host,
out_dir: out_dir.to_path_buf(),
};
let output = generate(config).expect("polyplugc::generate failed");
for file in &output.files {
let file_path = out_dir.join(&file.path);
if let Some(parent) = file_path.parent() {
std::fs::create_dir_all(parent).expect("failed to create parent dir");
}
std::fs::write(&file_path, &file.content).expect("failed to write generated file");
}
}
#[test]
fn test_generate_js_quickjs_files_exist() {
let root: PathBuf = workspace_root();
let api_toml: PathBuf = root.join("tests").join("fixtures").join("test_api.toml");
let out_dir: PathBuf =
PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("integration_codegen_js_quickjs");
std::fs::create_dir_all(&out_dir).expect("create out_dir");
generate_js_quickjs_bindings(&api_toml, &out_dir);
let expected_files: &[&str] = &["host/types.ts", "host/callers.ts"];
for rel_path in expected_files {
let full_path: PathBuf = out_dir.join(rel_path);
assert!(
full_path.exists(),
"Expected file not found: {}",
full_path.display()
);
}
println!(
"test_generate_js_quickjs_files_exist: all {} files present ✓",
expected_files.len()
);
}
#[test]
fn test_js_host_caller_function_count_guard_is_vm_safe() {
let root: PathBuf = workspace_root();
let api_toml: PathBuf = root.join("examples").join("api.toml");
let out_dir: PathBuf =
PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("js_quickjs_vm_safe_guard");
std::fs::create_dir_all(&out_dir).expect("create out_dir");
generate_js_quickjs_bindings(&api_toml, &out_dir);
let callers: String = std::fs::read_to_string(out_dir.join("host/callers.ts"))
.expect("read generated host/callers.ts");
assert!(
callers.contains(">= this.#view.functionCount()"),
"expected a function-index guard in the generated JS host caller"
);
for line in callers.lines() {
if line.contains(">= this.#view.functionCount()") {
assert!(
line.contains("this.#view.functionCount() > 0 &&"),
"JS host caller guard is not VM-safe (rejects VM guests): {}",
line.trim()
);
}
}
println!("test_js_host_caller_function_count_guard_is_vm_safe: guard is VM-safe ✓");
}