asupersync-conformance 0.3.3

Conformance test suite for async runtime specifications
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
//! HTTP/2 DATA frame END_STREAM conformance testing.
//!
//! This harness exercises the asupersync HTTP/2 connection's DATA frame
//! END_STREAM handling against RFC-backed expected states. The h2 reference
//! side is not wired yet, so matching the local expected state is reported as
//! XFAIL instead of a vendor-parity pass.

use asupersync::bytes::{Bytes, BytesMut};
use asupersync::http::h2::{
    Connection, Header, HpackEncoder, Settings,
    connection::ReceivedFrame,
    frame::{DataFrame, Frame, HeadersFrame, SettingsFrame},
};
use serde::{Deserialize, Serialize};
use std::fmt;

const H2_REFERENCE_UNAVAILABLE: &str =
    "h2 reference comparison unavailable in standalone frame harness";

/// Test verdict for individual conformance cases.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DataEndStreamTestVerdict {
    Pass,
    Fail,
    ExpectedFailure, // Known divergence
    Skipped,
}

impl fmt::Display for DataEndStreamTestVerdict {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Pass => write!(f, "PASS"),
            Self::Fail => write!(f, "FAIL"),
            Self::ExpectedFailure => write!(f, "XFAIL"),
            Self::Skipped => write!(f, "SKIP"),
        }
    }
}

/// Requirement level for conformance testing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RequirementLevel {
    Must,   // RFC MUST
    Should, // RFC SHOULD
    May,    // RFC MAY
}

/// Stream state after DATA END_STREAM processing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StreamEndStreamState {
    pub stream_id: u32,
    /// Stream state: Open, HalfClosedLocal, HalfClosedRemote, Closed
    pub state: String,
    /// Whether the stream can receive more data
    pub can_recv: bool,
    /// Whether the stream can send more data
    pub can_send: bool,
    /// Error code if stream was reset
    pub error_code: Option<String>,
}

/// Connection state after DATA END_STREAM processing for comparison.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DataEndStreamConnectionState {
    /// Connection state (should remain stable)
    pub connection_state: String,
    /// Stream states indexed by stream ID
    pub stream_states: Vec<StreamEndStreamState>,
    /// Whether any errors occurred during processing
    pub has_errors: bool,
    /// List of error messages for failed operations
    pub error_messages: Vec<String>,
}

/// Serializable DATA frame for test cases.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializableDataFrame {
    pub stream_id: u32,
    pub data: Vec<u8>,
    pub end_stream: bool,
}

impl From<DataFrame> for SerializableDataFrame {
    fn from(frame: DataFrame) -> Self {
        Self {
            stream_id: frame.stream_id,
            data: frame.data.to_vec(),
            end_stream: frame.end_stream,
        }
    }
}

impl From<SerializableDataFrame> for DataFrame {
    fn from(frame: SerializableDataFrame) -> Self {
        Self {
            stream_id: frame.stream_id,
            data: Bytes::from(frame.data),
            end_stream: frame.end_stream,
        }
    }
}

/// Single conformance test case for DATA frame END_STREAM handling.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataEndStreamConformanceCase {
    pub id: String,
    pub description: String,
    pub requirement_level: RequirementLevel,
    /// Initial stream setup (HEADERS frames to establish streams)
    pub initial_streams: Vec<u32>,
    /// Sequence of DATA frames to apply
    pub data_sequence: Vec<SerializableDataFrame>,
    /// Expected final connection state
    pub expected_connection_state: DataEndStreamConnectionState,
}

/// Result of a single conformance test.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataEndStreamConformanceResult {
    pub case_id: String,
    pub verdict: DataEndStreamTestVerdict,
    pub error: Option<String>,
    /// Asupersync's final connection state
    pub asupersync_state: Option<DataEndStreamConnectionState>,
    /// H2 reference's final connection state
    pub h2_state: Option<DataEndStreamConnectionState>,
    /// Differences detected between implementations
    pub differences: Vec<String>,
}

