opy-rs 0.3.9

Standalone OverPy-compatible .opy implementation with parsing, tooling, and bounded Workshop compilation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//! The shared nearest-spelling matcher for rejected OPY input (issue #469).
//!
//! One fold/score/rank pipeline produces the `did you mean` candidates on
//! `unknown-action`, `unknown-value`, `unknown-member`, `unknown-enum-member`
//! and rejected settings keys, over the same manifest, catalog and settings
//! data the compiler resolves against — the owner ships no second name
//! table. The vocabulary lookup (issue #465) ranks through this matcher, so
//! a rejection's candidates match the top entries of a lookup on the
//! rejected spelling.
//!
//! Every returned list is deterministic: equal scores order by the
//! candidate's spelling, and lists are bounded to [`CANDIDATE_LIMIT`].

use workshop_rs::catalog::{Catalog, Kind, Locale};
use workshop_rs::settings::{self, PathPart, SettingValueDomain};

use crate::manifest::{AliasKind, Function, FunctionKind, Manifest};

/// The bound on any reported candidate list.
pub(crate) const CANDIDATE_LIMIT: usize = 8;

/// One reportable spelling plus every form it matches on: the owner
/// spelling itself, `en-US` catalog display names, and reviewed source
/// aliases. Display-name forms keep display-name guesses ("Create HUD
/// Text") fold-exact against the spelling they name (`hudText`).
pub(crate) struct MatchCandidate {
    spelling: String,
    forms: Vec<String>,
}

impl MatchCandidate {
    /// A candidate that matches on its spelling alone (user-defined names,
    /// settings keys, enum members without a catalog display name).
    fn bare(spelling: impl Into<String>) -> MatchCandidate {
        let spelling = spelling.into();
        MatchCandidate {
            forms: vec![spelling.clone()],
            spelling,
        }
    }

    /// A manifest-declared callable: its source id and the `en-US` display
    /// spellings of its catalog entry.
    fn function(function: &Function, catalog: &Catalog) -> MatchCandidate {
        let mut forms = vec![function.id.clone()];
        if let Some(catalog_id) = &function.catalog_id {
            let kind = match function.kind {
                FunctionKind::Action | FunctionKind::MemberAction => Kind::Action,
                FunctionKind::Value | FunctionKind::MemberValue => Kind::Value,
            };
            if let Some(entry) = catalog.entry(kind, catalog_id) {
                let locale = Locale::new("en-US");
                forms.extend(entry.spellings(&locale).iter().cloned());
            }
        }
        MatchCandidate {
            spelling: function.id.clone(),
            forms,
        }
    }
}

/// Bare candidates for spellings that have no catalog display forms.
pub(crate) fn bare_candidates<I, S>(spellings: I) -> Vec<MatchCandidate>
where
    I: IntoIterator<Item = S>,
    S: Into<String>,
{
    spellings.into_iter().map(MatchCandidate::bare).collect()
}

/// The value-position call spellings an `unknown-value` diagnostic can
/// suggest: manifest values, their reviewed aliases, and the by-name
/// special calls (`sorted`, `createWorkshopSetting*`).
pub(crate) fn value_candidates(manifest: &Manifest, catalog: &Catalog) -> Vec<MatchCandidate> {
    // Member values join the pool too: a receiverless guess still finds the
    // name (`startForcingPlayerToBeHero` names `startForcingHero`); a wrong
    // call shape is a separate diagnostic axis from the spelling.
    let mut pool = function_candidates(manifest, catalog, FunctionKind::is_value);
    pool.extend(
        crate::lower::special_forms::SPECIAL_VALUE_CALLS
            .iter()
            .map(|(spelling, _)| MatchCandidate::bare(*spelling)),
    );
    pool
}

/// The statement-position call spellings an `unknown-action` diagnostic
/// can suggest, member actions included (see [`value_candidates`]).
pub(crate) fn action_candidates(manifest: &Manifest, catalog: &Catalog) -> Vec<MatchCandidate> {
    function_candidates(manifest, catalog, FunctionKind::is_action)
}

