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
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
use super::{
    opening::Decision,
    stored::{EXPOSURE_RANGE, FORMAT_VERSION, env_source, known_rig},
    *,
};
use crate::settings::{
    SettingsFile,
    model::{AppSettings, Backdrop},
};

const DESIGN: &str = "0b9e6a4e-5f1d-4c7a-9a52-1e3f7d8c2b10";

fn tent() -> LightingValues {
    LightingValues {
        version: FORMAT_VERSION,
        lighting_rig: "Light tent + black cards".to_string(),
        light_yaw_deg: 120.0,
        light_pitch_deg: 40.0,
        exposure: 1.5,
        surface_glare: 0.5,
        backdrop: Backdrop::White,
        env_map_path: Some("C:/hdr/studio.hdr".to_string()),
    }
}

fn memory_db() -> Mutex<Database> {
    Mutex::new(Database::new(Some(":memory:")).expect("open an in-memory library"))
}

fn close(a: f32, b: f32) -> bool {
    (a - b).abs() < 1e-3
}

// --- the stored JSON ---

#[test]
fn the_stored_values_round_trip_through_json() {
    let values = tent();
    let json = values.to_json().expect("serialises");
    assert_eq!(LightingValues::from_json(&json), Ok(values));
}

#[test]
fn a_row_with_only_the_required_keys_gets_defaults_for_the_rest() {
    let json = r#"{"lighting_rig":"Gem Studio Ring Lights","light_yaw_deg":10.0,
                       "light_pitch_deg":30.0,"exposure":1.0}"#;
    let values = LightingValues::from_json(json).expect("loads");
    assert_eq!(values.version, FORMAT_VERSION);
    assert!(close(values.surface_glare, 1.0), "full glare by default");
    assert_eq!(values.backdrop, Backdrop::default());
    assert_eq!(values.env_map_path, None);
}

#[test]
fn keys_a_newer_version_added_are_ignored() {
    let json = r#"{"version":7,"lighting_rig":"ISO hemisphere","light_yaw_deg":10.0,
                       "light_pitch_deg":30.0,"exposure":1.0,"a_new_setting":{"x":1}}"#;
    let values = LightingValues::from_json(json).expect("loads");
    assert_eq!(values.lighting_rig, "ISO hemisphere");
}

