indicatrix-worker 0.2.2

Library server for a indicatrix-backed design catalogue (`serve`, default build): serves indicatrix-net's read-only design-library protocol over mutual TLS. The optional `worker` feature adds render capacity on top -- accepting RenderRequests and replying with traced radiance -- and `render` a one-shot scene-to-PNG CLI.
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
use crate::serve::{
    connection::{BUILD_MISMATCH_CODE, VALIDATION_FAILED_CODE},
    handle_connection,
};
use indicatrix::{
    geometry::cuts::StandardGemCuts,
    optics::{materials::GemMaterial, raytracer::LightingPreset},
};
use indicatrix_net::{
    SceneState, handshake,
    messages::{
        Backend, Cancel, ClientMessage, Done, ErrorMsg, Hello, RenderRequest, StreamEvent, Welcome,
    },
};
use indicatrix_vault::db::sqlite::Database;
use std::{
    io::{Cursor, Read, Write},
    net::TcpListener,
    thread,
};

use crate::{render_core, stream_emit::TimeoutRead};

/// A fresh, empty, throwaway temp database, just to satisfy `handle_connection`'s
/// signature -- none of these tests exercise the library protocol itself. Tests never
/// touch `facet_diagrams.sqlite`, only their own throwaway temp files.
fn test_db() -> Database {
    let path = std::env::temp_dir().join(format!(
        "indicatrix-worker-serve-test-{}-{}.sqlite",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    Database::new(Some(path.to_str().unwrap())).unwrap()
}

fn tiny_scene() -> SceneState {
    SceneState {
        width: 4,
        height: 4,
        yaw: 0.4,
        pitch: 0.3,
        distance: 3.0,
        light_yaw: 0.85,
        light_pitch: 0.95,
        exposure: 1.0,
        max_bounces: 4,
        lighting_preset: LightingPreset::Daylight,
        material: GemMaterial::diamond(),
        planes: StandardGemCuts::standard_round_brilliant(),
        girdle_frosted: false,
    }
}

/// [`tiny_scene`] with dispersion removed: a Cauchy model with `b = c = 0` at
/// diamond's `n_d` (2.417), so every spectral channel refracts along the same
/// direction and no chromatic-termination decision can sit on its
/// `DIRECTION_MATCH_COS_TOL` knife edge -- see
/// `hybrid_gpu_and_cpu_split_sums_to_the_same_result_as_tracing_the_range_directly`.
#[cfg(feature = "gpu")]
fn tiny_scene_without_dispersion() -> SceneState {
    let mut scene = tiny_scene();
    scene.material.dispersion = indicatrix::optics::dispersion::DispersionModel::Cauchy {
        a: 2.417,
        b: 0.0,
        c: 0.0,
    };
    scene
}

/// A `Read + Write` over two independent in-memory buffers, standing in for one end
/// of a duplex connection: writes go to `out`, reads come from `in_`. Lets
/// `handle_connection` be driven with hand-assembled request bytes, no networking.
///
/// `TimeoutRead`-aware, to exercise `stream_emit::poll_for_client_message`'s polling
/// loop: with no timeout set (the default), exhausting `in_` reports `Ok(0)` (EOF).
/// With a timeout set (as `run_stream`'s emitter does while streaming), exhausting
/// `in_` reports `WouldBlock` instead -- "nothing new yet", letting a test simulate
/// "connection still open, no `CANCEL` sent" by simply not writing one.
struct DuplexHalf {
    in_: Cursor<Vec<u8>>,
    out: Vec<u8>,
    timeout_active: bool,
}

impl DuplexHalf {
    const fn new(input: Vec<u8>) -> Self {
        Self {
            in_: Cursor::new(input),
            out: Vec::new(),
            timeout_active: false,
        }
    }
}

impl Read for DuplexHalf {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let n = self.in_.read(buf)?;
        if n == 0 && self.timeout_active {
            return Err(std::io::Error::new(
                std::io::ErrorKind::WouldBlock,
                "DuplexHalf: no more scripted input (yet)",
            ));
        }
        Ok(n)
    }
}

impl Write for DuplexHalf {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.out.write(buf)
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

impl TimeoutRead for DuplexHalf {
    fn set_read_timeout(&mut self, duration: Option<std::time::Duration>) -> std::io::Result<()> {
        self.timeout_active = duration.is_some();
        Ok(())
    }
}

/// A no-op: `DuplexHalf::write` goes to an unbounded `Vec` and can never actually
/// block. See [`BackpressureDuplex`] below for a double that genuinely models a peer
/// who stops draining.
impl crate::stream_emit::TimeoutWrite for DuplexHalf {
    fn set_write_timeout(&mut self, _duration: Option<std::time::Duration>) -> std::io::Result<()> {
        Ok(())
    }
}

/// A `Read + Write` double modeling a peer that stops draining: `write()` succeeds
/// while `out` stays within `capacity`, then returns [`std::io::ErrorKind::TimedOut`]
/// as if a write-timeout deadline had already elapsed, so a test doesn't need a real
/// multi-second sleep. Only enforced while a write timeout is set
/// (`write_timeout_active`): the `WELCOME` handshake write always succeeds regardless
/// of `capacity`, like a real unbounded-by-default socket.
///
/// [`DuplexHalf`]'s unbounded-`Vec` `Write` side can never model this backpressure, so
/// it can't reproduce the real bug (the emitter blocking inside an unbounded
/// `write()`) that `repro_slow_reader_blocks_the_emitter_and_delays_cancel` shows
/// against real sockets. `BackpressureDuplex` closes that gap deterministically.
struct BackpressureDuplex {
    in_: Cursor<Vec<u8>>,
    out: Vec<u8>,
    capacity: usize,
    read_timeout_active: bool,
    write_timeout_active: bool,
    /// The `io::ErrorKind` a write past `capacity` reports once `write_timeout_active`.
    /// Defaults to `TimedOut` (a raw socket's kind) via [`Self::new`]; see
    /// [`Self::new_with_kind`] to simulate rustls' `WriteZero` instead (see
    /// `crate::stream_emit::is_stream_timeout`).
    timeout_kind: std::io::ErrorKind,
}

impl BackpressureDuplex {
    const fn new(input: Vec<u8>, capacity: usize) -> Self {
        Self::new_with_kind(input, capacity, std::io::ErrorKind::TimedOut)
    }

    /// Like [`Self::new`], but a write past `capacity` reports `kind` instead of always
    /// `TimedOut` -- lets a test simulate rustls' `WriteZero`-on-timeout behavior too.
    const fn new_with_kind(input: Vec<u8>, capacity: usize, kind: std::io::ErrorKind) -> Self {
        Self {
            in_: Cursor::new(input),
            out: Vec::new(),
            capacity,
            read_timeout_active: false,
            write_timeout_active: false,
            timeout_kind: kind,
        }
    }
}

impl Read for BackpressureDuplex {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let n = self.in_.read(buf)?;
        if n == 0 && self.read_timeout_active {
            return Err(std::io::Error::new(
                std::io::ErrorKind::WouldBlock,
                "BackpressureDuplex: no more scripted input (yet)",
            ));
        }
        Ok(n)
    }
}

impl Write for BackpressureDuplex {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        if self.write_timeout_active && self.out.len() + buf.len() > self.capacity {
            return Err(std::io::Error::new(
                self.timeout_kind,
                "BackpressureDuplex: peer isn't draining -- write timed out",
            ));
        }
        self.out.write(buf)
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

impl TimeoutRead for BackpressureDuplex {
    fn set_read_timeout(&mut self, duration: Option<std::time::Duration>) -> std::io::Result<()> {
        self.read_timeout_active = duration.is_some();
        Ok(())
    }
}

impl crate::stream_emit::TimeoutWrite for BackpressureDuplex {
    fn set_write_timeout(&mut self, duration: Option<std::time::Duration>) -> std::io::Result<()> {
        self.write_timeout_active = duration.is_some();
        Ok(())
    }
}

const fn live_progressive(cadence_ms: u32) -> indicatrix_net::messages::StreamConfig {
    indicatrix_net::messages::StreamConfig {
        transfer_mode: indicatrix_net::messages::TransferMode::LiveProgressive,
        cadence_ms,
        preview: None,
    }
}

const fn final_only(cadence_ms: u32) -> indicatrix_net::messages::StreamConfig {
    indicatrix_net::messages::StreamConfig {
        transfer_mode: indicatrix_net::messages::TransferMode::FinalOnly,
        cadence_ms,
        preview: None,
    }
}

/// Reads [`indicatrix_net::messages::StreamEvent`]s from `reader` (pairing each with
/// its raw payload, for `Frame`/`Preview`) until -- and including -- a `Done` or
/// `Error`, the two terminal variants for one `RENDER` reply.
fn read_stream_until_done<R: Read>(
    reader: &mut R,
) -> Vec<(indicatrix_net::messages::StreamEvent, Option<Vec<u8>>)> {
    let mut events = Vec::new();
    loop {
        let (event, payload) = indicatrix_net::messages::read_stream_event(reader).unwrap();
        let terminal = matches!(
            event,
            indicatrix_net::messages::StreamEvent::Done(_)
                | indicatrix_net::messages::StreamEvent::Error(_)
        );
        events.push((event, payload));
        if terminal {
            break;
        }
    }
    events
}

#[test]
fn handle_connection_refuses_a_mismatched_build_hash() {
    let mut input = Vec::new();
    let bad_hello = Hello {
        protocol_version: indicatrix_net::messages::PROTOCOL_VERSION,
        build_hash: [0xAB; 8],
    };
    indicatrix_net::messages::write_message(&mut input, &bad_hello).unwrap();

    let mut duplex = DuplexHalf::new(input);
    handle_connection(&mut duplex, 1, &test_db()).unwrap();

    let mut out_cursor = Cursor::new(duplex.out);
    let err: ErrorMsg = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();
    assert_eq!(err.code, BUILD_MISMATCH_CODE);
    assert!(err.message.contains("refusing to pair"), "{}", err.message);
}

#[test]
fn handle_connection_accepts_a_matching_build_hash_and_sends_welcome() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();
    // No RenderRequest follows -- the reader hits EOF, which handle_connection
    // treats as the peer having closed the connection after the handshake.

    let mut duplex = DuplexHalf::new(input);
    handle_connection(&mut duplex, 1, &test_db()).unwrap();

    let mut out_cursor = Cursor::new(duplex.out);
    let welcome: Welcome = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();
    assert_eq!(welcome.build_hash, handshake::local_hello().build_hash);
    assert!(matches!(
        welcome.render.as_ref().unwrap().backend,
        Backend::Cpu { .. }
    ));
}

#[test]
fn handle_connection_rejects_a_request_that_fails_validation_but_keeps_the_connection_open() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();

    let mut bad_scene = tiny_scene();
    bad_scene.planes[0].normal = [f32::NAN, 0.0, 0.0];
    let bad_request = RenderRequest {
        request_id: 1,
        scene: bad_scene,
        first_sample: 0,
        samples: 4,
        stream: final_only(0),
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(bad_request)),
    )
    .unwrap();

    // A second, well-formed request right after the bad one -- proves the
    // connection is still alive and serving requests after a validation failure.
    let good_request = RenderRequest {
        request_id: 2,
        scene: tiny_scene(),
        first_sample: 0,
        samples: 2,
        stream: final_only(0),
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(good_request)),
    )
    .unwrap();

    let mut duplex = DuplexHalf::new(input);
    handle_connection(&mut duplex, 1, &test_db()).unwrap();

    let mut out_cursor = Cursor::new(duplex.out);
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();

    // The bad request's reply: a single StreamEvent::Error, nothing else.
    let bad_events = read_stream_until_done(&mut out_cursor);
    assert_eq!(bad_events.len(), 1);
    let StreamEvent::Error(err) = &bad_events[0].0 else {
        panic!("expected StreamEvent::Error, got {:?}", bad_events[0].0);
    };
    assert_eq!(err.code, VALIDATION_FAILED_CODE);

    // The good request's reply: FinalOnly, so exactly one Frame (with correct
    // request_id and sample count) followed by Done { cancelled: false }.
    let good_events = read_stream_until_done(&mut out_cursor);
    let frames: Vec<_> = good_events
        .iter()
        .filter_map(|(e, p)| match e {
            StreamEvent::Frame(h) => Some((h, p.as_ref().unwrap())),
            _ => None,
        })
        .collect();
    assert_eq!(frames.len(), 1);
    let (header, payload) = frames[0];
    assert_eq!(header.request_id, 2);
    assert_eq!(header.samples, 2);
    assert_eq!(
        payload.len(),
        4 * 4 * indicatrix_net::radiance::BYTES_PER_PIXEL
    );
    assert!(matches!(
        good_events.last().unwrap().0,
        StreamEvent::Done(Done {
            cancelled: false,
            ..
        })
    ));
}