/// Summary statistics for the conformance run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataEndStreamComplianceSummary {
    pub total_cases: usize,
    pub passed: usize,
    pub failed: usize,
    pub expected_failures: usize,
    pub skipped: usize,
    pub compliance_score: f64, // passed / total
}

/// Complete conformance test report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataEndStreamComplianceReport {
    pub test_run_id: String,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub total_cases: usize,
    pub results: Vec<DataEndStreamConformanceResult>,
    pub summary: DataEndStreamComplianceSummary,
}

impl DataEndStreamComplianceReport {
    /// Create a new report with generated ID and timestamp.
    fn new(results: Vec<DataEndStreamConformanceResult>) -> Self {
        let total_cases = results.len();
        let passed = results
            .iter()
            .filter(|r| r.verdict == DataEndStreamTestVerdict::Pass)
            .count();
        let failed = results
            .iter()
            .filter(|r| r.verdict == DataEndStreamTestVerdict::Fail)
            .count();
        let expected_failures = results
            .iter()
            .filter(|r| r.verdict == DataEndStreamTestVerdict::ExpectedFailure)
            .count();
        let skipped = results
            .iter()
            .filter(|r| r.verdict == DataEndStreamTestVerdict::Skipped)
            .count();

        let compliance_score = if total_cases > 0 {
            passed as f64 / total_cases as f64
        } else {
            1.0
        };

        let summary = DataEndStreamComplianceSummary {
            total_cases,
            passed,
            failed,
            expected_failures,
            skipped,
            compliance_score,
        };

        Self {
            test_run_id: uuid::Uuid::new_v4().to_string(),
            timestamp: chrono::Utc::now(),
            total_cases,
            results,
            summary,
        }
    }
}

/// Main conformance tester for HTTP/2 DATA frame END_STREAM handling.
#[derive(Debug)]
pub struct DataEndStreamConformanceTester {
    pub test_cases: Vec<DataEndStreamConformanceCase>,
}

impl DataEndStreamConformanceTester {
    /// Create a new tester with predefined conformance cases.
    pub fn new() -> Self {
        Self {
            test_cases: create_data_end_stream_test_cases(),
        }
    }

    /// Run all conformance tests and return a report.
    pub async fn run_all_tests(&self) -> DataEndStreamComplianceReport {
        let mut results = Vec::new();

        for case in &self.test_cases {
            let result = self.run_single_test(case).await;
            results.push(result);
        }

        DataEndStreamComplianceReport::new(results)
    }

