mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 05:29:53 +00:00
MCP: a folder preference is checked as trimmed, and only a %XX escape is decoded
Under automation read roots, `ui.inspect` kept a folder preference whose stored value was readable even when the trimmed path the app uses was not: ` /outside/fonts` reads as a name in the working folder, while the app uses `/outside/fonts`. A folder preference now stays only when both forms are readable. A reply string is decoded for path matching only when it holds a `%XX` escape, so a string such as `width="100%"` is no longer decoded and thrown away. The library list line in the progress doc now states when a hidden library's `loaded/<file name>` id moves.
This commit is contained in:
@@ -648,7 +648,7 @@ fn occurrences(text: &str, needle: &str, fold: bool) -> Vec<usize> {
|
||||
/// in letter case keep their own ids, and then the ones written in another case. `None` when nothing
|
||||
/// changed.
|
||||
fn replace_text(s: &str, r: &Rewrite) -> Option<String> {
|
||||
let decoded = if s.contains('%') { decoded_with_spans(s).filter(|(decoded, _)| decoded != s) } else { None };
|
||||
let decoded = if has_percent_escape(s) { decoded_with_spans(s).filter(|(decoded, _)| decoded != s) } else { None };
|
||||
let mut found = Found::default();
|
||||
for group in &r.pairs {
|
||||
for fold in [false, true].into_iter().take(if r.fold { 2 } else { 1 }) {
|
||||
@@ -774,6 +774,12 @@ fn decoded_with_spans(text: &str) -> Option<(String, Vec<(usize, usize)>)> {
|
||||
Some((decoded, decoded_spans))
|
||||
}
|
||||
|
||||
/// Does `text` hold a `%XX` escape, one that [`decoded_with_spans`] decodes? Only such an escape
|
||||
/// changes the decoded text.
|
||||
fn has_percent_escape(text: &str) -> bool {
|
||||
text.as_bytes().windows(3).any(|w| w[0] == b'%' && w[1].is_ascii_hexdigit() && w[2].is_ascii_hexdigit())
|
||||
}
|
||||
|
||||
/// Is `from`, a relative path (one that does not start with a separator, a drive or `loaded/`), at
|
||||
/// `text[at..]` the end of a longer path: after any `.` and `..` parts just before it, is it preceded by
|
||||
/// a letter, a digit, `_`, `-`, `.`, `~` or a separator? `/work/data/icon.svg` and
|
||||
@@ -1060,13 +1066,14 @@ fn leave_out_unreadable_paths(ui: &mut serde_json::Map<String, Value>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes out of `prefs` the folder preferences ([`PREF_FOLDERS`]) the request may not read. An
|
||||
/// empty value (no folder) and a scratch disk choice ([`SCRATCH_CHOICES`]) stay.
|
||||
/// Takes out of `prefs` the folder preferences ([`PREF_FOLDERS`]) the request may not read, as written
|
||||
/// or trimmed as the app uses it. An empty value (no folder) and a scratch disk choice
|
||||
/// ([`SCRATCH_CHOICES`]) stay.
|
||||
fn leave_out_unreadable_folders(prefs: &mut serde_json::Map<String, Value>) {
|
||||
for key in PREF_FOLDERS {
|
||||
let Some(written) = prefs.get(key).and_then(Value::as_str) else { continue };
|
||||
let path = written.trim();
|
||||
if path.is_empty() || (key.starts_with("scratch") && SCRATCH_CHOICES.contains(&path)) || readable(written) {
|
||||
if path.is_empty() || (key.starts_with("scratch") && SCRATCH_CHOICES.contains(&path)) || (readable(written) && readable(path)) {
|
||||
continue;
|
||||
}
|
||||
prefs.remove(key);
|
||||
|
||||
@@ -131,6 +131,21 @@ fn a_reply_hides_a_path_in_any_case_and_escaped() {
|
||||
assert_eq!(reply["error"], "c.svg, c.svg, c.svg", "{reply}");
|
||||
}
|
||||
|
||||
/// Only a string with a `%XX` escape is decoded before it is searched: a `%` that begins no escape
|
||||
/// leaves the text as it is, and the reply is the same either way.
|
||||
#[test]
|
||||
fn only_a_percent_escape_is_decoded() {
|
||||
for (text, escaped) in [("width=\"100%\"", false), ("50% off", false), ("%zz%4", false), ("%", false), ("a%2Fb", true), ("%fF", true)] {
|
||||
assert_eq!(super::has_percent_escape(text), escaped, "{text}");
|
||||
let decoded = super::decoded_with_spans(text).map(|(decoded, _)| decoded);
|
||||
assert_eq!(decoded.as_deref() != Some(text), escaped, "{text}: an escape alone changes the decoded text");
|
||||
}
|
||||
let hidden = hiding(&[("loaded//libs/b.svg", "loaded/b.svg")], &[]);
|
||||
let mut reply = json!({"error": "100% of /libs/b.svg and file:///libs%2Fb.svg"});
|
||||
super::replace_in_value(&mut reply, &super::Rewrite { pairs: super::hidden_text(&hidden), hiding: &hidden, fold: false });
|
||||
assert_eq!(reply["error"], "100% of b.svg and b.svg", "{reply}");
|
||||
}
|
||||
|
||||
/// A path a failed request sent takes the place of a hidden library's id for the same path, written
|
||||
/// with the other separator, and in another letter case when matching in any case (as on Windows).
|
||||
#[test]
|
||||
|
||||
@@ -499,6 +499,31 @@ fn ui_inspect_checks_a_folder_preference_as_written() {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// `ui.inspect` checks a folder preference as the app uses it, trimmed, as well: with the working
|
||||
/// folder as the read root, a path outside it with a space before it reads as a name in the working
|
||||
/// folder, and is still left out, because the app trims it and uses the path outside.
|
||||
#[test]
|
||||
fn ui_inspect_checks_a_folder_preference_as_trimmed() {
|
||||
let (_, _, base) = confined("inspect-folder-trimmed");
|
||||
let cwd = std::env::current_dir().unwrap();
|
||||
let roots = AutomationRoots::new(&[&cwd], &[&base.join("out")]).unwrap();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let mut app = VectorcraftApp::new(Session::new(), Services::default()).with_control(rx).with_automation_roots(roots);
|
||||
let outside = s(base.join("outside"));
|
||||
// Without its drive, the path starts with a separator: trimmed, it is absolute on any platform.
|
||||
let rooted = match outside.as_bytes() {
|
||||
[drive, b':', ..] if drive.is_ascii_alphabetic() => &outside[2..],
|
||||
_ => outside.as_str(),
|
||||
};
|
||||
let spaced = format!(" {rooted}");
|
||||
app.ui.engine_prefs = json!({"fontsFolder": spaced, "pluginsFolder": ""});
|
||||
let r = request(&mut app, &tx, "ui.inspect", json!({}));
|
||||
let prefs = r["result"]["ui"]["engine_prefs"].as_object().unwrap();
|
||||
assert_eq!(prefs.get("pluginsFolder").and_then(Value::as_str), Some(""), "{r}");
|
||||
assert!(!prefs.contains_key("fontsFolder"), "{r}");
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// `ui.inspect` hides a hidden library's id and path in every string it holds: status text, the
|
||||
/// command palette query and a script's report show the id and file name shown, and the path appears
|
||||
/// nowhere in the reply.
|
||||
|
||||
@@ -769,7 +769,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- `ui.inspect` under `--automation-read-root`: the documents still opening, the recent files, library panels' files and folder preferences (saved, and in an open Preferences dialog) outside the read roots are left out of the reply, a library loaded from outside them shows by its file name in `ui.inspect` and `ui.docking`, and the app's state is not changed. `crates/ui-egui/src/tests/automation.rs` (`ui_inspect_leaves_out_paths_outside_the_read_root`).
|
||||
- Link replies under `--automation-read-root`: `links.list`, `links.check`, `links.info` and `document.open`'s link rows leave out the path, found path and folder of a linked file outside the read roots, and the stored size and date of it in `links.info`; each row keeps its id, file name and status, and the document's links are not changed. `crates/engine/src/tests/linkspanel.rs` (`link_paths_outside_the_read_roots_are_left_out`), `crates/engine/src/tests/placed_document.rs` (`link_info_leaves_out_a_placed_documents_path_outside_the_read_roots`).
|
||||
- Linked-file and font-file searches under `--automation-read-root`: `links.findFiles` and `text.findFontFiles` replies, also later polls and stops of a search another client started, leave out the files found, a link's saved path and the folder searched outside the caller's read roots, and a failed search gives its reason without the path; `image.info` gives `link: null` for a linked file outside them; `text.addFontFiles` gives `folder: null` and the copies' file names when the Fonts folder is outside them. `crates/engine/src/tests/linkfiles.rs`, `crates/engine/src/tests/fontfiles.rs`, `crates/engine/tests/font_folder.rs`.
|
||||
- Swatch, brush, symbol and graphic style library lists under `--automation-read-root`: every library stays listed, the paths of library files and the user library folder outside the read roots are `null`, a library loaded from outside them is listed and found as `loaded/<file name>` and keeps that id for the session, and the app's own list is not changed. `crates/engine/src/tests/library_roots.rs`.
|
||||
- Swatch, brush, symbol and graphic style library lists under `--automation-read-root`: every library stays listed, the paths of library files and the user library folder outside the read roots are `null`, a library loaded from outside them is listed and found as `loaded/<file name>` and keeps that id for the session unless a library loaded later without a file shows that id itself, which moves it to the next free `loaded/<file name> (N)`, and the app's own list is not changed. `crates/engine/src/tests/library_roots.rs`.
|
||||
- Preferences commands under `--automation-read-root`: `prefs.get`, `prefs.list`, `prefs.set` and `prefs.reset` show folder preferences outside the read roots as `null` and leave out an export preset's folder outside them, and the preferences are not changed. `crates/engine/src/tests/prefs_roots.rs`.
|
||||
- Plug-in, recovery and script report replies under `--automation-read-root`: `plugin.list`, `plugin.info` and `plugin.safeMode` name the plug-in folder, failed files, plug-in sources and disabled plug-ins' files outside the read roots by file name or `null`, `file.recovery.*` leave out a document's original file and the recovery folder outside them, and `ui.inspect`'s `scriptReport` names files a run wrote outside them by file name. `crates/engine/src/tests/plugins.rs` (`plugin_list_leaves_out_paths_outside_the_read_root`), `crates/engine/src/tests/recovery.rs` (`recovery_replies_leave_out_paths_outside_the_read_root`), `crates/ui-egui/src/tests/automation.rs` (`ui_inspect_script_report_leaves_out_files_outside_the_read_root`).
|
||||
- Scripts: `alert`, `confirm` and `prompt` open dialogs during a run, each with a Stop button that ends the run and puts every document back. In the app the run waits at the call for the person's answer; a run over MCP or the command line takes its answers in order from `script.run`'s `answers`, and the report lists each dialog with its answer. `crates/engine/src/tests/script_dialogs.rs`, `crates/ui-egui/src/tests/script_dialogs.rs`.
|
||||
|
||||
Reference in New Issue
Block a user