griip-core-dev 0.5.6.dev25546__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,179 @@
1
+ """Tests for find_compatible_registration."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from unittest.mock import MagicMock
6
+
7
+ import pytest
8
+
9
+ from griip_core.hardware.vrmcs_registration import adopt_existing_registration, find_compatible_registration
10
+
11
+
12
+ def _robot_info(robot_id: str, ip: str) -> MagicMock:
13
+ info = MagicMock()
14
+ info.id = robot_id
15
+ info.driver_params.params = {"ip": ip}
16
+ return info
17
+
18
+
19
+ def _robot_instance(robot_id: str, ip: str) -> MagicMock:
20
+ instance = MagicMock()
21
+ instance.id = robot_id
22
+ instance.driver_params.params = {"ip": ip}
23
+ return instance
24
+
25
+
26
+ def _mock_client(robots: list[MagicMock] | None = None, list_raises: Exception | None = None) -> MagicMock:
27
+ client = MagicMock()
28
+ if list_raises is not None:
29
+ client.stub.ListRobots.side_effect = list_raises
30
+ else:
31
+ list_response = MagicMock()
32
+ list_response.robots = robots if robots is not None else []
33
+ client.stub.ListRobots.return_value = list_response
34
+ return client
35
+
36
+
37
+ class TestFindCompatibleRegistration:
38
+ def test_returns_none_when_no_robots_registered(self) -> None:
39
+ client = _mock_client(robots=[])
40
+ assert find_compatible_registration(client, robot_id="0", robot_ip="10.0.0.1") is None
41
+
42
+ def test_returns_exact_id_match(self) -> None:
43
+ a = _robot_info("abc", "10.0.0.2")
44
+ b = _robot_info("xyz", "10.0.0.1")
45
+ client = _mock_client(robots=[a, b])
46
+
47
+ match = find_compatible_registration(client, robot_id="xyz", robot_ip="10.0.0.1")
48
+
49
+ assert match is b
50
+
51
+ def test_exact_id_match_wins_even_when_id_is_zero(self) -> None:
52
+ placeholder = _robot_info("0", "10.0.0.1")
53
+ uuid = _robot_info("ff83ba12", "10.0.0.1")
54
+ client = _mock_client(robots=[placeholder, uuid])
55
+
56
+ match = find_compatible_registration(client, robot_id="0", robot_ip="10.0.0.1")
57
+
58
+ assert match is placeholder
59
+
60
+ def test_ip_match_prefers_non_zero_when_id_differs(self) -> None:
61
+ placeholder = _robot_info("0", "10.0.0.1")
62
+ uuid = _robot_info("ff83ba12", "10.0.0.1")
63
+ client = _mock_client(robots=[placeholder, uuid])
64
+
65
+ match = find_compatible_registration(client, robot_id="some-other-id", robot_ip="10.0.0.1")
66
+
67
+ assert match is uuid
68
+
69
+ def test_falls_back_to_zero_when_only_zero_matches_ip_and_flag_enabled(self) -> None:
70
+ placeholder = _robot_info("0", "10.0.0.1")
71
+ client = _mock_client(robots=[placeholder])
72
+
73
+ match = find_compatible_registration(
74
+ client,
75
+ robot_id="some-other-id",
76
+ robot_ip="10.0.0.1",
77
+ allow_legacy_zero_id_match=True,
78
+ )
79
+
80
+ assert match is placeholder
81
+
82
+ def test_legacy_zero_id_rejected_by_default(self) -> None:
83
+ placeholder = _robot_info("0", "10.0.0.1")
84
+ client = _mock_client(robots=[placeholder])
85
+
86
+ match = find_compatible_registration(client, robot_id="some-other-id", robot_ip="10.0.0.1")
87
+
88
+ assert match is None
89
+
90
+ def test_returns_none_when_no_id_or_ip_match(self) -> None:
91
+ other = _robot_info("uuid-1", "10.0.0.99")
92
+ client = _mock_client(robots=[other])
93
+
94
+ match = find_compatible_registration(client, robot_id="some-other-id", robot_ip="10.0.0.1")
95
+
96
+ assert match is None
97
+
98
+ def test_returns_none_when_list_robots_raises(self) -> None:
99
+ client = _mock_client(list_raises=RuntimeError("pydantic blew up"))
100
+
101
+ match = find_compatible_registration(client, robot_id="0", robot_ip="10.0.0.1")
102
+
103
+ assert match is None
104
+
105
+ def test_quiet_suppresses_warnings(self, caplog: pytest.LogCaptureFixture) -> None:
106
+ client = _mock_client(list_raises=RuntimeError("boom"))
107
+
108
+ with caplog.at_level("WARNING", logger="griip_core.hardware.vrmcs_registration"):
109
+ find_compatible_registration(client, robot_id="0", robot_ip="10.0.0.1", quiet=True)
110
+
111
+ assert caplog.records == []
112
+
113
+ def test_exact_id_match_skipped_when_ip_differs(self) -> None:
114
+ wrong_ip = _robot_info("0", "10.0.0.99")
115
+ client = _mock_client(robots=[wrong_ip])
116
+
117
+ match = find_compatible_registration(client, robot_id="0", robot_ip="10.0.0.1")
118
+
119
+ assert match is None
120
+
121
+ def test_exact_id_match_with_wrong_ip_falls_through_to_ip_step(self) -> None:
122
+ wrong_ip = _robot_info("0", "10.0.0.99")
123
+ right_ip = _robot_info("ff83ba12", "10.0.0.1")
124
+ client = _mock_client(robots=[wrong_ip, right_ip])
125
+
126
+ match = find_compatible_registration(client, robot_id="0", robot_ip="10.0.0.1")
127
+
128
+ assert match is right_ip
129
+
130
+ def test_exact_id_match_accepts_empty_registered_ip(self) -> None:
131
+ legacy = _robot_info("0", "")
132
+ client = _mock_client(robots=[legacy])
133
+
134
+ match = find_compatible_registration(client, robot_id="0", robot_ip="10.0.0.1")
135
+
136
+ assert match is legacy
137
+
138
+ def test_require_ip_match_false_returns_exact_id_regardless_of_ip(self) -> None:
139
+ wrong_ip = _robot_info("0", "10.0.0.99")
140
+ client = _mock_client(robots=[wrong_ip])
141
+
142
+ match = find_compatible_registration(client, robot_id="0", robot_ip="10.0.0.1", require_ip_match=False)
143
+
144
+ assert match is wrong_ip
145
+
146
+
147
+ class TestAdoptExistingRegistration:
148
+ def test_explicit_zero_id_adopts_exact_zero_registration(self) -> None:
149
+ placeholder = _robot_info("0", "10.0.0.1")
150
+ uuid = _robot_info("ff83ba12", "10.0.0.1")
151
+ client = _mock_client(robots=[placeholder, uuid])
152
+ instance = _robot_instance("0", "10.0.0.1")
153
+
154
+ adopted = adopt_existing_registration(
155
+ client,
156
+ instance,
157
+ log=MagicMock(),
158
+ allow_legacy_zero_id_match=True,
159
+ )
160
+
161
+ assert adopted is True
162
+ assert instance.id == "0"
163
+
164
+ def test_auto_id_prefers_non_zero_ip_match_over_zero_placeholder(self) -> None:
165
+ placeholder = _robot_info("0", "10.0.0.1")
166
+ uuid = _robot_info("ff83ba12", "10.0.0.1")
167
+ client = _mock_client(robots=[placeholder, uuid])
168
+ instance = _robot_instance("0", "10.0.0.1")
169
+
170
+ adopted = adopt_existing_registration(
171
+ client,
172
+ instance,
173
+ log=MagicMock(),
174
+ allow_legacy_zero_id_match=True,
175
+ prefer_ip_match=True,
176
+ )
177
+
178
+ assert adopted is True
179
+ assert instance.id == "ff83ba12"