omena-transform-cst 0.4.0

Transform CST contract substrate for Omena CSS
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
//! Observation-indexed equivalence for transform outputs.
//!
//! This relation checks projections supplied by an observation carrier. It
//! cannot establish that the selected profile models every browser-observable
//! effect; that remains a corpus and modelling obligation.

use std::collections::BTreeSet;
use std::fmt;

use serde::Serialize;

use super::{
    ObservationKindV0, PassObservationSurfaceV0, TransformPassDescriptorV0,
    TransformPassObservationRecordV0, default_transform_pass_descriptors,
    default_transform_pass_observation_records,
};

pub const OBSERVATION_KIND_COUNT_V0: usize = 16;

pub const fn all_observation_kinds_v0() -> [ObservationKindV0; OBSERVATION_KIND_COUNT_V0] {
    [
        ObservationKindV0::SelectorMatching,
        ObservationKindV0::CascadeWinner,
        ObservationKindV0::CascadeWinnerEquality,
        ObservationKindV0::ExportedClassNames,
        ObservationKindV0::CustomPropertyComputedValue,
        ObservationKindV0::KeyframesReachability,
        ObservationKindV0::SourceMapTrace,
        ObservationKindV0::LayerRank,
        ObservationKindV0::Specificity,
        ObservationKindV0::Inheritance,
        ObservationKindV0::DeclarationOrder,
        ObservationKindV0::TargetPredicate,
        ObservationKindV0::ModuleResolution,
        ObservationKindV0::ImportContext,
        ObservationKindV0::ValueGraphReachability,
        ObservationKindV0::SemanticMarker,
    ]
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum TransformObserverV0 {
    RawBytes,
    Contract(ObservationKindV0),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum TransformObserverClassV0 {
    RawRepresentation,
    OutputProjection,
    ReadOnlyInput,
}

pub const fn observation_kind_observer_class_v0(
    kind: ObservationKindV0,
) -> TransformObserverClassV0 {
    match kind {
        ObservationKindV0::TargetPredicate => TransformObserverClassV0::ReadOnlyInput,
        ObservationKindV0::SelectorMatching
        | ObservationKindV0::CascadeWinner
        | ObservationKindV0::CascadeWinnerEquality
        | ObservationKindV0::ExportedClassNames
        | ObservationKindV0::CustomPropertyComputedValue
        | ObservationKindV0::KeyframesReachability
        | ObservationKindV0::SourceMapTrace
        | ObservationKindV0::LayerRank
        | ObservationKindV0::Specificity
        | ObservationKindV0::Inheritance
        | ObservationKindV0::DeclarationOrder
        | ObservationKindV0::ModuleResolution
        | ObservationKindV0::ImportContext
        | ObservationKindV0::ValueGraphReachability
        | ObservationKindV0::SemanticMarker => TransformObserverClassV0::OutputProjection,
    }
}

impl TransformObserverV0 {
    pub const fn observer_class(self) -> TransformObserverClassV0 {
        match self {
            Self::RawBytes => TransformObserverClassV0::RawRepresentation,
            Self::Contract(kind) => observation_kind_observer_class_v0(kind),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransformObservationProjectionV0 {
    kind: ObservationKindV0,
    value: String,
}

impl TransformObservationProjectionV0 {
    pub fn new(kind: ObservationKindV0, value: impl Into<String>) -> Self {
        Self {
            kind,
            value: value.into(),
        }
    }

    pub const fn kind(&self) -> ObservationKindV0 {
        self.kind
    }

    pub fn value(&self) -> &str {
        self.value.as_str()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TransformObservationOutputErrorV0 {
    DuplicateProjection { kind: ObservationKindV0 },
    MissingProjection { kind: ObservationKindV0 },
    ProjectionCount { expected: usize, actual: usize },
}

impl fmt::Display for TransformObservationOutputErrorV0 {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DuplicateProjection { kind } => {
                write!(formatter, "duplicate observation projection: {kind:?}")
            }
            Self::MissingProjection { kind } => {
                write!(formatter, "missing observation projection: {kind:?}")
            }
            Self::ProjectionCount { expected, actual } => write!(
                formatter,
                "observation projection count is {actual}; expected {expected}"
            ),
        }
    }
}

impl std::error::Error for TransformObservationOutputErrorV0 {}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransformObservationOutputV0 {
    raw_bytes: Vec<u8>,
    projections: Vec<TransformObservationProjectionV0>,
}

impl TransformObservationOutputV0 {
    pub fn new(
        raw_bytes: impl Into<Vec<u8>>,
        mut projections: Vec<TransformObservationProjectionV0>,
    ) -> Result<Self, TransformObservationOutputErrorV0> {
        let mut observed = BTreeSet::new();
        for projection in &projections {
            if !observed.insert(projection.kind) {
                return Err(TransformObservationOutputErrorV0::DuplicateProjection {
                    kind: projection.kind,
                });
            }
        }
        for kind in all_observation_kinds_v0() {
            if !observed.contains(&kind) {
                return Err(TransformObservationOutputErrorV0::MissingProjection { kind });
            }
        }
        if projections.len() != OBSERVATION_KIND_COUNT_V0 {
            return Err(TransformObservationOutputErrorV0::ProjectionCount {
                expected: OBSERVATION_KIND_COUNT_V0,
                actual: projections.len(),
            });
        }
        projections.sort_by_key(TransformObservationProjectionV0::kind);
        Ok(Self {
            raw_bytes: raw_bytes.into(),
            projections,
        })
    }

    pub fn raw_bytes(&self) -> &[u8] {
        self.raw_bytes.as_slice()
    }

    pub fn projections(&self) -> &[TransformObservationProjectionV0] {
        self.projections.as_slice()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TransformObservationProfileErrorV0 {
    EmptyProfileId,
    DuplicateObserver { observer: TransformObserverV0 },
}

impl fmt::Display for TransformObservationProfileErrorV0 {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyProfileId => formatter.write_str("observation profile id is empty"),
            Self::DuplicateObserver { observer } => {
                write!(
                    formatter,
                    "duplicate observation profile observer: {observer:?}"
                )
            }
        }
    }
}

impl std::error::Error for TransformObservationProfileErrorV0 {}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransformObservationProfileV0 {
    profile_id: String,
    observers: Vec<TransformObserverV0>,
}

impl TransformObservationProfileV0 {
    pub fn new(
        profile_id: impl Into<String>,
        observers: Vec<TransformObserverV0>,
    ) -> Result<Self, TransformObservationProfileErrorV0> {
        let profile_id = profile_id.into();
        if profile_id.is_empty() {
            return Err(TransformObservationProfileErrorV0::EmptyProfileId);
        }
        let mut observed = BTreeSet::new();
        for observer in &observers {
            if !observed.insert(*observer) {
                return Err(TransformObservationProfileErrorV0::DuplicateObserver {
                    observer: *observer,
                });
            }
        }
        Ok(Self {
            profile_id,
            observers,
        })
    }

    pub fn profile_id(&self) -> &str {
        self.profile_id.as_str()
    }

    pub fn observers(&self) -> &[TransformObserverV0] {
        self.observers.as_slice()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase", tag = "kind", content = "value")]
#[non_exhaustive]
pub enum ObservationProjectionValueV0 {
    RawBytes(Vec<u8>),
    Contract(String),
}

pub fn project_transform_observation_v0(
    output: &TransformObservationOutputV0,
    observer: TransformObserverV0,
) -> ObservationProjectionValueV0 {
    match observer {
        TransformObserverV0::RawBytes => {
            ObservationProjectionValueV0::RawBytes(output.raw_bytes.clone())
        }
        TransformObserverV0::Contract(kind) => {
            let Some(projection) = output
                .projections
                .iter()
                .find(|projection| projection.kind == kind)
            else {
                unreachable!("TransformObservationOutputV0 validates all 16 projections")
            };
            ObservationProjectionValueV0::Contract(projection.value.clone())
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransformObservationEquivalenceV0 {
    pub schema_version: &'static str,
    pub product: &'static str,
    pub profile_id: String,
    pub compared_observer_count: usize,
    pub differing_observers: Vec<TransformObserverV0>,
    pub equivalent: bool,
}

pub fn compare_transform_observation_outputs_v0(
    profile: &TransformObservationProfileV0,
    left: &TransformObservationOutputV0,
    right: &TransformObservationOutputV0,
) -> TransformObservationEquivalenceV0 {
    let differing_observers = profile
        .observers
        .iter()
        .copied()
        .filter(|observer| {
            project_transform_observation_v0(left, *observer)
                != project_transform_observation_v0(right, *observer)
        })
        .collect::<Vec<_>>();
    TransformObservationEquivalenceV0 {
        schema_version: "0",
        product: "omena-transform-cst.observation-indexed-equivalence",
        profile_id: profile.profile_id.clone(),
        compared_observer_count: profile.observers.len(),
        equivalent: differing_observers.is_empty(),
        differing_observers,
    }
}

/// Compare one authority-produced observation projection.
///
/// The caller supplies values computed by the owning parser or semantic
/// authority; this function owns the observation-indexed equality relation.
pub fn compare_transform_observation_projection_values_v0(
    profile_id: impl Into<String>,
    kind: ObservationKindV0,
    left_projection: &str,
    right_projection: &str,
) -> TransformObservationEquivalenceV0 {
    let observer = TransformObserverV0::Contract(kind);
    let equivalent = left_projection == right_projection;
    TransformObservationEquivalenceV0 {
        schema_version: "0",
        product: "omena-transform-cst.observation-indexed-equivalence",
        profile_id: profile_id.into(),
        compared_observer_count: 1,
        differing_observers: if equivalent {
            Vec::new()
        } else {
            vec![observer]
        },
        equivalent,
    }
}

/// Compare exact output bytes through the same observation relation used by
/// committed transform-independence data.
pub fn compare_raw_transform_observation_bytes_v0(
    profile_id: impl Into<String>,
    left: &[u8],
    right: &[u8],
) -> TransformObservationEquivalenceV0 {
    let equivalent = left == right;
    TransformObservationEquivalenceV0 {
        schema_version: "0",
        product: "omena-transform-cst.observation-indexed-equivalence",
        profile_id: profile_id.into(),
        compared_observer_count: 1,
        differing_observers: if equivalent {
            Vec::new()
        } else {
            vec![TransformObserverV0::RawBytes]
        },
        equivalent,
    }
}

pub fn observation_indexed_equivalent_v0(
    profile: &TransformObservationProfileV0,
    left: &TransformObservationOutputV0,
    right: &TransformObservationOutputV0,
) -> bool {
    compare_transform_observation_outputs_v0(profile, left, right).equivalent
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransformObservationMatrixV0 {
    descriptors: Vec<TransformPassDescriptorV0>,
    observation_records: Vec<TransformPassObservationRecordV0>,
}

impl TransformObservationMatrixV0 {
    pub fn descriptors(&self) -> &[TransformPassDescriptorV0] {
        self.descriptors.as_slice()
    }

    pub fn observation_records(&self) -> &[TransformPassObservationRecordV0] {
        self.observation_records.as_slice()
    }

    pub fn unknown_gap_count(&self) -> usize {
        self.observation_records
            .iter()
            .filter(|record| matches!(record.surface, PassObservationSurfaceV0::UnknownGap { .. }))
            .count()
    }
}

/// Extract the observation matrix through its public producers.
///
/// Keeping this call boundary explicit prevents the proof relation from
/// acquiring a second parser for the descriptor source table.
pub fn default_transform_observation_matrix_v0() -> TransformObservationMatrixV0 {
    TransformObservationMatrixV0 {
        descriptors: default_transform_pass_descriptors(),
        observation_records: default_transform_pass_observation_records(),
    }
}

#[cfg(test)]
mod tests {
    use serde::Deserialize;

    use super::*;

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct ObservationTruthTableRowV0 {
        case_id: String,
        pass_id: String,
        observation_kind: String,
        left_projection: String,
        right_projection: String,
        expected_equivalent: bool,
    }

    fn parse_observation_kind(value: &str) -> Option<ObservationKindV0> {
        all_observation_kinds_v0().into_iter().find(|kind| {
            serde_json::to_value(kind)
                .ok()
                .and_then(|value| value.as_str().map(str::to_string))
                .as_deref()
                == Some(value)
        })
    }

    fn output_with_override(
        raw: &str,
        kind: ObservationKindV0,
        value: &str,
    ) -> Result<TransformObservationOutputV0, TransformObservationOutputErrorV0> {
        let projections = all_observation_kinds_v0()
            .into_iter()
            .map(|candidate| {
                let projection = if candidate == kind {
                    value.to_string()
                } else {
                    format!("{candidate:?}:stable")
                };
                TransformObservationProjectionV0::new(candidate, projection)
            })
            .collect();
        TransformObservationOutputV0::new(raw.as_bytes(), projections)
    }

    #[test]
    fn public_matrix_extraction_is_complete_without_table_parsing() {
        let matrix = default_transform_observation_matrix_v0();
        assert_eq!(matrix.descriptors().len(), 44);
        assert_eq!(matrix.observation_records().len(), 44);
        assert_eq!(matrix.unknown_gap_count(), 0);
        assert_eq!(all_observation_kinds_v0().len(), OBSERVATION_KIND_COUNT_V0);
    }

    #[test]
    fn committed_truth_table_covers_every_observation_kind()
    -> Result<(), Box<dyn std::error::Error>> {
        let rows = serde_json::from_str::<Vec<ObservationTruthTableRowV0>>(include_str!(
            "../data/observation-equivalence-truth-table-v0.json"
        ))?;
        let matrix = default_transform_observation_matrix_v0();
        let mut covered = BTreeSet::new();
        for row in rows {
            let kind = parse_observation_kind(row.observation_kind.as_str())
                .ok_or_else(|| format!("unknown observation kind in {}", row.case_id))?;
            let record = matrix
                .observation_records()
                .iter()
                .find(|record| record.id == row.pass_id)
                .ok_or_else(|| format!("unknown pass in {}", row.case_id))?;
            let PassObservationSurfaceV0::Declared(contract) = &record.surface else {
                return Err(format!("unknown observation surface in {}", row.case_id).into());
            };
            assert!(
                contract.observes.contains(&kind) || contract.preserves.contains(&kind),
                "{} does not declare {:?}",
                row.pass_id,
                kind
            );
            let profile = TransformObservationProfileV0::new(
                format!("truth-table:{}", row.case_id),
                vec![TransformObserverV0::Contract(kind)],
            )?;
            let left = output_with_override("left", kind, row.left_projection.as_str())?;
            let right = output_with_override("right", kind, row.right_projection.as_str())?;
            assert_eq!(
                observation_indexed_equivalent_v0(&profile, &left, &right),
                row.expected_equivalent,
                "{}",
                row.case_id
            );
            covered.insert(kind);
        }
        assert_eq!(covered, all_observation_kinds_v0().into_iter().collect());
        Ok(())
    }

    #[test]
    fn declared_projection_relation_distinguishes_semantic_and_raw_values()
    -> Result<(), Box<dyn std::error::Error>> {
        let left = output_with_override(
            ".a { color: red; }",
            ObservationKindV0::SelectorMatching,
            "selector:.a;winner:color=red",
        )?;
        let right = output_with_override(
            ".a{color:red}",
            ObservationKindV0::SelectorMatching,
            "selector:.a;winner:color=red",
        )?;
        let parsed = TransformObservationProfileV0::new(
            "parsed-semantics",
            vec![TransformObserverV0::Contract(
                ObservationKindV0::SelectorMatching,
            )],
        )?;
        let raw =
            TransformObservationProfileV0::new("raw-bytes", vec![TransformObserverV0::RawBytes])?;

        let parsed_equivalent = observation_indexed_equivalent_v0(&parsed, &left, &right);
        let raw_equivalent = observation_indexed_equivalent_v0(&raw, &left, &right);
        println!(
            "declaredOnly=true parsedProfile={} rawProfile={}",
            parsed_equivalent, raw_equivalent
        );
        assert!(parsed_equivalent);
        assert!(!raw_equivalent);
        Ok(())
    }

    #[test]
    fn declared_projection_relation_scopes_a_cascade_winner_change()
    -> Result<(), Box<dyn std::error::Error>> {
        let matrix = default_transform_observation_matrix_v0();
        let whitespace = matrix
            .observation_records()
            .iter()
            .find(|record| record.id == "whitespace-strip")
            .ok_or("whitespace-strip observation record missing")?;
        let PassObservationSurfaceV0::Declared(contract) = &whitespace.surface else {
            return Err("whitespace-strip observation surface is unknown".into());
        };
        assert!(
            contract
                .preserves
                .contains(&ObservationKindV0::CascadeWinner)
        );
        assert!(
            contract
                .preserves
                .contains(&ObservationKindV0::SourceMapTrace)
        );

        let left = output_with_override(
            ".a{color:red}",
            ObservationKindV0::CascadeWinner,
            "winner:red",
        )?;
        let right = output_with_override(
            ".a{color:blue}",
            ObservationKindV0::CascadeWinner,
            "winner:blue",
        )?;
        let cascade = TransformObservationProfileV0::new(
            "cascade-winner",
            vec![TransformObserverV0::Contract(
                ObservationKindV0::CascadeWinner,
            )],
        )?;
        let disjoint = TransformObservationProfileV0::new(
            "source-map-trace",
            vec![TransformObserverV0::Contract(
                ObservationKindV0::SourceMapTrace,
            )],
        )?;

        let cascade_equivalent = observation_indexed_equivalent_v0(&cascade, &left, &right);
        let disjoint_equivalent = observation_indexed_equivalent_v0(&disjoint, &left, &right);
        println!(
            "declaredOnly=true cascadeProfile={} disjointSourceMapProfile={}",
            cascade_equivalent, disjoint_equivalent
        );
        assert!(!cascade_equivalent);
        assert!(disjoint_equivalent);
        Ok(())
    }

    #[test]
    fn target_predicate_is_explicitly_read_only_and_matches_the_matrix_asymmetry() {
        let matrix = default_transform_observation_matrix_v0();
        let mut observed = 0usize;
        let mut preserved = 0usize;
        for record in matrix.observation_records() {
            let PassObservationSurfaceV0::Declared(contract) = &record.surface else {
                continue;
            };
            observed += usize::from(
                contract
                    .observes
                    .contains(&ObservationKindV0::TargetPredicate),
            );
            preserved += usize::from(
                contract
                    .preserves
                    .contains(&ObservationKindV0::TargetPredicate),
            );
        }
        assert_eq!(observed, 13);
        assert_eq!(preserved, 0);
        assert_eq!(
            observation_kind_observer_class_v0(ObservationKindV0::TargetPredicate),
            TransformObserverClassV0::ReadOnlyInput
        );
    }
}