#[test]
fn text_that_is_not_the_stored_format_does_not_load() {
    assert!(LightingValues::from_json("not json").is_err());
    assert!(
        LightingValues::from_json("{}").is_err(),
        "the rig is required"
    );
    assert!(LightingValues::from_json(r#"{"lighting_rig":"x"}"#).is_err());
}

#[test]
fn values_are_limited_like_the_live_controls_and_never_not_a_number() {
    let wild = LightingValues {
        light_yaw_deg: 400.0,
        light_pitch_deg: 0.0,
        exposure: 99.0,
        surface_glare: 7.0,
        env_map_path: Some("   ".to_string()),
        ..tent()
    }
    .sanitised();
    assert!(
        close(wild.light_yaw_deg, 40.0),
        "yaw wraps: {}",
        wild.light_yaw_deg
    );
    assert!(
        close(wild.light_pitch_deg, PITCH_RANGE_RAD.0.to_degrees()),
        "pitch is raised to the lowest the controls allow: {}",
        wild.light_pitch_deg
    );
    assert!(close(wild.exposure, EXPOSURE_RANGE.1));
    assert!(close(wild.surface_glare, 1.0));
    assert_eq!(wild.env_map_path, None, "a blank path is no path");

    let nan = LightingValues {
        light_yaw_deg: f32::NAN,
        light_pitch_deg: f32::INFINITY,
        exposure: f32::NAN,
        ..tent()
    }
    .sanitised();
    let defaults = AppSettings::default();
    assert!(close(nan.light_yaw_deg, defaults.light_yaw_deg));
    assert!(nan.light_pitch_deg.is_finite());
    assert!(close(nan.exposure, defaults.exposure));
}

#[test]
fn the_normal_lighting_is_read_from_the_settings() {
    let mut app = AppSettings::default();
    let normal = LightingValues::from_app_settings(&app);
    assert_eq!(normal.lighting_rig, app.lighting_rig);
    assert!(close(normal.light_yaw_deg, app.light_yaw_deg));
    assert_eq!(normal.env_map_path, None, "no map is stored as no path");

    app.env_map_path = "C:/hdr/studio.hdr".to_string();
    app.backdrop = Backdrop::White;
    let with_map = LightingValues::from_app_settings(&app);
    assert_eq!(with_map.env_map_path.as_deref(), Some("C:/hdr/studio.hdr"));
    assert_eq!(with_map.backdrop, Backdrop::White);
}

#[test]
fn the_live_view_is_captured_from_the_render_context() {
    let ctx = RenderContext {
        light_yaw: 120.0_f32.to_radians(),
        light_pitch: 40.0_f32.to_radians(),
        exposure: 1.5,
        lighting_preset: LightingPreset::LightTent,
        surface_glare: 0.5,
        backdrop: Backdrop::White,
        ..RenderContext::default()
    };
    let values = LightingValues::from_live(&ctx, Some("C:/hdr/studio.hdr".to_string()));
    assert_eq!(values.lighting_rig, "Light tent + black cards");
    assert!(
        close(values.light_yaw_deg, 120.0),
        "{}",
        values.light_yaw_deg
    );
    assert!(
        close(values.light_pitch_deg, 40.0),
        "{}",
        values.light_pitch_deg
    );
    assert!(close(values.exposure, 1.5));
    assert!(close(values.surface_glare, 0.5));
    assert_eq!(values.backdrop, Backdrop::White);
    assert_eq!(values.env_map_path.as_deref(), Some("C:/hdr/studio.hdr"));
}

#[test]
fn a_path_typed_but_never_loaded_is_not_stored() {
    assert_eq!(env_path_for_capture(false, "C:/hdr/studio.hdr"), None);
    assert_eq!(env_path_for_capture(true, "   "), None);
    assert_eq!(
        env_path_for_capture(true, "  C:/hdr/studio.hdr "),
        Some("C:/hdr/studio.hdr".to_string())
    );
}

#[test]
fn the_summary_says_what_was_saved_in_plain_words() {
    let text = tent().summary();
    assert!(text.contains("Light tent + black cards"), "{text}");
    assert!(text.contains("120°"), "{text}");
    assert!(text.contains("40°"), "{text}");
    assert!(text.contains("1.50x"), "{text}");
    assert!(text.contains("HDR environment map"), "{text}");
    assert!(text.ends_with('.'), "{text}");

    let plain = LightingValues {
        env_map_path: None,
        ..tent()
    }
    .summary();
    assert!(!plain.contains("HDR"), "{plain}");
}

// --- is the rig still there ---

#[test]
fn every_rig_this_build_has_is_known_under_its_own_label() {
    for rig in LightingPreset::ALL {
        // A UV lamp is a rig only of a build with the `physical-color` feature.
        let expected = (!rig.is_uv_lamp() || cfg!(feature = "physical-color")).then_some(rig);
        assert_eq!(known_rig(rig.label()), expected, "{}", rig.label());
    }
}

#[test]
fn older_labels_the_parser_migrates_are_still_known() {
    assert_eq!(
        known_rig("D65 Daylight (5500K)"),
        Some(LightingPreset::Daylight)
    );
    assert_eq!(
        known_rig("ISO hemisphere (GemRay-style)"),
        Some(LightingPreset::IsoHemisphere)
    );
    assert_eq!(
        known_rig("Soft dome + ring lights"),
        Some(LightingPreset::LightTent)
    );
}

#[test]
fn a_rig_this_build_does_not_have_is_not_known() {
    assert_eq!(known_rig("Ring flash"), None);
    assert_eq!(known_rig(""), None);
}

// --- reading a row ---

#[test]
fn a_stored_row_becomes_a_lookup() {
    assert_eq!(lookup_from_row(None), Lookup::NoRow);
    let json = tent().to_json().expect("serialises");
    assert_eq!(lookup_from_row(Some(&json)), Lookup::Row(tent()));

    let gone = LightingValues {
        lighting_rig: "Ring flash".to_string(),
        ..tent()
    }
    .to_json()
    .expect("serialises");
    assert_eq!(
        lookup_from_row(Some(&gone)),
        Lookup::UnknownRig("Ring flash".to_string())
    );
    assert!(matches!(
        lookup_from_row(Some("garbage")),
        Lookup::Unreadable(_)
    ));
}

#[test]
fn only_a_missing_row_has_no_row() {
    assert!(!Lookup::NoRow.has_row());
    assert!(Lookup::Row(tent()).has_row());
    assert!(Lookup::UnknownRig("x".to_string()).has_row());
    assert!(Lookup::Unreadable("x".to_string()).has_row());
}

#[test]
fn the_dialog_line_explains_an_unusable_row() {
    assert_eq!(Lookup::NoRow.summary_text(), "");
    assert_eq!(Lookup::Row(tent()).summary_text(), tent().summary());
    let unknown = Lookup::UnknownRig("Ring flash".to_string()).summary_text();
    assert!(unknown.contains("Ring flash"), "{unknown}");
    assert!(unknown.contains("normal lighting"), "{unknown}");
    let unreadable = Lookup::Unreadable("bad".to_string()).summary_text();
    assert!(unreadable.contains("bad"), "{unreadable}");
}

// --- what opening a design does ---

#[test]
fn saved_lighting_is_applied_whether_or_not_another_designs_is_showing() {
    for active in [false, true] {
        let decision = decide(&Lookup::Row(tent()), active);
        assert_eq!(decision.plan, Plan::Apply(tent()));
        assert_eq!(decision.note, Some(Note::UsingSaved));
    }
}

#[test]
fn a_design_without_lighting_changes_nothing_unless_another_designs_is_showing() {
    assert_eq!(
        decide(&Lookup::NoRow, false),
        Decision {
            plan: Plan::Keep,
            note: None
        }
    );
    assert_eq!(
        decide(&Lookup::NoRow, true),
        Decision {
            plan: Plan::Restore,
            note: Some(Note::BackToNormal)
        }
    );
}

#[test]
fn unusable_lighting_falls_back_to_the_normal_lighting_and_says_so() {
    let unknown = Lookup::UnknownRig("Ring flash".to_string());
    assert_eq!(
        decide(&unknown, false),
        Decision {
            plan: Plan::Keep,
            note: Some(Note::UnknownRig("Ring flash".to_string()))
        }
    );
    assert_eq!(decide(&unknown, true).plan, Plan::Restore);

    let unreadable = Lookup::Unreadable("bad".to_string());
    assert_eq!(decide(&unreadable, false).plan, Plan::Keep);
    assert_eq!(decide(&unreadable, true).plan, Plan::Restore);
    assert_eq!(
        decide(&unreadable, true).note,
        Some(Note::Unreadable("bad".to_string()))
    );
}

#[test]
fn the_notes_are_plain_sentences_and_problems_are_warnings() {
    assert_eq!(
        note_text(&Note::UsingSaved),
        ("Using this design's saved lighting.".to_string(), "info")
    );
    assert_eq!(note_text(&Note::BackToNormal).1, "info");
    let (text, kind) = note_text(&Note::UnknownRig("Ring flash".to_string()));
    assert!(text.contains("Ring flash"), "{text}");
    assert_eq!(kind, "warning");
    assert_eq!(note_text(&Note::Unreadable("bad".to_string())).1, "warning");
}

#[test]
fn a_problem_is_reported_once_per_design() {
    let mut told = HashSet::new();
    assert!(first_time_for(&mut told, DESIGN));
    assert!(!first_time_for(&mut told, DESIGN), "not again");
    assert!(first_time_for(
        &mut told,
        "1c9e6a4e-5f1d-4c7a-9a52-1e3f7d8c2b10"
    ));
}

#[test]
fn a_note_never_replaces_a_warning_or_an_error_on_screen() {
    assert!(may_show_over(false, "error"), "nothing is showing");
    assert!(may_show_over(true, "info"));
    assert!(may_show_over(true, "success"));
    assert!(!may_show_over(true, "warning"));
    assert!(!may_show_over(true, "error"));
}

// --- the library ---

#[test]
fn saved_lighting_is_read_back_and_forgotten() {
    let db = memory_db();
    assert_eq!(read_lookup(&db, DESIGN), Lookup::NoRow);

    store_values(&db, DESIGN, &tent(), 1_700_000_000).expect("stores");
    assert_eq!(read_lookup(&db, DESIGN), Lookup::Row(tent()));

    let changed = LightingValues {
        exposure: 2.0,
        ..tent()
    };
    store_values(&db, DESIGN, &changed, 1_700_000_100).expect("replaces");
    assert_eq!(read_lookup(&db, DESIGN), Lookup::Row(changed));

    forget_values(&db, DESIGN).expect("forgets");
    assert_eq!(read_lookup(&db, DESIGN), Lookup::NoRow);
    forget_values(&db, DESIGN).expect("forgetting nothing is not an error");
}

#[test]
fn the_row_is_filed_under_the_rig_name_and_the_design() {
    let db = memory_db();
    store_values(&db, DESIGN, &tent(), 5).expect("stores");
    let row = db
        .lock()
        .expect("lock")
        .design_lighting(DESIGN)
        .expect("reads")
        .expect("a row");
    assert_eq!(row.preset_name, "Light tent + black cards");
    assert_eq!(row.updated_at, 5);
    // Another design has its own (no) lighting.
    assert_eq!(
        read_lookup(&db, "1c9e6a4e-5f1d-4c7a-9a52-1e3f7d8c2b10"),
        Lookup::NoRow
    );
}

#[test]
fn a_stored_rig_this_build_lacks_reads_as_unknown() {
    let db = memory_db();
    let gone = LightingValues {
        lighting_rig: "Ring flash".to_string(),
        ..tent()
    };
    store_values(&db, DESIGN, &gone, 1).expect("stores");
    assert_eq!(
        read_lookup(&db, DESIGN),
        Lookup::UnknownRig("Ring flash".to_string())
    );
}

#[test]
fn a_damaged_row_and_a_bad_design_id_are_reported_not_trusted() {
    let db = memory_db();
    db.lock()
        .expect("lock")
        .set_design_lighting(DESIGN, "Light tent + black cards", "{broken", 1)
        .expect("the library keeps the text as given");
    assert!(matches!(read_lookup(&db, DESIGN), Lookup::Unreadable(_)));

    assert!(store_values(&db, "not-a-uuid", &tent(), 1).is_err());
    assert!(forget_values(&db, "not-a-uuid").is_err());
    assert!(matches!(
        read_lookup(&db, "not-a-uuid"),
        Lookup::Unreadable(_)
    ));
}

// --- the session ---

fn remember_normal() {
    SESSION.with(|session| {
        session.borrow_mut().normal = Some(Normal {
            values: tent(),
            env_map: None,
            env_ui: EnvUi {
                loaded: false,
                status: String::new(),
                path_text: String::new(),
            },
        });
    });
}

#[test]
fn choosing_a_named_preset_lets_go_of_the_remembered_normal_lighting() {
    assert!(
        !override_active(),
        "a fresh session shows no design lighting"
    );
    remember_normal();
    assert!(override_active());
    normal_lighting_chosen();
    assert!(
        !override_active(),
        "the preset is the normal lighting now; nothing is restored over it later"
    );
}

#[test]
fn a_fresh_session_has_no_open_design_and_no_handles() {
    assert_eq!(open_design(), None);
    assert!(handles().is_none(), "the hook does nothing before setup");
}

// --- where the HDR map comes from ---

#[test]
fn a_map_that_is_loaded_already_is_not_decoded_again() {
    assert_eq!(
        env_source(Some("C:/hdr/studio.hdr"), Some("C:/hdr/studio.hdr")),
        EnvSource::AlreadyLoaded
    );
    // The dialog's field may carry blanks at the ends.
    assert_eq!(
        env_source(Some(" C:/hdr/studio.hdr"), Some("C:/hdr/studio.hdr  ")),
        EnvSource::AlreadyLoaded
    );
}

#[test]
fn another_map_or_none_loaded_is_decoded_and_no_map_is_the_studio_rig() {
    assert_eq!(
        env_source(Some("C:/hdr/other.hdr"), Some("C:/hdr/studio.hdr")),
        EnvSource::Decode("C:/hdr/other.hdr".to_string())
    );
    assert_eq!(
        env_source(Some(" C:/hdr/studio.hdr "), None),
        EnvSource::Decode("C:/hdr/studio.hdr".to_string()),
        "nothing loaded yet; the path is read as typed, without the blanks"
    );
    assert_eq!(env_source(None, None), EnvSource::Studio);
    assert_eq!(
        env_source(None, Some("C:/hdr/studio.hdr")),
        EnvSource::Studio,
        "the design uses no map, so the loaded one goes"
    );
}

#[test]
fn a_map_typed_but_never_loaded_is_not_taken_for_the_loaded_one() {
    // `loaded_env_path` hands `env_path_for_capture`'s answer to `env_source`.
    let loaded = env_path_for_capture(false, "C:/hdr/studio.hdr");
    assert_eq!(
        env_source(Some("C:/hdr/studio.hdr"), loaded.as_deref()),
        EnvSource::Decode("C:/hdr/studio.hdr".to_string())
    );
}

// --- answers that arrive late ---

#[test]
fn an_answer_for_an_earlier_opening_is_dropped() {
    let first = begin_generation();
    assert!(generation_is_current(first));
    let second = begin_generation();
    assert!(!generation_is_current(first), "another design opened");
    assert!(generation_is_current(second));
    assert!(answer_is_current(3, 3));
    assert!(!answer_is_current(3, 4));
}

#[test]
fn choosing_a_named_preset_voids_what_is_on_its_way() {
    let asked = begin_generation();
    remember_normal();
    normal_lighting_chosen();
    assert!(
        !generation_is_current(asked),
        "a map decoded for the design must not land over the preset"
    );
    assert!(!override_active());
}

// --- saving the lighting while a map is still being decoded ---

#[test]
fn the_map_being_decoded_is_stored_instead_of_the_one_still_on_screen() {
    let studio = Some("C:/hdr/studio.hdr".to_string());
    let big = Some("C:/hdr/big.hdr".to_string());
    // The dialog still shows studio.hdr while big.hdr, the one the lighting names, decodes.
    assert_eq!(
        env_path_to_store(Some(" C:/hdr/big.hdr "), true, "C:/hdr/studio.hdr"),
        big
    );
    assert_eq!(
        env_path_to_store(Some("C:/hdr/big.hdr"), false, ""),
        big,
        "no map was loaded before; the one on its way still counts"
    );
    // Nothing on its way: the map in use, exactly as before.
    assert_eq!(env_path_to_store(None, true, " C:/hdr/studio.hdr "), studio);
    assert_eq!(env_path_to_store(None, false, "C:/hdr/typed.hdr"), None);
    assert_eq!(env_path_to_store(None, true, "  "), None);
    // A blank pending path is no path.
    assert_eq!(
        env_path_to_store(Some("  "), true, "C:/hdr/studio.hdr"),
        studio
    );
}

#[test]
fn a_decode_on_its_way_is_remembered_until_something_newer_begins() {
    assert_eq!(pending_env_path(), None, "a fresh session decodes nothing");
    set_pending_env_path(Some("C:/hdr/big.hdr".to_string()));
    assert_eq!(pending_env_path().as_deref(), Some("C:/hdr/big.hdr"));

    // Another design opening voids it: its map is not the one the lighting names any more.
    begin_generation();
    assert_eq!(pending_env_path(), None);

    // So does choosing a named lighting preset.
    set_pending_env_path(Some("C:/hdr/big.hdr".to_string()));
    remember_normal();
    normal_lighting_chosen();
    assert_eq!(pending_env_path(), None);

    // And the decode's own answer clears it.
    set_pending_env_path(Some("C:/hdr/big.hdr".to_string()));
    set_pending_env_path(None);
    assert_eq!(pending_env_path(), None);
}

// --- the cutter's own map beats a decode on its way ---

#[test]
fn a_map_the_cutter_loads_while_a_decode_is_on_its_way_is_the_one_that_is_stored() {
    let opening = begin_generation();
    let decode = begin_env_decode("C:/hdr/big.hdr");
    assert_eq!(pending_env_path().as_deref(), Some("C:/hdr/big.hdr"));
    assert!(env_decode_is_current(decode));
    // Before the cutter acts, the decode's path wins over the map the dialog still shows.
    assert_eq!(
        env_path_to_store(pending_env_path().as_deref(), true, "C:/hdr/small.hdr"),
        Some("C:/hdr/big.hdr".to_string())
    );

    // The cutter loads small.hdr through the dialog: the newest choice.
    env_map_chosen();
    assert!(
        !env_decode_is_current(decode),
        "big.hdr must not land over the map the cutter just loaded"
    );
    assert_eq!(pending_env_path(), None);
    assert_eq!(
        env_path_to_store(pending_env_path().as_deref(), true, "C:/hdr/small.hdr"),
        Some("C:/hdr/small.hdr".to_string()),
        "the stored map is the one the dialog shows"
    );
    assert!(
        generation_is_current(opening),
        "the rest of the design's lighting, and a lookup still waiting, are not voided"
    );
}

#[test]
fn clearing_the_map_by_hand_voids_a_decode_on_its_way_too() {
    let decode = begin_env_decode("C:/hdr/big.hdr");
    env_map_chosen();
    assert!(!env_decode_is_current(decode));
    assert_eq!(pending_env_path(), None);
}

// --- the cutter's own map beats the saved lighting's, before its decode starts (F1-17) ---

#[test]
fn a_map_chosen_by_hand_while_the_lookup_waits_is_not_replaced_by_the_saved_map() {
    // The design opens and the library is busy: the lookup moves to a thread.
    let opening = begin_generation();
    assert!(
        !map_chosen_since_open(),
        "nothing chosen yet: the saved lighting's map may be used"
    );

    // The cutter loads small.hdr through the settings dialog meanwhile.
    env_map_chosen();
    assert!(map_chosen_since_open());
    assert!(
        generation_is_current(opening),
        "the lookup itself still counts: the rest of the saved lighting is applied"
    );

    // The lookup lands, naming big.hdr: no decode starts, and nothing clears the cutter's map.
    for wanted in [Some("C:/hdr/big.hdr"), Some("C:/hdr/small.hdr"), None] {
        assert_eq!(
            env_source_for_opening(map_chosen_since_open(), wanted, Some("C:/hdr/small.hdr")),
            EnvSource::ChosenByHand,
            "saved map {wanted:?}"
        );
    }
    // Nor is a decode of the saved map ever started, so there is nothing to land over it.
    assert_eq!(pending_env_path(), None);
}

#[test]
fn clearing_the_map_by_hand_while_the_lookup_waits_is_kept_too() {
    begin_generation();
    env_map_chosen();
    assert_eq!(
        env_source_for_opening(map_chosen_since_open(), Some("C:/hdr/big.hdr"), None),
        EnvSource::ChosenByHand,
        "the cutter cleared the map; the saved one does not come back"
    );
}

#[test]
fn the_saved_map_is_used_when_the_cutter_chose_none_since_the_opening_began() {
    begin_generation();
    assert_eq!(
        env_source_for_opening(
            map_chosen_since_open(),
            Some("C:/hdr/big.hdr"),
            Some("C:/hdr/small.hdr")
        ),
        EnvSource::Decode("C:/hdr/big.hdr".to_string())
    );
    assert_eq!(
        env_source_for_opening(map_chosen_since_open(), None, Some("C:/hdr/small.hdr")),
        EnvSource::Studio
    );
}

#[test]
fn a_map_chosen_before_the_opening_began_is_not_a_choice_since() {
    env_map_chosen();
    begin_generation();
    assert!(
        !map_chosen_since_open(),
        "the earlier choice is what the lighting is applied over"
    );
}

#[test]
fn the_openings_own_decode_is_not_a_choice_of_the_cutters() {
    begin_generation();
    begin_env_decode("C:/hdr/big.hdr");
    assert!(!map_chosen_since_open());
    env_map_chosen();
    assert!(map_chosen_since_open());
}

#[test]
fn a_newer_decode_outlives_the_answer_of_an_older_one() {
    let first = begin_env_decode("C:/hdr/first.hdr");
    let second = begin_env_decode("C:/hdr/second.hdr");
    assert!(!env_decode_is_current(first), "an older decode is stale");
    assert!(env_decode_is_current(second));
    assert_eq!(
        pending_env_path().as_deref(),
        Some("C:/hdr/second.hdr"),
        "the older answer leaves the newer decode's path alone"
    );
    // A map chosen after the newer decode began voids that one as well.
    env_map_chosen();
    assert!(!env_decode_is_current(second));
}

// --- reading the library without waiting ---

#[test]
fn a_busy_library_is_not_waited_for_and_a_free_one_is_read() {
    let db = memory_db();
    store_values(&db, DESIGN, &tent(), 1).expect("stores");
    {
        let _held = db.lock().expect("lock");
        assert_eq!(
            try_read_lookup(&db, DESIGN),
            None,
            "busy: the UI thread asks again later instead of waiting"
        );
    }
    assert_eq!(try_read_lookup(&db, DESIGN), Some(Lookup::Row(tent())));
    assert_eq!(
        try_read_lookup(&db, "1c9e6a4e-5f1d-4c7a-9a52-1e3f7d8c2b10"),
        Some(Lookup::NoRow)
    );
    assert!(matches!(
        try_read_lookup(&db, "not-a-uuid"),
        Some(Lookup::Unreadable(_))
    ));
}

// --- the settings file never takes a design's lighting ---

#[test]
fn values_written_into_the_settings_read_back_the_same() {
    let mut app = AppSettings::default();
    tent().write_into(&mut app);
    assert_eq!(LightingValues::from_app_settings(&app), tent());
    // No map is stored as an empty path, and reads back as none.
    LightingValues {
        env_map_path: None,
        ..tent()
    }
    .write_into(&mut app);
    assert_eq!(app.env_map_path, "");
    assert_eq!(LightingValues::from_app_settings(&app).env_map_path, None);
}

/// The remaining gap of `landed_w2_light.md`: nudge the exposure while a design's
/// lighting shows, close the app (a flush writes the file), and the settings file must
/// still hold the normal lighting.
#[test]
fn light_control_changes_while_a_designs_lighting_shows_never_reach_the_settings_file() {
    let dir = std::env::temp_dir().join(format!(
        "indicatrix-cut-design-lighting-{}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir).expect("a temp directory");
    let path = dir.join("settings.toml");
    let persister = SettingsPersister::spawn(path.clone(), SettingsFile::default());
    let normal = LightingValues::from_app_settings(&persister.snapshot().settings);

    // The design's lighting shows: the normal one is pinned over the file's lighting
    // fields, and the controls (still wired to the settings) write what they show.
    persister.set_disk_override(Some(normal.disk_pin()));
    persister.update(|file| tent().write_into(&mut file.settings));
    persister.update(|file| file.settings.exposure = 2.5);
    assert_eq!(
        persister.snapshot().settings.exposure,
        2.5,
        "the running app still has what the controls set"
    );

    // The app closes.
    persister.flush();
    let on_disk =
        LightingValues::from_app_settings(&crate::settings::store::load_or_default(&path).settings);
    assert_eq!(on_disk, normal, "the next start finds the normal lighting");

    // The lighting ends (another design opens): the in-memory settings are the normal
    // lighting again and the pin is let go; the file follows them.
    persister.update(|file| normal.write_into(&mut file.settings));
    persister.set_disk_override(None);
    persister.flush();
    let after =
        LightingValues::from_app_settings(&crate::settings::store::load_or_default(&path).settings);
    assert_eq!(after, normal);
    let _ = std::fs::remove_dir_all(&dir);
}