|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for _BranchPath. |
| 16 | +
|
| 17 | +Verifies that _BranchPath correctly parses, serializes, and manipulates |
| 18 | +hierarchical dynamic execution branch paths. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +from google.adk.events._branch_path import _BranchPath |
| 24 | +import pytest |
| 25 | + |
| 26 | + |
| 27 | +def test_from_string_with_empty_string_returns_empty_path(): |
| 28 | + """Parsing an empty string returns a _BranchPath with no segments.""" |
| 29 | + path = _BranchPath.from_string("") |
| 30 | + |
| 31 | + assert path.segments == [] |
| 32 | + assert str(path) == "" |
| 33 | + |
| 34 | + |
| 35 | +def test_from_string_with_single_segment_returns_path_with_one_segment(): |
| 36 | + """Parsing a single name returns a _BranchPath with one segment.""" |
| 37 | + path = _BranchPath.from_string("agent_0") |
| 38 | + |
| 39 | + assert path.segments == ["agent_0"] |
| 40 | + assert str(path) == "agent_0" |
| 41 | + |
| 42 | + |
| 43 | +def test_from_string_with_multiple_segments_returns_path_with_all_segments(): |
| 44 | + """Parsing a dot-separated string returns a _BranchPath with all segments.""" |
| 45 | + path = _BranchPath.from_string("parent.child.node") |
| 46 | + |
| 47 | + assert path.segments == ["parent", "child", "node"] |
| 48 | + assert str(path) == "parent.child.node" |
| 49 | + |
| 50 | + |
| 51 | +def test_equality_compares_path_segments(): |
| 52 | + """Two _BranchPath objects are equal if and only if their segments match.""" |
| 53 | + path1 = _BranchPath.from_string("parent.child") |
| 54 | + path2 = _BranchPath.from_string("parent.child") |
| 55 | + path3 = _BranchPath.from_string("parent.other") |
| 56 | + |
| 57 | + assert path1 == path2 |
| 58 | + assert path1 != path3 |
| 59 | + assert path1 != "parent.child" # Different type |
| 60 | + |
| 61 | + |
| 62 | +def test_run_ids_extracts_all_run_ids_from_path(): |
| 63 | + """run_ids extracts all run IDs (the part after '@') from all segments.""" |
| 64 | + # Given paths with various run ID patterns |
| 65 | + path_with_ids = _BranchPath.from_string("parent@1.child@2.node") |
| 66 | + path_no_ids = _BranchPath.from_string("parent.child") |
| 67 | + path_mixed = _BranchPath.from_string("parent@1.child.node@3") |
| 68 | + |
| 69 | + # Then the extracted run IDs match expectations |
| 70 | + assert path_with_ids.run_ids == {"1", "2"} |
| 71 | + assert path_no_ids.run_ids == set() |
| 72 | + assert path_mixed.run_ids == {"1", "3"} |
| 73 | + |
| 74 | + |
| 75 | +def test_parent_returns_parent_path_or_none_for_root(): |
| 76 | + """parent returns a new _BranchPath excluding the leaf segment, or None.""" |
| 77 | + path = _BranchPath.from_string("parent.child.node") |
| 78 | + |
| 79 | + assert path.parent == _BranchPath.from_string("parent.child") |
| 80 | + assert path.parent.parent == _BranchPath.from_string("parent") |
| 81 | + assert path.parent.parent.parent is None |
| 82 | + |
| 83 | + |
| 84 | +def test_is_descendant_of_verifies_path_hierarchy_safely(): |
| 85 | + """is_descendant_of returns True if the path is a strict sub-path of ancestor.""" |
| 86 | + # Given an ancestor and various comparison paths |
| 87 | + ancestor = _BranchPath.from_string("parent.child") |
| 88 | + descendant = _BranchPath.from_string("parent.child.node.leaf") |
| 89 | + not_descendant = _BranchPath.from_string("parent.other") |
| 90 | + same = _BranchPath.from_string("parent.child") |
| 91 | + |
| 92 | + # Then descendant checks match expectations |
| 93 | + assert descendant.is_descendant_of(ancestor) |
| 94 | + assert not ancestor.is_descendant_of(descendant) |
| 95 | + assert not not_descendant.is_descendant_of(ancestor) |
| 96 | + assert not same.is_descendant_of(ancestor) |
| 97 | + |
| 98 | + |
| 99 | +def test_is_descendant_of_is_immune_to_partial_name_prefix_match(): |
| 100 | + """is_descendant_of compares segments, avoiding partial string prefix bugs.""" |
| 101 | + # Given an ancestor and a path that has a partial string prefix but different segment |
| 102 | + ancestor = _BranchPath.from_string("agent_0") |
| 103 | + descendant_with_prefix = _BranchPath.from_string("agent_00.child") |
| 104 | + |
| 105 | + # Then it is not recognized as a descendant because segments don't match |
| 106 | + assert not descendant_with_prefix.is_descendant_of(ancestor) |
| 107 | + |
| 108 | + |
| 109 | +def test_common_prefix_finds_longest_shared_path(): |
| 110 | + """common_prefix returns the longest common prefix of a list of paths.""" |
| 111 | + # Given a list of paths sharing a common prefix |
| 112 | + paths = [ |
| 113 | + _BranchPath.from_string("parent.child.node1"), |
| 114 | + _BranchPath.from_string("parent.child.node2.leaf"), |
| 115 | + _BranchPath.from_string("parent.child.node3"), |
| 116 | + ] |
| 117 | + |
| 118 | + # When finding the common prefix |
| 119 | + result = _BranchPath.common_prefix(paths) |
| 120 | + |
| 121 | + # Then the result matches the shared parent path |
| 122 | + assert result == _BranchPath.from_string("parent.child") |
| 123 | + |
| 124 | + |
| 125 | +def test_common_prefix_with_no_shared_path_returns_empty(): |
| 126 | + """common_prefix returns an empty path if there is no shared prefix.""" |
| 127 | + paths = [ |
| 128 | + _BranchPath.from_string("parent.child"), |
| 129 | + _BranchPath.from_string("other.child"), |
| 130 | + ] |
| 131 | + |
| 132 | + result = _BranchPath.common_prefix(paths) |
| 133 | + |
| 134 | + assert result == _BranchPath.from_string("") |
| 135 | + |
| 136 | + |
| 137 | +def test_common_prefix_with_empty_list_returns_empty(): |
| 138 | + """common_prefix returns an empty path if the input list is empty.""" |
| 139 | + result = _BranchPath.common_prefix([]) |
| 140 | + |
| 141 | + assert result == _BranchPath.from_string("") |
| 142 | + |
| 143 | + |
| 144 | +def test_constructor_copies_segments_list(): |
| 145 | + """_BranchPath copies the input segments list to ensure immutability.""" |
| 146 | + segments = ["parent", "child"] |
| 147 | + path = _BranchPath(segments) |
| 148 | + |
| 149 | + # Mutate the original list |
| 150 | + segments.append("grandchild") |
| 151 | + |
| 152 | + # The path segments should remain unchanged |
| 153 | + assert path.segments == ["parent", "child"] |
| 154 | + |
| 155 | + |
| 156 | +def test_run_ids_filters_out_empty_run_ids(): |
| 157 | + """run_ids filters out segments with empty run IDs (e.g. ending with '@').""" |
| 158 | + path = _BranchPath.from_string("parent@.child@2.node@") |
| 159 | + |
| 160 | + assert path.run_ids == {"2"} |
0 commit comments