i18n-le 0.3.1

Identify the i18n library a project uses, then audit its catalogs by that library's rules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
//! The checks.
//!
//! **One named source locale is the reference, never the union of all of
//! them.** A union is actively wrong: one locale's typo'd key would
//! become a requirement every other locale is then missing, so a single
//! translator's slip becomes twenty-four findings against everybody
//! else. English is the contract; the others are measured against it.
//!
//! Everything here runs with the library already identified. There is no
//! fallback path, no inference, and no "if we could not tell" branch —
//! the grammar, the plural model and the metadata rule all arrive as
//! values, and if identification could not produce them the run refused
//! before reaching this module.
//!
//! **No translated value crosses this boundary, and the types are what
//! stop it.** A finding carries `Evidence`, which has six variants —
//! token names, counts, styles, shapes, occurrence counts and a
//! construct with a byte offset. None of them can hold a sentence, so
//! there is nothing to remember not to put in one. An `untranslated`
//! finding is proved by "source and target are byte-identical", which is
//! a fact about the text rather than the text.
//!
//! Keys are the exception, and deliberately: a finding that will not
//! name its key is not a finding. Where the layout says the key *is* the
//! English string — the VS Code bundle shape — English reaches the
//! report. A translation never does.

use std::collections::{HashMap, HashSet};

use serde::Serialize;

