Skip to main content

workshop_rs_cli/
census.rs

1//! Deterministic, sharded census of the canonical Workshop surface.
2//!
3//! The census is derived from this crate's catalog, settings table, and WIR
4//! capabilities. It is tooling that runs contract/regression probes and
5//! produces structured results, not a source-language inventory or a
6//! live-client oracle.
7
8use std::collections::{HashMap, HashSet};
9
10use serde::{Deserialize, Serialize};
11use sha2::{Digest, Sha256};
12
13use crate::conformance::{
14    CONFORMANCE_SCHEMA_VERSION, Comparison, ConformanceReason, ConformanceResult,
15    ConformanceStatus, Equivalence, FeatureId, FeatureKind, FeatureNamespace, ReasonCode,
16    TestArtifact, is_sha256_digest,
17};
18use workshop_rs::catalog::{Catalog, CatalogEntry, EnumDomain, Kind, Locale};
19use workshop_rs::gameplay::{self, HeroId};
20use workshop_rs::settings::{
21    self as settings_schema, SettingDefinition, SettingTarget, SettingTargetKind,
22    SettingValueDomain,
23};
24use workshop_rs::{WorkshopError, convert, emitter, parser, roundtrip};
25
26#[derive(Clone, Copy)]
27enum CensusCapabilityKind {
28    Variable,
29    PlayerVariable,
30    Subroutine,
31    ControlFlow,
32    String,
33}
34
35struct CensusCapability {
36    kind: CensusCapabilityKind,
37    name: &'static str,
38}
39
40const CENSUS_CAPABILITIES: &[CensusCapability] = &[
41    CensusCapability {
42        kind: CensusCapabilityKind::Variable,
43        name: "global",
44    },
45    CensusCapability {
46        kind: CensusCapabilityKind::PlayerVariable,
47        name: "player",
48    },
49    CensusCapability {
50        kind: CensusCapabilityKind::Subroutine,
51        name: "declaration-and-call",
52    },
53    CensusCapability {
54        kind: CensusCapabilityKind::ControlFlow,
55        name: "if",
56    },
57    CensusCapability {
58        kind: CensusCapabilityKind::ControlFlow,
59        name: "else-if",
60    },
61    CensusCapability {
62        kind: CensusCapabilityKind::ControlFlow,
63        name: "else",
64    },
65    CensusCapability {
66        kind: CensusCapabilityKind::ControlFlow,
67        name: "while",
68    },
69    CensusCapability {
70        kind: CensusCapabilityKind::ControlFlow,
71        name: "for-global-variable",
72    },
73    CensusCapability {
74        kind: CensusCapabilityKind::String,
75        name: "custom-string",
76    },
77];
78
79pub const CENSUS_SCHEMA_VERSION: u32 = 1;
80pub const CENSUS_IDENTITY_SCHEMA_VERSION: u32 = 1;
81const EN_US: &str = "en-US";
82const ZH_CN: &str = "zh-CN";
83const LOCALIZATION_EN_US_SOURCE: &str = r#"rule ("Localization") {
84    event {
85        Ongoing - Global;
86    }
87    actions {
88        Disable Inspector Recording;
89    }
90}
91"#;
92const LOCALIZATION_ZH_CN_SOURCE: &str = r#"rule ("Localization") {
93    event {
94        持续 - 全局;
95    }
96    actions {
97        禁用查看器录制;
98    }
99}
100"#;
101
102/// An explicit support classification for a census case.
103#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
104#[serde(tag = "kind", rename_all = "kebab-case")]
105pub enum CensusSupport {
106    Exercise,
107    Unsupported { detail: String },
108    KnownGap { detail: String },
109    Inconclusive { detail: String },
110}
111
112/// One deterministic case with explicit source-locale provenance.
113#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
114pub struct CensusCase {
115    pub case_id: String,
116    pub features: Vec<FeatureId>,
117    /// Locale of `source`; a zh-CN case is a real input, not an
118    /// implementation-generated conversion.
119    #[serde(default = "default_source_locale")]
120    pub source_locale: String,
121    pub source: String,
122    /// Independently recorded source text, when this case has an offline
123    /// expectation. None means the case remains inconclusive offline.
124    #[serde(default, skip_serializing_if = "Option::is_none")]
125    pub reference_source: Option<String>,
126    pub support: CensusSupport,
127}
128
129/// A named collection of independently attributable cases.
130#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
131pub struct CensusShard {
132    pub shard_id: String,
133    pub cases: Vec<CensusCase>,
134}
135
136impl CensusShard {
137    pub fn new(
138        shard_id: impl Into<String>,
139        mut cases: Vec<CensusCase>,
140    ) -> Result<Self, CensusError> {
141        let shard_id = shard_id.into();
142        validate_name("shard_id", &shard_id)?;
143        cases.sort_by(|left, right| left.case_id.cmp(&right.case_id));
144        for case in &cases {
145            case.validate()?;
146        }
147        if cases
148            .windows(2)
149            .any(|pair| pair[0].case_id == pair[1].case_id)
150        {
151            return Err(CensusError::new(format!(
152                "shard '{shard_id}' contains duplicate case IDs"
153            )));
154        }
155        Ok(Self { shard_id, cases })
156    }
157}
158
159impl CensusCase {
160    fn probe(case_id: impl Into<String>, features: Vec<FeatureId>, source: String) -> Self {
161        Self {
162            case_id: case_id.into(),
163            features,
164            source_locale: EN_US.to_string(),
165            source,
166            reference_source: None,
167            support: generated_probe_support(),
168        }
169    }
170
171    fn exercise(mut self) -> Self {
172        self.support = CensusSupport::Exercise;
173        self
174    }
175
176    fn locale(mut self, locale: &str) -> Self {
177        self.source_locale = locale.to_string();
178        self
179    }
180
181    #[cfg(test)]
182    fn with_support(mut self, support: CensusSupport) -> Self {
183        self.support = support;
184        self
185    }
186
187    fn validate(&self) -> Result<(), CensusError> {
188        validate_name("case_id", &self.case_id)?;
189        if self.features.is_empty() {
190            return Err(CensusError::new(format!(
191                "case '{}' has no feature IDs",
192                self.case_id
193            )));
194        }
195        if self.source.trim().is_empty() {
196            return Err(CensusError::new(format!(
197                "case '{}' has no source",
198                self.case_id
199            )));
200        }
201        validate_name("source_locale", &self.source_locale)?;
202        if self
203            .reference_source
204            .as_deref()
205            .is_some_and(|source| source.trim().is_empty())
206        {
207            return Err(CensusError::new(format!(
208                "case '{}' has an empty reference source",
209                self.case_id
210            )));
211        }
212        let mut features = HashSet::new();
213        if self
214            .features
215            .iter()
216            .any(|feature| !features.insert(feature))
217        {
218            return Err(CensusError::new(format!(
219                "case '{}' contains duplicate feature IDs",
220                self.case_id
221            )));
222        }
223        Ok(())
224    }
225}
226
227/// The complete census assembled from deterministic shards.
228#[derive(Debug, Clone, PartialEq, Eq)]
229pub struct Census {
230    shards: Vec<CensusShard>,
231}
232
233/// The stable identity of a reviewed census definition.
234#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
235#[serde(rename_all = "camelCase")]
236pub struct CensusIdentity {
237    pub schema_version: u32,
238    pub digest: String,
239    pub shards: Vec<String>,
240}
241
242impl Census {
243    /// Assemble shards in stable shard and case order.
244    pub fn assemble(mut shards: Vec<CensusShard>) -> Result<Self, CensusError> {
245        shards.sort_by(|left, right| left.shard_id.cmp(&right.shard_id));
246        let mut shard_ids = HashSet::new();
247        let mut case_ids = HashSet::new();
248        for shard in &shards {
249            if !shard_ids.insert(shard.shard_id.clone()) {
250                return Err(CensusError::new(format!(
251                    "duplicate census shard '{}'",
252                    shard.shard_id
253                )));
254            }
255            for case in &shard.cases {
256                if !case_ids.insert(case.case_id.clone()) {
257                    return Err(CensusError::new(format!(
258                        "duplicate census case '{}'",
259                        case.case_id
260                    )));
261                }
262            }
263        }
264        Ok(Self { shards })
265    }
266
267    /// Derive the current surface from the canonical catalog, settings table,
268    /// and WIR capability names owned by this crate.
269    pub fn builtin(catalog: &Catalog) -> Result<Self, CensusError> {
270        Self::assemble(vec![
271            catalog_shard(catalog, Kind::Event, "catalog-events")?,
272            catalog_shard(catalog, Kind::Action, "catalog-actions")?,
273            catalog_shard(catalog, Kind::Value, "catalog-values")?,
274            catalog_shard(catalog, Kind::Operator, "catalog-operators")?,
275            catalog_shard(catalog, Kind::Structural, "catalog-structural")?,
276            enum_shard(catalog)?,
277            settings_shard(catalog)?,
278            wir_shard()?,
279            localization_shard(catalog)?,
280            content_id_shard(catalog)?,
281        ])
282    }
283
284    pub fn shards(&self) -> &[CensusShard] {
285        &self.shards
286    }
287
288    pub fn cases(&self) -> impl Iterator<Item = &CensusCase> {
289        self.shards.iter().flat_map(|shard| shard.cases.iter())
290    }
291
292    /// Execute all cases. No result state is dropped or converted to success.
293    /// Cases are independent and the report sorts results by case ID, so the
294    /// run parallelizes over available cores without changing the output.
295    pub fn run(&self, catalog: &Catalog) -> CensusReport {
296        let cases: Vec<(&str, &CensusCase)> = self
297            .shards
298            .iter()
299            .flat_map(|shard| {
300                shard
301                    .cases
302                    .iter()
303                    .map(move |case| (shard.shard_id.as_str(), case))
304            })
305            .collect();
306        let workers = std::thread::available_parallelism()
307            .map(std::num::NonZero::get)
308            .unwrap_or(1)
309            .min(cases.len().max(1));
310        let results: Vec<ConformanceResult> = if workers <= 1 {
311            cases
312                .iter()
313                .map(|(shard_id, case)| run_case(case, shard_id, catalog))
314                .collect()
315        } else {
316            let next = std::sync::atomic::AtomicUsize::new(0);
317            std::thread::scope(|scope| {
318                let handles: Vec<_> = (0..workers)
319                    .map(|_| {
320                        scope.spawn(|| {
321                            let mut worker_results = Vec::new();
322                            loop {
323                                let index = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
324                                let Some((shard_id, case)) = cases.get(index) else {
325                                    break;
326                                };
327                                worker_results.push(run_case(case, shard_id, catalog));
328                            }
329                            worker_results
330                        })
331                    })
332                    .collect();
333                handles
334                    .into_iter()
335                    .flat_map(|handle| handle.join().expect("census worker panicked"))
336                    .collect()
337            })
338        };
339        let mut results = results;
340        results.sort_by(|left, right| left.case_id.cmp(&right.case_id));
341        CensusReport {
342            schema_version: CENSUS_SCHEMA_VERSION,
343            conformance_schema_version: CONFORMANCE_SCHEMA_VERSION,
344            catalog: catalog.identity(),
345            census: self.identity(),
346            results,
347        }
348    }
349
350    /// Return the deterministic identity of this census definition.
351    pub fn identity(&self) -> CensusIdentity {
352        let definition = self
353            .export_json()
354            .expect("census definitions must remain serializable");
355        CensusIdentity {
356            schema_version: CENSUS_IDENTITY_SCHEMA_VERSION,
357            digest: sha256(&definition),
358            shards: self
359                .shards
360                .iter()
361                .map(|shard| shard.shard_id.clone())
362                .collect(),
363        }
364    }
365
366    /// Export shard definitions without executing them.
367    pub fn export_json(&self) -> Result<String, CensusError> {
368        serde_json::to_string_pretty(&self.shards)
369            .map_err(|error| CensusError::new(format!("cannot serialize census shards: {error}")))
370    }
371}
372
373/// Machine-readable output from a census run.
374#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
375#[serde(rename_all = "camelCase")]
376pub struct CensusReport {
377    pub schema_version: u32,
378    pub conformance_schema_version: u32,
379    pub catalog: workshop_rs::catalog::CatalogIdentity,
380    pub census: CensusIdentity,
381    pub results: Vec<ConformanceResult>,
382}
383
384impl CensusReport {
385    pub fn validate(&self) -> Result<(), CensusError> {
386        let catalog =
387            Catalog::builtin().map_err(|error| CensusError::new(format!("catalog: {error}")))?;
388        self.validate_against(&catalog)
389    }
390
391    pub fn validate_against(&self, catalog: &Catalog) -> Result<(), CensusError> {
392        if self.schema_version != CENSUS_SCHEMA_VERSION {
393            return Err(CensusError::new("unsupported census schema version"));
394        }
395        if self.conformance_schema_version != CONFORMANCE_SCHEMA_VERSION {
396            return Err(CensusError::new("unsupported conformance schema version"));
397        }
398        if self.catalog != catalog.identity() {
399            return Err(CensusError::new(
400                "report catalog identity does not match the loaded catalog",
401            ));
402        }
403        if self.census.schema_version != CENSUS_IDENTITY_SCHEMA_VERSION {
404            return Err(CensusError::new(
405                "unsupported census identity schema version",
406            ));
407        }
408        if !is_sha256_digest(&self.census.digest) {
409            return Err(CensusError::new(
410                "census identity digest must be a SHA-256 hex digest",
411            ));
412        }
413        if self.census.shards.is_empty()
414            || self.census.shards.windows(2).any(|pair| pair[0] >= pair[1])
415        {
416            return Err(CensusError::new(
417                "report shards must be non-empty and strictly sorted",
418            ));
419        }
420        for result in &self.results {
421            result
422                .validate_against(catalog)
423                .map_err(|error| CensusError::new(error.to_string()))?;
424            let matching_shards = self
425                .census
426                .shards
427                .iter()
428                .filter(|shard| {
429                    result
430                        .case_id
431                        .strip_prefix(shard.as_str())
432                        .is_some_and(|rest| rest.starts_with('/'))
433                })
434                .count();
435            if matching_shards != 1 {
436                return Err(CensusError::new(format!(
437                    "result '{}' does not map to exactly one census shard",
438                    result.case_id
439                )));
440            }
441        }
442        Ok(())
443    }
444
445    pub fn to_json(&self) -> Result<String, CensusError> {
446        self.validate()?;
447        serde_json::to_string_pretty(self)
448            .map_err(|error| CensusError::new(format!("cannot serialize census report: {error}")))
449    }
450}
451
452#[derive(Debug, Clone, PartialEq, Eq)]
453pub struct CensusError {
454    pub message: String,
455}
456
457impl CensusError {
458    fn new(message: impl Into<String>) -> Self {
459        Self {
460            message: message.into(),
461        }
462    }
463}
464
465impl std::fmt::Display for CensusError {
466    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
467        formatter.write_str(&self.message)
468    }
469}
470
471impl std::error::Error for CensusError {}
472
473fn validate_name(field: &str, value: &str) -> Result<(), CensusError> {
474    if value.trim().is_empty() || value.chars().any(char::is_control) {
475        Err(CensusError::new(format!(
476            "{field} must be non-empty and printable"
477        )))
478    } else {
479        Ok(())
480    }
481}
482
483fn default_source_locale() -> String {
484    EN_US.to_string()
485}
486
487fn feature(namespace: FeatureNamespace, kind: FeatureKind, name: impl Into<String>) -> FeatureId {
488    FeatureId::new(namespace, kind, name).expect("canonical census feature ID")
489}
490
491fn catalog_feature(kind: Kind, id: &str) -> FeatureId {
492    FeatureId::from_catalog(kind, id).expect("catalog IDs are validated by Catalog::load")
493}
494
495fn catalog_shard(
496    catalog: &Catalog,
497    kind: Kind,
498    shard_id: &str,
499) -> Result<CensusShard, CensusError> {
500    let cases = catalog
501        .entries_of(kind)
502        .map(|entry| {
503            let source = match kind {
504                Kind::Event => event_probe(catalog, entry),
505                Kind::Action => action_probe(catalog, entry),
506                Kind::Value => value_probe(catalog, entry),
507                Kind::Operator => operator_probe(catalog, entry),
508                Kind::Structural => structural_probe(catalog, entry),
509                Kind::Setting => unreachable!("settings use the settings table"),
510                Kind::Enum => unreachable!("enum domains use the enum shard"),
511            };
512            CensusCase::probe(
513                format!("{shard_id}/{}", entry.id),
514                vec![catalog_feature(kind, &entry.id)],
515                source,
516            )
517        })
518        .collect();
519    CensusShard::new(shard_id, cases)
520}
521
522fn enum_shard(catalog: &Catalog) -> Result<CensusShard, CensusError> {
523    enum_member_cases(catalog, "catalog-enums", false, true)
524}
525
526fn content_id_shard(catalog: &Catalog) -> Result<CensusShard, CensusError> {
527    enum_member_cases(catalog, "content-ids", true, false)
528}
529
530fn enum_member_cases(
531    catalog: &Catalog,
532    shard_id: &str,
533    content_only: bool,
534    domain_feature: bool,
535) -> Result<CensusShard, CensusError> {
536    let mut cases = Vec::new();
537    for domain in catalog.enum_domains() {
538        if content_only && !matches!(domain.domain.as_str(), "Hero" | "Map") {
539            continue;
540        }
541        for member in &domain.members {
542            let mut features = Vec::with_capacity(2);
543            if domain_feature {
544                features.push(catalog_feature(Kind::Enum, &domain.domain));
545            }
546            features.push(
547                FeatureId::from_enum_member(&domain.domain, &member.member)
548                    .expect("canonical enum member ID"),
549            );
550            cases.push(CensusCase::probe(
551                format!("{shard_id}/{}/{}", domain.domain, member.member),
552                features,
553                enum_probe(catalog, domain, &member.member),
554            ));
555        }
556    }
557    CensusShard::new(shard_id, cases)
558}
559fn settings_shard(catalog: &Catalog) -> Result<CensusShard, CensusError> {
560    let cases = settings_schema::definitions()
561        .map(|definition| {
562            let path = definition.path().to_string();
563            CensusCase::probe(
564                format!("settings/{path}"),
565                vec![feature(
566                    FeatureNamespace::Settings,
567                    FeatureKind::Setting,
568                    path,
569                )],
570                settings_probe(&definition, catalog),
571            )
572        })
573        .collect();
574    CensusShard::new("settings", cases)
575}
576
577fn wir_shard() -> Result<CensusShard, CensusError> {
578    let cases = CENSUS_CAPABILITIES
579        .iter()
580        .map(|capability| match capability.kind {
581            CensusCapabilityKind::Variable => wir_case(
582                "variables-global",
583                FeatureKind::Variable,
584                capability.name,
585                variables_source(),
586            ),
587            CensusCapabilityKind::PlayerVariable => CensusCase::probe(
588                "wir/variables-player".to_string(),
589                vec![feature(
590                    FeatureNamespace::Wir,
591                    FeatureKind::Variable,
592                    capability.name,
593                )],
594                player_variable_source(),
595            ),
596            CensusCapabilityKind::Subroutine => wir_case(
597                "subroutine",
598                FeatureKind::Subroutine,
599                capability.name,
600                subroutine_source(),
601            ),
602            CensusCapabilityKind::ControlFlow => {
603                let actions = match capability.name {
604                    "if" => "If(True);\n    Wait(0);\nEnd;",
605                    "else-if" => "If(True);\n    Wait(0);\nElse If(False);\n    Wait(0);\nEnd;",
606                    "else" => "If(True);\n    Wait(0);\nElse;\n    Wait(0);\nEnd;",
607                    "while" => "While(True);\n    Wait(0);\nEnd;",
608                    "for-global-variable" => {
609                        "For Global Variable(probe, 0, 1, 1);\n    Wait(0);\nEnd;"
610                    }
611                    _ => unreachable!("unknown WIR control-flow census capability"),
612                };
613                control_flow_case(capability.name, actions)
614            }
615            CensusCapabilityKind::String => CensusCase::probe(
616                "wir/string/custom-string".to_string(),
617                vec![feature(
618                    FeatureNamespace::Wir,
619                    FeatureKind::String,
620                    capability.name,
621                )],
622                rule_source(
623                    "String",
624                    "Set Global Variable(probe, Custom String(\"census\"));",
625                ),
626            )
627            .exercise(),
628        })
629        .collect();
630    CensusShard::new("wir", cases)
631}
632
633fn localization_shard(catalog: &Catalog) -> Result<CensusShard, CensusError> {
634    let mut cases = vec![
635        CensusCase::probe(
636            "localization/en-us-to-zh-cn".to_string(),
637            vec![feature(
638                FeatureNamespace::Localization,
639                FeatureKind::Localization,
640                "en-us-to-zh-cn",
641            )],
642            LOCALIZATION_EN_US_SOURCE.to_string(),
643        ),
644        CensusCase::probe(
645            "localization/zh-cn-to-en-us".to_string(),
646            vec![feature(
647                FeatureNamespace::Localization,
648                FeatureKind::Localization,
649                "zh-cn-to-en-us",
650            )],
651            LOCALIZATION_ZH_CN_SOURCE.to_string(),
652        )
653        .locale(ZH_CN),
654    ];
655    // #448: every spelling the catalog declares for a locale gets a case
656    // written in that spelling, so an alias the frontend cannot represent
657    // shows up as an explicit result instead of never being exercised.
658    let settings_index = SettingsSpellings::build();
659    for locale in catalog.locales() {
660        for kind in [
661            Kind::Event,
662            Kind::Action,
663            Kind::Value,
664            Kind::Operator,
665            Kind::Structural,
666        ] {
667            for entry in catalog.entries_of(kind) {
668                for (index, spelling) in entry.spellings(locale).iter().enumerate() {
669                    cases.push(
670                        CensusCase::probe(
671                            format!(
672                                "localization/{}/{}/{}/{}",
673                                locale.as_str(),
674                                kind.as_str(),
675                                entry.id,
676                                index
677                            ),
678                            vec![catalog_feature(kind, &entry.id)],
679                            localized_probe(catalog, locale, kind, entry, spelling),
680                        )
681                        .locale(locale.as_str()),
682                    );
683                }
684            }
685        }
686        for domain in catalog.enum_domains() {
687            let domain_spelling = CONSTRUCTOR_DOMAINS
688                .contains(&domain.domain.as_str())
689                .then(|| domain.spelling(locale))
690                .flatten();
691            if let Some(spelling) = domain_spelling {
692                let Some(member) = domain.members.first() else {
693                    continue;
694                };
695                let member_spelling =
696                    localized_member(catalog, &domain.domain, &member.member, locale);
697                cases.push(
698                    CensusCase::probe(
699                        format!(
700                            "localization/{}/enum-domain/{}",
701                            locale.as_str(),
702                            domain.domain
703                        ),
704                        vec![catalog_feature(Kind::Enum, &domain.domain)],
705                        global_probe(catalog, locale, &format!("{spelling}({member_spelling})")),
706                    )
707                    .locale(locale.as_str()),
708                );
709            }
710            for member in &domain.members {
711                for (index, spelling) in member.spellings(locale).iter().enumerate() {
712                    cases.push(
713                        CensusCase::probe(
714                            format!(
715                                "localization/{}/enum/{}/{}/{}",
716                                locale.as_str(),
717                                domain.domain,
718                                member.member,
719                                index
720                            ),
721                            vec![
722                                FeatureId::from_enum_member(&domain.domain, &member.member)
723                                    .expect("canonical enum member ID"),
724                            ],
725                            member_probe(catalog, locale, domain, spelling),
726                        )
727                        .locale(locale.as_str()),
728                    );
729                }
730            }
731        }
732        for entry in catalog.localized_strings() {
733            for (index, spelling) in entry.spellings(locale).iter().enumerate() {
734                let string_call = catalog
735                    .spelling(Kind::Value, locale, "string")
736                    .or_else(|| catalog.spelling(Kind::Value, &Locale::new(EN_US), "string"))
737                    .unwrap_or("string");
738                cases.push(
739                    CensusCase::probe(
740                        format!(
741                            "localization/{}/localized-string/{}/{}",
742                            locale.as_str(),
743                            entry.id,
744                            index
745                        ),
746                        vec![feature(
747                            FeatureNamespace::Localization,
748                            FeatureKind::String,
749                            entry.id.clone(),
750                        )],
751                        global_probe(catalog, locale, &format!("{string_call}({spelling})")),
752                    )
753                    .locale(locale.as_str()),
754                );
755            }
756        }
757        for (row, name) in settings_schema::localized_setting_names()
758            .iter()
759            .enumerate()
760        {
761            if !name.locale.eq_ignore_ascii_case(locale.as_str()) {
762                continue;
763            }
764            let case_id = format!(
765                "localization/{}/settings/{}/{}",
766                locale.as_str(),
767                name.section,
768                row
769            );
770            let feature_id = feature(
771                FeatureNamespace::Settings,
772                FeatureKind::Setting,
773                format!("{}/{}", name.section, slug(name.english)),
774            );
775            match localized_settings_probe(catalog, &settings_index, name, locale) {
776                Ok(source) => cases.push(
777                    CensusCase::probe(case_id, vec![feature_id], source).locale(locale.as_str()),
778                ),
779                Err(detail) => cases.push(
780                    CensusCase {
781                        case_id,
782                        features: vec![feature_id],
783                        source_locale: locale.as_str().to_string(),
784                        source: "settings {\n}".to_string(),
785                        reference_source: None,
786                        support: CensusSupport::KnownGap {
787                            detail: format!(
788                                "settings {} {:?} for {}: {detail}",
789                                name.section, name.spelling, name.english
790                            ),
791                        },
792                    }
793                    .locale(locale.as_str()),
794                ),
795            }
796        }
797    }
798    CensusShard::new("localization", cases)
799}
800
801const CONSTRUCTOR_DOMAINS: &[&str] = &["Hero", "Button", "Color", "Map"];
802
803fn slug(name: &str) -> String {
804    name.chars()
805        .map(|character| {
806            if character.is_whitespace() || character == '/' {
807                '-'
808            } else {
809                character
810            }
811        })
812        .collect()
813}
814
815fn localized_spelling(catalog: &Catalog, kind: Kind, locale: &Locale, id: &str) -> String {
816    catalog
817        .spelling(kind, locale, id)
818        .or_else(|| catalog.spelling(kind, &Locale::new(EN_US), id))
819        .unwrap_or(id)
820        .to_string()
821}
822
823fn localized_member(catalog: &Catalog, domain: &str, member: &str, locale: &Locale) -> String {
824    catalog
825        .enum_spelling(domain, locale, member)
826        .or_else(|| catalog.enum_spelling(domain, &Locale::new(EN_US), member))
827        .unwrap_or(member)
828        .to_string()
829}
830
831fn probe_filler(catalog: &Catalog, locale: &Locale, entry: &CatalogEntry, index: usize) -> String {
832    if let Some(domain) = entry.param_domain(index) {
833        let member = catalog
834            .enum_domain(domain)
835            .and_then(|domain| domain.members.first())
836            .map(|member| member.member.as_str())
837            .unwrap_or("ALL");
838        let spelling = localized_member(catalog, domain, member, locale);
839        if CONSTRUCTOR_DOMAINS.contains(&domain) {
840            let domain_spelling = catalog
841                .enum_domain(domain)
842                .and_then(|domain| domain.spelling(locale))
843                .unwrap_or(domain);
844            return format!("{domain_spelling}({spelling})");
845        }
846        return spelling;
847    }
848    if let Some(default) = entry.param_default(index) {
849        return param_default_filler(catalog, locale, default);
850    }
851    match entry.param_type(index) {
852        Some("Variable") => "probe".to_string(),
853        Some("Subroutine") | Some("Subroutines") => "probe".to_string(),
854        Some("Bool") => localized_spelling(catalog, Kind::Value, locale, "true"),
855        // `Hero`-typed params take the member spelling bare; the enclosing
856        // `Hero(...)` call is the value under test or the entry's own name.
857        Some("Hero") => {
858            let member = catalog
859                .enum_domain("Hero")
860                .and_then(|domain| domain.members.first())
861                .map(|member| member.member.as_str())
862                .unwrap_or("ANA");
863            localized_member(catalog, "Hero", member, locale)
864        }
865        // String params emit canonically as `Custom String("...")`, so the
866        // probe writes that form instead of a bare literal (#448).
867        Some("String") => {
868            let custom_string = localized_spelling(catalog, Kind::Value, locale, "customString");
869            format!("{custom_string}(\"x\")")
870        }
871        Some(kind) if kind.contains("Player") || kind.contains("EntityId") => {
872            localized_spelling(catalog, Kind::Value, locale, "eventPlayer")
873        }
874        _ => "0".to_string(),
875    }
876}
877
878fn param_default_filler(catalog: &Catalog, locale: &Locale, default: &str) -> String {
879    if let Some((domain, member)) = default.split_once('.') {
880        let spelling = localized_member(catalog, domain, member, locale);
881        if CONSTRUCTOR_DOMAINS.contains(&domain) {
882            let domain_spelling = catalog
883                .enum_domain(domain)
884                .and_then(|domain| domain.spelling(locale))
885                .unwrap_or(domain);
886            return format!("{domain_spelling}({spelling})");
887        }
888        return spelling;
889    }
890    if catalog.entry(Kind::Value, default).is_some() {
891        return localized_spelling(catalog, Kind::Value, locale, default);
892    }
893    if let Some(spelling) = catalog.localized_string_spelling(locale, default) {
894        return spelling.to_string();
895    }
896    default.to_string()
897}
898
899fn probe_args(catalog: &Catalog, locale: &Locale, entry: &CatalogEntry) -> String {
900    (0..entry.param_count())
901        .map(|index| probe_filler(catalog, locale, entry, index))
902        .collect::<Vec<_>>()
903        .join(", ")
904}
905
906/// A probe rule written fully in `locale` (#448): every fixed token —
907/// section keywords, the header event, `Wait`, `If`, `End` — carries that
908/// locale's declared spelling so a cross-locale round trip compares the
909/// tested spelling rather than fallback noise in the boilerplate.
910fn probe_rule(catalog: &Catalog, locale: &Locale, actions: &str) -> String {
911    let variables = localized_spelling(catalog, Kind::Structural, locale, "variables");
912    let global = localized_spelling(catalog, Kind::Structural, locale, "global");
913    let player = localized_spelling(catalog, Kind::Structural, locale, "player");
914    let subroutines = localized_spelling(catalog, Kind::Structural, locale, "subroutines");
915    let rule = localized_spelling(catalog, Kind::Structural, locale, "rule");
916    let event = localized_spelling(catalog, Kind::Structural, locale, "event");
917    let ongoing_global = localized_spelling(catalog, Kind::Event, locale, "global");
918    let actions_section = localized_spelling(catalog, Kind::Structural, locale, "actions");
919    format!(
920        "{variables} {{\n    {global}:\n        0: probe\n    {player}:\n        0: probe\n}}\n\n{subroutines} {{\n    0: probe\n}}\n\n{rule} (\"probe\") {{\n    {event} {{\n        {ongoing_global};\n    }}\n    {actions_section} {{\n        {actions}\n    }}\n}}\n"
921    )
922}
923
924fn global_probe(catalog: &Catalog, locale: &Locale, operand: &str) -> String {
925    let set_global = localized_spelling(catalog, Kind::Structural, locale, "setGlobalVariable");
926    probe_rule(catalog, locale, &format!("{set_global}(probe, {operand});"))
927}
928
929fn localized_probe(
930    catalog: &Catalog,
931    locale: &Locale,
932    kind: Kind,
933    entry: &CatalogEntry,
934    spelling: &str,
935) -> String {
936    match kind {
937        Kind::Event => {
938            let extra = match entry.id.as_str() {
939                "global" => String::new(),
940                "subroutine" => "        probe;\n".to_string(),
941                _ => format!(
942                    "        {};\n        {};\n",
943                    localized_member(catalog, "EventTeam", "ALL", locale),
944                    localized_member(catalog, "EventPlayer", "ALL", locale),
945                ),
946            };
947            let subroutines = localized_spelling(catalog, Kind::Structural, locale, "subroutines");
948            let rule = localized_spelling(catalog, Kind::Structural, locale, "rule");
949            let event = localized_spelling(catalog, Kind::Structural, locale, "event");
950            let actions = localized_spelling(catalog, Kind::Structural, locale, "actions");
951            let wait = localized_spelling(catalog, Kind::Action, locale, "wait");
952            format!(
953                "{subroutines} {{\n    0: probe\n}}\n\n{rule} (\"e\") {{\n    {event} {{\n        {spelling};\n{extra}    }}\n    {actions} {{\n        {wait};\n    }}\n}}\n"
954            )
955        }
956        Kind::Action => {
957            let call = if entry.param_count() == 0 {
958                format!("{spelling};")
959            } else {
960                format!("{spelling}({});", probe_args(catalog, locale, entry))
961            };
962            // `For * Variable` actions open a block that needs `End`.
963            let call = if entry.id.starts_with("__for") {
964                let wait = localized_spelling(catalog, Kind::Action, locale, "wait");
965                let end = localized_spelling(catalog, Kind::Structural, locale, "end");
966                format!("{call}\n        {wait}(0);\n    {end};")
967            } else {
968                call
969            };
970            probe_rule(catalog, locale, &call)
971        }
972        Kind::Value => {
973            let operand = match entry.id.as_str() {
974                "string" => {
975                    let hello = catalog
976                        .localized_string_spelling(locale, "hello")
977                        .or_else(|| catalog.localized_string_spelling(&Locale::new(EN_US), "hello"))
978                        .unwrap_or("Hello");
979                    format!("{spelling}({hello})")
980                }
981                "compare" => {
982                    let eq = localized_spelling(catalog, Kind::Operator, locale, "==");
983                    format!("{spelling}(1, {eq}, 1)")
984                }
985                _ if entry.param_count() == 0 => spelling.to_string(),
986                _ => format!("{spelling}({})", probe_args(catalog, locale, entry)),
987            };
988            global_probe(catalog, locale, &operand)
989        }
990        Kind::Operator => {
991            if matches!(entry.id.as_str(), "==" | "!=" | "<" | "<=" | ">" | ">=") {
992                let if_ = localized_spelling(catalog, Kind::Structural, locale, "if");
993                let wait = localized_spelling(catalog, Kind::Action, locale, "wait");
994                let end = localized_spelling(catalog, Kind::Structural, locale, "end");
995                probe_rule(
996                    catalog,
997                    locale,
998                    &format!("{if_}(1 {spelling} 1);\n        {wait}(0);\n    {end};"),
999                )
1000            } else {
1001                let modify =
1002                    localized_spelling(catalog, Kind::Structural, locale, "modifyGlobalVariable");
1003                probe_rule(catalog, locale, &format!("{modify}(probe, {spelling}, 1);"))
1004            }
1005        }
1006        Kind::Structural => structural_spelling_probe(catalog, locale, &entry.id, spelling),
1007        _ => unreachable!("settings and enum spellings use their own probes"),
1008    }
1009}
1010
1011fn structural_spelling_probe(
1012    catalog: &Catalog,
1013    locale: &Locale,
1014    id: &str,
1015    spelling: &str,
1016) -> String {
1017    let kw = |name: &str| localized_spelling(catalog, Kind::Structural, locale, name);
1018    let wait = localized_spelling(catalog, Kind::Action, locale, "wait");
1019    let true_ = localized_spelling(catalog, Kind::Value, locale, "true");
1020    let ongoing_global = localized_spelling(catalog, Kind::Event, locale, "global");
1021    let event_player = localized_spelling(catalog, Kind::Value, locale, "eventPlayer");
1022    let end = kw("end");
1023    let block = |head: &str| format!("{head}\n        {wait}(0);\n    {end};");
1024    match id {
1025        "settings" => format!("{spelling} {{\n}}\n"),
1026        "workshop" => format!("{} {{\n    {spelling} {{\n    }}\n}}\n", kw("settings")),
1027        "variables" => format!(
1028            "{spelling} {{\n    {}:\n        0: probe\n}}\n",
1029            kw("global")
1030        ),
1031        "global" | "player" => {
1032            format!(
1033                "{} {{\n    {spelling}:\n        0: probe\n}}\n",
1034                kw("variables")
1035            )
1036        }
1037        "subroutines" => format!("{spelling} {{\n    0: probe\n}}\n"),
1038        "rule" => format!(
1039            "{spelling} (\"r\") {{\n    {} {{\n        {ongoing_global};\n    }}\n    {} {{\n        {wait};\n    }}\n}}\n",
1040            kw("event"),
1041            kw("actions"),
1042        ),
1043        "event" => format!(
1044            "{} (\"r\") {{\n    {spelling} {{\n        {ongoing_global};\n    }}\n    {} {{\n        {wait};\n    }}\n}}\n",
1045            kw("rule"),
1046            kw("actions"),
1047        ),
1048        "conditions" => {
1049            // Conditions emit canonically in `a == b` comparison form.
1050            let equals = localized_spelling(catalog, Kind::Operator, locale, "==");
1051            format!(
1052                "{} (\"c\") {{\n    {} {{\n        {ongoing_global};\n    }}\n    {spelling} {{\n        {true_} {equals} {true_};\n    }}\n    {} {{\n        {wait};\n    }}\n}}\n",
1053                kw("rule"),
1054                kw("event"),
1055                kw("actions"),
1056            )
1057        }
1058        "actions" => format!(
1059            "{} (\"r\") {{\n    {} {{\n        {ongoing_global};\n    }}\n    {spelling} {{\n        {wait};\n    }}\n}}\n",
1060            kw("rule"),
1061            kw("event"),
1062        ),
1063        "disabled" => probe_rule(catalog, locale, &format!("{spelling} {wait}(0);")),
1064        "if" => probe_rule(catalog, locale, &block(&format!("{spelling}({true_});"))),
1065        "elseIf" => {
1066            let if_ = kw("if");
1067            probe_rule(
1068                catalog,
1069                locale,
1070                &format!(
1071                    "{if_}({true_});\n        {wait}(0);\n    {spelling}({true_});\n        {wait}(0);\n    {end};"
1072                ),
1073            )
1074        }
1075        "else" => {
1076            let if_ = kw("if");
1077            probe_rule(
1078                catalog,
1079                locale,
1080                &format!(
1081                    "{if_}({true_});\n        {wait}(0);\n    {spelling};\n        {wait}(0);\n    {end};"
1082                ),
1083            )
1084        }
1085        "end" => {
1086            let if_ = kw("if");
1087            probe_rule(
1088                catalog,
1089                locale,
1090                &format!("{if_}({true_});\n        {wait}(0);\n    {spelling};"),
1091            )
1092        }
1093        "while" => probe_rule(catalog, locale, &block(&format!("{spelling}({true_});"))),
1094        "forGlobalVariable" => probe_rule(
1095            catalog,
1096            locale,
1097            &block(&format!("{spelling}(probe, 0, 1, 1);")),
1098        ),
1099        "forPlayerVariable" => probe_rule(
1100            catalog,
1101            locale,
1102            &block(&format!("{spelling}({event_player}, probe, 0, 1, 1);")),
1103        ),
1104        "setGlobalVariable" => probe_rule(catalog, locale, &format!("{spelling}(probe, 1);")),
1105        "modifyGlobalVariable" => {
1106            let add = localized_spelling(catalog, Kind::Operator, locale, "add");
1107            probe_rule(catalog, locale, &format!("{spelling}(probe, {add}, 1);"))
1108        }
1109        "setPlayerVariable" => probe_rule(
1110            catalog,
1111            locale,
1112            &format!("{spelling}({event_player}, probe, 1);"),
1113        ),
1114        "modifyPlayerVariable" => {
1115            let add = localized_spelling(catalog, Kind::Operator, locale, "add");
1116            probe_rule(
1117                catalog,
1118                locale,
1119                &format!("{spelling}({event_player}, probe, {add}, 1);"),
1120            )
1121        }
1122        "callSubroutine" => probe_rule(catalog, locale, &format!("{spelling}(probe);")),
1123        _ => probe_rule(catalog, locale, &format!("{spelling};")),
1124    }
1125}
1126
1127fn member_probe(catalog: &Catalog, locale: &Locale, domain: &EnumDomain, spelling: &str) -> String {
1128    if CONSTRUCTOR_DOMAINS.contains(&domain.domain.as_str()) {
1129        let domain_spelling = domain.spelling(locale).unwrap_or(&domain.domain);
1130        return global_probe(catalog, locale, &format!("{domain_spelling}({spelling})"));
1131    }
1132    if domain.domain == "EventTeam" || domain.domain == "EventPlayer" {
1133        let event = localized_spelling(catalog, Kind::Event, locale, "eachPlayer");
1134        let other = if domain.domain == "EventTeam" {
1135            localized_member(catalog, "EventPlayer", "ALL", locale)
1136        } else {
1137            localized_member(catalog, "EventTeam", "ALL", locale)
1138        };
1139        let (team, player) = if domain.domain == "EventTeam" {
1140            (spelling.to_string(), other)
1141        } else {
1142            (other, spelling.to_string())
1143        };
1144        let rule = localized_spelling(catalog, Kind::Structural, locale, "rule");
1145        let event_kw = localized_spelling(catalog, Kind::Structural, locale, "event");
1146        let actions = localized_spelling(catalog, Kind::Structural, locale, "actions");
1147        let wait = localized_spelling(catalog, Kind::Action, locale, "wait");
1148        return format!(
1149            "{rule} (\"e\") {{\n    {event_kw} {{\n        {event};\n        {team};\n        {player};\n    }}\n    {actions} {{\n        {wait};\n    }}\n}}\n"
1150        );
1151    }
1152    let host = [Kind::Value, Kind::Action]
1153        .into_iter()
1154        .flat_map(|kind| catalog.entries_of(kind))
1155        .filter(|entry| {
1156            (0..entry.param_count()).any(|index| entry.param_domain(index) == Some(&domain.domain))
1157        })
1158        .min_by_key(|entry| entry.required_param_count());
1159    let Some(host) = host else {
1160        return global_probe(catalog, locale, spelling);
1161    };
1162    let target = (0..host.param_count())
1163        .find(|index| host.param_domain(*index) == Some(&domain.domain))
1164        .expect("host was selected for carrying this domain");
1165    let mut call_args: Vec<String> = (0..host.param_count())
1166        .map(|index| probe_filler(catalog, locale, host, index))
1167        .collect();
1168    call_args[target] = spelling.to_string();
1169    let host_spelling = localized_spelling(catalog, host.kind, locale, &host.id);
1170    let call = format!("{host_spelling}({})", call_args.join(", "));
1171    match host.kind {
1172        Kind::Value => global_probe(catalog, locale, &call),
1173        _ => probe_rule(catalog, locale, &format!("{call};")),
1174    }
1175}
1176
1177struct SettingsSpellings {
1178    by_label: HashMap<String, Vec<SettingDefinition>>,
1179    by_member: HashMap<String, Vec<SettingDefinition>>,
1180    names: HashMap<(String, String, String), String>,
1181}
1182
1183impl SettingsSpellings {
1184    fn build() -> Self {
1185        let mut by_label: HashMap<String, Vec<SettingDefinition>> = HashMap::new();
1186        let mut by_member: HashMap<String, Vec<SettingDefinition>> = HashMap::new();
1187        for definition in settings_schema::definitions() {
1188            by_label
1189                .entry(definition.presentation().english_name.to_string())
1190                .or_default()
1191                .push(definition.clone());
1192            for member in definition.enum_members() {
1193                by_member
1194                    .entry(member.english_name().to_string())
1195                    .or_default()
1196                    .push(definition.clone());
1197            }
1198        }
1199        let mut names = HashMap::new();
1200        for row in settings_schema::localized_setting_names() {
1201            names.insert(
1202                (
1203                    row.locale.to_string(),
1204                    row.section.to_string(),
1205                    row.english.to_string(),
1206                ),
1207                row.spelling.to_string(),
1208            );
1209        }
1210        Self {
1211            by_label,
1212            by_member,
1213            names,
1214        }
1215    }
1216
1217    fn name(&self, locale: &str, section: &str, english: &str) -> String {
1218        self.lookup(locale, section, english)
1219            .or_else(|| self.lookup(EN_US, section, english))
1220            .unwrap_or(english)
1221            .to_string()
1222    }
1223
1224    fn lookup(&self, locale: &str, section: &str, english: &str) -> Option<&str> {
1225        self.names
1226            .get(&(locale.to_string(), section.to_string(), english.to_string()))
1227            .map(String::as_str)
1228    }
1229}
1230
1231/// A probe hero for `definition`'s `<hero>` slot: ability-scoped defs need
1232/// a hero that owns the slot; prefer a hero whose emitted name equals the
1233/// tested label so generic labels exercise the typed path (#448).
1234fn settings_probe_hero(definition: &SettingDefinition, english: &str) -> Option<Option<String>> {
1235    if !definition.path().contains(".<hero>.") && !definition.path().ends_with(".<hero>") {
1236        return Some(None);
1237    }
1238    let data = gameplay::data::builtin_ref().ok()?;
1239    let heroes = data.query().heroes().to_vec();
1240    let hero_ids: Vec<String> = match definition.target_kind() {
1241        SettingTargetKind::HeroAbility { slot, variant } => heroes
1242            .iter()
1243            .filter(|hero| {
1244                data.query()
1245                    .ability_name(hero.id().as_str(), slot.as_str(), variant.as_ref(), EN_US)
1246                    .is_ok()
1247            })
1248            .map(|hero| hero.id().as_str().to_string())
1249            .collect(),
1250        _ => heroes
1251            .iter()
1252            .map(|hero| hero.id().as_str().to_string())
1253            .collect(),
1254    };
1255    // Prefer a hero that actually renders the tested label (generic
1256    // spellings are shadowed by hero-specific ability names), but any hero
1257    // carrying the slot still writes the spelling verbatim.
1258    let mut matching = Vec::new();
1259    let mut any = Vec::new();
1260    for id in hero_ids {
1261        let target = match definition.target_kind() {
1262            SettingTargetKind::HeroAbility { slot, variant } => SettingTarget::HeroAbility {
1263                team: None,
1264                hero: HeroId::new(id.as_str()),
1265                slot,
1266                variant,
1267            },
1268            _ => SettingTarget::Hero {
1269                team: None,
1270                hero: HeroId::new(id.as_str()),
1271            },
1272        };
1273        let emitted = definition
1274            .localized_name(EN_US, &target)
1275            .ok()
1276            .flatten()
1277            .map(str::to_string);
1278        any.push(id.clone());
1279        if emitted.as_deref() == Some(english) {
1280            matching.push(id);
1281        }
1282    }
1283    matching.into_iter().chain(any).next().map(Some)
1284}
1285
1286/// Substitute the ability slot's localized name into a `%1$s` template
1287/// spelling — the only form in which such a label can appear in source.
1288/// Returns None when the definition is not hero-ability scoped or the hero
1289/// has no ability name to substitute (#448).
1290fn substitute_template(
1291    definition: &SettingDefinition,
1292    spelling: &str,
1293    hero: Option<&str>,
1294    locale: &str,
1295) -> Option<String> {
1296    if !spelling.contains("%1$s") {
1297        return Some(spelling.to_string());
1298    }
1299    let SettingTargetKind::HeroAbility { slot, variant } = definition.target_kind() else {
1300        return None;
1301    };
1302    let ability = gameplay::data::builtin_ref()
1303        .ok()?
1304        .query()
1305        .ability_name(hero?, slot.as_str(), variant.as_ref(), locale)
1306        .ok()?
1307        .to_string();
1308    Some(spelling.replace("%1$s", &ability))
1309}
1310
1311fn localized_settings_context(
1312    catalog: &Catalog,
1313    index: &SettingsSpellings,
1314    locale: &Locale,
1315    definition: &SettingDefinition,
1316    leaf: &str,
1317    hero: Option<&str>,
1318) -> String {
1319    let hero_name = hero
1320        .and_then(|id| {
1321            gameplay::data::builtin_ref()
1322                .ok()?
1323                .query()
1324                .heroes()
1325                .iter()
1326                .find(|hero| hero.id().as_str() == id)
1327                .and_then(|hero| hero.name().value().get(EN_US).map(str::to_string))
1328        })
1329        .map(|english| index.name(locale.as_str(), "heroes", &english))
1330        .unwrap_or_else(|| index.name(locale.as_str(), "heroes", "Ana"));
1331    let settings = localized_spelling(catalog, Kind::Structural, locale, "settings");
1332    let mut lines = vec![format!("{settings} {{")];
1333    let mut depth = 1;
1334    let parts: Vec<_> = definition.path().split('.').collect();
1335    for (part_index, part) in parts.iter().take(parts.len() - 1).enumerate() {
1336        let name = match *part {
1337            "gamemodes" => index.name(locale.as_str(), "namespaces", "modes"),
1338            "heroes" => index.name(locale.as_str(), "namespaces", "heroes"),
1339            "<team>" | "general" => index.name(locale.as_str(), "teams", "General"),
1340            "<hero>" => hero_name.clone(),
1341            value if part_index == 1 && parts.first() == Some(&"gamemodes") => {
1342                let english = catalog_enum_spelling(catalog, "Gamemode", value)
1343                    .unwrap_or_else(|| value.to_string());
1344                index.name(locale.as_str(), "modes", &english)
1345            }
1346            value => index.name(locale.as_str(), "labels", value),
1347        };
1348        lines.push(format!("{}{} {{", "    ".repeat(depth), name));
1349        depth += 1;
1350    }
1351    lines.push(format!("{}{leaf}", "    ".repeat(depth)));
1352    while depth > 1 {
1353        depth -= 1;
1354        lines.push(format!("{}{}", "    ".repeat(depth), "}"));
1355    }
1356    lines.push("}".to_string());
1357    lines.join("\n")
1358}
1359
1360fn leaf_value(
1361    index: &SettingsSpellings,
1362    locale: &Locale,
1363    definition: &SettingDefinition,
1364) -> String {
1365    match definition.domain() {
1366        SettingValueDomain::PresenceOnly => String::new(),
1367        SettingValueDomain::String => ": \"x\"".to_string(),
1368        SettingValueDomain::Boolean | SettingValueDomain::Enum { .. } => {
1369            let member = definition
1370                .enum_members()
1371                .next()
1372                .map(|member| {
1373                    // Settings members are declared under `enums`; the
1374                    // boolean tokens (On/Off/Yes/No) live under `tokens`.
1375                    index
1376                        .lookup(locale.as_str(), "enums", member.english_name())
1377                        .or_else(|| index.lookup(locale.as_str(), "tokens", member.english_name()))
1378                        .unwrap_or(member.english_name())
1379                        .to_string()
1380                })
1381                .unwrap_or_else(|| index.name(locale.as_str(), "tokens", "On"));
1382            format!(": {member}")
1383        }
1384        SettingValueDomain::Number(_) => ": 1".to_string(),
1385        SettingValueDomain::Percent(_) => ": 100%".to_string(),
1386        SettingValueDomain::HeroList | SettingValueDomain::MapList => " {\n}".to_string(),
1387        _ => ": 0".to_string(),
1388    }
1389}
1390
1391fn localized_settings_probe(
1392    catalog: &Catalog,
1393    index: &SettingsSpellings,
1394    name: &settings_schema::LocalizedSettingName,
1395    locale: &Locale,
1396) -> Result<String, String> {
1397    let english = name.english;
1398    let spelling = name.spelling;
1399    let loc = locale.as_str();
1400    let settings = localized_spelling(catalog, Kind::Structural, locale, "settings");
1401    let modes = index.name(loc, "namespaces", "modes");
1402    let heroes = index.name(loc, "namespaces", "heroes");
1403    let general = index.name(loc, "teams", "General");
1404    let assault = index.name(loc, "modes", "Assault");
1405    match name.section {
1406        "namespaces" => Ok(format!("{settings} {{\n    {spelling} {{\n    }}\n}}")),
1407        "modes" => Ok(format!(
1408            "{settings} {{\n    {modes} {{\n        {spelling} {{\n        }}\n    }}\n}}"
1409        )),
1410        "teams" => Ok(format!(
1411            "{settings} {{\n    {heroes} {{\n        {spelling} {{\n            {} {{\n            }}\n        }}\n    }}\n}}",
1412            index.name(loc, "heroes", "Ana")
1413        )),
1414        "heroes" => Ok(format!(
1415            "{settings} {{\n    {heroes} {{\n        {general} {{\n            {spelling} {{\n            }}\n        }}\n    }}\n}}"
1416        )),
1417        "maps" => Ok(format!(
1418            "{settings} {{\n    {modes} {{\n        {assault} {{\n            {} {{\n                {spelling}\n            }}\n        }}\n    }}\n}}",
1419            index.name(loc, "labels", "enabled maps")
1420        )),
1421        "tokens" => match english {
1422            "disabled" => Ok(format!(
1423                "{settings} {{\n    {modes} {{\n        {spelling} {assault} {{\n        }}\n    }}\n}}"
1424            )),
1425            _ => Ok(format!(
1426                "{settings} {{\n    {modes} {{\n        {assault} {{\n            {}: {spelling}\n        }}\n    }}\n}}",
1427                index.name(loc, "labels", "Competitive Rules")
1428            )),
1429        },
1430        "labels" => {
1431            let Some(defs) = index.by_label.get(english) else {
1432                return Err(format!("no settings definition named {english:?}"));
1433            };
1434            let mut reasons = Vec::new();
1435            for definition in defs {
1436                let hero = settings_probe_hero(definition, english);
1437                let leaf = match substitute_template(
1438                    definition,
1439                    spelling,
1440                    hero.as_ref().and_then(|hero| hero.as_deref()),
1441                    loc,
1442                ) {
1443                    Some(leaf) => leaf,
1444                    None => {
1445                        reasons.push("template needs a hero-ability context".to_string());
1446                        continue;
1447                    }
1448                };
1449                return Ok(localized_settings_context(
1450                    catalog,
1451                    index,
1452                    locale,
1453                    definition,
1454                    &format!("{leaf}{}", leaf_value(index, locale, definition)),
1455                    hero.as_ref().and_then(|hero| hero.as_deref()),
1456                ));
1457            }
1458            Err(reasons
1459                .into_iter()
1460                .next()
1461                .unwrap_or_else(|| "no probe context".to_string()))
1462        }
1463        "enums" => {
1464            let Some(defs) = index.by_member.get(english) else {
1465                return Err(format!("no settings definition takes member {english:?}"));
1466            };
1467            let definition = &defs[0];
1468            let hero = settings_probe_hero(definition, definition.presentation().english_name);
1469            let label = index.name(loc, "labels", definition.presentation().english_name);
1470            Ok(localized_settings_context(
1471                catalog,
1472                index,
1473                locale,
1474                definition,
1475                &format!("{label}: {spelling}"),
1476                hero.as_ref().and_then(|hero| hero.as_deref()),
1477            ))
1478        }
1479        other => Err(format!("unhandled settings section {other:?}")),
1480    }
1481}
1482
1483fn wir_case(case_id: &str, kind: FeatureKind, name: &str, source: String) -> CensusCase {
1484    CensusCase::probe(
1485        format!("wir/{case_id}"),
1486        vec![feature(FeatureNamespace::Wir, kind, name)],
1487        source,
1488    )
1489    .exercise()
1490}
1491
1492fn control_flow_case(name: &str, actions: &str) -> CensusCase {
1493    CensusCase::probe(
1494        format!("wir/control-flow/{name}"),
1495        vec![feature(
1496            FeatureNamespace::Wir,
1497            FeatureKind::ControlFlow,
1498            name,
1499        )],
1500        rule_source(name, actions),
1501    )
1502}
1503
1504fn generated_probe_support() -> CensusSupport {
1505    CensusSupport::Inconclusive {
1506        detail: "generated probe has no independently recorded expected result".to_string(),
1507    }
1508}
1509
1510fn rule_source(name: &str, actions: &str) -> String {
1511    format!(
1512        "variables {{\n    global:\n        0: probe\n}}\n\nrule (\"{name}\") {{\n    event {{\n        Ongoing - Global;\n    }}\n    actions {{\n        {actions}\n    }}\n}}\n"
1513    )
1514}
1515
1516fn variables_source() -> String {
1517    "variables {\n    global:\n        0: probe\n}\n\nrule (\"Global variable\") {\n    event {\n        Ongoing - Global;\n    }\n    actions {\n        Set Global Variable(probe, 1);\n    }\n}\n"
1518        .to_string()
1519}
1520
1521fn player_variable_source() -> String {
1522    "variables {\n    player:\n        0: probe\n}\n\nrule (\"Player variable\") {\n    event {\n        Ongoing - Each Player;\n        All;\n        All;\n    }\n    actions {\n        Set Player Variable(Event Player, probe, 1);\n    }\n}\n"
1523        .to_string()
1524}
1525
1526fn subroutine_source() -> String {
1527    "subroutines {\n    0: probe\n}\n\nrule (\"Subroutine\") {\n    event {\n        Subroutine;\n        probe;\n    }\n    actions {\n        Call Subroutine(probe);\n    }\n}\n"
1528        .to_string()
1529}
1530
1531fn event_probe(catalog: &Catalog, entry: &CatalogEntry) -> String {
1532    let spelling = catalog
1533        .spelling(Kind::Event, &Locale::new(EN_US), &entry.id)
1534        .unwrap_or(&entry.id);
1535    let filters = if matches!(entry.id.as_str(), "global" | "subroutine") {
1536        String::new()
1537    } else {
1538        "        All;\n        All;\n".to_string()
1539    };
1540    let subroutine = if entry.id == "subroutine" {
1541        "        probe;\n"
1542    } else {
1543        ""
1544    };
1545    format!(
1546        "subroutines {{\n    0: probe\n}}\n\nrule (\"Event\") {{\n    event {{\n        {spelling};\n{filters}{subroutine}    }}\n    actions {{\n        Wait;\n    }}\n}}\n"
1547    )
1548}
1549
1550fn action_probe(catalog: &Catalog, entry: &CatalogEntry) -> String {
1551    let spelling = catalog
1552        .spelling(Kind::Action, &Locale::new(EN_US), &entry.id)
1553        .unwrap_or(&entry.id);
1554    let call = if matches!(
1555        entry.id.as_str(),
1556        "chasePlayerVariableAtRate" | "chasePlayerVariableOverTime"
1557    ) {
1558        format!("{spelling}(Event Player, probe, 0, 1, 0);")
1559    } else {
1560        format!("{spelling};")
1561    };
1562    rule_source("Action", &call)
1563}
1564
1565fn value_probe(catalog: &Catalog, entry: &CatalogEntry) -> String {
1566    let spelling = catalog
1567        .spelling(Kind::Value, &Locale::new(EN_US), &entry.id)
1568        .unwrap_or(&entry.id);
1569    rule_source("Value", &format!("Set Global Variable(probe, {spelling});"))
1570}
1571
1572fn operator_probe(catalog: &Catalog, entry: &CatalogEntry) -> String {
1573    let spelling = catalog
1574        .spelling(Kind::Operator, &Locale::new(EN_US), &entry.id)
1575        .unwrap_or(&entry.id);
1576    rule_source(
1577        "Operator",
1578        &format!("If(1 {spelling} 1);\n    Wait(0);\nEnd;"),
1579    )
1580}
1581
1582fn structural_probe(catalog: &Catalog, entry: &CatalogEntry) -> String {
1583    let spelling = catalog
1584        .spelling(Kind::Structural, &Locale::new(EN_US), &entry.id)
1585        .unwrap_or(&entry.id);
1586    let actions = match entry.id.as_str() {
1587        "if" => format!("{spelling}(True);\n    Wait(0);\nEnd;"),
1588        "elseIf" => format!("If(True);\n    Wait(0);\n{spelling}(False);\n    Wait(0);\nEnd;"),
1589        "else" => format!("If(True);\n    Wait(0);\n{spelling};\n    Wait(0);\nEnd;"),
1590        "end" => format!("If(True);\n    Wait(0);\n{spelling};"),
1591        "while" => format!("{spelling}(True);\n    Wait(0);\nEnd;"),
1592        "forGlobalVariable" => format!("{spelling}(probe, 0, 1, 1);\n    Wait(0);\nEnd;"),
1593        "setGlobalVariable" => format!("{spelling}(probe, 1);"),
1594        "modifyGlobalVariable" => format!("{spelling}(probe, Add, 1);"),
1595        "setPlayerVariable" => format!("{spelling}(Event Player, probe, 1);"),
1596        "modifyPlayerVariable" => format!("{spelling}(Event Player, probe, Add, 1);"),
1597        "callSubroutine" => format!("{spelling}(probe);"),
1598        _ => format!("{spelling};"),
1599    };
1600    let prefix = match entry.id.as_str() {
1601        "setPlayerVariable" | "modifyPlayerVariable" => {
1602            "variables {\n    player:\n        0: probe\n}\n\n"
1603        }
1604        "callSubroutine" => "subroutines {\n    0: probe\n}\n\n",
1605        _ => "",
1606    };
1607    format!("{prefix}{}", rule_source("Structural", &actions))
1608}
1609
1610fn enum_probe(catalog: &Catalog, domain: &EnumDomain, member: &str) -> String {
1611    let locale = Locale::new(EN_US);
1612    let domain_spelling = catalog
1613        .spelling(Kind::Value, &locale, &domain.domain)
1614        .unwrap_or(&domain.domain);
1615    let member_spelling = catalog
1616        .enum_spelling(&domain.domain, &locale, member)
1617        .unwrap_or(member);
1618    // Only the constructor-form domains parse `Domain(Member)`; every other
1619    // domain writes the member bare (mirrors `Catalog::enum_member_form`).
1620    let operand = if CONSTRUCTOR_DOMAINS.contains(&domain.domain.as_str()) {
1621        format!("{domain_spelling}({member_spelling})")
1622    } else {
1623        member_spelling.to_string()
1624    };
1625    rule_source("Enum", &format!("Set Global Variable(probe, {operand});"))
1626}
1627
1628fn settings_probe(definition: &SettingDefinition, catalog: &Catalog) -> String {
1629    let mut lines = vec!["settings {".to_string()];
1630    let mut depth = 1;
1631    let parts: Vec<_> = definition.path().split('.').collect();
1632    for (index, part) in parts.iter().enumerate() {
1633        let name = match *part {
1634            "gamemodes" => "modes".to_string(),
1635            "heroes" => "heroes".to_string(),
1636            "<team>" => "General".to_string(),
1637            "<hero>" => {
1638                catalog_enum_spelling(catalog, "Hero", "mei").unwrap_or_else(|| "Mei".to_string())
1639            }
1640            "general" => "General".to_string(),
1641            value if index == 1 && parts.first() == Some(&"gamemodes") => {
1642                mode_spelling(catalog, value)
1643            }
1644            value => value.to_string(),
1645        };
1646        lines.push(format!("{}{} {{", "    ".repeat(depth), name));
1647        depth += 1;
1648    }
1649    let indent = "    ".repeat(depth);
1650    let name = definition.presentation().english_name;
1651    match definition.domain() {
1652        SettingValueDomain::PresenceOnly => lines.push(format!("{indent}{name}")),
1653        SettingValueDomain::String => lines.push(format!("{indent}{name}: \"census\"")),
1654        SettingValueDomain::Boolean => {
1655            let value = definition
1656                .enum_members()
1657                .next()
1658                .map(|member| member.english_name().to_string())
1659                .unwrap_or_else(|| "On".to_string());
1660            lines.push(format!("{indent}{name}: {value}"));
1661        }
1662        SettingValueDomain::Number(_) => lines.push(format!("{indent}{name}: 1")),
1663        SettingValueDomain::Percent(_) => lines.push(format!("{indent}{name}: 100%")),
1664        SettingValueDomain::Enum { .. } => {
1665            let value = definition
1666                .enum_members()
1667                .next()
1668                .map(|member| member.english_name().to_string())
1669                .unwrap_or_else(|| "Off".to_string());
1670            lines.push(format!("{indent}{name}: {value}"));
1671        }
1672        SettingValueDomain::HeroList | SettingValueDomain::MapList => {
1673            lines.push(format!("{indent}{name} {{"));
1674            lines.push(format!("{indent}}}"));
1675        }
1676        other => unreachable!("census has no probe for settings value domain {other:?}"),
1677    }
1678    while depth > 1 {
1679        depth -= 1;
1680        lines.push(format!("{}{}", "    ".repeat(depth), "}"));
1681    }
1682    lines.push("}".to_string());
1683    lines.join("\n")
1684}
1685
1686fn mode_spelling(catalog: &Catalog, key: &str) -> String {
1687    catalog_enum_spelling(catalog, "Gamemode", key).unwrap_or_else(|| key.to_string())
1688}
1689
1690fn catalog_enum_spelling(catalog: &Catalog, domain: &str, key: &str) -> Option<String> {
1691    let normalize = |value: &str| {
1692        value
1693            .chars()
1694            .filter(|character| character.is_ascii_alphanumeric())
1695            .flat_map(char::to_lowercase)
1696            .collect::<String>()
1697    };
1698    let normalized = normalize(key);
1699    let member = catalog
1700        .enum_domains()
1701        .find(|candidate| candidate.domain == domain)?
1702        .members
1703        .iter()
1704        .find(|candidate| normalize(&candidate.member) == normalized)?;
1705    catalog
1706        .enum_spelling(domain, &Locale::new(EN_US), &member.member)
1707        .map(str::to_string)
1708}
1709
1710fn run_case(case: &CensusCase, shard_id: &str, catalog: &Catalog) -> ConformanceResult {
1711    let source = artifact(
1712        format!("census/{shard_id}/{}.ws", case.case_id),
1713        &case.source,
1714    );
1715    let base = |status, comparison, reason, locale| ConformanceResult {
1716        schema_version: CONFORMANCE_SCHEMA_VERSION,
1717        case_id: case.case_id.clone(),
1718        features: case.features.clone(),
1719        status,
1720        comparison,
1721        source: source.clone(),
1722        catalog: catalog.identity(),
1723        locale,
1724        reason,
1725    };
1726    match &case.support {
1727        CensusSupport::Unsupported { detail } => base(
1728            ConformanceStatus::Unsupported,
1729            not_comparable(),
1730            Some(reason(ReasonCode::Unsupported, detail)),
1731            None,
1732        ),
1733        CensusSupport::KnownGap { detail } => base(
1734            ConformanceStatus::KnownGap,
1735            not_comparable(),
1736            Some(reason(ReasonCode::KnownGap, detail)),
1737            None,
1738        ),
1739        CensusSupport::Inconclusive { detail } => execute_case(case, base, catalog, Some(detail)),
1740        CensusSupport::Exercise => execute_case(case, base, catalog, None),
1741    }
1742}
1743
1744struct Fail {
1745    code: ReasonCode,
1746    detail: String,
1747    locale: Locale,
1748}
1749
1750impl Fail {
1751    fn workshop(error: &WorkshopError, locale: &Locale) -> Self {
1752        let code = match error {
1753            WorkshopError::Unsupported { .. } => ReasonCode::Unsupported,
1754            WorkshopError::MissingMapping { .. } => ReasonCode::KnownGap,
1755            _ => ReasonCode::UnexpectedRegression,
1756        };
1757        Self {
1758            code,
1759            detail: error.to_string(),
1760            locale: locale.clone(),
1761        }
1762    }
1763
1764    fn regression(detail: impl Into<String>, locale: &Locale) -> Self {
1765        Self {
1766            code: ReasonCode::UnexpectedRegression,
1767            detail: detail.into(),
1768            locale: locale.clone(),
1769        }
1770    }
1771}
1772
1773fn execute_case(
1774    case: &CensusCase,
1775    base: impl Fn(
1776        ConformanceStatus,
1777        Comparison,
1778        Option<ConformanceReason>,
1779        Option<Locale>,
1780    ) -> ConformanceResult,
1781    catalog: &Catalog,
1782    inconclusive_detail: Option<&str>,
1783) -> ConformanceResult {
1784    match run_pipeline(case, catalog, inconclusive_detail) {
1785        Ok((comparison, locale)) => {
1786            base(ConformanceStatus::Matched, comparison, None, Some(locale))
1787        }
1788        Err(fail) => failed_text(base, fail.code, fail.detail, Some(fail.locale)),
1789    }
1790}
1791
1792fn run_pipeline(
1793    case: &CensusCase,
1794    catalog: &Catalog,
1795    inconclusive_detail: Option<&str>,
1796) -> Result<(Comparison, Locale), Fail> {
1797    let source_locale = Locale::new(&case.source_locale);
1798    let target_locale = if source_locale.as_str() == ZH_CN {
1799        Locale::new(EN_US)
1800    } else {
1801        Locale::new(ZH_CN)
1802    };
1803
1804    let parse = |source: &str, locale: &Locale| {
1805        parser::parse_with_context(source, catalog, locale, catalog)
1806            .map_err(|error| Fail::workshop(&error, locale))
1807    };
1808    let validate = |program: &workshop_rs::Program, locale: &Locale| {
1809        program
1810            .validate()
1811            .map_err(|error| Fail::regression(error.to_string(), locale))
1812    };
1813    let emit = |program: &workshop_rs::Program, locale: &Locale| {
1814        emitter::emit(program, catalog, locale).map_err(|error| Fail::workshop(&error, locale))
1815    };
1816    let convert_or =
1817        |source: &str, from: &Locale, to: &Locale| -> Result<convert::Conversion, Fail> {
1818            convert::convert(source, catalog, from, to, &Default::default())
1819                .map_err(|error| Fail::workshop(&error, to))
1820        };
1821
1822    // Some declared spellings are inherently ambiguous in a locale: zh-CN
1823    // writes `下` for `down`, `Vector.DOWN`, and `Rounding.DOWN` alike, so a
1824    // faithful conversion reparses to a preserved `__ambiguous_enum` rather
1825    // than the resolved node. When the target program emits byte-identical
1826    // canonical text, the client-observable surface is right and only the
1827    // resolved-identity check is undecidable — report that explicitly as
1828    // Inconclusive instead of a regression (#448).
1829    let surface_equivalent =
1830        |a: &workshop_rs::Program, b: &workshop_rs::Program, locale: &Locale| {
1831            let (Ok(x), Ok(y)) = (emit(a, locale), emit(b, locale)) else {
1832                return false;
1833            };
1834            normalize_workshop(&x) == normalize_workshop(&y)
1835        };
1836
1837    let program = parse(&case.source, &source_locale)?;
1838    validate(&program, &source_locale)?;
1839    let emitted = emit(&program, &source_locale)?;
1840    let reparsed = parse(&emitted, &source_locale)?;
1841    validate(&reparsed, &source_locale)?;
1842    let reemitted = emit(&reparsed, &source_locale)?;
1843    if !roundtrip::equivalent(&program, &reparsed)
1844        || normalize_workshop(&emitted) != normalize_workshop(&reemitted)
1845    {
1846        return Err(Fail::regression(
1847            "en-US semantic or normalized gate diverged",
1848            &source_locale,
1849        ));
1850    }
1851
1852    let converted = convert_or(&case.source, &source_locale, &target_locale)?;
1853    let target_program = parse(&converted.text, &target_locale)?;
1854    validate(&target_program, &target_locale)?;
1855    if !roundtrip::equivalent(&program, &target_program) {
1856        if surface_equivalent(&program, &target_program, &target_locale) {
1857            return Err(Fail {
1858                code: ReasonCode::Inconclusive,
1859                detail: "converted text matches the canonical emission, but the \
1860                         target-locale spelling is ambiguous across enum domains; \
1861                         resolved semantics cannot be independently verified"
1862                    .to_string(),
1863                locale: target_locale,
1864            });
1865        }
1866        return Err(Fail::regression(
1867            "zh-CN conversion changed canonical WIR semantics",
1868            &target_locale,
1869        ));
1870    }
1871    let back = convert_or(&converted.text, &target_locale, &source_locale)?;
1872    let back_program = parse(&back.text, &source_locale)?;
1873    // Compare against the canonical emission, not the raw source: probes
1874    // written in a non-canonical declared alias (#448) still must land on
1875    // the same canonical program and surface form after a round trip.
1876    if !roundtrip::equivalent(&program, &back_program)
1877        || normalize_workshop(&back.text) != normalize_workshop(&emitted)
1878    {
1879        if surface_equivalent(&program, &back_program, &source_locale) {
1880            return Err(Fail {
1881                code: ReasonCode::Inconclusive,
1882                detail: "round-trip text matches the canonical emission, but the \
1883                         source-locale spelling is ambiguous across enum domains; \
1884                         resolved semantics cannot be independently verified"
1885                    .to_string(),
1886                locale: source_locale,
1887            });
1888        }
1889        return Err(Fail::regression(
1890            "cross-locale semantic or normalized gate diverged",
1891            &source_locale,
1892        ));
1893    }
1894
1895    let Some(reference_source) = case.reference_source.as_ref() else {
1896        return Err(Fail {
1897            code: ReasonCode::Inconclusive,
1898            detail: inconclusive_detail
1899                .unwrap_or(
1900                    "offline semantic and locale gates passed, but no independent expectation artifact is recorded",
1901                )
1902                .to_string(),
1903            locale: source_locale,
1904        });
1905    };
1906    let expected_program = parse(reference_source, &target_locale)?;
1907    if !roundtrip::equivalent(&program, &expected_program)
1908        || normalize_workshop(&converted.text) != normalize_workshop(reference_source)
1909    {
1910        return Err(Fail::regression(
1911            "conversion differed from the independent reference source",
1912            &target_locale,
1913        ));
1914    }
1915    Ok((
1916        Comparison {
1917            mode: Equivalence::Semantic,
1918            expected: Some(reference_artifact(case, reference_source)),
1919            observed: Some(artifact(
1920                format!("census/{}/converted-output.ws", case.case_id),
1921                &converted.text,
1922            )),
1923            normalizer: Some("canonical-wir;normalized-workshop-text".to_string()),
1924        },
1925        source_locale,
1926    ))
1927}
1928
1929fn failed_text(
1930    base: impl Fn(
1931        ConformanceStatus,
1932        Comparison,
1933        Option<ConformanceReason>,
1934        Option<Locale>,
1935    ) -> ConformanceResult,
1936    code: ReasonCode,
1937    detail: String,
1938    locale: Option<Locale>,
1939) -> ConformanceResult {
1940    let status = match code {
1941        ReasonCode::Unsupported => ConformanceStatus::Unsupported,
1942        ReasonCode::KnownGap => ConformanceStatus::KnownGap,
1943        ReasonCode::UnexpectedRegression => ConformanceStatus::UnexpectedRegression,
1944        ReasonCode::Inconclusive => ConformanceStatus::Inconclusive,
1945    };
1946    let comparison = if code == ReasonCode::UnexpectedRegression {
1947        Comparison {
1948            mode: Equivalence::Normalized,
1949            expected: None,
1950            observed: None,
1951            normalizer: Some("census-stage".to_string()),
1952        }
1953    } else {
1954        not_comparable()
1955    };
1956    base(status, comparison, Some(reason(code, &detail)), locale)
1957}
1958
1959fn reason(code: ReasonCode, detail: &str) -> ConformanceReason {
1960    ConformanceReason {
1961        code,
1962        detail: detail.to_string(),
1963    }
1964}
1965
1966fn not_comparable() -> Comparison {
1967    Comparison {
1968        mode: Equivalence::NotComparable,
1969        expected: None,
1970        observed: None,
1971        normalizer: None,
1972    }
1973}
1974
1975fn artifact(name: impl Into<String>, content: &str) -> TestArtifact {
1976    TestArtifact {
1977        name: name.into(),
1978        revision: None,
1979        path: None,
1980        sha256: Some(sha256(content)),
1981        license: Some("MIT".to_string()),
1982    }
1983}
1984
1985fn reference_artifact(case: &CensusCase, content: &str) -> TestArtifact {
1986    TestArtifact {
1987        name: format!("census reference for {}", case.case_id),
1988        revision: Some("census-v1".to_string()),
1989        path: Some("tests/fixtures/census/reference.ws".to_string()),
1990        sha256: Some(sha256(content)),
1991        license: Some("MIT".to_string()),
1992    }
1993}
1994
1995fn sha256(content: &str) -> String {
1996    let mut hasher = Sha256::new();
1997    hasher.update(content.as_bytes());
1998    format!("{:x}", hasher.finalize())
1999}
2000
2001fn normalize_workshop(text: &str) -> String {
2002    text.split_whitespace().collect::<Vec<_>>().join(" ")
2003}
2004
2005#[cfg(test)]
2006mod tests {
2007    use super::*;
2008
2009    #[test]
2010    fn builtin_census_is_derived_and_deterministic() {
2011        let catalog = Catalog::builtin().expect("builtin catalog");
2012        let first = Census::builtin(&catalog).expect("census");
2013        let second = Census::builtin(&catalog).expect("census");
2014        assert_eq!(first, second);
2015        assert_eq!(first.shards().first().unwrap().shard_id, "catalog-actions");
2016        assert!(
2017            first
2018                .cases()
2019                .any(|case| case.features.iter().any(|feature| feature.name == "wait"))
2020        );
2021        assert!(first.cases().any(|case| {
2022            case.features
2023                .iter()
2024                .any(|feature| feature.kind == FeatureKind::Setting)
2025        }));
2026        assert!(first.cases().any(|case| {
2027            case.features
2028                .iter()
2029                .any(|feature| feature.kind == FeatureKind::ControlFlow)
2030        }));
2031        assert_eq!(first.export_json().unwrap(), second.export_json().unwrap());
2032        assert_eq!(first.identity(), second.identity());
2033        assert_eq!(first.identity().digest.len(), 64);
2034    }
2035
2036    #[test]
2037    fn settings_probes_use_catalog_gamemode_spellings() {
2038        let catalog = Catalog::builtin().expect("builtin catalog");
2039
2040        for (mode, expected) in [
2041            ("ffa", "Deathmatch"),
2042            ("tdm", "Team Deathmatch"),
2043            ("ctf", "Capture The Flag"),
2044        ] {
2045            let prefix = format!("gamemodes.{mode}.");
2046            let definition = settings_schema::definitions()
2047                .find(|definition| definition.path().starts_with(&prefix))
2048                .unwrap_or_else(|| panic!("missing settings definition for {mode}"));
2049            let probe = settings_probe(&definition, &catalog);
2050            assert!(
2051                probe.contains(&format!("{expected} {{")),
2052                "{mode} probe used the wrong mode spelling:\n{probe}"
2053            );
2054        }
2055    }
2056
2057    #[test]
2058    fn explicit_non_matching_states_remain_machine_readable() {
2059        let feature_case = |id: &str, support| {
2060            CensusCase::probe(
2061                format!("state-tests/{id}"),
2062                vec![feature(FeatureNamespace::Wir, FeatureKind::Structural, id)],
2063                format!("rule (\"{id}\") {{}}"),
2064            )
2065            .with_support(support)
2066        };
2067        let shard = CensusShard::new(
2068            "state-tests",
2069            vec![
2070                feature_case(
2071                    "unsupported",
2072                    CensusSupport::Unsupported {
2073                        detail: "not declared".to_string(),
2074                    },
2075                ),
2076                feature_case(
2077                    "known-gap",
2078                    CensusSupport::KnownGap {
2079                        detail: "missing mapping".to_string(),
2080                    },
2081                ),
2082                feature_case(
2083                    "inconclusive",
2084                    CensusSupport::Inconclusive {
2085                        detail: "no oracle".to_string(),
2086                    },
2087                ),
2088            ],
2089        )
2090        .unwrap();
2091        let report = Census::assemble(vec![shard])
2092            .unwrap()
2093            .run(&Catalog::builtin().unwrap());
2094        report
2095            .validate()
2096            .expect("states use the current conformance adapter");
2097        let json = report.to_json().unwrap();
2098        assert!(json.contains("unsupported"));
2099        assert!(json.contains("known-gap"));
2100        assert!(json.contains("inconclusive"));
2101    }
2102
2103    #[test]
2104    fn builtin_census_report_validates_against_the_catalog() {
2105        let catalog = Catalog::builtin().expect("builtin catalog");
2106        let census = Census::builtin(&catalog).expect("census");
2107        let report = census.run(&catalog);
2108        report
2109            .validate_against(&catalog)
2110            .expect("census results use canonical catalog identities");
2111        assert_eq!(report.census, census.identity());
2112        assert_eq!(
2113            report.results.len(),
2114            census.cases().count(),
2115            "census drops or duplicates cases"
2116        );
2117        let localization_cases = census
2118            .cases()
2119            .filter(|case| case.case_id.starts_with("localization/"))
2120            .count();
2121        assert_eq!(
2122            report
2123                .results
2124                .iter()
2125                .filter(|result| result.case_id.starts_with("localization/"))
2126                .count(),
2127            localization_cases
2128        );
2129        assert!(
2130            localization_cases > 2,
2131            "localization shard must enumerate per-spelling cases"
2132        );
2133        assert!(
2134            report.results.iter().any(|result| {
2135                result.case_id.starts_with("localization/")
2136                    && result.status == ConformanceStatus::KnownGap
2137            }),
2138            "unwritable localized settings spellings must report as known gaps"
2139        );
2140    }
2141
2142    #[test]
2143    fn census_report_rejects_a_malformed_identity_digest() {
2144        let catalog = Catalog::builtin().expect("builtin catalog");
2145        let census = Census::assemble(vec![
2146            CensusShard::new(
2147                "state-tests",
2148                vec![CensusCase::probe(
2149                    "state-tests/probe".to_string(),
2150                    vec![feature(
2151                        FeatureNamespace::Wir,
2152                        FeatureKind::Structural,
2153                        "probe",
2154                    )],
2155                    "rule (\"probe\") {}".to_string(),
2156                )],
2157            )
2158            .unwrap(),
2159        ])
2160        .unwrap();
2161        let mut report = census.run(&catalog);
2162        report.census.digest = "not-a-digest".to_string();
2163
2164        let error = report
2165            .validate_against(&catalog)
2166            .expect_err("report identity must carry a SHA-256 digest");
2167        assert!(error.to_string().contains("SHA-256"));
2168    }
2169}
2170
2171#[cfg(test)]
2172mod probe_debug {
2173    use super::*;
2174
2175    #[test]
2176    fn dump_failing_localization_probes() {
2177        let catalog = Catalog::builtin().unwrap();
2178        let census = Census::builtin(&catalog).unwrap();
2179        let wanted = [
2180            "localization/es-mx/value/hero/0",
2181            "localization/fr-fr/value/hero/0",
2182        ];
2183        for case in census.cases() {
2184            if !wanted.contains(&case.case_id.as_str()) {
2185                continue;
2186            }
2187            println!("==== {} ====", case.case_id);
2188            println!("{}", case.source);
2189            let result = run_case(case, "localization", &catalog);
2190            println!(
2191                "-> {:?} {:?}",
2192                result.status,
2193                result.reason.map(|r| r.detail)
2194            );
2195        }
2196    }
2197}