indicatrix-editor 0.7.2

GUI-free faceting-design editor logic shared by the desktop editor and the wasm web app: the EditorSession (design, undo/redo with caller-supplied coalescing timestamps, selection, dirty tracking), tier/preform/material form parsing, plain-struct view models, the cutting sheet, Retarget, the guided walkthrough and the solve policy. No GUI toolkit, no threads, no clock, no filesystem.
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
//! The step content's own invariants, and the goal predicates behind the
//! automatic advance.

use super::{
    EIGHT_FOLD_INDICES, Group, HIGHLIGHT_TARGETS, MANUAL, NEW_DESIGN_CREATED, STEPS, Step,
    goal_reached, reached_completion, same_index_set,
    steps::{TIER_STEP, TIER_STEP_EXACT},
};
use crate::{
    EditorSession,
    view_model::{rows::tier_items_stale, solid_status::status_text_and_is_problem},
};
use indicatrix::geometry::meet_solver::{Block, MeetConstraint, classify_blocks};
use indicatrix_cut_core::{
    ConstraintTier, DEFAULT_MIN_FACET_AREA_FRACTION_OF_W2, Design, Edit, ManufacturabilityWarning,
    Risk, check_manufacturability, critical_angle_deg, exceeds_preform, windowing_risk,
};

/// Every completion key a step may use: [`MANUAL`], [`NEW_DESIGN_CREATED`] (an
/// event), and the state goals `progress::goal_reached` evaluates. A typo in a
/// step's key would otherwise silently mean "never completes".
const COMPLETION_KEYS: &[&str] = &[
    MANUAL,
    NEW_DESIGN_CREATED,
    "tier_named:G1",
    "tier_named:P1",
    "tier_named:C1",
    "tier_named:T",
    "material:Diamond",
    "solved_closed",
    "yield_applied",
];

#[test]
fn every_step_uses_a_known_completion_key_and_a_recognized_highlight_target() {
    for step in STEPS {
        assert!(
            COMPLETION_KEYS.contains(&step.completion),
            "step {:?} has an unknown completion key {:?}",
            step.title,
            step.completion
        );
        assert!(
            HIGHLIGHT_TARGETS.contains(&step.highlight_target),
            "step {:?} has an unrecognized highlight_target {:?}",
            step.title,
            step.highlight_target
        );
    }
}

#[test]
fn every_action_step_says_what_to_do_and_what_it_waits_for() {
    for step in STEPS {
        assert_ne!(step.title, "");
        assert_ne!(step.intro, "");
        if step.completion != MANUAL {
            assert!(
                !step.actions.is_empty(),
                "step {:?} has no actions",
                step.title
            );
            assert_ne!(
                step.waiting, "",
                "step {:?} has no waiting text",
                step.title
            );
            assert_ne!(step.check, "", "step {:?} has no check line", step.title);
        }
        for action in step.actions {
            assert_ne!(*action, "", "step {:?} has a blank action line", step.title);
        }
    }
}

#[test]
fn step_titles_are_unique() {
    for (i, step) in STEPS.iter().enumerate() {
        for other in &STEPS[i + 1..] {
            assert_ne!(step.title, other.title, "duplicate guide step title");
        }
    }
}

/// The walkthrough as the manual's chapter 7 lays it out: ten steps, the two
/// reading steps (8, "Check the orbits", and 10, the closing note) wait for Next,
/// and the closing step locks nothing.
#[test]
fn the_walkthrough_is_ten_steps_and_ends_unlocked() {
    assert_eq!(STEPS.len(), 10);
    let manual: Vec<&str> = STEPS
        .iter()
        .filter(|step| step.completion == MANUAL)
        .map(|step| step.title)
        .collect();
    assert_eq!(manual, ["Check the orbits", "You have a working design"]);
    let last = STEPS.last().expect("ten steps");
    for group in [
        Group::NewDesign,
        Group::TierForm,
        Group::TierTable,
        Group::DesignSettings,
        Group::Solve,
        Group::PreformTab,
        Group::Advanced,
        Group::ViewTabs,
        Group::FileOps,
        Group::History,
    ] {
        assert!(last.allow.contains(&group), "closing step locks {group:?}");
    }
}

