i18n-le 0.3.4

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
//! `check_catalogues` — the one tool this server offers.
//!
//! It takes catalogue *contents* directly and touches no filesystem. An
//! agent already has file-read tools; duplicating them here would add a
//! path-traversal surface for no capability.
//!
//! **The library is required, never detected.** Identification reads
//! manifests, config files, directory layouts and source call sites —
//! none of which exist on this surface. A tool that guessed instead
//! would be the thing this whole crate stopped doing, so the caller
//! names the library or gets a refusal.
//!
//! **Only keys, tokens and counts come back.** A translation catalogue
//! is a product's entire user-facing voice, and an agent asking whether
//! Spanish is complete does not need the Spanish. The privacy boundary
//! is the same one the CLI has, because it is the same code: findings
//! carry `Evidence`, and `Evidence` has no variant that holds a
//! sentence.

use serde_json::{Value, json};

use crate::library::{self, Id};
use crate::locale;
use crate::scan::{self, Document, ScanOptions, System};

const DEFAULT_MAX_RESULTS: usize = 500;
const MAX_MAX_RESULTS: usize = 5000;

pub(crate) fn definition() -> Value {
    json!({
        "name": "check_catalogues",
        "description": "Audit a set of translation catalogues against one of them and report what \
                        is structurally wrong: missing and extra keys, placeholders dropped or \
                        renamed in translation, constructs from another i18n convention, empty \
                        values, keys defined twice, and a path that is an object in one locale \
                        and a string in another. Takes file contents directly and reads no files. \
                        Only key names and structural facts are returned — never a translated \
                        string. The i18n library must be named: identifying it needs manifests, \
                        config files and source call sites, none of which this surface can see, \
                        and guessing the placeholder grammar from content is exactly what this \
                        tool does not do.",
        "inputSchema": {
            "type": "object",
            "properties": {
                "library": {
                    "type": "string",
                    "enum": library::names(),
                    "description": "Which i18n library wrote these catalogues. It supplies the \
                                    placeholder grammar, the plural model and which keys are \
                                    metadata.",
                },
                "files": {
                    "type": "array",
                    "minItems": 1,
                    "description": "The catalogues to audit. JSON, nested or flat.",
                    "items": {
                        "type": "object",
                        "properties": {
                            "path": {
                                "type": "string",
                                "description": "File name, e.g. \"pt-BR.json\" or \
                                                \"bundle.l10n.pt-br.json\". Used to label findings \
                                                and, when locale is absent, to work out which \
                                                locale this is.",
                            },
                            "locale": {
                                "type": "string",
                                "description": "The language tag, e.g. \"pt-BR\". Omit for the \
                                                base catalogue, or omit on every file to have it \
                                                read from the names.",
                            },
                            "content": { "type": "string", "description": "The file contents." },
                        },
                        "required": ["path", "content"],
                        "additionalProperties": false,
                    },
                },
                "source": {
                    "type": "string",
                    "description": "Which catalogue is the contract — a path or a language tag. \
                                    Without it, exactly one English candidate must exist or the \
                                    audit is refused rather than guessed at.",
                },
                "keysAreSource": {
                    "type": "boolean",
                    "default": false,
                    "description": "The key is itself the English string, as in a VS Code \
                                    bundle.l10n.json.",
                },
                "maxResults": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": MAX_MAX_RESULTS,
                    "default": DEFAULT_MAX_RESULTS,
                    "description": format!(
                        "Cap on returned findings (default {DEFAULT_MAX_RESULTS}). \
                         meta.truncated reports whether any were dropped."
                    ),
                },
            },
            "required": ["library", "files"],
            "additionalProperties": false,
        },
    })
}

pub(crate) fn run(arguments: &Value) -> Result<Value, String> {
    let library = read_library(arguments)?;
    let documents = read_files(arguments)?;
    let max_results = read_max_results(arguments)?;
    let keys_are_source = arguments
        .get("keysAreSource")
        .and_then(Value::as_bool)
        .unwrap_or(false);

    let mut report = scan::report_for(
        &documents,
        System {
            library,
            version: None,
            layout: None,
            keys_are_source,
            evidence: Vec::new(),
        },
        &ScanOptions {
            source: arguments
                .get("source")
                .and_then(Value::as_str)
                .map(str::to_string),
            keys_are_source,
        },
    )?;

    // The cap matters less than the flag: a silently shortened answer is
    // wrong in the most expensive way, and this is a tool whose whole
    // job is telling you what is absent.
    let truncated = report.findings.len() > max_results;
    let diagnostics: Vec<Value> = report
        .diagnostics
        .iter()
        .map(|diagnostic| {
            json!({
                "severity": diagnostic.severity,
                "code": diagnostic.code,
                "message": format!("{}: {}", diagnostic.file, diagnostic.message),
            })
        })
        .collect();

    report.findings.truncate(max_results);
    let count = report.findings.len();
    let data = serde_json::to_value(&report).expect("a report serializes");

    Ok(super::envelope(
        "check_catalogues",
        &data,
        count,
        &diagnostics,
        truncated,
    ))
}

