mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Validate action definitions with the durable encoder before publication
This commit is contained in:
1 parent
13abd9d916
commit
5e748bfc01
5 files changed
+112
-13
No files matched your search
@@ -420,7 +420,7 @@ fn rename_calls(params: &mut Value, id: &str, old: &str, new: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Only changed references need prepared copies; no library or Session snapshot is taken.
|
||||
/// Prepare only changed named-call parameters; the caller validates the proposed definitions.
|
||||
fn references(s: &Session, draft: &Draft, value: &mut Action) -> Vec<(usize, usize, Value)> {
|
||||
let mut changes = Vec::new();
|
||||
if !draft.source.is_some_and(|index| super::owns_name(&s.actions.list, index)) || draft.opening.name == value.name {
|
||||
@@ -520,20 +520,24 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
next.alternates = action_keys(&alternates);
|
||||
next.recording = None;
|
||||
let response = view(&next, &value);
|
||||
if target == s.actions.list.len() {
|
||||
s.actions.list.try_reserve(1).map_err(|_| bad(SAVE, "not enough memory for the action"))?;
|
||||
let mut proposed = super::library::definition_copy(&s.actions);
|
||||
if target == proposed.list.len() {
|
||||
proposed.list.try_reserve(1).map_err(|_| bad(SAVE, "not enough memory for the action"))?;
|
||||
}
|
||||
// All validation and copies are complete. No command execution or fallible work follows.
|
||||
// Include renamed callers in the same encoded candidate as the edited action.
|
||||
for (action, step, params) in changes {
|
||||
if let Some((_, old)) = s.actions.list.get_mut(action).and_then(|action| action.steps.get_mut(step)) {
|
||||
if let Some((_, old)) = proposed.list.get_mut(action).and_then(|action| action.steps.get_mut(step)) {
|
||||
*old = params;
|
||||
}
|
||||
}
|
||||
if target == s.actions.list.len() {
|
||||
s.actions.list.push(value);
|
||||
} else if let Some(old) = s.actions.list.get_mut(target) {
|
||||
if target == proposed.list.len() {
|
||||
proposed.list.push(value);
|
||||
} else if let Some(old) = proposed.list.get_mut(target) {
|
||||
*old = value;
|
||||
}
|
||||
super::library::check_definition(&proposed, &keys).map_err(|error| bad(SAVE, error))?;
|
||||
// Encoding succeeded. Publish definitions without replacing the live capture/owner state.
|
||||
s.actions.list = proposed.list;
|
||||
if changed {
|
||||
s.actions.touch();
|
||||
}
|
||||
|
||||
@@ -27,6 +27,26 @@ pub(crate) fn check_action_count<'a>(state: &ActionState, actions: impl IntoIter
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Copy only persisted definitions and row provenance for pre-publication encoding.
|
||||
/// Recording, editor ownership and playback counters remain in the live state.
|
||||
pub(crate) fn definition_copy(state: &ActionState) -> ActionState {
|
||||
ActionState {
|
||||
list: state.list.clone(),
|
||||
library: state.library.clone(),
|
||||
sets: state.sets.clone(),
|
||||
atn_set_rows: state.atn_set_rows.clone(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Use the durable encoder's complete limits before publishing a proposed definition.
|
||||
/// This checks eligibility, while backend I/O retains its ordinary warning/retry semantics.
|
||||
pub(crate) fn check_definition(state: &ActionState, shortcuts: &std::collections::BTreeMap<String, String>) -> Result<()> {
|
||||
let keys: Vec<_> =
|
||||
shortcuts.iter().filter_map(|(key, value)| key.strip_prefix(super::PLAY_SHORTCUT_PREFIX).map(|name| (name.to_string(), value.clone()))).collect();
|
||||
encode(state, &keys).map(|_| ())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Archive {
|
||||
bytes: Arc<[u8]>,
|
||||
@@ -301,10 +321,13 @@ struct OutputAction {
|
||||
items: Vec<Option<ItemOrigin>>,
|
||||
}
|
||||
|
||||
/// Resolve a physical source record, rejecting stale row/action indices.
|
||||
fn original_action(origin: &ActionOrigin) -> Result<&AtnAction> {
|
||||
origin.row.file()?.actions.get(origin.action).ok_or_else(|| "missing original action".into())
|
||||
}
|
||||
|
||||
/// Rebuild edited headers and steps while retaining provenance for unchanged event spans.
|
||||
/// Function keys bind only unique action names; opaque source headers remain available.
|
||||
fn action_record(action: &Action, set: &str, keys: &[(String, String)], unique_name: bool) -> Result<(AtnAction, OutputAction)> {
|
||||
let origin = action.library_origin.as_ref();
|
||||
let original = origin.map(original_action).transpose()?;
|
||||
|
||||
@@ -140,8 +140,11 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
crate::actions_cmds::library::check_action_count(&s.actions, s.actions.list.iter().chain(std::iter::once(&copied))).map_err(|error| bad(cmd, error))?;
|
||||
let at = idx + 1;
|
||||
let result = json!({"action": copied.name, "index": at, "steps": copied.steps.len()});
|
||||
s.actions.list.insert(at, copied);
|
||||
shift_action_calls(&mut s.actions.list, at, 1);
|
||||
let mut proposed = crate::actions_cmds::library::definition_copy(&s.actions);
|
||||
proposed.list.insert(at, copied);
|
||||
shift_action_calls(&mut proposed.list, at, 1);
|
||||
crate::actions_cmds::library::check_definition(&proposed, &s.prefs().shortcuts).map_err(|error| bad(cmd, error))?;
|
||||
s.actions.list = proposed.list;
|
||||
s.actions.touch();
|
||||
Ok(result)
|
||||
}
|
||||
@@ -179,11 +182,17 @@ fn duplicate_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let copied: Vec<_> = added.iter().map(|a| a.name.clone()).collect();
|
||||
let count = added.len();
|
||||
crate::actions_cmds::library::check_action_count(&s.actions, s.actions.list.iter().chain(added.iter())).map_err(|error| bad(cmd, error))?;
|
||||
s.actions.list.splice(at..at, added);
|
||||
let mut proposed = crate::actions_cmds::library::definition_copy(&s.actions);
|
||||
proposed.list.splice(at..at, added);
|
||||
if count != 0 {
|
||||
shift_action_calls(&mut s.actions.list, at, count);
|
||||
shift_action_calls(&mut proposed.list, at, count);
|
||||
}
|
||||
crate::atn_cmds::sidecar::duplicate_set_rows(&mut s.actions, source, &name);
|
||||
crate::atn_cmds::sidecar::duplicate_set_rows(&mut proposed, source, &name);
|
||||
crate::actions_cmds::library::check_definition(&proposed, &s.prefs().shortcuts).map_err(|error| bad(cmd, error))?;
|
||||
s.actions.list = proposed.list;
|
||||
s.actions.sets = proposed.sets;
|
||||
s.actions.library = proposed.library;
|
||||
s.actions.atn_set_rows = proposed.atn_set_rows;
|
||||
s.actions.touch();
|
||||
Ok(json!({"set":name,"actions":copied,"index":at}))
|
||||
}
|
||||
|
||||
@@ -35,6 +35,41 @@ fn session() -> Session {
|
||||
s
|
||||
}
|
||||
|
||||
/// Whole-action and folder copies use the same encoder budget as durable storage.
|
||||
#[test]
|
||||
fn duplicate_rejects_combined_metadata_size_without_changing_rows_or_store() {
|
||||
use photocraft_engine::preset_store::{self, ACTIONS_FILE, MAX_ACTIONS_BYTES, MemBackend, PresetBackend};
|
||||
for set in [false, true] {
|
||||
for admitted in [true, false] {
|
||||
let store = MemBackend::default();
|
||||
let mut s = Session::new();
|
||||
assert!(s.attach_preset_store(preset_store::open(Box::new(store.clone()))).is_empty());
|
||||
let mut source = Action::new("Source", vec![("select.all".into(), json!({}))]);
|
||||
source.set = Some("Group".into());
|
||||
source.description = "x".repeat(if admitted { 3 << 20 } else { 5 << 20 });
|
||||
s.actions.list.push(source);
|
||||
s.execute("actions.dialog", json!({"action":0,"enabled":false})).unwrap();
|
||||
let bytes = store.read(ACTIONS_FILE, MAX_ACTIONS_BYTES).unwrap();
|
||||
let before = s.actions.clone();
|
||||
let prefs = s.prefs().clone();
|
||||
let result = s.execute("actions.duplicate", if set { json!({"set":"Group"}) } else { json!({"action":0}) });
|
||||
if admitted {
|
||||
result.unwrap();
|
||||
let mut restored = Session::new();
|
||||
assert!(restored.attach_preset_store(preset_store::open(Box::new(store))).is_empty());
|
||||
assert_eq!(restored.actions.list.len(), 2);
|
||||
assert!(restored.actions.list.iter().all(|action| action.description.len() == 3 << 20));
|
||||
} else {
|
||||
let error = result.unwrap_err();
|
||||
assert!(error.to_string().contains("byte limit"), "{error}");
|
||||
assert_eq!(s.actions, before);
|
||||
assert_eq!(s.prefs(), &prefs);
|
||||
assert_eq!(store.read(ACTIONS_FILE, MAX_ACTIONS_BYTES).unwrap(), bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn action_count_admission_reserves_the_metadata_carrier_before_publication() {
|
||||
for metadata in [false, true] {
|
||||
|
||||
@@ -39,6 +39,34 @@ fn open(s: &mut Session, params: Value) -> u64 {
|
||||
s.execute("actions.editor.open", params).unwrap()["draft"].as_u64().unwrap()
|
||||
}
|
||||
|
||||
/// An encoder refusal retains the dirty capture and both in-memory and stored definitions.
|
||||
#[test]
|
||||
fn save_rejects_unencodable_metadata_before_ending_capture_or_publishing() {
|
||||
let (mut s, store) = session();
|
||||
let draft = open(&mut s, json!({"action":"Original"}));
|
||||
editor(&mut s, "update", draft, json!({"name":"Renamed","description":"x".repeat(9 << 20),"shortcut":"F8"})).unwrap();
|
||||
editor(&mut s, "record", draft, json!({})).unwrap();
|
||||
let before = s.actions.clone();
|
||||
let prefs = s.prefs().clone();
|
||||
let bytes = store.read(ACTIONS_FILE, MAX_ACTIONS_BYTES).unwrap();
|
||||
let error = editor(&mut s, "save", draft, json!({})).unwrap_err();
|
||||
assert!(error.to_string().contains("byte limit"), "{error}");
|
||||
assert_eq!(s.actions, before);
|
||||
assert_eq!(s.prefs(), &prefs);
|
||||
assert_eq!(store.read(ACTIONS_FILE, MAX_ACTIONS_BYTES).unwrap(), bytes);
|
||||
assert!(editor(&mut s, "get", draft, json!({})).unwrap()["dirty"].as_bool().unwrap());
|
||||
|
||||
// A large eligible replacement can still close the same capture and persist the rename.
|
||||
editor(&mut s, "update", draft, json!({"description":"x".repeat(3 << 20)})).unwrap();
|
||||
editor(&mut s, "save", draft, json!({})).unwrap();
|
||||
assert!(!editor(&mut s, "get", draft, json!({})).unwrap()["dirty"].as_bool().unwrap());
|
||||
let mut restored = Session::new();
|
||||
assert!(restored.attach_preset_store(preset_store::open(Box::new(store))).is_empty());
|
||||
assert_eq!(restored.actions.list[0].name, "Renamed");
|
||||
assert_eq!(restored.actions.list[0].description.len(), 3 << 20);
|
||||
assert_eq!(restored.actions.list[1].steps[0].1["action"], "Renamed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn namesake_rename_follows_only_the_first_resolved_occurrence_in_both_editors() {
|
||||
for modern in [false, true] {
|
||||
|
||||
Reference in new issue
Block a user