cadmpeg 0.6.8

Convert and inspect native CAD files.
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
#[cfg(feature = "rhino")]
use cadmpeg_test_support::wire;

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

#[cfg(any(feature = "iges", feature = "rhino", feature = "step", feature = "usd"))]
use cadmpeg_ir::codec::write::target::TargetRequest;
use cadmpeg_ir::codec::write::{EncodeInput, Encoder};
use cadmpeg_ir::CadIr;
use cadmpeg_registry::Format;

use crate::application::artifact_store::FileDestination;
use crate::application::document::LoadedDocument;
#[cfg(any(feature = "iges", feature = "rhino", feature = "step"))]
use crate::application::refusal::ConversionRefusal;

#[cfg(any(feature = "iges", feature = "rhino", feature = "step"))]
use super::export_target;
use super::{
    DestinationPolicy, LossPolicy, PreparedConversion, ResolvedDestination, TargetSelection,
};

#[test]
fn loss_policy_assigns_each_refusal_phase() {
    assert_eq!(
        [
            (LossPolicy::Allow, false, false),
            (LossPolicy::RejectDecode, true, false),
            (LossPolicy::RejectExport, false, true),
            (LossPolicy::RejectAny, true, true),
        ],
        [
            (
                LossPolicy::Allow,
                LossPolicy::Allow.rejects_decode(),
                LossPolicy::Allow.rejects_export(),
            ),
            (
                LossPolicy::RejectDecode,
                LossPolicy::RejectDecode.rejects_decode(),
                LossPolicy::RejectDecode.rejects_export(),
            ),
            (
                LossPolicy::RejectExport,
                LossPolicy::RejectExport.rejects_decode(),
                LossPolicy::RejectExport.rejects_export(),
            ),
            (
                LossPolicy::RejectAny,
                LossPolicy::RejectAny.rejects_decode(),
                LossPolicy::RejectAny.rejects_export(),
            ),
        ]
    );
}

#[test]
fn convert_file_destination_discards_binary_stdout_at_clap_admission() {
    use clap::Parser;
    let command = crate::Cli::try_parse_from([
        "cadmpeg",
        "convert",
        "input.step",
        "-o",
        "out.step",
        "--binary-stdout",
    ])
    .unwrap()
    .command;
    let crate::Command::Convert { destinations, .. } = command else {
        panic!("expected convert command");
    };
    assert_eq!(
        destinations.destination,
        DestinationPolicy::File(FileDestination {
            path: PathBuf::from("out.step"),
            overwrite: false,
        })
    );
}

fn prepared(
    ir: CadIr,
    format: Format,
    encoder: Box<dyn Encoder>,
    loss_policy: LossPolicy,
) -> PreparedConversion {
    let validation =
        cadmpeg_ir::validate_neutral(&ir, Vec::new()).expect("validate the neutral model");
    PreparedConversion {
        document: LoadedDocument::neutral(ir, 0),
        validation,
        encoder,
        selection: TargetSelection::new(format, None),
        assets: crate::loader::companions::Directory::beside(Path::new("input")),
        destination: ResolvedDestination::Stdout,
        loss_policy,
        allow_errors: false,
    }
}

#[cfg(feature = "iges")]
#[test]
fn encoder_planning_owns_unknown_explicit_target_admission() {
    let mut conversion = prepared(
        CadIr::empty(),
        Format::Iges,
        Box::new(cadmpeg_codec_iges::IgesCodec),
        LossPolicy::Allow,
    );
    conversion.selection.request = Some("nonesuch".into());

    let Err(error) = conversion.plan() else {
        panic!("the encoder must reject an unknown target");
    };
    assert!(matches!(
        error.refusal(),
        Some(ConversionRefusal::UnsupportedTarget { .. })
    ));
}