fn read_library(arguments: &Value) -> Result<Id, String> {
    let named = arguments
        .get("library")
        .and_then(Value::as_str)
        .ok_or_else(|| {
            format!(
                "library is required and must be one of {}. It cannot be worked out from file \
                 contents alone, and guessing it is what this tool exists not to do.",
                library::names().join(", ")
            )
        })?;
    Id::parse(named).ok_or_else(|| {
        format!(
            "{named} is not a library this reads. Try one of {}.",
            library::names().join(", ")
        )
    })
}

/// Every locale settled before the audit starts.
///
/// A caller that knows the locales says so and the names are not read at
/// all. A caller that leaves any of them out has all of them worked out
/// from the names together — which is the only way the name of a
/// `bundle.l10n.pt-br.json` can be told from that of a
/// `package.nls.json`, and mixing a supplied answer into that would
/// change what the rest resolve to.
fn read_files(arguments: &Value) -> Result<Vec<Document>, String> {
    let invalid =
        "files is required and must be a non-empty array of { path, content }".to_string();
    let items = arguments
        .get("files")
        .and_then(Value::as_array)
        .ok_or_else(|| invalid.clone())?;
    if items.is_empty() {
        return Err(invalid);
    }

    let mut names = Vec::new();
    let mut contents = Vec::new();
    let mut supplied = Vec::new();
    for item in items {
        let path = item
            .get("path")
            .and_then(Value::as_str)
            .ok_or_else(|| invalid.clone())?;
        let content = item
            .get("content")
            .and_then(Value::as_str)
            .ok_or_else(|| invalid.clone())?;
        names.push(path.to_string());
        contents.push(content.to_string());
        supplied.push(match item.get("locale").and_then(Value::as_str) {
            None => None,
            Some(tag) => Some(
                locale::canonicalise(tag).ok_or_else(|| format!("{tag} is not a language tag"))?,
            ),
        });
    }

    let locales = if supplied.iter().all(Option::is_some) {
        supplied
    } else {
        locale::locales_of(&names)?
    };

    Ok(names
        .into_iter()
        .zip(contents)
        .zip(locales)
        .map(|((name, content), locale)| Document {
            name,
            locale,
            content,
        })
        .collect())
}

