indicatrix-cut 0.6.2

Desktop faceting-design editor: library browsing, spectral 3D rendering, material retargeting, and a solid inspection view.
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
//! Identity pins for the editor logic shared with the browser app through
//! `crates/indicatrix-editor`: the tier-row/format view models, the yield and
//! proportion texts, the validation banner, the tier/preform/material form parsers,
//! a scripted edit sequence (its native TOML and history depth), the cutting-sheet
//! HTML and diagram PNG bytes, the Retarget proposals, and the Optimize result views.
//!
//! Every value below was recorded BEFORE that logic moved out of this crate, through
//! the desktop's own paths (re-exports or thin adapters afterwards), so a passing run
//! proves the move changed nothing the desktop shows or writes. Hashes are 64-bit
//! FNV-1a over a stable byte form (the raw bytes, or a `Debug` dump), so they do not
//! depend on `std`'s hasher.
//!
//! Two of the pins hold Windows bits: `solid_status_is_pinned` dumps the facet planes,
//! whose normals come from the facet angles through `sin` and `cos`, and
//! `retarget_output_is_pinned` dumps proposals whose angles, masts and Optimize scores
//! run through the same calls. Those go through the platform math library, and glibc
//! rounds a few arguments differently from the Windows runtime, so the dumps differ on
//! Linux in their last digits (seen 2026-10-01). Both tests are ignored off Windows;
//! running them there with `--ignored` prints that platform's hashes.

use super::{
    cut_sheet::{write_cutting_sheet_html, write_diagram_png},
    loading::{
        TierFormFields, design_from_asc_text, material_selection_for_accepted_suggestion,
        parse_angle_only, parse_index_list, parse_new_design_form, parse_preform_form,
        parse_tier_form, parse_tier_target, ri_override_to_preserve,
    },
    material_lookup::{
        EditorMaterialLookup, material_for_refractive_index, material_guess_candidates,
        nearest_built_in_material, resolved_gem_material,
    },
    retarget::{self, CrownShift, RetargetMode},
    state::{
        EditorState, angle_nudge_coalesce_key, builtin_preset_names, cutting_instructions_rows,
        design_label_text, design_material_index_from_name, design_material_options,
        design_to_gpu_planes, first_unresolved_meet_name, gear_choice_to_teeth,
        gear_index_from_teeth, gear_remap_preview, girdle_and_ratio_texts, index_chip_items,
        manufacturability_warnings_tagged, material_name_from_index, parse_design_material_form,
        parse_yield_form, preform_mm_texts, preform_y_offset_mm_text, proportion_verdicts,
        proportions_texts, representative_crown_and_pavilion_angles_deg, result_is_stale,
        ri_source_text, status_text_and_is_problem, status_text_and_is_problem_from_solved,
        tier_items, tier_items_from_solved, tier_items_stale_with_last_solved, tier_matches_filter,
        tiers_incomplete_under_proposed_symmetry, yield_report_texts,
        yield_report_texts_from_solved,
    },
    view::{
        build_optimize_preview_design, facet_count_from_solved, girdle_and_ratio_texts_from_solved,
        optimize_change_rows, optimize_result_rows, optimize_status_text, parse_optimize_weights,
        preform_mm_texts_from_solved, proportions_texts_from_solved,
    },
};
use indicatrix::{
    geometry::meet_solver::{MeetConstraint, SolvedTier},
    optics::{dispersion::DispersionModel, materials::GemMaterial},
};
use indicatrix_cut_core::{
    AngleChange, ConstraintTier, Design, Edit, MaterialSelection, ObjectiveComponents,
    OptimizeOutcome, PreformSpec, RemapRounding,
    native::{SaveExtras, save_native_only_toml},
    templates::TEMPLATES,
};
use std::{collections::BTreeSet, fmt::Write as _};

fn fnv1a(bytes: &[u8]) -> u64 {
    indicatrix_solid::mesh_cache::fnv1a_64(bytes.iter().copied())
}

fn custom_materials() -> Vec<GemMaterial> {
    let mut garnet = GemMaterial::diamond();
    garnet.name = "My Garnet".to_string();
    garnet.dispersion = DispersionModel::Cauchy {
        a: 1.74,
        b: 0.0,
        c: 0.0,
    };
    vec![garnet]
}

