nativeview-cli 0.1.0-rc.1

Developer CLI (`nv`) for Native View Engine: dev hot-reload, builds, pairing, SDK management, app scaffolding, and docs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Unit tests for `nv build` / `nv run` host cache, macOS bundle IDs, and dry-run planning.

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());

    // Generator mismatch (cached Ninja vs non-Ninja VS generator) wipes stale cache.
    assert!(reset_stale_cmake_cache(&build_dir, &src_a, None).unwrap());
    assert!(!build_dir.exists());

    // Source directory mismatch wipes stale cache.
    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());

    // Windows MSVC Ninja requires `cl.exe` on PATH; non-Windows only requires `ninja`.
    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));

    // Windows implib selection prefers `.dll.lib` on MSVC and `.dll.a` on GNU.
    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"));

    // `--pair-only` / `--pair-uri` / `--pair-dev` dry-run plans skip the app cdylib.
    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"));

    // iOS-only flags rejected on non-ios-sim targets.
    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}
        ]
      }
    }"#;

    // Booted iPad wins over shutdown iPhone when no iPhone is booted.
    assert_eq!(select_simulator_device(sample_json, None).unwrap().udid, "BOOT-IPAD");

    // Numeric runtime sort: iOS-18-10 > iOS-18-2 for duplicate `--device "iPhone 16 Pro"`.
    assert_eq!(
        select_simulator_device(sample_json, Some("iPhone 16 Pro")).unwrap().udid,
        "DUP-NEW"
    );

    // Booted duplicate beats shutdown newer duplicate for explicit `--device`.
    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"
    );
    // And default prefers booted iPhone over booted iPad.
    assert_eq!(select_simulator_device(&dup_booted_old, None).unwrap().udid, "DUP-OLD");

    // Exact UDID and case-insensitive substring queries work.
    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());

    // When none booted, picks newest available iPhone.
    let none_booted = sample_json.replace("\"Booted\"", "\"Shutdown\"");
    assert_eq!(select_simulator_device(&none_booted, None).unwrap().udid, "NEW-IPHONE");

    // Xcode 27 Simulator.app -> DeviceHub.app -> OpenByName fallback.
    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);

    // Cache dir isolates `min_ios` + CMake configure args lock (including Apple Clang pins).
    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");
    // `asset://icons/a%20b.png` maps to `assets/icons/a%20b.png` next to the document.
    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");
    // No `assets/` directory: nothing staged, no error.
    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);
}