test the wasm side (#6726)

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2025-05-06 20:04:34 -07:00
committed by GitHub
parent 17c326e654
commit e373d285fe
30 changed files with 594 additions and 330 deletions

View File

@ -48,8 +48,8 @@ impl Context {
) -> Result<kcl_lib::ExecutorContext, String> {
let config: kcl_lib::Configuration = serde_json::from_str(settings).map_err(|e| e.to_string())?;
let mut settings: kcl_lib::ExecutorSettings = config.into();
if let Some(path) = path {
settings.with_current_file(std::path::PathBuf::from(path));
if let Some(path_src) = path {
settings.with_current_file(kcl_lib::TypedPath::from(&path_src));
}
if is_mock {

View File

@ -4,6 +4,8 @@
mod context;
#[cfg(target_arch = "wasm32")]
mod lsp;
#[cfg(all(target_arch = "wasm32", test))]
mod tests;
#[cfg(target_arch = "wasm32")]
mod wasm;

View File

@ -0,0 +1,59 @@
//! Helper for starting a connection.
use std::env;
use kcl_lib::{
wasm_engine::{EngineCommandManager, FileSystemManager},
Program,
};
use pretty_assertions::assert_eq;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
#[wasm_bindgen]
pub async fn get_connection() -> Result<EngineCommandManager, JsValue> {
let token = if let Ok(token) = env::var("KITTYCAD_API_TOKEN") {
token
} else if let Ok(token) = env::var("ZOO_API_TOKEN") {
token
} else {
return Err("must set KITTYCAD_API_TOKEN or ZOO_API_TOKEN".into());
};
let mgr = EngineCommandManager::new();
let mgr_clone = mgr.clone();
wasm_bindgen_futures::spawn_local(async move {
// TODO: we should probably allow for setting the host as well.
mgr_clone.start_from_wasm(&token).await.unwrap();
});
// Return the JS object so the test can poke at it.
Ok(mgr)
}
#[wasm_bindgen]
pub async fn execute_code(code: &str) -> Result<JsValue, JsValue> {
let mgr = get_connection().await?;
let program = Program::parse_no_errs(code).map_err(String::from)?;
let ctx = crate::context::Context::new(mgr, FileSystemManager::new()).await?;
let result = ctx
.execute(&serde_json::to_string(&program).map_err(|e| e.to_string())?, None, "")
.await?;
Ok(result)
}
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
async fn wasm_basic_execution_single_file() {
let code = include_str!("../../../public/kcl-samples/gear/main.kcl");
let result = execute_code(code).await.unwrap();
assert_eq!(result, JsValue::from_str(""));
}