use std::path::Path; use wapm_targz_to_pirita::unpack_tar_gz; use wapm_to_webc::*; use webc::{ metadata::{Manifest, ManifestWithoutAtomSignatures}, v1::DirOrFile, v2::read::OwnedReader, }; #[cfg(not(target_arch = "wasm32"))] #[test] fn test_hotg_full() {} #[test] fn test_parse_manifest() { let _ = serde_json::from_str::(include_str!( "../wapm2pirita.manifest.json" )) .unwrap(); } fn write_tar_gz_archive_into_dir(unpacked_test_data: &wapm_to_webc::FileMap, temp_dir: &Path) { for (path, file_contents) in unpacked_test_data.iter() { match path { DirOrFile::Dir(_) => {} DirOrFile::File(fp) => { let target_path = temp_dir.join(fp); if let Some(parent) = target_path.parent() { let _ = std::fs::create_dir_all(parent); } let _ = std::fs::write(&target_path, file_contents); } }; } } static FIXTURE_WITH_BINDINGS_TAR_GZ: &[u8] = include_bytes!("../test/fixtures/fixture-with-bindings.tar.gz"); fn create_hotg_example_webc_with_bindings() -> OwnedReader { let unpacked_test_data = unpack_tar_gz(FIXTURE_WITH_BINDINGS_TAR_GZ.to_vec()).unwrap(); let temp_dir = webc::v1::webc_temp_dir() .join("test") .join("webc-with-bindings"); let _ = std::fs::remove_dir_all(&temp_dir); let _ = std::fs::create_dir_all(&temp_dir); let _ = std::fs::write( temp_dir.join("wasmer.toml"), r#" [package] name = "abc/test-package" version = "0.1.0" description = "sine" [[module]] name = "mod-with-exports" source = "mylib.wasm" bindings = { wit-exports = "exports.wit", wit-bindgen = "0.0.0" } [[command]] name = "run" module = "mod-with-exports" "#, ); write_tar_gz_archive_into_dir(&unpacked_test_data, &temp_dir); let out_dir = webc::v1::webc_temp_dir() .join("test") .join("webc-with-bindings.webc"); let _ = std::fs::remove_file(&out_dir); let cmd = PackOptions { inpath: temp_dir, out: out_dir.clone(), target: TargetVersion::V2, }; cmd.run().map_err(|e| format!("{cmd:#?}: {e}")).unwrap(); let webc = std::fs::read(out_dir).unwrap(); OwnedReader::parse(webc).unwrap() } static SINE_TAR_GZ: &[u8] = include_bytes!("../test/fixtures/sine.tar.gz"); static HOTG_TOML: &str = include_str!("../test/fixtures/wapm.toml"); fn create_hotg_example_webc() -> OwnedReader { let toml = HOTG_TOML; let unpacked_test_data = unpack_tar_gz(SINE_TAR_GZ.to_vec()).unwrap(); let temp_dir = webc::v1::webc_temp_dir().join("test").join("hotg"); let _ = std::fs::remove_dir_all(&temp_dir); let _ = std::fs::create_dir_all(&temp_dir); let _ = std::fs::write(temp_dir.join("wasmer.toml"), toml.as_bytes()); write_tar_gz_archive_into_dir(&unpacked_test_data, &temp_dir); let out_dir = webc::v1::webc_temp_dir().join("test").join("hotg-out.webc"); let _ = std::fs::remove_file(&out_dir); let cmd = PackOptions { inpath: temp_dir, out: out_dir.clone(), target: TargetVersion::V2, }; cmd.run().map_err(|e| format!("{cmd:#?}: {e}")).unwrap(); let webc = std::fs::read(out_dir).unwrap(); OwnedReader::parse(webc).unwrap() } #[test] fn test_convert_bindings() { let webc = create_hotg_example_webc_with_bindings(); let target_manifest = r#" { "package": { "wapm": { "name": "abc/test-package", "version": "0.1.0", "description": "sine" } }, "atoms": { "mod-with-exports": { "kind": "https://webc.org/kind/wasm", "signature": "sha256:GyPhg9dRScTVBPqp02pQQ+LKA3t3fXrNP6NLH1oiMpE=" } }, "commands": { "run": { "runner": "https://webc.org/runner/wasi", "annotations": { "wasi": { "atom": "mod-with-exports", "package": null, "main_args": null } } } }, "bindings": [ { "name": "library-bindings", "kind": "wit@0.0.0", "annotations": { "wit": { "module": "atoms://mod-with-exports", "exports": "metadata://exports.wit" } } } ], "entrypoint": "run" } "#; let target_manifest: Manifest = serde_json::from_str(target_manifest).unwrap(); pretty_assertions::assert_eq!(webc.manifest(), &target_manifest); } #[test] fn test_1_hotg() { let webc = create_hotg_example_webc(); let target_manifest = r#" { "use": { "elastic_net": "hotg-ai/elastic_net@^0.12.1", "train_test_split": "hotg-ai/train_test_split@^0.12.1" }, "package": { "wapm": { "name": "hotg-ai/sine", "version": "0.12.0", "description": "sine" } }, "atoms": { "sine": { "kind": "https://webc.org/kind/tensorflow-SavedModel", "signature": "sha256:+cDvAUxR9iBFVGTs9VlIri21hldwxDrd+sekUnMvo/k=" } }, "commands": { "run": { "runner": "https://webc.org/runner/rune", "annotations": { "image": "runicos/base", "version": 1, "pipeline": { "rand": { "args": { "length": "4" }, "outputs": [ { "type": "F32", "dimensions": [ 1, 1 ] } ], "capability": "proc_blocks/rand" }, "sine": { "model": "models/sine", "inputs": [ "mod360" ], "outputs": [ { "type": "F32", "dimensions": [ 1, 1 ] } ] }, "mod360": { "args": { "modulus": "360" }, "inputs": [ "rand" ], "outputs": [ { "type": "F32", "dimensions": [ 1, 1 ] } ], "proc-block": "proc_blocks/mod360" }, "serial": { "out": "serial", "inputs": [ "sine" ] } }, "resources": {} } } }, "entrypoint": "run" } "#; let target_manifest = serde_json::from_str(target_manifest).unwrap(); pretty_assertions::assert_eq!(webc.manifest(), &target_manifest); }