diff --git a/app/test/support/memory_backend.dart b/app/test/support/memory_backend.dart index e940ba2..eae55ea 100644 --- a/app/test/support/memory_backend.dart +++ b/app/test/support/memory_backend.dart @@ -926,23 +926,35 @@ class MemoryBackend implements GraphBackend { t.add(index[m]!); k.add(EdgeKind.contains.index); } - // Tags are virtual nodes after the pages; a page points at each of its tags. + // Tags are virtual nodes after the pages; a page points at each of its tags. A tag + // named like a hub uses the hub's node, and its members (already contained) skip it. + final hubNodes = { + for (final id in ids) + if (_isHub(id)) _tagKey(pages[id]!.title): index[id]!, + }; final tagNames = {}; for (final p in pages.values) { for (final tg in allTags(p)) { + if (hubNodes.containsKey(_tagKey(tg))) continue; tagNames.putIfAbsent(_tagKey(tg), () => tg); } } final tagKeys = tagNames.keys.toList()..sort(); final tagIndex = { + ...hubNodes, for (var j = 0; j < tagKeys.length; j++) tagKeys[j]: ids.length + j, }; + final contained = { + for (final (h, m) in containsEdges) (index[h]!, index[m]!), + }; for (final p in pages.values) { final i = index[p.id]; if (i == null) continue; for (final tg in allTags(p)) { + final to = tagIndex[_tagKey(tg)]!; + if (to == i || contained.contains((to, i))) continue; s.add(i); - t.add(tagIndex[_tagKey(tg)]!); + t.add(to); k.add(EdgeKind.tagged.index); } } diff --git a/app/test/tags_graph_test.dart b/app/test/tags_graph_test.dart index c40449c..168b7b6 100644 --- a/app/test/tags_graph_test.dart +++ b/app/test/tags_graph_test.dart @@ -81,7 +81,7 @@ void main() { expect(() => b.setPageTags(j.id, ['x']), throwsException); }); - test('a hub is an implicit tag of its members, merged once', () async { + test('a hub is an implicit tag of its members, shown as the hub', () async { final b = MemoryBackend(); final media = b.createPage('Media'); b.makeHub(media.id); @@ -100,17 +100,10 @@ void main() { expect(cat.single.count, 2); expect(b.pages[a.id]!.tags, isEmpty); + // In the graph the hub stands for its tag: no tag node, members only contained. final data = await b.graphData(); - final tags = data.nodes.where((n) => n.isTag).toList(); - expect(tags.length, 1); - expect(tags.single.backlinks, 2); - final ti = data.nodes.indexOf(tags.single); - final from = [ - for (var e = 0; e < data.kinds.length; e++) - if (data.kinds[e] == EdgeKind.tagged.index && data.targets[e] == ti) - data.nodes[data.sources[e]].title, - ]..sort(); - expect(from, ['Bleach', 'Naruto']); + expect(data.nodes.where((n) => n.isTag), isEmpty); + expect(data.kinds.contains(EdgeKind.tagged.index), isFalse); }); }); diff --git a/crates/synapse-ffi/src/api/graph_view.rs b/crates/synapse-ffi/src/api/graph_view.rs index cfb57df..e7647b8 100644 --- a/crates/synapse-ffi/src/api/graph_view.rs +++ b/crates/synapse-ffi/src/api/graph_view.rs @@ -285,7 +285,8 @@ pub(crate) fn build(g: &Graph, seq: i64) -> Result { } /// Tags are virtual nodes (`tag:`, appended after the pages): every page carrying a tag, -/// listed or implied by a type, gets one `TAGGED` edge to it. +/// listed or implied by a type, gets one `TAGGED` edge to it. A tag named like a hub already +/// in the graph (a hub's implicit tag) uses the hub's node instead of a second one. fn tagged( g: &Graph, index: &HashMap, @@ -301,7 +302,12 @@ fn tagged( .conn() .prepare("SELECT tag_key, page_id FROM page_tags ORDER BY tag_key, page_id")?; let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?; - let mut tag_index: HashMap = HashMap::new(); + let mut tag_index: HashMap = nodes + .iter() + .enumerate() + .filter(|(_, n)| n.is_hub) + .map(|(i, n)| (title_key(&n.title), i as u32)) + .collect(); for row in rows { let (key, page) = row?; let Some(&s) = index.get(&page) else { @@ -325,7 +331,10 @@ fn tagged( }); nodes.len() as u32 - 1 }); - b.add(s, t, edge_kind::TAGGED, 1.0); + // A hub's members already hang from it by a CONTAINS edge. + if !b.edges.contains_key(&(t, s, edge_kind::CONTAINS)) { + b.add(s, t, edge_kind::TAGGED, 1.0); + } } Ok(()) } diff --git a/crates/synapse-ffi/tests/api.rs b/crates/synapse-ffi/tests/api.rs index 32397f5..7225d39 100644 --- a/crates/synapse-ffi/tests/api.rs +++ b/crates/synapse-ffi/tests/api.rs @@ -364,6 +364,9 @@ fn legacy_candidates_and_purge() { let (keep, _) = page_with(&g, "Keep", &["x"]); page_with(&g, "contents", &["- x"]); page_with(&g, "now", &[]); + page_with(&g, "Blank", &["", " "]); + let (typed, _) = page_with(&g, "Typed", &[]); + g.set_page_type(typed, Some("persona".into())).unwrap(); g.create_hub("later".into(), String::new()).unwrap(); { let shared = g.shared(); @@ -389,6 +392,9 @@ fn legacy_candidates_and_purge() { assert!(!by("Template").preselected); assert_eq!(by("Template").reason, "type:: system"); assert!(c.iter().all(|x| x.title != "Keep")); + assert_eq!(by("Blank").reason, "Empty"); + assert!(by("Blank").preselected, "only blank blocks"); + assert!(c.iter().all(|x| x.title != "Typed"), "a type is content"); // A bad id rejects the call before anything is deleted. assert!(g.purge_pages(vec![todo.clone(), "nope".into()]).is_err()); @@ -453,6 +459,32 @@ fn tags_are_virtual_graph_nodes_with_their_own_edge_kind() { assert_eq!(g.pages_with_tag("IDEA".into()).unwrap().len(), 2); } +#[test] +fn a_tag_named_like_a_hub_uses_the_hub_node() { + let (_d, g) = open(); + let hub = g.create_hub("Proyectos".into(), String::new()).unwrap().id; + let (member, _) = page_with(&g, "Synapse", &["x"]); + let (loose, _) = page_with(&g, "Otro", &["x"]); + g.add_to_hub(member.clone(), hub.clone()).unwrap(); + g.add_page_tag(member, "proyectos".into()).unwrap(); + g.add_page_tag(loose, "Proyectos".into()).unwrap(); + let data = g.graph_data().unwrap(); + assert!( + data.nodes.iter().all(|n| !n.is_tag), + "no tag node beside the hub" + ); + let title = |i: u32| data.nodes[i as usize].title.clone(); + let tagged: Vec<(String, String)> = (0..data.kinds.len()) + .filter(|&e| data.kinds[e] == edge_kind::TAGGED) + .map(|e| (title(data.sources[e]), title(data.targets[e]))) + .collect(); + assert_eq!( + tagged, + [("Otro".to_string(), "Proyectos".to_string())], + "members already hang from the hub; other pages tagged with it point at it" + ); +} + #[test] fn page_type_and_tag_renaming_round_trip_through_the_api() { let (_d, g) = open(); @@ -475,7 +507,7 @@ fn page_type_and_tag_renaming_round_trip_through_the_api() { } #[test] -fn hub_members_point_at_one_virtual_tag_node_named_after_the_hub() { +fn a_hub_tag_and_a_same_name_type_need_no_tag_node_beside_the_hub() { let (_d, g) = open(); let hub = g.create_hub("Anime".into(), String::new()).unwrap(); let (a, _) = page_with(&g, "Naruto", &["x"]); @@ -484,16 +516,14 @@ fn hub_members_point_at_one_virtual_tag_node_named_after_the_hub() { g.add_to_hub(b.clone(), hub.id.clone()).unwrap(); g.set_page_type(b.clone(), Some("anime".into())).unwrap(); let data = g.graph_data().unwrap(); - let tags: Vec<_> = data.nodes.iter().filter(|n| n.is_tag).collect(); - assert_eq!(tags.len(), 1, "hub tag and same-name type merge"); - assert_eq!(tags[0].title, "Anime"); - let target = data.nodes.iter().position(|n| n.is_tag).unwrap() as u32; - let mut from: Vec = (0..data.kinds.len()) - .filter(|&e| data.kinds[e] == edge_kind::TAGGED && data.targets[e] == target) - .map(|e| data.nodes[data.sources[e] as usize].title.clone()) - .collect(); - from.sort(); - assert_eq!(from, ["Bleach", "Naruto"]); + assert!( + data.nodes.iter().all(|n| !n.is_tag), + "the hub stands for its tag" + ); + assert!( + !data.kinds.contains(&edge_kind::TAGGED), + "members already hang from the hub" + ); let cat = g.tag_catalog().unwrap(); assert_eq!( (cat[0].name.to_lowercase().as_str(), cat[0].count),