indicatrix-cut 0.7.2

Desktop faceting-design editor: library browsing, spectral 3D rendering, material retargeting, and a solid inspection view.
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
//! `EditorState`'s history/dirty-tracking tests: undo/redo, angle-nudge coalescing,
//! multi-select pruning, the inline-set-angle reject-before-apply contract, the
//! generation/design-epoch counters, and `result_is_stale`.

use super::super::*;
use indicatrix_cut_core::{ConstraintTier, Edit, FreshDesignSpec, MaterialSelection};

#[test]
fn editor_state_add_tier_then_undo_then_redo_round_trips() {
    let mut state = EditorState::fresh();
    let before = state.design.clone();

    state
        .apply(Edit::AddTier {
            index: 0,
            tier: ConstraintTier {
                angle_deg: 0.0,
                name: "T".to_string(),
                indices: vec![],
                constraint: indicatrix::geometry::meet_solver::MeetConstraint::ScaleReference(0.32),
                imported_meet: None,
                original_notes: None,
                detached: Vec::new(),
            },
        })
        .expect("add must apply");
    assert_eq!(state.design.tiers.len(), 1);
    assert!(state.history.can_undo());
    assert!(!state.history.can_redo());

    assert!(state.undo().unwrap());
    assert_eq!(state.design, before);
    assert!(!state.history.can_undo());
    assert!(state.history.can_redo());

    assert!(state.redo().unwrap());
    assert_eq!(state.design.tiers.len(), 1);
}

#[test]
fn editor_state_undo_on_an_empty_history_is_a_harmless_no_op() {
    let mut state = EditorState::fresh();
    assert!(!state.undo().unwrap());
    assert!(!state.redo().unwrap());
}

// --- EditorState::fresh_from_spec ---

#[test]
fn fresh_from_spec_round_trips_gear_symmetry_mirror_and_material() {
    let spec = FreshDesignSpec {
        gear_teeth: 80,
        symmetry_order: 5,
        mirror: false,
        material: MaterialSelection {
            name: Some("Quartz".to_string()),
            specific_gravity_override: None,
            refractive_index_override: Some(1.55),
            body_color_override: None,
            body_color_bands_override: None,
            absorption_path_scale_override: None,
        },
        preform: indicatrix_cut_core::PreformSpec::cylinder(80, 1.4, 1.0, 1.3),
    };
    let state = EditorState::fresh_from_template(spec, 0);
    assert_eq!(state.design.meta.gear_teeth, 80);
    assert_eq!(state.design.meta.symmetry_order, 5);
    assert!(!state.design.meta.mirror);
    assert_eq!(state.design.material.name.as_deref(), Some("Quartz"));
    assert_eq!(state.design.material.refractive_index_override, Some(1.55));
    assert_eq!(state.design.tiers.len(), 0);
    assert!(!state.history.can_undo());
}

// A `SetMaterial` edit never touches tier geometry, so the ONLY thing that could
// invalidate a material-keyed cache (in `bridge::render_thread`) is the material
// itself changing.

#[test]
fn set_material_edit_leaves_every_tier_untouched_but_bumps_generation() {
    let mut state = EditorState::fresh();
    state
        .apply(Edit::AddTier {
            index: 0,
            tier: ConstraintTier {
                angle_deg: -40.0,
                name: "P1".to_string(),
                indices: vec![0.0, 12.0],
                constraint: indicatrix::geometry::meet_solver::MeetConstraint::ScaleReference(0.5),
                imported_meet: None,
                original_notes: None,
                detached: Vec::new(),
            },
        })
        .unwrap();
    let tiers_before = state.design.tiers.clone();
    let generation_before = state.generation.load(std::sync::atomic::Ordering::Relaxed);

    state
        .apply(Edit::SetMaterial {
            material: MaterialSelection {
                name: Some("Quartz".to_string()),
                specific_gravity_override: None,
                refractive_index_override: None,
                body_color_override: None,
                body_color_bands_override: None,
                absorption_path_scale_override: None,
            },
        })
        .unwrap();

    assert_eq!(
        state.design.tiers, tiers_before,
        "SetMaterial must not touch geometry"
    );
    assert!(
        state.generation.load(std::sync::atomic::Ordering::Relaxed) > generation_before,
        "SetMaterial must still bump generation, which is what a material-keyed cache checks"
    );
}