/// Each action step unlocks the control its own actions name.
#[test]
fn each_action_step_unlocks_its_own_control() {
    let unlocks = |completion: &str, group: Group| {
        STEPS
            .iter()
            .find(|step| step.completion == completion)
            .is_some_and(|step| step.allow.contains(&group))
    };
    assert!(unlocks(NEW_DESIGN_CREATED, Group::NewDesign));
    for tier in ["G1", "P1", "C1", "T"] {
        assert!(unlocks(&format!("tier_named:{tier}"), Group::TierForm));
    }
    assert!(unlocks("material:Diamond", Group::DesignSettings));
    assert!(unlocks("solved_closed", Group::Solve));
    assert!(unlocks("yield_applied", Group::PreformTab));
    // The first step must not leave file actions open: a Load Selected there would
    // replace the design the walkthrough is about to build.
    assert!(!unlocks(NEW_DESIGN_CREATED, Group::FileOps));
}

/// A tier with an exact-scale meet. The one place the guide tests build a
/// `ConstraintTier` literal (`goal_tests` and `catalog_tests` reuse it).
pub(super) fn tier(name: &str, angle_deg: f64, indices: &[f64]) -> ConstraintTier {
    ConstraintTier {
        angle_deg,
        name: name.to_string(),
        indices: indices.to_vec(),
        constraint: MeetConstraint::ScaleReference(1.0),
        imported_meet: None,
        original_notes: None,
        detached: Vec::new(),
    }
}

/// A fresh 96-tooth, 8-fold design (`EditorSession::fresh`) with `tiers` added
/// through `History`, the way the tier form adds them.
pub(super) fn design_with(tiers: &[ConstraintTier]) -> Design {
    let mut state = EditorSession::fresh();
    for (index, tier) in tiers.iter().enumerate() {
        state
            .apply(Edit::AddTier {
                index,
                tier: tier.clone(),
            })
            .expect("add tier");
    }
    state.design
}

#[test]
fn tier_goals_need_the_right_name_angle_and_indices() {
    let good = design_with(&[tier("G1", 90.0, &EIGHT_FOLD_INDICES)]);
    assert!(goal_reached("tier_named:G1", &good, false));
    // Case and surrounding whitespace in the typed name do not matter.
    assert!(goal_reached(
        "tier_named:G1",
        &design_with(&[tier(" g1", 90.0, &EIGHT_FOLD_INDICES)]),
        false
    ));
    // The classic mistake the step warns about: 0.0 instead of 90.0.
    assert!(!goal_reached(
        "tier_named:G1",
        &design_with(&[tier("G1", 0.0, &EIGHT_FOLD_INDICES)]),
        false
    ));
    // One index short is not the girdle the step asked for.
    assert!(!goal_reached(
        "tier_named:G1",
        &design_with(&[tier("G1", 90.0, &EIGHT_FOLD_INDICES[..7])]),
        false
    ));
    // Right tier, wrong name.
    assert!(!goal_reached(
        "tier_named:G1",
        &design_with(&[tier("Girdle", 90.0, &EIGHT_FOLD_INDICES)]),
        false
    ));
    // A different step's tier does not complete this one.
    assert!(!goal_reached("tier_named:P1", &good, false));
    assert!(goal_reached(
        "tier_named:P1",
        &design_with(&[tier("P1", -40.0, &EIGHT_FOLD_INDICES)]),
        false
    ));
    assert!(goal_reached(
        "tier_named:C1",
        &design_with(&[tier("C1", 34.5, &EIGHT_FOLD_INDICES)]),
        false
    ));
    assert!(goal_reached(
        "tier_named:T",
        &design_with(&[tier("T", 0.0, &[])]),
        false
    ));
    assert!(!goal_reached(
        "tier_named:T",
        &design_with(&[tier("T", 0.0, &[12.0])]),
        false
    ));
}

