core(mermaid): fix error when 3+ subgraph levels (#29970)

This commit is contained in:
Adrián Panella 2025-03-04 12:27:49 -06:00 committed by GitHub
parent 417efa30a6
commit c599ba47d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 107 additions and 0 deletions

View File

@ -145,6 +145,9 @@ def draw_mermaid(
for nested_prefix in edge_groups:
if not nested_prefix.startswith(prefix + ":") or nested_prefix == prefix:
continue
# only go to first level subgraphs
if ":" in nested_prefix[len(prefix) + 1 :]:
continue
add_subgraph(edge_groups[nested_prefix], nested_prefix)
if prefix and not self_loop:

View File

@ -26,6 +26,37 @@
'''
# ---
# name: test_triple_nested_subgraph_mermaid[mermaid]
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
__start__([<p>__start__</p>]):::first
parent_1(parent_1)
child_child_1_grandchild_1(grandchild_1)
child_child_1_grandchild_1_greatgrandchild(greatgrandchild)
child_child_1_grandchild_2(grandchild_2<hr/><small><em>__interrupt = before</em></small>)
child_child_2(child_2)
parent_2(parent_2)
__end__([<p>__end__</p>]):::last
__start__ --> parent_1;
child_child_2 --> parent_2;
parent_1 --> child_child_1_grandchild_1;
parent_2 --> __end__;
subgraph child
child_child_1_grandchild_2 --> child_child_2;
subgraph child_1
child_child_1_grandchild_1_greatgrandchild --> child_child_1_grandchild_2;
subgraph grandchild_1
child_child_1_grandchild_1 --> child_child_1_grandchild_1_greatgrandchild;
end
end
end
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc
'''
# ---
# name: test_graph_mermaid_duplicate_nodes[mermaid]
'''
graph TD;

View File

@ -375,6 +375,79 @@ def test_double_nested_subgraph_mermaid(snapshot: SnapshotAssertion) -> None:
assert graph.draw_mermaid() == snapshot(name="mermaid")
def test_triple_nested_subgraph_mermaid(snapshot: SnapshotAssertion) -> None:
empty_data = BaseModel
nodes = {
"__start__": Node(
id="__start__", name="__start__", data=empty_data, metadata=None
),
"parent_1": Node(
id="parent_1", name="parent_1", data=empty_data, metadata=None
),
"child:child_1:grandchild_1": Node(
id="child:child_1:grandchild_1",
name="grandchild_1",
data=empty_data,
metadata=None,
),
"child:child_1:grandchild_1:greatgrandchild": Node(
id="child:child_1:grandchild_1:greatgrandchild",
name="greatgrandchild",
data=empty_data,
metadata=None,
),
"child:child_1:grandchild_2": Node(
id="child:child_1:grandchild_2",
name="grandchild_2",
data=empty_data,
metadata={"__interrupt": "before"},
),
"child:child_2": Node(
id="child:child_2", name="child_2", data=empty_data, metadata=None
),
"parent_2": Node(
id="parent_2", name="parent_2", data=empty_data, metadata=None
),
"__end__": Node(id="__end__", name="__end__", data=empty_data, metadata=None),
}
edges = [
Edge(
source="child:child_1:grandchild_1",
target="child:child_1:grandchild_1:greatgrandchild",
data=None,
conditional=False,
),
Edge(
source="child:child_1:grandchild_1:greatgrandchild",
target="child:child_1:grandchild_2",
data=None,
conditional=False,
),
Edge(
source="child:child_1:grandchild_2",
target="child:child_2",
data=None,
conditional=False,
),
Edge(source="__start__", target="parent_1", data=None, conditional=False),
Edge(
source="child:child_2",
target="parent_2",
data=None,
conditional=False,
),
Edge(
source="parent_1",
target="child:child_1:grandchild_1",
data=None,
conditional=False,
),
Edge(source="parent_2", target="__end__", data=None, conditional=False),
]
graph = Graph(nodes, edges)
assert graph.draw_mermaid() == snapshot(name="mermaid")
def test_runnable_get_graph_with_invalid_input_type() -> None:
"""Test that error isn't raised when getting graph with invalid input type."""