5a24289898fd
import.rs
//! Import: recursive folders, duplicate detection by content, add vs copy into the library.
use std::{
path::{Path, PathBuf},
sync::atomic::AtomicUsize,
};
use lightcraft_catalog::PhotoId;
use serde_json::{Value, json};
use crate::Session;
fn temp_dir(tag: &str) -> std::path::PathBuf {
let d = std::env::temp_dir().join(format!("lc-import-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
// Tests that take a folder "offline" rename it to this sibling; a left-over one (from a run
// whose process id is reused) is non-empty and would make that rename fail.
let _ = std::fs::remove_dir_all(d.with_extension("offline"));
std::fs::create_dir_all(&d).unwrap();
d
}
/// A small procedural PNG (distinct per seed).
fn write_png(path: &Path, seed: u8) {
let (w, h) = (48usize, 32usize);
let data: Vec<[u8; 4]> = (0..w * h).map(|i| [(i % w * 5) as u8, (i / w * 7) as u8, seed, 255]).collect();
let img = lightcraft_raster::Rgba8 { width: w, height: h, data };
let bytes = lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(&img), &lightcraft_codecs::EncodeMeta::default()).unwrap();
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, bytes).unwrap();
// undated files take the file's date: the tests' clock time (2026-01-14T05:58:48)
let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_768_370_328);
std::fs::File::options().write(true).open(path).unwrap().set_modified(t).unwrap();
}
fn exif_clock_block(original: Option<&str>, digitized: Option<&str>) -> Vec<u8> {
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
let mut exif = IfdBuilder::new();
if let Some(value) = original {
exif.set(0x9003, Value::Ascii(value.into()));
}
if let Some(value) = digitized {
exif.set(0x9004, Value::Ascii(value.into()));
}
let mut ifd0 = IfdBuilder::new();
ifd0.set_child(0x8769, exif);
TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap()
}
fn write_png_with_metadata(path: &Path, seed: u8, exif: Option<&[u8]>, xmp: Option<&str>) {
let (w, h) = (48usize, 32usize);
let data: Vec<[u8; 4]> = (0..w * h).map(|i| [(i % w * 5) as u8, (i / w * 7) as u8, seed, 255]).collect();
let img = lightcraft_raster::Rgba8 { width: w, height: h, data };
let metadata = lightcraft_codecs::EncodeMeta { exif, xmp, ..Default::default() };
let bytes = lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(&img), &metadata).unwrap();
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, bytes).unwrap();
// Keep the same known file-date fallback as write_png, distinct from the regression timestamp.
let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_768_370_328);
std::fs::File::options().write(true).open(path).unwrap().set_modified(t).unwrap();
}
fn ids(v: &Value, key: &str) -> usize {
v[key].as_array().map(Vec::len).unwrap_or(0)
}
#[test]
fn recursive_discovery_includes_sony_sr2_and_srf_case_insensitively() {
let src = temp_dir("sony-discovery");
std::fs::create_dir_all(src.join("trip/day2")).unwrap();
let names = ["r1.SR2", "f828.srf", "trip/r1.sr2", "trip/f828.SRF", "trip/day2/r1.Sr2", "trip/day2/f828.SrF", "control.JPG"];
// one component at a time: `join("trip/r1.sr2")` keeps the `/`, discovery yields the platform's separator
let mut expected: Vec<String> =
names.iter().map(|name| name.split('/').fold(src.clone(), |p, c| p.join(c)).to_string_lossy().into_owned()).collect();
for path in &expected {
std::fs::write(path, b"discovery only, not a decoded photo").unwrap();
}
std::fs::write(src.join("trip/notes.txt"), b"not a photo").unwrap();
let mut found = crate::import::expand(&[src.to_string_lossy().into_owned()], None);
found.sort();
expected.sort();
assert_eq!(found, expected);
assert_eq!(crate::import::expand(&expected, None), expected, "folder and direct discovery agree");
std::fs::remove_dir_all(src).unwrap();
}
#[test]
fn recursive_import_with_duplicates() {
let src = temp_dir("src");
write_png(&src.join("a.png"), 1);
write_png(&src.join("trip/b.png"), 2);
write_png(&src.join("trip/day2/c.PNG"), 3);
std::fs::copy(src.join("a.png"), src.join("trip/a-copy.png")).unwrap(); // same bytes
write_png(&src.join(".hidden/x.png"), 9);
std::fs::write(src.join("notes.txt"), "not a photo").unwrap();
std::fs::write(src.join("trip/broken.jpg"), "garbage").unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!(r["scanned"], 5, "{r}"); // a, b, c, a-copy, broken (hidden + txt skipped)
assert_eq!(ids(&r, "imported"), 3, "{r}");
assert_eq!(ids(&r, "duplicates"), 1, "{r}");
assert_eq!(r["duplicates"][0]["reason"], "content");
assert_eq!(ids(&r, "failed"), 1, "{r}");
assert_eq!(s.catalog.len(), 3);
assert!(s.catalog.photos().all(|p| p.content_hash.as_ref().is_some_and(|h| h.len() == 32) && p.width == 48));
// importing again: everything is a duplicate (by path), one undo step for the first import
let r2 = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!(ids(&r2, "imported"), 0);
assert_eq!(r2["duplicates"].as_array().unwrap().iter().filter(|d| d["reason"] == "path").count(), 3);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.catalog.len(), 0);
let _ = std::fs::remove_dir_all(&src);
}
#[test]
fn copy_into_library_and_persist() {
let src = temp_dir("copysrc");
let lib = temp_dir("copylib");
write_png(&src.join("one.png"), 4);
write_png(&src.join("sub/one.png"), 5); // same name, different content
let mut s = Session::new().with_fs();
assert!(s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "mode": "copy"})).is_err(), "copy needs a library");
s.open_library(&lib, false).unwrap();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "mode": "copy"})).unwrap();
assert_eq!(ids(&r, "imported"), 2, "{r}");
let paths: Vec<String> = s
.catalog
.photos()
.map(|p| match &p.source {
lightcraft_catalog::Source::File { path } => path.clone(),
_ => panic!(),
})
.collect();
for p in &paths {
assert!(Path::new(p).starts_with(lib.join("Originals")), "{p}");
assert!(Path::new(p).exists());
}
assert_ne!(paths[0], paths[1], "unique names");
// originals deleted from the source: the library copies remain usable after a restart
std::fs::remove_dir_all(&src).unwrap();
drop(s);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
assert_eq!(s.catalog.len(), 2);
let id = s.catalog.photos().next().unwrap().id;
assert!(s.render_now(id, 32, 32).is_ok());
// the library folder itself is never re-imported
let r = s.execute("library.import", &json!({"paths": [lib.to_string_lossy()]})).unwrap();
assert_eq!(r["scanned"], 0, "{r}");
let _ = std::fs::remove_dir_all(&lib);
}
#[test]
fn civil_dates() {
assert_eq!(crate::import::civil(0), "1970-01-01T00:00:00");
assert_eq!(crate::import::civil(951_782_400), "2000-02-29T00:00:00");
assert_eq!(crate::import::civil(1_790_000_000), "2026-09-21T14:13:20");
}
#[test]
fn exif_clock_import_uses_valid_fallback_dates_and_keeps_raw_tags() {
const INVALID: &str = "2026:01:01 12:34:56:99";
const XMP: &str = r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreateDate="2026-01-05T10:00:00Z"/></rdf:RDF></x:xmpmeta>"#;
let dir = temp_dir("exif-clock-fallbacks");
let digitized_path = dir.join("digitized.png");
let digitized_exif = exif_clock_block(Some(INVALID), Some("2026:01:02 10:00:00"));
write_png_with_metadata(&digitized_path, 1, Some(&digitized_exif), None);
let xmp_path = dir.join("xmp.png");
let malformed_only_exif = exif_clock_block(Some(INVALID), None);
write_png_with_metadata(&xmp_path, 2, Some(&malformed_only_exif), Some(XMP));
let mut s = Session::new().with_fs();
let imported = s.execute("library.import", &json!({"paths": [digitized_path.to_string_lossy(), xmp_path.to_string_lossy()]})).unwrap();
assert_eq!(ids(&imported, "imported"), 2, "{imported}");
let digitized = s.catalog.photos().find(|photo| photo.file_name == "digitized.png").unwrap();
let digitized_id = digitized.id.0;
assert_eq!(digitized.captured.as_deref(), Some("2026-01-02T10:00:00"));
let xmp = s.catalog.photos().find(|photo| photo.file_name == "xmp.png").unwrap();
let xmp_id = xmp.id.0;
assert_eq!(xmp.captured.as_deref(), Some("2026-01-05T10:00:00+00:00"));
let all = s.execute("photo.allMetadata", &json!({"id": digitized_id})).unwrap();
assert!(all["exif"].as_array().unwrap().iter().any(|row| row["tag"] == json!(0x9003) && row["value"] == INVALID), "{all}");
let all = s.execute("photo.allMetadata", &json!({"id": xmp_id})).unwrap();
assert!(all["exif"].as_array().unwrap().iter().any(|row| row["tag"] == json!(0x9003) && row["value"] == INVALID), "{all}");
assert!(all["xmp"].as_array().unwrap().iter().any(|row| row["name"] == "xmp:CreateDate" && row["value"] == "2026-01-05T10:00:00Z"), "{all}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn exif_clock_malformed_capture_cannot_fabricate_tracklog_match() {
use lightcraft_catalog::PhotoId;
const INVALID: &str = "2026:01:01 12:34:56:99";
const GPX: &str = r#"<gpx version="1.1" creator="test" xmlns="http://www.topografix.com/GPX/1/1"><trk><trkseg><trkpt lat="45" lon="7"><time>2026-01-01T12:34:56Z</time></trkpt></trkseg></trk></gpx>"#;
let dir = temp_dir("exif-clock-tracklog");
let malformed_path = dir.join("malformed.png");
let malformed_exif = exif_clock_block(Some(INVALID), None);
write_png_with_metadata(&malformed_path, 3, Some(&malformed_exif), None);
let valid_path = dir.join("valid.png");
let valid_exif = exif_clock_block(Some("2026:01:01 12:34:56"), None);
write_png_with_metadata(&valid_path, 4, Some(&valid_exif), None);
let mut s = Session::new().with_fs();
let imported = s.execute("library.import", &json!({"paths": [malformed_path.to_string_lossy()]})).unwrap();
assert_eq!(ids(&imported, "imported"), 1, "{imported}");
let bad_id = imported["imported"][0].as_u64().unwrap();
assert_eq!(
s.catalog.photo(PhotoId(bad_id)).unwrap().captured.as_deref(),
Some("2026-01-14T05:58:48"),
"malformed EXIF must leave the normal file-date fallback in effect"
);
let all = s.execute("photo.allMetadata", &json!({"id": bad_id})).unwrap();
assert!(
all["exif"].as_array().unwrap().iter().any(|row| row["tag"] == json!(0x9003) && row["value"] == INVALID),
"raw EXIF remains available: {all}"
);
// Leave both undo and redo populated so a no-match call must preserve the existing history branch.
s.execute("photo.setMeta", &json!({"ids": [bad_id], "title": "history control"})).unwrap();
s.execute("edit.undo", &json!({})).unwrap();
s.execute("library.select", &json!({"ids": [bad_id]})).unwrap();
assert!(!s.undo.is_empty() && !s.redo.is_empty());
let state = |session: &Session| {
(
session.catalog.to_snapshot(),
session.catalog.revision,
session.selection.clone(),
session.commits(),
session.undo.iter().map(|entry| (entry.label.clone(), entry.op.clone())).collect::<Vec<_>>(),
session.redo.iter().map(|entry| (entry.label.clone(), entry.op.clone())).collect::<Vec<_>>(),
)
};
let before = state(&s);
let dry = s.execute("photo.autoTagTracklog", &json!({"gpx": GPX, "ids": [bad_id], "maxGap": 0, "dryRun": true})).unwrap();
assert_eq!(dry["tagged"], 0, "{dry}");
assert_eq!(dry["skipped"]["outside"], 1, "the file-date fallback is outside the one-point track: {dry}");
assert_eq!(state(&s), before, "dry-run changed catalog, selection or history");
let actual = s.execute("photo.autoTagTracklog", &json!({"gpx": GPX, "ids": [bad_id], "maxGap": 0})).unwrap();
assert_eq!(actual["tagged"], 0, "{actual}");
assert_eq!(actual["skipped"]["outside"], 1, "{actual}");
assert_eq!(state(&s), before, "unmatched tracklog call changed catalog, selection or history");
assert_eq!(s.catalog.photo(PhotoId(bad_id)).unwrap().meta.gps, None);
let imported = s.execute("library.import", &json!({"paths": [valid_path.to_string_lossy()]})).unwrap();
assert_eq!(ids(&imported, "imported"), 1, "{imported}");
let valid_id = imported["imported"][0].as_u64().unwrap();
assert_eq!(s.catalog.photo(PhotoId(valid_id)).unwrap().captured.as_deref(), Some("2026-01-01T12:34:56"));
s.execute("library.select", &json!({"ids": [valid_id]})).unwrap();
let undo_before = s.undo.len();
let tagged = s.execute("photo.autoTagTracklog", &json!({"gpx": GPX, "ids": [valid_id], "maxGap": 0})).unwrap();
assert_eq!(tagged["tagged"], 1, "{tagged}");
assert_eq!(s.catalog.photo(PhotoId(valid_id)).unwrap().meta.gps, Some((45.0, 7.0)));
assert_eq!(s.undo.len(), undo_before + 1, "valid tagging is one undo step");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.catalog.photo(PhotoId(valid_id)).unwrap().meta.gps, None);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn browsing_a_folder_lists_its_photos_without_adding_them() {
use crate::LibrarySource;
let dir = std::env::temp_dir().join(format!("lc-browse-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("sub")).unwrap();
let png = |p: &std::path::Path, seed: u8| {
let img = lightcraft_raster::Rgba8::from_fn(16, 12, |x, y| [(x * 9) as u8, (y * 11) as u8, seed, 255]);
let b = crate::export::encode_image(&img, &crate::export::ExportOptions { format: crate::export::ExportFormat::Png, ..Default::default() })
.unwrap();
std::fs::write(p, b).unwrap();
};
png(&dir.join("a.png"), 1);
png(&dir.join("b.png"), 2);
png(&dir.join("sub/c.png"), 3);
std::fs::write(dir.join("notes.txt"), "x").unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.browse", &serde_json::json!({"path": dir.to_string_lossy()})).unwrap();
assert_eq!(r["photos"], 2, "{r}");
assert_eq!(s.source, LibrarySource::Folder);
assert_eq!(s.visible_cloned().len(), 2);
// not in the library
s.execute("library.source", &serde_json::json!({"kind": "all"})).unwrap();
assert!(s.visible_cloned().is_empty(), "browsed photos stay out of All Photos");
assert_eq!(s.execute("catalog.stats", &serde_json::json!({})).unwrap()["photos"], 0, "nor in the counts");
assert!(s.catalog.date_groups().is_empty());
// subfolders; browsing again reuses the photos
let r = s.execute("library.browse", &serde_json::json!({"path": dir.to_string_lossy(), "subfolders": true})).unwrap();
assert_eq!((r["photos"].as_u64(), r["new"].as_u64()), (Some(3), Some(1)));
// add one to the library
let first = s.visible_cloned()[0];
s.execute("photo.addToLibrary", &serde_json::json!({"ids": [first.0]})).unwrap();
// importing the folder for real brings in the rest (no duplicates of the browsed ones)
let r = s.execute("library.import", &serde_json::json!({"paths": [dir.to_string_lossy()]})).unwrap();
assert_eq!(r["imported"].as_array().map(Vec::len), Some(2), "{r}");
s.execute("library.source", &serde_json::json!({"kind": "all"})).unwrap();
assert_eq!(s.visible_cloned().len(), 3);
assert_eq!(s.catalog.photos().count(), 3);
assert!(s.execute("library.browse", &serde_json::json!({"path": dir.join("a.png").to_string_lossy()})).is_err());
let _ = std::fs::remove_dir_all(&dir);
}
/// PERF-CULL-1: the Local view reads only what the library lacks, newest file first for the default
/// (newest capture first) sort, so the first screen is read first; other sorts follow their key.
#[test]
fn browsing_queues_unknown_files_in_the_order_the_view_shows_them() {
use lightcraft_catalog::{Sort, SortKey};
let dir = temp_dir("browse-order");
let set_time = |p: &Path, secs: u64| {
let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs);
std::fs::File::options().write(true).open(p).unwrap().set_modified(t).unwrap();
};
for (name, seed, secs) in [("a.png", 1u8, 300u64), ("b.png", 2, 100), ("c.png", 3, 200)] {
write_png(&dir.join(name), seed);
set_time(&dir.join(name), 1_700_000_000 + secs);
}
let mut s = Session::new().with_fs();
let known = dir.join("b.png").to_string_lossy().to_string();
s.execute("library.import", &json!({"paths": [known.clone()]})).unwrap();
let files: Vec<String> = ["a.png", "b.png", "c.png", "gone.png"].iter().map(|n| dir.join(n).to_string_lossy().to_string()).collect();
let (input, _) = crate::import::ScanInput::new(&mut s, &[]);
let names = |v: Vec<String>| v.iter().map(|f| Path::new(f).file_name().unwrap().to_string_lossy().to_string()).collect::<Vec<_>>();
// newest first, the file already in the library left out, an unreadable one last
let q = crate::import::browse_queue(&input, files.clone(), &Sort::default());
assert_eq!(names(q), ["a.png", "c.png", "gone.png"]);
let q = crate::import::browse_queue(&input, files.clone(), &Sort { ascending: true, ..Sort::default() });
assert_eq!(names(q), ["c.png", "a.png", "gone.png"]);
let q = crate::import::browse_queue(&input, files.clone(), &Sort { key: SortKey::FileName, ascending: false, ..Sort::default() });
assert_eq!(names(q), ["gone.png", "c.png", "a.png"]);
// by size an unreadable file is last in both directions (it was sized 0, first when ascending)
for ascending in [true, false] {
let q = crate::import::browse_queue(&input, files.clone(), &Sort { key: SortKey::FileSize, ascending, ..Sort::default() });
assert_eq!(names(q).last().map(String::as_str), Some("gone.png"), "ascending {ascending}");
}
let q = crate::import::browse_queue(&input, files, &Sort { key: SortKey::Rating, ..Sort::default() });
assert_eq!(names(q), ["gone.png", "c.png", "a.png"], "new photos tie on rating: by name, descending as the view");
let _ = std::fs::remove_dir_all(&dir);
}
/// Choosing another folder while a browse orders its files (`cancel` set) stops the ordering: no queue comes
/// back, and nothing more is read (CodeRabbit on #3055). Browsing hands back the probes of earlier scans it took.
#[test]
fn a_cancelled_browse_stops_ordering_and_keeps_earlier_probes() {
use lightcraft_catalog::{Sort, SortKey};
use std::sync::atomic::AtomicBool;
let dir = temp_dir("browse-cancel");
write_png(&dir.join("a.png"), 1);
write_png(&dir.join("b.png"), 2);
let files: Vec<String> = ["a.png", "b.png"].iter().map(|n| dir.join(n).to_string_lossy().to_string()).collect();
let mut s = Session::new().with_fs();
s.import_probes.insert(files[0].clone(), crate::media::ProbeInfo { file_size: 1, ..Default::default() });
let (mut input, _) = crate::import::ScanInput::new(&mut s, &[]);
for key in [SortKey::CaptureDate, SortKey::FileSize, SortKey::FileName, SortKey::Random] {
let sort = Sort { key, ..Sort::default() };
assert_eq!(crate::import::browse_queue_until(&input, files.clone(), &sort, &AtomicBool::new(true)), None, "{key:?}");
assert_eq!(crate::import::browse_queue_until(&input, files.clone(), &sort, &AtomicBool::new(false)).map(|q| q.len()), Some(2), "{key:?}");
}
let probes = input.take_probes();
assert!(probes.contains_key(&files[0]), "the earlier scan's probe comes back");
let _ = std::fs::remove_dir_all(&dir);
}
/// Browsing a folder with its subfolders lists nothing inside the library's own folder (the
/// import that follows would read it as photos), as the import's scan skips it.
#[test]
fn browsing_with_subfolders_skips_the_librarys_own_folder() {
let dir = temp_dir("browse-skip-lib");
std::fs::create_dir_all(dir.join("lib")).unwrap();
let mut s = Session::new().with_fs();
s.open_library(dir.join("lib"), false).unwrap();
write_png(&dir.join("a.png"), 1);
write_png(&dir.join("lib").join("inside.png"), 2);
let (input, _) = crate::import::ScanInput::new(&mut s, &[]);
let root = dir.to_string_lossy().to_string();
let names = |v: Vec<String>| v.iter().map(|f| Path::new(f).file_name().unwrap().to_string_lossy().to_string()).collect::<Vec<_>>();
assert_eq!(names(crate::import::browse_files(&input, &root, true).unwrap()), ["a.png"]);
assert_eq!(names(crate::import::browse_files(&input, &root, false).unwrap()), ["a.png"]);
// the library's folder chosen itself, with or without its subfolders
let lib = dir.join("lib").to_string_lossy().to_string();
assert!(crate::import::browse_files(&input, &lib, false).unwrap().is_empty());
assert!(crate::import::browse_files(&input, &lib, true).unwrap().is_empty());
// the library's folder by another name: through `..`, or a symlink to it
std::fs::create_dir_all(dir.join("other")).unwrap();
let around = dir.join("other").join("..").join("lib").to_string_lossy().to_string();
assert!(crate::import::browse_files(&input, &around, false).unwrap().is_empty(), "{around}");
assert!(crate::import::browse_files(&input, &around, true).unwrap().is_empty(), "{around}");
#[cfg(unix)]
{
let alias = dir.join("alias");
std::os::unix::fs::symlink(dir.join("lib"), &alias).unwrap();
let alias = alias.to_string_lossy().to_string();
assert!(crate::import::browse_files(&input, &alias, false).unwrap().is_empty(), "{alias}");
assert!(crate::import::browse_files(&input, &alias, true).unwrap().is_empty(), "{alias}");
}
// a folder next to the library named through it is outside it: browsed (CodeRabbit on #3077)
std::fs::create_dir_all(dir.join("outside")).unwrap();
write_png(&dir.join("outside").join("next.png"), 3);
let next = dir.join("lib").join("..").join("outside").to_string_lossy().to_string();
assert_eq!(names(crate::import::browse_files(&input, &next, false).unwrap()), ["next.png"], "{next}");
assert_eq!(names(crate::import::browse_files(&input, &next, true).unwrap()), ["next.png"], "{next}");
// under the folder's one spelling: the paths match the folder's own, so the catalog's records are found
// (CodeRabbit on #3055)
let direct = dir.join("outside").to_string_lossy().to_string();
for sub in [false, true] {
assert_eq!(crate::import::browse_files(&input, &next, sub).unwrap(), crate::import::browse_files(&input, &direct, sub).unwrap());
}
// a folder inside the library that isn't there is an error too, not an empty folder
let gone = dir.join("lib").join("not-there").to_string_lossy().to_string();
assert!(crate::import::browse_files(&input, &gone, false).is_err(), "{gone}");
// a folder that can't be listed is an error, not an empty folder (its photos aren't "gone")
let missing = dir.join("not-there").to_string_lossy().to_string();
assert!(crate::import::browse_files(&input, &missing, false).is_err());
assert!(crate::import::browse_files(&input, &missing, true).is_err());
let _ = std::fs::remove_dir_all(&dir);
}
/// Browsing's queue breaks ties the way the catalog does (by name, in the view's direction), and
/// the import- and edit-date sorts, where new photos tie, go by name rather than file time.
#[test]
fn browsing_breaks_ties_by_name_in_the_views_direction() {
use lightcraft_catalog::{Sort, SortKey};
let dir = temp_dir("browse-ties");
// names and file times run in opposite directions; x and y share a time and a size
for (name, secs) in [("x.png", 100u64), ("y.png", 100), ("z.png", 50)] {
write_png(&dir.join(name), 7);
let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000 + secs);
std::fs::File::options().write(true).open(dir.join(name)).unwrap().set_modified(t).unwrap();
}
let files: Vec<String> = ["y.png", "z.png", "x.png"].iter().map(|n| dir.join(n).to_string_lossy().to_string()).collect();
let order = |key: SortKey, ascending: bool| {
let mut f = files.clone();
crate::import::browse_order(&mut f, &Sort { key, ascending, ..Sort::default() });
f.iter().map(|f| Path::new(f).file_name().unwrap().to_string_lossy().to_string()).collect::<Vec<_>>()
};
assert_eq!(order(SortKey::CaptureDate, false), ["y.png", "x.png", "z.png"]);
assert_eq!(order(SortKey::CaptureDate, true), ["z.png", "x.png", "y.png"]);
for key in [SortKey::ImportDate, SortKey::EditDate, SortKey::Rating] {
assert_eq!(order(key, true), ["x.png", "y.png", "z.png"], "{key:?}");
assert_eq!(order(key, false), ["z.png", "y.png", "x.png"], "{key:?}");
}
assert_eq!(order(SortKey::FileSize, true), ["x.png", "y.png", "z.png"]);
assert_eq!(order(SortKey::FileSize, false), ["z.png", "y.png", "x.png"]);
let _ = std::fs::remove_dir_all(&dir);
}
/// Photos a.png, b.png, c.png imported from `old`, then: a renamed into `moved`; b renamed, with
/// an impostor (same name and size, other bytes) under its old name; c gone, with two impostors.
fn renamed_and_ambiguous_scene(tag: &str) -> (std::path::PathBuf, Session) {
let dir = temp_dir(tag);
std::fs::create_dir_all(dir.join("old")).unwrap();
std::fs::create_dir_all(dir.join("moved/real")).unwrap();
for (n, seed) in [("a.png", 1), ("b.png", 2), ("c.png", 3)] {
write_png(&dir.join("old").join(n), seed);
}
let mut s = Session::new().with_fs();
s.execute("library.import", &json!({"paths": [dir.join("old").to_string_lossy()]})).unwrap();
let impostor = |src: &Path, dst: &Path| {
let mut b = std::fs::read(src).unwrap();
let k = b.len() - 20;
b[k] ^= 0xff;
std::fs::write(dst, b).unwrap();
};
std::fs::rename(dir.join("old/a.png"), dir.join("moved/Trip-001.png")).unwrap();
impostor(&dir.join("old/b.png"), &dir.join("moved/b.png"));
std::fs::rename(dir.join("old/b.png"), dir.join("moved/real/Trip-002.png")).unwrap();
impostor(&dir.join("old/c.png"), &dir.join("moved/c.png"));
impostor(&dir.join("old/c.png"), &dir.join("moved/real/c.png"));
std::fs::remove_file(dir.join("old/c.png")).unwrap();
(dir, s)
}
/// Issue #105: Find Missing finds renamed files by content, prefers the content match over a
/// same-name same-size impostor, and skips (reports) photos it can't tell apart.
#[test]
fn find_missing_matches_renamed_files_by_content() {
let (dir, mut s) = renamed_and_ambiguous_scene("missing-content");
let r = s.execute("library.findMissing", &json!({"folder": dir.join("moved").to_string_lossy()})).unwrap();
let found = r["found"].as_array().unwrap();
assert_eq!(found.len(), 2, "{r}");
let to = |name: &str| {
found.iter().find(|f| f["from"].as_str().unwrap().ends_with(name)).map(|f| (f["to"].as_str().unwrap().to_string(), f["by"].clone()))
};
assert_eq!(to("a.png"), Some((dir.join("moved").join("Trip-001.png").to_string_lossy().to_string(), json!("content"))));
assert_eq!(
to("b.png"),
Some((dir.join("moved").join("real").join("Trip-002.png").to_string_lossy().to_string(), json!("content"))),
"not the impostor"
);
assert_eq!(r["missing"], 1);
assert_eq!(r["ambiguous"][0]["candidates"].as_array().map(Vec::len), Some(2), "{r}");
let _ = std::fs::remove_dir_all(&dir);
}
/// A renamed file whose size is unknown (0) but whose content hash is known is still found by
/// content: the hash alone is enough to confirm it is the same photo.
#[test]
fn find_missing_matches_renamed_file_by_content_without_size() {
use lightcraft_catalog::{Op, Photo, Source};
let dir = temp_dir("missing-no-size");
std::fs::create_dir_all(dir.join("found")).unwrap();
let content = b"same pixels as the original";
let renamed = dir.join("found").join("Trip-001.png");
std::fs::write(&renamed, content).unwrap();
let hash = lightcraft_preview::hash_bytes(content).to_string();
let mut s = Session::new();
let id = s.catalog.alloc_photo_id();
let mut p = Photo::new(
id,
Source::File { path: dir.join("gone").join("orig.png").to_string_lossy().into_owned() },
"orig.png",
"PNG",
48,
32,
"2026-01-01T10:00:00",
);
p.file_size = 0;
p.content_hash = Some(hash);
s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
let r = s.execute("library.findMissing", &json!({"folder": dir.join("found").to_string_lossy()})).unwrap();
let found = r["found"].as_array().map(Vec::len);
assert_eq!(found, Some(1), "renamed file matched by content without a recorded size: {r}");
assert_eq!(r["found"][0]["to"], json!(renamed.to_string_lossy().to_string()), "{r}");
assert_eq!(r["found"][0]["by"], json!("content"), "{r}");
assert_eq!(r["missing"], 0, "{r}");
let _ = std::fs::remove_dir_all(&dir);
}
/// Issue #104: the app plans Find Missing on a worker thread (no session) and relinks the plan
/// afterwards; that gives exactly what the synchronous command gives — renamed files by content,
/// impostors and look-alikes skipped (`ambiguous`).
#[test]
fn find_missing_planned_on_a_worker_matches_the_command() {
use crate::cmd::missing::{find_candidates, plan_find_missing};
let (dir, mut s) = renamed_and_ambiguous_scene("missing-worker");
let folder = dir.join("moved").to_string_lossy().to_string();
let candidates = find_candidates(&s.catalog);
let f = folder.clone();
let plan = std::thread::spawn(move || plan_find_missing(&candidates, &f)).join().unwrap().unwrap();
assert_eq!((plan.found.len(), plan.missing, plan.ambiguous.len()), (2, 1, 1), "{plan:?}");
let applied = s.execute("library.findMissing", &plan.to_json()).unwrap();
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.execute("library.missing", &json!({})).unwrap().as_array().map(Vec::len), Some(3), "undo restores all three");
let direct = s.execute("library.findMissing", &json!({"folder": folder})).unwrap();
assert_eq!(applied, direct);
assert_eq!(direct["found"].as_array().map(Vec::len), Some(2), "{direct}");
assert_eq!(direct["ambiguous"].as_array().map(Vec::len), Some(1), "{direct}");
let _ = std::fs::remove_dir_all(&dir);
}
/// Issue #104: a plan made on a worker is re-checked when it is applied: a photo relinked
/// meanwhile is left alone, and a file that is now another photo's original isn't used.
#[test]
fn find_missing_plan_is_rechecked_when_applied() {
use crate::cmd::missing::{find_candidates, plan_find_missing};
let (dir, mut s) = renamed_and_ambiguous_scene("missing-recheck");
let plan = plan_find_missing(&find_candidates(&s.catalog), &dir.join("moved").to_string_lossy()).unwrap();
let to = |name: &str| plan.found.iter().find(|f| f.from.ends_with(name)).unwrap().clone();
let (a, b) = (to("a.png"), to("b.png"));
// meanwhile: a is relinked by hand (elsewhere), and the ambiguous c is pointed at b's file
std::fs::copy(&a.to, dir.join("a-elsewhere.png")).unwrap();
s.execute("photo.relink", &json!({"id": a.id.0, "path": dir.join("a-elsewhere.png").to_string_lossy()})).unwrap();
let c = plan.ambiguous[0].id;
s.execute("photo.relink", &json!({"id": c.0, "path": b.to})).unwrap();
let r = s.execute("library.findMissing", &plan.to_json()).unwrap();
assert_eq!(r["found"].as_array().map(Vec::len), Some(0), "{r}");
assert_eq!(r["ambiguous"].as_array().map(Vec::len), Some(0), "c isn't missing any more: {r}");
assert_eq!(r["missing"], 1, "b stays missing: its file is c's now: {r}");
let path_of = |id: lightcraft_catalog::PhotoId| match &s.catalog.photo(id).unwrap().source {
lightcraft_catalog::Source::File { path } => path.clone(),
_ => String::new(),
};
assert_eq!(path_of(a.id), dir.join("a-elsewhere.png").to_string_lossy());
assert_eq!(path_of(b.id), b.from);
let _ = std::fs::remove_dir_all(&dir);
}
/// A library made on Windows names its originals with Windows paths; opened on macOS or Linux
/// they are missing until Find Missing finds them by name, there as everywhere: the name follows
/// the last `/` or `\`, any letter case, the size checked when one is on record, look-alikes
/// reported rather than guessed, and undo puts back the path exactly as spelled.
#[test]
fn find_missing_finds_the_files_of_a_library_made_on_windows() {
use lightcraft_catalog::{Op, Photo, PhotoId, Source};
let dir = temp_dir("missing-windows");
for (rel, bytes) in [("found/a.jpg", &b"x"[..]), ("found/deep/b.jpg", b"xyz"), ("found/one/twin.jpg", b"x"), ("found/two/twin.jpg", b"x")] {
std::fs::create_dir_all(dir.join(rel).parent().unwrap()).unwrap();
std::fs::write(dir.join(rel), bytes).unwrap();
}
let mut s = Session::new();
// as a Windows library may list them: no content hash, and a size only for b
let mut add = |path: &str, size: u64| {
let id = s.catalog.alloc_photo_id();
let mut p = Photo::new(id, Source::File { path: path.into() }, "x.jpg", "JPEG", 60, 40, "2026-01-01T10:00:00");
p.file_size = size;
s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
id
};
let (a, b, twin) = (add(r"C:\Users\me\Photos\a.jpg", 0), add(r"D:\Shoot\2019\B.JPG", 3), add(r"C:\Users\me\Photos\twin.jpg", 0));
let path_of = |s: &Session, id: PhotoId| match &s.catalog.photo(id).unwrap().source {
Source::File { path } => path.clone(),
_ => String::new(),
};
let r = s.execute("library.findMissing", &json!({"folder": dir.join("found").to_string_lossy()})).unwrap();
assert_eq!(path_of(&s, a), dir.join("found").join("a.jpg").to_string_lossy(), "found by its name: {r}");
assert_eq!(path_of(&s, b), dir.join("found").join("deep").join("b.jpg").to_string_lossy(), "found by its name, any case, and size: {r}");
assert_eq!(path_of(&s, twin), r"C:\Users\me\Photos\twin.jpg", "two files of its name: not guessed: {r}");
assert_eq!(r["ambiguous"][0]["candidates"].as_array().map(Vec::len), Some(2), "both look-alikes reported: {r}");
assert_eq!(r["missing"], 1, "{r}");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(
(path_of(&s, a), path_of(&s, b)),
(r"C:\Users\me\Photos\a.jpg".to_string(), r"D:\Shoot\2019\B.JPG".to_string()),
"one undo step puts back the Windows paths as they were spelled"
);
let _ = std::fs::remove_dir_all(&dir);
}
/// On macOS and Linux `\` may be part of a file name. Find Missing names the files it finds by
/// the same rule as the missing photos, so `a\b.jpg` isn't relinked to a `b.jpg` beside it
/// unasked: the two are reported as look-alikes.
#[cfg(unix)]
#[test]
fn find_missing_does_not_take_a_look_alike_for_a_file_name_with_a_backslash() {
use lightcraft_catalog::{Op, Photo, Source};
let dir = temp_dir("missing-backslash");
std::fs::create_dir_all(dir.join("found")).unwrap();
for name in [r"a\b.jpg", "b.jpg"] {
std::fs::write(dir.join("found").join(name), b"x").unwrap();
}
let mut s = Session::new();
let gone = dir.join("gone").join(r"a\b.jpg").to_string_lossy().to_string();
let id = s.catalog.alloc_photo_id();
let p = Photo::new(id, Source::File { path: gone.clone() }, "x.jpg", "JPEG", 60, 40, "2026-01-01T10:00:00");
s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
let r = s.execute("library.findMissing", &json!({"folder": dir.join("found").to_string_lossy()})).unwrap();
assert!(matches!(&s.catalog.photo(id).unwrap().source, Source::File { path } if *path == gone), "not relinked to b.jpg: {r}");
assert_eq!(r["ambiguous"][0]["candidates"].as_array().map(Vec::len), Some(2), "both reported: {r}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn missing_files_are_found_and_relinked() {
let dir = std::env::temp_dir().join(format!("lc-missing-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("old")).unwrap();
std::fs::create_dir_all(dir.join("moved/deeper")).unwrap();
let png = |p: &std::path::Path, seed: u8| {
let img = lightcraft_raster::Rgba8::from_fn(16, 12, |x, y| [(x * 9) as u8, (y * 11) as u8, seed, 255]);
let b = crate::export::encode_image(&img, &crate::export::ExportOptions { format: crate::export::ExportFormat::Png, ..Default::default() })
.unwrap();
std::fs::write(p, b).unwrap();
};
png(&dir.join("old/a.png"), 1);
png(&dir.join("old/b.png"), 2);
let mut s = Session::new().with_fs();
s.execute("library.import", &serde_json::json!({"paths": [dir.join("old").to_string_lossy()]})).unwrap();
assert_eq!(s.execute("library.missing", &serde_json::json!({})).unwrap().as_array().map(Vec::len), Some(0));
// the files move away
std::fs::rename(dir.join("old/a.png"), dir.join("moved/a.png")).unwrap();
std::fs::rename(dir.join("old/b.png"), dir.join("moved/deeper/b.png")).unwrap();
let m = s.execute("library.missing", &serde_json::json!({})).unwrap();
assert_eq!(m.as_array().map(Vec::len), Some(2));
s.execute("library.source", &serde_json::json!({"kind": "missing"})).unwrap();
assert_eq!(s.visible_cloned().len(), 2, "the Missing Photos source lists them");
s.execute("library.source", &serde_json::json!({"kind": "all"})).unwrap();
// one by hand
let id = m[0]["id"].as_u64().unwrap();
let name = std::path::Path::new(m[0]["path"].as_str().unwrap()).file_name().unwrap().to_string_lossy().to_string();
let new = if name == "a.png" { dir.join("moved/a.png") } else { dir.join("moved/deeper/b.png") };
s.execute("photo.relink", &serde_json::json!({"id": id, "path": new.to_string_lossy()})).unwrap();
assert_eq!(s.execute("library.missing", &serde_json::json!({})).unwrap().as_array().map(Vec::len), Some(1));
assert!(s.execute("photo.relink", &serde_json::json!({"id": id, "path": dir.join("nope.png").to_string_lossy()})).is_err());
// the rest by searching a folder
let r = s.execute("library.findMissing", &serde_json::json!({"folder": dir.join("moved").to_string_lossy()})).unwrap();
assert_eq!((r["found"].as_array().map(Vec::len), r["missing"].as_u64()), (Some(1), Some(0)), "{r}");
// undo points the photo back at the old path but doesn't move any file
s.execute("edit.undo", &serde_json::json!({})).unwrap();
assert_eq!(s.execute("library.missing", &serde_json::json!({})).unwrap().as_array().map(Vec::len), Some(1));
assert!(dir.join("moved/a.png").exists() && dir.join("moved/deeper/b.png").exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn recently_added_covers_recent_imports_newest_first() {
let dir = std::env::temp_dir().join(format!("lc-recent-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let png = |name: &str, seed: u8| {
let img = lightcraft_raster::Rgba8::from_fn(16, 12, |x, y| [(x * 9) as u8, (y * 11) as u8, seed, 255]);
let b = crate::export::encode_image(&img, &crate::export::ExportOptions { format: crate::export::ExportFormat::Png, ..Default::default() })
.unwrap();
std::fs::write(dir.join(name), b).unwrap();
dir.join(name).to_string_lossy().to_string()
};
let mut s = Session::new().with_fs();
// three imports: 60 days ago, 10 days ago, today
for (when, name, seed) in [("2026-08-01T09:00:00", "old.png", 1), ("2026-09-20T09:00:00", "mid.png", 2), ("2026-09-30T09:00:00", "new.png", 3)] {
let path = png(name, seed);
let w = when.to_string();
s.clock = Box::new(move || w.clone());
s.execute("library.import", &serde_json::json!({"paths": [path]})).unwrap();
}
s.execute("library.source", &serde_json::json!({"kind": "recentlyAdded"})).unwrap();
let names: Vec<String> = s.visible_cloned().iter().map(|id| s.catalog.photo(*id).unwrap().file_name.clone()).collect();
assert_eq!(names, ["new.png", "mid.png"], "the last 30 days, newest import first");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn recently_added_keeps_an_import_batch_in_file_order() {
use lightcraft_catalog::{Op, Photo, PhotoId, Source};
let mut s = Session::new();
// one import batch: one `imported` string, ids running opposite to the names
let mut by_name = Vec::new();
for (n, name) in ["IMG_0001.png", "IMG_0002.png", "IMG_0003.png", "IMG_0004.png"].iter().enumerate() {
let id = PhotoId(5000 - n as u64);
let p = Photo::new(id, Source::File { path: name.to_string() }, name, "PNG", 16, 12, "2026-09-30T09:00:00");
s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
by_name.push(name.to_string());
}
s.clock = Box::new(|| "2026-10-01T09:00:00".to_string());
for ascending in [true, false] {
s.execute("library.sort", &serde_json::json!({"key": "captureDate", "ascending": ascending})).unwrap();
s.execute("library.source", &serde_json::json!({"kind": "recentlyAdded"})).unwrap();
let names: Vec<String> = s.visible_cloned().iter().map(|id| s.catalog.photo(*id).unwrap().file_name.clone()).collect();
assert_eq!(names, by_name, "ascending: {ascending}");
}
}
/// Copy imports: a destination folder, flat / by-month folders, renamed copies (numbered in
/// import order), and a metadata preset on every photo.
#[test]
fn copy_with_destination_organize_rename_and_metadata_preset() {
let src = temp_dir("orgsrc");
let dest = temp_dir("orgdest");
write_png(&src.join("a.png"), 1);
write_png(&src.join("b.png"), 2);
let mut s = Session::new().with_fs();
s.execute("metadata.savePreset", &json!({"name": "Studio", "fields": {"copyright": "© Studio", "creator": "Sam"}})).unwrap();
assert!(s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "metadataPreset": "Nope"})).is_err());
assert!(s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "mode": "copy", "organize": "weekly"})).is_err());
let r = s
.execute(
"library.import",
&json!({"paths": [src.to_string_lossy()], "mode": "copy", "destination": dest.to_string_lossy(), "organize": "flat",
"rename": "Shoot-{seq:3}", "renameStart": 7, "metadataPreset": "Studio"}),
)
.unwrap();
assert_eq!(ids(&r, "imported"), 2, "{r}");
let mut names: Vec<String> = s.catalog.photos().map(|p| p.file_name.clone()).collect();
names.sort();
assert_eq!(names, ["Shoot-007.png", "Shoot-008.png"], "catalogued under the new names");
assert!(dest.join("Shoot-007.png").exists() && dest.join("Shoot-008.png").exists(), "flat: straight into the destination");
assert!(s.catalog.photos().all(|p| p.meta.copyright == "© Studio" && p.meta.creator == "Sam"));
// by month, no library needed when a destination is given
let dest2 = temp_dir("orgdest2");
write_png(&src.join("c.png"), 3);
let r = s
.execute(
"library.import",
&json!({"paths": [src.join("c.png").to_string_lossy()], "mode": "copy", "destination": dest2.to_string_lossy(), "organize": "month"}),
)
.unwrap();
assert_eq!(ids(&r, "imported"), 1, "{r}");
let p = s.catalog.photos().find(|p| p.file_name == "c.png").unwrap();
let lightcraft_catalog::Source::File { path } = &p.source else { panic!() };
let rel = Path::new(path).strip_prefix(&dest2).unwrap();
assert_eq!(rel.components().count(), 3, "YYYY/YYYY-MM/c.png: {rel:?}");
assert_eq!(rel.parent().unwrap().file_name().unwrap().len(), 7);
for d in [&src, &dest, &dest2] {
let _ = std::fs::remove_dir_all(d);
}
}
#[test]
fn import_rename_sequence_uses_the_last_value_once() {
let base = temp_dir("rename-sequence-last");
let (src, dest, lib) = (base.join("source"), base.join("destination"), base.join("library"));
write_png(&src.join("one.png"), 1);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let last = usize::MAX;
let r = s
.execute(
"library.import",
&json!({
"paths": [src.join("one.png").to_string_lossy()],
"mode": "copy", "destination": dest.to_string_lossy(), "organize": "flat",
"rename": "Shot-{seq}", "renameStart": last as u64
}),
)
.unwrap();
assert_eq!(ids(&r, "imported"), 1, "{r}");
assert_eq!(r["failed"].as_array().map(Vec::len), Some(0), "{r}");
assert!(dest.join(format!("Shot-{last}.png")).is_file());
assert_eq!(s.catalog.len(), 1);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.catalog.len(), 0, "the import remains one undo step");
assert!(dest.join(format!("Shot-{last}.png")).is_file(), "undo leaves a successful copy on disk");
let _ = std::fs::remove_dir_all(base);
}
#[test]
fn import_rename_sequence_reports_exhaustion_and_keeps_prior_successes() {
let base = temp_dir("rename-sequence-exhausted");
let (src, dest, lib) = (base.join("source"), base.join("destination"), base.join("library"));
let paths: Vec<_> = (1..=3)
.map(|i| {
let path = src.join(format!("{i:02}.png"));
write_png(&path, i as u8);
path
})
.collect();
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let start = usize::MAX - 1;
let r = s
.execute(
"library.import",
&json!({
"paths": paths.iter().map(|p| p.to_string_lossy()).collect::<Vec<_>>(),
"mode": "copy", "destination": dest.to_string_lossy(), "organize": "flat",
"rename": "Shot-{seq}", "renameStart": start as u64
}),
)
.unwrap();
assert_eq!(ids(&r, "imported"), 2, "the final two representable values are used: {r}");
assert_eq!(r["failed"].as_array().map(Vec::len), Some(1), "{r}");
let failure = r["failed"][0][1].as_str().unwrap_or_default();
assert!(failure.contains("sequence") && failure.contains("renameStart"), "actionable failure: {failure}");
assert_eq!(s.catalog.len(), 2);
assert!(dest.join(format!("Shot-{start}.png")).is_file());
assert!(dest.join(format!("Shot-{}.png", usize::MAX)).is_file());
assert_eq!(std::fs::read_dir(&dest).unwrap().count(), 2, "the exhausted file gets no destination entry");
assert!(!paths[2].file_name().is_some_and(|name| dest.join(name).exists()), "the exhausted source gets no copy");
let _ = std::fs::remove_dir_all(base);
}
#[test]
fn import_rename_sequence_keeps_flushed_copy_and_move_results() {
for mode in ["copy", "move"] {
let base = temp_dir(&format!("rename-sequence-flush-{mode}"));
let (src, dest, lib) = (base.join("source"), base.join("destination"), base.join("library"));
let paths: Vec<_> = (1..=4)
.map(|i| {
let path = src.join(format!("{i:02}.png"));
write_png(&path, i as u8);
path
})
.collect();
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let start = usize::MAX - 2;
let r = s
.execute(
"library.import",
&json!({
"paths": paths.iter().map(|p| p.to_string_lossy()).collect::<Vec<_>>(),
"mode": mode, "destination": dest.to_string_lossy(), "organize": "flat",
"rename": "Holiday", "renameStart": start as u64
}),
)
.unwrap();
assert_eq!(ids(&r, "imported"), 3, "{mode}: earlier transfers survive sequence exhaustion: {r}");
assert_eq!(r["failed"].as_array().map(Vec::len), Some(1), "{mode}: {r}");
assert_eq!(s.catalog.len(), 3, "{mode}: all successful transfers are catalogued");
let mut output_names: Vec<_> = std::fs::read_dir(&dest)
.unwrap()
.filter_map(std::result::Result::ok)
.map(|entry| entry.file_name().to_string_lossy().into_owned())
.collect();
output_names.sort();
assert_eq!(output_names, ["Holiday-1.png", "Holiday-2.png", "Holiday.png"], "{mode}");
match mode {
"copy" => assert!(paths.iter().all(|path| path.is_file()), "copy leaves every source in place"),
"move" => {
assert!(paths.iter().take(3).all(|path| !path.exists()), "successful moves remove sources after cataloguing");
assert!(paths[3].is_file(), "the failed source remains in place");
assert_eq!(r["moved"].as_array().map(Vec::len), Some(3), "{r}");
}
other => panic!("no such mode: {other}"),
}
let _ = std::fs::remove_dir_all(base);
}
}
#[test]
fn import_rename_sequence_exhaustion_persists_across_batches() {
let base = temp_dir("rename-sequence-batches");
let (src, dest, lib) = (base.join("source"), base.join("destination"), base.join("library"));
let first = src.join("one.png");
let second = src.join("two.png");
write_png(&first, 1);
write_png(&second, 2);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let opts = crate::import::ImportOptions {
mode: crate::import::ImportMode::Copy,
destination: Some(dest.to_string_lossy().into_owned()),
organize: crate::import::Organize::Flat,
rename: Some("Shot-{seq}".into()),
rename_start: usize::MAX,
..Default::default()
};
let mut job = crate::import::ImportJob::new(&mut s, opts).unwrap();
let cancel = std::sync::atomic::AtomicBool::new(false);
let prepared = job.prepare(std::slice::from_ref(&first.to_string_lossy().to_string()), &cancel);
let first_result = crate::import::commit_prepared(&mut s, &job.opts, job.now(), prepared).unwrap();
assert_eq!(ids(&serde_json::to_value(first_result).unwrap(), "imported"), 1);
let prepared = job.prepare(std::slice::from_ref(&second.to_string_lossy().to_string()), &cancel);
let second_result = crate::import::commit_prepared(&mut s, &job.opts, job.now(), prepared).unwrap();
let second_result = serde_json::to_value(second_result).unwrap();
assert_eq!(ids(&second_result, "imported"), 0, "the exhausted job does not restart its sequence");
assert_eq!(second_result["failed"].as_array().map(Vec::len), Some(1), "{second_result}");
assert_eq!(s.catalog.len(), 1);
assert!(dest.join(format!("Shot-{}.png", usize::MAX)).is_file());
assert!(!dest.join("Shot-1.png").exists(), "no wraparound or reuse");
assert_eq!(
(job.done.load(std::sync::atomic::Ordering::Relaxed), job.total.load(std::sync::atomic::Ordering::Relaxed)),
(2, 2),
"both the copied and exhausted files advance import progress"
);
let _ = std::fs::remove_dir_all(base);
}
#[test]
fn import_rename_sequence_controls_ignore_the_counter_when_not_renaming() {
let base = temp_dir("rename-sequence-controls");
let (src, dest, lib) = (base.join("source"), base.join("destination"), base.join("library"));
let added = src.join("add.png");
let copied = src.join("copy.png");
write_png(&added, 1);
write_png(&copied, 2);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let added_report = s
.execute(
"library.import",
&json!({
"paths": [added.to_string_lossy()], "mode": "add", "rename": "Ignored-{seq}", "renameStart": usize::MAX as u64
}),
)
.unwrap();
let copied_report = s
.execute(
"library.import",
&json!({
"paths": [copied.to_string_lossy()], "mode": "copy", "destination": dest.to_string_lossy(), "organize": "flat",
"renameStart": usize::MAX as u64
}),
)
.unwrap();
assert_eq!(ids(&added_report, "imported"), 1, "Add does not use rename numbering: {added_report}");
assert_eq!(ids(&copied_report, "imported"), 1, "an empty rename template does not use the counter: {copied_report}");
assert!(dest.join("copy.png").is_file());
assert_eq!(s.catalog.len(), 2);
let _ = std::fs::remove_dir_all(base);
}
#[cfg(target_pointer_width = "32")]
#[test]
fn import_rename_sequence_rejects_a_start_outside_usize() {
let s = Session::new();
let too_large = u64::from(u32::MAX) + 1;
let parsed = crate::cmd::library::import_params(&s, &json!({"paths": ["unused.png"], "renameStart": too_large}));
assert!(parsed.is_err(), "32-bit import parameters must not truncate renameStart");
}
/// Copy with a custom folder template: `{date:%Y}/{date:%Y%m%d}` files a photo under
/// `2026/20260114/` (an undated file by the import time); templates that would leave the
/// destination are refused.
#[test]
fn copy_with_a_folder_template() {
let src = temp_dir("tplsrc");
let dest = temp_dir("tpldest");
write_png(&src.join("a.png"), 1);
let mut s = Session::new().with_fs();
s.clock = Box::new(|| "2026-01-14T05:58:48".to_string());
for bad in ["../{date}", "/tmp/{date}", "{date:%Y}/../x", "C:/x"] {
let r = s.execute(
"library.import",
&json!({"paths": [src.to_string_lossy()], "mode": "copy", "destination": dest.to_string_lossy(), "organize": bad}),
);
assert!(r.is_err(), "{bad} should be refused");
}
assert_eq!(s.catalog.len(), 0);
let r = s
.execute(
"library.import",
&json!({"paths": [src.to_string_lossy()], "mode": "copy", "destination": dest.to_string_lossy(), "organize": "{date:%Y}/{date:%Y%m%d}"}),
)
.unwrap();
assert_eq!(ids(&r, "imported"), 1, "{r}");
assert!(dest.join("2026").join("20260114").join("a.png").is_file());
for d in [&src, &dest] {
let _ = std::fs::remove_dir_all(d);
}
}
/// Auto import: files in the watched folder are added once their size held between two scans,
/// into the named album; non-photos are tried once; the selection stays put.
#[test]
fn auto_import_watched_folder() {
let dir = temp_dir("watch");
let mut s = Session::new().with_fs();
assert!(s.execute("library.autoImport", &json!({"folder": dir.join("nope").to_string_lossy()})).is_err());
s.execute("library.autoImport", &json!({"folder": dir.to_string_lossy(), "album": "Tethered"})).unwrap();
assert_eq!(s.execute("library.autoImportScan", &json!({})).unwrap()["imported"], json!([]));
write_png(&dir.join("one.png"), 1);
std::fs::write(
dir.join("one.xmp"),
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/" crs:CameraProfile="Color" crs:Exposure2012="0.5"/></rdf:RDF></x:xmpmeta>"#,
)
.unwrap();
std::fs::write(dir.join("notes.txt"), "not a photo").unwrap();
// first sight: wait (it may still be copying)
assert_eq!(s.execute("library.autoImportScan", &json!({})).unwrap()["imported"], json!([]));
let r = s.execute("library.autoImportScan", &json!({})).unwrap();
assert_eq!(r["imported"].as_array().unwrap().len(), 1, "{r}");
assert!(r["warnings"].as_array().unwrap().iter().any(|w| w["code"] == "cameraProfile" && w["profile"] == "Color"), "{r}");
let album = s.catalog.albums().find(|a| a.name == "Tethered").expect("album").id;
assert_eq!(s.catalog.album_count(album), 1);
assert!(s.selection.ids.is_empty(), "arrivals don't take the selection");
// nothing new: nothing happens, and the text file isn't retried
for _ in 0..2 {
assert_eq!(s.execute("library.autoImportScan", &json!({})).unwrap()["imported"], json!([]));
}
write_png(&dir.join("two.png"), 2);
s.execute("library.autoImportScan", &json!({})).unwrap();
s.execute("library.autoImportScan", &json!({})).unwrap();
assert_eq!(s.catalog.album_count(album), 2);
s.execute("library.autoImport", &json!({"folder": null})).unwrap();
assert_eq!(s.execute("library.autoImportScan", &json!({})).unwrap()["folder"], json!(null));
let _ = std::fs::remove_dir_all(&dir);
}
/// Smart previews: with the original offline the photo still renders (and edits apply) from
/// its proxy; without the proxy it can't be opened.
#[test]
fn smart_previews_stand_in_for_offline_originals() {
let src = temp_dir("smartsrc");
let lib = temp_dir("smartlib");
write_png(&src.join("a.png"), 7);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()]})).unwrap();
let id = s.active().unwrap();
let r = s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!(r["built"], 1, "{r}");
assert_eq!(s.execute("photo.smartPreview", &json!({})).unwrap(), json!({"smartPreview": true, "originalOnline": true}));
let online = s.render_now(id, 48, 32).unwrap().image;
// the drive goes away
std::fs::rename(&src, src.with_extension("offline")).unwrap();
s.media.forget(id);
assert_eq!(s.execute("photo.smartPreview", &json!({})).unwrap()["originalOnline"], false);
let offline = s.render_now(id, 48, 32).expect("renders from the smart preview").image;
let diff: f64 = online.data.iter().zip(&offline.data).map(|(a, b)| (a[0] as f64 - b[0] as f64).abs()).sum::<f64>() / online.data.len() as f64;
assert!(diff < 6.0, "the proxy looks like the original: {diff}");
s.execute("develop.set", &json!({"control": "light.exposure", "value": 1.0})).unwrap();
s.media.forget(id);
assert_ne!(s.render_now(id, 48, 32).unwrap().image.data, offline.data, "edits apply offline");
// without the proxy it can't be opened
s.execute("library.smartPreviews", &json!({"discard": true})).unwrap();
s.media.forget(id);
assert!(s.render_now(id, 48, 32).is_err());
let _ = std::fs::remove_dir_all(src.with_extension("offline"));
let _ = std::fs::remove_dir_all(&lib);
}
/// Issue #1534: the decoder facts a render interprets its settings against (a camera colour model,
/// the file's local tone map, its semantic mattes) must be stored with the smart preview, so an
/// edit made while the original is offline renders the same picture as the online source instead
/// of the header-facts fallback.
#[test]
fn smart_previews_keep_the_decoder_facts_offline_edits_need() {
use lightcraft_develop::{DevelopSettings, LocalAdjustments, Mask, MaskComponent, MaskOp, MaskShape, WbMode};
use lightcraft_pipeline::SourceInfo;
use lightcraft_pipeline::masks::{MatteKind, Mattes};
use lightcraft_raster::Rgb32f;
// A smooth scene: the proxy's 8-bit sRGB round trip stays within a code step, so a mismatch
// left between the offline and the online render is metadata the proxy failed to keep.
let pixels = Rgb32f::from_fn(96, 64, |x, y| {
let t = ((x * 7 + y * 3) % 96) as f32 / 96.0;
[0.02 + t * 1.2, 0.02 + t * 0.6, 0.02 + t * 0.25]
});
let camera_color = lightcraft_pipeline::CameraColor {
tags: lightcraft_raw::ColorData {
illuminant: [21, 17],
color_matrix: [Some(lightcraft_color::Mat3([[0.9, 0.2, -0.15], [-0.3, 1.25, 0.08], [0.02, -0.12, 0.85]])), None],
as_shot_neutral: Some([0.5, 1.0, 0.7]),
baseline_exposure: 0.3,
..Default::default()
},
developed_for: lightcraft_color::Xy { x: 0.32, y: 0.35 },
};
let mut mattes = Mattes::default();
mattes.push(
MatteKind::Sky,
lightcraft_raster::Image { width: 96, height: 64, data: (0..96 * 64usize).map(|i| if (i % 96) / 24 == 1 { 255 } else { 0 }).collect() },
);
let local_tone = lightcraft_pipeline::local_tone::LocalTone {
map: lightcraft_raw::gaintable::GainTableMap {
points_v: 1,
points_h: 2,
points_n: 1,
spacing_v: 1.0,
spacing_h: 1.0,
origin_v: 0.0,
origin_h: 0.0,
weights: [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0, 0.0, 0.0],
gamma: 1.0,
gains: vec![1.0, 4.0],
},
placement: lightcraft_raw::gaintable::SourcePlacement { rect: [0.0, 0.0, 1.0, 1.0], orientation: lightcraft_geom::Orientation::Normal },
};
// the facts differ per case; the rest is the raw source's usual shape
let info = |camera_color: Option<lightcraft_pipeline::CameraColor>,
local_tone: Option<lightcraft_pipeline::local_tone::LocalTone>,
mattes: Option<Mattes>| SourceInfo {
raw: true,
as_shot_temp: 5200.0,
as_shot_tint: 8.0,
camera_color: camera_color.map(std::sync::Arc::new),
local_tone: local_tone.map(std::sync::Arc::new),
mattes: mattes.map(std::sync::Arc::new),
..Default::default()
};
/// Build the proxy of `pixels`+`info`, apply `edit`, then render the proxy's own pixels and
/// stored facts online, and the same proxy offline (the original renamed away): how many
/// pixels differ. Identical stored pixels and identical edits must render identically; what
/// the proxy failed to keep shows up as a difference.
fn offline_diff(tag: &str, pixels: &Rgb32f, info: &SourceInfo, edit: DevelopSettings) -> usize {
let src = temp_dir(&format!("smartmeta-src-{tag}"));
let lib = temp_dir(&format!("smartmeta-lib-{tag}"));
write_png(&src.join("a.png"), 3);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let (p, i) = (pixels.clone(), info.clone());
s.media.file_loader = Some(std::sync::Arc::new(move |_path, _max_edge, _| Ok((p.clone(), i.clone()))));
s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()]})).unwrap();
let id = s.active().unwrap();
let r = s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!(r["built"], 1, "{r}");
s.set_develop(id, edit, "reproduction").unwrap();
let dir = s.media.smart_dir.clone().unwrap();
let stored = crate::smart::load(&crate::smart::sharded_file_path(&dir, s.catalog.photo(id).unwrap())).unwrap();
let mut online = s.render_job(id, 96, 96, false, true).unwrap();
// the proxy's own (post-JPEG) pixels, with the facts the original source had: the offline
// render must reach the same picture through the facts the proxy stored
online.source = crate::media::SourceRef::Loaded(Box::new(crate::media::DecodedSource::new(stored.image, Some(info.clone()))));
let online = online.run().rendered.unwrap().image;
// the drive goes away
std::fs::rename(&src, src.with_extension("offline")).unwrap();
s.media.rendered.clear();
s.media.clear_sources();
s.media.forget(id);
let offline = s.render_now(id, 96, 96).expect("renders from the smart preview").image;
assert_eq!(online.data.len(), offline.data.len(), "{tag}: same grid");
let differing = online.data.iter().zip(&offline.data).filter(|(a, b)| a != b).count();
let _ = std::fs::remove_dir_all(src.with_extension("offline"));
let _ = std::fs::remove_dir_all(&lib);
differing
}
// a Kelvin white balance is evaluated through the camera's colour model
let mut wb = DevelopSettings::default();
wb.wb.mode = WbMode::Custom;
wb.wb.temp = 3000.0;
wb.wb.tint = 20.0;
let differing = offline_diff("wb", &pixels, &info(Some(camera_color), None, None), wb);
assert_eq!(differing, 0, "the camera colour model is kept offline");
// an AI mask uses the mattes that came with the file
let mut sky = DevelopSettings::default();
sky.masks.push(Mask {
components: vec![MaskComponent { name: None, op: MaskOp::Add, invert: false, shape: MaskShape::Sky }],
adjust: LocalAdjustments { exposure: 2.0, ..Default::default() },
..Default::default()
});
let differing = offline_diff("sky", &pixels, &info(None, None, Some(mattes)), sky);
assert_eq!(differing, 0, "the file's mattes are kept offline");
// Camera local tone mapping reads the file's gain table map
let mut tone = DevelopSettings::default();
tone.profile.camera_local_tone = true;
tone.light.exposure = -3.0;
let differing = offline_diff("tone", &pixels, &info(None, Some(local_tone), None), tone);
assert_eq!(differing, 0, "the local tone map is kept offline");
}
/// Issue #106: a smart preview cut short (crash, full drive) counted as built forever and failed
/// exactly when the original went offline. Writes are atomic now, and a damaged proxy is not
/// reported as present and is rebuilt.
#[test]
fn damaged_smart_previews_are_rebuilt_and_failed_writes_leave_none() {
let src = temp_dir("smartdmg-src");
let lib = temp_dir("smartdmg-lib");
write_png(&src.join("a.png"), 5);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()]})).unwrap();
let id = s.active().unwrap();
let dir = s.media.smart_dir.clone().unwrap();
let file = crate::smart::sharded_file_path(&dir, s.catalog.photo(id).unwrap());
{
let _fault = lightcraft_catalog::safe_file::fail_writes_after(64);
let r = s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["built"].as_u64(), r["failed"].as_array().map(Vec::len)), (Some(0), Some(1)), "{r}");
}
assert!(!file.exists(), "no partial proxy");
assert_eq!(std::fs::read_dir(file.parent().unwrap()).unwrap().count(), 0, "no temp file left in shard");
assert_eq!(s.execute("photo.smartPreview", &json!({})).unwrap()["smartPreview"], false);
let r = s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["built"].as_u64(), r["repaired"].as_u64()), (Some(1), Some(0)), "{r}");
assert!(crate::smart::is_valid(&file));
// cut short, as by a crash before the fix
let full = std::fs::read(&file).unwrap();
std::fs::write(&file, &full[..full.len() / 2]).unwrap();
assert!(!crate::smart::is_valid(&file));
assert_eq!(s.execute("photo.smartPreview", &json!({})).unwrap()["smartPreview"], false, "not counted as there");
let r = s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["built"].as_u64(), r["repaired"].as_u64()), (Some(1), Some(1)), "{r}");
assert_eq!(std::fs::read(&file).unwrap(), full, "rebuilt whole");
// the app's background build (issue #104) checks and repairs the same way, on a worker thread
std::fs::write(&file, &full[..full.len() / 2]).unwrap();
let r = s.execute("library.smartPreviews", &json!({"background": true})).unwrap();
assert_eq!(r["what"], "smart previews", "{r}");
let t0 = std::time::Instant::now();
let r = loop {
let r = s.execute("library.previewProgress", &json!({})).unwrap();
if r["running"] == false || t0.elapsed() > std::time::Duration::from_secs(60) {
break r;
}
std::thread::sleep(std::time::Duration::from_millis(5));
};
assert_eq!((r["done"].as_u64(), r["failed"].as_u64(), r["repaired"].as_u64()), (Some(1), Some(0), Some(1)), "{r}");
assert_eq!(std::fs::read(&file).unwrap(), full, "rebuilt whole in the background");
// its row in the activity stack (issue #345) goes with it
let t0 = std::time::Instant::now();
while !s.activity.list().is_empty() && t0.elapsed() < std::time::Duration::from_secs(10) {
std::thread::sleep(std::time::Duration::from_millis(5));
}
assert!(s.activity.list().is_empty(), "{:?}", s.activity.list());
let _ = std::fs::remove_dir_all(&src);
let _ = std::fs::remove_dir_all(&lib);
}
/// LUT profiles: a .cube imports as a creative profile (into the library), renders differently
/// (Amount 0 = no change), is listed in the profile browser and comes back with the library.
#[test]
fn cube_luts_become_profiles() {
let src = temp_dir("cubesrc");
let lib = temp_dir("cubelib");
// a warm look: red up, blue down
let n = 9;
let mut cube = String::from("TITLE \"Warm Test\"\nLUT_3D_SIZE 9\n");
for b in 0..n {
for g in 0..n {
for r in 0..n {
let f = |v: usize| v as f32 / (n - 1) as f32;
cube.push_str(&format!("{} {} {}\n", (f(r) * 1.15).min(1.0), f(g), f(b) * 0.8));
}
}
}
std::fs::create_dir_all(src.join("Film Looks")).unwrap();
std::fs::write(src.join("Film Looks/warm.cube"), &cube).unwrap();
std::fs::write(src.join("Film Looks/broken.cube"), "LUT_3D_SIZE 4\n0 0 0\n").unwrap();
let mut s = Session::new().with_fs();
s.open_library(&lib, true).unwrap();
let r = s.execute("profile.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!((r["imported"].as_array().unwrap().len(), r["failed"].as_array().unwrap().len()), (1, 1), "{r}");
let id = r["imported"][0]["id"].as_str().unwrap().to_string();
assert_eq!((r["imported"][0]["name"].as_str(), r["imported"][0]["group"].as_str()), (Some("Warm Test"), Some("Film Looks")));
let menu = s.profile_menu();
assert!(menu["groups"].as_array().unwrap().iter().any(|g| g["name"] == "Film Looks"), "{menu}");
let photo = s.catalog.photos().next().unwrap().id;
s.execute("library.select", &json!({"ids": [photo.0]})).unwrap();
let base = s.render_now(photo, 48, 32).unwrap().image;
s.execute("develop.profile", &json!({"id": id, "amount": 100})).unwrap();
let warm = s.render_now(photo, 48, 32).unwrap().image;
let mean = |img: &lightcraft_raster::Rgba8, k: usize| img.data.iter().map(|p| p[k] as f64).sum::<f64>() / img.data.len() as f64;
assert!(
mean(&warm, 0) > mean(&base, 0) && mean(&warm, 2) < mean(&base, 2),
"warmer: R {} → {}, B {} → {}",
mean(&base, 0),
mean(&warm, 0),
mean(&base, 2),
mean(&warm, 2)
);
s.execute("develop.profile", &json!({"id": id, "amount": 0})).unwrap();
assert_eq!(s.render_now(photo, 48, 32).unwrap().image.data, base.data, "Amount 0 = the photo as it was");
assert_eq!(s.profile_info(&id), Some(("Warm Test", "Film Looks")));
s.execute("profile.favorite", &json!({"id": id})).unwrap();
assert!(s.profile_favorites.contains(&id));
// `profiles.list` lists it next to the built-in profiles (#618)
let listed = |s: &mut Session| {
let list = s.execute("profiles.list", &json!({})).unwrap();
let list = list.as_array().unwrap();
assert!(list.iter().any(|p| p["id"] == "lc.color"), "built-in profiles stay listed");
list.iter().find(|p| p["id"] == id.as_str()).cloned()
};
assert_eq!(listed(&mut s), Some(json!({"id": id, "name": "Warm Test", "group": "Film Looks", "favorite": true, "imported": true})));
// the library keeps it
drop(s);
lightcraft_pipeline::lut::unregister(&id);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
assert!(s.lut_profiles.iter().any(|p| p.id == id));
assert!(s.profile_favorites.contains(&id), "imported LUT favorite survives reopen");
assert_eq!(s.profile_info(&id), Some(("Warm Test", "Film Looks")));
assert!(lightcraft_pipeline::lut::get(&id).is_some(), "registered again on open");
assert!(listed(&mut s).is_some_and(|p| p["favorite"] == true), "listed after reopen");
s.execute("profile.deleteImported", &json!({"id": id})).unwrap();
assert!(lightcraft_pipeline::lut::get(&id).is_none());
assert_eq!(listed(&mut s), None, "a deleted profile is no longer listed");
let _ = std::fs::remove_dir_all(&src);
let _ = std::fs::remove_dir_all(&lib);
}
/// Without a library on disk an imported LUT lives only in this session: no copy of it is
/// left in the temp folder (#1952).
#[test]
fn cube_import_without_a_library_leaves_no_temp_copy() {
let src = temp_dir("cubemem");
let mut cube = String::from("TITLE \"Temp Copy Check\"\nLUT_3D_SIZE 2\n");
for b in 0..2 {
for g in 0..2 {
for r in 0..2 {
cube.push_str(&format!("{r} {g} {b}\n"));
}
}
}
std::fs::write(src.join("mem.cube"), &cube).unwrap();
let leak = std::env::temp_dir().join("lightcraft-imported-temp-copy-check.cube");
let _ = std::fs::remove_file(&leak);
let mut s = Session::new().with_fs();
let r = s.execute("profile.import", &json!({"paths": [src.join("mem.cube").to_string_lossy()]})).unwrap();
let id = r["imported"][0]["id"].as_str().unwrap().to_string();
assert!(lightcraft_pipeline::lut::get(&id).is_some(), "usable in this session");
let file = s.lut_profiles.iter().find(|p| p.id == id).unwrap().file.clone();
assert!(file.is_empty(), "no file kept without a library: {file}");
drop(s);
assert!(!leak.exists(), "{} left behind", leak.display());
lightcraft_pipeline::lut::unregister(&id);
let _ = std::fs::remove_dir_all(&src);
}
/// Folder rename / move on disk: files and sidecars go along, the photos are relinked and
/// still render; clashes and moving into itself are refused.
#[test]
fn folders_rename_and_move_with_their_photos() {
let root = temp_dir("folders");
let a = root.join("Trip");
write_png(&a.join("one.png"), 3);
std::fs::write(a.join("one.xmp"), "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"/>").unwrap();
std::fs::create_dir_all(root.join("Taken")).unwrap();
let mut s = Session::new().with_fs();
s.execute("library.import", &json!({"paths": [a.to_string_lossy()]})).unwrap();
let id = s.active().unwrap();
assert!(s.execute("folder.rename", &json!({"path": a.to_string_lossy(), "name": "Taken"})).is_err(), "name taken");
assert!(s.execute("folder.rename", &json!({"path": a.to_string_lossy(), "name": "a/b"})).is_err());
let r = s.execute("folder.rename", &json!({"path": a.to_string_lossy(), "name": "Italy 2026"})).unwrap();
assert_eq!(r["relinked"], 1);
let b = root.join("Italy 2026");
assert!(b.join("one.png").exists() && b.join("one.xmp").exists() && !a.exists());
let path = |s: &Session| match &s.catalog.photo(id).unwrap().source {
lightcraft_catalog::Source::File { path } => path.clone(),
_ => panic!(),
};
assert_eq!(path(&s), b.join("one.png").to_string_lossy());
s.media.forget(id);
assert!(s.render_now(id, 16, 16).is_ok());
assert!(s.execute("folder.move", &json!({"path": b.to_string_lossy(), "into": b.join("deeper").to_string_lossy()})).is_err(), "not into itself");
let r = s.execute("folder.move", &json!({"path": b.to_string_lossy(), "into": root.join("Archive").to_string_lossy()})).unwrap();
assert_eq!(r["relinked"], 1);
assert_eq!(path(&s), root.join("Archive").join("Italy 2026").join("one.png").to_string_lossy());
let _ = std::fs::remove_dir_all(&root);
}
/// Rename / Move Folder are undo steps (issue #97): undo renames the folder back on disk (files and
/// sidecars along) and restores the photos' paths, redo repeats it; a destination taken in the
/// meantime is never overwritten — the step is refused, reported and stays on the stack.
#[test]
fn folder_rename_and_move_undo_and_redo_on_disk() {
let root = temp_dir("folders-undo");
let a = root.join("Trip");
write_png(&a.join("one.png"), 3);
std::fs::write(a.join("one.xmp"), "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"/>").unwrap();
let mut s = Session::new().with_fs();
s.execute("library.import", &json!({"paths": [a.to_string_lossy()]})).unwrap();
let id = s.active().unwrap();
let path = |s: &Session| match &s.catalog.photo(id).unwrap().source {
lightcraft_catalog::Source::File { path } => path.clone(),
_ => panic!(),
};
let undo0 = s.undo.len();
let b = root.join("Italy");
s.execute("folder.rename", &json!({"path": a.to_string_lossy(), "name": "Italy"})).unwrap();
assert_eq!(s.undo.len(), undo0 + 1, "one undo step");
assert!(s.undo.last().unwrap().label.contains("Rename Folder"));
assert!(b.join("one.png").exists() && !a.exists());
s.execute("edit.undo", &json!({})).unwrap();
assert!(a.join("one.png").exists() && a.join("one.xmp").exists() && !b.exists(), "renamed back on disk");
assert_eq!(path(&s), a.join("one.png").to_string_lossy());
s.media.forget(id);
assert!(s.render_now(id, 16, 16).is_ok());
s.execute("edit.redo", &json!({})).unwrap();
assert!(b.join("one.png").exists() && b.join("one.xmp").exists() && !a.exists());
assert_eq!(path(&s), b.join("one.png").to_string_lossy());
// the old name is taken now: undo is refused, nothing is overwritten, the step stays
std::fs::create_dir_all(&a).unwrap();
std::fs::write(a.join("keep.txt"), "mine").unwrap();
let undo_n = s.undo.len();
let e = s.execute("edit.undo", &json!({})).unwrap_err().to_string();
assert!(e.contains("already exists"), "{e}");
assert_eq!(s.undo.len(), undo_n, "the step stays on the stack");
assert_eq!(std::fs::read_to_string(a.join("keep.txt")).unwrap(), "mine");
assert!(b.join("one.png").exists());
assert_eq!(path(&s), b.join("one.png").to_string_lossy());
std::fs::remove_dir_all(&a).unwrap();
// move into another folder, undo, redo
let into = root.join("Archive");
s.execute("folder.move", &json!({"path": b.to_string_lossy(), "into": into.to_string_lossy()})).unwrap();
let moved = into.join("Italy");
assert_eq!(path(&s), moved.join("one.png").to_string_lossy());
s.execute("edit.undo", &json!({})).unwrap();
assert!(b.join("one.png").exists() && !moved.exists());
assert_eq!(path(&s), b.join("one.png").to_string_lossy());
// the redo target is taken (even by an empty folder): refused, nothing replaced
std::fs::create_dir_all(&moved).unwrap();
assert!(s.execute("edit.redo", &json!({})).is_err());
assert!(b.join("one.png").exists());
std::fs::remove_dir(&moved).unwrap();
s.execute("edit.redo", &json!({})).unwrap();
assert!(moved.join("one.png").exists() && moved.join("one.xmp").exists() && !b.exists());
assert_eq!(path(&s), moved.join("one.png").to_string_lossy());
let _ = std::fs::remove_dir_all(&root);
}
/// Assisted culling: a burst (same scene, seconds apart) of a sharp and two soft shots, plus an
/// unrelated photo: the burst is grouped, the sharp one is its best, blurry shots can be rejected.
#[test]
fn assisted_culling_groups_bursts_and_scores_focus() {
let dir = temp_dir("cull");
let scene = lightcraft_scenes::demo_library()[0].render(480, 320);
let soften = |img: &lightcraft_raster::Rgb32f, r: usize| {
let (w, h) = (img.width, img.height);
let mut out = img.clone();
for y in 0..h {
for x in 0..w {
let (mut acc, mut n) = ([0f32; 3], 0.0);
for yy in y.saturating_sub(r)..(y + r + 1).min(h) {
for xx in x.saturating_sub(r)..(x + r + 1).min(w) {
let c = img.data[yy * w + xx];
acc = [acc[0] + c[0], acc[1] + c[1], acc[2] + c[2]];
n += 1.0;
}
}
out.data[y * w + x] = acc.map(|v| v / n);
}
}
out
};
let save = |img: &lightcraft_raster::Rgb32f, name: &str| {
let data: Vec<[u8; 4]> = img
.data
.iter()
.map(|c| {
let e = |v: f32| (lightcraft_color::transfer::linear_to_srgb(v.clamp(0.0, 1.0)) * 255.0).round() as u8;
[e(c[0]), e(c[1]), e(c[2]), 255]
})
.collect();
let png = lightcraft_codecs::encode_png(
&lightcraft_codecs::EncodeImage::rgba8(&lightcraft_raster::Rgba8 { width: img.width, height: img.height, data }),
&Default::default(),
)
.unwrap();
std::fs::write(dir.join(name), png).unwrap();
};
save(&soften(&scene, 3), "a_soft.png");
save(&scene, "b_sharp.png");
save(&soften(&scene, 6), "c_softer.png");
save(&lightcraft_scenes::demo_library()[7].render(480, 320), "d_other.png");
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [dir.to_string_lossy()]})).unwrap();
let ids: Vec<u64> = r["imported"].as_array().unwrap().iter().filter_map(Value::as_u64).collect();
let by_name = |s: &Session, n: &str| s.catalog.photos().find(|p| p.file_name == n).unwrap().id;
for (i, n) in ["a_soft.png", "b_sharp.png", "c_softer.png"].iter().enumerate() {
let id = by_name(&s, n);
s.commit("t", lightcraft_catalog::Op::SetCaptured { id, captured: Some(format!("2026-05-01T10:00:0{i}")) }).unwrap();
}
let other = by_name(&s, "d_other.png");
s.commit("t", lightcraft_catalog::Op::SetCaptured { id: other, captured: Some("2026-05-01T10:00:04".into()) }).unwrap();
let r = s.execute("photo.analyze", &json!({"ids": ids, "rejectBelow": 0.0, "pickBest": true})).unwrap();
assert_eq!(r["groups"], 1, "{r}");
let a = |s: &Session, n: &str| s.catalog.photo(by_name(s, n)).unwrap().analysis.unwrap();
let (soft, sharp, softer, odd) = (a(&s, "a_soft.png"), a(&s, "b_sharp.png"), a(&s, "c_softer.png"), a(&s, "d_other.png"));
eprintln!("sharpness: sharp {} soft {} softer {} other {}", sharp.sharpness, soft.sharpness, softer.sharpness, odd.sharpness);
assert!(sharp.sharpness > soft.sharpness && soft.sharpness > softer.sharpness);
assert!(sharp.best && !soft.best && sharp.group.is_some() && sharp.group == softer.group);
assert!(odd.group.is_none(), "an unrelated photo isn't in the burst");
assert_eq!(s.catalog.photo(by_name(&s, "b_sharp.png")).unwrap().flag, lightcraft_catalog::Flag::Pick);
// reject the blurry ones (by score), as a rule too
let cut = (soft.sharpness + sharp.sharpness) / 2.0;
s.execute("photo.analyze", &json!({"ids": ids, "rejectBelow": cut})).unwrap();
assert_eq!(s.catalog.photo(by_name(&s, "c_softer.png")).unwrap().flag, lightcraft_catalog::Flag::Reject);
s.execute("library.filter", &json!({"ruleSet": {"rules": [{"field": "bestOfGroup", "op": "is", "value": true}]}})).unwrap();
let vis = s.visible_cloned();
assert!(vis.contains(&by_name(&s, "b_sharp.png")) && !vis.contains(&by_name(&s, "a_soft.png")) && vis.contains(&other));
let _ = std::fs::remove_dir_all(&dir);
}
/// Assisted culling: a photo listed twice is analysed once and is not a burst with itself.
#[test]
fn assisted_culling_ignores_repeated_ids() {
let mut s = Session::with_demo();
let id = s.visible_cloned()[0];
let r = s.execute("photo.analyze", &json!({"ids": [id.0, id.0]})).unwrap();
assert_eq!(r["groups"], 0, "a photo listed twice is not a burst of two: {r}");
assert_eq!(r["photos"].as_array().map(Vec::len), Some(1), "{r}");
let an = s.catalog.photo(id).unwrap().analysis.unwrap();
assert!(an.group.is_none() && !an.best, "{an:?}");
}
/// Assisted culling: two look-alike shots one second apart across midnight form a burst at the end of
/// every month, including those with fewer than 31 days.
#[test]
fn assisted_culling_groups_bursts_across_month_end() {
let dir = temp_dir("cull-month-end");
for (name, (w, h)) in [("a.png", (480, 320)), ("b.png", (482, 321))] {
let img = lightcraft_scenes::demo_library()[0].render(w, h);
let data: Vec<[u8; 4]> = img
.data
.iter()
.map(|c| {
let e = |v: f32| (lightcraft_color::transfer::linear_to_srgb(v.clamp(0.0, 1.0)) * 255.0).round() as u8;
[e(c[0]), e(c[1]), e(c[2]), 255]
})
.collect();
let png = lightcraft_codecs::encode_png(
&lightcraft_codecs::EncodeImage::rgba8(&lightcraft_raster::Rgba8 { width: img.width, height: img.height, data }),
&Default::default(),
)
.unwrap();
std::fs::write(dir.join(name), png).unwrap();
}
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [dir.to_string_lossy()]})).unwrap();
let ids: Vec<u64> = r["imported"].as_array().unwrap().iter().filter_map(Value::as_u64).collect();
let by_name = |s: &Session, n: &str| s.catalog.photos().find(|p| p.file_name == n).unwrap().id;
let (a, b) = (by_name(&s, "a.png"), by_name(&s, "b.png"));
for (before, after) in [
("2026-01-31T23:59:59", "2026-02-01T00:00:00"),
("2026-02-28T23:59:59", "2026-03-01T00:00:00"),
("2026-04-30T23:59:59", "2026-05-01T00:00:00"),
("2026-06-30T23:59:59", "2026-07-01T00:00:00"),
("2026-09-30T23:59:59", "2026-10-01T00:00:00"),
("2026-11-30T23:59:59", "2026-12-01T00:00:00"),
("2026-12-31T23:59:59", "2027-01-01T00:00:00"),
] {
s.commit("t", lightcraft_catalog::Op::SetCaptured { id: a, captured: Some(before.into()) }).unwrap();
s.commit("t", lightcraft_catalog::Op::SetCaptured { id: b, captured: Some(after.into()) }).unwrap();
let r = s.execute("photo.analyze", &json!({"ids": ids})).unwrap();
assert_eq!(r["groups"], 1, "shots 1 s apart ({before} / {after}) must form a burst: {r}");
}
let _ = std::fs::remove_dir_all(&dir);
}
/// Assisted culling: `burstGap` and `similarity` move the burst limits (#2275), and values out of
/// range are refused before anything is measured or stored.
#[test]
fn assisted_culling_burst_limits_are_adjustable() {
let dir = temp_dir("cull-limits");
for (name, (w, h)) in [("a.png", (480, 320)), ("b.png", (482, 321))] {
let img = lightcraft_scenes::demo_library()[0].render(w, h);
let data: Vec<[u8; 4]> = img
.data
.iter()
.map(|c| {
let e = |v: f32| (lightcraft_color::transfer::linear_to_srgb(v.clamp(0.0, 1.0)) * 255.0).round() as u8;
[e(c[0]), e(c[1]), e(c[2]), 255]
})
.collect();
let png = lightcraft_codecs::encode_png(
&lightcraft_codecs::EncodeImage::rgba8(&lightcraft_raster::Rgba8 { width: img.width, height: img.height, data }),
&Default::default(),
)
.unwrap();
std::fs::write(dir.join(name), png).unwrap();
}
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [dir.to_string_lossy()]})).unwrap();
let ids: Vec<u64> = r["imported"].as_array().unwrap().iter().filter_map(Value::as_u64).collect();
let by_name = |s: &Session, n: &str| s.catalog.photos().find(|p| p.file_name == n).unwrap().id;
let (a, b) = (by_name(&s, "a.png"), by_name(&s, "b.png"));
s.commit("t", lightcraft_catalog::Op::SetCaptured { id: a, captured: Some("2026-05-01T10:00:00".into()) }).unwrap();
s.commit("t", lightcraft_catalog::Op::SetCaptured { id: b, captured: Some("2026-05-01T10:00:15".into()) }).unwrap();
// 15 s apart: past the default 10 s, inside a 20 s gap
assert_eq!(s.execute("photo.analyze", &json!({"ids": ids})).unwrap()["groups"], 0);
assert_eq!(s.execute("photo.analyze", &json!({"ids": ids, "burstGap": 20})).unwrap()["groups"], 1);
// the look-alike pair is a burst at the default similarity, not when it must be identical
let strict = s.execute("photo.analyze", &json!({"ids": ids, "burstGap": 20, "similarity": 1.0})).unwrap();
assert_eq!(strict["groups"], 0, "{strict}");
// bad values change nothing
let undo_before = s.commits();
for bad in
[json!({"burstGap": -1}), json!({"burstGap": 86401}), json!({"burstGap": "10"}), json!({"similarity": 1.5}), json!({"similarity": -0.1})]
{
let mut p = bad.clone();
p["ids"] = json!(ids);
assert!(s.execute("photo.analyze", &p).is_err(), "{bad} must be refused");
}
assert_eq!(s.commits(), undo_before);
let _ = std::fs::remove_dir_all(&dir);
}
/// Find Dust Spots: soft dark spots on a smooth sky are found and healed (one undo step).
#[test]
fn dust_spots_are_found_and_healed() {
let dir = temp_dir("dust");
let (w, h) = (900usize, 600usize);
let spots = [(180.0f32, 140.0f32), (600.0, 300.0)];
let data: Vec<[u8; 4]> = (0..w * h)
.map(|i| {
let (x, y) = ((i % w) as f32, (i / w) as f32);
let mut v = 0.5 + 0.3 * (y / h as f32);
for (cx, cy) in spots {
v -= 0.07 * (-((x - cx).powi(2) + (y - cy).powi(2)) / (2.0 * 25.0)).exp();
}
let b = (v * 255.0) as u8;
[b, b, (b as f32 * 1.15).min(255.0) as u8, 255]
})
.collect();
let png = lightcraft_codecs::encode_png(
&lightcraft_codecs::EncodeImage::rgba8(&lightcraft_raster::Rgba8 { width: w, height: h, data }),
&Default::default(),
)
.unwrap();
std::fs::write(dir.join("sky.png"), png).unwrap();
let mut s = Session::new().with_fs();
s.execute("library.import", &json!({"paths": [dir.join("sky.png").to_string_lossy()]})).unwrap();
let id = s.active().unwrap();
let before = s.render_now(id, 900, 600).unwrap().image;
let r = s.execute("spot.findDust", &json!({})).unwrap();
assert_eq!(r["added"], 2, "{r}");
let after = s.render_now(id, 900, 600).unwrap().image;
let at = |img: &lightcraft_raster::Rgba8, x: usize, y: usize| img.data[y * 900 + x][0] as i32;
for (cx, cy) in spots {
assert!(at(&after, cx as usize, cy as usize) > at(&before, cx as usize, cy as usize) + 6, "the spot at {cx},{cy} is lifted");
}
s.execute("edit.undo", &json!({})).unwrap();
assert!(s.develop_of(id).unwrap().spots.is_empty(), "one undo step");
let _ = std::fs::remove_dir_all(&dir);
}
/// Smart previews can live outside the library: the folder is saved per library, used for
/// building, status and offline rendering, changing it needs an explicit choice for the
/// previews already built, and an unavailable folder is an error (never a silent fallback).
#[test]
fn smart_previews_folder_is_chosen_per_library() {
let src = temp_dir("smartloc-src");
let lib = temp_dir("smartloc-lib");
let away = temp_dir("smartloc-away");
write_png(&src.join("a.png"), 7);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()]})).unwrap();
let id = s.active().unwrap();
let loc = s.execute("library.smartPreviewsLocation", &json!({})).unwrap();
assert_eq!(loc["custom"], false);
assert_eq!(loc["path"], lib.join("Smart Previews").to_string_lossy().as_ref());
s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!(crate::smart::stats(&lib.join("Smart Previews")).0, 1);
// A flat proxy from older builds remains discoverable and migrates to the new sharded layout.
let dir = lib.join("Smart Previews");
let photo = s.catalog.photo(id).unwrap();
let shard = crate::smart::sharded_file_path(&dir, photo);
let flat = dir.join(crate::smart::file_name(photo));
std::fs::rename(&shard, &flat).unwrap();
assert_eq!(s.execute("photo.smartPreview", &json!({})).unwrap()["smartPreview"], true);
// relative paths and missing drives are refused, and nothing is created on this drive
assert!(s.execute("library.smartPreviewsLocation", &json!({"path": "rel/dir"})).is_err());
let gone = away.join("unplugged/previews");
let err = s.execute("library.smartPreviewsLocation", &json!({"path": gone.to_string_lossy()})).unwrap_err().to_string();
assert!(err.contains("not available"), "{err}");
assert!(!away.join("unplugged").exists());
// the previews already built need an explicit choice
let to = away.join("proxies");
let err = s.execute("library.smartPreviewsLocation", &json!({"path": to.to_string_lossy()})).unwrap_err().to_string();
assert!(err.contains("existing"), "{err}");
assert_eq!(s.execute("library.smartPreviewsLocation", &json!({})).unwrap()["custom"], false);
let r = s.execute("library.smartPreviewsLocation", &json!({"path": to.to_string_lossy(), "existing": "move"})).unwrap();
assert_eq!((r["custom"].clone(), r["handled"].clone(), r["count"].clone()), (json!(true), json!(1), json!(1)), "{r}");
assert_eq!(crate::smart::stats(&lib.join("Smart Previews")).0, 0);
assert_eq!(s.execute("photo.smartPreview", &json!({})).unwrap()["smartPreview"], true);
// the setting is saved with the library
drop(s); // one session per library (issue #99)
let mut again = Session::new().with_fs();
again.open_library(&lib, false).unwrap();
assert_eq!(again.execute("library.smartPreviewsLocation", &json!({})).unwrap()["path"], to.to_string_lossy().as_ref());
// offline original: the render comes from the proxy in the chosen folder
std::fs::rename(&src, src.with_extension("offline")).unwrap();
again.media.forget(id);
again.render_now(id, 48, 32).expect("renders from the smart preview in the chosen folder");
// the chosen drive goes away: building says so and creates nothing on this drive
let drive = away.join("drive");
std::fs::create_dir_all(&drive).unwrap();
let on_drive = drive.join("proxies");
again.execute("library.smartPreviewsLocation", &json!({"path": on_drive.to_string_lossy(), "existing": "leave"})).unwrap();
std::fs::remove_dir_all(&drive).unwrap();
let loc = again.execute("library.smartPreviewsLocation", &json!({})).unwrap();
assert_eq!(loc["available"], false, "{loc}");
let err = again.execute("library.smartPreviews", &json!({"ids": [id.0]})).unwrap_err().to_string();
assert!(err.contains("not available"), "{err}");
assert!(!drive.exists());
// back to the library's own folder, discarding the proxies left in the first chosen folder
let r = again.execute("library.smartPreviewsLocation", &json!({"reset": true})).unwrap();
assert_eq!(r["custom"], false, "{r}");
assert_eq!(r["path"], lib.join("Smart Previews").to_string_lossy().as_ref());
// (the open library's files are removed below: on Windows an open file cannot be)
drop(again);
for d in [&lib, &away, &src.with_extension("offline")] {
let _ = std::fs::remove_dir_all(d);
}
}
/// Missing Photos covers library photos only: a small library next to many Local browse
/// records (and a deleted photo) checks — and lists, and counts — the library files alone.
#[test]
fn missing_photos_skip_local_browse_records() {
use lightcraft_catalog::{Op, Photo, PhotoId, Source};
let gone = std::env::temp_dir().join(format!("lc-missing-scope-{}", std::process::id()));
let mut s = Session::new();
let mut ops = Vec::new();
for i in 0..205u64 {
let path = gone.join(format!("IMG_{i:04}.jpg")).to_string_lossy().to_string();
let mut p = Photo::new(PhotoId(i + 1), Source::File { path }, &format!("IMG_{i:04}.jpg"), "JPEG", 60, 40, "2026-01-01T00:00:00");
p.local = i >= 5; // 5 library photos, 200 seen while browsing
p.deleted = i == 4;
ops.push(Op::AddPhoto { photo: Box::new(p) });
}
s.commit("Add", Op::Batch { ops }).unwrap();
let mut checked = 0;
let lost = crate::cmd::missing::missing_with(&s.catalog, |_| {
checked += 1;
false
});
assert_eq!((checked, lost.len()), (4, 4), "only the non-deleted library files are checked");
assert_eq!(crate::cmd::missing::candidates(&s.catalog).len(), 4, "the sidebar count checks the same files");
assert_eq!(s.execute("library.missing", &json!({})).unwrap().as_array().map(Vec::len), Some(4));
s.execute("library.source", &json!({"kind": "missing"})).unwrap();
assert_eq!(s.visible_cloned().len(), 4, "the view lists what the count counts");
}
/// `library.missing` asks the session's existence check (the one the availability cache and
/// the tests replace), and a sweep on another thread answers what the command does: the same
/// files in the same order, progress on the way, and a stop that leaves the rest unchecked.
#[test]
fn missing_sweep_answers_what_the_command_does_and_can_stop() {
use lightcraft_catalog::{Op, Photo, PhotoId, Source};
use std::sync::atomic::{AtomicUsize, Ordering};
let mut s = Session::new();
let mut ops = Vec::new();
for i in 0..200u64 {
let path = format!("/share/IMG_{i:04}.jpg");
let p = Photo::new(PhotoId(i + 1), Source::File { path }, &format!("IMG_{i:04}.jpg"), "JPEG", 60, 40, "2026-01-01T00:00:00");
ops.push(Op::AddPhoto { photo: Box::new(p) });
}
s.commit("Add", Op::Batch { ops }).unwrap();
// the first hundred files are gone
s.media.availability.set_probe(std::sync::Arc::new(|f| !f.contains("_00")));
let asked = s.execute("library.missing", &json!({})).unwrap();
assert_eq!(asked.as_array().map(Vec::len), Some(100), "the command used the session's check: {asked}");
let files = crate::cmd::missing::checked(&s.catalog);
assert_eq!(files.len(), 200);
let reports = AtomicUsize::new(0);
let probe = s.media.availability.probe();
let swept = crate::cmd::missing::sweep(
files.clone(),
|f| probe(f),
|| false,
|_| {
reports.fetch_add(1, Ordering::Relaxed);
},
);
assert_eq!(crate::cmd::missing::missing_json(swept.unwrap()), asked);
assert!(reports.load(Ordering::Relaxed) >= 2, "progress is reported on the way");
// stopped after 10 files: nothing is answered, and the rest is not looked at
let looked = AtomicUsize::new(0);
let stopped = crate::cmd::missing::sweep(
files,
|_| {
looked.fetch_add(1, Ordering::Relaxed);
true
},
|| looked.load(Ordering::Relaxed) >= 10,
|_| {},
);
assert_eq!((stopped, looked.load(Ordering::Relaxed)), (None, 10));
}
/// A stop that arrives while the last file is being checked is honoured: no answer.
#[test]
fn missing_sweep_honours_a_stop_during_the_last_check() {
use lightcraft_catalog::PhotoId;
use std::sync::atomic::{AtomicBool, Ordering};
let stop = AtomicBool::new(false);
let files = vec![(PhotoId(1), "/a".to_string()), (PhotoId(2), "/b".to_string())];
let swept = crate::cmd::missing::sweep(
files,
|f| {
if f == "/b" {
stop.store(true, Ordering::Relaxed);
}
false
},
|| stop.load(Ordering::Relaxed),
|_| {},
);
assert_eq!(swept, None);
}
/// `library.browse` follows `library.source`'s rule for the active photo (issue #1447): a library
/// photo the folder doesn't show gives way to the folder's first photo; one it shows stays.
#[test]
fn browsing_a_folder_keeps_or_picks_the_active_photo() {
let dir = temp_dir("browse-active");
write_png(&dir.join("a.png"), 1);
write_png(&dir.join("b.png"), 2);
let mut s = Session::with_demo().with_fs();
let library = s.visible_cloned()[0];
s.execute("library.select", &json!({"ids": [library.0]})).unwrap();
s.execute("library.browse", &json!({"path": dir.to_string_lossy()})).unwrap();
let vis = s.visible_cloned();
assert_eq!(vis.len(), 2);
assert_eq!(s.selection.active, Some(vis[0]), "the folder's first photo, not {library:?}");
s.execute("library.select", &json!({"ids": [vis[1].0]})).unwrap();
s.execute("library.browse", &json!({"path": dir.to_string_lossy()})).unwrap();
assert_eq!(s.selection.active, Some(vis[1]), "a photo the folder shows stays active");
let _ = std::fs::remove_dir_all(&dir);
}
/// Import a file that is in Recently Deleted (issues #298, #706): by default the photo comes
/// back with its edits; `onDeleted: skip` leaves it there but the report says it is in the trash;
/// `onDeleted: fresh` replaces the trashed record with a new one.
fn trashed_photo(tag: &str) -> (Session, std::path::PathBuf, u64) {
let src = temp_dir(tag);
write_png(&src.join("a.png"), 1);
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
let id = r["imported"][0].as_u64().unwrap();
s.execute("photo.rate", &json!({"ids": [id], "rating": 4})).unwrap();
s.execute("photo.delete", &json!({"ids": [id]})).unwrap();
(s, src, id)
}
/// Issue #706: import a photo, delete it, import the same file again. Before, the import skipped
/// it as "already in the library" (a duplicate by path) while the grid showed it deleted, and the
/// user was stuck. Now the photo is restored: it is back in All Photos, with its edits.
#[test]
fn importing_a_deleted_photo_again_brings_it_back() {
let (mut s, src, id) = trashed_photo("trash-reimport");
assert_eq!(s.visible_cloned().len(), 0, "deleted: not in All Photos");
let r = s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()]})).unwrap();
assert_eq!(r["restored"], json!([id]), "{r}");
assert_eq!(ids(&r, "duplicates"), 0, "not reported as a duplicate: {r}");
let p = s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap();
assert!(!p.deleted && p.rating == 4, "back with its edits");
assert_eq!(s.catalog.len(), 1, "no second record");
assert_eq!(s.visible_cloned(), vec![lightcraft_catalog::PhotoId(id)], "shown again in All Photos");
// undo puts it back in Recently Deleted
s.execute("edit.undo", &json!({})).unwrap();
assert!(s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap().deleted);
let _ = std::fs::remove_dir_all(&src);
}
/// The same sequence with the library on disk and a restart between the delete and the import:
/// the trashed record is read back from the catalog and restored, not refused.
#[test]
fn importing_a_deleted_photo_again_after_reopening_the_library_brings_it_back() {
let src = temp_dir("trash-reimport-reopen");
write_png(&src.join("a.png"), 1);
let lib = src.join("library");
let id = {
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let r = s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()], "albumName": "Trip"})).unwrap();
let id = r["imported"][0].as_u64().unwrap();
s.execute("photo.rate", &json!({"ids": [id], "rating": 2})).unwrap();
s.execute("photo.delete", &json!({"ids": [id]})).unwrap();
s.close_library().unwrap();
id
};
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
assert!(s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap().deleted, "deleted state persisted");
let r = s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()]})).unwrap();
assert_eq!(r["restored"], json!([id]), "{r}");
assert_eq!(ids(&r, "duplicates"), 0, "{r}");
let p = s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap();
assert!(!p.deleted && p.rating == 2, "restored with its edits");
let album = s.catalog.albums().find(|a| a.name == "Trip").unwrap();
assert_eq!(album.photos, vec![lightcraft_catalog::PhotoId(id)], "still in its album");
assert_eq!(s.visible_cloned().len(), 1);
let _ = std::fs::remove_dir_all(&src);
}
/// A deleted photo whose file was moved meanwhile: importing the file from its new place restores
/// the photo and points it at the file (matched by content), instead of bringing back a record
/// whose file is missing.
#[test]
fn importing_a_deleted_photo_from_its_new_place_restores_and_relinks_it() {
let (mut s, src, id) = trashed_photo("trash-reimport-moved");
std::fs::create_dir_all(src.join("moved")).unwrap();
std::fs::rename(src.join("a.png"), src.join("moved/b.png")).unwrap();
let r = s.execute("library.import", &json!({"paths": [src.join("moved/b.png").to_string_lossy()]})).unwrap();
assert_eq!(r["restored"], json!([id]), "{r}");
let p = s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap();
assert!(!p.deleted && p.rating == 4);
assert_eq!(p.source, lightcraft_catalog::Source::File { path: src.join("moved/b.png").to_string_lossy().to_string() });
assert_eq!(p.file_name, "b.png");
assert_eq!(s.catalog.len(), 1);
let _ = std::fs::remove_dir_all(&src);
}
/// Only an import brings a deleted photo back. Looking at its folder in Local, and Auto Import
/// seeing its file again in the watched folder (a copy: the source is not a path the library
/// knows), leave it in Recently Deleted.
#[test]
fn browsing_and_auto_import_never_restore_a_deleted_photo() {
let (mut s, src, id) = trashed_photo("trash-no-silent-restore");
let r = s.execute("library.browse", &json!({"path": src.to_string_lossy()})).unwrap();
assert_eq!(r["new"], 0, "{r}");
assert!(s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap().deleted, "browsing only looks");
assert_eq!(s.catalog.len(), 1);
// auto import in copy mode: the trashed photo's copy is in the library's folder, so the watched
// folder's file is new by path and a duplicate by content
let lib = src.join("library");
let watched = src.join("watched");
write_png(&watched.join("w.png"), 7);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let r = s.execute("library.import", &json!({"paths": [watched.join("w.png").to_string_lossy()], "mode": "copy"})).unwrap();
let copied = r["imported"][0].as_u64().unwrap();
s.execute("photo.delete", &json!({"ids": [copied]})).unwrap();
s.execute("library.autoImport", &json!({"folder": watched.to_string_lossy(), "copy": true})).unwrap();
for _ in 0..3 {
let r = s.execute("library.autoImportScan", &json!({})).unwrap();
assert_eq!(r["imported"], json!([]), "{r}");
}
assert!(s.catalog.photo(lightcraft_catalog::PhotoId(copied)).unwrap().deleted, "still deleted");
assert_eq!(s.catalog.len(), 1, "no second copy");
let _ = std::fs::remove_dir_all(&src);
}
/// `onDeleted: skip` keeps the old behaviour for callers that want it: nothing imported, the
/// duplicate reported with `existingDeleted`.
#[test]
fn duplicate_in_recently_deleted_is_reported_as_such() {
let (mut s, src, id) = trashed_photo("trash-report");
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "onDeleted": "skip"})).unwrap();
assert_eq!(ids(&r, "imported"), 0, "{r}");
assert_eq!(r["duplicates"][0]["existing"], id, "{r}");
assert_eq!(r["duplicates"][0]["existingDeleted"], true, "{r}");
assert!(s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap().deleted, "skip: left in the trash");
let _ = std::fs::remove_dir_all(&src);
}
#[test]
fn import_can_restore_a_duplicate_from_recently_deleted() {
let (mut s, src, id) = trashed_photo("trash-restore");
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "onDeleted": "restore"})).unwrap();
assert_eq!(r["restored"], json!([id]), "{r}");
assert_eq!(ids(&r, "duplicates"), 0, "{r}");
let p = s.catalog.photo(lightcraft_catalog::PhotoId(id)).unwrap();
assert!(!p.deleted && p.rating == 4, "restored with its edits");
let _ = std::fs::remove_dir_all(&src);
}
#[test]
fn import_can_replace_a_duplicate_from_recently_deleted() {
let (mut s, src, id) = trashed_photo("trash-fresh");
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "onDeleted": "fresh"})).unwrap();
assert_eq!(ids(&r, "imported"), 1, "{r}");
let new = r["imported"][0].as_u64().unwrap();
assert_ne!(new, id);
assert!(s.catalog.photo(lightcraft_catalog::PhotoId(id)).is_none(), "the trashed record is gone");
let p = s.catalog.photo(lightcraft_catalog::PhotoId(new)).unwrap();
assert!(!p.deleted && p.rating == 0, "a new photo without the old edits");
assert_eq!(s.catalog.len(), 1);
let _ = std::fs::remove_dir_all(&src);
}
/// A trashed photo imported again as a new one owns its stem sidecar: its own old record is not a
/// rival for `a.xmp` (issue #1905's ranking), so the fresh photo reads it.
#[test]
fn a_fresh_reimport_reads_its_own_stem_sidecar() {
let (mut s, src, id) = trashed_photo("trash-fresh-sidecar");
std::fs::write(
src.join("a.xmp"),
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:Rating="3"/></rdf:RDF></x:xmpmeta>"#,
)
.unwrap();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "onDeleted": "fresh"})).unwrap();
assert_eq!(ids(&r, "imported"), 1, "{r}");
let new = r["imported"][0].as_u64().unwrap();
assert_ne!(new, id);
assert_eq!(s.catalog.photo(lightcraft_catalog::PhotoId(new)).unwrap().rating, 3, "the fresh photo reads a.xmp: {r}");
let _ = std::fs::remove_dir_all(&src);
}
#[test]
fn unknown_on_deleted_is_an_error() {
let mut s = Session::new().with_fs();
let e = s.execute("library.import", &json!({"paths": [], "onDeleted": "bogus"})).unwrap_err().to_string();
assert!(e.contains("onDeleted"), "{e}");
}
#[test]
fn empty_recently_deleted_removes_only_trashed_photos() {
let (mut s, src, id) = trashed_photo("trash-empty");
write_png(&src.join("b.png"), 2);
let r = s.execute("library.import", &json!({"paths": [src.join("b.png").to_string_lossy()]})).unwrap();
let keep = r["imported"][0].as_u64().unwrap();
let r = s.execute("library.emptyRecentlyDeleted", &json!({})).unwrap();
assert_eq!(r["deleted"], 1, "{r}");
assert!(s.catalog.photo(lightcraft_catalog::PhotoId(id)).is_none());
assert!(s.catalog.photo(lightcraft_catalog::PhotoId(keep)).is_some());
// nothing to empty is not an error
assert_eq!(s.execute("library.emptyRecentlyDeleted", &json!({})).unwrap()["deleted"], 0);
s.execute("edit.undo", &json!({})).unwrap();
let _ = std::fs::remove_dir_all(&src);
}
/// Two trashed photos that share an album and a two-photo stack: emptying (or permanently
/// deleting) them together must not fail or leave dangling references.
fn two_trashed_in_album_and_stack(tag: &str) -> (Session, std::path::PathBuf, [u64; 2], u64) {
let src = temp_dir(tag);
write_png(&src.join("a.png"), 1);
write_png(&src.join("b.png"), 2);
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
let ids = [r["imported"][0].as_u64().unwrap(), r["imported"][1].as_u64().unwrap()];
let al = s.execute("album.create", &json!({"name": "Trip"})).unwrap()["id"].as_u64().unwrap();
s.execute("album.addPhotos", &json!({"id": al, "ids": ids})).unwrap();
s.execute("library.select", &json!({"ids": ids})).unwrap();
s.execute("stack.group", &json!({"ids": ids})).unwrap();
s.execute("photo.delete", &json!({"ids": ids})).unwrap();
(s, src, ids, al)
}
#[test]
fn emptying_trashed_photos_of_one_album_and_stack_leaves_nothing_dangling() {
let (mut s, src, ids, al) = two_trashed_in_album_and_stack("trash-empty-refs");
let r = s.execute("library.emptyRecentlyDeleted", &json!({})).unwrap();
assert_eq!(r["deleted"], 2, "{r}");
assert_eq!(s.catalog.len(), 0);
assert!(s.catalog.album(lightcraft_catalog::AlbumId(al)).unwrap().photos.is_empty(), "album still lists a removed photo");
assert!(ids.iter().all(|i| s.catalog.stack_of(lightcraft_catalog::PhotoId(*i)).is_none()));
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.catalog.len(), 2, "one undo step brings both back");
assert_eq!(s.catalog.album(lightcraft_catalog::AlbumId(al)).unwrap().photos.len(), 2);
assert!(s.catalog.stack_of(lightcraft_catalog::PhotoId(ids[0])).is_none(), "soft-delete already left the stack");
assert!(s.catalog.photos().all(|p| p.deleted), "back in Recently Deleted");
let _ = std::fs::remove_dir_all(&src);
}
#[test]
fn deleting_several_photos_permanently_leaves_nothing_dangling() {
let (mut s, src, ids, al) = two_trashed_in_album_and_stack("trash-perm-refs");
s.execute("library.select", &json!({"ids": ids})).unwrap();
let files = [src.join("a.png"), src.join("b.png")].map(|path| {
let bytes = std::fs::read(&path).unwrap();
(path, bytes)
});
let snapshot = s.catalog.to_snapshot();
s.drain_log();
s.execute("photo.deletePermanently", &json!({"ids": ids})).unwrap();
assert!(s.catalog.album(lightcraft_catalog::AlbumId(al)).unwrap().photos.is_empty());
assert!(ids.iter().all(|i| s.catalog.photo(lightcraft_catalog::PhotoId(*i)).is_none()));
let removed = s.catalog.to_snapshot();
let log: String = s.drain_log().iter().map(lightcraft_catalog::Catalog::op_to_log_line).collect();
let mut reopened = lightcraft_catalog::Catalog::from_snapshot(&snapshot).unwrap();
reopened.replay(&log).unwrap();
assert_eq!(reopened.to_snapshot(), removed);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.catalog.to_snapshot(), snapshot, "one undo restores photos, album and stack");
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!(s.catalog.to_snapshot(), removed);
for (path, bytes) in files {
assert_eq!(std::fs::read(path).unwrap(), bytes, "permanent deletion only removes catalog records");
}
let _ = std::fs::remove_dir_all(&src);
}
#[test]
fn fresh_import_of_two_trashed_photos_in_one_album_and_stack_works() {
let (mut s, src, ids, al) = two_trashed_in_album_and_stack("trash-fresh-refs");
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "onDeleted": "fresh"})).unwrap();
assert_eq!(ids_len(&r), 2, "{r}");
assert!(s.catalog.album(lightcraft_catalog::AlbumId(al)).unwrap().photos.is_empty());
assert!(ids.iter().all(|i| s.catalog.photo(lightcraft_catalog::PhotoId(*i)).is_none()));
let _ = std::fs::remove_dir_all(&src);
}
fn ids_len(r: &Value) -> usize {
ids(r, "imported")
}
#[test]
fn restore_import_applies_the_album_to_restored_photos() {
let (mut s, src, ids, _) = trashed_photo_ids("trash-restore-album");
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "onDeleted": "restore", "albumName": "Back"})).unwrap();
assert_eq!(r["restored"], json!(ids), "{r}");
let album = r["album"].as_u64().expect("album created for the restored photos");
assert_eq!(s.catalog.album(lightcraft_catalog::AlbumId(album)).unwrap().photos.len(), 1);
let _ = std::fs::remove_dir_all(&src);
}
fn trashed_photo_ids(tag: &str) -> (Session, std::path::PathBuf, [u64; 1], u64) {
let (s, src, id) = trashed_photo(tag);
(s, src, [id], 0)
}
/// Issue #374: launch arguments never turn into a walk of a whole drive or home folder. An empty
/// argument is ignored; `.`, `..` and (on Windows) `/` are resolved against the working folder a
/// launcher picked, which is often a drive root; a root is replaced by its memory card's DCIM
/// folder or refused, and so is the home folder.
#[test]
fn launch_paths_never_become_a_drive_root_or_the_home_folder() {
use crate::import::{LaunchPath, launch_path};
let root = if cfg!(windows) { Path::new("C:\\") } else { Path::new("/") };
let home = if cfg!(windows) { Path::new("C:\\Users\\me") } else { Path::new("/home/me") };
let none = |_: &Path| None;
let refused = |r: Option<LaunchPath>| matches!(r, Some(LaunchPath::Refused { .. }));
for empty in ["", " "] {
assert_eq!(launch_path(empty, Some(root), Some(home), none), None, "{empty:?}");
}
// the working folder is the drive root: `.` (and `..`) name the whole drive
for arg in [".", "..", "./", ".\\..", "/"] {
let arg = if cfg!(windows) { arg.to_string() } else { arg.replace('\\', "/") };
assert!(refused(launch_path(&arg, Some(root), Some(home), none)), "{arg:?}");
}
assert!(refused(launch_path("..", Some(&home.join("Pictures")), Some(home), none)), "up to the home folder");
assert!(refused(launch_path(&home.to_string_lossy(), None, Some(home), none)));
assert!(refused(launch_path(".", None, Some(home), none)), "relative, no working folder");
// a memory card's root: its camera folder
let dcim = |r: &Path| Some(r.join("DCIM"));
assert_eq!(launch_path(".", Some(root), Some(home), dcim), Some(LaunchPath::Import(root.join("DCIM").to_string_lossy().to_string())));
// ordinary folders and files resolve to absolute paths
let pics = home.join("Pictures");
assert_eq!(launch_path("Pictures", Some(home), Some(home), none), Some(LaunchPath::Import(pics.to_string_lossy().to_string())));
assert_eq!(
launch_path(&pics.join("a.jpg").to_string_lossy(), None, Some(home), none),
Some(LaunchPath::Import(pics.join("a.jpg").to_string_lossy().to_string()))
);
}
/// Issue #374: a folder import is bounded — a tree deeper or larger than the limits is not walked
/// to the end (it stops and says so), and what it found is still returned.
#[test]
fn expand_stops_at_the_walk_limits() {
let d = temp_dir("bounded");
let mut deep = d.clone();
for i in 0..8 {
deep = deep.join(format!("d{i}"));
std::fs::create_dir_all(&deep).unwrap();
std::fs::write(deep.join(format!("p{i}.jpg")), b"x").unwrap();
}
let top = [d.to_string_lossy().to_string()];
let (all, cut) = crate::import::expand_within(&top, None, crate::walk::Limits::default());
assert_eq!((all.len(), cut), (8, false));
let (some, cut) = crate::import::expand_within(&top, None, crate::walk::Limits { max_depth: 3, max_entries: 1000 });
assert_eq!((some.len(), cut), (2, true), "{some:?}");
let (few, cut) = crate::import::expand_within(&top, None, crate::walk::Limits { max_depth: 32, max_entries: 5 });
assert!(cut && few.len() < 8, "{few:?}");
let _ = std::fs::remove_dir_all(&d);
}
/// `levels` folders named `d` nested below `root` (created); the innermost one's path.
fn nested(root: &Path, levels: usize) -> std::path::PathBuf {
let mut dir = root.to_path_buf();
for _ in 0..levels {
dir = dir.join("d");
}
std::fs::create_dir_all(&dir).unwrap();
dir
}
/// Expanding folders reports what the walks left out — files of an unsupported type, a walk that
/// stopped at a bound — and returns the same files as `expand_within`; a file named on its own is
/// attempted whatever its extension and is not counted as unsupported.
#[test]
fn expand_report_counts_what_the_walks_left_out() {
use crate::import::{expand_report, expand_within};
let d = temp_dir("expand-report");
write_png(&d.join("a.png"), 1);
write_png(&d.join("sub/b.png"), 2);
std::fs::write(d.join("notes.txt"), "x").unwrap();
std::fs::write(d.join("sub/readme.md"), "x").unwrap();
std::fs::write(d.join("sub/.hidden.txt"), "x").unwrap();
let named = temp_dir("expand-report-named");
std::fs::write(named.join("odd.dat"), "x").unwrap();
let paths = [d.to_string_lossy().to_string(), named.join("odd.dat").to_string_lossy().to_string()];
let limits = crate::walk::Limits::default();
let e = expand_report(&paths, None, limits);
assert_eq!(e.files.len(), 3, "both photos and the named file: {:?}", e.files);
assert_eq!((e.unsupported, e.unreadable, e.truncated), (2, 0, false), "notes.txt and readme.md; the hidden file is not counted: {e:?}");
assert_eq!(expand_within(&paths, None, limits), (e.files.clone(), false), "expand_within is the same walk");
// stopping one level down: the second folder is never listed, so its files are not counted
let e = expand_report(&paths[..1], None, crate::walk::Limits { max_depth: 1, max_entries: 1000 });
assert_eq!((e.files.len(), e.unsupported, e.truncated), (1, 1, true), "{e:?}");
let _ = std::fs::remove_dir_all(&d);
let _ = std::fs::remove_dir_all(&named);
}
/// A scan says that its folder walk stopped at the depth limit (there may be more photos), and
/// does not when the tree ends within it.
#[test]
fn scan_with_reports_a_walk_that_stopped_at_a_bound() {
use crate::import::{ScanInput, ScanProgress, scan_with};
let scan = |dir: &Path| {
let mut s = Session::new().with_fs();
let (input, paths) = ScanInput::new(&mut s, &[dir.to_string_lossy().to_string()]);
scan_with(input, &paths, &ScanProgress::default())
};
// the deepest folder a walk lists is one level above the limit; one more folder is never entered
let cut = temp_dir("scan-cut");
write_png(&cut.join("top.png"), 1);
write_png(&nested(&cut, crate::walk::MAX_DEPTH - 1).join("last.png"), 2);
nested(&cut, crate::walk::MAX_DEPTH);
let out = scan(&cut);
assert_eq!(out.candidates.len(), 2, "what was reached is still returned");
assert_eq!((out.truncated, out.unreadable, out.unsupported), (true, 0, 0));
let whole = temp_dir("scan-whole");
write_png(&whole.join("top.png"), 1);
write_png(&nested(&whole, crate::walk::MAX_DEPTH - 1).join("last.png"), 2);
let out = scan(&whole);
assert_eq!(out.candidates.len(), 2);
assert_eq!((out.truncated, out.unreadable, out.unsupported), (false, 0, 0));
let _ = std::fs::remove_dir_all(&cut);
let _ = std::fs::remove_dir_all(&whole);
}
/// A folder this process is not allowed to list, until it is dropped (Unix: no permissions;
/// Windows: a deny entry for Everyone on listing it).
struct Unlistable(std::path::PathBuf);
impl Unlistable {
/// `None` when the folder can still be listed (the super-user ignores permissions) or the
/// means to deny it is missing: the test then has nothing to check.
fn new(dir: &Path) -> Option<Unlistable> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o000)).ok()?;
}
#[cfg(windows)]
{
let denied = std::process::Command::new("icacls").arg(dir).args(["/deny", "*S-1-1-0:(RD)"]).output();
if !denied.is_ok_and(|o| o.status.success()) {
return None;
}
}
// from here the folder is restored on drop, whatever the test does
let guard = Unlistable(dir.to_path_buf());
std::fs::read_dir(dir).is_err().then_some(guard)
}
}
impl Drop for Unlistable {
fn drop(&mut self) {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(&self.0, std::fs::Permissions::from_mode(0o755));
}
#[cfg(windows)]
{
let _ = std::process::Command::new("icacls").arg(&self.0).args(["/remove:d", "*S-1-1-0"]).output();
}
}
}
/// A folder the scan can't list is counted — by the scan and by `library.importPreview` — and the
/// photos around it are still found.
#[test]
fn an_unlistable_folder_is_reported_by_the_scan() {
use crate::import::{ScanInput, ScanProgress, scan_with};
let src = temp_dir("unlistable");
write_png(&src.join("a.png"), 1);
write_png(&src.join("locked").join("b.png"), 2);
let Some(locked) = Unlistable::new(&src.join("locked")) else {
eprintln!("skipped: this process can still list a folder it was denied");
let _ = std::fs::remove_dir_all(&src);
return;
};
let mut s = Session::new().with_fs();
let (input, paths) = ScanInput::new(&mut s, &[src.to_string_lossy().to_string()]);
let out = scan_with(input, &paths, &ScanProgress::default());
assert_eq!(out.candidates.len(), 1, "only the photo outside the folder");
assert_eq!((out.unreadable, out.unsupported, out.truncated), (1, 0, false));
let r = s.execute("library.importPreview", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!((r["scanned"].clone(), r["skipped"].clone()), (json!(1), json!({"unreadable": 1, "unsupported": 0})), "{r}");
drop(locked);
let _ = std::fs::remove_dir_all(&src);
}
/// `library.importPreview` reports the walk's diagnostics next to the keys it always returned, and
/// still leaves its probes for the import that follows.
#[test]
fn import_preview_reports_what_the_scan_skipped() {
let src = temp_dir("preview-diag");
write_png(&src.join("a.png"), 1);
write_png(&src.join("sub/b.png"), 2);
std::fs::write(src.join("notes.txt"), b"not a photo").unwrap();
std::fs::write(src.join("sub/readme.md"), b"x").unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.importPreview", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!((r["scanned"].clone(), r["duplicates"].clone()), (json!(2), json!(0)), "{r}");
assert_eq!(ids(&r, "candidates"), 2, "{r}");
assert_eq!(r["scanLimited"], false, "{r}");
assert_eq!(r["skipped"], json!({"unreadable": 0, "unsupported": 2}), "notes.txt and readme.md are reported: {r}");
for f in [src.join("a.png"), src.join("sub").join("b.png")] {
let key = f.to_string_lossy().to_string();
assert!(s.import_probes.contains_key(&key), "the probe of {key} is kept for the import: {:?}", s.import_probes.keys());
}
// a second preview reuses them: a file that has not changed is not read again
let reads = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
let (counted, probe) = (reads.clone(), s.media.file_probe.clone().unwrap());
s.media.file_probe = Some(std::sync::Arc::new(move |path: &str| {
counted.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
probe(path)
}));
let again = s.execute("library.importPreview", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!(again["scanned"], 2, "{again}");
assert_eq!(reads.load(std::sync::atomic::Ordering::Relaxed), 0, "the first preview's probes are reused");
// a file named on its own is tried, not reported as skipped
let r = s.execute("library.importPreview", &json!({"paths": [src.join("notes.txt").to_string_lossy()]})).unwrap();
assert_eq!((r["scanned"].clone(), r["skipped"]["unsupported"].clone()), (json!(1), json!(0)), "{r}");
// a walk that stopped at the depth limit says so
let deep = temp_dir("preview-deep");
write_png(&deep.join("top.png"), 3);
nested(&deep, crate::walk::MAX_DEPTH);
let r = s.execute("library.importPreview", &json!({"paths": [deep.to_string_lossy()]})).unwrap();
assert_eq!((r["scanned"].clone(), r["scanLimited"].clone()), (json!(1), json!(true)), "{r}");
let _ = std::fs::remove_dir_all(&src);
let _ = std::fs::remove_dir_all(&deep);
}
/// XMP sidecars are read with their photos, so they are not "skipped": the expansion, the scan and
/// `library.importPreview` count them (in any letter case, in subfolders too) as nothing, and still
/// count every other file that is not a photo.
#[test]
fn xmp_sidecars_are_not_counted_as_unsupported() {
use crate::import::{ScanInput, ScanProgress, expand_report, scan_with};
let mut s = Session::new().with_fs();
// sidecars beside a photo, a sidecar without one, in any case, one deeper; plus a text file
let mixed = temp_dir("sidecars-mixed");
write_png(&mixed.join("a.png"), 1);
std::fs::write(mixed.join("a.xmp"), b"x").unwrap();
std::fs::write(mixed.join("b.XMP"), b"x").unwrap();
std::fs::create_dir_all(mixed.join("sub")).unwrap();
std::fs::write(mixed.join("sub").join("c.Xmp"), b"x").unwrap();
std::fs::write(mixed.join("c.txt"), b"x").unwrap();
// only photos and sidecars
let tidy = temp_dir("sidecars-only");
write_png(&tidy.join("a.png"), 2);
std::fs::write(tidy.join("a.xmp"), b"x").unwrap();
std::fs::write(tidy.join("b.XMP"), b"x").unwrap();
for (dir, unsupported) in [(&mixed, 1usize), (&tidy, 0)] {
let path = dir.to_string_lossy().to_string();
let e = expand_report(std::slice::from_ref(&path), None, crate::walk::Limits::default());
assert_eq!((e.files.len(), e.unsupported), (1, unsupported), "expand_report {e:?}");
let (input, paths) = ScanInput::new(&mut s, std::slice::from_ref(&path));
let out = scan_with(input, &paths, &ScanProgress::default());
assert_eq!((out.candidates.len(), out.unsupported), (1, unsupported), "scan_with");
let r = s.execute("library.importPreview", &json!({"paths": [path]})).unwrap();
assert_eq!((r["scanned"].clone(), r["skipped"]["unsupported"].clone()), (json!(1), json!(unsupported)), "{r}");
}
let _ = std::fs::remove_dir_all(&mixed);
let _ = std::fs::remove_dir_all(&tidy);
}
/// Raw containers LightCraft cannot decode but shows by their embedded JPEG are picked up from a folder
/// (the extensions are in `import::EXTENSIONS`; the files differ so none is a duplicate) and import as preview only.
#[test]
fn folder_import_picks_up_undecodable_raw_containers_as_preview_only() {
use lightcraft_codecs::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
let src = temp_dir("preview-only-exts");
let px: Vec<u8> = (0..40 * 30).flat_map(|i| [(i % 251) as u8, 128, 200]).collect();
let jpeg = encode_jpeg(&EncodeImage::new(40, 30, 3, Samples::U8(&px)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
// CRW / MRW / X3F: their magic, then the JPEG. IIQ / KDC / MOS / ERF: a TIFF whose IFD0 only points at the JPEG.
let tiff = {
let off = 8 + 2 + 24 + 4;
let mut f = b"II*\0\x08\0\0\0\x02\0".to_vec();
for (tag, v) in [(513u16, off as u32), (514, jpeg.len() as u32)] {
f.extend_from_slice(&tag.to_le_bytes());
f.extend_from_slice(&[4, 0, 1, 0, 0, 0]);
f.extend_from_slice(&v.to_le_bytes());
}
f.extend_from_slice(&[0, 0, 0, 0]);
f.extend_from_slice(&jpeg);
f
};
let with_magic = |magic: &[u8]| [magic, &[0x11u8; 64][..], &jpeg].concat();
let files: [(&str, Vec<u8>); 7] = [
("a.CRW", with_magic(b"II\x1a\0\0\0HEAPCCDR")),
("b.mrw", with_magic(b"\0MRM\0\x01\0\0")),
("c.x3f", with_magic(b"FOVb\x02\0\x02\0")),
("d.iiq", [&tiff[..], b"d"].concat()),
("e.kdc", [&tiff[..], b"e"].concat()),
("f.mos", [&tiff[..], b"f"].concat()),
("g.erf", [&tiff[..], b"g"].concat()),
];
for (name, bytes) in &files {
std::fs::write(src.join(name), bytes).unwrap();
}
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!(r["scanned"], 7, "{r}");
assert_eq!(ids(&r, "imported"), 7, "{r}");
assert_eq!(ids(&r, "failed"), 0, "{r}");
assert!(s.catalog.photos().all(|p| p.preview_only.is_some() && p.kind == lightcraft_catalog::MediaKind::Raw && (p.width, p.height) == (40, 30)));
let _ = std::fs::remove_dir_all(&src);
}
/// Rewrite the JSON header line of the smart preview at `file`.
fn edit_smart_header(file: &Path, edit: impl FnOnce(&mut Value)) {
let bytes = std::fs::read(file).unwrap();
let start = b"LCSP1\n".len();
let nl = start + bytes[start..].iter().position(|b| *b == b'\n').unwrap();
let mut head: Value = serde_json::from_slice(&bytes[start..nl]).unwrap();
edit(&mut head);
let mut out = bytes[..start].to_vec();
out.extend_from_slice(head.to_string().as_bytes());
out.extend_from_slice(&bytes[nl..]);
std::fs::write(file, out).unwrap();
}
fn smart_header(file: &Path) -> Value {
let bytes = std::fs::read(file).unwrap();
let start = b"LCSP1\n".len();
let nl = start + bytes[start..].iter().position(|b| *b == b'\n').unwrap();
serde_json::from_slice(&bytes[start..nl]).unwrap()
}
/// A camera tone curve with a recognisable shape (`k` changes it).
fn test_tone(k: f32) -> lightcraft_pipeline::tone::CameraTone {
lightcraft_pipeline::tone::CameraTone::new(std::array::from_fn(|i| {
let x = 0.004 * 1.17f32.powi(i as i32);
[x, 0.95 * (1.0 - (-k * x).exp())]
}))
.unwrap()
}
/// `plain`, but reporting `tone` as the camera look the (current) fit gives the file; counts its calls.
fn look_loader(
plain: crate::media::FileLoader,
tone: lightcraft_pipeline::tone::CameraTone,
calls: std::sync::Arc<std::sync::atomic::AtomicUsize>,
) -> crate::media::FileLoader {
std::sync::Arc::new(move |path, max_edge, dcp| {
calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let (img, mut info) = plain(path, max_edge, dcp)?;
info.camera_tone = Some(tone);
Ok((img, info))
})
}
/// The follow-up to the camera tone curve change (#499): a smart preview keeps the look the fit
/// gave it, so one written by an older fit is rebuilt when its original is online, kept while the
/// original is offline, and not rebuilt again once current. Only raws that take the per-file
/// camera look go stale (a PNG's proxy never does), and the opening scan runs once per fit version.
#[test]
fn smart_previews_from_an_older_look_fit_are_refreshed() {
for format in ["ARW", "DNG"] {
smart_look_refresh_for(format);
}
}
fn smart_look_refresh_for(format: &str) {
let mut case = SmartLookCase::new(format);
assert_current_smart_look_is_not_rebuilt(&mut case);
let (old_bytes, png_bytes) = keep_offline_previews(&mut case);
refresh_online_previews(&mut case, &old_bytes, &png_bytes);
let _ = std::fs::remove_dir_all(&case.src);
let _ = std::fs::remove_dir_all(&case.lib);
}
struct SmartLookCase {
src: PathBuf,
lib: PathBuf,
session: Session,
id: PhotoId,
dir: PathBuf,
file: PathBuf,
png_file: PathBuf,
old: lightcraft_pipeline::tone::CameraTone,
new: lightcraft_pipeline::tone::CameraTone,
calls: std::sync::Arc<std::sync::atomic::AtomicUsize>,
plain_loader: crate::media::FileLoader,
}
impl SmartLookCase {
fn new(format: &str) -> Self {
let src = temp_dir(&format!("smartlook-src-{format}"));
let lib = temp_dir(&format!("smartlook-lib-{format}"));
write_png(&src.join("a.png"), 3);
write_png(&src.join("b.png"), 9);
let mut session = Session::new().with_fs();
session.open_library(&lib, false).unwrap();
session.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy(), src.join("b.png").to_string_lossy()]})).unwrap();
let find = |s: &Session, name: &str| s.catalog.photos().find(|p| p.file_name == name).unwrap().id;
let (id, png_id) = (find(&session, "a.png"), find(&session, "b.png"));
// `a` stands for a raw that takes the per-file camera look (the test loader decodes its pixels)
let mut raw = (**session.catalog.photo(id).unwrap()).clone();
raw.format = format.into();
raw.kind = lightcraft_catalog::MediaKind::Raw;
session.catalog.apply(lightcraft_catalog::Op::RemovePhoto { id }).unwrap();
session.catalog.apply(lightcraft_catalog::Op::AddPhoto { photo: Box::new(raw) }).unwrap();
assert_eq!(session.catalog.photo(id).unwrap().relative_wb(), format == "ARW");
assert!(!session.catalog.photo(png_id).unwrap().relative_wb());
session.execute("library.select", &json!({"ids": [id.0, png_id.0], "active": id.0})).unwrap();
let dir = session.media.smart_dir.clone().unwrap();
let file = crate::smart::sharded_file_path(&dir, session.catalog.photo(id).unwrap());
let png_file = crate::smart::sharded_file_path(&dir, session.catalog.photo(png_id).unwrap());
let (old, new) = (test_tone(1.5), test_tone(3.5));
let calls = std::sync::Arc::new(AtomicUsize::new(0));
let plain_loader = session.media.file_loader.clone().unwrap();
session.media.file_loader = Some(look_loader(plain_loader.clone(), old, calls.clone()));
Self { src, lib, session, id, dir, file, png_file, old, new, calls, plain_loader }
}
}
fn assert_current_smart_look_is_not_rebuilt(case: &mut SmartLookCase) {
let r = case.session.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["built"].as_u64(), r["refreshed"].as_u64()), (Some(2), Some(0)), "{r}");
assert_eq!(smart_header(&case.file)["look_version"], json!(crate::camera_preview::LOOK_VERSION), "the writer stamps the fit version");
assert!(!crate::smart::is_stale(&case.file));
// current header: kept as it is, the original is not even read
let before = std::fs::read(&case.file).unwrap();
case.calls.store(0, std::sync::atomic::Ordering::SeqCst);
case.session.media.file_loader = Some(look_loader(case.plain_loader.clone(), case.new, case.calls.clone()));
let r = case.session.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["built"].as_u64(), r["refreshed"].as_u64()), (Some(2), Some(0)), "{r}");
assert_eq!(std::fs::read(&case.file).unwrap(), before, "a current proxy is not rebuilt");
assert_eq!(case.calls.load(std::sync::atomic::Ordering::SeqCst), 0);
}
fn keep_offline_previews(case: &mut SmartLookCase) -> (Vec<u8>, Vec<u8>) {
// old headers (no field), originals offline: kept unchanged, and still marked stale
for f in [&case.file, &case.png_file] {
edit_smart_header(f, |h| {
h.as_object_mut().unwrap().remove("look_version");
});
}
assert!(crate::smart::is_stale(&case.file), "no field = version 0");
let old_bytes = std::fs::read(&case.file).unwrap();
let png_bytes = std::fs::read(&case.png_file).unwrap();
std::fs::rename(&case.src, case.src.with_extension("offline")).unwrap();
case.session.media.forget(case.id);
let r = case.session.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["built"].as_u64(), r["refreshed"].as_u64(), r["staleKept"].as_u64()), (Some(2), Some(0), Some(1)), "{r}");
assert!(r["failed"].as_array().unwrap().is_empty(), "{r}");
assert_eq!(std::fs::read(&case.file).unwrap(), old_bytes, "offline: the stored preview and curve are untouched");
assert!(crate::smart::is_stale(&case.file), "and it stays marked for the next time");
assert_eq!(crate::smart::decode(&old_bytes).unwrap().1, Some(case.old), "the old look is still there");
// ... and the opening refresh keeps it too, then marks the folder checked
assert!(!crate::smart::look_checked(&case.dir));
crate::cmd::previews::refresh_stale_smart_previews(&mut case.session).unwrap().join().unwrap();
assert_eq!(std::fs::read(&case.file).unwrap(), old_bytes);
assert!(crate::smart::look_checked(&case.dir));
(old_bytes, png_bytes)
}
fn refresh_online_previews(case: &mut SmartLookCase, old_bytes: &[u8], png_bytes: &[u8]) {
// the original comes back: the scan has run at this version, so the next opening leaves it;
// Build Smart Previews rebuilds it with the current look
std::fs::rename(case.src.with_extension("offline"), &case.src).unwrap();
case.calls.store(0, std::sync::atomic::Ordering::SeqCst);
assert!(crate::cmd::previews::refresh_stale_smart_previews(&mut case.session).is_none(), "checked once per version");
assert_eq!(std::fs::read(&case.file).unwrap(), old_bytes);
let r = case.session.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["refreshed"].as_u64(), case.calls.load(std::sync::atomic::Ordering::SeqCst)), (Some(1), 1), "{r}");
assert_eq!(smart_header(&case.file)["look_version"], json!(crate::camera_preview::LOOK_VERSION));
assert_eq!(crate::smart::decode(&std::fs::read(&case.file).unwrap()).unwrap().1, Some(case.new), "rebuilt with the current look");
assert!(!crate::smart::is_stale(&case.file));
assert_eq!(std::fs::read(&case.png_file).unwrap(), png_bytes, "a PNG's proxy is never rebuilt for the look");
// a folder checked at an older version (or not at all) is scanned once at the next opening
edit_smart_header(&case.file, |h| h["look_version"] = json!(0));
std::fs::write(case.dir.join("look-version"), b"0\n").unwrap();
assert!(!crate::smart::look_checked(&case.dir));
case.session.media.forget(case.id);
case.calls.store(0, std::sync::atomic::Ordering::SeqCst);
crate::cmd::previews::refresh_stale_smart_previews(&mut case.session).unwrap().join().unwrap();
assert_eq!(case.calls.load(std::sync::atomic::Ordering::SeqCst), 1, "only the raw's original is read");
assert!(!crate::smart::is_stale(&case.file));
assert_eq!(std::fs::read(&case.png_file).unwrap(), png_bytes);
assert!(crate::smart::look_checked(&case.dir));
// once only
let rebuilt = std::fs::read(&case.file).unwrap();
assert!(crate::cmd::previews::refresh_stale_smart_previews(&mut case.session).is_none());
let r = case.session.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["refreshed"].as_u64(), case.calls.load(std::sync::atomic::Ordering::SeqCst)), (Some(0), 1), "{r}");
assert_eq!(std::fs::read(&case.file).unwrap(), rebuilt);
// Build Smart Previews refreshes a stale one the same way
edit_smart_header(&case.file, |h| h["look_version"] = json!(0));
case.session.media.file_loader = Some(case.plain_loader.clone());
let r = case.session.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!(r["refreshed"], 1, "{r}");
assert_eq!(crate::smart::decode(&std::fs::read(&case.file).unwrap()).unwrap().1, None, "this fit gives no curve: none is kept");
}
/// Issue #1534 (review): a proxy's encoding version is migrated independently of the per-file
/// camera look. A DNG keeps an absolute white balance, so it never takes that look; at head its
/// existing proxy was kept at an older encoding even with its original online, so the decoder
/// facts the newer encoding carries never reached it through a normal rebuild.
#[test]
fn a_proxy_with_an_older_encoding_is_refreshed_without_the_camera_look() {
let src = temp_dir("smartenc-src");
let lib = temp_dir("smartenc-lib");
write_png(&src.join("a.png"), 3);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
// the test loader decodes the catalogue photo's "DNG" pixels
let pixels = lightcraft_raster::Rgb32f::from_fn(48, 32, |x, y| [0.02 + (x % 9) as f32 * 0.05, 0.03 + (y % 7) as f32 * 0.04, 0.06]);
let info = lightcraft_pipeline::SourceInfo { raw: true, as_shot_temp: 5200.0, as_shot_tint: 6.0, ..Default::default() };
let (p, i) = (pixels.clone(), info.clone());
s.media.file_loader = Some(std::sync::Arc::new(move |_path, _max_edge, _| Ok((p.clone(), i.clone()))));
s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()]})).unwrap();
let id = s.active().unwrap();
let mut raw = (**s.catalog.photo(id).unwrap()).clone();
raw.format = "DNG".into();
raw.kind = lightcraft_catalog::MediaKind::Raw;
s.catalog.apply(lightcraft_catalog::Op::RemovePhoto { id }).unwrap();
s.catalog.apply(lightcraft_catalog::Op::AddPhoto { photo: Box::new(raw) }).unwrap();
assert!(s.catalog.photo(id).unwrap().develops_raw() && !s.catalog.photo(id).unwrap().fitted_look(), "a DNG does not take the camera look");
s.execute("library.select", &json!({"ids": [id.0], "active": id.0})).unwrap();
let dir = s.media.smart_dir.clone().unwrap();
let file = crate::smart::sharded_file_path(&dir, s.catalog.photo(id).unwrap());
let r = s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["built"].as_u64(), r["refreshed"].as_u64()), (Some(1), Some(0)), "{r}");
assert_eq!(smart_header(&file)["enc"], json!(crate::smart::ENCODE_VERSION));
// an existing proxy from the previous encoding, its original online: the migration must reach
// it through a normal rebuild even though the photo never takes the camera look
edit_smart_header(&file, |h| h["enc"] = json!(crate::smart::ENCODE_VERSION - 1));
let stale_bytes = std::fs::read(&file).unwrap();
assert!(crate::smart::is_stale(&file), "the older encoding is stale even without the camera look");
let r = s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["built"].as_u64(), r["refreshed"].as_u64(), r["staleKept"].as_u64()), (Some(1), Some(1), Some(0)), "{r}");
assert_eq!(smart_header(&file)["enc"], json!(crate::smart::ENCODE_VERSION), "migrated to the current encoding");
assert!(!crate::smart::is_stale(&file));
assert_ne!(std::fs::read(&file).unwrap(), stale_bytes, "the proxy was rebuilt");
let _ = std::fs::remove_dir_all(&src);
let _ = std::fs::remove_dir_all(&lib);
}
/// Issue #1534 (review): a cached proxy source is not the original. After an offline edit rendered
/// from a stale proxy, Build Smart Previews read that cached source back and refreshed the proxy
/// from itself, stamping its older look as current. A stale refresh reads the original only, so an
/// offline original leaves the stored look — and the bytes — as they are.
#[test]
fn a_stale_refresh_never_rebuilds_a_proxy_from_itself() {
let src = temp_dir("smartself-src");
let lib = temp_dir("smartself-lib");
write_png(&src.join("a.png"), 3);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let pixels = lightcraft_raster::Rgb32f::from_fn(48, 32, |x, y| [0.02 + (x % 9) as f32 * 0.05, 0.03 + (y % 7) as f32 * 0.04, 0.06]);
let info = lightcraft_pipeline::SourceInfo { raw: true, as_shot_temp: 5200.0, as_shot_tint: 6.0, ..Default::default() };
let (p, i) = (pixels.clone(), info.clone());
s.media.file_loader = Some(std::sync::Arc::new(move |_path, _max_edge, _| Ok((p.clone(), i.clone()))));
s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()]})).unwrap();
let id = s.active().unwrap();
// a raw that takes the per-file camera look, so an older fit makes its proxy stale
let mut raw = (**s.catalog.photo(id).unwrap()).clone();
raw.format = "ARW".into();
raw.kind = lightcraft_catalog::MediaKind::Raw;
s.catalog.apply(lightcraft_catalog::Op::RemovePhoto { id }).unwrap();
s.catalog.apply(lightcraft_catalog::Op::AddPhoto { photo: Box::new(raw) }).unwrap();
assert!(s.catalog.photo(id).unwrap().fitted_look());
s.execute("library.select", &json!({"ids": [id.0], "active": id.0})).unwrap();
let dir = s.media.smart_dir.clone().unwrap();
let file = crate::smart::sharded_file_path(&dir, s.catalog.photo(id).unwrap());
let r = s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!(r["built"], 1, "{r}");
edit_smart_header(&file, |h| h["look_version"] = json!(0));
let stale_bytes = std::fs::read(&file).unwrap();
assert!(crate::smart::is_stale(&file), "the stored look is behind this build's fit");
// the original goes offline, then a preview-size render loads the proxy into the source cache
std::fs::rename(&src, src.with_extension("offline")).unwrap();
s.media.forget(id);
let _ = s.render_now(id, 600, 600).expect("renders from the smart preview");
// Build Smart Previews must not take that cached proxy for the original: it can't refresh a
// stale proxy from itself
let r = s.execute("library.smartPreviews", &json!({})).unwrap();
assert_eq!((r["refreshed"].as_u64(), r["staleKept"].as_u64()), (Some(0), Some(1)), "{r}");
assert_eq!(std::fs::read(&file).unwrap(), stale_bytes, "the offline proxy is preserved");
assert!(crate::smart::is_stale(&file), "still stale: the original was not read");
let _ = std::fs::remove_dir_all(src.with_extension("offline"));
let _ = std::fs::remove_dir_all(&lib);
}
/// The look marker: missing, damaged or older than this build's fit version counts as unchecked.
#[test]
fn smart_preview_look_marker_is_read_safely() {
let dir = temp_dir("smartlook-marker");
assert!(!crate::smart::look_checked(&dir), "no marker");
let cur = crate::camera_preview::LOOK_VERSION;
let enc = crate::smart::ENCODE_VERSION;
for (text, checked) in [
(String::new(), false),
("banana".into(), false),
("-1".into(), false),
(format!("{}", cur.saturating_sub(1)), false),
("9".repeat(200), false),
// Written before the encode version was recorded: only says the look was checked, so the
// folder is scanned once more and the old-encoding proxies are rebuilt.
(format!("{cur}\n"), false),
(format!("{cur} {}\n", enc.saturating_sub(1)), false),
(format!("{cur} banana\n"), false),
(format!("{cur} {enc}\n"), true),
(format!(" {} {}\n", cur + 1, enc + 1), true),
] {
std::fs::write(dir.join("look-version"), &text).unwrap();
assert_eq!(crate::smart::look_checked(&dir), checked, "{text:?}");
}
std::fs::remove_file(dir.join("look-version")).unwrap();
crate::smart::mark_look_checked(&dir).unwrap();
assert!(crate::smart::look_checked(&dir));
assert!(crate::smart::mark_look_checked(&dir.join("missing")).is_err());
let _ = std::fs::remove_dir_all(&dir);
}
/// A hostile or damaged `look_version` never panics: anything that is not a non-negative integer
/// counts as 0 (rebuilt when the original is online); a version from a newer build is left alone.
#[test]
fn malformed_smart_preview_look_versions_are_handled() {
let dir = temp_dir("smartlook-bad");
let img = lightcraft_scenes::demo_library()[0].render(32, 20);
let file = dir.join("a.lcsp");
std::fs::write(&file, crate::smart::encode(&img, None).unwrap()).unwrap();
let cur = u64::from(crate::camera_preview::LOOK_VERSION);
for bad in [json!("1"), json!(-1), json!(1.5), json!(null), json!([]), json!({}), json!(true), json!(1e30), json!(-0.0)] {
let shown = bad.to_string();
edit_smart_header(&file, |h| h["look_version"] = bad);
assert!(crate::smart::is_valid(&file), "{shown}");
assert!(crate::smart::is_stale(&file), "{shown} counts as version 0");
assert!(crate::smart::decode(&std::fs::read(&file).unwrap()).is_ok(), "{shown}");
}
for fine in [json!(cur), json!(cur + 1), json!(u64::MAX)] {
let shown = fine.to_string();
edit_smart_header(&file, |h| h["look_version"] = fine);
assert!(!crate::smart::is_stale(&file), "{shown}");
}
// not a header at all, or missing
assert!(!crate::smart::is_stale(&dir.join("missing.lcsp")));
std::fs::write(&file, b"LCSP1\n{not json\n\xFF\xD8").unwrap();
assert!(!crate::smart::is_stale(&file));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_file_gone_since_the_import_review_is_not_imported() {
let src = temp_dir("gone-since-review");
write_png(&src.join("a.png"), 1);
write_png(&src.join("b.png"), 2);
let mut s = Session::new().with_fs();
s.execute("library.importPreview", &json!({"paths": [src.to_string_lossy()]})).unwrap();
std::fs::remove_file(src.join("b.png")).unwrap();
let r = s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy(), src.join("b.png").to_string_lossy()]})).unwrap();
assert_eq!((ids(&r, "imported"), ids(&r, "failed")), (1, 1), "{r}");
assert_eq!(s.catalog.len(), 1, "no photo for a file that isn't there");
let _ = std::fs::remove_dir_all(&src);
}
/// A HEIC laid out like an iPhone's (a grid of HEVC pictures, a Display P3 profile, a quarter turn
/// in the container, a thumbnail) imports as a normal photo that develops and exports upright like
/// a JPEG. In a build without the codecs' `heif` feature it is reported as failed, saying why.
#[test]
fn heic_imports_as_a_normal_photo_or_says_why_not() {
use craft_heif::testdata::{Prop, Spec, build};
let src = temp_dir("heic");
let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150];
let p3 = lightcraft_codecs::icc::write_named(lightcraft_codecs::NamedSpace::DisplayP3);
let bytes = build(&Spec { props: vec![Prop::Icc(p3), Prop::Irot(1)], thumbnail: Some(&photo), ..Spec::new(96, 64, &photo) });
std::fs::write(src.join("IMG_0001.HEIC"), &bytes).unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
if !lightcraft_codecs::Format::Heif.can_decode() {
assert_eq!((ids(&r, "imported"), ids(&r, "failed")), (0, 1), "{r}");
assert!(r["failed"].to_string().contains("isn't included in this build"), "{r}");
let _ = std::fs::remove_dir_all(&src);
return;
}
assert_eq!((ids(&r, "imported"), ids(&r, "failed")), (1, 0), "{r}");
let p = s.catalog.photos().next().unwrap();
let id = p.id;
// Upright (the container's turn applied) and not preview-only.
assert_eq!((p.kind, p.preview_only.clone(), p.width, p.height), (lightcraft_catalog::MediaKind::Image, None, 64, 96));
s.execute("library.select", &json!({"ids": [id.0], "active": id.0})).unwrap();
let plain = crate::export::export_photo(&mut s, id, &crate::export::ExportOptions::from_json(&json!({"format": "png"})), 1).unwrap();
assert_eq!((plain.width, plain.height), (64, 96));
s.execute("develop.set", &json!({"control": "light.exposure", "value": 1.0})).unwrap();
let brighter = crate::export::export_photo(&mut s, id, &crate::export::ExportOptions::from_json(&json!({"format": "png"})), 1).unwrap();
let mean = |png: &[u8]| {
let d = lightcraft_codecs::decode(png, Default::default()).unwrap();
d.image.data.iter().map(|p| p[1]).sum::<f32>() / d.image.data.len() as f32
};
assert!(mean(&brighter.bytes) > mean(&plain.bytes) * 1.3, "exposure +1 brightens the HEIC");
let _ = std::fs::remove_dir_all(&src);
}
/// Issue #2284: a photo taken at 13:00 in UTC-5 must be catalogued as 13:00. HEICs carried no Exif
/// (issue #1233), so every one of them fell back to the file's modification time, which the import
/// writes as UTC: an iPhone photo landed five hours late and after 19:00 on the wrong day.
#[test]
fn a_heic_is_dated_by_its_exif_and_not_by_the_file() {
use craft_heif::testdata::{Spec, build};
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
let src = temp_dir("heic-capture-time");
let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150];
let mut exif = IfdBuilder::new();
exif.set(t::DATE_TIME_ORIGINAL, Value::Ascii("2026:10:18 13:00:00".into()));
exif.set(t::OFFSET_TIME_ORIGINAL, Value::Ascii("-05:00".into()));
let mut ifd0 = IfdBuilder::new();
ifd0.set(t::MAKE, Value::Ascii("Apple".into()));
ifd0.set(t::MODEL, Value::Ascii("iPhone 15 Pro".into()));
// HEIF turns the picture in the container; its Exif Orientation only mirrors that, so reading
// the Exif must not turn it a second time.
ifd0.set(t::ORIENTATION, Value::Short(vec![6]));
ifd0.set_child(t::EXIF_IFD, exif);
let exif = TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap();
let bytes = build(&Spec { exif: Some(exif), ..Spec::new(96, 64, &photo) });
let path = src.join("IMG_0002.HEIC");
std::fs::write(&path, &bytes).unwrap();
// The probe reads the metadata in every build; only decoding the picture needs the feature.
let info = crate::files::probe_bytes("IMG_0002.HEIC", &bytes);
if lightcraft_codecs::Format::Heif.can_decode() {
let info = info.unwrap();
assert_eq!(info.captured.as_deref(), Some("2026-10-18T13:00:00-05:00"));
assert_eq!(info.meta.camera, "Apple iPhone 15 Pro");
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!((ids(&r, "imported"), ids(&r, "failed")), (1, 0), "{r}");
let p = s.catalog.photos().next().unwrap();
assert_eq!(p.captured.as_deref(), Some("2026-10-18T13:00:00-05:00"), "the time the camera wrote, not the file's");
assert_eq!((p.width, p.height), (96, 64), "upright: the container's own transforms, not the Exif tag as well");
assert_eq!(lightcraft_catalog::dates::display_time(p.date()), "October 18, 2026 at 1:00:00 PM");
} else {
assert!(info.is_err(), "without the feature a HEIC cannot be opened at all");
assert_eq!(lightcraft_meta::extract(&bytes).capture_time.map(|d| d.to_iso()).as_deref(), Some("2026-10-18T13:00:00-05:00"));
}
let _ = std::fs::remove_dir_all(&src);
}
/// Issue #705: Nikon (and most cameras) write `.JPG` and `.NEF`, and a user may rename to `.Jpg`.
/// The folder scan, `library.browse` and the sidecar lookup match extensions in any letter
/// case. (The file dialog's filters, the one place that was case-sensitive on Linux before
/// #342, are checked in `apps/lightcraft/src/dialog_filter.rs`.)
#[test]
fn upper_and_mixed_case_extensions_are_found_by_import_browse_and_sidecar_lookup() {
use lightcraft_codecs::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
let src = temp_dir("upper-exts");
let jpeg = |seed: u8| {
let px: Vec<u8> = (0..40 * 30).flat_map(|i| [(i % 251) as u8, seed, 200]).collect();
encode_jpeg(&EncodeImage::new(40, 30, 3, Samples::U8(&px)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap()
};
std::fs::write(src.join("a.JPG"), jpeg(1)).unwrap();
std::fs::write(src.join("b.Jpg"), jpeg(2)).unwrap();
std::fs::write(src.join("c.JPEG"), jpeg(3)).unwrap();
write_png(&src.join("d.PNG"), 4);
// a raw container LightCraft shows by its embedded JPEG (a TIFF whose IFD0 points at one)
let nef = {
let j = jpeg(5);
let off = 8 + 2 + 24 + 4;
let mut f = b"II*\0\x08\0\0\0\x02\0".to_vec();
for (tag, v) in [(513u16, off as u32), (514, j.len() as u32)] {
f.extend_from_slice(&tag.to_le_bytes());
f.extend_from_slice(&[4, 0, 1, 0, 0, 0]);
f.extend_from_slice(&v.to_le_bytes());
}
f.extend_from_slice(&[0, 0, 0, 0]);
f.extend_from_slice(&j);
f
};
std::fs::write(src.join("e.NEF"), nef).unwrap();
std::fs::write(src.join("notes.TXT"), "not a photo").unwrap();
// sidecars: stem-named with an upper-case extension, and full-named beside a mixed-case photo
let sidecar = |rating: u8| {
format!(
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:Rating="{rating}"/></rdf:RDF></x:xmpmeta>"#
)
};
std::fs::write(src.join("a.XMP"), sidecar(4)).unwrap();
std::fs::write(src.join("b.Jpg.xmp"), sidecar(2)).unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!(r["scanned"], 5, "{r}"); // the five photos; notes.TXT is not one
assert_eq!(ids(&r, "imported"), 5, "{r}");
assert_eq!(ids(&r, "failed"), 0, "{r}");
assert_eq!(r["sidecars"], 2, "{r}");
let names: Vec<(String, String, u8)> = s.catalog.photos().map(|p| (p.file_name.clone(), p.format.clone(), p.rating)).collect();
let by_name = |n: &str| names.iter().find(|(f, _, _)| f == n).cloned().unwrap_or_else(|| panic!("{n} missing from {names:?}"));
assert_eq!(by_name("a.JPG").2, 4, "a.XMP read");
assert_eq!(by_name("b.Jpg").2, 2, "b.Jpg.xmp read");
assert_eq!(by_name("c.JPEG").1, "JPEG");
assert_eq!(by_name("d.PNG").1, "PNG");
let nef = s.catalog.photos().find(|p| p.file_name == "e.NEF").unwrap();
assert!(nef.kind == lightcraft_catalog::MediaKind::Raw && nef.preview_only.is_some(), "{nef:?}");
// Browse Folder (the Local section) lists the same five, without adding them to the library
let mut b = Session::new().with_fs();
let r = b.execute("library.browse", &json!({"path": src.to_string_lossy()})).unwrap();
assert_eq!(r["photos"], 5, "{r}");
assert_eq!(r["new"], 5, "{r}");
assert_eq!(r["failed"], 0, "{r}");
let visible = b.visible_cloned();
let shown: Vec<String> = visible.iter().filter_map(|id| b.catalog.photo(*id).map(|p| p.file_name.clone())).collect();
for n in ["a.JPG", "b.Jpg", "c.JPEG", "d.PNG", "e.NEF"] {
assert!(shown.iter().any(|f| f == n), "{n} not shown: {shown:?}");
}
let _ = std::fs::remove_dir_all(&src);
}
/// Sidecars are found and read whatever their name's case, header or byte-order mark, so a
/// raw's rating and crop never silently stay behind while the file's own IPTC comes in (fork #43).
#[test]
fn raw_sidecar_rating_and_crop_survive_naming_and_encoding_variants() {
use lightcraft_codecs::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
let px: Vec<u8> = (0..60 * 40).flat_map(|i| [(i % 251) as u8, 90, 200]).collect();
let j = encode_jpeg(&EncodeImage::new(60, 40, 3, Samples::U8(&px)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
let mut nef = b"II*\0\x08\0\0\0\x02\0".to_vec();
for (tag, v) in [(513u16, 38u32), (514, j.len() as u32)] {
nef.extend_from_slice(&tag.to_le_bytes());
nef.extend_from_slice(&[4, 0, 1, 0, 0, 0]);
nef.extend_from_slice(&v.to_le_bytes());
}
nef.extend_from_slice(&[0, 0, 0, 0]);
nef.extend_from_slice(&j);
let body = r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/" xmp:Rating="3" crs:HasCrop="True" crs:CropLeft="0.2" crs:CropTop="0.1" crs:CropRight="0.8" crs:CropBottom="0.9" crs:CropAngle="1.5"/></rdf:RDF></x:xmpmeta>"#;
let cases = [
("darktable-full", "DSC_0001.NEF.xmp", body.to_string()),
("lower-full", "DSC_0001.nef.xmp", body.to_string()),
("upper-full", "DSC_0001.NEF.XMP", body.to_string()),
("mixed-stem", "DSC_0001.Xmp", body.to_string()),
("bom-and-decl", "DSC_0001.xmp", format!("\u{feff}<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- written by another app -->\n{body}")),
];
for (name, file, text) in cases {
let src = temp_dir(&format!("sc-variant-{name}"));
std::fs::write(src.join("DSC_0001.NEF"), &nef).unwrap();
std::fs::write(src.join(file), text).unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!((ids(&r, "imported"), r["sidecars"].as_u64()), (1, Some(1)), "{name}: {r}");
let p = s.catalog.photos().next().unwrap();
assert_eq!(p.rating, 3, "{name}");
let c = p.develop.to_json()["crop"]["geometry"].clone();
assert!((c["rect"]["x0"].as_f64().unwrap_or(0.0) - 0.2).abs() < 1e-6, "{name}: {c}");
if name == "lower-full" {
// a move takes that sidecar along
let dest = temp_dir("sc-variant-dest");
let mut s = Session::new().with_fs();
let args = json!({"paths": [src.to_string_lossy()], "mode": "move", "destination": dest.to_string_lossy(), "organize": "flat"});
let r = s.execute("library.import", &args).unwrap();
assert_eq!(s.catalog.photos().next().unwrap().rating, 3, "{r}");
assert!(std::fs::read_dir(&dest).unwrap().flatten().any(|e| e.file_name().to_string_lossy().to_lowercase().ends_with(".xmp")), "{r}");
assert!(!src.join(file).exists(), "no sidecar left behind: {r}");
let _ = std::fs::remove_dir_all(&dest);
}
let _ = std::fs::remove_dir_all(&src);
}
}
/// Issue #1001: a Lightroom preset that names a profile applies it once the user has imported a
/// profile of that name (before or after the preset), maps the basic Adobe profiles to ours, and
/// reports the ones it can't use.
#[test]
fn presets_apply_the_imported_profile_they_name() {
let src = temp_dir("lookref");
let preset = |file: &str, name: &str, look: &str| {
let x = format!(
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="" xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/"
crs:PresetType="Normal" crs:Name="{name}" crs:UUID="{file}" crs:Exposure2012="0" crs:GrainAmount="20">
<crs:Look><rdf:Description crs:Name="{look}" crs:Amount="0.6" crs:UUID="1234ABCD">
<crs:Group><rdf:Alt><rdf:li xml:lang="x-default">Film Pack</rdf:li></rdf:Alt></crs:Group>
</rdf:Description></crs:Look>
</rdf:Description></rdf:RDF></x:xmpmeta>"#
);
std::fs::write(src.join(format!("{file}.xmp")), x).unwrap();
};
preset("a1", "Portra Soft", "Portra 400");
preset("a2", "Mono", "Adobe Monochrome");
preset("a3", "Artsy", "Artistic 01");
let mut s = Session::with_demo();
let r = s.execute("preset.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
let by_name = |r: &serde_json::Value, n: &str| r["imported"].as_array().unwrap().iter().find(|i| i["name"] == n).cloned().unwrap();
assert_eq!(by_name(&r, "Portra Soft")["unmapped"], json!(["Profile “Portra 400” (not imported yet)"]), "{r}");
assert_eq!(by_name(&r, "Mono")["profile"], json!({"name": "Adobe Monochrome", "id": "lc.mono"}));
assert_eq!(by_name(&r, "Artsy")["unmapped"], json!(["Profile “Artistic 01” (Lightroom's own)"]));
let photo = s.catalog.photos().next().unwrap().id;
s.execute("library.select", &json!({"ids": [photo.0]})).unwrap();
let pid = |s: &Session, n: &str| s.presets.iter().find(|p| p.name == n).unwrap().id.clone();
let apply = |s: &mut Session, n: &str| {
let id = pid(s, n);
s.execute("preset.apply", &json!({"id": id})).unwrap();
s.develop_of(photo).unwrap()
};
// not imported yet: only grain comes across, the photo keeps its profile
let d = apply(&mut s, "Portra Soft");
assert_eq!((d.profile.id.as_str(), d.grain.amount), ("lc.color", 20.0));
// the basic Adobe profiles are ours
assert_eq!(apply(&mut s, "Mono").profile.id, "lc.mono");
s.execute("develop.profile", &json!({"id": "lc.color", "amount": 100})).unwrap();
assert_eq!(apply(&mut s, "Artsy").profile.id, "lc.color", "Lightroom's own creative profiles aren't ours");
// importing the profile afterwards makes the preset use it, at the preset's amount
let mut cube = String::from("TITLE \"Portra 400\"\nLUT_3D_SIZE 2\n");
for b in 0..2 {
for g in 0..2 {
for r in 0..2 {
cube.push_str(&format!("{r} {g} {b}\n"));
}
}
}
std::fs::create_dir_all(src.join("profiles/Film Pack")).unwrap();
std::fs::write(src.join("profiles/Film Pack/portra.cube"), &cube).unwrap();
let r = s.execute("profile.import", &json!({"paths": [src.join("profiles").to_string_lossy()]})).unwrap();
let lut = r["imported"][0]["id"].as_str().unwrap().to_string();
let d = apply(&mut s, "Portra Soft");
assert_eq!((d.profile.id.as_str(), d.profile.amount, d.grain.amount), (lut.as_str(), 60.0, 20.0));
// a preset imported after the profile resolves straight away
preset("a4", "Portra Hard", "portra 400");
let r = s.execute("preset.import", &json!({"paths": [src.join("a4.xmp").to_string_lossy()]})).unwrap();
assert_eq!((r["imported"][0]["profile"]["id"].as_str(), r["imported"][0]["unmapped"].clone()), (Some(lut.as_str()), json!([])), "{r}");
// deleting the profile leaves the photo's own profile alone again
s.execute("profile.deleteImported", &json!({"id": lut})).unwrap();
s.execute("develop.profile", &json!({"id": "lc.vivid", "amount": 100})).unwrap();
assert_eq!(apply(&mut s, "Portra Soft").profile.id, "lc.vivid");
let _ = std::fs::remove_dir_all(&src);
}
/// A raw container LightCraft shows by its embedded JPEG: a TIFF whose IFD0 points at one.
fn write_raw(path: &Path) {
use lightcraft_codecs::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
let px: Vec<u8> = (0..48 * 32).flat_map(|i| [(i % 251) as u8, 91, 201]).collect();
let j = encode_jpeg(&EncodeImage::new(48, 32, 3, Samples::U8(&px)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
let off = 8 + 2 + 24 + 4;
let mut f = b"II*\0\x08\0\0\0\x02\0".to_vec();
for (tag, v) in [(513u16, off as u32), (514, j.len() as u32)] {
f.extend_from_slice(&tag.to_le_bytes());
f.extend_from_slice(&[4, 0, 1, 0, 0, 0]);
f.extend_from_slice(&v.to_le_bytes());
}
f.extend_from_slice(&[0, 0, 0, 0]);
f.extend_from_slice(&j);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, f).unwrap();
}
/// An edited TIFF of a raw: the same name, a TIFF (not a raw) to LightCraft.
fn write_tiff(path: &Path, seed: u8) {
use lightcraft_codecs::{EncodeImage, EncodeMeta, Samples, TiffCompression, encode_tiff};
let (w, h) = (48usize, 32usize);
let px: Vec<u8> = (0..w * h).flat_map(|i| [(i % w * 5) as u8, (i / w * 7) as u8, seed]).collect();
let bytes = encode_tiff(&EncodeImage::new(w as u32, h as u32, 3, Samples::U8(&px)), TiffCompression::Deflate, &EncodeMeta::default()).unwrap();
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, bytes).unwrap();
}
/// Issue #1905 (the import side of #1523): the stem sidecar `IMG_1.xmp` belongs to the raw first,
/// so a TIFF or JPEG arriving beside a raw of the same name reads its own `IMG_1.TIF.xmp`, never
/// the raw's packet, with or without the raw. Before, the arriving file took the raw's rating,
/// label, metadata and develop settings.
#[test]
fn a_file_arriving_beside_its_raws_stem_reads_its_own_sidecar() {
let sidecar = |rating: u8| {
format!(
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:Rating="{rating}"/></rdf:RDF></x:xmpmeta>"#
)
};
let read = |s: &Session, name: &str| -> u8 { s.catalog.photos().find(|p| p.file_name == name).map(|p| p.rating).unwrap_or_default() };
// the raw and the edited TIFF of it, same name, in one import
let src = temp_dir("stem-pair");
write_raw(&src.join("IMG_1.NEF"));
write_tiff(&src.join("IMG_1.TIF"), 7);
std::fs::write(src.join("IMG_1.xmp"), sidecar(5)).unwrap();
std::fs::write(src.join("IMG_1.TIF.xmp"), sidecar(2)).unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!((ids(&r, "imported"), ids(&r, "failed")), (2, 0), "{r}");
assert_eq!(r["sidecars"], 2, "{r}");
assert_eq!(read(&s, "IMG_1.NEF"), 5, "the raw reads the stem sidecar, which is its own");
assert_eq!(read(&s, "IMG_1.TIF"), 2, "the TIFF reads its own sidecar, never the raw's");
let _ = std::fs::remove_dir_all(&src);
// the TIFF with no sidecar of its own: nothing of the raw's packet comes across
let src = temp_dir("stem-none");
write_raw(&src.join("IMG_2.NEF"));
write_tiff(&src.join("IMG_2.TIF"), 8);
std::fs::write(src.join("IMG_2.xmp"), sidecar(4)).unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!((ids(&r, "imported"), ids(&r, "failed")), (2, 0), "{r}");
assert_eq!(r["sidecars"], 1, "{r}");
assert_eq!(read(&s, "IMG_2.NEF"), 4);
assert_eq!(read(&s, "IMG_2.TIF"), 0, "the raw's packet is not the TIFF's");
let _ = std::fs::remove_dir_all(&src);
// the raw already in the library, the edited TIFF arriving later
use lightcraft_catalog::{MediaKind, Op, Photo, Source};
let src = temp_dir("stem-after");
write_raw(&src.join("IMG_3.NEF"));
write_tiff(&src.join("IMG_3.TIF"), 9);
std::fs::write(src.join("IMG_3.xmp"), sidecar(3)).unwrap();
let mut s = Session::new().with_fs();
let raw = {
let id = s.catalog.alloc_photo_id();
let path = src.join("IMG_3.NEF").to_string_lossy().into_owned();
let mut p = Photo::new(id, Source::File { path }, "IMG_3.NEF", "NEF", 48, 32, "2026-01-14T05:58:48");
p.kind = MediaKind::Raw;
p
};
s.catalog.apply(Op::AddPhoto { photo: Box::new(raw) }).unwrap();
let r = s.execute("library.import", &json!({"paths": [src.join("IMG_3.TIF").to_string_lossy()]})).unwrap();
assert_eq!((ids(&r, "imported"), ids(&r, "failed")), (1, 0), "{r}");
assert_eq!(r["sidecars"], 0, "{r}");
assert_eq!(read(&s, "IMG_3.TIF"), 0, "the catalogued raw owns IMG_3.xmp");
let _ = std::fs::remove_dir_all(&src);
}
#[test]
fn heic_exif_reaches_the_catalog_and_all_metadata() {
use craft_heif::testdata::{Spec, build};
let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150];
let exif = lightcraft_meta::write_exif(&lightcraft_meta::Metadata {
make: Some("Synthetic".into()),
model: Some("Issue sweep camera".into()),
capture_time: lightcraft_meta::DateTime::parse_iso("2026-10-12T12:34:56"),
..Default::default()
});
let bytes = build(&Spec { exif: Some(exif), ..Spec::new(96, 64, &photo) });
let m = lightcraft_meta::extract(&bytes);
assert_eq!(m.model.as_deref(), Some("Issue sweep camera"));
assert_eq!(m.capture_time.map(|d| d.to_iso()).as_deref(), Some("2026-10-12T12:34:56"));
if !lightcraft_codecs::Format::Heif.can_decode() {
return; // importing needs the decoder; `heic_imports_as_a_normal_photo_or_says_why_not` checks the error
}
let src = temp_dir("heic-exif");
std::fs::write(src.join("IMG_0002.HEIC"), &bytes).unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!(ids(&r, "imported"), 1, "{r}");
let p = s.catalog.photos().next().unwrap().clone();
assert_eq!((p.meta.camera.as_str(), p.captured.as_deref()), ("Synthetic Issue sweep camera", Some("2026-10-12T12:34:56")));
let all = s.execute("photo.allMetadata", &json!({"id": p.id.0})).unwrap();
assert!(all["exif"].as_array().unwrap().iter().any(|r| r["name"] == "Model" && r["value"] == "Issue sweep camera"), "{all}");
let _ = std::fs::remove_dir_all(&src);
}
fn heic_camera_exif(date: &str) -> Vec<u8> {
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
let mut exif = IfdBuilder::new();
exif.set(t::DATE_TIME_ORIGINAL, Value::Ascii(date.into()));
exif.set(t::OFFSET_TIME_ORIGINAL, Value::Ascii("-05:00".into()));
let mut ifd0 = IfdBuilder::new();
ifd0.set(t::MAKE, Value::Ascii("Apple".into()));
ifd0.set(t::MODEL, Value::Ascii("iPhone 15 Pro".into()));
ifd0.set(t::ORIENTATION, Value::Short(vec![6]));
ifd0.set_child(t::EXIF_IFD, exif);
TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap()
}
#[test]
fn heic_exif_orientation_does_not_rotate_the_picture_twice() {
use craft_heif::testdata::{Prop, Spec, build};
let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150];
for (props, size) in [(vec![], (96, 64)), (vec![Prop::Irot(3)], (64, 96))] {
let plain = build(&Spec { props: props.clone(), ..Spec::new(96, 64, &photo) });
let bytes = build(&Spec { props, exif: Some(heic_camera_exif("2026:10:18 13:00:00")), ..Spec::new(96, 64, &photo) });
assert_eq!(lightcraft_meta::extract(&bytes).orientation, Some(lightcraft_geom::Orientation::Rotate90));
if lightcraft_codecs::Format::Heif.can_decode() {
let header = lightcraft_codecs::read_header(&bytes).unwrap();
assert_eq!((header.width, header.height, header.orientation), (size.0, size.1, 1));
let info = crate::files::probe_bytes("rotated.HEIC", &bytes).unwrap();
assert_eq!((info.width, info.height), size);
let (image, _) = crate::files::load_bytes(&bytes, 96).unwrap();
let (without_exif, _) = crate::files::load_bytes(&plain, 96).unwrap();
assert_eq!((image.width, image.height), (size.0 as usize, size.1 as usize));
assert_eq!(image.data, without_exif.data, "Exif must not turn the decoded pixels again");
} else {
assert!(crate::files::probe_bytes("rotated.HEIC", &bytes).unwrap_err().contains("isn't included in this build"));
assert!(crate::files::load_bytes(&bytes, 96).unwrap_err().contains("isn't included in this build"));
}
}
}
/// A batch-edit recipe changes the imported photos' actual develop settings (not just an option
/// label), renders, is one undo step, and can be saved as a preset that stores only what it names.
#[test]
fn a_batch_edit_recipe_changes_imported_photos() {
let src = temp_dir("recipe");
write_png(&src.join("a.png"), 1);
write_png(&src.join("b.png"), 2);
let mut s = Session::new().with_fs();
let r = s
.execute(
"library.import",
&json!({
"paths": [src.to_string_lossy()],
"recipe": [{"id": "light.exposure", "value": 0.5}, {"id": "color.vibrance", "value": 20}],
}),
)
.unwrap();
assert_eq!(ids(&r, "imported"), 2, "{r}");
for p in s.catalog.photos() {
assert_eq!(p.develop.light.exposure, 0.5, "{}", p.file_name);
assert_eq!(p.develop.color.vibrance, 20.0, "{}", p.file_name);
assert!(p.history.iter().any(|h| h.label == "Import adjustments"), "{}", p.file_name);
}
// the edit really renders (the recipe is applied to the settings, not only recorded)
let id = s.catalog.photos().next().unwrap().id;
assert!(s.render_now(id, 32, 32).is_ok());
// saving the recipe as a preset keeps only the settings it names
let p = s.execute("preset.saveRecipe", &json!({"name": "Warm Trip", "recipe": [{"id": "light.exposure", "value": 0.5}]})).unwrap();
let saved = s.presets.iter().find(|x| x.id == p["id"].as_str().unwrap()).unwrap();
assert_eq!(saved.settings.pointer("/light/exposure"), Some(&json!(0.5)), "{}", saved.settings);
assert!(saved.settings.get("color").is_none(), "an unnamed group is not stored: {}", saved.settings);
// one undo step for the whole import
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.catalog.len(), 0);
let _ = std::fs::remove_dir_all(&src);
}
/// A malformed recipe, or one naming a control a recipe may not name, is refused before anything
/// is imported, and by the command that saves one: unknown ids, controls that belong to one photo,
/// geometry and crop controls (the slider refits the crop around them), white balance (a
/// temperature means something else on each kind of file), and lists longer than the control list.
#[test]
fn a_bad_recipe_is_refused() {
let mut s = Session::new().with_fs();
let err = s.execute("library.import", &json!({"paths": ["/nonexistent-lc"], "recipe": [{"id": "nope.nope", "value": 1}]})).unwrap_err();
assert!(format!("{err}").contains("unknown recipe control"), "{err}");
let err = s.execute("library.import", &json!({"paths": ["/nonexistent-lc"], "recipe": "x"})).unwrap_err();
assert!(format!("{err}").contains("recipe"), "{err}");
assert_eq!(s.catalog.len(), 0);
let too_many: Vec<Value> = (0..lightcraft_develop::controls::CONTROLS.len() + 1).map(|_| json!({"id": "light.exposure", "value": 1})).collect();
for (bad, why) in [
(json!([{"value": 1}]), "needs an `id`"),
(json!([{"id": "light.exposure"}]), "numeric"),
(json!([{"id": "light.exposure", "value": "high"}]), "numeric"),
(json!([7]), "needs an `id`"),
(json!([{"id": "pointColor.0.hueShift", "value": 5}]), "unknown recipe control"),
(json!([{"id": "redEye.0.pupilSize", "value": 5}]), "unknown recipe control"),
(json!([{"id": "crop.angle", "value": 5}]), "geometry"),
(json!([{"id": "geometry.rotate", "value": 5}]), "geometry"),
(json!([{"id": "wb.temp", "value": 5200}]), "white balance"),
(json!([{"id": "wb.tint", "value": 5}]), "white balance"),
(json!([{"id": "light.exposure", "value": 1}, {"id": "crop.angle", "value": 5}]), "geometry"),
(json!(too_many), "at most"),
] {
let err = s.execute("library.import", &json!({"paths": ["/nonexistent-lc"], "recipe": bad})).unwrap_err().to_string();
assert!(err.contains(why), "{why}: {err}");
let before = s.presets.len();
let err = s.execute("preset.saveRecipe", &json!({"name": "Bad", "recipe": bad})).unwrap_err().to_string();
assert!(err.contains(why), "{why}: {err}");
assert_eq!(s.presets.len(), before, "nothing saved for {why}");
}
// the job itself refuses what the command would (a recipe built in code, not from JSON)
let opts = crate::import::ImportOptions { recipe: vec![("crop.angle".into(), 5.0)], ..Default::default() };
assert!(crate::import::ImportJob::new(&mut s, opts).is_err());
// no name, a name that is too long, or nothing in the recipe: no preset
let one = json!([{"id": "light.exposure", "value": 1}]);
for params in [json!({"recipe": one}), json!({"name": " ", "recipe": one}), json!({"name": "x".repeat(101), "recipe": one})] {
assert!(s.execute("preset.saveRecipe", ¶ms).is_err(), "{params}");
}
assert!(s.execute("preset.saveRecipe", &json!({"name": "x".repeat(100), "recipe": one})).is_ok(), "a name of 100 characters is fine");
assert!(s.execute("preset.saveRecipe", &json!({"name": "Empty", "recipe": []})).is_err());
assert!(s.execute("preset.saveRecipe", &json!({"name": "Empty"})).is_err());
assert!(!s.presets.iter().any(|p| p.name == "Empty"));
}
/// The recipe's values go through the control specs like a slider's: past the range they stop at
/// it, for the photo and for the preset. A control named twice counts once, the last value wins.
#[test]
fn recipe_values_are_clamped_like_a_slider() {
let src = temp_dir("recipe-clamp");
write_png(&src.join("a.png"), 1);
let mut s = Session::new().with_fs();
let recipe = json!([{"id": "light.exposure", "value": 1}, {"id": "light.exposure", "value": 99}, {"id": "light.contrast", "value": -1000}]);
s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "recipe": recipe})).unwrap();
let p = s.catalog.photos().next().unwrap();
assert_eq!((p.develop.light.exposure, p.develop.light.contrast), (5.0, -100.0));
let saved = s.execute("preset.saveRecipe", &json!({"name": "Clamped", "recipe": recipe})).unwrap();
let saved = s.presets.iter().find(|x| x.id == saved["id"].as_str().unwrap()).unwrap();
assert_eq!(saved.settings.pointer("/light/exposure"), Some(&json!(5.0)), "{}", saved.settings);
assert_eq!(saved.settings.pointer("/light/contrast"), Some(&json!(-100.0)), "{}", saved.settings);
let _ = std::fs::remove_dir_all(&src);
}
/// A session whose files probe as DNG raws shot at 4800 K, tint +12 (the pixels stay the PNG's;
/// nothing here renders them), to see what an import does to a raw's as-shot white balance.
fn session_with_raw_probe() -> Session {
let mut s = Session::new().with_fs();
let real = s.media.file_probe.clone().unwrap();
s.media.file_probe = Some(std::sync::Arc::new(move |path: &str| {
let mut info = real(path)?;
info.kind = lightcraft_catalog::MediaKind::Raw;
info.format = "DNG".into();
info.as_shot_wb = Some((4800.0, 12.0));
Ok(info)
}));
s
}
fn import_one(s: &mut Session, path: &Path, recipe: Value) -> lightcraft_catalog::PhotoId {
let r = s.execute("library.import", &json!({"paths": [path.to_string_lossy()], "recipe": recipe})).unwrap();
lightcraft_catalog::PhotoId(r["imported"][0].as_u64().unwrap_or_else(|| panic!("{path:?} not imported: {r}")))
}
fn white_balance(s: &Session, id: lightcraft_catalog::PhotoId) -> lightcraft_develop::WhiteBalance {
s.catalog.photo(id).unwrap().develop.wb
}
/// A recipe never touches white balance: a raw keeps the as-shot temperature, tint and scale it was
/// imported with, whatever else the recipe sets, and a recipe cannot name a temperature or tint.
#[test]
fn a_recipe_leaves_a_raws_as_shot_white_balance_alone() {
use lightcraft_develop::{WbMode, WbScale};
let src = temp_dir("recipe-wb");
write_png(&src.join("a.png"), 1);
write_png(&src.join("b.png"), 2);
let mut s = session_with_raw_probe();
let plain = import_one(&mut s, &src.join("a.png"), json!([]));
let w = white_balance(&s, plain);
assert_eq!((w.mode, w.temp, w.tint, w.scale), (WbMode::AsShot, 4800.0, 12.0, WbScale::Kelvin), "{w:?}");
let with = import_one(&mut s, &src.join("b.png"), json!([{"id": "light.exposure", "value": 0.5}, {"id": "color.vibrance", "value": 30}]));
assert_eq!(white_balance(&s, with), w, "the as-shot white balance is as the import left it");
assert_eq!(s.catalog.photo(with).unwrap().develop.light.exposure, 0.5);
for id in ["wb.temp", "wb.tint"] {
let err = s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "recipe": [{"id": id, "value": 5200}]})).unwrap_err();
assert!(err.to_string().contains("white balance"), "{err}");
}
let _ = std::fs::remove_dir_all(&src);
}
/// Saved as a preset and applied to a photo as it was imported, a recipe gives the settings
/// importing with it gives (for a raw too), and the preset holds a control the recipe sets to its
/// default: that is a reset, not "nothing named".
#[test]
fn a_recipe_saved_as_a_preset_gives_what_the_recipe_gives() {
let src = temp_dir("recipe-preset");
let recipes = [
json!([{"id": "light.exposure", "value": 0.5}, {"id": "color.vibrance", "value": 20}]),
json!([{"id": "light.exposure", "value": 99}]),
json!([{"id": "detail.sharpenAmount", "value": 0}, {"id": "grain.amount", "value": 30}]),
json!([{"id": "vignette.amount", "value": -25}, {"id": "effects.clarity", "value": 15}, {"id": "mixer.red.hue", "value": 10}]),
];
let mut s = session_with_raw_probe();
for (i, recipe) in recipes.iter().enumerate() {
write_png(&src.join(format!("with{i}.png")), 2 * i as u8);
write_png(&src.join(format!("plain{i}.png")), 2 * i as u8 + 1);
let with = import_one(&mut s, &src.join(format!("with{i}.png")), recipe.clone());
let plain = import_one(&mut s, &src.join(format!("plain{i}.png")), json!([]));
let saved = s.execute("preset.saveRecipe", &json!({"name": format!("Recipe {i}"), "recipe": recipe})).unwrap();
s.selection = crate::Selection::single(plain);
s.execute("preset.apply", &json!({"id": saved["id"], "ids": [plain.0]})).unwrap();
let (with, plain) = (s.catalog.photo(with).unwrap(), s.catalog.photo(plain).unwrap());
assert_eq!(with.develop, plain.develop, "recipe {recipe}");
}
// the sharpening reset (a raw's default is 40) is in the preset although 0 is the control's default
let reset = s.presets.iter().find(|p| p.name == "Recipe 2").unwrap();
assert_eq!(reset.settings.pointer("/detail/sharpen_amount"), Some(&json!(0.0)), "{}", reset.settings);
assert_eq!(s.catalog.photos().find(|p| p.file_name == "with2.png").unwrap().develop.detail.sharpen_amount, 0.0);
let _ = std::fs::remove_dir_all(&src);
}
/// The recipe applies to the photos an import adds. A file the library has (by path or by
/// content), a trashed one that comes back, and a browsed one that joins keep their own
/// settings; a trashed one imported afresh is a new photo and gets the recipe.
#[test]
fn a_recipe_only_touches_the_photos_an_import_adds() {
let src = temp_dir("recipe-skipped");
write_png(&src.join("a.png"), 1);
write_png(&src.join("b.png"), 2);
write_png(&src.join("c.png"), 3);
std::fs::copy(src.join("a.png"), src.join("a-copy.png")).unwrap(); // same bytes as a
let recipe = json!([{"id": "color.vibrance", "value": 20}]);
let adjusted = |s: &Session, name: &str| {
let p = s.catalog.photos().find(|p| p.file_name == name && !p.deleted).unwrap_or_else(|| panic!("no {name}"));
(p.develop.color.vibrance, p.history.iter().any(|h| h.label == "Import adjustments"))
};
let mut s = Session::new().with_fs();
let first = import_one(&mut s, &src.join("a.png"), json!([]));
s.selection = crate::Selection::single(first);
s.execute("develop.set", &json!({"control": "light.exposure", "value": 1.5})).unwrap();
let a_before = s.catalog.photo(first).unwrap().develop.clone();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()], "recipe": recipe})).unwrap();
assert_eq!(ids(&r, "imported"), 2, "{r}"); // b and c
assert_eq!(ids(&r, "duplicates"), 2, "{r}"); // a (same path) and a-copy (same content)
assert_eq!(adjusted(&s, "b.png"), (20.0, true));
assert_eq!(adjusted(&s, "c.png"), (20.0, true));
assert_eq!(adjusted(&s, "a.png"), (0.0, false), "a duplicate is not edited");
assert_eq!(s.catalog.photo(first).unwrap().develop, a_before, "nor is the photo it duplicates");
assert_eq!(s.catalog.len(), 3);
// in Recently Deleted: restored with its own settings, no recipe
s.execute("photo.delete", &json!({"ids": [first.0]})).unwrap();
let r = s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()], "recipe": recipe})).unwrap();
assert_eq!((ids(&r, "imported"), ids(&r, "restored")), (0, 1), "{r}");
assert_eq!(s.catalog.photo(first).unwrap().develop, a_before, "a restored photo keeps its edits");
assert!(!s.catalog.photo(first).unwrap().history.iter().any(|h| h.label == "Import adjustments"));
// …imported afresh, it is a new photo and gets the recipe
s.execute("photo.delete", &json!({"ids": [first.0]})).unwrap();
let r = s.execute("library.import", &json!({"paths": [src.join("a.png").to_string_lossy()], "recipe": recipe, "onDeleted": "fresh"})).unwrap();
assert_eq!(ids(&r, "imported"), 1, "{r}");
assert_eq!(adjusted(&s, "a.png"), (20.0, true));
// browsed first (Local), then imported: the record joins the library as it is, as it does for
// a preset or keywords
let other = temp_dir("recipe-browsed");
write_png(&other.join("d.png"), 4);
s.execute("library.browse", &json!({"path": other.to_string_lossy()})).unwrap();
let r = s.execute("library.import", &json!({"paths": [other.join("d.png").to_string_lossy()], "recipe": recipe})).unwrap();
assert_eq!(ids(&r, "imported"), 1, "{r}");
assert_eq!(adjusted(&s, "d.png"), (0.0, false), "a browsed photo that joins keeps its own settings");
let _ = std::fs::remove_dir_all(&src);
let _ = std::fs::remove_dir_all(&other);
}
/// A saved recipe never takes another preset's id: with presets A and B, A deleted, a recipe saved
/// as "B" is its own preset and applying it applies the recipe, not the older B. Saving the same
/// recipe under the same name again is that preset, and the presets are in presets.json.
#[test]
fn saved_recipe_presets_get_distinct_ids_and_survive_reopening() {
let lib = temp_dir("recipe-presets-lib");
let src = temp_dir("recipe-presets-src");
write_png(&src.join("a.png"), 1);
let mut s = Session::new().with_fs();
s.open_library(&lib, false).unwrap();
let photo = import_one(&mut s, &src.join("a.png"), json!([]));
s.selection = crate::Selection::single(photo);
let create = |s: &mut Session, name: &str| s.execute("preset.create", &json!({"name": name})).unwrap()["id"].as_str().unwrap().to_string();
let (a, old_b) = (create(&mut s, "A"), create(&mut s, "B"));
s.execute("preset.delete", &json!({"id": a})).unwrap();
let recipe = json!([{"id": "light.exposure", "value": 0.7}]);
let saved = s.execute("preset.saveRecipe", &json!({"name": "B", "group": "User Presets", "recipe": recipe})).unwrap();
let new_b = saved["id"].as_str().unwrap().to_string();
assert_ne!(new_b, old_b);
let mut all: Vec<&str> = s.presets.iter().map(|p| p.id.as_str()).collect();
let n = all.len();
all.sort();
all.dedup();
assert_eq!(all.len(), n, "every preset has its own id");
s.execute("preset.apply", &json!({"id": new_b})).unwrap();
assert_eq!(s.catalog.photo(photo).unwrap().develop.light.exposure, 0.7, "the saved recipe is what gets applied");
// the same recipe, name and group: the preset it already is
let again = s.execute("preset.saveRecipe", &json!({"name": "B", "group": "User Presets", "recipe": recipe})).unwrap();
assert_eq!(again["id"], saved["id"]);
assert_eq!(s.presets.iter().filter(|p| p.name == "B").count(), 2);
drop(s);
let mut t = Session::new().with_fs();
t.open_library(&lib, false).unwrap();
let mut bs: Vec<&str> = t.presets.iter().filter(|p| p.name == "B").map(|p| p.id.as_str()).collect();
bs.sort();
let mut expected = [old_b.as_str(), new_b.as_str()];
expected.sort();
assert_eq!(bs, expected, "both are still there after reopening");
assert!(!t.presets.iter().any(|p| p.name == "A"), "the deleted one stays deleted");
drop(t);
let _ = std::fs::remove_dir_all(&lib);
let _ = std::fs::remove_dir_all(&src);
}
/// Both commands are in the command list with their parameters documented, which is where the
/// control channel, the CLI and MCP's `list_commands` read them.
#[test]
fn the_recipe_commands_are_listed_with_their_parameters() {
let s = Session::new();
let commands = s.commands();
let import = commands.iter().find(|c| c.id == "library.import").unwrap();
assert!(import.params.contains("recipe?: [{id:") && import.params.contains("Import adjustments"), "{}", import.params);
assert!(import.params.contains("no white balance, geometry or crop") && import.params.contains("is not changed"), "{}", import.params);
let save = commands.iter().find(|c| c.id == "preset.saveRecipe").expect("preset.saveRecipe is a command");
assert!(save.enabled && save.params.contains("recipe: [{id:") && save.params.contains("name"), "{}", save.params);
}
/// A saved recipe that resets an HDR control to its default is applied as the recipe would apply it:
/// alone, and next to another adjustment, on a photo whose HDR settings are not the default.
#[test]
fn a_saved_hdr_reset_applies_to_existing_hdr_settings() {
let mut s = Session::new();
for recipe in [json!([{"id": "hdr.maxEv", "value": 3}]), json!([{"id": "hdr.maxEv", "value": 3}, {"id": "light.exposure", "value": 0.5}])] {
let p = s.execute("preset.saveRecipe", &json!({"name": format!("Reset {recipe}"), "recipe": recipe})).unwrap();
let saved = s.presets.iter().find(|x| x.id == p["id"].as_str().unwrap()).unwrap().settings.clone();
let mut base = lightcraft_develop::DevelopSettings::default();
base.hdr.enabled = true;
base.hdr.max_ev = 1.5;
let out = lightcraft_develop::apply_partial(&base, &saved, 1.0);
assert_eq!(out.hdr.max_ev, 3.0, "{saved}");
assert!(out.hdr.enabled, "the reset leaves the other HDR settings alone: {saved}");
let want_exposure = if recipe.as_array().unwrap().len() > 1 { 0.5 } else { base.light.exposure };
assert_eq!(out.light.exposure, want_exposure, "{saved}");
}
}
/// A folder's one spelling: `.` is the current folder, `.` and `..` are taken out, never above the
/// root (CodeRabbit on #3055 / #3080).
#[test]
fn a_browsed_folder_has_one_spelling() {
use crate::import::browse_path;
let root = std::env::temp_dir();
assert_eq!(browse_path(&root.join("lib").join("..").join("outside")).unwrap(), root.join("outside"));
assert_eq!(browse_path(&root.join(".").join("a").join(".").join("b")).unwrap(), root.join("a").join("b"));
let top: PathBuf = root.ancestors().last().map(Path::to_path_buf).unwrap_or_default();
assert_eq!(browse_path(&top.join("..").join("x")).unwrap(), top.join("x"), "never above the root");
assert_eq!(browse_path(Path::new(".")).unwrap(), std::env::current_dir().unwrap(), "`.` is the current folder");
assert_eq!(browse_path(Path::new("a/..")).unwrap(), std::env::current_dir().unwrap());
// the UI's spelling, without the disk: the same where no link comes before a `..`
let by_name = crate::import::browse_spelling(&root.join("lib").join("..").join("outside")).unwrap();
assert_eq!(by_name, root.join("outside"));
assert_eq!(crate::import::browse_spelling(Path::new(".")).unwrap(), std::env::current_dir().unwrap());
}
/// `link/..` is the folder the system reads (POSIX: above where the link points; Windows: the
/// folder holding the link): browsing it lists what the system lists there (CodeRabbit on #3055 /
/// #3077 / #3080).
#[test]
fn a_link_before_dotdot_names_the_folder_the_system_reads() {
let dir = temp_dir("browse-link-dotdot");
std::fs::create_dir_all(dir.join("real").join("sub")).unwrap();
write_png(&dir.join("real").join("in-real.png"), 1);
write_png(&dir.join("beside-link.png"), 2);
let link = dir.join("link");
#[cfg(unix)]
let made = std::os::unix::fs::symlink(dir.join("real").join("sub"), &link);
#[cfg(windows)]
let made = std::os::windows::fs::symlink_dir(dir.join("real").join("sub"), &link);
if let Err(e) = made {
lightcraft_testkit::skip!(System, "can't make a folder link here: {e}");
let _ = std::fs::remove_dir_all(&dir);
return;
}
let mut s = Session::new().with_fs();
let (input, _) = crate::import::ScanInput::new(&mut s, &[]);
let through = link.join("..");
let names = |v: Vec<String>| v.iter().map(|f| Path::new(f).file_name().unwrap().to_string_lossy().to_string()).collect::<Vec<_>>();
let listed = names(crate::import::browse_files(&input, &through.to_string_lossy(), false).unwrap());
let mut system: Vec<String> =
std::fs::read_dir(&through).unwrap().flatten().map(|e| e.file_name().to_string_lossy().to_string()).filter(|n| n.ends_with(".png")).collect();
system.sort();
assert_eq!(listed, system, "{}", through.display());
#[cfg(unix)]
assert_eq!(listed, ["in-real.png"]);
let _ = std::fs::remove_dir_all(&dir);
}
/// A broken link before `..` is an error, not the link's own folder by name: the system can't read
/// `broken/..` either (CodeRabbit on #3577).
#[cfg(unix)]
#[test]
fn a_broken_link_before_dotdot_is_an_error() {
let dir = temp_dir("browse-broken-link-dotdot");
std::fs::create_dir_all(&dir).unwrap();
let link = dir.join("broken");
std::os::unix::fs::symlink(dir.join("nowhere"), &link).unwrap();
assert!(crate::import::browse_path(&link.join("..")).is_err());
// without reading the disk the spelling is still the folder by name
assert_eq!(crate::import::browse_spelling(&link.join("..")).unwrap(), dir);
let _ = std::fs::remove_dir_all(&dir);
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.