/// Clamp quietly, reject loudly.
fn read_max_results(arguments: &Value) -> Result<usize, String> {
    let Some(raw) = arguments.get("maxResults") else {
        return Ok(DEFAULT_MAX_RESULTS);
    };
    let invalid = "maxResults must be a positive integer";
    let value = raw.as_u64().ok_or_else(|| invalid.to_string())?;
    if value < 1 {
        return Err(invalid.to_string());
    }
    Ok(usize::try_from(value)
        .unwrap_or(MAX_MAX_RESULTS)
        .min(MAX_MAX_RESULTS))
}

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

    use super::*;
    use crate::corpus::document;

    const CASES: &str = include_str!("../../fixtures/mcp-check-catalogues.json");

    #[derive(Debug, Deserialize)]
    struct Case {
        name: String,
        files: Option<Vec<String>>,
        arguments: Value,
        expected: Option<Value>,
        #[serde(rename = "expectedError")]
        expected_error: Option<String>,
    }

    #[test]
    fn every_corpus_case_answers_identically() {
        let cases: Vec<Case> = serde_json::from_str(CASES).expect("the corpus is valid JSON");
        assert!(!cases.is_empty(), "the corpus is empty");

        for case in cases {
            let mut arguments = case.arguments.clone();
            if let Some(names) = &case.files {
                arguments["files"] = Value::Array(
                    names
                        .iter()
                        .map(|name| json!({ "path": name, "content": document(name) }))
                        .collect(),
                );
            }

            match (case.expected, case.expected_error) {
                (_, Some(expected)) => {
                    assert_eq!(
                        run(&arguments).expect_err(&case.name),
                        expected,
                        "{}",
                        case.name
                    );
                }
                (Some(expected), None) => {
                    assert_eq!(
                        run(&arguments).expect(&case.name),
                        expected,
                        "{}",
                        case.name
                    );
                }
                (None, None) => panic!("{} pins neither a result nor an error", case.name),
            }
        }
    }

    #[test]
    fn the_tool_name_is_pinned() {
        assert_eq!(definition()["name"], "check_catalogues");
    }

    /// **The honest split.** Nothing here can see a manifest, so nothing
    /// here may decide which library this is.
    #[test]
    fn the_library_is_required_and_says_why() {
        let error = run(&json!({ "files": [{ "path": "en.json", "content": "{}" }] }))
            .expect_err("a refusal");
        assert!(error.contains("library is required"), "{error}");
        assert!(error.contains("guessing"), "{error}");
        for known in library::names() {
            assert!(error.contains(known), "{error} omits {known}");
        }
    }

    #[test]
    fn a_library_this_does_not_read_is_refused() {
        let error = run(&json!({
            "library": "gettext",
            "files": [{ "path": "en.json", "content": "{}" }]
        }))
        .expect_err("a refusal");
        assert!(error.contains("gettext"), "{error}");
    }

    /// The property this crate rests on, asserted on the surface a model
    /// actually calls.
    #[test]
    fn no_translated_value_ever_reaches_the_answer() {
        let result = run(&json!({
            "library": "i18next",
            "files": [
                { "path": "en.json", "content": r#"{"a":"in {{timeframe}}","b":"Save"}"# },
                { "path": "es.json", "content": r#"{"a":"en {{periodo}}","z":"sobrante"}"# },
            ]
        }))
        .expect("a result");
        let rendered = serde_json::to_string(&result).expect("serializes");
        assert!(!rendered.contains("en {{periodo}}"), "{rendered}");
        assert!(!rendered.contains("sobrante\""), "{rendered}");
        assert!(rendered.contains("periodo"), "the token is the finding");
    }

    /// The schema may not offer a way to read values either.
    #[test]
    fn the_schema_offers_no_way_to_ask_for_values() {
        let definition = definition();
        let properties = definition["inputSchema"]["properties"]
            .as_object()
            .expect("properties");
        for absent in ["values", "showValues", "includeValues", "translate"] {
            assert!(!properties.contains_key(absent), "{absent} is offered");
        }
    }

    #[test]
    fn a_missing_files_argument_is_refused() {
        let error = run(&json!({ "library": "i18next" })).expect_err("a refusal");
        assert!(error.contains("files is required"), "{error}");
        assert!(run(&json!({ "library": "i18next", "files": [] })).is_err());
    }

    #[test]
    fn a_locale_that_is_not_a_language_tag_is_refused() {
        let error = run(&json!({
            "library": "i18next",
            "files": [{ "path": "en.json", "locale": "Spanish", "content": "{}" }]
        }))
        .expect_err("a refusal");
        assert!(error.contains("Spanish"), "{error}");
    }

    #[test]
    fn a_supplied_locale_is_used_instead_of_the_name() {
        let result = run(&json!({
            "library": "i18next",
            "files": [
                { "path": "one.json", "locale": "en", "content": r#"{"a":"one"}"# },
                { "path": "two.json", "locale": "es", "content": r#"{"a":"uno"}"# },
            ]
        }))
        .expect("a result");
        assert_eq!(result["data"]["source"]["path"], "one.json");
        assert_eq!(result["data"]["files"][1]["locale"], "es");
    }

    #[test]
    fn a_fractional_cap_is_refused() {
        let error = run(&json!({
            "library": "i18next",
            "files": [{ "path": "en.json", "content": "{}" }],
            "maxResults": 1.5
        }))
        .expect_err("a refusal");
        assert_eq!(error, "maxResults must be a positive integer");
    }

    /// The corpus feeds translations in as *arguments*; no expectation
    /// may carry one back out.
    #[test]
    fn no_corpus_expectation_carries_a_translated_value() {
        let cases: Vec<Value> = serde_json::from_str(CASES).expect("the corpus is valid JSON");
        for case in cases {
            let Some(expected) = case.get("expected") else {
                continue;
            };
            let rendered = serde_json::to_string(expected).expect("serializes");
            for translated in [
                "Bienvenido",
                "Bon retour",
                "Übersicht",
                "Kataloge",
                "Verificar",
                "Déconnexion",
                "Ola ",
            ] {
                assert!(!rendered.contains(translated), "{rendered}");
            }
        }
    }
}