/// A real `RenderRequest` over a real loopback `TcpStream`, driven through
/// `handle_connection` directly (not `run`'s accept loop): request goes in, a
/// correctly-sized summed radiance buffer comes back.
#[test]
fn serve_round_trip_over_a_loopback_socket_returns_a_correctly_sized_buffer() {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();

    let server = thread::spawn(move || {
        let (stream, _) = listener.accept().unwrap();
        handle_connection(stream, 2, &test_db()).unwrap();
    });

    let mut client = std::net::TcpStream::connect(addr).unwrap();
    indicatrix_net::messages::write_message(&mut client, &handshake::local_hello()).unwrap();
    let welcome: Welcome = indicatrix_net::messages::read_message(&mut client).unwrap();
    assert!(matches!(
        welcome.render.as_ref().unwrap().backend,
        Backend::Cpu { .. }
    ));

    let scene = tiny_scene();
    let request = RenderRequest {
        request_id: 5,
        scene,
        first_sample: 10,
        samples: 3,
        stream: final_only(0),
    };
    indicatrix_net::messages::write_message(
        &mut client,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();

    let events = read_stream_until_done(&mut client);
    let frames: Vec<_> = events
        .iter()
        .filter_map(|(e, p)| match e {
            StreamEvent::Frame(h) => Some((h, p.as_ref().unwrap())),
            _ => None,
        })
        .collect();
    assert_eq!(frames.len(), 1);
    let (header, payload) = frames[0];
    assert_eq!(header.request_id, 5);
    assert_eq!(header.first_sample, 10);
    assert_eq!(header.samples, 3);
    assert_eq!(
        payload.len(),
        4 * 4 * indicatrix_net::radiance::BYTES_PER_PIXEL
    );

    drop(client); // close the connection so handle_connection's read loop sees EOF and returns
    server.join().unwrap();
}

#[test]
fn serve_round_trip_result_matches_tracing_the_same_range_directly() {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();
    let scene = tiny_scene();
    let scene_for_server = scene.clone();

    let server = thread::spawn(move || {
        let (stream, _) = listener.accept().unwrap();
        handle_connection(stream, 2, &test_db()).unwrap();
    });

    let mut client = std::net::TcpStream::connect(addr).unwrap();
    indicatrix_net::messages::write_message(&mut client, &handshake::local_hello()).unwrap();
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut client).unwrap();

    let request = RenderRequest {
        request_id: 6,
        scene: scene.clone(),
        first_sample: 0,
        samples: 4,
        stream: final_only(0),
    };
    indicatrix_net::messages::write_message(
        &mut client,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();
    let events = read_stream_until_done(&mut client);
    let (_header, payload) = events
        .iter()
        .find_map(|(e, p)| match e {
            StreamEvent::Frame(h) => Some((h, p.as_ref().unwrap())),
            _ => None,
        })
        .unwrap();
    drop(client);
    server.join().unwrap();

    let over_the_wire =
        indicatrix_net::radiance::decode(payload, scene.width, scene.height).unwrap();
    let direct = render_core::trace_samples(&scene_for_server, 0, 4, 2);
    // Relative tolerance, not bit-exact: the server sub-batches internally
    // (`stream_emit::run_tracer`) and sums sub-batches, rather than tracing the whole
    // range in one call like `direct` does, and float addition isn't associative.
    for (a, b) in over_the_wire.iter().zip(&direct) {
        let diff = (*a - *b).abs();
        let scale = a.abs().max(b.abs()).max(glam::Vec3::splat(1e-6));
        assert!(
            (diff / scale).max_element() < 1e-3,
            "over_the_wire={a:?} direct={b:?}"
        );
    }
}

/// GPU counterpart to `serve_round_trip_over_a_loopback_socket_returns_a_correctly_sized_buffer`:
/// acquires a real [`indicatrix::renderer::gpu_backend::GpuBackend`] and drives
/// `handle_connection_with_gpu` over a real loopback socket. With a usable adapter this
/// proves `WELCOME` reports `Backend::Gpu` and the request still comes back correctly
/// sized. With no usable adapter, `GpuBackend::acquire` declines and this degrades to
/// re-checking CPU behavior rather than failing.
#[cfg(feature = "gpu")]
#[test]
fn serve_round_trip_over_gpu_reports_backend_gpu_and_a_correctly_sized_buffer() {
    use crate::{cli::ComputeMode, serve::handle_connection_with_gpu};
    use indicatrix::renderer::gpu_backend::GpuBackend;
    use std::{net::TcpStream, sync::Arc};

    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();
    let scene = tiny_scene(); // diamond -- isotropic, so GpuBackend accepts it.

    let server = thread::spawn(move || {
        let (stream, _) = listener.accept().unwrap();
        let gpu = Arc::new(GpuBackend::acquire());
        handle_connection_with_gpu(stream, 2, &gpu, &test_db(), ComputeMode::default()).unwrap();
    });

    let mut client = TcpStream::connect(addr).unwrap();
    indicatrix_net::messages::write_message(&mut client, &handshake::local_hello()).unwrap();
    let welcome: Welcome = indicatrix_net::messages::read_message(&mut client).unwrap();

    let request = RenderRequest {
        request_id: 7,
        scene: scene.clone(),
        first_sample: 0,
        samples: 4,
        stream: final_only(0),
    };
    indicatrix_net::messages::write_message(
        &mut client,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();
    let events = read_stream_until_done(&mut client);
    let (header, payload) = events
        .iter()
        .find_map(|(e, p)| match e {
            StreamEvent::Frame(h) => Some((h, p.as_ref().unwrap())),
            _ => None,
        })
        .unwrap();
    assert_eq!(header.samples, 4);
    assert_eq!(
        payload.len(),
        scene.width as usize * scene.height as usize * indicatrix_net::radiance::BYTES_PER_PIXEL
    );

    drop(client);
    server.join().unwrap();

    // Only asserted with a usable adapter -- a machine with none legitimately reports
    // Backend::Cpu, not a test failure.
    if let Some(Backend::Gpu { adapter }) = welcome.render.map(|r| r.backend) {
        assert!(!adapter.is_empty(), "adapter label must be non-empty");
    }
}

/// Exercises `stream_emit::tracer::run_tracer`'s hybrid CPU+GPU path over a real
/// loopback socket. `samples` here (32) is above `render_core::hybrid::HYBRID_MIN_SPP`
/// (8) -- unlike every other GPU test in this file -- so this actually triggers
/// `render_core::hybrid::calibrate` and a `hybrid_trace` split across both engines.
/// Proves the split doesn't change the per-pixel sum (same float-reordering tolerance
/// as `serve_round_trip_result_matches_tracing_the_same_range_directly`) and the total
/// sample count still matches what was requested. With no usable adapter, this
/// degrades to re-checking the plain CPU path, same as the GPU test above.
///
/// # Why this test's scene has no dispersion
///
/// The strict per-pixel tolerance only holds when CPU and GPU produce the same
/// per-sample radiance up to float-addition reordering. On a dispersive material they
/// occasionally don't: `refraction.rs`'s chromatic-termination decisions compare a dot
/// product against `DIRECTION_MATCH_COS_TOL = 1 - 1e-6`, so a cosine within a few ULPs
/// of that threshold can flip between the two engines' independently rounded paths.
/// Both outcomes are valid, unbiased estimators, but the flipped sample renormalises
/// over a different MIS family, moving pixel sums by up to several percent run to run
/// -- a property of the tolerance, not of the split-and-glue logic this test exists to
/// prove. Hence [`tiny_scene_without_dispersion`], where no decision can sit on the
/// threshold. Dispersive CPU/GPU parity is covered statistically instead, by the GPU
/// equivalence harness's Tier 3 image comparisons (`examples/gpu_equivalence_harness.rs`).
#[cfg(feature = "gpu")]
#[test]
fn hybrid_gpu_and_cpu_split_sums_to_the_same_result_as_tracing_the_range_directly() {
    use crate::{cli::ComputeMode, serve::handle_connection_with_gpu};
    use indicatrix::renderer::gpu_backend::GpuBackend;
    use std::{net::TcpStream, sync::Arc};

    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();
    // Diamond's index without its dispersion -- isotropic, so GpuBackend accepts it,
    // and knife-edge-free, see this test's own doc comment.
    let scene = tiny_scene_without_dispersion();
    let scene_for_direct = scene.clone();

    let server = thread::spawn(move || {
        let (stream, _) = listener.accept().unwrap();
        let gpu = Arc::new(GpuBackend::acquire());
        // `Hybrid` explicitly: exercising `run_tracer`'s calibrate/hybrid_trace path
        // only runs for this mode.
        handle_connection_with_gpu(stream, 2, &gpu, &test_db(), ComputeMode::Hybrid).unwrap();
    });

    let mut client = TcpStream::connect(addr).unwrap();
    indicatrix_net::messages::write_message(&mut client, &handshake::local_hello()).unwrap();
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut client).unwrap();

    let request = RenderRequest {
        request_id: 8,
        scene: scene.clone(),
        first_sample: 0,
        samples: 32,
        stream: final_only(0),
    };
    indicatrix_net::messages::write_message(
        &mut client,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();
    let events = read_stream_until_done(&mut client);
    let (header, payload) = events
        .iter()
        .find_map(|(e, p)| match e {
            StreamEvent::Frame(h) => Some((h, p.as_ref().unwrap())),
            _ => None,
        })
        .unwrap();
    assert_eq!(header.samples, 32);

    drop(client);
    server.join().unwrap();

    let over_the_wire =
        indicatrix_net::radiance::decode(payload, scene.width, scene.height).unwrap();
    let direct = render_core::trace_samples(&scene_for_direct, 0, 32, 2);

    // Same tolerance as `serve_round_trip_result_matches_tracing_the_same_range_directly`;
    // holds here because the scene is non-dispersive (see this test's doc comment).
    for (a, b) in over_the_wire.iter().zip(&direct) {
        let diff = (*a - *b).abs();
        let scale = a.abs().max(b.abs()).max(glam::Vec3::splat(1e-6));
        let rel = (diff / scale).max_element();
        assert!(rel < 1e-3, "over_the_wire={a:?} direct={b:?} rel={rel}");
    }
}

// ---- Progressive streaming ---------------------------------------------------

/// A scene with enough per-sample work that a several-dozen-sample request takes long
/// enough for `run_stream`'s emitter to get multiple chances to poll/emit before the
/// tracer finishes -- unlike `tiny_scene`, which can finish before the emitter's first
/// loop iteration runs. Used by tests that need to observe more than one emission
/// deterministically.
fn heavier_scene() -> SceneState {
    SceneState {
        width: 24,
        height: 24,
        yaw: 0.4,
        pitch: 0.3,
        distance: 3.0,
        light_yaw: 0.85,
        light_pitch: 0.95,
        exposure: 1.0,
        max_bounces: 6,
        lighting_preset: LightingPreset::Daylight,
        material: GemMaterial::diamond(),
        planes: StandardGemCuts::standard_round_brilliant(),
        girdle_frosted: false,
    }
}

/// The `request_id` carried by one `StreamEvent` -- every variant but `Error`
/// carries one; see `indicatrix_net::messages`' docs on why that's what makes a stale
/// reply mechanically identifiable.
fn event_request_id(event: &StreamEvent) -> u32 {
    match event {
        StreamEvent::Frame(h) => h.request_id,
        StreamEvent::Preview(h) => h.request_id,
        StreamEvent::Progress(p) => p.request_id,
        StreamEvent::Done(d) => d.request_id,
        StreamEvent::Error(e) => panic!("unexpected StreamEvent::Error: {e:?}"),
    }
}

#[test]
fn delta_frames_tile_the_requested_sample_range_with_no_gaps_or_overlaps() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();
    let request = RenderRequest {
        request_id: 11,
        scene: heavier_scene(),
        first_sample: 100,
        samples: 64,
        stream: live_progressive(0), // due on every emitter tick
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();

    let mut duplex = DuplexHalf::new(input);
    handle_connection(&mut duplex, 2, &test_db()).unwrap();

    let mut out_cursor = Cursor::new(duplex.out);
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();
    let events = read_stream_until_done(&mut out_cursor);

    let mut ranges: Vec<(u32, u32)> = events
        .iter()
        .filter_map(|(e, _)| match e {
            StreamEvent::Frame(h) => Some((h.first_sample, h.samples)),
            _ => None,
        })
        .collect();
    assert!(!ranges.is_empty(), "expected at least one FRAME delta");
    ranges.sort_by_key(|r| r.0);

    let mut cursor = 100u32;
    for (first, samples) in &ranges {
        assert_eq!(
            *first, cursor,
            "gap or overlap: expected next delta to start at {cursor}, got {first}"
        );
        cursor += samples;
    }
    assert_eq!(
        cursor,
        100 + 64,
        "deltas must tile the whole requested range exactly"
    );

    assert!(matches!(
        events.last().unwrap().0,
        StreamEvent::Done(Done {
            cancelled: false,
            ..
        })
    ));
}

#[test]
fn final_only_transfer_mode_still_emits_progress() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();
    let request = RenderRequest {
        request_id: 12,
        scene: heavier_scene(),
        first_sample: 0,
        samples: 64,
        stream: final_only(0),
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();

    let mut duplex = DuplexHalf::new(input);
    handle_connection(&mut duplex, 2, &test_db()).unwrap();

    let mut out_cursor = Cursor::new(duplex.out);
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();
    let events = read_stream_until_done(&mut out_cursor);

    let frame_count = events
        .iter()
        .filter(|(e, _)| matches!(e, StreamEvent::Frame(_)))
        .count();
    assert_eq!(
        frame_count, 1,
        "FinalOnly must send exactly one FRAME, covering the whole request"
    );

    let progress_count = events
        .iter()
        .filter(|(e, _)| matches!(e, StreamEvent::Progress(_)))
        .count();
    assert!(
        progress_count >= 1,
        "FinalOnly must still emit PROGRESS on the cadence even though FRAME only arrives once"
    );

    assert!(matches!(
        events.last().unwrap().0,
        StreamEvent::Done(Done {
            cancelled: false,
            ..
        })
    ));
}

#[test]
fn cancel_mid_stream_produces_done_cancelled_and_no_further_payload() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();
    let request = RenderRequest {
        request_id: 13,
        scene: heavier_scene(),
        first_sample: 0,
        samples: 64,
        stream: live_progressive(0),
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::Cancel(Cancel { request_id: 13 }),
    )
    .unwrap();

    let mut duplex = DuplexHalf::new(input);
    handle_connection(&mut duplex, 2, &test_db()).unwrap();

    let mut out_cursor = Cursor::new(duplex.out);
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();
    let events = read_stream_until_done(&mut out_cursor);

    for (event, _) in &events {
        assert_eq!(event_request_id(event), 13);
    }
    assert!(matches!(
        events.last().unwrap().0,
        StreamEvent::Done(Done {
            cancelled: true,
            ..
        })
    ));

    // No further payload: handle_connection's outer loop went straight back to
    // reading the next RenderRequest and hit EOF, writing nothing more.
    assert_eq!(
        out_cursor.position(),
        out_cursor.get_ref().len() as u64,
        "no bytes may follow DONE{{cancelled: true, ..}}"
    );
}

/// Why this passes while the real bug (`repro_slow_reader_blocks_the_emitter_and_delays_cancel`,
/// further down) doesn't reproduce on `DuplexHalf`: `CANCEL` is already sitting in the
/// scripted input at time zero, and `DuplexHalf::write`'s unbounded `Vec` can never
/// block. The real bug needs `CANCEL` arriving *while* the emitter is stuck inside a
/// blocked `write()` -- `BackpressureDuplex` supplies that.
#[test]
fn a_peer_that_never_drains_ends_the_connection_rather_than_hanging_forever() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();
    let request = RenderRequest {
        request_id: 21,
        scene: heavier_scene(),
        first_sample: 0,
        samples: 64,
        stream: live_progressive(0),
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();

    // Tiny on purpose: the WELCOME handshake write always gets through regardless of
    // capacity (see BackpressureDuplex's doc comment), so this only needs to be
    // smaller than the first FRAME/PROGRESS write once streaming starts.
    let mut duplex = BackpressureDuplex::new(input, 8);
    let result = handle_connection(&mut duplex, 2, &test_db());

    // The bound this closes: this used to hang forever; now it returns an error
    // within one WRITE_TIMEOUT.
    let err = result.expect_err(
        "a peer that never drains must end the connection with an error, not hang forever",
    );
    assert!(
        matches!(
            err,
            indicatrix_net::messages::NetError::Framing(indicatrix_net::framing::FramingError::Io(ref e))
                if e.kind() == std::io::ErrorKind::TimedOut
        ),
        "expected the write-timeout error to surface as-is, got {err:?}"
    );
}

/// The same bound as the test above, but against a peer that reports `WriteZero`
/// instead of `TimedOut` -- what a real `rustls` `StreamOwned` reports for an
/// underlying write timeout (see `crate::stream_emit::is_stream_timeout`). Nothing in
/// `run_stream`'s write path classifies by kind before propagating, so this must
/// behave identically: an error, not a hang, with `WriteZero` preserved as-is.
#[test]
fn a_peer_that_never_drains_ends_the_connection_rather_than_hanging_forever_write_zero() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();
    let request = RenderRequest {
        request_id: 23,
        scene: heavier_scene(),
        first_sample: 0,
        samples: 64,
        stream: live_progressive(0),
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();

    let mut duplex = BackpressureDuplex::new_with_kind(input, 8, std::io::ErrorKind::WriteZero);
    let result = handle_connection(&mut duplex, 2, &test_db());

    let err = result.expect_err(
        "a peer that never drains must end the connection with an error, not hang forever, \
         regardless of which io::ErrorKind the write failure carries",
    );
    assert!(
        matches!(
            err,
            indicatrix_net::messages::NetError::Framing(indicatrix_net::framing::FramingError::Io(ref e))
                if e.kind() == std::io::ErrorKind::WriteZero
        ),
        "expected the WriteZero error to surface as-is, got {err:?}"
    );
}

/// The positive case: a peer with plenty of room to drain into never sees a write
/// timeout, and `CANCEL` still produces `DONE{cancelled: true}` and no further
/// payload -- proving `WRITE_TIMEOUT` bounds the pathological case above without
/// punishing a merely-finite, still-draining peer.
#[test]
fn cancel_still_completes_cleanly_against_a_finite_but_sufficient_peer() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();
    let request = RenderRequest {
        request_id: 22,
        scene: heavier_scene(),
        first_sample: 0,
        samples: 64,
        stream: live_progressive(0),
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::Cancel(Cancel { request_id: 22 }),
    )
    .unwrap();

    // Generous: comfortably more than everything this tiny 24x24 request could
    // possibly produce before CANCEL is observed.
    let mut duplex = BackpressureDuplex::new(input, 1_000_000);
    handle_connection(&mut duplex, 2, &test_db()).unwrap();

    let mut out_cursor = Cursor::new(duplex.out);
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();
    let events = read_stream_until_done(&mut out_cursor);

    assert!(matches!(
        events.last().unwrap().0,
        StreamEvent::Done(Done {
            cancelled: true,
            ..
        })
    ));
}