#[cfg(feature = "step")]
fn step_ir_with_unrepresentable_native_content() -> CadIr {
    let mut ir = CadIr::empty();
    ir.native.namespace_mut("f3d").arenas_mut().insert(
        "asm_histories".into(),
        vec![cadmpeg_ir::NativeRecord::new(
            cadmpeg_ir::ids::Identity::new("f3d:test:asm-history#0").expect("valid identity"),
            serde_json::Map::default(),
        )
        .expect("valid native identity")],
    );
    ir.finalize();
    ir
}

/// STEP planning returns concrete loss rows, and the shared application
/// gate names them when export-loss rejection is active.
#[cfg(feature = "step")]
#[test]
fn step_export_losses_are_rejected_by_the_shared_plan_gate() {
    let conversion = prepared(
        step_ir_with_unrepresentable_native_content(),
        Format::Step,
        Box::new(cadmpeg_codec_step::StepCodec::default()),
        LossPolicy::RejectExport,
    );
    let Err(error) = conversion.plan() else {
        panic!("the plan loss must refuse");
    };
    let refusal = error
        .refusal()
        .expect("the shared gate returns a typed refusal");
    assert!(
        matches!(refusal, ConversionRefusal::ExportLossRejected { .. }),
        "{refusal:?}"
    );
    let message = refusal.evidence().message;
    assert!(message.contains("source-native record(s)"), "{message}");
    let report = refusal
        .evidence()
        .reports
        .export
        .expect("the refusal retains the completed export plan report");
    assert!(
        report
            .losses
            .iter()
            .any(|loss| loss.message.contains("source-native record(s)")),
        "{:?}",
        report.losses
    );
}

/// Without export-loss rejection the same STEP plan succeeds and retains
/// the loss rows for the caller and report.
#[cfg(feature = "step")]
#[test]
fn step_export_losses_remain_on_the_plan_without_rejection() {
    let conversion = prepared(
        step_ir_with_unrepresentable_native_content(),
        Format::Step,
        Box::new(cadmpeg_codec_step::StepCodec::default()),
        LossPolicy::Allow,
    );
    let planned = conversion.plan().expect("loss reporting is not refusal");
    assert!(
        planned
            .plan
            .report()
            .losses
            .iter()
            .any(|loss| loss.message.contains("source-native record(s)")),
        "{:?}",
        planned.plan.report().losses
    );
}

/// A Rhino write omits product structure it cannot represent. The wrapper
/// charges that omission as an export loss, so the shared gate refuses it.
#[cfg(feature = "rhino")]
#[test]
fn rhino_omitted_product_structure_is_rejected_by_the_shared_plan_gate() {
    let mut ir = CadIr::empty();
    ir.model.points.push(cadmpeg_ir::topology::Point::new(
        cadmpeg_ir::ids::PointId::mint("cadir:model:point#pin").expect("identity grammar"),
        cadmpeg_ir::features::FinitePoint3::new(cadmpeg_ir::math::Point3::new(0.0, 0.0, 0.0))
            .expect("finite position"),
        None,
    ));
    ir.model
        .product_definitions
        .push(cadmpeg_ir::products::ProductDefinition {
            sketches: Vec::default(),
            id: "cadir:model:product#group"
                .try_into()
                .expect("valid identity"),
            kind: cadmpeg_ir::products::ProductDefinitionKind::Group,
            source_name: Some("Group".into()),
            label: None,
            description: None,
            part_number: None,
            bom_properties: std::collections::BTreeMap::new(),
            bodies: Vec::new(),
            native_ref: None,
        });
    let mut conversion = prepared(
        ir,
        Format::Rhino,
        Box::new(cadmpeg_codec_rhino::RhinoCodec),
        LossPolicy::RejectExport,
    );
    // The lone point is an orphan carrier, a check error the Rhino payload
    // carries. This test covers the export-loss gate, so it permits errors.
    conversion.allow_errors = true;
    let Err(error) = conversion.plan() else {
        panic!("the omitted product definition must refuse");
    };
    let refusal = error
        .refusal()
        .expect("the shared gate returns a typed refusal");
    let ConversionRefusal::ExportLossRejected { export_report, .. } = refusal else {
        panic!("{refusal:?}");
    };
    assert!(
        export_report
            .losses
            .iter()
            .any(|loss| loss.message.contains("model arena `product_definitions`")),
        "{:?}",
        export_report.losses
    );
}

