eggress-testkit 1.0.2

Test utilities for eggress proxy
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
//! Composition matrix validation for the pproxy parity model.
//!
//! Validates `docs/parity/composition_matrix.toml` — the machine-readable
//! composition graph that maps protocol×role×traffic_kind combinations to
//! capability IDs and evidence.
//!
//! The composition matrix complements the flat capability manifest by
//! preventing false parity claims: a protocol cannot be claimed as
//! supported merely because one isolated implementation exists.

use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};

use serde::Deserialize;
use thiserror::Error;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Allowed protocol values in the composition matrix.
pub const ALLOWED_PROTOCOLS: &[&str] = &[
    "direct",
    "http",
    "https",
    "socks4",
    "socks4a",
    "socks5",
    "shadowsocks",
    "trojan",
    "ssh",
    "ws",
    "wss",
    "raw",
    "tunnel",
    "h2",
    "quic",
    "h3",
    "unix",
    "redir",
];

/// Allowed role values.
pub const ALLOWED_ROLES: &[&str] = &[
    "listener",
    "upstream",
    "chain_hop",
    "terminal",
    "reverse_server",
    "reverse_client",
];

/// Allowed traffic kind values.
pub const ALLOWED_TRAFFIC_KINDS: &[&str] = &["tcp", "udp"];

/// Allowed tier values (same as manifest).
pub const ALLOWED_TIERS: &[&str] = &[
    "drop_in",
    "compatible_with_warning",
    "native_equivalent",
    "intentional_non_parity",
    "unsupported",
];

/// Allowed evidence values (same as manifest).
pub const ALLOWED_EVIDENCE: &[&str] = &[
    "differential",
    "integration",
    "unit",
    "synthetic",
    "docs_only",
    "none",
];

/// Allowed constraint types.
pub const ALLOWED_CONSTRAINT_TYPES: &[&str] = &[
    "chain_max_hops",
    "platform",
    "requires_tls",
    "no_udp",
    "no_chain",
    "protocol_crate_only",
    "upstream_only_no_listener",
];

/// Allowed caveat class values.
pub const ALLOWED_CAVEAT_CLASSES: &[&str] = &[
    "protocol_crate_only",
    "missing_protocol_command",
    "missing_protocol_role",
    "missing_protocol_transport",
    "deferred_by_adr",
    "intentional_non_parity",
    "cli_process_model",
    "translator_scope_gap",
];

/// Pinned schema version.
pub const PINNED_SCHEMA_VERSION: &str = "1";

// ---------------------------------------------------------------------------
// Data model
// ---------------------------------------------------------------------------

/// Top-level matrix metadata.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct CompositionMatrixMeta {
    pub schema_version: String,
    pub manifest_ref: String,
    #[serde(default)]
    pub description: String,
}

/// A single composition cell mapping protocol×role×traffic_kind to capabilities.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct CompositionCell {
    pub protocol: String,
    pub role: String,
    pub traffic_kind: String,
    pub tier: String,
    pub evidence: String,
    #[serde(default)]
    pub capability_ids: Vec<String>,
    #[serde(default)]
    pub notes: String,
    #[serde(default)]
    pub caveat_class: String,
    #[serde(default)]
    pub rationale: String,
    /// Optional chain max hops constraint for this cell.
    pub chain_max: Option<u32>,
}

/// A chain composition (listener->upstream transition).
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct ChainComposition {
    pub from_protocol: String,
    pub to_protocol: String,
    pub traffic_kind: String,
    pub tier: String,
    pub evidence: String,
    #[serde(default)]
    pub capability_ids: Vec<String>,
    #[serde(default)]
    pub notes: String,
}

/// A global constraint on the composition matrix.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct CompositionConstraint {
    #[serde(rename = "type")]
    pub constraint_type: String,
    #[serde(default)]
    pub value: Option<u32>,
    #[serde(default)]
    pub applies_to: Vec<String>,
    #[serde(default)]
    pub description: String,
}

/// The parsed composition matrix.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct CompositionMatrix {
    pub matrix: CompositionMatrixMeta,
    #[serde(default)]
    pub cell: Vec<CompositionCell>,
    #[serde(default)]
    pub chain: Vec<ChainComposition>,
    #[serde(default)]
    pub constraint: Vec<CompositionConstraint>,
}

// ---------------------------------------------------------------------------
// Validation errors
// ---------------------------------------------------------------------------