/// The member-call spellings an `unknown-member` diagnostic can suggest
/// (`eventPlayer.member(...)`, `player.member`).
pub(crate) fn member_candidates(manifest: &Manifest, catalog: &Catalog) -> Vec<MatchCandidate> {
    function_candidates(manifest, catalog, FunctionKind::is_member)
}

/// The members of a builtin domain an `unknown-enum-member` diagnostic
/// can suggest. The pool is the resolver's own acceptance surface:
/// [`crate::enums::domain_members`] reports the canonical OPY spellings
/// (so `SOLDIER`, never the catalog id `SOLDIER_76`), the reference
/// aliases the same table declares (`MCCREE`), and the `en-US` display
/// names.
pub(crate) fn enum_member_candidates(
    catalog: &Catalog,
    source_domain: &str,
) -> Vec<MatchCandidate> {
    let mut pool = Vec::new();
    for member in crate::enums::domain_members(source_domain, catalog).unwrap_or_default() {
        let mut forms = vec![member.member.clone()];
        forms.extend(member.display_name.iter().cloned());
        pool.push(MatchCandidate {
            spelling: member.member,
            forms,
        });
        pool.extend(member.aliases.iter().cloned().map(MatchCandidate::bare));
    }
    pool
}

fn function_candidates(
    manifest: &Manifest,
    catalog: &Catalog,
    position: impl Fn(FunctionKind) -> bool,
) -> Vec<MatchCandidate> {
    let mut pool: Vec<MatchCandidate> = manifest
        .functions()
        .iter()
        .filter(|function| position(function.kind))
        .map(|function| MatchCandidate::function(function, catalog))
        .collect();
    // A declared source alias is an accepted spelling in its own right, so it
    // is a candidate of its own (`buttonStr` names `buttonString`, not the
    // alias's canonical target).
    pool.extend(
        manifest
            .aliases()
            .iter()
            .filter(|alias| {
                alias_target(alias, manifest).is_some_and(|target| position(target.kind))
            })
            .map(|alias| MatchCandidate::bare(alias.source.clone())),
    );
    pool
}

/// The function an alias resolves to, through its declared kind.
fn alias_target<'a>(
    alias: &crate::manifest::Alias,
    manifest: &'a Manifest,
) -> Option<&'a Function> {
    match alias.kind {
        AliasKind::FunctionAlias => manifest.function(&alias.target),
        AliasKind::MemberAlias => manifest.member(&alias.target),
    }
}

// ---------------------------------------------------------------------------
// Ranking — fold both sides to lowercase alphanumerics, then score
// ---------------------------------------------------------------------------

/// Fold a spelling or phrase to a comparable form: lowercase letters and
/// digits only; `_`, `.`, `:`, `%`, and whitespace are dropped. The
/// lookup (`crate::lookup`) folds through this same function.
pub(crate) fn fold(text: &str) -> String {
    text.chars()
        .filter(|c| c.is_alphanumeric())
        .flat_map(|c| c.to_lowercase())
        .collect()
}

/// Score a query against one match form; lower is closer, `None` is
/// unrelated. Exact folded equality beats prefix beats substring beats
/// edit distance. The lookup scores its hit forms through this same
/// function.
pub(crate) fn score(query: &str, form: &str) -> Option<u32> {
    let query = fold(query);
    let form = fold(form);
    if query.is_empty() || form.is_empty() {
        return None;
    }
    if query == form {
        return Some(0);
    }
    if form.starts_with(&query) {
        return Some((form.len() - query.len()) as u32 + 2);
    }
    if query.starts_with(&form) {
        return Some((query.len() - form.len()) as u32 + 4);
    }
    // Substring matching on very short spellings is pure noise (a one-letter
    // display name like Icon's "X" hides inside unrelated folds).
    if form.len().min(query.len()) >= 3 && (form.contains(&query) || query.contains(&form)) {
        return Some((form.len().max(query.len()) - form.len().min(query.len())) as u32 + 10);
    }
    let distance = levenshtein(&query, &form);
    // Short names need tight edits; longer names admit proportionally
    // larger distance (a ≥50%-similar guess like `startForcingPlayerToBeHero`
    // still finds `startForcingHero`).
    let close = distance * 3 <= query.len() as u32 + 1
        || distance * 2 <= query.len().max(form.len()) as u32;
    close.then_some(distance * 10 + 20)
}