#[test]
fn stale_request_id_frames_are_identifiable() {
    // Two separate connections: a client that moved on to a new request_id (via any
    // means) can mechanically recognize a reply carrying the old one as stale. The
    // pipelined-on-one-connection scenario gets its own test below.
    fn run_one_request(request_id: u32) -> Vec<(StreamEvent, Option<Vec<u8>>)> {
        let mut input = Vec::new();
        indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();
        let request = RenderRequest {
            request_id,
            scene: tiny_scene(),
            first_sample: 0,
            samples: 4,
            stream: final_only(0),
        };
        indicatrix_net::messages::write_message(
            &mut input,
            &ClientMessage::RenderRequest(Box::new(request)),
        )
        .unwrap();

        let mut duplex = DuplexHalf::new(input);
        handle_connection(&mut duplex, 1, &test_db()).unwrap();

        let mut out_cursor = Cursor::new(duplex.out);
        let _welcome: Welcome = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();
        read_stream_until_done(&mut out_cursor)
    }

    let first_events = run_one_request(21);
    let second_events = run_one_request(22);

    // A client tracking "current epoch = 22" can identify every event belonging to
    // the first, now-stale request_id as one to drop.
    assert_ne!(first_events.len(), 0);
    for (event, _) in &first_events {
        assert_eq!(event_request_id(event), 21);
        assert_ne!(event_request_id(event), 22);
    }
    assert_ne!(second_events.len(), 0);
    for (event, _) in &second_events {
        assert_eq!(event_request_id(event), 22);
    }
}