fn custom_sg() -> Vec<(String, f64)> {
    vec![("My Garnet".to_string(), 3.9)]
}

fn template_design(index: usize) -> Design {
    let template = &TEMPLATES[index];
    Design::new(
        PreformSpec::block(2.0, 1.0, 2.0),
        template.schedule_meta(),
        template.tiers(),
    )
}

fn tier(name: &str, angle_deg: f64, indices: &[f64], constraint: MeetConstraint) -> ConstraintTier {
    ConstraintTier {
        angle_deg,
        name: name.to_string(),
        indices: indices.to_vec(),
        constraint,
        imported_meet: None,
        original_notes: None,
        detached: Vec::new(),
    }
}

/// Five fixed designs: three templates (one with a girdle diameter and a custom
/// material, one with an RI override), a design with no anchor, and the empty
/// startup design.
fn fixtures() -> Vec<Design> {
    let d0 = template_design(0);
    let mut d1 = template_design(1);
    d1.girdle_diameter_mm = Some(6.5);
    d1.material.name = Some("My Garnet".to_string());
    let mut d2 = template_design(2);
    d2.material.refractive_index_override = Some(1.66);
    let mut d3 = fresh_design();
    d3.tiers = vec![
        tier(
            "P1",
            -40.0,
            &[0.0, 12.0, 24.0],
            MeetConstraint::MeetExisting,
        ),
        tier(
            "C1",
            34.5,
            &[0.0, 12.0],
            MeetConstraint::MeetNamed(vec!["P1".to_string(), "Nope".to_string()]),
        ),
    ];
    vec![d0, d1, d2, d3, fresh_design()]
}

/// The startup design `EditorState::fresh` builds.
fn fresh_design() -> Design {
    Design::fresh(PreformSpec::cylinder(96, 1.5, 1.0, 1.5), 96, 8, 1.54)
}

fn solved(design: &Design) -> Option<Vec<SolvedTier>> {
    design.solve().ok()
}

fn row_dump(design: &Design, customs: &[GemMaterial]) -> String {
    let n_d = design.effective_refractive_index_with(customs);
    let mut out = String::new();
    let _ = writeln!(out, "{:?}", tier_items(design, n_d));
    let _ = writeln!(
        out,
        "{:?}",
        tier_items_stale_with_last_solved(design, n_d, None, &BTreeSet::new())
    );
    let _ = writeln!(out, "{:?}", manufacturability_warnings_tagged(design, None));
    if let Some(s) = solved(design) {
        let _ = writeln!(out, "{:?}", tier_items_from_solved(design, &s, n_d));
        let _ = writeln!(
            out,
            "{:?}",
            tier_items_stale_with_last_solved(design, n_d, Some(&s), &BTreeSet::from([0]))
        );
        let _ = writeln!(out, "{:?}", cutting_instructions_rows(design, &s));
        let _ = writeln!(
            out,
            "{:?}",
            manufacturability_warnings_tagged(design, Some(&s))
        );
    }
    let _ = writeln!(
        out,
        "{:?}",
        representative_crown_and_pavilion_angles_deg(design)
    );
    let _ = writeln!(
        out,
        "{} {}",
        tiers_incomplete_under_proposed_symmetry(design, 6, false),
        tiers_incomplete_under_proposed_symmetry(design, 8, true)
    );
    let names = ["T".to_string(), "Nope".to_string(), "crown".to_string()];
    let _ = writeln!(out, "{:?}", first_unresolved_meet_name(design, &names));
    for rounding in [
        RemapRounding::Nearest,
        RemapRounding::Floor,
        RemapRounding::Ceil,
    ] {
        let _ = writeln!(
            out,
            "{:?}",
            gear_remap_preview(design, design.meta.gear_teeth, 80, rounding)
        );
    }
    out
}