/// Segment-aware scoring for a settings path: the query's dot-separated
/// segments must be a prefix of the path's segments, where a template
/// segment (`<team>`, `<hero>`) accepts any segment spelling and the last
/// query segment may be a prefix of its path segment. `None` when the
/// query is not a path prefix — near-name ranking falls back to
/// [`score`]. Lookup settings queries score `MatchKind::Path` forms
/// through this.
pub(crate) fn path_score(query: &str, path: &str) -> Option<u32> {
    let segments: Vec<String> = query.trim().split('.').map(fold).collect();
    if segments.iter().any(String::is_empty) {
        return None;
    }
    let path_segments: Vec<&str> = path.split('.').collect();
    if segments.len() > path_segments.len() {
        return None;
    }
    let mut penalty = 0u32;
    for (index, segment) in segments.iter().enumerate() {
        let target = path_segments[index];
        if (target.starts_with('<') && target.ends_with('>')) || fold(target) == *segment {
            continue;
        }
        let target_folded = fold(target);
        if index + 1 == segments.len() && target_folded.starts_with(segment.as_str()) {
            penalty += (target_folded.len() - segment.len()) as u32 + 2;
            continue;
        }
        return None;
    }
    Some(penalty + (path_segments.len() - segments.len()) as u32)
}

fn levenshtein(a: &str, b: &str) -> u32 {
    let a: Vec<char> = a.chars().collect();
    let b: Vec<char> = b.chars().collect();
    let mut row: Vec<u32> = (0..=b.len() as u32).collect();
    for (i, &x) in a.iter().enumerate() {
        let mut prev = row[0];
        row[0] = i as u32 + 1;
        for (j, &y) in b.iter().enumerate() {
            let next = (row[j] + 1)
                .min(row[j + 1] + 1)
                .min(prev + u32::from(x != y));
            prev = row[j + 1];
            row[j + 1] = next;
        }
    }
    row[b.len()]
}

/// Rank `pool` by closeness to `query`, best first, bounded to
/// [`CANDIDATE_LIMIT`]. A candidate's score is its best match form; the
/// reported text is always the owner spelling. Ties order by spelling so
/// repeated runs return the same list in the same order.
pub(crate) fn rank(query: &str, pool: &[MatchCandidate]) -> Vec<String> {
    let mut scored: Vec<(u32, &str)> = pool
        .iter()
        .filter_map(|candidate| {
            candidate
                .forms
                .iter()
                .filter_map(|form| score(query, form))
                .min()
                .map(|score| (score, candidate.spelling.as_str()))
        })
        .collect();
    scored.sort_by(|(a, x), (b, y)| a.cmp(b).then_with(|| x.cmp(y)));
    scored.dedup_by(|a, b| a.1 == b.1);
    scored
        .into_iter()
        .take(CANDIDATE_LIMIT)
        .map(|(_, spelling)| spelling.to_string())
        .collect()
}

/// Rank `pool`, falling back to the whole pool spelled out (sorted,
/// bounded) when nothing is near: for a closed candidate space — an
/// enum domain's members, the keys valid at one settings path — the
/// candidates are literally its members.
pub(crate) fn rank_closed(query: &str, pool: &[MatchCandidate]) -> Vec<String> {
    let ranked = rank(query, pool);
    if !ranked.is_empty() {
        return ranked;
    }
    let mut spellings: Vec<String> = pool
        .iter()
        .map(|candidate| candidate.spelling.clone())
        .collect();
    spellings.sort();
    spellings.dedup();
    spellings.truncate(CANDIDATE_LIMIT);
    spellings
}