struct NotImplementedEncoder;

impl cadmpeg_ir::codec::write::EncoderBackend for NotImplementedEncoder {
    const FORMAT: cadmpeg_ir::codec::FormatId =
        cadmpeg_ir::codec::FormatId::new("not-implemented-test");
    const NEEDS: cadmpeg_ir::codec::write::DepthNeed =
        cadmpeg_ir::codec::write::DepthNeed::AtLeast(cadmpeg_core::decode::DecodeDepth::Full);
    type Target = cadmpeg_ir::codec::write::target::DialectFree;
    const TARGET: Self::Target = cadmpeg_ir::codec::write::target::DialectFree;

    fn plan_resolved(
        &self,
        _input: EncodeInput<'_>,
        _target: (),
    ) -> Result<cadmpeg_ir::codec::write::ExportBody, cadmpeg_core::CodecError> {
        Err(cadmpeg_core::CodecError::NotImplemented(
            "writer path is not implemented".into(),
        ))
    }
}

/// A writer implementation failure is not evidence of an export loss,
/// even when the application would reject real plan losses.
#[test]
fn not_implemented_plan_failure_is_not_reclassified_as_export_loss() {
    let conversion = prepared(
        CadIr::empty(),
        Format::Cadir,
        Box::new(NotImplementedEncoder),
        LossPolicy::RejectExport,
    );
    let Err(error) = conversion.plan() else {
        panic!("the synthetic writer is not implemented");
    };
    assert_eq!(
        error.to_string(),
        "not implemented yet: writer path is not implemented"
    );
    assert!(error.refusal().is_none());
}

/// Flag absence is always `Inherit`; the encoder decides what that means.
///
/// The selection is an owned-string adapter and nothing else. The
/// cross-format catalog default is owned by write resolution, so the command
/// line does not decide it a second time.
#[cfg(feature = "iges")]
#[test]
fn flag_absence_is_always_an_inherit_request() {
    assert_eq!(
        TargetSelection::new(Format::Iges, None).request(),
        TargetRequest::Inherit
    );
    let named = TargetSelection::new(Format::Iges, Some("iges:5.1-fixed-ascii".to_owned()));
    assert_eq!(
        named.request(),
        TargetRequest::Explicit("iges:5.1-fixed-ascii")
    );
}

#[cfg(feature = "iges")]
#[test]
fn target_selection_owns_format_and_dialect_grammar() {
    let format_only = TargetSelection::resolve(Some("iges"), None).expect("format resolves");
    assert_eq!(format_only.format, Format::Iges);
    assert_eq!(format_only.request(), TargetRequest::Inherit);

    let qualified = TargetSelection::resolve(Some("iges:5.1"), None).expect("alias resolves");
    assert_eq!(qualified.format, Format::Iges);
    assert_eq!(qualified.request(), TargetRequest::Explicit("5.1"));

    let inferred = TargetSelection::resolve(Some("5.1"), Some(Path::new("part.iges")))
        .expect("output extension supplies the format");
    assert_eq!(inferred.format, Format::Iges);
    assert_eq!(inferred.request(), TargetRequest::Explicit("5.1"));
}

#[cfg(feature = "step")]
#[test]
fn a_format_alias_does_not_become_a_dialect_request() {
    let selection = TargetSelection::resolve(Some("stp"), Some(Path::new("part.stp")))
        .expect("the identity registry owns the STEP alias");
    assert_eq!(selection.format, Format::Step);
    assert_eq!(selection.request(), TargetRequest::Inherit);
}

