Files
photocraft/crates/engine/src/tests.rs
T
Carter SmithandBrandon Thomas 2a2459f57a Drag-and-drop: place onto the canvas in Free Transform, open at a tab slot (#739)
* Drag-and-drop: place onto the canvas in Free Transform, open at a tab slot

Like Photoshop, a file dropped on the active document's canvas is placed as an
embedded smart object layer and enters Free Transform (Esc takes the place back,
Enter makes place + transform one "Place Embedded" step; several files are placed
one after another). Dropped on the tab strip, it opens with its tab at the slot
under the pointer, shown by an insertion line while dragging. Elsewhere it opens
as before.

winit 0.30 drops carry no position, so the desktop shell reads the pointer from
the OS (apps/photocraft/src/cursor.rs: AppKit, x11rb, winsafe; no unsafe).

* Drop product names from comments, translate the place error, keep move_item panic-free

- Reword the new comments that named another product ("the reference app").
- "Couldn't place {name}: {error}" is a tl! string with entries in every
  complete catalog; `failed` now takes the finished message.
- The now-public `move_item` returns None for an out-of-range index instead of
  panicking (regression test in move_document's test).

* Tab moves as a document.move command, views follow their documents, drop the windows-sys lock bump

- document.move ({document?, to}) reorders tabs for the UI, agents and scripts; tab-slot drops and
  background opens go through it.
- sync_views keeps each view and its windows with their document by DocId, so moving or closing a
  tab never hands its view to another document.
- Restore os_pipe's windows-sys 0.52 lock entry (unrelated to this change).
- cursor::client_points: Windows pixel-to-point conversion, tested across monitors with different DPIs.
- de and pt-br strings for the place error.

---------

Co-authored-by: Brandon Thomas <[email protected]>
2026-10-07 18:57:50 -04:00

702 lines
34 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use super::*;
use serde_json::json;
fn session_with_doc() -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": 64, "height": 48})).unwrap();
s
}
fn px(s: &mut Session, x: i32, y: i32) -> Vec<f32> {
serde_json::from_value(s.execute("document.pixel", json!({"x": x, "y": y})).unwrap()).unwrap()
}
#[test]
fn document_pixel_at_the_coordinate_limits() {
let mut s = session_with_doc();
// The last representable column used to panic (its 1x1 rect saturated to empty).
assert_eq!(px(&mut s, i32::MAX, 0), vec![0.0; 4]);
assert_eq!(px(&mut s, i32::MIN, i32::MAX), vec![0.0; 4]);
// Values beyond i32 used to wrap around onto the canvas (2^32 read column 0).
let e = s.execute("document.pixel", json!({"x": 1i64 << 32, "y": 0})).unwrap_err();
assert!(matches!(e, EngineError::BadParams { .. }), "{e}");
}
#[test]
fn command_ids_are_unique_and_documented() {
let mut seen = std::collections::HashSet::new();
for c in command_specs() {
assert!(seen.insert(c.id), "duplicate command id {}", c.id);
assert!(!c.label.is_empty());
assert!(c.id.contains('.'), "{} should be namespaced", c.id);
}
assert!(command_specs().len() > 60, "{}", command_specs().len());
}
#[test]
fn unknown_and_disabled_commands_error() {
let mut s = Session::new();
assert!(matches!(s.execute("nope.nothing", json!({})), Err(EngineError::UnknownCommand(_))));
assert!(matches!(s.execute("layer.new.layer", json!({})), Err(EngineError::Disabled(..))));
assert!(!s.is_enabled("edit.undo"));
}
#[test]
fn file_new_takes_whole_floats_and_survives_odd_sizes() {
// #254: the New dialog sent `512.0`; it must not fall back to 1920 x 1080.
let mut s = Session::new();
s.execute("file.new", json!({"width": 512.0, "height": 511.6})).unwrap();
let d = &s.active().unwrap().doc;
assert_eq!((d.size.width, d.size.height), (512, 512));
s.execute("file.new", json!({"width": -5.0, "height": "x"})).unwrap();
let d = &s.active().unwrap().doc;
assert_eq!((d.size.width, d.size.height), (1, 1080));
}
#[test]
fn file_new_variants() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 10, "height": 5, "background": "transparent"})).unwrap();
assert_eq!(s.active().unwrap().doc.layers[0].name, "Layer 1");
s.execute("file.new", json!({"width": 10, "height": 5, "mode": "cmyk", "depth": 16})).unwrap();
let d = &s.active().unwrap().doc;
assert_eq!(d.mode, photocraft_color::ColorMode::Cmyk);
assert_eq!(d.depth, photocraft_color::SampleType::U16);
s.execute("file.new", json!({"background": "#ff0000", "width": 4, "height": 4})).unwrap();
assert_eq!(px(&mut s, 1, 1), vec![1.0, 0.0, 0.0, 1.0]);
assert_eq!(s.documents().len(), 3);
s.execute("file.close", json!({})).unwrap();
assert_eq!(s.documents().len(), 2);
}
#[test]
fn layer_lifecycle_with_undo() {
let mut s = session_with_doc();
let r = s.execute("layer.new.layer", json!({})).unwrap();
let id = r["layer"].as_u64().unwrap();
assert_eq!(s.active().unwrap().active_layer, Some(LayerId(id)));
s.execute("layer.setProps", json!({"name": "Ink", "opacity": 0.5, "blend": "multiply"})).unwrap();
let doc = &s.active().unwrap().doc;
let l = doc.layer(LayerId(id)).unwrap();
assert_eq!((l.name.as_str(), l.opacity, l.blend), ("Ink", 0.5, photocraft_color::BlendMode::Multiply));
s.execute("layer.duplicate", json!({})).unwrap();
assert_eq!(s.active().unwrap().doc.layer_count(), 3);
s.execute("layer.delete", json!({})).unwrap();
assert_eq!(s.active().unwrap().doc.layer_count(), 2);
for _ in 0..4 {
s.execute("edit.undo", json!({})).unwrap();
}
assert_eq!(s.active().unwrap().doc.layer_count(), 1);
assert!(!s.is_enabled("edit.undo"));
s.execute("edit.redo", json!({})).unwrap();
assert_eq!(s.active().unwrap().doc.layer_count(), 2);
}
#[test]
fn bad_blend_mode_is_a_param_error() {
let mut s = session_with_doc();
let e = s.execute("layer.setProps", json!({"blend": "sparkle"})).unwrap_err();
assert!(matches!(e, EngineError::BadParams { .. }), "{e}");
}
#[test]
fn adjustment_layers_change_composite() {
let mut s = session_with_doc();
s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap();
assert_eq!(px(&mut s, 3, 3), vec![0.0, 0.0, 0.0, 1.0]);
let doc = s.execute("document.inspect", json!({})).unwrap();
assert_eq!(doc["layers"][0]["kind"], "Adjustment");
assert_eq!(doc["layers"][0]["name"], "Invert 1");
s.execute("layer.setProps", json!({"visible": false})).unwrap();
assert_eq!(px(&mut s, 3, 3), vec![1.0, 1.0, 1.0, 1.0]);
}
#[test]
fn every_adjustment_command_runs() {
let mut s = session_with_doc();
let ids: Vec<&str> =
command_specs().iter().map(|c| c.id).filter(|id| id.starts_with("layer.newAdjustmentLayer.") || id.starts_with("image.adjustments.")).collect();
assert!(ids.len() >= 28);
for id in ids {
// re-select the background for destructive ones
let bg = s.active().unwrap().doc.layers[0].id;
s.select_layer(bg).unwrap();
s.execute(id, json!({})).unwrap_or_else(|e| panic!("{id}: {e}"));
}
}
#[test]
fn threshold_and_hue_params() {
let mut s = session_with_doc();
s.execute("edit.fill", json!({"color": "#404040"})).unwrap();
s.execute("layer.newAdjustmentLayer.threshold", json!({"level": 128})).unwrap();
assert_eq!(px(&mut s, 0, 0), vec![0.0, 0.0, 0.0, 1.0]);
s.execute("layer.setAdjustment", json!({"level": 10})).unwrap();
assert_eq!(px(&mut s, 0, 0), vec![1.0, 1.0, 1.0, 1.0]);
}
#[test]
fn paint_stroke_and_selection() {
let mut s = session_with_doc();
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 32, "height": 48})).unwrap();
let r = s.execute("paint.stroke", json!({"points": [[4, 20], [60, 20]], "size": 6, "color": "#0000ff"})).unwrap();
assert!(r["damage"][2].as_i64().unwrap() > 0);
assert_eq!(px(&mut s, 10, 20), vec![0.0, 0.0, 1.0, 1.0]);
assert_eq!(px(&mut s, 50, 20), vec![1.0, 1.0, 1.0, 1.0], "outside selection stays white");
s.execute("select.deselect", json!({})).unwrap();
s.execute("paint.stroke", json!({"points": [[4, 30], [60, 30]], "size": 6, "erase": false})).unwrap();
assert_eq!(px(&mut s, 50, 30), vec![0.0, 0.0, 0.0, 1.0], "foreground is black");
}
#[test]
fn marquee_dragged_past_the_canvas_stops_at_its_edge() {
let mut s = session_with_doc(); // 64 × 48
let bounds = |s: &mut Session| s.execute("document.inspect", json!({})).unwrap()["selectionBounds"].clone();
s.execute("select.rect", json!({"x": -20, "y": 10, "width": 200, "height": 100})).unwrap();
assert_eq!(bounds(&mut s), json!([0, 10, 64, 38]));
s.execute("select.rect", json!({"x": -30, "y": -30, "width": 200, "height": 200, "ellipse": true})).unwrap();
assert_eq!(bounds(&mut s), json!([0, 0, 64, 48]));
// Entirely off the canvas: nothing is selected.
s.execute("select.rect", json!({"x": 100, "y": 100, "width": 20, "height": 20})).unwrap();
assert_eq!(bounds(&mut s), Value::Null);
}
#[test]
fn marquee_steps_are_named_after_their_tool() {
// #513: the elliptical marquee shares `select.rect` but records its own step name.
let mut s = session_with_doc();
let last = |s: &Session| s.active().unwrap().history.undo_label().map(str::to_string);
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 10, "ellipse": true})).unwrap();
assert_eq!(last(&s).as_deref(), Some("Elliptical Marquee"));
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 10})).unwrap();
assert_eq!(last(&s).as_deref(), Some("Rectangular Marquee"));
}
#[test]
fn undo_and_redo_restore_the_targeted_layers() {
// #495: each history state brings back the layers it targeted when it was created.
let mut s = session_with_doc();
let target = |s: &Session| {
let st = s.active().unwrap();
(st.active_layer.unwrap(), st.selected_layers())
};
let select = |s: &mut Session, id: LayerId, mode: &str| {
let steps = s.active().unwrap().history.past_len();
s.execute("layer.select", json!({"layer": id.0, "mode": mode})).unwrap();
assert_eq!(s.active().unwrap().history.past_len(), steps, "selecting is not a step");
};
let bg = target(&s).0;
s.execute("layer.new.layer", json!({})).unwrap();
let new = target(&s).0;
assert!(s.undo());
assert_eq!(target(&s), (bg, vec![bg]), "the new document's target");
assert!(s.redo());
assert_eq!(target(&s), (new, vec![new]), "redo targets the new layer again");
// So Fill paints the new layer, not the background.
s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
let doc = &s.active().unwrap().doc;
assert!(!doc.layer(new).unwrap().surface().unwrap().content_bounds().is_empty());
let mut v = [0.0f32; 4];
doc.layer(bg).unwrap().surface().unwrap().read_pixel(5, 5, &mut v);
assert_eq!(v, [1.0; 4], "the background is untouched");
// Selecting another layer doesn't change what a state targets: undo returns to New Layer's
// target and redo to Fill's.
select(&mut s, bg, "replace");
assert!(s.undo());
assert_eq!(target(&s), (new, vec![new]));
select(&mut s, bg, "replace");
assert!(s.redo());
assert_eq!(target(&s), (new, vec![new]), "redo targets the filled layer");
// Undoing a delete targets the restored layer.
s.execute("layer.delete", json!({})).unwrap();
assert_eq!(target(&s).0, bg);
assert!(s.undo());
assert_eq!(target(&s), (new, vec![new]));
// A multi-layer target comes back too: a step taken with both layers selected...
select(&mut s, bg, "add");
s.execute("select.all", json!({})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
assert!(s.undo());
assert_eq!(target(&s), (bg, vec![bg, new]));
// ...and the copies a multi-layer duplicate selects after its edit.
s.execute("layer.duplicate", json!({})).unwrap();
let copies = target(&s);
assert_eq!(copies.1.len(), 2);
select(&mut s, bg, "replace");
assert!(s.undo());
assert!(s.redo());
assert_eq!(target(&s), copies);
}
#[test]
fn selection_modes() {
let mut s = session_with_doc();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
s.execute("select.rect", json!({"x": 20, "y": 0, "width": 10, "height": 10, "mode": "add"})).unwrap();
let b = s.execute("document.inspect", json!({})).unwrap()["selectionBounds"].clone();
assert_eq!(b, json!([0, 0, 30, 10]));
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 10, "mode": "subtract"})).unwrap();
assert_eq!(s.execute("document.inspect", json!({})).unwrap()["selectionBounds"], json!([20, 0, 10, 10]));
s.execute("select.inverse", json!({})).unwrap();
s.execute("select.all", json!({})).unwrap();
assert_eq!(s.execute("document.inspect", json!({})).unwrap()["selectionBounds"], json!([0, 0, 64, 48]));
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20, "ellipse": true})).unwrap();
}
#[test]
fn fill_clear_and_masks() {
let mut s = session_with_doc();
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("edit.fill", json!({"color": "#00ff00"})).unwrap();
assert_eq!(px(&mut s, 5, 5), vec![0.0, 1.0, 0.0, 1.0]);
s.execute("layer.layerMask.hideAll", json!({})).unwrap();
assert_eq!(px(&mut s, 5, 5), vec![1.0, 1.0, 1.0, 1.0]);
s.execute("layer.layerMask.delete", json!({})).unwrap();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
s.execute("edit.clear", json!({})).unwrap();
assert_eq!(px(&mut s, 5, 5), vec![1.0, 1.0, 1.0, 1.0]);
assert_eq!(px(&mut s, 20, 20), vec![0.0, 1.0, 0.0, 1.0]);
}
#[test]
fn clipping_and_merge_and_flatten() {
let mut s = session_with_doc();
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 48})).unwrap();
s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
s.execute("select.deselect", json!({})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap();
s.execute("layer.createClippingMask", json!({})).unwrap();
assert_eq!(px(&mut s, 5, 5), vec![0.0, 0.0, 1.0, 1.0]);
assert_eq!(px(&mut s, 30, 5), vec![1.0, 1.0, 1.0, 1.0]);
s.execute("layer.mergeDown", json!({})).unwrap();
assert_eq!(s.active().unwrap().doc.layer_count(), 2);
assert_eq!(px(&mut s, 5, 5), vec![0.0, 0.0, 1.0, 1.0]);
assert_eq!(px(&mut s, 30, 5), vec![1.0, 1.0, 1.0, 1.0]);
s.execute("layer.flattenImage", json!({})).unwrap();
assert_eq!(s.active().unwrap().doc.layer_count(), 1);
assert_eq!(px(&mut s, 5, 5), vec![0.0, 0.0, 1.0, 1.0]);
}
#[test]
fn arrange_and_group() {
let mut s = session_with_doc();
let a = s.execute("layer.new.layer", json!({"name": "A"})).unwrap()["layer"].as_u64().unwrap();
s.execute("layer.new.layer", json!({"name": "B"})).unwrap();
s.execute("layer.select", json!({"layer": a})).unwrap();
s.execute("layer.arrange.bringToFront", json!({})).unwrap();
let names: Vec<String> = s.active().unwrap().doc.layers.iter().map(|l| l.name.clone()).collect();
assert_eq!(names, ["Background", "B", "A"]);
assert!(s.execute("layer.arrange.bringForward", json!({})).is_err());
s.execute("layer.groupLayers", json!({})).unwrap();
let doc = s.execute("document.inspect", json!({})).unwrap();
assert_eq!(doc["layers"][0]["kind"], "Group");
assert_eq!(doc["layers"][0]["children"][0]["name"], "A");
}
#[test]
fn canvas_flips_and_rotation() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 10, "height": 4})).unwrap();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 2, "height": 1})).unwrap();
s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
s.execute("select.deselect", json!({})).unwrap();
s.execute("image.imageRotation.flipCanvasHorizontal", json!({})).unwrap();
assert_eq!(px(&mut s, 9, 0), vec![1.0, 0.0, 0.0, 1.0]);
assert_eq!(px(&mut s, 0, 0), vec![1.0, 1.0, 1.0, 1.0]);
s.execute("image.imageRotation.flipCanvasVertical", json!({})).unwrap();
assert_eq!(px(&mut s, 9, 3), vec![1.0, 0.0, 0.0, 1.0]);
s.execute("image.imageRotation.90cw", json!({})).unwrap();
let d = &s.active().unwrap().doc;
assert_eq!((d.size.width, d.size.height), (4, 10));
assert_eq!(px(&mut s, 0, 9), vec![1.0, 0.0, 0.0, 1.0]);
s.execute("image.imageRotation.90ccw", json!({})).unwrap();
s.execute("image.imageRotation.180", json!({})).unwrap();
assert_eq!(px(&mut s, 0, 0), vec![1.0, 0.0, 0.0, 1.0]);
}
#[test]
fn image_rotation_rotates_every_layer_not_just_the_active_one() {
// Issue #1: Image Rotation must rotate the whole canvas, not only the selected layer.
let mut s = Session::new();
s.execute("file.new", json!({"width": 20, "height": 10, "background": "white"})).unwrap();
// A second (lower) layer with a red dot at the top-left; keep a third layer active.
s.execute("layer.new.layer", json!({})).unwrap();
s.edit("dot", |doc, a| {
doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(2, 2, 4, 4), &[1.0, 0.0, 0.0, 1.0]);
Ok(())
})
.unwrap();
let dotted = s.active().unwrap().active_layer.unwrap();
s.execute("layer.new.layer", json!({})).unwrap(); // a different, empty active layer
assert_ne!(s.active().unwrap().active_layer.unwrap(), dotted, "active layer is not the dotted one");
s.execute("image.imageRotation.180", json!({})).unwrap();
// The non-active layer's content rotated too: (2,2)..(4,4) in 20x10 → (16,6)..(18,8).
let st = s.active().unwrap();
let surf = st.doc.layer(dotted).unwrap().surface().unwrap();
assert_eq!(surf.rgba(17, 6), [1.0, 0.0, 0.0, 1.0], "dot moved to the opposite corner");
assert_eq!(surf.rgba(3, 3)[3], 0.0, "original spot is now empty");
// The Background layer rotated as well (still fully opaque white everywhere).
assert_eq!(st.doc.layers[0].surface().unwrap().rgba(1, 1), [1.0, 1.0, 1.0, 1.0]);
}
#[test]
fn fill_layers_and_colors() {
let mut s = session_with_doc();
s.execute("tools.setColors", json!({"foreground": "#112233"})).unwrap();
s.execute("tools.swapColors", json!({})).unwrap();
assert_eq!(s.tools.foreground, [1.0, 1.0, 1.0, 1.0]);
s.execute("tools.defaultColors", json!({})).unwrap();
s.execute("layer.newFillLayer.solidColor", json!({"color": "#ff00ff"})).unwrap();
assert_eq!(px(&mut s, 1, 1), vec![1.0, 0.0, 1.0, 1.0]);
s.execute("layer.newFillLayer.gradient", json!({"from": "#000000", "to": "#ffffff", "angle": 0})).unwrap();
let l = px(&mut s, 0, 10)[0];
let r = px(&mut s, 63, 10)[0];
assert!(l < r);
}
#[test]
fn journal_records_mutations_only() {
let mut s = session_with_doc();
s.execute("document.inspect", json!({})).unwrap();
s.execute("layer.new.layer", json!({"name": "x"})).unwrap();
let ids: Vec<&str> = s.journal.iter().map(|(id, _)| id.as_str()).collect();
assert_eq!(ids, ["file.new", "layer.new.layer"]);
// replaying the journal reproduces the document
let mut replay = Session::new();
for (id, p) in s.journal.clone() {
replay.execute(&id, p).unwrap();
}
assert_eq!(replay.active().unwrap().doc.layer_count(), s.active().unwrap().doc.layer_count());
}
#[test]
fn command_list_reports_enablement() {
let mut s = Session::new();
let list = s.execute("command.list", json!({})).unwrap();
let find = |id: &str| list.as_array().unwrap().iter().find(|c| c["id"] == id).unwrap().clone();
assert_eq!(find("file.new")["enabled"], true);
assert_eq!(find("layer.new.layer")["enabled"], false);
}
#[test]
fn translate_moves_pixels_and_respects_locks() {
let mut s = session_with_doc();
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 4, "height": 4})).unwrap();
s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
s.execute("select.deselect", json!({})).unwrap();
s.execute("layer.translate", json!({"dx": 10, "dy": 5})).unwrap();
assert_eq!(px(&mut s, 11, 6), vec![1.0, 0.0, 0.0, 1.0]);
assert_eq!(px(&mut s, 1, 1), vec![1.0, 1.0, 1.0, 1.0]);
// Background is position-locked
let bg = s.active().unwrap().doc.layers[0].id;
s.select_layer(bg).unwrap();
assert!(s.execute("layer.translate", json!({"dx": 1, "dy": 0})).is_err());
}
#[test]
fn damage_is_reported_for_strokes_only() {
let mut s = session_with_doc();
s.execute("layer.new.layer", json!({})).unwrap();
assert_eq!(s.active().unwrap().last_damage, None);
s.execute("paint.stroke", json!({"points": [[10, 10], [20, 10]], "size": 4})).unwrap();
let d = s.active().unwrap().last_damage.unwrap();
assert!(d.contains(15, 10) && d.width() < 30);
s.execute("edit.undo", json!({})).unwrap();
assert_eq!(s.active().unwrap().last_damage, None);
}
#[test]
fn integer_params_accept_json_floats() {
// UIs send coordinates as floats (e.g. 12.0); every integer parameter must accept them.
let mut s = session_with_doc();
s.execute("select.rect", json!({"x": 1.0, "y": 2.0, "width": 10.0, "height": 5.4})).unwrap();
assert_eq!(s.execute("document.inspect", json!({})).unwrap()["selectionBounds"], json!([1, 2, 10, 5]));
let px: Vec<f32> = serde_json::from_value(s.execute("document.pixel", json!({"x": 3.0, "y": 3.0})).unwrap()).unwrap();
assert_eq!(px, vec![1.0, 1.0, 1.0, 1.0]);
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("layer.translate", json!({"dx": 2.0, "dy": -1.0})).unwrap();
}
#[test]
fn move_to_refuses_to_nest_past_the_group_depth_cap() {
let mut s = session_with_doc();
let deep = s.execute("layer.new.layer", json!({"name": "Deep"})).unwrap()["layer"].as_u64().unwrap();
for _ in 0..photocraft_doc::MAX_GROUP_DEPTH - 1 {
s.execute("layer.groupLayers", json!({"layer": deep})).unwrap();
}
// `Deep` sits inside MAX - 1 groups. Moving a two-level group (G2 > G1 > Leaf) beside it
// would put `Leaf` at MAX + 1.
let path = s.active().unwrap().doc.path_of(photocraft_doc::LayerId(deep)).unwrap();
assert_eq!(path.len() - 1, photocraft_doc::MAX_GROUP_DEPTH - 1);
let leaf = s.execute("layer.new.layer", json!({"name": "Leaf"})).unwrap()["layer"].as_u64().unwrap();
let g1 = s.execute("layer.groupLayers", json!({"layer": leaf})).unwrap()["layer"].as_u64().unwrap();
let g = s.execute("layer.groupLayers", json!({"layer": g1})).unwrap()["layer"].as_u64().unwrap();
let past = s.active().unwrap().history.past_len();
let err = s.execute("layer.moveTo", json!({"layer": g, "target": deep, "position": "above"})).unwrap_err();
assert!(err.to_string().contains("deeper than"), "{err}");
assert!(s.active().unwrap().doc.max_group_depth() <= photocraft_doc::MAX_GROUP_DEPTH);
assert_eq!(s.active().unwrap().history.past_len(), past, "no history step recorded");
// A plain layer beside the deepest one still fits.
s.execute("layer.moveTo", json!({"layer": leaf, "target": deep, "position": "above"})).unwrap();
assert_eq!(s.active().unwrap().doc.max_group_depth(), photocraft_doc::MAX_GROUP_DEPTH - 1);
}
#[test]
fn artboard_from_layers_refuses_to_nest_past_the_group_depth_cap() {
let mut s = session_with_doc();
let leaf = s.execute("layer.new.layer", json!({"name": "Leaf"})).unwrap()["layer"].as_u64().unwrap();
let mut top = leaf;
for _ in 0..photocraft_doc::MAX_GROUP_DEPTH {
top = s.execute("layer.groupLayers", json!({"layer": top})).unwrap()["layer"].as_u64().unwrap();
}
assert_eq!(s.active().unwrap().doc.max_group_depth(), photocraft_doc::MAX_GROUP_DEPTH);
s.execute("layer.select", json!({"layer": top})).unwrap();
let past = s.active().unwrap().history.past_len();
let err = s.execute("layer.new.artboardFromLayers", json!({})).unwrap_err();
assert!(err.to_string().contains("deeper than"), "{err}");
assert_eq!(s.active().unwrap().doc.max_group_depth(), photocraft_doc::MAX_GROUP_DEPTH);
assert_eq!(s.active().unwrap().history.past_len(), past, "no history step recorded");
}
#[test]
fn move_to_reorders_and_nests() {
let mut s = session_with_doc();
let a = s.execute("layer.new.layer", json!({"name": "A"})).unwrap()["layer"].as_u64().unwrap();
let b = s.execute("layer.new.layer", json!({"name": "B"})).unwrap()["layer"].as_u64().unwrap();
let g = s.execute("layer.new.group", json!({"name": "G"})).unwrap()["layer"].as_u64().unwrap();
let names = |s: &Session| s.active().unwrap().doc.layers.iter().map(|l| l.name.clone()).collect::<Vec<_>>();
// bottom->top: Background, A, B, G ; move A above B
s.execute("layer.moveTo", json!({"layer": a, "target": b, "position": "above"})).unwrap();
assert_eq!(names(&s), ["Background", "B", "A", "G"]);
s.execute("layer.moveTo", json!({"layer": a, "target": g, "position": "into"})).unwrap();
assert_eq!(names(&s), ["Background", "B", "G"]);
assert_eq!(s.execute("document.inspect", json!({})).unwrap()["layers"][0]["children"][0]["name"], "A");
// groups can't go into themselves
assert!(s.execute("layer.moveTo", json!({"layer": g, "target": a, "position": "into"})).is_err());
// one undo step
s.execute("edit.undo", json!({})).unwrap();
assert_eq!(names(&s), ["Background", "B", "A", "G"]);
}
#[test]
fn selecting_a_layer_does_not_dirty_the_document() {
let mut s = session_with_doc();
let bg = s.active().unwrap().doc.layers[0].id;
assert!(!s.active().unwrap().is_dirty());
s.execute("layer.select", json!({"layer": bg.0})).unwrap();
assert!(!s.active().unwrap().is_dirty());
s.execute("layer.new.layer", json!({})).unwrap();
assert!(s.active().unwrap().is_dirty());
}
#[test]
fn coalesced_edits_share_one_history_step() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 20, "height": 20})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
let steps = |s: &Session| s.active().unwrap().history.entries().len();
let base = steps(&s);
for o in [10, 20, 30] {
s.execute("layer.setProps", json!({"opacity": o as f64 / 100.0, "coalesce": "drag-1"})).unwrap();
}
assert_eq!(steps(&s), base + 1, "three coalesced edits = one step");
// A different key starts a new step; an uncoalesced edit breaks the chain.
s.execute("layer.setProps", json!({"opacity": 0.4, "coalesce": "drag-2"})).unwrap();
s.execute("layer.setProps", json!({"opacity": 0.5})).unwrap();
s.execute("layer.setProps", json!({"opacity": 0.6, "coalesce": "drag-2"})).unwrap();
assert_eq!(steps(&s), base + 4);
// Undo returns to the state before the whole coalesced run.
s.undo();
s.undo();
s.undo();
s.undo();
let st = s.active().unwrap();
let l = st.doc.layer(st.active_layer.unwrap()).unwrap().opacity;
assert!((l - 1.0).abs() < 1e-6, "{l}");
}
#[test]
fn type_edit_rerenders_cache_to_new_text() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 800, "height": 400})).unwrap();
let r = s.execute("type.create", json!({"x": 50, "y": 200, "text": "Lorem Ipsum", "size": 48, "coalesce": "k"})).unwrap();
let id = r["layer"].as_u64().unwrap();
let width = |s: &Session| {
let st = s.active().unwrap();
st.doc.layer(photocraft_doc::LayerId(id)).unwrap().surface().unwrap().content_bounds().width()
};
let w0 = width(&s);
s.execute("type.edit", json!({"layer": id, "replace": {"start": 0, "end": 11, "text": "Photocraft"}, "coalesce": "k"})).unwrap();
let w1 = width(&s);
assert!(w0 > 150 && w1 > 150, "cache widths {w0} → {w1}");
}
#[test]
fn levels_and_curves_params_cover_output_and_channels() {
use photocraft_doc::Adjustment;
let a = crate::commands::adjustment_from_params("levels", &json!({"inBlack": 10, "outWhite": 200, "green": {"gamma": 1.5}}));
let Adjustment::Levels { master, per_channel, .. } = a else { panic!() };
assert!((master.in_black - 10.0 / 255.0).abs() < 1e-6 && (master.out_white - 200.0 / 255.0).abs() < 1e-6);
assert_eq!(per_channel[1].gamma, 1.5);
assert_eq!(per_channel[0].gamma, 1.0);
let c = crate::commands::adjustment_from_params("curves", &json!({"points": [[0, 0], [128, 160], [255, 255]], "blue": [[0, 20], [255, 235]]}));
let Adjustment::Curves { master, per_channel, .. } = c else { panic!() };
assert_eq!(master.len(), 3);
assert!((per_channel[2][0].output - 20.0 / 255.0).abs() < 1e-6);
assert_eq!(per_channel[0].len(), 2);
}
#[test]
fn painting_can_target_the_layer_mask() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 40, "height": 40})).unwrap();
s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap();
let id = s.active().unwrap().active_layer.unwrap();
s.edit("mask", |doc, _| {
doc.layer_mut(id).unwrap().mask = Some(photocraft_doc::LayerMask::reveal_all());
Ok(())
})
.unwrap();
// Adjustment layers are paintable through their mask only.
assert!(s.execute("paint.stroke", json!({"points": [[20, 20]], "size": 10, "hardness": 1.0, "color": "#000000"})).is_err());
s.execute("paint.stroke", json!({"points": [[20, 20]], "size": 10, "hardness": 1.0, "color": "#000000", "target": "mask"})).unwrap();
let m = |s: &Session, x, y| s.active().unwrap().doc.layer(id).unwrap().mask.as_ref().unwrap().surface.pixel(x, y)[0];
assert!(m(&s, 20, 20) < 0.01, "painted black");
assert!(m(&s, 2, 2) > 0.99, "rest still revealed");
// Eraser on a mask paints the background colour (white).
s.execute("paint.stroke", json!({"points": [[20, 20]], "size": 10, "hardness": 1.0, "erase": true, "target": "mask"})).unwrap();
assert!(m(&s, 20, 20) > 0.99);
// Gradient into the mask.
s.execute("paint.gradient", json!({"from": [0, 0], "to": [40, 0], "colors": ["#000000", "#ffffff"], "target": "mask"})).unwrap();
assert!(m(&s, 2, 20) < 0.1 && m(&s, 38, 20) > 0.9);
}
#[test]
fn layer_locks_are_set_and_enforced() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 20, "height": 20})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("layer.setProps", json!({"locks": {"pixels": true}})).unwrap();
assert!(s.execute("paint.stroke", json!({"points": [[5, 5]], "size": 4})).is_err(), "pixel lock blocks painting");
s.execute("layer.setProps", json!({"locks": {"pixels": false, "position": true}})).unwrap();
s.execute("paint.stroke", json!({"points": [[5, 5]], "size": 4})).unwrap();
assert!(s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 3, 0]})).is_err(), "position lock blocks transforms");
assert!(s.execute("layer.setProps", json!({"locks": {"bogus": true}})).is_err());
let st = s.active().unwrap();
let l = st.doc.layer(st.active_layer.unwrap()).unwrap();
assert!(l.locks.position && !l.locks.pixels);
}
#[test]
fn moving_a_layer_moves_its_effects_reference_point() {
let mut s = session_with_doc();
s.execute("layer.new.layer", json!({})).unwrap();
s.edit("ref", |doc, active| {
doc.layer_mut(active.unwrap()).unwrap().effects.reference = Some((-201.0, 9.0));
Ok(())
})
.unwrap();
s.execute("layer.translate", json!({"dx": 10, "dy": 5})).unwrap();
let d = s.active().unwrap();
let l = d.doc.layer(d.active_layer.unwrap()).unwrap();
assert_eq!(l.effects.reference, Some((-191.0, 14.0)));
}
#[test]
fn marquee_feather_and_anti_aliased_ellipse() {
let sel_at = |s: &Session, x: i32, y: i32| {
let mut v = [0.0f32];
if let Some(m) = s.active().unwrap().doc.selection.as_ref() {
m.read_pixel(x, y, &mut v);
}
v[0]
};
// Hard rectangle: fully in or out.
let mut s = session_with_doc();
s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
assert_eq!(sel_at(&s, 10, 20), 1.0);
assert_eq!(sel_at(&s, 9, 20), 0.0);
// Feather softens only the new shape's edge: partial coverage across the boundary.
s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20, "feather": 3})).unwrap();
let (inside, edge, outside) = (sel_at(&s, 20, 20), sel_at(&s, 10, 20), sel_at(&s, 8, 20));
assert!(inside > 0.95 && edge > 0.2 && edge < 0.8 && outside > 0.0 && outside < edge, "{inside} {edge} {outside}");
// Anti-aliased ellipse edges have partial coverage; aliased ones don't.
let partial = |s: &Session| (0..48).flat_map(|y| (0..64).map(move |x| (x, y))).filter(|&(x, y)| (0.01..0.99).contains(&sel_at(s, x, y))).count();
s.execute("select.rect", json!({"x": 5, "y": 5, "width": 40, "height": 30, "ellipse": true})).unwrap();
assert!(partial(&s) > 20);
s.execute("select.rect", json!({"x": 5, "y": 5, "width": 40, "height": 30, "ellipse": true, "antiAlias": false})).unwrap();
assert_eq!(partial(&s), 0);
}
#[test]
fn file_new_resolution_and_background_color() {
let mut s = Session::new();
s.tools.background = [1.0, 0.0, 0.0, 1.0];
s.execute("file.new", json!({"width": 8, "height": 8, "resolution": 300, "background": "backgroundColor"})).unwrap();
assert_eq!(s.active().unwrap().doc.resolution_dpi, 300.0);
let p = px(&mut s, 2, 2);
assert!(p[0] > 0.99 && p[1] < 0.01, "{p:?}");
}
#[test]
fn duplicating_the_background_unlocks_the_copy() {
let mut s = session_with_doc();
s.execute("layer.duplicate", json!({})).unwrap();
let st = s.active().unwrap();
let copy = st.doc.layer(st.active_layer.unwrap()).unwrap();
assert_eq!(copy.name, "Background copy");
assert_eq!(copy.locks, photocraft_doc::Locks::default());
assert!(st.doc.layers[0].locks.transparency);
}
#[test]
fn advanced_blending_channels() {
let mut s = session_with_doc();
let r = s.execute("layer.new.layer", json!({})).unwrap();
let id = r["layer"].as_u64().unwrap();
s.execute("layer.setProps", json!({"channels": [true, true, false]})).unwrap();
assert_eq!(s.active().unwrap().doc.layer(LayerId(id)).unwrap().excluded_channels, 0b100);
let ins = crate::inspect::layer(s.active().unwrap().doc.layer(LayerId(id)).unwrap());
assert_eq!(ins["channels"], json!([true, true, false, true]));
s.execute("edit.undo", json!({})).unwrap();
assert_eq!(s.active().unwrap().doc.layer(LayerId(id)).unwrap().excluded_channels, 0);
}
#[test]
fn move_document_reorders_tabs_and_keeps_the_active_one() {
let mut s = Session::new();
for name in ["a", "b", "c"] {
s.execute("file.new", json!({"width": 4, "height": 4, "name": name})).unwrap();
}
let names = |s: &Session| s.documents().iter().map(|d| d.doc.name.clone()).collect::<Vec<_>>();
let first = names(&s);
s.set_active(0);
assert_eq!(s.move_document(2, 0), Some(0));
assert_eq!(names(&s), [first[2].clone(), first[0].clone(), first[1].clone()]);
assert_eq!(s.active_index(), Some(1), "the active document follows its tab");
// `to` past the end moves to the last tab; `from` out of range does nothing.
assert_eq!(s.move_document(0, 99), Some(2));
assert_eq!(names(&s), first);
assert_eq!(s.move_document(3, 0), None);
let mut v = vec![1, 2];
assert_eq!(move_item(&mut v, 2, 0), None, "out of range: no panic, nothing moves");
assert_eq!(v, [1, 2]);
assert_eq!(names(&s), first);
assert_eq!(s.active_index(), Some(0));
// The command (for the UI, agents and scripts) moves the active document by default.
assert_eq!(s.execute("document.move", json!({"to": 2})).unwrap(), json!({"document": 2}));
assert_eq!(names(&s), [first[1].clone(), first[2].clone(), first[0].clone()]);
assert_eq!(s.execute("document.move", json!({"document": 2, "to": 0})).unwrap(), json!({"document": 0}));
assert_eq!(names(&s), first);
for bad in [json!({}), json!({"to": -1}), json!({"to": "1"}), json!({"document": 3, "to": 0}), json!({"document": 1.5, "to": 0})] {
assert!(s.execute("document.move", bad.clone()).is_err(), "{bad}");
}
assert_eq!(names(&s), first);
}