    /// Run a single conformance test case.
    async fn run_single_test(
        &self,
        case: &DataEndStreamConformanceCase,
    ) -> DataEndStreamConformanceResult {
        // Test asupersync implementation
        let asupersync_result = self.test_asupersync_data_end_stream(case).await;

        // Test h2 reference implementation. If the external reference is not
        // wired, keep this as a live conformance check against the RFC-backed
        // expected state instead of reporting an invented comparison.
        let h2_result = self.test_h2_data_end_stream(case).await;

        // Compare results
        let (verdict, error, differences) = match (&asupersync_result, &h2_result) {
            (Ok(asupersync_state), Err(h2_err)) if h2_err == H2_REFERENCE_UNAVAILABLE => {
                let differences = self
                    .compare_connection_states(asupersync_state, &case.expected_connection_state);
                if differences.is_empty() {
                    (
                        DataEndStreamTestVerdict::ExpectedFailure,
                        Some(format!(
                            "{h2_err}; live asupersync matched the RFC-expected state but vendor parity remains unexercised"
                        )),
                        differences,
                    )
                } else {
                    (
                        DataEndStreamTestVerdict::Fail,
                        Some(format!(
                            "Live asupersync DATA END_STREAM state differed from expected RFC behavior while {h2_err}"
                        )),
                        differences,
                    )
                }
            }
            (Err(asupersync_err), Err(h2_err)) if h2_err == H2_REFERENCE_UNAVAILABLE => (
                DataEndStreamTestVerdict::Fail,
                Some(format!(
                    "Live asupersync DATA END_STREAM processing failed while {h2_err}: {asupersync_err}"
                )),
                vec![format!("asupersync_error: {asupersync_err}")],
            ),
            (Ok(asupersync_state), Ok(h2_state)) => {
                let differences = self.compare_connection_states(asupersync_state, h2_state);
                if differences.is_empty() {
                    (DataEndStreamTestVerdict::Pass, None, differences)
                } else {
                    (
                        DataEndStreamTestVerdict::Fail,
                        Some(format!(
                            "Connection state differences: {}",
                            differences.join(", ")
                        )),
                        differences,
                    )
                }
            }
            (_, Err(h2_err)) if h2_err == H2_REFERENCE_UNAVAILABLE => (
                DataEndStreamTestVerdict::Skipped,
                Some(h2_err.clone()),
                Vec::new(),
            ),
            (Err(asupersync_err), Err(h2_err)) => {
                // Both failed - check if they failed the same way
                if asupersync_err == h2_err {
                    (DataEndStreamTestVerdict::Pass, None, Vec::new())
                } else {
                    (
                        DataEndStreamTestVerdict::Fail,
                        Some(format!(
                            "Different error behaviors: asupersync={}, h2={}",
                            asupersync_err, h2_err
                        )),
                        vec![format!(
                            "Error divergence: {} vs {}",
                            asupersync_err, h2_err
                        )],
                    )
                }
            }
            (Ok(_), Err(h2_err)) => (
                DataEndStreamTestVerdict::Fail,
                Some(format!("asupersync succeeded, h2 failed: {}", h2_err)),
                vec!["Implementation success divergence".to_string()],
            ),
            (Err(asupersync_err), Ok(_)) => (
                DataEndStreamTestVerdict::Fail,
                Some(format!(
                    "asupersync failed, h2 succeeded: {}",
                    asupersync_err
                )),
                vec!["Implementation success divergence".to_string()],
            ),
        };

        DataEndStreamConformanceResult {
            case_id: case.id.clone(),
            verdict,
            error,
            asupersync_state: asupersync_result.as_ref().ok().cloned(),
            h2_state: h2_result.as_ref().ok().cloned(),
            differences,
        }
    }

    /// Test asupersync DATA END_STREAM handling.
    async fn test_asupersync_data_end_stream(
        &self,
        case: &DataEndStreamConformanceCase,
    ) -> Result<DataEndStreamConnectionState, String> {
        let settings = Settings::default();
        let mut connection = Connection::server(settings);
        let mut error_messages = Vec::new();
        accept_peer_settings(&mut connection)?;

        // Initialize streams
        for &stream_id in &case.initial_streams {
            if let Err(e) = initialize_remote_stream(&mut connection, stream_id) {
                return Err(format!("Failed to initialize stream {}: {}", stream_id, e));
            }
        }

        // Apply DATA sequence
        for serializable_frame in &case.data_sequence {
            let data_frame: DataFrame = serializable_frame.clone().into();
            match process_live_data_frame(&mut connection, &data_frame) {
                Ok(_) => {}
                Err(e) => {
                    error_messages.push(format!(
                        "DATA frame error on stream {}: {}",
                        data_frame.stream_id, e
                    ));
                }
            }
        }

        // Extract connection state
        let connection_state = extract_asupersync_data_end_stream_state(
            &connection,
            &case.initial_streams,
            error_messages,
        );
        Ok(connection_state)
    }

    /// Test h2 reference DATA END_STREAM handling.
    async fn test_h2_data_end_stream(
        &self,
        _case: &DataEndStreamConformanceCase,
    ) -> Result<DataEndStreamConnectionState, String> {
        Err(H2_REFERENCE_UNAVAILABLE.to_string())
    }