#[test]
fn target_selection_rejects_an_empty_qualified_dialect() {
    let error = TargetSelection::resolve(Some("cadir:"), None).unwrap_err();
    assert!(error.to_string().contains("nothing after the colon"));
    assert!(error.refusal().is_none());
}

/// Export-loss rejection is not a target.
///
/// A loss flag that also named a target would turn `convert a.step -o
/// b.step --reject-lossy=export` into an explicit AP214 request and lose
/// the identity default. Only `--to` may say what to write.
#[cfg(feature = "step")]
#[test]
fn rejecting_export_losses_does_not_name_a_target() {
    let target = export_target(TargetSelection::new(Format::Step, None));

    assert!(target.selection.request.is_none(), "{:?}", target.selection);
    assert_eq!(target.selection.request(), TargetRequest::Inherit);

    let named = export_target(TargetSelection::new(
        Format::Step,
        Some("step:ap242-e3".to_owned()),
    ));
    assert_eq!(
        named.selection.request(),
        TargetRequest::Explicit("step:ap242-e3")
    );
}

/// `--to` carries the dialect half verbatim; the encoder resolves it.
///
/// Both spellings the grammar admits reach `plan` unchanged, and both
/// resolve, because catalog lookup matches a row by id or by alias.
/// Resolving here instead would put a second copy of every catalog in the
/// CLI, which is the drift the registries exist to kill.
#[cfg(feature = "rhino")]
#[test]
fn an_alias_and_an_id_reach_the_encoder_unresolved_and_both_resolve() {
    use cadmpeg_core::dialect::DialectId;

    let ir = CadIr::empty();
    for spelling in ["archive-60", "60"] {
        let target = export_target(TargetSelection::new(
            Format::Rhino,
            Some(spelling.to_owned()),
        ));
        assert_eq!(
            target.selection.request(),
            TargetRequest::Explicit(spelling)
        );
        let plan = target
            .encoder
            .plan(
                EncodeInput::new(&ir, None),
                TargetRequest::Explicit(spelling),
            )
            .expect("the catalog carries the row under both spellings");
        assert_eq!(
            wire::field_or_default::<Option<cadmpeg_core::dialect::DialectId>>(
                plan.report(),
                "identity/target"
            )
            .as_ref()
            .map(DialectId::as_str),
            Some("rhino:archive-60")
        );
    }
}

/// A dialect outside the catalog reaches the encoder and is refused by the
/// sealed planning boundary.
#[cfg(feature = "iges")]
#[test]
fn an_unknown_dialect_is_refused_by_plan_with_its_catalog() {
    let target = export_target(TargetSelection::new(
        Format::Iges,
        Some("ap242e3".to_owned()),
    ));
    let error = target
        .encoder
        .plan(
            EncodeInput::new(&CadIr::empty(), None),
            target.selection.request(),
        )
        .expect_err("a STEP alias is not an IGES target");
    let message = error.to_string();
    assert!(message.contains("ap242e3"), "{message}");
    assert!(message.contains("iges:5.3-fixed-ascii"), "{message}");
}

#[test]
fn loss_policy_cli_preserves_omission_and_rejects_allow_spelling() {
    use clap::Parser;
    for (flag, expected) in [
        (None, LossPolicy::Allow),
        (Some("--reject-lossy"), LossPolicy::RejectAny),
        (Some("--reject-lossy=decode"), LossPolicy::RejectDecode),
        (Some("--reject-lossy=export"), LossPolicy::RejectExport),
        (Some("--reject-lossy=any"), LossPolicy::RejectAny),
    ] {
        let mut args = vec!["cadmpeg", "convert", "input.step"];
        args.extend(flag);
        let crate::Command::Convert { reject_lossy, .. } =
            crate::Cli::try_parse_from(args).unwrap().command
        else {
            panic!("expected convert command");
        };
        assert_eq!(reject_lossy.unwrap_or_default(), expected);
    }
    let error =
        crate::Cli::try_parse_from(["cadmpeg", "convert", "input.step", "--reject-lossy=allow"])
            .unwrap_err();
    assert_eq!(error.kind(), clap::error::ErrorKind::InvalidValue);
}