#[test]
fn tier_rows_and_formats_are_pinned() {
    let customs = custom_materials();
    let got: Vec<u64> = fixtures()
        .iter()
        .map(|d| fnv1a(row_dump(d, &customs).as_bytes()))
        .collect();
    let chips = format!(
        "{:?}",
        index_chip_items(&[0.0, 12.0, 24.5, 95.999_999_999_9], &[12.0])
    );
    assert_eq!(
        (got, fnv1a(chips.as_bytes())),
        (
            vec![
                11_903_447_992_736_157_353,
                4_501_220_536_405_905_337,
                15_303_436_785_860_121_761,
                10_651_142_665_447_707_371,
                17_206_618_785_430_422_565,
            ],
            8_329_660_661_060_172_670
        )
    );
}

fn yield_dump(design: &Design, customs: &[GemMaterial]) -> String {
    let sg = custom_sg();
    let n_d = design.effective_refractive_index_with(customs);
    let mut out = String::new();
    let _ = writeln!(out, "{:?}", yield_report_texts(design, &sg));
    let _ = writeln!(out, "{:?}", proportions_texts(design));
    let _ = writeln!(out, "{:?}", girdle_and_ratio_texts(design));
    let _ = writeln!(out, "{:?}", preform_mm_texts(design));
    if let Some(s) = solved(design) {
        let _ = writeln!(out, "{:?}", yield_report_texts_from_solved(design, &s, &sg));
        let _ = writeln!(out, "{:?}", proportions_texts_from_solved(design, &s));
        let _ = writeln!(out, "{:?}", girdle_and_ratio_texts_from_solved(design, &s));
        let _ = writeln!(out, "{:?}", preform_mm_texts_from_solved(design, &s));
        if let Some(props) = design.stone_proportions(&s) {
            let v = proportion_verdicts(design, &props, n_d);
            for verdict in [
                &v.table_pct,
                &v.crown_angle,
                &v.pavilion_angle,
                &v.total_depth_pct,
                &v.girdle_pct,
            ] {
                let _ = writeln!(out, "{} {}", verdict.level, verdict.reason);
            }
        }
    }
    out
}

#[test]
fn yield_and_proportion_texts_are_pinned() {
    let customs = custom_materials();
    let got: Vec<u64> = fixtures()
        .iter()
        .map(|d| fnv1a(yield_dump(d, &customs).as_bytes()))
        .collect();
    let misc = format!(
        "{} {} {} {} {}",
        preform_y_offset_mm_text(0.3, Some(4.0)),
        preform_y_offset_mm_text(-0.125, Some(3.3)),
        preform_y_offset_mm_text(0.3, None),
        design_label_text(Some("stone.asc")),
        design_label_text(None)
    );
    assert_eq!(
        (got, misc.as_str()),
        (
            vec![
                4_014_893_606_236_560_153,
                14_597_099_822_765_318_231,
                15_920_986_950_943_351_804,
                6_638_080_532_459_035_504,
                4_860_840_979_635_099_632,
            ],
            "1.20 -0.41  stone.asc Untitled design"
        )
    );
}

fn status_dump(design: &Design) -> String {
    let mut out = String::new();
    let _ = writeln!(out, "{:?}", status_text_and_is_problem(design));
    if let Some(s) = solved(design) {
        let _ = writeln!(
            out,
            "{:?}",
            status_text_and_is_problem_from_solved(design, &s)
        );
        let _ = writeln!(out, "{}", facet_count_from_solved(design, &s));
    }
    let _ = writeln!(out, "{:?}", design_to_gpu_planes(design));
    out
}

#[test]
#[cfg_attr(
    not(windows),
    ignore = "pinned to the Windows math library's rounding of the facet angles in the plane dump"
)]
fn solid_status_is_pinned() {
    let got: Vec<u64> = fixtures()
        .iter()
        .map(|d| fnv1a(status_dump(d).as_bytes()))
        .collect();
    let filters: Vec<bool> = [
        ("Crown Main", ""),
        ("Crown Main", "  main "),
        ("Crown Main", "MAIN"),
        ("Pavilion", "crown"),
    ]
    .iter()
    .map(|(h, f)| tier_matches_filter(h, f))
    .chain([
        result_is_stale(None, 3),
        result_is_stale(Some(3), 3),
        result_is_stale(Some(2), 3),
    ])
    .collect();
    assert_eq!(
        (got, filters),
        (
            vec![
                5_603_206_944_834_717_328,
                6_179_831_918_453_333_942,
                842_089_732_556_684_961,
                1_089_956_317_437_908_903,
                4_120_773_658_827_740_825,
            ],
            vec![true, true, true, false, false, false, true]
        )
    );
}