    /// Compare connection states between implementations.
    fn compare_connection_states(
        &self,
        asupersync: &DataEndStreamConnectionState,
        h2: &DataEndStreamConnectionState,
    ) -> Vec<String> {
        let mut differences = Vec::new();

        if asupersync.connection_state != h2.connection_state {
            differences.push(format!(
                "connection_state differs: asupersync={}, h2={}",
                asupersync.connection_state, h2.connection_state
            ));
        }

        if asupersync.has_errors != h2.has_errors {
            differences.push(format!(
                "has_errors differs: asupersync={}, h2={}",
                asupersync.has_errors, h2.has_errors
            ));
        }

        // Compare stream states
        if asupersync.stream_states.len() != h2.stream_states.len() {
            differences.push(format!(
                "stream_states count differs: asupersync={}, h2={}",
                asupersync.stream_states.len(),
                h2.stream_states.len()
            ));
        } else {
            for (asupersync_stream, h2_stream) in
                asupersync.stream_states.iter().zip(&h2.stream_states)
            {
                if asupersync_stream.stream_id != h2_stream.stream_id {
                    differences.push(format!(
                        "stream_id mismatch: asupersync={}, h2={}",
                        asupersync_stream.stream_id, h2_stream.stream_id
                    ));
                }
                if asupersync_stream.state != h2_stream.state {
                    differences.push(format!(
                        "stream {} state differs: asupersync={}, h2={}",
                        asupersync_stream.stream_id, asupersync_stream.state, h2_stream.state
                    ));
                }
                if asupersync_stream.can_recv != h2_stream.can_recv {
                    differences.push(format!(
                        "stream {} can_recv differs: asupersync={}, h2={}",
                        asupersync_stream.stream_id, asupersync_stream.can_recv, h2_stream.can_recv
                    ));
                }
                if asupersync_stream.can_send != h2_stream.can_send {
                    differences.push(format!(
                        "stream {} can_send differs: asupersync={}, h2={}",
                        asupersync_stream.stream_id, asupersync_stream.can_send, h2_stream.can_send
                    ));
                }
            }
        }

        // Compare error messages (order-independent)
        if asupersync.error_messages.len() != h2.error_messages.len() {
            differences.push(format!(
                "error_messages count differs: asupersync={}, h2={}",
                asupersync.error_messages.len(),
                h2.error_messages.len()
            ));
        } else {
            let mut asupersync_errors = asupersync.error_messages.clone();
            let mut h2_errors = h2.error_messages.clone();
            asupersync_errors.sort();
            h2_errors.sort();

            if asupersync_errors != h2_errors {
                differences.push("error_messages content differs".to_string());
            }
        }

        differences
    }

    /// Generate a markdown report.
    pub fn generate_markdown_report(&self, report: &DataEndStreamComplianceReport) -> String {
        let mut output = String::new();
        output.push_str("# HTTP/2 DATA Frame END_STREAM Conformance Report\n\n");

        output.push_str(&format!("**Test Run ID:** {}\n", report.test_run_id));
        output.push_str(&format!("**Timestamp:** {}\n", report.timestamp));
        output.push_str(&format!("**Total Test Cases:** {}\n\n", report.total_cases));

        output.push_str("## Summary\n\n");
        output.push_str(&format!("- **Passed:** {}\n", report.summary.passed));
        output.push_str(&format!("- **Failed:** {}\n", report.summary.failed));
        output.push_str(&format!(
            "- **Expected Failures:** {}\n",
            report.summary.expected_failures
        ));
        output.push_str(&format!("- **Skipped:** {}\n", report.summary.skipped));
        output.push_str(&format!(
            "- **Compliance Score:** {:.1}%\n\n",
            report.summary.compliance_score * 100.0
        ));

        if report.summary.failed > 0 {
            output.push_str("## Failures\n\n");
            for result in &report.results {
                if result.verdict == DataEndStreamTestVerdict::Fail {
                    output.push_str(&format!("### {}\n", result.case_id));
                    if let Some(error) = &result.error {
                        output.push_str(&format!("**Error:** {}\n", error));
                    }
                    if !result.differences.is_empty() {
                        output.push_str("**Differences:**\n");
                        for diff in &result.differences {
                            output.push_str(&format!("- {}\n", diff));
                        }
                    }
                    output.push('\n');
                }
            }
        }

        output.push_str("## All Results\n\n");
        output.push_str("| Case ID | Verdict | Description |\n");
        output.push_str("|---------|---------|-------------|\n");
        for result in &report.results {
            output.push_str(&format!(
                "| {} | {} | Case {} |\n",
                result.case_id, result.verdict, result.case_id
            ));
        }

        output
    }
}