use crate::catalogue::{Duplicate, Entry, Shape};
use crate::library::{Id, Interpolation, Library, Plurals};
use crate::message::{self, Construct, Token};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum Severity {
    Error,
    Warning,
    /// Does not fail a run on its own. A string added to English today
    /// is legitimately untranslated until the next translation pass, and
    /// a tool that broke the build for it would be turned off.
    Info,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum Kind {
    MissingKey,
    ExtraKey,
    PlaceholderCountMismatch,
    PlaceholderNameMismatch,
    /// The target wrote an interpolation in a style this library does
    /// not read, where the source had a real placeholder.
    PlaceholderStyleMismatch,
    /// A construct from a convention this project does not use. The
    /// loader renders it to the user verbatim.
    ConventionMismatch,
    EmptyValue,
    Untranslated,
    DuplicateKeyWithinFile,
    /// A path that is an object in one locale and a string in another.
    /// Invisible to flatten-then-compare, which is why it is a check.
    StructureMismatch,
}

/// What proves a finding.
///
/// **Every variant is metadata.** Counts, styles, shapes, token names,
/// occurrence counts and byte offsets are facts *about* the strings;
/// none of them is a string a translator wrote. There is no variant to
/// add a sentence to, which is the boundary enforced rather than
/// remembered.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(untagged)]
pub(crate) enum Evidence {
    Tokens {
        #[serde(rename = "sourceTokens")]
        source: Vec<String>,
        #[serde(rename = "targetTokens")]
        target: Vec<String>,
    },
    Counts {
        #[serde(rename = "sourceCount")]
        source: usize,
        #[serde(rename = "targetCount")]
        target: usize,
    },
    Styles {
        #[serde(rename = "sourceStyle")]
        source: Interpolation,
        #[serde(rename = "targetStyle")]
        target: Interpolation,
    },
    Shapes {
        #[serde(rename = "sourceShape")]
        source: Shape,
        #[serde(rename = "targetShape")]
        target: Shape,
    },
    Occurrences {
        occurrences: usize,
    },
    Foreign {
        construct: Construct,
        offset: usize,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub(crate) struct Finding {
    pub(crate) severity: Severity,
    pub(crate) kind: Kind,
    pub(crate) file: String,
    pub(crate) key: String,
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub(crate) evidence: Option<Evidence>,
}

/// One catalogue, as the audit sees it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Catalogue {
    pub(crate) path: String,
    pub(crate) locale: Option<String>,
    pub(crate) entries: Vec<Entry>,
    pub(crate) duplicates: Vec<Duplicate>,
}

impl Catalogue {
    /// Translatable keys. The objects on the way down are structure, not
    /// something anybody translates.
    pub(crate) fn key_count(&self) -> usize {
        self.entries
            .iter()
            .filter(|entry| entry.shape != Shape::Object)
            .count()
    }

    /// Drop the keys this library calls metadata.
    ///
    /// Done here rather than in the reader because identification reads
    /// the raw key set: `@greeting` beside `greeting` is the evidence
    /// that says the file is ARB, and a reader that had already dropped
    /// it would have thrown that away.
    pub(crate) fn without_metadata(mut self, library: &Library) -> Self {
        self.entries
            .retain(|entry| !library.is_metadata(&entry.key));
        self.duplicates
            .retain(|duplicate| !library.is_metadata(&duplicate.key));
        self
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct Options {
    pub(crate) library: Id,
    /// The key itself is the English string, as in a VS Code
    /// `bundle.l10n.json`. A property of the layout, so it arrives here
    /// rather than being read off the library.
    pub(crate) keys_are_source: bool,
}

/// A catalogue set with its reference already resolved.
///
/// **The invariant is the type's, not the caller's.** `audit` used to
/// take a bare `usize` and index with it, so "the resolver only ever
/// hands back a position that is there" was a promise kept by whoever
/// called it — and a source outside the set was `index out of bounds`
/// rather than an answer. The only constructor checks once; nothing
/// after it indexes anything.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Set<'a> {
    files: &'a [Catalogue],
    /// Where the reference sits, kept to tell it apart while iterating
    /// and to line up with anything built alongside `files`. Never used
    /// to look a catalogue up.
    index: usize,
    source: &'a Catalogue,
}

impl<'a> Set<'a> {
    pub(crate) fn new(files: &'a [Catalogue], index: usize) -> Option<Self> {
        Some(Self {
            files,
            index,
            source: files.get(index)?,
        })
    }

    /// Where the reference sits, for a caller lining something up
    /// alongside `files` — the report's summaries. Whoever takes it must
    /// `get` with it, never `[]`.
    pub(crate) fn index(self) -> usize {
        self.index
    }
}

/// Audit every catalogue in the set against its source.
pub(crate) fn audit(set: Set<'_>, options: Options) -> Vec<Finding> {
    let library = options.library.library();
    let reference = set.source;

    let mut findings = Vec::new();
    findings.extend(convention_findings(reference, options));

    for (index, file) in set.files.iter().enumerate() {
        if index == set.index {
            findings.extend(duplicate_findings(file));
            findings.extend(empty_findings(file));
            continue;
        }
        against(reference, file, library, options, &mut findings);
    }
    findings
}

/// Constructs from another convention. The loader renders them verbatim,
/// which is a defect the identified library makes nameable.
fn convention_findings(file: &Catalogue, options: Options) -> Vec<Finding> {
    let grammar = options.library.library().grammar;
    file.entries
        .iter()
        .filter_map(|entry| source_text(entry, options.keys_are_source).map(|t| (&entry.key, t)))
        .flat_map(|(key, text)| {
            message::parse(grammar, text)
                .foreign
                .into_iter()
                .map(move |found| Finding {
                    severity: Severity::Error,
                    kind: Kind::ConventionMismatch,
                    file: file.path.clone(),
                    key: key.clone(),
                    evidence: Some(Evidence::Foreign {
                        construct: found.construct,
                        offset: found.offset,
                    }),
                })
        })
        .collect()
}

/// A key the file defines twice. The loader keeps the last, so the other
/// translation is text nobody will ever see.
fn duplicate_findings(file: &Catalogue) -> impl Iterator<Item = Finding> + '_ {
    file.duplicates.iter().map(|duplicate| Finding {
        severity: Severity::Error,
        kind: Kind::DuplicateKeyWithinFile,
        file: file.path.clone(),
        key: duplicate.key.clone(),
        evidence: Some(Evidence::Occurrences {
            occurrences: duplicate.occurrences,
        }),
    })
}

fn empty_findings(file: &Catalogue) -> impl Iterator<Item = Finding> + '_ {
    file.entries
        .iter()
        .filter(|entry| {
            entry
                .text
                .as_ref()
                .is_some_and(|text| text.trim().is_empty())
        })
        .map(|entry| Finding {
            severity: Severity::Warning,
            kind: Kind::EmptyValue,
            file: file.path.clone(),
            key: entry.key.clone(),
            evidence: None,
        })
}

fn against(
    reference: &Catalogue,
    target: &Catalogue,
    library: &Library,
    options: Options,
    findings: &mut Vec<Finding>,
) {
    findings.extend(duplicate_findings(target));
    // A key whose text is already the finding is not compared further:
    // a `convention-mismatch` is the root cause, and a count difference
    // on top of it is the same defect reported twice.
    let conventions = convention_findings(target, options);
    let mismatched: HashSet<String> = conventions
        .iter()
        .map(|finding| finding.key.clone())
        .collect();
    findings.extend(conventions);

    let index: HashMap<&str, &Entry> = target
        .entries
        .iter()
        .map(|entry| (entry.key.as_str(), entry))
        .collect();

    // Structure first, and everything below a divergence is suppressed.
    // When `a` is an object here and a string there, every leaf under
    // `a` is also "missing" — reporting forty of those instead of the
    // one thing that is actually wrong buries the answer.
    let diverged = structure_findings(reference, &index, target, findings);

    // A `match` rather than a comparison, so a fifth plural model fails
    // the build here instead of silently being audited as if it had
    // none.
    let fold = match library.plurals {
        Plurals::KeySuffix => true,
        Plurals::Icu | Plurals::None => false,
    };
    let source_bases = bases(reference, fold);
    let target_bases = bases(target, fold);
    findings.extend(absent(
        reference,
        &target_bases,
        &diverged,
        fold,
        &target.path,
        Kind::MissingKey,
    ));
    findings.extend(absent(
        target,
        &source_bases,
        &diverged,
        fold,
        &target.path,
        Kind::ExtraKey,
    ));

    for entry in &reference.entries {
        let Some(source_text) = source_text(entry, options.keys_are_source) else {
            continue;
        };
        let Some(target_entry) = index.get(entry.key.as_str()) else {
            continue;
        };
        let Some(target_text) = target_entry.text.as_deref() else {
            continue;
        };
        // An empty translation is already the finding; comparing the
        // placeholders of a string that is not there would report the
        // same defect a second time under a different name.
        if target_text.trim().is_empty() {
            continue;
        }
        if !mismatched.contains(&entry.key)
            && let Some(finding) =
                placeholder_finding(&entry.key, source_text, target_text, library)
        {
            findings.push(Finding {
                file: target.path.clone(),
                ..finding
            });
            continue;
        }
        if !source_text.is_empty() && source_text == target_text {
            findings.push(Finding {
                severity: Severity::Info,
                kind: Kind::Untranslated,
                file: target.path.clone(),
                key: entry.key.clone(),
                evidence: None,
            });
        }
    }

    findings.extend(empty_findings(target));
}

/// Every path a file defines, reduced to its plural base.
///
/// **Objects count.** A path that is an object here and a string there
/// is a structure mismatch, not an extra key, and leaving objects out
/// made it both.
fn bases(file: &Catalogue, fold: bool) -> HashSet<&str> {
    file.entries
        .iter()
        .map(|entry| base_of(&entry.key, fold))
        .collect()
}

/// Keys of `file` whose base is not in `present`, deduplicated by base.
///
/// Reporting the base rather than the variant is what makes a plural
/// finding one line instead of six.
fn absent(
    file: &Catalogue,
    present: &HashSet<&str>,
    diverged: &[String],
    fold: bool,
    reported_as: &str,
    kind: Kind,
) -> Vec<Finding> {
    let mut seen: HashSet<&str> = HashSet::new();
    file.entries
        .iter()
        .filter(|entry| entry.shape != Shape::Object)
        .filter(|entry| !is_below(&entry.key, diverged))
        .filter_map(|entry| {
            let base = base_of(&entry.key, fold);
            (!present.contains(base) && seen.insert(base)).then(|| Finding {
                severity: Severity::Error,
                kind,
                file: reported_as.to_string(),
                key: base.to_string(),
                evidence: None,
            })
        })
        .collect()
}

fn base_of(key: &str, fold: bool) -> &str {
    if fold { message::plural_base(key) } else { key }
}

/// The paths that hold different kinds of thing in the two files.
fn structure_findings(
    reference: &Catalogue,
    index: &HashMap<&str, &Entry>,
    target: &Catalogue,
    findings: &mut Vec<Finding>,
) -> Vec<String> {
    let mut diverged = Vec::new();
    for entry in &reference.entries {
        let Some(other) = index.get(entry.key.as_str()) else {
            continue;
        };
        if other.shape == entry.shape {
            continue;
        }
        diverged.push(entry.key.clone());
        findings.push(Finding {
            severity: Severity::Error,
            kind: Kind::StructureMismatch,
            file: target.path.clone(),
            key: entry.key.clone(),
            evidence: Some(Evidence::Shapes {
                source: entry.shape,
                target: other.shape,
            }),
        });
    }
    diverged
}

fn is_below(key: &str, diverged: &[String]) -> bool {
    diverged.iter().any(|prefix| {
        key.starts_with(prefix.as_str()) && key.as_bytes().get(prefix.len()) == Some(&b'.')
    })
}

/// The one placeholder finding a key deserves, or none.
///
/// Style before count before names: a target that wrote its
/// interpolation in another style has token names that are not
/// comparable to the source's, so reporting a name-set difference on top
/// would be two findings for one mistake.
fn placeholder_finding(
    key: &str,
    source: &str,
    target: &str,
    library: &Library,
) -> Option<Finding> {
    let grammar = library.grammar;
    let source_parsed = message::parse(grammar, source);
    let target_parsed = message::parse(grammar, target);

    // A `{name}` in an i18next catalogue is prose — until the source has
    // a real placeholder at the same key, at which point it is a
    // translator having written the wrong braces.
    if !source_parsed.tokens.is_empty()
        && let Some(other) = target_parsed.others.first()
    {
        return Some(finding(
            key,
            Kind::PlaceholderStyleMismatch,
            Evidence::Styles {
                source: grammar.interpolation,
                target: *other,
            },
        ));
    }
    if source_parsed.tokens.len() != target_parsed.tokens.len() {
        return Some(finding(
            key,
            Kind::PlaceholderCountMismatch,
            Evidence::Counts {
                source: source_parsed.tokens.len(),
                target: target_parsed.tokens.len(),
            },
        ));
    }

    // The failure machine translation produces: same count, renamed
    // token. `{{timeframe}}` comes back as `{{periodo}}`, compiles, and
    // renders the literal to a user.
    let (source_names, target_names) = (names(&source_parsed.tokens), names(&target_parsed.tokens));
    (source_names != target_names).then(|| {
        finding(
            key,
            Kind::PlaceholderNameMismatch,
            Evidence::Tokens {
                source: source_names,
                target: target_names,
            },
        )
    })
}

fn finding(key: &str, kind: Kind, evidence: Evidence) -> Finding {
    Finding {
        severity: Severity::Error,
        kind,
        file: String::new(),
        key: key.to_string(),
        evidence: Some(evidence),
    }
}

fn names(tokens: &[Token]) -> Vec<String> {
    let mut names: Vec<String> = tokens.iter().map(|token| token.name.clone()).collect();
    names.sort_unstable();
    names.dedup();
    names
}

/// Which text of an entry is the English one.
fn source_text(entry: &Entry, keys_are_source: bool) -> Option<&str> {
    if entry.shape != Shape::Text {
        return None;
    }
    if keys_are_source {
        return Some(entry.key.as_str());
    }
    entry.text.as_deref()
}

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

    fn catalogue(path: &str, content: &str) -> Catalogue {
        let parsed = catalogue::parse(content).expect("parses");
        Catalogue {
            path: path.to_string(),
            locale: crate::locale::canonicalise(path.trim_end_matches(".json")),
            entries: parsed.entries,
            duplicates: parsed.duplicates,
        }
    }

    fn under(library: Id, source: &str, target: &str) -> Vec<Finding> {
        let files = [
            catalogue("en.json", source).without_metadata(library.library()),
            catalogue("es.json", target).without_metadata(library.library()),
        ];
        audit(
            Set::new(&files, 0).expect("a source in the set"),
            Options {
                library,
                keys_are_source: false,
            },
        )
    }

    fn kinds(findings: &[Finding]) -> Vec<Kind> {
        findings.iter().map(|finding| finding.kind).collect()
    }

    /// **The audit cannot be asked to compare against a catalogue that
    /// is not there.** It took a bare index and used it, so a resolver
    /// bug was `index out of bounds` — a panic, in a tool whose product
    /// is an exit code a shell branches on. `Set` is the only door in
    /// and it is checked once, on the way through.
    #[test]
    fn a_source_outside_the_set_is_refused_at_the_door() {
        let files = [catalogue("en.json", r#"{"a":"one"}"#)];
        assert!(Set::new(&files, 1).is_none(), "one past the end");
        assert!(Set::new(&files, 9).is_none());
        assert!(Set::new(&[], 0).is_none(), "an empty set has no source");
        let set = Set::new(&files, 0).expect("a source in the set");
        assert_eq!(set.index(), 0);
        assert_eq!(set.source.path, "en.json");
    }

    #[test]
    fn matching_catalogues_have_nothing_to_say() {
        assert_eq!(under(Id::I18next, r#"{"a":"one"}"#, r#"{"a":"uno"}"#), []);
    }

    #[test]
    fn a_key_the_target_lacks_is_missing() {
        let findings = under(Id::I18next, r#"{"a":"one","b":"two"}"#, r#"{"a":"uno"}"#);
        assert_eq!(kinds(&findings), [Kind::MissingKey]);
        assert_eq!(findings[0].key, "b");
        assert_eq!(findings[0].file, "es.json");
    }

    #[test]
    fn a_key_the_source_lacks_is_extra() {
        let findings = under(Id::I18next, r#"{"a":"one"}"#, r#"{"a":"uno","z":"zeta"}"#);
        assert_eq!(kinds(&findings), [Kind::ExtraKey]);
        assert_eq!(findings[0].key, "z");
    }

    /// **The check both hand-rolled parity tests in this tree miss.**
    /// Flatten-then-compare cannot see it, because after flattening the
    /// two files simply have different key sets.
    #[test]
    fn an_object_in_one_locale_and_a_string_in_another_is_a_structure_mismatch() {
        let findings = under(Id::I18next, r#"{"a":{"b":"one"}}"#, r#"{"a":"uno"}"#);
        assert_eq!(kinds(&findings), [Kind::StructureMismatch]);
        assert_eq!(
            findings[0].evidence,
            Some(Evidence::Shapes {
                source: Shape::Object,
                target: Shape::Text
            })
        );
    }

    #[test]
    fn nothing_below_a_structure_mismatch_is_reported_twice() {
        let findings = under(
            Id::I18next,
            r#"{"a":{"b":"one","c":"two","d":"three"}}"#,
            r#"{"a":"uno"}"#,
        );
        assert_eq!(kinds(&findings), [Kind::StructureMismatch]);
    }

    #[test]
    fn a_duplicated_key_is_reported_with_its_count() {
        let findings = under(Id::I18next, r#"{"a":"one"}"#, r#"{"a":"uno","a":"otra"}"#);
        assert_eq!(kinds(&findings), [Kind::DuplicateKeyWithinFile]);
        assert_eq!(
            findings[0].evidence,
            Some(Evidence::Occurrences { occurrences: 2 })
        );
    }

    #[test]
    fn an_empty_translation_is_a_warning_and_not_also_untranslated() {
        let findings = under(Id::I18next, r#"{"a":"one"}"#, r#"{"a":"   "}"#);
        assert_eq!(kinds(&findings), [Kind::EmptyValue]);
        assert_eq!(findings[0].severity, Severity::Warning);
        assert_eq!(
            kinds(&under(Id::I18next, r#"{"a":""}"#, r#"{"a":""}"#)),
            [Kind::EmptyValue, Kind::EmptyValue]
        );
    }

    #[test]
    fn a_byte_identical_translation_is_information() {
        let findings = under(Id::I18next, r#"{"a":"Dashboard"}"#, r#"{"a":"Dashboard"}"#);
        assert_eq!(kinds(&findings), [Kind::Untranslated]);
        assert_eq!(findings[0].severity, Severity::Info);
    }

    #[test]
    fn a_dropped_placeholder_is_a_count_mismatch() {
        let findings = under(Id::I18next, r#"{"a":"{{x}} of {{y}}"}"#, r#"{"a":"{{x}}"}"#);
        assert_eq!(kinds(&findings), [Kind::PlaceholderCountMismatch]);
        assert_eq!(
            findings[0].evidence,
            Some(Evidence::Counts {
                source: 2,
                target: 1
            })
        );
    }

    /// The failure machine translation actually produces.
    #[test]
    fn a_renamed_placeholder_is_a_name_mismatch() {
        let findings = under(
            Id::I18next,
            r#"{"a":"in {{timeframe}}"}"#,
            r#"{"a":"en {{periodo}}"}"#,
        );
        assert_eq!(kinds(&findings), [Kind::PlaceholderNameMismatch]);
        assert_eq!(
            findings[0].evidence,
            Some(Evidence::Tokens {
                source: vec!["timeframe".to_string()],
                target: vec!["periodo".to_string()],
            })
        );
    }

    /// A `{name}` in an i18next file is prose — until the source had a
    /// real placeholder at the same key.
    #[test]
    fn a_wrong_brace_style_is_a_style_mismatch_only_where_a_placeholder_belonged() {
        let findings = under(Id::I18next, r#"{"a":"{{name}}"}"#, r#"{"a":"{name}"}"#);
        assert_eq!(kinds(&findings), [Kind::PlaceholderStyleMismatch]);
        assert_eq!(
            findings[0].evidence,
            Some(Evidence::Styles {
                source: Interpolation::DoubleBrace,
                target: Interpolation::SingleBrace
            })
        );

        let prose = under(
            Id::I18next,
            r#"{"a":"Use braces"}"#,
            r#"{"a":"Escribe {llaves}"}"#,
        );
        assert_eq!(kinds(&prose), [], "no placeholder belonged there");
    }

    /// **The finding the identification makes possible.** An ICU plural
    /// in an i18next project renders its braces to the user.
    #[test]
    fn a_construct_from_another_convention_is_a_finding() {
        let findings = under(
            Id::I18next,
            r#"{"a":"{n, plural, other {#}}","b":"in {{timeframe}}"}"#,
            r#"{"a":"x","b":"en {{periodo}}"}"#,
        );
        assert_eq!(
            kinds(&findings),
            [Kind::ConventionMismatch, Kind::PlaceholderNameMismatch]
        );
        assert_eq!(findings[0].file, "en.json");
        assert_eq!(
            findings[0].evidence,
            Some(Evidence::Foreign {
                construct: Construct::IcuArgument,
                offset: 0
            })
        );
    }

    #[test]
    fn a_convention_mismatch_suppresses_the_placeholder_check_for_that_key() {
        let findings = under(
            Id::I18next,
            r#"{"a":"{{count}} items"}"#,
            r#"{"a":"{count, plural, other {# elementos}}"}"#,
        );
        assert_eq!(kinds(&findings), [Kind::ConventionMismatch]);
        assert_eq!(findings[0].file, "es.json");
    }

    /// The same construct under the library that writes it is ordinary.
    #[test]
    fn an_icu_plural_is_compared_as_one_token_under_an_icu_library() {
        assert_eq!(
            kinds(&under(
                Id::NextIntl,
                r#"{"a":"{count, plural, one {# item} other {# items}}"}"#,
                r#"{"a":"{count, plural, other {# elementos}}"}"#,
            )),
            []
        );
        assert_eq!(
            kinds(&under(
                Id::NextIntl,
                r#"{"a":"{count, plural, one {# item} other {# items}}"}"#,
                r#"{"a":"elementos"}"#,
            )),
            [Kind::PlaceholderCountMismatch]
        );
    }

    /// **Plural key suffixes.** Polish needs `_few` and Japanese needs
    /// only `_other`; requiring English's exact variant set would report
    /// a finding on every correctly translated plural in the project.
    #[test]
    fn plural_variants_are_one_key_where_the_library_writes_them_that_way() {
        assert_eq!(
            kinds(&under(
                Id::I18next,
                r#"{"item_one":"{{count}} item","item_other":"{{count}} items"}"#,
                r#"{"item_one":"{{count}} element","item_few":"{{count}} elementy","item_many":"{{count}} elementów","item_other":"{{count}} elementu"}"#,
            )),
            []
        );
        let gone = under(
            Id::I18next,
            r#"{"item_one":"{{count}} item","item_other":"{{count}} items"}"#,
            r#"{"other":"x"}"#,
        );
        assert_eq!(kinds(&gone), [Kind::MissingKey, Kind::ExtraKey]);
        assert_eq!(gone[0].key, "item", "the base, named once");
    }

    /// And under a library whose plurals live in the message, the
    /// suffixes are ordinary keys.
    #[test]
    fn plural_variants_are_ordinary_keys_under_an_icu_library() {
        assert_eq!(
            kinds(&under(
                Id::NextIntl,
                r#"{"item_one":"a","item_other":"b"}"#,
                r#"{"item_one":"c","item_few":"d","item_other":"e"}"#,
            )),
            [Kind::ExtraKey]
        );
    }

    /// ARB's `@key` siblings are metadata, and comparing them would
    /// report a finding on every message in the file.
    #[test]
    fn arb_metadata_is_not_compared() {
        let findings = under(
            Id::FlutterArb,
            r#"{"@@locale":"en","greeting":"Hi {name}","@greeting":{"description":"a"}}"#,
            r#"{"@@locale":"es","greeting":"Hola {name}"}"#,
        );
        assert_eq!(kinds(&findings), []);
    }

    /// The VS Code bundle shape: the key is the English sentence.
    #[test]
    fn keys_can_be_the_source() {
        let library = Id::VscodeL10n;
        let files = [
            catalogue("bundle.l10n.json", r#"{"Save {0}":"Save {0}"}"#),
            catalogue("bundle.l10n.de.json", r#"{"Save {0}":"Save {0}"}"#),
        ];
        let findings = audit(
            Set::new(&files, 0).expect("a source in the set"),
            Options {
                library,
                keys_are_source: true,
            },
        );
        assert_eq!(kinds(&findings), [Kind::Untranslated]);
    }

    /// **The property the whole crate rests on.**
    #[test]
    fn no_translated_value_reaches_the_report() {
        let findings = under(
            Id::I18next,
            r#"{"a":"in {{timeframe}}","b":"Dashboard","c":"one"}"#,
            r#"{"a":"en {{periodo}}","b":"Dashboard","c":"uno","d":"sobrante"}"#,
        );
        let rendered = serde_json::to_string(&findings).expect("serializes");
        for translated in ["en {{periodo}}", "uno", "sobrante", "Dashboard"] {
            assert!(!rendered.contains(translated), "{rendered}");
        }
        assert!(rendered.contains("periodo"), "the token is the finding");
        assert!(rendered.contains(r#""key":"d""#), "the key is the finding");
    }

    #[test]
    fn a_convention_mismatch_carries_no_value_either() {
        let findings = under(
            Id::I18next,
            r#"{"a":"one"}"#,
            r#"{"a":"{n, plural, other {# elementos secretos}}"}"#,
        );
        let rendered = serde_json::to_string(&findings).expect("serializes");
        assert!(!rendered.contains("elementos"), "{rendered}");
        assert!(rendered.contains("icu-argument"), "{rendered}");
    }

    #[test]
    fn a_single_catalogue_has_nothing_to_compare_against() {
        let files = [catalogue("en.json", r#"{"a":"one"}"#)];
        assert_eq!(
            audit(
                Set::new(&files, 0).expect("a source in the set"),
                Options {
                    library: Id::I18next,
                    keys_are_source: false
                }
            ),
            []
        );
    }
}