vesper-player-cli 0.5.3

The vesper command-line tool for authoring, validating, and installing plugins.
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
// The CLI parses this request on every host before the macOS compatibility
// gate rejects unsupported execution.
#![cfg_attr(not(target_os = "macos"), allow(dead_code))]

use std::io::Write;
use std::path::{Path, PathBuf};

use crate::ios::IosError;

const EXPECTED_ACCEPTANCE_TESTS: u64 = 3;

pub(crate) struct IosOptionalPluginDeviceRequest {
    pub(crate) release_directory: PathBuf,
    pub(crate) device: String,
    pub(crate) development_team: String,
    pub(crate) output_directory: PathBuf,
    pub(crate) allow_provisioning_updates: bool,
}

pub(crate) fn ensure_supported_host() -> Result<(), IosError> {
    if cfg!(target_os = "macos") {
        Ok(())
    } else {
        Err(IosError::compatibility(
            "iOS optional-plugin device verification requires macOS",
        ))
    }
}

pub(crate) fn verify(
    root: &Path,
    request: IosOptionalPluginDeviceRequest,
    output: &mut dyn Write,
    diagnostics: &mut dyn Write,
) -> Result<(), IosError> {
    ensure_supported_host()?;

    #[cfg(target_os = "macos")]
    {
        implementation::verify(root, request, output, diagnostics)
    }
    #[cfg(not(target_os = "macos"))]
    {
        let _ = (root, request, output, diagnostics);
        unreachable!("the host gate rejects non-macOS verification")
    }
}

#[cfg(target_os = "macos")]
mod implementation {
    use std::collections::BTreeSet;
    use std::env;
    use std::ffi::OsStr;
    use std::fs;
    use std::io::{self, Write};
    use std::path::{Path, PathBuf};
    use std::process::{Command, ExitStatus, Stdio};
    use std::time::Duration;

    use nix::unistd::{AccessFlags, access};
    use serde::{Deserialize, Serialize};
    use serde_json::Value;

    use super::{EXPECTED_ACCEPTANCE_TESTS, IosError, IosOptionalPluginDeviceRequest};
    use crate::external_process::{self, ExternalProcessErrorKind};
    use crate::ios_optional_release::OptionalReleaseArchiveEvidence;

    const MAX_PROCESS_OUTPUT_BYTES: usize = 64 * 1024 * 1024;
    const MAX_XCODEGEN_SPEC_BYTES: usize = 4 * 1024 * 1024;
    const MAX_XCRESULT_SUMMARY_BYTES: usize = 1024 * 1024;
    const MAX_XCRESULT_TESTS_BYTES: usize = 4 * 1024 * 1024;
    const MAX_XCRESULT_TEST_NODES: usize = 4096;
    const MAX_XCODE_PRODUCT_ENTRIES: usize = 4096;
    const MAX_DEVICE_IDENTIFIER_BYTES: usize = 256;
    const MAX_TEAM_IDENTIFIER_BYTES: usize = 64;
    const DEVICE_XCTEST_TIMEOUT: Duration = Duration::from_secs(10 * 60);
    const TEST_CLASS: &str = "VesperPlayerHostDemoTests/VesperOptionalPluginDeviceAcceptanceTests";
    const TEST_CLASS_NAME: &str = "VesperOptionalPluginDeviceAcceptanceTests";

    #[derive(Debug)]
    struct RequiredTools {
        xcodegen: PathBuf,
        xcodebuild: PathBuf,
        xcrun: PathBuf,
    }

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct XcresultSummary {
        result: String,
        total_test_count: u64,
        passed_tests: u64,
        failed_tests: u64,
        skipped_tests: u64,
        expected_failures: u64,
    }

    #[derive(Debug, Deserialize)]
    struct XcresultTests {
        #[serde(default, rename = "testNodes")]
        test_nodes: Vec<XcresultTestNode>,
    }

    #[derive(Debug, Deserialize)]
    struct XcresultTestNode {
        #[serde(default)]
        children: Vec<XcresultTestNode>,
        name: String,
        #[serde(rename = "nodeType")]
        node_type: String,
    }