// --- angle_nudge_coalesce_key ---

#[test]
fn angle_nudge_coalesce_key_is_order_independent() {
    assert_eq!(
        angle_nudge_coalesce_key(&[3, 4]),
        angle_nudge_coalesce_key(&[4, 3]),
    );
}

#[test]
fn angle_nudge_coalesce_key_distinguishes_a_single_tier_from_a_group_containing_it() {
    // Nudging tier 3 alone must never coalesce with nudging {3, 4} together, even
    // though the same tier is involved in both.
    assert_ne!(
        angle_nudge_coalesce_key(&[3]),
        angle_nudge_coalesce_key(&[3, 4]),
    );
}

#[test]
fn angle_nudge_coalesce_key_distinguishes_disjoint_targets() {
    assert_ne!(
        angle_nudge_coalesce_key(&[0]),
        angle_nudge_coalesce_key(&[1]),
    );
}

// --- EditorState::nudge_angles: the arrows move the SHOWN number ---

/// The tier table prints a pavilion tier's angle without its minus sign, so the inline cell's
/// Up arrow and the Offset box have to make that shown number bigger for a pavilion tier too.
#[test]
fn the_nudge_path_moves_the_shown_angle_of_a_pavilion_tier_up_on_up() {
    let mut state = EditorState::fresh();
    for (index, (name, angle)) in [("C1", 34.5), ("P1", -40.0)].into_iter().enumerate() {
        state
            .apply(Edit::AddTier {
                index,
                tier: ConstraintTier {
                    angle_deg: angle,
                    name: name.to_string(),
                    indices: vec![],
                    constraint: indicatrix::geometry::meet_solver::MeetConstraint::ScaleReference(
                        0.5,
                    ),
                    imported_meet: None,
                    original_notes: None,
                    detached: Vec::new(),
                },
            })
            .unwrap();
    }
    state.nudge_angles(&[0, 1], 0.5).unwrap().unwrap();
    let shown: Vec<f64> = state
        .design
        .tiers
        .iter()
        .map(|tier| tier.angle_deg.abs())
        .collect();
    assert_eq!(shown, [35.0, 40.5]);
    assert!(
        state.design.tiers[1].angle_deg.is_sign_negative(),
        "the pavilion tier keeps its stored sign"
    );
}

// --- EditorState::apply_coalescing / multi_selected pruning ---

fn pavilion_tier(name: &str, angle_deg: f64) -> ConstraintTier {
    ConstraintTier {
        angle_deg,
        name: name.to_string(),
        indices: vec![],
        constraint: indicatrix::geometry::meet_solver::MeetConstraint::ScaleReference(0.5),
        imported_meet: None,
        original_notes: None,
        detached: Vec::new(),
    }
}

#[test]
fn apply_coalescing_through_editor_state_merges_into_one_undo_step() {
    let mut state = EditorState::fresh();
    state
        .apply(Edit::AddTier {
            index: 0,
            tier: pavilion_tier("P1", -40.0),
        })
        .unwrap();
    let before_nudges = state.design.clone();
    let key = angle_nudge_coalesce_key(&[0]);

    state
        .apply_coalescing(
            Edit::ModifyTier {
                index: 0,
                tier: pavilion_tier("P1", -40.1),
            },
            key,
        )
        .expect("first nudge must apply");
    state
        .apply_coalescing(
            Edit::ModifyTier {
                index: 0,
                tier: pavilion_tier("P1", -40.2),
            },
            key,
        )
        .expect("second nudge must apply");
    assert_eq!(state.design.tiers[0].angle_deg, -40.2);

    // One undo reverts BOTH nudges, all the way back to before the burst started.
    assert!(state.undo().unwrap());
    assert_eq!(state.design, before_nudges);
}