/// A single validation error or warning.
#[derive(Debug, Clone, Error, PartialEq, Eq)]
pub enum CompositionValidationError {
    #[error("schema version mismatch: got {got}, expected {expected}")]
    SchemaVersionMismatch { got: String, expected: String },

    #[error("unknown protocol: {0}")]
    UnknownProtocol(String),

    #[error("unknown role: {0}")]
    UnknownRole(String),

    #[error("unknown traffic_kind: {0}")]
    UnknownTrafficKind(String),

    #[error("unknown tier: {0}")]
    UnknownTier(String),

    #[error("unknown evidence: {0}")]
    UnknownEvidence(String),

    #[error("unknown constraint type: {0}")]
    UnknownConstraintType(String),

    #[error("unknown caveat_class: {0}")]
    UnknownCaveatClass(String),

    #[error("duplicate cell: protocol={protocol} role={role} traffic_kind={traffic_kind}")]
    DuplicateCell {
        protocol: String,
        role: String,
        traffic_kind: String,
    },

    #[error("duplicate chain: from={from_protocol} to={to_protocol} traffic_kind={traffic_kind}")]
    DuplicateChain {
        from_protocol: String,
        to_protocol: String,
        traffic_kind: String,
    },

    #[error("unsupported cell has non-empty capability_ids: protocol={protocol} role={role}")]
    UnsupportedCellWithCapabilities { protocol: String, role: String },

    #[error("protocol-crate-only cell has tier=drop_in: {0}")]
    ProtocolCrateOnlyDropIn(String),

    #[error("drop_in cell with evidence weaker than integration: {protocol}/{role}")]
    DropInWeakEvidence { protocol: String, role: String },

    #[error("chain composition missing from_protocol or to_protocol")]
    ChainMissingProtocols,

    #[error("chain composition with chain_max < 2: {from_protocol} -> {to_protocol}")]
    ChainMaxTooSmall {
        from_protocol: String,
        to_protocol: String,
    },

    #[error("capability_id not found in manifest: {0}")]
    UnknownCapabilityId(String),

    #[error("constraint applies_to references unknown protocol: {0}")]
    ConstraintUnknownProtocol(String),

    #[error("empty composition matrix (no cells or chains)")]
    EmptyMatrix,

    #[error("io error: {0}")]
    IoError(String),

    #[error("toml parse error: {0}")]
    ParseError(String),
}

impl From<toml::de::Error> for CompositionValidationError {
    fn from(e: toml::de::Error) -> Self {
        CompositionValidationError::ParseError(e.to_string())
    }
}

/// Collection of validation errors and warnings.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CompositionValidationResult {
    pub errors: Vec<CompositionValidationError>,
    pub warnings: Vec<CompositionValidationError>,
}

impl CompositionValidationResult {
    pub fn push_error(&mut self, err: CompositionValidationError) {
        self.errors.push(err);
    }

    pub fn push_warning(&mut self, warn: CompositionValidationError) {
        self.warnings.push(warn);
    }

    pub fn is_empty(&self) -> bool {
        self.errors.is_empty() && self.warnings.is_empty()
    }

    pub fn has_errors(&self) -> bool {
        !self.errors.is_empty()
    }

    pub fn len(&self) -> usize {
        self.errors.len() + self.warnings.len()
    }
}

// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------