fn tier_form_dump() -> String {
    let cases: [(&str, i32, &str, &str, &str); 12] = [
        ("-41.0", 2, "0.65", "P1", "0, 12, 24"),
        ("34.5", 1, "G1, P1", "C1", "0:12:96"),
        ("0", 0, "", "T", ""),
        ("90", 2, "1.0", "G1", "3 x8"),
        ("91", 2, "1.0", "G1", "0"),
        ("abc", 2, "1.0", "G1", "0"),
        ("40", 1, " , ", "C2", "0"),
        ("40", 7, "", "C2", "0"),
        ("40", 3, "2.5", "C3", "0 96"),
        ("40", 2, "1", "Girdle", "0, 0"),
        ("-40", 2, "1", "P9", "5 X4"),
        ("40", 2, "1", "c1/New", "1.5"),
    ];
    let mut out = String::new();
    for (angle, kind, text, name, indices) in cases {
        let form = TierFormFields {
            angle,
            constraint_kind: kind,
            constraint_text: text,
            name,
            indices,
            gear_teeth_abs: 96,
            imported_meet: Some(MeetConstraint::MeetExisting),
            original_notes: Some("note".to_string()),
            other_tier_names: vec!["Girdle".to_string(), "C1".to_string()],
        };
        let _ = writeln!(out, "{:?}", parse_tier_form(form));
        let _ = writeln!(out, "{:?}", parse_tier_target(kind, text));
    }
    for (text, gear) in [
        ("0 12 24", 96),
        ("0:8:64", 64),
        ("90:-12:0", 96),
        ("12 x8", 96),
        ("12 x7", 96),
        ("1; 2, 3", 96),
        ("100", 96),
        ("0:0:5", 96),
        ("x8", 96),
    ] {
        let _ = writeln!(out, "{:?}", parse_index_list(text, gear));
    }
    for text in ["-41.0", " 12 ", "NaN", "inf", "90.01", "-90", "x"] {
        let _ = writeln!(out, "{:?}", parse_angle_only(text));
    }
    out
}

fn preform_and_material_dump() -> String {
    let mut out = String::new();
    for (shape, hw, lw, depth) in [
        (0, "1.2", "1.5", "0.8"),
        (1, "1.0", "1.0", "0.8"),
        (1, "0", "1.0", "0.8"),
        (0, "w", "1", "1"),
    ] {
        let _ = writeln!(out, "{:?}", parse_preform_form(shape, hw, lw, depth, 64));
    }
    let preform = PreformSpec::cylinder(80, 1.5, 1.0, 1.5);
    for (symmetry, material) in [("6", 9), ("8", 0), ("0", 1), ("x", 3), ("4", 99)] {
        let _ = writeln!(
            out,
            "{:?}",
            parse_new_design_form(80, preform, symmetry, true, material)
        );
    }
    let current = MaterialSelection {
        name: Some("My Garnet".to_string()),
        specific_gravity_override: Some(3.1),
        refractive_index_override: Some(1.8),
        body_colour_override: None,
    };
    for (girdle, sg) in [("6.5", ""), ("", "3.2"), ("-1", ""), ("x", ""), ("", "0")] {
        let _ = writeln!(out, "{:?}", parse_yield_form(girdle, 3, sg, &current));
    }
    let customs = custom_materials();
    let options = design_material_options(&customs);
    let _ = writeln!(out, "{options:?} {:?}", builtin_preset_names());
    for (index, ri) in [(0, ""), (1, "1.9"), (5, "x"), (40, "0.9"), (-1, "")] {
        let _ = writeln!(
            out,
            "{:?}",
            parse_design_material_form(index, ri, &options, &current)
        );
    }
    for name in [Some("diamond"), Some("My Garnet"), Some("Nope"), None] {
        let _ = writeln!(
            out,
            "{} {}",
            design_material_index_from_name(name, &options),
            ri_source_text(
                &MaterialSelection {
                    name: name.map(str::to_string),
                    specific_gravity_override: None,
                    refractive_index_override: None,
                    body_colour_override: None,
                },
                &customs
            )
        );
    }
    let _ = writeln!(out, "{}", ri_source_text(&current, &customs));
    for index in -1..40 {
        let _ = write!(out, "{:?},", material_name_from_index(index));
    }
    for (preset, custom) in [(0, ""), (5, ""), (6, "50"), (6, "-3"), (9, "x"), (-2, "72")] {
        let _ = writeln!(out, "{:?}", gear_choice_to_teeth(preset, custom));
    }
    for teeth in [96, 80, 77, 72, 64, 120, 50, -96] {
        let _ = write!(out, "{},", gear_index_from_teeth(teeth));
    }
    out
}

