piphi-runtime-testkit-python 0.1.4__tar.gz → 0.2.0__tar.gz
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.
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/PKG-INFO +1 -1
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/pyproject.toml +2 -2
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/src/piphi_runtime_testkit_python/__init__.py +21 -1
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/src/piphi_runtime_testkit_python/assertions.py +36 -0
- piphi_runtime_testkit_python-0.2.0/src/piphi_runtime_testkit_python/camera.py +150 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_assertions.py +30 -0
- piphi_runtime_testkit_python-0.2.0/tests/test_camera.py +63 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/LICENSE +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/README.md +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/src/piphi_runtime_testkit_python/behavior_contract.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/src/piphi_runtime_testkit_python/builders.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/src/piphi_runtime_testkit_python/mock_core.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/src/piphi_runtime_testkit_python/pytest_plugin.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/__init__.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_behavior_contract.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_builders.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_fastapi_runtime_example.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_mock_core.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_pytest_plugin.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_runtime_config_contract.py +0 -0
- {piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_runtime_flow.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "piphi-runtime-testkit-python"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.0"
|
|
4
4
|
description = "Pytest-first helpers for testing PiPhi runtime integrations"
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "KelvinSan", email = "support@piphi.network" },
|
|
@@ -55,7 +55,7 @@ release-version = "python scripts/release.py"
|
|
|
55
55
|
dev = [
|
|
56
56
|
"fastapi>=0.115,<1.0",
|
|
57
57
|
"httpx>=0.27,<1.0",
|
|
58
|
-
"piphi-runtime-kit-python[fastapi]
|
|
58
|
+
"piphi-runtime-kit-python[fastapi]>=0.8.0,<1.0.0",
|
|
59
59
|
"pytest>=8.3,<9.0",
|
|
60
60
|
"pytest-asyncio>=0.24,<1.0",
|
|
61
61
|
"twine>=6.2.0",
|
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
"""Pytest-first helpers for testing PiPhi runtime integrations."""
|
|
2
2
|
|
|
3
|
-
from .assertions import
|
|
3
|
+
from .assertions import (
|
|
4
|
+
assert_entities_response,
|
|
5
|
+
assert_event_sent,
|
|
6
|
+
assert_state_refresh_response,
|
|
7
|
+
assert_telemetry_sent,
|
|
8
|
+
)
|
|
4
9
|
from .builders import (
|
|
5
10
|
build_config_payload,
|
|
6
11
|
build_config_snapshot,
|
|
7
12
|
build_core_config_row,
|
|
8
13
|
build_runtime_headers,
|
|
9
14
|
)
|
|
15
|
+
from .camera import (
|
|
16
|
+
CAMERA_MEDIA_CONTRACT_VERSION,
|
|
17
|
+
assert_camera_capability,
|
|
18
|
+
assert_public_camera_message_redacted,
|
|
19
|
+
build_camera_session_offer,
|
|
20
|
+
build_camera_sidecar_session_open,
|
|
21
|
+
build_camera_source,
|
|
22
|
+
)
|
|
10
23
|
from .behavior_contract import (
|
|
11
24
|
BehaviorTelemetryIssue,
|
|
12
25
|
assert_behavior_conditions_match_telemetry,
|
|
@@ -17,12 +30,19 @@ from .mock_core import CapturedRequest, MockCoreServer, RUNTIME_CONFIG_FETCH_PAT
|
|
|
17
30
|
__all__ = [
|
|
18
31
|
"CapturedRequest",
|
|
19
32
|
"BehaviorTelemetryIssue",
|
|
33
|
+
"CAMERA_MEDIA_CONTRACT_VERSION",
|
|
20
34
|
"MockCoreServer",
|
|
21
35
|
"RUNTIME_CONFIG_FETCH_PATH",
|
|
22
36
|
"assert_entities_response",
|
|
37
|
+
"assert_camera_capability",
|
|
23
38
|
"assert_behavior_conditions_match_telemetry",
|
|
24
39
|
"assert_event_sent",
|
|
40
|
+
"assert_state_refresh_response",
|
|
25
41
|
"assert_telemetry_sent",
|
|
42
|
+
"assert_public_camera_message_redacted",
|
|
43
|
+
"build_camera_session_offer",
|
|
44
|
+
"build_camera_sidecar_session_open",
|
|
45
|
+
"build_camera_source",
|
|
26
46
|
"build_config_payload",
|
|
27
47
|
"build_config_snapshot",
|
|
28
48
|
"build_core_config_row",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
from datetime import datetime
|
|
3
4
|
from typing import Any
|
|
4
5
|
|
|
5
6
|
from .mock_core import CapturedRequest, MockCoreServer
|
|
@@ -166,3 +167,38 @@ def assert_entities_response(payload: Any) -> dict[str, Any]:
|
|
|
166
167
|
raise AssertionError(f"Expected /entities payload commands to be an object, but received {commands!r}.")
|
|
167
168
|
|
|
168
169
|
return payload
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def assert_state_refresh_response(
|
|
173
|
+
payload: Any,
|
|
174
|
+
*,
|
|
175
|
+
request_id: str,
|
|
176
|
+
) -> dict[str, Any]:
|
|
177
|
+
"""Assert that ``/state?refresh=true`` proves an upstream state read."""
|
|
178
|
+
|
|
179
|
+
if not isinstance(payload, dict):
|
|
180
|
+
raise AssertionError(
|
|
181
|
+
f"Expected refreshed /state payload to be an object, but received {payload!r}."
|
|
182
|
+
)
|
|
183
|
+
receipt = payload.get("refresh")
|
|
184
|
+
if not isinstance(receipt, dict):
|
|
185
|
+
raise AssertionError("Expected refreshed /state payload to include a refresh receipt.")
|
|
186
|
+
if receipt.get("request_id") != request_id:
|
|
187
|
+
raise AssertionError("Expected refresh receipt to echo the current refresh request id.")
|
|
188
|
+
if receipt.get("performed") is not True or receipt.get("status") != "refreshed":
|
|
189
|
+
raise AssertionError("Expected refresh receipt to confirm an upstream read.")
|
|
190
|
+
source = receipt.get("source")
|
|
191
|
+
if not isinstance(source, str) or not source.strip():
|
|
192
|
+
raise AssertionError("Expected refresh receipt to name its upstream source.")
|
|
193
|
+
observed_at = receipt.get("observed_at")
|
|
194
|
+
try:
|
|
195
|
+
parsed = datetime.fromisoformat(str(observed_at).replace("Z", "+00:00"))
|
|
196
|
+
except ValueError as exc:
|
|
197
|
+
raise AssertionError(
|
|
198
|
+
"Expected refresh receipt to include a valid observation timestamp."
|
|
199
|
+
) from exc
|
|
200
|
+
if parsed.tzinfo is None:
|
|
201
|
+
raise AssertionError(
|
|
202
|
+
"Expected refresh observation timestamp to include a timezone."
|
|
203
|
+
)
|
|
204
|
+
return payload
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""Camera-media v1 builders and conformance assertions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
from urllib.parse import urlsplit
|
|
7
|
+
|
|
8
|
+
CAMERA_MEDIA_CONTRACT_VERSION = "camera.media.v1"
|
|
9
|
+
PUBLIC_CAMERA_AUDIENCES = {"browser-core"}
|
|
10
|
+
CAMERA_SECRET_FIELDS = {
|
|
11
|
+
"authorization",
|
|
12
|
+
"password",
|
|
13
|
+
"source_url",
|
|
14
|
+
"token",
|
|
15
|
+
"username",
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def build_camera_source(
|
|
20
|
+
*,
|
|
21
|
+
camera_id: str = "camera-1",
|
|
22
|
+
source_url: str = "rtsp://camera.local/live",
|
|
23
|
+
preferred_transport: str = "tcp",
|
|
24
|
+
connect_timeout_seconds: int = 10,
|
|
25
|
+
) -> dict[str, Any]:
|
|
26
|
+
"""Build an internal camera source descriptor for runtime/sidecar tests."""
|
|
27
|
+
|
|
28
|
+
parsed = urlsplit(source_url)
|
|
29
|
+
return {
|
|
30
|
+
"contract_version": CAMERA_MEDIA_CONTRACT_VERSION,
|
|
31
|
+
"message_type": "camera.source",
|
|
32
|
+
"audience": "core-sidecar",
|
|
33
|
+
"camera_id": camera_id,
|
|
34
|
+
"source_url": source_url,
|
|
35
|
+
"source_protocol": parsed.scheme.lower(),
|
|
36
|
+
"preferred_transport": preferred_transport,
|
|
37
|
+
"connect_timeout_seconds": connect_timeout_seconds,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def build_camera_session_offer(
|
|
42
|
+
*,
|
|
43
|
+
sdp: str = "v=0\r\na=recvonly\r\n",
|
|
44
|
+
include_audio: bool = False,
|
|
45
|
+
) -> dict[str, Any]:
|
|
46
|
+
"""Build a browser-to-Core WebRTC offer."""
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
"contract_version": CAMERA_MEDIA_CONTRACT_VERSION,
|
|
50
|
+
"message_type": "camera.session.offer",
|
|
51
|
+
"audience": "browser-core",
|
|
52
|
+
"type": "offer",
|
|
53
|
+
"sdp": sdp,
|
|
54
|
+
"include_audio": include_audio,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def build_camera_sidecar_session_open(
|
|
59
|
+
*,
|
|
60
|
+
source: dict[str, Any] | None = None,
|
|
61
|
+
offer: dict[str, Any] | None = None,
|
|
62
|
+
idle_timeout_seconds: int = 60,
|
|
63
|
+
) -> dict[str, Any]:
|
|
64
|
+
"""Build the private Core-to-sidecar session request."""
|
|
65
|
+
|
|
66
|
+
browser_offer = offer or build_camera_session_offer()
|
|
67
|
+
return {
|
|
68
|
+
"contract_version": CAMERA_MEDIA_CONTRACT_VERSION,
|
|
69
|
+
"message_type": "camera.sidecar.session.open",
|
|
70
|
+
"audience": "core-sidecar",
|
|
71
|
+
"source": source or build_camera_source(),
|
|
72
|
+
"offer": {
|
|
73
|
+
"type": browser_offer["type"],
|
|
74
|
+
"sdp": browser_offer["sdp"],
|
|
75
|
+
"include_audio": bool(browser_offer.get("include_audio", False)),
|
|
76
|
+
},
|
|
77
|
+
"idle_timeout_seconds": idle_timeout_seconds,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _field_names(value: object) -> set[str]:
|
|
82
|
+
if isinstance(value, dict):
|
|
83
|
+
names = {str(key).lower() for key in value}
|
|
84
|
+
for nested in value.values():
|
|
85
|
+
names.update(_field_names(nested))
|
|
86
|
+
return names
|
|
87
|
+
if isinstance(value, list):
|
|
88
|
+
names: set[str] = set()
|
|
89
|
+
for nested in value:
|
|
90
|
+
names.update(_field_names(nested))
|
|
91
|
+
return names
|
|
92
|
+
return set()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def assert_public_camera_message_redacted(payload: Any) -> dict[str, Any]:
|
|
96
|
+
"""Assert that a browser-facing camera message contains no source secrets."""
|
|
97
|
+
|
|
98
|
+
if not isinstance(payload, dict):
|
|
99
|
+
raise AssertionError( # noqa: TRY004 - assertion helper intentionally raises AssertionError
|
|
100
|
+
f"Expected camera message object, received {payload!r}."
|
|
101
|
+
)
|
|
102
|
+
if payload.get("contract_version") != CAMERA_MEDIA_CONTRACT_VERSION:
|
|
103
|
+
raise AssertionError("Expected camera.media.v1 contract_version.")
|
|
104
|
+
if payload.get("audience") not in PUBLIC_CAMERA_AUDIENCES:
|
|
105
|
+
raise AssertionError("Expected a browser-facing camera message.")
|
|
106
|
+
leaked = _field_names(payload) & CAMERA_SECRET_FIELDS
|
|
107
|
+
if leaked:
|
|
108
|
+
raise AssertionError(f"Browser-facing camera message leaked secret fields: {sorted(leaked)!r}.")
|
|
109
|
+
return payload
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def assert_camera_capability(
|
|
113
|
+
entities_payload: Any,
|
|
114
|
+
*,
|
|
115
|
+
capability_id: str = "camera_stream",
|
|
116
|
+
) -> dict[str, Any]:
|
|
117
|
+
"""Assert that an entities response advertises a usable camera capability."""
|
|
118
|
+
|
|
119
|
+
if not isinstance(entities_payload, dict):
|
|
120
|
+
raise AssertionError( # noqa: TRY004 - assertion helper intentionally raises AssertionError
|
|
121
|
+
"Expected /entities payload to be an object."
|
|
122
|
+
)
|
|
123
|
+
capabilities = entities_payload.get("capabilities")
|
|
124
|
+
if not isinstance(capabilities, dict):
|
|
125
|
+
raise AssertionError( # noqa: TRY004 - assertion helper intentionally raises AssertionError
|
|
126
|
+
"Expected /entities capabilities to be an object."
|
|
127
|
+
)
|
|
128
|
+
capability = capabilities.get(capability_id)
|
|
129
|
+
if not isinstance(capability, dict) or capability.get("kind") != "camera":
|
|
130
|
+
raise AssertionError(f"Expected {capability_id!r} to be a camera capability.")
|
|
131
|
+
camera = capability.get("camera")
|
|
132
|
+
if not isinstance(camera, dict):
|
|
133
|
+
raise AssertionError( # noqa: TRY004 - assertion helper intentionally raises AssertionError
|
|
134
|
+
f"Expected {capability_id!r} to declare camera media endpoints."
|
|
135
|
+
)
|
|
136
|
+
endpoint_names = {
|
|
137
|
+
"gateway_source_endpoint",
|
|
138
|
+
"snapshot_endpoint",
|
|
139
|
+
"stream_endpoint",
|
|
140
|
+
"webrtc_endpoint",
|
|
141
|
+
}
|
|
142
|
+
if not endpoint_names.intersection(camera):
|
|
143
|
+
raise AssertionError(f"Expected {capability_id!r} to declare at least one media endpoint.")
|
|
144
|
+
entities = entities_payload.get("entities")
|
|
145
|
+
if not isinstance(entities, list) or not any(
|
|
146
|
+
isinstance(entity, dict) and capability_id in (entity.get("capabilities") or [])
|
|
147
|
+
for entity in entities
|
|
148
|
+
):
|
|
149
|
+
raise AssertionError(f"Expected an entity to advertise {capability_id!r}.")
|
|
150
|
+
return capability
|
{piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_assertions.py
RENAMED
|
@@ -5,6 +5,7 @@ import pytest
|
|
|
5
5
|
from piphi_runtime_testkit_python.assertions import (
|
|
6
6
|
assert_entities_response,
|
|
7
7
|
assert_event_sent,
|
|
8
|
+
assert_state_refresh_response,
|
|
8
9
|
assert_telemetry_sent,
|
|
9
10
|
)
|
|
10
11
|
from piphi_runtime_testkit_python.mock_core import CapturedRequest, MockCoreServer
|
|
@@ -200,3 +201,32 @@ def test_assert_entities_response_rejects_invalid_entity_shape():
|
|
|
200
201
|
assert_entities_response(payload)
|
|
201
202
|
|
|
202
203
|
assert "capabilities" in str(excinfo.value)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def test_assert_state_refresh_response_requires_matching_upstream_receipt():
|
|
207
|
+
payload = {
|
|
208
|
+
"entries": {},
|
|
209
|
+
"refresh": {
|
|
210
|
+
"request_id": "refresh-1",
|
|
211
|
+
"performed": True,
|
|
212
|
+
"status": "refreshed",
|
|
213
|
+
"observed_at": "2026-10-08T15:00:00+00:00",
|
|
214
|
+
"source": "device",
|
|
215
|
+
},
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
assert assert_state_refresh_response(payload, request_id="refresh-1") is payload
|
|
219
|
+
|
|
220
|
+
with pytest.raises(AssertionError, match="current refresh request id"):
|
|
221
|
+
assert_state_refresh_response(payload, request_id="refresh-2")
|
|
222
|
+
|
|
223
|
+
cached = {
|
|
224
|
+
"entries": {},
|
|
225
|
+
"refresh": {
|
|
226
|
+
**payload["refresh"],
|
|
227
|
+
"performed": False,
|
|
228
|
+
"status": "unsupported",
|
|
229
|
+
},
|
|
230
|
+
}
|
|
231
|
+
with pytest.raises(AssertionError, match="upstream read"):
|
|
232
|
+
assert_state_refresh_response(cached, request_id="refresh-1")
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from piphi_runtime_testkit_python import (
|
|
6
|
+
assert_camera_capability,
|
|
7
|
+
assert_public_camera_message_redacted,
|
|
8
|
+
build_camera_session_offer,
|
|
9
|
+
build_camera_sidecar_session_open,
|
|
10
|
+
build_camera_source,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_camera_builders_keep_private_source_out_of_browser_offer() -> None:
|
|
15
|
+
source = build_camera_source(source_url="rtsps://user:secret@camera.local/live")
|
|
16
|
+
offer = build_camera_session_offer(include_audio=True)
|
|
17
|
+
sidecar_request = build_camera_sidecar_session_open(source=source, offer=offer)
|
|
18
|
+
|
|
19
|
+
assert source["audience"] == "core-sidecar"
|
|
20
|
+
assert source["source_protocol"] == "rtsps"
|
|
21
|
+
assert "source_url" not in offer
|
|
22
|
+
assert sidecar_request["source"] == source
|
|
23
|
+
assert sidecar_request["offer"]["include_audio"] is True
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_public_camera_redaction_assertion_rejects_secret_fields() -> None:
|
|
27
|
+
assert_public_camera_message_redacted({
|
|
28
|
+
"contract_version": "camera.media.v1",
|
|
29
|
+
"message_type": "camera.session.opened",
|
|
30
|
+
"audience": "browser-core",
|
|
31
|
+
"session_id": "opaque",
|
|
32
|
+
"type": "answer",
|
|
33
|
+
"sdp": "v=0",
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
with pytest.raises(AssertionError, match="source_url"):
|
|
37
|
+
assert_public_camera_message_redacted({
|
|
38
|
+
"contract_version": "camera.media.v1",
|
|
39
|
+
"message_type": "camera.session.opened",
|
|
40
|
+
"audience": "browser-core",
|
|
41
|
+
"source_url": "rtsp://user:secret@camera.local/live",
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_camera_capability_assertion_requires_endpoint_and_entity() -> None:
|
|
46
|
+
payload = {
|
|
47
|
+
"capabilities": {
|
|
48
|
+
"camera_stream": {
|
|
49
|
+
"kind": "camera",
|
|
50
|
+
"camera": {"gateway_source_endpoint": "/v1/cameras/{config_id}/source"},
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"entities": [
|
|
54
|
+
{
|
|
55
|
+
"id": "front-door",
|
|
56
|
+
"name": "Front door",
|
|
57
|
+
"capabilities": ["camera_stream"],
|
|
58
|
+
}
|
|
59
|
+
],
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
capability = assert_camera_capability(payload)
|
|
63
|
+
assert capability["kind"] == "camera"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_builders.py
RENAMED
|
File without changes
|
|
File without changes
|
{piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_mock_core.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{piphi_runtime_testkit_python-0.1.4 → piphi_runtime_testkit_python-0.2.0}/tests/test_runtime_flow.py
RENAMED
|
File without changes
|