impl Default for DataEndStreamConformanceTester {
    fn default() -> Self {
        Self::new()
    }
}

fn accept_peer_settings(connection: &mut Connection) -> Result<(), String> {
    let received = connection
        .process_frame(Frame::Settings(SettingsFrame::new(vec![])))
        .map_err(|err| err.to_string())?;
    if received.is_some() {
        return Err("SETTINGS handshake produced an application frame".to_string());
    }

    match connection.next_frame() {
        Some(Frame::Settings(settings)) if settings.ack => Ok(()),
        other => Err(format!(
            "SETTINGS handshake should queue exactly one ACK, got {other:?}"
        )),
    }
}

fn request_header_block(stream_id: u32) -> Bytes {
    let headers = [
        Header::new(":method", "GET"),
        Header::new(":scheme", "https"),
        Header::new(":authority", "example.test"),
        Header::new(":path", format!("/stream/{stream_id}")),
    ];
    let mut encoder = HpackEncoder::new();
    let mut block = BytesMut::new();
    encoder.encode(&headers, &mut block);
    block.freeze()
}

/// Open a peer-initiated stream through the production HEADERS path.
fn initialize_remote_stream(connection: &mut Connection, stream_id: u32) -> Result<(), String> {
    let headers = HeadersFrame::new(stream_id, request_header_block(stream_id), false, true);
    match connection
        .process_frame(Frame::Headers(headers))
        .map_err(|err| err.to_string())?
    {
        Some(ReceivedFrame::Headers {
            stream_id: received_stream_id,
            end_stream,
            ..
        }) if received_stream_id == stream_id && !end_stream => Ok(()),
        other => Err(format!(
            "HEADERS stream initialization produced unexpected frame: {other:?}"
        )),
    }
}

/// Process a DATA frame through the production connection state machine.
fn process_live_data_frame(
    connection: &mut Connection,
    data_frame: &DataFrame,
) -> Result<(), String> {
    match connection.process_frame(Frame::Data(data_frame.clone())) {
        Ok(Some(ReceivedFrame::Data {
            stream_id,
            end_stream,
            ..
        })) if stream_id == data_frame.stream_id && end_stream == data_frame.end_stream => Ok(()),
        Ok(None) => Ok(()),
        Ok(other) => Err(format!("unexpected DATA result frame: {other:?}")),
        Err(err) => Err(format!("{:?}", err.code)),
    }
}

/// Extract DATA END_STREAM-related connection state from asupersync connection.
fn extract_asupersync_data_end_stream_state(
    connection: &Connection,
    stream_ids: &[u32],
    error_messages: Vec<String>,
) -> DataEndStreamConnectionState {
    let stream_states = stream_ids
        .iter()
        .filter_map(|&stream_id| {
            let stream = connection.stream(stream_id)?;
            let state = stream.state();
            Some(StreamEndStreamState {
                stream_id,
                state: format!("{state:?}"),
                can_recv: state.can_recv(),
                can_send: state.can_send(),
                error_code: stream.error_code().map(|code| format!("{code:?}")),
            })
        })
        .collect();

    DataEndStreamConnectionState {
        connection_state: format!("{:?}", connection.state()),
        stream_states,
        has_errors: !error_messages.is_empty(),
        error_messages,
    }
}