/// The scenario `stream_emit::run_stream`'s pipelining support exists for: a client
/// writes its next `RenderRequest` right behind the first, on the same connection,
/// without reading `DONE` for the first one. Without that support this is the failure
/// mode `poll_for_client_message` warns about (spurious error, connection torn down) --
/// `handle_connection` returning `Ok(())` is itself part of what this test proves.
#[test]
fn pipelined_render_request_on_one_connection_is_queued_after_an_implicit_cancel() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();

    // Heavy enough that the emitter gets multiple chances to poll (and observe the
    // pipelined request) before the tracer finishes on its own.
    let first_request = RenderRequest {
        request_id: 41,
        scene: heavier_scene(),
        first_sample: 0,
        samples: 64,
        stream: live_progressive(0),
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(first_request)),
    )
    .unwrap();

    // Written immediately behind the first -- no DONE read in between. A different
    // (smaller) scene so a leaked buffer from the first request would show up as a
    // payload-size mismatch rather than silently passing.
    let second_request = RenderRequest {
        request_id: 42,
        scene: tiny_scene(),
        first_sample: 0,
        samples: 4,
        stream: final_only(0),
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(second_request)),
    )
    .unwrap();

    let mut duplex = DuplexHalf::new(input);
    handle_connection(&mut duplex, 2, &test_db()).unwrap();

    let mut out_cursor = Cursor::new(duplex.out);
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();

    // The first request's reply: implicitly cancelled, like an explicit CANCEL --
    // every event carries request_id 41, ending in DONE { cancelled: true }.
    let first_events = read_stream_until_done(&mut out_cursor);
    for (event, _) in &first_events {
        assert_eq!(event_request_id(event), 41);
    }
    assert!(
        matches!(
            first_events.last().unwrap().0,
            StreamEvent::Done(Done {
                cancelled: true,
                ..
            })
        ),
        "the superseded request must still get a proper DONE {{ cancelled: true }}, \
         not be silently dropped"
    );

    // The pipelined request's reply, immediately following on the same connection.
    // FinalOnly, so exactly one FRAME sized for its own scene, then DONE { cancelled: false }.
    let second_events = read_stream_until_done(&mut out_cursor);
    let frames: Vec<_> = second_events
        .iter()
        .filter_map(|(e, p)| match e {
            StreamEvent::Frame(h) => Some((h, p.as_ref().unwrap())),
            _ => None,
        })
        .collect();
    assert_eq!(frames.len(), 1);
    let (header, payload) = frames[0];
    assert_eq!(header.request_id, 42);
    assert_eq!(header.samples, 4);
    // 4x4 (tiny_scene), not 24x24 -- proves accumulation started fresh, not
    // inheriting the cancelled first request's buffers.
    assert_eq!(
        payload.len(),
        4 * 4 * indicatrix_net::radiance::BYTES_PER_PIXEL
    );
    for (event, _) in &second_events {
        assert_eq!(event_request_id(event), 42);
    }
    assert!(matches!(
        second_events.last().unwrap().0,
        StreamEvent::Done(Done {
            cancelled: false,
            ..
        })
    ));

    // Nothing beyond the two requests' worth of events was written.
    assert_eq!(
        out_cursor.position(),
        out_cursor.get_ref().len() as u64,
        "no bytes may follow the second request's DONE"
    );
}

