@@ -1604,20 +1604,28 @@ def test_bare_id_across_different_paths_is_path_qualified(capsys):
16041604 assert "minted by two different files" not in capsys .readouterr ().err
16051605
16061606
1607- def test_bare_id_naming_an_existing_file_is_not_renamed (capsys ):
1608- """`protocol.md` exists, so a bare `protocol` may be a genuine reference to it:
1609- the group is not path-qualified and takes the definer-wins collapse path."""
1610- nodes = [
1607+ def _three_bare_protocols ():
1608+ return [
16111609 {"id" : "protocol" , "label" : "protocol" , "file_type" : "document" ,
16121610 "source_file" : "protocol.md" },
16131611 {"id" : "protocol" , "label" : "protocol (docs)" , "file_type" : "document" ,
16141612 "source_file" : "docs/protocol.md" },
16151613 {"id" : "protocol" , "label" : "protocol (firmware)" , "file_type" : "document" ,
16161614 "source_file" : "docs/firmware/protocol.md" },
16171615 ]
1618- result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1619- assert [(n ["id" ], n ["source_file" ]) for n in result_nodes ] == [("protocol" , "protocol.md" )]
1620- assert "WARNING" in capsys .readouterr ().err
1616+
1617+
1618+ def test_bare_ids_in_three_files_keep_one_node_per_file (capsys ):
1619+ """protocol.md exists and the other two files each have only a bare `protocol`.
1620+ Neither has a page of its own, so the bare id is their page: protocol.md keeps
1621+ `protocol` and the others are path-qualified. Nothing is dropped (#4281)."""
1622+ result_nodes , _ = deduplicate_entities (_three_bare_protocols (), [], communities = {})
1623+ assert {(n ["id" ], n ["source_file" ]) for n in result_nodes } == {
1624+ ("protocol" , "protocol.md" ),
1625+ ("docs_protocol" , "docs/protocol.md" ),
1626+ ("docs_firmware_protocol" , "docs/firmware/protocol.md" ),
1627+ }
1628+ assert "minted by two different files" not in capsys .readouterr ().err
16211629
16221630
16231631def test_build_with_absolute_edge_paths_moves_edges_to_renamed_node (tmp_path ):
@@ -1961,17 +1969,100 @@ def test_mention_without_an_edge_gives_the_same_two_documents():
19611969 assert list (G .edges ) == []
19621970
19631971
1964- def test_top_level_collision_keeps_todays_result ():
1965- """Case R_toplevel. KNOWN LIMITATION, pinned so a change is noticed: with a
1966- top-level protocol.md that also mints `protocol`, the three files collide and the
1967- label tiebreak drops the top-level document's node. This is a separate genuine
1968- collision from the mention rule and is not fixed here (#4281)."""
1972+ def test_top_level_file_keeps_its_own_node_next_to_dropped_folder_docs ():
1973+ """Case R_toplevel. protocol.md, docs/protocol.md and docs/firmware/protocol.md
1974+ all mint `protocol`. Each file keeps its own node, and the firmware reference
1975+ lands on the top-level `protocol` node (#4281)."""
19691976 from graphify .build import build
19701977
19711978 G = build ([_protocol_reference_input (top_level = True )])
1972- assert sorted (G .nodes ) == ["docs_firmware_protocol" , "docs_protocol" ]
1979+ assert sorted (G .nodes ) == ["docs_firmware_protocol" , "docs_protocol" , "protocol" ]
19731980 assert {frozenset (e ) for e in G .edges } == {
1974- frozenset (("docs_firmware_protocol" , "docs_protocol" ))}
1981+ frozenset (("docs_firmware_protocol" , "protocol" ))}
1982+
1983+
1984+ def _reference_to_top_level (* , edge = True , docs_label = "protocol" , top_label = "Protocol" ):
1985+ docs = "docs/protocol.md"
1986+ nodes = [_mention_node ("docs_protocol" , "Docs protocol" , docs ),
1987+ _mention_node ("protocol" , docs_label , docs ),
1988+ _mention_node ("protocol" , top_label , "protocol.md" )]
1989+ edges = [_mention_edge ("docs_protocol" , "protocol" , docs )] if edge else []
1990+ return nodes , edges
1991+
1992+
1993+ def test_genuine_reference_to_top_level_file_still_collapses ():
1994+ """docs/protocol.md has its own `docs_protocol` page and a `protocol` it
1995+ references. That bare id is the top-level file, not a second page of docs/:
1996+ it collapses onto protocol.md's node and the edge ends there (#4281)."""
1997+ nodes , edges = _reference_to_top_level ()
1998+ kept , kept_edges = deduplicate_entities (nodes , edges , communities = {})
1999+ assert {(n ["id" ], n ["source_file" ]) for n in kept } == {
2000+ ("docs_protocol" , "docs/protocol.md" ), ("protocol" , "protocol.md" )}
2001+ assert [(e ["source" ], e ["target" ]) for e in kept_edges ] == [("docs_protocol" , "protocol" )]
2002+
2003+
2004+ def test_reference_to_top_level_file_without_an_edge_still_collapses ():
2005+ """Same input with no `references` edge: the docs `protocol` still collapses
2006+ into protocol.md's node, because docs/protocol.md has its own page (#4281)."""
2007+ nodes , edges = _reference_to_top_level (edge = False )
2008+ kept , kept_edges = deduplicate_entities (nodes , edges , communities = {})
2009+ assert {(n ["id" ], n ["source_file" ]) for n in kept } == {
2010+ ("docs_protocol" , "docs/protocol.md" ), ("protocol" , "protocol.md" )}
2011+ assert kept_edges == []
2012+
2013+
2014+ def test_top_level_file_survives_whatever_the_labels_are ():
2015+ """Case R_toplevel, label-swap. The bare `protocol` from docs/protocol.md has the
2016+ SHORTER label. docs/protocol.md has its own page, so that node is a reference and
2017+ protocol.md's node survives; the docs_protocol edge ends on it (#4281)."""
2018+ nodes , edges = _reference_to_top_level (docs_label = "Protocol" , top_label = "Protocol (top-level)" )
2019+ kept , kept_edges = deduplicate_entities (nodes , edges , communities = {})
2020+ assert {(n ["id" ], n ["label" ], n ["source_file" ]) for n in kept } == {
2021+ ("docs_protocol" , "Docs protocol" , "docs/protocol.md" ),
2022+ ("protocol" , "Protocol (top-level)" , "protocol.md" )}
2023+ assert [(e ["source" ], e ["target" ]) for e in kept_edges ] == [("docs_protocol" , "protocol" )]
2024+
2025+
2026+ def test_top_level_file_survives_without_a_references_edge ():
2027+ """Same label-swap input with no `references` edge, so `_mentions` has no
2028+ evidence. The ranking alone must keep protocol.md's node: a bare slice from a
2029+ file that has its own page is a reference, whatever the labels (#4281)."""
2030+ nodes , _ = _reference_to_top_level (edge = False , docs_label = "Protocol" ,
2031+ top_label = "Protocol (top-level)" )
2032+ for order in (nodes , list (reversed (nodes ))):
2033+ kept , _ = deduplicate_entities ([dict (n ) for n in order ], [], communities = {})
2034+ survivor = next (n for n in kept if n ["id" ] == "protocol" )
2035+ assert (survivor ["label" ], survivor ["source_file" ]) == ("Protocol (top-level)" , "protocol.md" )
2036+
2037+
2038+ def test_top_level_collision_result_does_not_depend_on_input_order ():
2039+ """The three-file case fed in reverse order keeps the same nodes (#4281)."""
2040+ nodes = _three_bare_protocols ()
2041+ fwd , _ = deduplicate_entities ([dict (n ) for n in nodes ], [], communities = {})
2042+ rev , _ = deduplicate_entities ([dict (n ) for n in reversed (nodes )], [], communities = {})
2043+ assert {(n ["id" ], n ["source_file" ]) for n in fwd } == {
2044+ (n ["id" ], n ["source_file" ]) for n in rev }
2045+
2046+
2047+ def test_dropped_folder_doc_with_own_stem_entity_falls_back_to_collapse ():
2048+ """KNOWN BOUNDARY, pinned so a change is noticed. docs/protocol.md emits `protocol`
2049+ AND `docs_protocol_header` (an entity spelled with its full stem), so the file
2050+ counts as having its own page and the bare id may be a reference. It collapses
2051+ into protocol.md's node, as before this change (#4281)."""
2052+ nodes = [_mention_node ("protocol" , "protocol" , "docs/protocol.md" ),
2053+ _mention_node ("docs_protocol_header" , "Header" , "docs/protocol.md" ),
2054+ _mention_node ("protocol" , "Protocol" , "protocol.md" )]
2055+ kept , _ = deduplicate_entities (nodes , [], communities = {})
2056+ assert {(n ["id" ], n ["source_file" ]) for n in kept } == {
2057+ ("docs_protocol_header" , "docs/protocol.md" ), ("protocol" , "protocol.md" )}
2058+
2059+
2060+ def test_lone_bare_id_without_a_top_level_file_is_left_to_build ():
2061+ """A single `protocol` from docs/protocol.md and no protocol.md anywhere: this
2062+ pass does not act, the id stays `protocol` for build's remap (#4281)."""
2063+ nodes = [_mention_node ("protocol" , "Protocol" , "docs/protocol.md" )]
2064+ kept , _ = deduplicate_entities (nodes , [], communities = {})
2065+ assert [n ["id" ] for n in kept ] == ["protocol" ]
19752066
19762067
19772068def test_entity_described_in_two_documents_collapses_to_one_node (capsys ):
0 commit comments