/// Validate a composition matrix against the schema and manifest.
///
/// Returns `Ok(())` on success, or `Err(result)` with errors/warnings.
pub fn validate_composition_matrix(
    matrix: &CompositionMatrix,
    manifest_capability_ids: &HashSet<&str>,
) -> Result<(), CompositionValidationResult> {
    let mut result = CompositionValidationResult::default();

    // Schema version check
    if matrix.matrix.schema_version != PINNED_SCHEMA_VERSION {
        result.push_error(CompositionValidationError::SchemaVersionMismatch {
            got: matrix.matrix.schema_version.clone(),
            expected: PINNED_SCHEMA_VERSION.to_string(),
        });
    }

    // Empty matrix check
    if matrix.cell.is_empty() && matrix.chain.is_empty() {
        result.push_error(CompositionValidationError::EmptyMatrix);
    }

    // Validate cells
    let mut seen_cells: HashSet<(&str, &str, &str)> = HashSet::new();
    for cell in &matrix.cell {
        if !ALLOWED_PROTOCOLS.contains(&cell.protocol.as_str()) {
            result.push_error(CompositionValidationError::UnknownProtocol(
                cell.protocol.clone(),
            ));
        }

        if !ALLOWED_ROLES.contains(&cell.role.as_str()) {
            result.push_error(CompositionValidationError::UnknownRole(cell.role.clone()));
        }

        if !ALLOWED_TRAFFIC_KINDS.contains(&cell.traffic_kind.as_str()) {
            result.push_error(CompositionValidationError::UnknownTrafficKind(
                cell.traffic_kind.clone(),
            ));
        }

        if !ALLOWED_TIERS.contains(&cell.tier.as_str()) {
            result.push_error(CompositionValidationError::UnknownTier(cell.tier.clone()));
        }

        if !ALLOWED_EVIDENCE.contains(&cell.evidence.as_str()) {
            result.push_error(CompositionValidationError::UnknownEvidence(
                cell.evidence.clone(),
            ));
        }

        if !cell.caveat_class.is_empty()
            && !ALLOWED_CAVEAT_CLASSES.contains(&cell.caveat_class.as_str())
        {
            result.push_error(CompositionValidationError::UnknownCaveatClass(
                cell.caveat_class.clone(),
            ));
        }

        let key = (
            cell.protocol.as_str(),
            cell.role.as_str(),
            cell.traffic_kind.as_str(),
        );
        if !seen_cells.insert(key) {
            result.push_error(CompositionValidationError::DuplicateCell {
                protocol: cell.protocol.clone(),
                role: cell.role.clone(),
                traffic_kind: cell.traffic_kind.clone(),
            });
        }

        if cell.tier == "unsupported" && !cell.capability_ids.is_empty() {
            result.push_error(
                CompositionValidationError::UnsupportedCellWithCapabilities {
                    protocol: cell.protocol.clone(),
                    role: cell.role.clone(),
                },
            );
        }

        if cell.caveat_class == "protocol_crate_only" && cell.tier == "drop_in" {
            result.push_error(CompositionValidationError::ProtocolCrateOnlyDropIn(
                format!("{}/{}", cell.protocol, cell.role),
            ));
        }

        if cell.tier == "drop_in"
            && matches!(
                cell.evidence.as_str(),
                "unit" | "synthetic" | "docs_only" | "none"
            )
        {
            result.push_warning(CompositionValidationError::DropInWeakEvidence {
                protocol: cell.protocol.clone(),
                role: cell.role.clone(),
            });
        }

        for cap_id in &cell.capability_ids {
            if !manifest_capability_ids.contains(cap_id.as_str()) {
                result.push_error(CompositionValidationError::UnknownCapabilityId(
                    cap_id.clone(),
                ));
            }
        }
    }

    // Validate chains
    let mut seen_chains: HashSet<(&str, &str, &str)> = HashSet::new();
    for chain in &matrix.chain {
        if !ALLOWED_PROTOCOLS.contains(&chain.from_protocol.as_str()) {
            result.push_error(CompositionValidationError::UnknownProtocol(
                chain.from_protocol.clone(),
            ));
        }
        if !ALLOWED_PROTOCOLS.contains(&chain.to_protocol.as_str()) {
            result.push_error(CompositionValidationError::UnknownProtocol(
                chain.to_protocol.clone(),
            ));
        }

        if !ALLOWED_TRAFFIC_KINDS.contains(&chain.traffic_kind.as_str()) {
            result.push_error(CompositionValidationError::UnknownTrafficKind(
                chain.traffic_kind.clone(),
            ));
        }

        if !ALLOWED_TIERS.contains(&chain.tier.as_str()) {
            result.push_error(CompositionValidationError::UnknownTier(chain.tier.clone()));
        }

        if !ALLOWED_EVIDENCE.contains(&chain.evidence.as_str()) {
            result.push_error(CompositionValidationError::UnknownEvidence(
                chain.evidence.clone(),
            ));
        }

        let key = (
            chain.from_protocol.as_str(),
            chain.to_protocol.as_str(),
            chain.traffic_kind.as_str(),
        );
        if !seen_chains.insert(key) {
            result.push_error(CompositionValidationError::DuplicateChain {
                from_protocol: chain.from_protocol.clone(),
                to_protocol: chain.to_protocol.clone(),
                traffic_kind: chain.traffic_kind.clone(),
            });
        }

        for cap_id in &chain.capability_ids {
            if !manifest_capability_ids.contains(cap_id.as_str()) {
                result.push_error(CompositionValidationError::UnknownCapabilityId(
                    cap_id.clone(),
                ));
            }
        }
    }

    // Validate constraints
    for constraint in &matrix.constraint {
        if !ALLOWED_CONSTRAINT_TYPES.contains(&constraint.constraint_type.as_str()) {
            result.push_error(CompositionValidationError::UnknownConstraintType(
                constraint.constraint_type.clone(),
            ));
        }

        for proto in &constraint.applies_to {
            if !ALLOWED_PROTOCOLS.contains(&proto.as_str()) {
                result.push_error(CompositionValidationError::ConstraintUnknownProtocol(
                    proto.clone(),
                ));
            }
        }
    }

    if result.errors.is_empty() {
        Ok(())
    } else {
        Err(result)
    }
}