fn material_search_dump() -> String {
    let mut out = String::new();
    for n_d in [1.54, 1.76, 1.72, 2.16, 1.9, 2.417, 1.62] {
        let _ = writeln!(
            out,
            "{:?} {:?} {:?} {:?} {:?}",
            nearest_built_in_material(n_d, 0.02),
            material_guess_candidates(n_d, 0.05),
            material_for_refractive_index(n_d).map(|(name, gem)| (name, gem.name)),
            ri_override_to_preserve("Quartz", n_d),
            material_selection_for_accepted_suggestion("Sapphire", n_d, &MaterialSelection::none())
        );
    }
    let asc = "GemCad 5.0\ng 96 0.0\ny 4 y\nI 1.54\na -41.000000 0.64991234 92 n 1 84\n";
    match design_from_asc_text("pin.asc", asc, Some("1.25")) {
        Ok(loaded) => {
            let _ = writeln!(
                out,
                "{:?} {:?} {:?} {} {:?}",
                loaded.design.tiers,
                loaded.design.preform,
                loaded.design.meta,
                loaded.used_placeholder,
                loaded.asc_filename
            );
        }
        Err(e) => {
            let _ = writeln!(out, "{e}");
        }
    }
    out
}

#[test]
fn form_parsing_is_pinned() {
    let got = (
        fnv1a(tier_form_dump().as_bytes()),
        fnv1a(preform_and_material_dump().as_bytes()),
        fnv1a(material_search_dump().as_bytes()),
    );
    assert_eq!(
        got,
        (
            7_808_841_572_456_204_551,
            15_758_716_057_735_816_184,
            4_662_660_901_061_571_820
        )
    );
}

fn nudge(state: &mut EditorState, index: usize, delta: f64, out: &mut String) {
    let current = state.design.tiers[index].angle_deg;
    let outcome = state.apply_coalescing(
        Edit::RetargetAngles {
            changes: vec![(index, current, current + delta)],
        },
        angle_nudge_coalesce_key(&[index]),
    );
    let _ = writeln!(out, "nudge {outcome:?}");
}

