M3: a style or variable applied or detached inside an instance counts nothing

counting_links compared library_links, which walked every node, so applying or
detaching a library style or variable on an instance or a layer in it counted,
while duplicating that instance counts only its component. library_links now
walks the pages and skips instances with what they hold, as count_inserted does.
Test: library_asset_counts_tests::applying_and_detaching_inside_an_instance_counts_nothing
(an instance, its layer, a nested instance and its layer). The server test waits
for the detachments too, not only the insertions.
This commit is contained in:
bflatastic committed 2026-11-18 19:38:54 +07:00
1 parent 64b662b869
commit b63368e39c
3 files changed
+58 -5

No files matched your search

@@ -165,7 +165,12 @@ fn inserts_and_detaches_count_per_style_and_variable_in_the_period() {
ex(&mut b, "style.detach", json!({"ids": [two.0], "which": "fill"}));
ex(&mut b, "library.bindVariable", json!({"library": lid, "variable": "Accent", "ids": [two.0], "field": "fill"}));
let u = usage_when(&mut a, &mut b, |u| u["styles"][0]["insertions"] == 2 && u["variables"][0]["insertions"] == 1);
let u = usage_when(&mut a, &mut b, |u| {
u["assetActionsKnown"] == true
&& u["styles"][0]["insertions"] == 2
&& u["styles"][0]["detachments"] == 1
&& u["variables"][0]["insertions"] == 1
});
assert_eq!(u["assetActionsKnown"], true, "{u}");
assert_eq!(u["styles"][0]["detachments"], 1, "{u}");
assert_eq!(u["variables"][0]["detachments"], 0, "{u}");
+12 -3
View File
@@ -93,14 +93,23 @@ fn asset_record<'a>(doc: &'a Document, local: &str) -> Option<&'a ImportedRecord
doc.libraries.styles.iter().chain(&doc.libraries.variables).find(|r| r.local == local)
}
/// How many layers link to each local copy of a library style or variable.
/// How many layers link to each local copy of a library style or variable. An instance and the
/// layers in it don't count: an instance counts as its component alone, as in [`count_inserted`].
fn library_links(doc: &Document) -> BTreeMap<String, u32> {
let mut links = BTreeMap::new();
if doc.libraries.styles.is_empty() && doc.libraries.variables.is_empty() {
return links;
}
for local in doc.nodes().flat_map(asset_links).filter(|l| asset_record(doc, l).is_some()) {
*links.entry(local.to_string()).or_default() += 1;
let mut stack = doc.pages.clone();
while let Some(id) = stack.pop() {
let Some(n) = doc.node(id) else { continue };
if matches!(n.kind, NodeKind::Instance { .. }) {
continue;
}
stack.extend(n.children.iter().copied());
for local in asset_links(n).into_iter().filter(|l| asset_record(doc, l).is_some()) {
*links.entry(local.to_string()).or_default() += 1;
}
}
links
}
@@ -8,7 +8,7 @@ use serde_json::{Value, json};
use crate::Session;
use crate::components::tests::{ex, idv};
use crate::library_server::asset_actions;
use figcraft_doc::{AssetCount, NodeId, PublishedLibrary};
use figcraft_doc::{AssetCount, NodeId, NodeKind, PublishedLibrary};
/// A file with a published library holding a paint style, a colour variable and a Card, and a
/// second file that uses it: the second session, the library's id and its keys.
@@ -81,6 +81,45 @@ fn an_instance_counts_as_its_component_alone() {
assert_eq!(s.doc().unwrap().libraries.inserted[0].count, 2);
}
/// Applying and detaching a library style or variable on an instance, on a layer in it, or on a
/// layer of an instance nested in it counts nothing either: what an instance uses counts as it.
#[test]
fn applying_and_detaching_inside_an_instance_counts_nothing() {
let (mut s, lib, _) = user();
let placed = idv(&ex(&mut s, "library.place", json!({"library": lib, "component": "Card", "x": 0, "y": 0})));
// A Button holding a rectangle and an Icon instance (the Icon holds a dot), and a Button instance.
let icon = idv(&ex(&mut s, "frame.create", json!({"x": 0, "y": 200, "width": 24, "height": 24, "name": "Icon"})));
ex(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 8, "height": 8, "parent": icon.0}));
let icon = idv(&ex(&mut s, "component.create", json!({"ids": [icon.0]})));
let button = idv(&ex(&mut s, "frame.create", json!({"x": 100, "y": 200, "width": 80, "height": 40, "name": "Button"})));
ex(&mut s, "shape.rectangle", json!({"x": 0, "y": 0, "width": 80, "height": 40, "parent": button.0}));
ex(&mut s, "instance.create", json!({"component": icon.0, "parent": button.0, "x": 4, "y": 4}));
let button = idv(&ex(&mut s, "component.create", json!({"ids": [button.0]})));
let i = idv(&ex(&mut s, "instance.create", json!({"component": button.0, "x": 300, "y": 200})));
let (layer, nested, deep) = {
let d = s.doc().unwrap();
let kids = |id: NodeId| d.node(id).unwrap().children.clone();
let nested = kids(i).into_iter().find(|k| matches!(d.node(*k).unwrap().kind, NodeKind::Instance { .. })).unwrap();
let layer = kids(i).into_iter().find(|k| *k != nested).unwrap();
(layer, nested, kids(nested)[0])
};
let targets = [placed.0, i.0, layer.0, nested.0, deep.0];
ex(&mut s, "library.applyStyle", json!({"library": lib, "style": "Brand", "ids": targets}));
ex(&mut s, "library.bindVariable", json!({"library": lib, "variable": "Accent", "ids": targets, "field": "stroke"}));
let d = s.doc().unwrap();
assert!(d.node(deep).unwrap().styles.fill.is_some() && !d.node(deep).unwrap().bound.is_empty(), "the nested layer took both");
ex(&mut s, "style.detach", json!({"ids": targets, "which": "fill"}));
ex(&mut s, "variable.bind", json!({"ids": targets, "field": "stroke"}));
assert_eq!(counts(&s), ((0, 0), (0, 0)));
// The same on a plain layer counts.
let plain = rect(&mut s, 600);
ex(&mut s, "library.applyStyle", json!({"library": lib, "style": "Brand", "ids": [plain.0]}));
ex(&mut s, "library.bindVariable", json!({"library": lib, "variable": "Accent", "ids": [plain.0], "field": "stroke"}));
ex(&mut s, "style.detach", json!({"ids": [plain.0], "which": "fill"}));
ex(&mut s, "variable.bind", json!({"ids": [plain.0], "field": "stroke"}));
assert_eq!(counts(&s), ((1, 1), (1, 1)));
}
#[test]
fn only_the_detach_buttons_detach() {
let (mut s, lib, _) = user();