/// Parse and validate a composition matrix from a TOML file.
pub fn validate_composition_matrix_file(
    path: &Path,
    manifest_capability_ids: &HashSet<&str>,
) -> Result<CompositionMatrix, CompositionValidationResult> {
    let content = fs::read_to_string(path).map_err(|e| {
        let mut result = CompositionValidationResult::default();
        result.push_error(CompositionValidationError::SchemaVersionMismatch {
            got: format!("read error: {}", e),
            expected: "valid TOML file".to_string(),
        });
        result
    })?;

    let matrix: CompositionMatrix = toml::from_str(&content).map_err(|e| {
        let mut result = CompositionValidationResult::default();
        result.push_error(CompositionValidationError::SchemaVersionMismatch {
            got: format!("parse error: {}", e),
            expected: "valid TOML structure".to_string(),
        });
        result
    })?;

    validate_composition_matrix(&matrix, manifest_capability_ids)?;
    Ok(matrix)
}

/// Find the composition matrix file path relative to the workspace root.
pub fn find_composition_matrix_path() -> Option<PathBuf> {
    let candidates = [
        "docs/parity/composition_matrix.toml",
        "../docs/parity/composition_matrix.toml",
        "../../docs/parity/composition_matrix.toml",
    ];

    for candidate in &candidates {
        let path = Path::new(candidate);
        if path.exists() {
            return Some(path.to_path_buf());
        }
    }

    None
}

/// Query whether a specific composition is supported.
pub fn query_composition<'a>(
    matrix: &'a CompositionMatrix,
    protocol: &str,
    role: &str,
    traffic_kind: &str,
) -> Option<&'a CompositionCell> {
    matrix
        .cell
        .iter()
        .find(|c| c.protocol == protocol && c.role == role && c.traffic_kind == traffic_kind)
}

/// Query whether a chain composition is supported.
pub fn query_chain<'a>(
    matrix: &'a CompositionMatrix,
    from_protocol: &str,
    to_protocol: &str,
    traffic_kind: &str,
) -> Option<&'a ChainComposition> {
    matrix.chain.iter().find(|c| {
        c.from_protocol == from_protocol
            && c.to_protocol == to_protocol
            && c.traffic_kind == traffic_kind
    })
}

/// Get all supported protocols for a given role and traffic kind.
pub fn supported_protocols<'a>(
    matrix: &'a CompositionMatrix,
    role: &str,
    traffic_kind: &str,
) -> Vec<&'a str> {
    matrix
        .cell
        .iter()
        .filter(|c| c.role == role && c.traffic_kind == traffic_kind && c.tier != "unsupported")
        .map(|c| c.protocol.as_str())
        .collect()
}

/// Get all supported roles for a given protocol and traffic kind.
pub fn supported_roles<'a>(
    matrix: &'a CompositionMatrix,
    protocol: &str,
    traffic_kind: &str,
) -> Vec<&'a str> {
    matrix
        .cell
        .iter()
        .filter(|c| {
            c.protocol == protocol && c.traffic_kind == traffic_kind && c.tier != "unsupported"
        })
        .map(|c| c.role.as_str())
        .collect()
}

/// Count cells by tier.
pub fn count_by_tier(matrix: &CompositionMatrix) -> std::collections::HashMap<String, usize> {
    let mut counts = std::collections::HashMap::new();
    for cell in &matrix.cell {
        *counts.entry(cell.tier.clone()).or_insert(0) += 1;
    }
    counts
}

