| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| //! Invocation-owned copy for CUA documentation and recovery guidance. | |
| use maka_agent_facts::language::Language; | |
| #[allow(unused_imports)] | |
| mod generated_catalog { | |
| include!(concat!(env!("OUT_DIR"), "/i18n_catalog.rs")); | |
| } | |
| pub use generated_catalog::i18n_catalog; | |
| #[cfg(feature = "native-desktop")] | |
| include!(concat!(env!("OUT_DIR"), "/i18n_docs.rs")); | |
| #[cfg(feature = "native-desktop")] | |
| fn documentation_source(language: Language) -> &'static str { | |
| i18n_docs::get(language) | |
| } | |
| #[cfg(feature = "native-desktop")] | |
| fn doc_blocks(source: &'static str) -> impl Iterator<Item = (&'static str, &'static str)> { | |
| source.split("<!-- doc: ").skip(1).filter_map(|block| { | |
| let (names, rest) = block.split_once(" -->")?; | |
| let (body, _) = rest.split_once("<!-- /doc -->")?; | |
| let body = body.strip_prefix('\n').unwrap_or(body); | |
| Some((names.trim(), body.trim_end())) | |
| }) | |
| } | |
| /// Read an exact named topic from the localized documentation bundle. | |
| /// Name availability is not a capability grant or a claim that its feature is enabled. | |
| #[cfg(feature = "native-desktop")] | |
| pub(crate) fn topic(language: Language, name: &str) -> Option<String> { | |
| if name.is_empty() || name.bytes().any(|b| b.is_ascii_whitespace()) { | |
| return None; | |
| } | |
| let source = documentation_source(language); | |
| let body = doc_blocks(source).find_map(|(names, body)| { | |
| names | |
| .split_ascii_whitespace() | |
| .any(|candidate| candidate == name) | |
| .then_some(body) | |
| })?; | |
| if name == "core" { | |
| let topics: Vec<&str> = doc_blocks(source) | |
| .filter_map(|(names, _)| names.split_ascii_whitespace().next()) | |
| .filter(|canonical| *canonical != "core") | |
| .collect(); | |
| let list = topics.join(", "); | |
| let index = i18n_catalog::repl::more_topics( | |
| language, | |
| "`nodeRepl.write(await agent.documentation.get(name))`", | |
| &list, | |
| ); | |
| return Some(format!("{body}\n\n{index}")); | |
| } | |
| Some(body.to_owned()) | |
| } | |
| pub fn reset(language: Language) -> &'static str { | |
| i18n_catalog::repl::reset(language) | |
| } | |
| pub fn partial_observations(language: Language) -> &'static str { | |
| i18n_catalog::repl::partial_observations(language) | |
| } | |
| pub fn closed(language: Language) -> &'static str { | |
| i18n_catalog::repl::closed(language) | |
| } | |
| pub fn javascript_error(language: Language, message: &str) -> String { | |
| i18n_catalog::repl::javascript_error(language, message) | |
| } | |
| #[cfg(feature = "native-desktop")] | |
| pub(crate) fn uncertain(language: Language) -> &'static str { | |
| i18n_catalog::driver::uncertain(language) | |
| } | |
| #[cfg(feature = "native-desktop")] | |
| pub(crate) fn inactive(language: Language) -> &'static str { | |
| i18n_catalog::repl::inactive(language) | |
| } | |
| #[cfg(feature = "native-desktop")] | |
| pub(crate) fn terminal(language: Language) -> &'static str { | |
| i18n_catalog::driver::terminal(language) | |
| } | |
| #[cfg(feature = "native-desktop")] | |
| pub(crate) fn background_unavailable(language: Language) -> &'static str { | |
| i18n_catalog::driver::background_unavailable(language) | |
| } | |
| // CDP preserves the original error and uncertainty class; the accepted browser | |
| // dispatch owns this warning for every unknown outcome. | |
| #[cfg(feature = "native-desktop")] | |
| pub(crate) fn browser_unknown(language: Language) -> &'static str { | |
| i18n_catalog::browser::unknown_outcome_suffix(language) | |
| } | |
| #[cfg(all(test, feature = "native-desktop"))] | |
| mod named_documentation_tests { | |
| use super::*; | |
| #[test] | |
| fn named_lookup_is_exact_and_cannot_become_a_filesystem_path() { | |
| let base_names: Vec<&str> = doc_blocks(documentation_source(Language::EN)) | |
| .flat_map(|(names, _)| names.split_ascii_whitespace()) | |
| .collect(); | |
| assert!(base_names.len() >= 26); | |
| for required in [ | |
| "core", | |
| "actions", | |
| "desktop", | |
| "desktop/raw", | |
| "browser", | |
| "playwright", | |
| "browser/pointer", | |
| "downloads", | |
| "files", | |
| "audio", | |
| "permissions", | |
| "cursor", | |
| "session-naming", | |
| ] { | |
| assert!( | |
| base_names.contains(&required), | |
| "missing expected topic {required:?}" | |
| ); | |
| } | |
| for language in Language::all() { | |
| for name in &base_names { | |
| let body = topic(language, name) | |
| .unwrap_or_else(|| panic!("missing doc {name:?} in {language:?}")); | |
| assert!( | |
| !body.trim().is_empty(), | |
| "empty doc {name:?} in {language:?}" | |
| ); | |
| assert!(!body.contains("<!-- doc:"), "leaked marker in {name:?}"); | |
| assert!( | |
| !body.contains("<!-- /doc -->"), | |
| "leaked close marker in {name:?}" | |
| ); | |
| } | |
| let core = topic(language, "core").unwrap(); | |
| assert!( | |
| core.contains("nodeRepl.write(await agent.documentation.get(name))") | |
| && core.contains("playwright"), | |
| "core topic must include generated topic index in {language:?}" | |
| ); | |
| for missing in [ | |
| "", | |
| " session-naming", | |
| "session-naming ", | |
| "Session-Naming", | |
| "../api", | |
| "__proto__", | |
| ] { | |
| assert!(topic(language, missing).is_none(), "accepted {missing:?}"); | |
| } | |
| } | |
| } | |
| #[test] | |
| fn catalog_preserves_placeholders_apostrophes_and_zh_hk_overlay() { | |
| assert_eq!( | |
| i18n_catalog::desktop::window_header(Language::EN, r#""Notes""#, r#""Draft""#), | |
| r#"Window: "Draft", App: "Notes"."# | |
| ); | |
| assert_eq!( | |
| i18n_catalog::browser::tab_header(Language::ZH_CN, "t1", "https://example.com"), | |
| "标签页 t1: https://example.com" | |
| ); | |
| // Inherited from zh-TW | |
| assert_eq!( | |
| i18n_catalog::browser::tab_header(Language::ZH_HK, "t1", "https://example.com"), | |
| i18n_catalog::browser::tab_header(Language::ZH_TW, "t1", "https://example.com") | |
| ); | |
| // Genuine zh-HK overlay override differs from zh-TW | |
| assert_ne!( | |
| i18n_catalog::activity::capture_screenshot(Language::ZH_HK), | |
| i18n_catalog::activity::capture_screenshot(Language::ZH_TW) | |
| ); | |
| assert_eq!( | |
| javascript_error(Language::EN, "boom"), | |
| "Cua JavaScript error: boom" | |
| ); | |
| assert_eq!( | |
| javascript_error(Language::ZH_TW, "boom"), | |
| "Cua JavaScript 錯誤:boom" | |
| ); | |
| assert!( | |
| i18n_catalog::permission::unknown_risk_warning(Language::EN) | |
| .starts_with("This app's risk classification") | |
| ); | |
| } | |
| } |