#[test]
fn preview_frames_never_enter_the_full_resolution_accumulator_path() {
    let mut input = Vec::new();
    indicatrix_net::messages::write_message(&mut input, &handshake::local_hello()).unwrap();

    let scene = tiny_scene(); // 4x4
    let mut stream_cfg = live_progressive(0);
    stream_cfg.preview = Some(indicatrix_net::messages::PreviewConfig {
        width: 2,
        height: 2,
    });
    let request = RenderRequest {
        request_id: 31,
        scene: scene.clone(),
        first_sample: 0,
        samples: 8,
        stream: stream_cfg,
    };
    indicatrix_net::messages::write_message(
        &mut input,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();

    let mut duplex = DuplexHalf::new(input);
    handle_connection(&mut duplex, 1, &test_db()).unwrap();

    let mut out_cursor = Cursor::new(duplex.out);
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut out_cursor).unwrap();
    let events = read_stream_until_done(&mut out_cursor);

    let previews: Vec<_> = events
        .iter()
        .filter_map(|(e, p)| match e {
            StreamEvent::Preview(h) => Some((h, p.as_ref().unwrap())),
            _ => None,
        })
        .collect();
    assert!(
        !previews.is_empty(),
        "expected at least one PREVIEW with a preview configured"
    );

    for (header, payload) in &previews {
        assert_eq!(header.width, 2);
        assert_eq!(header.height, 2);

        // Decoding it against the scene's full resolution must fail with a length
        // mismatch -- a PREVIEW payload can never be silently fed into the FRAME path.
        let full_res_attempt = indicatrix_net::radiance::decode(payload, scene.width, scene.height);
        assert!(
            matches!(
                full_res_attempt,
                Err(indicatrix_net::radiance::RadianceError::LengthMismatch { .. })
            ),
            "{full_res_attempt:?}"
        );

        // It DOES decode correctly at its own declared (reduced) resolution -- it's
        // valid radiance data, just never for the full-resolution accumulator.
        assert!(indicatrix_net::radiance::decode(payload, header.width, header.height).is_ok());
    }
}