/// Count chain compositions by tier.
pub fn count_chains_by_tier(
    matrix: &CompositionMatrix,
) -> std::collections::HashMap<String, usize> {
    let mut counts = std::collections::HashMap::new();
    for chain in &matrix.chain {
        *counts.entry(chain.tier.clone()).or_insert(0) += 1;
    }
    counts
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// Runtime composition validator backed by a loaded matrix.
///
/// Wraps a `CompositionMatrix` and provides efficient query methods
/// for checking whether a given protocol×role×traffic_kind combination
/// is supported, and what tier/evidence it has.
pub struct CompositionValidator {
    matrix: CompositionMatrix,
}

impl CompositionValidator {
    /// Load the composition matrix from the canonical path.
    pub fn load() -> Option<Self> {
        let path = find_composition_matrix_path()?;
        let content = fs::read_to_string(&path).ok()?;
        let matrix: CompositionMatrix = toml::from_str(&content).ok()?;
        Some(Self { matrix })
    }

    /// Load from an explicit file path.
    pub fn from_file(path: &Path) -> Result<Self, CompositionValidationError> {
        let content = fs::read_to_string(path)
            .map_err(|e| CompositionValidationError::IoError(format!("{}: {e}", path.display())))?;
        let matrix: CompositionMatrix = toml::from_str(&content)?;
        Ok(Self { matrix })
    }

    /// Query a single composition cell.
    pub fn query(
        &self,
        protocol: &str,
        role: &str,
        traffic_kind: &str,
    ) -> Option<&CompositionCell> {
        query_composition(&self.matrix, protocol, role, traffic_kind)
    }

    /// Query a chain composition.
    pub fn query_chain(
        &self,
        from_protocol: &str,
        to_protocol: &str,
        traffic_kind: &str,
    ) -> Option<&ChainComposition> {
        query_chain(&self.matrix, from_protocol, to_protocol, traffic_kind)
    }

    /// Check if a protocol+role+traffic_kind combination is supported (non-unsupported tier).
    pub fn is_supported(&self, protocol: &str, role: &str, traffic_kind: &str) -> bool {
        self.query(protocol, role, traffic_kind)
            .map(|c| c.tier != "unsupported")
            .unwrap_or(false)
    }

    /// Get all protocols supporting a given role+traffic_kind at or above a tier.
    pub fn protocols_at_or_above_tier(
        &self,
        role: &str,
        traffic_kind: &str,
        min_tier: &str,
    ) -> Vec<&str> {
        let tier_order = |t: &str| match t {
            "drop_in" => 0,
            "compatible_with_warning" => 1,
            "native_equivalent" => 2,
            "intentional_non_parity" => 3,
            "unsupported" => 4,
            _ => 5,
        };
        let min_rank = tier_order(min_tier);

        self.matrix
            .cell
            .iter()
            .filter(|c| {
                c.role == role && c.traffic_kind == traffic_kind && tier_order(&c.tier) <= min_rank
            })
            .map(|c| c.protocol.as_str())
            .collect()
    }

    /// Get all constraints that apply to a given protocol.
    pub fn constraints_for(&self, protocol: &str) -> Vec<&CompositionConstraint> {
        self.matrix
            .constraint
            .iter()
            .filter(|c| c.applies_to.iter().any(|p| p == protocol))
            .collect()
    }

    /// Return a reference to the inner matrix.
    pub fn inner(&self) -> &CompositionMatrix {
        &self.matrix
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Derive the canonical manifest path from a composition matrix path.
    fn manifest_path_from_matrix(matrix_path: &Path) -> PathBuf {
        // composition_matrix.toml is in docs/parity/, manifest is sibling
        matrix_path
            .parent()
            .unwrap_or(Path::new("."))
            .join("pproxy_capability_manifest.toml")
    }

    fn minimal_matrix() -> CompositionMatrix {
        CompositionMatrix {
            matrix: CompositionMatrixMeta {
                schema_version: PINNED_SCHEMA_VERSION.to_string(),
                manifest_ref: "test.toml".to_string(),
                description: "test".to_string(),
            },
            cell: vec![CompositionCell {
                protocol: "socks5".to_string(),
                role: "listener".to_string(),
                traffic_kind: "tcp".to_string(),
                tier: "drop_in".to_string(),
                evidence: "integration".to_string(),
                capability_ids: vec![],
                notes: String::new(),
                caveat_class: String::new(),
                rationale: String::new(),
                chain_max: None,
            }],
            chain: vec![],
            constraint: vec![],
        }
    }

    #[test]
    fn valid_minimal_matrix() {
        let matrix = minimal_matrix();
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_ok(), "expected Ok, got {:?}", result.err());
    }

    #[test]
    fn empty_matrix_rejected() {
        let mut matrix = minimal_matrix();
        matrix.cell.clear();
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::EmptyMatrix)));
    }

    #[test]
    fn unknown_protocol_rejected() {
        let mut matrix = minimal_matrix();
        matrix.cell[0].protocol = "bogus".to_string();
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::UnknownProtocol(_))));
    }

    #[test]
    fn unknown_role_rejected() {
        let mut matrix = minimal_matrix();
        matrix.cell[0].role = "bogus".to_string();
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::UnknownRole(_))));
    }

    #[test]
    fn unknown_tier_rejected() {
        let mut matrix = minimal_matrix();
        matrix.cell[0].tier = "bogus".to_string();
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::UnknownTier(_))));
    }

    #[test]
    fn unsupported_cell_with_capabilities_rejected() {
        let mut matrix = minimal_matrix();
        matrix.cell[0].tier = "unsupported".to_string();
        matrix.cell[0].capability_ids = vec!["some.cap".to_string()];
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.errors.iter().any(|e| matches!(
            e,
            CompositionValidationError::UnsupportedCellWithCapabilities { .. }
        )));
    }

    #[test]
    fn protocol_crate_only_drop_in_rejected() {
        let mut matrix = minimal_matrix();
        matrix.cell[0].protocol = "ws".to_string();
        matrix.cell[0].tier = "drop_in".to_string();
        matrix.cell[0].caveat_class = "protocol_crate_only".to_string();
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::ProtocolCrateOnlyDropIn(_))));
    }

    #[test]
    fn drop_in_with_weak_evidence_warns() {
        let mut matrix = minimal_matrix();
        matrix.cell[0].tier = "drop_in".to_string();
        matrix.cell[0].evidence = "unit".to_string();
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        // Ok(()) means no errors, but warnings are populated
        assert!(result.is_ok());
    }

    #[test]
    fn duplicate_cell_rejected() {
        let mut matrix = minimal_matrix();
        matrix.cell.push(CompositionCell {
            protocol: "socks5".to_string(),
            role: "listener".to_string(),
            traffic_kind: "tcp".to_string(),
            tier: "drop_in".to_string(),
            evidence: "integration".to_string(),
            capability_ids: vec![],
            notes: String::new(),
            caveat_class: String::new(),
            rationale: String::new(),
            chain_max: None,
        });
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::DuplicateCell { .. })));
    }

    #[test]
    fn unknown_capability_id_rejected() {
        let mut matrix = minimal_matrix();
        matrix.cell[0].capability_ids = vec!["nonexistent.cap".to_string()];
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::UnknownCapabilityId(_))));
    }

    #[test]
    fn known_capability_id_accepted() {
        let mut matrix = minimal_matrix();
        matrix.cell[0].capability_ids = vec!["protocol.socks5.connect_ipv4".to_string()];
        let mut caps = HashSet::new();
        caps.insert("protocol.socks5.connect_ipv4");
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_ok(), "expected Ok, got {:?}", result.err());
    }

    #[test]
    fn chain_validated() {
        let mut matrix = minimal_matrix();
        matrix.chain.push(ChainComposition {
            from_protocol: "socks5".to_string(),
            to_protocol: "http".to_string(),
            traffic_kind: "tcp".to_string(),
            tier: "drop_in".to_string(),
            evidence: "integration".to_string(),
            capability_ids: vec![],
            notes: String::new(),
        });
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_ok(), "expected Ok, got {:?}", result.err());
    }

    #[test]
    fn chain_unknown_protocol_rejected() {
        let mut matrix = minimal_matrix();
        matrix.chain.push(ChainComposition {
            from_protocol: "bogus".to_string(),
            to_protocol: "http".to_string(),
            traffic_kind: "tcp".to_string(),
            tier: "drop_in".to_string(),
            evidence: "integration".to_string(),
            capability_ids: vec![],
            notes: String::new(),
        });
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::UnknownProtocol(_))));
    }

    #[test]
    fn schema_version_mismatch_rejected() {
        let mut matrix = minimal_matrix();
        matrix.matrix.schema_version = "99".to_string();
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::SchemaVersionMismatch { .. })));
    }

    #[test]
    fn constraint_unknown_type_rejected() {
        let mut matrix = minimal_matrix();
        matrix.constraint.push(CompositionConstraint {
            constraint_type: "bogus".to_string(),
            value: None,
            applies_to: vec![],
            description: "test".to_string(),
        });
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::UnknownConstraintType(_))));
    }

    #[test]
    fn constraint_unknown_protocol_rejected() {
        let mut matrix = minimal_matrix();
        matrix.constraint.push(CompositionConstraint {
            constraint_type: "no_udp".to_string(),
            value: None,
            applies_to: vec!["bogus".to_string()],
            description: "test".to_string(),
        });
        let caps = HashSet::new();
        let result = validate_composition_matrix(&matrix, &caps);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err
            .errors
            .iter()
            .any(|e| matches!(e, CompositionValidationError::ConstraintUnknownProtocol(_))));
    }

    #[test]
    fn query_composition_found() {
        let matrix = minimal_matrix();
        let cell = query_composition(&matrix, "socks5", "listener", "tcp");
        assert!(cell.is_some());
        assert_eq!(cell.unwrap().tier, "drop_in");
    }

    #[test]
    fn query_composition_not_found() {
        let matrix = minimal_matrix();
        let cell = query_composition(&matrix, "http", "listener", "tcp");
        assert!(cell.is_none());
    }

    #[test]
    fn supported_protocols_lists_correct() {
        let mut matrix = minimal_matrix();
        matrix.cell.push(CompositionCell {
            protocol: "http".to_string(),
            role: "listener".to_string(),
            traffic_kind: "tcp".to_string(),
            tier: "drop_in".to_string(),
            evidence: "integration".to_string(),
            capability_ids: vec![],
            notes: String::new(),
            caveat_class: String::new(),
            rationale: String::new(),
            chain_max: None,
        });
        let protos = supported_protocols(&matrix, "listener", "tcp");
        assert!(protos.contains(&"socks5"));
        assert!(protos.contains(&"http"));
    }

    #[test]
    fn count_by_tier_works() {
        let mut matrix = minimal_matrix();
        matrix.cell.push(CompositionCell {
            protocol: "http".to_string(),
            role: "listener".to_string(),
            traffic_kind: "tcp".to_string(),
            tier: "drop_in".to_string(),
            evidence: "integration".to_string(),
            capability_ids: vec![],
            notes: String::new(),
            caveat_class: String::new(),
            rationale: String::new(),
            chain_max: None,
        });
        let counts = count_by_tier(&matrix);
        assert_eq!(counts.get("drop_in"), Some(&2));
    }

    #[test]
    fn real_composition_matrix_validates() {
        let Some(matrix_path) = find_composition_matrix_path() else {
            return;
        };

        // Load manifest capability IDs from the canonical manifest
        let manifest_path = manifest_path_from_matrix(&matrix_path);
        let mut manifest_ids: HashSet<&str> = HashSet::new();
        let manifest_content = match fs::read_to_string(&manifest_path) {
            Ok(c) => c,
            Err(_) => return, // manifest not available in this test environment
        };
        let manifest_value: toml::Value = match manifest_content.parse() {
            Ok(v) => v,
            Err(_) => return,
        };
        if let Some(caps) = manifest_value.get("capability").and_then(|v| v.as_array()) {
            for cap in caps {
                if let Some(id) = cap.get("id").and_then(|v| v.as_str()) {
                    manifest_ids.insert(id);
                }
            }
        }

        let result = validate_composition_matrix_file(&matrix_path, &manifest_ids);
        assert!(
            result.is_ok(),
            "real composition matrix validation failed: {:?}",
            result.err()
        );
    }

    #[test]
    fn composition_validator_load() {
        let validator = CompositionValidator::load();
        assert!(validator.is_some(), "should load from canonical path");
    }

    #[test]
    fn composition_validator_query() {
        let validator = CompositionValidator::load().unwrap();
        let cell = validator.query("http", "listener", "tcp");
        assert!(cell.is_some());
        assert_eq!(cell.unwrap().tier, "drop_in");
    }

    #[test]
    fn composition_validator_is_supported() {
        let validator = CompositionValidator::load().unwrap();
        assert!(validator.is_supported("http", "listener", "tcp"));
        assert!(validator.is_supported("socks5", "listener", "tcp"));
        assert!(validator.is_supported("socks5", "upstream", "udp"));
        assert!(!validator.is_supported("ssh", "listener", "tcp"));
        assert!(!validator.is_supported("quic", "listener", "tcp"));
    }

    #[test]
    fn composition_validator_protocols_at_or_above_tier() {
        let validator = CompositionValidator::load().unwrap();
        let protos = validator.protocols_at_or_above_tier("listener", "tcp", "drop_in");
        assert!(protos.contains(&"http"));
        assert!(protos.contains(&"socks5"));
        assert!(!protos.contains(&"ssh"));
    }

    #[test]
    fn composition_validator_constraints_for() {
        let validator = CompositionValidator::load().unwrap();
        let constraints = validator.constraints_for("http");
        assert!(!constraints.is_empty());
    }

    /// Table-driven: every drop_in cell should have non-empty capability_ids
    /// (except reverse proxy cells which have not yet been added to the manifest)
    #[test]
    fn drop_in_cells_have_capability_ids() {
        let validator = CompositionValidator::load().unwrap();
        for cell in &validator.matrix.cell {
            if cell.tier == "drop_in"
                && cell.role != "reverse_server"
                && cell.role != "reverse_client"
            {
                assert!(
                    !cell.capability_ids.is_empty(),
                    "drop_in cell {}/{} has empty capability_ids",
                    cell.protocol,
                    cell.role
                );
            }
        }
    }

    /// Table-driven: every intentional_non_parity cell should have rationale
    #[test]
    fn intentional_non_parity_cells_have_rationale() {
        let validator = CompositionValidator::load().unwrap();
        for cell in &validator.matrix.cell {
            if cell.tier == "intentional_non_parity" {
                assert!(
                    !cell.rationale.is_empty(),
                    "intentional_non_parity cell {}/{} has no rationale",
                    cell.protocol,
                    cell.role
                );
            }
        }
    }

    /// Table-driven: every protocol-crate-only cell has tier != drop_in
    #[test]
    fn protocol_crate_only_not_drop_in() {
        let validator = CompositionValidator::load().unwrap();
        for cell in &validator.matrix.cell {
            if cell.caveat_class == "protocol_crate_only" {
                assert_ne!(
                    cell.tier, "drop_in",
                    "protocol-crate-only cell {}/{} has tier=drop_in",
                    cell.protocol, cell.role
                );
            }
        }
    }

    /// Table-driven: chain compositions reference valid protocols
    #[test]
    fn chain_protocols_are_valid() {
        let validator = CompositionValidator::load().unwrap();
        for chain in &validator.matrix.chain {
            assert!(
                ALLOWED_PROTOCOLS.contains(&chain.from_protocol.as_str()),
                "chain has unknown from_protocol: {}",
                chain.from_protocol
            );
            assert!(
                ALLOWED_PROTOCOLS.contains(&chain.to_protocol.as_str()),
                "chain has unknown to_protocol: {}",
                chain.to_protocol
            );
        }
    }

    /// Table-driven: constraints reference valid protocols in applies_to
    #[test]
    fn constraint_applies_to_valid_protocols() {
        let validator = CompositionValidator::load().unwrap();
        for constraint in &validator.matrix.constraint {
            for proto in &constraint.applies_to {
                assert!(
                    ALLOWED_PROTOCOLS.contains(&proto.as_str()),
                    "constraint type={} applies_to unknown protocol: {}",
                    constraint.constraint_type,
                    proto
                );
            }
        }
    }

    /// Table-driven: every drop_in upstream cell has UDP or TCP capability
    #[test]
    fn drop_in_upstream_cells_have_traffic_kind() {
        let validator = CompositionValidator::load().unwrap();
        for cell in &validator.matrix.cell {
            if cell.role == "upstream" && cell.tier == "drop_in" {
                assert!(
                    cell.traffic_kind == "tcp" || cell.traffic_kind == "udp",
                    "drop_in upstream cell {}/{} has unexpected traffic_kind: {}",
                    cell.protocol,
                    cell.role,
                    cell.traffic_kind
                );
            }
        }
    }

    /// Table-driven: listener cells are TCP (except socks5 and shadowsocks UDP ASSOCIATE)
    #[test]
    fn listener_cells_are_tcp_or_udp_associate() {
        let validator = CompositionValidator::load().unwrap();
        // Protocols that legitimately have UDP listener cells
        let udp_listener_protos = ["socks5", "shadowsocks"];
        for cell in &validator.matrix.cell {
            if cell.role == "listener" {
                if udp_listener_protos.contains(&cell.protocol.as_str())
                    && cell.traffic_kind == "udp"
                {
                    continue;
                }
                assert_eq!(
                    cell.traffic_kind, "tcp",
                    "listener cell {}/{} has traffic_kind={} (expected tcp)",
                    cell.protocol, cell.role, cell.traffic_kind
                );
            }
        }
    }
}