/// Append a `(did you mean …?)` suffix naming the closest `ranked`
/// candidates (at most three; the list must already be [`rank`]ed). A
/// message that already ends in one — a canonical settings rejection
/// embeds its own suggestion — has it replaced, so the rejection reports
/// one candidate list, not two.
pub(crate) fn did_you_mean(message: String, ranked: &[String]) -> String {
    let Some((first, rest)) = ranked.split_first() else {
        return message;
    };
    let message = match message.rfind(" (did you mean ") {
        Some(at) if message.ends_with("?)") => &message[..at],
        _ => message.as_str(),
    };
    let others = rest
        .iter()
        .take(2)
        .map(|s| format!("'{s}'"))
        .collect::<Vec<_>>()
        .join(" or ");
    if others.is_empty() {
        format!("{message} (did you mean '{first}'?)")
    } else {
        format!("{message} (did you mean '{first}', {others}?)")
    }
}

// ---------------------------------------------------------------------------
// Settings candidates — the keys or members valid at the rejected path
// ---------------------------------------------------------------------------

/// The candidates a settings-emission rejection carries: the leaf keys or
/// enum members valid at the rejected member's path, resolved by walking
/// the source `settings` tree to the reported span. The owner's own single
/// `suggestion` (already in the message) is the candidate of record for
/// positions whose spelling pool is not enumerable through the public
/// settings API (hero group names, list element spellings).
pub(crate) fn settings_member_candidates(
    hir_settings: Option<&crate::hir::Settings>,
    error: &workshop_rs::WorkshopError,
    suggestion: Option<&str>,
) -> Vec<String> {
    let Some(hir_settings) = hir_settings else {
        return Vec::new();
    };
    let Some(span) = crate::compiler::workshop_error_span(error) else {
        return Vec::new();
    };
    let mut path = Vec::new();
    match find_settings_node(&hir_settings.children, &mut path, &span) {
        // A leaf member: the key either resolves (the value or kind is the
        // problem) or is itself outside the emission table.
        Some(SettingsMatch::Member { path, name }) => {
            let member_path: Vec<PathPart<'_>> = {
                let mut full = path;
                full.push(PathPart::Part(name));
                full
            };
            match settings::definition(&member_path) {
                Some(definition) => {
                    // A `BoolEnum` key is Boolean-typed over an enum domain
                    // and accepts `true` only.
                    if matches!(definition.domain(), SettingValueDomain::Boolean)
                        && definition.enum_members().next().is_some()
                    {
                        vec!["true".to_string()]
                    } else {
                        definition
                            .enum_members()
                            .take(CANDIDATE_LIMIT)
                            .map(|member| member.id().to_string())
                            .collect()
                    }
                }
                None => {
                    let keys = sibling_keys(&member_path[..member_path.len() - 1]);
                    rank_closed(name, &bare_candidates(keys.iter()))
                }
            }
        }
        // A hero group under `heroes.<team>`: hero names are the accepted
        // spelling pool, which the owner only exposes through `suggestion`.
        Some(SettingsMatch::Group { path })
            if matches!(
                path.as_slice(),
                [PathPart::Part("heroes"), PathPart::Team, PathPart::Hero]
            ) =>
        {
            suggestion.into_iter().map(str::to_string).collect()
        }
        // A team group or a structural rejection: no enumerable pool.
        Some(SettingsMatch::Group { .. }) => Vec::new(),
        // No node matched the reported span (list-element spans land here):
        // the owner's suggestion is the only available candidate.
        None => suggestion.into_iter().map(str::to_string).collect(),
    }
}

/// The leaf keys valid at `parent_path` — every declaration whose last part
/// resolves at that parent (so `gamemodes.ffa` inherits `gamemodes.general`).
pub(crate) fn sibling_keys(parent_path: &[PathPart<'_>]) -> Vec<String> {
    let mut keys: Vec<String> = Vec::new();
    for definition in settings::definitions() {
        let Some(leaf) = definition.path().rsplit('.').next() else {
            continue;
        };
        let mut probe = parent_path.to_vec();
        probe.push(PathPart::Part(leaf));
        if settings::definition(&probe).is_some() && !keys.iter().any(|key| key == leaf) {
            keys.push(leaf.to_string());
        }
    }
    keys
}

/// The settings node found at a reported span, with its emission-table path.
enum SettingsMatch<'a> {
    Member {
        path: Vec<PathPart<'a>>,
        name: &'a str,
    },
    Group {
        path: Vec<PathPart<'a>>,
    },
}