#[cfg(feature = "fcstd")]
fn checked_design_history() -> (LoadedDocument, cadmpeg_ir::report::check::ValidationReport) {
    let inputs = cadmpeg_registry::InputCatalog::with_builtins();
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../corpus/freecad_fcstd/fixtures/design_history.FCStd");
    let options = cadmpeg_ir::codec::DecodeOptions::default();
    let document = crate::loader::load_artifact(&inputs, &path, options, None)
        .expect("load the FreeCAD design-history fixture");
    let validation = crate::application::validators::validate_loaded(
        &inputs,
        &document,
        &options.policy,
        cadmpeg_measure::WorkProfile::Desktop,
    )
    .expect("validate the FreeCAD design-history fixture");
    assert_eq!(validation.error_count(), 0, "{validation:?}");
    (document, validation)
}

#[cfg(all(feature = "fcstd", feature = "step"))]
fn prepare_cadir_for_step(ir: &CadIr, path: &Path) -> PreparedConversion {
    use cadmpeg_ir::codec::write::CadirEncoder;
    use cadmpeg_ir::codec::DecodeOptions;
    use cadmpeg_registry::InputCatalog;

    CadirEncoder
        .plan(EncodeInput::new(ir, None), TargetRequest::Inherit)
        .expect("plan the mutated CADIR document")
        .write_to(&mut std::fs::File::create(path).expect("create CADIR input"))
        .expect("write the mutated CADIR document");
    super::prepare(
        &InputCatalog::with_builtins(),
        &super::SourceRequest {
            path,
            forced: None,
            options: DecodeOptions::default(),
            work: cadmpeg_measure::WorkProfile::Desktop,
        },
        export_target(TargetSelection::new(Format::Step, None)),
        &super::ConversionPolicy {
            losses: LossPolicy::Allow,
            allow_errors: false,
            destination: DestinationPolicy::Stdout {
                allow_binary: false,
            },
        },
    )
    .expect("prepare must retain validation errors for the export plan gate")
}

#[cfg(all(feature = "fcstd", feature = "step"))]
#[test]
fn a_design_arena_error_does_not_block_a_step_export() {
    use cadmpeg_ir::features::FeatureId;
    use cadmpeg_ir::report::{check::Check, Severity};

    let (mut document, _) = checked_design_history();
    let parameter_id = document.ir.model.parameters[0].id.to_string();
    document.ir.model.parameters[0].owner =
        Some(FeatureId::mint("fcstd:test:feature#missing").expect("valid absent feature identity"));
    let directory = tempfile::tempdir().expect("temporary CADIR directory");
    let conversion = prepare_cadir_for_step(&document.ir, &directory.path().join("design.json"));
    let planned = conversion.plan().expect("STEP drops the design arenas");
    let report = &planned.prepared().validation;
    assert!(
        report.findings.iter().any(|finding| {
            finding.severity >= Severity::Error
                && finding.check == Check::ReferentialIntegrity
                && finding.entity.as_deref() == Some(parameter_id.as_str())
        }),
        "{report:?}"
    );
    assert!(
        report
            .findings
            .iter()
            .filter(|finding| finding.severity >= Severity::Error)
            .all(|finding| document
                .ir
                .model
                .parameters
                .iter()
                .any(|parameter| { finding.entity.as_deref() == Some(parameter.id.as_str()) })),
        "{report:?}"
    );
    assert_eq!(planned.carried_errors(), 0);
}

