Integration: Integrations prefs read leniently, dialog test and restart note (#4097)

- prefs_ui `unimplemented_preferences_are_hidden_from_the_dialog` still asserted the
  Integrations section is hidden, so test (ubuntu/windows) failed (FredApps' review).
  It now asserts both rows are visible; scratchDisks stays hidden.
- `integrations()` parsed the whole preferences file strictly, so one wrong-typed value
  anywhere (`{"interface":{"enableAnimations":"nope"},"integrations":{"allowAgentControl":false}}`)
  reset Allow Agent Control to on while the dialog (lenient load, #6611) showed it off.
  The section is now read on its own, and field by field when it doesn't read as a whole
  (a list like `[false]` reads as a whole, as in the app); a test compares it with the
  app's own load.
- Both rows are read only at launch: they say "Applies at next launch." like System Title Bar.
- Removed the stale "not by these yet" comment in HIDDEN_UNTIL_IMPLEMENTED.

Co-authored-by: Erica Franco <[email protected]>
Co-authored-by: FredApps <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
committed 2026-10-25 12:02:55 +01:00
1 parent 8fd157afef
commit 8208034bef
3 files changed
+96 -10

No files matched your search

+58 -3
View File
@@ -95,10 +95,27 @@ fn custom_titlebar(prefs_file: Option<&std::path::Path>) -> bool {
/// Preferences › Integrations, read before the window opens the way [`custom_titlebar`] is:
/// whether agents may drive this launch at all, and the port to listen on when no `--control`
/// was given (0 = only with `--control`). A missing or unreadable file keeps the defaults.
///
/// The section is read on its own, and when it doesn't read as a whole each value on its own,
/// as the app's lenient load reads them (#6611): a wrong-typed value elsewhere in the file, or
/// in the other field, must not turn Allow Agent Control back on while the dialog shows it off.
fn integrations(prefs_file: Option<&std::path::Path>) -> photocraft_engine::prefs::Integrations {
let prefs: photocraft_engine::prefs::Preferences =
prefs_file.and_then(|p| std::fs::read_to_string(p).ok()).and_then(|t| serde_json::from_str(&t).ok()).unwrap_or_default();
prefs.integrations
use serde_json::Value;
let mut out = photocraft_engine::prefs::Preferences::default().integrations;
let file: Option<Value> = prefs_file.and_then(|p| std::fs::read_to_string(p).ok()).and_then(|t| serde_json::from_str(&t).ok());
let Some(section) = file.as_ref().and_then(|v| v.get("integrations")) else {
return out;
};
if let Ok(whole) = serde_json::from_value(section.clone()) {
return whole;
}
if let Some(allow) = section.get("allowAgentControl").and_then(Value::as_bool) {
out.allow_agent_control = allow;
}
if let Some(port) = section.get("controlPort").and_then(Value::as_u64).and_then(|p| u32::try_from(p).ok()) {
out.control_port = port;
}
out
}
/// The control port this launch listens on, and the warning to print when a request was refused.
@@ -821,6 +838,44 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
/// The app loads preferences leniently (#6611): one wrong-typed value resets only itself. So
/// a bad value elsewhere, or in the port, must not turn Allow Agent Control back on.
#[test]
fn a_bad_value_elsewhere_keeps_agent_control_off() {
let dir = std::env::temp_dir().join(format!("pc-integrations-lenient-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("prefs.json");
// What the Preferences dialog shows for the same file.
let shown = |text: &str| {
let mut session = photocraft_engine::Session::new();
session.load_prefs_json(text).unwrap();
session.prefs().integrations.clone()
};
let read = |text: &str| {
std::fs::write(&file, text).unwrap();
let read = super::integrations(Some(&file));
assert_eq!(read, shown(text), "{text}");
read
};
let r = read(r#"{"interface":{"enableAnimations":"nope"},"integrations":{"allowAgentControl":false,"controlPort":7878}}"#);
assert!(!r.allow_agent_control, "a bad value in another section");
assert_eq!(r.control_port, 7878);
let r = read(r#"{"integrations":{"allowAgentControl":false,"controlPort":"x"}}"#);
assert!(!r.allow_agent_control, "a bad port");
assert_eq!(r.control_port, 0, "the bad port alone takes its default");
// A bad Allow Agent Control alone takes its default.
let r = read(r#"{"integrations":{"allowAgentControl":"no","controlPort":7878}}"#);
assert_eq!(r, photocraft_engine::prefs::Integrations { allow_agent_control: true, control_port: 7878 });
read(r#"{"integrations":{"allowAgentControl":false,"controlPort":-1}}"#);
read(r#"{"integrations":{"allowAgentControl":false,"controlPort":4294967296}}"#);
read(r#"{"integrations":"off"}"#);
// serde reads a struct from a list too; the app does.
assert!(!read(r#"{"integrations":[false]}"#).allow_agent_control, "a list");
assert_eq!(read(r#"{"integrations":[false,7878]}"#).control_port, 7878);
let _ = std::fs::remove_dir_all(&dir);
}
/// X11 startup notification and docks match a window to its launcher by `StartupWMClass`,
/// so it has to be the class the window carries: the app id, on Wayland and X11 alike.
#[test]
-2
View File
@@ -1134,8 +1134,6 @@ pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &[
"rawDefaults.sharpenFor",
"rawDefaults.openAsSmartObject",
"rawDefaults.applyAutoTone",
// Agent access is governed by the launch flags (`--control`, the automation roots), not
// by these yet.
];
/// Is the preference at `path` (`"section.key"`) hidden from the Preferences dialog?
+38 -5
View File
@@ -1840,6 +1840,17 @@ fn memory_usage_row(ui: &mut egui::Ui, obj: &mut Map<String, Value>) {
ui.add_space(8.0);
}
/// Settings read only when the app starts, which say "Applies at next launch." under their row.
fn read_at_launch(path: &str) -> bool {
match path {
// Only Windows and Linux draw their own title bar.
"interface.systemTitleBar" => cfg!(all(not(target_arch = "wasm32"), not(target_os = "macos"))),
// The desktop app decides on the control server before its window opens.
"integrations.allowAgentControl" | "integrations.controlPort" => cfg!(not(target_arch = "wasm32")),
_ => false,
}
}
/// One grid row per setting in `keys` (already filtered by `field_visible`).
fn field_rows(ui: &mut egui::Ui, section: &str, obj: &mut Map<String, Value>, keys: Vec<String>, lang: crate::i18n::Lang) {
let t = Tokens::get(ui.ctx());
@@ -1852,7 +1863,7 @@ fn field_rows(ui: &mut egui::Ui, section: &str, obj: &mut Map<String, Value>, ke
Value::Bool(b) => {
ui.label("");
let mut b = *b;
if path == "interface.systemTitleBar" && cfg!(all(not(target_arch = "wasm32"), not(target_os = "macos"))) {
if read_at_launch(&path) {
ui.vertical(|ui| {
crate::widgets::checkbox(ui, &mut b, &label);
ui.label(RichText::new(tl!("Applies at next launch.")).color(t.text_dim));
@@ -1906,6 +1917,12 @@ fn field_rows(ui: &mut egui::Ui, section: &str, obj: &mut Map<String, Value>, ke
let mut x = n.as_i64().unwrap_or(0);
ui.add(egui::DragValue::new(&mut x).range(lo as i64..=hi as i64).custom_parser(crate::widgets::parse_num));
obj.insert(k, json!(x));
// A row of its own, so the label stays level with its field.
if read_at_launch(&path) {
ui.end_row();
ui.label("");
ui.label(RichText::new(tl!("Applies at next launch.")).color(t.text_dim));
}
} else {
let mut x = n.as_f64().unwrap_or(0.0);
ui.add(egui::DragValue::new(&mut x).range(lo..=hi).speed(0.1).max_decimals(3).custom_parser(crate::widgets::parse_num));
@@ -4089,10 +4106,12 @@ mod tests {
let values = prefs::Preferences::default().to_json();
assert!(has_visible_fields(&values, "general"));
assert!(has_visible_fields(&values, "fileHandling"));
// Every setting of these sections is still unimplemented.
for section in ["integrations", "scratchDisks"] {
assert!(!has_visible_fields(&values, section), "{section}");
}
// Every setting of this section is still unimplemented.
assert!(!has_visible_fields(&values, "scratchDisks"), "scratchDisks");
// Allow Agent Control and Control Port are read at launch (apps/photocraft main.rs).
assert!(has_visible_fields(&values, "integrations"));
assert!(!prefs::is_hidden("integrations.allowAgentControl"));
assert!(!prefs::is_hidden("integrations.controlPort"));
// "Fill new type layers with placeholder text" and "Use Escape to Commit" are live; other Type rows stay hidden.
assert!(has_visible_fields(&values, "type"));
assert!(!prefs::is_hidden("type.fillNewTypeLayersWithPlaceholder"));
@@ -4337,6 +4356,20 @@ mod tests {
assert!(h.query_by_label("Applies at next launch.").is_none());
}
/// #4097: Allow Agent Control and Control Port are read when the app starts, and say so.
#[test]
fn integrations_explain_next_launch() {
use egui_kittest::{Harness, kittest::Queryable};
let obj = json!({"allowAgentControl": true, "controlPort": 0}).as_object().unwrap().clone();
let mut h = Harness::new_ui_state(
|ui, obj: &mut Map<String, Value>| section_fields(ui, "integrations", obj, &[], crate::i18n::Lang::from_pref("en"), None, ""),
obj,
);
h.run_steps(4);
h.get_by_label("Allow agent control");
assert_eq!(h.query_all_by_label("Applies at next launch.").count(), 2, "one notice under each row");
}
#[test]
fn next_launch_notice_does_not_mark_unrelated_interface_preferences() {
use egui_kittest::{Harness, kittest::Queryable};