/// The scripted sequence: three tiers added, three coalescing nudges, undo, redo,
/// mirror, complete orbit -- through the same `EditorState` entry points the
/// desktop's callbacks use.
fn scripted_sequence() -> (String, String) {
    let mut log = String::new();
    let mut state = EditorState::fresh();
    let eight = [0.0, 12.0, 24.0, 36.0, 48.0, 60.0, 72.0, 84.0];
    for (index, t) in [
        tier("G1", 90.0, &eight, MeetConstraint::ScaleReference(1.0)),
        tier(
            "P1",
            -40.0,
            &eight,
            MeetConstraint::MeetNamed(vec!["G1".to_string()]),
        ),
        tier(
            "C1",
            34.5,
            &[0.0, 12.0, 24.0],
            MeetConstraint::MeetNamed(vec!["G1".to_string()]),
        ),
    ]
    .into_iter()
    .enumerate()
    {
        let outcome = state.apply(Edit::AddTier { index, tier: t });
        let _ = writeln!(log, "add {outcome:?}");
    }
    for _ in 0..3 {
        nudge(&mut state, 1, -0.25, &mut log);
    }
    let _ = writeln!(log, "undo {:?}", state.undo());
    let _ = writeln!(log, "redo {:?}", state.redo());
    let mirrored = state
        .design
        .mirror_indices(2)
        .and_then(|edit| state.apply(edit));
    let _ = writeln!(log, "mirror {mirrored:?}");
    let units = state.design.orbit_units(2);
    let anchors: Vec<f64> = units
        .iter()
        .flatten()
        .filter(|unit| !unit.is_complete())
        .filter_map(|unit| unit.members.first().copied())
        .collect();
    for position in anchors {
        let outcome = state
            .design
            .add_orbit_member(2, position)
            .and_then(|edit| state.apply(edit));
        let _ = writeln!(log, "orbit {position} {outcome:?}");
    }
    let toml = save_native_only_toml(&state.design, "pin.asc", None, &SaveExtras::default())
        .unwrap_or_else(|e| e.to_string());
    let generation = state.generation.load(std::sync::atomic::Ordering::Relaxed);
    let _ = writeln!(log, "generation {generation} dirty {}", state.is_dirty());
    let mut depth = 0;
    while matches!(state.undo(), Ok(true)) {
        depth += 1;
    }
    let mut redo_depth = 0;
    while matches!(state.redo(), Ok(true)) {
        redo_depth += 1;
    }
    let replayed = save_native_only_toml(&state.design, "pin.asc", None, &SaveExtras::default())
        .unwrap_or_else(|e| e.to_string());
    let _ = writeln!(
        log,
        "depth {depth} redo {redo_depth} replay_equal {}",
        replayed == toml
    );
    (toml, log)
}

#[test]
fn scripted_edit_sequence_is_pinned() {
    let (toml, log) = scripted_sequence();
    assert_eq!(
        (
            fnv1a(toml.as_bytes()),
            toml.len(),
            fnv1a(log.as_bytes()),
            log.as_str()
        ),
        (
            8_529_034_122_875_928_046,
            1078,
            6_969_999_423_672_735_711,
            "add Ok(())\nadd Ok(())\nadd Ok(())\nnudge Ok(())\nnudge Ok(())\nnudge Ok(())\n\
             undo Ok(true)\nredo Ok(true)\nmirror Ok(())\norbit -0 Ok(())\n\
             generation 10 dirty true\ndepth 6 redo 6 replay_equal true\n"
        )
    );
}