#[cfg(all(feature = "fcstd", feature = "step"))]
#[test]
fn a_brep_error_blocks_a_step_export() {
    use crate::application::refusal::CheckOperation;
    use cadmpeg_ir::ids::SurfaceId;
    use cadmpeg_ir::report::Severity;

    let (mut document, _) = checked_design_history();
    let face_id = document.ir.model.faces[0].id.to_string();
    document.ir.model.faces[0].surface =
        SurfaceId::mint("fcstd:test:surface#missing").expect("valid absent surface identity");
    let directory = tempfile::tempdir().expect("temporary CADIR directory");
    let conversion = prepare_cadir_for_step(&document.ir, &directory.path().join("brep.json"));
    let Err(error) = conversion.plan() else {
        panic!("STEP must refuse an error in the B-rep it writes");
    };
    let Some(ConversionRefusal::CheckFailed {
        operation: CheckOperation::Export { carried },
        validation,
        ..
    }) = error.refusal()
    else {
        panic!("expected an export check refusal, got {error:?}");
    };
    assert!(*carried >= 1);
    assert_eq!(*carried, validation.error_count());
    assert!(
        validation.findings.iter().any(|finding| {
            finding.severity >= Severity::Error
                && finding.entity.as_deref() == Some(face_id.as_str())
        }),
        "{validation:?}"
    );
    assert!(
        validation
            .findings
            .iter()
            .filter(|finding| finding.severity >= Severity::Error)
            .all(|finding| finding.entity.as_deref().is_none_or(|id| {
                document
                    .ir
                    .model
                    .faces
                    .iter()
                    .any(|face| face.id.as_str() == id)
                    || document
                        .ir
                        .model
                        .surfaces
                        .iter()
                        .any(|surface| surface.id.as_str() == id)
            })),
        "{validation:?}"
    );
}

#[cfg(all(feature = "fcstd", feature = "step"))]
#[test]
fn an_error_without_an_entity_blocks_a_step_export() {
    use crate::application::refusal::CheckOperation;
    use cadmpeg_ir::report::{
        check::{Check, Finding},
        Severity,
    };

    let (document, _) = checked_design_history();
    let feature_id = document.ir.model.features[0].id.to_string();
    for include_design_error in [false, true] {
        let mut conversion = prepared(
            document.ir.clone(),
            Format::Step,
            export_target(TargetSelection::new(Format::Step, None)).encoder,
            LossPolicy::Allow,
        );
        assert_eq!(conversion.validation.error_count(), 0);
        conversion.validation.findings.push(Finding {
            related: Vec::new(),
            check: Check::Counts,
            severity: Severity::Error,
            message: "synthetic document-wide error".into(),
            entity: None,
        });
        if include_design_error {
            conversion.validation.findings.push(Finding {
                related: Vec::new(),
                check: Check::Counts,
                severity: Severity::Error,
                message: "synthetic design error".into(),
                entity: Some(feature_id.clone()),
            });
        }
        let Err(error) = conversion.plan() else {
            panic!("an unlocated error must refuse the export");
        };
        assert!(
            matches!(
                error.refusal(),
                Some(ConversionRefusal::CheckFailed {
                    operation: CheckOperation::Export { carried: 1 },
                    ..
                })
            ),
            "{error:?}"
        );
        if include_design_error {
            assert!(
                error
                    .to_string()
                    .contains("2 error(s), 1 in what the export writes"),
                "{error}"
            );
        }
    }
}

