Files
bflatastic fd9546a177 figcraft-cli diff: compare what two files hold, so a round trip can be checked in CI (#2384)
`figcraft-cli diff A B [--json]` counts layers by type, fills, strokes,
effects, layout grids, auto layout frames, export settings, prototype
interactions and flows, variable bindings, style links, styles by kind,
variables, collections, modes, images and comments in both files and
prints the counts that differ. It exits 1 when one differs, so a job
can run `convert in.fig out.fig` and `diff in.fig out.fig` and fail on
anything the save dropped. Takes the automation roots like `info`.
2026-12-01 13:49:02 +07:00

54 lines
2.5 KiB
Rust

//! `diff`: two files' counts by kind, and an exit status a CI job can test (#2384).
use std::process::Command;
use figcraft_doc::proto::{Action, Easing, Interaction, Transition, Trigger};
use figcraft_doc::{Document, NodeKind};
fn bin() -> Command {
let mut command = Command::new(env!("CARGO_BIN_EXE_figcraft-cli"));
command.env("CRAFT_FLAGS", "-AI-MCP");
command
}
/// A page with two frames, the first linking to the second.
fn prototype() -> Document {
let mut doc = Document::new("Prototype");
let page = doc.first_page().unwrap();
let home = doc.create(page, NodeKind::Frame, "Home", 0.0, 0.0, 40.0, 40.0).unwrap();
let dest = doc.create(page, NodeKind::Frame, "Next", 60.0, 0.0, 40.0, 40.0).unwrap();
doc.node_mut(home).unwrap().interactions.push(Interaction {
trigger: Trigger::OnClick,
actions: vec![Action::Navigate { dest, preserve_scroll: false }],
transition: Transition::Instant,
duration_ms: 0,
easing: Easing::Linear,
delay_ms: 0,
});
doc
}
#[test]
fn same_files_pass_and_a_lost_interaction_fails_with_both_counts() {
let d = std::env::temp_dir().join(format!("figcraft-cli-test-{}-diff", std::process::id()));
std::fs::create_dir_all(&d).unwrap();
let mut doc = prototype();
let (a, b) = (d.join("a.figcraft"), d.join("b.figcraft"));
std::fs::write(&a, figcraft_format::save_bytes(&doc, None).unwrap()).unwrap();
std::fs::write(&b, figcraft_format::save_bytes(&doc, None).unwrap()).unwrap();
let same = bin().arg("diff").arg(&a).arg(&b).output().unwrap();
assert!(same.status.success(), "{}", String::from_utf8_lossy(&same.stderr));
assert!(String::from_utf8_lossy(&same.stdout).starts_with("same counts"));
let home = doc.nodes().find(|n| !n.interactions.is_empty()).map(|n| n.id).unwrap();
doc.node_mut(home).unwrap().interactions.clear();
std::fs::write(&b, figcraft_format::save_bytes(&doc, None).unwrap()).unwrap();
let lost = bin().arg("diff").arg(&a).arg(&b).output().unwrap();
assert_eq!(lost.status.code(), Some(1));
assert!(String::from_utf8_lossy(&lost.stdout).contains("interactions: 1 → 0 (-1)"));
let json = bin().args(["diff", "--json"]).arg(&a).arg(&b).output().unwrap();
let v: serde_json::Value = serde_json::from_slice(&json.stdout).unwrap();
assert_eq!((v["same"].as_bool(), v["changed"].as_object().map(|c| c.len())), (Some(false), Some(1)));
assert_eq!(v["changed"]["interactions"], serde_json::json!({"a": 1, "b": 0}));
let _ = std::fs::remove_dir_all(d);
}