diff --git a/crates/engine/src/cmd/notes_cmds.rs b/crates/engine/src/cmd/notes_cmds.rs index e5530e17aa4..65203052293 100644 --- a/crates/engine/src/cmd/notes_cmds.rs +++ b/crates/engine/src/cmd/notes_cmds.rs @@ -190,11 +190,28 @@ fn import(s: &mut Session, p: &Value) -> Result { import_notes_from(s, &name, &bytes) } +/// Photoshop's alert when a source gives no notes: one without any, or one it cannot read as a +/// notes source (#13915). +pub const NO_NOTES_IMPORTED: &str = "No notes were imported."; + +/// The error for a source the import could not read. A memory refusal says what the user can do +/// about it and keeps its own (typed) message; anything else (not an image, damaged, truncated) +/// gave no notes (#13915). +fn source_error(name: &str, error: photocraft_io::IoError) -> EngineError { + use photocraft_io::IoError; + match error { + IoError::OutOfMemory(_) | IoError::PsdApprovalRequired { .. } | IoError::PsdImportBudget { .. } => crate::error::io_open_error(name, error), + _ => EngineError::Other(NO_NOTES_IMPORTED.into()), + } +} + /// File › Import › Notes… from file bytes (what a file picker returns). pub fn import_notes_from(s: &mut Session, name: &str, bytes: &[u8]) -> Result { - let src = crate::file_cmds::import(name, bytes)?; + // The steps of `file_cmds::import`, with the io error still typed for `source_error`. + crate::allocation::checkpoint("importing document")?; + let src = photocraft_io::import_for_open(name, bytes, &photocraft_io::ImportOptions::default()).map_err(|e| source_error(name, e))?.0.document; if src.notes.is_empty() { - return Err(EngineError::Other(format!("{name} has no notes"))); + return Err(EngineError::Other(NO_NOTES_IMPORTED.into())); } let d = s.active().ok_or(EngineError::NoDocument)?.doc.clone(); let (w, h) = (f64::from(d.size.width), f64::from(d.size.height)); @@ -270,6 +287,10 @@ pub fn specs() -> Vec { ] } +#[cfg(test)] +#[path = "notes_import_message_tests.rs"] +mod notes_import_message_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/crates/engine/src/cmd/notes_import_message_tests.rs b/crates/engine/src/cmd/notes_import_message_tests.rs new file mode 100644 index 00000000000..bf686b7516d --- /dev/null +++ b/crates/engine/src/cmd/notes_import_message_tests.rs @@ -0,0 +1,111 @@ +//! #13915: File › Import › Notes… from a source that gives no notes fails as Photoshop does, with +//! "No notes were imported.", no history step and the document unmodified. A file that cannot be +//! read at all, or one this machine has no memory for, keeps its own error. + +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Document, Note, Size}; +use serde_json::json; + +use super::{NO_NOTES_IMPORTED, import_notes_from, source_error}; +use crate::{EngineError, Session}; + +fn session() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 100, "height": 80})).unwrap(); + s +} + +fn source(notes: usize) -> Document { + let mut d = Document::new("src", Size::new(60, 40), ColorMode::Rgb, SampleType::U8); + for i in 0..notes { + d.notes.push(Note { text: format!("note {i}"), position: [10.0, 10.0], ..Default::default() }); + } + d +} + +fn pcraft(notes: usize) -> Vec { + photocraft_format::save_to_bytes(&source(notes), &Default::default()).unwrap() +} + +fn psd(notes: usize) -> Vec { + photocraft_io::export(&source(notes), "x.psd", &photocraft_io::ExportOptions::default()).unwrap().bytes +} + +/// Imports `bytes` as `name` and checks the refusal leaves the document as it was. +fn refused(name: &str, bytes: &[u8]) -> String { + let mut s = session(); + let past = s.active().unwrap().history.past_len(); + let dirty = s.active().unwrap().is_dirty(); + let e = import_notes_from(&mut s, name, bytes).expect_err(name).to_string(); + let a = s.active().unwrap(); + assert_eq!(a.history.past_len(), past, "{name}: no history step"); + assert_eq!(a.is_dirty(), dirty, "{name}: the document stays unmodified"); + assert!(a.doc.notes.is_empty(), "{name}"); + e +} + +#[test] +fn a_file_without_notes_says_no_notes_were_imported() { + assert_eq!(refused("none.pcraft", &pcraft(0)), NO_NOTES_IMPORTED); + assert_eq!(refused("none.psd", &psd(0)), NO_NOTES_IMPORTED); + assert_eq!(NO_NOTES_IMPORTED, "No notes were imported."); +} + +#[test] +fn a_file_that_is_no_notes_source_says_the_same() { + // Not an image. + assert_eq!(refused("x.txt", b"just some text\n"), NO_NOTES_IMPORTED); + assert_eq!(refused("empty.psd", &[]), NO_NOTES_IMPORTED); + // Damaged: a PSD signature with nothing valid behind it. + assert_eq!(refused("bad.psd", b"8BPS\0\x01\xff\xff\xff\xff\xff\xff"), NO_NOTES_IMPORTED); + // Truncated files that do have notes, cut at several lengths. + for (name, bytes) in [("cut.psd", psd(2)), ("cut.pcraft", pcraft(2))] { + for len in [4, 26, bytes.len() / 2, bytes.len() - 1] { + assert_eq!(refused(name, &bytes[..len]), NO_NOTES_IMPORTED, "{name} cut to {len} bytes"); + } + } +} + +#[test] +fn a_file_with_notes_still_imports() { + for (name, bytes) in [("a.pcraft", pcraft(2)), ("a.psd", psd(2))] { + let mut s = session(); + let past = s.active().unwrap().history.past_len(); + let r = import_notes_from(&mut s, name, &bytes).unwrap(); + assert_eq!(r["imported"], 2, "{name}"); + assert_eq!(s.active().unwrap().doc.notes.len(), 2, "{name}"); + assert_eq!(s.active().unwrap().history.past_len(), past + 1, "{name}"); + } +} + +#[test] +fn a_memory_refusal_keeps_its_own_message() { + // A pixel buffer the decoder could not allocate: the file may be fine. + let e = source_error("big.psd", photocraft_io::IoError::OutOfMemory("not enough memory to interleave PSD pixels".into())).to_string(); + assert_eq!(e, "big.psd: not enough memory: not enough memory to interleave PSD pixels"); + let e = source_error("big.psd", photocraft_io::IoError::PsdImportBudget { required: 9000, limit: 4000, free_memory: None }); + assert!(matches!(e, EngineError::PsdHardBudget { .. }), "{e:?}"); + let e = source_error("big.psd", photocraft_io::IoError::PsdApprovalRequired { required: 9000, soft_limit: 2000, hard_limit: 4000, available_ram: None }); + assert!(matches!(e, EngineError::PsdApprovalRequired { .. }), "{e:?}"); + // Not a memory problem: no notes. + assert_eq!(source_error("x.txt", photocraft_io::IoError::UnknownFormat("x.txt".into())).to_string(), NO_NOTES_IMPORTED); + // The allocation checkpoint before decoding keeps its message too, and changes nothing. + let (mut s, bytes) = (session(), pcraft(2)); + let past = s.active().unwrap().history.past_len(); + crate::allocation::fail_next_for_test(); + let e = import_notes_from(&mut s, "a.pcraft", &bytes).unwrap_err().to_string(); + assert!(e.starts_with("not enough memory for importing document"), "{e}"); + assert_eq!(s.active().unwrap().history.past_len(), past); + assert!(s.active().unwrap().doc.notes.is_empty()); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_file_that_cannot_be_read_keeps_its_own_error() { + let mut s = session(); + let path = std::env::temp_dir().join(format!("pc-notes-missing-{}", std::process::id())).join("gone.psd"); + let p = path.to_string_lossy().to_string(); + let e = s.execute("file.import.notes", json!({"path": p})).unwrap_err().to_string(); + assert!(e.starts_with(&format!("{p}: ")), "{e}"); + assert_ne!(e, NO_NOTES_IMPORTED); +}