#[test]
fn multi_selected_is_pruned_when_a_tier_it_names_is_removed() {
    let mut state = EditorState::fresh();
    state
        .apply(Edit::AddTier {
            index: 0,
            tier: pavilion_tier("P1", -40.0),
        })
        .unwrap();
    state
        .apply(Edit::AddTier {
            index: 1,
            tier: pavilion_tier("P2", -35.0),
        })
        .unwrap();
    state.multi_selected.insert(0);
    state.multi_selected.insert(1);

    state.apply(Edit::RemoveTier { index: 1 }).unwrap();

    assert!(
        state.multi_selected.contains(&0),
        "tier 0 still exists and must stay selected"
    );
    assert!(
        !state.multi_selected.contains(&1),
        "tier 1 no longer exists -- must be dropped from the selection, not left dangling"
    );
}

// --- inline_set_angle's own rejection path (`loading::tier_form::parse_angle_only`) ---

#[test]
fn inline_set_angle_rejection_leaves_the_design_and_history_untouched() {
    // Drives the very function the inline angle cell's callback calls
    // (`set_tier_angle_from_text`) with text that cannot be an angle: it must report
    // `Err` (which the callback shows as an error toast) and leave the design, the
    // undo history and the generation counter exactly as they were.
    let mut state = EditorState::fresh();
    state
        .apply(Edit::AddTier {
            index: 0,
            tier: pavilion_tier("P1", -40.0),
        })
        .unwrap();

    for bad in [
        "not-a-number",
        "",
        "NaN",
        "inf",
        "-inf",
        "1e999",
        "90.01",
        "-410",
    ] {
        let history_before = state.history.clone();
        let design_before = state.design.clone();
        let generation_before = state.generation.load(std::sync::atomic::Ordering::Relaxed);

        let outcome = state.set_tier_angle_from_text(0, bad);

        assert!(
            outcome.is_err(),
            "{bad:?} must be rejected, got {:?}",
            outcome.map(|_| ())
        );
        assert_eq!(state.history, history_before, "{bad:?} touched the history");
        assert_eq!(state.design, design_before, "{bad:?} touched the design");
        assert_eq!(
            state.generation.load(std::sync::atomic::Ordering::Relaxed),
            generation_before,
            "{bad:?} bumped the generation"
        );
    }
}

#[test]
fn inline_set_angle_accepts_a_well_formed_value() {
    // The accept-path counterpart: a valid angle parses to the exact `f64` an
    // `Edit::ModifyTier` would then carry.
    assert_eq!(
        crate::gui::editor::loading::parse_angle_only(" -41.5 ").unwrap(),
        -41.5
    );
}

// --- design_epoch: telling an edit apart from a wholesale replacement ---
//
// A background Deep Solve/Optimize can only distinguish "the cutter edited this
// design" from "the cutter loaded a different one" if these two counters move
// independently -- see `EditorState::design_epoch`'s own doc comment and
// `callbacks::solve_actions::RunProvenance`.

fn a_tier() -> ConstraintTier {
    ConstraintTier {
        angle_deg: -40.0,
        name: "P1".to_string(),
        indices: vec![0.0, 12.0],
        constraint: indicatrix::geometry::meet_solver::MeetConstraint::ScaleReference(0.5),
        imported_meet: None,
        original_notes: None,
        detached: Vec::new(),
    }
}