#[test]
fn cutting_sheet_and_diagram_bytes_are_pinned() {
    let customs = custom_materials();
    let dir =
        std::env::temp_dir().join(format!("indicatrix-cut-editor-pins-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let mut got: Vec<(usize, u64)> = Vec::new();
    for (i, design) in fixtures().iter().take(3).enumerate() {
        let s = solved(design).expect("templates solve");
        let html = dir.join(format!("sheet{i}.html"));
        write_cutting_sheet_html(design, &s, &html, &customs).unwrap();
        let bytes = std::fs::read(&html).unwrap();
        got.push((bytes.len(), fnv1a(&bytes)));
        let png = dir.join(format!("diagram{i}.png"));
        write_diagram_png(design, &s, &png).unwrap();
        let bytes = std::fs::read(&png).unwrap();
        got.push((bytes.len(), fnv1a(&bytes)));
    }
    let _ = std::fs::remove_dir_all(&dir);
    assert_eq!(
        got,
        [
            (80803, 7_067_950_724_805_645_129),
            (122_055, 17_204_512_700_538_578_767),
            (80229, 2_758_044_140_940_148_492),
            (121_075, 82_326_695_121_513_353),
            (81140, 15_725_016_216_430_464_209),
            (122_698, 1_653_056_722_631_375_577),
        ]
    );
}

fn resolved(
    selection: &MaterialSelection,
    customs: &[GemMaterial],
) -> indicatrix_cut_core::ResolvedMaterial {
    let lookup = EditorMaterialLookup::new(customs);
    let resolved = selection.resolve(&lookup);
    let gem = resolved_gem_material(selection, &lookup);
    indicatrix_cut_core::ResolvedMaterial { gem, ..resolved }
}

#[test]
#[cfg_attr(
    not(windows),
    ignore = "pinned to the Windows math library's rounding in the proposal angles and the Optimize scoring"
)]
fn retarget_output_is_pinned() {
    let customs = custom_materials();
    let targets = [
        MaterialSelection {
            name: Some("Sapphire".to_string()),
            specific_gravity_override: None,
            refractive_index_override: None,
            body_colour_override: None,
        },
        MaterialSelection {
            name: Some("My Garnet".to_string()),
            specific_gravity_override: None,
            refractive_index_override: None,
            body_colour_override: None,
        },
        MaterialSelection {
            name: None,
            specific_gravity_override: None,
            refractive_index_override: Some(2.0),
            body_colour_override: None,
        },
    ];
    let crowns = [
        CrownShift::default(),
        CrownShift {
            fraction: 0.5,
            scale_by_ratio: false,
        },
        CrownShift {
            fraction: 0.0,
            scale_by_ratio: true,
        },
    ];
    let mut got = Vec::new();
    for design in fixtures().iter().take(3) {
        let mut dump = String::new();
        for target in &targets {
            let target = resolved(target, &customs);
            for crown in crowns {
                match retarget::build_proposal(
                    design,
                    &target,
                    crown,
                    RetargetMode::Shift,
                    &customs,
                ) {
                    Ok(proposal) => {
                        let _ = writeln!(
                            dump,
                            "{proposal:?} {:?}",
                            retarget::apply(design, &proposal)
                        );
                    }
                    Err(e) => {
                        let _ = writeln!(dump, "{e}");
                    }
                }
            }
            let optimize = retarget::build_proposal(
                design,
                &target,
                CrownShift::default(),
                RetargetMode::Optimize(indicatrix_cut_core::OptimizeConfig::default()),
                &customs,
            );
            let _ = writeln!(dump, "{:?}", optimize.err());
        }
        got.push(fnv1a(dump.as_bytes()));
    }
    assert_eq!(
        got,
        [
            501_324_477_922_563_186,
            796_213_417_993_335_374,
            5_945_022_532_389_829_461,
        ]
    );
}

fn sample_outcome(cancelled: bool, polish: usize) -> OptimizeOutcome {
    OptimizeOutcome {
        before: ObjectiveComponents {
            windowing_pct: 12.5,
            extinction_pct: 8.0,
            tilt_brilliance_pct: 60.0,
        },
        before_score: 20.0,
        before_yield_loss_pct: 30.0,
        after: ObjectiveComponents {
            windowing_pct: 9.25,
            extinction_pct: 11.0,
            tilt_brilliance_pct: 60.0,
        },
        after_score: 15.0,
        after_yield_loss_pct: 31.5,
        evaluations: 42,
        changes: vec![
            AngleChange {
                index: 1,
                from_deg: -40.0,
                to_deg: -41.5,
            },
            AngleChange {
                index: 9,
                from_deg: 30.0,
                to_deg: 31.0,
            },
        ],
        cancelled,
        polish_evaluations: polish,
        polish_improvement: 0.42,
    }
}

#[test]
fn optimize_views_are_pinned() {
    let design = template_design(0);
    let mut dump = String::new();
    for (cancelled, polish) in [(false, 0), (true, 31)] {
        let outcome = sample_outcome(cancelled, polish);
        let _ = writeln!(
            dump,
            "{:?} {} {:?}",
            optimize_result_rows(&outcome),
            optimize_status_text(&outcome),
            optimize_change_rows(&outcome, &design)
        );
        let preview = build_optimize_preview_design(&design, &outcome);
        let _ = writeln!(dump, "{:?}", preview.tiers);
    }
    let mut empty = sample_outcome(false, 0);
    empty.changes.clear();
    let _ = writeln!(dump, "{}", optimize_status_text(&empty));
    for (w, e, t, y) in [
        ("1", "2.5", "0", 0.0),
        ("x", "1", "1", 0.4),
        ("1", "-0.5", "1", 0.4),
        ("NaN", "1", "1", 0.0),
    ] {
        let _ = writeln!(dump, "{:?}", parse_optimize_weights(w, e, t, y));
    }
    assert_eq!(fnv1a(dump.as_bytes()), 167_664_852_704_437_451);
}