#[test]
fn index_sets_compare_as_gear_positions() {
    assert!(same_index_set(&[84.0, 0.0, 12.0], &[0.0, 12.0, 84.0], 96.0));
    assert!(same_index_set(&[96.0], &[0.0], 96.0));
    assert!(same_index_set(&[], &[], 96.0));
    assert!(same_index_set(&[0.0], &[], 96.0));
    assert!(!same_index_set(&[0.0, 12.0], &[0.0, 24.0], 96.0));
    assert!(!same_index_set(&[0.0, 0.0], &[0.0, 12.0], 96.0));
}

#[test]
fn material_solve_and_yield_goals_read_the_design() {
    let mut design = design_with(&[tier("G1", 90.0, &EIGHT_FOLD_INDICES)]);
    assert!(!goal_reached("material:Diamond", &design, false));
    design.material.name = Some("Diamond".to_string());
    assert!(goal_reached("material:Diamond", &design, false));

    assert!(goal_reached("solved_closed", &design, true));
    assert!(!goal_reached("solved_closed", &design, false));
    // A zero-tier design "solves" to its bare preform, which is not the stone.
    assert!(!goal_reached("solved_closed", &design_with(&[]), true));

    assert!(!goal_reached("yield_applied", &design, false));
    design.girdle_diameter_mm = Some(6.5);
    assert!(goal_reached("yield_applied", &design, false));
}

/// `reached_completion` is `goal_reached` on the step's own key: what the two UIs call
/// after each change to the design.
#[test]
fn reached_completion_names_the_step_key_only_once_its_goal_holds() {
    let empty = design_with(&[]);
    let girdle = design_with(&[tier("G1", 90.0, &EIGHT_FOLD_INDICES)]);
    let step_of = |completion: &str| {
        STEPS
            .iter()
            .position(|step| step.completion == completion)
            .expect("a step with that key")
    };
    let g1 = step_of("tier_named:G1");
    assert_eq!(reached_completion(g1, &empty, false), None);
    assert_eq!(
        reached_completion(g1, &girdle, false),
        Some("tier_named:G1")
    );
    // A later step's goal does not complete an earlier one, nor the reverse.
    assert_eq!(
        reached_completion(step_of("tier_named:P1"), &girdle, false),
        None
    );
    let solve = step_of("solved_closed");
    assert_eq!(reached_completion(solve, &girdle, false), None);
    assert_eq!(
        reached_completion(solve, &girdle, true),
        Some("solved_closed")
    );
    // Reading steps, the new-design event and an index past the end never complete.
    assert_eq!(reached_completion(step_of(MANUAL), &girdle, true), None);
    assert_eq!(
        reached_completion(step_of(NEW_DESIGN_CREATED), &girdle, true),
        None
    );
    assert_eq!(reached_completion(STEPS.len(), &girdle, true), None);
}

/// Neither a reading step nor the new-design event can be completed by a design
/// predicate: the first only advances on Next, the second only through the
/// explicit `notify` in `do_new_design_create`'s success path, so a rejected form
/// (which never reaches it) can never advance.
#[test]
fn manual_and_event_keys_never_complete_from_state() {
    let design = design_with(&[tier("G1", 90.0, &EIGHT_FOLD_INDICES)]);
    assert!(!goal_reached(MANUAL, &design, true));
    assert!(!goal_reached(NEW_DESIGN_CREATED, &design, true));
    assert!(!goal_reached("tier_named:X9", &design, true));
}

/// The Meets setting a step's "Meets: ..." line tells the learner to choose.
fn typed_meets(text: &str) -> MeetConstraint {
    if text == "Unspecified vertex" {
        return MeetConstraint::MeetExisting;
    }
    if let Some(value) = text.strip_prefix("Exact scale value \u{2014} ") {
        return MeetConstraint::ScaleReference(value.trim().parse().expect("a scale value"));
    }
    if let Some(names) = text.strip_prefix("Named facet(s) \u{2014} ") {
        return MeetConstraint::MeetNamed(
            names
                .split(',')
                .map(|name| name.trim().to_owned())
                .collect(),
        );
    }
    panic!("a Meets line this test does not know: {text:?}");
}