#[cfg(feature = "fcstd")]
#[test]
fn a_design_arena_error_blocks_a_freecad_round_trip() {
    use crate::application::refusal::{CheckOperation, ConversionRefusal};
    use cadmpeg_ir::report::{
        check::{Check, Finding},
        Severity,
    };

    let (document, validation) = checked_design_history();
    let feature_id = document.ir.model.features[0].id.to_string();
    for inject_error in [false, true] {
        let target = super::export_target(TargetSelection::new(Format::Fcstd, None));
        let mut conversion = PreparedConversion {
            document: document.clone(),
            validation: validation.clone(),
            encoder: target.encoder,
            selection: target.selection,
            assets: crate::loader::companions::Directory::beside(Path::new("input")),
            destination: ResolvedDestination::Stdout,
            loss_policy: LossPolicy::Allow,
            allow_errors: false,
        };
        if inject_error {
            conversion.validation.findings.push(Finding {
                related: Vec::new(),
                check: Check::Counts,
                severity: Severity::Error,
                message: "synthetic design error".into(),
                entity: Some(feature_id.clone()),
            });
            let Err(error) = conversion.plan() else {
                panic!("FreeCAD carries design errors through complete coverage");
            };
            assert!(
                matches!(
                    error.refusal(),
                    Some(ConversionRefusal::CheckFailed {
                        operation: CheckOperation::Export { carried: 1 },
                        ..
                    })
                ),
                "{error:?}"
            );
        } else {
            let planned = conversion
                .plan()
                .expect("the unchanged FreeCAD fixture must plan");
            assert_eq!(planned.carried_errors(), 0);
        }
    }
}

#[cfg(all(feature = "fcstd", feature = "step"))]
#[test]
fn allow_errors_writes_past_a_carried_error() {
    use cadmpeg_ir::report::{
        check::{Check, Finding},
        Severity,
    };

    let (document, _) = checked_design_history();
    let mut conversion = prepared(
        document.ir,
        Format::Step,
        export_target(TargetSelection::new(Format::Step, None)).encoder,
        LossPolicy::Allow,
    );
    assert_eq!(conversion.validation.error_count(), 0);
    conversion.validation.findings.push(Finding {
        related: Vec::new(),
        check: Check::Counts,
        severity: Severity::Error,
        message: "synthetic document-wide error".into(),
        entity: None,
    });
    conversion.allow_errors = true;
    let planned = conversion
        .plan()
        .expect("allow-errors permits a carried error");
    assert_eq!(planned.carried_errors(), 1);
}

#[cfg(feature = "step")]
#[test]
fn an_f3d_native_design_record_error_does_not_block_a_step_export() {
    use cadmpeg_ir::report::{
        check::{Check, Finding},
        Severity,
    };

    let mut conversion = prepared(
        step_ir_with_unrepresentable_native_content(),
        Format::Step,
        export_target(TargetSelection::new(Format::Step, None)).encoder,
        LossPolicy::Allow,
    );
    assert_eq!(conversion.validation.error_count(), 0);
    let finding = Finding {
        related: Vec::new(),
        check: Check::Counts,
        severity: Severity::Error,
        message: "synthetic native design record error".into(),
        entity: Some("f3d:test:asm-history#0".into()),
    };
    conversion.validation.findings.push(finding.clone());
    let planned = conversion
        .plan()
        .expect("STEP drops the foreign native namespace");
    assert_eq!(planned.carried_errors(), 0);
    assert!(planned.prepared().validation.findings.contains(&finding));
}

#[cfg(feature = "usd")]
#[test]
fn an_output_extension_that_names_a_dialect_selects_it() {
    let package = Some(Path::new("assembly.usdz"));
    let inferred = TargetSelection::resolve(None, package).expect("format from the extension");
    assert_eq!(inferred.format, Format::Usd);
    assert_eq!(inferred.request(), TargetRequest::Explicit("usdz"));
    let format_only = TargetSelection::resolve(Some("usd"), package).expect("format resolves");
    assert_eq!(format_only.request(), TargetRequest::Explicit("usdz"));
    let explicit =
        TargetSelection::resolve(Some("usd:usda-1.0"), package).expect("dialect resolves");
    assert_eq!(explicit.request(), TargetRequest::Explicit("usda-1.0"));
    let layer = TargetSelection::resolve(None, Some(Path::new("assembly.usda")))
        .expect("format from the extension");
    assert_eq!(layer.request(), TargetRequest::Inherit);
    let binary = TargetSelection::resolve(None, Some(Path::new("assembly.usdc")))
        .expect("format from the extension");
    assert_eq!(binary.request(), TargetRequest::Explicit("usdc"));
}