use std::fs;
use std::path::{Path, PathBuf};
use nativeview_cli::manifest::{ManifestAppInfo, NvManifest};
use super::host_cache::{
cmake_configure_args, host_cache_dir, locate_cmake_shell_binary, modules_cache_key,
reset_stale_cmake_cache, select_windows_implib, should_use_ninja_for_host, HostShell,
};
use super::apple::macos_bundle::{macos_bundle_id, should_direct_exec_macos};
use super::pipeline::{
resolve_target_and_shell, resolved_out_dir, run_build_command, run_run_command,
AppBuildOptions, AppRunOptions, BuildTarget,
};
use super::plan_format::desktop_shell_launch_args;
use super::web_bundle::{build_web_app, copy_file_ensuring_parent, stage_app_assets};
use crate::cargo_env::{self, AppContext, NvSdkRoot};
use crate::static_http::mime_type_for;
#[test]
fn host_cache_dir_and_cmake_args_encode_modules_and_zero_recursive_cargo() {
assert_eq!(modules_cache_key(&[]), "base");
assert_eq!(modules_cache_key(&["blur".into(), "camera".into()]), "blur+camera");
let cache = host_cache_dir(
Path::new("/opt/nv/target"),
HostShell::Macos,
false,
&["camera".into()],
);
assert_eq!(cache, PathBuf::from("/opt/nv/target/nv-hosts/macos-debug-camera"));
let args = cmake_configure_args(
Path::new("/opt/nv/hosts/macos-shell"),
&cache,
HostShell::Macos,
false,
&["blur".into(), "camera".into()],
Path::new("/opt/nv/target/debug/libnv_runtime.a"),
None,
true,
true,
);
assert!(args.contains(&"-G".to_owned()));
assert!(args.contains(&"Ninja".to_owned()));
assert!(args.contains(&"-DNV_SKIP_CODEGEN=ON".to_owned()));
assert!(args.contains(&"-DNV_RUNTIME_PREBUILT_LIB=/opt/nv/target/debug/libnv_runtime.a".to_owned()));
assert!(args.contains(&"-DNV_HOST_SANITIZE=OFF".to_owned()));
assert!(args.contains(&"-DNV_MODULES=blur;camera".to_owned()));
assert!(args.contains(&"-DCMAKE_C_COMPILER=/usr/bin/clang".to_owned()));
assert!(args.contains(&"-DCMAKE_CXX_COMPILER=/usr/bin/clang++".to_owned()));
let win_args = cmake_configure_args(
Path::new("/opt/nv/hosts/windows-shell"),
Path::new("/opt/nv/target/nv-hosts/windows-release-base"),
HostShell::Windows,
true,
&[],
Path::new("/opt/nv/target/release/nv_runtime.lib"),
Some((
Path::new("/opt/nv/target/release/nativeview_accessibility_windows.dll"),
Path::new("/opt/nv/target/release/nativeview_accessibility_windows.dll.lib"),
)),
false,
false,
);
assert!(win_args.contains(&"-DNV_MODULES=".to_owned()));
assert!(win_args.contains(&"-DNV_A11Y_WIN_PREBUILT_DLL=/opt/nv/target/release/nativeview_accessibility_windows.dll".to_owned()));
assert!(win_args.contains(&"-DNV_A11Y_WIN_PREBUILT_IMPLIB=/opt/nv/target/release/nativeview_accessibility_windows.dll.lib".to_owned()));
assert!(!win_args.iter().any(|a| a.contains("/usr/bin/clang")));
}
#[test]
fn stale_cmake_cache_detection_and_multiconfig_binary_probe() {
let tmp = std::env::temp_dir().join(format!("nv-host-cache-test-{}", std::process::id()));
let _ = fs::remove_dir_all(&tmp);
let src_a = tmp.join("worktree-a/hosts/macos-shell");
let src_b = tmp.join("worktree-b/hosts/macos-shell");
let build_dir = tmp.join("build");
fs::create_dir_all(&src_a).unwrap();
fs::create_dir_all(&src_b).unwrap();
fs::create_dir_all(build_dir.join("Release")).unwrap();
fs::write(
build_dir.join("CMakeCache.txt"),
format!(
"CMAKE_HOME_DIRECTORY:INTERNAL={}\nCMAKE_GENERATOR:INTERNAL=Ninja\n",
src_a.display()
),
)
.unwrap();
fs::write(build_dir.join("Release/nv_windows_shell.exe"), b"mz").unwrap();
assert_eq!(
locate_cmake_shell_binary(&build_dir, "nv_windows_shell", true, ".exe"),
Some(build_dir.join("Release/nv_windows_shell.exe"))
);
assert!(!reset_stale_cmake_cache(&build_dir, &src_a, Some("Ninja")).unwrap());
assert!(build_dir.exists());
assert!(reset_stale_cmake_cache(&build_dir, &src_a, None).unwrap());
assert!(!build_dir.exists());
fs::create_dir_all(&build_dir).unwrap();
fs::write(
build_dir.join("CMakeCache.txt"),
format!("CMAKE_HOME_DIRECTORY:INTERNAL={}\nCMAKE_GENERATOR:INTERNAL=Ninja\n", src_a.display()),
)
.unwrap();
assert!(reset_stale_cmake_cache(&build_dir, &src_b, Some("Ninja")).unwrap());
assert!(!build_dir.exists());
assert!(!should_use_ninja_for_host(true, true, false));
assert!(should_use_ninja_for_host(true, true, true));
assert!(should_use_ninja_for_host(false, true, false));
let lib_dir = tmp.join("winlibs");
fs::create_dir_all(&lib_dir).unwrap();
let msvc_lib = lib_dir.join("nativeview_accessibility_windows.dll.lib");
let gnu_lib = lib_dir.join("libnativeview_accessibility_windows.dll.a");
fs::write(&msvc_lib, b"lib").unwrap();
fs::write(&gnu_lib, b"a").unwrap();
let siblings = vec![gnu_lib.clone(), msvc_lib.clone()];
assert_eq!(select_windows_implib(&siblings, &lib_dir, true), Some(msvc_lib));
assert_eq!(select_windows_implib(&siblings, &lib_dir, false), Some(gnu_lib));
let _ = fs::remove_dir_all(&tmp);
}
#[test]
fn target_shell_validation_bundle_ids_and_launch_args() {
let manifest = NvManifest::parse_toml(
r#"
[app]
name = "NV Camera Demo"
id = "io.github.nativeview.camera.demo"
presence = "adaptive"
width = 920
height = 860
[app.shell]
linux = "skia"
"#,
)
.unwrap();
let info = ManifestAppInfo::new("nativeview-example-camera-demo", "0.1.0", "nativeview_camera_demo");
assert_eq!(macos_bundle_id(&manifest, &info, HostShell::Macos), "io.github.nativeview.camera.demo");
assert_eq!(macos_bundle_id(&manifest, &info, HostShell::Skia), "io.github.nativeview.camera.demo.skia");
assert_eq!(
resolve_target_and_shell(Some("linux"), None, &manifest).unwrap(),
(BuildTarget::Linux, Some(HostShell::Skia))
);
assert_eq!(
resolve_target_and_shell(Some("linux"), Some("qt"), &manifest).unwrap(),
(BuildTarget::Linux, Some(HostShell::Qt))
);
assert!(resolve_target_and_shell(Some("macos"), Some("gtk"), &manifest).is_err());
assert!(resolve_target_and_shell(Some("web"), Some("skia"), &manifest).is_err());
let out = resolved_out_dir(
Path::new("/opt/nv/target"),
"nativeview_camera_demo",
BuildTarget::Macos,
Some(HostShell::Skia),
None,
Path::new("/opt/nv"),
);
assert_eq!(out, PathBuf::from("/opt/nv/target/nv-apps/nativeview_camera_demo/macos-skia"));
assert!(should_direct_exec_macos(true, &[]));
assert!(should_direct_exec_macos(false, &["--screenshot".into(), "out.png".into()]));
assert!(should_direct_exec_macos(false, &["--help".into()]));
assert!(!should_direct_exec_macos(false, &["--width".into(), "800".into()]));
assert_eq!(mime_type_for(Path::new("manifest.json")), "application/json; charset=utf-8");
assert_eq!(mime_type_for(Path::new("app.webmanifest")), "application/json; charset=utf-8");
let args = desktop_shell_launch_args(
HostShell::Skia,
Path::new("/out/libnativeview_camera_demo.so"),
&manifest,
&["--backend".into(), "cpu".into()],
);
assert_eq!(
args,
vec![
"--dylib",
"/out/libnativeview_camera_demo.so",
"--presence",
"adaptive",
"--width",
"920",
"--height",
"860",
"--no-ws",
"--backend",
"cpu",
]
);
}
#[test]
fn dry_run_plans_for_real_examples_cover_all_desktop_and_web_targets() {
let root = cargo_env::find_workspace_root();
let camera_dir = root.join("packages/modules/camera/example");
let scene_dir = root.join("packages/modules/app-scene/example");
let cam_macos = run_build_command(
AppBuildOptions {
app_path: Some(camera_dir.clone()),
package: None,
target: Some("macos".into()),
shell: Some("macos".into()),
kit: None,
platforms: None,
release: false,
out: None,
sdk_root: Some(root.clone()),
dry_run: true,
},
&root,
)
.expect("camera macos dry-run");
assert!(cam_macos.contains("modules: camera"));
assert!(cam_macos.contains("-DNV_MODULES=camera"));
assert!(cam_macos.contains("io.github.nativeview.camera.demo"));
let cam_linux = run_run_command(
AppRunOptions {
app_path: Some(camera_dir.clone()),
package: None,
target: Some("linux".into()),
shell: None,
release: false,
sdk_root: Some(root.clone()),
http_port: 8090,
direct: false,
device: None,
headless: false,
pair_only: false,
pair_uri: None,
pair_dev: false,
dry_run: true,
shell_args: Vec::new(),
},
&root,
)
.expect("camera linux dry-run");
assert!(cam_linux.contains("shell=skia"));
assert!(cam_linux.contains("--width 920 --height 860 --no-ws"));
let cam_macos_direct = run_run_command(
AppRunOptions {
app_path: Some(camera_dir.clone()),
package: None,
target: Some("macos".into()),
shell: Some("skia".into()),
release: false,
sdk_root: Some(root.clone()),
http_port: 8090,
direct: false,
device: None,
headless: false,
pair_only: false,
pair_uri: None,
pair_dev: false,
dry_run: true,
shell_args: vec!["--screenshot".into(), "out.png".into()],
},
&root,
)
.expect("camera macos screenshot direct dry-run");
assert!(cam_macos_direct.contains("Contents/MacOS/NvCameraDemo --screenshot out.png"));
assert!(!cam_macos_direct.contains("open -W -n"));
let cam_ios = run_run_command(
AppRunOptions {
app_path: Some(camera_dir),
package: None,
target: Some("ios-sim".into()),
shell: None,
release: false,
sdk_root: Some(root.clone()),
http_port: 8090,
direct: false,
device: Some("iPhone 16 Pro".into()),
headless: true,
pair_only: false,
pair_uri: None,
pair_dev: false,
dry_run: true,
shell_args: Vec::new(),
},
&root,
)
.expect("camera ios-sim dry-run");
assert!(cam_ios.contains("modules: camera"));
assert!(cam_ios.contains("--target aarch64-apple-ios-sim"));
assert!(cam_ios.contains("-DCMAKE_SYSTEM_NAME=iOS"));
assert!(cam_ios.contains("-DNV_MODULES=camera"));
assert!(cam_ios.contains("ios-sim-shell-debug-ios17.0-camera"));
assert!(cam_ios.contains("-DCMAKE_C_COMPILER=/usr/bin/clang"));
assert!(cam_ios.contains("NvCameraDemo.app/libnv_app.dylib"));
assert!(cam_ios.contains("device iPhone 16 Pro (headless=true)"));
assert!(cam_ios.contains("xcrun simctl launch iPhone 16 Pro io.github.nativeview.camera.demo"));
let cam_pair_uri = run_run_command(
AppRunOptions {
app_path: Some(scene_dir.clone()),
package: None,
target: Some("ios-sim".into()),
shell: None,
release: false,
sdk_root: Some(root.clone()),
http_port: 8090,
direct: false,
device: None,
headless: false,
pair_only: false,
pair_uri: Some("nv://pair?host=127.0.0.1:8092".into()),
pair_dev: false,
dry_run: true,
shell_args: Vec::new(),
},
&root,
)
.expect("ios-sim --pair-uri dry-run");
assert!(cam_pair_uri.contains("skip --pair-only"));
assert!(!cam_pair_uri.contains("libnv_app.dylib"));
assert!(cam_pair_uri.contains("SIMCTL_CHILD_NV_PAIR_CONNECT=nv://pair?host=127.0.0.1:8092"));
let cam_pair_dev = run_run_command(
AppRunOptions {
app_path: Some(scene_dir.clone()),
package: None,
target: Some("ios-sim".into()),
shell: None,
release: false,
sdk_root: Some(root.clone()),
http_port: 8090,
direct: false,
device: None,
headless: true,
pair_only: false,
pair_uri: None,
pair_dev: true,
dry_run: true,
shell_args: Vec::new(),
},
&root,
)
.expect("ios-sim --pair-dev dry-run");
assert!(cam_pair_dev.contains("skip --pair-only"));
let bad_flag = run_run_command(
AppRunOptions {
app_path: Some(scene_dir.clone()),
package: None,
target: Some("macos".into()),
shell: None,
release: false,
sdk_root: Some(root.clone()),
http_port: 8090,
direct: false,
device: None,
headless: true,
pair_only: false,
pair_uri: None,
pair_dev: false,
dry_run: true,
shell_args: Vec::new(),
},
&root,
)
.expect_err("must reject --headless on macos target");
assert!(bad_flag.contains("only supported with --target ios-sim"), "got: {bad_flag}");
let scene_macos = run_build_command(
AppBuildOptions {
app_path: Some(scene_dir.clone()),
package: None,
target: Some("macos".into()),
shell: Some("macos".into()),
kit: None,
platforms: None,
release: false,
out: None,
sdk_root: Some(root.clone()),
dry_run: true,
},
&root,
)
.expect("app-scene macos dry-run");
assert!(scene_macos.contains("modules: base"));
assert!(scene_macos.contains("-DNV_MODULES="));
let scene_web = run_run_command(
AppRunOptions {
app_path: Some(scene_dir),
package: None,
target: Some("web".into()),
shell: None,
release: true,
sdk_root: Some(root.clone()),
http_port: 8093,
direct: false,
device: None,
headless: false,
pair_only: false,
pair_uri: None,
pair_dev: false,
dry_run: true,
shell_args: Vec::new(),
},
&root,
)
.expect("app-scene web dry-run");
assert!(scene_web.contains("--target wasm32-unknown-unknown"));
assert!(scene_web.contains("ext "));
assert!(scene_web.contains("http://localhost:8093/"));
}
#[test]
fn ios_sim_device_selection_and_gui_fallback_cover_all_modes() {
use super::apple::ios_bundle::{
ios_sim_cmake_configure_args, ios_sim_host_cache_dir, resolve_simulator_gui_app,
select_simulator_device, SimulatorGuiApp,
};
let sample_json = r#"{
"devices": {
"com.apple.CoreSimulator.SimRuntime.tvOS-18-0": [
{"udid": "TV-1", "name": "Apple TV", "state": "Booted", "isAvailable": true}
],
"com.apple.CoreSimulator.SimRuntime.iOS-18-2": [
{"udid": "OLD-IPAD", "name": "iPad Air", "state": "Shutdown", "isAvailable": true},
{"udid": "DUP-OLD", "name": "iPhone 16 Pro", "state": "Shutdown", "isAvailable": true}
],
"com.apple.CoreSimulator.SimRuntime.iOS-18-10": [
{"udid": "DUP-NEW", "name": "iPhone 16 Pro", "state": "Shutdown", "isAvailable": true},
{"udid": "BOOT-IPAD", "name": "iPad Pro", "state": "Booted", "isAvailable": true}
],
"com.apple.CoreSimulator.SimRuntime.iOS-27-0": [
{"udid": "NEW-IPHONE", "name": "iPhone 17 Pro", "state": "Shutdown", "isAvailable": true},
{"udid": "UNAVAIL", "name": "iPhone 17 Max", "state": "Shutdown", "isAvailable": false}
]
}
}"#;
assert_eq!(select_simulator_device(sample_json, None).unwrap().udid, "BOOT-IPAD");
assert_eq!(
select_simulator_device(sample_json, Some("iPhone 16 Pro")).unwrap().udid,
"DUP-NEW"
);
let dup_booted_old = sample_json.replace(
r#"{"udid": "DUP-OLD", "name": "iPhone 16 Pro", "state": "Shutdown""#,
r#"{"udid": "DUP-OLD", "name": "iPhone 16 Pro", "state": "Booted""#,
);
assert_eq!(
select_simulator_device(&dup_booted_old, Some("iPhone 16 Pro")).unwrap().udid,
"DUP-OLD"
);
assert_eq!(select_simulator_device(&dup_booted_old, None).unwrap().udid, "DUP-OLD");
assert_eq!(select_simulator_device(sample_json, Some("NEW-IPHONE")).unwrap().name, "iPhone 17 Pro");
assert_eq!(select_simulator_device(sample_json, Some("ipad air")).unwrap().udid, "OLD-IPAD");
assert!(select_simulator_device(sample_json, Some("iPhone 99")).is_err());
let none_booted = sample_json.replace("\"Booted\"", "\"Shutdown\"");
assert_eq!(select_simulator_device(&none_booted, None).unwrap().udid, "NEW-IPHONE");
let tmp = std::env::temp_dir().join(format!("nv-ios-gui-test-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&tmp);
let dev_dir = tmp.join("Xcode.app/Contents/Developer");
std::fs::create_dir_all(&dev_dir).unwrap();
assert_eq!(resolve_simulator_gui_app(Some(&dev_dir)), SimulatorGuiApp::OpenByName);
let hub = tmp.join("Xcode.app/Contents/Applications/DeviceHub.app");
std::fs::create_dir_all(&hub).unwrap();
assert_eq!(resolve_simulator_gui_app(Some(&dev_dir)), SimulatorGuiApp::DeviceHubApp(hub));
let sim = dev_dir.join("Applications/Simulator.app");
std::fs::create_dir_all(&sim).unwrap();
assert_eq!(resolve_simulator_gui_app(Some(&dev_dir)), SimulatorGuiApp::SimulatorApp(sim));
let _ = std::fs::remove_dir_all(&tmp);
let mods = vec!["camera".to_owned()];
assert_eq!(
ios_sim_host_cache_dir(Path::new("/tmp/t"), false, "17.0", &mods),
PathBuf::from("/tmp/t/nv-hosts/ios-sim-shell-debug-ios17.0-camera")
);
assert_ne!(
ios_sim_host_cache_dir(Path::new("/tmp/t"), false, "17.0", &mods),
ios_sim_host_cache_dir(Path::new("/tmp/t"), false, "18.0", &mods)
);
let cfg = ios_sim_cmake_configure_args(
Path::new("/sdk/hosts/ios-shell"),
Path::new("/tmp/b"),
false,
&mods,
Path::new("/tmp/t/aarch64-apple-ios-sim/debug/libnv_runtime.a"),
"17.0",
true,
);
assert!(cfg.contains(&"-DCMAKE_SYSTEM_NAME=iOS".to_owned()));
assert!(cfg.contains(&"-DCMAKE_OSX_SYSROOT=iphonesimulator".to_owned()));
assert!(cfg.contains(&"-DCMAKE_OSX_ARCHITECTURES=arm64".to_owned()));
assert!(cfg.contains(&"-DCMAKE_OSX_DEPLOYMENT_TARGET=17.0".to_owned()));
assert!(cfg.contains(&"-DCMAKE_C_COMPILER=/usr/bin/clang".to_owned()));
assert!(cfg.contains(&"-DCMAKE_OBJCXX_COMPILER=/usr/bin/clang++".to_owned()));
assert!(cfg.contains(&"-DNV_SKIP_CODEGEN=ON".to_owned()));
assert!(cfg.contains(&"-DNV_MODULES=camera".to_owned()));
#[cfg(target_os = "macos")]
{
use super::apple::ios_bundle::package_ios_sim_app_bundle;
use nativeview_cli::manifest::ManifestAppInfo;
let pkg_tmp =
std::env::temp_dir().join(format!("nv-ios-pkg-test-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&pkg_tmp);
std::fs::create_dir_all(&pkg_tmp).unwrap();
let fake_bin = Path::new("/usr/bin/true");
let manifest = NvManifest::parse_toml(
r#"
[app]
name = "Pkg Test"
id = "io.github.nativeview.pkg.test"
executable = "PkgTest"
[apple.entitlements]
"com.apple.security.device.camera" = true
"#,
)
.unwrap();
let info = ManifestAppInfo {
package_name: "pkg-test".into(),
package_version: "0.1.0".into(),
lib_name: "pkg_test".into(),
};
let out = package_ios_sim_app_bundle(&pkg_tmp, &manifest, &info, fake_bin, None)
.expect("pair-only package_ios_sim_app_bundle must succeed on macOS");
assert!(out.dylib_path.is_none());
let plist = std::fs::read_to_string(&out.plist_path).unwrap();
assert!(plist.contains("<string>NvIosSceneDelegate</string>"));
assert!(plist.contains("<key>UILaunchScreen</key>"));
assert!(plist.contains("<string>nv</string>"));
assert!(plist.contains("<key>NSLocalNetworkUsageDescription</key>"));
let _ = std::fs::remove_dir_all(&pkg_tmp);
}
}
#[test]
fn web_bundle_errors_without_web_feature_or_missing_assets() {
let app = AppContext {
manifest_path: PathBuf::from("/opt/app/Cargo.toml"),
manifest_dir: PathBuf::from("/opt/app"),
workspace_root: PathBuf::from("/opt/app"),
target_directory: PathBuf::from("/opt/app/target"),
package_id: "app@0.1.0".into(),
package_name: "no-web-app".into(),
package_version: "0.1.0".into(),
lib_name: "no_web_app".into(),
has_web_feature: false,
has_lockfile: false,
host_modules: Vec::new(),
path_deps: Vec::new(),
};
let err = build_web_app(
&app,
Some(&NvSdkRoot(PathBuf::from("/opt/nv"))),
&NvManifest::default(),
Path::new("/tmp/out"),
false,
)
.expect_err("must reject package without [features] web");
assert!(err.contains("web"), "got: {err}");
let missing_err =
copy_file_ensuring_parent(Path::new("/tmp/nv-does-not-exist-xyz"), Path::new("/tmp/dst"))
.expect_err("must reject missing web asset");
assert!(missing_err.contains("required asset not found"), "got: {missing_err}");
}
#[test]
fn web_stage_copies_the_app_assets_dir_for_asset_uris() {
let base = std::env::temp_dir().join(format!("nv_stage_app_assets_{}", std::process::id()));
let _ = fs::remove_dir_all(&base);
let (app, stage) = (base.join("app"), base.join("stage"));
fs::create_dir_all(app.join("assets/icons/nested")).expect("app assets");
fs::write(app.join("assets/icons/a b.png"), "png").expect("asset");
fs::write(app.join("assets/icons/nested/x.svg"), "svg").expect("nested asset");
fs::create_dir_all(&stage).expect("stage");
stage_app_assets(&app, &stage).expect("stage assets");
assert_eq!(fs::read_to_string(stage.join("assets/icons/a b.png")).expect("copied"), "png");
assert_eq!(fs::read_to_string(stage.join("assets/icons/nested/x.svg")).expect("copied"), "svg");
let bare = base.join("bare");
fs::create_dir_all(&bare).expect("bare");
stage_app_assets(&bare, &stage.join("bare-out")).expect("no assets is fine");
assert!(!stage.join("bare-out").exists());
let _ = fs::remove_dir_all(&base);
}