/// The tier a tier-authoring step tells the learner to add, read back from the step's own
/// action lines (the text the guide panel shows), so the tests below check the numbers a
/// learner really types.
fn typed_tier(step: &Step) -> ConstraintTier {
    let (mut angle, mut meets, mut name, mut indices) = (None, None, None, None);
    for action in step.actions {
        if let Some(rest) = action.strip_prefix("Angle (deg): ") {
            // The girdle's line goes on to mention the preset: only the number counts.
            angle = rest
                .split_whitespace()
                .next()
                .and_then(|number| number.parse::<f64>().ok());
        } else if let Some(rest) = action.strip_prefix("Meets: ") {
            meets = Some(typed_meets(rest));
        } else if let Some(rest) = action.strip_prefix("Name: ") {
            name = Some(rest.trim().to_owned());
        } else if let Some(rest) = action.strip_prefix("Indices: ") {
            indices = Some(if rest == "leave blank" {
                Vec::new()
            } else {
                rest.split(',')
                    .map(|index| index.trim().parse::<f64>().expect("an index"))
                    .collect()
            });
        }
    }
    let name = name.unwrap_or_else(|| panic!("step {:?} has no Name line", step.title));
    let mut typed = tier(
        &name,
        angle.unwrap_or_else(|| panic!("step {:?} has no Angle line", step.title)),
        &indices.unwrap_or_else(|| panic!("step {:?} has no Indices line", step.title)),
    );
    typed.constraint = meets.unwrap_or_else(|| panic!("step {:?} has no Meets line", step.title));
    typed
}

/// The steps that add a tier, in order.
fn tier_steps() -> impl Iterator<Item = &'static Step> {
    STEPS
        .iter()
        .filter(|step| step.completion.starts_with("tier_named:"))
}

/// The tiers the four tier steps tell the learner to add.
fn walkthrough_tiers() -> Vec<ConstraintTier> {
    tier_steps().map(typed_tier).collect()
}

/// The value of a tier that states its scale.
fn scale_of(tier: &ConstraintTier) -> f64 {
    let MeetConstraint::ScaleReference(value) = tier.constraint else {
        panic!("tier {:?} does not state a scale value", tier.name);
    };
    value
}

/// The walkthrough has to reach its own "Solve and check" step: the numbers it tells the
/// learner to type give a design that solves to a closed stone with a girdle band and a
/// table. The solver needs an exact scale value in each block (crown, pavilion and girdle),
/// so a design with a Named facet(s) meet as the only crown and pavilion tiers never solves.
#[test]
fn the_walkthroughs_typed_values_solve_to_a_closed_stone_with_a_girdle_and_a_table() {
    let tiers = walkthrough_tiers();
    let names: Vec<&str> = tiers.iter().map(|tier| tier.name.as_str()).collect();
    assert_eq!(names, ["G1", "P1", "C1", "T"]);
    let design = design_with(&tiers);

    // What the learner types is what each step's own goal asks for.
    for step in tier_steps() {
        assert!(
            goal_reached(step.completion, &design, false),
            "the values typed in step {:?} do not meet its goal",
            step.title
        );
    }

    // Every block has an exact scale value of its own.
    let inputs = design.meet_tier_inputs();
    let blocks = classify_blocks(&inputs);
    for block in [Block::Crown, Block::Pavilion, Block::Girdle] {
        assert!(blocks.contains(&block), "no {block:?} tier");
        assert!(
            inputs.iter().zip(&blocks).any(|(input, &of)| {
                of == block && matches!(input.constraint, MeetConstraint::ScaleReference(_))
            }),
            "the {block:?} block has no exact scale value"
        );
    }
    let solved = design
        .solve()
        .expect("every block is anchored, so the design solves");

    // The Solve step's own verdict: a closed solid, not a problem.
    let (status, is_problem) = status_text_and_is_problem(&design);
    assert!(
        !is_problem && status.starts_with("Closed solid"),
        "{status}"
    );

    // A girdle band thicker than nothing but thin, and a table.
    let proportions = design
        .stone_proportions(&solved)
        .expect("a closed solid has proportions");
    assert!(
        proportions.girdle_thickness.is_some_and(|band| band > 0.0),
        "the girdle band has no thickness"
    );
    let girdle = proportions.girdle_to_width_percent.expect("a girdle band");
    assert!(
        (1.0..=9.0).contains(&girdle),
        "girdle {girdle:.2} % of width"
    );
    let table = proportions.table_percent.expect("a table facet");
    assert!(
        (35.0..=80.0).contains(&table),
        "table {table:.1} % of width"
    );

    // Every facet reaches the surface, and the blank the first step asks for is big enough:
    // no preform plane cuts the stone.
    let vanishing: Vec<String> =
        check_manufacturability(&design, &solved, DEFAULT_MIN_FACET_AREA_FRACTION_OF_W2)
            .into_iter()
            .filter_map(|warning| match warning {
                ManufacturabilityWarning::VanishingFacet { tier_name, .. } => Some(tier_name),
                _ => None,
            })
            .collect();
    assert!(
        vanishing.is_empty(),
        "facets that never appear: {vanishing:?}"
    );
    assert!(
        exceeds_preform(&design, &solved).is_none(),
        "the stone is bigger than the blank"
    );
}