/// Create predefined test cases for DATA frame END_STREAM conformance.
fn create_data_end_stream_test_cases() -> Vec<DataEndStreamConformanceCase> {
    vec![
        // Test Case 1: Basic DATA with END_STREAM
        DataEndStreamConformanceCase {
            id: "data-end-stream-001".to_string(),
            description: "Basic DATA frame with END_STREAM closes stream correctly".to_string(),
            requirement_level: RequirementLevel::Must,
            initial_streams: vec![1],
            data_sequence: vec![SerializableDataFrame {
                stream_id: 1,
                data: b"Hello, World!".to_vec(),
                end_stream: true,
            }],
            expected_connection_state: DataEndStreamConnectionState {
                connection_state: "Open".to_string(),
                stream_states: vec![StreamEndStreamState {
                    stream_id: 1,
                    state: "HalfClosedRemote".to_string(),
                    can_recv: false,
                    can_send: true,
                    error_code: None,
                }],
                has_errors: false,
                error_messages: Vec::new(),
            },
        },
        // Test Case 2: DATA after END_STREAM should be rejected
        DataEndStreamConformanceCase {
            id: "data-end-stream-002".to_string(),
            description: "DATA frame after END_STREAM is rejected with StreamClosed error"
                .to_string(),
            requirement_level: RequirementLevel::Must,
            initial_streams: vec![1],
            data_sequence: vec![
                SerializableDataFrame {
                    stream_id: 1,
                    data: b"First message".to_vec(),
                    end_stream: true,
                },
                SerializableDataFrame {
                    stream_id: 1,
                    data: b"Should be rejected".to_vec(),
                    end_stream: false,
                },
            ],
            expected_connection_state: DataEndStreamConnectionState {
                connection_state: "Open".to_string(),
                stream_states: vec![StreamEndStreamState {
                    stream_id: 1,
                    state: "HalfClosedRemote".to_string(),
                    can_recv: false,
                    can_send: true,
                    error_code: None,
                }],
                has_errors: true,
                error_messages: vec!["DATA frame error on stream 1: StreamClosed".to_string()],
            },
        },
        // Test Case 3: Multiple DATA frames, last with END_STREAM
        DataEndStreamConformanceCase {
            id: "data-end-stream-003".to_string(),
            description: "Multiple DATA frames, only last has END_STREAM".to_string(),
            requirement_level: RequirementLevel::Must,
            initial_streams: vec![1],
            data_sequence: vec![
                SerializableDataFrame {
                    stream_id: 1,
                    data: b"Chunk 1".to_vec(),
                    end_stream: false,
                },
                SerializableDataFrame {
                    stream_id: 1,
                    data: b"Chunk 2".to_vec(),
                    end_stream: false,
                },
                SerializableDataFrame {
                    stream_id: 1,
                    data: b"Final chunk".to_vec(),
                    end_stream: true,
                },
            ],
            expected_connection_state: DataEndStreamConnectionState {
                connection_state: "Open".to_string(),
                stream_states: vec![StreamEndStreamState {
                    stream_id: 1,
                    state: "HalfClosedRemote".to_string(),
                    can_recv: false,
                    can_send: true,
                    error_code: None,
                }],
                has_errors: false,
                error_messages: Vec::new(),
            },
        },
        // Test Case 4: Empty DATA with END_STREAM
        DataEndStreamConformanceCase {
            id: "data-end-stream-004".to_string(),
            description: "Empty DATA frame with END_STREAM closes stream".to_string(),
            requirement_level: RequirementLevel::Must,
            initial_streams: vec![1],
            data_sequence: vec![SerializableDataFrame {
                stream_id: 1,
                data: Vec::new(), // Empty payload
                end_stream: true,
            }],
            expected_connection_state: DataEndStreamConnectionState {
                connection_state: "Open".to_string(),
                stream_states: vec![StreamEndStreamState {
                    stream_id: 1,
                    state: "HalfClosedRemote".to_string(),
                    can_recv: false,
                    can_send: true,
                    error_code: None,
                }],
                has_errors: false,
                error_messages: Vec::new(),
            },
        },
        // Test Case 5: Multiple streams with END_STREAM
        DataEndStreamConformanceCase {
            id: "data-end-stream-005".to_string(),
            description: "Multiple streams each closed with END_STREAM".to_string(),
            requirement_level: RequirementLevel::Should,
            initial_streams: vec![1, 3, 5],
            data_sequence: vec![
                SerializableDataFrame {
                    stream_id: 1,
                    data: b"Stream 1 data".to_vec(),
                    end_stream: true,
                },
                SerializableDataFrame {
                    stream_id: 3,
                    data: b"Stream 3 data".to_vec(),
                    end_stream: true,
                },
                SerializableDataFrame {
                    stream_id: 5,
                    data: b"Stream 5 data".to_vec(),
                    end_stream: true,
                },
            ],
            expected_connection_state: DataEndStreamConnectionState {
                connection_state: "Open".to_string(),
                stream_states: vec![
                    StreamEndStreamState {
                        stream_id: 1,
                        state: "HalfClosedRemote".to_string(),
                        can_recv: false,
                        can_send: true,
                        error_code: None,
                    },
                    StreamEndStreamState {
                        stream_id: 3,
                        state: "HalfClosedRemote".to_string(),
                        can_recv: false,
                        can_send: true,
                        error_code: None,
                    },
                    StreamEndStreamState {
                        stream_id: 5,
                        state: "HalfClosedRemote".to_string(),
                        can_recv: false,
                        can_send: true,
                        error_code: None,
                    },
                ],
                has_errors: false,
                error_messages: Vec::new(),
            },
        },
        // Test Case 6: Large DATA with END_STREAM
        DataEndStreamConformanceCase {
            id: "data-end-stream-006".to_string(),
            description: "Large DATA frame with END_STREAM handles correctly".to_string(),
            requirement_level: RequirementLevel::Should,
            initial_streams: vec![1],
            data_sequence: vec![SerializableDataFrame {
                stream_id: 1,
                data: vec![0u8; 8192], // 8KB payload
                end_stream: true,
            }],
            expected_connection_state: DataEndStreamConnectionState {
                connection_state: "Open".to_string(),
                stream_states: vec![StreamEndStreamState {
                    stream_id: 1,
                    state: "HalfClosedRemote".to_string(),
                    can_recv: false,
                    can_send: true,
                    error_code: None,
                }],
                has_errors: false,
                error_messages: Vec::new(),
            },
        },
        // Test Case 7: Attempt to send multiple END_STREAM frames
        DataEndStreamConformanceCase {
            id: "data-end-stream-007".to_string(),
            description: "Multiple END_STREAM frames on same stream should be rejected".to_string(),
            requirement_level: RequirementLevel::Must,
            initial_streams: vec![1],
            data_sequence: vec![
                SerializableDataFrame {
                    stream_id: 1,
                    data: b"First end".to_vec(),
                    end_stream: true,
                },
                SerializableDataFrame {
                    stream_id: 1,
                    data: b"Second end".to_vec(),
                    end_stream: true, // Second END_STREAM should be rejected
                },
            ],
            expected_connection_state: DataEndStreamConnectionState {
                connection_state: "Open".to_string(),
                stream_states: vec![StreamEndStreamState {
                    stream_id: 1,
                    state: "HalfClosedRemote".to_string(),
                    can_recv: false,
                    can_send: true,
                    error_code: None,
                }],
                has_errors: true,
                error_messages: vec!["DATA frame error on stream 1: StreamClosed".to_string()],
            },
        },
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use asupersync::http::h2::ErrorCode;

    #[tokio::test]
    async fn h2_reference_unavailable_still_runs_live_data_assertions() {
        let tester = DataEndStreamConformanceTester::new();
        let report = tester.run_all_tests().await;

        assert_eq!(report.total_cases, 7);
        assert_eq!(report.summary.passed, 0);
        assert_eq!(report.summary.failed, 0);
        assert_eq!(report.summary.expected_failures, 7);
        assert_eq!(report.summary.skipped, 0);
        assert_eq!(report.summary.compliance_score, 0.0);
        assert!(
            report
                .results
                .iter()
                .all(|result| result.h2_state.is_none()),
            "h2 reference is intentionally not wired for this harness"
        );
        assert!(
            report
                .results
                .iter()
                .all(|result| result.asupersync_state.is_some()),
            "every case must exercise the live asupersync connection"
        );
    }

    #[tokio::test]
    async fn h2_reference_gap_is_reported_as_expected_failure_not_pass() {
        let tester = DataEndStreamConformanceTester::new();
        let report = tester.run_all_tests().await;

        assert!(
            report
                .results
                .iter()
                .all(|result| result.verdict == DataEndStreamTestVerdict::ExpectedFailure),
            "unwired h2 vendor parity must not be reported as full pass: {:?}",
            report.results
        );
        assert!(
            report.results.iter().all(|result| result
                .error
                .as_deref()
                .is_some_and(|error| error.contains(H2_REFERENCE_UNAVAILABLE)
                    && error.contains("vendor parity remains unexercised"))),
            "each expected failure should explain the missing h2 reference parity"
        );
    }

    #[test]
    fn data_end_stream_moves_stream_half_closed_remote() {
        let mut connection = Connection::server(Settings::default());
        accept_peer_settings(&mut connection).expect("SETTINGS handshake");
        initialize_remote_stream(&mut connection, 1).expect("open stream");

        let data = DataFrame::new(1, Bytes::from_static(b"done"), true);
        process_live_data_frame(&mut connection, &data).expect("DATA should process");

        let stream = connection.stream(1).expect("stream exists");
        assert_eq!(format!("{:?}", stream.state()), "HalfClosedRemote");
        assert!(!stream.state().can_recv());
        assert!(stream.state().can_send());
    }

    #[test]
    fn data_after_end_stream_reports_stream_closed() {
        let mut connection = Connection::server(Settings::default());
        accept_peer_settings(&mut connection).expect("SETTINGS handshake");
        initialize_remote_stream(&mut connection, 1).expect("open stream");

        let first = DataFrame::new(1, Bytes::from_static(b"done"), true);
        process_live_data_frame(&mut connection, &first).expect("first DATA should process");

        let second = DataFrame::new(1, Bytes::from_static(b"again"), false);
        let error = process_live_data_frame(&mut connection, &second)
            .expect_err("DATA after END_STREAM must fail");
        assert_eq!(error, "StreamClosed");
    }

    #[test]
    fn data_frame_updates_connection_and_stream_receive_windows() {
        let mut connection = Connection::server(Settings::default());
        accept_peer_settings(&mut connection).expect("SETTINGS handshake");
        initialize_remote_stream(&mut connection, 1).expect("open stream");

        let connection_window_before = connection.recv_window();
        let stream_window_before = connection.stream(1).unwrap().recv_window();
        let data = DataFrame::new(1, Bytes::from_static(b"windowed"), false);
        process_live_data_frame(&mut connection, &data).expect("DATA should process");

        assert_eq!(connection.recv_window(), connection_window_before - 8);
        assert_eq!(
            connection.stream(1).unwrap().recv_window(),
            stream_window_before - 8
        );
        assert_eq!(
            format!("{:?}", connection.stream(1).unwrap().state()),
            "Open"
        );
    }

    #[test]
    fn headers_after_data_end_stream_reports_stream_closed() {
        let mut connection = Connection::server(Settings::default());
        accept_peer_settings(&mut connection).expect("SETTINGS handshake");
        initialize_remote_stream(&mut connection, 1).expect("open stream");

        let data = DataFrame::new(1, Bytes::from_static(b"done"), true);
        process_live_data_frame(&mut connection, &data).expect("DATA should close remote side");

        let trailers = HeadersFrame::new(1, Bytes::new(), true, true);
        let error = connection
            .process_frame(Frame::Headers(trailers))
            .expect_err("HEADERS after END_STREAM must fail");
        assert_eq!(error.code, ErrorCode::StreamClosed);
        assert_eq!(error.stream_id, Some(1));
    }
}