    #[derive(Serialize)]
    struct VerifiedReleaseProvenance<'a> {
        schema_version: u32,
        canonical_release_directory: &'a str,
        archives: &'a [OptionalReleaseArchiveEvidence],
    }

    pub(super) fn verify(
        root: &Path,
        request: IosOptionalPluginDeviceRequest,
        output: &mut dyn Write,
        diagnostics: &mut dyn Write,
    ) -> Result<(), IosError> {
        validate_identifier(
            &request.device,
            MAX_DEVICE_IDENTIFIER_BYTES,
            "iOS device identifier",
        )?;
        validate_team_identifier(&request.development_team)?;
        let tools = resolve_required_tools()?;
        let release_directory = require_regular_directory(
            &request.release_directory,
            "complete iOS release directory",
        )?;
        let example_project_directory = require_regular_directory(
            &root.join("examples/ios-swift-host"),
            "iOS example project directory",
        )?;
        let project_manifest = require_regular_file(
            &example_project_directory.join("project.yml"),
            "iOS example XcodeGen manifest",
        )?;
        let player_kit = require_regular_directory(
            &root.join("lib/ios/VesperPlayerKit"),
            "VesperPlayerKit package directory",
        )?;
        let verified = crate::ios_optional_release::prepare_verified_optional_release_snapshot(
            root,
            &release_directory,
        )?;
        let release_directory = verified.canonical_release_directory().to_path_buf();
        let output_directory = create_output_directory(&request.output_directory)?;
        let optional_package = output_directory.join("VerifiedOptionalPluginsPackage");
        verified.materialize_optional_package(&optional_package)?;
        let acceptance_project_directory = output_directory.join("Project");
        fs::create_dir(&acceptance_project_directory).map_err(|error| {
            IosError::storage(format!(
                "failed to create isolated iOS optional-plugin project directory '{}': {error}",
                acceptance_project_directory.display()
            ))
        })?;

        let device_spec = acceptance_project_directory.join("device-project.json");
        let resolved_spec = dump_project_spec(
            &tools.xcodegen,
            &project_manifest,
            &example_project_directory,
            diagnostics,
        )?;
        write_device_project_spec(
            &device_spec,
            resolved_spec,
            &player_kit,
            &optional_package,
            verified.optional_frameworks(),
        )?;
        let provenance = output_directory.join("verified-release-inputs.json");
        write_verified_release_provenance(
            &provenance,
            &release_directory,
            verified.archive_evidence(),
        )?;
        let derived_data = output_directory.join("DerivedData");
        let result_bundle = output_directory.join("VesperOptionalPlugins.xcresult");

        let mut generate = Command::new(&tools.xcodegen);
        generate
            .arg("generate")
            .arg("--spec")
            .arg(&device_spec)
            .arg("--project")
            .arg(&acceptance_project_directory)
            .arg("--project-root")
            .arg(&example_project_directory)
            .arg("--no-env")
            .current_dir(&acceptance_project_directory)
            .stdin(Stdio::null());
        run_captured(
            &mut generate,
            "iOS optional-plugin Xcode project generation",
            diagnostics,
        )?;

        let project = require_regular_directory(
            &acceptance_project_directory.join("VesperPlayerHostDemo.xcodeproj"),
            "generated iOS example Xcode project",
        )?;
        let destination = format!("platform=iOS,id={}", request.device);
        let mut build = Command::new(&tools.xcodebuild);
        build
            .arg("build-for-testing")
            .arg("-project")
            .arg(&project)
            .arg("-scheme")
            .arg("VesperPlayerHostDemo")
            .arg("-configuration")
            .arg("Release")
            .arg("-destination")
            .arg(&destination)
            .arg("-destination-timeout")
            .arg("120")
            .arg("-derivedDataPath")
            .arg(&derived_data)
            .arg(format!("DEVELOPMENT_TEAM={}", request.development_team))
            .arg("CODE_SIGN_STYLE=Automatic")
            .arg("ENABLE_TESTABILITY=YES")
            .arg("ARCHS=arm64")
            .arg("ONLY_ACTIVE_ARCH=YES")
            .arg("-parallel-testing-enabled")
            .arg("NO")
            .arg(format!("-only-testing:{TEST_CLASS}"))
            .stdin(Stdio::null());
        if request.allow_provisioning_updates {
            build.arg("-allowProvisioningUpdates");
        }
        run_captured(
            &mut build,
            "iOS optional-plugin Release build-for-testing",
            diagnostics,
        )?;

        let products = require_regular_directory(
            &derived_data.join("Build/Products"),
            "iOS optional-plugin Xcode build products",
        )?;
        let application = require_regular_directory(
            &products.join("Release-iphoneos/VesperPlayerHostDemo.app"),
            "iOS optional-plugin Release application",
        )?;
        let xctestrun = discover_xctestrun(&products)?;
        let profile_hash =
            crate::ios::verify_embedded_optional_framework_contract(&application, true)?;

        let mut test = Command::new(&tools.xcodebuild);
        test.arg("test-without-building")
            .arg("-xctestrun")
            .arg(&xctestrun)
            .arg("-destination")
            .arg(&destination)
            .arg("-destination-timeout")
            .arg("120")
            .arg("-resultBundlePath")
            .arg(&result_bundle)
            .arg("-parallel-testing-enabled")
            .arg("NO")
            .arg(format!("-only-testing:{TEST_CLASS}"))
            .stdin(Stdio::null());
        if request.allow_provisioning_updates {
            test.arg("-allowProvisioningUpdates");
        }
        let test_status = run_captured_status_with_timeout(
            &mut test,
            "iOS optional-plugin Release device XCTest",
            diagnostics,
            DEVICE_XCTEST_TIMEOUT,
        )?;
        let result_bundle = match require_regular_directory(
            &result_bundle,
            "iOS optional-plugin XCResult bundle",
        ) {
            Ok(result_bundle) => result_bundle,
            Err(_) if !test_status.success() => {
                return classify_status(test_status, "iOS optional-plugin Release device XCTest");
            }
            Err(error) => return Err(error),
        };

        let tests = read_xcresult_tests(&tools.xcrun, &result_bundle, diagnostics)?;
        validate_acceptance_test_tree(&tests, test_status.success())?;
        let summary = read_xcresult_summary(&tools.xcrun, &result_bundle, diagnostics)?;
        validate_summary(&summary)?;
        classify_status(test_status, "iOS optional-plugin Release device XCTest")?;
        writeln!(
            output,
            "Verified {EXPECTED_ACCEPTANCE_TESTS} iOS optional-plugin Release tests on device {} (0 failed, 0 skipped).",
            request.device
        )
        .and_then(|_| writeln!(output, "Release directory: {}", release_directory.display()))
        .and_then(|_| writeln!(output, "Verified application: {}", application.display()))
        .and_then(|_| writeln!(output, "FFmpeg profile hash: {profile_hash}"))
        .and_then(|_| writeln!(output, "Provenance: {}", provenance.display()))
        .and_then(|_| writeln!(output, "XCResult: {}", result_bundle.display()))
        .and_then(|_| {
            for artifact in verified.archive_evidence() {
                writeln!(
                    output,
                    "Release artifact SHA-256: {} {}",
                    artifact.archive_name, artifact.release_archive_sha256
                )?;
                writeln!(
                    output,
                    "Tested artifact SHA-256: {} {}",
                    artifact.archive_name, artifact.verified_archive_sha256
                )?;
            }
            Ok(())
        })
        .map_err(|error| output_error("write verification result", error))
    }

    fn discover_xctestrun(products: &Path) -> Result<PathBuf, IosError> {
        let entries = fs::read_dir(products).map_err(|error| {
            IosError::storage(format!(
                "failed to scan iOS optional-plugin Xcode products '{}': {error}",
                products.display()
            ))
        })?;
        let mut matches = Vec::new();
        let mut count = 0_usize;
        for entry in entries {
            count = count.checked_add(1).ok_or_else(|| {
                IosError::conformance("iOS optional-plugin Xcode product count overflowed")
            })?;
            if count > MAX_XCODE_PRODUCT_ENTRIES {
                return Err(IosError::conformance(format!(
                    "iOS optional-plugin Xcode products contain more than {MAX_XCODE_PRODUCT_ENTRIES} entries"
                )));
            }
            let entry = entry.map_err(|error| {
                IosError::storage(format!(
                    "failed to inspect an iOS optional-plugin Xcode product: {error}"
                ))
            })?;
            let path = entry.path();
            let metadata = fs::symlink_metadata(&path).map_err(|error| {
                IosError::storage(format!(
                    "failed to inspect iOS optional-plugin Xcode product '{}': {error}",
                    path.display()
                ))
            })?;
            if metadata.file_type().is_file() && path.extension() == Some(OsStr::new("xctestrun")) {
                matches.push(path);
            }
        }
        match matches.as_slice() {
            [path] => Ok(path.clone()),
            _ => Err(IosError::conformance(format!(
                "iOS optional-plugin device verification requires exactly one xctestrun file, found {} under '{}'",
                matches.len(),
                products.display()
            ))),
        }
    }

    fn write_device_project_spec(
        path: &Path,
        mut resolved_spec: Value,
        player_kit: &Path,
        optional_package: &Path,
        frameworks: &'static [&'static str],
    ) -> Result<(), IosError> {
        let player_kit = require_regular_directory(player_kit, "VesperPlayerKit package")?;
        let player_kit = player_kit.to_str().ok_or_else(|| {
            IosError::storage(format!(
                "VesperPlayerKit package path is not valid UTF-8: {}",
                player_kit.display()
            ))
        })?;
        let optional_package =
            require_regular_directory(optional_package, "verified optional-plugin Swift package")?;
        let optional_artifacts = require_regular_directory(
            &optional_package.join("Artifacts"),
            "verified optional-plugin artifact directory",
        )?;
        let root = resolved_spec
            .as_object_mut()
            .ok_or_else(|| IosError::conformance("XcodeGen project dump is not an object"))?;
        let packages = root
            .get_mut("packages")
            .and_then(Value::as_object_mut)
            .ok_or_else(|| IosError::conformance("XcodeGen project dump has no packages object"))?;
        let player_kit_package = packages
            .get_mut("VesperPlayerKit")
            .and_then(Value::as_object_mut)
            .ok_or_else(|| {
                IosError::conformance(
                    "XcodeGen project dump has no VesperPlayerKit package declaration",
                )
            })?;
        if player_kit_package.len() != 1
            || !player_kit_package.get("path").is_some_and(Value::is_string)
        {
            return Err(IosError::conformance(
                "canonical VesperPlayerKit package declaration must contain only a local path",
            ));
        }
        player_kit_package.insert("path".to_owned(), Value::String(player_kit.to_owned()));
        if packages.contains_key("VesperPlayerOptionalPlugins") {
            return Err(IosError::conformance(
                "canonical iOS example already declares VesperPlayerOptionalPlugins",
            ));
        }
        let targets = root
            .get_mut("targets")
            .and_then(Value::as_object_mut)
            .ok_or_else(|| IosError::conformance("XcodeGen project dump has no targets object"))?;
        let application = targets
            .get_mut("VesperPlayerHostDemo")
            .and_then(Value::as_object_mut)
            .ok_or_else(|| {
                IosError::conformance(
                    "XcodeGen project dump has no VesperPlayerHostDemo application target",
                )
            })?;
        let dependencies = application
            .get_mut("dependencies")
            .and_then(Value::as_array_mut)
            .ok_or_else(|| {
                IosError::conformance(
                    "XcodeGen project dump has no VesperPlayerHostDemo dependency list",
                )
            })?;
        let mut replaced = BTreeSet::new();
        for dependency in dependencies {
            let Some(object) = dependency.as_object() else {
                continue;
            };
            if object
                .get("package")
                .and_then(Value::as_str)
                .is_some_and(|package| package == "VesperPlayerOptionalPlugins")
            {
                return Err(IosError::conformance(
                    "canonical iOS example contains an unexpected optional-plugin package dependency",
                ));
            }
            let Some(framework_path) = object.get("framework").and_then(Value::as_str) else {
                continue;
            };
            let matched = frameworks.iter().copied().find(|framework| {
                framework_path
                    == format!(
                        "../../lib/ios/VesperPlayerOptionalPlugins/Artifacts/{framework}.xcframework"
                    )
            });
            if let Some(framework) = matched {
                if !replaced.insert(framework) {
                    return Err(IosError::conformance(format!(
                        "canonical iOS example declares {framework} more than once"
                    )));
                }
                let verified_framework = require_regular_directory(
                    &optional_artifacts.join(format!("{framework}.xcframework")),
                    &format!("verified {framework} XCFramework"),
                )?;
                let verified_framework = verified_framework.to_str().ok_or_else(|| {
                    IosError::storage(format!(
                        "verified {framework} XCFramework path is not valid UTF-8: {}",
                        verified_framework.display()
                    ))
                })?;
                *dependency = serde_json::json!({
                    "framework": verified_framework,
                    "embed": true,
                    "link": false,
                    "codeSign": true,
                });
            } else if framework_path.contains("VesperPlayerOptionalPlugins/Artifacts") {
                return Err(IosError::conformance(format!(
                    "canonical iOS example contains an unknown optional-plugin artifact dependency: {framework_path}"
                )));
            }
        }
        let missing = frameworks
            .iter()
            .copied()
            .filter(|framework| !replaced.contains(framework))
            .collect::<Vec<_>>();
        if !missing.is_empty() {
            return Err(IosError::conformance(format!(
                "canonical iOS example is missing optional-plugin artifact dependencies: {}",
                missing.join(", ")
            )));
        }

        let project_directory = path.parent().ok_or_else(|| {
            IosError::storage(format!(
                "isolated iOS optional-plugin project spec must have a parent: {}",
                path.display()
            ))
        })?;
        let generated_directory = project_directory.join("Generated");
        fs::create_dir(&generated_directory).map_err(|error| {
            IosError::storage(format!(
                "failed to create isolated iOS optional-plugin generated-file directory '{}': {error}",
                generated_directory.display()
            ))
        })?;
        let generated_info = generated_directory.join("Info.plist");
        let generated_info = generated_info.to_str().ok_or_else(|| {
            IosError::storage(format!(
                "isolated iOS optional-plugin Info.plist path is not valid UTF-8: {}",
                generated_info.display()
            ))
        })?;
        let info = application
            .get_mut("info")
            .and_then(Value::as_object_mut)
            .ok_or_else(|| {
                IosError::conformance(
                    "XcodeGen project dump has no VesperPlayerHostDemo Info.plist declaration",
                )
            })?;
        let canonical_info_path = info
            .get("path")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                IosError::conformance(
                    "canonical VesperPlayerHostDemo Info.plist path must be a string",
                )
            })?
            .to_owned();
        exclude_canonical_info_from_sources(application, &canonical_info_path)?;
        let info = application
            .get_mut("info")
            .and_then(Value::as_object_mut)
            .ok_or_else(|| {
                IosError::worker(
                    "VesperPlayerHostDemo Info.plist declaration disappeared during isolation",
                )
            })?;
        info.insert("path".to_owned(), Value::String(generated_info.to_owned()));

        if contains_workspace_optional_artifact_reference(&resolved_spec) {
            return Err(IosError::worker(
                "isolated iOS optional-plugin project still references workspace artifacts",
            ));
        }
        let bytes = serde_json::to_vec_pretty(&resolved_spec).map_err(|error| {
            IosError::worker(format!(
                "failed to encode isolated iOS optional-plugin device project: {error}"
            ))
        })?;
        let mut file = fs::OpenOptions::new()
            .create_new(true)
            .write(true)
            .open(path)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to create isolated iOS optional-plugin device project '{}': {error}",
                    path.display()
                ))
            })?;
        file.write_all(&bytes)
            .and_then(|()| file.write_all(b"\n"))
            .and_then(|()| file.sync_all())
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to write isolated iOS optional-plugin device project '{}': {error}",
                    path.display()
                ))
            })
    }

    fn exclude_canonical_info_from_sources(
        application: &mut serde_json::Map<String, Value>,
        canonical_info_path: &str,
    ) -> Result<(), IosError> {
        let info_path = Path::new(canonical_info_path);
        if info_path.is_absolute()
            || info_path
                .components()
                .any(|component| !matches!(component, std::path::Component::Normal(_)))
        {
            return Err(IosError::conformance(format!(
                "canonical VesperPlayerHostDemo Info.plist path must be a normalized relative path: {canonical_info_path}"
            )));
        }
        let sources = application
            .get_mut("sources")
            .and_then(Value::as_array_mut)
            .ok_or_else(|| {
                IosError::conformance(
                    "XcodeGen project dump has no VesperPlayerHostDemo source list",
                )
            })?;
        let mut covering_sources = 0_usize;
        for source in sources {
            let source_path = match source {
                Value::String(path) => path.as_str(),
                Value::Object(object) => {
                    object.get("path").and_then(Value::as_str).ok_or_else(|| {
                        IosError::conformance(
                            "canonical VesperPlayerHostDemo source object has no path string",
                        )
                    })?
                }
                _ => {
                    return Err(IosError::conformance(
                        "canonical VesperPlayerHostDemo source entry must be a path or object",
                    ));
                }
            };
            let source_path = Path::new(source_path);
            if source_path.is_absolute()
                || source_path
                    .components()
                    .any(|component| !matches!(component, std::path::Component::Normal(_)))
            {
                return Err(IosError::conformance(
                    "canonical VesperPlayerHostDemo source paths must be normalized and relative",
                ));
            }
            let Ok(relative_info) = info_path.strip_prefix(source_path) else {
                continue;
            };
            if relative_info.as_os_str().is_empty() {
                return Err(IosError::conformance(
                    "canonical VesperPlayerHostDemo Info.plist must be contained by a source directory",
                ));
            }
            covering_sources = covering_sources.checked_add(1).ok_or_else(|| {
                IosError::worker("VesperPlayerHostDemo source coverage count overflowed")
            })?;
            let relative_info = relative_info.to_str().ok_or_else(|| {
                IosError::conformance(
                    "canonical VesperPlayerHostDemo Info.plist exclusion is not valid UTF-8",
                )
            })?;
            match source {
                Value::String(path) => {
                    let path = path.clone();
                    *source = serde_json::json!({
                        "path": path,
                        "excludes": [relative_info],
                    });
                }
                Value::Object(object) => {
                    let excludes = object
                        .entry("excludes".to_owned())
                        .or_insert_with(|| Value::Array(Vec::new()))
                        .as_array_mut()
                        .ok_or_else(|| {
                            IosError::conformance(
                                "canonical VesperPlayerHostDemo source excludes must be an array",
                            )
                        })?;
                    if !excludes
                        .iter()
                        .any(|exclude| exclude.as_str() == Some(relative_info))
                    {
                        excludes.push(Value::String(relative_info.to_owned()));
                    }
                }
                _ => unreachable!("source entry shape was validated above"),
            }
        }
        if covering_sources != 1 {
            return Err(IosError::conformance(format!(
                "canonical VesperPlayerHostDemo Info.plist must be covered by exactly one source directory, found {covering_sources}"
            )));
        }
        Ok(())
    }

    fn contains_workspace_optional_artifact_reference(value: &Value) -> bool {
        match value {
            Value::String(value) => value.contains("VesperPlayerOptionalPlugins/Artifacts"),
            Value::Array(values) => values
                .iter()
                .any(contains_workspace_optional_artifact_reference),
            Value::Object(values) => values
                .values()
                .any(contains_workspace_optional_artifact_reference),
            Value::Null | Value::Bool(_) | Value::Number(_) => false,
        }
    }

    fn dump_project_spec(
        xcodegen: &Path,
        project_manifest: &Path,
        project_root: &Path,
        diagnostics: &mut dyn Write,
    ) -> Result<Value, IosError> {
        let mut command = Command::new(xcodegen);
        command
            .arg("dump")
            .arg("--type")
            .arg("json")
            .arg("--no-env")
            .arg("--spec")
            .arg(project_manifest)
            .arg("--project-root")
            .arg(project_root)
            .current_dir(project_root)
            .stdin(Stdio::null());
        let result = external_process::run_interruptible_capture(
            &mut command,
            "iOS optional-plugin XcodeGen project dump",
            MAX_XCODEGEN_SPEC_BYTES,
            MAX_PROCESS_OUTPUT_BYTES,
        )
        .map_err(map_process_error)?;
        diagnostics.write_all(&result.stderr).map_err(|error| {
            output_error("write iOS optional-plugin XcodeGen dump diagnostics", error)
        })?;
        classify_status(result.status, "iOS optional-plugin XcodeGen project dump")?;
        serde_json::from_slice(&result.stdout).map_err(|error| {
            IosError::conformance(format!(
                "iOS optional-plugin XcodeGen project dump is invalid JSON: {error}"
            ))
        })
    }

    fn write_verified_release_provenance(
        path: &Path,
        release_directory: &Path,
        archives: &[OptionalReleaseArchiveEvidence],
    ) -> Result<(), IosError> {
        let canonical_release_directory = release_directory.to_str().ok_or_else(|| {
            IosError::storage(format!(
                "canonical iOS release directory is not valid UTF-8: {}",
                release_directory.display()
            ))
        })?;
        let provenance = VerifiedReleaseProvenance {
            schema_version: 1,
            canonical_release_directory,
            archives,
        };
        let bytes = serde_json::to_vec_pretty(&provenance).map_err(|error| {
            IosError::worker(format!(
                "failed to encode verified iOS release provenance: {error}"
            ))
        })?;
        let mut file = fs::OpenOptions::new()
            .create_new(true)
            .write(true)
            .open(path)
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to create verified iOS release provenance '{}': {error}",
                    path.display()
                ))
            })?;
        file.write_all(&bytes)
            .and_then(|()| file.write_all(b"\n"))
            .and_then(|()| file.sync_all())
            .map_err(|error| {
                IosError::storage(format!(
                    "failed to write verified iOS release provenance '{}': {error}",
                    path.display()
                ))
            })
    }

    fn validate_identifier(value: &str, maximum_bytes: usize, label: &str) -> Result<(), IosError> {
        if value.is_empty()
            || value.len() > maximum_bytes
            || value.trim() != value
            || value.chars().any(char::is_control)
        {
            return Err(IosError::compatibility(format!(
                "{label} must be non-empty, at most {maximum_bytes} bytes, and contain no surrounding whitespace or control characters"
            )));
        }
        Ok(())
    }

    fn validate_team_identifier(value: &str) -> Result<(), IosError> {
        validate_identifier(
            value,
            MAX_TEAM_IDENTIFIER_BYTES,
            "Apple Development Team identifier",
        )?;
        if !value.bytes().all(|byte| byte.is_ascii_alphanumeric()) {
            return Err(IosError::compatibility(
                "Apple Development Team identifier must contain only ASCII letters and digits",
            ));
        }
        Ok(())
    }

    fn create_output_directory(path: &Path) -> Result<PathBuf, IosError> {
        if path.as_os_str().is_empty() {
            return Err(IosError::storage(
                "iOS optional-plugin device output directory must not be empty",
            ));
        }
        match fs::symlink_metadata(path) {
            Ok(_) => {
                return Err(IosError::storage(format!(
                    "iOS optional-plugin device output directory already exists: {}",
                    path.display()
                )));
            }
            Err(error) if error.kind() == io::ErrorKind::NotFound => {}
            Err(error) => {
                return Err(IosError::storage(format!(
                    "failed to inspect iOS optional-plugin device output directory '{}': {error}",
                    path.display()
                )));
            }
        }
        let parent = path
            .parent()
            .filter(|parent| !parent.as_os_str().is_empty())
            .ok_or_else(|| {
                IosError::storage(format!(
                    "iOS optional-plugin device output directory must have an existing parent: {}",
                    path.display()
                ))
            })?;
        require_regular_directory(parent, "iOS optional-plugin device output parent")?;
        fs::create_dir(path).map_err(|error| {
            IosError::storage(format!(
                "failed to create iOS optional-plugin device output directory '{}': {error}",
                path.display()
            ))
        })?;
        fs::canonicalize(path).map_err(|error| {
            IosError::storage(format!(
                "failed to resolve iOS optional-plugin device output directory '{}': {error}",
                path.display()
            ))
        })
    }

    fn read_xcresult_summary(
        xcrun: &Path,
        result_bundle: &Path,
        diagnostics: &mut dyn Write,
    ) -> Result<XcresultSummary, IosError> {
        let mut command = Command::new(xcrun);
        command
            .args(["xcresulttool", "get", "test-results", "summary", "--path"])
            .arg(result_bundle)
            .stdin(Stdio::null());
        let result = external_process::run_interruptible_capture(
            &mut command,
            "iOS optional-plugin XCResult summary",
            MAX_XCRESULT_SUMMARY_BYTES,
            MAX_XCRESULT_SUMMARY_BYTES,
        )
        .map_err(map_process_error)?;
        diagnostics.write_all(&result.stderr).map_err(|error| {
            output_error("write iOS optional-plugin XCResult diagnostics", error)
        })?;
        classify_status(result.status, "iOS optional-plugin XCResult summary")?;
        serde_json::from_slice(&result.stdout).map_err(|error| {
            IosError::conformance(format!(
                "iOS optional-plugin XCResult summary is invalid JSON: {error}"
            ))
        })
    }

    fn read_xcresult_tests(
        xcrun: &Path,
        result_bundle: &Path,
        diagnostics: &mut dyn Write,
    ) -> Result<XcresultTests, IosError> {
        let mut command = Command::new(xcrun);
        command
            .args(["xcresulttool", "get", "test-results", "tests", "--path"])
            .arg(result_bundle)
            .stdin(Stdio::null());
        let result = external_process::run_interruptible_capture(
            &mut command,
            "iOS optional-plugin XCResult test tree",
            MAX_XCRESULT_TESTS_BYTES,
            MAX_XCRESULT_SUMMARY_BYTES,
        )
        .map_err(map_process_error)?;
        diagnostics.write_all(&result.stderr).map_err(|error| {
            output_error("write iOS optional-plugin XCResult diagnostics", error)
        })?;
        classify_status(result.status, "iOS optional-plugin XCResult test tree")?;
        serde_json::from_slice(&result.stdout).map_err(|error| {
            IosError::conformance(format!(
                "iOS optional-plugin XCResult test tree is invalid JSON: {error}"
            ))
        })
    }

    fn validate_acceptance_test_tree(
        tests: &XcresultTests,
        xcode_succeeded: bool,
    ) -> Result<(), IosError> {
        let mut pending = tests.test_nodes.iter().collect::<Vec<_>>();
        let mut visited = 0_usize;
        let mut suites = 0_usize;
        let mut acceptance_cases = 0_usize;
        while let Some(node) = pending.pop() {
            visited += 1;
            if visited > MAX_XCRESULT_TEST_NODES {
                return Err(IosError::conformance(format!(
                    "iOS optional-plugin XCResult test tree contains more than {MAX_XCRESULT_TEST_NODES} nodes"
                )));
            }
            if node.node_type == "Test Suite" && node.name == TEST_CLASS_NAME {
                suites += 1;
                acceptance_cases += count_test_cases(&node.children, &mut visited)?;
            } else {
                pending.extend(node.children.iter());
            }
        }

        if suites == 1 && acceptance_cases == EXPECTED_ACCEPTANCE_TESTS as usize {
            return Ok(());
        }
        let message = format!(
            "iOS optional-plugin Release device acceptance did not execute the expected test class: suites={suites}, cases={acceptance_cases}, expectedCases={EXPECTED_ACCEPTANCE_TESTS}"
        );
        if xcode_succeeded {
            Err(IosError::conformance(message))
        } else {
            Err(IosError::worker(message))
        }
    }

    fn count_test_cases(
        children: &[XcresultTestNode],
        visited: &mut usize,
    ) -> Result<usize, IosError> {
        let mut pending = children.iter().collect::<Vec<_>>();
        let mut cases = 0_usize;
        while let Some(node) = pending.pop() {
            *visited += 1;
            if *visited > MAX_XCRESULT_TEST_NODES {
                return Err(IosError::conformance(format!(
                    "iOS optional-plugin XCResult test tree contains more than {MAX_XCRESULT_TEST_NODES} nodes"
                )));
            }
            if node.node_type == "Test Case" {
                cases += 1;
            }
            pending.extend(node.children.iter());
        }
        Ok(cases)
    }

    fn validate_summary(summary: &XcresultSummary) -> Result<(), IosError> {
        if summary.result != "Passed"
            || summary.total_test_count != EXPECTED_ACCEPTANCE_TESTS
            || summary.passed_tests != EXPECTED_ACCEPTANCE_TESTS
            || summary.failed_tests != 0
            || summary.skipped_tests != 0
            || summary.expected_failures != 0
        {
            return Err(IosError::conformance(format!(
                "iOS optional-plugin Release device acceptance requires {EXPECTED_ACCEPTANCE_TESTS} passed, 0 failed, 0 skipped, and 0 expected failures; result={}, total={}, passed={}, failed={}, skipped={}, expectedFailures={}",
                summary.result,
                summary.total_test_count,
                summary.passed_tests,
                summary.failed_tests,
                summary.skipped_tests,
                summary.expected_failures
            )));
        }
        Ok(())
    }

    fn resolve_required_tools() -> Result<RequiredTools, IosError> {
        Ok(RequiredTools {
            xcodegen: require_path_command("xcodegen")?,
            xcodebuild: require_path_command("xcodebuild")?,
            xcrun: require_path_command("xcrun")?,
        })
    }

    fn require_path_command(name: &str) -> Result<PathBuf, IosError> {
        let paths = env::var_os("PATH").unwrap_or_default();
        env::split_paths(&paths)
            .find_map(|directory| {
                let candidate = directory.join(name);
                fs::metadata(&candidate)
                    .is_ok_and(|metadata| metadata.is_file())
                    .then_some(candidate)
            })
            .filter(|candidate| access(candidate, AccessFlags::X_OK).is_ok())
            .ok_or_else(|| IosError::compatibility(format!("Missing required command: {name}")))
    }

    fn require_regular_file(path: &Path, label: &str) -> Result<PathBuf, IosError> {
        require_regular_path(path, label, false)
    }

    fn require_regular_directory(path: &Path, label: &str) -> Result<PathBuf, IosError> {
        require_regular_path(path, label, true)
    }

    fn require_regular_path(
        path: &Path,
        label: &str,
        directory: bool,
    ) -> Result<PathBuf, IosError> {
        let metadata = fs::symlink_metadata(path).map_err(|error| {
            IosError::storage(format!(
                "failed to inspect {label} '{}': {error}",
                path.display()
            ))
        })?;
        let expected = if directory {
            metadata.file_type().is_dir()
        } else {
            metadata.file_type().is_file()
        };
        if !expected {
            return Err(IosError::storage(format!(
                "{label} '{}' must be a regular non-symlink {}",
                path.display(),
                if directory { "directory" } else { "file" }
            )));
        }
        fs::canonicalize(path)
            .map_err(|error| IosError::storage(format!("failed to resolve {label}: {error}")))
    }

    fn run_captured(
        command: &mut Command,
        label: &str,
        diagnostics: &mut dyn Write,
    ) -> Result<(), IosError> {
        let status = run_captured_status(command, label, diagnostics)?;
        classify_status(status, label)
    }

    fn run_captured_status(
        command: &mut Command,
        label: &str,
        diagnostics: &mut dyn Write,
    ) -> Result<ExitStatus, IosError> {
        let result = external_process::run_interruptible_capture(
            command,
            label,
            MAX_PROCESS_OUTPUT_BYTES,
            MAX_PROCESS_OUTPUT_BYTES,
        )
        .map_err(map_process_error)?;
        diagnostics
            .write_all(&result.stdout)
            .and_then(|_| diagnostics.write_all(&result.stderr))
            .and_then(|_| diagnostics.flush())
            .map_err(|error| {
                output_error("write iOS optional-plugin process diagnostics", error)
            })?;
        Ok(result.status)
    }

    fn run_captured_status_with_timeout(
        command: &mut Command,
        label: &str,
        diagnostics: &mut dyn Write,
        timeout: Duration,
    ) -> Result<ExitStatus, IosError> {
        let result = external_process::run_interruptible_capture_with_timeout(
            command,
            label,
            MAX_PROCESS_OUTPUT_BYTES,
            MAX_PROCESS_OUTPUT_BYTES,
            timeout,
        )
        .map_err(map_process_error)?;
        diagnostics
            .write_all(&result.stdout)
            .and_then(|_| diagnostics.write_all(&result.stderr))
            .and_then(|_| diagnostics.flush())
            .map_err(|error| {
                output_error("write iOS optional-plugin process diagnostics", error)
            })?;
        Ok(result.status)
    }

    fn classify_status(status: ExitStatus, label: &str) -> Result<(), IosError> {
        if status.success() {
            Ok(())
        } else {
            Err(IosError::worker(format!(
                "{label} terminated unsuccessfully ({status})"
            )))
        }
    }

    fn map_process_error(error: external_process::ExternalProcessError) -> IosError {
        match error.kind() {
            ExternalProcessErrorKind::Compatibility => IosError::compatibility(error.to_string()),
            ExternalProcessErrorKind::Worker | ExternalProcessErrorKind::Cancelled => {
                IosError::worker(error.to_string())
            }
        }
    }

    fn output_error(operation: &str, error: io::Error) -> IosError {
        IosError::storage(format!("failed to {operation}: {error}"))
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        fn resolved_project_fixture() -> Value {
            let optional_dependencies = crate::ios_optional_release::optional_frameworks()
                .iter()
                .copied()
                .map(|framework| {
                    serde_json::json!({
                        "framework": format!(
                            "../../lib/ios/VesperPlayerOptionalPlugins/Artifacts/{framework}.xcframework"
                        ),
                        "embed": true,
                        "link": false,
                        "codeSign": true,
                    })
                });
            let mut dependencies = vec![serde_json::json!({
                "package": "VesperPlayerKit",
                "product": "VesperPlayerKit",
            })];
            dependencies.extend(optional_dependencies);
            serde_json::json!({
                "packages": {
                    "VesperPlayerKit": { "path": "../../lib/ios/VesperPlayerKit" }
                },
                "targets": {
                    "VesperPlayerHostDemo": {
                        "dependencies": dependencies,
                        "sources": ["Sources"],
                        "info": {
                            "path": "Sources/Info.plist",
                            "properties": { "UIRequiresFullScreen": true }
                        }
                    }
                }
            })
        }

        fn create_verified_package_fixture(path: &Path) {
            fs::create_dir(path).expect("create verified package fixture");
            let artifacts = path.join("Artifacts");
            fs::create_dir(&artifacts).expect("create verified artifacts fixture");
            for framework in crate::ios_optional_release::optional_frameworks() {
                fs::create_dir(artifacts.join(format!("{framework}.xcframework")))
                    .expect("create verified XCFramework fixture");
            }
        }

        fn passing_summary() -> XcresultSummary {
            XcresultSummary {
                result: "Passed".to_owned(),
                total_test_count: EXPECTED_ACCEPTANCE_TESTS,
                passed_tests: EXPECTED_ACCEPTANCE_TESTS,
                failed_tests: 0,
                skipped_tests: 0,
                expected_failures: 0,
            }
        }

        fn acceptance_test_tree(case_count: usize) -> XcresultTests {
            XcresultTests {
                test_nodes: vec![XcresultTestNode {
                    name: TEST_CLASS_NAME.to_owned(),
                    node_type: "Test Suite".to_owned(),
                    children: (0..case_count)
                        .map(|index| XcresultTestNode {
                            name: format!("testAcceptance{index}()"),
                            node_type: "Test Case".to_owned(),
                            children: Vec::new(),
                        })
                        .collect(),
                }],
            }
        }

        fn bootstrap_failure_test_tree() -> XcresultTests {
            XcresultTests {
                test_nodes: vec![XcresultTestNode {
                    name: "System Failures".to_owned(),
                    node_type: "Test Suite".to_owned(),
                    children: vec![XcresultTestNode {
                        name: "VesperPlayerHostDemo encountered an error".to_owned(),
                        node_type: "Test Case".to_owned(),
                        children: Vec::new(),
                    }],
                }],
            }
        }

        #[test]
        fn acceptance_summary_requires_all_three_tests_to_run_and_pass() {
            validate_summary(&passing_summary()).expect("three passing tests are accepted");

            let mut skipped = passing_summary();
            skipped.passed_tests = 2;
            skipped.skipped_tests = 1;
            let error = validate_summary(&skipped).expect_err("a skipped test must fail the gate");
            assert!(error.to_string().contains("2"));
            assert!(error.to_string().contains("skipped=1"));

            let mut incomplete = passing_summary();
            incomplete.total_test_count = 2;
            incomplete.passed_tests = 2;
            let error =
                validate_summary(&incomplete).expect_err("an incomplete test class must fail");
            assert!(error.to_string().contains("total=2"));
        }

        #[test]
        fn acceptance_tree_distinguishes_xcode_system_failures_from_test_cases() {
            validate_acceptance_test_tree(
                &acceptance_test_tree(EXPECTED_ACCEPTANCE_TESTS as usize),
                false,
            )
            .expect("three acceptance cases prove that the test class executed");

            let error = validate_acceptance_test_tree(&bootstrap_failure_test_tree(), false)
                .expect_err("an Xcode system failure must not imply plugin conformance");
            assert_eq!(error.kind(), crate::ios::IosErrorKind::Worker);
            assert!(error.to_string().contains("suites=0, cases=0"));

            let error = validate_acceptance_test_tree(&acceptance_test_tree(2), true)
                .expect_err("a successful but incomplete test selection is conformance failure");
            assert_eq!(error.kind(), crate::ios::IosErrorKind::Conformance);
            assert!(error.to_string().contains("suites=1, cases=2"));
        }

        #[test]
        fn ordinary_xcode_failure_is_worker_failure_without_test_evidence() {
            let status = Command::new("/bin/sh")
                .args(["-c", "exit 65"])
                .status()
                .expect("produce an ordinary xcodebuild failure status");
            let error = classify_status(status, "locked-device bootstrap")
                .expect_err("an ordinary xcodebuild failure must not imply conformance");

            assert_eq!(error.kind(), crate::ios::IosErrorKind::Worker);
            assert!(error.to_string().contains("locked-device bootstrap"));
        }

        #[test]
        fn device_and_team_identifiers_reject_ambiguous_input() {
            validate_identifier("00008140-000471243E29801C", 256, "device")
                .expect("physical device UDID is valid");
            validate_team_identifier("983LPXU7G4").expect("development team is valid");

            assert!(validate_identifier(" device", 256, "device").is_err());
            assert!(validate_identifier("device\n", 256, "device").is_err());
            assert!(validate_team_identifier("983L-PXU7G4").is_err());
        }

        #[test]
        fn xctestrun_discovery_requires_one_regular_top_level_file() {
            let directory = tempfile::tempdir().expect("temporary xctestrun fixture");
            let products = directory.path();
            let first = products.join("VesperPlayerHostDemo_iphoneos.xctestrun");
            fs::write(&first, b"fixture\n").expect("write xctestrun fixture");
            fs::create_dir(products.join("ignored.xctestrun"))
                .expect("create non-file xctestrun fixture");
            assert_eq!(
                discover_xctestrun(products).expect("discover one xctestrun"),
                first
            );

            let second = products.join("Duplicate.xctestrun");
            fs::write(&second, b"duplicate\n").expect("write duplicate xctestrun fixture");
            let duplicate = discover_xctestrun(products)
                .expect_err("duplicate xctestrun files must be rejected");
            assert!(duplicate.to_string().contains("exactly one"));
            fs::remove_file(first).expect("remove first xctestrun fixture");
            fs::remove_file(second).expect("remove second xctestrun fixture");
            let missing =
                discover_xctestrun(products).expect_err("missing xctestrun must be rejected");
            assert!(missing.to_string().contains("found 0"));
        }

        #[test]
        fn device_project_replaces_exactly_the_verified_optional_dependencies() {
            let directory = tempfile::tempdir().expect("temporary device project fixture");
            let package = directory.path().join("verified package ${PATH} - test");
            create_verified_package_fixture(&package);
            let player_kit = directory.path().join("VesperPlayerKit");
            fs::create_dir(&player_kit).expect("create VesperPlayerKit fixture");
            let project = directory.path().join("Project");
            fs::create_dir(&project).expect("create project fixture");
            let spec = project.join("device-project.json");

            write_device_project_spec(
                &spec,
                resolved_project_fixture(),
                &player_kit,
                &package,
                crate::ios_optional_release::optional_frameworks(),
            )
            .expect("write isolated project spec");

            let value: Value =
                serde_json::from_slice(&fs::read(&spec).expect("read isolated project spec"))
                    .expect("parse isolated project spec");
            assert!(!contains_workspace_optional_artifact_reference(&value));
            assert_eq!(
                value["packages"]["VesperPlayerKit"]["path"],
                fs::canonicalize(&player_kit)
                    .expect("canonicalize VesperPlayerKit fixture")
                    .to_str()
                    .expect("UTF-8 fixture path")
            );
            assert!(value["packages"]["VesperPlayerOptionalPlugins"].is_null());
            let dependencies = value["targets"]["VesperPlayerHostDemo"]["dependencies"]
                .as_array()
                .expect("application dependencies");
            let frameworks = dependencies
                .iter()
                .filter_map(|dependency| {
                    dependency["framework"]
                        .as_str()
                        .and_then(|path| Path::new(path).file_stem())
                        .and_then(|name| name.to_str())
                })
                .collect::<BTreeSet<_>>();
            assert_eq!(
                frameworks,
                crate::ios_optional_release::optional_frameworks()
                    .iter()
                    .copied()
                    .collect()
            );
            let verified_root = fs::canonicalize(package.join("Artifacts"))
                .expect("canonicalize verified artifacts fixture");
            for dependency in dependencies
                .iter()
                .filter(|dependency| dependency["framework"].is_string())
            {
                let framework = Path::new(
                    dependency["framework"]
                        .as_str()
                        .expect("verified framework path"),
                );
                assert!(framework.starts_with(&verified_root));
                assert!(framework.is_dir());
                assert_eq!(dependency["embed"], true);
                assert_eq!(dependency["link"], false);
                assert_eq!(dependency["codeSign"], true);
                assert!(dependency["package"].is_null());
                assert!(dependency["product"].is_null());
            }
            assert_eq!(
                value["targets"]["VesperPlayerHostDemo"]["info"]["path"],
                project
                    .join("Generated/Info.plist")
                    .to_str()
                    .expect("UTF-8 fixture path")
            );
            assert_eq!(
                value["targets"]["VesperPlayerHostDemo"]["sources"],
                serde_json::json!([{
                    "path": "Sources",
                    "excludes": ["Info.plist"],
                }])
            );
        }

        #[test]
        fn device_project_rejects_info_without_exact_source_ownership() {
            let directory = tempfile::tempdir().expect("temporary device project fixture");
            let package = directory.path().join("package");
            create_verified_package_fixture(&package);
            let player_kit = directory.path().join("VesperPlayerKit");
            fs::create_dir(&player_kit).expect("create VesperPlayerKit fixture");
            for (label, sources) in [
                ("missing", serde_json::json!(["OtherSources"])),
                (
                    "duplicate",
                    serde_json::json!(["Sources", { "path": "Sources" }]),
                ),
            ] {
                let project = directory.path().join(label);
                fs::create_dir(&project).expect("create project fixture");
                let mut value = resolved_project_fixture();
                value["targets"]["VesperPlayerHostDemo"]["sources"] = sources;
                let error = write_device_project_spec(
                    &project.join("device-project.json"),
                    value,
                    &player_kit,
                    &package,
                    crate::ios_optional_release::optional_frameworks(),
                )
                .expect_err("ambiguous Info.plist source ownership must fail");
                assert!(
                    error.to_string().contains("exactly one source directory"),
                    "unexpected {label} diagnostic: {error}"
                );
            }
        }

        #[test]
        fn device_project_rejects_missing_duplicate_and_unknown_optional_dependencies() {
            let directory = tempfile::tempdir().expect("temporary device project fixture");
            let package = directory.path().join("package");
            create_verified_package_fixture(&package);
            let player_kit = directory.path().join("VesperPlayerKit");
            fs::create_dir(&player_kit).expect("create VesperPlayerKit fixture");
            for (label, mutate) in [("missing", 0_u8), ("duplicate", 1_u8), ("unknown", 2_u8)] {
                let project = directory.path().join(label);
                fs::create_dir(&project).expect("create project fixture");
                let mut value = resolved_project_fixture();
                let dependencies = value["targets"]["VesperPlayerHostDemo"]["dependencies"]
                    .as_array_mut()
                    .expect("application dependencies");
                match mutate {
                    0 => {
                        dependencies.pop();
                    }
                    1 => dependencies.push(dependencies[1].clone()),
                    2 => {
                        dependencies[1]["framework"] = Value::String(
                            "../../lib/ios/VesperPlayerOptionalPlugins/Artifacts/Unknown.xcframework"
                                .to_owned(),
                        );
                    }
                    _ => unreachable!(),
                }
                let error = write_device_project_spec(
                    &project.join("device-project.json"),
                    value,
                    &player_kit,
                    &package,
                    crate::ios_optional_release::optional_frameworks(),
                )
                .expect_err("dependency drift must fail");
                assert!(
                    error.to_string().contains(label)
                        || (label == "duplicate" && error.to_string().contains("more than once")),
                    "unexpected {label} diagnostic: {error}"
                );
            }
        }
    }
}