/// Walk the settings tree to the node whose span the rejection reported,
/// building the emission-table path (`gamemodes.<mode>`, `heroes.<team>`,
/// `heroes.<team>.<hero>`) on the way down.
fn find_settings_node<'a>(
    children: &'a [crate::hir::types::SettingsNode],
    path: &mut Vec<PathPart<'a>>,
    span: &workshop_rs::source::Span,
) -> Option<SettingsMatch<'a>> {
    use crate::hir::types::SettingsNode as Node;
    for node in children {
        if let Node::Group { name, children, .. } = node {
            path.push(settings_path_part(name, path));
            if let Some(found) = find_settings_node(children, path, span) {
                return Some(found);
            }
            if settings_span_matches(node.span(), span) {
                return Some(SettingsMatch::Group {
                    path: std::mem::take(path),
                });
            }
            path.pop();
        } else if settings_span_matches(node.span(), span) {
            return Some(SettingsMatch::Member {
                path: path.clone(),
                name: settings_member_name(node),
            });
        }
    }
    None
}

/// The path part a group name occupies, given the path so far.
fn settings_path_part<'a>(name: &'a str, path: &[PathPart<'a>]) -> PathPart<'a> {
    match path {
        [PathPart::Part("heroes")] => PathPart::Team,
        [PathPart::Part("heroes"), PathPart::Team] => PathPart::Hero,
        _ => PathPart::Part(name),
    }
}

/// Whether the HIR node span is the span a `WorkshopError` reported.
fn settings_span_matches(
    span: Option<&crate::hir::Span>,
    reported: &workshop_rs::source::Span,
) -> bool {
    let Some(span) = span else { return false };
    span.file == reported.file.index() as u32
        && span.start.line == reported.start.line
        && span.start.col == reported.start.col
        && span.end.line == reported.end.line
        && span.end.col == reported.end.col
}

fn settings_member_name(node: &crate::hir::types::SettingsNode) -> &str {
    use crate::hir::types::SettingsNode as Node;
    match node {
        Node::Number { name, .. }
        | Node::Bool { name, .. }
        | Node::String { name, .. }
        | Node::Raw { name, .. }
        | Node::List { name, .. } => name,
        Node::Group { .. } => unreachable!("groups are not leaf members"),
    }
}

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

    #[test]
    fn rank_is_deterministic_and_bounded() {
        let pool = bare_candidates((0..20).map(|i| format!("tolerant{i}")));
        let first = rank("tolerant", &pool);
        let second = rank("tolerant", &pool);
        assert_eq!(first, second);
        assert!(first.len() <= CANDIDATE_LIMIT);
        let mut sorted = first.clone();
        sorted.sort();
        assert_eq!(first, sorted, "equal scores order by spelling");
    }

    #[test]
    fn rank_prefers_exact_then_prefix_then_edit_distance() {
        let pool = bare_candidates(["waits", "wait", "weigh", "totally"]);
        assert_eq!(
            rank("wait", &pool)[..2],
            ["wait", "waits"],
            "exact beats prefix; unrelated names drop out"
        );
    }

    #[test]
    fn did_you_mean_replaces_an_embedded_suggestion() {
        // A canonical settings rejection already carries its own `did you
        // mean` suffix; the ranked candidates replace it instead of
        // stacking a second one.
        let base = "settings key 'main.descriptino' is outside the emission table \
                    (did you mean 'Description'?)"
            .to_string();
        assert_eq!(
            did_you_mean(base.clone(), &["description".to_string()]),
            "settings key 'main.descriptino' is outside the emission table \
             (did you mean 'description'?)"
        );
        // With no candidates the owner's suffix stays — it is the only
        // candidate information the rejection carries.
        assert_eq!(did_you_mean(base.clone(), &[]), base);
    }

    #[test]
    fn did_you_mean_formats_up_to_three_candidates() {
        let base = "unknown action 'x'".to_string();
        assert_eq!(did_you_mean(base.clone(), &[]), "unknown action 'x'");
        assert_eq!(
            did_you_mean(base.clone(), &["hudText".to_string()]),
            "unknown action 'x' (did you mean 'hudText'?)"
        );
        assert_eq!(
            did_you_mean(base.clone(), &["a".to_string(), "b".to_string()]),
            "unknown action 'x' (did you mean 'a', 'b'?)"
        );
        assert_eq!(
            did_you_mean(
                base,
                &[
                    "a".to_string(),
                    "b".to_string(),
                    "c".to_string(),
                    "d".to_string()
                ]
            ),
            "unknown action 'x' (did you mean 'a', 'b' or 'c'?)"
        );
    }

    #[test]
    fn function_candidates_match_display_names() {
        let manifest = Manifest::builtin().expect("builtin manifest");
        let catalog = Catalog::builtin().expect("builtin catalog");
        let pool = action_candidates(manifest, &catalog);
        // The Workshop display name "Create HUD Text" folds onto `hudText`.
        assert_eq!(rank("createHudText", &pool)[0], "hudText");
        // A receiverless guess at a member action still finds its name.
        assert_eq!(
            rank("startForcingPlayerToBeHero", &pool)[0],
            "startForcingHero"
        );
    }

    #[test]
    fn enum_member_candidates_cover_the_accepted_spelling_surface() {
        let catalog = Catalog::builtin().expect("builtin catalog");
        let pool = enum_member_candidates(&catalog, "Hero");
        let ranked = rank("SOLDIER76", &pool);
        assert_eq!(
            ranked.first().map(String::as_str),
            Some("SOLDIER"),
            "the canonical OPY spelling wins: {ranked:?}"
        );
        assert!(
            !ranked.iter().any(|spelling| spelling == "SOLDIER_76"),
            "the catalog id is never a candidate: {ranked:?}"
        );
        assert_eq!(rank("MCCEE", &pool)[0], "MCCREE");
        // Display names match too ("Blizzard World" names `Map.BLIZZ_WORLD`;
        // the catalog id `BLIZZARD_WORLD` is not one).
        let map_pool = enum_member_candidates(&catalog, "Map");
        let ranked = rank("blizzard world", &map_pool);
        assert_eq!(ranked[0], "BLIZZ_WORLD");
        assert!(!ranked.iter().any(|spelling| spelling == "BLIZZARD_WORLD"));
    }

    /// The candidate pool is exactly the resolver's reported surface: every
    /// `domain_members` spelling and alias, nothing else.
    #[test]
    fn enum_member_candidates_match_the_lookup_surface() {
        let catalog = Catalog::builtin().expect("builtin catalog");
        let mut expected: Vec<String> = Vec::new();
        for member in crate::enums::domain_members("Clip", &catalog).expect("Clip members") {
            expected.push(member.member);
            expected.extend(member.aliases);
        }
        let mut reported: Vec<String> = enum_member_candidates(&catalog, "Clip")
            .into_iter()
            .map(|candidate| candidate.spelling)
            .collect();
        expected.sort_unstable();
        reported.sort_unstable();
        assert_eq!(reported, expected);
    }

    #[test]
    fn path_score_matches_segment_by_segment() {
        // A template segment accepts any spelling and `%` stays in the leaf.
        assert_eq!(
            path_score(
                "heroes.team1.junkrat.health%",
                "heroes.<team>.<hero>.health%"
            ),
            Some(0)
        );
        // Path prefixes answer their path; non-prefixes fall back to the
        // folded near-name score.
        assert_eq!(
            path_score("gamemodes.ffa", "gamemodes.general.scoreToWin"),
            None
        );
        assert!(path_score("gamemodes.ffa", "gamemodes.ffa.scoreToWin").is_some());
        assert_eq!(path_score("health%", "heroes.<team>.<hero>.health%"), None);
    }
}