#[test]
fn an_ordinary_edit_bumps_generation_but_never_the_design_epoch() {
    let mut state = EditorState::fresh();
    let epoch_before = state
        .design_epoch
        .load(std::sync::atomic::Ordering::Relaxed);
    let generation_before = state.generation.load(std::sync::atomic::Ordering::Relaxed);

    state
        .apply(Edit::AddTier {
            index: 0,
            tier: a_tier(),
        })
        .unwrap();

    assert_ne!(
        state.generation.load(std::sync::atomic::Ordering::Relaxed),
        generation_before,
        "an edit must still look stale to a run in flight"
    );
    assert_eq!(
        state
            .design_epoch
            .load(std::sync::atomic::Ordering::Relaxed),
        epoch_before,
        "an edit is not a new design -- a finished run's verdict is still about this \
         one, and is shown with a caveat rather than discarded"
    );
}

#[test]
fn replace_wholesale_bumps_the_design_epoch_too() {
    let mut state = EditorState::fresh();
    let epoch_before = state
        .design_epoch
        .load(std::sync::atomic::Ordering::Relaxed);
    let generation_before = state.generation.load(std::sync::atomic::Ordering::Relaxed);

    state.replace_wholesale(EditorState::fresh());

    assert_ne!(
        state.generation.load(std::sync::atomic::Ordering::Relaxed),
        generation_before
    );
    assert_ne!(
        state
            .design_epoch
            .load(std::sync::atomic::Ordering::Relaxed),
        epoch_before,
        "New/Load must be distinguishable from an edit"
    );
}

#[test]
fn a_clone_captured_before_a_replacement_still_observes_the_epoch_bump() {
    // The whole point of carrying the `Arc` across the replacement instead of
    // adopting the replacement's own fresh one: a completion closure captured its
    // clone minutes ago and is the only thing that will ever read it.
    let mut state = EditorState::fresh();
    let captured = std::sync::Arc::clone(&state.design_epoch);
    let started = captured.load(std::sync::atomic::Ordering::Relaxed);

    state.replace_wholesale(EditorState::fresh());

    assert_ne!(
        captured.load(std::sync::atomic::Ordering::Relaxed),
        started,
        "a fresh Arc here would silently defeat the check for exactly the runs it \
         exists to catch"
    );
}

#[test]
fn the_epoch_keeps_counting_across_several_replacements() {
    let mut state = EditorState::fresh();
    let captured = std::sync::Arc::clone(&state.design_epoch);
    let started = captured.load(std::sync::atomic::Ordering::Relaxed);

    state.replace_wholesale(EditorState::fresh());
    state.replace_wholesale(EditorState::fresh());

    assert_eq!(
        captured.load(std::sync::atomic::Ordering::Relaxed),
        started + 2,
        "loading design B then design C must not land back on A's own epoch"
    );
}

// --- result_is_stale ---

#[test]
fn a_result_with_no_generation_stamped_is_never_stale() {
    assert!(!result_is_stale(None, 0));
    assert!(!result_is_stale(None, 42));
}

#[test]
fn a_result_stamped_with_the_current_generation_is_not_stale() {
    assert!(!result_is_stale(Some(5), 5));
}

#[test]
fn a_result_is_stale_one_generation_later() {
    // A result computed at generation N is stale at N+1.
    assert!(result_is_stale(Some(5), 6));
}

#[test]
fn a_result_is_stale_many_generations_later_too() {
    assert!(result_is_stale(Some(0), 100));
}

#[test]
fn deep_solve_result_generation_starts_unset_on_a_fresh_state() {
    let state = EditorState::fresh();
    assert_eq!(state.deep_solve_result_generation, None);
}

#[test]
fn replace_wholesale_carries_over_the_replacements_own_unset_deep_solve_result() {
    // A brand-new/loaded design has never had its own Deep Solve run yet, however
    // stale the PREVIOUS design's verdict was -- see `EditorState::
    // deep_solve_result_generation`'s own doc comment for why this must not
    // survive a wholesale replacement.
    let mut state = EditorState::fresh();
    state.deep_solve_result_generation = Some(3);
    state.replace_wholesale(EditorState::fresh());
    assert_eq!(state.deep_solve_result_generation, None);
}