// ---- Real-socket repro: does the emitter block on a slow reader? -----------

/// Manual repro (real `TcpStream`, real OS socket buffers -- `DuplexHalf`'s unbounded
/// `Vec` can never model this): starts a real `serve` connection, sends a
/// `RenderRequest` big enough that `FRAME` payloads fill the OS send buffer if the peer
/// never reads, then never reads for a while before sending `CANCEL`. If the emitter
/// thread blocks inside `write_all` once the socket backs up -- the same thread that
/// polls for `CANCEL` -- then `CANCEL` sits unread until this test drains the read
/// side again.
///
/// `WRITE_TIMEOUT` bounds any single blocked write, including the cancelled `DONE`
/// write and the `StreamEvent::Progress` heartbeat while waiting for the tracer to
/// stop, so against a peer that truly never drains, expect `DONE{cancelled:true}` (or
/// a transport error) within roughly one to two `WRITE_TIMEOUT`s of `CANCEL` being
/// sent, never unbounded.
#[test]
#[ignore = "manual repro: real sockets + sleeps, prints timing to demonstrate the write-blocks-the-poll-loop theory"]
fn repro_slow_reader_blocks_the_emitter_and_delays_cancel() {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();

    let server = thread::spawn(move || {
        let (stream, _) = listener.accept().unwrap();
        handle_connection(stream, 0, &test_db())
    });

    let mut client = std::net::TcpStream::connect(addr).unwrap();
    indicatrix_net::messages::write_message(&mut client, &handshake::local_hello()).unwrap();
    let _welcome: Welcome = indicatrix_net::messages::read_message(&mut client).unwrap();

    // Large enough that LiveProgressive FRAME deltas add up to several MB before the
    // request finishes -- past a default OS socket-buffer size if nothing reads them.
    let scene = SceneState {
        width: 1200,
        height: 1200,
        max_bounces: 2,
        ..tiny_scene()
    };
    let request = RenderRequest {
        request_id: 99,
        scene,
        first_sample: 0,
        samples: 65_536,
        stream: indicatrix_net::messages::StreamConfig {
            transfer_mode: indicatrix_net::messages::TransferMode::LiveProgressive,
            cadence_ms: 100,
            preview: None,
        },
    };
    indicatrix_net::messages::write_message(
        &mut client,
        &ClientMessage::RenderRequest(Box::new(request)),
    )
    .unwrap();

    // Deliberately never read. Give the tracer/emitter time to produce and attempt
    // to write several cadence ticks' worth of FRAME data.
    eprintln!("not reading for 8s -- letting FRAME writes pile up unread");
    thread::sleep(std::time::Duration::from_secs(8));

    eprintln!("sending CANCEL now, still not reading FRAME payload");
    let cancel_sent_at = std::time::Instant::now();
    indicatrix_net::messages::write_message(
        &mut client,
        &ClientMessage::Cancel(Cancel { request_id: 99 }),
    )
    .unwrap();

    // Now start draining and time how long DONE{cancelled: true} takes to show up.
    // If the emitter were never blocked, this arrives promptly (~100-200ms); a long
    // delay confirms the write-blocks-the-poll-loop theory.
    client
        .set_read_timeout(Some(std::time::Duration::from_secs(30)))
        .unwrap();
    let drain_start = std::time::Instant::now();
    let mut frame_count = 0u32;
    let mut progress_count = 0u32;
    loop {
        let event: Result<(StreamEvent, Option<Vec<u8>>), _> =
            indicatrix_net::messages::read_stream_event(&mut client);
        match event {
            Ok((StreamEvent::Done(done), _)) => {
                eprintln!(
                    "DONE{{cancelled={}, samples_done={}, effective_cadence_ms={}}} arrived \
                     {:?} after CANCEL was sent ({:?} to drain from when reading resumed); \
                     saw {frame_count} FRAMEs and {progress_count} PROGRESSes total",
                    done.cancelled,
                    done.stats.samples_done,
                    done.stats.effective_cadence_ms,
                    cancel_sent_at.elapsed(),
                    drain_start.elapsed()
                );
                break;
            }
            Ok((StreamEvent::Frame(_), _)) => frame_count += 1,
            Ok((StreamEvent::Progress(_), _)) => progress_count += 1,
            Ok(_) => {}
            Err(e) => {
                eprintln!("stream ended before DONE: {e:?}");
                break;
            }
        }
    }

    drop(client);
    let _ = server.join();
}

// ---- Mutual-TLS tests -------------------------------------------------------
//
// Each test builds a real throwaway private CA and issues real certificates via
// `crate::pki`, then drives a real TLS handshake over a real loopback `TcpStream` --
// nothing is mocked at the `rustls` layer, to catch what a mock would paper over (a
// missing SAN, a wrong trust anchor, an allowlist that isn't actually consulted).
mod mtls;