/// Why the table states its height too: an Unspecified vertex table on a crown of main facets
/// alone settles on the next vertex below the point where they meet, which is the girdle's top
/// edge, so the table would cut the whole crown away.
#[test]
fn an_unspecified_table_would_sink_to_the_girdle_and_cut_the_crown_away() {
    let mut tiers = walkthrough_tiers();
    let girdle = scale_of(&tiers[0]);
    let crown = scale_of(&tiers[2]);
    tiers[3].constraint = MeetConstraint::MeetExisting;
    let design = design_with(&tiers);
    let solved = design.solve().expect("the three blocks are still anchored");

    let crown_angle = tiers[2].angle_deg.to_radians();
    let girdle_top = girdle.mul_add(-crown_angle.sin(), crown) / crown_angle.cos();
    assert!(
        (solved[3].mast - girdle_top).abs() < 1e-6,
        "the table landed at {}, the girdle's top edge is at {girdle_top}",
        solved[3].mast
    );
    // The stated table is well above it.
    assert!(scale_of(&walkthrough_tiers()[3]) > girdle_top + 0.2);
}

/// The Simple interface leaves "Exact scale value" out of the Meets list for a new tier, so
/// each of the four steps that ask for it unlocks the Advanced controls and says why. The
/// girdle step also names the preset that fills the entry in, but a learner who types the
/// girdle by hand needs the full list as much as the others do.
#[test]
fn the_steps_that_ask_for_an_exact_scale_value_can_be_done_in_the_simple_interface() {
    for step in tier_steps() {
        let asks = step
            .actions
            .iter()
            .any(|action| action.starts_with("Meets: Exact scale value"));
        assert!(asks, "step {:?} should state its scale value", step.title);
        assert!(
            step.allow.contains(&Group::Advanced),
            "step {:?} asks for Exact scale value, which Simple hides, and unlocks nothing \
             that shows it",
            step.title
        );
        assert!(
            step.why.contains("shows the Advanced controls"),
            "step {:?}: the explanation says why the Advanced controls show",
            step.title
        );
    }
    // The girdle step keeps the shortcut and says what the one click sets.
    let girdle = step_with("tier_named:G1");
    assert!(
        girdle.actions.iter().any(|action| {
            action.contains("Girdle Facet Preset") && action.contains("Exact scale value 1.0")
        }),
        "the girdle step names the preset and what it sets"
    );
}

/// One definition of the two tier-step lock sets serves the walkthrough and the generated
/// lessons; the second is the first plus the Advanced controls.
#[test]
fn the_exact_scale_lock_set_is_the_plain_tier_lock_set_plus_advanced() {
    assert!(TIER_STEP_EXACT.contains(&Group::Advanced));
    assert!(!TIER_STEP.contains(&Group::Advanced));
    assert!(
        TIER_STEP_EXACT
            .iter()
            .filter(|group| **group != Group::Advanced)
            .eq(TIER_STEP.iter())
    );
}

/// The Solve step tells the learner to look at a tier's angle and Meets setting and offers the
/// Add Anchor button for a block with no anchor, so the tier form and the tier table must be
/// open on it.
#[test]
fn the_solve_step_leaves_open_what_its_explanation_sends_the_learner_to() {
    let solve = step_with("solved_closed");
    assert!(solve.allow.contains(&Group::Solve));
    assert!(solve.why.contains("tier form") && solve.why.contains("Add Anchor"));
    assert!(solve.allow.contains(&Group::TierForm));
    assert!(solve.allow.contains(&Group::TierTable));
}

/// The step that completes on `completion`.
fn step_with(completion: &str) -> &'static Step {
    STEPS
        .iter()
        .find(|step| step.completion == completion)
        .unwrap_or_else(|| panic!("no step completes on {completion:?}"))
}

/// What the learner sees in the P1 row's MARGIN cell: its text and the colour word its risk
/// level draws in (`tier_table_row.slint`: green safe, amber marginal, red windows).
fn p1_margin_cell(design: &Design) -> (String, &'static str) {
    let rows = tier_items_stale(design, design.effective_refractive_index());
    let row = rows
        .iter()
        .find(|row| row.name == "P1")
        .expect("the walkthrough's design has a P1 row");
    let colour = match row.risk_level {
        0 => "green",
        1 => "amber",
        2 => "red",
        other => panic!("risk level {other} draws no colour"),
    };
    (row.margin_text.clone(), colour)
}

/// The colour word the windowing rule gives a margin.
const fn colour_of(risk: Risk) -> &'static str {
    match risk {
        Risk::Safe => "green",
        Risk::Marginal => "amber",
        Risk::Windows => "red",
    }
}

/// What the pavilion step and the material step tell the learner to look for in the P1 row is
/// what the tier table shows, so the wording cannot drift from the windowing rule again: with
/// no material the design uses its default index 1.54 (critical angle 40.5 degrees) and a
/// -40 degree pavilion reads a few tenths of a degree below it, in red; Diamond turns it green.
#[test]
fn the_pavilion_and_material_steps_describe_the_margin_the_tier_table_shows() {
    let mut design = design_with(&walkthrough_tiers());
    let pavilion_angle = walkthrough_tiers()[1].angle_deg;

    // Step 3's state: no material has been picked.
    let pavilion = step_with("tier_named:P1");
    let default_ri = design.effective_refractive_index();
    let (text, colour) = p1_margin_cell(&design);
    assert_eq!(
        colour,
        colour_of(windowing_risk(pavilion_angle, default_ri)),
        "the row's colour follows the windowing rule"
    );
    assert!(
        pavilion.check.contains(&text) && pavilion.check.contains(colour),
        "step {:?} asks for a MARGIN cell that does not read {text} in {colour}: {}",
        pavilion.title,
        pavilion.check
    );
    assert!(
        !pavilion.check.contains("Safe"),
        "the first look at P1 is not Safe"
    );
    assert!(
        pavilion.why.contains(&format!("{default_ri}"))
            && pavilion
                .why
                .contains(&format!("{:.1} degrees", critical_angle_deg(default_ri))),
        "the explanation gives the default index and its critical angle: {}",
        pavilion.why
    );

    // Step 6's state: Diamond is applied.
    design.material.name = Some("Diamond".to_owned());
    let material = step_with("material:Diamond");
    let diamond_ri = design.effective_refractive_index();
    let (text, colour) = p1_margin_cell(&design);
    assert_eq!(
        colour,
        colour_of(windowing_risk(pavilion_angle, diamond_ri)),
        "the row's colour follows the windowing rule"
    );
    assert_eq!(colour, "green", "Diamond makes the walkthrough's P1 Safe");
    assert!(
        material.check.contains(&text) && material.check.contains(colour),
        "step {:?} asks for a MARGIN cell that does not read {text} in {colour}: {}",
        material.title,
        material.check
    );
    assert!(
        material
            .why
            .contains(&format!("{:.1} degrees", critical_angle_deg(diamond_ri)))
            && material
                .why
                .contains(&format!("{:.1} degrees", critical_angle_deg(default_ri))),
        "the explanation gives both critical angles: {}",
        material.why
    );
}